Blender V4.5
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_span.hh"
14#include "BLI_utildefines.h"
15
16#include "GPU_common.hh"
17#include "GPU_vertex_format.hh"
18
29
31
32
39
41 /* can be extended to support more types */
43 GPU_USAGE_STATIC = 1, /* do not keep data in memory */
45 GPU_USAGE_DEVICE_ONLY = 3, /* Do not do host->device data transfers. */
46
48 /* Flag for vertex buffers used for textures. Skips additional padding/compaction to ensure
49 * format matches the texture exactly. Can be masked with other properties, and is stripped
50 * during VertBuf::init. */
52};
53
55
56namespace blender::gpu {
57
62class VertBuf {
63 public:
64 static size_t memory_usage;
65
73
74#ifndef NDEBUG
77#endif
78
79 protected:
81 uchar *data_ = nullptr;
82
85
86 private:
88 int handle_refcount_ = 1;
89
90 public:
91 VertBuf();
92 virtual ~VertBuf();
93
94 void init(const GPUVertFormat &format, GPUUsageType usage);
95 void clear();
96
97 /* Data management. */
98 void allocate(uint vert_len);
99 void resize(uint vert_len);
100 void upload();
101 virtual void bind_as_ssbo(uint binding) = 0;
102 virtual void bind_as_texture(uint binding) = 0;
103
104 virtual void wrap_handle(uint64_t handle) = 0;
105
106 /* Size of the data allocated. */
107 size_t size_alloc_get() const
108 {
109 BLI_assert(this->format.packed);
110 return size_t(this->vertex_alloc) * this->format.stride;
111 }
112 /* Size of the data uploaded to the GPU. */
113 size_t size_used_get() const
114 {
115 BLI_assert(format.packed);
116 return size_t(this->vertex_len) * this->format.stride;
117 }
118
120 {
121 handle_refcount_++;
122 }
124 {
125 BLI_assert(handle_refcount_ > 0);
126 handle_refcount_--;
127 if (handle_refcount_ == 0) {
128 delete this;
129 }
130 }
131
133 {
134 return usage_;
135 }
136
141 template<typename T> MutableSpan<T> data()
142 {
143 return MutableSpan<uchar>(data_, this->size_alloc_get()).cast<T>();
144 }
145
146 virtual void update_sub(uint start, uint len, const void *data) = 0;
147 virtual void read(void *data) const = 0;
148
149 protected:
150 virtual void acquire_data() = 0;
151 virtual void resize_data() = 0;
152 virtual void release_data() = 0;
153 virtual void upload_data() = 0;
154};
155
156} // namespace blender::gpu
157
160 GPUUsageType usage);
161
162#define GPU_vertbuf_create_with_format(format) \
163 GPU_vertbuf_create_with_format_ex(format, GPU_USAGE_STATIC)
164
173
179
181 const GPUVertFormat &format,
183
185 const GPUVertFormat &format,
186 uint v_len);
187
189
190#define GPU_vertbuf_init_with_format(verts, format) \
191 GPU_vertbuf_init_with_format_ex(verts, format, GPU_USAGE_STATIC)
192
207
214void GPU_vertbuf_attr_set(blender::gpu::VertBuf *, uint a_idx, uint v_idx, const void *data);
215
217void GPU_vertbuf_vert_set(blender::gpu::VertBuf *verts, uint v_idx, const void *data);
218
222void GPU_vertbuf_attr_fill(blender::gpu::VertBuf *, uint a_idx, const void *data);
223
225 uint a_idx,
226 uint stride,
227 const void *data);
228
237 unsigned char *data;
238 unsigned char *data_init;
239#ifndef NDEBUG
240 /* Only for overflow check */
241 unsigned char *_data_end;
242#endif
243};
244
246{
247 unsigned char *data = a->data;
248 a->data += a->stride;
249#ifndef NDEBUG
250 BLI_assert(data < a->_data_end);
251#endif
252 return (void *)data;
253}
254
256{
257 return ((a->data - a->data_init) / a->stride);
258}
259
261
267
274
276
283
284/* Metrics */
286
287/* Macros */
288#define GPU_VERTBUF_DISCARD_SAFE(verts) \
289 do { \
290 if (verts != nullptr) { \
291 GPU_vertbuf_discard(verts); \
292 verts = nullptr; \
293 } \
294 } while (0)
295
296namespace blender::gpu {
297
299 public:
301 {
303 }
304};
305
306using VertBufPtr = std::unique_ptr<gpu::VertBuf, gpu::VertBufDeleter>;
307
308} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:46
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)
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
constexpr MutableSpan< NewT > cast() const
Definition BLI_span.hh:749
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 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