Blender V5.0
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::gpu {
24
25class GPULogParser;
26class Context;
27
28/* Set to 1 to log the full source of shaders that fail to compile. */
29#define DEBUG_LOG_SHADER_SRC_ON_ERROR 0
30
36#define SOURCES_INDEX_VERSION 0
37#define SOURCES_INDEX_SPECIALIZATION_CONSTANTS 1
38
43class Shader {
44 public:
49
50 /* Default specialization constants state as defined inside ShaderCreateInfo.
51 * Should be considered as const after init(). */
52 std::unique_ptr<const shader::SpecializationConstants> constants;
53
54 /* WORKAROUND: True if this shader is a polyline shader and needs an appropriate setup to render.
55 * Eventually, in the future, we should modify the user code instead of relying on such hacks. */
56 bool is_polyline = false;
57
58 protected:
60 char name[64];
61
62 /* Parent shader can be used for shaders which are derived from the same source material.
63 * The child shader can pull information from its parent to prepare additional resources
64 * such as PSOs upfront. This enables asynchronous PSO compilation which mitigates stuttering
65 * when updating new materials. */
67
68 public:
69 Shader(const char *name);
70 virtual ~Shader();
71
72 /* TODO: Remove `is_batch_compilation`. */
73 virtual void init(const shader::ShaderCreateInfo &info, bool is_batch_compilation) = 0;
74
79 virtual bool finalize(const shader::ShaderCreateInfo *info = nullptr) = 0;
80 /* Pre-warms PSOs using parent shader's cached PSO descriptors. Limit specifies maximum PSOs to
81 * warm. If -1, compiles all PSO permutations in parent shader.
82 *
83 * See `GPU_shader_warm_cache(..)` in `GPU_shader.hh` for more information. */
84 virtual void warm_cache(int limit) = 0;
85
86 virtual void bind(const shader::SpecializationConstants *constants_state) = 0;
87 virtual void unbind() = 0;
88
89 virtual void uniform_float(int location, int comp_len, int array_size, const float *data) = 0;
90 virtual void uniform_int(int location, int comp_len, int array_size, const int *data) = 0;
91
92 /* Add specialization constant declarations to shader instance. */
94
95 std::string defines_declare(const shader::ShaderCreateInfo &info) const;
96 virtual std::string resources_declare(const shader::ShaderCreateInfo &info) const = 0;
97 virtual std::string vertex_interface_declare(const shader::ShaderCreateInfo &info) const = 0;
98 virtual std::string fragment_interface_declare(const shader::ShaderCreateInfo &info) const = 0;
99 virtual std::string geometry_interface_declare(const shader::ShaderCreateInfo &info) const = 0;
100 virtual std::string geometry_layout_declare(const shader::ShaderCreateInfo &info) const = 0;
101 virtual std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const = 0;
102
104 {
105 return name;
106 }
107
108 void parent_set(Shader *parent)
109 {
110 parent_shader_ = parent;
111 }
112
114 {
115 return parent_shader_;
116 }
117
119 static void set_srgb_uniform(Context *ctx, gpu::Shader *shader);
120 static void set_framebuffer_srgb_target(int use_srgb_to_linear);
121
122 protected:
123 void print_log(Span<StringRefNull> sources,
124 const char *log,
125 const char *stage,
126 bool error,
127 GPULogParser *parser);
128};
129
131 struct Sources {
132 std::string vert;
133 std::string geom;
134 std::string frag;
135 std::string comp;
136 };
137
138 struct Batch {
139 Vector<Shader *> shaders;
141
142 Vector<ShaderSpecialization> specializations;
143
144 std::atomic<int> pending_compilations = 0;
145
146 bool is_specialization_batch()
147 {
148 return !specializations.is_empty();
149 }
150
151 bool is_ready()
152 {
153 BLI_assert(pending_compilations >= 0);
154 return pending_compilations == 0;
155 }
156
157 void free_shaders()
158 {
159 for (Shader *shader : shaders) {
160 if (shader) {
162 }
163 }
164 shaders.clear();
165 }
166 };
168 std::mutex mutex_;
169 std::condition_variable compilation_finished_notification_;
170
171 struct ParallelWork {
172 Batch *batch = nullptr;
173 int shader_index = 0;
174 };
175
176 struct CompilationQueue {
177 std::deque<ParallelWork> low_priority;
178 std::deque<ParallelWork> normal_priority;
179 std::deque<ParallelWork> high_priority;
180
181 void push(ParallelWork &&work, CompilationPriority priority)
182 {
183 switch (priority) {
185 low_priority.push_back(work);
186 break;
188 normal_priority.push_back(work);
189 break;
191 high_priority.push_back(work);
192 break;
193 default:
195 break;
196 }
197 }
198
199 ParallelWork pop()
200 {
201 if (!high_priority.empty()) {
202 ParallelWork work = high_priority.front();
203 high_priority.pop_front();
204 return work;
205 }
206 if (!normal_priority.empty()) {
207 ParallelWork work = normal_priority.front();
208 normal_priority.pop_front();
209 return work;
210 }
211 if (!low_priority.empty()) {
212 ParallelWork work = low_priority.front();
213 low_priority.pop_front();
214 return work;
215 }
217 return {};
218 }
219
220 bool is_empty()
221 {
222 return low_priority.empty() && normal_priority.empty() && high_priority.empty();
223 }
224
225 void remove_batch(Batch *batch)
226 {
227 auto remove = [](std::deque<ParallelWork> &queue, Batch *batch) {
228 for (ParallelWork &work : queue) {
229 if (work.batch == batch) {
230 work = {};
231 batch->pending_compilations--;
232 }
233 }
234
235 queue.erase(std::remove_if(queue.begin(),
236 queue.end(),
237 [](const ParallelWork &work) { return !work.batch; }),
238 queue.end());
239 };
240
241 remove(low_priority, batch);
242 remove(normal_priority, batch);
243 remove(high_priority, batch);
244 }
245 };
246 CompilationQueue compilation_queue_;
247
248 std::unique_ptr<GPUWorker> compilation_worker_;
249
250 bool support_specializations_;
251
252 void *pop_work();
253 void do_work(void *work_payload);
254
255 BatchHandle next_batch_handle_ = 1;
256
257 bool is_compiling_impl();
258
259 protected:
260 /* Must be called earlier from the destructor of the subclass if the compilation process relies
261 * on subclass resources. */
263 {
264 compilation_worker_.reset();
265 }
266
267 public:
268 ShaderCompiler(uint32_t threads_count = 1,
270 bool support_specializations = false);
271 virtual ~ShaderCompiler();
272
273 Shader *compile(const shader::ShaderCreateInfo &info, bool is_batch_compilation);
274
275 virtual Shader *compile_shader(const shader::ShaderCreateInfo &info);
276 virtual void specialize_shader(ShaderSpecialization & /*specialization*/) {};
277
279 CompilationPriority priority);
280 void batch_cancel(BatchHandle &handle);
281 bool batch_is_ready(BatchHandle handle);
283
285 CompilationPriority priority);
286
288
289 bool is_compiling();
290 void wait_for_all();
291};
292
299
300struct LogCursor {
301 int source = -1;
302 int row = -1;
303 int column = -1;
305};
306
311
313 public:
314 virtual const char *parse_line(const char *source_combined,
315 const char *log_line,
316 GPULogItem &log_item) = 0;
317
318 protected:
319 const char *skip_severity(const char *log_line,
320 GPULogItem &log_item,
321 const char *error_msg,
322 const char *warning_msg,
323 const char *note_msg) const;
324 const char *skip_separators(const char *log_line, const StringRef separators) const;
325 const char *skip_until(const char *log_line, char stop_char) const;
326 bool at_number(const char *log_line) const;
327 bool at_any(const char *log_line, const StringRef chars) const;
328 int parse_number(const char *log_line, const char **r_new_position) const;
329
330 static size_t line_start_get(StringRefNull source_combined, size_t target_line);
331 static StringRef filename_get(StringRefNull source_combined, size_t pos);
332 static size_t source_line_get(StringRefNull source_combined, size_t pos);
333
335};
336
337void printf_begin(Context *ctx);
338void printf_end(Context *ctx);
339
340} // namespace blender::gpu
341
342/* XXX do not use it. Special hack to use OCIO with batch API. */
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
void GPU_shader_free(blender::gpu::Shader *shader)
int64_t BatchHandle
Definition GPU_shader.hh:82
CompilationPriority
Definition GPU_shader.hh:80
int64_t SpecializationBatchHandle
BMesh const char void * data
bool is_empty() const
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
void batch_cancel(BatchHandle &handle)
BatchHandle batch_compile(Span< const shader::ShaderCreateInfo * > &infos, CompilationPriority priority)
bool specialization_batch_is_ready(SpecializationBatchHandle &handle)
Vector< Shader * > batch_finalize(BatchHandle &handle)
Shader * compile(const shader::ShaderCreateInfo &info, bool is_batch_compilation)
virtual void specialize_shader(ShaderSpecialization &)
bool batch_is_ready(BatchHandle handle)
ShaderCompiler(uint32_t threads_count=1, GPUWorker::ContextType context_type=GPUWorker::ContextType::PerThread, bool support_specializations=false)
SpecializationBatchHandle precompile_specializations(Span< ShaderSpecialization > specializations, CompilationPriority priority)
virtual Shader * compile_shader(const shader::ShaderCreateInfo &info)
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::string defines_declare(const shader::ShaderCreateInfo &info) const
Definition gpu_shader.cc:35
virtual void compute_shader_from_glsl(MutableSpan< StringRefNull > sources)=0
virtual void init(const shader::ShaderCreateInfo &info, bool is_batch_compilation)=0
virtual void bind(const shader::SpecializationConstants *constants_state)=0
std::unique_ptr< const shader::SpecializationConstants > constants
virtual void vertex_shader_from_glsl(MutableSpan< StringRefNull > sources)=0
virtual std::string compute_layout_declare(const shader::ShaderCreateInfo &info) const =0
Shader * parent_get() const
virtual void fragment_shader_from_glsl(MutableSpan< StringRefNull > sources)=0
virtual bool finalize(const shader::ShaderCreateInfo *info=nullptr)=0
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 geometry_shader_from_glsl(MutableSpan< StringRefNull > sources)=0
virtual void warm_cache(int limit)=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 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)
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)
Definition gpu_shader.cc:57
static void set_srgb_uniform(Context *ctx, gpu::Shader *shader)
struct @021025263243242147216143265077100330027142264337::@225245033123204053237120173316075113304004012000 batch
uint pos
#define log
blender::gpu::Shader * immGetShader()
static void error(const char *str)
void printf_begin(Context *ctx)
void printf_end(Context *ctx)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...