13#include <fmt/format.h>
41#define SHADER_SOURCE(filename_underscore, filename, filepath) \
42 extern char datatoc_##filename_underscore[];
43#include "glsl_compositor_source_list.h"
44#include "glsl_draw_source_list.h"
45#include "glsl_eevee_source_list.h"
46#include "glsl_gpencil_source_list.h"
47#include "glsl_gpu_source_list.h"
48#include "glsl_image_source_list.h"
49#include "glsl_ocio_source_list.h"
50#include "glsl_overlay_source_list.h"
51#include "glsl_select_id_source_list.h"
52#include "glsl_workbench_source_list.h"
54# include "glsl_osd_source_list.h"
61namespace gpu::shader {
110#if GPU_SHADER_PRINTF_ENABLE
220 "Input source should have no comments.");
242 if (format_map->
contains(format_hash)) {
244 print_error(format, 0,
"printf format hash collision.");
256 format = std::regex_replace(format, std::regex(R
"(\\n)"), "\n");
257 format = std::regex_replace(format, std::regex(R
"(\\v)"), "\v");
258 format = std::regex_replace(format, std::regex(R
"(\\t)"), "\t");
259 format = std::regex_replace(format, std::regex(R
"(\\')"), "\'");
260 format = std::regex_replace(format, std::regex(R
"(\\")"), "\"");
261 format = std::regex_replace(format, std::regex(R
"(\\\\)"), "\\");
265 int64_t start = 0, end = 0, cursor = -1;
266 while ((end = format.find_first_of(
'%', cursor + 1)) != -1) {
267 if (end - start > 0) {
269 fmt.
format_blocks.append({type, format.substr(start, end - start)});
273 switch (format[end + 1]) {
295 fmt.
format_blocks.append({type, format.substr(start, format.size() - start)});
297 format_map->
add(format_hash, fmt);
305 GPUFunction *func = MEM_new<GPUFunction>(__func__);
307 func->
source =
reinterpret_cast<void *
>(
this);
309 for (
auto arg : arguments) {
324 if (other_source !=
this) {
325 const char *msg =
"Function redefinition or overload in two different files ...";
329 "... previous definition was here");
350 std::cerr <<
" error: " << message <<
"\n";
352 << input.
substr(line_start, line_end - line_start) <<
"\n";
363 if (this->dependencies_init) {
366 this->dependencies_init =
true;
379 if (dependency_source ==
nullptr) {
380 std::string error = std::string(
"Dependency not found : ") + dependency_name;
404#define CLOG_FILE_INCLUDE(_from, _include) \
405 if (CLOG_CHECK(&LOG, CLG_LEVEL_TRACE) && \
406 (from).filename.c_str() != (_include).filename.c_str()) \
408 const char *from_filename = (_from).filename.c_str(); \
409 const char *include_filename = (_include).filename.c_str(); \
410 const int from_size = int((_from).source.size()); \
411 const int include_size = int((_include).source.size()); \
412 std::string link = fmt::format( \
413 "{}_{} --> {}_{}\n", from_filename, from_size, include_filename, include_size); \
414 std::string style = fmt::format("style {}_{} fill:#{:x}{:x}0\n", \
417 min_uu(15, include_size / 1000), \
418 15 - min_uu(15, include_size / 1000)); \
419 CLG_log_raw(LOG.type, link.c_str()); \
420 CLG_log_raw(LOG.type, style.c_str()); \
428 if (generated_src.filename == this->filename) {
429 gen_src = &generated_src;
439 if (source_content.c_str() == (gen_src ? gen_src->
content.c_str() : this->source.c_str())) {
447 included.append(this->source);
448 for (
const auto &dependency : this->dependencies) {
450 dependency->source_get(
result, included, generated_sources, dict, *
this);
453 result.append(this->source);
458 included.append(gen_src->
content);
460 for (
const auto &dependency_name : gen_src->
dependencies) {
461 BLI_assert_msg(dependency_name != this->filename,
"Recursive include");
464 if (dependency_source ==
nullptr) {
466 std::cerr <<
"Generated dependency not found : " + dependency_name << std::endl;
470 dependency_source->
source_get(
result, included, generated_sources, dict, *
this);
477 std::cerr <<
"warn: Generated source not provided. Using fallback for : " << this->filename
479 included.append(this->source);
482 for (
const auto &dependency : this->dependencies) {
484 dependency->source_get(
result, included, generated_sources, dict, *
this);
487 result.append(this->source);
504 out_builtins |= dep->builtins_get();
511 return (
filename.startswith(
"gpu_shader_material_") ||
512 filename.startswith(
"gpu_shader_common_") ||
513 filename.startswith(
"gpu_shader_compositor_")) &&
520#include "glsl_compositor_metadata_list.hh"
521#include "glsl_draw_metadata_list.hh"
522#include "glsl_eevee_metadata_list.hh"
523#include "glsl_gpencil_metadata_list.hh"
524#include "glsl_gpu_metadata_list.hh"
525#include "glsl_image_metadata_list.hh"
526#include "glsl_ocio_metadata_list.hh"
527#include "glsl_overlay_metadata_list.hh"
528#include "glsl_select_id_metadata_list.hh"
529#include "glsl_workbench_metadata_list.hh"
530#ifdef WITH_OPENSUBDIV
531# include "glsl_osd_metadata_list.hh"
538using namespace blender::gpu;
545#ifdef WITH_OPENSUBDIV
547static std::string osd_patch_basis;
556#define SHADER_SOURCE(filename_underscore, filename, filepath) \
557 g_sources->add_new(filename, \
558 new GPUSource(filepath, \
560 datatoc_##filename_underscore, \
563 gpu::shader::metadata_##filename_underscore));
565#include "glsl_gpu_source_list.h"
567#include "glsl_compositor_source_list.h"
568#include "glsl_draw_source_list.h"
569#include "glsl_eevee_source_list.h"
570#include "glsl_gpencil_source_list.h"
571#include "glsl_image_source_list.h"
572#include "glsl_ocio_source_list.h"
573#include "glsl_overlay_source_list.h"
574#include "glsl_select_id_source_list.h"
575#include "glsl_workbench_source_list.h"
576#ifdef WITH_OPENSUBDIV
577# include "glsl_osd_source_list.h"
580#ifdef WITH_OPENSUBDIV
586 if (source_ptr_opt) {
587 delete source_ptr_opt.value();
590 "osd_patch_basis.glsl",
592 "osd_patch_basis.glsl",
593 (osd_patch_basis).c_str(),
601 errors += value->init_dependencies(*
g_sources);
603 BLI_assert_msg(errors == 0,
"Dependency errors detected: Aborting");
606#if GPU_SHADER_PRINTF_ENABLE
612 if (value->filename.startswith(
"gpu_shader_material_")) {
641 BLI_assert_msg(function !=
nullptr,
"Requested function not in the function library");
648 BLI_assert_msg(function !=
nullptr,
"Requested function not in the function library");
674 if (shader_source_name.
is_empty()) {
678 std::cerr <<
"Error: Could not find \"" << shader_source_name
679 <<
"\" in the list of registered source.\n";
690 if (shader_source_name.
is_empty()) {
694 std::cerr <<
"Error: Could not find \"" << shader_source_name
695 <<
"\" in the list of registered source.\n";
710 if (src ==
nullptr) {
711 std::cerr <<
"Error source not found : " << shader_source_name << std::endl;
727 if (src ==
nullptr) {
728 std::cerr <<
"Error source not found : " << shader_source_name << std::endl;
736 if (source->source == source_string) {
737 return source->filename;
void CLG_log_raw(const CLG_LogType *lg, const char *message)
#define CLOG_CHECK(clg_ref, verbose_level,...)
#define CLOG_TRACE(clg_ref,...)
__forceinline const float4 insert(const float4 &a, const float b)
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
std::optional< Value > pop_try(const Key &key)
ValueIterator values() const &
bool add(const Key &key, const Value &value)
const Value & lookup(const Key &key) const
Value lookup_default(const Key &key, const Value &default_value) const
void add_new(const Key &key, const Value &value)
bool contains(const Key &key) const
constexpr const char * c_str() const
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
constexpr const char * end() const
constexpr const char * begin() const
constexpr bool is_empty() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
constexpr const char * c_str() const
const char * openSubdiv_getGLSLPatchBasisSource()
#define CLOG_FILE_INCLUDE(_from, _include)
size_t line_number(const std::string_view &str, size_t pos)
size_t char_number(const std::string_view &str, size_t pos)
Vector< shader::GeneratedSource, 0 > GeneratedSourceList
static bool g_shader_use_printf
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)
Span< ShaderCreateInfo::SharedVariable > gpu_shader_dependency_get_shared_variables(const StringRefNull shader_source_name)
const PrintfFormat & gpu_shader_dependency_get_printf_format(uint32_t format_hash)
BuiltinBits gpu_shader_dependency_get_builtins(const StringRefNull shader_source_name)
bool gpu_shader_dependency_has_printf()
bool gpu_shader_dependency_force_gpu_print_injection()
StringRefNull gpu_shader_dependency_get_filename_from_source_string(const StringRef source_string)
Find the name of the file from which the given string was generated.
StringRefNull gpu_shader_dependency_get_source(const StringRefNull shader_source_name)
Map< StringRef, GPUSource * > GPUSourceDictionary
Map< uint32_t, shader::PrintfFormat > GPUPrintFormatMap
Map< StringRef, GPUFunction * > GPUFunctionDictionary
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static GPUPrintFormatMap * g_formats
void gpu_material_library_use_function(Set< StringRefNull > &used_libraries, const char *name)
void gpu_shader_dependency_exit()
GPUFunction * gpu_material_library_get_function(const char *name)
static bool force_printf_injection
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static GPUSourceDictionary * g_sources
void gpu_shader_dependency_init()
static GPUFunctionDictionary * g_functions
GPUFunctionQual paramqual[MAX_PARAMETER]
char name[MAX_FUNCTION_NAME]
GPUType paramtype[MAX_PARAMETER]
Vector< GPUSource * > dependencies
bool is_from_material_library() const
void add_function(StringRefNull name, Span< shader::metadata::ArgumentFormat > arguments, GPUFunctionDictionary *g_functions)
GPUType convert_type(shader::metadata::Type type)
void build(Vector< StringRefNull > &result, const shader::GeneratedSourceList &generated_sources, const GPUSourceDictionary &dict) const
void source_get(Vector< StringRefNull > &result, Vector< StringRefNull > &included, const shader::GeneratedSourceList &generated_sources, const GPUSourceDictionary &dict, const GPUSource &from) const
Vector< shader::ShaderCreateInfo::SharedVariable, 0 > shared_variables
shader::BuiltinBits builtins_get() const
void add_shared_variable(const shader::Type type, const StringRefNull name)
void add_dependency(StringRef line)
Vector< StringRef > dependencies_names
GPUFunctionQual convert_qualifier(shader::metadata::Qualifier qualifier)
int init_dependencies(const GPUSourceDictionary &dict)
void add_builtin(shader::metadata::Builtin builtin)
std::string patched_source
shader::BuiltinBits builtins
void add_printf_format(uint32_t format_hash, std::string format, GPUPrintFormatMap *format_map)
void print_error(const StringRef &input, int64_t offset, const StringRef message)
GPUSource(const char *path, const char *file, const char *datatoc, GPUFunctionDictionary *g_functions, GPUPrintFormatMap *g_formats, std::function< void(GPUSource &, GPUFunctionDictionary *, GPUPrintFormatMap *)> metadata_fn)
Vector< StringRefNull > dependencies