Blender V5.3
gl_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
8
9#include "GPU_texture.hh"
10
11#include "gl_context.hh"
12
13#include "gl_vertex_buffer.hh"
14
15namespace blender::gpu {
16
18{
20 return;
21 }
22
23 /* Discard previous data if any. */
24 MEM_SAFE_DELETE(data_);
26}
27
29{
31 return;
32 }
33
34 data_ = static_cast<uchar *>(
36}
37
39{
40 if (is_wrapper_) {
41 return;
42 }
43
44 if (vbo_id_ != 0) {
45 GPU_TEXTURE_FREE_SAFE(buffer_texture_);
47 vbo_id_ = 0;
48 memory_usage -= vbo_size_;
49 }
50
51 MEM_SAFE_DELETE(data_);
52}
53
55{
56 this->bind();
57}
58
60{
61 BLI_assert(GLContext::get() != nullptr);
62
63 if (vbo_id_ == 0) {
64 glGenBuffers(1, &vbo_id_);
65 }
66
67 glBindBuffer(GL_ARRAY_BUFFER, vbo_id_);
68
70 vbo_size_ = this->size_used_get();
71
72 /* This is fine on some systems but will crash on others. */
73 BLI_assert(vbo_size_ != 0);
74 /* Orphan the vbo to avoid sync then upload data. */
75 glBufferData(GL_ARRAY_BUFFER, ceil_to_multiple_ul(vbo_size_, 16), nullptr, to_gl(usage_));
76 /* Do not transfer data from host to device when buffer is device only. */
78 glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_size_, data_);
79 }
80 memory_usage += vbo_size_;
81
82 if (usage_ == GPU_USAGE_STATIC) {
83 MEM_SAFE_DELETE(data_);
84 }
87 }
88}
89
91{
92 bind();
93 BLI_assert(vbo_id_ != 0);
94 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, vbo_id_);
95
96#ifndef NDEBUG
97 BLI_assert(binding < 16);
98 GLContext::get()->bound_ssbo_slots |= 1 << binding;
99#endif
100}
101
103{
104 bind();
105 BLI_assert(vbo_id_ != 0);
106 if (buffer_texture_ == nullptr) {
107 buffer_texture_ = GPU_texture_create_from_vertbuf("vertbuf_as_texture", this);
108 }
109 GPU_texture_bind(buffer_texture_, binding);
110}
111
112void GLVertBuf::read(void *data) const
113{
114 BLI_assert(is_active());
115 void *result = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
116 memcpy(data, result, size_used_get());
117 glUnmapBuffer(GL_ARRAY_BUFFER);
118}
119
121{
122 BLI_assert(vbo_id_ == 0);
123 BLI_assert(glIsBuffer(uint(handle)));
124 is_wrapper_ = true;
125 vbo_id_ = uint(handle);
126 /* We assume the data is already on the device, so no need to allocate or send it. */
128}
129
130bool GLVertBuf::is_active() const
131{
132 if (!vbo_id_) {
133 return false;
134 }
135 int active_vbo_id = 0;
136 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &active_vbo_id);
137 return vbo_id_ == active_vbo_id;
138}
139
140void GLVertBuf::update_sub(uint start, uint len, const void *data)
141{
142 glBufferSubData(GL_ARRAY_BUFFER, start, len, data);
143}
144
146 uint source_first_vertex,
147 uint dest_first_vertex,
149{
150 BLI_assert(format.stride == source_buf.format.stride);
151 BLI_assert_msg(size_t(source_first_vertex) + vertex_len <= source_buf.vertex_len,
152 "Copy source range exceeds the source vertex buffer bounds");
153 BLI_assert_msg(size_t(dest_first_vertex) + vertex_len <= this->vertex_len,
154 "Copy destination range exceeds the vertex buffer bounds");
155 GLVertBuf &src = static_cast<GLVertBuf &>(source_buf);
156 BLI_assert_msg(vbo_id_, "GPU_vertbuf_use() not called on this buffer");
157 BLI_assert_msg(src.vbo_id_, "GPU_vertbuf_use() not called on the source buffer");
158
159 const size_t src_offset = size_t(source_first_vertex) * src.format.stride;
160 const size_t dst_offset = size_t(dest_first_vertex) * format.stride;
161 const size_t copy_size = size_t(vertex_len) * format.stride;
162
164 glCopyNamedBufferSubData(src.vbo_id_, vbo_id_, src_offset, dst_offset, copy_size);
165 }
166 else {
167 glBindBuffer(GL_COPY_READ_BUFFER, src.vbo_id_);
168 glBindBuffer(GL_COPY_WRITE_BUFFER, vbo_id_);
169 glCopyBufferSubData(
170 GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, src_offset, dst_offset, copy_size);
171 glBindBuffer(GL_COPY_READ_BUFFER, 0);
172 glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
173 }
174}
175
176} // namespace blender::gpu
#define GPU_TEXTURE_FREE_SAFE(texture)
#define BLI_assert_msg(a, msg)
#define MEM_realloc_uninitialized(vmemh, len)
static void buffer_free(GLuint buf_id)
static bool direct_state_access_support
Definition gl_context.hh:60
static GLContext * get()
void bind_as_texture(uint binding) override
void read(void *data) const override
void bind_as_ssbo(uint binding) override
void wrap_handle(uint64_t handle) override
void update_sub(uint start, uint len, const void *data) override
void copy_sub(VertBuf &source_buf, uint source_first_vertex, uint dest_first_vertex, uint vertex_len) override
Copy from part of source vertex buffer into the vertex buffer.
MutableSpan< T > data()
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
GLenum to_gl(eGPUDataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
unsigned char uchar
Definition BKE_text.h:58
gpu::Texture * GPU_texture_create_from_vertbuf(const char *name, gpu::VertBuf *vertex_buf)
@ GPU_VERTBUF_DATA_UPLOADED
void GPU_texture_bind(gpu::Texture *texture, int unit)