diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2020-02-12 14:48:44 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2020-02-15 22:33:16 +0300 |
commit | 007f1b74a67302fb4c164eb26969419434a98aee (patch) | |
tree | f56ba0eecbbc11fc22e6a47aa0d76582e553e3da /source/blender/gpu/intern/gpu_codegen.c | |
parent | 6701db773e660e5306e305a1b10f8ea52955f06b (diff) |
Cleanup: split off code from gpu_codegen.c into smaller files
Diffstat (limited to 'source/blender/gpu/intern/gpu_codegen.c')
-rw-r--r-- | source/blender/gpu/intern/gpu_codegen.c | 940 |
1 files changed, 49 insertions, 891 deletions
diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c index 4787015e3d6..2f9af536b8c 100644 --- a/source/blender/gpu/intern/gpu_codegen.c +++ b/source/blender/gpu/intern/gpu_codegen.c @@ -27,8 +27,6 @@ #include "DNA_customdata_types.h" #include "DNA_image_types.h" -#include "DNA_material_types.h" -#include "DNA_node_types.h" #include "BLI_blenlib.h" #include "BLI_hash_mm2a.h" @@ -38,10 +36,10 @@ #include "BLI_ghash.h" #include "BLI_threads.h" -#include "BKE_material.h" - #include "PIL_time.h" +#include "BKE_material.h" + #include "GPU_extensions.h" #include "GPU_glew.h" #include "GPU_material.h" @@ -53,13 +51,11 @@ #include "gpu_codegen.h" #include "gpu_material_library.h" +#include "gpu_node_graph.h" #include <string.h> #include <stdarg.h> -extern char datatoc_gpu_shader_material_glsl[]; -extern char datatoc_gpu_shader_geometry_glsl[]; - extern char datatoc_gpu_shader_common_obinfos_lib_glsl[]; extern char datatoc_common_view_lib_glsl[]; @@ -132,281 +128,6 @@ static GPUPass *gpu_pass_cache_resolve_collision(GPUPass *pass, return NULL; } -/* -------------------- GPU Codegen ------------------ */ - -/* type definitions and constants */ - -#define MAX_FUNCTION_NAME 64 -#define MAX_PARAMETER 32 - -typedef enum { - FUNCTION_QUAL_IN, - FUNCTION_QUAL_OUT, - FUNCTION_QUAL_INOUT, -} GPUFunctionQual; - -typedef struct GPUFunction { - char name[MAX_FUNCTION_NAME]; - eGPUType paramtype[MAX_PARAMETER]; - GPUFunctionQual paramqual[MAX_PARAMETER]; - int totparam; - GPUMaterialLibrary *library; -} GPUFunction; - -/* Indices match the eGPUType enum */ -static const char *GPU_DATATYPE_STR[17] = { - "", - "float", - "vec2", - "vec3", - "vec4", - NULL, - NULL, - NULL, - NULL, - "mat3", - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - "mat4", -}; - -/* GLSL code parsing for finding function definitions. - * These are stored in a hash for lookup when creating a material. */ - -static GHash *FUNCTION_HASH = NULL; -#if 0 -static char *FUNCTION_PROTOTYPES = NULL; -static GPUShader *FUNCTION_LIB = NULL; -#endif - -static int gpu_str_prefix(const char *str, const char *prefix) -{ - while (*str && *prefix) { - if (*str != *prefix) { - return 0; - } - - str++; - prefix++; - } - - return (*prefix == '\0'); -} - -static char *gpu_str_skip_token(char *str, char *token, int max) -{ - int len = 0; - - /* skip a variable/function name */ - while (*str) { - if (ELEM(*str, ' ', '(', ')', ',', ';', '\t', '\n', '\r')) { - break; - } - else { - if (token && len < max - 1) { - *token = *str; - token++; - len++; - } - str++; - } - } - - if (token) { - *token = '\0'; - } - - /* skip the next special characters: - * note the missing ')' */ - while (*str) { - if (ELEM(*str, ' ', '(', ',', ';', '\t', '\n', '\r')) { - str++; - } - else { - break; - } - } - - return str; -} - -static void gpu_parse_material_library(GHash *hash, GPUMaterialLibrary *library) -{ - GPUFunction *function; - eGPUType type; - GPUFunctionQual qual; - int i; - char *code = library->code; - - while ((code = strstr(code, "void "))) { - function = MEM_callocN(sizeof(GPUFunction), "GPUFunction"); - function->library = library; - - code = gpu_str_skip_token(code, NULL, 0); - code = gpu_str_skip_token(code, function->name, MAX_FUNCTION_NAME); - - /* get parameters */ - while (*code && *code != ')') { - /* test if it's an input or output */ - qual = FUNCTION_QUAL_IN; - if (gpu_str_prefix(code, "out ")) { - qual = FUNCTION_QUAL_OUT; - } - if (gpu_str_prefix(code, "inout ")) { - qual = FUNCTION_QUAL_INOUT; - } - if ((qual != FUNCTION_QUAL_IN) || gpu_str_prefix(code, "in ")) { - code = gpu_str_skip_token(code, NULL, 0); - } - - /* test for type */ - type = GPU_NONE; - for (i = 1; i < ARRAY_SIZE(GPU_DATATYPE_STR); i++) { - if (GPU_DATATYPE_STR[i] && gpu_str_prefix(code, GPU_DATATYPE_STR[i])) { - type = i; - break; - } - } - - if (!type && gpu_str_prefix(code, "samplerCube")) { - type = GPU_TEXCUBE; - } - if (!type && gpu_str_prefix(code, "sampler2DShadow")) { - type = GPU_SHADOW2D; - } - if (!type && gpu_str_prefix(code, "sampler1DArray")) { - type = GPU_TEX1D_ARRAY; - } - if (!type && gpu_str_prefix(code, "sampler2DArray")) { - type = GPU_TEX2D_ARRAY; - } - if (!type && gpu_str_prefix(code, "sampler2D")) { - type = GPU_TEX2D; - } - if (!type && gpu_str_prefix(code, "sampler3D")) { - type = GPU_TEX3D; - } - - if (!type && gpu_str_prefix(code, "Closure")) { - type = GPU_CLOSURE; - } - - if (type) { - /* add parameter */ - code = gpu_str_skip_token(code, NULL, 0); - code = gpu_str_skip_token(code, NULL, 0); - function->paramqual[function->totparam] = qual; - function->paramtype[function->totparam] = type; - function->totparam++; - } - else { - fprintf(stderr, "GPU invalid function parameter in %s.\n", function->name); - break; - } - } - - if (function->name[0] == '\0' || function->totparam == 0) { - fprintf(stderr, "GPU functions parse error.\n"); - MEM_freeN(function); - break; - } - - BLI_ghash_insert(hash, function->name, function); - } -} - -#if 0 -static char *gpu_generate_function_prototyps(GHash *hash) -{ - DynStr *ds = BLI_dynstr_new(); - GHashIterator *ghi; - GPUFunction *function; - char *name, *prototypes; - int a; - - /* automatically generate function prototypes to add to the top of the - * generated code, to avoid have to add the actual code & recompile all */ - ghi = BLI_ghashIterator_new(hash); - - for (; !BLI_ghashIterator_done(ghi); BLI_ghashIterator_step(ghi)) { - name = BLI_ghashIterator_getValue(ghi); - function = BLI_ghashIterator_getValue(ghi); - - BLI_dynstr_appendf(ds, "void %s(", name); - for (a = 0; a < function->totparam; a++) { - if (function->paramqual[a] == FUNCTION_QUAL_OUT) { - BLI_dynstr_append(ds, "out "); - } - else if (function->paramqual[a] == FUNCTION_QUAL_INOUT) { - BLI_dynstr_append(ds, "inout "); - } - - if (function->paramtype[a] == GPU_TEX2D) { - BLI_dynstr_append(ds, "sampler2D"); - } - else if (function->paramtype[a] == GPU_SHADOW2D) { - BLI_dynstr_append(ds, "sampler2DShadow"); - } - else { - BLI_dynstr_append(ds, GPU_DATATYPE_STR[function->paramtype[a]]); - } -# if 0 - BLI_dynstr_appendf(ds, " param%d", a); -# endif - - if (a != function->totparam - 1) { - BLI_dynstr_append(ds, ", "); - } - } - BLI_dynstr_append(ds, ");\n"); - } - - BLI_dynstr_append(ds, "\n"); - - prototypes = BLI_dynstr_get_cstring(ds); - BLI_dynstr_free(ds); - - return prototypes; -} -#endif - -static GPUFunction *gpu_lookup_function(const char *name) -{ - return BLI_ghash_lookup(FUNCTION_HASH, (const void *)name); -} - -void gpu_codegen_init(void) -{ - GPU_code_generate_glsl_lib(); -} - -void gpu_codegen_exit(void) -{ - BKE_material_defaults_free_gpu(); - - if (FUNCTION_HASH) { - BLI_ghash_free(FUNCTION_HASH, NULL, MEM_freeN); - FUNCTION_HASH = NULL; - } - - GPU_shader_free_builtin_shaders(); - -#if 0 - if (FUNCTION_PROTOTYPES) { - MEM_freeN(FUNCTION_PROTOTYPES); - FUNCTION_PROTOTYPES = NULL; - } - if (FUNCTION_LIB) { - GPU_shader_free(FUNCTION_LIB); - FUNCTION_LIB = NULL; - } -#endif -} - /* GLSL code generation */ static void codegen_convert_datatype(DynStr *ds, int from, int to, const char *tmp, int id) @@ -485,7 +206,7 @@ static void codegen_print_datatype(DynStr *ds, const eGPUType type, float *data) { int i; - BLI_dynstr_appendf(ds, "%s(", GPU_DATATYPE_STR[type]); + BLI_dynstr_appendf(ds, "%s(", gpu_data_type_to_string(type)); for (i = 0; i < type; i++) { BLI_dynstr_appendf(ds, "%.12f", data[i]); @@ -508,7 +229,7 @@ static int codegen_input_has_texture(GPUInput *input) } } -const char *GPU_builtin_name(eGPUBuiltin builtin) +static const char *gpu_builtin_name(eGPUBuiltin builtin) { if (builtin == GPU_VIEW_MATRIX) { return "unfviewmat"; @@ -676,18 +397,18 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, /* only define each builtin uniform/varying once */ if (!(builtins & input->builtin)) { builtins |= input->builtin; - name = GPU_builtin_name(input->builtin); + name = gpu_builtin_name(input->builtin); - if (gpu_str_prefix(name, "samp")) { + if (BLI_str_startswith(name, "samp")) { if ((input->builtin == GPU_VOLUME_DENSITY) || (input->builtin == GPU_VOLUME_FLAME)) { BLI_dynstr_appendf(ds, "uniform sampler3D %s;\n", name); } } - else if (gpu_str_prefix(name, "unf")) { - BLI_dynstr_appendf(ds, "uniform %s %s;\n", GPU_DATATYPE_STR[input->type], name); + else if (BLI_str_startswith(name, "unf")) { + BLI_dynstr_appendf(ds, "uniform %s %s;\n", gpu_data_type_to_string(input->type), name); } else { - BLI_dynstr_appendf(ds, "in %s %s;\n", GPU_DATATYPE_STR[input->type], name); + BLI_dynstr_appendf(ds, "in %s %s;\n", gpu_data_type_to_string(input->type), name); } } } @@ -702,12 +423,14 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, } } else if (input->source == GPU_SOURCE_CONSTANT) { - BLI_dynstr_appendf(ds, "const %s cons%d = ", GPU_DATATYPE_STR[input->type], input->id); + BLI_dynstr_appendf( + ds, "const %s cons%d = ", gpu_data_type_to_string(input->type), input->id); codegen_print_datatype(ds, input->type, input->vec); BLI_dynstr_append(ds, ";\n"); } else if (input->source == GPU_SOURCE_ATTR && input->attr_first) { - BLI_dynstr_appendf(ds, "in %s var%d;\n", GPU_DATATYPE_STR[input->type], input->attr_id); + BLI_dynstr_appendf( + ds, "in %s var%d;\n", gpu_data_type_to_string(input->type), input->attr_id); } } } @@ -721,7 +444,7 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, for (LinkData *link = ubo_inputs.first; link; link = link->next) { input = link->data; - BLI_dynstr_appendf(ds, "\t%s unf%d;\n", GPU_DATATYPE_STR[input->type], input->id); + BLI_dynstr_appendf(ds, "\t%s unf%d;\n", gpu_data_type_to_string(input->type), input->id); } BLI_dynstr_append(ds, "};\n"); BLI_freelistN(&ubo_inputs); @@ -744,7 +467,7 @@ static void codegen_declare_tmps(DynStr *ds, ListBase *nodes) 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); + BLI_dynstr_appendf(ds, "\t%s tmp%d;\n", gpu_data_type_to_string(output->type), output->id); } } } @@ -814,7 +537,7 @@ static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *final BLI_dynstr_append(ds, "facingwnormal"); } else { - BLI_dynstr_append(ds, GPU_builtin_name(input->builtin)); + BLI_dynstr_append(ds, gpu_builtin_name(input->builtin)); } } else if (input->source == GPU_SOURCE_STRUCT) { @@ -1021,7 +744,7 @@ static char *code_generate_vertex(ListBase *nodes, const char *vert_code, bool u else if (input->attr_name[0] == '\0') { BLI_dynstr_appendf(ds, "DEFINE_ATTR(%s, %s);\n", - GPU_DATATYPE_STR[input->type], + gpu_data_type_to_string(input->type), attr_prefix_get(input->attr_type)); BLI_dynstr_appendf( ds, "#define att%d %s\n", input->attr_id, attr_prefix_get(input->attr_type)); @@ -1032,7 +755,7 @@ static char *code_generate_vertex(ListBase *nodes, const char *vert_code, bool u input->attr_name, attr_safe_name, GPU_MAX_SAFE_ATTRIB_NAME); BLI_dynstr_appendf(ds, "DEFINE_ATTR(%s, %s%s);\n", - GPU_DATATYPE_STR[input->type], + gpu_data_type_to_string(input->type), attr_prefix_get(input->attr_type), attr_safe_name); BLI_dynstr_appendf(ds, @@ -1043,7 +766,7 @@ static char *code_generate_vertex(ListBase *nodes, const char *vert_code, bool u } BLI_dynstr_appendf(ds, "out %s var%d%s;\n", - GPU_DATATYPE_STR[input->type], + gpu_data_type_to_string(input->type), input->attr_id, use_geom ? "g" : ""); } @@ -1143,7 +866,7 @@ static char *code_generate_vertex(ListBase *nodes, const char *vert_code, bool u "\tvar%d%s = hair_get_customdata_%s(att%d);\n", input->attr_id, use_geom ? "g" : "", - GPU_DATATYPE_STR[input->type], + gpu_data_type_to_string(input->type), input->attr_id); } } @@ -1245,8 +968,10 @@ static char *code_generate_geometry(ListBase *nodes, const char *geom_code, cons builtins |= input->builtin; } if (input->source == GPU_SOURCE_ATTR && input->attr_first) { - BLI_dynstr_appendf(ds, "in %s var%dg[];\n", GPU_DATATYPE_STR[input->type], input->attr_id); - BLI_dynstr_appendf(ds, "out %s var%d;\n", GPU_DATATYPE_STR[input->type], input->attr_id); + BLI_dynstr_appendf( + ds, "in %s var%dg[];\n", gpu_data_type_to_string(input->type), input->attr_id); + BLI_dynstr_appendf( + ds, "out %s var%d;\n", gpu_data_type_to_string(input->type), input->attr_id); } } } @@ -1369,320 +1094,12 @@ static char *code_generate_geometry(ListBase *nodes, const char *geom_code, cons return code; } -void GPU_code_generate_glsl_lib(void) -{ - /* Only parse GLSL shader files once. */ - if (FUNCTION_HASH) { - return; - } - - FUNCTION_HASH = BLI_ghash_str_new("GPU_lookup_function gh"); - for (int i = 0; gpu_material_libraries[i]; i++) { - gpu_parse_material_library(FUNCTION_HASH, gpu_material_libraries[i]); - } -} - -/* GPU pass binding/unbinding */ - GPUShader *GPU_pass_shader_get(GPUPass *pass) { return pass->shader; } -void GPU_nodes_extract_dynamic_inputs(GPUShader *shader, ListBase *inputs, ListBase *nodes) -{ - GPUNode *node; - GPUInput *next, *input; - - BLI_listbase_clear(inputs); - - if (!shader) { - return; - } - - for (node = nodes->first; node; node = node->next) { - 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. Builtins have - * constant names. */ - if (ELEM(input->source, GPU_SOURCE_ATTR, GPU_SOURCE_BUILTIN)) { - continue; - } - - if (input->source == GPU_SOURCE_TEX) { - BLI_snprintf(input->shadername, sizeof(input->shadername), "samp%d", input->texid); - } - else { - BLI_snprintf(input->shadername, sizeof(input->shadername), "unf%d", input->id); - } - - if (input->source == GPU_SOURCE_TEX) { - if (input->bindtex) { - input->shaderloc = GPU_shader_get_uniform_ensure(shader, input->shadername); - /* extract nodes */ - BLI_remlink(&node->inputs, input); - BLI_addtail(inputs, input); - } - } - } - } -} - -/* Node Link Functions */ - -static GPUNodeLink *GPU_node_link_create(void) -{ - GPUNodeLink *link = MEM_callocN(sizeof(GPUNodeLink), "GPUNodeLink"); - 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) { - link->output->link = NULL; - } - MEM_freeN(link); - } -} - -/* Node Functions */ - -static GPUNode *GPU_node_begin(const char *name) -{ - GPUNode *node = MEM_callocN(sizeof(GPUNode), "GPUNode"); - - node->name = name; - - return node; -} - -static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const eGPUType type) -{ - GPUInput *input; - GPUNode *outnode; - const char *name; - - if (link->link_type == GPU_NODE_LINK_OUTPUT) { - outnode = link->output->node; - name = outnode->name; - input = outnode->inputs.first; - - if ((STR_ELEM(name, "set_value", "set_rgb", "set_rgba")) && (input->type == type)) { - input = MEM_dupallocN(outnode->inputs.first); - if (input->link) { - input->link->users++; - } - BLI_addtail(&node->inputs, input); - return; - } - } - - input = MEM_callocN(sizeof(GPUInput), "GPUInput"); - input->node = node; - input->type = type; - - 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: - case GPU_NODE_LINK_IMAGE_TILEMAP: - input->source = GPU_SOURCE_TEX; - input->ima = link->ima; - input->iuser = link->iuser; - break; - case GPU_NODE_LINK_ATTR: - input->source = GPU_SOURCE_ATTR; - input->attr_type = link->attr_type; - BLI_strncpy(input->attr_name, link->attr_name, sizeof(input->attr_name)); - 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; - } - - 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) { - /* For now INT is supported as float. */ - case SOCK_INT: - case SOCK_FLOAT: - return "set_value"; - case SOCK_VECTOR: - return "set_rgb"; - case SOCK_RGBA: - return "set_rgba"; - default: - BLI_assert(!"No gpu function for non-supported eNodeSocketDatatype"); - return NULL; - } -} - -/** - * Link stack uniform buffer. - * This is called for the input/output sockets that are note connected. - */ -static GPUNodeLink *gpu_uniformbuffer_link(GPUMaterial *mat, - bNode *node, - GPUNodeStack *stack, - const int index, - const eNodeSocketInOut in_out) -{ - bNodeSocket *socket; - - if (in_out == SOCK_IN) { - socket = BLI_findlink(&node->inputs, index); - } - else { - socket = BLI_findlink(&node->outputs, index); - } - - BLI_assert(socket != NULL); - BLI_assert(socket->in_out == in_out); - - if ((socket->flag & SOCK_HIDE_VALUE) == 0) { - GPUNodeLink *link; - switch (socket->type) { - case SOCK_FLOAT: { - bNodeSocketValueFloat *socket_data = socket->default_value; - link = GPU_uniform(&socket_data->value); - break; - } - case SOCK_VECTOR: { - bNodeSocketValueVector *socket_data = socket->default_value; - link = GPU_uniform(socket_data->value); - break; - } - case SOCK_RGBA: { - bNodeSocketValueRGBA *socket_data = socket->default_value; - link = GPU_uniform(socket_data->value); - break; - } - default: - return NULL; - break; - } - - if (in_out == SOCK_IN) { - GPU_link(mat, gpu_uniform_set_function_from_type(socket->type), link, &stack->link); - } - return link; - } - return NULL; -} - -static void gpu_node_input_socket( - GPUMaterial *material, bNode *bnode, GPUNode *node, GPUNodeStack *sock, const int index) -{ - if (sock->link) { - gpu_node_input_link(node, sock->link, sock->type); - } - else if ((material != NULL) && - (gpu_uniformbuffer_link(material, bnode, sock, index, SOCK_IN) != NULL)) { - gpu_node_input_link(node, sock->link, sock->type); - } - else { - gpu_node_input_link(node, GPU_constant(sock->vec), sock->type); - } -} - -static void gpu_node_output(GPUNode *node, const eGPUType type, GPUNodeLink **link) -{ - GPUOutput *output = MEM_callocN(sizeof(GPUOutput), "GPUOutput"); - - 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; - - /* note: the caller owns the reference to the link, GPUOutput - * merely points to it, and if the node is destroyed it will - * set that pointer to NULL */ - } - - BLI_addtail(&node->outputs, output); -} - -void GPU_inputs_free(ListBase *inputs) -{ - GPUInput *input; - - for (input = inputs->first; input; input = input->next) { - if (input->link) { - gpu_node_link_free(input->link); - } - } - - BLI_freelistN(inputs); -} - -static void gpu_node_free(GPUNode *node) -{ - GPUOutput *output; - - GPU_inputs_free(&node->inputs); - - for (output = node->outputs.first; output; output = output->next) { - if (output->link) { - output->link->output = NULL; - gpu_node_link_free(output->link); - } - } - - BLI_freelistN(&node->outputs); - MEM_freeN(node); -} - -static void gpu_nodes_free(ListBase *nodes) -{ - GPUNode *node; - - while ((node = BLI_pophead(nodes))) { - gpu_node_free(node); - } -} - -/* vertex attributes */ +/* Vertex Attributes */ static void gpu_nodes_get_vertex_attrs(ListBase *nodes, GPUVertAttrLayers *attrs) { @@ -1723,284 +1140,16 @@ static void gpu_nodes_get_vertex_attrs(ListBase *nodes, GPUVertAttrLayers *attrs } } -/* varargs linking */ - -GPUNodeLink *GPU_attribute(const CustomDataType type, const char *name) -{ - GPUNodeLink *link = GPU_node_link_create(); - link->link_type = GPU_NODE_LINK_ATTR; - link->attr_name = name; - /* Fall back to the UV layer, which matches old behavior. */ - if (type == CD_AUTO_FROM_NAME && name[0] == '\0') { - link->attr_type = CD_MTFACE; - } - else { - link->attr_type = type; - } - return link; -} - -GPUNodeLink *GPU_constant(float *num) -{ - GPUNodeLink *link = GPU_node_link_create(); - link->link_type = GPU_NODE_LINK_CONSTANT; - link->data = num; - return link; -} - -GPUNodeLink *GPU_uniform(float *num) -{ - GPUNodeLink *link = GPU_node_link_create(); - link->link_type = GPU_NODE_LINK_UNIFORM; - link->data = num; - return link; -} - -GPUNodeLink *GPU_image(Image *ima, ImageUser *iuser) -{ - GPUNodeLink *link = GPU_node_link_create(); - link->link_type = GPU_NODE_LINK_IMAGE_BLENDER; - link->ima = ima; - link->iuser = iuser; - return link; -} - -GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row) -{ - GPUNodeLink *link = GPU_node_link_create(); - 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(eGPUBuiltin builtin) -{ - GPUNodeLink *link = GPU_node_link_create(); - link->link_type = GPU_NODE_LINK_BUILTIN; - link->builtin = builtin; - return link; -} - -static void gpu_material_use_library_with_dependencies(GSet *used_libraries, - GPUMaterialLibrary *library) -{ - if (BLI_gset_add(used_libraries, library->code)) { - for (int i = 0; library->dependencies[i]; i++) { - gpu_material_use_library_with_dependencies(used_libraries, library->dependencies[i]); - } - } -} - -static void gpu_material_use_library(GPUMaterial *material, GPUMaterialLibrary *library) -{ - GSet *used_libraries = gpu_material_used_libraries(material); - gpu_material_use_library_with_dependencies(used_libraries, library); -} - -bool GPU_link(GPUMaterial *mat, const char *name, ...) -{ - GPUNode *node; - GPUFunction *function; - GPUNodeLink *link, **linkptr; - va_list params; - int i; - - function = gpu_lookup_function(name); - if (!function) { - fprintf(stderr, "GPU failed to find function %s\n", name); - return false; - } - - gpu_material_use_library(mat, function->library); - - node = GPU_node_begin(name); - - va_start(params, name); - for (i = 0; i < function->totparam; i++) { - if (function->paramqual[i] != FUNCTION_QUAL_IN) { - 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); - - gpu_material_add_node(mat, node); - - return true; -} - -bool GPU_stack_link(GPUMaterial *material, - bNode *bnode, - const char *name, - GPUNodeStack *in, - GPUNodeStack *out, - ...) -{ - GPUNode *node; - GPUFunction *function; - GPUNodeLink *link, **linkptr; - va_list params; - int i, totin, totout; - - function = gpu_lookup_function(name); - if (!function) { - fprintf(stderr, "GPU failed to find function %s\n", name); - return false; - } - - gpu_material_use_library(material, function->library); - - node = GPU_node_begin(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++; - } - } - } - - va_start(params, out); - for (i = 0; i < function->totparam; i++) { - if (function->paramqual[i] != FUNCTION_QUAL_IN) { - 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) { - gpu_node_input_socket(NULL, NULL, node, link->socket, -1); - } - else { - gpu_node_input_link(node, link, function->paramtype[i]); - } - } - else { - totin--; - } - } - } - va_end(params); - - gpu_material_add_node(material, node); - - return true; -} - -GPUNodeLink *GPU_uniformbuffer_link_out(GPUMaterial *mat, - bNode *node, - GPUNodeStack *stack, - const int index) -{ - return gpu_uniformbuffer_link(mat, node, stack, index, SOCK_OUT); -} - /* Pass create/free */ -static void gpu_nodes_tag(GPUNodeLink *link) -{ - GPUNode *node; - GPUInput *input; - - if (!link->output) { - return; - } - - node = link->output->node; - if (node->tag) { - return; - } - - node->tag = true; - for (input = node->inputs.first; input; input = input->next) { - if (input->link) { - gpu_nodes_tag(input->link); - } - } -} - -static void gpu_nodes_prune(ListBase *nodes, GPUNodeLink *outlink) -{ - GPUNode *node, *next; - - for (node = nodes->first; node; node = node->next) { - node->tag = false; - } - - gpu_nodes_tag(outlink); - - for (node = nodes->first; node; node = next) { - next = node->next; - - if (!node->tag) { - BLI_remlink(nodes, node); - gpu_node_free(node); - } - } -} - static bool gpu_pass_is_valid(GPUPass *pass) { /* Shader is not null if compilation is successful. */ return (pass->compiled == false || pass->shader != NULL); } -static char *code_generate_material_library(GPUMaterial *material, const char *frag_lib) -{ - DynStr *ds = BLI_dynstr_new(); - - if (frag_lib) { - BLI_dynstr_append(ds, frag_lib); - } - - GSet *used_libraries = gpu_material_used_libraries(material); - - /* Always include those because they may be needed by the execution function. */ - gpu_material_use_library_with_dependencies(used_libraries, - &gpu_shader_material_world_normals_library); - - /* Add library code in order, for dependencies. */ - for (int i = 0; gpu_material_libraries[i]; i++) { - GPUMaterialLibrary *library = gpu_material_libraries[i]; - if (BLI_gset_haskey(used_libraries, library->code)) { - BLI_dynstr_append(ds, library->code); - } - } - - char *result = BLI_dynstr_get_cstring(ds); - BLI_dynstr_free(ds); - - return result; -} - GPUPass *GPU_generate_pass(GPUMaterial *material, - GPUNodeLink *frag_outlink, - struct GPUVertAttrLayers *attrs, - ListBase *nodes, - int *builtins, + GPUNodeGraph *graph, const char *vert_code, const char *geom_code, const char *frag_lib, @@ -2011,14 +1160,15 @@ GPUPass *GPU_generate_pass(GPUMaterial *material, /* Prune the unused nodes and extract attributes before compiling so the * generated VBOs are ready to accept the future shader. */ - gpu_nodes_prune(nodes, frag_outlink); - gpu_nodes_get_vertex_attrs(nodes, attrs); + gpu_node_graph_prune_unused(graph); + gpu_nodes_get_vertex_attrs(&graph->nodes, &graph->attrs); /* generate code */ - char *fragmentgen = code_generate_fragment(material, nodes, frag_outlink->output, builtins); + char *fragmentgen = code_generate_fragment( + material, &graph->nodes, graph->outlink->output, &graph->builtins); /* Cache lookup: Reuse shaders already compiled */ - uint32_t hash = gpu_pass_hash(fragmentgen, defines, attrs); + uint32_t hash = gpu_pass_hash(fragmentgen, defines, &graph->attrs); pass_hash = gpu_pass_cache_lookup(hash); if (pass_hash && (pass_hash->next == NULL || pass_hash->next->hash != hash)) { @@ -2034,10 +1184,11 @@ GPUPass *GPU_generate_pass(GPUMaterial *material, /* Either the shader is not compiled or there is a hash collision... * continue generating the shader strings. */ - char *tmp = code_generate_material_library(material, frag_lib); + GSet *used_libraries = gpu_material_used_libraries(material); + char *tmp = gpu_material_library_generate_code(used_libraries, frag_lib); - geometrycode = code_generate_geometry(nodes, geom_code, defines); - vertexcode = code_generate_vertex(nodes, vert_code, (geometrycode != NULL)); + geometrycode = code_generate_geometry(&graph->nodes, geom_code, defines); + vertexcode = code_generate_vertex(&graph->nodes, vert_code, (geometrycode != NULL)); fragmentcode = BLI_strdupcat(tmp, fragmentgen); MEM_freeN(fragmentgen); @@ -2111,7 +1262,7 @@ static int count_active_texture_sampler(GPUShader *shader, char *source) code++; } /* Skip following spaces. */ - if (gpu_str_prefix(code, "sampler")) { + if (BLI_str_startswith(code, "sampler")) { /* Move past "uniform". */ code += 7; /* Skip sampler type suffix. */ @@ -2233,11 +1384,6 @@ static void gpu_pass_free(GPUPass *pass) MEM_freeN(pass); } -void GPU_pass_free_nodes(ListBase *nodes) -{ - gpu_nodes_free(nodes); -} - void GPU_pass_cache_garbage_collect(void) { static int lasttime = 0; @@ -2283,3 +1429,15 @@ void GPU_pass_cache_free(void) BLI_spin_end(&pass_cache_spin); } + +/* Module */ + +void gpu_codegen_init(void) +{ +} + +void gpu_codegen_exit(void) +{ + BKE_material_defaults_free_gpu(); + GPU_shader_free_builtin_shaders(); +} |