Commit: b02786ae6be71d3b7581b672b7896005f68c8b63
Author: Mike Erwin
Date:   Wed Apr 12 18:55:32 2017 -0400
Branches: blender2.8
https://developer.blender.org/rBb02786ae6be71d3b7581b672b7896005f68c8b63

Gawain: use ShaderInterface to manage uniforms

This eliminates tons of glGetUniformLocation calls from the drawing loop. Vast 
majority of code can keep making the same function calls. They're just faster 
now!

- Batch_Uniform*
- immUniform*
- gpuBindMatrices
- and others

===================================================================

M       intern/gawain/gawain/batch.h
M       intern/gawain/gawain/immediate.h
M       intern/gawain/src/batch.c
M       intern/gawain/src/immediate.c
M       intern/opencolorio/ocio_impl_glsl.cc
M       source/blender/draw/intern/draw_manager.c
M       source/blender/gpu/GPU_matrix.h
M       source/blender/gpu/intern/gpu_batch.c
M       source/blender/gpu/intern/gpu_compositing.c
M       source/blender/gpu/intern/gpu_immediate.c
M       source/blender/gpu/intern/gpu_matrix.c
M       source/blender/gpu/intern/gpu_shader.c

===================================================================

diff --git a/intern/gawain/gawain/batch.h b/intern/gawain/gawain/batch.h
index 2802dad3db3..9915b0b57f4 100644
--- a/intern/gawain/gawain/batch.h
+++ b/intern/gawain/gawain/batch.h
@@ -13,6 +13,7 @@
 
 #include "vertex_buffer.h"
 #include "element.h"
+#include "shader_interface.h"
 
 typedef enum {
        READY_TO_FORMAT,
@@ -38,6 +39,7 @@ typedef struct Batch {
 
        // state
        GLuint program;
+       const ShaderInterface* interface;
 } Batch;
 
 Batch* Batch_create(PrimitiveType, VertexBuffer*, ElementList*);
@@ -48,7 +50,7 @@ void Batch_discard_all(Batch*); // including verts & elem
 
 int Batch_add_VertexBuffer(Batch*, VertexBuffer*);
 
-void Batch_set_program(Batch*, GLuint program);
+void Batch_set_program(Batch*, GLuint program, const ShaderInterface*);
 // Entire batch draws with one shader program, but can be redrawn later with 
another program.
 // Vertex shader's inputs must be compatible with the batch's vertex format.
 
diff --git a/intern/gawain/gawain/immediate.h b/intern/gawain/gawain/immediate.h
index 62754306abe..b08be688cf4 100644
--- a/intern/gawain/gawain/immediate.h
+++ b/intern/gawain/gawain/immediate.h
@@ -13,13 +13,14 @@
 
 #include "vertex_format.h"
 #include "primitive.h"
+#include "shader_interface.h"
 
 #define IMM_BATCH_COMBO 1
 
 
 VertexFormat* immVertexFormat(void); // returns a cleared vertex format, ready 
for add_attrib
 
-void immBindProgram(GLuint program); // every immBegin must have a program 
bound first
+void immBindProgram(GLuint program, const ShaderInterface*); // every immBegin 
must have a program bound first
 void immUnbindProgram(void); // call after your last immEnd, or before binding 
another program
 
 void immBegin(PrimitiveType, unsigned vertex_ct); // must supply exactly 
vertex_ct vertices
diff --git a/intern/gawain/src/batch.c b/intern/gawain/src/batch.c
index 7e904cbd487..5de6335e0bb 100644
--- a/intern/gawain/src/batch.c
+++ b/intern/gawain/src/batch.c
@@ -88,13 +88,14 @@ int Batch_add_VertexBuffer(Batch* batch, VertexBuffer* 
verts)
        return -1;
        }
 
-void Batch_set_program(Batch* batch, GLuint program)
+void Batch_set_program(Batch* batch, GLuint program, const ShaderInterface* 
shaderface)
        {
 #if TRUST_NO_ONE
        assert(glIsProgram(program));
 #endif
 
        batch->program = program;
+       batch->interface = shaderface;
        batch->program_dirty = true;
 
        Batch_use_program(batch); // hack! to make Batch_Uniform* simpler
@@ -172,92 +173,58 @@ void Batch_done_using_program(Batch* batch)
                }
        }
 
-void Batch_Uniform1i(Batch* batch, const char* name, int value)
-       {
-       int loc = glGetUniformLocation(batch->program, name);
-
 #if TRUST_NO_ONE
-       assert(loc != -1);
+  #define GET_UNIFORM const ShaderInput* uniform = 
ShaderInterface_uniform(batch->interface, name); assert(uniform);
+#else
+  #define GET_UNIFORM const ShaderInput* uniform = 
ShaderInterface_uniform(batch->interface, name);
 #endif
 
-       glUniform1i(loc, value);
+void Batch_Uniform1i(Batch* batch, const char* name, int value)
+       {
+       GET_UNIFORM
+       glUniform1i(uniform->location, value);
        }
 
 void Batch_Uniform1b(Batch* batch, const char* name, bool value)
        {
-       int loc = glGetUniformLocation(batch->program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform1i(loc, value ? GL_TRUE : GL_FALSE);
+       GET_UNIFORM
+       glUniform1i(uniform->location, value ? GL_TRUE : GL_FALSE);
        }
 
 void Batch_Uniform2f(Batch* batch, const char* name, float x, float y)
        {
-       int loc = glGetUniformLocation(batch->program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform2f(loc, x, y);
+       GET_UNIFORM
+       glUniform2f(uniform->location, x, y);
        }
 
 void Batch_Uniform3f(Batch* batch, const char* name, float x, float y, float z)
        {
-       int loc = glGetUniformLocation(batch->program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform3f(loc, x, y, z);
+       GET_UNIFORM
+       glUniform3f(uniform->location, x, y, z);
        }
 
 void Batch_Uniform4f(Batch* batch, const char* name, float x, float y, float 
z, float w)
        {
-       int loc = glGetUniformLocation(batch->program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform4f(loc, x, y, z, w);
+       GET_UNIFORM
+       glUniform4f(uniform->location, x, y, z, w);
        }
 
 void Batch_Uniform1f(Batch* batch, const char* name, float x)
        {
-       int loc = glGetUniformLocation(batch->program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform1f(loc, x);
+       GET_UNIFORM
+       glUniform1f(uniform->location, x);
        }
 
 void Batch_Uniform3fv(Batch* batch, const char* name, const float data[3])
        {
-       int loc = glGetUniformLocation(batch->program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform3fv(loc, 1, data);
+       GET_UNIFORM
+       glUniform3fv(uniform->location, 1, data);
        }
 
 void Batch_Uniform4fv(Batch* batch, const char* name, const float data[4])
        {
-       int loc = glGetUniformLocation(batch->program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform4fv(loc, 1, data);
+       GET_UNIFORM
+       glUniform4fv(uniform->location, 1, data);
        }
 
 static void Batch_prime(Batch* batch)
diff --git a/intern/gawain/src/immediate.c b/intern/gawain/src/immediate.c
index 2fd2d2480a7..f384887a2ad 100644
--- a/intern/gawain/src/immediate.c
+++ b/intern/gawain/src/immediate.c
@@ -15,7 +15,7 @@
 #include <string.h>
 
 // necessary functions from matrix API
-extern void gpuBindMatrices(GLuint program);
+extern void gpuBindMatrices(const ShaderInterface*);
 extern bool gpuMatricesDirty(void);
 
 typedef struct {
@@ -44,6 +44,7 @@ typedef struct {
        GLuint vao_id;
        
        GLuint bound_program;
+       const ShaderInterface* shader_interface;
        AttribBinding attrib_binding;
        uint16_t prev_enabled_attrib_bits; // <-- only affects this VAO, so 
we're ok
 } Immediate;
@@ -117,21 +118,22 @@ VertexFormat* immVertexFormat(void)
        return &imm.vertex_format;
        }
 
-void immBindProgram(GLuint program)
+void immBindProgram(GLuint program, const ShaderInterface* shaderface)
        {
 #if TRUST_NO_ONE
        assert(imm.bound_program == 0);
        assert(glIsProgram(program));
 #endif
 
+       imm.bound_program = program;
+       imm.shader_interface = shaderface;
+
        if (!imm.vertex_format.packed)
                VertexFormat_pack(&imm.vertex_format);
 
        glUseProgram(program);
        get_attrib_locations(&imm.vertex_format, &imm.attrib_binding, program);
-       imm.bound_program = program;
-
-       gpuBindMatrices(program);
+       gpuBindMatrices(shaderface);
        }
 
 void immUnbindProgram(void)
@@ -267,7 +269,7 @@ Batch* immBeginBatch(PrimitiveType prim_type, unsigned 
vertex_ct)
        imm.batch = Batch_create(prim_type, verts, NULL);
        imm.batch->phase = BUILDING;
 
-       Batch_set_program(imm.batch, imm.bound_program);
+       Batch_set_program(imm.batch, imm.bound_program, imm.shader_interface);
 
        return imm.batch;
        }
@@ -336,7 +338,7 @@ static void immDrawSetup(void)
                }
 
        if (gpuMatricesDirty())
-               gpuBindMatrices(imm.bound_program);
+               gpuBindMatrices(imm.shader_interface);
        }
 
 void immEnd(void)
@@ -716,126 +718,76 @@ void immVertex2iv(unsigned attrib_id, const int data[2])
 
 // --- generic uniform functions ---
 
-void immUniform1f(const char* name, float x)
-       {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
 #if TRUST_NO_ONE
-       assert(loc != -1);
+  #define GET_UNIFORM const ShaderInput* uniform = 
ShaderInterface_uniform(imm.shader_interface, name); assert(uniform);
+#else
+  #define GET_UNIFORM const ShaderInput* uniform = 
ShaderInterface_uniform(imm.shader_interface, name);
 #endif
 
-       glUniform1f(loc, x);
+void immUniform1f(const char* name, float x)
+       {
+       GET_UNIFORM
+       glUniform1f(uniform->location, x);
        }
 
 void immUniform2f(const char* name, float x, float y)
 {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform2f(loc, x, y);
+       GET_UNIFORM
+       glUniform2f(uniform->location, x, y);
 }
 
 void immUniform2fv(const char* name, const float data[2])
 {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform2fv(loc, 1, data);
+       GET_UNIFORM
+       glUniform2fv(uniform->location, 1, data);
 }
 
 void immUniform3f(const char* name, float x, float y, float z)
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform3f(loc, x, y, z);
+       GET_UNIFORM
+       glUniform3f(uniform->location, x, y, z);
        }
 
 void immUniform3fv(const char* name, const float data[3])
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform3fv(loc, 1, data);
+       GET_UNIFORM
+       glUniform3fv(uniform->location, 1, data);
        }
 
 void immUniformArray3fv(const char* name, const float *data, int count)
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-       assert(count > 0);
-#endif
-
-       glUniform3fv(loc, count, data);
+       GET_UNIFORM
+       glUniform3fv(uniform->location, count, data);
        }
 
 void immUniform4f(const char* name, float x, float y, float z, float w)
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform4f(loc, x, y, z, w);
+       GET_UNIFORM
+       glUniform4f(uniform->location, x, y, z, w);
        }
 
 void immUniform4fv(const char* name, const float data[4])
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform4fv(loc, 1, data);
+       GET_UNIFORM
+       glUniform4fv(uniform->location, 1, data);
        }
 
 void immUniformMatrix4fv(const char* name, const float data[4][4])
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniformMatrix4fv(loc, 1, GL_FALSE, (float *)data);
+       GET_UNIFORM
+       glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (float *)data);
        }
 
 void immUniform1i(const char* name, int x)
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform1i(loc, x);
+       GET_UNIFORM
+       glUniform1i(uniform->location, x);
        }
 
 void immUniform4iv(const char* name, const int data[4])
        {
-       int loc = glGetUniformLocation(imm.bound_program, name);
-
-#if TRUST_NO_ONE
-       assert(loc != -1);
-#endif
-
-       glUniform4iv(loc, 1, data);
+       GET_UNIFOR

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to