Blender V5.3
GPU_shader.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include <atomic>
14#include <mutex>
15#include <optional>
16
17#include "BLI_span.hh"
18#include "BLI_string_ref.hh"
19#include "BLI_utility_mixins.hh"
20#include "BLI_vector.hh"
21
22#include "GPU_common_types.hh"
23#include "GPU_shader_builtin.hh"
24
25struct GPUShaderCreateInfo;
26namespace blender {
27
28namespace gpu {
29class VertBuf;
30class Shader;
31namespace shader {
32struct ShaderCreateInfo;
33} // namespace shader
34} // namespace gpu
35
37/* Hardware limit is 16. Position attribute is always needed so we reduce to 15.
38 * This makes sure the GPUVertexFormat name buffer does not overflow. */
39constexpr static int GPU_MAX_ATTR = 15;
40
41/* -------------------------------------------------------------------- */
44
52
57gpu::Shader *GPU_shader_create_from_info(const GPUShaderCreateInfo *_info);
58
62gpu::Shader *GPU_shader_create_from_info_python(const GPUShaderCreateInfo *_info);
63
70gpu::Shader *GPU_shader_create_from_info_name(const char *info_name);
71
77const GPUShaderCreateInfo *GPU_shader_create_info_get(const char *info_name);
78
83bool GPU_shader_create_info_check_error(const GPUShaderCreateInfo *_info, char r_error[128]);
84
86
96 const GPUShaderCreateInfo *info, CompilationPriority priority = CompilationPriority::High);
113
132
134
135/* -------------------------------------------------------------------- */
138
140
142
143/* -------------------------------------------------------------------- */
146
155 const gpu::shader::SpecializationConstants *constants_state = nullptr);
156
162void GPU_shader_unbind();
163
169
171
172/* -------------------------------------------------------------------- */
175
177
179
180/* -------------------------------------------------------------------- */
183
188int GPU_shader_get_ubo_binding(gpu::Shader *shader, const char *name);
189int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name);
191int GPU_shader_get_tlas_binding(gpu::Shader *shader, const char *name);
192
197int GPU_shader_get_uniform(gpu::Shader *shader, const char *name);
198
202int GPU_shader_get_constant(gpu::Shader *shader, const char *name);
203
209 gpu::Shader *shader, int location, int length, int array_size, const float *value);
211 gpu::Shader *shader, int location, int length, int array_size, const int *value);
212
219void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value);
220void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value);
221void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value);
222void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y);
223void GPU_shader_uniform_3f(gpu::Shader *sh, const char *name, float x, float y, float z);
224void GPU_shader_uniform_4f(gpu::Shader *sh, const char *name, float x, float y, float z, float w);
225void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2]);
226void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3]);
227void GPU_shader_uniform_4fv(gpu::Shader *sh, const char *name, const float data[4]);
228void GPU_shader_uniform_2iv(gpu::Shader *sh, const char *name, const int data[2]);
229void GPU_shader_uniform_3iv(gpu::Shader *sh, const char *name, const int data[3]);
230void GPU_shader_uniform_mat4(gpu::Shader *sh, const char *name, const float data[4][4]);
231void GPU_shader_uniform_mat3(gpu::Shader *sh, const char *name, const float data[3][3]);
232void GPU_shader_uniform_mat3_as_mat4(gpu::Shader *sh, const char *name, const float data[3][3]);
233void GPU_shader_uniform_1f_array(gpu::Shader *sh, const char *name, int len, const float *val);
235 const char *name,
236 int len,
237 const float (*val)[2]);
239 const char *name,
240 int len,
241 const float (*val)[4]);
242
244
245/* -------------------------------------------------------------------- */
250
253int GPU_shader_get_attribute(const gpu::Shader *shader, const char *name);
255 int attr_location,
256 char r_name[256],
257 int *r_type);
259 int ssbo_location,
260 char r_name[256]);
261
263
264/* -------------------------------------------------------------------- */
271
272/* Return the default constants.
273 * All constants available for this shader should fit the returned structure. */
275
277
282
295 const ShaderSpecialization &specialization,
297
303
309
311
312/* -------------------------------------------------------------------- */
317
355
356/* We expect the parent shader to be compiled and already have some cached PSOs when being assigned
357 * as a reference. Ensure the parent shader still exists when `GPU_shader_cache_warm(..)` is
358 * called. */
360
365 GPU_UNIFORM_MODEL = 0, /* mat4 ModelMatrix */
366 GPU_UNIFORM_VIEW, /* mat4 ViewMatrix */
367 GPU_UNIFORM_MODELVIEW, /* mat4 ModelViewMatrix */
368 GPU_UNIFORM_PROJECTION, /* mat4 ProjectionMatrix */
369 GPU_UNIFORM_VIEWPROJECTION, /* mat4 ViewProjectionMatrix */
370 GPU_UNIFORM_MVP, /* mat4 ModelViewProjectionMatrix */
371
372 GPU_UNIFORM_MODEL_INV, /* mat4 ModelMatrixInverse */
373 GPU_UNIFORM_VIEW_INV, /* mat4 ViewMatrixInverse */
374 GPU_UNIFORM_MODELVIEW_INV, /* mat4 ModelViewMatrixInverse */
375 GPU_UNIFORM_PROJECTION_INV, /* mat4 ProjectionMatrixInverse */
376 GPU_UNIFORM_VIEWPROJECTION_INV, /* mat4 ViewProjectionMatrixInverse */
377
378 GPU_UNIFORM_NORMAL, /* mat3 NormalMatrix */
379 GPU_UNIFORM_CLIPPLANES, /* vec4 WorldClipPlanes[] */
380
381 GPU_UNIFORM_COLOR, /* vec4 color */
382 GPU_UNIFORM_BASE_INSTANCE, /* int baseInstance */
383 GPU_UNIFORM_SRGB_TRANSFORM, /* bool srgbTarget */
384 GPU_UNIFORM_SCENE_LINEAR_XFORM, /* float3x3 gpu_scene_linear_to_xyz */
385};
386#define GPU_NUM_UNIFORMS (GPU_UNIFORM_SCENE_LINEAR_XFORM + 1)
387
392int GPU_shader_get_builtin_uniform(gpu::Shader *shader, int builtin);
393
400
402
416
418int GPU_shader_get_uniform_block(gpu::Shader *shader, const char *name);
419
421
422#define GPU_SHADER_FREE_SAFE(shader) \
423 do { \
424 if (shader != nullptr) { \
425 GPU_shader_free(shader); \
426 shader = nullptr; \
427 } \
428 } while (0)
429
430namespace gpu {
431
432/* gpu::Shader wrapper that makes compilation threadsafe.
433 * The compilation is deferred until the first get() call.
434 * Concurrently using the shader from multiple threads is still unsafe. */
436 private:
437 std::string info_name_;
438 std::atomic<gpu::Shader *> shader_ = nullptr;
439 /* TODO: Failed compilation detection should be supported by the gpu::Shader API. */
440 std::atomic<bool> failed_ = false;
441 std::mutex mutex_;
442 /* Handle for async compilation. */
443 AsyncCompilationHandle compilation_handle_ = 0;
444
445 void move(StaticShader &&other)
446 {
447 std::scoped_lock lock1(mutex_);
448 std::scoped_lock lock2(other.mutex_);
449 BLI_assert(shader_ == nullptr && info_name_.empty());
450 std::swap(info_name_, other.info_name_);
451 /* No std::swap support for atomics. */
452 shader_.exchange(other.shader_.exchange(shader_));
453 failed_.exchange(other.failed_.exchange(failed_));
454 std::swap(compilation_handle_, other.compilation_handle_);
455 }
456
457 public:
458 StaticShader(std::string info_name) : info_name_(info_name) {}
459
460 StaticShader() = default;
462 {
463 move(std::move(other));
464 }
466 {
467 move(std::move(other));
468 return *this;
469 };
470
472 {
473 if (compilation_handle_) {
474 GPU_shader_async_compilation_cancel(compilation_handle_);
475 }
476 GPU_SHADER_FREE_SAFE(shader_);
477 }
478
479 /* Schedule the shader to be compile in a worker thread. */
481 {
482 if (is_ready()) {
483 return;
484 }
485
486 std::scoped_lock lock(mutex_);
487
488 if (compilation_handle_) {
489 if (GPU_shader_async_compilation_is_ready(compilation_handle_)) {
490 shader_ = GPU_shader_async_compilation_finalize(compilation_handle_);
491 failed_ = shader_ == nullptr;
492 }
493 return;
494 }
495
496 if (!shader_ && !failed_ && !compilation_handle_) {
497 BLI_assert(!info_name_.empty());
498 const GPUShaderCreateInfo *create_info = GPU_shader_create_info_get(info_name_.c_str());
499 compilation_handle_ = GPU_shader_async_compilation(create_info);
500 }
501 }
502
503 bool is_ready()
504 {
505 return shader_ || failed_;
506 }
507
509 {
510 if (is_ready()) {
511 return shader_;
512 }
513
514 std::scoped_lock lock(mutex_);
515
516 if (!shader_ && !failed_) {
517 if (compilation_handle_) {
518 shader_ = GPU_shader_async_compilation_finalize(compilation_handle_);
519 }
520 else {
521 BLI_assert(!info_name_.empty());
522 shader_ = GPU_shader_create_from_info_name(info_name_.c_str());
523 }
524 failed_ = shader_ == nullptr;
525 }
526
527 return shader_;
528 }
529
530 /* For batch compiled shaders. */
531 /* TODO: Find a better way to handle this. */
533 {
534 std::scoped_lock lock(mutex_);
535 BLI_assert(shader_ == nullptr);
536 shader_ = shader;
537 }
538};
539
540/* Thread-safe container for StaticShader cache classes.
541 * The class instance creation is deferred until the first get() call. */
542template<typename T> class StaticShaderCache {
543 std::atomic<T *> cache_ = nullptr;
544 std::mutex mutex_;
545
546 public:
548 {
549 BLI_assert(cache_ == nullptr);
550 }
551
552 template<typename... Args> T &get(Args &&...constructor_args)
553 {
554 if (cache_) {
555 return *cache_;
556 }
557
558 std::lock_guard lock(mutex_);
559
560 if (cache_ == nullptr) {
561 cache_ = new T(std::forward<Args>(constructor_args)...);
562 }
563 return *cache_;
564 }
565
566 void release()
567 {
568 if (!cache_) {
569 return;
570 }
571
572 std::lock_guard lock(mutex_);
573
574 if (cache_) {
575 delete cache_;
576 cache_ = nullptr;
577 }
578 }
579
580 std::lock_guard<std::mutex> lock_guard()
581 {
582 return std::lock_guard(mutex_);
583 }
584};
585
586} // namespace gpu
587} // namespace blender
#define GPU_SHADER_FREE_SAFE(shader)
volatile int lock
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
NonCopyable(const NonCopyable &other)=delete
T & get(Args &&...constructor_args)
std::lock_guard< std::mutex > lock_guard()
StaticShader & operator=(StaticShader &&other)
void set(gpu::Shader *shader)
StaticShader(StaticShader &&other)
StaticShader(std::string info_name)
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define T
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_shader_uniform_2iv(gpu::Shader *sh, const char *name, const int data[2])
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
void GPU_shader_compiler_wait_for_all()
__int64 int64_t
Definition BKE_text.h:90
void GPU_shader_compiler_pause()
void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y)
unsigned int uint
Definition BKE_text.h:55
gpu::Shader * GPU_shader_create_from_info_python(const GPUShaderCreateInfo *_info)
void GPU_shader_uniform_mat4(gpu::Shader *sh, const char *name, const float data[4][4])
int GPU_shader_get_constant(gpu::Shader *shader, const char *name)
void GPU_shader_compile_static()
void GPU_shader_uniform_3f(gpu::Shader *sh, const char *name, float x, float y, float z)
std::string GPU_shader_preprocess_source(StringRefNull original, gpu::shader::ShaderCreateInfo &info)
void GPU_shader_async_compilation_cancel(AsyncCompilationHandle &handle)
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
static constexpr int GPU_MAX_ATTR
Definition GPU_shader.hh:39
void GPU_shader_uniform_4fv(gpu::Shader *sh, const char *name, const float data[4])
void GPU_shader_uniform_3iv(gpu::Shader *sh, const char *name, const int data[3])
void GPU_shader_uniform_1f_array(gpu::Shader *sh, const char *name, int len, const float *val)
int GPU_shader_get_attribute(const gpu::Shader *shader, const char *name)
void GPU_shader_async_specialization_cancel(AsyncSpecializationHandle &handle)
void GPU_shader_uniform_int_ex(gpu::Shader *shader, int location, int length, int array_size, const int *value)
CompilationPriority
Definition GPU_shader.hh:85
AsyncSpecializationHandle GPU_shader_async_specialization(const ShaderSpecialization &specialization, CompilationPriority priority=CompilationPriority::High)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
void GPU_shader_set_parent(gpu::Shader *shader, gpu::Shader *parent)
@ GPU_UNIFORM_VIEWPROJECTION
@ GPU_UNIFORM_BASE_INSTANCE
@ GPU_UNIFORM_SCENE_LINEAR_XFORM
@ GPU_UNIFORM_VIEWPROJECTION_INV
@ GPU_UNIFORM_PROJECTION_INV
@ GPU_UNIFORM_MODELVIEW
@ GPU_UNIFORM_MODEL_INV
@ GPU_UNIFORM_SRGB_TRANSFORM
@ GPU_UNIFORM_MVP
@ GPU_UNIFORM_CLIPPLANES
@ GPU_UNIFORM_COLOR
@ GPU_UNIFORM_NORMAL
@ GPU_UNIFORM_MODELVIEW_INV
@ GPU_UNIFORM_MODEL
@ GPU_UNIFORM_VIEW
@ GPU_UNIFORM_VIEW_INV
@ GPU_UNIFORM_PROJECTION
void GPU_shader_uniform_4fv_array(gpu::Shader *sh, const char *name, int len, const float(*val)[4])
void GPU_shader_uniform_2fv_array(gpu::Shader *sh, const char *name, int len, const float(*val)[2])
bool GPU_shader_get_ssbo_input_info(const gpu::Shader *shader, int ssbo_location, char r_name[256])
BMesh const char void * data
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
bool GPU_shader_compiler_has_pending_work()
void GPU_shader_uniform_float_ex(gpu::Shader *shader, int location, int length, int array_size, const float *value)
int GPU_shader_get_tlas_binding(gpu::Shader *shader, const char *name)
void GPU_shader_cache_dir_clear_old()
uint GPU_shader_get_attribute_len(const gpu::Shader *shader)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
int64_t AsyncSpecializationHandle
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
int64_t AsyncCompilationHandle
Definition GPU_shader.hh:87
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
int GPU_shader_get_uniform(gpu::Shader *shader, const char *name)
gpu::Shader * GPU_shader_get_bound()
bool GPU_shader_async_compilation_is_ready(AsyncCompilationHandle handle)
uint GPU_shader_get_ssbo_input_len(const gpu::Shader *shader)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void GPU_shader_warm_cache(gpu::Shader *shader, int limit)
gpu::Shader * GPU_shader_async_compilation_finalize(AsyncCompilationHandle &handle)
const char * GPU_shader_get_name(gpu::Shader *shader)
int GPU_shader_get_ubo_binding(gpu::Shader *shader, const char *name)
const GPUShaderCreateInfo * GPU_shader_create_info_get(const char *info_name)
void GPU_shader_free(gpu::Shader *shader)
int GPU_shader_get_builtin_uniform(gpu::Shader *shader, int builtin)
void GPU_shader_uniform_mat3_as_mat4(gpu::Shader *sh, const char *name, const float data[3][3])
AsyncCompilationHandle GPU_shader_async_compilation(const GPUShaderCreateInfo *info, CompilationPriority priority=CompilationPriority::High)
void GPU_shader_uniform_mat3(gpu::Shader *sh, const char *name, const float data[3][3])
bool GPU_shader_async_specialization_is_ready(AsyncSpecializationHandle &handle)
bool GPU_shader_get_attribute_info(const gpu::Shader *shader, int attr_location, char r_name[256], int *r_type)
GPUUniformBlockBuiltin
@ GPU_UNIFORM_BLOCK_MODEL
@ GPU_UNIFORM_BLOCK_DRW_VIEW
@ GPU_UNIFORM_BLOCK_DRW_CLIPPING
@ GPU_UNIFORM_BLOCK_VIEW
@ GPU_UNIFORM_BLOCK_DRW_MODEL
@ GPU_UNIFORM_BLOCK_INFO
@ GPU_UNIFORM_BLOCK_DRW_INFOS
@ GPU_NUM_UNIFORM_BLOCKS
void GPU_shader_uniform_4f(gpu::Shader *sh, const char *name, float x, float y, float z, float w)
void GPU_shader_compiler_resume()
bool GPU_shader_create_info_check_error(const GPUShaderCreateInfo *_info, char r_error[128])
int GPU_shader_get_uniform_block(gpu::Shader *shader, const char *name)
const gpu::shader::SpecializationConstants & GPU_shader_get_default_constant_state(gpu::Shader *sh)
gpu::shader::SpecializationConstants constants
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...