Blender V5.3
gpu_pass.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_map.hh"
14#include "BLI_span.hh"
15#include "BLI_time.hh"
16#include "BLI_vector.hh"
17
18#include "GPU_capabilities.hh"
19#include "GPU_context.hh"
20#include "GPU_pass.hh"
21#include "GPU_vertex_format.hh"
22#include "gpu_codegen.hh"
23
24#include <mutex>
25#include <string>
26
27namespace blender {
28
29using namespace blender::gpu::shader;
30
31static bool gpu_pass_validate(GPUCodegenCreateInfo *create_info);
32
33/* -------------------------------------------------------------------- */
36
37struct GPUPass {
38 static inline std::atomic<uint64_t> compilation_counts = 0;
39
42 std::atomic<gpu::Shader *> shader = nullptr;
43 std::atomic<GPUPassStatus> status = GPU_PASS_QUEUED;
44 /* Orphaned GPUPasses gets freed by the garbage collector. */
45 std::atomic<int> refcount = 1;
46 double creation_timestamp = 0.0f;
47 /* The last time the refcount was greater than 0. */
48 double gc_timestamp = 0.0f;
49
51
56 bool should_optimize = false;
58
59 /* Number of seconds after creation required before compiling an optimization pass. */
60 static constexpr float optimization_delay = 10.0f;
61
63 bool deferred_compilation,
65 bool should_optimize)
66 : create_info(info),
70 {
72 if (is_optimization_pass && deferred_compilation) {
73 // Defer until all non optimization passes are compiled.
74 return;
75 }
76
77 GPUShaderCreateInfo *base_info = reinterpret_cast<GPUShaderCreateInfo *>(create_info);
78
79 if (deferred_compilation) {
81 }
82 else {
85 }
86 }
87
99
104
106 {
107 BLI_assert_msg(create_info, "GPUPass::finalize_compilation() called more than once.");
108
109 if (compilation_handle) {
111 }
112
114
116 fprintf(stderr, "gpu::Shader: error: too many samplers in shader.\n");
117 }
118
120
121 MEM_delete(create_info);
122 create_info = nullptr;
123 }
124
125 void update(double timestamp)
126 {
127 update_compilation(timestamp);
128 update_gc_timestamp(timestamp);
129 }
130
131 void update_compilation(double timestamp)
132 {
133 if (compilation_handle) {
136 }
137 }
138 else if (status == GPU_PASS_QUEUED && refcount > 0 &&
139 ((creation_timestamp + optimization_delay) <= timestamp))
140 {
142 GPUShaderCreateInfo *base_info = reinterpret_cast<GPUShaderCreateInfo *>(create_info);
144 }
145 }
146
147 void update_gc_timestamp(double timestamp)
148 {
149 if (refcount != 0 || gc_timestamp == 0.0f) {
150 gc_timestamp = timestamp;
151 }
152 }
153
154 bool should_gc(int gc_collect_rate, double timestamp)
155 {
156 BLI_assert(gc_timestamp != 0.0f);
158 (timestamp - gc_timestamp) >= gc_collect_rate;
159 }
160};
161
163{
164 return pass->status;
165}
166
168{
169 /* Returns optimization heuristic prepared during
170 * initial codegen.
171 * NOTE: Only enabled on Metal, since it doesn't seem to yield any performance improvements for
172 * other backends. */
174}
175
177{
178 return pass->shader;
179}
180
182{
183 int previous_refcount = pass->refcount++;
184 UNUSED_VARS_NDEBUG(previous_refcount);
185 BLI_assert(previous_refcount > 0);
186}
187
189{
190 int previous_refcount = pass->refcount--;
191 UNUSED_VARS_NDEBUG(previous_refcount);
192 BLI_assert(previous_refcount > 0);
193}
194
199
204
206
207/* -------------------------------------------------------------------- */
214
216
218 static constexpr float gc_collect_rate_ = 60.0f;
219 static constexpr float optimization_gc_collect_rate_ = 1.0f;
220
221 Map<uint32_t, std::unique_ptr<GPUPass>> passes_[GPU_MAT_ENGINE_MAX][2 /*is_optimization_pass*/];
222 std::mutex mutex_;
223
224 public:
226 {
227 /* Pause to prevent new compilations to start while we are cancelling them. */
229 for (int i : IndexRange(GPU_MAT_ENGINE_MAX)) {
230 passes_[i][0].clear();
231 passes_[i][1].clear();
232 }
234 }
235
237 GPUCodegen &codegen,
238 bool deferred_compilation,
239 bool is_optimization_pass)
240 {
241 std::lock_guard lock(mutex_);
242
243 passes_[engine][is_optimization_pass].add(
244 codegen.hash_get(),
245 std::make_unique<GPUPass>(codegen.create_info,
246 deferred_compilation,
247 is_optimization_pass,
248 !is_optimization_pass && codegen.should_optimize_heuristic()));
249 };
250
252 size_t hash,
253 bool allow_deferred,
254 bool is_optimization_pass)
255 {
256 std::lock_guard lock(mutex_);
257 std::unique_ptr<GPUPass> *pass = passes_[engine][is_optimization_pass].lookup_ptr(hash);
258 if (!allow_deferred && pass && pass->get()->status == GPU_PASS_QUEUED) {
259 pass->get()->finalize_compilation();
260 }
261 return pass ? pass->get() : nullptr;
262 }
263
264 void update()
265 {
266 std::lock_guard lock(mutex_);
267
268 double timestamp = BLI_time_now_seconds();
269
270 /* Base Passes. */
271 for (auto &engine_passes : passes_) {
272 for (std::unique_ptr<GPUPass> &pass : engine_passes[false].values()) {
273 pass->update(timestamp);
274 }
275
276 engine_passes[false].remove_if(
277 [&](auto item) { return item.value->should_gc(gc_collect_rate_, timestamp); });
278 }
279
280 /* Optimization Passes */
281 for (auto &engine_passes : passes_) {
282 for (std::unique_ptr<GPUPass> &pass : engine_passes[true].values()) {
283 pass->update(timestamp);
284 }
285
286 engine_passes[true].remove_if([&](auto item) {
287 return item.value->should_gc(optimization_gc_collect_rate_, timestamp);
288 });
289 }
290 }
291
292 std::mutex &get_mutex()
293 {
294 return mutex_;
295 }
296};
297
298static GPUPassCache *g_cache = nullptr;
299
301{
302 if (pass->status == GPU_PASS_QUEUED) {
303 std::lock_guard lock(g_cache->get_mutex());
304 if (pass->status == GPU_PASS_QUEUED) {
305 pass->finalize_compilation();
306 }
307 }
308}
309
311{
312 g_cache = MEM_new<GPUPassCache>(__func__);
313}
314
316{
317 g_cache->update();
318}
319
325
327{
328 MEM_SAFE_DELETE(g_cache);
329}
330
332
333/* -------------------------------------------------------------------- */
336
338{
339 int samplers_len = 0;
340 for (const ShaderCreateInfo::Resource &res : create_info->resources_get_all_()) {
342 samplers_len++;
343 }
344 }
345
346 /* Validate against GPU limit. */
347 return samplers_len <= GPU_max_textures();
348}
349
351 GPUNodeGraph *graph,
352 const char *debug_name,
353 eGPUMaterialEngine engine,
354 bool deferred_compilation,
355 GPUCodegenCallbackFn finalize_source_cb,
356 void *thunk,
357 bool optimize_graph)
358{
360
361 GPUCodegen codegen(material, graph, debug_name);
362 codegen.generate_material_props();
363 codegen.extract_uniform_list();
364
365 if (optimize_graph) {
366 /* The optimized version of the shader should not re-generate a UBO.
367 * For the builtin uniforms (i.e. cryptomatte), the non-optimized-pass UBO will be reused.
368 * Ensure that inputs are sorted the same way so that the UBO declaration still match. */
369 codegen.sort_uniform_inputs();
370 }
371 else {
372 codegen.generate_uniform_buffer();
373 }
374
375 /* If Optimize flag is passed in, we are generating an optimized
376 * variant of the GPUMaterial's GPUPass. */
377 if (optimize_graph) {
379 }
380
381 /* Extract attributes before compiling so the generated VBOs are ready to accept the future
382 * shader. */
384
385 codegen.generate_graphs();
386
387 GPUPass *pass = nullptr;
388
389 /* Cache lookup: Reuse shaders already compiled. */
390 pass = g_cache->get(engine, codegen.hash_get(), deferred_compilation, optimize_graph);
391
392 if (pass) {
393 pass->refcount++;
394 return pass;
395 }
396
397 /* The shader is not compiled, continue generating the shader strings. */
398 codegen.generate_attribs();
399 codegen.generate_resources();
400
401 /* Make engine add its own code and implement the generated functions. */
402 finalize_source_cb(thunk, material, &codegen.output);
403
404 codegen.create_info->finalize();
405 g_cache->add(engine, codegen, deferred_compilation, optimize_graph);
406 codegen.create_info = nullptr;
407
408 return g_cache->get(engine, codegen.hash_get(), deferred_compilation, optimize_graph);
409}
410
412
413} // namespace blender
Platform independent time functions.
#define GPU_SHADER_FREE_SAFE(shader)
Read Blender memory allocation and freeing.
volatile int lock
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
std::mutex & get_mutex()
Definition gpu_pass.cc:292
void add(eGPUMaterialEngine engine, GPUCodegen &codegen, bool deferred_compilation, bool is_optimization_pass)
Definition gpu_pass.cc:236
GPUPass * get(eGPUMaterialEngine engine, size_t hash, bool allow_deferred, bool is_optimization_pass)
Definition gpu_pass.cc:251
constexpr IndexRange()=default
GPUCodegenCreateInfo * create_info
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static GPUPassCache * g_cache
Definition gpu_pass.cc:298
unsigned __int64 uint64_t
Definition BKE_text.h:91
void GPU_shader_compiler_wait_for_all()
void GPU_shader_compiler_pause()
bool GPU_pass_should_optimize(GPUPass *pass)
Definition gpu_pass.cc:167
GPUBackendType GPU_backend_get_type()
void GPU_pass_acquire(GPUPass *pass)
Definition gpu_pass.cc:181
void GPU_shader_async_compilation_cancel(AsyncCompilationHandle &handle)
gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
@ GPU_MAT_ENGINE_MAX
CompilationPriority
Definition GPU_shader.hh:85
gpu::Shader * GPU_pass_shader_get(GPUPass *pass)
Definition gpu_pass.cc:176
uint64_t GPU_pass_compilation_timestamp(GPUPass *pass)
Definition gpu_pass.cc:200
void GPU_pass_cache_update()
Definition gpu_pass.cc:315
GPUPassStatus
Definition GPU_pass.hh:22
@ GPU_PASS_QUEUED
Definition GPU_pass.hh:24
@ GPU_PASS_FAILED
Definition GPU_pass.hh:23
@ GPU_PASS_SUCCESS
Definition GPU_pass.hh:25
GPUPassStatus GPU_pass_status(GPUPass *pass)
Definition gpu_pass.cc:162
int GPU_max_textures()
static bool gpu_pass_validate(GPUCodegenCreateInfo *create_info)
Definition gpu_pass.cc:337
int64_t AsyncCompilationHandle
Definition GPU_shader.hh:87
void GPU_pass_cache_init()
Definition gpu_pass.cc:310
void GPU_pass_release(GPUPass *pass)
Definition gpu_pass.cc:188
void GPU_pass_cache_free()
Definition gpu_pass.cc:326
bool GPU_shader_async_compilation_is_ready(AsyncCompilationHandle handle)
void GPU_pass_cache_wait_for_all()
Definition gpu_pass.cc:320
gpu::Shader * GPU_shader_async_compilation_finalize(AsyncCompilationHandle &handle)
uint64_t GPU_pass_global_compilation_count()
Definition gpu_pass.cc:195
void gpu_node_graph_finalize_uniform_attrs(GPUNodeGraph *graph)
double BLI_time_now_seconds()
Definition time.cc:30
GPUPass * GPU_generate_pass(GPUMaterial *material, GPUNodeGraph *graph, const char *debug_name, eGPUMaterialEngine engine, bool deferred_compilation, GPUCodegenCallbackFn finalize_source_cb, void *thunk, bool optimize_graph)
Definition gpu_pass.cc:350
void gpu_node_graph_prune_unused(GPUNodeGraph *graph)
AsyncCompilationHandle GPU_shader_async_compilation(const GPUShaderCreateInfo *info, CompilationPriority priority=CompilationPriority::High)
void GPU_pass_ensure_its_ready(GPUPass *pass)
Definition gpu_pass.cc:300
void(*)(void *thunk, GPUMaterial *mat, struct GPUCodegenOutput *codegen) GPUCodegenCallbackFn
void gpu_node_graph_optimize(GPUNodeGraph *graph)
void GPU_shader_compiler_resume()
#define hash
Definition noise_c.cc:156
bool is_optimization_pass
Definition gpu_pass.cc:57
void update(double timestamp)
Definition gpu_pass.cc:125
std::atomic< int > refcount
Definition gpu_pass.cc:45
GPUPass(GPUCodegenCreateInfo *info, bool deferred_compilation, bool is_optimization_pass, bool should_optimize)
Definition gpu_pass.cc:62
AsyncCompilationHandle compilation_handle
Definition gpu_pass.cc:41
static std::atomic< uint64_t > compilation_counts
Definition gpu_pass.cc:38
std::atomic< gpu::Shader * > shader
Definition gpu_pass.cc:42
std::atomic< GPUPassStatus > status
Definition gpu_pass.cc:43
static constexpr float optimization_delay
Definition gpu_pass.cc:60
void update_compilation(double timestamp)
Definition gpu_pass.cc:131
double creation_timestamp
Definition gpu_pass.cc:46
CompilationPriority compilation_priority()
Definition gpu_pass.cc:100
double gc_timestamp
Definition gpu_pass.cc:48
GPUCodegenCreateInfo * create_info
Definition gpu_pass.cc:40
void update_gc_timestamp(double timestamp)
Definition gpu_pass.cc:147
uint64_t compilation_timestamp
Definition gpu_pass.cc:50
void finalize_compilation()
Definition gpu_pass.cc:105
bool should_gc(int gc_collect_rate, double timestamp)
Definition gpu_pass.cc:154
void finalize(const bool recursive=false)