From 637bee81ccb270d5d91df3974943ea8eb18feba9 Mon Sep 17 00:00:00 2001 From: Jorge Bernal Date: Thu, 4 Oct 2018 23:40:51 +0200 Subject: [PATCH] UPBGE: Fix for #797 Introduce new array of GPUType sizes to make it extensible and adding more comments too. --- source/blender/gpu/GPU_material.h | 4 +++- source/blender/gpu/intern/gpu_codegen.c | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h index 75776a2ae3e5..60f8a3ef36f1 100644 --- a/source/blender/gpu/GPU_material.h +++ b/source/blender/gpu/GPU_material.h @@ -68,6 +68,7 @@ typedef struct GPUParticleInfo GPUParticleInfo; /* Functions to create GPU Materials nodes */ typedef enum GPUType { + /* Types taken into account by GPU_DATATYPE_STR and GPU_DATATYPE_SIZE arrays */ /* The value indicates the number of elements in each type */ GPU_NONE = 0, GPU_FLOAT = 1, @@ -77,7 +78,8 @@ typedef enum GPUType { GPU_MAT3 = 9, GPU_MAT4 = 16, - GPU_INT = 17, + GPU_INT = 17, /* this value doesn't point the number of elements */ + /* end of types taken into account by GPU_DATATYPE_STR and GPU_DATATYPE_SIZE arrays */ GPU_TEX2D = 1002, GPU_SHADOW2D = 1003, diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c index 0d3ae56daf35..891314899b9c 100644 --- a/source/blender/gpu/intern/gpu_codegen.c +++ b/source/blender/gpu/intern/gpu_codegen.c @@ -55,6 +55,11 @@ #include #include +#ifndef ARRAYSIZE +// There is a better way, but this is good enough for our purpose. +# define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a))) +#endif + extern char datatoc_gpu_shader_material_glsl[]; extern char datatoc_gpu_shader_vertex_glsl[]; extern char datatoc_gpu_shader_vertex_world_glsl[]; @@ -90,6 +95,10 @@ static const char *GPU_DATATYPE_STR[18] = { "", "float", "vec2", "vec3", "vec4", NULL, NULL, NULL, NULL, "mat3", NULL, NULL, NULL, NULL, NULL, NULL, "mat4", "int" }; +/* Indices match the GPUType size */ +static const unsigned int GPU_DATATYPE_SIZE[18] = { + 0, 1, 2, 3, 4, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 16, 1 +}; /* GLSL code parsing for finding function definitions. * These are stored in a hash for lookup when creating a material. */ @@ -172,7 +181,7 @@ static void gpu_parse_functions_string(GHash *hash, char *code) /* test for type */ type = GPU_NONE; - for (i = 1; i <= 17; i++) { + for (i = 1; i <= ARRAYSIZE(GPU_DATATYPE_STR) - 1; i++) { if (GPU_DATATYPE_STR[i] && gpu_str_prefix(code, GPU_DATATYPE_STR[i])) { type = i; break; @@ -1297,7 +1306,7 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType input->type = type; input->source = GPU_SOURCE_VEC_UNIFORM; - memcpy(input->vec, link->ptr1, type * sizeof(float)); + memcpy(input->vec, link->ptr1, GPU_DATATYPE_SIZE[type] * sizeof(float)); if (link->dynamic) { input->dynamicvec = link->ptr1; input->dynamictype = link->dynamictype;