Blender V5.3
gpu_material.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12
13#include "BKE_lib_id.hh"
14#include "MEM_guardedalloc.h"
15
16#include "DNA_material_types.h"
17#include "DNA_scene_types.h"
18#include "DNA_world_types.h"
19
20#include "BLI_listbase.hh"
21#include "BLI_math_vector_c.hh"
22#include "BLI_string.hh"
23#include "BLI_time.hh"
24#include "BLI_utildefines.hh"
25
26#include "BKE_main.hh"
27#include "BKE_material.hh"
28#include "BKE_node.hh"
29#include "BKE_node_runtime.hh"
31
32#include "NOD_shader.h"
34
35#include "GPU_material.hh"
36#include "GPU_pass.hh"
37#include "GPU_shader.hh"
38#include "GPU_texture.hh"
39#include "GPU_uniform_buffer.hh"
40
41#include "DRW_engine.hh"
42
43#include "gpu_node_graph.hh"
44
45#include "atomic_ops.h"
46
47namespace blender {
48
51
52/* Structs */
53#define MAX_COLOR_BAND 128
54#define MAX_GPU_SKIES 8
55
60
65
67 /* Contains #gpu::Shader and source code for deferred compilation.
68 * Can be shared between materials sharing same node-tree topology. */
69 GPUPass *pass = nullptr;
70 /* Optimized GPUPass, situationally compiled after initial pass for optimal realtime performance.
71 * This shader variant bakes dynamic uniform data as constant. This variant will not use
72 * the ubo, and instead bake constants directly into the shader source. */
74
75 /* UBOs for this material parameters. */
76 gpu::UniformBuf *ubo = nullptr;
77 /* Some flags about the nodetree & the needed resources. */
79 /* The engine type this material is compiled for. */
81 /* Identify shader variations (shadow, probe, world background...) */
83 /* Number of generated function. */
85
86 /* Source material, might be null. */
88 /* 1D Texture array containing all color bands. */
90 /* Builder for coba_tex. */
92 /* 2D Texture array containing all sky textures. */
94 /* Builder for sky_tex. */
96 /* Low level node graph(s). Also contains resources needed by the material. */
98
99 bool has_surface_output = false;
100 bool has_volume_output = false;
102
103 std::string name;
104
106
108 {
110
111 if (optimized_pass != nullptr) {
113 }
114 if (pass != nullptr) {
116 }
117 if (ubo != nullptr) {
119 }
120 if (coba_builder != nullptr) {
121 MEM_delete(coba_builder);
122 }
123 if (coba_tex != nullptr) {
125 }
126 if (sky_tex != nullptr) {
128 }
129 }
130};
131
132/* Public API */
133
135 Material *ma,
136 bNodeTree *ntree,
137 ListBaseT<LinkData> *gpumaterials,
138 const char *name,
139 eGPUMaterialEngine engine,
140 uint64_t shader_uuid,
141 bool deferred_compilation,
142 GPUCodegenCallbackFn callback,
143 void *thunk,
144 GPUMaterialPassReplacementCallbackFn pass_replacement_cb)
145{
146 /* Search if this material is not already compiled. */
147 for (LinkData &link : *gpumaterials) {
148 GPUMaterial *mat = static_cast<GPUMaterial *>(link.data);
149 if (mat->uuid == shader_uuid && mat->engine == engine) {
150 if (!deferred_compilation) {
152 }
153 return {mat};
154 }
155 }
156
158
159 GPUMaterial *mat = MEM_new<GPUMaterial>(__func__, engine);
160 mat->source_material = ma;
161 mat->uuid = shader_uuid;
162 mat->name = name;
163 result.material = mat;
164
165 /* Localize tree to create links for reroute and mute. */
167 nullptr, (StringRef(ntree->id.name) + " Inlined").c_str(), ntree->idname);
170 inline_params.allow_preserving_repeat_zones = true;
171 inline_params.target_engine_ = engine == GPU_MAT_EEVEE ? SHD_OUTPUT_EEVEE : SHD_OUTPUT_ALL;
172 nodes::inline_shader_node_tree(*ntree, *localtree, inline_params);
173
175 result.errors.append({error.node,
176 std::move(error.message),
178 }
179
180 ntreeGPUMaterialNodes(localtree, mat);
181
184
185 /* Use default material pass when possible. */
186 if (GPUPass *default_pass = pass_replacement_cb ? pass_replacement_cb(thunk, mat) : nullptr) {
187 mat->pass = default_pass;
189 /* WORKAROUND:
190 * The node tree code is never executed in default replaced passes,
191 * but the GPU validation will still complain if the node tree UBO is not bound.
192 * So we create a dummy UBO with (at least) the size of the default material one (192 bytes).
193 * We allocate 256 bytes to leave some room for future changes. */
194 mat->ubo = GPU_uniformbuf_create_ex(256, nullptr, "Dummy UBO");
195 }
196 else {
197 /* Create source code and search pass cache for an already compiled version. */
198 mat->pass = GPU_generate_pass(
199 mat, &mat->graph, mat->name.c_str(), engine, deferred_compilation, callback, thunk, false);
200 }
201
202 /* Determine whether we should generate an optimized variant of the graph.
203 * Heuristic is based on complexity of default material pass and shader node graph. */
204 if (GPU_pass_should_optimize(mat->pass)) {
206 mat, &mat->graph, mat->name.c_str(), engine, true, callback, thunk, true);
207 }
208
210
211#if 0
212 /* Dump localtree to a .dot file */
213 std::cout << ">>>>>>>>>>>>>>>>>" << name << std::endl
214 << blender::bke::node_tree_to_dot(*localtree) << std::endl;
215#endif
216
217 /* Only free after GPU_pass_shader_get where gpu::UniformBuf read data from the local
218 * tree. */
219 BKE_id_free(nullptr, &localtree->id);
220
221 /* Note that even if building the shader fails in some way, we want to keep
222 * it to avoid trying to compile again and again, and simply do not use
223 * the actual shader on drawing. */
224 LinkData *link = MEM_new_zeroed<LinkData>("GPUMaterialLink");
225 link->data = mat;
226 BLI_addtail(gpumaterials, link);
227
228 return result;
229}
230
231/* Make sure the two enums are actually the same. */
238
240 ConstructGPUMaterialFn construct_function_cb,
241 GPUCodegenCallbackFn generate_code_function_cb,
242 void *thunk,
243 const uint64_t uuid)
244{
245 /* Allocate a new material and its material graph. */
246 GPUMaterial *material = MEM_new<GPUMaterial>(__func__, engine);
247 material->uuid = uuid;
248
249 /* Construct the material graph by adding and linking the necessary GPU material nodes. */
250 construct_function_cb(thunk, material);
251
252 /* Create and initialize the texture storing color bands used by Ramp and Curve nodes. */
254
255 /* Lookup an existing pass in the cache or generate a new one. */
256 material->pass = GPU_generate_pass(material,
257 &material->graph,
258 __func__,
259 engine,
260 false,
261 generate_code_function_cb,
262 thunk,
263 false);
264
265 /* Determine whether we should generate an optimized variant of the graph.
266 * Heuristic is based on complexity of default material pass and shader node graph. */
267 if (GPU_pass_should_optimize(material->pass)) {
268 material->optimized_pass = GPU_generate_pass(material,
269 &material->graph,
270 __func__,
271 engine,
272 true,
273 generate_code_function_cb,
274 thunk,
275 true);
276 }
277
279
280 return material;
281}
282
284{
285 MEM_delete(material);
286}
287
288void GPU_material_free(ListBaseT<LinkData> *gpumaterial)
289{
290 for (LinkData &link : *gpumaterial) {
291 GPUMaterial *material = static_cast<GPUMaterial *>(link.data);
292 GPU_material_free_single(material);
293 }
294 gpumaterial->free_no_destruct();
295}
296
298{
299 for (Material &ma : bmain->materials) {
300 GPU_material_free(&ma.gpumaterial);
301 }
302
303 for (World &wo : bmain->worlds) {
304 GPU_material_free(&wo.gpumaterial);
305 }
306
308}
309
310const char *GPU_material_get_name(GPUMaterial *material)
311{
312 return material->name.c_str();
313}
314
316{
317 return mat->uuid;
318}
319
321{
322 return material->source_material;
323}
324
326{
327 /* If an optimized pass variant is available, and optimization is
328 * flagged as complete, we use this one instead. */
330 material->optimized_pass :
331 material->pass;
332}
333
338
340{
341 switch (GPU_pass_status(mat->pass)) {
342 case GPU_PASS_SUCCESS:
343 return GPU_MAT_SUCCESS;
344 case GPU_PASS_QUEUED:
345 return GPU_MAT_QUEUED;
346 default:
347 return GPU_MAT_FAILED;
348 }
349}
350
367
372
377
382
387
389{
390 return (mat->flag & flag) != 0;
391}
392
394{
395 return mat->flag;
396}
397
399{
400 if ((flag & GPU_MATFLAG_GLOSSY) && (mat->flag & GPU_MATFLAG_GLOSSY)) {
401 /* Tag material using multiple glossy BSDF as using clear coat. */
402 mat->flag |= GPU_MATFLAG_COAT;
403 }
404 mat->flag |= flag;
405}
406
407void GPU_material_uniform_buffer_create(GPUMaterial *material, ListBaseT<LinkData> *inputs)
408{
409 material->ubo = GPU_uniformbuf_create_from_list(inputs, material->name.c_str());
410}
411
413{
414 return material->ubo;
415}
416
417ListBaseT<GPUMaterialAttribute> GPU_material_attributes(const GPUMaterial *material)
418{
419 return material->graph.attributes;
420}
421
422ListBaseT<GPUMaterialTexture> GPU_material_textures(GPUMaterial *material)
423{
424 return material->graph.textures;
425}
426
428{
429 const GPUUniformAttrList *attrs = &material->graph.uniform_attrs;
430 return attrs->count > 0 ? attrs : nullptr;
431}
432
433const ListBaseT<GPULayerAttr> *GPU_material_layer_attributes(const GPUMaterial *material)
434{
435 const ListBaseT<GPULayerAttr> *attrs = &material->graph.layer_attrs;
436 return !attrs->is_empty() ? attrs : nullptr;
437}
438
440{
441 return &material->graph;
442}
443
444/* Resources */
445
447 GPUMaterial *mat, int width, int height, const float *pixels, float *row)
448{
449 /* In order to put all sky textures into one 2D array texture,
450 * we need them to be the same size. */
451 BLI_assert(width == GPU_SKY_WIDTH);
452 BLI_assert(height == GPU_SKY_HEIGHT);
453 UNUSED_VARS_NDEBUG(width, height);
454
455 if (mat->sky_builder == nullptr) {
457 mat->sky_builder->current_layer = 0;
458 }
459
460 int layer = mat->sky_builder->current_layer;
461 *row = float(layer);
462
463 if (*row == MAX_GPU_SKIES) {
464 printf("Too many sky textures in shader!\n");
465 }
466 else {
467 float *dst = reinterpret_cast<float *>(mat->sky_builder->pixels[layer]);
468 memcpy(dst, pixels, sizeof(float) * GPU_SKY_WIDTH * GPU_SKY_HEIGHT * 4);
469 mat->sky_builder->current_layer += 1;
470 }
471
472 return &mat->sky_tex;
473}
474
476 int size,
477 const float *pixels,
478 float *r_row)
479{
480 /* In order to put all the color-bands into one 1D array texture,
481 * we need them to be the same size. */
482 BLI_assert(size == CM_TABLE + 1);
483 UNUSED_VARS_NDEBUG(size);
484
485 if (mat->coba_builder == nullptr) {
486 mat->coba_builder = MEM_new_uninitialized<GPUColorBandBuilder>("GPUColorBandBuilder");
487 mat->coba_builder->current_layer = 0;
488 }
489
490 int layer = mat->coba_builder->current_layer;
491 *r_row = float(layer);
492
493 if (*r_row == MAX_COLOR_BAND) {
494 printf("Too many color band in shader! Remove some Curve, Black Body or Color Ramp Node.\n");
495 }
496 else {
497 float *dst = reinterpret_cast<float *>(mat->coba_builder->pixels[layer]);
498 memcpy(dst, pixels, sizeof(float) * (CM_TABLE + 1) * 4);
499 mat->coba_builder->current_layer += 1;
500 }
501
502 return &mat->coba_tex;
503}
504
506{
507 if (mat->coba_builder == nullptr) {
508 return;
509 }
510
511 GPUColorBandBuilder *builder = mat->coba_builder;
512
513 mat->coba_tex = GPU_texture_create_1d_array("mat_ramp",
514 CM_TABLE + 1,
515 builder->current_layer,
516 1,
517 gpu::TextureFormat::SFLOAT_32_32_32_32,
519 reinterpret_cast<float *>(builder->pixels));
520
521 MEM_delete(builder);
522 mat->coba_builder = nullptr;
523}
524
526{
527 if (mat->sky_builder == nullptr) {
528 return;
529 }
530
531 mat->sky_tex = GPU_texture_create_2d_array("mat_sky",
535 1,
536 gpu::TextureFormat::SFLOAT_32_32_32_32,
538 reinterpret_cast<float *>(mat->sky_builder->pixels));
539
540 MEM_delete(mat->sky_builder);
541 mat->sky_builder = nullptr;
542}
543
544/* Code generation */
545
547{
548 if (!material->graph.outlink_surface) {
549 material->graph.outlink_surface = link;
550 material->has_surface_output = true;
551 }
552}
553
555{
556 if (!material->graph.outlink_volume) {
557 material->graph.outlink_volume = link;
558 material->has_volume_output = true;
559 }
560}
561
563{
564 if (!material->graph.outlink_displacement) {
565 material->graph.outlink_displacement = link;
566 material->has_displacement_output = true;
567 }
568}
569
571{
572 if (!material->graph.outlink_thickness) {
573 material->graph.outlink_thickness = link;
574 }
575}
576
578{
580 aov_link->outlink = link;
581 aov_link->hash = hash;
582 BLI_addtail(&material->graph.outlink_aovs, aov_link);
583}
584
586{
588 compositor_link->outlink = link;
589 BLI_addtail(&material->graph.outlink_compositor, compositor_link);
590}
591
593 GPUType return_type,
594 GPUNodeLink **link)
595{
596 /* Force cast to return type. */
597 switch (return_type) {
598 case GPU_FLOAT:
599 GPU_link(material, "set_value", *link, link);
600 break;
601 case GPU_VEC3:
602 GPU_link(material, "set_rgb", *link, link);
603 break;
604 case GPU_VEC4:
605 GPU_link(material, "set_rgba", *link, link);
606 break;
607 default:
608 BLI_assert(0);
609 break;
610 }
611
613 func_link->outlink = *link;
614 SNPRINTF(func_link->name, "ntree_fn%d", material->generated_function_len++);
615 BLI_addtail(&material->graph.material_functions, func_link);
616
617 return func_link->name;
618}
619
620} // namespace blender
General operations, lookup, etc. for materials.
#define SNPRINTF(dst, format,...)
Platform independent time functions.
#define GPU_SKY_WIDTH
#define GPU_SKY_HEIGHT
#define CM_TABLE
Read Blender memory allocation and freeing.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
constexpr StringRef()
#define MAX_GPU_SKIES
#define MAX_COLOR_BAND
#define layer
bNodeTree * node_tree_add_tree(Main *bmain, StringRef name, StringRef idname)
Definition node.cc:4992
std::string node_tree_to_dot(const bNodeTree &tree, const bNodeTreeToDotOptions &options=bNodeTreeToDotOptions())
bool inline_shader_node_tree(const bNodeTree &src_tree, bNodeTree &dst_tree, InlineShaderNodeTreeParams &params)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
bool GPU_pass_should_optimize(GPUPass *pass)
Definition gpu_pass.cc:167
bool GPU_material_has_volume_output(GPUMaterial *mat)
bool GPU_material_has_displacement_output(GPUMaterial *mat)
GPUNodeGraph * gpu_material_node_graph(GPUMaterial *material)
void GPU_uniformbuf_free(gpu::UniformBuf *ubo)
void GPU_material_output_surface(GPUMaterial *material, GPUNodeLink *link)
void GPU_pass_acquire(GPUPass *pass)
Definition gpu_pass.cc:181
@ GPU_MATFLAG_COAT
@ GPU_MATFLAG_GLOSSY
@ GPU_MATFLAG_UPDATED
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
static void gpu_material_sky_texture_build(GPUMaterial *mat)
void ntreeGPUMaterialNodes(struct bNodeTree *localtree, struct GPUMaterial *mat)
@ GPU_MAT_SUCCESS
gpu::Shader * GPU_material_get_shader(GPUMaterial *material)
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
gpu::Texture ** gpu_material_ramp_texture_row_set(GPUMaterial *mat, int size, const float *pixels, float *r_row)
void GPU_materials_free(Main *bmain)
gpu::UniformBuf * GPU_material_uniform_buffer_get(GPUMaterial *material)
void GPU_material_uniform_buffer_create(GPUMaterial *material, ListBaseT< LinkData > *inputs)
eGPUMaterialFlag GPU_material_flag(const GPUMaterial *mat)
void GPU_material_add_output_link_composite(GPUMaterial *material, GPUNodeLink *link)
gpu::Texture ** gpu_material_sky_texture_layer_set(GPUMaterial *mat, int width, int height, const float *pixels, float *row)
void GPU_material_free(ListBaseT< LinkData > *gpumaterial)
gpu::Shader * GPU_pass_shader_get(GPUPass *pass)
Definition gpu_pass.cc:176
uint64_t GPU_pass_compilation_timestamp(GPUPass *pass)
Definition gpu_pass.cc:200
static void gpu_material_ramp_texture_build(GPUMaterial *mat)
@ GPU_PASS_QUEUED
Definition GPU_pass.hh:24
@ GPU_PASS_SUCCESS
Definition GPU_pass.hh:25
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool GPU_link(GPUMaterial *mat, const char *name,...)
@ GPU_TEXTURE_USAGE_SHADER_READ
GPUPass * GPU_material_get_pass(GPUMaterial *material)
GPUPassStatus GPU_pass_status(GPUPass *pass)
Definition gpu_pass.cc:162
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
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)
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_node_graph_free(GPUNodeGraph *graph)
bool GPU_material_has_surface_output(GPUMaterial *mat)
void BKE_id_free(Main *bmain, void *idv)
void gpu_node_graph_free_nodes(GPUNodeGraph *graph)
gpu::UniformBuf * GPU_uniformbuf_create_ex(size_t size, const void *data, const char *name)
void GPU_material_output_displacement(GPUMaterial *material, GPUNodeLink *link)
void GPU_pass_release(GPUPass *pass)
Definition gpu_pass.cc:188
void GPU_material_output_volume(GPUMaterial *material, GPUNodeLink *link)
ListBaseT< GPUMaterialTexture > GPU_material_textures(GPUMaterial *material)
GPUPass *(*)(void *thunk, GPUMaterial *mat) GPUMaterialPassReplacementCallbackFn
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)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
gpu::UniformBuf * GPU_uniformbuf_create_from_list(ListBaseT< LinkData > *inputs, const char *name)
void GPU_texture_free(gpu::Texture *texture)
@ NTREE_IS_GPU_SHADER_INTERNAL
uint64_t GPU_material_compilation_timestamp(GPUMaterial *mat)
uint64_t GPU_material_uuid_get(GPUMaterial *mat)
Material * GPU_material_get_material(GPUMaterial *material)
eGPUMaterialOptimizationStatus
@ GPU_MAT_OPTIMIZATION_SUCCESS
@ GPU_MAT_OPTIMIZATION_SKIP
@ GPU_MAT_OPTIMIZATION_QUEUED
const GPUUniformAttrList * GPU_material_uniform_attributes(const GPUMaterial *material)
GPUPass * GPU_generate_pass(GPUMaterial *material, GPUNodeGraph *graph, const char *debug_name, eGPUMaterialEngine engine, bool deferred_compilation, GPUCodegenCallbackFn finalize_source_cb, void *thunk, bool optimize_graph)
Definition gpu_pass.cc:350
const ListBaseT< GPULayerAttr > * GPU_material_layer_attributes(const GPUMaterial *material)
void(*)(void *thunk, GPUMaterial *material) ConstructGPUMaterialFn
void GPU_pass_ensure_its_ready(GPUPass *pass)
Definition gpu_pass.cc:300
void(*)(void *thunk, GPUMaterial *mat, struct GPUCodegenOutput *codegen) GPUCodegenCallbackFn
eGPUMaterialOptimizationStatus GPU_material_optimization_status(GPUMaterial *mat)
const char * GPU_material_get_name(GPUMaterial *material)
gpu::Texture * GPU_texture_create_1d_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
GPUMaterialStatus GPU_material_status(GPUMaterial *mat)
void GPU_material_output_thickness(GPUMaterial *material, GPUNodeLink *link)
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float pixels[MAX_COLOR_BAND][CM_TABLE+1][4]
gpu::Texture * coba_tex
GPUColorBandBuilder * coba_builder
eGPUMaterialFlag flag
eGPUMaterialEngine engine
gpu::Texture * sky_tex
gpu::UniformBuf * ubo
GPUMaterial(eGPUMaterialEngine engine)
Material * source_material
GPUSkyBuilder * sky_builder
ListBaseT< GPUMaterialAttribute > attributes
ListBaseT< GPUNodeGraphFunctionLink > material_functions
GPUNodeLink * outlink_surface
ListBaseT< GPULayerAttr > layer_attrs
GPUNodeLink * outlink_displacement
ListBaseT< GPUNodeGraphOutputLink > outlink_aovs
GPUNodeLink * outlink_thickness
ListBaseT< GPUNodeGraphOutputLink > outlink_compositor
GPUNodeLink * outlink_volume
ListBaseT< GPUMaterialTexture > textures
GPUUniformAttrList uniform_attrs
float pixels[MAX_GPU_SKIES][GPU_SKY_WIDTH *GPU_SKY_HEIGHT][4]
char name[258]
Definition DNA_ID.h:433
ListBaseT< World > worlds
Definition BKE_main.hh:399
ListBaseT< Material > materials
Definition BKE_main.hh:392
eNodeTree_Flag flag