Gawain: Make common uniforms become builtins
authorClément Foucault <foucault.clem@gmail.com>
Sun, 8 Oct 2017 13:49:25 +0000 (15:49 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Sun, 8 Oct 2017 13:49:25 +0000 (15:49 +0200)
This improves eevee's cache performance by 13% in my test.

intern/gawain/gawain/gwn_shader_interface.h
intern/gawain/src/gwn_shader_interface.c
source/blender/draw/intern/draw_manager.c
source/blender/gpu/GPU_shader.h
source/blender/gpu/intern/gpu_shader.c

index 720cde40b9327233b854f4ecba10df866c38ffdf..345ad8d389b417e9e8ae95ad50d7b20b6c0e40d6 100644 (file)
 typedef enum {
        GWN_UNIFORM_NONE = 0, // uninitialized/unknown
 
+       GWN_UNIFORM_MODEL,      // mat4 ModelMatrix
+       GWN_UNIFORM_VIEW,       // mat4 ViewMatrix
        GWN_UNIFORM_MODELVIEW,  // mat4 ModelViewMatrix
        GWN_UNIFORM_PROJECTION, // mat4 ProjectionMatrix
+       GWN_UNIFORM_VIEWPROJECTION, // mat4 ViewProjectionMatrix
        GWN_UNIFORM_MVP,        // mat4 ModelViewProjectionMatrix
 
-       GWN_UNIFORM_MODELVIEW_INV,  // mat4 ModelViewInverseMatrix
-       GWN_UNIFORM_PROJECTION_INV, // mat4 ProjectionInverseMatrix
+       GWN_UNIFORM_MODEL_INV,           // mat4 ModelMatrixInverse
+       GWN_UNIFORM_VIEW_INV,            // mat4 ViewMatrixInverse
+       GWN_UNIFORM_MODELVIEW_INV,       // mat4 ModelViewMatrixInverse
+       GWN_UNIFORM_PROJECTION_INV,      // mat4 ProjectionMatrixInverse
+       GWN_UNIFORM_VIEWPROJECTION_INV,  // mat4 ViewProjectionMatrixInverse
 
-       GWN_UNIFORM_NORMAL,     // mat3 NormalMatrix
+       GWN_UNIFORM_NORMAL,      // mat3 NormalMatrix
+       GWN_UNIFORM_WORLDNORMAL, // mat3 WorldNormalMatrix
+       GWN_UNIFORM_CAMERATEXCO, // vec4 CameraTexCoFactors
+       GWN_UNIFORM_ORCO,        // vec3 OrcoTexCoFactors[]
+       GWN_UNIFORM_CLIPPLANES,  // vec4 ClipPlanes[]
 
        GWN_UNIFORM_COLOR, // vec4 color
+       GWN_UNIFORM_EYE, // vec3 eye
 
        GWN_UNIFORM_CUSTOM, // custom uniform, not one of the above built-ins
 
index f4877c17087e331656fe58b09893a10185882fb6..1fb941f922585e30ea6baed7e8d087f58458a844 100644 (file)
@@ -26,16 +26,27 @@ static const char* BuiltinUniform_name(Gwn_UniformBuiltin u)
                {
                [GWN_UNIFORM_NONE] = NULL,
 
+               [GWN_UNIFORM_MODEL] = "ModelMatrix",
+               [GWN_UNIFORM_VIEW] = "ViewMatrix",
                [GWN_UNIFORM_MODELVIEW] = "ModelViewMatrix",
                [GWN_UNIFORM_PROJECTION] = "ProjectionMatrix",
+               [GWN_UNIFORM_VIEWPROJECTION] = "ViewProjectionMatrix",
                [GWN_UNIFORM_MVP] = "ModelViewProjectionMatrix",
 
-               [GWN_UNIFORM_MODELVIEW_INV] = "ModelViewInverseMatrix",
-               [GWN_UNIFORM_PROJECTION_INV] = "ProjectionInverseMatrix",
+               [GWN_UNIFORM_MODEL_INV] = "ModelMatrixInverse",
+               [GWN_UNIFORM_VIEW_INV] = "ViewMatrixInverse",
+               [GWN_UNIFORM_MODELVIEW_INV] = "ModelViewMatrixInverse",
+               [GWN_UNIFORM_PROJECTION_INV] = "ProjectionMatrixInverse",
+               [GWN_UNIFORM_VIEWPROJECTION_INV] = "ViewProjectionMatrixInverse",
 
                [GWN_UNIFORM_NORMAL] = "NormalMatrix",
+               [GWN_UNIFORM_WORLDNORMAL] = "WorldNormalMatrix",
+               [GWN_UNIFORM_CAMERATEXCO] = "CameraTexCoFactors",
+               [GWN_UNIFORM_ORCO] = "OrcoTexCoFactors",
+               [GWN_UNIFORM_CLIPPLANES] = "ClipPlanes",
 
                [GWN_UNIFORM_COLOR] = "color",
+               [GWN_UNIFORM_EYE] = "eye",
 
                [GWN_UNIFORM_CUSTOM] = NULL,
                [GWN_NUM_UNIFORMS] = NULL,
@@ -135,16 +146,12 @@ GWN_INLINE void buckets_free(Gwn_ShaderInput* buckets[GWN_NUM_SHADERINTERFACE_BU
                }
        }
 
-// keep these in sync with Gwn_UniformBuiltin order
-#define FIRST_UNIFORM GWN_UNIFORM_MODELVIEW
-#define LAST_UNIFORM GWN_UNIFORM_COLOR
-
 static bool setup_builtin_uniform(Gwn_ShaderInput* input, const char* name)
        {
        // TODO: reject DOUBLE, IMAGE, ATOMIC_COUNTER gl_types
 
        // detect built-in uniforms (name must match)
-       for (Gwn_UniformBuiltin u = FIRST_UNIFORM; u <= LAST_UNIFORM; ++u)
+       for (Gwn_UniformBuiltin u = GWN_UNIFORM_NONE + 1; u < GWN_UNIFORM_CUSTOM; ++u)
                {
                const char* builtin_name = BuiltinUniform_name(u);
                if (match(name, builtin_name))
index 1c83b813355c0e36e61a156ebd3b51e96aba84c0..38a767b7e348239c3641e79fd66e1b3a3414f538 100644 (file)
@@ -641,23 +641,23 @@ static DRWInterface *DRW_interface_create(GPUShader *shader)
 {
        DRWInterface *interface = MEM_mallocN(sizeof(DRWInterface), "DRWInterface");
 
-       interface->model = GPU_shader_get_uniform(shader, "ModelMatrix");
-       interface->modelinverse = GPU_shader_get_uniform(shader, "ModelMatrixInverse");
-       interface->modelview = GPU_shader_get_uniform(shader, "ModelViewMatrix");
-       interface->modelviewinverse = GPU_shader_get_uniform(shader, "ModelViewMatrixInverse");
-       interface->projection = GPU_shader_get_uniform(shader, "ProjectionMatrix");
-       interface->projectioninverse = GPU_shader_get_uniform(shader, "ProjectionMatrixInverse");
-       interface->view = GPU_shader_get_uniform(shader, "ViewMatrix");
-       interface->viewinverse = GPU_shader_get_uniform(shader, "ViewMatrixInverse");
-       interface->viewprojection = GPU_shader_get_uniform(shader, "ViewProjectionMatrix");
-       interface->viewprojectioninverse = GPU_shader_get_uniform(shader, "ViewProjectionMatrixInverse");
-       interface->modelviewprojection = GPU_shader_get_uniform(shader, "ModelViewProjectionMatrix");
-       interface->normal = GPU_shader_get_uniform(shader, "NormalMatrix");
-       interface->worldnormal = GPU_shader_get_uniform(shader, "WorldNormalMatrix");
-       interface->camtexfac = GPU_shader_get_uniform(shader, "CameraTexCoFactors");
-       interface->orcotexfac = GPU_shader_get_uniform(shader, "OrcoTexCoFactors[0]");
-       interface->eye = GPU_shader_get_uniform(shader, "eye");
-       interface->clipplanes = GPU_shader_get_uniform(shader, "ClipPlanes[0]");
+       interface->model = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODEL);
+       interface->modelinverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODEL_INV);
+       interface->modelview = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODELVIEW);
+       interface->modelviewinverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODELVIEW_INV);
+       interface->projection = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_PROJECTION);
+       interface->projectioninverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_PROJECTION_INV);
+       interface->view = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEW);
+       interface->viewinverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEW_INV);
+       interface->viewprojection = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEWPROJECTION);
+       interface->viewprojectioninverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEWPROJECTION_INV);
+       interface->modelviewprojection = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MVP);
+       interface->normal = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_NORMAL);
+       interface->worldnormal = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_WORLDNORMAL);
+       interface->camtexfac = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_CAMERATEXCO);
+       interface->orcotexfac = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_ORCO);
+       interface->clipplanes = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_CLIPPLANES);
+       interface->eye = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_EYE);
        interface->instance_count = 0;
        interface->attribs_count = 0;
        interface->attribs_stride = 0;
index 5b62bafd4020ed5e1c7c01f56f628158ed361dfb..f3de3240c26425ed2d1529acef927257b7ed0df1 100644 (file)
@@ -76,6 +76,7 @@ void *GPU_fx_shader_get_interface(GPUShader *shader);
 void GPU_fx_shader_set_interface(GPUShader *shader, void *interface);
 
 int GPU_shader_get_uniform(GPUShader *shader, const char *name);
+int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin);
 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name);
 void GPU_shader_uniform_vector(GPUShader *shader, int location, int length,
        int arraysize, const float *value);
index 66049b1d2a16220ea801c3ee718adc72a631123f..cc1e1e8bb143a2db0bf2fc5bc1342c864b6c43a2 100644 (file)
@@ -557,6 +557,13 @@ int GPU_shader_get_uniform(GPUShader *shader, const char *name)
        return uniform ? uniform->location : -1;
 }
 
+int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin)
+{
+       BLI_assert(shader && shader->program);
+       const Gwn_ShaderInput *uniform = GWN_shaderinterface_uniform_builtin(shader->interface, builtin);
+       return uniform ? uniform->location : -1;
+}
+
 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
 {
        BLI_assert(shader && shader->program);