GPUCodegen: Add New GPU_SOURCE_STRUCT to be used for Closure Sockets

This way we dont rely on the static array of chars that was causing T52385.

That fixes T52385.
This commit is contained in:
Clément Foucault 2017-09-14 01:02:10 +02:00
parent a01fbc6689
commit 3b080c3f66
Notes: blender-bot 2023-02-14 06:41:43 +01:00
Referenced by issue #52855, Eevee: viewport artifacts
Referenced by issue #52385, Blender 2.8: Crash with background nodes
3 changed files with 38 additions and 23 deletions

View File

@ -70,6 +70,7 @@ typedef struct GPUParticleInfo GPUParticleInfo;
/* Functions to create GPU Materials nodes */
typedef enum GPUType {
/* Keep in sync with GPU_DATATYPE_STR */
/* The value indicates the number of elements in each type */
GPU_NONE = 0,
GPU_FLOAT = 1,
@ -79,11 +80,15 @@ typedef enum GPUType {
GPU_MAT3 = 9,
GPU_MAT4 = 16,
GPU_CLOSURE = 17,
/* Values not in GPU_DATATYPE_STR */
GPU_TEX2D = 1002,
GPU_SHADOW2D = 1003,
GPU_TEXCUBE = 1004,
/* GLSL Struct types */
GPU_CLOSURE = 1005,
/* Opengl Attributes */
GPU_ATTRIB = 3001
} GPUType;

View File

@ -88,10 +88,9 @@ typedef struct GPUFunction {
} GPUFunction;
/* Indices match the GPUType enum */
static const char *GPU_DATATYPE_STR[18] = {
static const char *GPU_DATATYPE_STR[17] = {
"", "float", "vec2", "vec3", "vec4",
NULL, NULL, NULL, NULL, "mat3", NULL, NULL, NULL, NULL, NULL, NULL, "mat4",
"Closure"
NULL, NULL, NULL, NULL, "mat3", NULL, NULL, NULL, NULL, NULL, NULL, "mat4"
};
/* GLSL code parsing for finding function definitions.
@ -192,6 +191,10 @@ static void gpu_parse_functions_string(GHash *hash, char *code)
type = GPU_TEX2D;
}
if (!type && gpu_str_prefix(code, "Closure")) {
type = GPU_CLOSURE;
}
if (type) {
/* add parameter */
code = gpu_str_skip_token(code, NULL, 0);
@ -362,11 +365,6 @@ static void codegen_print_datatype(DynStr *ds, const GPUType type, float *data)
{
int i;
if (type == GPU_CLOSURE) {
BLI_dynstr_append(ds, "CLOSURE_DEFAULT");
return;
}
BLI_dynstr_appendf(ds, "%s(", GPU_DATATYPE_STR[type]);
for (i = 0; i < type; i++) {
@ -550,6 +548,10 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds,
}
}
}
else if (input->source == GPU_SOURCE_STRUCT) {
/* Add other struct here if needed. */
BLI_dynstr_appendf(ds, "Closure strct%d = CLOSURE_DEFAULT;\n", input->id);
}
else if (input->source == GPU_SOURCE_VEC_UNIFORM) {
if (input->dynamictype == GPU_DYNAMIC_UBO) {
if (!input->link) {
@ -563,16 +565,8 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds,
GPU_DATATYPE_STR[input->type], input->id);
}
else {
if (input->type != GPU_CLOSURE) {
/* for others use const so the compiler can do folding */
BLI_dynstr_appendf(ds, "const %s cons%d = ",
GPU_DATATYPE_STR[input->type], input->id);
}
else {
/* const keyword does not work with struct */
BLI_dynstr_appendf(ds, "%s cons%d = ",
GPU_DATATYPE_STR[input->type], input->id);
}
BLI_dynstr_appendf(ds, "const %s cons%d = ",
GPU_DATATYPE_STR[input->type], input->id);
codegen_print_datatype(ds, input->type, input->vec);
BLI_dynstr_append(ds, ";\n");
}
@ -637,8 +631,13 @@ static void codegen_declare_tmps(DynStr *ds, ListBase *nodes)
/* declare temporary variables for node output storage */
for (output = node->outputs.first; output; output = output->next) {
BLI_dynstr_appendf(ds, "\t%s tmp%d;\n",
GPU_DATATYPE_STR[output->type], output->id);
if (output->type == GPU_CLOSURE) {
BLI_dynstr_appendf(ds, "\tClosure tmp%d;\n", output->id);
}
else {
BLI_dynstr_appendf(ds, "\t%s tmp%d;\n",
GPU_DATATYPE_STR[output->type], output->id);
}
}
}
@ -682,6 +681,9 @@ static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *final
else
BLI_dynstr_append(ds, GPU_builtin_name(input->builtin));
}
else if (input->source == GPU_SOURCE_STRUCT) {
BLI_dynstr_appendf(ds, "strct%d", input->id);
}
else if (input->source == GPU_SOURCE_VEC_UNIFORM) {
if (input->dynamicvec)
BLI_dynstr_appendf(ds, "unf%d", input->id);
@ -1525,6 +1527,12 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType
BLI_strncpy(input->attribname, link->attribname, sizeof(input->attribname));
MEM_freeN(link);
}
else if (type == GPU_CLOSURE) {
input->type = type;
input->source = GPU_SOURCE_STRUCT;
MEM_freeN(link);
}
else {
/* uniform vector */
input->type = type;
@ -1536,6 +1544,7 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType
input->dynamictype = link->dynamictype;
input->dynamicdata = link->ptr2;
}
MEM_freeN(link);
}

View File

@ -57,7 +57,8 @@ typedef enum GPUDataSource {
GPU_SOURCE_OPENGL_BUILTIN,
GPU_SOURCE_TEX_PIXEL,
GPU_SOURCE_TEX,
GPU_SOURCE_ATTRIB
GPU_SOURCE_ATTRIB,
GPU_SOURCE_STRUCT
} GPUDataSource;
typedef enum {