Blender V5.3
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_array_utils.hh"
14#include "BLI_assert.hh"
15#include "BLI_enum_flags.hh"
16#include "BLI_math_base_c.hh"
17#include "BLI_span.hh"
18#include "BLI_virtual_array.hh"
19
20#include "GPU_common.hh"
21#include "GPU_vertex_format.hh"
22
23namespace blender {
24
35
37
38
45
47 /* can be extended to support more types */
49 GPU_USAGE_STATIC = 1, /* do not keep data in memory */
51 GPU_USAGE_DEVICE_ONLY = 3, /* Do not do host->device data transfers. */
52
54 /* Flag for vertex buffers used for textures. Skips additional padding/compaction to ensure
55 * format matches the texture exactly. Can be masked with other properties, and is stripped
56 * during VertBuf::init. */
58};
59
61
62namespace gpu {
63class VertBuf;
64} // namespace gpu
65
66void GPU_vertbuf_discard(gpu::VertBuf *);
67
68gpu::VertBuf *GPU_vertbuf_calloc();
69gpu::VertBuf *GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage);
70
75
76namespace gpu {
77
79 public:
81 {
83 }
84};
85
86using VertBufPtr = std::unique_ptr<gpu::VertBuf, gpu::VertBufDeleter>;
87
92class VertBuf {
93 public:
94 static size_t memory_usage;
95
103
104#ifndef NDEBUG
107#endif
108
109 protected:
111 uchar *data_ = nullptr;
112
115
116 private:
118 int handle_refcount_ = 1;
119
120 public:
121 VertBuf();
122 virtual ~VertBuf();
123
124 template<typename FormatT>
126 {
127 BLI_assert(size > 0);
128 VertBufPtr buf = VertBufPtr(GPU_vertbuf_create_with_format_ex(FormatT::format(), usage));
129 /* GPU formats needs to be aligned to 4 bytes. */
130 buf->allocate(ceil_to_multiple_u(size * sizeof(FormatT), 4) / sizeof(FormatT));
131 return buf;
132 }
133
134 template<typename T>
135 static VertBufPtr from_size(const int size, GPUUsageType usage = GPU_USAGE_STATIC)
136 {
138 }
139
140 template<typename T> static VertBufPtr from_span(const Span<T> data)
141 {
142 BLI_assert(!data.is_empty());
145 /* GPU formats needs to be aligned to 4 bytes. */
146 buf->allocate(ceil_to_multiple_u(data.size_in_bytes(), 4) / sizeof(GenericVertexFormat<T>));
147 array_utils::copy(data, buf->data<T>().slice(0, data.size()));
148 return buf;
149 }
150
151 template<typename T> static VertBufPtr from_varray(const VArray<T> &array)
152 {
153 BLI_assert(!array.is_empty());
156 /* GPU formats needs to be aligned to 4 bytes. */
157 buf->allocate(ceil_to_multiple_u(array.size() * sizeof(T), 4) /
158 sizeof(GenericVertexFormat<T>));
159 array_utils::copy(array, buf->data<T>().slice(0, array.size()));
160 return buf;
161 }
162
163 template<typename T> static VertBufPtr device_only(uint size)
164 {
165 BLI_assert(size > 0);
169 buf->allocate(size);
170 return buf;
171 }
172
173 void init(const GPUVertFormat &format, GPUUsageType usage);
174 void clear();
175
176 /* Data management. */
177 void allocate(uint vert_len);
178 void resize(uint vert_len);
179 void upload();
180 virtual void bind_as_ssbo(uint binding) = 0;
181 virtual void bind_as_texture(uint binding) = 0;
182
183 virtual void wrap_handle(uint64_t handle) = 0;
184
185 /* Size of the data allocated. */
186 size_t size_alloc_get() const
187 {
188 BLI_assert(this->format.packed);
189 return size_t(this->vertex_alloc) * this->format.stride;
190 }
191 /* Size of the data uploaded to the GPU. */
192 size_t size_used_get() const
193 {
194 BLI_assert(format.packed);
195 return size_t(this->vertex_len) * this->format.stride;
196 }
197
199 {
200 handle_refcount_++;
201 }
203 {
204 BLI_assert(handle_refcount_ > 0);
205 handle_refcount_--;
206 if (handle_refcount_ == 0) {
207 delete this;
208 }
209 }
210
212 {
213 return usage_;
214 }
215
220 template<typename T> MutableSpan<T> data()
221 {
222 return MutableSpan<uchar>(data_, this->size_alloc_get()).cast<T>();
223 }
224
225 virtual void update_sub(uint start, uint len, const void *data) = 0;
240 virtual void copy_sub(VertBuf &source_buf,
241 uint source_first_vertex,
242 uint dest_first_vertex,
243 uint vertex_len) = 0;
244 virtual void read(void *data) const = 0;
245
246 protected:
247 virtual void acquire_data() = 0;
248 virtual void resize_data() = 0;
249 virtual void release_data() = 0;
250 virtual void upload_data() = 0;
251};
252
253} // namespace gpu
254
259void GPU_vertbuf_read(const gpu::VertBuf *verts, void *data);
262
268
270 const GPUVertFormat &format,
272
274 const GPUVertFormat &format,
275 uint v_len);
276
278
279#define GPU_vertbuf_init_with_format(verts, format) \
280 GPU_vertbuf_init_with_format_ex(verts, format, GPU_USAGE_STATIC)
281
285void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len);
289void GPU_vertbuf_data_resize(gpu::VertBuf &verts, uint v_len);
295void GPU_vertbuf_data_len_set(gpu::VertBuf &verts, uint v_len);
296
303void GPU_vertbuf_attr_set(gpu::VertBuf *, uint a_idx, uint v_idx, const void *data);
304
306void GPU_vertbuf_vert_set(gpu::VertBuf *verts, uint v_idx, const void *data);
307
311void GPU_vertbuf_attr_fill(gpu::VertBuf *, uint a_idx, const void *data);
312
313void GPU_vertbuf_attr_fill_stride(gpu::VertBuf *, uint a_idx, uint stride, const void *data);
314
323 unsigned char *data;
324 unsigned char *data_init;
325#ifndef NDEBUG
326 /* Only for overflow check */
327 unsigned char *_data_end;
328#endif
329};
330
332{
333 unsigned char *data = a->data;
334 a->data += a->stride;
335#ifndef NDEBUG
336 BLI_assert(data < a->_data_end);
337#endif
338 return static_cast<void *>(data);
339}
340
342{
343 return ((a->data - a->data_init) / a->stride);
344}
345
346void GPU_vertbuf_attr_get_raw_data(gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access);
347
348const GPUVertFormat *GPU_vertbuf_get_format(const gpu::VertBuf *verts);
349uint GPU_vertbuf_get_vertex_alloc(const gpu::VertBuf *verts);
350uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts);
351GPUVertBufStatus GPU_vertbuf_get_status(const gpu::VertBuf *verts);
352void GPU_vertbuf_tag_dirty(gpu::VertBuf *verts);
353
357void GPU_vertbuf_use(gpu::VertBuf *);
358void GPU_vertbuf_bind_as_ssbo(gpu::VertBuf *verts, int binding);
359void GPU_vertbuf_bind_as_texture(gpu::VertBuf *verts, int binding);
360
361void GPU_vertbuf_wrap_handle(gpu::VertBuf *verts, uint64_t handle);
362
368void GPU_vertbuf_update_sub(gpu::VertBuf *verts, uint start, uint len, const void *data);
369
370/* Metrics */
372
373/* Macros */
374#define GPU_VERTBUF_DISCARD_SAFE(verts) \
375 do { \
376 if (verts != nullptr) { \
377 GPU_vertbuf_discard(verts); \
378 verts = nullptr; \
379 } \
380 } while (0)
381
382} // namespace blender
#define ENUM_OPERATORS(_enum_type)
#define GPU_INLINE
Definition GPU_common.hh:18
unsigned long long int uint64_t
size_t size() const
constexpr MutableSpan()=default
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)
virtual void copy_sub(VertBuf &source_buf, uint source_first_vertex, uint dest_first_vertex, uint vertex_len)=0
Copy from part of source vertex buffer into the vertex buffer.
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
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define T
ENUM_OPERATORS(Layer::Flags)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
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
GPU_INLINE uint GPU_vertbuf_raw_used(const GPUVertBufRaw *a)
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
static gpu::VertBuf * GPU_vertbuf_create_with_format(const GPUVertFormat &format)
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 *)
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
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 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_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)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
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)