Blender V5.0
GPU_vertex_buffer.hh
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#pragma once
12
13#include "BLI_math_base.h"
14#include "BLI_span.hh"
15#include "BLI_utildefines.h"
16#include "BLI_virtual_array.hh"
17
18#include "GPU_common.hh"
19#include "GPU_vertex_format.hh"
20
31
33
34
41
43 /* can be extended to support more types */
45 GPU_USAGE_STATIC = 1, /* do not keep data in memory */
47 GPU_USAGE_DEVICE_ONLY = 3, /* Do not do host->device data transfers. */
48
50 /* Flag for vertex buffers used for textures. Skips additional padding/compaction to ensure
51 * format matches the texture exactly. Can be masked with other properties, and is stripped
52 * during VertBuf::init. */
54};
55
57
58namespace blender::gpu {
59class VertBuf;
60} // namespace blender::gpu
61
63
66 GPUUsageType usage);
67
72
73namespace blender::gpu {
74
76 public:
78 {
80 }
81};
82
83using VertBufPtr = std::unique_ptr<gpu::VertBuf, gpu::VertBufDeleter>;
84
89class VertBuf {
90 public:
91 static size_t memory_usage;
92
100
101#ifndef NDEBUG
104#endif
105
106 protected:
108 uchar *data_ = nullptr;
109
112
113 private:
115 int handle_refcount_ = 1;
116
117 public:
118 VertBuf();
119 virtual ~VertBuf();
120
121 template<typename FormatT>
123 {
124 BLI_assert(size > 0);
125 VertBufPtr buf = VertBufPtr(GPU_vertbuf_create_with_format_ex(FormatT::format(), usage));
126 /* GPU formats needs to be aligned to 4 bytes. */
127 buf->allocate(ceil_to_multiple_u(size * sizeof(FormatT), 4) / sizeof(FormatT));
128 return buf;
129 }
130
131 template<typename T>
136
137 template<typename T> static VertBufPtr from_span(const Span<T> data)
138 {
139 BLI_assert(!data.is_empty());
142 /* GPU formats needs to be aligned to 4 bytes. */
143 buf->allocate(ceil_to_multiple_u(data.size_in_bytes(), 4) / sizeof(GenericVertexFormat<T>));
144 buf->data<T>().slice(0, data.size()).copy_from(data);
145 return buf;
146 }
147
148 template<typename T> static VertBufPtr from_varray(const VArray<T> &array)
149 {
150 BLI_assert(!array.is_empty());
153 /* GPU formats needs to be aligned to 4 bytes. */
154 buf->allocate(ceil_to_multiple_u(array.size() * sizeof(T), 4) /
155 sizeof(GenericVertexFormat<T>));
156 array.materialize(buf->data<T>().slice(0, array.size()));
157 return buf;
158 }
159
160 template<typename T> static VertBufPtr device_only(uint size)
161 {
162 BLI_assert(size > 0);
166 buf->allocate(size);
167 return buf;
168 }
169
170 void init(const GPUVertFormat &format, GPUUsageType usage);
171 void clear();
172
173 /* Data management. */
174 void allocate(uint vert_len);
175 void resize(uint vert_len);
176 void upload();
177 virtual void bind_as_ssbo(uint binding) = 0;
178 virtual void bind_as_texture(uint binding) = 0;
179
180 virtual void wrap_handle(uint64_t handle) = 0;
181
182 /* Size of the data allocated. */
183 size_t size_alloc_get() const
184 {
185 BLI_assert(this->format.packed);
186 return size_t(this->vertex_alloc) * this->format.stride;
187 }
188 /* Size of the data uploaded to the GPU. */
189 size_t size_used_get() const
190 {
191 BLI_assert(format.packed);
192 return size_t(this->vertex_len) * this->format.stride;
193 }
194
196 {
197 handle_refcount_++;
198 }
200 {
201 BLI_assert(handle_refcount_ > 0);
202 handle_refcount_--;
203 if (handle_refcount_ == 0) {
204 delete this;
205 }
206 }
207
209 {
210 return usage_;
211 }
212
217 template<typename T> MutableSpan<T> data()
218 {
219 return MutableSpan<uchar>(data_, this->size_alloc_get()).cast<T>();
220 }
221
222 virtual void update_sub(uint start, uint len, const void *data) = 0;
223 virtual void read(void *data) const = 0;
224
225 protected:
226 virtual void acquire_data() = 0;
227 virtual void resize_data() = 0;
228 virtual void release_data() = 0;
229 virtual void upload_data() = 0;
230};
231
232} // namespace blender::gpu
233
241
247
249 const GPUVertFormat &format,
251
253 const GPUVertFormat &format,
254 uint v_len);
255
257
258#define GPU_vertbuf_init_with_format(verts, format) \
259 GPU_vertbuf_init_with_format_ex(verts, format, GPU_USAGE_STATIC)
260
275
282void GPU_vertbuf_attr_set(blender::gpu::VertBuf *, uint a_idx, uint v_idx, const void *data);
283
285void GPU_vertbuf_vert_set(blender::gpu::VertBuf *verts, uint v_idx, const void *data);
286
290void GPU_vertbuf_attr_fill(blender::gpu::VertBuf *, uint a_idx, const void *data);
291
293 uint a_idx,
294 uint stride,
295 const void *data);
296
305 unsigned char *data;
306 unsigned char *data_init;
307#ifndef NDEBUG
308 /* Only for overflow check */
309 unsigned char *_data_end;
310#endif
311};
312
314{
315 unsigned char *data = a->data;
316 a->data += a->stride;
317#ifndef NDEBUG
318 BLI_assert(data < a->_data_end);
319#endif
320 return (void *)data;
321}
322
324{
325 return ((a->data - a->data_init) / a->stride);
326}
327
329
335
342
344
351
352/* Metrics */
354
355/* Macros */
356#define GPU_VERTBUF_DISCARD_SAFE(verts) \
357 do { \
358 if (verts != nullptr) { \
359 GPU_vertbuf_discard(verts); \
360 verts = nullptr; \
361 } \
362 } while (0)
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE uint ceil_to_multiple_u(uint a, uint b)
unsigned char uchar
unsigned int uint
#define ENUM_OPERATORS(_type, _max)
#define GPU_INLINE
Definition GPU_common.hh:18
void GPU_vertbuf_attr_get_raw_data(blender::gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
void GPU_vertbuf_attr_fill_stride(blender::gpu::VertBuf *, uint a_idx, uint stride, const void *data)
blender::gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
void GPU_vertbuf_init_build_on_device(blender::gpu::VertBuf &verts, const GPUVertFormat &format, uint v_len)
void GPU_vertbuf_vert_set(blender::gpu::VertBuf *verts, uint v_idx, const void *data)
void GPU_vertbuf_handle_ref_remove(blender::gpu::VertBuf *verts)
void GPU_vertbuf_use(blender::gpu::VertBuf *)
void GPU_vertbuf_data_resize(blender::gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_attr_fill(blender::gpu::VertBuf *, uint a_idx, const void *data)
blender::gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
void GPU_vertbuf_tag_dirty(blender::gpu::VertBuf *verts)
void GPU_vertbuf_handle_ref_add(blender::gpu::VertBuf *verts)
static blender::gpu::VertBuf * GPU_vertbuf_create_with_format(const GPUVertFormat &format)
void GPU_vertbuf_data_len_set(blender::gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_attr_set(blender::gpu::VertBuf *, uint a_idx, uint v_idx, const void *data)
GPUVertBufStatus
@ GPU_VERTBUF_INIT
@ GPU_VERTBUF_INVALID
@ GPU_VERTBUF_DATA_DIRTY
@ GPU_VERTBUF_DATA_UPLOADED
void GPU_vertbuf_wrap_handle(blender::gpu::VertBuf *verts, uint64_t handle)
void GPU_vertbuf_read(const blender::gpu::VertBuf *verts, void *data)
uint GPU_vertbuf_get_memory_usage()
GPUVertBufStatus GPU_vertbuf_get_status(const blender::gpu::VertBuf *verts)
blender::gpu::VertBuf * GPU_vertbuf_calloc()
void GPU_vertbuf_clear(blender::gpu::VertBuf *verts)
const GPUVertFormat * GPU_vertbuf_get_format(const blender::gpu::VertBuf *verts)
GPU_INLINE uint GPU_vertbuf_raw_used(const GPUVertBufRaw *a)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_bind_as_ssbo(blender::gpu::VertBuf *verts, int binding)
void GPU_vertbuf_update_sub(blender::gpu::VertBuf *verts, uint start, uint len, const void *data)
void GPU_vertbuf_init_with_format_ex(blender::gpu::VertBuf &verts, const GPUVertFormat &format, GPUUsageType)
void GPU_vertbuf_bind_as_texture(blender::gpu::VertBuf *verts, int binding)
uint GPU_vertbuf_get_vertex_len(const blender::gpu::VertBuf *verts)
void GPU_vertbuf_discard(blender::gpu::VertBuf *)
@ GPU_USAGE_STATIC
@ GPU_USAGE_STREAM
@ GPU_USAGE_DYNAMIC
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
@ GPU_USAGE_DEVICE_ONLY
uint GPU_vertbuf_get_vertex_alloc(const blender::gpu::VertBuf *verts)
BMesh const char void * data
void init()
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
size_t size() const
constexpr MutableSpan< NewT > cast() const
Definition BLI_span.hh:749
static VertBufPtr device_only(uint size)
virtual void read(void *data) const =0
virtual void wrap_handle(uint64_t handle)=0
static VertBufPtr from_varray(const VArray< T > &array)
static VertBufPtr from_size(const int size, GPUUsageType usage=GPU_USAGE_STATIC)
virtual void bind_as_ssbo(uint binding)=0
static VertBufPtr from_span(const Span< T > data)
void resize(uint vert_len)
virtual void upload_data()=0
static VertBufPtr from_size_with_format(const int size, GPUUsageType usage=GPU_USAGE_STATIC)
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
static float verts[][3]
format
#define T
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
unsigned char * data_init
unsigned char * data
unsigned char * _data_end
uint len