Blender V5.3
gpu_shader_private.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_map.hh"
12#include "BLI_span.hh"
13#include "BLI_string_ref.hh"
14
15#include "GPU_shader.hh"
16#include "GPU_worker.hh"
19
20#include <deque>
21#include <string>
22
23namespace blender {
24
25namespace gpu {
26
27class GPULogParser;
28class Context;
29
30/* Set to 1 to log the full source of shaders that fail to compile. */
31#define DEBUG_LOG_SHADER_SRC_ON_ERROR 0
32
38#define SOURCES_INDEX_VERSION 0
39#define SOURCES_INDEX_SPECIALIZATION_CONSTANTS 1
40
47
52class Shader {
53 public:
58
59 /* Default specialization constants state as defined inside ShaderCreateInfo.
60 * Should be considered as const after init(). */
61 std::unique_ptr<const shader::SpecializationConstants> constants;
62
63 /* WORKAROUND: True if this shader is a polyline shader and needs an appropriate setup to render.
64 * Eventually, in the future, we should modify the user code instead of relying on such hacks. */
65 bool is_polyline = false;
66 /* WORKAROUND: Skip preprocessor for shader that we can't ensure the content.
67 * This avoid crashes until we make the preprocessor robust to errors.
68 * Also the preprocessor doesn't support forward declaration at the moment and needs manual
69 * tweaking. */
70 bool skip_preprocessor = false;
71
72 protected:
74 char name[64];
75
76 /* Parent shader can be used for shaders which are derived from the same source material.
77 * The child shader can pull information from its parent to prepare additional resources
78 * such as PSOs upfront. This enables asynchronous PSO compilation which mitigates stuttering
79 * when updating new materials. */
81
82 /* In some situation, a backend might want to transform the create infos before it is being
83 * parsed. */
84 std::unique_ptr<PatchedShaderCreateInfo> patched_info_;
85
86 public:
87 Shader(const char *name);
88 virtual ~Shader();
89
90 virtual void init(const shader::ShaderCreateInfo &info, bool is_codegen_only) = 0;
91
92 /* Patch create infos for any additional resources that could be needed. */
94 const shader::ShaderCreateInfo &original_info) = 0;
95
97 MutableSpan<StringRefNull> sources) = 0;
99 MutableSpan<StringRefNull> sources) = 0;
101 MutableSpan<StringRefNull> sources) = 0;
103 MutableSpan<StringRefNull> sources) = 0;
104 virtual bool finalize(const shader::ShaderCreateInfo *info = nullptr) = 0;
105 /* Pre-warms PSOs using parent shader's cached PSO descriptors. Limit specifies maximum PSOs to
106 * warm. If -1, compiles all PSO permutations in parent shader.
107 *
108 * See `GPU_shader_warm_cache(..)` in `GPU_shader.hh` for more information. */
109 virtual void warm_cache(int limit) = 0;
110
111 virtual void bind(const shader::SpecializationConstants *constants_state) = 0;
112 virtual void unbind() = 0;
113
114 virtual void uniform_float(int location, int comp_len, int array_size, const float *data) = 0;
115 virtual void uniform_int(int location, int comp_len, int array_size, const int *data) = 0;
116
117 /* Add specialization constant declarations to shader instance. */
119
120 static std::string defines_declare(const shader::ShaderCreateInfo &info);
121 virtual std::string resources_declare(const shader::ShaderCreateInfo &info) const = 0;
122 virtual std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const = 0;
123 virtual std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const = 0;
124 virtual std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const = 0;
125 virtual std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const = 0;
126 virtual std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const = 0;
127
129 {
130 return name;
131 }
132
133 void parent_set(Shader *parent)
134 {
135 parent_shader_ = parent;
136 }
137
139 {
140 return parent_shader_;
141 }
142
144 static void set_srgb_uniform(Context *ctx, gpu::Shader *shader);
145 static void set_framebuffer_srgb_target(int use_srgb_to_linear);
146
147 static void dump_source_to_disk(StringRef shader_name,
148 StringRef shader_name_with_stage_name,
149 StringRef extension,
150 StringRef source);
151
152 static std::string run_preprocessor(StringRef source, bool no_dead_code_elimination);
153
154 protected:
155 void print_log(Span<StringRefNull> sources,
156 const char *log,
157 const char *stage,
158 bool error,
160};
161
163 struct Sources {
164 std::string vert;
165 std::string geom;
166 std::string frag;
167 std::string comp;
168 };
169
170 struct AsyncCompilation;
171 struct ParallelWork {
172 ShaderCompiler *compiler = nullptr;
173 ShaderCompiler::AsyncCompilation *compilation = nullptr;
174 WorkID id = 0;
175 };
176
177 struct AsyncCompilation {
178 Shader *shader = nullptr;
179 const shader::ShaderCreateInfo *info = nullptr;
180
181 std::unique_ptr<ShaderSpecialization> specialization = nullptr;
182
183 std::unique_ptr<ParallelWork> work;
184
185 std::atomic<bool> is_ready = false;
186
187 bool is_specialization()
188 {
189 return specialization != nullptr;
190 }
191 };
193 std::mutex mutex_;
194 std::condition_variable compilation_finished_notification_;
195
196 std::unique_ptr<GPUWorker> compilation_worker_;
197
198 bool support_specializations_;
199
200 static void do_work_static_cb(void *payload);
201 void do_work(ParallelWork &work);
202
203 AsyncCompilationHandle next_handle_ = 1;
204
205 bool is_compiling_impl();
206
207 bool is_paused_ = false;
208 std::condition_variable pause_finished_notification_;
209
210 protected:
211 /* Must be called earlier from the destructor of the subclass if the compilation process relies
212 * on subclass resources. */
214 {
215 compilation_worker_.reset();
216 }
217
218 public:
219 ShaderCompiler(uint32_t threads_count = 1,
221 bool support_specializations = false);
222 virtual ~ShaderCompiler();
223
224 Shader *compile(const shader::ShaderCreateInfo &info, bool is_codegen_only);
225
226 virtual Shader *compile_shader(const shader::ShaderCreateInfo &info);
227 virtual void specialize_shader(const ShaderSpecialization & /*specialization*/) {};
228
230 CompilationPriority priority);
234
236 CompilationPriority priority);
238
239 bool is_compiling();
240 void wait_for_all();
241 void pause_all();
242 void continue_all();
243};
244
245enum class Severity {
246 Unknown,
247 Warning,
250};
251
252struct LogCursor {
253 int source = -1;
254 int row = -1;
255 int column = -1;
257};
258
263
265 public:
266 virtual const char *parse_line(const char *source_combined,
267 const char *log_line,
268 GPULogItem &log_item) = 0;
269
270 protected:
271 const char *skip_severity(const char *log_line,
272 GPULogItem &log_item,
273 const char *error_msg,
274 const char *warning_msg,
275 const char *note_msg) const;
276 const char *skip_separators(const char *log_line, const StringRef separators) const;
277 const char *skip_until(const char *log_line, char stop_char) const;
278 bool at_number(const char *log_line) const;
279 bool at_any(const char *log_line, const StringRef chars) const;
280 int parse_number(const char *log_line, const char **r_new_position) const;
281
282 static size_t line_start_get(StringRefNull source_combined, size_t target_line);
283 static StringRef filename_get(StringRefNull source_combined, size_t pos);
284 static size_t source_line_get(StringRefNull source_combined, size_t pos);
285
287};
288
289void printf_begin(Context *ctx);
290void printf_end(Context *ctx);
291
292} // namespace gpu
293
294/* XXX do not use it. Special hack to use OCIO with batch API. */
296
297} // namespace blender
static size_t line_start_get(StringRefNull source_combined, size_t target_line)
static StringRef filename_get(StringRefNull source_combined, size_t pos)
int parse_number(const char *log_line, const char **r_new_position) const
const char * skip_separators(const char *log_line, const StringRef separators) const
bool at_number(const char *log_line) const
static size_t source_line_get(StringRefNull source_combined, size_t pos)
bool at_any(const char *log_line, const StringRef chars) const
virtual const char * parse_line(const char *source_combined, const char *log_line, GPULogItem &log_item)=0
const char * skip_until(const char *log_line, char stop_char) const
MEM_CXX_CLASS_ALLOC_FUNCS("GPULogParser")
const char * skip_severity(const char *log_line, GPULogItem &log_item, const char *error_msg, const char *warning_msg, const char *note_msg) const
AsyncCompilationHandle async_compilation(const shader::ShaderCreateInfo *info, CompilationPriority priority)
bool async_compilation_is_ready(AsyncCompilationHandle handle)
Shader * compile(const shader::ShaderCreateInfo &info, bool is_codegen_only)
virtual void specialize_shader(const ShaderSpecialization &)
bool async_specialization_is_ready(AsyncSpecializationHandle &handle)
void async_compilation_cancel(AsyncCompilationHandle &handle)
Shader * async_compilation_finalize(AsyncCompilationHandle &handle)
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)
AsyncSpecializationHandle async_specialization(const ShaderSpecialization &specialization, CompilationPriority priority)
virtual void unbind()=0
virtual std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const =0
virtual void uniform_int(int location, int comp_len, int array_size, const int *data)=0
std::unique_ptr< PatchedShaderCreateInfo > patched_info_
static void dump_source_to_disk(StringRef shader_name, StringRef shader_name_with_stage_name, StringRef extension, StringRef source)
Definition gpu_shader.cc:43
virtual void bind(const shader::SpecializationConstants *constants_state)=0
std::unique_ptr< const shader::SpecializationConstants > constants
virtual void geometry_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources)=0
virtual std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const =0
virtual void init(const shader::ShaderCreateInfo &info, bool is_codegen_only)=0
Shader * parent_get() const
virtual void fragment_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources)=0
virtual bool finalize(const shader::ShaderCreateInfo *info=nullptr)=0
static std::string defines_declare(const shader::ShaderCreateInfo &info)
Definition gpu_shader.cc:91
StringRefNull name_get() const
virtual std::string resources_declare(const shader::ShaderCreateInfo &info) const =0
virtual std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const =0
void specialization_constants_init(const shader::ShaderCreateInfo &info)
virtual void warm_cache(int limit)=0
virtual void compute_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources)=0
void parent_set(Shader *parent)
virtual std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const =0
static void set_scene_linear_to_xyz_uniform(gpu::Shader *shader)
virtual void vertex_shader_from_glsl(const shader::ShaderCreateInfo &info, MutableSpan< StringRefNull > sources)=0
static std::string run_preprocessor(StringRef source, bool no_dead_code_elimination)
virtual void uniform_float(int location, int comp_len, int array_size, const float *data)=0
void print_log(Span< StringRefNull > sources, const char *log, const char *stage, bool error, GPULogParser *parser)
virtual const shader::ShaderCreateInfo & patch_create_info(const shader::ShaderCreateInfo &original_info)=0
static void set_framebuffer_srgb_target(int use_srgb_to_linear)
virtual std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const =0
Shader(const char *name)
static void set_srgb_uniform(Context *ctx, gpu::Shader *shader)
#define log
Vector< std::unique_ptr< std::string >, 0 > ShaderCreateInfoStringCache
void printf_begin(Context *ctx)
uint64_t WorkID
Definition GPU_worker.hh:25
void printf_end(Context *ctx)
BMesh Iterators.
CompilationPriority
Definition GPU_shader.hh:85
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
int64_t AsyncSpecializationHandle
int64_t AsyncCompilationHandle
Definition GPU_shader.hh:87
gpu::Shader * immGetShader()
shader::ShaderCreateInfoStringCache names
PatchedShaderCreateInfo(const shader::ShaderCreateInfo &info_)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...