Blender V5.3
gl_context.hh
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#pragma once
10
12
13#include "GPU_framebuffer.hh"
14
15#include "BKE_global.hh"
16#include "BLI_set.hh"
17#include "BLI_vector.hh"
18
19#include "gl_state.hh"
20
21#include <mutex>
22
23namespace blender::gpu {
24
25class GLVaoCache;
26
28 class OrphanList {
30 std::mutex mutex_;
32 Vector<GLuint> handles_;
33
34 public:
35 void clear(FunctionRef<void(GLuint, GLuint *)> free_fn);
36 void append(GLuint handle);
37 };
38
39 public:
41 OrphanList textures;
42 OrphanList buffers;
43 OrphanList shaders;
44 OrphanList programs;
45
46 void orphans_clear();
47};
48
49class GLContext : public Context {
50 public:
52
53 static GLint max_cubemap_size;
54 static GLint max_ubo_binds;
55 static GLint max_ssbo_binds;
56
58
62 /* layered_rendering_support requires GL_ARB_shader_viewport_layer_array, which is a superset of
63 * GL_AMD_vertex_shader_viewport_index (vertex_shader_viewport_index_support) and
64 * GL_AMD_vertex_shader_layer (vertex_shader_layer_support) with additional support for
65 * tessellation evaluation shaders (which are not used by the GPU module). */
70 static bool multi_bind_support;
76
78
81
84
88
89 private:
94 Set<GLVaoCache *> vao_caches_;
95 Set<gpu::FrameBuffer *> framebuffers_;
97 std::mutex lists_mutex_;
99 Vector<GLuint> orphaned_vertarrays_;
100 Vector<GLuint> orphaned_framebuffers_;
102 GLSharedOrphanLists &shared_orphan_list_;
103
104 struct TimeQuery {
105 std::string name;
106 union {
107 GLuint handles[2];
108 struct {
109 GLuint handle_start, handle_end;
110 };
111 };
112 bool finished;
113 int64_t cpu_start;
114 int64_t cpu_end;
115 };
116 struct FrameQueries {
117 Vector<TimeQuery> queries;
118 };
119 Vector<FrameQueries> frame_timings;
120
121 void process_frame_timings();
122
123 class GHOST_IContext *ghost_context_;
124
125 public:
126 GLContext(GHOST_IWindow *ghost_window,
127 GHOST_IContext *ghost_context,
128 GLSharedOrphanLists &shared_orphan_list);
129 ~GLContext();
130
131 static void check_error(const char *info);
132
133 void activate() override;
134 void deactivate() override;
135 void begin_frame() override;
136 void end_frame() override;
137
138 void flush() override;
139 void finish() override;
140
141 void memory_statistics_get(int *r_total_mem, int *r_free_mem) override;
142
143 static GLContext *get()
144 {
145 return static_cast<GLContext *>(Context::get());
146 }
147
149 {
150 GLContext *ctx = GLContext::get();
151 return static_cast<GLStateManager *>(ctx->state_manager);
152 };
153
154 /* These need to be called with the context the id was created with. */
155 void vao_free(GLuint vao_id);
156 void fbo_free(GLuint fbo_id);
157 /* These can be called by any threads even without OpenGL ctx. Deletion will be delayed. */
158 static void buffer_free(GLuint buf_id);
159 static void texture_free(GLuint tex_id);
160 static void shader_free(GLuint shader_id);
161 static void program_free(GLuint program_id);
162
163 void vao_cache_register(GLVaoCache *cache);
164 void vao_cache_unregister(GLVaoCache *cache);
165
166 void debug_group_begin(const char *name, int index) override;
167 void debug_group_end() override;
168 bool debug_capture_begin(const char *title) override;
169 void debug_capture_end() override;
170 void *debug_capture_scope_create(const char *name) override;
171 bool debug_capture_scope_begin(void *scope) override;
172 void debug_capture_scope_end(void *scope) override;
173
174 void debug_unbind_all_ubo() override;
175 void debug_unbind_all_ssbo() override;
176
177 private:
178 static void orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex, GLuint id);
179 void orphans_clear();
180
181 MEM_CXX_CLASS_ALLOC_FUNCS("GLContext")
182};
183
184} // namespace blender::gpu
static Context * get()
void vao_cache_register(GLVaoCache *cache)
void * debug_capture_scope_create(const char *name) override
Definition gl_debug.cc:514
static bool stencil_texturing_support
Definition gl_context.hh:72
void debug_unbind_all_ubo() override
Definition gl_debug.cc:545
static bool layered_rendering_support
Definition gl_context.hh:66
void debug_capture_end() override
Definition gl_debug.cc:500
void activate() override
static bool debug_layer_support
Definition gl_context.hh:59
bool debug_capture_begin(const char *title) override
Definition gl_debug.cc:480
void debug_group_begin(const char *name, int index) override
Definition gl_debug.cc:371
static bool framebuffer_fetch_support
Definition gl_context.hh:61
static void buffer_free(GLuint buf_id)
void fbo_free(GLuint fbo_id)
static void shader_free(GLuint shader_id)
void flush() override
void debug_capture_scope_end(void *scope) override
Definition gl_debug.cc:533
static GLint max_ssbo_binds
Definition gl_context.hh:55
void vao_free(GLuint vao_id)
static bool debug_layer_workaround
Definition gl_context.hh:79
static bool vertex_shader_layer_support
Definition gl_context.hh:68
static GLStateManager * state_manager_active_get()
static void texture_free(GLuint tex_id)
void debug_group_end() override
Definition gl_debug.cc:400
void vao_cache_unregister(GLVaoCache *cache)
static GLint max_ubo_binds
Definition gl_context.hh:54
static void check_error(const char *info)
void begin_frame() override
static bool direct_state_access_support
Definition gl_context.hh:60
static GLContext * get()
static bool texture_filter_anisotropic_support
Definition gl_context.hh:74
void deactivate() override
static bool unused_fb_slot_workaround
Definition gl_context.hh:80
void memory_statistics_get(int *r_total_mem, int *r_free_mem) override
bool debug_capture_scope_begin(void *scope) override
Definition gl_debug.cc:519
static bool multi_bind_support
Definition gl_context.hh:70
static GLint max_cubemap_size
Definition gl_context.hh:53
void finish() override
static bool texture_barrier_support
Definition gl_context.hh:73
static bool native_barycentric_support
Definition gl_context.hh:69
static bool multi_bind_image_support
Definition gl_context.hh:71
void debug_unbind_all_ssbo() override
Definition gl_debug.cc:550
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
static bool vertex_shader_viewport_index_support
Definition gl_context.hh:67
static bool derivative_control_support
Definition gl_context.hh:75
__int64 int64_t
Definition BKE_text.h:90
unsigned short uint16_t
Definition BKE_text.h:80