54 std::string sub_str = pattern.
substr(1, pattern.
size() - 2);
55 if (shader_name.
find(sub_str) == std::string::npos) {
60 std::string sub_str = pattern.
substr(1);
61 if (!shader_name.
endswith(sub_str)) {
66 std::string sub_str = pattern.
substr(0, pattern.
size() - 1);
71 else if (shader_name != pattern) {
75 namespace fs = std::filesystem;
76 fs::path shader_dir = fs::current_path() /
"Shaders";
77 fs::create_directories(shader_dir);
78 fs::path file_path = shader_dir / (shader_name_with_stage_name + extension);
80 std::ofstream output_source_file(file_path);
81 if (output_source_file) {
82 output_source_file << source;
83 output_source_file.close();
84 std::cout <<
"Shader Source Debug: Writing file: " << file_path <<
"\n";
87 std::cerr <<
"Shader Source Debug: Failed to open file: " << file_path <<
"\n";
94 for (
const auto &def : info.
defines_) {
95 defines +=
"#define ";
117 for (
char &c :
name) {
121 if (!std::isalnum(c)) {
130 "Shader must be unbound from context before being freed");
139 sources.
append(
"version");
143 sources.
append(
"#define GPU_SHADER\n");
146 sources.
append(
"#define GPU_ATI\n");
149 sources.
append(
"#define GPU_NVIDIA\n");
152 sources.
append(
"#define GPU_INTEL\n");
155 sources.
append(
"#define GPU_APPLE\n");
159 sources.
append(
"#define OS_WIN\n");
162 sources.
append(
"#define OS_MAC\n");
165 sources.
append(
"#define OS_UNIX\n");
171 sources.
append(
"#define GPU_OPENGL\n");
174 sources.
append(
"#define GPU_METAL\n");
177 sources.
append(
"#define GPU_VULKAN\n");
220 std::cerr <<
"Warning: Trying to compile \"" << info.
name_
221 <<
"\" which was not marked for static compilation.\n";
243 if (error.has_value()) {
244 std::cerr << error->full_report << std::endl;
245 return "\n#error conversion failed\n";
253 info.
define(
"USE_WORLD_CLIP_PLANES");
255 return processed_str;
266 std::array<StringRefNull, 2> includes = {
267 "gpu_shader_python_base.glsl",
268 "gpu_shader_python_typedef_lib.glsl",
282 info.
define(
"WITH_MATRIX_EQ_OPERATORS");
288 auto preprocess_source = [&](
const std::string &input_src) {
289 std::string processed_str;
290 processed_str +=
"\n";
291 processed_str +=
"#ifdef CREATE_INFO_RES_PASS_pyGPU_Shader\n";
292 processed_str +=
"CREATE_INFO_RES_PASS_pyGPU_Shader\n";
293 processed_str +=
"#endif\n";
295 return processed_str;
366 printf(
"Compiling all static GPU shaders. This process takes a while.\n");
387 "Shader requires specialization constants but none was passed");
393 shader->bind(constants_state);
401 if (constants_state) {
402 shader->bind(constants_state);
411#if GPU_SHADER_PRINTF_ENABLE
421 if (ctx ==
nullptr) {
449 return shader->name_get().c_str();
464 Shader *shd_parent = parent;
471 shader->warm_cache(limit);
493 constants = std::make_unique<const shader::SpecializationConstants>(std::move(constants_tmp));
528 const ShaderInput *constant = interface->constant_get(name);
529 return constant ? constant->
location : -1;
541 const ShaderInput *ssbo = interface->ssbo_get(name);
556 return ubo ? ubo->
binding : -1;
562 const ShaderInput *tex = interface->uniform_get(name);
563 return tex ? tex->
binding : -1;
569 const ShaderInput *tlas = interface->tlas_get(name);
576 return interface->valid_bindings_get(interface->inputs_, interface->attr_len_);
582 return interface->ssbo_len_;
588 const ShaderInput *attr = interface->attr_get(name);
599 const ShaderInput *attr = interface->attr_get(attr_location);
604 BLI_strncpy(r_name, interface->input_name_get(attr), 256);
605 *r_type = attr->location != -1 ? interface->attr_types_[attr->location] : -1;
613 const ShaderInput *ssbo_input = interface->ssbo_get(ssbo_location);
618 BLI_strncpy(r_name, interface->input_name_get(ssbo_input), 256);
631 shader->uniform_float(loc,
len, array_size, value);
637 shader->uniform_int(loc,
len, array_size, value);
653 const float data[2] = {
x,
y};
659 const float data[3] = {
x,
y,
z};
733 const float (*val)[2])
742 const float (*val)[4])
797 using Clock = std::chrono::steady_clock;
811 else if (
G.profile_gpu) {
812 start_time = Clock::now();
851 if (!specialized_info.
builtins_.is_empty()) {
855 specialized_info.
builtins_.remove_if(predicate);
859 for (
auto value : specialized_info.
builtins_) {
860 builtin_combined = builtin_combined | value.bit;
863 specialized_info.
builtins(builtin_combined);
867 auto cleanup_duplicates = [&](
auto &resources) {
868 auto tmp = resources;
870 resources.extend_non_duplicates(tmp);
877 const std::string error = specialized_info.
check_error();
878 if (!error.empty()) {
879 std::cerr << error <<
"\n";
887 shader->specialization_constants_init(info);
888 shader->init(info, is_codegen_only);
890 shader->fragment_output_bits = 0;
892 shader->fragment_output_bits |= 1u << frag_out.
index;
898 std::string defines =
shader->defines_declare(info);
899 std::string resources =
shader->resources_declare(info);
918 defines +=
"#define SRT_CONSTANT_" + constant.name +
" " + std::to_string(constant.value.i) +
922 defines +=
"#define USE_GPU_SHADER_CREATE_INFO\n";
927 std::string
interface =
shader->vertex_interface_declare(info);
931 if (!info.geometry_source_.is_empty()) {
932 sources.append(
"#define USE_GEOMETRY_SHADER\n");
934 sources.append(defines);
935 sources.append(resources);
936 sources.append(interface);
937 sources.extend(code);
939 if (info.vertex_entry_fn_ !=
"main") {
940 sources.append(
"void main() { ");
941 sources.append(info.vertex_entry_fn_);
942 sources.append(
"(); }\n");
945 shader->vertex_shader_from_glsl(info, sources);
951 std::string
interface =
shader->fragment_interface_declare(info);
955 if (!info.geometry_source_.is_empty()) {
956 sources.append(
"#define USE_GEOMETRY_SHADER\n");
958 sources.append(defines);
959 sources.append(resources);
960 sources.append(interface);
961 sources.extend(code);
963 if (info.fragment_entry_fn_ !=
"main") {
964 sources.append(
"void main() { ");
965 sources.append(info.fragment_entry_fn_);
966 sources.append(
"(); }\n");
969 shader->fragment_shader_from_glsl(info, sources);
975 std::string layout =
shader->geometry_layout_declare(info);
976 std::string
interface =
shader->geometry_interface_declare(info);
980 sources.append(
"#define GPU_GEOMETRY_SHADER\n");
981 sources.append(defines);
982 sources.append(resources);
983 sources.append(layout);
984 sources.append(interface);
985 sources.extend(code);
987 if (info.geometry_entry_fn_ !=
"main") {
988 sources.append(
"void main() { ");
989 sources.append(info.geometry_entry_fn_);
990 sources.append(
"(); }\n");
993 shader->geometry_shader_from_glsl(info, sources);
999 std::string layout =
shader->compute_layout_declare(info);
1003 sources.append(defines);
1004 sources.append(layout);
1005 sources.append(resources);
1006 sources.extend(code);
1009 sources.append(
"void main() { ");
1011 sources.append(
"(); }\n");
1014 shader->compute_shader_from_glsl(info, sources);
1017 if (!
shader->finalize(&info)) {
1027 else if (
G.profile_gpu) {
1031 start_time.time_since_epoch().count(),
1032 end_time.time_since_epoch().count());
1034 start_time.time_since_epoch().count(),
1035 end_time.time_since_epoch().count());
1057 bool support_specializations)
1059 support_specializations_ = support_specializations;
1062 compilation_worker_ = std::make_unique<GPUWorker>(
1063 threads_count, context_type, do_work_static_cb);
1069 compilation_worker_.reset();
1083 std::unique_lock
lock(mutex_);
1085 AsyncCompilation *compilation = MEM_new<AsyncCompilation>(__func__);
1086 compilation->info = info;
1089 async_compilations_.add(handle, compilation);
1091 if (compilation_worker_) {
1092 compilation->work = std::make_unique<ParallelWork>(ParallelWork{
this, compilation});
1093 compilation->work->id = compilation_worker_->push_work(compilation->work.get(),
1097 compilation->shader =
compile(*info,
false);
1098 compilation->is_ready =
true;
1107 std::unique_lock
lock(mutex_);
1109 AsyncCompilation *compilation = async_compilations_.pop(handle);
1110 if (compilation_worker_ && compilation_worker_->cancel_work(compilation->work->id)) {
1111 compilation->is_ready =
true;
1116 compilation_finished_notification_.wait(
lock, [&]() {
return compilation->is_ready ==
true; });
1118 compilation->shader =
nullptr;
1119 MEM_delete(compilation);
1125 compilation_finished_notification_.notify_all();
1130 std::lock_guard
lock(mutex_);
1132 return async_compilations_.lookup(handle)->is_ready;
1137 std::unique_lock
lock(mutex_);
1139 compilation_finished_notification_.wait(
1140 lock, [&]() {
return async_compilations_.lookup(handle)->is_ready ==
true; });
1142 AsyncCompilation *compilation = async_compilations_.pop(handle);
1144 MEM_delete(compilation);
1153 if (!compilation_worker_ || !support_specializations_) {
1157 std::lock_guard
lock(mutex_);
1159 AsyncCompilation *compilation = MEM_new<AsyncCompilation>(__func__);
1160 compilation->specialization = std::make_unique<ShaderSpecialization>(specialization);
1163 async_compilations_.add(handle, compilation);
1165 compilation->work = std::make_unique<ParallelWork>(ParallelWork{
this, compilation});
1166 compilation->work->id = compilation_worker_->push_work(compilation->work.get(),
1178 std::lock_guard
lock(mutex_);
1179 if (async_compilations_.lookup(handle)->is_ready) {
1180 AsyncCompilation *compilation = async_compilations_.pop(handle);
1181 MEM_delete(compilation);
1188void ShaderCompiler::do_work_static_cb(
void *payload)
1190 ParallelWork *work =
reinterpret_cast<ParallelWork *
>(payload);
1191 work->compiler->do_work(*work);
1194void ShaderCompiler::do_work(ParallelWork &work)
1196 AsyncCompilation *compilation = work.compilation;
1199 if (!compilation->is_specialization()) {
1200 compilation->shader = compile_shader(*compilation->info);
1203 specialize_shader(*compilation->specialization);
1207 std::unique_lock
lock(mutex_);
1208 compilation->is_ready =
true;
1211 compilation_finished_notification_.notify_all();
1215 std::unique_lock
lock(mutex_);
1216 pause_finished_notification_.wait(
lock, [&]() {
return !is_paused_; });
1219bool ShaderCompiler::is_compiling_impl()
1224 if (compilation_worker_ ==
nullptr) {
1228 if (!compilation_worker_->is_empty()) {
1232 for (AsyncCompilation *compilation : async_compilations_.values()) {
1233 if (!compilation->is_ready) {
1243 std::unique_lock
lock(mutex_);
1244 return is_compiling_impl();
1251 std::unique_lock
lock(mutex_);
1253 compilation_finished_notification_.wait(
lock, [&]() {
return !is_compiling_impl(); });
1258 std::unique_lock
lock(mutex_);
1266 std::unique_lock
lock(mutex_);
1270 pause_finished_notification_.notify_all();
#define CLOG_DEBUG(clg_ref,...)
#define GPU_DEBUG_SHADER_COMPILATION_GROUP
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
void append(const T &value)
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr bool endswith(StringRef suffix) const
Vector< StorageBuf * > printf_buf
int shader_builtin_srgb_transform
bool shader_builtin_srgb_is_dirty
ShaderCompiler * get_compiler()
virtual void shader_cache_dir_clear_old()=0
static GPUBackend * get()
static ProfileReport & get()
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)
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 ~ShaderCompiler()
virtual Shader * compile_shader(const shader::ShaderCreateInfo &info)
AsyncSpecializationHandle async_specialization(const ShaderSpecialization &specialization, CompilationPriority priority)
static void dump_source_to_disk(StringRef shader_name, StringRef shader_name_with_stage_name, StringRef extension, StringRef source)
std::unique_ptr< const shader::SpecializationConstants > constants
ShaderInterface * interface
static std::string defines_declare(const shader::ShaderCreateInfo &info)
void specialization_constants_init(const shader::ShaderCreateInfo &info)
void parent_set(Shader *parent)
static void set_scene_linear_to_xyz_uniform(gpu::Shader *shader)
static void set_framebuffer_srgb_target(int use_srgb_to_linear)
static void set_srgb_uniform(Context *ctx, gpu::Shader *shader)
Result convert(metadata::Source external_sources_symbols={})
#define GPU_SHADER_PRINTF_SLOT
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
std::chrono::high_resolution_clock Clock
float3x3 scene_linear_to_rec709
Vector< StringRefNull > gpu_shader_dependency_get_resolved_source(const StringRefNull shader_source_name, const shader::GeneratedSourceList &generated_sources, const StringRefNull shader_name)
shader::BuiltinBits convert_builtin_bit(shader::metadata::Builtin builtin)
static ThreadQueueWorkPriority to_work_priority(CompilationPriority priority)
Clock::time_point TimePoint
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()
void GPU_shader_compiler_pause()
char * STRNCPY(char(&dst)[N], const char *src)
void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y)
@ BLI_THREAD_QUEUE_WORK_PRIORITY_HIGH
@ BLI_THREAD_QUEUE_WORK_PRIORITY_NORMAL
@ BLI_THREAD_QUEUE_WORK_PRIORITY_LOW
bool gpu_shader_create_info_compile_all(const char *name_starts_with_filter)
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])
void copy_m4_m3(float m1[4][4], const float m2[3][3])
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)
GPUBackendType GPU_backend_get_type()
void GPU_shader_async_compilation_cancel(AsyncCompilationHandle &handle)
bool GPU_use_main_context_workaround()
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
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])
char datatoc_gpu_shader_colorspace_lib_glsl[]
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)
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_SCENE_LINEAR_XFORM
@ GPU_UNIFORM_SRGB_TRANSFORM
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])
void GPU_matrix_bind(gpu::Shader *shader)
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
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_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
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
void GPU_debug_group_end()
int GPU_shader_get_uniform(gpu::Shader *shader, const char *name)
const GPUShaderCreateInfo * gpu_shader_create_info_get(const char *info_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)
static void standard_defines(Vector< StringRefNull > &sources)
bool GPU_matrix_dirty_get()
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)
void GPU_shader_uniform_4f(gpu::Shader *sh, const char *name, float x, float y, float z, float w)
void GPU_shader_compiler_resume()
void GPU_debug_group_begin(const char *name)
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)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
StringRefNull vertex_source_
StringRefNull compute_source_
Vector< Resource, 0 > geometry_resources_
StringRefNull compute_entry_fn_
Self & compute_source(StringRefNull filename)
Vector< BuiltinBit, 0 > builtins_
Self & fragment_source(StringRefNull filename)
std::string fragment_source_generated
Vector< SpecializationConstant, 0 > specialization_constants_
Vector< CompilationConstant, 0 > compilation_constants_
GeneratedSourceList generated_sources
Vector< VertIn, 0 > vertex_inputs_
std::string resource_guard_defines(Span< CompilationConstant > constants) const
Vector< StageInterfaceInfoHandle, 0 > vertex_out_interfaces_
std::string check_error() const
Vector< Resource, 0 > pass_resources_
Vector< PushConst, 0 > push_constants_
StringRefNull geometry_source_
std::string vertex_source_generated
StringRefNull vertex_entry_fn_
std::string iface_guard_defines() const
std::string typedef_source_generated
Self & builtins(BuiltinBits builtin, ConditionFn cond=nullptr)
Self & vertex_source(StringRefNull filename)
StringRefNull fragment_source_
Vector< Resource, 0 > batch_resources_
bool do_static_compilation_
std::string compute_source_generated
Vector< FragOut, 0 > fragment_outputs_
StringRefNull fragment_entry_fn_
Self & define(StringRefNull name, StringRefNull value="")
BuiltinBits builtins_combined() const
Vector< std::array< StringRefNull, 2 >, 0 > defines_
Vector< SpecializationConstant::Value, 8 > values
Vector< gpu::shader::Type, 8 > types