Skip to content

Commit

Permalink
Merge branch 'shadow_bias_improve' (Close #100)
Browse files Browse the repository at this point in the history
  • Loading branch information
darksylinc committed Jun 24, 2020
2 parents d1c0837 + 47d81e0 commit 2f639df
Show file tree
Hide file tree
Showing 37 changed files with 384 additions and 155 deletions.
2 changes: 2 additions & 0 deletions Components/Hlms/Pbs/include/OgreHlmsPbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ namespace Ogre

ConstBufferPool::BufferPool const *mLastBoundPool;

float mConstantBiasScale;

bool mHasSeparateSamplers;
DescriptorSetTexture const *mLastDescTexture;
DescriptorSetSampler const *mLastDescSampler;
Expand Down
19 changes: 7 additions & 12 deletions Components/Hlms/Pbs/src/OgreHlmsPbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ namespace Ogre
mDecalsDiffuseMergedEmissive( false ),
mDecalsSamplerblock( 0 ),
mLastBoundPool( 0 ),
mConstantBiasScale( 0.1f ),
mHasSeparateSamplers( 0 ),
mLastDescTexture( 0 ),
mLastDescSampler( 0 ),
Expand Down Expand Up @@ -1491,6 +1492,7 @@ namespace Ogre
retVal.setProperties = mSetProperties;

CamerasInProgress cameras = sceneManager->getCamerasInProgress();
mConstantBiasScale = cameras.renderingCamera->_getConstantBiasScale();
Matrix4 viewMatrix = cameras.renderingCamera->getVrViewMatrix( 0 );

Matrix4 projectionMatrix = cameras.renderingCamera->getProjectionMatrixWithRSDepth();
Expand Down Expand Up @@ -1578,7 +1580,8 @@ namespace Ogre

//mat4 view + mat4 shadowRcv[numShadowMapLights].texViewProj +
// vec2 shadowRcv[numShadowMapLights].shadowDepthRange +
// vec2 padding +
// float normalOffsetBias +
// float padding +
// vec4 shadowRcv[numShadowMapLights].invShadowMapSize +
//mat3 invViewMatCubemap (upgraded to three vec4)
mapSize += ( 16 + (16 + 2 + 2 + 4) * numShadowMapLights + 4 * 3 ) * 4;
Expand Down Expand Up @@ -1899,7 +1902,7 @@ namespace Ogre
*passBufferPtr++ = fNear;
}
*passBufferPtr++ = 1.0f / depthRange;
*passBufferPtr++ = 0.0f;
*passBufferPtr++ = shadowNode->getNormalOffsetBias( (size_t)shadowMapTexIdx );
*passBufferPtr++ = 0.0f; //Padding


Expand Down Expand Up @@ -3210,8 +3213,8 @@ namespace Ogre
currentMappedTexBuffer = mStartMappedTexBuffer + currentConstOffset;
}

*reinterpret_cast<float * RESTRICT_ALIAS>( currentMappedConstBuffer+1 ) = datablock->
mShadowConstantBias;
*reinterpret_cast<float * RESTRICT_ALIAS>( currentMappedConstBuffer + 1 ) =
datablock->mShadowConstantBias * mConstantBiasScale;
#if !OGRE_NO_FINE_LIGHT_MASK_GRANULARITY
*( currentMappedConstBuffer+2u ) = queuedRenderable.movableObject->getLightMask();
#endif
Expand Down Expand Up @@ -3496,14 +3499,6 @@ namespace Ogre
void HlmsPbs::setShadowSettings( ShadowFilter filter )
{
mShadowFilter = filter;

if( mShadowFilter == ExponentialShadowMaps && mHlmsManager->getShadowMappingUseBackFaces() )
{
LogManager::getSingleton().logMessage(
"QUALITY WARNING: It is highly recommended that you call "
"mHlmsManager->setShadowMappingUseBackFaces( false ) when using Exponential "
"Shadow Maps (HlmsPbs::setShadowSettings)" );
}
}
//-----------------------------------------------------------------------------------
void HlmsPbs::setEsmK( uint16 K )
Expand Down
1 change: 1 addition & 0 deletions Components/Hlms/Unlit/include/OgreHlmsUnlit.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ namespace Ogre
DescriptorSetTexture const *mLastDescTexture;
DescriptorSetSampler const *mLastDescSampler;

float mConstantBiasScale;
bool mUsingInstancedStereo;

bool mUsingExponentialShadowMaps;
Expand Down
15 changes: 5 additions & 10 deletions Components/Hlms/Unlit/src/OgreHlmsUnlit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace Ogre
mHasSeparateSamplers( 0 ),
mLastDescTexture( 0 ),
mLastDescSampler( 0 ),
mConstantBiasScale( 0.1f ),
mUsingInstancedStereo( false ),
mUsingExponentialShadowMaps( false ),
mEsmK( 600u ),
Expand All @@ -104,6 +105,7 @@ namespace Ogre
mLastBoundPool( 0 ),
mLastDescTexture( 0 ),
mLastDescSampler( 0 ),
mConstantBiasScale( 0.1f ),
mUsingInstancedStereo( false ),
mUsingExponentialShadowMaps( false ),
mEsmK( 600u ),
Expand Down Expand Up @@ -624,6 +626,7 @@ namespace Ogre
retVal.pso.pass = passCache.passPso;

mUsingInstancedStereo = isInstancedStereo;
mConstantBiasScale = cameras.renderingCamera->_getConstantBiasScale();
Matrix4 viewMatrix = cameras.renderingCamera->getViewMatrix(true);

Matrix4 projectionMatrix = cameras.renderingCamera->getProjectionMatrixWithRSDepth();
Expand Down Expand Up @@ -958,8 +961,8 @@ namespace Ogre

//uint materialIdx[]
*currentMappedConstBuffer = datablock->getAssignedSlot();
*reinterpret_cast<float * RESTRICT_ALIAS>( currentMappedConstBuffer+1 ) = datablock->
mShadowConstantBias;
*reinterpret_cast<float * RESTRICT_ALIAS>( currentMappedConstBuffer + 1 ) =
datablock->mShadowConstantBias * mConstantBiasScale;
*(currentMappedConstBuffer+2) = useIdentityProjection;
currentMappedConstBuffer += 4;

Expand Down Expand Up @@ -1058,14 +1061,6 @@ namespace Ogre
void HlmsUnlit::setShadowSettings( bool useExponentialShadowMaps )
{
mUsingExponentialShadowMaps = useExponentialShadowMaps;

if( mUsingExponentialShadowMaps && mHlmsManager->getShadowMappingUseBackFaces() )
{
LogManager::getSingleton().logMessage(
"QUALITY WARNING: It is highly recommended that you call "
"mHlmsManager->setShadowMappingUseBackFaces( false ) when using Exponential "
"Shadow Maps (HlmsUnlit::setShadowSettings)" );
}
}
//-----------------------------------------------------------------------------------
void HlmsUnlit::setEsmK( uint16 K )
Expand Down
1 change: 1 addition & 0 deletions Docs/2.0/JSON/PbsAllSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ including invalid combinations (i.e. having a Metallic texture and a Specular te
"unique_name" :
{
"scissor_test" : false,
"depth_clamp" : false,
"depth_check" : true,
"depth_write" : true,
"depth_function" : "less" "less_equal" "equal" "not_equal" "greater_equal" "greater" "never" "always",
Expand Down
1 change: 1 addition & 0 deletions Docs/2.0/JSON/PbsExample.material.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"unique_name" :
{
"scissor_test" : false,
"depth_clamp" : false,
"depth_check" : true,
"depth_write" : true,
"depth_function" : "less_equal",
Expand Down
16 changes: 16 additions & 0 deletions Docs/src/manual/compositor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,22 @@ thus the recommended values are num\_stable\_splits = 1 or num\_stable\_splits =

The default is num\_stable\_splits = 0 which disables the feature

- normal\_offset\_bias <value>

Normal-offset bias is per cascade / shadow map to fight shadow acne and self shadowing artifacts
Very large values can cause misalignments between the objects and their shadows (if they're touching)

Default is 0.00004

- constant\_bias\_scale <value>

Constant bias is per material (tweak HlmsDatablock::mShadowConstantBias).
This value lets you multiply it 'mShadowConstantBias * constantBiasScale' per cascade / shadow map

Large values can cause peter-panning.

Default is 0.1 for backwards compatibility with older materials created by Ogre 2.2.2 and earlier

- pssm\_lambda \<lambda\>

Only used by PSSM techniques. Value usually between 0 & 1. The default
Expand Down
5 changes: 5 additions & 0 deletions OgreMain/include/Compositor/OgreCompositorShadowNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ namespace Ogre
/// return a valid pointer.
const Light* getLightAssociatedWith( uint32 shadowMapIdx ) const;

/// Returns 0 if shadowMapIdx is out of bounds
size_t getLightIdxAssociatedWith( const size_t shadowMapIdx ) const;

/** Outputs the min & max depth range for the given camera. 0 & 100000 if camera not found
@remarks
Performs linear search O(N), except the overload that provides a shadowMapIdx
Expand Down Expand Up @@ -280,6 +283,8 @@ namespace Ogre
const TextureGpuVec& getContiguousShadowMapTex(void) const { return mContiguousShadowMapTex; }
uint32 getIndexToContiguousShadowMapTex( size_t shadowMapIdx ) const;

float getNormalOffsetBias( const size_t shadowMapIdx ) const;

/** Marks a shadow map as statically updated, and ties the given light to always use
that shadow map.
@remarks
Expand Down
22 changes: 22 additions & 0 deletions OgreMain/include/Compositor/OgreCompositorShadowNodeDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ namespace Ogre
size_t light; //Render Nth closest light
size_t split; //Split for that light (only for PSSM/CSM)

/// Constant bias is per material (tweak HlmsDatablock::mShadowConstantBias).
/// This value lets you multiply it 'mShadowConstantBias * constantBiasScale'
/// per cascade / shadow map
///
/// This is applied on top of the autocalculated bias from autoConstantBiasScale
float constantBiasScale;
/// Normal offset bias is per cascade / shadow map
///
/// This is applied on top of the autocalculated bias from autoNormalOffsetBiasScale
float normalOffsetBias;

/// 0 to disable.
/// Non-zero to increase bias based on orthographic projection's window size.
float autoConstantBiasScale;
/// 0 to disable.
/// Non-zero to increase bias based on orthographic projection's window size.
float autoNormalOffsetBiasScale;

ShadowMapTechniques shadowMapTechnique;

//PSSM params
Expand All @@ -87,6 +105,10 @@ namespace Ogre
arrayIdx( _arrayIdx ),
light( _light ),
split( _split ),
constantBiasScale( 1.0f ),
normalOffsetBias( 168.0f ),
autoConstantBiasScale( 100.0f ),
autoNormalOffsetBiasScale( 4.0f ),
shadowMapTechnique( t ),
pssmLambda( 0.95f ),
splitPadding( 1.0f ),
Expand Down
10 changes: 10 additions & 0 deletions OgreMain/include/OgreCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,15 @@ namespace Ogre {
/// Camera to use for LOD calculation
const Camera* mLodCamera;

bool mNeedsDepthClamp;

/// Whether or not the minimum display size of objects should take effect for this camera
bool mUseMinPixelSize;
/// @see Camera::getPixelDisplayRatio
Real mPixelDisplayRatio;

float mConstantBiasScale;

/// Each frame it is set to all false. After rendering each RQ, it is set to true
vector<bool>::type mRenderedRqs;

Expand Down Expand Up @@ -709,6 +713,9 @@ namespace Ogre {
*/
bool getUseMinPixelSize() const { return mUseMinPixelSize; }

void _setNeedsDepthClamp( bool bNeedsDepthClamp );
bool getNeedsDepthClamp( void ) const { return mNeedsDepthClamp; }

/** Returns an estimated ratio between a pixel and the display area it represents.
For orthographic cameras this function returns the amount of meters covered by
a single pixel along the vertical axis. For perspective cameras the value
Expand All @@ -721,6 +728,9 @@ namespace Ogre {
*/
Real getPixelDisplayRatio() const { return mPixelDisplayRatio; }

void _setConstantBiasScale( const float bias ) { mConstantBiasScale = bias; }
float _getConstantBiasScale( void ) const { return mConstantBiasScale; }

/** Called at the beginning of each frame to know which RenderQueue IDs have been rendered
@param numRqs
Max number of total possible render queues in this frame
Expand Down
3 changes: 0 additions & 3 deletions OgreMain/include/OgreHlms.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,6 @@ namespace Ogre
static int32 getProperty( const HlmsPropertyVec &properties,
IdString key, int32 defaultVal=0 );

/// Internal use. @see HlmsManager::setShadowMappingUseBackFaces
void _notifyShadowMappingBackFaceSetting(void);

void _clearShaderCache(void);

virtual void _changeRenderSystem( RenderSystem *newRs );
Expand Down
2 changes: 2 additions & 0 deletions OgreMain/include/OgreHlmsDatablock.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace Ogre
struct _OgreExport HlmsMacroblock : public BasicBlock
{
bool mScissorTestEnabled;
bool mDepthClamp;
bool mDepthCheck;
bool mDepthWrite;
CompareFunction mDepthFunc;
Expand Down Expand Up @@ -125,6 +126,7 @@ namespace Ogre
//Don't include the ID in the comparision
return mAllowGlobalDefaults != _r.mAllowGlobalDefaults ||
mScissorTestEnabled != _r.mScissorTestEnabled ||
mDepthClamp != _r.mDepthClamp ||
mDepthCheck != _r.mDepthCheck ||
mDepthWrite != _r.mDepthWrite ||
mDepthFunc != _r.mDepthFunc ||
Expand Down
15 changes: 0 additions & 15 deletions OgreMain/include/OgreHlmsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ namespace Ogre
InputLayoutsVec mInputLayouts;

RenderSystem *mRenderSystem;
bool mShadowMappingUseBackFaces;

public: typedef std::map<IdString, HlmsDatablock*> HlmsDatablockMap;
protected:
Expand Down Expand Up @@ -321,20 +320,6 @@ namespace Ogre
void registerComputeHlms( HlmsCompute *provider );
void unregisterComputeHlms(void);

/** Sets whether or not shadow casters should be rendered into shadow
textures using their back faces rather than their front faces.
@remarks
Rendering back faces rather than front faces into a shadow texture
can help minimise depth comparison issues, if you're using depth
shadowmapping. You will probably still need some biasing but you
won't need as much. For solid objects the result is the same anyway,
if you have objects with holes you may want to turn this option off.
The default is to enable this option.
*/
void setShadowMappingUseBackFaces( bool useBackFaces );

bool getShadowMappingUseBackFaces(void) { return mShadowMappingUseBackFaces; }

void _changeRenderSystem( RenderSystem *newRs );

RenderSystem* getRenderSystem(void) const { return mRenderSystem; }
Expand Down
1 change: 1 addition & 0 deletions OgreMain/include/OgreHlmsPso.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ namespace Ogre
ForceDisableDepthWrites = 1u << 0u,
InvertVertexWinding = 1u << 1u,
NoDepthBuffer = 1u << 2u,
ForceDepthClamp = 1u << 3u,
};
};

Expand Down
1 change: 1 addition & 0 deletions OgreMain/include/OgreRenderSystemCapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ namespace Ogre
RSC_CONST_BUFFER_SLOTS_IN_SHADER = OGRE_CAPS_VALUE(CAPS_CATEGORY_COMMON_3, 10),
RSC_TEXTURE_COMPRESSION_ASTC = OGRE_CAPS_VALUE(CAPS_CATEGORY_COMMON_3, 11),
RSC_STORE_AND_MULTISAMPLE_RESOLVE = OGRE_CAPS_VALUE(CAPS_CATEGORY_COMMON_3, 12),
RSC_DEPTH_CLAMP = OGRE_CAPS_VALUE(CAPS_CATEGORY_COMMON_3, 13),

// ***** DirectX specific caps *****
/// Is DirectX feature "per stage constants" supported
Expand Down
2 changes: 2 additions & 0 deletions OgreMain/include/OgreScriptCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,8 @@ namespace Ogre
ID_SHADOW_NODE,
ID_NUM_SPLITS,
ID_NUM_STABLE_SPLITS,
ID_NORMAL_OFFSET_BIAS,
ID_CONSTANT_BIAS_SCALE,
ID_PSSM_SPLIT_PADDING,
ID_PSSM_SPLIT_BLEND,
ID_PSSM_SPLIT_FADE,
Expand Down
Loading

0 comments on commit 2f639df

Please sign in to comment.