Skip to content

Commit

Permalink
#5566: Reorganise the shader program activation code, to enable custo…
Browse files Browse the repository at this point in the history
…m behaviour of subclasses
  • Loading branch information
codereader committed Apr 9, 2021
1 parent 4c3a09c commit 945fbcb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 37 deletions.
18 changes: 18 additions & 0 deletions radiantcore/rendersystem/backend/DepthFillPass.cpp
Expand Up @@ -2,6 +2,7 @@

#include "../OpenGLRenderSystem.h"
#include "GLProgramFactory.h"
#include "glprogram/GLSLDepthFillAlphaProgram.h"

namespace render
{
Expand Down Expand Up @@ -30,6 +31,23 @@ DepthFillPass::DepthFillPass(OpenGLShader& owner, OpenGLRenderSystem& renderSyst

// Load the GLSL program tailored for this pass
_glState.glProgram = renderSystem.getGLProgramFactory().getBuiltInProgram("depthFillAlpha");
assert(dynamic_cast<GLSLDepthFillAlphaProgram*>(_glState.glProgram));
}

void DepthFillPass::activateShaderProgram(OpenGLState& current)
{
// We need a program, it is set up in the constructor
assert(_glState.glProgram);

// Let the base class enable the program
OpenGLShaderPass::activateShaderProgram(current);

auto zFillAlphaProgram = static_cast<GLSLDepthFillAlphaProgram*>(current.glProgram);

zFillAlphaProgram->applyAlphaTest(_glState.alphaThreshold);

setTextureState(current.texture0, _glState.texture0, GL_TEXTURE0, GL_TEXTURE_2D);
setupTextureMatrix(GL_TEXTURE0, _glState.stage0);
}

}
3 changes: 3 additions & 0 deletions radiantcore/rendersystem/backend/DepthFillPass.h
Expand Up @@ -16,6 +16,9 @@ class DepthFillPass :
{
public:
DepthFillPass(OpenGLShader& owner, OpenGLRenderSystem& renderSystem);

protected:
virtual void activateShaderProgram(OpenGLState& current) override;
};

}
75 changes: 39 additions & 36 deletions radiantcore/rendersystem/backend/OpenGLShaderPass.cpp
Expand Up @@ -16,12 +16,9 @@
namespace render
{

namespace
{

// Bind the given texture to the texture unit, if it is different from the
// current state, then set the current state to the new texture.
inline void setTextureState(GLint& current,
void OpenGLShaderPass::setTextureState(GLint& current,
const GLint& texture,
GLenum textureUnit,
GLenum textureMode)
Expand All @@ -37,7 +34,7 @@ inline void setTextureState(GLint& current,
}

// Same as setTextureState() above without texture unit parameter
inline void setTextureState(GLint& current,
void OpenGLShaderPass::setTextureState(GLint& current,
const GLint& texture,
GLenum textureMode)
{
Expand All @@ -49,6 +46,9 @@ inline void setTextureState(GLint& current,
}
}

namespace
{

// Utility function to toggle an OpenGL state flag
inline void setState(unsigned int state,
unsigned int delta,
Expand Down Expand Up @@ -287,38 +287,13 @@ void OpenGLShaderPass::applyState(OpenGLState& current,
const unsigned changingBitsMask = requiredState ^ current.getRenderFlags();

// Set the GLProgram if required
GLProgram* program = (requiredState & RENDER_PROGRAM) != 0
? _glState.glProgram
: 0;

if (program != current.glProgram)
if (requiredState & RENDER_PROGRAM)
{
if (current.glProgram != 0)
{
current.glProgram->disable();
glColor4fv(current.getColour());
}

current.glProgram = program;

if (current.glProgram != 0)
{
current.glProgram->enable();
}

// Check if we need to set the alpha test value
if ((requiredState & (RENDER_ALPHATEST | RENDER_FILL)) == (RENDER_ALPHATEST | RENDER_FILL))
{
auto zFillAlphaProgram = dynamic_cast<GLSLDepthFillAlphaProgram*>(current.glProgram);

if (zFillAlphaProgram != nullptr)
{
zFillAlphaProgram->applyAlphaTest(_glState.alphaThreshold);

setTextureState(current.texture0, _glState.texture0, GL_TEXTURE0, GL_TEXTURE_2D);
setupTextureMatrix(GL_TEXTURE0, _glState.stage0);
}
}
activateShaderProgram(current);
}
else
{
deactivateShaderProgram(current);
}

// State changes. Only perform these if changingBitsMask > 0, since if there are
Expand Down Expand Up @@ -530,6 +505,34 @@ void OpenGLShaderPass::applyState(OpenGLState& current,
debug::assertNoGlErrors();
}

void OpenGLShaderPass::activateShaderProgram(OpenGLState& current)
{
if (current.glProgram == _glState.glProgram)
{
// nothing to do
return;
}

// Deactivate the previous program first
deactivateShaderProgram(current);

if (_glState.glProgram != nullptr)
{
current.glProgram = _glState.glProgram;
current.glProgram->enable();
}
}

void OpenGLShaderPass::deactivateShaderProgram(OpenGLState& current)
{
if (current.glProgram == nullptr) return;

current.glProgram->disable();
glColor4fv(current.getColour());

current.glProgram = nullptr;
}

// Add a Renderable to this bucket
void OpenGLShaderPass::addRenderable(const OpenGLRenderable& renderable,
const Matrix4& modelview,
Expand Down
14 changes: 13 additions & 1 deletion radiantcore/rendersystem/backend/OpenGLShaderPass.h
Expand Up @@ -69,7 +69,16 @@ class OpenGLShaderPass
typedef std::map<const IRenderEntity*, Renderables> RenderablesByEntity;
RenderablesByEntity _renderables;

private:
protected:

void setTextureState(GLint& current,
const GLint& texture,
GLenum textureUnit,
GLenum textureMode);

void setTextureState(GLint& current,
const GLint& texture,
GLenum textureMode);

// Apply own state to the "current" state object passed in as a reference,
// in combination with the global state mask, as well as setting
Expand Down Expand Up @@ -119,6 +128,9 @@ class OpenGLShaderPass
const Matrix4& objTransform,
std::size_t time);

virtual void activateShaderProgram(OpenGLState& current);
virtual void deactivateShaderProgram(OpenGLState& current);

public:

OpenGLShaderPass(OpenGLShader& owner) :
Expand Down

0 comments on commit 945fbcb

Please sign in to comment.