3159: Rename QuadContainerAsSprite to QuadContainerEx r=def- a=Jupeyy

Only affects GL 3.3+

Just a rename and a bit of shader cleanup, so its more useful for the future, bcs in #3113 i saw, that there was no possibility to change quad vertices' color at runtime in GL 3.3

Co-authored-by: Jupeyy <jupjopjap@gmail.com>
This commit is contained in:
bors[bot] 2020-10-20 16:57:49 +00:00 committed by GitHub
commit ee20ecc9ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 130 additions and 102 deletions

View file

@ -1172,10 +1172,10 @@ set(EXPECTED_DATA
shader/pipeline.vert
shader/prim.frag
shader/prim.vert
shader/primex.frag
shader/primex.vert
shader/quad.frag
shader/quad.vert
shader/sprite.frag
shader/sprite.vert
shader/spritemulti.frag
shader/spritemulti.vert
shader/text.frag

19
data/shader/primex.frag Normal file
View file

@ -0,0 +1,19 @@
#ifdef TW_TEXTURED
uniform sampler2D gTextureSampler;
#endif
uniform vec4 gVerticesColor;
noperspective in vec2 texCoord;
noperspective in vec4 vertColor;
out vec4 FragClr;
void main()
{
#ifdef TW_TEXTURED
vec4 tex = texture(gTextureSampler, texCoord);
FragClr = tex * vertColor * gVerticesColor;
#else
FragClr = vertColor * gVerticesColor;
#endif
}

28
data/shader/primex.vert Normal file
View file

@ -0,0 +1,28 @@
layout (location = 0) in vec2 inVertex;
layout (location = 1) in vec2 inVertexTexCoord;
layout (location = 2) in vec4 inVertexColor;
uniform mat4x2 gPos;
uniform float gRotation;
uniform vec2 gCenter;
noperspective out vec2 texCoord;
noperspective out vec4 vertColor;
void main()
{
vec2 FinalPos = vec2(inVertex.xy);
if(gRotation != 0.0)
{
float X = FinalPos.x - gCenter.x;
float Y = FinalPos.y - gCenter.y;
FinalPos.x = X * cos(gRotation) - Y * sin(gRotation) + gCenter.x;
FinalPos.y = X * sin(gRotation) + Y * cos(gRotation) + gCenter.y;
}
gl_Position = vec4(gPos * vec4(FinalPos, 0.0, 1.0), 0.0, 1.0);
texCoord = inVertexTexCoord;
vertColor = inVertexColor;
}

View file

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

View file

@ -1,28 +0,0 @@
layout (location = 0) in vec2 inVertex;
layout (location = 1) in vec2 inVertexTexCoord;
layout (location = 2) in vec4 inVertexColor;
uniform mat4x2 Pos;
uniform float Rotation;
uniform vec2 Center;
noperspective out vec2 texCoord;
noperspective out vec4 vertColor;
void main()
{
vec2 FinalPos = vec2(inVertex.xy);
if(Rotation != 0.0)
{
float X = FinalPos.x - Center.x;
float Y = FinalPos.y - Center.y;
FinalPos.x = X * cos(Rotation) - Y * sin(Rotation) + Center.x;
FinalPos.y = X * sin(Rotation) + Y * cos(Rotation) + Center.y;
}
gl_Position = vec4(Pos * vec4(FinalPos, 0.0, 1.0), 0.0, 1.0);
texCoord = inVertexTexCoord;
vertColor = inVertexColor;
}

View file

@ -920,7 +920,7 @@ bool CCommandProcessorFragment_OpenGL::RunCommand(const CCommandBuffer::SCommand
case CCommandBuffer::CMD_RENDER_TEXT: Cmd_RenderText(static_cast<const CCommandBuffer::SCommand_RenderText *>(pBaseCommand)); break;
case CCommandBuffer::CMD_RENDER_TEXT_STREAM: Cmd_RenderTextStream(static_cast<const CCommandBuffer::SCommand_RenderTextStream *>(pBaseCommand)); break;
case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER: Cmd_RenderQuadContainer(static_cast<const CCommandBuffer::SCommand_RenderQuadContainer *>(pBaseCommand)); break;
case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_SPRITE: Cmd_RenderQuadContainerAsSprite(static_cast<const CCommandBuffer::SCommand_RenderQuadContainerAsSprite *>(pBaseCommand)); break;
case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_EX: Cmd_RenderQuadContainerEx(static_cast<const CCommandBuffer::SCommand_RenderQuadContainerEx *>(pBaseCommand)); break;
case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_SPRITE_MULTIPLE: Cmd_RenderQuadContainerAsSpriteMultiple(static_cast<const CCommandBuffer::SCommand_RenderQuadContainerAsSpriteMultiple *>(pBaseCommand)); break;
default: return false;
}
@ -2226,7 +2226,8 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
m_pQuadProgram = new CGLSLQuadProgram;
m_pQuadProgramTextured = new CGLSLQuadProgram;
m_pTextProgram = new CGLSLTextProgram;
m_pSpriteProgram = new CGLSLSpriteProgram;
m_pPrimitiveExProgram = new CGLSLPrimitiveExProgram;
m_pPrimitiveExProgramTextured = new CGLSLPrimitiveExProgram;
m_pSpriteProgramMultiple = new CGLSLSpriteMultipleProgram;
m_LastProgramID = 0;
@ -2482,26 +2483,52 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand
{
CGLSL PrimitiveVertexShader;
CGLSL PrimitiveFragmentShader;
PrimitiveVertexShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/sprite.vert", GL_VERTEX_SHADER);
PrimitiveFragmentShader.LoadShader(&ShaderCompiler, pCommand->m_pStorage, "shader/sprite.frag", GL_FRAGMENT_SHADER);
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_pSpriteProgram->CreateProgram();
m_pSpriteProgram->AddShader(&PrimitiveVertexShader);
m_pSpriteProgram->AddShader(&PrimitiveFragmentShader);
m_pSpriteProgram->LinkProgram();
m_pPrimitiveExProgram->CreateProgram();
m_pPrimitiveExProgram->AddShader(&PrimitiveVertexShader);
m_pPrimitiveExProgram->AddShader(&PrimitiveFragmentShader);
m_pPrimitiveExProgram->LinkProgram();
UseProgram(m_pSpriteProgram);
UseProgram(m_pPrimitiveExProgram);
m_pSpriteProgram->m_LocPos = m_pSpriteProgram->GetUniformLoc("Pos");
m_pSpriteProgram->m_LocIsTextured = -1;
m_pSpriteProgram->m_LocTextureSampler = m_pSpriteProgram->GetUniformLoc("textureSampler");
m_pSpriteProgram->m_LocRotation = m_pSpriteProgram->GetUniformLoc("Rotation");
m_pSpriteProgram->m_LocCenter = m_pSpriteProgram->GetUniformLoc("Center");
m_pSpriteProgram->m_LocVertciesColor = m_pSpriteProgram->GetUniformLoc("VerticesColor");
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_pSpriteProgram->SetUniform(m_pSpriteProgram->m_LocRotation, 0.0f);
m_pPrimitiveExProgram->SetUniform(m_pPrimitiveExProgram->m_LocRotation, 0.0f);
float Center[2] = {0.f, 0.f};
m_pSpriteProgram->SetUniformVec2(m_pSpriteProgram->m_LocCenter, 1, Center);
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);
}
{
CGLSL PrimitiveVertexShader;
@ -2623,7 +2650,8 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Shutdown(const SCommand_Shutdown *
m_pPrimitive3DProgram->DeleteProgram();
m_pPrimitive3DProgramTextured->DeleteProgram();
m_pTextProgram->DeleteProgram();
m_pSpriteProgram->DeleteProgram();
m_pPrimitiveExProgram->DeleteProgram();
m_pPrimitiveExProgramTextured->DeleteProgram();
m_pSpriteProgramMultiple->DeleteProgram();
//clean up everything
@ -2639,7 +2667,8 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Shutdown(const SCommand_Shutdown *
delete m_pPrimitive3DProgram;
delete m_pPrimitive3DProgramTextured;
delete m_pTextProgram;
delete m_pSpriteProgram;
delete m_pPrimitiveExProgram;
delete m_pPrimitiveExProgramTextured;
delete m_pSpriteProgramMultiple;
glBindVertexArray(0);
@ -3597,7 +3626,7 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderQuadContainer(const CCommand
glDrawElements(GL_TRIANGLES, pCommand->m_DrawNum, GL_UNSIGNED_INT, pCommand->m_pOffset);
}
void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderQuadContainerAsSprite(const CCommandBuffer::SCommand_RenderQuadContainerAsSprite *pCommand)
void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderQuadContainerEx(const CCommandBuffer::SCommand_RenderQuadContainerEx *pCommand)
{
if(pCommand->m_DrawNum == 0)
{
@ -3620,43 +3649,35 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_RenderQuadContainerAsSprite(const
BufferContainer.m_LastIndexBufferBound = m_QuadDrawIndexBufferID;
}
UseProgram(m_pSpriteProgram);
SetState(pCommand->m_State, m_pSpriteProgram);
if(pCommand->m_State.m_Texture < 0)
CGLSLPrimitiveExProgram *pProgram = m_pPrimitiveExProgram;
if(pCommand->m_State.m_Texture >= 0 && pCommand->m_State.m_Texture < CCommandBuffer::MAX_TEXTURES)
{
if(m_UseMultipleTextureUnits && m_pSpriteProgram->m_LastTextureSampler >= 0)
{
m_TextureSlotBoundToUnit[m_pSpriteProgram->m_LastTextureSampler].m_TextureSlot = -1;
}
m_TextureSlotBoundToUnit[0].m_TextureSlot = -1;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
m_pSpriteProgram->SetUniform(m_pSpriteProgram->m_LocTextureSampler, 0);
m_pSpriteProgram->m_LastTextureSampler = -1;
glBindSampler(0, 0);
pProgram = m_pPrimitiveExProgramTextured;
}
if(pCommand->m_Rotation != 0.0f && (m_pSpriteProgram->m_LastCenter[0] != pCommand->m_Center.x || m_pSpriteProgram->m_LastCenter[1] != pCommand->m_Center.y))
UseProgram(pProgram);
SetState(pCommand->m_State, pProgram);
if(pCommand->m_Rotation != 0.0f && (pProgram->m_LastCenter[0] != pCommand->m_Center.x || pProgram->m_LastCenter[1] != pCommand->m_Center.y))
{
m_pSpriteProgram->SetUniformVec2(m_pSpriteProgram->m_LocCenter, 1, (float *)&pCommand->m_Center);
m_pSpriteProgram->m_LastCenter[0] = pCommand->m_Center.x;
m_pSpriteProgram->m_LastCenter[1] = pCommand->m_Center.y;
pProgram->SetUniformVec2(pProgram->m_LocCenter, 1, (float *)&pCommand->m_Center);
pProgram->m_LastCenter[0] = pCommand->m_Center.x;
pProgram->m_LastCenter[1] = pCommand->m_Center.y;
}
if(m_pSpriteProgram->m_LastRotation != pCommand->m_Rotation)
if(pProgram->m_LastRotation != pCommand->m_Rotation)
{
m_pSpriteProgram->SetUniform(m_pSpriteProgram->m_LocRotation, pCommand->m_Rotation);
m_pSpriteProgram->m_LastRotation = pCommand->m_Rotation;
pProgram->SetUniform(pProgram->m_LocRotation, pCommand->m_Rotation);
pProgram->m_LastRotation = pCommand->m_Rotation;
}
if(m_pSpriteProgram->m_LastVertciesColor[0] != pCommand->m_VertexColor.r || m_pSpriteProgram->m_LastVertciesColor[1] != pCommand->m_VertexColor.g || m_pSpriteProgram->m_LastVertciesColor[2] != pCommand->m_VertexColor.b || m_pSpriteProgram->m_LastVertciesColor[3] != pCommand->m_VertexColor.a)
if(pProgram->m_LastVertciesColor[0] != pCommand->m_VertexColor.r || pProgram->m_LastVertciesColor[1] != pCommand->m_VertexColor.g || pProgram->m_LastVertciesColor[2] != pCommand->m_VertexColor.b || pProgram->m_LastVertciesColor[3] != pCommand->m_VertexColor.a)
{
m_pSpriteProgram->SetUniformVec4(m_pSpriteProgram->m_LocVertciesColor, 1, (float *)&pCommand->m_VertexColor);
m_pSpriteProgram->m_LastVertciesColor[0] = pCommand->m_VertexColor.r;
m_pSpriteProgram->m_LastVertciesColor[1] = pCommand->m_VertexColor.g;
m_pSpriteProgram->m_LastVertciesColor[2] = pCommand->m_VertexColor.b;
m_pSpriteProgram->m_LastVertciesColor[3] = pCommand->m_VertexColor.a;
pProgram->SetUniformVec4(pProgram->m_LocVertciesColor, 1, (float *)&pCommand->m_VertexColor);
pProgram->m_LastVertciesColor[0] = pCommand->m_VertexColor.r;
pProgram->m_LastVertciesColor[1] = pCommand->m_VertexColor.g;
pProgram->m_LastVertciesColor[2] = pCommand->m_VertexColor.b;
pProgram->m_LastVertciesColor[3] = pCommand->m_VertexColor.a;
}
glDrawElements(GL_TRIANGLES, pCommand->m_DrawNum, GL_UNSIGNED_INT, pCommand->m_pOffset);

View file

@ -103,7 +103,7 @@ class CGLSLPrimitiveProgram;
class CGLSLQuadProgram;
class CGLSLTileProgram;
class CGLSLTextProgram;
class CGLSLSpriteProgram;
class CGLSLPrimitiveExProgram;
class CGLSLSpriteMultipleProgram;
// takes care of opengl related rendering
@ -206,7 +206,7 @@ protected:
virtual void Cmd_RenderText(const CCommandBuffer::SCommand_RenderText *pCommand) {}
virtual void Cmd_RenderTextStream(const CCommandBuffer::SCommand_RenderTextStream *pCommand) {}
virtual void Cmd_RenderQuadContainer(const CCommandBuffer::SCommand_RenderQuadContainer *pCommand) {}
virtual void Cmd_RenderQuadContainerAsSprite(const CCommandBuffer::SCommand_RenderQuadContainerAsSprite *pCommand) {}
virtual void Cmd_RenderQuadContainerEx(const CCommandBuffer::SCommand_RenderQuadContainerEx *pCommand) {}
virtual void Cmd_RenderQuadContainerAsSpriteMultiple(const CCommandBuffer::SCommand_RenderQuadContainerAsSpriteMultiple *pCommand) {}
public:
@ -317,7 +317,8 @@ class CCommandProcessorFragment_OpenGL3_3 : public CCommandProcessorFragment_Ope
CGLSLQuadProgram *m_pQuadProgram;
CGLSLQuadProgram *m_pQuadProgramTextured;
CGLSLTextProgram *m_pTextProgram;
CGLSLSpriteProgram *m_pSpriteProgram;
CGLSLPrimitiveExProgram *m_pPrimitiveExProgram;
CGLSLPrimitiveExProgram *m_pPrimitiveExProgramTextured;
CGLSLSpriteMultipleProgram *m_pSpriteProgramMultiple;
GLuint m_LastProgramID;
@ -388,7 +389,7 @@ protected:
void Cmd_RenderText(const CCommandBuffer::SCommand_RenderText *pCommand) override;
void Cmd_RenderTextStream(const CCommandBuffer::SCommand_RenderTextStream *pCommand) override;
void Cmd_RenderQuadContainer(const CCommandBuffer::SCommand_RenderQuadContainer *pCommand) override;
void Cmd_RenderQuadContainerAsSprite(const CCommandBuffer::SCommand_RenderQuadContainerAsSprite *pCommand) override;
void Cmd_RenderQuadContainerEx(const CCommandBuffer::SCommand_RenderQuadContainerEx *pCommand) override;
void Cmd_RenderQuadContainerAsSpriteMultiple(const CCommandBuffer::SCommand_RenderQuadContainerAsSpriteMultiple *pCommand) override;
public:

View file

@ -1460,7 +1460,7 @@ void CGraphics_Threaded::RenderQuadContainerEx(int ContainerIndex, int QuadOffse
return;
SQuadContainer::SQuad &Quad = Container.m_Quads[QuadOffset];
CCommandBuffer::SCommand_RenderQuadContainerAsSprite Cmd;
CCommandBuffer::SCommand_RenderQuadContainerEx Cmd;
WrapClamp();
@ -1493,7 +1493,7 @@ void CGraphics_Threaded::RenderQuadContainerEx(int ContainerIndex, int QuadOffse
if(!m_pCommandBuffer->AddCommand(Cmd))
{
dbg_msg("graphics", "failed to allocate memory for render quad container sprite");
dbg_msg("graphics", "failed to allocate memory for render quad container extended");
return;
}
}

View file

@ -114,7 +114,7 @@ public:
CMD_RENDER_TEXT, // render text
CMD_RENDER_TEXT_STREAM, // render text stream
CMD_RENDER_QUAD_CONTAINER, // render a quad buffer container
CMD_RENDER_QUAD_CONTAINER_SPRITE, // render a quad buffer container as sprite
CMD_RENDER_QUAD_CONTAINER_EX, // render a quad buffer container with extended parameters
CMD_RENDER_QUAD_CONTAINER_SPRITE_MULTIPLE, // render a quad buffer container as sprite multiple times
// swap
@ -447,10 +447,10 @@ public:
void *m_pOffset;
};
struct SCommand_RenderQuadContainerAsSprite : public SCommand
struct SCommand_RenderQuadContainerEx : public SCommand
{
SCommand_RenderQuadContainerAsSprite() :
SCommand(CMD_RENDER_QUAD_CONTAINER_SPRITE) {}
SCommand_RenderQuadContainerEx() :
SCommand(CMD_RENDER_QUAD_CONTAINER_EX) {}
SState m_State;
int m_BufferContainerIndex;

View file

@ -89,10 +89,10 @@ class CGLSLPrimitiveProgram : public CGLSLTWProgram
public:
};
class CGLSLSpriteProgram : public CGLSLTWProgram
class CGLSLPrimitiveExProgram : public CGLSLTWProgram
{
public:
CGLSLSpriteProgram() :
CGLSLPrimitiveExProgram() :
CGLSLTWProgram()
{
m_LastRotation = 0.f;