Shader code cleanup

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-01-27 23:19:04 -04:00
parent e360346c21
commit 470098b7e6
7 changed files with 91 additions and 80 deletions
+16 -35
View File
@@ -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<Shader::Language, Shader::LANGUAGE_MAX_ENUM>::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::Language, Shader::LANGUAGE_MAX_ENUM> Shader::languages(Shader::languageEntries, sizeof(Shader::languageEntries));
StringMap<Shader::ShaderStage, Shader::STAGE_MAX_ENUM>::Entry Shader::stageNameEntries[] =
{
{"vertex", Shader::STAGE_VERTEX},
{"pixel", Shader::STAGE_PIXEL},
{ "vertex", STAGE_VERTEX },
{ "pixel", STAGE_PIXEL },
};
StringMap<Shader::ShaderStage, Shader::STAGE_MAX_ENUM> Shader::stageNames(Shader::stageNameEntries, sizeof(Shader::stageNameEntries));
StringMap<VertexAttribID, ATTRIB_MAX_ENUM>::Entry Shader::attribNameEntries[] =
{
{"VertexPosition", ATTRIB_POS},
{"VertexTexCoord", ATTRIB_TEXCOORD},
{"VertexColor", ATTRIB_COLOR},
{"ConstantColor", ATTRIB_CONSTANTCOLOR},
};
StringMap<VertexAttribID, ATTRIB_MAX_ENUM> Shader::attribNames(Shader::attribNameEntries, sizeof(Shader::attribNameEntries));
StringMap<Shader::BuiltinUniform, Shader::BUILTIN_MAX_ENUM>::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::BuiltinUniform, Shader::BUILTIN_MAX_ENUM> Shader::builtinNames(Shader::builtinNameEntries, sizeof(Shader::builtinNameEntries));
+8 -14
View File
@@ -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<ShaderStage, STAGE_MAX_ENUM>::Entry stageNameEntries[];
static StringMap<ShaderStage, STAGE_MAX_ENUM> stageNames;
// Names for the generic vertex attributes used by love.
static StringMap<VertexAttribID, ATTRIB_MAX_ENUM>::Entry attribNameEntries[];
static StringMap<VertexAttribID, ATTRIB_MAX_ENUM> attribNames;
// Names for the built-in uniform variables.
static StringMap<BuiltinUniform, BUILTIN_MAX_ENUM>::Entry builtinNameEntries[];
static StringMap<BuiltinUniform, BUILTIN_MAX_ENUM> builtinNames;
+2 -2
View File
@@ -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);
+9 -9
View File
@@ -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);
+33 -20
View File
@@ -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;
}]],
}
+20
View File
@@ -143,6 +143,16 @@ void fillIndices(TriangleIndexMode mode, uint32 vertexStart, uint32 vertexCount,
fillIndicesT(mode, vertexStart, vertexCount, indices);
}
static StringMap<VertexAttribID, ATTRIB_MAX_ENUM>::Entry attribNameEntries[] =
{
{ "VertexPosition", ATTRIB_POS },
{ "VertexTexCoord", ATTRIB_TEXCOORD },
{ "VertexColor", ATTRIB_COLOR },
{ "ConstantColor", ATTRIB_CONSTANTCOLOR },
};
static StringMap<VertexAttribID, ATTRIB_MAX_ENUM> attribNames(attribNameEntries, sizeof(attribNameEntries));
static StringMap<IndexDataType, INDEX_MAX_ENUM>::Entry indexTypeEntries[] =
{
{ "uint16", INDEX_UINT16 },
@@ -160,6 +170,16 @@ static StringMap<Usage, USAGE_MAX_ENUM>::Entry usageEntries[] =
static StringMap<Usage, USAGE_MAX_ENUM> 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);
+3
View File
@@ -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);