3292: Some shader refactoring r=def- a=Jupeyy

Small refactoring for GL 3.3+ shaders.
Removes "if" checks where unneeded or CPU can calculate the result faster.

Also renames some uniforms to be consistent within all shaders.

## Checklist

- [x] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Jupeyy <jupjopjap@gmail.com>
This commit is contained in:
bors[bot] 2020-11-11 17:32:04 +00:00 committed by GitHub
commit bddb02e34d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 164 additions and 154 deletions

View file

@ -1,5 +1,4 @@
uniform int isTextured; uniform sampler2D gTextureSampler;
uniform sampler2D textureSampler;
noperspective in vec2 texCoord; noperspective in vec2 texCoord;
noperspective in vec4 vertColor; noperspective in vec4 vertColor;
@ -7,10 +6,10 @@ noperspective in vec4 vertColor;
out vec4 FragClr; out vec4 FragClr;
void main() void main()
{ {
if(isTextured == 1) #ifdef TW_TEXTURED
{ vec4 tex = texture(gTextureSampler, texCoord);
vec4 tex = texture(textureSampler, texCoord);
FragClr = tex * vertColor; FragClr = tex * vertColor;
} #else
else FragClr = vertColor; FragClr = vertColor;
#endif
} }

View file

@ -2,14 +2,14 @@ layout (location = 0) in vec2 inVertex;
layout (location = 1) in vec2 inVertexTexCoord; layout (location = 1) in vec2 inVertexTexCoord;
layout (location = 2) in vec4 inVertexColor; layout (location = 2) in vec4 inVertexColor;
uniform mat4x2 Pos; uniform mat4x2 gPos;
noperspective out vec2 texCoord; noperspective out vec2 texCoord;
noperspective out vec4 vertColor; noperspective out vec4 vertColor;
void main() void main()
{ {
gl_Position = vec4(Pos * vec4(inVertex, 0.0, 1.0), 0.0, 1.0); gl_Position = vec4(gPos * vec4(inVertex, 0.0, 1.0), 0.0, 1.0);
texCoord = inVertexTexCoord; texCoord = inVertexTexCoord;
vertColor = inVertexColor; vertColor = inVertexColor;
} }

View file

@ -4,7 +4,9 @@ layout (location = 2) in vec4 inVertexColor;
uniform mat4x2 gPos; uniform mat4x2 gPos;
#ifndef TW_ROTATIONLESS
uniform float gRotation; uniform float gRotation;
#endif
uniform vec2 gCenter; uniform vec2 gCenter;
noperspective out vec2 texCoord; noperspective out vec2 texCoord;
@ -13,14 +15,13 @@ noperspective out vec4 vertColor;
void main() void main()
{ {
vec2 FinalPos = vec2(inVertex.xy); vec2 FinalPos = vec2(inVertex.xy);
if(gRotation != 0.0) #ifndef TW_ROTATIONLESS
{
float X = FinalPos.x - gCenter.x; float X = FinalPos.x - gCenter.x;
float Y = FinalPos.y - gCenter.y; float Y = FinalPos.y - gCenter.y;
FinalPos.x = X * cos(gRotation) - Y * sin(gRotation) + gCenter.x; FinalPos.x = X * cos(gRotation) - Y * sin(gRotation) + gCenter.x;
FinalPos.y = X * sin(gRotation) + Y * cos(gRotation) + gCenter.y; FinalPos.y = X * sin(gRotation) + Y * cos(gRotation) + gCenter.y;
} #endif
gl_Position = vec4(gPos * vec4(FinalPos, 0.0, 1.0), 0.0, 1.0); gl_Position = vec4(gPos * vec4(FinalPos, 0.0, 1.0), 0.0, 1.0);
texCoord = inVertexTexCoord; texCoord = inVertexTexCoord;

View file

@ -1,6 +1,6 @@
uniform sampler2D textureSampler; uniform sampler2D gTextureSampler;
uniform vec4 VerticesColor; uniform vec4 gVerticesColor;
noperspective in vec2 texCoord; noperspective in vec2 texCoord;
noperspective in vec4 vertColor; noperspective in vec4 vertColor;
@ -8,6 +8,6 @@ noperspective in vec4 vertColor;
out vec4 FragClr; out vec4 FragClr;
void main() void main()
{ {
vec4 tex = texture(textureSampler, texCoord); vec4 tex = texture(gTextureSampler, texCoord);
FragClr = tex * vertColor * VerticesColor; FragClr = tex * vertColor * gVerticesColor;
} }

View file

@ -2,10 +2,10 @@ layout (location = 0) in vec2 inVertex;
layout (location = 1) in vec2 inVertexTexCoord; layout (location = 1) in vec2 inVertexTexCoord;
layout (location = 2) in vec4 inVertexColor; layout (location = 2) in vec4 inVertexColor;
uniform mat4x2 Pos; uniform mat4x2 gPos;
uniform vec4 RSP[228]; uniform vec4 gRSP[228];
uniform vec2 Center; uniform vec2 gCenter;
noperspective out vec2 texCoord; noperspective out vec2 texCoord;
noperspective out vec4 vertColor; noperspective out vec4 vertColor;
@ -13,22 +13,22 @@ noperspective out vec4 vertColor;
void main() void main()
{ {
vec2 FinalPos = vec2(inVertex.xy); vec2 FinalPos = vec2(inVertex.xy);
if(RSP[gl_InstanceID].w != 0.0) if(gRSP[gl_InstanceID].w != 0.0)
{ {
float X = FinalPos.x - Center.x; float X = FinalPos.x - gCenter.x;
float Y = FinalPos.y - Center.y; float Y = FinalPos.y - gCenter.y;
FinalPos.x = X * cos(RSP[gl_InstanceID].w) - Y * sin(RSP[gl_InstanceID].w) + Center.x; FinalPos.x = X * cos(gRSP[gl_InstanceID].w) - Y * sin(gRSP[gl_InstanceID].w) + gCenter.x;
FinalPos.y = X * sin(RSP[gl_InstanceID].w) + Y * cos(RSP[gl_InstanceID].w) + Center.y; FinalPos.y = X * sin(gRSP[gl_InstanceID].w) + Y * cos(gRSP[gl_InstanceID].w) + gCenter.y;
} }
FinalPos.x *= RSP[gl_InstanceID].z; FinalPos.x *= gRSP[gl_InstanceID].z;
FinalPos.y *= RSP[gl_InstanceID].z; FinalPos.y *= gRSP[gl_InstanceID].z;
FinalPos.x += RSP[gl_InstanceID].x; FinalPos.x += gRSP[gl_InstanceID].x;
FinalPos.y += RSP[gl_InstanceID].y; FinalPos.y += gRSP[gl_InstanceID].y;
gl_Position = vec4(Pos * vec4(FinalPos, 0.0, 1.0), 0.0, 1.0); gl_Position = vec4(gPos * vec4(FinalPos, 0.0, 1.0), 0.0, 1.0);
texCoord = inVertexTexCoord; texCoord = inVertexTexCoord;
vertColor = inVertexColor; vertColor = inVertexColor;
} }

View file

@ -1,8 +1,8 @@
uniform sampler2D textSampler; uniform sampler2D gTextSampler;
uniform sampler2D textOutlineSampler; uniform sampler2D gTextOutlineSampler;
uniform vec4 vertColor; uniform vec4 gVertColor;
uniform vec4 vertOutlineColor; uniform vec4 gVertOutlineColor;
noperspective in vec2 texCoord; noperspective in vec2 texCoord;
noperspective in vec4 outVertColor; noperspective in vec4 outVertColor;
@ -10,8 +10,8 @@ noperspective in vec4 outVertColor;
out vec4 FragClr; out vec4 FragClr;
void main() void main()
{ {
vec4 textColor = vertColor * outVertColor * texture(textSampler, texCoord); vec4 textColor = gVertColor * outVertColor * texture(gTextSampler, texCoord);
vec4 textOutlineTex = vertOutlineColor * texture(textOutlineSampler, texCoord); vec4 textOutlineTex = gVertOutlineColor * texture(gTextOutlineSampler, texCoord);
// ratio between the two textures // ratio between the two textures
float OutlineBlend = (1.0 - textColor.a); float OutlineBlend = (1.0 - textColor.a);
@ -29,10 +29,6 @@ void main()
float RealAlpha = (textOutlineFrag.a + textColor.a); float RealAlpha = (textOutlineFrag.a + textColor.a);
// discard transparent fragments
if(RealAlpha == 0.0)
discard;
// simply add the color we will loose through blending // simply add the color we will loose through blending
FragClr = vec4(finalFragColor / RealAlpha, RealAlpha); FragClr = vec4(finalFragColor / RealAlpha, RealAlpha);
} }

View file

@ -2,16 +2,16 @@ layout (location = 0) in vec2 inVertex;
layout (location = 1) in vec2 inVertexTexCoord; layout (location = 1) in vec2 inVertexTexCoord;
layout (location = 2) in vec4 inVertexColor; layout (location = 2) in vec4 inVertexColor;
uniform mat4x2 Pos; uniform mat4x2 gPos;
uniform float textureSize; uniform float gTextureSize;
noperspective out vec2 texCoord; noperspective out vec2 texCoord;
noperspective out vec4 outVertColor; noperspective out vec4 outVertColor;
void main() void main()
{ {
gl_Position = vec4(Pos * vec4(inVertex, 0.0, 1.0), 0.0, 1.0); gl_Position = vec4(gPos * vec4(inVertex, 0.0, 1.0), 0.0, 1.0);
texCoord = vec2(inVertexTexCoord.x / textureSize, inVertexTexCoord.y / textureSize); texCoord = vec2(inVertexTexCoord.x / gTextureSize, inVertexTexCoord.y / gTextureSize);
outVertColor = inVertexColor; outVertColor = inVertexColor;
} }

View file

@ -335,6 +335,11 @@ void *CCommandProcessorFragment_OpenGL::Resize(int Width, int Height, int NewWid
return pTmpData; return pTmpData;
} }
bool CCommandProcessorFragment_OpenGL::IsTexturedState(const CCommandBuffer::SState &State)
{
return State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES;
}
void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &State, bool Use2DArrayTextures) void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &State, bool Use2DArrayTextures)
{ {
// blend // blend
@ -387,7 +392,7 @@ void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &St
} }
// texture // texture
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(State))
{ {
if(!Use2DArrayTextures) if(!Use2DArrayTextures)
{ {
@ -1009,7 +1014,7 @@ void CCommandProcessorFragment_OpenGL2::SetState(const CCommandBuffer::SState &S
} }
// texture // texture
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(State))
{ {
int Slot = 0; int Slot = 0;
if(m_UseMultipleTextureUnits) if(m_UseMultipleTextureUnits)
@ -1063,14 +1068,6 @@ void CCommandProcessorFragment_OpenGL2::SetState(const CCommandBuffer::SState &S
} }
} }
} }
if(pProgram->m_LocIsTextured != -1)
{
if(pProgram->m_LastIsTextured != 1)
{
pProgram->SetUniform(pProgram->m_LocIsTextured, 1);
pProgram->m_LastIsTextured = 1;
}
}
if(pProgram->m_LastTextureSampler != Slot) if(pProgram->m_LastTextureSampler != Slot)
{ {
@ -1102,17 +1099,6 @@ void CCommandProcessorFragment_OpenGL2::SetState(const CCommandBuffer::SState &S
m_aTextures[State.m_Texture].m_LastWrapMode = State.m_WrapMode; m_aTextures[State.m_Texture].m_LastWrapMode = State.m_WrapMode;
} }
} }
else
{
if(pProgram->m_LocIsTextured != -1)
{
if(pProgram->m_LastIsTextured != 0)
{
pProgram->SetUniform(pProgram->m_LocIsTextured, 0);
pProgram->m_LastIsTextured = 0;
}
}
}
if(pProgram->m_LastScreen[0] != State.m_ScreenTL.x || pProgram->m_LastScreen[1] != State.m_ScreenTL.y || pProgram->m_LastScreen[2] != State.m_ScreenBR.x || pProgram->m_LastScreen[3] != State.m_ScreenBR.y) if(pProgram->m_LastScreen[0] != State.m_ScreenTL.x || pProgram->m_LastScreen[1] != State.m_ScreenTL.y || pProgram->m_LastScreen[2] != State.m_ScreenBR.x || pProgram->m_LastScreen[3] != State.m_ScreenBR.y)
{ {
@ -1616,7 +1602,7 @@ void CCommandProcessorFragment_OpenGL2::Cmd_RenderTex3D(const CCommandBuffer::SC
if(m_HasShaders) if(m_HasShaders)
{ {
CGLSLPrimitiveProgram *pProgram = NULL; CGLSLPrimitiveProgram *pProgram = NULL;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
{ {
pProgram = m_pPrimitive3DProgramTextured; pProgram = m_pPrimitive3DProgramTextured;
} }
@ -1861,7 +1847,7 @@ void CCommandProcessorFragment_OpenGL2::RenderBorderTileEmulation(SBufferContain
if(m_HasShaders) if(m_HasShaders)
{ {
CGLSLPrimitiveProgram *pProgram = NULL; CGLSLPrimitiveProgram *pProgram = NULL;
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(State))
{ {
pProgram = m_pPrimitive3DProgramTextured; pProgram = m_pPrimitive3DProgramTextured;
} }
@ -1946,7 +1932,7 @@ void CCommandProcessorFragment_OpenGL2::RenderBorderTileLineEmulation(SBufferCon
if(m_HasShaders) if(m_HasShaders)
{ {
CGLSLPrimitiveProgram *pProgram = NULL; CGLSLPrimitiveProgram *pProgram = NULL;
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(State))
{ {
pProgram = m_pPrimitive3DProgramTextured; pProgram = m_pPrimitive3DProgramTextured;
} }
@ -2065,7 +2051,7 @@ void CCommandProcessorFragment_OpenGL2::Cmd_RenderTileLayer(const CCommandBuffer
if(m_HasShaders) if(m_HasShaders)
{ {
CGLSLTileProgram *pProgram = NULL; CGLSLTileProgram *pProgram = NULL;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
{ {
pProgram = m_pTileProgramTextured; pProgram = m_pTileProgramTextured;
} }
@ -2192,6 +2178,37 @@ void CCommandProcessorFragment_OpenGL3_3::UseProgram(CGLSLTWProgram *pProgram)
} }
} }
void CCommandProcessorFragment_OpenGL3_3::InitPrimExProgram(CGLSLPrimitiveExProgram *pProgram, CGLSLCompiler *pCompiler, IStorage *pStorage, bool Textured, bool Rotationless)
{
CGLSL PrimitiveVertexShader;
CGLSL PrimitiveFragmentShader;
if(Textured)
pCompiler->AddDefine("TW_TEXTURED", "");
if(Rotationless)
pCompiler->AddDefine("TW_ROTATIONLESS", "");
PrimitiveVertexShader.LoadShader(pCompiler, pStorage, "shader/primex.vert", GL_VERTEX_SHADER);
PrimitiveFragmentShader.LoadShader(pCompiler, pStorage, "shader/primex.frag", GL_FRAGMENT_SHADER);
if(Textured || Rotationless)
pCompiler->ClearDefines();
pProgram->CreateProgram();
pProgram->AddShader(&PrimitiveVertexShader);
pProgram->AddShader(&PrimitiveFragmentShader);
pProgram->LinkProgram();
UseProgram(pProgram);
pProgram->m_LocPos = pProgram->GetUniformLoc("gPos");
pProgram->m_LocTextureSampler = pProgram->GetUniformLoc("gTextureSampler");
pProgram->m_LocRotation = pProgram->GetUniformLoc("gRotation");
pProgram->m_LocCenter = pProgram->GetUniformLoc("gCenter");
pProgram->m_LocVertciesColor = pProgram->GetUniformLoc("gVerticesColor");
pProgram->SetUniform(pProgram->m_LocRotation, 0.0f);
float Center[2] = {0.f, 0.f};
pProgram->SetUniformVec2(pProgram->m_LocCenter, 1, Center);
}
void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand) void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand)
{ {
m_OpenGLTextureLodBIAS = g_Config.m_GfxOpenGLTextureLODBIAS; m_OpenGLTextureLodBIAS = g_Config.m_GfxOpenGLTextureLODBIAS;
@ -2215,6 +2232,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_LastBlendMode = CCommandBuffer::BLEND_ALPHA; m_LastBlendMode = CCommandBuffer::BLEND_ALPHA;
m_LastClipEnable = false; m_LastClipEnable = false;
m_pPrimitiveProgram = new CGLSLPrimitiveProgram; m_pPrimitiveProgram = new CGLSLPrimitiveProgram;
m_pPrimitiveProgramTextured = new CGLSLPrimitiveProgram;
m_pTileProgram = new CGLSLTileProgram; m_pTileProgram = new CGLSLTileProgram;
m_pTileProgramTextured = new CGLSLTileProgram; m_pTileProgramTextured = new CGLSLTileProgram;
m_pPrimitive3DProgram = new CGLSLPrimitiveProgram; m_pPrimitive3DProgram = new CGLSLPrimitiveProgram;
@ -2228,6 +2246,8 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pTextProgram = new CGLSLTextProgram; m_pTextProgram = new CGLSLTextProgram;
m_pPrimitiveExProgram = new CGLSLPrimitiveExProgram; m_pPrimitiveExProgram = new CGLSLPrimitiveExProgram;
m_pPrimitiveExProgramTextured = new CGLSLPrimitiveExProgram; m_pPrimitiveExProgramTextured = new CGLSLPrimitiveExProgram;
m_pPrimitiveExProgramRotationless = new CGLSLPrimitiveExProgram;
m_pPrimitiveExProgramTexturedRotationless = new CGLSLPrimitiveExProgram;
m_pSpriteProgramMultiple = new CGLSLSpriteMultipleProgram; m_pSpriteProgramMultiple = new CGLSLSpriteMultipleProgram;
m_LastProgramID = 0; m_LastProgramID = 0;
@ -2251,9 +2271,26 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
UseProgram(m_pPrimitiveProgram); UseProgram(m_pPrimitiveProgram);
m_pPrimitiveProgram->m_LocPos = m_pPrimitiveProgram->GetUniformLoc("Pos"); m_pPrimitiveProgram->m_LocPos = m_pPrimitiveProgram->GetUniformLoc("gPos");
m_pPrimitiveProgram->m_LocIsTextured = m_pPrimitiveProgram->GetUniformLoc("isTextured"); m_pPrimitiveProgram->m_LocTextureSampler = m_pPrimitiveProgram->GetUniformLoc("gTextureSampler");
m_pPrimitiveProgram->m_LocTextureSampler = m_pPrimitiveProgram->GetUniformLoc("textureSampler"); }
{
CGLSL PrimitiveVertexShader;
CGLSL PrimitiveFragmentShader;
ShaderCompiler.AddDefine("TW_TEXTURED", "");
PrimitiveVertexShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/prim.vert", GL_VERTEX_SHADER);
PrimitiveFragmentShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/prim.frag", GL_FRAGMENT_SHADER);
ShaderCompiler.ClearDefines();
m_pPrimitiveProgramTextured->CreateProgram();
m_pPrimitiveProgramTextured->AddShader(&PrimitiveVertexShader);
m_pPrimitiveProgramTextured->AddShader(&PrimitiveFragmentShader);
m_pPrimitiveProgramTextured->LinkProgram();
UseProgram(m_pPrimitiveProgramTextured);
m_pPrimitiveProgramTextured->m_LocPos = m_pPrimitiveProgramTextured->GetUniformLoc("gPos");
m_pPrimitiveProgramTextured->m_LocTextureSampler = m_pPrimitiveProgramTextured->GetUniformLoc("gTextureSampler");
} }
{ {
@ -2471,65 +2508,18 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
UseProgram(m_pTextProgram); UseProgram(m_pTextProgram);
m_pTextProgram->m_LocPos = m_pTextProgram->GetUniformLoc("Pos"); m_pTextProgram->m_LocPos = m_pTextProgram->GetUniformLoc("gPos");
m_pTextProgram->m_LocIsTextured = -1;
m_pTextProgram->m_LocTextureSampler = -1; m_pTextProgram->m_LocTextureSampler = -1;
m_pTextProgram->m_LocTextSampler = m_pTextProgram->GetUniformLoc("textSampler"); m_pTextProgram->m_LocTextSampler = m_pTextProgram->GetUniformLoc("gTextSampler");
m_pTextProgram->m_LocTextOutlineSampler = m_pTextProgram->GetUniformLoc("textOutlineSampler"); m_pTextProgram->m_LocTextOutlineSampler = m_pTextProgram->GetUniformLoc("gTextOutlineSampler");
m_pTextProgram->m_LocColor = m_pTextProgram->GetUniformLoc("vertColor"); m_pTextProgram->m_LocColor = m_pTextProgram->GetUniformLoc("gVertColor");
m_pTextProgram->m_LocOutlineColor = m_pTextProgram->GetUniformLoc("vertOutlineColor"); m_pTextProgram->m_LocOutlineColor = m_pTextProgram->GetUniformLoc("gVertOutlineColor");
m_pTextProgram->m_LocTextureSize = m_pTextProgram->GetUniformLoc("textureSize"); m_pTextProgram->m_LocTextureSize = m_pTextProgram->GetUniformLoc("gTextureSize");
}
{
CGLSL PrimitiveVertexShader;
CGLSL PrimitiveFragmentShader;
PrimitiveVertexShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/primex.vert", GL_VERTEX_SHADER);
PrimitiveFragmentShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/primex.frag", GL_FRAGMENT_SHADER);
m_pPrimitiveExProgram->CreateProgram();
m_pPrimitiveExProgram->AddShader(&PrimitiveVertexShader);
m_pPrimitiveExProgram->AddShader(&PrimitiveFragmentShader);
m_pPrimitiveExProgram->LinkProgram();
UseProgram(m_pPrimitiveExProgram);
m_pPrimitiveExProgram->m_LocPos = m_pPrimitiveExProgram->GetUniformLoc("gPos");
m_pPrimitiveExProgram->m_LocIsTextured = -1;
m_pPrimitiveExProgram->m_LocTextureSampler = -1;
m_pPrimitiveExProgram->m_LocRotation = m_pPrimitiveExProgram->GetUniformLoc("gRotation");
m_pPrimitiveExProgram->m_LocCenter = m_pPrimitiveExProgram->GetUniformLoc("gCenter");
m_pPrimitiveExProgram->m_LocVertciesColor = m_pPrimitiveExProgram->GetUniformLoc("gVerticesColor");
m_pPrimitiveExProgram->SetUniform(m_pPrimitiveExProgram->m_LocRotation, 0.0f);
float Center[2] = {0.f, 0.f};
m_pPrimitiveExProgram->SetUniformVec2(m_pPrimitiveExProgram->m_LocCenter, 1, Center);
}
{
CGLSL PrimitiveVertexShader;
CGLSL PrimitiveFragmentShader;
ShaderCompiler.AddDefine("TW_TEXTURED", "");
PrimitiveVertexShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/primex.vert", GL_VERTEX_SHADER);
PrimitiveFragmentShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/primex.frag", GL_FRAGMENT_SHADER);
ShaderCompiler.ClearDefines();
m_pPrimitiveExProgramTextured->CreateProgram();
m_pPrimitiveExProgramTextured->AddShader(&PrimitiveVertexShader);
m_pPrimitiveExProgramTextured->AddShader(&PrimitiveFragmentShader);
m_pPrimitiveExProgramTextured->LinkProgram();
UseProgram(m_pPrimitiveExProgramTextured);
m_pPrimitiveExProgramTextured->m_LocPos = m_pPrimitiveExProgramTextured->GetUniformLoc("gPos");
m_pPrimitiveExProgramTextured->m_LocIsTextured = -1;
m_pPrimitiveExProgramTextured->m_LocTextureSampler = m_pPrimitiveExProgramTextured->GetUniformLoc("gTextureSampler");
m_pPrimitiveExProgramTextured->m_LocRotation = m_pPrimitiveExProgramTextured->GetUniformLoc("gRotation");
m_pPrimitiveExProgramTextured->m_LocCenter = m_pPrimitiveExProgramTextured->GetUniformLoc("gCenter");
m_pPrimitiveExProgramTextured->m_LocVertciesColor = m_pPrimitiveExProgramTextured->GetUniformLoc("gVerticesColor");
m_pPrimitiveExProgramTextured->SetUniform(m_pPrimitiveExProgramTextured->m_LocRotation, 0.0f);
float Center[2] = {0.f, 0.f};
m_pPrimitiveExProgramTextured->SetUniformVec2(m_pPrimitiveExProgramTextured->m_LocCenter, 1, Center);
} }
InitPrimExProgram(m_pPrimitiveExProgram, &ShaderCompiler, pCommand->m_pStorage, false, false);
InitPrimExProgram(m_pPrimitiveExProgramTextured, &ShaderCompiler, pCommand->m_pStorage, true, false);
InitPrimExProgram(m_pPrimitiveExProgramRotationless, &ShaderCompiler, pCommand->m_pStorage, false, true);
InitPrimExProgram(m_pPrimitiveExProgramTexturedRotationless, &ShaderCompiler, pCommand->m_pStorage, true, true);
{ {
CGLSL PrimitiveVertexShader; CGLSL PrimitiveVertexShader;
CGLSL PrimitiveFragmentShader; CGLSL PrimitiveFragmentShader;
@ -2543,12 +2533,11 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
UseProgram(m_pSpriteProgramMultiple); UseProgram(m_pSpriteProgramMultiple);
m_pSpriteProgramMultiple->m_LocPos = m_pSpriteProgramMultiple->GetUniformLoc("Pos"); m_pSpriteProgramMultiple->m_LocPos = m_pSpriteProgramMultiple->GetUniformLoc("gPos");
m_pSpriteProgramMultiple->m_LocIsTextured = -1; m_pSpriteProgramMultiple->m_LocTextureSampler = m_pSpriteProgramMultiple->GetUniformLoc("gTextureSampler");
m_pSpriteProgramMultiple->m_LocTextureSampler = m_pSpriteProgramMultiple->GetUniformLoc("textureSampler"); m_pSpriteProgramMultiple->m_LocRSP = m_pSpriteProgramMultiple->GetUniformLoc("gRSP[0]");
m_pSpriteProgramMultiple->m_LocRSP = m_pSpriteProgramMultiple->GetUniformLoc("RSP[0]"); m_pSpriteProgramMultiple->m_LocCenter = m_pSpriteProgramMultiple->GetUniformLoc("gCenter");
m_pSpriteProgramMultiple->m_LocCenter = m_pSpriteProgramMultiple->GetUniformLoc("Center"); m_pSpriteProgramMultiple->m_LocVertciesColor = m_pSpriteProgramMultiple->GetUniformLoc("gVerticesColor");
m_pSpriteProgramMultiple->m_LocVertciesColor = m_pSpriteProgramMultiple->GetUniformLoc("VerticesColor");
float Center[2] = {0.f, 0.f}; float Center[2] = {0.f, 0.f};
m_pSpriteProgramMultiple->SetUniformVec2(m_pSpriteProgramMultiple->m_LocCenter, 1, Center); m_pSpriteProgramMultiple->SetUniformVec2(m_pSpriteProgramMultiple->m_LocCenter, 1, Center);
@ -2639,6 +2628,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Shutdown(const SCommand_Shutdown *
glUseProgram(0); glUseProgram(0);
m_pPrimitiveProgram->DeleteProgram(); m_pPrimitiveProgram->DeleteProgram();
m_pPrimitiveProgramTextured->DeleteProgram();
m_pBorderTileProgram->DeleteProgram(); m_pBorderTileProgram->DeleteProgram();
m_pBorderTileProgramTextured->DeleteProgram(); m_pBorderTileProgramTextured->DeleteProgram();
m_pBorderTileLineProgram->DeleteProgram(); m_pBorderTileLineProgram->DeleteProgram();
@ -2652,10 +2642,13 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Shutdown(const SCommand_Shutdown *
m_pTextProgram->DeleteProgram(); m_pTextProgram->DeleteProgram();
m_pPrimitiveExProgram->DeleteProgram(); m_pPrimitiveExProgram->DeleteProgram();
m_pPrimitiveExProgramTextured->DeleteProgram(); m_pPrimitiveExProgramTextured->DeleteProgram();
m_pPrimitiveExProgramRotationless->DeleteProgram();
m_pPrimitiveExProgramTexturedRotationless->DeleteProgram();
m_pSpriteProgramMultiple->DeleteProgram(); m_pSpriteProgramMultiple->DeleteProgram();
//clean up everything //clean up everything
delete m_pPrimitiveProgram; delete m_pPrimitiveProgram;
delete m_pPrimitiveProgramTextured;
delete m_pBorderTileProgram; delete m_pBorderTileProgram;
delete m_pBorderTileProgramTextured; delete m_pBorderTileProgramTextured;
delete m_pBorderTileLineProgram; delete m_pBorderTileLineProgram;
@ -2669,6 +2662,8 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Shutdown(const SCommand_Shutdown *
delete m_pTextProgram; delete m_pTextProgram;
delete m_pPrimitiveExProgram; delete m_pPrimitiveExProgram;
delete m_pPrimitiveExProgramTextured; delete m_pPrimitiveExProgramTextured;
delete m_pPrimitiveExProgramRotationless;
delete m_pPrimitiveExProgramTexturedRotationless;
delete m_pSpriteProgramMultiple; delete m_pSpriteProgramMultiple;
glBindVertexArray(0); glBindVertexArray(0);
@ -2989,8 +2984,11 @@ void CCommandProcessorFragment_OpenGL3_3::UploadStreamBufferData(unsigned int Pr
void CCommandProcessorFragment_OpenGL3_3::Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand) void CCommandProcessorFragment_OpenGL3_3::Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand)
{ {
UseProgram(m_pPrimitiveProgram); CGLSLTWProgram *pProgram = m_pPrimitiveProgram;
SetState(pCommand->m_State, m_pPrimitiveProgram); if(IsTexturedState(pCommand->m_State))
pProgram = m_pPrimitiveProgramTextured;
UseProgram(pProgram);
SetState(pCommand->m_State, pProgram);
UploadStreamBufferData(pCommand->m_PrimType, pCommand->m_pVertices, sizeof(CCommandBuffer::SVertex), pCommand->m_PrimCount); UploadStreamBufferData(pCommand->m_PrimType, pCommand->m_pVertices, sizeof(CCommandBuffer::SVertex), pCommand->m_PrimCount);
@ -3023,7 +3021,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Render(const CCommandBuffer::SComm
void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderTex3D(const CCommandBuffer::SCommand_RenderTex3D *pCommand) void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderTex3D(const CCommandBuffer::SCommand_RenderTex3D *pCommand)
{ {
CGLSLPrimitiveProgram *pProg = m_pPrimitive3DProgram; CGLSLPrimitiveProgram *pProg = m_pPrimitive3DProgram;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
pProg = m_pPrimitive3DProgramTextured; pProg = m_pPrimitive3DProgramTextured;
UseProgram(pProg); UseProgram(pProg);
SetState(pCommand->m_State, pProg, true); SetState(pCommand->m_State, pProg, true);
@ -3315,7 +3313,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderBorderTile(const CCommandBuf
return; return;
CGLSLTileProgram *pProgram = NULL; CGLSLTileProgram *pProgram = NULL;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
{ {
pProgram = m_pBorderTileProgramTextured; pProgram = m_pBorderTileProgramTextured;
} }
@ -3351,7 +3349,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderBorderTileLine(const CComman
return; return;
CGLSLTileProgram *pProgram = NULL; CGLSLTileProgram *pProgram = NULL;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
{ {
pProgram = m_pBorderTileLineProgramTextured; pProgram = m_pBorderTileLineProgramTextured;
} }
@ -3390,7 +3388,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderTileLayer(const CCommandBuff
} }
CGLSLTileProgram *pProgram = NULL; CGLSLTileProgram *pProgram = NULL;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
{ {
pProgram = m_pTileProgramTextured; pProgram = m_pTileProgramTextured;
} }
@ -3431,7 +3429,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderQuadLayer(const CCommandBuff
} }
CGLSLQuadProgram *pProgram = NULL; CGLSLQuadProgram *pProgram = NULL;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
{ {
pProgram = m_pQuadProgramTextured; pProgram = m_pQuadProgramTextured;
} }
@ -3628,8 +3626,11 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderQuadContainer(const CCommand
BufferContainer.m_LastIndexBufferBound = m_QuadDrawIndexBufferID; BufferContainer.m_LastIndexBufferBound = m_QuadDrawIndexBufferID;
} }
UseProgram(m_pPrimitiveProgram); CGLSLTWProgram *pProgram = m_pPrimitiveProgram;
SetState(pCommand->m_State, m_pPrimitiveProgram); if(IsTexturedState(pCommand->m_State))
pProgram = m_pPrimitiveProgramTextured;
UseProgram(pProgram);
SetState(pCommand->m_State, pProgram);
glDrawElements(GL_TRIANGLES, pCommand->m_DrawNum, GL_UNSIGNED_INT, pCommand->m_pOffset); glDrawElements(GL_TRIANGLES, pCommand->m_DrawNum, GL_UNSIGNED_INT, pCommand->m_pOffset);
} }
@ -3657,10 +3658,18 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderQuadContainerEx(const CComma
BufferContainer.m_LastIndexBufferBound = m_QuadDrawIndexBufferID; BufferContainer.m_LastIndexBufferBound = m_QuadDrawIndexBufferID;
} }
CGLSLPrimitiveExProgram *pProgram = m_pPrimitiveExProgram; CGLSLPrimitiveExProgram *pProgram = m_pPrimitiveExProgramRotationless;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES) if(IsTexturedState(pCommand->m_State))
{ {
if(pCommand->m_Rotation != 0.0f)
pProgram = m_pPrimitiveExProgramTextured; pProgram = m_pPrimitiveExProgramTextured;
else
pProgram = m_pPrimitiveExProgramTexturedRotationless;
}
else
{
if(pCommand->m_Rotation != 0.0f)
pProgram = m_pPrimitiveExProgram;
} }
UseProgram(pProgram); UseProgram(pProgram);

View file

@ -170,6 +170,7 @@ public:
}; };
protected: protected:
bool IsTexturedState(const CCommandBuffer::SState &State);
void SetState(const CCommandBuffer::SState &State, bool Use2DArrayTexture = false); void SetState(const CCommandBuffer::SState &State, bool Use2DArrayTexture = false);
virtual bool IsNewApi() { return false; } virtual bool IsNewApi() { return false; }
void DestroyTexture(int Slot); void DestroyTexture(int Slot);
@ -310,6 +311,7 @@ class CCommandProcessorFragment_OpenGL3_3 : public CCommandProcessorFragment_Ope
static const int m_MaxQuadsPossible = 256; static const int m_MaxQuadsPossible = 256;
CGLSLPrimitiveProgram *m_pPrimitiveProgram; CGLSLPrimitiveProgram *m_pPrimitiveProgram;
CGLSLPrimitiveProgram *m_pPrimitiveProgramTextured;
CGLSLTileProgram *m_pBorderTileProgram; CGLSLTileProgram *m_pBorderTileProgram;
CGLSLTileProgram *m_pBorderTileProgramTextured; CGLSLTileProgram *m_pBorderTileProgramTextured;
CGLSLTileProgram *m_pBorderTileLineProgram; CGLSLTileProgram *m_pBorderTileLineProgram;
@ -319,6 +321,8 @@ class CCommandProcessorFragment_OpenGL3_3 : public CCommandProcessorFragment_Ope
CGLSLTextProgram *m_pTextProgram; CGLSLTextProgram *m_pTextProgram;
CGLSLPrimitiveExProgram *m_pPrimitiveExProgram; CGLSLPrimitiveExProgram *m_pPrimitiveExProgram;
CGLSLPrimitiveExProgram *m_pPrimitiveExProgramTextured; CGLSLPrimitiveExProgram *m_pPrimitiveExProgramTextured;
CGLSLPrimitiveExProgram *m_pPrimitiveExProgramRotationless;
CGLSLPrimitiveExProgram *m_pPrimitiveExProgramTexturedRotationless;
CGLSLSpriteMultipleProgram *m_pSpriteProgramMultiple; CGLSLSpriteMultipleProgram *m_pSpriteProgramMultiple;
GLuint m_LastProgramID; GLuint m_LastProgramID;
@ -353,6 +357,8 @@ class CCommandProcessorFragment_OpenGL3_3 : public CCommandProcessorFragment_Ope
CCommandBuffer::SColorf m_ClearColor; CCommandBuffer::SColorf m_ClearColor;
void InitPrimExProgram(CGLSLPrimitiveExProgram *pProgram, class CGLSLCompiler *pCompiler, class IStorage *pStorage, bool Textured, bool Rotationless);
protected: protected:
static int TexFormatToNewOpenGLFormat(int TexFormat); static int TexFormatToNewOpenGLFormat(int TexFormat);
bool IsNewApi() override { return true; } bool IsNewApi() override { return true; }

View file

@ -45,13 +45,12 @@ class CGLSLTWProgram : public CGLSLProgram
{ {
public: public:
CGLSLTWProgram() : CGLSLTWProgram() :
m_LocPos(-1), m_LocIsTextured(-1), m_LocTextureSampler(-1), m_LastTextureSampler(-1), m_LastIsTextured(-1) m_LocPos(-1), m_LocTextureSampler(-1), m_LastTextureSampler(-1), m_LastIsTextured(-1)
{ {
m_LastScreen[0] = m_LastScreen[1] = m_LastScreen[2] = m_LastScreen[3] = -1.f; m_LastScreen[0] = m_LastScreen[1] = m_LastScreen[2] = m_LastScreen[3] = -1.f;
} }
int m_LocPos; int m_LocPos;
int m_LocIsTextured;
int m_LocTextureSampler; int m_LocTextureSampler;
int m_LastTextureSampler; int m_LastTextureSampler;