Minimizes render state changes

TRAC #12154 This patch reduces redundant calls to SetRenderState Signed-off-by: Nicolas Capens Signed-off-by: Daniel Koch Author: Shannon Woods git-svn-id: https://angleproject.googlecode.com/svn/trunk@260 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 092bd481
...@@ -36,8 +36,6 @@ namespace gl ...@@ -36,8 +36,6 @@ namespace gl
Context::Context(const egl::Config *config) Context::Context(const egl::Config *config)
: mConfig(config) : mConfig(config)
{ {
mAppliedRenderTargetSerial = 0;
setClearColor(0.0f, 0.0f, 0.0f, 0.0f); setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
mState.depthClearValue = 1.0f; mState.depthClearValue = 1.0f;
...@@ -288,6 +286,17 @@ void Context::markAllStateDirty() ...@@ -288,6 +286,17 @@ void Context::markAllStateDirty()
{ {
mAppliedRenderTargetSerial = 0; mAppliedRenderTargetSerial = 0;
mAppliedProgram = 0; mAppliedProgram = 0;
mClearStateDirty = true;
mCullStateDirty = true;
mDepthStateDirty = true;
mMaskStateDirty = true;
mBlendStateDirty = true;
mStencilStateDirty = true;
mPolygonOffsetStateDirty = true;
mScissorStateDirty = true;
mSampleStateDirty = true;
mDitherStateDirty = true;
} }
void Context::setClearColor(float red, float green, float blue, float alpha) void Context::setClearColor(float red, float green, float blue, float alpha)
...@@ -310,7 +319,11 @@ void Context::setClearStencil(int stencil) ...@@ -310,7 +319,11 @@ void Context::setClearStencil(int stencil)
void Context::setCullFace(bool enabled) void Context::setCullFace(bool enabled)
{ {
if (mState.cullFace != enabled)
{
mState.cullFace = enabled; mState.cullFace = enabled;
mCullStateDirty = true;
}
} }
bool Context::isCullFaceEnabled() const bool Context::isCullFaceEnabled() const
...@@ -320,17 +333,29 @@ bool Context::isCullFaceEnabled() const ...@@ -320,17 +333,29 @@ bool Context::isCullFaceEnabled() const
void Context::setCullMode(GLenum mode) void Context::setCullMode(GLenum mode)
{ {
if (mState.cullMode != mode)
{
mState.cullMode = mode; mState.cullMode = mode;
mCullStateDirty = true;
}
} }
void Context::setFrontFace(GLenum front) void Context::setFrontFace(GLenum front)
{ {
if (mState.frontFace != front)
{
mState.frontFace = front; mState.frontFace = front;
mFrontFaceDirty = true;
}
} }
void Context::setDepthTest(bool enabled) void Context::setDepthTest(bool enabled)
{ {
if (mState.depthTest != enabled)
{
mState.depthTest = enabled; mState.depthTest = enabled;
mDepthStateDirty = true;
}
} }
bool Context::isDepthTestEnabled() const bool Context::isDepthTestEnabled() const
...@@ -340,7 +365,11 @@ bool Context::isDepthTestEnabled() const ...@@ -340,7 +365,11 @@ bool Context::isDepthTestEnabled() const
void Context::setDepthFunc(GLenum depthFunc) void Context::setDepthFunc(GLenum depthFunc)
{ {
if (mState.depthFunc != depthFunc)
{
mState.depthFunc = depthFunc; mState.depthFunc = depthFunc;
mDepthStateDirty = true;
}
} }
void Context::setDepthRange(float zNear, float zFar) void Context::setDepthRange(float zNear, float zFar)
...@@ -351,7 +380,11 @@ void Context::setDepthRange(float zNear, float zFar) ...@@ -351,7 +380,11 @@ void Context::setDepthRange(float zNear, float zFar)
void Context::setBlend(bool enabled) void Context::setBlend(bool enabled)
{ {
if (mState.blend != enabled)
{
mState.blend = enabled; mState.blend = enabled;
mBlendStateDirty = true;
}
} }
bool Context::isBlendEnabled() const bool Context::isBlendEnabled() const
...@@ -361,29 +394,52 @@ bool Context::isBlendEnabled() const ...@@ -361,29 +394,52 @@ bool Context::isBlendEnabled() const
void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha) void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
{ {
if (mState.sourceBlendRGB != sourceRGB ||
mState.sourceBlendAlpha != sourceAlpha ||
mState.destBlendRGB != destRGB ||
mState.destBlendAlpha != destAlpha)
{
mState.sourceBlendRGB = sourceRGB; mState.sourceBlendRGB = sourceRGB;
mState.destBlendRGB = destRGB; mState.destBlendRGB = destRGB;
mState.sourceBlendAlpha = sourceAlpha; mState.sourceBlendAlpha = sourceAlpha;
mState.destBlendAlpha = destAlpha; mState.destBlendAlpha = destAlpha;
mBlendStateDirty = true;
}
} }
void Context::setBlendColor(float red, float green, float blue, float alpha) void Context::setBlendColor(float red, float green, float blue, float alpha)
{ {
if (mState.blendColor.red != red ||
mState.blendColor.green != green ||
mState.blendColor.blue != blue ||
mState.blendColor.alpha != alpha)
{
mState.blendColor.red = red; mState.blendColor.red = red;
mState.blendColor.green = green; mState.blendColor.green = green;
mState.blendColor.blue = blue; mState.blendColor.blue = blue;
mState.blendColor.alpha = alpha; mState.blendColor.alpha = alpha;
mBlendStateDirty = true;
}
} }
void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation) void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
{ {
if (mState.blendEquationRGB != rgbEquation ||
mState.blendEquationAlpha != alphaEquation)
{
mState.blendEquationRGB = rgbEquation; mState.blendEquationRGB = rgbEquation;
mState.blendEquationAlpha = alphaEquation; mState.blendEquationAlpha = alphaEquation;
mBlendStateDirty = true;
}
} }
void Context::setStencilTest(bool enabled) void Context::setStencilTest(bool enabled)
{ {
if (mState.stencilTest != enabled)
{
mState.stencilTest = enabled; mState.stencilTest = enabled;
mStencilStateDirty = true;
}
} }
bool Context::isStencilTestEnabled() const bool Context::isStencilTestEnabled() const
...@@ -393,61 +449,107 @@ bool Context::isStencilTestEnabled() const ...@@ -393,61 +449,107 @@ bool Context::isStencilTestEnabled() const
void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask) void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
{ {
if (mState.stencilFunc != stencilFunc ||
mState.stencilRef != stencilRef ||
mState.stencilMask != stencilMask)
{
mState.stencilFunc = stencilFunc; mState.stencilFunc = stencilFunc;
mState.stencilRef = stencilRef; mState.stencilRef = stencilRef;
mState.stencilMask = stencilMask; mState.stencilMask = stencilMask;
mStencilStateDirty = true;
}
} }
void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask) void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
{ {
if (mState.stencilBackFunc != stencilBackFunc ||
mState.stencilBackRef != stencilBackRef ||
mState.stencilBackMask != stencilBackMask)
{
mState.stencilBackFunc = stencilBackFunc; mState.stencilBackFunc = stencilBackFunc;
mState.stencilBackRef = stencilBackRef; mState.stencilBackRef = stencilBackRef;
mState.stencilBackMask = stencilBackMask; mState.stencilBackMask = stencilBackMask;
mStencilStateDirty = true;
}
} }
void Context::setStencilWritemask(GLuint stencilWritemask) void Context::setStencilWritemask(GLuint stencilWritemask)
{ {
if (mState.stencilWritemask != stencilWritemask)
{
mState.stencilWritemask = stencilWritemask; mState.stencilWritemask = stencilWritemask;
mStencilStateDirty = true;
}
} }
void Context::setStencilBackWritemask(GLuint stencilBackWritemask) void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
{ {
if (mState.stencilBackWritemask != stencilBackWritemask)
{
mState.stencilBackWritemask = stencilBackWritemask; mState.stencilBackWritemask = stencilBackWritemask;
mStencilStateDirty = true;
}
} }
void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass) void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
{ {
if (mState.stencilFail != stencilFail ||
mState.stencilPassDepthFail != stencilPassDepthFail ||
mState.stencilPassDepthPass != stencilPassDepthPass)
{
mState.stencilFail = stencilFail; mState.stencilFail = stencilFail;
mState.stencilPassDepthFail = stencilPassDepthFail; mState.stencilPassDepthFail = stencilPassDepthFail;
mState.stencilPassDepthPass = stencilPassDepthPass; mState.stencilPassDepthPass = stencilPassDepthPass;
mStencilStateDirty = true;
}
} }
void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass) void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
{ {
if (mState.stencilBackFail != stencilBackFail ||
mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
{
mState.stencilBackFail = stencilBackFail; mState.stencilBackFail = stencilBackFail;
mState.stencilBackPassDepthFail = stencilBackPassDepthFail; mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
mState.stencilBackPassDepthPass = stencilBackPassDepthPass; mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
mStencilStateDirty = true;
}
} }
void Context::setPolygonOffsetFill(bool enabled) void Context::setPolygonOffsetFill(bool enabled)
{ {
if (mState.polygonOffsetFill != enabled)
{
mState.polygonOffsetFill = enabled; mState.polygonOffsetFill = enabled;
mPolygonOffsetStateDirty = true;
}
} }
bool Context::isPolygonOffsetFillEnabled() const bool Context::isPolygonOffsetFillEnabled() const
{ {
return mState.polygonOffsetFill; return mState.polygonOffsetFill;
} }
void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units) void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
{ {
if (mState.polygonOffsetFactor != factor ||
mState.polygonOffsetUnits != units)
{
mState.polygonOffsetFactor = factor; mState.polygonOffsetFactor = factor;
mState.polygonOffsetUnits = units; mState.polygonOffsetUnits = units;
mPolygonOffsetStateDirty = true;
}
} }
void Context::setSampleAlphaToCoverage(bool enabled) void Context::setSampleAlphaToCoverage(bool enabled)
{ {
if (mState.sampleAlphaToCoverage != enabled)
{
mState.sampleAlphaToCoverage = enabled; mState.sampleAlphaToCoverage = enabled;
mSampleStateDirty = true;
}
} }
bool Context::isSampleAlphaToCoverageEnabled() const bool Context::isSampleAlphaToCoverageEnabled() const
...@@ -457,7 +559,11 @@ bool Context::isSampleAlphaToCoverageEnabled() const ...@@ -457,7 +559,11 @@ bool Context::isSampleAlphaToCoverageEnabled() const
void Context::setSampleCoverage(bool enabled) void Context::setSampleCoverage(bool enabled)
{ {
if (mState.sampleCoverage != enabled)
{
mState.sampleCoverage = enabled; mState.sampleCoverage = enabled;
mSampleStateDirty = true;
}
} }
bool Context::isSampleCoverageEnabled() const bool Context::isSampleCoverageEnabled() const
...@@ -467,13 +573,22 @@ bool Context::isSampleCoverageEnabled() const ...@@ -467,13 +573,22 @@ bool Context::isSampleCoverageEnabled() const
void Context::setSampleCoverageParams(GLclampf value, GLboolean invert) void Context::setSampleCoverageParams(GLclampf value, GLboolean invert)
{ {
if (mState.sampleCoverageValue != value ||
mState.sampleCoverageInvert != invert)
{
mState.sampleCoverageValue = value; mState.sampleCoverageValue = value;
mState.sampleCoverageInvert = invert; mState.sampleCoverageInvert = invert;
mSampleStateDirty = true;
}
} }
void Context::setScissorTest(bool enabled) void Context::setScissorTest(bool enabled)
{ {
if (mState.scissorTest != enabled)
{
mState.scissorTest = enabled; mState.scissorTest = enabled;
mScissorStateDirty = true;
}
} }
bool Context::isScissorTestEnabled() const bool Context::isScissorTestEnabled() const
...@@ -483,7 +598,11 @@ bool Context::isScissorTestEnabled() const ...@@ -483,7 +598,11 @@ bool Context::isScissorTestEnabled() const
void Context::setDither(bool enabled) void Context::setDither(bool enabled)
{ {
if (mState.dither != enabled)
{
mState.dither = enabled; mState.dither = enabled;
mDitherStateDirty = true;
}
} }
bool Context::isDitherEnabled() const bool Context::isDitherEnabled() const
...@@ -511,23 +630,37 @@ void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height) ...@@ -511,23 +630,37 @@ void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height) void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
{ {
if (mState.scissorX != x || mState.scissorY != y ||
mState.scissorWidth != width || mState.scissorHeight != height)
{
mState.scissorX = x; mState.scissorX = x;
mState.scissorY = y; mState.scissorY = y;
mState.scissorWidth = width; mState.scissorWidth = width;
mState.scissorHeight = height; mState.scissorHeight = height;
mScissorStateDirty = true;
}
} }
void Context::setColorMask(bool red, bool green, bool blue, bool alpha) void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
{ {
if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
{
mState.colorMaskRed = red; mState.colorMaskRed = red;
mState.colorMaskGreen = green; mState.colorMaskGreen = green;
mState.colorMaskBlue = blue; mState.colorMaskBlue = blue;
mState.colorMaskAlpha = alpha; mState.colorMaskAlpha = alpha;
mMaskStateDirty = true;
}
} }
void Context::setDepthMask(bool mask) void Context::setDepthMask(bool mask)
{ {
if (mState.depthMask != mask)
{
mState.depthMask = mask; mState.depthMask = mask;
mMaskStateDirty = true;
}
} }
void Context::setActiveSampler(int active) void Context::setActiveSampler(int active)
...@@ -1541,6 +1674,8 @@ bool Context::applyRenderTarget(bool ignoreViewport) ...@@ -1541,6 +1674,8 @@ bool Context::applyRenderTarget(bool ignoreViewport)
device->SetViewport(&viewport); device->SetViewport(&viewport);
if (mScissorStateDirty)
{
if (mState.scissorTest) if (mState.scissorTest)
{ {
RECT rect = {mState.scissorX, RECT rect = {mState.scissorX,
...@@ -1556,6 +1691,9 @@ bool Context::applyRenderTarget(bool ignoreViewport) ...@@ -1556,6 +1691,9 @@ bool Context::applyRenderTarget(bool ignoreViewport)
device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
} }
mScissorStateDirty = false;
}
if (mState.currentProgram) if (mState.currentProgram)
{ {
Program *programObject = getCurrentProgram(); Program *programObject = getCurrentProgram();
...@@ -1602,6 +1740,8 @@ void Context::applyState(GLenum drawMode) ...@@ -1602,6 +1740,8 @@ void Context::applyState(GLenum drawMode)
GLint alwaysFront = !isTriangleMode(drawMode); GLint alwaysFront = !isTriangleMode(drawMode);
programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront); programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
if (mCullStateDirty || mFrontFaceDirty)
{
if (mState.cullFace) if (mState.cullFace)
{ {
device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace)); device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
...@@ -1611,6 +1751,11 @@ void Context::applyState(GLenum drawMode) ...@@ -1611,6 +1751,11 @@ void Context::applyState(GLenum drawMode)
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
} }
mCullStateDirty = false;
}
if (mDepthStateDirty)
{
if (mState.depthTest) if (mState.depthTest)
{ {
device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
...@@ -1621,6 +1766,11 @@ void Context::applyState(GLenum drawMode) ...@@ -1621,6 +1766,11 @@ void Context::applyState(GLenum drawMode)
device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
} }
mDepthStateDirty = false;
}
if (mBlendStateDirty)
{
if (mState.blend) if (mState.blend)
{ {
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
...@@ -1663,6 +1813,11 @@ void Context::applyState(GLenum drawMode) ...@@ -1663,6 +1813,11 @@ void Context::applyState(GLenum drawMode)
device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
} }
mBlendStateDirty = false;
}
if (mStencilStateDirty || mFrontFaceDirty)
{
if (mState.stencilTest && hasStencil()) if (mState.stencilTest && hasStencil())
{ {
device->SetRenderState(D3DRS_STENCILENABLE, TRUE); device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
...@@ -1713,10 +1868,20 @@ void Context::applyState(GLenum drawMode) ...@@ -1713,10 +1868,20 @@ void Context::applyState(GLenum drawMode)
device->SetRenderState(D3DRS_STENCILENABLE, FALSE); device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
} }
mStencilStateDirty = false;
}
if (mMaskStateDirty)
{
device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
mState.colorMaskBlue, mState.colorMaskAlpha)); mState.colorMaskBlue, mState.colorMaskAlpha));
device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE); device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
mMaskStateDirty = false;
}
if (mPolygonOffsetStateDirty)
{
if (mState.polygonOffsetFill) if (mState.polygonOffsetFill)
{ {
gl::Depthbuffer *depthbuffer = getFramebuffer()->getDepthbuffer(); gl::Depthbuffer *depthbuffer = getFramebuffer()->getDepthbuffer();
...@@ -1733,7 +1898,10 @@ void Context::applyState(GLenum drawMode) ...@@ -1733,7 +1898,10 @@ void Context::applyState(GLenum drawMode)
device->SetRenderState(D3DRS_DEPTHBIAS, 0); device->SetRenderState(D3DRS_DEPTHBIAS, 0);
} }
if (mConfig->mMultiSample != 0) mPolygonOffsetStateDirty = false;
}
if (mConfig->mMultiSample != 0 && mSampleStateDirty)
{ {
if (mState.sampleAlphaToCoverage) if (mState.sampleAlphaToCoverage)
{ {
...@@ -1744,9 +1912,18 @@ void Context::applyState(GLenum drawMode) ...@@ -1744,9 +1912,18 @@ void Context::applyState(GLenum drawMode)
{ {
FIXME("Sample coverage is unimplemented."); FIXME("Sample coverage is unimplemented.");
} }
mSampleStateDirty = false;
} }
if (mDitherStateDirty)
{
device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE); device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
mDitherStateDirty = false;
}
mFrontFaceDirty = false;
} }
// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program. // Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
......
...@@ -449,6 +449,20 @@ class Context ...@@ -449,6 +449,20 @@ class Context
const char *mPsProfile; const char *mPsProfile;
const char *mVsProfile; const char *mVsProfile;
// state caching flags
bool mClearStateDirty;
bool mCullStateDirty;
bool mDepthStateDirty;
bool mMaskStateDirty;
bool mPixelPackingStateDirty;
bool mBlendStateDirty;
bool mStencilStateDirty;
bool mPolygonOffsetStateDirty;
bool mScissorStateDirty;
bool mSampleStateDirty;
bool mFrontFaceDirty;
bool mDitherStateDirty;
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment