UE4 crashes when using dynamic cascaded shadows on instanced static meshes that use materials with world position offset in mobile

UE4 crashes with the error “Couldn’t find Shader TShadowDepthPSPixelShadowDepth_NonPerspectiveCorrectfalse for Material Resource”.

Reproduction steps:

  1. Set the preview rendering level to default mobile/html 5
  2. Create a default lit material with a constant (or anything) attached to the world position offset slot, and check the box for use with instanced static meshes
  3. Create an actor blueprint with an instanced static mesh component
  4. In the details panel for the instanced static mesh component, select a static mesh and the material just created
  5. Add some instances
  6. Enable the instanced static mesh component to receive combined static and csm shadows (under mobile in the details panel)
  7. Create a stationary directional light with the setting “dynamic shadow distance stationary light” set to a non-zero value (such as 2000)
  8. Try to drag the blueprint into the level and the editor should crash

This only occurs when the preview rendering level is set to mobile. It doesn’t occur when set to Shader Model 5. Nor does it occur with normal static mesh components. Is this a bug?

(edit: I’ve updated to the latest release, and it’s occurring even without cascaded shadows, seems like the issue is with the world position offset and instanced static meshes)

After more testing, it looks like casting any type of dynamic shadow (cascade or modular) with an instanced static mesh that uses a material with world position offset does not work in the mobile preview nor with launching to a mobile device. Static meshes do work though, so is the engine just forgetting to compile a shader permutation? The full error message I get is:

[2016.10.17-07.29.28:388][926]LogWindows:Error: Couldn't find Shader TShadowDepthPSPixelShadowDepth_NonPerspectiveCorrectfalse for Material Resource M_Hex1!
[2016.10.17-07.29.28:388][926]LogWindows:Error: 		With VF=FInstancedStaticMeshVertexFactory, Platform=PCD3D_ES2
[2016.10.17-07.29.28:388][926]LogWindows:Error: 		ShouldCache: Mat=1, VF=1, Shader=0 
[2016.10.17-07.29.28:388][926]LogWindows:Error: 		MaterialUsageDesc: LightingModel=MSM_DefaultLit, BlendMode=BLEND_Opaque, SpecialEngine=0, TwoSided=0, TSNormal=1, Masked=0, Distorted=0, WritesEveryPixel=1, ModifiesMeshPosition=1, Usage={bUsedWithInstancedStaticMeshes}

This feature is very important for my project. Any help with resolving this issue would be greatly appreciated!

Hi,

Yeah, it looks like TShadowDepthPS::ShouldCache is missing a test for this.

Can you try changing the content of TShadowDepthPS::ShouldCache() ‘if (!IsFeatureLevelSupported(Platform, ERHIFeatureLevel::SM4))’ branch to be:

		if (!IsFeatureLevelSupported(Platform, ERHIFeatureLevel::SM4))
		{
			return (Material->IsSpecialEngineMaterial()
					// Only compile for masked or lit translucent materials
					|| !Material->WritesEveryPixel(true)
					|| (Material->MaterialMayModifyMeshPosition() && Material->IsUsedWithInstancedStaticMeshes())
					)
				&& ShaderMode == PixelShadowDepth_NonPerspectiveCorrect
				// Don't render ShadowDepth for translucent unlit materials
				&& (!IsTranslucentBlendMode(Material->GetBlendMode()) && Material->GetShadingModel() != MSM_Unlit)
				&& !bRenderReflectiveShadowMap;
		}

and let me know if that fixes it for you.

Awesome! The code above fixes the issue. I can confirm it works for the mobile previewer in 4.13.1. I’m not 100% sure if it works when packaging/launching to a mobile device in 4.13, since I can rarely get my projects to package and launch without an error (most likely due to a buggy automation tool in 4.13).

Instead, I went ahead and tested it on 4.12.5, and everything works perfectly so far. Thank you so much!

Great stuff, thanks for the report.

I am having this exact same issue with 4.14 - same error, and only when using mobile preview. Using Instanced Static Meshes with World Position Offset (no cascades)

Have you tried Allan Bentham’s solution posted below? I haven’t tried it in 4.14 yet, but I assume it should work. You will need access to the source code, though. Since it does seem like a bug, I thought Epic Games would’ve included this fix in an official release by now.

Hey, thanks for the reply.

I’m using the binary engine, from the launcher - no source code.

I guess you’ll have to wait until they patch the fix. I’d recommend looking into getting a source build running, though. There are some things you just can’t do with the official released build.

Thanks - I’ll do some research in to building from source and give it a go.

Hopefully this will get fixed in a later update, though…but I’m unsure if Epic are aware of the issue or not / have logged it.

Yeah, I’m not sure if they’ve logged it either. The fix just requires adding a couple lines of code, and it doesn’t look like it should break anything either. I’ll try asking Allan Bentham if they’re going to include the fix in any upcoming releases.

Hi Allan, will this fix be included in any upcoming official release of the engine? Some users who don’t have access to the source code are having the same issue. Thanks!

Did this ever get fixed in the branch?

no, this problem still persists in 4.19.2

This problem was fixed in later version of Unreal but came back now in 5.4 like a lot of similar bugs that were already fixed. Seems mobile has no priority at all in quality assurance.