Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans Goudey <h.goudey@me.com>2022-07-26 16:37:08 +0300
committerHans Goudey <h.goudey@me.com>2022-07-26 16:37:38 +0300
commit1998269b109f65373336087da7f9751a3c3317f3 (patch)
tree5aa1123e516fd2874fa6626dcba193aec88cacde /source/blender/gpu
parent5945a90df99b919229c876c2fd294f035e24831a (diff)
Refactor: Extract color attributes as generic attributes
Previously there was a special extraction process for "vertex colors" that copied the color data to the GPU with a special format. Instead, this patch replaces this with use of the generic attribute extraction. This reduces the number of code paths, allowing easier optimization in the future. To make it possible to use the generic extraction system for attributes but also assign aliases for use by shaders, some changes are necessary. First, the GPU material attribute can now store whether it actually refers to the default color attribute, rather than a specific name. This replaces the hack to use `CD_MCOL` in the color attribute shader node. Second, the extraction code checks the names against the default and active names and assigns aliases if the request corresponds to a special active attribute. Finally, support for byte color attributes was added to the generic attribute extraction. Differential Revision: https://developer.blender.org/D15205
Diffstat (limited to 'source/blender/gpu')
-rw-r--r--source/blender/gpu/GPU_material.h11
-rw-r--r--source/blender/gpu/intern/gpu_node_graph.c35
2 files changed, 38 insertions, 8 deletions
diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h
index c0633f0323d..3ca465fa57a 100644
--- a/source/blender/gpu/GPU_material.h
+++ b/source/blender/gpu/GPU_material.h
@@ -131,6 +131,11 @@ typedef void (*GPUCodegenCallbackFn)(void *thunk, GPUMaterial *mat, GPUCodegenOu
GPUNodeLink *GPU_constant(const float *num);
GPUNodeLink *GPU_uniform(const float *num);
GPUNodeLink *GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name);
+/**
+ * Add a GPU attribute that refers to the default color attribute on a geometry.
+ * The name, type, and domain are unknown and do not depend on the material.
+ */
+GPUNodeLink *GPU_attribute_default_color(GPUMaterial *mat);
GPUNodeLink *GPU_attribute_with_default(GPUMaterial *mat,
eCustomDataType type,
const char *name,
@@ -266,6 +271,12 @@ typedef struct GPUMaterialAttribute {
eGPUDefaultValue default_value; /* Only for volumes attributes. */
int id;
int users;
+ /**
+ * If true, the corresponding attribute is the specified default color attribute on the mesh,
+ * if it exists. In that case the type and name data can vary per geometry, so it will not be
+ * valid here.
+ */
+ bool is_default_color;
} GPUMaterialAttribute;
typedef struct GPUMaterialTexture {
diff --git a/source/blender/gpu/intern/gpu_node_graph.c b/source/blender/gpu/intern/gpu_node_graph.c
index 1338c5312c2..684070dbdc0 100644
--- a/source/blender/gpu/intern/gpu_node_graph.c
+++ b/source/blender/gpu/intern/gpu_node_graph.c
@@ -328,13 +328,14 @@ void gpu_node_graph_finalize_uniform_attrs(GPUNodeGraph *graph)
/* Attributes and Textures */
-static char attr_prefix_get(eCustomDataType type)
+static char attr_prefix_get(GPUMaterialAttribute *attr)
{
- switch (type) {
+ if (attr->is_default_color) {
+ return 'c';
+ }
+ switch (attr->type) {
case CD_TANGENT:
return 't';
- case CD_MCOL:
- return 'c';
case CD_AUTO_FROM_NAME:
return 'a';
case CD_HAIRLENGTH:
@@ -353,7 +354,7 @@ static void attr_input_name(GPUMaterialAttribute *attr)
STRNCPY(attr->input_name, "orco");
}
else {
- attr->input_name[0] = attr_prefix_get(attr->type);
+ attr->input_name[0] = attr_prefix_get(attr);
attr->input_name[1] = '\0';
if (attr->name[0] != '\0') {
/* XXX FIXME: see notes in mesh_render_data_create() */
@@ -365,13 +366,15 @@ static void attr_input_name(GPUMaterialAttribute *attr)
/** Add a new varying attribute of given type and name. Returns NULL if out of slots. */
static GPUMaterialAttribute *gpu_node_graph_add_attribute(GPUNodeGraph *graph,
eCustomDataType type,
- const char *name)
+ const char *name,
+ const bool is_default_color)
{
/* Find existing attribute. */
int num_attributes = 0;
GPUMaterialAttribute *attr = graph->attributes.first;
for (; attr; attr = attr->next) {
- if (attr->type == type && STREQ(attr->name, name)) {
+ if (attr->type == type && STREQ(attr->name, name) &&
+ attr->is_default_color == is_default_color) {
break;
}
num_attributes++;
@@ -380,6 +383,7 @@ static GPUMaterialAttribute *gpu_node_graph_add_attribute(GPUNodeGraph *graph,
/* Add new requested attribute if it's within GPU limits. */
if (attr == NULL) {
attr = MEM_callocN(sizeof(*attr), __func__);
+ attr->is_default_color = is_default_color;
attr->type = type;
STRNCPY(attr->name, name);
attr_input_name(attr);
@@ -471,7 +475,7 @@ static GPUMaterialTexture *gpu_node_graph_add_texture(GPUNodeGraph *graph,
GPUNodeLink *GPU_attribute(GPUMaterial *mat, const eCustomDataType type, const char *name)
{
GPUNodeGraph *graph = gpu_material_node_graph(mat);
- GPUMaterialAttribute *attr = gpu_node_graph_add_attribute(graph, type, name);
+ GPUMaterialAttribute *attr = gpu_node_graph_add_attribute(graph, type, name, false);
if (type == CD_ORCO) {
/* OPTI: orco might be computed from local positions and needs object infos. */
@@ -490,6 +494,21 @@ GPUNodeLink *GPU_attribute(GPUMaterial *mat, const eCustomDataType type, const c
return link;
}
+GPUNodeLink *GPU_attribute_default_color(GPUMaterial *mat)
+{
+ GPUNodeGraph *graph = gpu_material_node_graph(mat);
+ GPUMaterialAttribute *attr = gpu_node_graph_add_attribute(graph, CD_AUTO_FROM_NAME, "", true);
+ if (attr == NULL) {
+ 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();
+ link->link_type = GPU_NODE_LINK_ATTR;
+ link->attr = attr;
+ return link;
+}
+
GPUNodeLink *GPU_attribute_with_default(GPUMaterial *mat,
const eCustomDataType type,
const char *name,