Blender V5.3
gl_shader.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 "MEM_guardedalloc.h"
12
13#include <epoxy/gl.h>
14
15#include "BLI_map.hh"
16#include "BLI_subprocess.hh"
17#include "BLI_utility_mixins.hh"
18
19#include "GPU_capabilities.hh"
21#include "gpu_shader_private.hh"
22
23#include <functional>
24#include <mutex>
25
26namespace blender::gpu {
27
38struct GLSource {
39 std::string source;
40 std::optional<StringRefNull> source_ref;
41
42 GLSource() = default;
43 GLSource(StringRefNull other_source);
44};
45class GLSources : public Vector<GLSource> {
46 public:
49 std::string to_string() const;
50};
51
57 std::string comp;
58 std::string vert;
59 std::string geom;
60 std::string frag;
61
62 /* Returns the size (in bytes) required to store the source of all the used stages. */
63 size_t size();
64};
65
69class GLShader : public Shader {
73 friend class GLShaderCompiler;
74
75 private:
76 struct GLProgram {
78 GLuint program_id = 0;
80 GLuint vert_shader = 0;
81 GLuint geom_shader = 0;
82 GLuint frag_shader = 0;
83 GLuint compute_shader = 0;
84
85 std::mutex compilation_mutex;
86
87 GLProgram() {}
88 ~GLProgram();
89
90 void program_link(StringRefNull shader_name);
91 };
92
93 using GLProgramCacheKey = Vector<shader::SpecializationConstant::Value>;
96
97 std::mutex program_cache_mutex_;
98
100 GLProgram *main_program_ = nullptr;
101
102 /* When true, the shader generates its GLSources but it's not compiled.
103 * (Used for subprocess compilation) */
104 bool is_codegen_only_ = false;
105
111 GLSources vertex_sources_;
112 GLSources geometry_sources_;
113 GLSources fragment_sources_;
114 GLSources compute_sources_;
115
116 Vector<const char *> specialization_constant_names_;
117
118 void update_program_and_sources(GLSources &stage_sources, MutableSpan<StringRefNull> sources);
119
124 GLShader::GLProgram &program_get(const shader::SpecializationConstants *constants_state);
125
127 bool compilation_failed_ = false;
128
129 std::string debug_source;
130
131 public:
132 GLShader(const char *name);
133 ~GLShader();
134
135 void init(const shader::ShaderCreateInfo &info, bool is_codegen_only) override;
136
138 const shader::ShaderCreateInfo &original_info) override
139 {
140 return original_info;
141 }
142
145 MutableSpan<StringRefNull> sources) override;
147 MutableSpan<StringRefNull> sources) override;
149 MutableSpan<StringRefNull> sources) override;
151 MutableSpan<StringRefNull> sources) override;
152 bool finalize(const shader::ShaderCreateInfo *info = nullptr) override;
153 bool post_finalize(const shader::ShaderCreateInfo *info = nullptr);
154 void warm_cache(int /*limit*/) override {};
155
156 std::string resources_declare(const shader::ShaderCreateInfo &info) const override;
157 std::string constants_declare(const shader::SpecializationConstants &constants_state) const;
158 std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override;
159 std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const override;
160 std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override;
161 std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const override;
162 std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const override;
163
164 void bind(const shader::SpecializationConstants *constants_state) override;
165 void unbind() override;
166
167 void uniform_float(int location, int comp_len, int array_size, const float *data) override;
168 void uniform_int(int location, int comp_len, int array_size, const int *data) override;
169
170 bool is_compute() const
171 {
172 if (!vertex_sources_.is_empty()) {
173 return false;
174 }
175 if (!compute_sources_.is_empty()) {
176 return true;
177 }
178 return main_program_->compute_shader != 0;
179 }
180
182
183 private:
184 StringRefNull glsl_patch_get(GLenum gl_stage);
185
186 bool has_specialization_constants() const
187 {
188 return constants->types.is_empty() == false;
189 }
190
192 GLuint create_shader_stage(GLenum gl_stage,
194 GLSources &gl_sources,
195 const shader::SpecializationConstants &constants_state);
196
201 std::string workaround_geometry_shader_source_create(const shader::ShaderCreateInfo &info);
202
203 bool do_geometry_shader_injection(const shader::ShaderCreateInfo *info) const;
204
205 MEM_CXX_CLASS_ALLOC_FUNCS("GLShader");
206};
207
209 public:
211 : ShaderCompiler(GPU_max_parallel_compilations(), GPUWorker::ContextType::PerThread, true) {
212 };
213
214 virtual void specialize_shader(const ShaderSpecialization &specialization) override;
215};
216
217#if BLI_SUBPROCESS_SUPPORT
218
219class GLCompilerWorker {
220 friend class GLSubprocessShaderCompiler;
221
222 private:
223 BlenderSubprocess subprocess_;
224 std::unique_ptr<SharedMemory> shared_mem_;
225 std::unique_ptr<SharedSemaphore> start_semaphore_;
226 std::unique_ptr<SharedSemaphore> end_semaphore_;
227 std::unique_ptr<SharedSemaphore> close_semaphore_;
228 enum State {
229 /* The worker has been acquired and the compilation has been requested. */
230 COMPILATION_REQUESTED,
231 /* The shader binary result is ready to be read. */
232 COMPILATION_READY,
233 /* The binary result has been loaded into a program and the worker can be released. */
234 COMPILATION_FINISHED,
235 /* The worker is not currently in use and can be acquired. */
236 AVAILABLE
237 };
238 std::atomic<State> state_ = AVAILABLE;
239 double compilation_start = 0;
240
241 GLCompilerWorker();
242 ~GLCompilerWorker();
243
244 void compile(const GLSourcesBaked &sources);
245 bool block_until_ready();
246 bool load_program_binary(GLint program);
247 void release();
248
249 /* Check if the process may have closed/crashed/hanged. */
250 bool is_lost();
251};
252
254 private:
255 Vector<GLCompilerWorker *> workers_;
256 std::mutex workers_mutex_;
257
258 GLCompilerWorker *get_compiler_worker();
259
260 GLShader::GLProgram *specialization_program_get(ShaderSpecialization &specialization);
261
262 public:
263 GLSubprocessShaderCompiler()
264 : ShaderCompiler(GPU_max_parallel_compilations(), GPUWorker::ContextType::PerThread, true) {
265 };
266 virtual ~GLSubprocessShaderCompiler() override;
267
268 virtual Shader *compile_shader(const shader::ShaderCreateInfo &info) override;
269 virtual void specialize_shader(const ShaderSpecialization &specialization) override;
270};
271
272#else
273
275
276#endif
277
278class GLLogParser : public GPULogParser {
279 public:
280 const char *parse_line(const char *source_combined,
281 const char *log_line,
282 GPULogItem &log_item) override;
283
284 protected:
285 const char *skip_severity_prefix(const char *log_line, GPULogItem &log_item);
286 const char *skip_severity_keyword(const char *log_line, GPULogItem &log_item);
287
289};
290
291} // namespace blender::gpu
Read Blender memory allocation and freeing.
NonCopyable(const NonCopyable &other)=delete
const char * parse_line(const char *source_combined, const char *log_line, GPULogItem &log_item) override
const char * skip_severity_prefix(const char *log_line, GPULogItem &log_item)
const char * skip_severity_keyword(const char *log_line, GPULogItem &log_item)
MEM_CXX_CLASS_ALLOC_FUNCS("GLLogParser")
virtual void specialize_shader(const ShaderSpecialization &specialization) override
bool is_compute() const
Definition gl_shader.hh:170
GLSourcesBaked get_sources()
std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:691
std::string resources_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:567
std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:754
void vertex_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
void compute_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:878
bool finalize(const shader::ShaderCreateInfo *info=nullptr) override
void uniform_float(int location, int comp_len, int array_size, const float *data) override
void fragment_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
void geometry_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources) override
bool post_finalize(const shader::ShaderCreateInfo *info=nullptr)
GLShader(const char *name)
Definition gl_shader.cc:64
friend class GLShaderCompiler
Definition gl_shader.hh:73
friend class GLSubprocessShaderCompiler
Definition gl_shader.hh:72
void bind(const shader::SpecializationConstants *constants_state) override
void uniform_int(int location, int comp_len, int array_size, const int *data) override
std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:921
void warm_cache(int) override
Definition gl_shader.hh:154
const shader::ShaderCreateInfo & patch_create_info(const shader::ShaderCreateInfo &original_info) override
Definition gl_shader.hh:137
std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const override
Definition gl_shader.cc:897
void unbind() override
std::string constants_declare(const shader::SpecializationConstants &constants_state) const
Definition gl_shader.cc:641
Vector< StringRefNull > sources_get() const
std::string to_string() const
GLSources & operator=(Span< StringRefNull > other)
virtual void specialize_shader(const ShaderSpecialization &)
ShaderCompiler(uint32_t threads_count=1, GPUWorker::ContextType context_type=GPUWorker::ContextType::PerThread, bool support_specializations=false)
virtual Shader * compile_shader(const shader::ShaderCreateInfo &info)
std::unique_ptr< const shader::SpecializationConstants > constants
Shader(const char *name)
BMesh const char void * data
int GPU_max_parallel_compilations()
std::optional< StringRefNull > source_ref
Definition gl_shader.hh:40
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...