Blender V5.0
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 {
24namespace gpu {
25
26class GLVaoCache;
27
29 class OrphanList {
31 std::mutex mutex_;
33 Vector<GLuint> handles_;
34
35 public:
36 void clear(FunctionRef<void(GLuint, GLuint *)> free_fn);
37 void append(GLuint handle);
38 };
39
40 public:
42 OrphanList textures;
43 OrphanList buffers;
44 OrphanList shaders;
45 OrphanList programs;
46
47 void orphans_clear();
48};
49
50class GLContext : public Context {
51 public:
53
54 static GLint max_cubemap_size;
55 static GLint max_ubo_binds;
56 static GLint max_ssbo_binds;
57
59
66 static bool multi_bind_support;
71
73
77
80
84
85 private:
90 Set<GLVaoCache *> vao_caches_;
91 Set<gpu::FrameBuffer *> framebuffers_;
93 std::mutex lists_mutex_;
95 Vector<GLuint> orphaned_vertarrays_;
96 Vector<GLuint> orphaned_framebuffers_;
98 GLSharedOrphanLists &shared_orphan_list_;
99
100 struct TimeQuery {
101 std::string name;
102 union {
103 GLuint handles[2];
104 struct {
105 GLuint handle_start, handle_end;
106 };
107 };
108 bool finished;
109 int64_t cpu_start;
110 int64_t cpu_end;
111 };
112 struct FrameQueries {
113 Vector<TimeQuery> queries;
114 };
115 Vector<FrameQueries> frame_timings;
116
117 void process_frame_timings();
118
119 public:
120 GLContext(void *ghost_window, GLSharedOrphanLists &shared_orphan_list);
121 ~GLContext();
122
123 static void check_error(const char *info);
124
125 void activate() override;
126 void deactivate() override;
127 void begin_frame() override;
128 void end_frame() override;
129
130 void flush() override;
131 void finish() override;
132
133 void memory_statistics_get(int *r_total_mem, int *r_free_mem) override;
134
135 static GLContext *get()
136 {
137 return static_cast<GLContext *>(Context::get());
138 }
139
141 {
142 GLContext *ctx = GLContext::get();
143 return static_cast<GLStateManager *>(ctx->state_manager);
144 };
145
146 /* These need to be called with the context the id was created with. */
147 void vao_free(GLuint vao_id);
148 void fbo_free(GLuint fbo_id);
149 /* These can be called by any threads even without OpenGL ctx. Deletion will be delayed. */
150 static void buffer_free(GLuint buf_id);
151 static void texture_free(GLuint tex_id);
152 static void shader_free(GLuint shader_id);
153 static void program_free(GLuint program_id);
154
155 void vao_cache_register(GLVaoCache *cache);
156 void vao_cache_unregister(GLVaoCache *cache);
157
158 void debug_group_begin(const char *name, int index) override;
159 void debug_group_end() override;
160 bool debug_capture_begin(const char *title) override;
161 void debug_capture_end() override;
162 void *debug_capture_scope_create(const char *name) override;
163 bool debug_capture_scope_begin(void *scope) override;
164 void debug_capture_scope_end(void *scope) override;
165
166 void debug_unbind_all_ubo() override;
167 void debug_unbind_all_ssbo() override;
168
169 private:
170 static void orphans_add(Vector<GLuint> &orphan_list, std::mutex &list_mutex, GLuint id);
171 void orphans_clear();
172
173 MEM_CXX_CLASS_ALLOC_FUNCS("GLContext")
174};
175
176} // namespace gpu
177} // namespace blender
long long int int64_t
static Context * get()
void * debug_capture_scope_create(const char *name) override
Definition gl_debug.cc:512
static bool stencil_texturing_support
Definition gl_context.hh:68
void debug_unbind_all_ubo() override
Definition gl_debug.cc:543
static bool layered_rendering_support
Definition gl_context.hh:64
void debug_capture_end() override
Definition gl_debug.cc:498
static bool debug_layer_support
Definition gl_context.hh:60
bool debug_capture_begin(const char *title) override
Definition gl_debug.cc:478
void debug_group_begin(const char *name, int index) override
Definition gl_debug.cc:369
static bool framebuffer_fetch_support
Definition gl_context.hh:63
void memory_statistics_get(int *r_total_mem, int *r_free_mem) override
static bool explicit_location_support
Definition gl_context.hh:62
static void buffer_free(GLuint buf_id)
void debug_capture_scope_end(void *scope) override
Definition gl_debug.cc:531
static GLint max_ssbo_binds
Definition gl_context.hh:56
static bool debug_layer_workaround
Definition gl_context.hh:74
static GLStateManager * state_manager_active_get()
void begin_frame() override
void debug_group_end() override
Definition gl_debug.cc:398
void activate() override
void flush() override
static void shader_free(GLuint shader_id)
static GLint max_ubo_binds
Definition gl_context.hh:55
static void check_error(const char *info)
GLContext(void *ghost_window, GLSharedOrphanLists &shared_orphan_list)
Definition gl_context.cc:36
void vao_cache_unregister(GLVaoCache *cache)
static bool direct_state_access_support
Definition gl_context.hh:61
static GLContext * get()
void finish() override
static bool texture_filter_anisotropic_support
Definition gl_context.hh:70
static void texture_free(GLuint tex_id)
static bool unused_fb_slot_workaround
Definition gl_context.hh:75
void deactivate() override
bool debug_capture_scope_begin(void *scope) override
Definition gl_debug.cc:517
static bool multi_bind_support
Definition gl_context.hh:66
static GLint max_cubemap_size
Definition gl_context.hh:54
void vao_free(GLuint vao_id)
static bool texture_barrier_support
Definition gl_context.hh:69
static bool native_barycentric_support
Definition gl_context.hh:65
static void program_free(GLuint program_id)
static bool multi_bind_image_support
Definition gl_context.hh:67
void end_frame() override
static bool generate_mipmap_workaround
Definition gl_context.hh:76
void debug_unbind_all_ssbo() override
Definition gl_debug.cc:548
void vao_cache_register(GLVaoCache *cache)
void fbo_free(GLuint fbo_id)
static void clear(Message &msg)
Definition msgfmt.cc:213
const char * name
ParamHandle ** handles