Blender V5.3
gpu_vertex_buffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 by Mike Erwin. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include "gpu_backend.hh"
15
16#include "GPU_vertex_buffer.hh"
17
18#include "gpu_context_private.hh" /* TODO: remove. */
19
20#include <cstring>
21
22namespace blender {
23
24/* -------------------------------------------------------------------- */
27
28namespace gpu {
29
30size_t VertBuf::memory_usage = 0;
31
33{
34 /* Needed by some code check. */
35 format.attr_len = 0;
36}
37
39{
40 /* Should already have been cleared. */
42}
43
45{
46#if 0 /* Disabled until Grease pencil. Comply to this. */
48 BLI_assert_msg(format.attr_len == 1,
49 "Only single attribute format are supported for buffer textures");
50 }
51#endif
52 /* Strip extended usage flags. */
54#ifndef NDEBUG
55 /* Store extended usage. */
56 extended_usage_ = usage;
57#endif
59 GPU_vertformat_copy(&this->format, format);
60 if (!this->format.packed) {
61 VertexFormat_pack(&this->format);
62 }
64}
65
67{
68 this->release_data();
70}
71
72void VertBuf::allocate(uint vert_len)
73{
75 /* Catch any unnecessary usage. */
76 BLI_assert(vertex_alloc != vert_len || data_ == nullptr);
77 vertex_len = vertex_alloc = vert_len;
78
79 this->acquire_data();
80
82}
83
84void VertBuf::resize(uint vert_len)
85{
86 /* Catch any unnecessary usage. */
87 BLI_assert(vertex_alloc != vert_len);
88 vertex_len = vertex_alloc = vert_len;
89
90 this->resize_data();
91
93}
94
96{
97 this->upload_data();
98}
99
100} // namespace gpu
101
103
104/* -------------------------------------------------------------------- */
107
108using namespace blender::gpu;
109
110/* -------- Creation & deletion -------- */
111
116
118{
119 VertBuf *verts = GPU_vertbuf_calloc();
120 verts->init(format, usage);
121 return verts;
122}
123
125 const GPUVertFormat &format,
126 GPUUsageType usage)
127{
128 verts.init(format, usage);
129}
130
132{
134 GPU_vertbuf_data_alloc(verts, v_len);
135}
136
138{
140 GPU_vertbuf_data_alloc(*verts, v_len);
141 return verts;
142}
143
144void GPU_vertbuf_read(const VertBuf *verts, void *data)
145{
146 verts->read(data);
147}
148
150{
151 verts->clear();
152}
153
155{
156 verts->clear();
157 verts->reference_remove();
158}
159
161{
162 verts->reference_add();
163}
164
166{
167 verts->reference_remove();
168}
169
170/* -------- Data update -------- */
171
173{
174 verts.allocate(v_len);
175}
176
178{
179 verts.resize(v_len);
180}
181
183{
184 BLI_assert(verts.data<uchar>().data() != nullptr); /* Only for dynamic data. */
185 BLI_assert(v_len <= verts.vertex_alloc);
186 verts.vertex_len = v_len;
187}
188
189void GPU_vertbuf_attr_set(VertBuf *verts, uint a_idx, uint v_idx, const void *data)
190{
192 const GPUVertFormat *format = &verts->format;
193 const GPUVertAttr *a = &format->attrs[a_idx];
194 BLI_assert(v_idx < verts->vertex_alloc);
195 BLI_assert(a_idx < format->attr_len);
196 BLI_assert(verts->data<uchar>().data() != nullptr);
198 memcpy(verts->data<uchar>().data() + a->offset + v_idx * format->stride, data, a->type.size());
199}
200
201void GPU_vertbuf_attr_fill(VertBuf *verts, uint a_idx, const void *data)
202{
203 const GPUVertFormat *format = &verts->format;
204 BLI_assert(a_idx < format->attr_len);
205 const GPUVertAttr *a = &format->attrs[a_idx];
206 const uint stride = a->type.size(); /* tightly packed input data */
208 GPU_vertbuf_attr_fill_stride(verts, a_idx, stride, data);
209}
210
211void GPU_vertbuf_vert_set(VertBuf *verts, uint v_idx, const void *data)
212{
214 const GPUVertFormat *format = &verts->format;
215 BLI_assert(v_idx < verts->vertex_alloc);
216 BLI_assert(verts->data<uchar>().data() != nullptr);
218 memcpy(verts->data<uchar>().data() + v_idx * format->stride, data, format->stride);
219}
220
221void GPU_vertbuf_attr_fill_stride(VertBuf *verts, uint a_idx, uint stride, const void *data)
222{
224 const GPUVertFormat *format = &verts->format;
225 const GPUVertAttr *a = &format->attrs[a_idx];
226 BLI_assert(a_idx < format->attr_len);
227 BLI_assert(verts->data<uchar>().data() != nullptr);
229 const uint vertex_len = verts->vertex_len;
230
231 if (format->attr_len == 1 && stride == format->stride) {
232 /* we can copy it all at once */
233 memcpy(verts->data<uchar>().data(), data, vertex_len * a->type.size());
234 }
235 else {
236 /* we must copy it per vertex */
237 for (uint v = 0; v < vertex_len; v++) {
238 memcpy(verts->data<uchar>().data() + a->offset + v * format->stride,
239 static_cast<const uchar *>(data) + v * stride,
240 a->type.size());
241 }
242 }
243}
244
246{
247 const GPUVertFormat *format = &verts->format;
248 const GPUVertAttr *a = &format->attrs[a_idx];
249 BLI_assert(a_idx < format->attr_len);
250 BLI_assert(verts->data<uchar>().data() != nullptr);
251
254 access->size = a->type.size();
255 access->stride = format->stride;
256 access->data = static_cast<uchar *>(verts->data<uchar>().data()) + a->offset;
257 access->data_init = access->data;
258#ifndef NDEBUG
259 access->_data_end = access->data_init + size_t(verts->vertex_alloc * format->stride);
260#endif
261}
262
263/* -------- Getters -------- */
264
266{
267 return &verts->format;
268}
269
271{
272 return verts->vertex_alloc;
273}
274
276{
277 return verts->vertex_len;
278}
279
281{
282 return verts->flag;
283}
284
286{
288}
289
294
296{
297 verts->upload();
298}
299
301{
302 verts->wrap_handle(handle);
303}
304
305void GPU_vertbuf_bind_as_ssbo(VertBuf *verts, int binding)
306{
307 verts->bind_as_ssbo(binding);
308}
309
310void GPU_vertbuf_bind_as_texture(VertBuf *verts, int binding)
311{
312 verts->bind_as_texture(binding);
313}
314
315void GPU_vertbuf_update_sub(VertBuf *verts, uint start, uint len, const void *data)
316{
317 verts->update_sub(start, len, data);
318}
319
321
322} // namespace blender
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
static GPUBackend * get()
virtual VertBuf * vertbuf_alloc()=0
virtual void read(void *data) const =0
virtual void wrap_handle(uint64_t handle)=0
void init(const GPUVertFormat &format, GPUUsageType usage)
virtual void bind_as_ssbo(uint binding)=0
void resize(uint vert_len)
virtual void upload_data()=0
void allocate(uint vert_len)
GPUUsageType get_usage_type() const
virtual void resize_data()=0
virtual void bind_as_texture(uint binding)=0
virtual void release_data()=0
MutableSpan< T > data()
virtual void acquire_data()=0
virtual void update_sub(uint start, uint len, const void *data)=0
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_vertbuf_tag_dirty(gpu::VertBuf *verts)
unsigned __int64 uint64_t
Definition BKE_text.h:91
void GPU_vertbuf_bind_as_texture(gpu::VertBuf *verts, int binding)
void GPU_vertbuf_wrap_handle(gpu::VertBuf *verts, uint64_t handle)
void GPU_vertbuf_init_with_format_ex(gpu::VertBuf &verts, const GPUVertFormat &format, GPUUsageType)
unsigned int uint
Definition BKE_text.h:55
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
void GPU_vertbuf_handle_ref_remove(gpu::VertBuf *verts)
uint GPU_vertbuf_get_memory_usage()
void GPU_vertbuf_read(const gpu::VertBuf *verts, void *data)
void GPU_vertbuf_use(gpu::VertBuf *)
ATTR_WARN_UNUSED_RESULT const BMVert * v
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
GPUVertBufStatus GPU_vertbuf_get_status(const gpu::VertBuf *verts)
void GPU_vertbuf_bind_as_ssbo(gpu::VertBuf *verts, int binding)
void GPU_vertbuf_attr_fill_stride(gpu::VertBuf *, uint a_idx, uint stride, const void *data)
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
void GPU_vertbuf_attr_set(gpu::VertBuf *, uint a_idx, uint v_idx, const void *data)
gpu::VertBuf * GPU_vertbuf_calloc()
void GPU_vertbuf_update_sub(gpu::VertBuf *verts, uint start, uint len, const void *data)
void GPU_vertbuf_handle_ref_add(gpu::VertBuf *verts)
void GPU_vertbuf_clear(gpu::VertBuf *verts)
void VertexFormat_pack(GPUVertFormat *format)
void GPU_vertbuf_init_build_on_device(gpu::VertBuf &verts, const GPUVertFormat &format, uint v_len)
@ GPU_VERTBUF_DATA_UPLOADED
gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
void GPU_vertbuf_attr_get_raw_data(gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
void GPU_vertbuf_vert_set(gpu::VertBuf *verts, uint v_idx, const void *data)
void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat &src)
void GPU_vertbuf_attr_fill(gpu::VertBuf *, uint a_idx, const void *data)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_data_resize(gpu::VertBuf &verts, uint v_len)
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
uint GPU_vertbuf_get_vertex_alloc(const gpu::VertBuf *verts)
void GPU_vertbuf_discard(gpu::VertBuf *)
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
void GPU_vertbuf_data_len_set(gpu::VertBuf &verts, uint v_len)
struct blender::GPUVertAttr::Type type