From 470098b7e633a77e114eb8fb25d08262d63fb40f Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Fri, 27 Jan 2017 23:19:04 -0400 Subject: [PATCH] Shader code cleanup --HG-- branch : minor --- src/modules/graphics/Shader.cpp | 51 ++++++------------ src/modules/graphics/Shader.h | 22 +++----- src/modules/graphics/opengl/Mesh.cpp | 4 +- src/modules/graphics/opengl/Shader.cpp | 18 +++---- src/modules/graphics/opengl/wrap_Graphics.lua | 53 ++++++++++++------- src/modules/graphics/vertex.cpp | 20 +++++++ src/modules/graphics/vertex.h | 3 ++ 7 files changed, 91 insertions(+), 80 deletions(-) diff --git a/src/modules/graphics/Shader.cpp b/src/modules/graphics/Shader.cpp index ba13af39d..c450d20f2 100644 --- a/src/modules/graphics/Shader.cpp +++ b/src/modules/graphics/Shader.cpp @@ -269,16 +269,6 @@ bool Shader::getConstant(ShaderStage in, const char *&out) return stageNames.find(in, out); } -bool Shader::getConstant(const char *in, VertexAttribID &out) -{ - return attribNames.find(in, out); -} - -bool Shader::getConstant(VertexAttribID in, const char *&out) -{ - return attribNames.find(in, out); -} - bool Shader::getConstant(const char *in, BuiltinUniform &out) { return builtinNames.find(in, out); @@ -291,43 +281,34 @@ bool Shader::getConstant(BuiltinUniform in, const char *&out) StringMap::Entry Shader::languageEntries[] = { - {"glsl1", LANGUAGE_GLSL1 }, - {"glsles1", LANGUAGE_GLSLES1}, - {"glsl3", LANGUAGE_GLSL3 }, - {"glsles3", LANGUAGE_GLSLES3}, + { "glsl1", LANGUAGE_GLSL1 }, + { "glsles1", LANGUAGE_GLSLES1 }, + { "glsl3", LANGUAGE_GLSL3 }, + { "glsles3", LANGUAGE_GLSLES3 }, }; StringMap Shader::languages(Shader::languageEntries, sizeof(Shader::languageEntries)); StringMap::Entry Shader::stageNameEntries[] = { - {"vertex", Shader::STAGE_VERTEX}, - {"pixel", Shader::STAGE_PIXEL}, + { "vertex", STAGE_VERTEX }, + { "pixel", STAGE_PIXEL }, }; StringMap Shader::stageNames(Shader::stageNameEntries, sizeof(Shader::stageNameEntries)); -StringMap::Entry Shader::attribNameEntries[] = -{ - {"VertexPosition", ATTRIB_POS}, - {"VertexTexCoord", ATTRIB_TEXCOORD}, - {"VertexColor", ATTRIB_COLOR}, - {"ConstantColor", ATTRIB_CONSTANTCOLOR}, -}; - -StringMap Shader::attribNames(Shader::attribNameEntries, sizeof(Shader::attribNameEntries)); - StringMap::Entry Shader::builtinNameEntries[] = { - {"TransformMatrix", Shader::BUILTIN_TRANSFORM_MATRIX}, - {"ProjectionMatrix", Shader::BUILTIN_PROJECTION_MATRIX}, - {"TransformProjectionMatrix", Shader::BUILTIN_TRANSFORM_PROJECTION_MATRIX}, - {"NormalMatrix", Shader::BUILTIN_NORMAL_MATRIX}, - {"love_PointSize", Shader::BUILTIN_POINT_SIZE}, - {"love_ScreenSize", Shader::BUILTIN_SCREEN_SIZE}, - {"love_VideoYChannel", Shader::BUILTIN_VIDEO_Y_CHANNEL}, - {"love_VideoCbChannel", Shader::BUILTIN_VIDEO_CB_CHANNEL}, - {"love_VideoCrChannel", Shader::BUILTIN_VIDEO_CR_CHANNEL}, + { "MainTex", BUILTIN_TEXTURE_MAIN }, + { "love_VideoYChannel", BUILTIN_TEXTURE_VIDEO_Y }, + { "love_VideoCbChannel", BUILTIN_TEXTURE_VIDEO_CB }, + { "love_VideoCrChannel", BUILTIN_TEXTURE_VIDEO_CR }, + { "TransformMatrix", BUILTIN_MATRIX_TRANSFORM }, + { "ProjectionMatrix", BUILTIN_MATRIX_PROJECTION }, + { "TransformProjectionMatrix", BUILTIN_MATRIX_TRANSFORM_PROJECTION }, + { "NormalMatrix", BUILTIN_MATRIX_NORMAL }, + { "love_PointSize", BUILTIN_POINT_SIZE }, + { "love_ScreenSize", BUILTIN_SCREEN_SIZE }, }; StringMap Shader::builtinNames(Shader::builtinNameEntries, sizeof(Shader::builtinNameEntries)); diff --git a/src/modules/graphics/Shader.h b/src/modules/graphics/Shader.h index 0e6db6cb8..85f30bd35 100644 --- a/src/modules/graphics/Shader.h +++ b/src/modules/graphics/Shader.h @@ -64,15 +64,16 @@ public: // Built-in uniform variables. enum BuiltinUniform { - BUILTIN_TRANSFORM_MATRIX = 0, - BUILTIN_PROJECTION_MATRIX, - BUILTIN_TRANSFORM_PROJECTION_MATRIX, - BUILTIN_NORMAL_MATRIX, + BUILTIN_TEXTURE_MAIN, + BUILTIN_TEXTURE_VIDEO_Y, + BUILTIN_TEXTURE_VIDEO_CB, + BUILTIN_TEXTURE_VIDEO_CR, + BUILTIN_MATRIX_TRANSFORM, + BUILTIN_MATRIX_PROJECTION, + BUILTIN_MATRIX_TRANSFORM_PROJECTION, + BUILTIN_MATRIX_NORMAL, BUILTIN_POINT_SIZE, BUILTIN_SCREEN_SIZE, - BUILTIN_VIDEO_Y_CHANNEL, - BUILTIN_VIDEO_CB_CHANNEL, - BUILTIN_VIDEO_CR_CHANNEL, BUILTIN_MAX_ENUM }; @@ -181,9 +182,6 @@ public: static bool getConstant(const char *in, ShaderStage &out); static bool getConstant(ShaderStage in, const char *&out); - static bool getConstant(const char *in, VertexAttribID &out); - static bool getConstant(VertexAttribID in, const char *&out); - static bool getConstant(const char *in, BuiltinUniform &out); static bool getConstant(BuiltinUniform in, const char *&out); @@ -200,10 +198,6 @@ private: static StringMap::Entry stageNameEntries[]; static StringMap stageNames; - // Names for the generic vertex attributes used by love. - static StringMap::Entry attribNameEntries[]; - static StringMap attribNames; - // Names for the built-in uniform variables. static StringMap::Entry builtinNameEntries[]; static StringMap builtinNames; diff --git a/src/modules/graphics/opengl/Mesh.cpp b/src/modules/graphics/opengl/Mesh.cpp index de37e512a..01b45d6cf 100644 --- a/src/modules/graphics/opengl/Mesh.cpp +++ b/src/modules/graphics/opengl/Mesh.cpp @@ -39,7 +39,7 @@ namespace opengl static const char *getBuiltinAttribName(VertexAttribID attribid) { const char *name = ""; - Shader::getConstant(attribid, name); + vertex::getConstant(attribid, name); return name; } @@ -578,7 +578,7 @@ int Mesh::bindAttributeToShaderInput(int attributeindex, const std::string &inpu // If the attribute is one of the LOVE-defined ones, use the constant // attribute index for it, otherwise query the index from the shader. VertexAttribID builtinattrib; - if (Shader::getConstant(inputname.c_str(), builtinattrib)) + if (vertex::getConstant(inputname.c_str(), builtinattrib)) attriblocation = (GLint) builtinattrib; else if (Shader::current) attriblocation = ((Shader *) Shader::current)->getAttribLocation(inputname); diff --git a/src/modules/graphics/opengl/Shader.cpp b/src/modules/graphics/opengl/Shader.cpp index 753b54348..64738ac8a 100644 --- a/src/modules/graphics/opengl/Shader.cpp +++ b/src/modules/graphics/opengl/Shader.cpp @@ -392,7 +392,7 @@ bool Shader::loadVolatile() for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++) { const char *name = nullptr; - if (getConstant((VertexAttribID) i, name)) + if (vertex::getConstant((VertexAttribID) i, name)) glBindAttribLocation(program, i, (const GLchar *) name); } @@ -419,7 +419,7 @@ bool Shader::loadVolatile() for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++) { const char *name = nullptr; - if (getConstant(VertexAttribID(i), name)) + if (vertex::getConstant(VertexAttribID(i), name)) builtinAttributes[i] = glGetAttribLocation(program, name); else builtinAttributes[i] = -1; @@ -745,9 +745,9 @@ void Shader::setVideoTextures(ptrdiff_t ytexture, ptrdiff_t cbtexture, ptrdiff_t if (videoTextureUnits[0] == 0) { const BuiltinUniform builtins[3] = { - BUILTIN_VIDEO_Y_CHANNEL, - BUILTIN_VIDEO_CB_CHANNEL, - BUILTIN_VIDEO_CR_CHANNEL, + BUILTIN_TEXTURE_VIDEO_Y, + BUILTIN_TEXTURE_VIDEO_CB, + BUILTIN_TEXTURE_VIDEO_CR, }; for (int i = 0; i < 3; i++) @@ -857,14 +857,14 @@ void Shader::updateBuiltinUniforms() // Only upload the matrices if they've changed. if (memcmp(curxform.getElements(), lastTransformMatrix.getElements(), sizeof(float) * 16) != 0) { - GLint location = builtinUniforms[BUILTIN_TRANSFORM_MATRIX]; + GLint location = builtinUniforms[BUILTIN_MATRIX_TRANSFORM]; if (location >= 0) glUniformMatrix4fv(location, 1, GL_FALSE, curxform.getElements()); // Also upload the re-calculated normal matrix, if possible. The normal // matrix is the transpose of the inverse of the rotation portion // (top-left 3x3) of the transform matrix. - location = builtinUniforms[BUILTIN_NORMAL_MATRIX]; + location = builtinUniforms[BUILTIN_MATRIX_NORMAL]; if (location >= 0) { Matrix3 normalmatrix = Matrix3(curxform).transposedInverse(); @@ -877,7 +877,7 @@ void Shader::updateBuiltinUniforms() if (memcmp(curproj.getElements(), lastProjectionMatrix.getElements(), sizeof(float) * 16) != 0) { - GLint location = builtinUniforms[BUILTIN_PROJECTION_MATRIX]; + GLint location = builtinUniforms[BUILTIN_MATRIX_PROJECTION]; if (location >= 0) glUniformMatrix4fv(location, 1, GL_FALSE, curproj.getElements()); @@ -887,7 +887,7 @@ void Shader::updateBuiltinUniforms() if (tpmatrixneedsupdate) { - GLint location = builtinUniforms[BUILTIN_TRANSFORM_PROJECTION_MATRIX]; + GLint location = builtinUniforms[BUILTIN_MATRIX_TRANSFORM_PROJECTION]; if (location >= 0) { Matrix4 tp_matrix(curproj, curxform); diff --git a/src/modules/graphics/opengl/wrap_Graphics.lua b/src/modules/graphics/opengl/wrap_Graphics.lua index 80921003b..eca9a4c6d 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.lua +++ b/src/modules/graphics/opengl/wrap_Graphics.lua @@ -42,15 +42,14 @@ GLSL.SYNTAX = [[ #endif #define number float #define Image sampler2D -#define extern uniform -#pragma optionNV(strict on)]] +#define extern uniform]] -- Uniforms shared by the vertex and pixel shader stages. GLSL.UNIFORMS = [[ // According to the GLSL ES 1.0 spec, uniform precision must match between stages, // but we can't guarantee that highp is always supported in fragment shaders... // We *really* don't want to use mediump for these in vertex shaders though. -#if defined(VERTEX) || defined(GL_FRAGMENT_PRECISION_HIGH) +#if defined(VERTEX) || __VERSION__ > 100 || defined(GL_FRAGMENT_PRECISION_HIGH) #define LOVE_UNIFORM_PRECISION highp #else #define LOVE_UNIFORM_PRECISION mediump @@ -137,6 +136,8 @@ GLSL.VERTEX = { HEADER = [[ #define LOVE_PRECISE_GAMMA +#define love_Position gl_Position + #if __VERSION__ >= 130 #define attribute in #define varying out @@ -148,26 +149,31 @@ GLSL.VERTEX = { #ifdef GL_ES uniform mediump float love_PointSize; -#endif +#endif]], + FUNCTIONS = [[ +void updatePointSize() { +#ifdef GL_ES + gl_PointSize = love_PointSize; +#endif +}]], + + MAIN = [[ attribute vec4 VertexPosition; attribute vec4 VertexTexCoord; attribute vec4 VertexColor; attribute vec4 ConstantColor; varying vec4 VaryingTexCoord; -varying vec4 VaryingColor;]], +varying vec4 VaryingColor; - FUNCTIONS = "", +vec4 position(mat4 transform_proj, vec4 vertpos); - FOOTER = [[ void main() { VaryingTexCoord = VertexTexCoord; VaryingColor = gammaCorrectColor(VertexColor) * ConstantColor; -#ifdef GL_ES - gl_PointSize = love_PointSize; -#endif - gl_Position = position(TransformProjectionMatrix, VertexPosition); + updatePointSize(); + love_Position = position(TransformProjectionMatrix, VertexPosition); }]], } @@ -195,10 +201,7 @@ GLSL.PIXEL = { #endif // See Shader::checkSetScreenParams in Shader.cpp. -#define love_PixelCoord (vec2(gl_FragCoord.x, (gl_FragCoord.y * love_ScreenSize.z) + love_ScreenSize.w)) - -varying mediump vec4 VaryingTexCoord; -varying mediump vec4 VaryingColor;]], +#define love_PixelCoord (vec2(gl_FragCoord.x, (gl_FragCoord.y * love_ScreenSize.z) + love_ScreenSize.w))]], FUNCTIONS = [[ uniform sampler2D love_VideoYChannel; @@ -221,14 +224,24 @@ vec4 VideoTexel(vec2 texcoords) { return gammaCorrectColor(color); }]], - FOOTER = [[ + MAIN = [[ uniform sampler2D MainTex; +varying mediump vec4 VaryingTexCoord; +varying mediump vec4 VaryingColor; + +vec4 effect(vec4 vcolor, Image tex, vec2 texcoord, vec2 pixcoord); + void main() { love_PixelColor = effect(VaryingColor, MainTex, VaryingTexCoord.st, love_PixelCoord); }]], - FOOTER_MULTI_CANVAS = [[ + MAIN_MULTI_CANVAS = [[ uniform sampler2D MainTex; +varying mediump vec4 VaryingTexCoord; +varying mediump vec4 VaryingColor; + +void effects(vec4 vcolor, Image tex, vec2 texcoord, vec2 pixcoord); + void main() { effects(VaryingColor, MainTex, VaryingTexCoord.st, love_PixelCoord); }]], @@ -252,9 +265,9 @@ local function createShaderStageCode(stage, code, lang, gles, glsl1on3, gammacor GLSL.UNIFORMS, GLSL.FUNCTIONS, GLSL[stage].FUNCTIONS, + multicanvas and GLSL[stage].MAIN_MULTI_CANVAS or GLSL[stage].MAIN, (lang == "glsl3" or gles) and "#line 1" or "#line 0", code, - multicanvas and GLSL[stage].FOOTER_MULTI_CANVAS or GLSL[stage].FOOTER, } return table_concat(lines, "\n") end @@ -368,11 +381,11 @@ vec4 position(mat4 transform_proj, vec4 vertpos) { return transform_proj * vertpos; }]], pixel = [[ -vec4 effect(mediump vec4 vcolor, Image tex, vec2 texcoord, vec2 pixcoord) { +vec4 effect(vec4 vcolor, Image tex, vec2 texcoord, vec2 pixcoord) { return Texel(tex, texcoord) * vcolor; }]], videopixel = [[ -vec4 effect(mediump vec4 vcolor, Image tex, vec2 texcoord, vec2 pixcoord) { +vec4 effect(vec4 vcolor, Image tex, vec2 texcoord, vec2 pixcoord) { return VideoTexel(texcoord) * vcolor; }]], } diff --git a/src/modules/graphics/vertex.cpp b/src/modules/graphics/vertex.cpp index 96b2c2039..65744b28e 100644 --- a/src/modules/graphics/vertex.cpp +++ b/src/modules/graphics/vertex.cpp @@ -143,6 +143,16 @@ void fillIndices(TriangleIndexMode mode, uint32 vertexStart, uint32 vertexCount, fillIndicesT(mode, vertexStart, vertexCount, indices); } +static StringMap::Entry attribNameEntries[] = +{ + { "VertexPosition", ATTRIB_POS }, + { "VertexTexCoord", ATTRIB_TEXCOORD }, + { "VertexColor", ATTRIB_COLOR }, + { "ConstantColor", ATTRIB_CONSTANTCOLOR }, +}; + +static StringMap attribNames(attribNameEntries, sizeof(attribNameEntries)); + static StringMap::Entry indexTypeEntries[] = { { "uint16", INDEX_UINT16 }, @@ -160,6 +170,16 @@ static StringMap::Entry usageEntries[] = static StringMap usages(usageEntries, sizeof(usageEntries)); +bool getConstant(const char *in, VertexAttribID &out) +{ + return attribNames.find(in, out); +} + +bool getConstant(VertexAttribID in, const char *&out) +{ + return attribNames.find(in, out); +} + bool getConstant(const char *in, IndexDataType &out) { return indexTypes.find(in, out); diff --git a/src/modules/graphics/vertex.h b/src/modules/graphics/vertex.h index eba88b6f3..62d12fbed 100644 --- a/src/modules/graphics/vertex.h +++ b/src/modules/graphics/vertex.h @@ -129,6 +129,9 @@ int getIndexCount(TriangleIndexMode mode, int vertexCount); void fillIndices(TriangleIndexMode mode, uint16 vertexStart, uint16 vertexCount, uint16 *indices); void fillIndices(TriangleIndexMode mode, uint32 vertexStart, uint32 vertexCount, uint32 *indices); +bool getConstant(const char *in, VertexAttribID &out); +bool getConstant(VertexAttribID in, const char *&out); + bool getConstant(const char *in, IndexDataType &out); bool getConstant(IndexDataType in, const char *&out);