Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GPU_vertex_buffer.h « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 637acdec85e9c1e32b7844471c33c109e2c286b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2016 by Mike Erwin.
 * All rights reserved.
 */

/** \file \ingroup gpu
 *
 * GPU vertex buffer
 */

#ifndef __GPU_VERTEX_BUFFER_H__
#define __GPU_VERTEX_BUFFER_H__

#include "GPU_vertex_format.h"

#define VRAM_USAGE 1
/**
 * How to create a #GPUVertBuf:
 * 1) verts = GPU_vertbuf_create() or GPU_vertbuf_init(verts)
 * 2) GPU_vertformat_attr_add(verts->format, ...)
 * 3) GPU_vertbuf_data_alloc(verts, vertex_len) <-- finalizes/packs vertex format
 * 4) GPU_vertbuf_attr_fill(verts, pos, application_pos_buffer)
 */

/* Is GPUVertBuf always used as part of a GPUBatch? */

typedef enum {
	/* can be extended to support more types */
	GPU_USAGE_STREAM,
	GPU_USAGE_STATIC, /* do not keep data in memory */
	GPU_USAGE_DYNAMIC
} GPUUsageType;

typedef struct GPUVertBuf {
	GPUVertFormat format;
	uint vertex_len;    /* number of verts we want to draw */
	uint vertex_alloc;  /* number of verts data */
	bool dirty;
	unsigned char *data; /* NULL indicates data in VRAM (unmapped) */
	uint32_t vbo_id; /* 0 indicates not yet allocated */
	GPUUsageType usage; /* usage hint for GL optimisation */
} GPUVertBuf;

GPUVertBuf *GPU_vertbuf_create(GPUUsageType);
GPUVertBuf *GPU_vertbuf_create_with_format_ex(const GPUVertFormat *, GPUUsageType);

#define GPU_vertbuf_create_with_format(format) \
	GPU_vertbuf_create_with_format_ex(format, GPU_USAGE_STATIC)

void GPU_vertbuf_discard(GPUVertBuf *);

void GPU_vertbuf_init(GPUVertBuf *, GPUUsageType);
void GPU_vertbuf_init_with_format_ex(GPUVertBuf *, const GPUVertFormat *, GPUUsageType);

#define GPU_vertbuf_init_with_format(verts, format) \
	GPU_vertbuf_init_with_format_ex(verts, format, GPU_USAGE_STATIC)

uint GPU_vertbuf_size_get(const GPUVertBuf *);
void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len);
void GPU_vertbuf_data_resize(GPUVertBuf *, uint v_len);
void GPU_vertbuf_data_len_set(GPUVertBuf *, uint v_len);

/* The most important #set_attr variant is the untyped one. Get it right first.
 * It takes a void* so the app developer is responsible for matching their app data types
 * to the vertex attribute's type and component count. They're in control of both, so this
 * should not be a problem. */

void GPU_vertbuf_attr_set(GPUVertBuf *, uint a_idx, uint v_idx, const void *data);
void GPU_vertbuf_attr_fill(GPUVertBuf *, uint a_idx, const void *data); /* tightly packed, non interleaved input data */
void GPU_vertbuf_attr_fill_stride(GPUVertBuf *, uint a_idx, uint stride, const void *data);

/* For low level access only */
typedef struct GPUVertBufRaw {
	uint size;
	uint stride;
	unsigned char *data;
	unsigned char *data_init;
#if TRUST_NO_ONE
	/* Only for overflow check */
	unsigned char *_data_end;
#endif
} GPUVertBufRaw;

GPU_INLINE void *GPU_vertbuf_raw_step(GPUVertBufRaw *a)
{
	unsigned char *data = a->data;
	a->data += a->stride;
#if TRUST_NO_ONE
	assert(data < a->_data_end);
#endif
	return (void *)data;
}

GPU_INLINE uint GPU_vertbuf_raw_used(GPUVertBufRaw *a)
{
	return ((a->data - a->data_init) / a->stride);
}

void GPU_vertbuf_attr_get_raw_data(GPUVertBuf *, uint a_idx, GPUVertBufRaw *access);

void GPU_vertbuf_use(GPUVertBuf *);

/* Metrics */
uint GPU_vertbuf_get_memory_usage(void);

/* Macros */
#define GPU_VERTBUF_DISCARD_SAFE(verts) do { \
	if (verts != NULL) { \
		GPU_vertbuf_discard(verts); \
		verts = NULL; \
	} \
} while (0)

#endif /* __GPU_VERTEX_BUFFER_H__ */