Skip to content

Commit

Permalink
Fix crash if using alpha testing in Unlit materials
Browse files Browse the repository at this point in the history
Thanks user haloman30 for reporting it.

See https://forums.ogre3d.org/viewtopic.php?p=556120
  • Loading branch information
darksylinc committed Jun 17, 2024
1 parent 3b711b4 commit deb1836
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Components/Hlms/Pbs/include/OgreHlmsPbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ namespace Ogre

void calculateHashFor( Renderable *renderable, uint32 &outHash, uint32 &outCasterHash ) override;
void calculateHashForPreCreate( Renderable *renderable, PiecesMap *inOutPieces ) override;
void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces ) override;
void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces,
const PiecesMap *normalPassPieces ) override;

void notifyPropertiesMergedPreGenerationStep() override;

Expand Down
3 changes: 2 additions & 1 deletion Components/Hlms/Pbs/src/OgreHlmsPbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,8 @@ namespace Ogre
setProperty( PbsProperty::MaterialsPerBuffer, static_cast<int>( mSlotsPerPool ) );
}
//-----------------------------------------------------------------------------------
void HlmsPbs::calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces )
void HlmsPbs::calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces,
const PiecesMap * )
{
HlmsPbsDatablock *datablock = static_cast<HlmsPbsDatablock *>( renderable->getDatablock() );
const bool hasAlphaTest = datablock->getAlphaTest() != CMPF_ALWAYS_PASS;
Expand Down
3 changes: 2 additions & 1 deletion Components/Hlms/Unlit/include/OgreHlmsUnlit.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ namespace Ogre

void calculateHashFor( Renderable *renderable, uint32 &outHash, uint32 &outCasterHash ) override;
void calculateHashForPreCreate( Renderable *renderable, PiecesMap *inOutPieces ) override;
void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces ) override;
void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces,
const PiecesMap *normalPassPieces ) override;

void notifyPropertiesMergedPreGenerationStep() override;

Expand Down
10 changes: 8 additions & 2 deletions Components/Hlms/Unlit/src/OgreHlmsUnlit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ namespace Ogre
setProperty( UnlitProperty::MaterialsPerBuffer, static_cast<int>( mSlotsPerPool ) );
}
//-----------------------------------------------------------------------------------
void HlmsUnlit::calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces )
void HlmsUnlit::calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces,
const PiecesMap *normalPassPieces )
{
// HlmsUnlitDatablock *datablock = static_cast<HlmsUnlitDatablock*>(
// renderable->getDatablock() );
Expand Down Expand Up @@ -535,6 +536,11 @@ namespace Ogre
}
}
}
else
{
inOutPieces[VertexShader] = normalPassPieces[VertexShader];
inOutPieces[PixelShader] = normalPassPieces[PixelShader];
}

if( mFastShaderBuildHack )
setProperty( UnlitProperty::MaterialsPerBuffer, static_cast<int>( 2 ) );
Expand Down Expand Up @@ -1017,7 +1023,7 @@ namespace Ogre
// ---- PIXEL SHADER ----
//---------------------------------------------------------------------------

if( !casterPass )
if( !casterPass || datablock->getAlphaTest() != CMPF_ALWAYS_PASS )
{
if( datablock->mTexturesDescSet != mLastDescTexture )
{
Expand Down
5 changes: 4 additions & 1 deletion OgreMain/include/OgreHlms.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,10 @@ namespace Ogre
uint16 calculateHashForV2( Renderable *renderable );

virtual void calculateHashForPreCreate( Renderable *renderable, PiecesMap *inOutPieces ) {}
virtual void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces ) {}
virtual void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces,
const PiecesMap *normalPassPieces )
{
}

HlmsCache preparePassHashBase( const Ogre::CompositorShadowNode *shadowNode, bool casterPass,
bool dualParaboloid, SceneManager *sceneManager );
Expand Down
2 changes: 1 addition & 1 deletion OgreMain/src/OgreHlms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2710,7 +2710,7 @@ namespace Ogre
VertexArrayObject *vao = renderable->getVaos( VpShadow )[0];
setProperty( HlmsPsoProp::InputLayoutId, vao->getInputLayoutId() );
}
calculateHashForPreCaster( renderable, piecesCaster );
calculateHashForPreCaster( renderable, piecesCaster, pieces );
setProperty( HlmsPsoProp::Macroblock,
renderable->getDatablock()->getMacroblock( true )->mLifetimeId );
setProperty( HlmsPsoProp::Blendblock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ namespace Ogre

void calculateHashFor( Renderable *renderable, uint32 &outHash, uint32 &outCasterHash ) override;
void calculateHashForPreCreate( Renderable *renderable, PiecesMap *inOutPieces ) override;
void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces ) override;
void calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces,
const PiecesMap *normalPassPieces ) override;

void notifyPropertiesMergedPreGenerationStep() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ namespace Ogre
#endif
}
//-----------------------------------------------------------------------------------
void HlmsTerra::calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces )
void HlmsTerra::calculateHashForPreCaster( Renderable *renderable, PiecesMap *inOutPieces,
const PiecesMap * )
{
// Override, since shadow casting is very basic
mSetProperties.clear();
Expand Down
6 changes: 4 additions & 2 deletions Samples/Media/Hlms/Unlit/Any/500.StructsUnlit_piece_all.any
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
@pset( texcoord, 0 )

@piece( VStoPS_block )
@property( !hlms_shadowcaster )
@property( !hlms_shadowcaster || alpha_test )
@property( syntax != metal )
FLAT_INTERPOLANT( uint drawId, @counter(texcoord) );
@else
Expand All @@ -103,7 +103,9 @@
@end
@foreach( out_uv_half_count, n )
INTERPOLANT( float@value( out_uv_half_count@n ) uv@n, @counter(texcoord) );@end
@else
@end

@property( hlms_shadowcaster )
@property( (!hlms_shadow_uses_depth_texture || exponential_shadow_maps) && !hlms_shadowcaster_point )
INTERPOLANT( float depth, @counter(texcoord) );
@end
Expand Down
2 changes: 1 addition & 1 deletion Samples/Media/Hlms/Unlit/GLSL/PixelShader_ps.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ layout(location = FRAG_COLOR, index = 0) out midf outColour;
// START UNIFORM DECLARATION
@insertpiece( custom_ps_uniformDeclaration )
// END UNIFORM DECLARATION
@property( !hlms_shadowcaster || !hlms_shadow_uses_depth_texture || exponential_shadow_maps )
@property( !hlms_shadowcaster || !hlms_shadow_uses_depth_texture || alpha_test || exponential_shadow_maps )
vulkan_layout( location = 0 ) in block
{
@insertpiece( VStoPS_block )
Expand Down
2 changes: 1 addition & 1 deletion Samples/Media/Hlms/Unlit/GLSL/VertexShader_vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ vulkan_layout( OGRE_POSITION ) in vec4 vertex;

@insertpiece( custom_vs_attributes )

@property( !hlms_shadowcaster || !hlms_shadow_uses_depth_texture || exponential_shadow_maps )
@property( !hlms_shadowcaster || !hlms_shadow_uses_depth_texture || alpha_test || exponential_shadow_maps )
vulkan_layout( location = 0 ) out block
{
@insertpiece( VStoPS_block )
Expand Down

0 comments on commit deb1836

Please sign in to comment.