Blender V4.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
11#pragma once
12
13#include "BLI_span.hh"
14#include "BLI_utildefines.h"
15
16#include "GPU_vertex_format.hh"
17
28
30
31
40 /* can be extended to support more types */
42 GPU_USAGE_STATIC = 1, /* do not keep data in memory */
44 GPU_USAGE_DEVICE_ONLY = 3, /* Do not do host->device data transfers. */
45
47 /* Flag for vertex buffers used for textures. Skips additional padding/compaction to ensure
48 * format matches the texture exactly. Can be masked with other properties, and is stripped
49 * during VertBuf::init. */
51};
52
54
55namespace blender::gpu {
56
61class VertBuf {
62 public:
63 static size_t memory_usage;
64
72
73#ifndef NDEBUG
76#endif
77
78 protected:
80 uchar *data_ = nullptr;
81
84
85 private:
87 int handle_refcount_ = 1;
88
89 public:
90 VertBuf();
91 virtual ~VertBuf();
92
93 void init(const GPUVertFormat &format, GPUUsageType usage);
94 void clear();
95
96 /* Data management. */
97 void allocate(uint vert_len);
98 void resize(uint vert_len);
99 void upload();
100 virtual void bind_as_ssbo(uint binding) = 0;
101 virtual void bind_as_texture(uint binding) = 0;
102
103 virtual void wrap_handle(uint64_t handle) = 0;
104
106
107 /* Size of the data allocated. */
108 size_t size_alloc_get() const
109 {
110 BLI_assert(this->format.packed);
111 return size_t(this->vertex_alloc) * this->format.stride;
112 }
113 /* Size of the data uploaded to the GPU. */
114 size_t size_used_get() const
115 {
116 BLI_assert(format.packed);
117 return size_t(this->vertex_len) * this->format.stride;
118 }
119
121 {
122 handle_refcount_++;
123 }
125 {
126 BLI_assert(handle_refcount_ > 0);
127 handle_refcount_--;
128 if (handle_refcount_ == 0) {
129 delete this;
130 }
131 }
132
134 {
135 return usage_;
136 }
137
142 template<typename T> MutableSpan<T> data()
143 {
144 return MutableSpan<uchar>(data_, this->size_alloc_get()).cast<T>();
145 }
146
147 virtual void update_sub(uint start, uint len, const void *data) = 0;
148 virtual void read(void *data) const = 0;
149
150 protected:
151 virtual void acquire_data() = 0;
152 virtual void resize_data() = 0;
153 virtual void release_data() = 0;
154 virtual void upload_data() = 0;
155 virtual void duplicate_data(VertBuf *dst) = 0;
156};
157
158} // namespace blender::gpu
159
162 GPUUsageType usage);
163
164#define GPU_vertbuf_create_with_format(format) \
165 GPU_vertbuf_create_with_format_ex(format, GPU_USAGE_STATIC)
166
171void GPU_vertbuf_read(const blender::gpu::VertBuf *verts, void *data);
175
181
183 const GPUVertFormat &format,
185
187 const GPUVertFormat &format,
188 uint v_len);
189
190#define GPU_vertbuf_init_with_format(verts, format) \
191 GPU_vertbuf_init_with_format_ex(verts, format, GPU_USAGE_STATIC)
192
194
209
216void GPU_vertbuf_attr_set(blender::gpu::VertBuf *, uint a_idx, uint v_idx, const void *data);
217
219void GPU_vertbuf_vert_set(blender::gpu::VertBuf *verts, uint v_idx, const void *data);
220
224void GPU_vertbuf_attr_fill(blender::gpu::VertBuf *, uint a_idx, const void *data);
225
227 uint a_idx,
228 uint stride,
229 const void *data);
230
239 unsigned char *data;
240 unsigned char *data_init;
241#ifndef NDEBUG
242 /* Only for overflow check */
243 unsigned char *_data_end;
244#endif
245};
246
248{
249 unsigned char *data = a->data;
250 a->data += a->stride;
251#ifndef NDEBUG
252 BLI_assert(data < a->_data_end);
253#endif
254 return (void *)data;
255}
256
258{
259 return ((a->data - a->data_init) / a->stride);
260}
261
263
269
276
278
284void GPU_vertbuf_update_sub(blender::gpu::VertBuf *verts, uint start, uint len, const void *data);
285
286/* Metrics */
288
289/* Macros */
290#define GPU_VERTBUF_DISCARD_SAFE(verts) \
291 do { \
292 if (verts != nullptr) { \
293 GPU_vertbuf_discard(verts); \
294 verts = nullptr; \
295 } \
296 } while (0)
#define BLI_assert(a)
Definition BLI_assert.h:50
unsigned char uchar
unsigned int uint
#define ENUM_OPERATORS(_type, _max)
#define GPU_INLINE
Definition GPU_common.hh:33
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)
void GPU_vertbuf_init_build_on_device(blender::gpu::VertBuf &verts, const GPUVertFormat &format, uint v_len)
blender::gpu::VertBuf * GPU_vertbuf_duplicate(blender::gpu::VertBuf *verts)
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)
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)
void init()
constexpr MutableSpan< NewT > cast() const
Definition BLI_span.hh:736
virtual void read(void *data) const =0
virtual void wrap_handle(uint64_t handle)=0
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 duplicate_data(VertBuf *dst)=0
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
int len
static float verts[][3]
format
unsigned __int64 uint64_t
Definition stdint.h:90
unsigned char * data_init
unsigned char * data
unsigned char * _data_end