I’ve run into hitches in our mobile game that seem to come from rebuilding the shader state when we spawn objects that have materials otherwise not already in the scene. Is this common and what is a good practice for addressing/preventing this?
Below is the code bit from engine source - OpenGLShaders.cpp - where I added a quick scope to catch the spikes and a screenshot of a sample profile with only an actor that adds a new box to the scene with one of our unique materials once per second.
Thank you for any advice,
Denis
...
SCOPE_CYCLE_COUNTER(STAT_OpenGLCreateBoundShaderStateTime);
if(!PixelShaderRHI)
{
// use special null pixel shader when PixelShader was set to NULL
PixelShaderRHI = TShaderMapRef<FNULLPS>(GetGlobalShaderMap(GMaxRHIFeatureLevel))->GetPixelShader();
}
// Check for an existing bound shader state which matches the parameters
FCachedBoundShaderStateLink* CachedBoundShaderStateLink = GetCachedBoundShaderState(
VertexDeclarationRHI,
VertexShaderRHI,
PixelShaderRHI,
HullShaderRHI,
DomainShaderRHI,
GeometryShaderRHI
);
if(CachedBoundShaderStateLink)
{
// If we've already created a bound shader state with these parameters, reuse it.
return CachedBoundShaderStateLink->BoundShaderState;
}
else
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_RedoShaderState);
check(VertexDeclarationRHI);
FOpenGLVertexDeclaration* VertexDeclaration = ResourceCast(VertexDeclarationRHI);
FOpenGLVertexShader* VertexShader = ResourceCast(VertexShaderRHI);
FOpenGLPixelShader* PixelShader = ResourceCast(PixelShaderRHI);
FOpenGLHullShader* HullShader = ResourceCast(HullShaderRHI);
FOpenGLDomainShader* DomainShader = ResourceCast(DomainShaderRHI);
FOpenGLGeometryShader* GeometryShader = ResourceCast(GeometryShaderRHI);
FOpenGLLinkedProgramConfiguration Config;
...