Blender V4.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
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. */
25 data_ = (uchar *)MEM_mallocN(sizeof(uchar) * this->size_alloc_get(), __func__);
26}
27
29{
31 return;
32 }
33
34 data_ = (uchar *)MEM_reallocN(data_, sizeof(uchar) * this->size_alloc_get());
35}
36
38{
39 if (is_wrapper_) {
40 return;
41 }
42
43 if (vbo_id_ != 0) {
44 GPU_TEXTURE_FREE_SAFE(buffer_texture_);
45 GLContext::buf_free(vbo_id_);
46 vbo_id_ = 0;
47 memory_usage -= vbo_size_;
48 }
49
51}
52
54{
55 BLI_assert(GLContext::get() != nullptr);
56 GLVertBuf *src = this;
57 GLVertBuf *dst = static_cast<GLVertBuf *>(dst_);
58 dst->buffer_texture_ = nullptr;
59
60 if (src->vbo_id_ != 0) {
61 dst->vbo_size_ = src->size_used_get();
62
63 glGenBuffers(1, &dst->vbo_id_);
64 glBindBuffer(GL_COPY_WRITE_BUFFER, dst->vbo_id_);
65 glBufferData(GL_COPY_WRITE_BUFFER, dst->vbo_size_, nullptr, to_gl(dst->usage_));
66
67 glBindBuffer(GL_COPY_READ_BUFFER, src->vbo_id_);
68
69 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, dst->vbo_size_);
70
71 memory_usage += dst->vbo_size_;
72 }
73
74 if (data_ != nullptr) {
75 dst->data_ = (uchar *)MEM_dupallocN(src->data_);
76 }
77}
78
80{
81 this->bind();
82}
83
85{
86 BLI_assert(GLContext::get() != nullptr);
87
88 if (vbo_id_ == 0) {
89 glGenBuffers(1, &vbo_id_);
90 }
91
92 glBindBuffer(GL_ARRAY_BUFFER, vbo_id_);
93
95 vbo_size_ = this->size_used_get();
96
97 BLI_assert(vbo_size_ != 0);
98 /* Orphan the vbo to avoid sync then upload data. */
99 glBufferData(GL_ARRAY_BUFFER, ceil_to_multiple_ul(vbo_size_, 16), nullptr, to_gl(usage_));
100 /* Do not transfer data from host to device when buffer is device only. */
102 glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_size_, data_);
103 }
104 memory_usage += vbo_size_;
105
106 if (usage_ == GPU_USAGE_STATIC) {
108 }
109 flag &= ~GPU_VERTBUF_DATA_DIRTY;
111 }
112}
113
115{
116 bind();
117 BLI_assert(vbo_id_ != 0);
118 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, vbo_id_);
119
120#ifndef NDEBUG
121 BLI_assert(binding < 16);
122 GLContext::get()->bound_ssbo_slots |= 1 << binding;
123#endif
124}
125
127{
128 bind();
129 BLI_assert(vbo_id_ != 0);
130 if (buffer_texture_ == nullptr) {
131 buffer_texture_ = GPU_texture_create_from_vertbuf("vertbuf_as_texture", this);
132 }
133 GPU_texture_bind(buffer_texture_, binding);
134}
135
136void GLVertBuf::read(void *data) const
137{
138 BLI_assert(is_active());
139 void *result = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
140 memcpy(data, result, size_used_get());
141 glUnmapBuffer(GL_ARRAY_BUFFER);
142}
143
145{
146 BLI_assert(vbo_id_ == 0);
147 BLI_assert(glIsBuffer(uint(handle)));
148 is_wrapper_ = true;
149 vbo_id_ = uint(handle);
150 /* We assume the data is already on the device, so no need to allocate or send it. */
152}
153
154bool GLVertBuf::is_active() const
155{
156 if (!vbo_id_) {
157 return false;
158 }
159 int active_vbo_id = 0;
160 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &active_vbo_id);
161 return vbo_id_ == active_vbo_id;
162}
163
164void GLVertBuf::update_sub(uint start, uint len, const void *data)
165{
166 glBufferSubData(GL_ARRAY_BUFFER, start, len, data);
167}
168
169} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:50
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
unsigned char uchar
unsigned int uint
void GPU_texture_bind(GPUTexture *texture, int unit)
GPUTexture * GPU_texture_create_from_vertbuf(const char *name, blender::gpu::VertBuf *vertex_buf)
#define GPU_TEXTURE_FREE_SAFE(texture)
@ GPU_VERTBUF_DATA_DIRTY
@ GPU_VERTBUF_DATA_UPLOADED
@ GPU_USAGE_STATIC
@ GPU_USAGE_DEVICE_ONLY
#define MEM_SAFE_FREE(v)
#define MEM_reallocN(vmemh, len)
static void buf_free(GLuint buf_id)
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 duplicate_data(VertBuf *dst) override
void update_sub(uint start, uint len, const void *data) override
int len
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
static GLenum to_gl(const GPUAttachmentType type)
unsigned __int64 uint64_t
Definition stdint.h:90