2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2005 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup gpu
|
|
|
|
|
*
|
|
|
|
|
* Intermediate node graph for generating GLSL shaders.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-12-29 12:01:32 -05:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_node_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_ghash.h"
|
|
|
|
|
#include "BLI_listbase.h"
|
2025-10-08 16:38:14 +02:00
|
|
|
#include "BLI_stack.hh"
|
2020-02-12 12:48:44 +01:00
|
|
|
#include "BLI_string.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2020-02-12 12:48:44 +01:00
|
|
|
|
2024-03-23 01:24:18 +01:00
|
|
|
#include "GPU_texture.hh"
|
|
|
|
|
#include "GPU_vertex_format.hh"
|
2020-06-03 13:35:15 +02:00
|
|
|
|
2024-03-23 01:24:18 +01:00
|
|
|
#include "gpu_material_library.hh"
|
|
|
|
|
#include "gpu_node_graph.hh"
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
/* Node Link Functions */
|
|
|
|
|
|
2022-12-29 12:01:32 -05:00
|
|
|
static GPUNodeLink *gpu_node_link_create()
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2025-03-05 16:35:09 +01:00
|
|
|
GPUNodeLink *link = MEM_callocN<GPUNodeLink>("GPUNodeLink");
|
2020-02-12 12:48:44 +01:00
|
|
|
link->users++;
|
|
|
|
|
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void gpu_node_link_free(GPUNodeLink *link)
|
|
|
|
|
{
|
|
|
|
|
link->users--;
|
|
|
|
|
|
|
|
|
|
if (link->users < 0) {
|
|
|
|
|
fprintf(stderr, "gpu_node_link_free: negative refcount\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (link->users == 0) {
|
|
|
|
|
if (link->output) {
|
2022-11-19 11:51:42 +01:00
|
|
|
link->output->link = nullptr;
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
MEM_freeN(link);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Node Functions */
|
|
|
|
|
|
|
|
|
|
static GPUNode *gpu_node_create(const char *name)
|
|
|
|
|
{
|
2025-03-05 16:35:09 +01:00
|
|
|
GPUNode *node = MEM_callocN<GPUNode>("GPUNode");
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
node->name = name;
|
2025-10-08 16:38:14 +02:00
|
|
|
node->zone_index = -1;
|
|
|
|
|
node->is_zone_end = false;
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 15:11:02 +02:00
|
|
|
static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType type)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
|
|
|
|
GPUInput *input;
|
|
|
|
|
GPUNode *outnode;
|
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
|
|
if (link->link_type == GPU_NODE_LINK_OUTPUT) {
|
|
|
|
|
outnode = link->output->node;
|
|
|
|
|
name = outnode->name;
|
2022-11-18 11:08:39 +01:00
|
|
|
input = static_cast<GPUInput *>(outnode->inputs.first);
|
2020-02-12 12:48:44 +01:00
|
|
|
|
2021-07-16 11:45:52 +10:00
|
|
|
if (STR_ELEM(name, "set_value", "set_rgb", "set_rgba") && (input->type == type)) {
|
2022-11-18 11:08:39 +01:00
|
|
|
input = static_cast<GPUInput *>(MEM_dupallocN(outnode->inputs.first));
|
2022-07-29 08:37:57 +02:00
|
|
|
|
|
|
|
|
switch (input->source) {
|
|
|
|
|
case GPU_SOURCE_ATTR:
|
|
|
|
|
input->attr->users++;
|
|
|
|
|
break;
|
|
|
|
|
case GPU_SOURCE_UNIFORM_ATTR:
|
|
|
|
|
input->uniform_attr->users++;
|
|
|
|
|
break;
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
case GPU_SOURCE_LAYER_ATTR:
|
|
|
|
|
input->layer_attr->users++;
|
|
|
|
|
break;
|
2022-07-29 08:37:57 +02:00
|
|
|
case GPU_SOURCE_TEX:
|
|
|
|
|
input->texture->users++;
|
|
|
|
|
break;
|
2023-12-21 18:55:32 +01:00
|
|
|
case GPU_SOURCE_TEX_TILED_MAPPING:
|
|
|
|
|
/* Already handled by GPU_SOURCE_TEX. */
|
2022-07-29 08:37:57 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 12:48:44 +01:00
|
|
|
if (input->link) {
|
|
|
|
|
input->link->users++;
|
|
|
|
|
}
|
2022-07-29 08:37:57 +02:00
|
|
|
|
2020-02-12 12:48:44 +01:00
|
|
|
BLI_addtail(&node->inputs, input);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 16:35:09 +01:00
|
|
|
input = MEM_callocN<GPUInput>("GPUInput");
|
2020-02-12 12:48:44 +01:00
|
|
|
input->node = node;
|
|
|
|
|
input->type = type;
|
|
|
|
|
|
|
|
|
|
switch (link->link_type) {
|
|
|
|
|
case GPU_NODE_LINK_OUTPUT:
|
|
|
|
|
input->source = GPU_SOURCE_OUTPUT;
|
|
|
|
|
input->link = link;
|
|
|
|
|
link->users++;
|
|
|
|
|
break;
|
2020-02-27 13:55:29 +01:00
|
|
|
case GPU_NODE_LINK_IMAGE:
|
|
|
|
|
case GPU_NODE_LINK_IMAGE_TILED:
|
2022-09-16 15:04:47 +02:00
|
|
|
case GPU_NODE_LINK_IMAGE_SKY:
|
2020-02-12 12:48:44 +01:00
|
|
|
case GPU_NODE_LINK_COLORBAND:
|
|
|
|
|
input->source = GPU_SOURCE_TEX;
|
2020-02-27 13:55:29 +01:00
|
|
|
input->texture = link->texture;
|
2020-02-12 12:48:44 +01:00
|
|
|
break;
|
2020-02-27 13:55:29 +01:00
|
|
|
case GPU_NODE_LINK_IMAGE_TILED_MAPPING:
|
|
|
|
|
input->source = GPU_SOURCE_TEX_TILED_MAPPING;
|
|
|
|
|
input->texture = link->texture;
|
2020-02-12 12:48:44 +01:00
|
|
|
break;
|
|
|
|
|
case GPU_NODE_LINK_ATTR:
|
|
|
|
|
input->source = GPU_SOURCE_ATTR;
|
2020-02-27 13:55:29 +01:00
|
|
|
input->attr = link->attr;
|
2021-02-05 16:23:34 +11:00
|
|
|
/* Fail-safe handling if the same attribute is used with different data-types for
|
2020-08-04 17:56:39 +03:00
|
|
|
* some reason (only really makes sense with float/vec2/vec3/vec4 though). This
|
|
|
|
|
* can happen if mixing the generic Attribute node with specialized ones. */
|
|
|
|
|
CLAMP_MIN(input->attr->gputype, type);
|
2020-02-12 12:48:44 +01:00
|
|
|
break;
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
case GPU_NODE_LINK_UNIFORM_ATTR:
|
|
|
|
|
input->source = GPU_SOURCE_UNIFORM_ATTR;
|
|
|
|
|
input->uniform_attr = link->uniform_attr;
|
|
|
|
|
break;
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
case GPU_NODE_LINK_LAYER_ATTR:
|
|
|
|
|
input->source = GPU_SOURCE_LAYER_ATTR;
|
|
|
|
|
input->layer_attr = link->layer_attr;
|
|
|
|
|
break;
|
2020-02-12 12:48:44 +01:00
|
|
|
case GPU_NODE_LINK_CONSTANT:
|
|
|
|
|
input->source = (type == GPU_CLOSURE) ? GPU_SOURCE_STRUCT : GPU_SOURCE_CONSTANT;
|
|
|
|
|
break;
|
|
|
|
|
case GPU_NODE_LINK_UNIFORM:
|
|
|
|
|
input->source = GPU_SOURCE_UNIFORM;
|
|
|
|
|
break;
|
2022-04-14 18:47:58 +02:00
|
|
|
case GPU_NODE_LINK_DIFFERENTIATE_FLOAT_FN:
|
|
|
|
|
input->source = GPU_SOURCE_FUNCTION_CALL;
|
|
|
|
|
/* NOTE(@fclem): End of function call is the return variable set during codegen. */
|
2025-03-24 15:53:35 +01:00
|
|
|
SNPRINTF(input->function_call,
|
|
|
|
|
"dF_branch_incomplete(%s(), %g, ",
|
|
|
|
|
link->differentiate_float.function_name,
|
|
|
|
|
link->differentiate_float.filter_width);
|
2022-04-14 18:47:58 +02:00
|
|
|
break;
|
2020-02-12 12:48:44 +01:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ELEM(input->source, GPU_SOURCE_CONSTANT, GPU_SOURCE_UNIFORM)) {
|
|
|
|
|
memcpy(input->vec, link->data, type * sizeof(float));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (link->link_type != GPU_NODE_LINK_OUTPUT) {
|
|
|
|
|
MEM_freeN(link);
|
|
|
|
|
}
|
|
|
|
|
BLI_addtail(&node->inputs, input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *gpu_uniform_set_function_from_type(eNodeSocketDatatype type)
|
|
|
|
|
{
|
|
|
|
|
switch (type) {
|
2023-10-05 21:55:10 +02:00
|
|
|
/* For now INT & BOOL are supported as float. */
|
2020-02-12 12:48:44 +01:00
|
|
|
case SOCK_INT:
|
|
|
|
|
case SOCK_FLOAT:
|
2023-10-05 21:55:10 +02:00
|
|
|
case SOCK_BOOLEAN:
|
2020-02-12 12:48:44 +01:00
|
|
|
return "set_value";
|
|
|
|
|
case SOCK_VECTOR:
|
|
|
|
|
return "set_rgb";
|
|
|
|
|
case SOCK_RGBA:
|
|
|
|
|
return "set_rgba";
|
|
|
|
|
default:
|
2021-07-15 18:23:28 +10:00
|
|
|
BLI_assert_msg(0, "No gpu function for non-supported eNodeSocketDatatype");
|
2022-11-19 11:51:42 +01:00
|
|
|
return nullptr;
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Link stack uniform buffer.
|
2022-02-26 02:15:22 +01:00
|
|
|
* This is called for the input/output sockets that are not connected.
|
2020-02-12 12:48:44 +01:00
|
|
|
*/
|
|
|
|
|
static GPUNodeLink *gpu_uniformbuffer_link(GPUMaterial *mat,
|
2022-08-31 11:49:35 -05:00
|
|
|
const bNode *node,
|
2020-02-12 12:48:44 +01:00
|
|
|
GPUNodeStack *stack,
|
|
|
|
|
const int index,
|
|
|
|
|
const eNodeSocketInOut in_out)
|
|
|
|
|
{
|
|
|
|
|
bNodeSocket *socket;
|
|
|
|
|
|
|
|
|
|
if (in_out == SOCK_IN) {
|
2022-11-18 11:08:39 +01:00
|
|
|
socket = static_cast<bNodeSocket *>(BLI_findlink(&node->inputs, index));
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2022-11-18 11:08:39 +01:00
|
|
|
socket = static_cast<bNodeSocket *>(BLI_findlink(&node->outputs, index));
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
BLI_assert(socket != nullptr);
|
2020-02-12 12:48:44 +01:00
|
|
|
BLI_assert(socket->in_out == in_out);
|
|
|
|
|
|
2022-07-29 08:37:57 +02:00
|
|
|
if (socket->flag & SOCK_HIDE_VALUE) {
|
2022-11-19 11:51:42 +01:00
|
|
|
return nullptr;
|
2022-07-29 08:37:57 +02:00
|
|
|
}
|
2020-02-12 12:48:44 +01:00
|
|
|
|
2025-10-08 16:38:14 +02:00
|
|
|
if (!ELEM(socket->type, SOCK_INT, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA)) {
|
2022-11-19 11:51:42 +01:00
|
|
|
return nullptr;
|
2022-07-29 08:37:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUNodeLink *link = GPU_uniform(stack->vec);
|
|
|
|
|
|
|
|
|
|
if (in_out == SOCK_IN) {
|
2022-11-18 11:08:39 +01:00
|
|
|
GPU_link(mat,
|
|
|
|
|
gpu_uniform_set_function_from_type(eNodeSocketDatatype(socket->type)),
|
|
|
|
|
link,
|
|
|
|
|
&stack->link);
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
2022-07-29 08:37:57 +02:00
|
|
|
|
|
|
|
|
return link;
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void gpu_node_input_socket(
|
2022-08-31 11:49:35 -05:00
|
|
|
GPUMaterial *material, const bNode *bnode, GPUNode *node, GPUNodeStack *sock, const int index)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
|
|
|
|
if (sock->link) {
|
|
|
|
|
gpu_node_input_link(node, sock->link, sock->type);
|
|
|
|
|
}
|
2022-11-19 11:51:42 +01:00
|
|
|
else if ((material != nullptr) &&
|
|
|
|
|
(gpu_uniformbuffer_link(material, bnode, sock, index, SOCK_IN) != nullptr))
|
|
|
|
|
{
|
2020-02-12 12:48:44 +01:00
|
|
|
gpu_node_input_link(node, sock->link, sock->type);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
gpu_node_input_link(node, GPU_constant(sock->vec), sock->type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 15:11:02 +02:00
|
|
|
static void gpu_node_output(GPUNode *node, const GPUType type, GPUNodeLink **link)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2025-03-05 16:35:09 +01:00
|
|
|
GPUOutput *output = MEM_callocN<GPUOutput>("GPUOutput");
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
output->type = type;
|
|
|
|
|
output->node = node;
|
|
|
|
|
|
|
|
|
|
if (link) {
|
|
|
|
|
*link = output->link = gpu_node_link_create();
|
|
|
|
|
output->link->link_type = GPU_NODE_LINK_OUTPUT;
|
|
|
|
|
output->link->output = output;
|
|
|
|
|
|
2021-07-03 23:08:40 +10:00
|
|
|
/* NOTE: the caller owns the reference to the link, GPUOutput
|
2020-02-12 12:48:44 +01:00
|
|
|
* merely points to it, and if the node is destroyed it will
|
2022-11-19 11:51:42 +01:00
|
|
|
* set that pointer to nullptr */
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_addtail(&node->outputs, output);
|
|
|
|
|
}
|
|
|
|
|
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
/* Uniform Attribute Functions */
|
|
|
|
|
|
|
|
|
|
static int uniform_attr_sort_cmp(const void *a, const void *b)
|
|
|
|
|
{
|
2022-11-18 11:08:39 +01:00
|
|
|
const GPUUniformAttr *attr_a = static_cast<const GPUUniformAttr *>(a),
|
|
|
|
|
*attr_b = static_cast<const GPUUniformAttr *>(b);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
|
|
|
|
|
int cmps = strcmp(attr_a->name, attr_b->name);
|
|
|
|
|
if (cmps != 0) {
|
|
|
|
|
return cmps > 0 ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (attr_a->use_dupli && !attr_b->use_dupli);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 17:04:52 +10:00
|
|
|
static uint uniform_attr_list_hash(const void *key)
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
{
|
2022-11-18 11:08:39 +01:00
|
|
|
const GPUUniformAttrList *attrs = static_cast<const GPUUniformAttrList *>(key);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
return attrs->hash_code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool uniform_attr_list_cmp(const void *a, const void *b)
|
|
|
|
|
{
|
2022-11-18 11:08:39 +01:00
|
|
|
const GPUUniformAttrList *set_a = static_cast<const GPUUniformAttrList *>(a),
|
|
|
|
|
*set_b = static_cast<const GPUUniformAttrList *>(b);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
|
|
|
|
|
if (set_a->hash_code != set_b->hash_code || set_a->count != set_b->count) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 11:08:39 +01:00
|
|
|
GPUUniformAttr *attr_a = static_cast<GPUUniformAttr *>(set_a->list.first),
|
|
|
|
|
*attr_b = static_cast<GPUUniformAttr *>(set_b->list.first);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
|
|
|
|
|
for (; attr_a && attr_b; attr_a = attr_a->next, attr_b = attr_b->next) {
|
|
|
|
|
if (!STREQ(attr_a->name, attr_b->name) || attr_a->use_dupli != attr_b->use_dupli) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attr_a || attr_b;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
GHash *GPU_uniform_attr_list_hash_new(const char *info)
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
{
|
|
|
|
|
return BLI_ghash_new(uniform_attr_list_hash, uniform_attr_list_cmp, info);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 19:01:12 +02:00
|
|
|
void GPU_uniform_attr_list_copy(GPUUniformAttrList *dest, const GPUUniformAttrList *src)
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
{
|
|
|
|
|
dest->count = src->count;
|
|
|
|
|
dest->hash_code = src->hash_code;
|
|
|
|
|
BLI_duplicatelist(&dest->list, &src->list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPU_uniform_attr_list_free(GPUUniformAttrList *set)
|
|
|
|
|
{
|
|
|
|
|
set->count = 0;
|
|
|
|
|
set->hash_code = 0;
|
|
|
|
|
BLI_freelistN(&set->list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void gpu_node_graph_finalize_uniform_attrs(GPUNodeGraph *graph)
|
|
|
|
|
{
|
|
|
|
|
GPUUniformAttrList *attrs = &graph->uniform_attrs;
|
|
|
|
|
BLI_assert(attrs->count == BLI_listbase_count(&attrs->list));
|
|
|
|
|
|
|
|
|
|
/* Sort the attributes by name to ensure a stable order. */
|
|
|
|
|
BLI_listbase_sort(&attrs->list, uniform_attr_sort_cmp);
|
|
|
|
|
|
|
|
|
|
/* Compute the indices and the hash code. */
|
|
|
|
|
int next_id = 0;
|
|
|
|
|
attrs->hash_code = 0;
|
|
|
|
|
|
|
|
|
|
LISTBASE_FOREACH (GPUUniformAttr *, attr, &attrs->list) {
|
|
|
|
|
attr->id = next_id++;
|
2022-09-01 14:46:17 +02:00
|
|
|
attrs->hash_code ^= BLI_ghashutil_uinthash(attr->hash_code + (1 << (attr->id + 1)));
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 13:55:29 +01:00
|
|
|
/* Attributes and Textures */
|
2020-02-12 12:48:44 +01:00
|
|
|
|
2024-07-29 13:01:12 +10:00
|
|
|
static char attr_prefix_get(const GPUMaterialAttribute *attr)
|
2022-04-19 12:01:16 +02:00
|
|
|
{
|
2022-07-26 08:37:08 -05:00
|
|
|
if (attr->is_default_color) {
|
|
|
|
|
return 'c';
|
|
|
|
|
}
|
2023-06-28 17:17:31 +02:00
|
|
|
if (attr->is_hair_length) {
|
|
|
|
|
return 'l';
|
|
|
|
|
}
|
2025-08-27 09:49:43 +02:00
|
|
|
if (attr->is_hair_intercept) {
|
|
|
|
|
return 'i';
|
|
|
|
|
}
|
2022-07-26 08:37:08 -05:00
|
|
|
switch (attr->type) {
|
2022-04-19 12:01:16 +02:00
|
|
|
case CD_TANGENT:
|
|
|
|
|
return 't';
|
|
|
|
|
case CD_AUTO_FROM_NAME:
|
|
|
|
|
return 'a';
|
|
|
|
|
default:
|
|
|
|
|
BLI_assert_msg(0, "GPUVertAttr Prefix type not found : This should not happen!");
|
|
|
|
|
return '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void attr_input_name(GPUMaterialAttribute *attr)
|
|
|
|
|
{
|
2022-06-05 12:04:42 +02:00
|
|
|
/* NOTE: Replicate changes to mesh_render_data_create() in draw_cache_impl_mesh.cc */
|
2022-04-19 12:01:16 +02:00
|
|
|
if (attr->type == CD_ORCO) {
|
|
|
|
|
/* OPTI: orco is computed from local positions, but only if no modifier is present. */
|
|
|
|
|
STRNCPY(attr->input_name, "orco");
|
|
|
|
|
}
|
|
|
|
|
else {
|
2022-07-26 08:37:08 -05:00
|
|
|
attr->input_name[0] = attr_prefix_get(attr);
|
2022-04-19 12:01:16 +02:00
|
|
|
attr->input_name[1] = '\0';
|
|
|
|
|
if (attr->name[0] != '\0') {
|
|
|
|
|
/* XXX FIXME: see notes in mesh_render_data_create() */
|
|
|
|
|
GPU_vertformat_safe_attr_name(attr->name, &attr->input_name[1], GPU_MAX_SAFE_ATTR_NAME);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
/** Add a new varying attribute of given type and name. Returns nullptr if out of slots. */
|
2020-02-27 13:55:29 +01:00
|
|
|
static GPUMaterialAttribute *gpu_node_graph_add_attribute(GPUNodeGraph *graph,
|
2022-06-01 14:38:06 +10:00
|
|
|
eCustomDataType type,
|
2022-07-26 08:37:08 -05:00
|
|
|
const char *name,
|
2023-10-18 11:13:38 +02:00
|
|
|
const bool is_default_color,
|
2025-08-27 09:49:43 +02:00
|
|
|
const bool is_hair_length,
|
|
|
|
|
const bool is_hair_intercept)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2020-02-27 13:55:29 +01:00
|
|
|
/* Find existing attribute. */
|
|
|
|
|
int num_attributes = 0;
|
2022-11-18 11:08:39 +01:00
|
|
|
GPUMaterialAttribute *attr = static_cast<GPUMaterialAttribute *>(graph->attributes.first);
|
2020-02-27 13:55:29 +01:00
|
|
|
for (; attr; attr = attr->next) {
|
2022-07-26 08:37:08 -05:00
|
|
|
if (attr->type == type && STREQ(attr->name, name) &&
|
2025-08-27 09:49:43 +02:00
|
|
|
attr->is_default_color == is_default_color && attr->is_hair_length == is_hair_length &&
|
|
|
|
|
attr->is_hair_intercept == is_hair_intercept)
|
2023-10-18 11:13:38 +02:00
|
|
|
{
|
2020-02-27 13:55:29 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
num_attributes++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add new requested attribute if it's within GPU limits. */
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr == nullptr) {
|
2025-03-05 16:35:09 +01:00
|
|
|
attr = MEM_callocN<GPUMaterialAttribute>(__func__);
|
2022-07-26 08:37:08 -05:00
|
|
|
attr->is_default_color = is_default_color;
|
2023-10-18 11:13:38 +02:00
|
|
|
attr->is_hair_length = is_hair_length;
|
2025-08-27 09:49:43 +02:00
|
|
|
attr->is_hair_intercept = is_hair_intercept;
|
2020-02-27 13:55:29 +01:00
|
|
|
attr->type = type;
|
|
|
|
|
STRNCPY(attr->name, name);
|
2022-04-19 12:01:16 +02:00
|
|
|
attr_input_name(attr);
|
2020-02-27 13:55:29 +01:00
|
|
|
attr->id = num_attributes;
|
|
|
|
|
BLI_addtail(&graph->attributes, attr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr != nullptr) {
|
2020-02-27 13:55:29 +01:00
|
|
|
attr->users++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
/** Add a new uniform attribute of given type and name. Returns nullptr if out of slots. */
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
static GPUUniformAttr *gpu_node_graph_add_uniform_attribute(GPUNodeGraph *graph,
|
|
|
|
|
const char *name,
|
|
|
|
|
bool use_dupli)
|
|
|
|
|
{
|
|
|
|
|
/* Find existing attribute. */
|
|
|
|
|
GPUUniformAttrList *attrs = &graph->uniform_attrs;
|
2022-11-18 11:08:39 +01:00
|
|
|
GPUUniformAttr *attr = static_cast<GPUUniformAttr *>(attrs->list.first);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
|
|
|
|
|
for (; attr; attr = attr->next) {
|
|
|
|
|
if (STREQ(attr->name, name) && attr->use_dupli == use_dupli) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add new requested attribute if it's within GPU limits. */
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr == nullptr && attrs->count < GPU_MAX_UNIFORM_ATTR) {
|
2025-03-05 16:35:09 +01:00
|
|
|
attr = MEM_callocN<GPUUniformAttr>(__func__);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
STRNCPY(attr->name, name);
|
|
|
|
|
attr->use_dupli = use_dupli;
|
2022-09-01 14:46:17 +02:00
|
|
|
attr->hash_code = BLI_ghashutil_strhash_p(attr->name) << 1 | (attr->use_dupli ? 0 : 1);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
attr->id = -1;
|
|
|
|
|
BLI_addtail(&attrs->list, attr);
|
|
|
|
|
attrs->count++;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr != nullptr) {
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
attr->users++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
/** Add a new uniform attribute of given type and name. Returns nullptr if out of slots. */
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
static GPULayerAttr *gpu_node_graph_add_layer_attribute(GPUNodeGraph *graph, const char *name)
|
|
|
|
|
{
|
|
|
|
|
/* Find existing attribute. */
|
|
|
|
|
ListBase *attrs = &graph->layer_attrs;
|
2022-11-18 11:08:39 +01:00
|
|
|
GPULayerAttr *attr = static_cast<GPULayerAttr *>(attrs->first);
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
|
|
|
|
|
for (; attr; attr = attr->next) {
|
|
|
|
|
if (STREQ(attr->name, name)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add new requested attribute to the list. */
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr == nullptr) {
|
2025-03-05 16:35:09 +01:00
|
|
|
attr = MEM_callocN<GPULayerAttr>(__func__);
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
STRNCPY(attr->name, name);
|
|
|
|
|
attr->hash_code = BLI_ghashutil_strhash_p(attr->name);
|
|
|
|
|
BLI_addtail(attrs, attr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr != nullptr) {
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
attr->users++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 13:55:29 +01:00
|
|
|
static GPUMaterialTexture *gpu_node_graph_add_texture(GPUNodeGraph *graph,
|
|
|
|
|
Image *ima,
|
|
|
|
|
ImageUser *iuser,
|
2025-07-22 09:48:10 +02:00
|
|
|
blender::gpu::Texture **colorband,
|
|
|
|
|
blender::gpu::Texture **sky,
|
2023-03-15 17:58:25 +01:00
|
|
|
bool is_tiled,
|
2023-04-04 15:16:07 +02:00
|
|
|
GPUSamplerState sampler_state)
|
2020-02-27 13:55:29 +01:00
|
|
|
{
|
|
|
|
|
/* Find existing texture. */
|
|
|
|
|
int num_textures = 0;
|
2022-11-18 11:08:39 +01:00
|
|
|
GPUMaterialTexture *tex = static_cast<GPUMaterialTexture *>(graph->textures.first);
|
2020-02-27 13:55:29 +01:00
|
|
|
for (; tex; tex = tex->next) {
|
2022-09-16 15:04:47 +02:00
|
|
|
if (tex->ima == ima && tex->colorband == colorband && tex->sky == sky &&
|
|
|
|
|
tex->sampler_state == sampler_state)
|
|
|
|
|
{
|
2020-02-27 13:55:29 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
num_textures++;
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
2020-02-27 13:55:29 +01:00
|
|
|
|
|
|
|
|
/* Add new requested texture. */
|
2022-11-19 11:51:42 +01:00
|
|
|
if (tex == nullptr) {
|
2025-03-05 16:35:09 +01:00
|
|
|
tex = MEM_callocN<GPUMaterialTexture>(__func__);
|
2020-02-27 13:55:29 +01:00
|
|
|
tex->ima = ima;
|
2022-11-19 11:51:42 +01:00
|
|
|
if (iuser != nullptr) {
|
2021-12-25 11:14:02 +01:00
|
|
|
tex->iuser = *iuser;
|
|
|
|
|
tex->iuser_available = true;
|
|
|
|
|
}
|
2020-02-27 13:55:29 +01:00
|
|
|
tex->colorband = colorband;
|
2022-09-16 15:04:47 +02:00
|
|
|
tex->sky = sky;
|
2020-06-03 13:35:15 +02:00
|
|
|
tex->sampler_state = sampler_state;
|
2023-05-09 12:50:37 +10:00
|
|
|
SNPRINTF(tex->sampler_name, "samp%d", num_textures);
|
2023-03-15 17:58:25 +01:00
|
|
|
if (is_tiled) {
|
2023-05-09 12:50:37 +10:00
|
|
|
SNPRINTF(tex->tiled_mapping_name, "tsamp%d", num_textures);
|
2020-02-27 13:55:29 +01:00
|
|
|
}
|
|
|
|
|
BLI_addtail(&graph->textures, tex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tex->users++;
|
|
|
|
|
|
|
|
|
|
return tex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Creating Inputs */
|
|
|
|
|
|
2022-06-01 14:38:06 +10:00
|
|
|
GPUNodeLink *GPU_attribute(GPUMaterial *mat, const eCustomDataType type, const char *name)
|
2020-02-27 13:55:29 +01:00
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
2025-08-27 09:49:43 +02:00
|
|
|
GPUMaterialAttribute *attr = gpu_node_graph_add_attribute(
|
|
|
|
|
graph, type, name, false, false, false);
|
2020-02-27 13:55:29 +01:00
|
|
|
|
2022-04-14 18:47:58 +02:00
|
|
|
if (type == CD_ORCO) {
|
2025-04-17 12:06:12 +10:00
|
|
|
/* OPTI: orco might be computed from local positions and needs object information. */
|
2022-04-14 18:47:58 +02:00
|
|
|
GPU_material_flag_set(mat, GPU_MATFLAG_OBJECT_INFO);
|
|
|
|
|
}
|
|
|
|
|
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
/* Dummy fallback if out of slots. */
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr == nullptr) {
|
2020-02-27 13:55:29 +01:00
|
|
|
static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
|
|
|
|
|
return GPU_constant(zero_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
2022-07-26 08:37:08 -05:00
|
|
|
link->link_type = GPU_NODE_LINK_ATTR;
|
|
|
|
|
link->attr = attr;
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUNodeLink *GPU_attribute_default_color(GPUMaterial *mat)
|
|
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
2023-10-18 11:13:38 +02:00
|
|
|
GPUMaterialAttribute *attr = gpu_node_graph_add_attribute(
|
2025-08-27 09:49:43 +02:00
|
|
|
graph, CD_AUTO_FROM_NAME, "", true, false, false);
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr == nullptr) {
|
2022-07-26 08:37:08 -05:00
|
|
|
static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
|
|
|
|
|
return GPU_constant(zero_data);
|
|
|
|
|
}
|
|
|
|
|
attr->is_default_color = true;
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
2020-02-27 13:55:29 +01:00
|
|
|
link->link_type = GPU_NODE_LINK_ATTR;
|
|
|
|
|
link->attr = attr;
|
2020-02-12 12:48:44 +01:00
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-28 17:17:31 +02:00
|
|
|
GPUNodeLink *GPU_attribute_hair_length(GPUMaterial *mat)
|
|
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
2023-10-18 11:13:38 +02:00
|
|
|
GPUMaterialAttribute *attr = gpu_node_graph_add_attribute(
|
2025-08-27 09:49:43 +02:00
|
|
|
graph, CD_AUTO_FROM_NAME, "", false, true, false);
|
|
|
|
|
if (attr == nullptr) {
|
|
|
|
|
static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
|
|
|
|
|
return GPU_constant(zero_data);
|
|
|
|
|
}
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_ATTR;
|
|
|
|
|
link->attr = attr;
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUNodeLink *GPU_attribute_hair_intercept(GPUMaterial *mat)
|
|
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
|
|
|
|
GPUMaterialAttribute *attr = gpu_node_graph_add_attribute(
|
|
|
|
|
graph, CD_AUTO_FROM_NAME, "", false, false, true);
|
2023-06-28 17:17:31 +02:00
|
|
|
if (attr == nullptr) {
|
|
|
|
|
static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
|
|
|
|
|
return GPU_constant(zero_data);
|
|
|
|
|
}
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_ATTR;
|
|
|
|
|
link->attr = attr;
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 12:01:16 +02:00
|
|
|
GPUNodeLink *GPU_attribute_with_default(GPUMaterial *mat,
|
2022-06-01 14:38:06 +10:00
|
|
|
const eCustomDataType type,
|
2022-04-19 12:01:16 +02:00
|
|
|
const char *name,
|
2025-09-15 15:11:02 +02:00
|
|
|
GPUDefaultValue default_value)
|
2022-04-19 12:01:16 +02:00
|
|
|
{
|
|
|
|
|
GPUNodeLink *link = GPU_attribute(mat, type, name);
|
|
|
|
|
if (link->link_type == GPU_NODE_LINK_ATTR) {
|
|
|
|
|
link->attr->default_value = default_value;
|
|
|
|
|
}
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 14:46:17 +02:00
|
|
|
GPUNodeLink *GPU_uniform_attribute(GPUMaterial *mat,
|
|
|
|
|
const char *name,
|
|
|
|
|
bool use_dupli,
|
|
|
|
|
uint32_t *r_hash)
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
|
|
|
|
GPUUniformAttr *attr = gpu_node_graph_add_uniform_attribute(graph, name, use_dupli);
|
|
|
|
|
|
|
|
|
|
/* Dummy fallback if out of slots. */
|
2022-11-19 11:51:42 +01:00
|
|
|
if (attr == nullptr) {
|
2022-09-01 14:46:17 +02:00
|
|
|
*r_hash = 0;
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
static const float zero_data[GPU_MAX_CONSTANT_DATA] = {0.0f};
|
|
|
|
|
return GPU_constant(zero_data);
|
|
|
|
|
}
|
2022-09-01 14:46:17 +02:00
|
|
|
*r_hash = attr->hash_code;
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_UNIFORM_ATTR;
|
|
|
|
|
link->uniform_attr = attr;
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
GPUNodeLink *GPU_layer_attribute(GPUMaterial *mat, const char *name)
|
|
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
|
|
|
|
GPULayerAttr *attr = gpu_node_graph_add_layer_attribute(graph, name);
|
|
|
|
|
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_LAYER_ATTR;
|
|
|
|
|
link->layer_attr = attr;
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 13:55:29 +01:00
|
|
|
GPUNodeLink *GPU_constant(const float *num)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_CONSTANT;
|
|
|
|
|
link->data = num;
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 13:55:29 +01:00
|
|
|
GPUNodeLink *GPU_uniform(const float *num)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_UNIFORM;
|
|
|
|
|
link->data = num;
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 15:53:35 +01:00
|
|
|
GPUNodeLink *GPU_differentiate_float_function(const char *function_name, const float filter_width)
|
2022-04-14 18:47:58 +02:00
|
|
|
{
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_DIFFERENTIATE_FLOAT_FN;
|
2025-03-24 15:53:35 +01:00
|
|
|
link->differentiate_float.function_name = function_name;
|
|
|
|
|
link->differentiate_float.filter_width = filter_width;
|
2022-04-14 18:47:58 +02:00
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 13:35:15 +02:00
|
|
|
GPUNodeLink *GPU_image(GPUMaterial *mat,
|
|
|
|
|
Image *ima,
|
|
|
|
|
ImageUser *iuser,
|
2023-04-04 15:16:07 +02:00
|
|
|
GPUSamplerState sampler_state)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2020-02-27 13:55:29 +01:00
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
2020-02-12 12:48:44 +01:00
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
2020-02-27 13:55:29 +01:00
|
|
|
link->link_type = GPU_NODE_LINK_IMAGE;
|
2020-06-03 13:35:15 +02:00
|
|
|
link->texture = gpu_node_graph_add_texture(
|
2023-03-15 17:58:25 +01:00
|
|
|
graph, ima, iuser, nullptr, nullptr, false, sampler_state);
|
2022-09-16 15:04:47 +02:00
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUNodeLink *GPU_image_sky(GPUMaterial *mat,
|
|
|
|
|
int width,
|
|
|
|
|
int height,
|
|
|
|
|
const float *pixels,
|
|
|
|
|
float *layer,
|
2023-04-04 15:16:07 +02:00
|
|
|
GPUSamplerState sampler_state)
|
2022-09-16 15:04:47 +02:00
|
|
|
{
|
2025-07-22 09:48:10 +02:00
|
|
|
blender::gpu::Texture **sky = gpu_material_sky_texture_layer_set(
|
|
|
|
|
mat, width, height, pixels, layer);
|
2022-09-16 15:04:47 +02:00
|
|
|
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
|
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_IMAGE_SKY;
|
|
|
|
|
link->texture = gpu_node_graph_add_texture(
|
2023-03-15 17:58:25 +01:00
|
|
|
graph, nullptr, nullptr, nullptr, sky, false, sampler_state);
|
2020-02-27 13:55:29 +01:00
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 17:58:25 +01:00
|
|
|
void GPU_image_tiled(GPUMaterial *mat,
|
2023-06-03 08:36:28 +10:00
|
|
|
Image *ima,
|
|
|
|
|
ImageUser *iuser,
|
2023-04-04 15:16:07 +02:00
|
|
|
GPUSamplerState sampler_state,
|
2023-03-15 17:58:25 +01:00
|
|
|
GPUNodeLink **r_image_tiled_link,
|
|
|
|
|
GPUNodeLink **r_image_tiled_mapping_link)
|
2020-02-27 13:55:29 +01:00
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
2023-03-15 17:58:25 +01:00
|
|
|
GPUMaterialTexture *texture = gpu_node_graph_add_texture(
|
|
|
|
|
graph, ima, iuser, nullptr, nullptr, true, sampler_state);
|
2020-02-27 13:55:29 +01:00
|
|
|
|
2023-03-15 17:58:25 +01:00
|
|
|
(*r_image_tiled_link) = gpu_node_link_create();
|
|
|
|
|
(*r_image_tiled_link)->link_type = GPU_NODE_LINK_IMAGE_TILED;
|
|
|
|
|
(*r_image_tiled_link)->texture = texture;
|
|
|
|
|
|
|
|
|
|
(*r_image_tiled_mapping_link) = gpu_node_link_create();
|
|
|
|
|
(*r_image_tiled_mapping_link)->link_type = GPU_NODE_LINK_IMAGE_TILED_MAPPING;
|
|
|
|
|
(*r_image_tiled_mapping_link)->texture = texture;
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-29 13:01:12 +10:00
|
|
|
GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2025-07-22 09:48:10 +02:00
|
|
|
blender::gpu::Texture **colorband = gpu_material_ramp_texture_row_set(mat, size, pixels, r_row);
|
2020-02-27 13:55:29 +01:00
|
|
|
MEM_freeN(pixels);
|
|
|
|
|
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
2020-02-12 12:48:44 +01:00
|
|
|
GPUNodeLink *link = gpu_node_link_create();
|
|
|
|
|
link->link_type = GPU_NODE_LINK_COLORBAND;
|
2020-06-03 13:35:15 +02:00
|
|
|
link->texture = gpu_node_graph_add_texture(
|
2023-04-04 15:16:07 +02:00
|
|
|
graph, nullptr, nullptr, colorband, nullptr, false, GPUSamplerState::internal_sampler());
|
2020-02-12 12:48:44 +01:00
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Creating Nodes */
|
|
|
|
|
|
|
|
|
|
bool GPU_link(GPUMaterial *mat, const char *name, ...)
|
|
|
|
|
{
|
2022-04-14 18:47:58 +02:00
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(mat);
|
2020-02-12 12:48:44 +01:00
|
|
|
GPUNode *node;
|
|
|
|
|
GPUFunction *function;
|
|
|
|
|
GPUNodeLink *link, **linkptr;
|
|
|
|
|
va_list params;
|
|
|
|
|
int i;
|
|
|
|
|
|
2025-09-22 10:24:10 +02:00
|
|
|
function = gpu_material_library_get_function(name);
|
2020-02-12 12:48:44 +01:00
|
|
|
if (!function) {
|
|
|
|
|
fprintf(stderr, "GPU failed to find function %s\n", name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = gpu_node_create(name);
|
|
|
|
|
|
|
|
|
|
va_start(params, name);
|
|
|
|
|
for (i = 0; i < function->totparam; i++) {
|
2022-05-09 23:51:50 +02:00
|
|
|
if (function->paramqual[i] == FUNCTION_QUAL_OUT) {
|
2020-02-12 12:48:44 +01:00
|
|
|
linkptr = va_arg(params, GPUNodeLink **);
|
|
|
|
|
gpu_node_output(node, function->paramtype[i], linkptr);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
link = va_arg(params, GPUNodeLink *);
|
|
|
|
|
gpu_node_input_link(node, link, function->paramtype[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
va_end(params);
|
|
|
|
|
|
2020-02-27 13:55:29 +01:00
|
|
|
BLI_addtail(&graph->nodes, node);
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 18:47:58 +02:00
|
|
|
static bool gpu_stack_link_v(GPUMaterial *material,
|
2022-08-31 11:49:35 -05:00
|
|
|
const bNode *bnode,
|
2022-04-14 18:47:58 +02:00
|
|
|
const char *name,
|
|
|
|
|
GPUNodeStack *in,
|
|
|
|
|
GPUNodeStack *out,
|
|
|
|
|
va_list params)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2022-04-14 18:47:58 +02:00
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(material);
|
2020-02-12 12:48:44 +01:00
|
|
|
GPUNode *node;
|
|
|
|
|
GPUFunction *function;
|
|
|
|
|
GPUNodeLink *link, **linkptr;
|
|
|
|
|
int i, totin, totout;
|
|
|
|
|
|
2025-09-22 10:24:10 +02:00
|
|
|
function = gpu_material_library_get_function(name);
|
2020-02-12 12:48:44 +01:00
|
|
|
if (!function) {
|
|
|
|
|
fprintf(stderr, "GPU failed to find function %s\n", name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = gpu_node_create(name);
|
|
|
|
|
totin = 0;
|
|
|
|
|
totout = 0;
|
|
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
|
for (i = 0; !in[i].end; i++) {
|
|
|
|
|
if (in[i].type != GPU_NONE) {
|
|
|
|
|
gpu_node_input_socket(material, bnode, node, &in[i], i);
|
|
|
|
|
totin++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (out) {
|
|
|
|
|
for (i = 0; !out[i].end; i++) {
|
|
|
|
|
if (out[i].type != GPU_NONE) {
|
|
|
|
|
gpu_node_output(node, out[i].type, &out[i].link);
|
|
|
|
|
totout++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < function->totparam; i++) {
|
2022-05-09 23:51:50 +02:00
|
|
|
if (function->paramqual[i] == FUNCTION_QUAL_OUT) {
|
2020-02-12 12:48:44 +01:00
|
|
|
if (totout == 0) {
|
|
|
|
|
linkptr = va_arg(params, GPUNodeLink **);
|
|
|
|
|
gpu_node_output(node, function->paramtype[i], linkptr);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
totout--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (totin == 0) {
|
|
|
|
|
link = va_arg(params, GPUNodeLink *);
|
|
|
|
|
if (link->socket) {
|
2022-11-19 11:51:42 +01:00
|
|
|
gpu_node_input_socket(nullptr, nullptr, node, link->socket, -1);
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
gpu_node_input_link(node, link, function->paramtype[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
totin--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 13:55:29 +01:00
|
|
|
BLI_addtail(&graph->nodes, node);
|
2020-02-12 12:48:44 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 18:47:58 +02:00
|
|
|
bool GPU_stack_link(GPUMaterial *material,
|
2022-08-31 11:49:35 -05:00
|
|
|
const bNode *bnode,
|
2022-04-14 18:47:58 +02:00
|
|
|
const char *name,
|
|
|
|
|
GPUNodeStack *in,
|
|
|
|
|
GPUNodeStack *out,
|
|
|
|
|
...)
|
|
|
|
|
{
|
|
|
|
|
va_list params;
|
|
|
|
|
va_start(params, out);
|
|
|
|
|
bool valid = gpu_stack_link_v(material, bnode, name, in, out, params);
|
|
|
|
|
va_end(params);
|
|
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 16:38:14 +02:00
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
GPUNodeGraph *graph = gpu_material_node_graph(material);
|
|
|
|
|
GPUNode *node;
|
|
|
|
|
int i, totin, totout;
|
|
|
|
|
|
|
|
|
|
node = gpu_node_create(name);
|
|
|
|
|
node->zone_index = zone_index;
|
|
|
|
|
node->is_zone_end = is_zone_end;
|
|
|
|
|
|
|
|
|
|
totin = 0;
|
|
|
|
|
totout = 0;
|
|
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
|
for (i = 0; !in[i].end; i++) {
|
|
|
|
|
if (in[i].type != GPU_NONE) {
|
|
|
|
|
gpu_node_input_socket(material, bnode, node, &in[i], i);
|
|
|
|
|
totin++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (out) {
|
|
|
|
|
for (i = 0; !out[i].end; i++) {
|
|
|
|
|
if (out[i].type != GPU_NONE) {
|
|
|
|
|
gpu_node_output(node, out[i].type, &out[i].link);
|
|
|
|
|
totout++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LISTBASE_FOREACH_INDEX (GPUInput *, input, &node->inputs, i) {
|
|
|
|
|
input->is_zone_io = i >= in_argument_count;
|
|
|
|
|
input->is_duplicate = input->is_zone_io && is_zone_end;
|
|
|
|
|
}
|
|
|
|
|
LISTBASE_FOREACH_INDEX (GPUOutput *, output, &node->outputs, i) {
|
|
|
|
|
output->is_zone_io = i >= out_argument_count;
|
|
|
|
|
output->is_duplicate = output->is_zone_io;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_addtail(&graph->nodes, node);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 12:48:44 +01:00
|
|
|
/* Node Graph */
|
|
|
|
|
|
|
|
|
|
static void gpu_inputs_free(ListBase *inputs)
|
|
|
|
|
{
|
2023-08-04 08:51:13 +10:00
|
|
|
LISTBASE_FOREACH (GPUInput *, input, inputs) {
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
switch (input->source) {
|
|
|
|
|
case GPU_SOURCE_ATTR:
|
|
|
|
|
input->attr->users--;
|
|
|
|
|
break;
|
|
|
|
|
case GPU_SOURCE_UNIFORM_ATTR:
|
|
|
|
|
input->uniform_attr->users--;
|
|
|
|
|
break;
|
|
|
|
|
case GPU_SOURCE_LAYER_ATTR:
|
|
|
|
|
input->layer_attr->users--;
|
|
|
|
|
break;
|
|
|
|
|
case GPU_SOURCE_TEX:
|
|
|
|
|
input->texture->users--;
|
|
|
|
|
break;
|
2023-12-21 18:55:32 +01:00
|
|
|
case GPU_SOURCE_TEX_TILED_MAPPING:
|
|
|
|
|
/* Already handled by GPU_SOURCE_TEX. */
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
default:
|
|
|
|
|
break;
|
2020-02-27 13:55:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-12 12:48:44 +01:00
|
|
|
if (input->link) {
|
|
|
|
|
gpu_node_link_free(input->link);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_freelistN(inputs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void gpu_node_free(GPUNode *node)
|
|
|
|
|
{
|
|
|
|
|
gpu_inputs_free(&node->inputs);
|
|
|
|
|
|
2023-08-04 08:51:13 +10:00
|
|
|
LISTBASE_FOREACH (GPUOutput *, output, &node->outputs) {
|
2020-02-12 12:48:44 +01:00
|
|
|
if (output->link) {
|
2022-11-19 11:51:42 +01:00
|
|
|
output->link->output = nullptr;
|
2020-02-12 12:48:44 +01:00
|
|
|
gpu_node_link_free(output->link);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BLI_freelistN(&node->outputs);
|
|
|
|
|
MEM_freeN(node);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 10:47:20 +01:00
|
|
|
void gpu_node_graph_free_nodes(GPUNodeGraph *graph)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2023-08-03 18:23:49 +10:00
|
|
|
while (GPUNode *node = static_cast<GPUNode *>(BLI_pophead(&graph->nodes))) {
|
2020-02-12 12:48:44 +01:00
|
|
|
gpu_node_free(node);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
graph->outlink_surface = nullptr;
|
|
|
|
|
graph->outlink_volume = nullptr;
|
|
|
|
|
graph->outlink_displacement = nullptr;
|
|
|
|
|
graph->outlink_thickness = nullptr;
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-14 10:47:20 +01:00
|
|
|
void gpu_node_graph_free(GPUNodeGraph *graph)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2020-12-04 08:13:54 +01:00
|
|
|
BLI_freelistN(&graph->outlink_aovs);
|
2022-04-14 18:47:58 +02:00
|
|
|
BLI_freelistN(&graph->material_functions);
|
2022-07-29 08:37:57 +02:00
|
|
|
BLI_freelistN(&graph->outlink_compositor);
|
2020-02-14 10:47:20 +01:00
|
|
|
gpu_node_graph_free_nodes(graph);
|
2020-03-11 14:58:19 +01:00
|
|
|
|
2020-02-14 10:47:20 +01:00
|
|
|
BLI_freelistN(&graph->textures);
|
2020-02-27 13:55:29 +01:00
|
|
|
BLI_freelistN(&graph->attributes);
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
GPU_uniform_attr_list_free(&graph->uniform_attrs);
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
BLI_freelistN(&graph->layer_attrs);
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Prune Unused Nodes */
|
|
|
|
|
|
2025-10-08 16:38:14 +02:00
|
|
|
void gpu_nodes_tag(GPUNodeGraph *graph, GPUNodeLink *link_start, GPUNodeTag tag)
|
2020-02-12 12:48:44 +01:00
|
|
|
{
|
2025-10-08 16:38:14 +02:00
|
|
|
if (!link_start || !link_start->output) {
|
2020-02-12 12:48:44 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 16:38:14 +02:00
|
|
|
blender::Stack<GPUNode *> stack;
|
|
|
|
|
blender::Stack<GPUNode *> zone_stack;
|
|
|
|
|
stack.push(link_start->output->node);
|
2020-02-12 12:48:44 +01:00
|
|
|
|
2025-10-08 16:38:14 +02:00
|
|
|
while (!stack.is_empty() || !zone_stack.is_empty()) {
|
|
|
|
|
GPUNode *node = !stack.is_empty() ? stack.pop() : zone_stack.pop();
|
|
|
|
|
|
|
|
|
|
if (node->tag & tag) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node->tag |= tag;
|
|
|
|
|
LISTBASE_FOREACH (GPUInput *, input, &node->inputs) {
|
|
|
|
|
if (input->link && input->link->output) {
|
|
|
|
|
stack.push(input->link->output->node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Zone input nodes are implicitly linked to their corresponding zone output nodes,
|
|
|
|
|
* even if there is no GPUNodeLink between them. */
|
|
|
|
|
if (node->is_zone_end) {
|
|
|
|
|
LISTBASE_FOREACH (GPUNode *, node2, &graph->nodes) {
|
|
|
|
|
if (node2->zone_index == node->zone_index && !node2->is_zone_end && !(node2->tag & tag)) {
|
|
|
|
|
node2->tag |= tag;
|
|
|
|
|
LISTBASE_FOREACH (GPUInput *, input, &node2->inputs) {
|
|
|
|
|
if (input->link && input->link->output) {
|
|
|
|
|
zone_stack.push(input->link->output->node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void gpu_node_graph_prune_unused(GPUNodeGraph *graph)
|
|
|
|
|
{
|
2020-04-03 19:15:01 +02:00
|
|
|
LISTBASE_FOREACH (GPUNode *, node, &graph->nodes) {
|
2022-04-14 18:47:58 +02:00
|
|
|
node->tag = GPU_NODE_TAG_NONE;
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
2025-10-08 16:38:14 +02:00
|
|
|
gpu_nodes_tag(graph, graph->outlink_surface, GPU_NODE_TAG_SURFACE);
|
|
|
|
|
gpu_nodes_tag(graph, graph->outlink_volume, GPU_NODE_TAG_VOLUME);
|
|
|
|
|
gpu_nodes_tag(graph, graph->outlink_displacement, GPU_NODE_TAG_DISPLACEMENT);
|
|
|
|
|
gpu_nodes_tag(graph, graph->outlink_thickness, GPU_NODE_TAG_THICKNESS);
|
2022-04-14 18:47:58 +02:00
|
|
|
|
2020-12-04 08:13:54 +01:00
|
|
|
LISTBASE_FOREACH (GPUNodeGraphOutputLink *, aovlink, &graph->outlink_aovs) {
|
2025-10-08 16:38:14 +02:00
|
|
|
gpu_nodes_tag(graph, aovlink->outlink, GPU_NODE_TAG_AOV);
|
2022-04-14 18:47:58 +02:00
|
|
|
}
|
|
|
|
|
LISTBASE_FOREACH (GPUNodeGraphFunctionLink *, funclink, &graph->material_functions) {
|
2025-10-08 16:38:14 +02:00
|
|
|
gpu_nodes_tag(graph, funclink->outlink, GPU_NODE_TAG_FUNCTION);
|
2020-12-04 08:13:54 +01:00
|
|
|
}
|
2022-07-29 08:37:57 +02:00
|
|
|
LISTBASE_FOREACH (GPUNodeGraphOutputLink *, compositor_link, &graph->outlink_compositor) {
|
2025-10-08 16:38:14 +02:00
|
|
|
gpu_nodes_tag(graph, compositor_link->outlink, GPU_NODE_TAG_COMPOSITOR);
|
2022-07-29 08:37:57 +02:00
|
|
|
}
|
2020-02-12 12:48:44 +01:00
|
|
|
|
2022-11-19 11:51:42 +01:00
|
|
|
for (GPUNode *node = static_cast<GPUNode *>(graph->nodes.first), *next = nullptr; node;
|
2022-11-18 11:08:39 +01:00
|
|
|
node = next)
|
|
|
|
|
{
|
2020-02-12 12:48:44 +01:00
|
|
|
next = node->next;
|
|
|
|
|
|
2022-04-14 18:47:58 +02:00
|
|
|
if (node->tag == GPU_NODE_TAG_NONE) {
|
2020-02-12 12:48:44 +01:00
|
|
|
BLI_remlink(&graph->nodes, node);
|
|
|
|
|
gpu_node_free(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-27 13:55:29 +01:00
|
|
|
|
2022-11-18 11:08:39 +01:00
|
|
|
for (GPUMaterialAttribute *attr = static_cast<GPUMaterialAttribute *>(graph->attributes.first),
|
2022-11-19 11:51:42 +01:00
|
|
|
*next = nullptr;
|
2022-11-18 11:08:39 +01:00
|
|
|
attr;
|
|
|
|
|
attr = next)
|
|
|
|
|
{
|
2020-02-27 13:55:29 +01:00
|
|
|
next = attr->next;
|
|
|
|
|
if (attr->users == 0) {
|
|
|
|
|
BLI_freelinkN(&graph->attributes, attr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 11:08:39 +01:00
|
|
|
for (GPUMaterialTexture *tex = static_cast<GPUMaterialTexture *>(graph->textures.first),
|
2022-11-19 11:51:42 +01:00
|
|
|
*next = nullptr;
|
2022-11-18 11:08:39 +01:00
|
|
|
tex;
|
|
|
|
|
tex = next)
|
|
|
|
|
{
|
2020-02-27 13:55:29 +01:00
|
|
|
next = tex->next;
|
|
|
|
|
if (tex->users == 0) {
|
|
|
|
|
BLI_freelinkN(&graph->textures, tex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-11 14:58:19 +01:00
|
|
|
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
GPUUniformAttrList *uattrs = &graph->uniform_attrs;
|
|
|
|
|
|
|
|
|
|
LISTBASE_FOREACH_MUTABLE (GPUUniformAttr *, attr, &uattrs->list) {
|
|
|
|
|
if (attr->users == 0) {
|
|
|
|
|
BLI_freelinkN(&uattrs->list, attr);
|
|
|
|
|
uattrs->count--;
|
|
|
|
|
}
|
|
|
|
|
}
|
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
2022-09-12 00:30:58 +03:00
|
|
|
|
|
|
|
|
LISTBASE_FOREACH_MUTABLE (GPULayerAttr *, attr, &graph->layer_attrs) {
|
|
|
|
|
if (attr->users == 0) {
|
|
|
|
|
BLI_freelinkN(&graph->layer_attrs, attr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 12:48:44 +01:00
|
|
|
}
|
2023-02-14 21:51:03 +01:00
|
|
|
|
|
|
|
|
void gpu_node_graph_optimize(GPUNodeGraph *graph)
|
|
|
|
|
{
|
|
|
|
|
/* Replace all uniform node links with constant. */
|
|
|
|
|
LISTBASE_FOREACH (GPUNode *, node, &graph->nodes) {
|
|
|
|
|
LISTBASE_FOREACH (GPUInput *, input, &node->inputs) {
|
|
|
|
|
if (input->link) {
|
|
|
|
|
if (input->link->link_type == GPU_NODE_LINK_UNIFORM) {
|
|
|
|
|
input->link->link_type = GPU_NODE_LINK_CONSTANT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (input->source == GPU_SOURCE_UNIFORM) {
|
|
|
|
|
input->source = (input->type == GPU_CLOSURE) ? GPU_SOURCE_STRUCT : GPU_SOURCE_CONSTANT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO: Consider performing other node graph optimizations here. */
|
|
|
|
|
}
|