Blender V5.3
GPU_material.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
8
9#pragma once
10
11#include <string>
12#include <variant>
13
14#include "BLI_assert.hh"
15#include "BLI_enum_flags.hh"
16#include "BLI_math_base_c.hh"
19#include "BLI_set.hh"
20
21#include "DNA_customdata_types.h" /* for eCustomDataType */
22#include "DNA_image_types.h"
23#include "DNA_listBase.h"
24
25#include "GPU_shader.hh" /* for GPUShaderCreateInfo */
26#include "GPU_texture.hh" /* for GPUSamplerState */
27
28namespace blender {
29
30struct GHash;
31struct GPUMaterial;
32struct GPUInput;
33struct GPUNodeLink;
34struct GPUNodeStack;
35struct GPUPass;
36namespace gpu {
37class Texture;
38class UniformBuf;
39} // namespace gpu
40struct Image;
41struct ImageUser;
42struct Main;
43struct Material;
44struct Scene;
45struct bNode;
46struct bNodeTree;
47
51
57
63
64/* GPU_MAT_OPTIMIZATION_SKIP for cases where we do not
65 * plan to perform optimization on a given material. */
71
81 GPU_MATFLAG_AO = (1 << 8),
82 /* Signals the presence of multiple reflection closures. */
83 GPU_MATFLAG_COAT = (1 << 9),
89
92
94 GPU_MATFLAG_AOV = (1 << 19),
95
97 /* Signals that these specific closures might *not* be colorless.
98 * If this flag is not set, all closures are ensured to not be tinted. */
102
103 /* Set if the material uses the "Is Diffuse / Glossy Ray" output of the light path node. */
105
106 /* Signals scene time use. */
108
109 /* Tells the render engine the material was just compiled or updated. */
111};
113
114using GPUCodegenCallbackFn = void (*)(void *thunk,
115 GPUMaterial *mat,
116 struct GPUCodegenOutput *codegen);
122
125
127 enum class WarningType {
128 Error = 0,
129 Warning = 1,
130 Info = 2,
131 };
132
133 struct Error {
134 const bNode *node;
135 std::string message;
137 };
139};
140
143 Material *ma,
144 bNodeTree *ntree,
145 ListBaseT<LinkData> *gpumaterials,
146 const char *name,
147 eGPUMaterialEngine engine,
148 uint64_t shader_uuid,
149 bool deferred_compilation,
150 GPUCodegenCallbackFn callback,
151 void *thunk,
152 GPUMaterialPassReplacementCallbackFn pass_replacement_cb = nullptr);
153
154/* A callback passed to GPU_material_from_callbacks to construct the material graph by adding and
155 * linking the necessary GPU material nodes. */
156using ConstructGPUMaterialFn = void (*)(void *thunk, GPUMaterial *material);
157
158/* Construct a GPU material from a set of callbacks. See the callback types for more information.
159 * The given thunk will be passed as the first parameter of each callback. The UUID identify a
160 * possible variation for the same graph, for instance, a shader whose output is half precision or
161 * full precision. */
163 ConstructGPUMaterialFn construct_function_cb,
164 GPUCodegenCallbackFn generate_code_function_cb,
165 void *thunk,
166 const uint64_t uuid);
167
169void GPU_material_free(ListBaseT<LinkData> *gpumaterial);
170
171void GPU_materials_free(Main *bmain);
172
176
177const char *GPU_material_get_name(GPUMaterial *material);
178
187
192
194
201void GPU_material_uniform_buffer_create(GPUMaterial *material, ListBaseT<LinkData> *inputs);
202
206
208
210
213
214 /* Meaningful part of the attribute set key. */
215 char name[256]; /* Multiple MAX_CUSTOMDATA_LAYER_NAME */
218
219 /* Helper fields used by code generation. */
220 int users;
221};
222
223const ListBaseT<GPULayerAttr> *GPU_material_layer_attributes(const GPUMaterial *material);
224
225/* Requested Material Attributes and Textures */
226
255
256/* Element count of GPU_MAT4. */
257constexpr int GPU_MAX_CONSTANT_DATA = 16;
258
259constexpr int gpu_type_element_count(const GPUType type)
260{
261 switch (type) {
262 case GPU_FLOAT:
263 case GPU_INT:
264 case GPU_BOOL:
265 return 1;
266 case GPU_VEC2:
267 case GPU_INT2:
268 return 2;
269 case GPU_VEC3:
270 case GPU_INT3:
271 return 3;
272 case GPU_VEC4:
273 case GPU_INT4:
274 return 4;
275 case GPU_MAT3:
276 return 9;
277 case GPU_MAT4:
278 return 16;
279 case GPU_NONE:
280 case GPU_TEX1D_ARRAY:
281 case GPU_TEX2D:
282 case GPU_TEX2D_ARRAY:
283 case GPU_TEX3D:
284 case GPU_CLOSURE:
285 case GPU_ATTR:
287 case GPU_SHADING_DATA:
288 break;
289 }
290
292 return 0;
293}
294
296{
297 switch (count) {
298 case 1:
299 return GPU_FLOAT;
300 case 2:
301 return GPU_VEC2;
302 case 3:
303 return GPU_VEC3;
304 case 4:
305 return GPU_VEC4;
306 case 9:
307 return GPU_MAT3;
308 case 16:
309 return GPU_MAT4;
310 }
311
313 return GPU_NONE;
314}
315
316constexpr GPUType gpu_int_type_from_element_count(const int count)
317{
318 switch (count) {
319 case 1:
320 return GPU_INT;
321 case 2:
322 return GPU_INT2;
323 case 3:
324 return GPU_INT3;
325 case 4:
326 return GPU_INT4;
327 }
328
330 return GPU_NONE;
331}
332
337
340 int type; /* eCustomDataType */
341 char name[/*MAX_CUSTOMDATA_LAYER_NAME*/ 68];
342 char input_name[/*GPU_MAX_SAFE_ATTR_NAME + 1*/ 12 + 1];
344 GPUDefaultValue default_value; /* Only for volumes attributes. */
345 int id;
346 int users;
361};
362
370 char sampler_name[32]; /* Name of sampler in GLSL. */
371 char tiled_mapping_name[32]; /* Name of tile mapping sampler in GLSL. */
372 int users;
374};
375
376ListBaseT<GPUMaterialAttribute> GPU_material_attributes(const GPUMaterial *material);
377ListBaseT<GPUMaterialTexture> GPU_material_textures(GPUMaterial *material);
378
381
382 /* Meaningful part of the attribute set key. */
383 char name[/*MAX_CUSTOMDATA_LAYER_NAME*/ 68];
387
388 /* Helper fields used by code generation. */
389 short id;
390 int users;
391};
392
394 ListBaseT<GPUUniformAttr> list;
395
396 /* List length and hash code precomputed for fast lookup and comparison. */
397 unsigned int count, hash_code;
398};
399
401
402/* Functions to create GPU Materials nodes. */
403/* TODO: Move to its own header. */
404
409using GPUNodeStackValue = std::variant<std::monostate,
410 float,
411 float2,
412 float3,
413 float4,
414 float3x3,
415 float4x4,
416 int,
417 int2,
418 int3,
419 int4,
420 bool>;
421
426{
427 switch (type) {
428 case GPU_FLOAT:
429 return 0.0f;
430 case GPU_VEC2:
431 return float2(0.0f);
432 case GPU_VEC3:
433 return float3(0.0f);
434 case GPU_VEC4:
435 return float4(0.0f);
436 case GPU_MAT3:
437 return float3x3::zero();
438 case GPU_MAT4:
439 return float4x4::zero();
440 case GPU_INT:
441 return 0;
442 case GPU_INT2:
443 return int2(0);
444 case GPU_INT3:
445 return int3(0);
446 case GPU_INT4:
447 return int4(0);
448 case GPU_BOOL:
449 return false;
450 case GPU_NONE:
451 case GPU_TEX1D_ARRAY:
452 case GPU_TEX2D:
453 case GPU_TEX2D_ARRAY:
454 case GPU_TEX3D:
455 case GPU_CLOSURE:
456 case GPU_ATTR:
458 case GPU_SHADING_DATA:
459 break;
460 }
461
462 return std::monostate{};
463}
464
467 GPUNodeStackValue value{std::monostate{}};
468 GPUNodeLink *link = nullptr;
469 bool hasinput = false;
470 bool hasoutput = false;
471 short sockettype = 0;
472 bool end = false;
473
474 /* Return true if the socket might contain a polychromatic value.
475 * This is a conservative heuristic that allows for optimization. */
476 bool might_be_tinted() const
477 {
478 if (this->link) {
479 return true;
480 }
481 switch (this->type) {
482 case GPU_VEC3: {
483 const float3 &vec = std::get<float3>(this->value);
484 return (vec[0] != vec[1]) || (vec[1] != vec[2]);
485 }
486 case GPU_VEC4: {
487 const float4 &vec = std::get<float4>(this->value);
488 return (vec[0] != vec[1]) || (vec[1] != vec[2]);
489 }
490 default:
491 break;
492 }
494 return true;
495 }
496
497 bool socket_not_zero() const
498 {
499 if (this->link) {
500 return true;
501 }
502 switch (this->type) {
503 case GPU_FLOAT:
504 return saturate_f(std::get<float>(this->value)) > near_zero;
505 case GPU_INT:
506 return std::get<int>(this->value) != 0;
507 case GPU_BOOL:
508 return std::get<bool>(this->value);
509 default:
510 break;
511 }
513 return true;
514 }
515
516 bool socket_not_one() const
517 {
518 if (this->link) {
519 return true;
520 }
521 switch (this->type) {
522 case GPU_FLOAT:
523 return saturate_f(std::get<float>(this->value)) < near_one;
524 case GPU_INT:
525 return std::get<int>(this->value) != 1;
526 case GPU_BOOL:
527 return !std::get<bool>(this->value);
528 default:
529 break;
530 }
532 return true;
533 }
534
535 bool socket_not_black() const
536 {
537 if (this->link) {
538 return true;
539 }
540 switch (this->type) {
541 case GPU_VEC3: {
542 const float3 &vec = std::get<float3>(this->value);
543 return saturate_f(vec[0]) > near_zero || saturate_f(vec[1]) > near_zero ||
544 saturate_f(vec[2]) > near_zero;
545 }
546 case GPU_VEC4: {
547 const float4 &vec = std::get<float4>(this->value);
548 return saturate_f(vec[0]) > near_zero || saturate_f(vec[1]) > near_zero ||
549 saturate_f(vec[2]) > near_zero;
550 }
551 default:
552 break;
553 }
555 return true;
556 }
557
558 bool socket_not_white() const
559 {
560 if (this->link) {
561 return true;
562 }
563 switch (this->type) {
564 case GPU_VEC3: {
565 const float3 &vec = std::get<float3>(this->value);
566 return saturate_f(vec[0]) < near_one || saturate_f(vec[1]) < near_one ||
567 saturate_f(vec[2]) < near_one;
568 }
569 case GPU_VEC4: {
570 const float4 &vec = std::get<float4>(this->value);
571 return saturate_f(vec[0]) < near_one || saturate_f(vec[1]) < near_one ||
572 saturate_f(vec[2]) < near_one;
573 }
574 default:
575 break;
576 }
578 return true;
579 }
580
581 private:
582 static constexpr float near_zero = 1e-5f;
583 static constexpr float near_one = 1.0f - 1e-5f;
584 float saturate_f(const float f) const
585 {
586 return clamp_f(f, 0.0f, 1.0f);
587 }
588};
589
591 std::string serialized;
593
594 bool empty() const
595 {
596 return serialized.empty();
597 }
598
599 std::string serialized_or_default(std::string value) const
600 {
601 return serialized.empty() ? value : serialized;
602 }
603};
604
617
620GPUNodeLink *GPU_constant(const float *num);
621GPUNodeLink *GPU_uniform(const float *num);
622GPUNodeLink *GPU_constant(const int *num);
623GPUNodeLink *GPU_uniform(const int *num);
624GPUNodeLink *GPU_constant(const bool *num);
625GPUNodeLink *GPU_uniform(const bool *num);
628GPUNodeLink *GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name);
640 eCustomDataType type,
641 const char *name,
642 GPUDefaultValue default_value);
644 const char *name,
645 bool use_dupli,
646 uint32_t *r_hash);
647GPUNodeLink *GPU_layer_attribute(GPUMaterial *mat, const char *name);
649 Image *ima,
650 ImageUser *iuser,
651 GPUSamplerState sampler_state);
653 Image *ima,
654 ImageUser *iuser,
655 GPUSamplerState sampler_state,
656 GPUNodeLink **r_image_tiled_link,
657 GPUNodeLink **r_image_tiled_mapping_link);
659 int width,
660 int height,
661 const float *pixels,
662 float *layer,
663 GPUSamplerState sampler_state);
664GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row);
665
671GPUNodeLink *GPU_differentiate_float_function(const char *function_name, const float filter_width);
672
673bool GPU_link(GPUMaterial *mat, const char *name, ...);
675 const bNode *node,
676 const char *name,
679 ...);
680
681bool GPU_stack_link_zone(GPUMaterial *material,
682 const bNode *bnode,
683 const char *name,
686 int zone_index,
687 bool is_zone_end,
688 int in_argument_count,
689 int out_argument_count);
690
695
697
699
709 GPUType return_type,
710 GPUNodeLink **link);
711
714
715GHash *GPU_uniform_attr_list_hash_new(const char *info);
718
719/* Returns the GPU node stack of the input with the given identifier in the given node within the
720 * given inputs stack array. */
721GPUNodeStack &GPU_node_get_input(const bNode &node, GPUNodeStack inputs[], StringRef identifier);
722
723/* Returns the GPU node stack of the output with the given identifier in the given node within the
724 * given output stack array. */
725GPUNodeStack &GPU_node_get_output(const bNode &node, GPUNodeStack outputs[], StringRef identifier);
726
727/* Returns the GPU node link of the input with the given identifier in the given node within the
728 * given inputs stack array, if the input is not linked, a uniform link carrying the value of the
729 * input will be created and returned. It is expected that the caller will use the returned link in
730 * a GPU material, otherwise, the link may not be properly freed. */
732 GPUNodeStack inputs[],
733 StringRef identifier);
734
735} // namespace blender
These structs are the foundation for all linked lists in the library system.
#define BLI_assert_unreachable()
#define layer
ENUM_OPERATORS(Layer::Flags)
BMesh Iterators.
GPUNodeLink * GPU_uniform_attribute(GPUMaterial *mat, const char *name, bool use_dupli, uint32_t *r_hash)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
unsigned __int64 uint64_t
Definition BKE_text.h:91
bool GPU_material_has_volume_output(GPUMaterial *mat)
VecBase< int32_t, 4 > int4
constexpr GPUType gpu_int_type_from_element_count(const int count)
bool GPU_material_has_displacement_output(GPUMaterial *mat)
constexpr GPUType gpu_float_type_from_element_count(const int count)
constexpr int GPU_MAX_CONSTANT_DATA
GPUNodeLink * GPU_image(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state)
GPUNodeLink * GPU_node_get_input_link(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
void GPU_material_output_surface(GPUMaterial *material, GPUNodeLink *link)
GPUNodeLink * GPU_attribute_with_default(GPUMaterial *mat, eCustomDataType type, const char *name, GPUDefaultValue default_value)
@ GPU_MATFLAG_SHADER_TO_RGBA
@ GPU_MATFLAG_COAT
@ GPU_MATFLAG_SHADOW_OFFSET
@ GPU_MATFLAG_TRANSPARENT_MAYBE_COLORED
@ GPU_MATFLAG_HOLDOUT
@ GPU_MATFLAG_SUBSURFACE
@ GPU_MATFLAG_LIGHT_ATTRIBUTE
@ GPU_MATFLAG_VOLUME_ABSORPTION
@ GPU_MATFLAG_TRANSLUCENT
@ GPU_MATFLAG_SCENE_TIME
@ GPU_MATFLAG_GLOSSY
@ GPU_MATFLAG_REFRACT
@ GPU_MATFLAG_BARYCENTRIC
@ GPU_MATFLAG_EMISSION
@ GPU_MATFLAG_IS_DIFFUSE_OR_GLOSSY_RAY_FLAG
@ GPU_MATFLAG_VOLUME_SCATTER
@ GPU_MATFLAG_DIFFUSE
@ GPU_MATFLAG_LIGHTING
@ GPU_MATFLAG_REFLECTION_MAYBE_COLORED
@ GPU_MATFLAG_RAYCAST
@ GPU_MATFLAG_OBJECT_INFO
@ GPU_MATFLAG_AOV
@ GPU_MATFLAG_UPDATED
@ GPU_MATFLAG_REFRACTION_MAYBE_COLORED
@ GPU_MATFLAG_TRANSPARENT
GPUNodeStack & GPU_node_get_input(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
@ GPU_KERNEL_GLOBALS
@ GPU_MAT_SUCCESS
GPUNodeLink * GPU_layer_attribute(GPUMaterial *mat, const char *name)
gpu::Shader * GPU_material_get_shader(GPUMaterial *material)
MatBase< float, 4, 4 > float4x4
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
void GPU_materials_free(Main *bmain)
gpu::UniformBuf * GPU_material_uniform_buffer_get(GPUMaterial *material)
@ GPU_MAT_COMPOSITOR
@ GPU_MAT_ENGINE_MAX
void GPU_material_uniform_buffer_create(GPUMaterial *material, ListBaseT< LinkData > *inputs)
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_stack_link_zone(GPUMaterial *material, const bNode *bnode, const char *name, GPUNodeStack *in, GPUNodeStack *out, int zone_index, bool is_zone_end, int in_argument_count, int out_argument_count)
eGPUMaterialFlag GPU_material_flag(const GPUMaterial *mat)
VecBase< float, 4 > float4
GPUNodeLink * GPU_attribute_hair_length(GPUMaterial *mat)
GPUNodeStackValue GPU_node_stack_default_value(const GPUType type)
void GPU_material_add_output_link_composite(GPUMaterial *material, GPUNodeLink *link)
void GPU_material_free(ListBaseT< LinkData > *gpumaterial)
unsigned int uint32_t
Definition BKE_text.h:81
GPUNodeLink * GPU_constant(const float *num)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeLink * GPU_attribute_default_color(GPUMaterial *mat)
std::variant< std::monostate, float, float2, float3, float4, float3x3, float4x4, int, int2, int3, int4, bool > GPUNodeStackValue
GPUPass * GPU_material_get_pass(GPUMaterial *material)
GPUNodeStack & GPU_node_get_output(const bNode &node, GPUNodeStack outputs[], StringRef identifier)
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
ListBaseT< GPUMaterialAttribute > GPU_material_attributes(const GPUMaterial *material)
void GPU_image_tiled(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state, GPUNodeLink **r_image_tiled_link, GPUNodeLink **r_image_tiled_mapping_link)
GPUMaterial * GPU_material_from_callbacks(eGPUMaterialEngine engine, ConstructGPUMaterialFn construct_function_cb, GPUCodegenCallbackFn generate_code_function_cb, void *thunk, const uint64_t uuid)
GPUMaterialFromNodeTreeResult GPU_material_from_nodetree(Material *ma, bNodeTree *ntree, ListBaseT< LinkData > *gpumaterials, const char *name, eGPUMaterialEngine engine, uint64_t shader_uuid, bool deferred_compilation, GPUCodegenCallbackFn callback, void *thunk, GPUMaterialPassReplacementCallbackFn pass_replacement_cb=nullptr)
void GPU_uniform_attr_list_copy(GPUUniformAttrList *dest, const GPUUniformAttrList *src)
ATTR_WARN_UNUSED_RESULT const size_t num
bool GPU_material_has_surface_output(GPUMaterial *mat)
MatBase< float, 3, 3 > float3x3
void GPU_material_output_displacement(GPUMaterial *material, GPUNodeLink *link)
GPUNodeLink * GPU_differentiate_float_function(const char *function_name, const float filter_width)
void GPU_material_output_volume(GPUMaterial *material, GPUNodeLink *link)
ListBaseT< GPUMaterialTexture > GPU_material_textures(GPUMaterial *material)
GPUPass *(*)(void *thunk, GPUMaterial *mat) GPUMaterialPassReplacementCallbackFn
GPUNodeLink * GPU_shading_data()
void GPU_material_add_output_link_aov(GPUMaterial *material, GPUNodeLink *link, int hash)
char * GPU_material_split_sub_function(GPUMaterial *material, GPUType return_type, GPUNodeLink **link)
void GPU_material_free_single(GPUMaterial *material)
GPUNodeLink * GPU_image_sky(GPUMaterial *mat, int width, int height, const float *pixels, float *layer, GPUSamplerState sampler_state)
uint64_t GPU_material_compilation_timestamp(GPUMaterial *mat)
uint64_t GPU_material_uuid_get(GPUMaterial *mat)
Material * GPU_material_get_material(GPUMaterial *material)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
GPUNodeLink * GPU_kernel_globals()
eGPUMaterialOptimizationStatus
@ GPU_MAT_OPTIMIZATION_SUCCESS
@ GPU_MAT_OPTIMIZATION_SKIP
@ GPU_MAT_OPTIMIZATION_QUEUED
const GPUUniformAttrList * GPU_material_uniform_attributes(const GPUMaterial *material)
VecBase< float, 3 > float3
const ListBaseT< GPULayerAttr > * GPU_material_layer_attributes(const GPUMaterial *material)
void(*)(void *thunk, GPUMaterial *material) ConstructGPUMaterialFn
void(*)(void *thunk, GPUMaterial *mat, struct GPUCodegenOutput *codegen) GPUCodegenCallbackFn
void GPU_uniform_attr_list_free(GPUUniformAttrList *set)
eGPUMaterialOptimizationStatus GPU_material_optimization_status(GPUMaterial *mat)
constexpr int gpu_type_element_count(const GPUType type)
const char * GPU_material_get_name(GPUMaterial *material)
GPUMaterialStatus GPU_material_status(GPUMaterial *mat)
void GPU_material_output_thickness(GPUMaterial *material, GPUNodeLink *link)
MINLINE float clamp_f(float value, float min, float max)
GHash * GPU_uniform_attr_list_hash_new(const char *info)
GPUNodeLink * GPU_attribute_hair_intercept(GPUMaterial *mat)
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
GPUShaderCreateInfo * create_info
Vector< GPUGraphOutput > material_functions
std::string serialized_or_default(std::string value) const
Vector< StringRefNull > dependencies
GPUMaterialAttribute * prev
GPUMaterialAttribute * next
GPUMaterialTexture * prev
GPUMaterialTexture * next
GPUNodeStackValue value
bool socket_not_black() const
bool socket_not_one() const
bool might_be_tinted() const
bool socket_not_zero() const
bool socket_not_white() const
ListBaseT< GPUUniformAttr > list
GPUUniformAttr * prev
GPUUniformAttr * next