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
+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;
}]],
}