Vertex shaders optimization

This commit is contained in:
raysan5 2016-01-13 17:13:28 +01:00
parent bb49102a4b
commit fb6ef2c2f4
41 changed files with 86 additions and 92 deletions

View file

@ -336,8 +336,8 @@ typedef struct Shader {
int colorLoc; // Color attibute location point (vertex shader)
// Uniforms
int projectionLoc; // Projection matrix uniform location point (vertex shader)
int modelviewLoc; // ModelView matrix uniform location point (vertex shader)
int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader)
int modelLoc; // Model transformation matrix uniform location point (vertex shader)
int viewLoc; // View transformation matrix uniform location point (vertex shader)
int tintColorLoc; // Color uniform location point (fragment shader)

View file

@ -1295,9 +1295,10 @@ void rlglDraw(void)
if ((lines.vCounter > 0) || (triangles.vCounter > 0) || (quads.vCounter > 0))
{
glUseProgram(currentShader.id);
Matrix matMVP = MatrixMultiply(modelview, projection); // Create modelview-projection matrix
glUniformMatrix4fv(currentShader.projectionLoc, 1, false, MatrixToFloat(projection));
glUniformMatrix4fv(currentShader.modelviewLoc, 1, false, MatrixToFloat(modelview));
glUniformMatrix4fv(currentShader.mvpLoc, 1, false, MatrixToFloat(matMVP));
glUniform1i(currentShader.mapDiffuseLoc, 0);
}
@ -1520,14 +1521,14 @@ void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 r
Matrix matModelView = MatrixMultiply(matModel, matView); // Transform to camera-space coordinates
// Calculate model-view-projection matrix (MVP)
//Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates
Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates
// NOTE: Drawing in OpenGL 3.3+, matrices are passed to shader
// TODO: Reduce number of matrices passed to shaders, use only matMVP
glUniformMatrix4fv(model.shader.modelLoc, 1, false, MatrixToFloat(matModel));
glUniformMatrix4fv(model.shader.viewLoc, 1, false, MatrixToFloat(matView));
glUniformMatrix4fv(model.shader.projectionLoc, 1, false, MatrixToFloat(matProjection));
glUniformMatrix4fv(model.shader.modelviewLoc, 1, false, MatrixToFloat(matModelView));
glUniformMatrix4fv(model.shader.mvpLoc, 1, false, MatrixToFloat(matMVP));
// Apply color tinting to model
// NOTE: Just update one uniform on fragment shader
@ -2247,13 +2248,13 @@ Shader LoadShader(char *vsFileName, char *fsFileName)
shader.colorLoc = -1;
// Get handles to GLSL uniform locations (vertex shader)
shader.modelviewLoc = glGetUniformLocation(shader.id, "modelviewMatrix");
shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix");
shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix");
shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix");
shader.projectionLoc = glGetUniformLocation(shader.id, "projectionMatrix");
// Get handles to GLSL uniform locations (fragment shader)
shader.tintColorLoc = glGetUniformLocation(shader.id, "tintColor");
shader.tintColorLoc = glGetUniformLocation(shader.id, "fragTintColor");
shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0");
shader.mapNormalLoc = -1; // It can be set later
shader.mapSpecularLoc = -1; // It can be set later
@ -2738,40 +2739,39 @@ static Shader LoadDefaultShader(void)
"in vec2 vertexTexCoord; \n"
"in vec4 vertexColor; \n"
"out vec2 fragTexCoord; \n"
"out vec4 tintColor; \n"
"out vec4 fragTintColor; \n"
#elif defined(GRAPHICS_API_OPENGL_ES2)
char vShaderStr[] = "#version 100 \n"
"attribute vec3 vertexPosition; \n"
"attribute vec2 vertexTexCoord; \n"
"attribute vec4 vertexColor; \n"
"varying vec2 fragTexCoord; \n"
"varying vec4 tintColor; \n"
"varying vec4 fragTintColor; \n"
#endif
"uniform mat4 projectionMatrix; \n"
"uniform mat4 modelviewMatrix; \n"
"uniform mat4 mvpMatrix; \n"
"void main() \n"
"{ \n"
" fragTexCoord = vertexTexCoord; \n"
" tintColor = vertexColor; \n"
" gl_Position = projectionMatrix*modelviewMatrix*vec4(vertexPosition, 1.0); \n"
" fragTintColor = vertexColor; \n"
" gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); \n"
"} \n";
// Fragment shader directly defined, no external file required
#if defined(GRAPHICS_API_OPENGL_33)
char fShaderStr[] = "#version 330 \n"
"in vec2 fragTexCoord; \n"
"in vec4 tintColor; \n"
"in vec4 fragTintColor; \n"
#elif defined(GRAPHICS_API_OPENGL_ES2)
char fShaderStr[] = "#version 100 \n"
"precision mediump float; \n" // precision required for OpenGL ES2 (WebGL)
"varying vec2 fragTexCoord; \n"
"varying vec4 tintColor; \n"
"varying vec4 fragTintColor; \n"
#endif
"uniform sampler2D texture0; \n"
"void main() \n"
"{ \n"
" vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0, use texture() instead
" gl_FragColor = texelColor*tintColor; \n"
" gl_FragColor = texelColor*fragTintColor; \n"
"} \n";
shader.id = LoadShaderProgram(vShaderStr, fShaderStr);
@ -2788,10 +2788,10 @@ static Shader LoadDefaultShader(void)
shader.normalLoc = -1;
// Get handles to GLSL uniform locations (vertex shader)
shader.modelviewLoc = glGetUniformLocation(shader.id, "modelviewMatrix");
shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix");
shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix");
shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix");
shader.projectionLoc = glGetUniformLocation(shader.id, "projectionMatrix");
// Get handles to GLSL uniform locations (fragment shader)
shader.tintColorLoc = -1;
@ -2831,12 +2831,11 @@ static Shader LoadSimpleShader(void)
"attribute vec3 vertexNormal; \n"
"varying vec2 fragTexCoord; \n"
#endif
"uniform mat4 projectionMatrix; \n"
"uniform mat4 modelviewMatrix; \n"
"uniform mat4 mvpMatrix; \n"
"void main() \n"
"{ \n"
" fragTexCoord = vertexTexCoord; \n"
" gl_Position = projectionMatrix*modelviewMatrix*vec4(vertexPosition, 1.0); \n"
" gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); \n"
"} \n";
// Fragment shader directly defined, no external file required
@ -2849,11 +2848,11 @@ static Shader LoadSimpleShader(void)
"varying vec2 fragTexCoord; \n"
#endif
"uniform sampler2D texture0; \n"
"uniform vec4 tintColor; \n"
"uniform vec4 fragTintColor; \n"
"void main() \n"
"{ \n"
" vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0, use texture() instead
" gl_FragColor = texelColor*tintColor; \n"
" gl_FragColor = texelColor*fragTintColor; \n"
"} \n";
shader.id = LoadShaderProgram(vShaderStr, fShaderStr);
@ -2870,13 +2869,13 @@ static Shader LoadSimpleShader(void)
shader.colorLoc = -1;
// Get handles to GLSL uniform locations (vertex shader)
shader.modelviewLoc = glGetUniformLocation(shader.id, "modelviewMatrix");
shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix");
shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix");
shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix");
shader.projectionLoc = glGetUniformLocation(shader.id, "projectionMatrix");
// Get handles to GLSL uniform locations (fragment shader)
shader.tintColorLoc = glGetUniformLocation(shader.id, "tintColor");
shader.tintColorLoc = glGetUniformLocation(shader.id, "fragTintColor");
shader.mapDiffuseLoc = glGetUniformLocation(shader.id, "texture0");
shader.mapNormalLoc = -1; // It can be set later
shader.mapSpecularLoc = -1; // It can be set later

View file

@ -159,8 +159,8 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion;
int colorLoc; // Color attibute location point (vertex shader)
// Uniforms
int projectionLoc; // Projection matrix uniform location point (vertex shader)
int modelviewLoc; // ModelView matrix uniform location point (vertex shader)
int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader)
int modelLoc; // Model transformation matrix uniform location point (vertex shader)
int viewLoc; // View transformation matrix uniform location point (vertex shader)
int tintColorLoc; // Color uniform location point (fragment shader)