Torque3D/Engine/source/gfx/gl/gfxGLStateBlock.cpp

189 lines
7.5 KiB
C++
Raw Permalink Normal View History

2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "gfx/gl/gfxGLStateBlock.h"
#include "gfx/gl/gfxGLDevice.h"
#include "gfx/gl/gfxGLEnumTranslate.h"
#include "gfx/gl/gfxGLUtils.h"
#include "gfx/gl/gfxGLTextureObject.h"
2014-11-08 16:41:17 +00:00
#include "core/crc.h"
2012-09-19 15:15:01 +00:00
2014-11-08 16:41:17 +00:00
namespace DictHash
{
inline U32 hash(const GFXSamplerStateDesc &data)
{
return CRC::calculateCRC(&data, sizeof(GFXSamplerStateDesc));;
}
}
2012-09-19 15:15:01 +00:00
GFXGLStateBlock::GFXGLStateBlock(const GFXStateBlockDesc& desc) :
mDesc(desc),
mCachedHashValue(desc.getHashValue())
{
if( !GFXGL->mCapabilities.samplerObjects )
2017-01-12 04:34:46 +00:00
return;
2014-11-08 16:41:17 +00:00
static Map<GFXSamplerStateDesc, U32> mSamplersMap;
2017-01-12 04:34:46 +00:00
for(int i = 0; i < TEXTURE_STAGE_COUNT; ++i)
{
GLuint &id = mSamplerObjects[i];
GFXSamplerStateDesc &ssd = mDesc.samplers[i];
2014-11-08 16:41:17 +00:00
Map<GFXSamplerStateDesc, U32>::Iterator itr = mSamplersMap.find(ssd);
if(itr == mSamplersMap.end())
{
2017-01-12 04:34:46 +00:00
glGenSamplers(1, &id);
2014-11-08 16:41:17 +00:00
2017-01-12 04:34:46 +00:00
glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 1) );
glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
glSamplerParameteri(id, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
glSamplerParameteri(id, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
glSamplerParameteri(id, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
//compare modes
const bool comparison = ssd.samplerFunc != GFXCmpNever;
glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, comparison ? GL_COMPARE_R_TO_TEXTURE_ARB : GL_NONE );
glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, GFXGLCmpFunc[ssd.samplerFunc]);
if (static_cast< GFXGLDevice* >(GFX)->supportsAnisotropic())
2017-01-12 04:34:46 +00:00
glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
2014-11-08 16:41:17 +00:00
mSamplersMap[ssd] = id;
}
else
id = itr->value;
2017-01-12 04:34:46 +00:00
}
2012-09-19 15:15:01 +00:00
}
GFXGLStateBlock::~GFXGLStateBlock()
{
}
/// Returns the hash value of the desc that created this block
U32 GFXGLStateBlock::getHashValue() const
{
return mCachedHashValue;
}
/// Returns a GFXStateBlockDesc that this block represents
const GFXStateBlockDesc& GFXGLStateBlock::getDesc() const
{
return mDesc;
}
/// Called by OpenGL device to active this state block.
/// @param oldState The current state, used to make sure we don't set redundant states on the device. Pass NULL to reset all states.
void GFXGLStateBlock::activate(const GFXGLStateBlock* oldState)
{
2016-05-07 03:44:41 +00:00
PROFILE_SCOPE(GFXGLStateBlock_Activate);
2012-09-19 15:15:01 +00:00
// Big scary warning copied from Apple docs
// http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_performance/chapter_13_section_2.html#//apple_ref/doc/uid/TP40001987-CH213-SW12
// Don't set a state that's already set. Once a feature is enabled, it does not need to be enabled again.
// Calling an enable function more than once does nothing except waste time because OpenGL does not check
// the state of a feature when you call glEnable or glDisable. For instance, if you call glEnable(GL_LIGHTING)
// more than once, OpenGL does not check to see if the lighting state is already enabled. It simply updates
// the state value even if that value is identical to the current value.
#define STATE_CHANGE(state) (!oldState || oldState->mDesc.state != mDesc.state)
#define TOGGLE_STATE(state, enum) if(mDesc.state) glEnable(enum); else glDisable(enum)
#define CHECK_TOGGLE_STATE(state, enum) if(!oldState || oldState->mDesc.state != mDesc.state) if(mDesc.state) glEnable(enum); else glDisable(enum)
2012-09-19 15:15:01 +00:00
// Blending
CHECK_TOGGLE_STATE(blendEnable, GL_BLEND);
if(STATE_CHANGE(blendSrc) || STATE_CHANGE(blendDest))
glBlendFunc(GFXGLBlend[mDesc.blendSrc], GFXGLBlend[mDesc.blendDest]);
if(STATE_CHANGE(blendOp))
glBlendEquation(GFXGLBlendOp[mDesc.blendOp]);
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of: renderPrePassMgr.cpp related: A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages() D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets) Reflections: A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever. B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected. C) 2 things best described bluntly as working hacks: C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly. C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material. Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
if (mDesc.separateAlphaBlendEnable == true)
{
if (STATE_CHANGE(separateAlphaBlendSrc) || STATE_CHANGE(separateAlphaBlendDest))
glBlendFuncSeparate(GFXGLBlend[mDesc.blendSrc], GFXGLBlend[mDesc.blendDest], GFXGLBlend[mDesc.separateAlphaBlendSrc], GFXGLBlend[mDesc.separateAlphaBlendDest]);
if (STATE_CHANGE(separateAlphaBlendOp))
glBlendEquationSeparate(GFXGLBlendOp[mDesc.blendOp], GFXGLBlendOp[mDesc.separateAlphaBlendOp]);
}
2012-09-19 15:15:01 +00:00
// Color write masks
if(STATE_CHANGE(colorWriteRed) || STATE_CHANGE(colorWriteBlue) || STATE_CHANGE(colorWriteGreen) || STATE_CHANGE(colorWriteAlpha))
glColorMask(mDesc.colorWriteRed, mDesc.colorWriteBlue, mDesc.colorWriteGreen, mDesc.colorWriteAlpha);
// Culling
if(STATE_CHANGE(cullMode))
{
TOGGLE_STATE(cullMode, GL_CULL_FACE);
glCullFace(GFXGLCullMode[mDesc.cullMode]);
}
// Depth
CHECK_TOGGLE_STATE(zEnable, GL_DEPTH_TEST);
if(STATE_CHANGE(zFunc))
glDepthFunc(GFXGLCmpFunc[mDesc.zFunc]);
if (STATE_CHANGE(zBias))
2012-09-19 15:15:01 +00:00
{
if (mDesc.zBias == 0)
{
glDisable(GL_POLYGON_OFFSET_FILL);
}
else
{
//this assumes 24bit depth
const F32 depthMul = F32((1 << 24) - 1);
2012-09-19 15:15:01 +00:00
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(mDesc.zSlopeBias, mDesc.zBias * depthMul);
}
2012-09-19 15:15:01 +00:00
}
if(STATE_CHANGE(zWriteEnable))
glDepthMask(mDesc.zWriteEnable);
// Stencil
CHECK_TOGGLE_STATE(stencilEnable, GL_STENCIL_TEST);
if(STATE_CHANGE(stencilFunc) || STATE_CHANGE(stencilRef) || STATE_CHANGE(stencilMask))
glStencilFunc(GFXGLCmpFunc[mDesc.stencilFunc], mDesc.stencilRef, mDesc.stencilMask);
if(STATE_CHANGE(stencilFailOp) || STATE_CHANGE(stencilZFailOp) || STATE_CHANGE(stencilPassOp))
glStencilOp(GFXGLStencilOp[mDesc.stencilFailOp], GFXGLStencilOp[mDesc.stencilZFailOp], GFXGLStencilOp[mDesc.stencilPassOp]);
if(STATE_CHANGE(stencilWriteMask))
glStencilMask(mDesc.stencilWriteMask);
2014-11-08 16:41:17 +00:00
2012-09-19 15:15:01 +00:00
if(STATE_CHANGE(fillMode))
glPolygonMode(GL_FRONT_AND_BACK, GFXGLFillMode[mDesc.fillMode]);
#undef CHECK_STATE
#undef TOGGLE_STATE
#undef CHECK_TOGGLE_STATE
2014-11-08 16:41:17 +00:00
//sampler objects
if( GFXGL->mCapabilities.samplerObjects )
2012-09-19 15:15:01 +00:00
{
2014-11-08 16:41:17 +00:00
for (U32 i = 0; i < getMin(getOwningDevice()->getNumSamplers(), (U32) TEXTURE_STAGE_COUNT); i++)
2012-09-19 15:15:01 +00:00
{
2014-11-08 16:41:17 +00:00
if(!oldState || oldState->mSamplerObjects[i] != mSamplerObjects[i])
2017-01-12 04:34:46 +00:00
glBindSampler(i, mSamplerObjects[i] );
2012-09-19 15:15:01 +00:00
}
2017-01-12 04:34:46 +00:00
}
2012-09-19 15:15:01 +00:00
2014-11-08 16:41:17 +00:00
// TODO: states added for detail blend
2012-09-19 15:15:01 +00:00
}