diff options
-rw-r--r-- | source/blender/draw/intern/draw_manager_data.c | 45 | ||||
-rw-r--r-- | source/blender/gpu/GPU_material.h | 71 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_codegen.c | 363 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_codegen.h | 125 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_uniformbuffer.c | 2 |
5 files changed, 181 insertions, 425 deletions
diff --git a/source/blender/draw/intern/draw_manager_data.c b/source/blender/draw/intern/draw_manager_data.c index b97101c0e6d..08045687795 100644 --- a/source/blender/draw/intern/draw_manager_data.c +++ b/source/blender/draw/intern/draw_manager_data.c @@ -757,46 +757,25 @@ static DRWShadingGroup *drw_shgroup_material_create_ex(GPUPass *gpupass, DRWPass static DRWShadingGroup *drw_shgroup_material_inputs(DRWShadingGroup *grp, struct GPUMaterial *material) { - /* TODO : Ideally we should not convert. But since the whole codegen - * is relying on GPUPass we keep it as is for now. */ - ListBase *inputs = GPU_material_get_inputs(material); /* Converting dynamic GPUInput to DRWUniform */ for (GPUInput *input = inputs->first; input; input = input->next) { /* Textures */ - if (input->ima) { - double time = 0.0; /* TODO make time variable */ - GPUTexture *tex = GPU_texture_from_blender( - input->ima, input->iuser, input->textarget, input->image_isdata, time); + if (input->source == GPU_SOURCE_TEX) { + GPUTexture *tex = NULL; - if (input->bindtex) { - DRW_shgroup_uniform_texture(grp, input->shadername, tex); + if (input->ima) { + double time = 0.0; /* TODO make time variable */ + tex = GPU_texture_from_blender(input->ima, input->iuser, GL_TEXTURE_2D, input->image_isdata, time); } - } - /* Color Ramps */ - else if (input->tex) { - /* HACK : input->tex is a (GPUTexture **) in this case. */ - GPUTexture *tex = *((GPUTexture **)input->tex); - DRW_shgroup_uniform_texture(grp, input->shadername, tex); - } - /* Floats */ - else { - switch (input->type) { - case GPU_FLOAT: - case GPU_VEC2: - case GPU_VEC3: - case GPU_VEC4: - /* Should already be in the material ubo. */ - break; - case GPU_MAT3: - DRW_shgroup_uniform_mat3(grp, input->shadername, (float (*)[3])input->dynamicvec); - break; - case GPU_MAT4: - DRW_shgroup_uniform_mat4(grp, input->shadername, (float (*)[4])input->dynamicvec); - break; - default: - break; + else { + /* Color Ramps */ + tex = *input->coba; + } + + if (input->bindtex) { + drw_shgroup_uniform_create_ex(grp, input->shaderloc, DRW_UNIFORM_TEXTURE, tex, 0, 1); } } } diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h index 5bbe9d6d904..ab55b9c61e7 100644 --- a/source/blender/gpu/GPU_material.h +++ b/source/blender/gpu/GPU_material.h @@ -115,11 +115,6 @@ typedef enum GPUBuiltin { GPU_VOLUME_TEMPERATURE = (1 << 18) } GPUBuiltin; -typedef enum GPUOpenGLBuiltin { - GPU_MATCAP_NORMAL = 1, - GPU_COLOR = 2, -} GPUOpenGLBuiltin; - typedef enum GPUMatType { GPU_MATERIAL_TYPE_MESH = 1, GPU_MATERIAL_TYPE_WORLD = 2, @@ -157,72 +152,6 @@ typedef enum GPUMaterialStatus { GPU_MAT_SUCCESS, } GPUMaterialStatus; -#define GPU_DYNAMIC_GROUP_FROM_TYPE(f) ((f) & 0xFFFF0000) - -#define GPU_DYNAMIC_GROUP_MISC 0x00010000 -#define GPU_DYNAMIC_GROUP_LAMP 0x00020000 -#define GPU_DYNAMIC_GROUP_OBJECT 0x00030000 -#define GPU_DYNAMIC_GROUP_SAMPLER 0x00040000 -#define GPU_DYNAMIC_GROUP_MIST 0x00050000 -#define GPU_DYNAMIC_GROUP_WORLD 0x00060000 -#define GPU_DYNAMIC_GROUP_MAT 0x00070000 -#define GPU_DYNAMIC_UBO 0x00080000 - -typedef enum GPUDynamicType { - - GPU_DYNAMIC_NONE = 0, - - GPU_DYNAMIC_OBJECT_VIEWMAT = 1 | GPU_DYNAMIC_GROUP_OBJECT, - GPU_DYNAMIC_OBJECT_MAT = 2 | GPU_DYNAMIC_GROUP_OBJECT, - GPU_DYNAMIC_OBJECT_VIEWIMAT = 3 | GPU_DYNAMIC_GROUP_OBJECT, - GPU_DYNAMIC_OBJECT_IMAT = 4 | GPU_DYNAMIC_GROUP_OBJECT, - GPU_DYNAMIC_OBJECT_COLOR = 5 | GPU_DYNAMIC_GROUP_OBJECT, - GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE = 6 | GPU_DYNAMIC_GROUP_OBJECT, - GPU_DYNAMIC_OBJECT_LOCTOVIEWMAT = 7 | GPU_DYNAMIC_GROUP_OBJECT, - GPU_DYNAMIC_OBJECT_LOCTOVIEWIMAT = 8 | GPU_DYNAMIC_GROUP_OBJECT, - - GPU_DYNAMIC_LAMP_DYNVEC = 1 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_DYNCO = 2 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_DYNIMAT = 3 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_DYNPERSMAT = 4 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_DYNENERGY = 5 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_DYNCOL = 6 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_DISTANCE = 7 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_ATT1 = 8 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_ATT2 = 9 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_SPOTSIZE = 10 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_SPOTBLEND = 11 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_SPOTSCALE = 12 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_COEFFCONST = 13 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_COEFFLIN = 14 | GPU_DYNAMIC_GROUP_LAMP, - GPU_DYNAMIC_LAMP_COEFFQUAD = 15 | GPU_DYNAMIC_GROUP_LAMP, - - GPU_DYNAMIC_SAMPLER_2DBUFFER = 1 | GPU_DYNAMIC_GROUP_SAMPLER, - GPU_DYNAMIC_SAMPLER_2DIMAGE = 2 | GPU_DYNAMIC_GROUP_SAMPLER, - GPU_DYNAMIC_SAMPLER_2DSHADOW = 3 | GPU_DYNAMIC_GROUP_SAMPLER, - - GPU_DYNAMIC_MIST_ENABLE = 1 | GPU_DYNAMIC_GROUP_MIST, - GPU_DYNAMIC_MIST_START = 2 | GPU_DYNAMIC_GROUP_MIST, - GPU_DYNAMIC_MIST_DISTANCE = 3 | GPU_DYNAMIC_GROUP_MIST, - GPU_DYNAMIC_MIST_INTENSITY = 4 | GPU_DYNAMIC_GROUP_MIST, - GPU_DYNAMIC_MIST_TYPE = 5 | GPU_DYNAMIC_GROUP_MIST, - GPU_DYNAMIC_MIST_COLOR = 6 | GPU_DYNAMIC_GROUP_MIST, - - GPU_DYNAMIC_HORIZON_COLOR = 1 | GPU_DYNAMIC_GROUP_WORLD, - GPU_DYNAMIC_AMBIENT_COLOR = 2 | GPU_DYNAMIC_GROUP_WORLD, - GPU_DYNAMIC_ZENITH_COLOR = 3 | GPU_DYNAMIC_GROUP_WORLD, - - GPU_DYNAMIC_MAT_DIFFRGB = 1 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_REF = 2 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_SPECRGB = 3 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_SPEC = 4 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_HARD = 5 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_EMIT = 6 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_AMB = 7 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_ALPHA = 8 | GPU_DYNAMIC_GROUP_MAT, - GPU_DYNAMIC_MAT_MIR = 9 | GPU_DYNAMIC_GROUP_MAT -} GPUDynamicType; - GPUNodeLink *GPU_attribute(CustomDataType type, const char *name); GPUNodeLink *GPU_constant(float *num); GPUNodeLink *GPU_uniform(float *num); diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c index 0eee9bf6ead..0cda67b8968 100644 --- a/source/blender/gpu/intern/gpu_codegen.c +++ b/source/blender/gpu/intern/gpu_codegen.c @@ -137,12 +137,8 @@ static GPUPass *gpu_pass_cache_resolve_collision( /* type definitions and constants */ -enum { - MAX_FUNCTION_NAME = 64 -}; -enum { - MAX_PARAMETER = 32 -}; +#define MAX_FUNCTION_NAME 64 +#define MAX_PARAMETER 32 typedef enum { FUNCTION_QUAL_IN, @@ -467,10 +463,8 @@ static int codegen_input_has_texture(GPUInput *input) { if (input->link) return 0; - else if (input->ima || input->prv) - return 1; else - return input->tex != NULL; + return (input->source == GPU_SOURCE_TEX); } const char *GPU_builtin_name(GPUBuiltin builtin) @@ -535,70 +529,44 @@ static void codegen_set_texid(GHash *bindhash, GPUInput *input, int *texid, void static void codegen_set_unique_ids(ListBase *nodes) { - GHash *bindhash, *definehash; + GHash *bindhash; GPUNode *node; GPUInput *input; GPUOutput *output; int id = 1, texid = 0; bindhash = BLI_ghash_ptr_new("codegen_set_unique_ids1 gh"); - definehash = BLI_ghash_ptr_new("codegen_set_unique_ids2 gh"); for (node = nodes->first; node; node = node->next) { for (input = node->inputs.first; input; input = input->next) { /* set id for unique names of uniform variables */ input->id = id++; - input->bindtex = false; - input->definetex = false; - /* set texid used for settings texture slot with multitexture */ - if (codegen_input_has_texture(input) && - ((input->source == GPU_SOURCE_TEX) || (input->source == GPU_SOURCE_TEX_PIXEL))) - { - /* assign only one texid per buffer to avoid sampling - * the same texture twice */ - if (input->link) { - /* input is texture from buffer */ - codegen_set_texid(bindhash, input, &texid, input->link); - } - else if (input->ima) { + /* set texid used for settings texture slot */ + if (codegen_input_has_texture(input)) { + input->bindtex = false; + if (input->ima) { /* input is texture from image */ codegen_set_texid(bindhash, input, &texid, input->ima); } - else if (input->prv) { - /* input is texture from preview render */ - codegen_set_texid(bindhash, input, &texid, input->prv); - } - else if (input->tex) { - /* input is user created texture, check tex pointer */ - codegen_set_texid(bindhash, input, &texid, input->tex); + else if (input->coba) { + /* input is color band texture, check coba pointer */ + codegen_set_texid(bindhash, input, &texid, input->coba); } - - /* make sure this pixel is defined exactly once */ - if (input->source == GPU_SOURCE_TEX_PIXEL) { - if (input->ima) { - if (!BLI_ghash_haskey(definehash, input->ima)) { - input->definetex = true; - BLI_ghash_insert(definehash, input->ima, SET_INT_IN_POINTER(input->texid)); - } - } - else { - if (!BLI_ghash_haskey(definehash, input->link)) { - input->definetex = true; - BLI_ghash_insert(definehash, input->link, SET_INT_IN_POINTER(input->texid)); - } - } + else { + /* Either input->ima or input->coba should be non-NULL. */ + BLI_assert(0); } } } - for (output = node->outputs.first; output; output = output->next) + for (output = node->outputs.first; output; output = output->next) { /* set id for unique names of tmp variables storing output */ output->id = id++; + } } BLI_ghash_free(bindhash, NULL, NULL); - BLI_ghash_free(definehash, NULL, NULL); } /** @@ -615,14 +583,12 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, /* print uniforms */ for (node = nodes->first; node; node = node->next) { for (input = node->inputs.first; input; input = input->next) { - if ((input->source == GPU_SOURCE_TEX) || (input->source == GPU_SOURCE_TEX_PIXEL)) { + if (input->source == GPU_SOURCE_TEX) { /* create exactly one sampler for each texture */ if (codegen_input_has_texture(input) && input->bindtex) { BLI_dynstr_appendf( ds, "uniform %s samp%d;\n", - (input->textype == GPU_TEX1D_ARRAY) ? "sampler1DArray" : - (input->textype == GPU_TEX2D) ? "sampler2D" : - (input->textype == GPU_TEXCUBE) ? "samplerCube" : "sampler2DShadow", + (input->coba) ? "sampler1DArray" : "sampler2D", input->texid); } } @@ -656,27 +622,19 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, /* 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) { - /* We handle the UBOuniforms separately. */ - BLI_addtail(&ubo_inputs, BLI_genericNodeN(input)); - } - } - else if (input->dynamicvec) { - /* only create uniforms for dynamic vectors */ - BLI_dynstr_appendf( - ds, "uniform %s unf%d;\n", - GPU_DATATYPE_STR[input->type], input->id); - } - else { - 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"); + else if (input->source == GPU_SOURCE_UNIFORM) { + if (!input->link) { + /* We handle the UBOuniforms separately. */ + BLI_addtail(&ubo_inputs, BLI_genericNodeN(input)); } } + else if (input->source == GPU_SOURCE_CONSTANT) { + 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"); + } else if (input->source == GPU_SOURCE_ATTRIB && input->attribfirst) { #ifdef WITH_OPENSUBDIV bool skip_opensubdiv = input->attribtype == CD_TANGENT; @@ -722,23 +680,9 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, static void codegen_declare_tmps(DynStr *ds, ListBase *nodes) { GPUNode *node; - GPUInput *input; GPUOutput *output; for (node = nodes->first; node; node = node->next) { - /* load pixels from textures */ - for (input = node->inputs.first; input; input = input->next) { - if (input->source == GPU_SOURCE_TEX_PIXEL) { - if (codegen_input_has_texture(input) && input->definetex) { - BLI_dynstr_appendf( - ds, "\tvec4 tex%d = texture2D(", input->texid); - BLI_dynstr_appendf( - ds, "samp%d, gl_TexCoord[%d].st);\n", - input->texid, input->texid); - } - } - } - /* declare temporary variables for node output storage */ for (output = node->outputs.first; output; output = output->next) { if (output->type == GPU_CLOSURE) { @@ -768,15 +712,14 @@ static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *final for (input = node->inputs.first; input; input = input->next) { if (input->source == GPU_SOURCE_TEX) { BLI_dynstr_appendf(ds, "samp%d", input->texid); - if (input->link) - BLI_dynstr_appendf(ds, ", gl_TexCoord[%d].st", input->texid); } - else if (input->source == GPU_SOURCE_TEX_PIXEL) { + else if (input->source == GPU_SOURCE_OUTPUT) { codegen_convert_datatype( ds, input->link->output->type, input->type, "tmp", input->link->output->id); } else if (input->source == GPU_SOURCE_BUILTIN) { + /* TODO(fclem) get rid of that. */ if (input->builtin == GPU_INVERSE_VIEW_MATRIX) BLI_dynstr_append(ds, "viewinv"); else if (input->builtin == GPU_VIEW_MATRIX) @@ -797,21 +740,15 @@ static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *final 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); - else - BLI_dynstr_appendf(ds, "cons%d", input->id); + else if (input->source == GPU_SOURCE_UNIFORM) { + BLI_dynstr_appendf(ds, "unf%d", input->id); + } + else if (input->source == GPU_SOURCE_CONSTANT) { + BLI_dynstr_appendf(ds, "cons%d", input->id); } else if (input->source == GPU_SOURCE_ATTRIB) { BLI_dynstr_appendf(ds, "var%d", input->attribid); } - else if (input->source == GPU_SOURCE_OPENGL_BUILTIN) { - if (input->oglbuiltin == GPU_MATCAP_NORMAL) - BLI_dynstr_append(ds, "gl_SecondaryColor"); - else if (input->oglbuiltin == GPU_COLOR) - BLI_dynstr_append(ds, "gl_Color"); - } BLI_dynstr_append(ds, ", "); } @@ -855,6 +792,7 @@ static char *code_generate_fragment(GPUMaterial *material, ListBase *nodes, GPUO BLI_dynstr_append(ds, "Closure nodetree_exec(void)\n{\n"); + /* TODO(fclem) get rid of that. */ if (builtins & GPU_VIEW_MATRIX) BLI_dynstr_append(ds, "\tmat4 viewmat = ViewMatrix;\n"); if (builtins & GPU_CAMERA_TEXCO_FACTORS) @@ -1174,7 +1112,6 @@ void GPU_nodes_extract_dynamic_inputs(GPUShader *shader, ListBase *inputs, ListB { GPUNode *node; GPUInput *next, *input; - int extract, z; BLI_listbase_clear(inputs); @@ -1184,48 +1121,30 @@ void GPU_nodes_extract_dynamic_inputs(GPUShader *shader, ListBase *inputs, ListB GPU_shader_bind(shader); for (node = nodes->first; node; node = node->next) { - z = 0; + int z = 0; for (input = node->inputs.first; input; input = next, z++) { next = input->next; /* attributes don't need to be bound, they already have - * an id that the drawing functions will use */ - if (input->source == GPU_SOURCE_ATTRIB) { - continue; - } - - if (input->source == GPU_SOURCE_BUILTIN || - input->source == GPU_SOURCE_OPENGL_BUILTIN) - { + * an id that the drawing functions will use. Builtins have + * constant names. */ + if (ELEM(input->source, GPU_SOURCE_ATTRIB, GPU_SOURCE_BUILTIN)) { continue; } - if (input->ima || input->tex || input->prv) + if (input->source == GPU_SOURCE_TEX) BLI_snprintf(input->shadername, sizeof(input->shadername), "samp%d", input->texid); - else + else { BLI_snprintf(input->shadername, sizeof(input->shadername), "unf%d", input->id); - - /* pass non-dynamic uniforms to opengl */ - extract = 0; - - if (input->ima || input->tex || input->prv) { - if (input->bindtex) - extract = 1; - } - else if (input->dynamictype == GPU_DYNAMIC_UBO) { - /* Don't extract UBOs */ - } - else if (input->dynamicvec) { - extract = 1; } - if (extract) - input->shaderloc = GPU_shader_get_uniform(shader, input->shadername); - - /* extract nodes */ - if (extract) { - BLI_remlink(&node->inputs, input); - BLI_addtail(inputs, input); + if (input->source == GPU_SOURCE_TEX) { + if (input->bindtex) { + input->shaderloc = GPU_shader_get_uniform(shader, input->shadername); + /* extract nodes */ + BLI_remlink(&node->inputs, input); + BLI_addtail(inputs, input); + } } } } @@ -1238,7 +1157,6 @@ void GPU_nodes_extract_dynamic_inputs(GPUShader *shader, ListBase *inputs, ListB static GPUNodeLink *GPU_node_link_create(void) { GPUNodeLink *link = MEM_callocN(sizeof(GPUNodeLink), "GPUNodeLink"); - link->type = GPU_NONE; link->users++; return link; @@ -1275,7 +1193,7 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType GPUNode *outnode; const char *name; - if (link->output) { + if (link->link_type == GPU_NODE_LINK_OUTPUT) { outnode = link->output->node; name = outnode->name; input = outnode->inputs.first; @@ -1284,7 +1202,6 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType (input->type == type)) { input = MEM_dupallocN(outnode->inputs.first); - input->type = type; if (input->link) input->link->users++; BLI_addtail(&node->inputs, input); @@ -1294,107 +1211,50 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType input = MEM_callocN(sizeof(GPUInput), "GPUInput"); input->node = node; + input->type = type; - if (link->builtin) { - /* builtin uniform */ - input->type = type; - input->source = GPU_SOURCE_BUILTIN; - input->builtin = link->builtin; - - MEM_freeN(link); - } - else if (link->oglbuiltin) { - /* builtin uniform */ - input->type = type; - input->source = GPU_SOURCE_OPENGL_BUILTIN; - input->oglbuiltin = link->oglbuiltin; - - MEM_freeN(link); - } - else if (link->output) { - /* link to a node output */ - input->type = type; - input->source = GPU_SOURCE_TEX_PIXEL; - input->link = link; - link->users++; - } - else if (link->dynamictex) { - /* dynamic texture, GPUTexture is updated/deleted externally */ - input->type = type; - input->source = GPU_SOURCE_TEX; - - input->tex = link->dynamictex; - input->textarget = GL_TEXTURE_2D; - input->textype = type; - input->dynamictex = true; - input->dynamicdata = link->ptr2; - MEM_freeN(link); - } - else if (link->texture) { - /* small texture created on the fly, like for colorbands */ - input->type = GPU_VEC4; - input->source = GPU_SOURCE_TEX; - input->textype = GPU_TEX1D_ARRAY; - input->tex = link->ptr1; /* HACK ptr1 is actually a (GPUTexture **). */ - input->textarget = GL_TEXTURE_1D_ARRAY; - MEM_freeN(link); - } - else if (link->image) { - /* blender image */ - input->type = GPU_VEC4; - input->source = GPU_SOURCE_TEX; - - if (link->image == GPU_NODE_LINK_IMAGE_PREVIEW) { - input->prv = link->ptr1; - input->textarget = GL_TEXTURE_2D; - input->textype = GPU_TEX2D; - } - else if (link->image == GPU_NODE_LINK_IMAGE_BLENDER) { - input->ima = link->ptr1; - input->iuser = link->ptr2; - input->image_isdata = link->image_isdata; - input->textarget = GL_TEXTURE_2D; - input->textype = GPU_TEX2D; - } - else if (link->image == GPU_NODE_LINK_IMAGE_CUBE_MAP) { - input->ima = link->ptr1; - input->iuser = link->ptr2; + switch (link->link_type) { + case GPU_NODE_LINK_BUILTIN: + input->source = GPU_SOURCE_BUILTIN; + input->builtin = link->builtin; + break; + case GPU_NODE_LINK_OUTPUT: + input->source = GPU_SOURCE_OUTPUT; + input->link = link; + link->users++; + break; + case GPU_NODE_LINK_COLORBAND: + input->source = GPU_SOURCE_TEX; + input->coba = link->coba; + break; + case GPU_NODE_LINK_IMAGE_BLENDER: + input->source = GPU_SOURCE_TEX; + input->ima = link->ima; + input->iuser = link->iuser; input->image_isdata = link->image_isdata; - input->textarget = GL_TEXTURE_CUBE_MAP; - input->textype = GPU_TEXCUBE; - } - MEM_freeN(link); - } - else if (link->attribtype) { - /* vertex attribute */ - input->type = type; - input->source = GPU_SOURCE_ATTRIB; - - input->attribtype = link->attribtype; - BLI_strncpy(input->attribname, link->attribname, sizeof(input->attribname)); - MEM_freeN(link); + break; + case GPU_NODE_LINK_ATTRIB: + input->source = GPU_SOURCE_ATTRIB; + input->attribtype = link->attribtype; + BLI_strncpy(input->attribname, link->attribname, sizeof(input->attribname)); + break; + 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; + default: + break; } - else if (type == GPU_CLOSURE) { - input->type = type; - input->source = GPU_SOURCE_STRUCT; - MEM_freeN(link); + if (ELEM(input->source, GPU_SOURCE_CONSTANT, GPU_SOURCE_UNIFORM)) { + memcpy(input->vec, link->data, type * sizeof(float)); } - else { - /* uniform vector */ - input->type = type; - input->source = GPU_SOURCE_VEC_UNIFORM; - - memcpy(input->vec, link->ptr1, type * sizeof(float)); - if (link->dynamic) { - input->dynamicvec = link->ptr1; - input->dynamictype = link->dynamictype; - input->dynamicdata = link->ptr2; - } + if (link->link_type != GPU_NODE_LINK_OUTPUT) { MEM_freeN(link); } - BLI_addtail(&node->inputs, input); } @@ -1484,9 +1344,7 @@ static void gpu_node_input_socket(GPUMaterial *material, bNode *bnode, GPUNode * gpu_node_input_link(node, sock->link, sock->type); } else { - GPUNodeLink *link = GPU_node_link_create(); - link->ptr1 = sock->vec; - gpu_node_input_link(node, link, sock->type); + gpu_node_input_link(node, GPU_constant(sock->vec), sock->type); } } @@ -1499,7 +1357,7 @@ static void gpu_node_output(GPUNode *node, const GPUType type, GPUNodeLink **lin if (link) { *link = output->link = GPU_node_link_create(); - output->link->type = type; + output->link->link_type = GPU_NODE_LINK_OUTPUT; output->link->output = output; /* note: the caller owns the reference to the link, GPUOutput @@ -1517,8 +1375,6 @@ void GPU_inputs_free(ListBase *inputs) for (input = inputs->first; input; input = input->next) { if (input->link) gpu_node_link_free(input->link); - else if (input->tex && !input->dynamictex) - GPU_texture_free(input->tex); } BLI_freelistN(inputs); @@ -1576,7 +1432,7 @@ void GPU_nodes_get_vertex_attributes(ListBase *nodes, GPUVertexAttribs *attribs) if (a < GPU_MAX_ATTRIB) { if (a == attribs->totlayer) { input->attribid = attribs->totlayer++; - input->attribfirst = 1; + input->attribfirst = true; attribs->layer[a].type = input->attribtype; attribs->layer[a].attribid = input->attribid; @@ -1598,7 +1454,8 @@ void GPU_nodes_get_vertex_attributes(ListBase *nodes, GPUVertexAttribs *attribs) GPUNodeLink *GPU_attribute(const CustomDataType type, const char *name) { GPUNodeLink *link = GPU_node_link_create(); - + link->link_type = GPU_NODE_LINK_ATTRIB; + link->attribname = name; /* Fall back to the UV layer, which matches old behavior. */ if (type == CD_AUTO_FROM_NAME && name[0] == '\0') { link->attribtype = CD_MTFACE; @@ -1606,63 +1463,49 @@ GPUNodeLink *GPU_attribute(const CustomDataType type, const char *name) else { link->attribtype = type; } - - link->attribname = name; - return link; } GPUNodeLink *GPU_constant(float *num) { GPUNodeLink *link = GPU_node_link_create(); - - link->ptr1 = num; - link->ptr2 = NULL; - + link->link_type = GPU_NODE_LINK_CONSTANT; + link->data = num; return link; } GPUNodeLink *GPU_uniform(float *num) { GPUNodeLink *link = GPU_node_link_create(); - link->ptr1 = num; - link->ptr2 = NULL; - link->dynamic = true; - link->dynamictype = GPU_DYNAMIC_UBO; - + link->link_type = GPU_NODE_LINK_UNIFORM; + link->data = num; return link; } GPUNodeLink *GPU_image(Image *ima, ImageUser *iuser, bool is_data) { GPUNodeLink *link = GPU_node_link_create(); - - link->image = GPU_NODE_LINK_IMAGE_BLENDER; - link->ptr1 = ima; - link->ptr2 = iuser; + link->link_type = GPU_NODE_LINK_IMAGE_BLENDER; + link->ima = ima; + link->iuser = iuser; link->image_isdata = is_data; - return link; } -GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *layer) +GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row) { GPUNodeLink *link = GPU_node_link_create(); - - link->texture = true; - link->ptr1 = gpu_material_ramp_texture_row_set(mat, size, pixels, layer); - + link->link_type = GPU_NODE_LINK_COLORBAND; + link->coba = gpu_material_ramp_texture_row_set(mat, size, pixels, row); MEM_freeN(pixels); - return link; } GPUNodeLink *GPU_builtin(GPUBuiltin builtin) { GPUNodeLink *link = GPU_node_link_create(); - + link->link_type = GPU_NODE_LINK_BUILTIN; link->builtin = builtin; - return link; } diff --git a/source/blender/gpu/intern/gpu_codegen.h b/source/blender/gpu/intern/gpu_codegen.h index 21dcaf6b591..7a86bed55de 100644 --- a/source/blender/gpu/intern/gpu_codegen.h +++ b/source/blender/gpu/intern/gpu_codegen.h @@ -52,21 +52,25 @@ struct PreviewImage; */ typedef enum GPUDataSource { - GPU_SOURCE_VEC_UNIFORM, + GPU_SOURCE_OUTPUT, + GPU_SOURCE_CONSTANT, + GPU_SOURCE_UNIFORM, + GPU_SOURCE_ATTRIB, GPU_SOURCE_BUILTIN, - GPU_SOURCE_OPENGL_BUILTIN, - GPU_SOURCE_TEX_PIXEL, + GPU_SOURCE_STRUCT, GPU_SOURCE_TEX, - GPU_SOURCE_ATTRIB, - GPU_SOURCE_STRUCT } GPUDataSource; typedef enum { - GPU_NODE_LINK_IMAGE_NONE = 0, - GPU_NODE_LINK_IMAGE_BLENDER = 1, - GPU_NODE_LINK_IMAGE_PREVIEW = 2, - GPU_NODE_LINK_IMAGE_CUBE_MAP = 3 -} GPUNodeLinkImage; + GPU_NODE_LINK_NONE = 0, + GPU_NODE_LINK_ATTRIB, + GPU_NODE_LINK_BUILTIN, + GPU_NODE_LINK_COLORBAND, + GPU_NODE_LINK_CONSTANT, + GPU_NODE_LINK_IMAGE_BLENDER, + GPU_NODE_LINK_OUTPUT, + GPU_NODE_LINK_UNIFORM, +} GPUNodeLinkType; struct GPUNode { struct GPUNode *next, *prev; @@ -83,31 +87,30 @@ struct GPUNode { struct GPUNodeLink { GPUNodeStack *socket; - CustomDataType attribtype; - const char *attribname; - - GPUNodeLinkImage image; - bool image_isdata; - - bool texture; - int texturesize; - - void *ptr1, *ptr2; - - bool dynamic; - GPUDynamicType dynamictype; - - GPUType type; - - /* Refcount */ - int users; - - struct GPUTexture *dynamictex; - - GPUBuiltin builtin; - GPUOpenGLBuiltin oglbuiltin; - - struct GPUOutput *output; + GPUNodeLinkType link_type; + int users; /* Refcount */ + + union { + /* GPU_NODE_LINK_CONSTANT | GPU_NODE_LINK_UNIFORM */ + float *data; + /* GPU_NODE_LINK_BUILTIN */ + GPUBuiltin builtin; + /* GPU_NODE_LINK_COLORBAND */ + struct GPUTexture **coba; + /* GPU_NODE_LINK_OUTPUT */ + struct GPUOutput *output; + /* GPU_NODE_LINK_ATTRIB */ + struct { + const char *attribname; + CustomDataType attribtype; + }; + /* GPU_NODE_LINK_IMAGE_BLENDER */ + struct { + struct Image *ima; + struct ImageUser *iuser; + bool image_isdata; + }; + }; }; typedef struct GPUOutput { @@ -123,37 +126,39 @@ typedef struct GPUInput { struct GPUInput *next, *prev; GPUNode *node; - GPUType type; /* datatype */ + GPUNodeLink *link; + int id; /* unique id as created by code generator */ + GPUDataSource source; /* data source */ - int id; /* unique id as created by code generator */ - int texid; /* number for multitexture, starting from zero */ - int attribid; /* id for vertex attributes */ - bool bindtex; /* input is responsible for binding the texture? */ - bool definetex; /* input is responsible for defining the pixel? */ - int textarget; /* GL texture target, e.g. GL_TEXTURE_2D */ - GPUType textype; /* datatype */ - - struct Image *ima; /* image */ - struct ImageUser *iuser; /* image user */ - struct PreviewImage *prv; /* preview images & icons */ - bool image_isdata; /* image does not contain color data */ - float *dynamicvec; /* vector data in case it is dynamic */ - GPUDynamicType dynamictype; /* origin of the dynamic uniform */ - void *dynamicdata; /* data source of the dynamic uniform */ - struct GPUTexture *tex; /* input texture, only set at runtime */ int shaderloc; /* id from opengl */ char shadername[32]; /* name in shader */ - float vec[16]; /* vector data */ - GPUNodeLink *link; - bool dynamictex; /* dynamic? */ - CustomDataType attribtype; /* attribute type */ - char attribname[MAX_CUSTOMDATA_LAYER_NAME]; /* attribute name */ - int attribfirst; /* this is the first one that is bound */ - GPUBuiltin builtin; /* builtin uniform */ - GPUOpenGLBuiltin oglbuiltin; /* opengl built in varying */ + /* Content based on GPUDataSource */ + union { + /* GPU_SOURCE_CONSTANT | GPU_SOURCE_UNIFORM */ + float vec[16]; /* vector data */ + /* GPU_SOURCE_BUILTIN */ + GPUBuiltin builtin; /* builtin uniform */ + /* GPU_SOURCE_TEX */ + struct { + struct GPUTexture **coba; /* input texture, only set at runtime */ + struct Image *ima; /* image */ + struct ImageUser *iuser; /* image user */ + bool image_isdata; /* image does not contain color data */ + bool bindtex; /* input is responsible for binding the texture? */ + int texid; /* number for multitexture, starting from zero */ + GPUType textype; /* texture type (2D, 1D Array ...) */ + }; + /* GPU_SOURCE_ATTRIB */ + struct { + char attribname[MAX_CUSTOMDATA_LAYER_NAME]; /* attribute name */ + int attribid; /* id for vertex attributes */ + bool attribfirst; /* this is the first one that is bound */ + CustomDataType attribtype; /* attribute type */ + }; + }; } GPUInput; struct GPUPass { diff --git a/source/blender/gpu/intern/gpu_uniformbuffer.c b/source/blender/gpu/intern/gpu_uniformbuffer.c index 3f5706c1f7b..82b82d622a4 100644 --- a/source/blender/gpu/intern/gpu_uniformbuffer.c +++ b/source/blender/gpu/intern/gpu_uniformbuffer.c @@ -159,7 +159,7 @@ GPUUniformBuffer *GPU_uniformbuffer_dynamic_create(ListBase *inputs, char err_ou float *offset = ubo->data; for (LinkData *link = inputs->first; link; link = link->next) { GPUInput *input = link->data; - memcpy(offset, input->dynamicvec, input->type * sizeof(float)); + memcpy(offset, input->vec, input->type * sizeof(float)); offset += get_padded_gpu_type(link); } |