diff --git a/CMakeLists.txt b/CMakeLists.txt index 67a6ac2c6..7e3811443 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/data/shader/primex.frag b/data/shader/primex.frag new file mode 100644 index 000000000..ad4654b0c --- /dev/null +++ b/data/shader/primex.frag @@ -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 +} diff --git a/data/shader/primex.vert b/data/shader/primex.vert new file mode 100644 index 000000000..1aa7fc875 --- /dev/null +++ b/data/shader/primex.vert @@ -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; +} diff --git a/data/shader/sprite.frag b/data/shader/sprite.frag deleted file mode 100644 index 37a5a6433..000000000 --- a/data/shader/sprite.frag +++ /dev/null @@ -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; -} diff --git a/data/shader/sprite.vert b/data/shader/sprite.vert deleted file mode 100644 index 5c75cc8d5..000000000 --- a/data/shader/sprite.vert +++ /dev/null @@ -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; -} diff --git a/src/engine/client/backend_sdl.cpp b/src/engine/client/backend_sdl.cpp index d364574fb..16d65fa4b 100644 --- a/src/engine/client/backend_sdl.cpp +++ b/src/engine/client/backend_sdl.cpp @@ -920,7 +920,7 @@ bool CCommandProcessorFragment_OpenGL::RunCommand(const CCommandBuffer::SCommand case CCommandBuffer::CMD_RENDER_TEXT: Cmd_RenderText(static_cast(pBaseCommand)); break; case CCommandBuffer::CMD_RENDER_TEXT_STREAM: Cmd_RenderTextStream(static_cast(pBaseCommand)); break; case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER: Cmd_RenderQuadContainer(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_SPRITE: Cmd_RenderQuadContainerAsSprite(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_EX: Cmd_RenderQuadContainerEx(static_cast(pBaseCommand)); break; case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_SPRITE_MULTIPLE: Cmd_RenderQuadContainerAsSpriteMultiple(static_cast(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); diff --git a/src/engine/client/backend_sdl.h b/src/engine/client/backend_sdl.h index 3e8af15f9..a686c53a7 100644 --- a/src/engine/client/backend_sdl.h +++ b/src/engine/client/backend_sdl.h @@ -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: diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 33869e1f8..4011db1e1 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -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; } } diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 48bba4b7b..9d3f8b690 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -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; diff --git a/src/engine/client/opengl_sl_program.h b/src/engine/client/opengl_sl_program.h index aacbbbd6b..c09542f35 100644 --- a/src/engine/client/opengl_sl_program.h +++ b/src/engine/client/opengl_sl_program.h @@ -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;