Blender V5.3
gl_context.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_assert.hh"
10#include "BLI_utildefines.hh"
11
12#include "BKE_global.hh"
13
14#include "GPU_framebuffer.hh"
15
18
19#include "gl_debug.hh"
20#include "gl_immediate.hh"
21#include "gl_state.hh"
22#include "gl_uniform_buffer.hh"
23
24#include "gl_backend.hh" /* TODO: remove. */
25#include "gl_context.hh"
26
27namespace blender {
28
29using namespace blender::gpu;
30
31/* -------------------------------------------------------------------- */
34
36 GHOST_IContext *ghost_context,
37 GLSharedOrphanLists &shared_orphan_list)
38 : shared_orphan_list_(shared_orphan_list)
39{
41
42 if (G.debug & G_DEBUG_GPU) {
44 }
45
46 float data[4] = {0.0f, 0.0f, 0.0f, 1.0f};
47 glGenBuffers(1, &default_attr_vbo_);
48 glBindBuffer(GL_ARRAY_BUFFER, default_attr_vbo_);
49 glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
50 glBindBuffer(GL_ARRAY_BUFFER, 0);
51
53 imm = new GLImmediate();
54 ghost_window_ = ghost_window;
55
56 if (ghost_window) {
57 GLuint default_fbo = ghost_window->getDefaultFramebuffer();
58
60 ghost_window->getClientBounds(bounds);
61 const int w = bounds.getWidth();
62 const int h = bounds.getHeight();
63
64 if (default_fbo != 0) {
65 /* Bind default framebuffer, otherwise state might be undefined. */
66 glBindFramebuffer(GL_FRAMEBUFFER, default_fbo);
67 front_left = new GLFrameBuffer("front_left", this, GL_COLOR_ATTACHMENT0, default_fbo, w, h);
68 back_left = new GLFrameBuffer("back_left", this, GL_COLOR_ATTACHMENT0, default_fbo, w, h);
69 }
70 else {
71 front_left = new GLFrameBuffer("front_left", this, GL_FRONT_LEFT, 0, w, h);
72 back_left = new GLFrameBuffer("back_left", this, GL_BACK_LEFT, 0, w, h);
73 }
74
75 GLboolean supports_stereo_quad_buffer = GL_FALSE;
76 glGetBooleanv(GL_STEREO, &supports_stereo_quad_buffer);
77 if (supports_stereo_quad_buffer) {
78 front_right = new GLFrameBuffer("front_right", this, GL_FRONT_RIGHT, 0, w, h);
79 back_right = new GLFrameBuffer("back_right", this, GL_BACK_RIGHT, 0, w, h);
80 }
81 }
82 else {
83 /* For off-screen contexts. Default frame-buffer is null. */
84 back_left = new GLFrameBuffer("back_left", this, GL_NONE, 0, 0, 0);
85 }
86 ghost_context_ = ghost_context;
87
89 static_cast<GLStateManager *>(state_manager)->active_fb = static_cast<GLFrameBuffer *>(
90 active_fb);
91}
92
94{
95 if (G.profile_gpu) {
96 /* Ensure query results are available. */
97 finish();
98 process_frame_timings();
99 }
101 BLI_assert(orphaned_framebuffers_.is_empty());
102 BLI_assert(orphaned_vertarrays_.is_empty());
103 /* For now don't allow GPUFrameBuffers to be reuse in another context. */
104 BLI_assert(framebuffers_.is_empty());
105 /* Delete VAO's so the batch can be reused in another context. */
106 for (GLVaoCache *cache : vao_caches_) {
107 cache->clear();
108 }
109 glDeleteBuffers(1, &default_attr_vbo_);
110
112}
113
115
116/* -------------------------------------------------------------------- */
119
121{
122 /* Make sure no other context is already bound to this thread. */
123 BLI_assert(is_active_ == false);
124 /* Make sure the active GHOST context matches the one this GPU Context was created for. */
126
127 is_active_ = true;
128 thread_ = pthread_self();
129
130 /* Clear accumulated orphans. */
131 orphans_clear();
132
133 if (ghost_window_) {
134 /* Get the correct framebuffer size for the internal framebuffers. */
135 GHOST_Rect bounds = {0};
137
138 const int w = bounds.getWidth();
139 const int h = bounds.getHeight();
140
141 if (front_left) {
142 front_left->size_set(w, h);
143 }
144 if (back_left) {
145 back_left->size_set(w, h);
146 }
147 if (front_right) {
149 }
150 if (back_right) {
151 back_right->size_set(w, h);
152 }
153 }
154
155 /* Not really following the state but we should consider
156 * no ubo bound when activating a context. */
157 bound_ubo_slots = 0;
159
160 immActivate();
161}
162
164{
166 is_active_ = false;
167}
168
170{
171 /* No-op. */
172}
173
175{
176 process_frame_timings();
177}
178
180
181/* -------------------------------------------------------------------- */
184
186{
187 glFlush();
188}
189
191{
192 glFinish();
193}
194
196
197/* -------------------------------------------------------------------- */
203
204void GLSharedOrphanLists::OrphanList::clear(FunctionRef<void(GLuint, GLuint *)> free_fn)
205{
206 std::scoped_lock lock(mutex_);
207 if (!handles_.is_empty()) {
208 free_fn(uint(handles_.size()), handles_.data());
209 handles_.clear();
210 }
211};
212
213void GLSharedOrphanLists::OrphanList::append(GLuint handle)
214{
215 std::scoped_lock lock(mutex_);
216 handles_.append(handle);
217};
218
220{
221 /* Check if any context is active on this thread! */
223
224 buffers.clear(glDeleteBuffers);
225 textures.clear(glDeleteTextures);
226 shaders.clear([](GLuint size, GLuint *handles) {
227 for (uint i = 0; i < size; i++) {
228 glDeleteShader(handles[i]);
229 }
230 });
231 programs.clear([](GLuint size, GLuint *handles) {
232 for (uint i = 0; i < size; i++) {
233 glDeleteProgram(handles[i]);
234 }
235 });
236};
237
238void GLContext::orphans_clear()
239{
240 /* Check if context has been activated by another thread! */
241 BLI_assert(this->is_active_on_thread());
242
243 lists_mutex_.lock();
244 if (!orphaned_vertarrays_.is_empty()) {
245 glDeleteVertexArrays(uint(orphaned_vertarrays_.size()), orphaned_vertarrays_.data());
246 orphaned_vertarrays_.clear();
247 }
248 if (!orphaned_framebuffers_.is_empty()) {
249 glDeleteFramebuffers(uint(orphaned_framebuffers_.size()), orphaned_framebuffers_.data());
250 orphaned_framebuffers_.clear();
251 }
252 lists_mutex_.unlock();
253
254 shared_orphan_list_.orphans_clear();
255};
256
257void GLContext::orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex, GLuint id)
258{
259 list_mutex.lock();
260 orphan_list.append(id);
261 list_mutex.unlock();
262}
263
264void GLContext::vao_free(GLuint vao_id)
265{
266 if (this == GLContext::get()) {
267 glDeleteVertexArrays(1, &vao_id);
268 }
269 else {
270 orphans_add(orphaned_vertarrays_, lists_mutex_, vao_id);
271 }
272}
273
274void GLContext::fbo_free(GLuint fbo_id)
275{
276 if (this == GLContext::get()) {
277 glDeleteFramebuffers(1, &fbo_id);
278 }
279 else {
280 orphans_add(orphaned_framebuffers_, lists_mutex_, fbo_id);
281 }
282}
283
284void GLContext::buffer_free(GLuint buf_id)
285{
286 /* Any context can free. */
287 if (GLContext::get()) {
288 glDeleteBuffers(1, &buf_id);
289 }
290 else {
292 orphan_list.buffers.append(buf_id);
293 }
294}
295
296void GLContext::texture_free(GLuint tex_id)
297{
298 /* Any context can free. */
299 if (GLContext::get()) {
300 glDeleteTextures(1, &tex_id);
301 }
302 else {
304 orphan_list.textures.append(tex_id);
305 }
306}
307
308void GLContext::shader_free(GLuint shader_id)
309{
310 /* Any context can free. */
311 if (GLContext::get()) {
312 glDeleteShader(shader_id);
313 }
314 else {
316 orphan_list.shaders.append(shader_id);
317 }
318}
319
320void GLContext::program_free(GLuint program_id)
321{
322 /* Any context can free. */
323 if (GLContext::get()) {
324 glDeleteProgram(program_id);
325 }
326 else {
328 orphan_list.programs.append(program_id);
329 }
330}
331
333
334/* -------------------------------------------------------------------- */
341
343{
344 lists_mutex_.lock();
345 vao_caches_.add(cache);
346 lists_mutex_.unlock();
347}
348
350{
351 lists_mutex_.lock();
352 vao_caches_.remove(cache);
353 lists_mutex_.unlock();
354}
355
357
358/* -------------------------------------------------------------------- */
361
362void GLContext::memory_statistics_get(int *r_total_mem, int *r_free_mem)
363{
364 if (epoxy_has_gl_extension("GL_NVX_gpu_memory_info")) {
365 /* Returned value in Kb. */
366 glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, r_total_mem);
367 glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, r_free_mem);
368 }
369 else if (epoxy_has_gl_extension("GL_ATI_meminfo")) {
370 int stats[4];
371 glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, stats);
372
373 *r_total_mem = 0;
374 *r_free_mem = stats[0]; /* Total memory free in the pool. */
375 }
376 else {
377 *r_total_mem = 0;
378 *r_free_mem = 0;
379 }
380}
381
383
384} // namespace blender
volatile int lock
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
static GHOST_IContext * getActiveDrawingContext()
virtual void getClientBounds(GHOST_Rect &bounds) const =0
virtual unsigned int getDefaultFramebuffer()=0
void size_set(int width, int height)
GLSharedOrphanLists & shared_orphan_list_get()
void remove_context_id(int context_id)
static GLBackend * get()
Definition gl_backend.hh:86
void add_context_id(int context_id)
Definition gl_backend.hh:96
void vao_cache_register(GLVaoCache *cache)
void activate() override
static void buffer_free(GLuint buf_id)
void fbo_free(GLuint fbo_id)
static void shader_free(GLuint shader_id)
void flush() override
void vao_free(GLuint vao_id)
static void texture_free(GLuint tex_id)
void vao_cache_unregister(GLVaoCache *cache)
void begin_frame() override
static GLContext * get()
void deactivate() override
void memory_statistics_get(int *r_total_mem, int *r_free_mem) override
void finish() override
static void program_free(GLuint program_id)
void end_frame() override
GLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context, GLSharedOrphanLists &shared_orphan_list)
Definition gl_context.cc:35
GLFrameBuffer(const char *name)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
Global G
Definition blender.cc:57
BMesh const char void * data
void immDeactivate()
void immActivate()
int size() const
Definition vector.hh:130