29using namespace blender::gpu;
48void GLVaoCache::init()
52 is_dynamic_vao_count =
false;
54 static_vaos.interfaces[i] =
nullptr;
55 static_vaos.vao_ids[i] = 0;
57 vao_base_instance_ = 0;
65 if (!is_dynamic_vao_count) {
68 if (static_vaos.vao_ids[i] == 0) {
74 static_vaos.interfaces[i] = interface;
75 static_vaos.vao_ids[i] = vao;
80 if (static_vaos.interfaces[i] !=
nullptr) {
82 context_->vao_free(static_vaos.vao_ids[i]);
86 is_dynamic_vao_count =
true;
88 dynamic_vaos.count = GPU_BATCH_VAO_DYN_ALLOC_COUNT;
90 dynamic_vaos.count,
"dyn vaos interfaces");
95 if (is_dynamic_vao_count) {
97 for (i = 0; i < dynamic_vaos.count; i++) {
98 if (dynamic_vaos.vao_ids[i] == 0) {
103 if (i == dynamic_vaos.count) {
105 i = dynamic_vaos.count;
106 dynamic_vaos.count += GPU_BATCH_VAO_DYN_ALLOC_COUNT;
108 (
void *)dynamic_vaos.interfaces,
sizeof(
GLShaderInterface *) * dynamic_vaos.count));
109 dynamic_vaos.vao_ids =
static_cast<GLuint *
>(
112 dynamic_vaos.interfaces[i] = interface;
113 dynamic_vaos.vao_ids[i] = vao;
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]);
129 interfaces[i] =
nullptr;
134 if (interface_ == interface) {
135 interface_ =
nullptr;
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;
148 if (context_ ==
nullptr) {
152 if (context_ == ctx) {
153 glDeleteVertexArrays(count, vaos);
154 glDeleteVertexArrays(1, &vao_base_instance_);
158 for (
int i = 0; i < count; i++) {
159 context_->vao_free(vaos[i]);
161 context_->vao_free(vao_base_instance_);
164 for (
int i = 0; i < count; i++) {
165 if (interfaces[i] !=
nullptr) {
170 if (is_dynamic_vao_count) {
171 MEM_delete(dynamic_vaos.interfaces);
172 MEM_delete(dynamic_vaos.vao_ids);
176 context_->vao_cache_unregister(
this);
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];
195void GLVaoCache::context_check()
200 if (context_ != ctx) {
201 if (context_ !=
nullptr) {
214 this->context_check();
218 if (interface_ != interface) {
219 interface_ = interface;
220 vao_id_ = this->
lookup(interface_);
224 glGenVertexArrays(1, &vao_id_);
225 this->
insert(interface_, vao_id_);
259 GLenum gl_type =
to_gl(prim_type);
267 glDrawElementsInstancedBaseVertexBaseInstance(
268 gl_type, v_count, index_type, v_first_ofs, i_count, base_index, i_first);
271 glDrawArraysInstancedBaseInstance(gl_type, v_first, v_count, i_count, i_first);
280 dynamic_cast<GLStorageBuf *
>(indirect_buf)->bind_as(GL_DRAW_INDIRECT_BUFFER);
282 GLenum gl_type =
to_gl(prim_type);
286 glDrawElementsIndirect(gl_type, index_type,
reinterpret_cast<GLvoid *
>(offset));
289 glDrawArraysIndirect(gl_type,
reinterpret_cast<GLvoid *
>(offset));
292 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
303 dynamic_cast<GLStorageBuf *
>(indirect_buf)->bind_as(GL_DRAW_INDIRECT_BUFFER);
305 GLenum gl_type =
to_gl(prim_type);
309 glMultiDrawElementsIndirect(
310 gl_type, index_type,
reinterpret_cast<GLvoid *
>(offset), count, stride);
313 glMultiDrawArraysIndirect(gl_type,
reinterpret_cast<GLvoid *
>(offset), count, stride);
316 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
#define MEM_realloc_zeroed(vmemh, len)
StateManager * state_manager
GLIndexBuf * elem_() const
void multi_draw_indirect(StorageBuf *indirect_buf, int count, intptr_t offset, intptr_t stride) override
void draw_indirect(StorageBuf *indirect_buf, intptr_t offset) override
void draw(int v_first, int v_count, int i_first, int i_count) override
void vao_cache_register(GLVaoCache *cache)
void vao_cache_unregister(GLVaoCache *cache)
void * offset_ptr(uint additional_vertex_offset) const
GLuint vao_get(Batch *batch)
void remove(const GLShaderInterface *interface)
void insert(const GLShaderInterface *interface, GLuint vao_id)
GLuint lookup(const GLShaderInterface *interface)
GPUIndexBufType index_type_
virtual void apply_state()=0
#define GL_CHECK_RESOURCES(info)
#define GPU_VAO_STATIC_LEN
void update_bindings(const GLuint vao, const Batch *batch, const ShaderInterface *interface)
GLenum to_gl(eGPUDataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1