71 result.allocate_texture(domain);
77 bind_material_resources(
shader);
109void ShaderOperation::bind_inputs(gpu::Shader *shader)
114 for (GPUMaterialAttribute &
attribute : attributes) {
119void ShaderOperation::bind_outputs(gpu::Shader *shader)
126void ShaderOperation::construct_material(
void *thunk, GPUMaterial *material)
129 operation->material_ = material;
130 for (
const bNode *
node : operation->node_tree_evaluator_.pixel_compile_unit()) {
131 operation->shader_nodes_.add_new(
node, std::make_unique<ShaderNode>(*
node));
133 operation->link_node_inputs(*
node);
135 operation->shader_nodes_.lookup(
node)->compile(material);
137 operation->populate_results_for_node(*
node);
141void ShaderOperation::link_node_inputs(
const bNode &
node)
143 for (
const bNodeSocket *input :
node.input_sockets()) {
147 this->link_node_input_unavailable(*input);
152 this->link_node_input_unavailable(*input);
159 if (!input_descriptor.implicit_input.has_value()) {
161 this->link_node_input_constant(*input);
164 this->link_node_input_implicit(*input);
172 this->link_node_input_internal(*input, *
output);
178 this->link_node_input_external(*input, *
output);
182 this->convert_input_link_type(*input, *
output);
210 return "set_float4x4";
214 return "set_quaternion";
234void ShaderOperation::link_node_input_unavailable(
const bNodeSocket &input)
254 switch (input.
type) {
337void ShaderOperation::link_node_input_constant(
const bNodeSocket &input)
344 GPU_link(material_,
"set_float4x4_default", &stack.
link);
355void ShaderOperation::link_node_input_implicit(
const bNodeSocket &input)
357 ShaderNode &
node = *shader_nodes_.lookup(&input.owner_node());
358 GPUNodeStack &stack =
node.get_input(input.identifier);
361 const ImplicitInputType implicit_input = input_descriptor.implicit_input.value();
365 if (implicit_input_to_material_attribute_map_.contains(implicit_input)) {
372 existing_input_descriptor.domain_priority =
math::min(
373 existing_input_descriptor.domain_priority, input_descriptor.domain_priority);
377 stack.link = implicit_input_to_material_attribute_map_.lookup(implicit_input);
382 const std::string input_identifier =
"implicit_input" + std::to_string(implicit_input_index);
392 GPUNodeLink *attribute_link;
399 implicit_input_to_material_attribute_map_.add(implicit_input, attribute_link);
403 stack.link = attribute_link;
406void ShaderOperation::link_node_input_internal(
const bNodeSocket &input_socket,
407 const bNodeSocket &output_socket)
409 ShaderNode &output_node = *shader_nodes_.lookup(&output_socket.owner_node());
410 GPUNodeStack &output_stack = output_node.get_output(output_socket.identifier);
412 ShaderNode &input_node = *shader_nodes_.lookup(&input_socket.owner_node());
413 GPUNodeStack &input_stack = input_node.get_input(input_socket.identifier);
415 input_stack.link = output_stack.link;
469void ShaderOperation::link_node_input_external(
const bNodeSocket &input_socket,
472 ShaderNode &
node = *shader_nodes_.lookup(&input_socket.owner_node());
479 if (
result.is_single_value()) {
482 this->link_node_input_unavailable(input_socket);
494 if (!output_to_material_attribute_map_.contains(&output_socket)) {
496 declare_operation_input(input_socket, output_socket);
506 input_descriptor.domain_priority =
math::min(
507 input_descriptor.domain_priority,
516 stack.
link = output_to_material_attribute_map_.lookup(&output_socket);
519ResultType ShaderOperation::get_source_output_type(
const bNodeSocket &input,
520 const bNodeSocket &
output)
537void ShaderOperation::declare_operation_input(
const bNodeSocket &input_socket,
538 const bNodeSocket &output_socket)
540 const int input_index = output_to_material_attribute_map_.size();
541 std::string input_identifier =
"input" + std::to_string(input_index);
546 input_descriptor.type = this->get_source_output_type(input_socket, output_socket);
554 GPUNodeLink *attribute_link;
561 output_to_material_attribute_map_.add(&output_socket, attribute_link);
574void ShaderOperation::populate_results_for_node(
const bNode &
node)
578 const bool needs_node_previews = flag_is_set(this->
context().needed_side_effect_output_types(),
580 const bNodeSocket *preview_output =
nullptr;
581 if (node_needs_preview && needs_node_previews) {
585 for (
const bNodeSocket *
output :
node.output_sockets()) {
599 const bool is_preview_output =
output == preview_output;
600 if (is_preview_output) {
604 if (is_operation_output || is_preview_output) {
605 populate_operation_result(*
output);
614 return "node_compositor_store_output_float";
616 return "node_compositor_store_output_float2";
618 return "node_compositor_store_output_float3";
620 return "node_compositor_store_output_float4";
622 return "node_compositor_store_output_color";
624 return "node_compositor_store_output_int";
626 return "node_compositor_store_output_int2";
628 return "node_compositor_store_output_int3";
630 return "node_compositor_store_output_int4";
632 return "node_compositor_store_output_bool";
634 return "node_compositor_store_output_float4x4";
636 return "node_compositor_store_output_menu";
638 return "node_compositor_store_output_quaternion";
658void ShaderOperation::populate_operation_result(
const bNodeSocket &output_socket)
661 std::string output_identifier =
"output" + std::to_string(output_id);
669 ShaderNode &
node = *shader_nodes_.lookup(&output_socket.owner_node());
681 GPU_link(material_, store_function_name, id_link, output_link, &storer_output_link);
731 if (target_type == source_type) {
735 ShaderNode &input_node = *shader_nodes_.lookup(&input.owner_node());
736 GPUNodeStack &input_stack = input_node.get_input(input.
identifier);
748 const UString function_name =
UString(
754 float luminance_coefficients[3];
757 function_name.c_str(),
763 GPU_link(material_, function_name.c_str(), input_stack.link, &input_stack.link);
767using namespace gpu::shader;
769void ShaderOperation::generate_code(
void *thunk,
770 GPUMaterial *material,
771 GPUCodegenOutput *code_generator_output)
774 ShaderCreateInfo &shader_create_info = *
reinterpret_cast<ShaderCreateInfo *
>(
775 code_generator_output->create_info);
777 shader_create_info.local_group_size(16, 16);
780 shader_create_info.typedef_source(
"gpu_shader_compositor_code_generation.glsl");
786 shader_create_info.compute_source(
"gpu_shader_compositor_main.glsl");
788 std::string generated_resource_header = shader_create_info.typedef_source_generated;
790 generated_resource_header +=
"#ifdef CREATE_INFO_RES_PASS_compositor_nodetrees\n";
791 generated_resource_header +=
"CREATE_INFO_RES_PASS_compositor_nodetrees\n";
792 generated_resource_header +=
"#endif\n";
793 generated_resource_header +=
"#ifdef CREATE_INFO_RES_BATCH_compositor_nodetrees\n";
794 generated_resource_header +=
"CREATE_INFO_RES_BATCH_compositor_nodetrees\n";
795 generated_resource_header +=
"#endif\n";
796 generated_resource_header +=
"#ifdef CREATE_INFO_RES_GEOMETRY_compositor_nodetrees\n";
797 generated_resource_header +=
"CREATE_INFO_RES_GEOMETRY_compositor_nodetrees\n";
798 generated_resource_header +=
"#endif\n";
799 generated_resource_header +=
"\n";
801 shader_create_info.generated_sources.append(
802 {
"gpu_shader_compositor_nodetree_type.glsl", {}, generated_resource_header});
804 std::string store_code = operation->generate_code_for_outputs(shader_create_info);
805 shader_create_info.generated_sources.append(
806 {
"gpu_shader_compositor_store.glsl", {}, store_code});
808 std::string eval_code;
809 eval_code +=
"void evaluate()\n{\n";
811 eval_code += operation->generate_code_for_inputs(material, shader_create_info);
813 eval_code += code_generator_output->composite.serialized;
819 for (
auto &res : shader_create_info.batch_resources_) {
820 res.info_name =
"compositor_nodetrees";
822 for (
auto &res : shader_create_info.pass_resources_) {
823 res.info_name =
"compositor_nodetrees";
825 for (
auto &res : shader_create_info.geometry_resources_) {
826 res.info_name =
"compositor_nodetrees";
829 Vector<StringRefNull> dependencies = code_generator_output->composite.dependencies;
830 dependencies.prepend(
"compositor_node_tree_infos.hh");
831 dependencies.prepend(
"gpu_shader_compositor_texture_utilities.glsl");
832 dependencies.prepend(
"gpu_shader_compositor_code_generation.glsl");
834 shader_create_info.generated_sources.append(
835 {
"gpu_shader_compositor_eval.glsl", dependencies, eval_code});
844 return "vec4(value)";
846 return "vec4(value, 0.0f, 0.0f)";
848 return "vec4(value, 0.0f)";
854 return "ivec4(value)";
856 return "ivec4(value, 0, 0)";
858 return "ivec4(value, 0)";
860 return "ivec4(value)";
862 return "ivec4(int(value))";
866 return "ivec4(value)";
897 return ImageType::Float2D;
904 return ImageType::Int2D;
906 return ImageType::Float2DArray;
923 return ImageType::Float2D;
926std::string ShaderOperation::generate_code_for_outputs(
ShaderCreateInfo &shader_create_info)
928 const std::string store_float_function_header =
"void store_float(const uint id, float value)";
929 const std::string store_float2_function_header =
"void store_float2(const uint id, vec2 value)";
930 const std::string store_float3_function_header =
"void store_float3(const uint id, vec3 value)";
931 const std::string store_float4_function_header =
"void store_float4(const uint id, vec4 value)";
932 const std::string store_color_function_header =
"void store_color(const uint id, vec4 value)";
933 const std::string store_int_function_header =
"void store_int(const uint id, int value)";
934 const std::string store_int2_function_header =
"void store_int2(const uint id, ivec2 value)";
935 const std::string store_int3_function_header =
"void store_int3(const uint id, ivec3 value)";
936 const std::string store_int4_function_header =
"void store_int4(const uint id, ivec4 value)";
937 const std::string store_bool_function_header =
"void store_bool(const uint id, bool value)";
938 const std::string store_float4x4_function_header =
939 "void store_float4x4(const uint id, float4x4 value)";
940 const std::string store_menu_function_header =
"void store_menu(const uint id, int value)";
941 const std::string store_quaternion_function_header =
942 "void store_quaternion(const uint id, vec4 value)";
947 std::stringstream store_float_function;
948 std::stringstream store_float2_function;
949 std::stringstream store_float3_function;
950 std::stringstream store_float4_function;
951 std::stringstream store_color_function;
952 std::stringstream store_int_function;
953 std::stringstream store_int2_function;
954 std::stringstream store_int3_function;
955 std::stringstream store_int4_function;
956 std::stringstream store_bool_function;
957 std::stringstream store_float4x4_function;
958 std::stringstream store_menu_function;
959 std::stringstream store_quaternion_function;
960 const std::string store_function_start =
"\n{\n switch (id) {\n";
961 store_float_function << store_float_function_header << store_function_start;
962 store_float2_function << store_float2_function_header << store_function_start;
963 store_float3_function << store_float3_function_header << store_function_start;
964 store_float4_function << store_float4_function_header << store_function_start;
965 store_color_function << store_color_function_header << store_function_start;
966 store_int_function << store_int_function_header << store_function_start;
967 store_int2_function << store_int2_function_header << store_function_start;
968 store_int3_function << store_int3_function_header << store_function_start;
969 store_int4_function << store_int4_function_header << store_function_start;
970 store_bool_function << store_bool_function_header << store_function_start;
971 store_float4x4_function << store_float4x4_function_header << store_function_start;
972 store_menu_function << store_menu_function_header << store_function_start;
973 store_quaternion_function << store_quaternion_function_header << store_function_start;
975 shader_create_info.
builtins(BuiltinBits::GLOBAL_INVOCATION_ID);
977 int output_index = 0;
982 shader_create_info.
image(output_index,
983 result.get_gpu_texture_format(),
990 const std::string case_identifier =
StringRef(output_identifier).drop_known_prefix(
"output");
991 const std::string case_line =
" case " + case_identifier +
":\n";
992 const std::string break_line =
" break;\n";
997 std::stringstream common_case_code;
998 common_case_code << case_line;
999 common_case_code <<
" imageStore(" << output_identifier
1000 <<
", ivec2(gl_GlobalInvocationID.xy), " << store_expression <<
");\n";
1001 common_case_code << break_line;
1006 store_float_function << common_case_code.str();
1009 store_float2_function << common_case_code.str();
1012 store_float3_function << common_case_code.str();
1015 store_float4_function << common_case_code.str();
1018 store_color_function << common_case_code.str();
1021 store_int_function << common_case_code.str();
1024 store_int2_function << common_case_code.str();
1027 store_int3_function << common_case_code.str();
1030 store_int4_function << common_case_code.str();
1033 store_bool_function << common_case_code.str();
1037 store_float4x4_function << case_line;
1038 store_float4x4_function <<
" for (int i = 0; i < 4; i++) {\n";
1039 store_float4x4_function <<
" imageStore(" << output_identifier
1040 <<
", ivec3(gl_GlobalInvocationID.xy, i), value[i]);\n";
1041 store_float4x4_function <<
" }\n";
1042 store_float4x4_function << break_line;
1045 store_menu_function << common_case_code.str();
1048 store_quaternion_function << common_case_code.str();
1067 const std::string store_function_end =
" }\n}\n\n";
1068 store_float_function << store_function_end;
1069 store_float2_function << store_function_end;
1070 store_float3_function << store_function_end;
1071 store_float4_function << store_function_end;
1072 store_color_function << store_function_end;
1073 store_int_function << store_function_end;
1074 store_int2_function << store_function_end;
1075 store_int3_function << store_function_end;
1076 store_int4_function << store_function_end;
1077 store_bool_function << store_function_end;
1078 store_float4x4_function << store_function_end;
1079 store_menu_function << store_function_end;
1080 store_quaternion_function << store_function_end;
1082 return store_float_function.str() + store_float2_function.str() + store_float3_function.str() +
1083 store_float4_function.str() + store_color_function.str() + store_int_function.str() +
1084 store_int2_function.str() + store_int3_function.str() + store_int4_function.str() +
1085 store_bool_function.str() + store_float4x4_function.str() + store_menu_function.str() +
1086 store_quaternion_function.str();
1187std::string ShaderOperation::generate_code_for_inputs(
GPUMaterial *material,
1193 if (attributes.is_empty()) {
1203 int input_slot_location = textures.count();
1204 for (GPUMaterialAttribute &
attribute : attributes) {
1206 shader_create_info.
sampler(input_slot_location,
1210 input_slot_location++;
1216 std::stringstream declare_attributes;
1217 declare_attributes <<
"struct {\n";
1218 for (GPUMaterialAttribute &
attribute : attributes) {
1221 declare_attributes <<
" " << type <<
" v" <<
attribute.id <<
";\n";
1223 declare_attributes <<
"} var_attrs;\n\n";
1225 code += declare_attributes.str();
1229 shader_create_info.
typedef_source(
"gpu_shader_compositor_texture_utilities.glsl");
1233 std::stringstream initialize_attributes;
1234 for (GPUMaterialAttribute &
attribute : attributes) {
1236 initialize_attributes <<
"var_attrs.v" <<
attribute.id <<
" = ";
1238 initialize_attributes <<
"texture_load_float4x4(" <<
attribute.name
1239 <<
", ivec2(gl_GlobalInvocationID.xy));\n";
1244 initialize_attributes << type <<
"(texture_load(" <<
attribute.name;
1245 initialize_attributes <<
", ivec2(gl_GlobalInvocationID.xy))." << swizzle <<
");\n";
1248 initialize_attributes <<
"\n";
1250 code += initialize_attributes.str();
#define BLI_assert_unreachable()
const Value & lookup(const Key &key) const
void add_new(const Key &key, const Value &value)
bool contains(const Key &key) const
virtual ResultPrecision get_precision() const
Result & get_result_from_output_socket(const bNodeSocket &output)
PixelCompileUnit & pixel_compile_unit()
const Schedule & schedule()
Result & get_result(StringRef identifier)
void populate_result(StringRef identifier, ResultType type)
Context & context() const
Result & get_input(StringRef identifier) const
virtual Domain compute_domain()
InputDescriptor & get_input_descriptor(StringRef identifier)
void declare_input_descriptor(StringRef identifier, InputDescriptor descriptor)
Map< ImplicitInputType, std::string > implicit_inputs_to_input_identifiers_map_
VectorSet< const bNodeSocket * > preview_outputs_
Map< std::string, const bNodeSocket * > inputs_to_linked_outputs_map_
NodeTreeEvaluator & node_tree_evaluator_
Map< const bNodeSocket *, std::string > output_sockets_to_output_identifiers_map_
Map< std::string, int > inputs_to_reference_counts_map_
PixelOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context, const bool is_single_value)
Map< const bNodeSocket *, std::string > outputs_to_declared_inputs_map_
static const char * type_name(const ResultType type)
static bool is_single_value_only_type(ResultType type)
static ResultType type(gpu::TextureFormat format)
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
static const CPPType & cpp_type(const ResultType type)
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
~ShaderOperation() override
ShaderOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context)
#define storage(slot, qualifiers)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const DataTypeConversions & get_implicit_type_conversions()
int compute_output_reference_count(const bNodeSocket &output, const Schedule &schedule, const VectorSet< const bNode * > *ignored_nodes=nullptr)
static GPUNodeLink * get_result_single_value_link(const Result &result)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
const bNodeSocket * find_preview_output_socket(const bNode &node)
bool is_node_preview_needed(const bNode &node)
const bNodeSocket * get_output_linked_to_input(const bNodeSocket &input)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
static const char * glsl_store_expression_from_result_type(ResultType type)
static GPUNodeLink * get_input_value_link(const bNodeSocket &input, GPUNodeStack &stack)
static const char * glsl_swizzle_from_result_type(ResultType type)
static ImageType gpu_image_type_from_result_type(const ResultType type)
static const char * glsl_type_from_result_type(ResultType type)
ResultType get_node_socket_result_type(const bNodeSocket *socket)
static GPUNodeLink * get_default_input_value_link(const ResultType type)
InputDescriptor input_descriptor_from_input_socket(const bNodeSocket *socket)
static const char * get_store_function_name(ResultType type)
bool is_socket_available(const bNodeSocket *socket)
static const char * get_set_function_name(const ResultType type)
QuaternionBase< float > Quaternion
QuaternionBase< T > to_quaternion(const AxisAngleBase< T, AngleT > &axis_angle)
EulerXYZBase< float > EulerXYZ
T min(const T &a, const T &b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
void GPU_texture_unbind_all()
gpu::Shader * GPU_material_get_shader(GPUMaterial *material)
gpu::UniformBuf * GPU_material_uniform_buffer_get(GPUMaterial *material)
GPUNodeLink * GPU_uniform(const float *num)
GPUNodeStackValue GPU_node_stack_default_value(const GPUType type)
void GPU_material_add_output_link_composite(GPUMaterial *material, GPUNodeLink *link)
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
ListBaseT< GPUMaterialAttribute > GPU_material_attributes(const GPUMaterial *material)
GPUMaterial * GPU_material_from_callbacks(eGPUMaterialEngine engine, ConstructGPUMaterialFn construct_function_cb, GPUCodegenCallbackFn generate_code_function_cb, void *thunk, const uint64_t uuid)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_uniformbuf_bind(gpu::UniformBuf *ubo, int slot)
void GPU_debug_group_end()
ListBaseT< GPUMaterialTexture > GPU_material_textures(GPUMaterial *material)
void GPU_material_free_single(GPUMaterial *material)
void GPU_texture_bind(gpu::Texture *texture, int unit)
int GPU_shader_get_ubo_binding(gpu::Shader *shader, const char *name)
void GPU_texture_image_unbind_all()
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
void GPU_uniformbuf_debug_unbind_all()
void GPU_debug_group_begin(const char *name)
const T * base_ptr() const
Set< const bNodeSocket * > unneeded_inputs
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...
Self & image(int slot, TextureFormat format, Qualifier qualifiers, ImageReadWriteType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)
Self & typedef_source(StringRefNull filename)
Self & builtins(BuiltinBits builtin, ConditionFn cond=nullptr)
Self & sampler(int slot, ImageType type, StringRefNull name, Frequency freq=Frequency::PASS, ConditionFn cond=nullptr)