13#include <fmt/format.h>
37#define SHADER_SOURCE(filename_underscore, filename, filepath) \
38 extern char datatoc_##filename_underscore[];
39#include "glsl_compositor_source_list.h"
40#include "glsl_draw_source_list.h"
41#include "glsl_gpu_source_list.h"
42#include "glsl_ocio_source_list.h"
44# include "glsl_osd_source_list.h"
114#if GPU_SHADER_PRINTF_ENABLE
197 if (format_map->
contains(format_hash)) {
211 format = std::regex_replace(
format, std::regex(R
"(\\n)"), "\n");
212 format = std::regex_replace(
format, std::regex(R
"(\\v)"), "\v");
213 format = std::regex_replace(
format, std::regex(R
"(\\t)"), "\t");
214 format = std::regex_replace(
format, std::regex(R
"(\\')"), "\'");
215 format = std::regex_replace(
format, std::regex(R
"(\\")"), "\"");
216 format = std::regex_replace(
format, std::regex(R
"(\\\\)"), "\\");
221 while ((end =
format.find_first_of(
'%', start + 1)) != -1) {
226 switch (
format[end + 1]) {
246 format_map->
add(format_hash, fmt);
254 GPUFunction *func = MEM_new<GPUFunction>(__func__);
255 name.copy_utf8_truncated(func->
name,
sizeof(func->
name));
256 func->
source =
reinterpret_cast<void *
>(
this);
258 for (
auto arg : arguments) {
273 if (other_source !=
this) {
274 const char *msg =
"Function redefinition or overload in two different files ...";
278 "... previous definition was here");
293 int64_t char_number = offset - line_start + 1;
297 std::cerr <<
fullpath <<
":" << line_number <<
":" << char_number;
299 std::cerr <<
" error: " << message <<
"\n";
300 std::cerr << std::setw(5) << line_number <<
" | "
301 <<
input.substr(line_start, line_end - line_start) <<
"\n";
303 for (
int64_t i = 0;
i < char_number - 1;
i++) {
312 if (this->dependencies_init) {
315 this->dependencies_init =
true;
328 if (dependency_source ==
nullptr) {
329 std::string
error = std::string(
"Dependency not found : ") + dependency_name;
350#define CLOG_FILE_INCLUDE(_from, _include) \
351 if (CLOG_CHECK(&LOG, CLG_LEVEL_INFO) && (from).filename.c_str() != (_include).filename.c_str()) \
353 const char *from_filename = (_from).filename.c_str(); \
354 const char *include_filename = (_include).filename.c_str(); \
355 const int from_size = int((_from).source.size()); \
356 const int include_size = int((_include).source.size()); \
357 std::string link = fmt::format( \
358 "{}_{} --> {}_{}\n", from_filename, from_size, include_filename, include_size); \
359 std::string style = fmt::format("style {}_{} fill:#{:x}{:x}0\n", \
362 min_uu(15, include_size / 1000), \
363 15 - min_uu(15, include_size / 1000)); \
364 CLG_log_raw(LOG.type, link.c_str()); \
365 CLG_log_raw(LOG.type, style.c_str()); \
372 if (source_content.c_str() == this->source.c_str()) {
380 for (
const auto &dependency : this->dependencies) {
382 dependency->source_get(
result, generated_sources, dict, *
this);
385 result.append(this->source);
392 if (generated_src.filename == this->filename) {
394 for (
const auto &dependency_name : generated_src.dependencies) {
395 BLI_assert_msg(dependency_name != this->filename,
"Recursive include");
398 if (dependency_source ==
nullptr) {
400 std::cerr <<
"Generated dependency not found : " + dependency_name << std::endl;
407 result.append(generated_src.content);
412 std::cerr <<
"warn: Generated source not provided. Using fallback for : " << this->filename
416 for (
const auto &dependency : this->dependencies) {
418 dependency->source_get(
result, generated_sources, dict, *
this);
421 result.append(this->source);
436 out_builtins |= dep->builtins_get();
443 return (
filename.startswith(
"gpu_shader_material_") ||
444 filename.startswith(
"gpu_shader_common_") ||
445 filename.startswith(
"gpu_shader_compositor_")) &&
452#include "glsl_compositor_metadata_list.hh"
453#include "glsl_draw_metadata_list.hh"
454#include "glsl_gpu_metadata_list.hh"
455#include "glsl_ocio_metadata_list.hh"
456#ifdef WITH_OPENSUBDIV
457# include "glsl_osd_metadata_list.hh"
477#define SHADER_SOURCE(filename_underscore, filename, filepath) \
478 g_sources->add_new(filename, \
479 new GPUSource(filepath, \
481 datatoc_##filename_underscore, \
484 blender::gpu::shader::metadata_##filename_underscore));
486#include "glsl_compositor_source_list.h"
487#include "glsl_draw_source_list.h"
488#include "glsl_gpu_source_list.h"
489#include "glsl_ocio_source_list.h"
490#ifdef WITH_OPENSUBDIV
491# include "glsl_osd_source_list.h"
494#ifdef WITH_OPENSUBDIV
497 "osd_patch_basis.glsl",
499 "osd_patch_basis.glsl",
500 patch_basis_source.
c_str(),
507 for (
auto *value :
g_sources->values()) {
508 errors += value->init_dependencies(*
g_sources);
510 BLI_assert_msg(errors == 0,
"Dependency errors detected: Aborting");
513#if GPU_SHADER_PRINTF_ENABLE
517 for (
auto *value :
g_sources->values()) {
519 if (value->filename.startswith(
"gpu_shader_material_")) {
531 for (
auto *value :
g_sources->values()) {
548 BLI_assert_msg(function !=
nullptr,
"Requested function not in the function library");
556 BLI_assert_msg(function !=
nullptr,
"Requested function not in the function library");
582 if (shader_source_name.
is_empty()) {
585 if (
g_sources->contains(shader_source_name) ==
false) {
586 std::cerr <<
"Error: Could not find \"" << shader_source_name
587 <<
"\" in the list of registered source.\n";
602 if (src ==
nullptr) {
603 std::cerr <<
"Error source not found : " << shader_source_name << std::endl;
605 CLOG_INFO(&
LOG,
"Resolved Source Tree (Mermaid flowchart) %s", shader_name.
c_str());
619 if (src ==
nullptr) {
620 std::cerr <<
"Error source not found : " << shader_source_name << std::endl;
627 for (
auto &source :
g_sources->values()) {
628 if (source->source == source_string) {
629 return source->filename;
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
void BLI_kdtree_nd_ insert(KDTree *tree, int index, const float co[KD_DIMS]) ATTR_NONNULL(1
#define UNUSED_VARS_NDEBUG(...)
void CLG_log_raw(const CLG_LogType *lg, const char *message)
#define CLOG_CHECK(clg_ref, verbose_level,...)
#define CLOG_INFO(clg_ref,...)
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
bool contains(const Key &key) const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr const char * begin() const
constexpr const char * end() const
constexpr bool is_empty() const
constexpr const char * c_str() const
const char * openSubdiv_getGLSLPatchBasisSource()
static bool force_printf_injection
void gpu_shader_dependency_init()
#define CLOG_FILE_INCLUDE(_from, _include)
void gpu_material_library_use_function(blender::Set< blender::StringRefNull > &used_libraries, const char *name)
void gpu_shader_dependency_exit()
static GPUSourceDictionary * g_sources
static GPUPrintFormatMap * g_formats
GPUFunction * gpu_material_library_get_function(const char *name)
static GPUFunctionDictionary * g_functions
static void error(const char *str)
Vector< shader::GeneratedSource, 0 > GeneratedSourceList
Vector< StringRefNull > gpu_shader_dependency_get_resolved_source(const StringRefNull shader_source_name, const shader::GeneratedSourceList &generated_sources, const StringRefNull shader_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
char name[MAX_FUNCTION_NAME]
GPUType paramtype[MAX_PARAMETER]
GPUFunctionQual paramqual[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
shader::BuiltinBits builtins_get() const
void source_get(Vector< StringRefNull > &result, const shader::GeneratedSourceList &generated_sources, const GPUSourceDictionary &dict, const GPUSource &from) const
shader::BuiltinBits convert_builtin_bit(shader::metadata::Builtin builtin)
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)