Blender  V3.3
gl_uniform_buffer.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
8 #include "BLI_string.h"
9 
10 #include "gpu_context_private.hh"
11 
12 #include "gl_debug.hh"
13 #include "gl_uniform_buffer.hh"
14 
15 namespace blender::gpu {
16 
17 /* -------------------------------------------------------------------- */
21 GLUniformBuf::GLUniformBuf(size_t size, const char *name) : UniformBuf(size, name)
22 {
23  /* Do not create ubo GL buffer here to allow allocation from any thread. */
25 }
26 
28 {
29  GLContext::buf_free(ubo_id_);
30 }
31 
34 /* -------------------------------------------------------------------- */
38 void GLUniformBuf::init()
39 {
41 
42  glGenBuffers(1, &ubo_id_);
43  glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_);
44  glBufferData(GL_UNIFORM_BUFFER, size_in_bytes_, nullptr, GL_DYNAMIC_DRAW);
45 
46  debug::object_label(GL_UNIFORM_BUFFER, ubo_id_, name_);
47 }
48 
49 void GLUniformBuf::update(const void *data)
50 {
51  if (ubo_id_ == 0) {
52  this->init();
53  }
54  glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_);
55  glBufferSubData(GL_UNIFORM_BUFFER, 0, size_in_bytes_, data);
56  glBindBuffer(GL_UNIFORM_BUFFER, 0);
57 }
58 
61 /* -------------------------------------------------------------------- */
65 void GLUniformBuf::bind(int slot)
66 {
67  if (slot >= GLContext::max_ubo_binds) {
68  fprintf(stderr,
69  "Error: Trying to bind \"%s\" ubo to slot %d which is above the reported limit of %d.",
70  name_,
71  slot,
73  return;
74  }
75 
76  if (ubo_id_ == 0) {
77  this->init();
78  }
79 
80  if (data_ != nullptr) {
81  this->update(data_);
83  }
84 
85  slot_ = slot;
86  glBindBufferBase(GL_UNIFORM_BUFFER, slot_, ubo_id_);
87 
88 #ifdef DEBUG
89  BLI_assert(slot < 16);
90  GLContext::get()->bound_ubo_slots |= 1 << slot;
91 #endif
92 }
93 
95 {
96 #ifdef DEBUG
97  /* NOTE: This only unbinds the last bound slot. */
98  glBindBufferBase(GL_UNIFORM_BUFFER, slot_, 0);
99  /* Hope that the context did not change. */
100  GLContext::get()->bound_ubo_slots &= ~(1 << slot_);
101 #endif
102  slot_ = 0;
103 }
104 
107 } // namespace blender::gpu
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define MEM_SAFE_FREE(v)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
static void buf_free(GLuint buf_id)
Definition: gl_context.cc:250
static GLint max_ubo_binds
Definition: gl_context.hh:47
static GLint max_ubo_size
Definition: gl_context.hh:46
static GLContext * get()
Definition: gl_context.hh:117
GLUniformBuf(size_t size, const char *name)
void update(const void *data) override
void bind(int slot) override
void object_label(GLenum type, GLuint object, const char *name)
Definition: gl_debug.cc:328