Blender V5.3
gl_batch.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
12
13#include "BLI_assert.hh"
14
15#include "GPU_batch.hh"
16#include "gpu_shader_private.hh"
17
18#include "gl_context.hh"
19#include "gl_debug.hh"
20#include "gl_index_buffer.hh"
21#include "gl_primitive.hh"
22#include "gl_storage_buffer.hh"
23#include "gl_vertex_array.hh"
24
25#include "gl_batch.hh"
26
27namespace blender {
28
29using namespace blender::gpu;
30
31/* -------------------------------------------------------------------- */
37
39{
40 init();
41}
42
44{
45 this->clear();
46}
47
48void GLVaoCache::init()
49{
50 context_ = nullptr;
51 interface_ = nullptr;
52 is_dynamic_vao_count = false;
53 for (int i = 0; i < GPU_VAO_STATIC_LEN; i++) {
54 static_vaos.interfaces[i] = nullptr;
55 static_vaos.vao_ids[i] = 0;
56 }
57 vao_base_instance_ = 0;
58 base_instance_ = 0;
59 vao_id_ = 0;
60}
61
62void GLVaoCache::insert(const GLShaderInterface *interface, GLuint vao)
63{
64 /* Now insert the cache. */
65 if (!is_dynamic_vao_count) {
66 int i; /* find first unused slot */
67 for (i = 0; i < GPU_VAO_STATIC_LEN; i++) {
68 if (static_vaos.vao_ids[i] == 0) {
69 break;
70 }
71 }
72
73 if (i < GPU_VAO_STATIC_LEN) {
74 static_vaos.interfaces[i] = interface;
75 static_vaos.vao_ids[i] = vao;
76 }
77 else {
78 /* Erase previous entries, they will be added back if drawn again. */
79 for (int i = 0; i < GPU_VAO_STATIC_LEN; i++) {
80 if (static_vaos.interfaces[i] != nullptr) {
81 const_cast<GLShaderInterface *>(static_vaos.interfaces[i])->ref_remove(this);
82 context_->vao_free(static_vaos.vao_ids[i]);
83 }
84 }
85 /* Not enough place switch to dynamic. */
86 is_dynamic_vao_count = true;
87 /* Init dynamic arrays and let the branch below set the values. */
88 dynamic_vaos.count = GPU_BATCH_VAO_DYN_ALLOC_COUNT;
90 dynamic_vaos.count, "dyn vaos interfaces");
91 dynamic_vaos.vao_ids = MEM_new_array_zeroed<GLuint>(dynamic_vaos.count, "dyn vaos ids");
92 }
93 }
94
95 if (is_dynamic_vao_count) {
96 int i; /* find first unused slot */
97 for (i = 0; i < dynamic_vaos.count; i++) {
98 if (dynamic_vaos.vao_ids[i] == 0) {
99 break;
100 }
101 }
102
103 if (i == dynamic_vaos.count) {
104 /* Not enough place, realloc the array. */
105 i = dynamic_vaos.count;
106 dynamic_vaos.count += GPU_BATCH_VAO_DYN_ALLOC_COUNT;
107 dynamic_vaos.interfaces = static_cast<const GLShaderInterface **>(MEM_realloc_zeroed(
108 (void *)dynamic_vaos.interfaces, sizeof(GLShaderInterface *) * dynamic_vaos.count));
109 dynamic_vaos.vao_ids = static_cast<GLuint *>(
110 MEM_realloc_zeroed(dynamic_vaos.vao_ids, sizeof(GLuint) * dynamic_vaos.count));
111 }
112 dynamic_vaos.interfaces[i] = interface;
113 dynamic_vaos.vao_ids[i] = vao;
114 }
115
116 const_cast<GLShaderInterface *>(interface)->ref_add(this);
117}
118
120{
121 const int count = (is_dynamic_vao_count) ? dynamic_vaos.count : GPU_VAO_STATIC_LEN;
122 GLuint *vaos = (is_dynamic_vao_count) ? dynamic_vaos.vao_ids : static_vaos.vao_ids;
123 const GLShaderInterface **interfaces = (is_dynamic_vao_count) ? dynamic_vaos.interfaces :
124 static_vaos.interfaces;
125 for (int i = 0; i < count; i++) {
126 if (interfaces[i] == interface) {
127 context_->vao_free(vaos[i]);
128 vaos[i] = 0;
129 interfaces[i] = nullptr;
130 break; /* cannot have duplicates */
131 }
132 }
133
134 if (interface_ == interface) {
135 interface_ = nullptr;
136 vao_id_ = 0;
137 }
138}
139
141{
142 GLContext *ctx = GLContext::get();
143 const int count = (is_dynamic_vao_count) ? dynamic_vaos.count : GPU_VAO_STATIC_LEN;
144 GLuint *vaos = (is_dynamic_vao_count) ? dynamic_vaos.vao_ids : static_vaos.vao_ids;
145 const GLShaderInterface **interfaces = (is_dynamic_vao_count) ? dynamic_vaos.interfaces :
146 static_vaos.interfaces;
147 /* Early out, nothing to free. */
148 if (context_ == nullptr) {
149 return;
150 }
151
152 if (context_ == ctx) {
153 glDeleteVertexArrays(count, vaos);
154 glDeleteVertexArrays(1, &vao_base_instance_);
155 }
156 else {
157 /* TODO(fclem): Slow way. Could avoid multiple mutex lock here */
158 for (int i = 0; i < count; i++) {
159 context_->vao_free(vaos[i]);
160 }
161 context_->vao_free(vao_base_instance_);
162 }
163
164 for (int i = 0; i < count; i++) {
165 if (interfaces[i] != nullptr) {
166 const_cast<GLShaderInterface *>(interfaces[i])->ref_remove(this);
167 }
168 }
169
170 if (is_dynamic_vao_count) {
171 MEM_delete(dynamic_vaos.interfaces);
172 MEM_delete(dynamic_vaos.vao_ids);
173 }
174
175 if (context_) {
176 context_->vao_cache_unregister(this);
177 }
178 /* Reinitialize. */
179 this->init();
180}
181
182GLuint GLVaoCache::lookup(const GLShaderInterface *interface)
183{
184 const int count = (is_dynamic_vao_count) ? dynamic_vaos.count : GPU_VAO_STATIC_LEN;
185 const GLShaderInterface **interfaces = (is_dynamic_vao_count) ? dynamic_vaos.interfaces :
186 static_vaos.interfaces;
187 for (int i = 0; i < count; i++) {
188 if (interfaces[i] == interface) {
189 return (is_dynamic_vao_count) ? dynamic_vaos.vao_ids[i] : static_vaos.vao_ids[i];
190 }
191 }
192 return 0;
193}
194
195void GLVaoCache::context_check()
196{
197 GLContext *ctx = GLContext::get();
198 BLI_assert(ctx);
199
200 if (context_ != ctx) {
201 if (context_ != nullptr) {
202 /* IMPORTANT: Trying to draw a batch in multiple different context will trash the VAO cache.
203 * This has major performance impact and should be avoided in most cases. */
204 context_->vao_cache_unregister(this);
205 }
206 this->clear();
207 context_ = ctx;
208 context_->vao_cache_register(this);
209 }
210}
211
212GLuint GLVaoCache::vao_get(Batch *batch)
213{
214 this->context_check();
215
217 GLShaderInterface *interface = static_cast<GLShaderInterface *>(shader->interface);
218 if (interface_ != interface) {
219 interface_ = interface;
220 vao_id_ = this->lookup(interface_);
221
222 if (vao_id_ == 0) {
223 /* Cache miss, create a new VAO. */
224 glGenVertexArrays(1, &vao_id_);
225 this->insert(interface_, vao_id_);
226 GLVertArray::update_bindings(vao_id_, batch, interface_);
227 }
228 }
229
230 return vao_id_;
231}
232
234
235/* -------------------------------------------------------------------- */
238
240{
242
243 if (flag & GPU_BATCH_DIRTY) {
244 flag &= ~GPU_BATCH_DIRTY;
246 }
247
248 glBindVertexArray(vao_cache_.vao_get(this));
249}
250
251void GLBatch::draw(int v_first, int v_count, int i_first, int i_count)
252{
253 GL_CHECK_RESOURCES("Batch");
254
255 this->bind();
256
257 BLI_assert(v_count > 0 && i_count > 0);
258
259 GLenum gl_type = to_gl(prim_type);
260
261 if (elem) {
262 const GLIndexBuf *el = this->elem_();
263 GLenum index_type = to_gl(el->index_type_);
264 GLint base_index = el->index_base_;
265 void *v_first_ofs = el->offset_ptr(v_first);
266
267 glDrawElementsInstancedBaseVertexBaseInstance(
268 gl_type, v_count, index_type, v_first_ofs, i_count, base_index, i_first);
269 }
270 else {
271 glDrawArraysInstancedBaseInstance(gl_type, v_first, v_count, i_count, i_first);
272 }
273}
274
276{
277 GL_CHECK_RESOURCES("Batch");
278
279 this->bind();
280 dynamic_cast<GLStorageBuf *>(indirect_buf)->bind_as(GL_DRAW_INDIRECT_BUFFER);
281
282 GLenum gl_type = to_gl(prim_type);
283 if (elem) {
284 const GLIndexBuf *el = this->elem_();
285 GLenum index_type = to_gl(el->index_type_);
286 glDrawElementsIndirect(gl_type, index_type, reinterpret_cast<GLvoid *>(offset));
287 }
288 else {
289 glDrawArraysIndirect(gl_type, reinterpret_cast<GLvoid *>(offset));
290 }
291 /* Unbind. */
292 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
293}
294
296 int count,
297 intptr_t offset,
298 intptr_t stride)
299{
300 GL_CHECK_RESOURCES("Batch");
301
302 this->bind();
303 dynamic_cast<GLStorageBuf *>(indirect_buf)->bind_as(GL_DRAW_INDIRECT_BUFFER);
304
305 GLenum gl_type = to_gl(prim_type);
306 if (elem) {
307 const GLIndexBuf *el = this->elem_();
308 GLenum index_type = to_gl(el->index_type_);
309 glMultiDrawElementsIndirect(
310 gl_type, index_type, reinterpret_cast<GLvoid *>(offset), count, stride);
311 }
312 else {
313 glMultiDrawArraysIndirect(gl_type, reinterpret_cast<GLvoid *>(offset), count, stride);
314 }
315 /* Unbind. */
316 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
317}
318
320
321} // namespace blender
#define MEM_realloc_zeroed(vmemh, len)
GLIndexBuf * elem_() const
Definition gl_batch.hh:102
void multi_draw_indirect(StorageBuf *indirect_buf, int count, intptr_t offset, intptr_t stride) override
Definition gl_batch.cc:295
void draw_indirect(StorageBuf *indirect_buf, intptr_t offset) override
Definition gl_batch.cc:275
GLVaoCache vao_cache_
Definition gl_batch.hh:89
void draw(int v_first, int v_count, int i_first, int i_count) override
Definition gl_batch.cc:251
void vao_cache_register(GLVaoCache *cache)
void vao_cache_unregister(GLVaoCache *cache)
static GLContext * get()
void * offset_ptr(uint additional_vertex_offset) const
GLuint vao_get(Batch *batch)
Definition gl_batch.cc:212
void remove(const GLShaderInterface *interface)
Definition gl_batch.cc:119
void insert(const GLShaderInterface *interface, GLuint vao_id)
Definition gl_batch.cc:62
GLuint lookup(const GLShaderInterface *interface)
Definition gl_batch.cc:182
GPUIndexBufType index_type_
virtual void apply_state()=0
#define GL_CHECK_RESOURCES(info)
Definition gl_debug.hh:14
#define GPU_VAO_STATIC_LEN
Definition mtl_batch.hh:26
void update_bindings(const GLuint vao, const Batch *batch, const ShaderInterface *interface)
GLenum to_gl(eGPUDataFormat format)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ GPU_BATCH_DIRTY
Definition GPU_batch.hh:55
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
_W64 int intptr_t
Definition BKE_text.h:119