reduce redundant state changes

This commit is contained in:
Ted John 2016-06-12 00:07:01 +01:00
parent e771834b87
commit 3c96171801
8 changed files with 55 additions and 22 deletions

View File

@ -35,6 +35,7 @@ CopyFramebufferShader::CopyFramebufferShader() : OpenGLShaderProgram("copyframeb
Use();
SetTextureCoordinates(0, 0, 1, 1);
glUniform1i(uTexture, 0);
}
CopyFramebufferShader::~CopyFramebufferShader()
@ -72,9 +73,7 @@ void CopyFramebufferShader::SetTextureCoordinates(sint32 left, sint32 top, sint3
void CopyFramebufferShader::SetTexture(GLuint texture)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uTexture, 0);
OpenGLAPI::SetTexture2D(0, texture);
}
void CopyFramebufferShader::Draw()

View File

@ -32,6 +32,10 @@ DrawImageMaskedShader::DrawImageMaskedShader() : OpenGLShaderProgram("drawimagem
glBindVertexArray(_vao);
glEnableVertexAttribArray(vIndex);
glVertexAttribIPointer(vIndex, 1, GL_INT, 0, 0);
Use();
glUniform1i(uTextureMask, 0);
glUniform1i(uTextureColour, 1);
}
DrawImageMaskedShader::~DrawImageMaskedShader()
@ -70,16 +74,12 @@ void DrawImageMaskedShader::SetBounds(sint32 left, sint32 top, sint32 right, sin
void DrawImageMaskedShader::SetTextureMask(GLuint texture)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uTextureMask, 0);
OpenGLAPI::SetTexture2D(0, texture);
}
void DrawImageMaskedShader::SetTextureColour(GLuint texture)
{
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uTextureColour, 1);
OpenGLAPI::SetTexture2D(1, texture);
}
void DrawImageMaskedShader::Draw(sint32 left, sint32 top, sint32 right, sint32 bottom)

View File

@ -36,6 +36,7 @@ DrawImageShader::DrawImageShader() : OpenGLShaderProgram("drawimage")
Use();
SetFlags(0);
SetTextureCoordinates(0, 0, 1, 1);
glUniform1i(uTexture, 0);
}
DrawImageShader::~DrawImageShader()
@ -81,9 +82,7 @@ void DrawImageShader::SetTextureCoordinates(sint32 left, sint32 top, sint32 righ
void DrawImageShader::SetTexture(GLuint texture)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uTexture, 0);
OpenGLAPI::SetTexture2D(0, texture);
}
void DrawImageShader::SetColour(vec4f colour)

View File

@ -36,6 +36,7 @@ FillRectShader::FillRectShader() : OpenGLShaderProgram("fillrect")
Use();
SetFlags(0);
glUniform1i(uSourceFramebuffer, 0);
}
FillRectShader::~FillRectShader()
@ -85,9 +86,7 @@ void FillRectShader::SetColour(int index, vec4f colour)
void FillRectShader::SetSourceFramebuffer(GLuint texture)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uSourceFramebuffer, 0);
OpenGLAPI::SetTexture2D(0, texture);
}
void FillRectShader::Draw(sint32 left, sint32 top, sint32 right, sint32 bottom)

View File

@ -108,6 +108,21 @@ static const char * TryLoadAllProcAddresses()
return nullptr;
}
namespace OpenGLState
{
uint16 ActiveTexture = UINT16_MAX;
GLuint CurrentProgram = UINT32_MAX;
}
void OpenGLAPI::SetTexture2D(uint16 index, GLuint texture)
{
if (OpenGLState::ActiveTexture != index)
{
glActiveTexture(GL_TEXTURE0 + index);
}
glBindTexture(GL_TEXTURE_2D, texture);
}
#endif /* #if OPENGL_NO_LINK */
bool OpenGLAPI::Initialise()

View File

@ -16,6 +16,8 @@
#pragma once
#include "../../../common.h"
#if OPENGL_NO_LINK
// BEGIN [Do not define 1.1 function signatures]
@ -155,4 +157,11 @@ GLAPI_DECL PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer GLAP
namespace OpenGLAPI
{
bool Initialise();
void SetTexture2D(uint16 index, GLuint texture);
}
namespace OpenGLState
{
extern uint16 ActiveTexture;
extern GLuint CurrentProgram;
}

View File

@ -191,6 +191,7 @@ public:
TextureCache * GetTextureCache() const { return _textureCache; }
void Initialise();
void Resize(sint32 width, sint32 height);
void Clear(uint32 colour) override;
void FillRect(uint32 colour, sint32 x, sint32 y, sint32 w, sint32 h) override;
@ -270,6 +271,7 @@ public:
{
ConfigureBits(width, height, width);
ConfigureCanvas();
_drawingContext->Resize(width, height);
}
void SetPalette(SDL_Color * palette) override
@ -483,6 +485,18 @@ void OpenGLDrawingContext::Initialise()
_fillRectShader = new FillRectShader();
}
void OpenGLDrawingContext::Resize(sint32 width, sint32 height)
{
_drawImageShader->Use();
_drawImageShader->SetScreenSize(width, height);
_drawImageMaskedShader->Use();
_drawImageMaskedShader->SetScreenSize(width, height);
_drawLineShader->Use();
_drawLineShader->SetScreenSize(width, height);
_fillRectShader->Use();
_fillRectShader->SetScreenSize(width, height);
}
void OpenGLDrawingContext::Clear(uint32 colour)
{
FillRect(colour, _clipLeft - _offsetX, _clipTop - _offsetY, _clipRight, _clipBottom);
@ -530,7 +544,6 @@ void OpenGLDrawingContext::FillRect(uint32 colour, sint32 left, sint32 top, sint
_fillRectShader->SetFlags(0);
}
_fillRectShader->SetScreenSize(gScreenWidth, gScreenHeight);
_fillRectShader->SetColour(0, paletteColour[0]);
_fillRectShader->SetColour(1, paletteColour[1]);
_fillRectShader->SetClip(_clipLeft, _clipTop, _clipRight, _clipBottom);
@ -547,7 +560,6 @@ void OpenGLDrawingContext::DrawLine(uint32 colour, sint32 x1, sint32 y1, sint32
vec4f paletteColour = _engine->GLPalette[colour & 0xFF];
_drawLineShader->Use();
_drawLineShader->SetScreenSize(gScreenWidth, gScreenHeight);
_drawLineShader->SetClip(_clipLeft, _clipTop, _clipRight, _clipBottom);
_drawLineShader->SetColour(paletteColour);
_drawLineShader->Draw(x1, y1, x2, y2);
@ -607,7 +619,6 @@ void OpenGLDrawingContext::DrawSprite(uint32 image, sint32 x, sint32 y, uint32 t
bottom += _offsetY;
_drawImageShader->Use();
_drawImageShader->SetScreenSize(gScreenWidth, gScreenHeight);
_drawImageShader->SetClip(_clipLeft, _clipTop, _clipRight, _clipBottom);
_drawImageShader->SetTexture(texture);
_drawImageShader->Draw(left, top, right, bottom);
@ -646,7 +657,6 @@ void OpenGLDrawingContext::DrawSpriteRawMasked(sint32 x, sint32 y, uint32 maskIm
bottom += _offsetY;
_drawImageMaskedShader->Use();
_drawImageMaskedShader->SetScreenSize(gScreenWidth, gScreenHeight);
_drawImageMaskedShader->SetClip(_clipLeft, _clipTop, _clipRight, _clipBottom);
_drawImageMaskedShader->SetTextureMask(textureMask);
_drawImageMaskedShader->SetTextureColour(textureColour);
@ -687,7 +697,6 @@ void OpenGLDrawingContext::DrawSpriteSolid(uint32 image, sint32 x, sint32 y, uin
bottom += _offsetY;
_drawImageShader->Use();
_drawImageShader->SetScreenSize(gScreenWidth, gScreenHeight);
_drawImageShader->SetClip(_clipLeft, _clipTop, _clipRight, _clipBottom);
_drawImageShader->SetTexture(texture);
_drawImageShader->SetFlags(1);
@ -728,7 +737,6 @@ void OpenGLDrawingContext::DrawGlyph(uint32 image, sint32 x, sint32 y, uint8 * p
bottom += _offsetY;
_drawImageShader->Use();
_drawImageShader->SetScreenSize(gScreenWidth, gScreenHeight);
_drawImageShader->SetClip(_clipLeft, _clipTop, _clipRight, _clipBottom);
_drawImageShader->SetTexture(texture);
_drawImageShader->Draw(left, top, right, bottom);

View File

@ -148,7 +148,11 @@ GLuint OpenGLShaderProgram::GetUniformLocation(const char * name)
void OpenGLShaderProgram::Use()
{
glUseProgram(_id);
if (OpenGLState::CurrentProgram != _id)
{
OpenGLState::CurrentProgram = _id;
glUseProgram(_id);
}
}
bool OpenGLShaderProgram::Link()