shader _com_error

I’m attempting to program a new shader in a plugin. I’m using the LensDistortion plugin as example, according to the documentation, as well as the UE4ShaderPluginDemo.

However, I’m getting constant crashes with an _com_error exception while executing the render function on the render thread, code below.

static void DrawShaderToRenderTarget_RenderThread(
	FRHICommandListImmediate& RHICmdList,
	const FLinearColor& InputColor,
	FTextureRenderTargetResource* OutTextureRenderTargetResource,
	ERHIFeatureLevel::Type FeatureLevel)
{
	check(IsInRenderingThread());

	SCOPED_DRAW_EVENT(RHICmdList, DrawShaderToRenderTarget_RenderThread);

	SetRenderTarget(
		RHICmdList,
		OutTextureRenderTargetResource->GetRenderTargetTexture(),
		FTextureRHIRef(),
		ESimpleRenderTargetMode::EUninitializedColorAndDepth,
		FExclusiveDepthStencil::DepthNop_StencilNop);

	RHICmdList.SetViewport(
		0, 0, 0.f,
		OutTextureRenderTargetResource->GetSizeX(), OutTextureRenderTargetResource->GetSizeY(), 1.f);

	TShaderMap<FGlobalShaderType>* GlobalShaderMap = GetGlobalShaderMap(FeatureLevel);
	TShaderMapRef<FEventHorizonShaderVS> VertexShader(GlobalShaderMap);
	TShaderMapRef<FEventHorizonShaderPS> PixelShader(GlobalShaderMap);

	FGraphicsPipelineStateInitializer GraphicsPSOInit;
	RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
	GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
	GraphicsPSOInit.BlendState = TStaticBlendState<>::GetRHI();
	GraphicsPSOInit.RasterizerState = TStaticRasterizerState<>::GetRHI();
	GraphicsPSOInit.PrimitiveType = PT_TriangleList;
	GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GetVertexDeclarationFVector4();
	GraphicsPSOInit.BoundShaderState.VertexShaderRHI = GETSAFERHISHADER_VERTEX(*VertexShader);
	GraphicsPSOInit.BoundShaderState.PixelShaderRHI = GETSAFERHISHADER_PIXEL(*PixelShader);
	SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);

	RHICmdList.SetViewport(
		0, 0, 0.f,
		OutTextureRenderTargetResource->GetSizeX(), OutTextureRenderTargetResource->GetSizeY(), 1.f);

	VertexShader->SetParameters(RHICmdList, VertexShader->GetVertexShader(), InputColor);
	PixelShader->SetParameters(RHICmdList, PixelShader->GetPixelShader(), InputColor);

	uint32 PrimitiveCount = 16 * 16 * 2;
	RHICmdList.DrawPrimitive(PT_TriangleList, 0, PrimitiveCount, 1);

	RHICmdList.CopyToResolveTarget(
		OutTextureRenderTargetResource->GetRenderTargetTexture(),
		OutTextureRenderTargetResource->TextureRHI,
		false, FResolveParams()
	);
}

The second “RHICmdList.SetViewport” call, after the GraphicsPSOInit calls is where the exception occurs. When I comment out that line, the exception happens on the next line of code. If I comment out all the lines until the end of the function, the exception happens at the end of the function.

I have no idea what causes this, since I use almost the exact code from the LensDistortion plugin. The function that calls this uses the ENQUEUE_UNIQUE_RENDER_COMMAND_FOURPARAMETER macro with the four input parameters. The same error occurs when using the ENQUEUE_RENDER_COMMAND macro as it’s written in the example plugin.

At one point I did see another error in the log referring to Direct3D, CreateInputLayout and E_InvalidArg, though I don’t know what caused that.

Okay, after a bit more fiddling, it appears it’s an access violation:
Exception thrown at 0x00007FFAC80AC748 (UE4Editor-D3D11RHI.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x00000000000000C0.
It happens with SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit)