Blender V5.3
gl_backend.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
11#include "GPU_capabilities.hh"
12#include "GPU_platform.hh"
13#include "GPU_work_in_flight.hh"
14
15#include "gpu_backend.hh"
16
17#include "BLI_threads.hh"
18#include "BLI_vector.hh"
19
21
22#ifdef WITH_RENDERDOC
23# include "renderdoc_api.hh"
24#endif
25
26#include "gl_batch.hh"
28#include "gl_compute.hh"
29#include "gl_context.hh"
30#include "gl_framebuffer.hh"
31#include "gl_index_buffer.hh"
32#include "gl_query.hh"
33#include "gl_shader.hh"
34#include "gl_storage_buffer.hh"
35#include "gl_texture.hh"
36#include "gl_texture_pool.hh"
37#include "gl_uniform_buffer.hh"
38#include "gl_vertex_buffer.hh"
39#include "gl_work_in_flight.hh"
40
41namespace blender::gpu {
42
43class GLBackend : public GPUBackend {
44 private:
45 GLSharedOrphanLists shared_orphan_list_;
46#ifdef WITH_RENDERDOC
48#endif
49
50 Set<int> valid_contexts_;
51 std::mutex valid_contexts_mutex_;
52
53 public:
55 {
56 /* platform_init needs to go first. */
57 GLBackend::platform_init();
58
59 GLBackend::capabilities_init();
60 GLBackend::log_extensions();
61 GLBackend::log_workarounds();
63 }
65 {
66 GLBackend::platform_exit();
67 }
68
69 void init_resources() override
70 {
72 compiler_ = MEM_new<GLSubprocessShaderCompiler>(__func__);
73 }
74 else {
75 compiler_ = MEM_new<GLShaderCompiler>(__func__);
76 }
77 };
78
79 void delete_resources() override
80 {
81 /* Delete any resources with context active. */
83 MEM_delete(compiler_);
84 }
85
86 static GLBackend *get()
87 {
88 return static_cast<GLBackend *>(GPUBackend::get());
89 }
90
91 Context *context_alloc(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context) override
92 {
93 return new GLContext(ghost_window, ghost_context, shared_orphan_list_);
94 };
95
96 void add_context_id(int context_id)
97 {
98 std::lock_guard lock(valid_contexts_mutex_);
99 valid_contexts_.add(context_id);
100 }
101
102 void remove_context_id(int context_id)
103 {
104 std::lock_guard lock(valid_contexts_mutex_);
105 valid_contexts_.remove(context_id);
106 }
107
108 bool is_valid_context_id(int context_id)
109 {
110 std::lock_guard lock(valid_contexts_mutex_);
111 return valid_contexts_.contains(context_id);
112 }
113
114 Batch *batch_alloc() override
115 {
116 return new GLBatch();
117 };
118
119 Fence *fence_alloc() override
120 {
121 return new GLFence();
122 };
123
124 WorkInFlight *work_in_flight_alloc(unsigned int max_in_flight) override
125 {
126 return new GLWorkInFlight(max_in_flight);
127 };
128
129 FrameBuffer *framebuffer_alloc(const char *name) override
130 {
131 return new GLFrameBuffer(name);
132 };
133
135 {
136 return new GLIndexBuf();
137 };
138
139 PixelBuffer *pixelbuf_alloc(size_t size) override
140 {
141 return new GLPixelBuffer(size);
142 };
143
145 {
146 return new GLQueryPool();
147 };
148
149 Shader *shader_alloc(const char *name) override
150 {
151 return new GLShader(name);
152 };
153
154 Texture *texture_alloc(const char *name) override
155 {
156 return new GLTexture(name);
157 };
158
159 TexturePool *texturepool_alloc() override;
160
161 UniformBuf *uniformbuf_alloc(size_t size, const char *name) override
162 {
163 return new GLUniformBuf(size, name);
164 };
165
166 StorageBuf *storagebuf_alloc(size_t size, GPUUsageType usage, const char *name) override
167 {
168 return new GLStorageBuf(size, usage, name);
169 };
170
172 {
173 return new GLVertBuf();
174 };
175
176 TopLevelAS *tlas_alloc(const char * /*name*/) override
177 {
178 /* OpenGL doesn't support Ray Queries. */
180 return nullptr;
181 }
182 BottomLevelAS *blas_alloc(const char * /*name*/) override
183 {
184 /* OpenGL doesn't support Ray Queries. */
186 return nullptr;
187 }
188
190 {
191 return shared_orphan_list_;
192 };
193
194 void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len) override
195 {
197 GLCompute::dispatch(groups_x_len, groups_y_len, groups_z_len);
198 }
199
200 void compute_dispatch_indirect(StorageBuf *indirect_buf) override
201 {
203
204 dynamic_cast<GLStorageBuf *>(indirect_buf)->bind_as(GL_DISPATCH_INDIRECT_BUFFER);
205 /* This barrier needs to be here as it only work on the currently bound indirect buffer. */
206 glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
207
208 glDispatchComputeIndirect(GLintptr(0));
209 /* Unbind. */
210 glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0);
211 }
212
214 {
215#if BLI_SUBPROCESS_SUPPORT
216 GL_shader_cache_dir_clear_old();
217#endif
218 }
219
220 /* Render Frame Coordination */
221 void render_begin() override {};
222 void render_end() override {};
223 void render_step(bool /*force_resource_release*/) override {};
224
225 bool debug_capture_begin(const char *title);
226 void debug_capture_end();
227
228 private:
229 static void platform_init();
230 static void platform_exit();
231
232 static void capabilities_init();
233
234 static void log_extensions();
235 static void log_workarounds();
236};
237
238} // namespace blender::gpu
volatile int lock
#define BLI_assert_unreachable()
GLSharedOrphanLists & shared_orphan_list_get()
Fence * fence_alloc() override
WorkInFlight * work_in_flight_alloc(unsigned int max_in_flight) override
void compute_dispatch_indirect(StorageBuf *indirect_buf) override
Shader * shader_alloc(const char *name) override
IndexBuf * indexbuf_alloc() override
StorageBuf * storagebuf_alloc(size_t size, GPUUsageType usage, const char *name) override
void shader_cache_dir_clear_old() override
void init_resources() override
Definition gl_backend.hh:69
bool is_valid_context_id(int context_id)
VertBuf * vertbuf_alloc() override
Texture * texture_alloc(const char *name) override
void remove_context_id(int context_id)
void render_end() override
static GLBackend * get()
Definition gl_backend.hh:86
void add_context_id(int context_id)
Definition gl_backend.hh:96
Batch * batch_alloc() override
Context * context_alloc(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context) override
Definition gl_backend.hh:91
void render_begin() override
bool debug_capture_begin(const char *title)
Definition gl_debug.cc:485
TopLevelAS * tlas_alloc(const char *) override
void delete_resources() override
Definition gl_backend.hh:79
BottomLevelAS * blas_alloc(const char *) override
QueryPool * querypool_alloc() override
PixelBuffer * pixelbuf_alloc(size_t size) override
void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len) override
UniformBuf * uniformbuf_alloc(size_t size, const char *name) override
void render_step(bool) override
FrameBuffer * framebuffer_alloc(const char *name) override
TexturePool * texturepool_alloc() override
static void dispatch(int group_x_len, int group_y_len, int group_z_len)
Definition gl_compute.cc:15
static GLStateManager * state_manager_active_get()
static GLContext * get()
GLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context, GLSharedOrphanLists &shared_orphan_list)
Definition gl_context.cc:35
GLFrameBuffer(const char *name)
GLShader(const char *name)
Definition gl_shader.cc:64
void apply_state() override
Definition gl_state.cc:54
GLStorageBuf(size_t size, GPUUsageType usage, const char *name)
GLTexture(const char *name)
Definition gl_texture.cc:37
static void samplers_init()
static void samplers_free()
GLUniformBuf(size_t size, const char *name)
GLWorkInFlight(unsigned int max_in_flight)
ShaderCompiler * compiler_
static GPUBackend * get()
GPUCapabilities GCaps