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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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.
*
* Contributor(s): Blender Foundation, Clément Foucault
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/intern/gpu_vertex_buffer.c
* \ingroup gpu
*
* GPU vertex buffer
*/
#include "MEM_guardedalloc.h"
#include "GPU_vertex_buffer.h"
#include "gpu_context_private.h"
#include "gpu_vertex_format_private.h"
#include <stdlib.h>
#include <string.h>
#define KEEP_SINGLE_COPY 1
static uint vbo_memory_usage;
static GLenum convert_usage_type_to_gl(GPUUsageType type)
{
static const GLenum table[] = {
[GPU_USAGE_STREAM] = GL_STREAM_DRAW,
[GPU_USAGE_STATIC] = GL_STATIC_DRAW,
[GPU_USAGE_DYNAMIC] = GL_DYNAMIC_DRAW
};
return table[type];
}
GPUVertBuf *GPU_vertbuf_create(GPUUsageType usage)
{
GPUVertBuf *verts = MEM_mallocN(sizeof(GPUVertBuf), "GPUVertBuf");
GPU_vertbuf_init(verts, usage);
return verts;
}
GPUVertBuf *GPU_vertbuf_create_with_format_ex(const GPUVertFormat *format, GPUUsageType usage)
{
GPUVertBuf *verts = GPU_vertbuf_create(usage);
GPU_vertformat_copy(&verts->format, format);
if (!format->packed) {
VertexFormat_pack(&verts->format);
}
return verts;
/* this function might seem redundant, but there is potential for memory savings here... */
/* TODO: implement those memory savings */
}
void GPU_vertbuf_init(GPUVertBuf *verts, GPUUsageType usage)
{
memset(verts, 0, sizeof(GPUVertBuf));
verts->usage = usage;
verts->dirty = true;
}
void GPU_vertbuf_init_with_format_ex(GPUVertBuf *verts, const GPUVertFormat *format, GPUUsageType usage)
{
GPU_vertbuf_init(verts, usage);
GPU_vertformat_copy(&verts->format, format);
if (!format->packed) {
VertexFormat_pack(&verts->format);
}
}
void GPU_vertbuf_discard(GPUVertBuf *verts)
{
if (verts->vbo_id) {
GPU_buf_free(verts->vbo_id);
#if VRAM_USAGE
vbo_memory_usage -= GPU_vertbuf_size_get(verts);
#endif
}
if (verts->data) {
MEM_freeN(verts->data);
}
MEM_freeN(verts);
}
uint GPU_vertbuf_size_get(const GPUVertBuf *verts)
{
return vertex_buffer_size(&verts->format, verts->vertex_len);
}
/* create a new allocation, discarding any existing data */
void GPU_vertbuf_data_alloc(GPUVertBuf *verts, uint v_len)
{
GPUVertFormat *format = &verts->format;
if (!format->packed) {
VertexFormat_pack(format);
}
#if TRUST_NO_ONE
/* catch any unnecessary use */
assert(verts->vertex_alloc != v_len || verts->data == NULL);
#endif
/* only create the buffer the 1st time */
if (verts->vbo_id == 0) {
verts->vbo_id = GPU_buf_alloc();
}
/* discard previous data if any */
if (verts->data) {
MEM_freeN(verts->data);
}
#if VRAM_USAGE
uint new_size = vertex_buffer_size(&verts->format, v_len);
vbo_memory_usage += new_size - GPU_vertbuf_size_get(verts);
#endif
verts->dirty = true;
verts->vertex_len = verts->vertex_alloc = v_len;
verts->data = MEM_mallocN(sizeof(GLubyte) * GPU_vertbuf_size_get(verts), "GPUVertBuf data");
}
/* resize buffer keeping existing data */
void GPU_vertbuf_data_resize(GPUVertBuf *verts, uint v_len)
{
#if TRUST_NO_ONE
assert(verts->data != NULL);
assert(verts->vertex_alloc != v_len);
#endif
#if VRAM_USAGE
uint new_size = vertex_buffer_size(&verts->format, v_len);
vbo_memory_usage += new_size - GPU_vertbuf_size_get(verts);
#endif
verts->dirty = true;
verts->vertex_len = verts->vertex_alloc = v_len;
verts->data = MEM_reallocN(verts->data, sizeof(GLubyte) * GPU_vertbuf_size_get(verts));
}
/* Set vertex count but does not change allocation.
* Only this many verts will be uploaded to the GPU and rendered.
* This is useful for streaming data. */
void GPU_vertbuf_vertex_count_set(GPUVertBuf *verts, uint v_len)
{
#if TRUST_NO_ONE
assert(verts->data != NULL); /* only for dynamic data */
assert(v_len <= verts->vertex_alloc);
#endif
#if VRAM_USAGE
uint new_size = vertex_buffer_size(&verts->format, v_len);
vbo_memory_usage += new_size - GPU_vertbuf_size_get(verts);
#endif
verts->vertex_len = v_len;
}
void GPU_vertbuf_attr_set(GPUVertBuf *verts, uint a_idx, uint v_idx, const void *data)
{
const GPUVertFormat *format = &verts->format;
const GPUVertAttr *a = format->attribs + a_idx;
#if TRUST_NO_ONE
assert(a_idx < format->attr_len);
assert(v_idx < verts->vertex_alloc);
assert(verts->data != NULL);
#endif
verts->dirty = true;
memcpy((GLubyte *)verts->data + a->offset + v_idx * format->stride, data, a->sz);
}
void GPU_vertbuf_attr_fill(GPUVertBuf *verts, uint a_idx, const void *data)
{
const GPUVertFormat *format = &verts->format;
const GPUVertAttr *a = format->attribs + a_idx;
#if TRUST_NO_ONE
assert(a_idx < format->attr_len);
#endif
const uint stride = a->sz; /* tightly packed input data */
GPU_vertbuf_attr_fill_stride(verts, a_idx, stride, data);
}
void GPU_vertbuf_attr_fill_stride(GPUVertBuf *verts, uint a_idx, uint stride, const void *data)
{
const GPUVertFormat *format = &verts->format;
const GPUVertAttr *a = format->attribs + a_idx;
#if TRUST_NO_ONE
assert(a_idx < format->attr_len);
assert(verts->data != NULL);
#endif
verts->dirty = true;
const uint vertex_len = verts->vertex_len;
if (format->attr_len == 1 && stride == format->stride) {
/* we can copy it all at once */
memcpy(verts->data, data, vertex_len * a->sz);
}
else {
/* we must copy it per vertex */
for (uint v = 0; v < vertex_len; ++v) {
memcpy((GLubyte *)verts->data + a->offset + v * format->stride, (const GLubyte *)data + v * stride, a->sz);
}
}
}
void GPU_vertbuf_attr_get_raw_data(GPUVertBuf *verts, uint a_idx, GPUVertBufRaw *access)
{
const GPUVertFormat *format = &verts->format;
const GPUVertAttr *a = format->attribs + a_idx;
#if TRUST_NO_ONE
assert(a_idx < format->attr_len);
assert(verts->data != NULL);
#endif
verts->dirty = true;
access->size = a->sz;
access->stride = format->stride;
access->data = (GLubyte *)verts->data + a->offset;
access->data_init = access->data;
#if TRUST_NO_ONE
access->_data_end = access->data_init + (size_t)(verts->vertex_alloc * format->stride);
#endif
}
static void VertBuffer_upload_data(GPUVertBuf *verts)
{
uint buffer_sz = GPU_vertbuf_size_get(verts);
/* orphan the vbo to avoid sync */
glBufferData(GL_ARRAY_BUFFER, buffer_sz, NULL, convert_usage_type_to_gl(verts->usage));
/* upload data */
glBufferSubData(GL_ARRAY_BUFFER, 0, buffer_sz, verts->data);
if (verts->usage == GPU_USAGE_STATIC) {
MEM_freeN(verts->data);
verts->data = NULL;
}
verts->dirty = false;
}
void GPU_vertbuf_use(GPUVertBuf *verts)
{
glBindBuffer(GL_ARRAY_BUFFER, verts->vbo_id);
if (verts->dirty) {
VertBuffer_upload_data(verts);
}
}
uint GPU_vertbuf_get_memory_usage(void)
{
return vbo_memory_usage;
}
|