UE Crashes when using compute shaders

Hello everyone.

I’m using compute shaders to deform a mesh. I’m having a consistent crash when running/compiling my code:

Assertion failed: !bInitializedSerializationHistory [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.5\Engine\Source\Runtime\ShaderCore\Private\Shader.cpp] [Line: 93] 

Usually this appears if I don’t set my project’s LoadingPhase to “PostConfigInit”. However, I have already done this. The editor opens and I can run my project fine. Problem is when I make changes to my code and have to recompile. Directly compiling through the editor crashes after a while. Compiling through Visual Studio only works if I open the editor, make it crash on purpose, and reopen afterwards. Only then does it apply the changes made.

I’m not posting my code right now because its fairly long, and I figured there could be some basic stuff that I’m missing. If that’s not the case, I’ll try to supply at least a sample of the code that I think might be causing the problem.

Thanks!

Im just taking a stab here since the error is in regards to bInitializedSerialiationHistory. Do your compute shaders have bound parameters? ie you call Parameter.Bind in the constructor? If so, are you serializing those parameters? I’ve noticed not doing that can potentially cause crashes.

The answer is “yes” to all your questions. In that regard I’m doing almost exactly what you’re doing in your FluidSurface plugin.

Actually, let me be a bit more detailed, while also providing some of my own suspicions:

Here’s the code that dispatches the compute shader. In 4.5 I got an error in GetGlobalShaderMap, so I worked around it using GetMaxRHIFeatureLevel(), as can be seen below:

ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
	Compute,
	FUnorderedAccessViewRHIRef&, data_uav, data_uav,
	dreng_cs_params&, parameters, parameters,
	{

	TShaderMapRef<FDrengCS> test_cs(GetGlobalShaderMap(GetMaxRHIFeatureLevel()));
	//TShaderMapRef<FDrengCS> test_cs(GetGlobalShaderMap());
	
	FComputeShaderRHIParamRef cs_ref = test_cs->GetComputeShader();

	RHICmdList.SetComputeShader(cs_ref);

	if (test_cs->RWBuffer.IsBound()){
		RHICmdList.SetUAVParameter(cs_ref, test_cs->RWBuffer.GetBaseIndex(), data_uav);
	}

	test_cs->set_uniform_buffers(RHICmdList, parameters);

	RHICmdList.DispatchComputeShader(32, 32, 1);

	test_cs->unbind_buffers(RHICmdList);
	
	}
);

Another possible reason for the crash is that I’m using this code directly in the Tick function of an Actor. Could there be some limitation in that aspect?

After attacking this issue again, I’ve found out that the very definition of the Global Shader is enough to make the editor crash on compilation. I’m not even using it!

Truth is, this code isn’t within a module or a plugin, it is in the same source folder as all the other code. Is that what might be wrong? Does this global shader NEED to be in a plugin or module that has the PostConfigInit option? I had hoped that placing it in the .uproject file would be enough.

Anyway, I’m attaching the code that causes problems. I don’t see anything wrong with it, so I’m assuming its the PostConfigInit thing, but one may never know.

So do you have PostConfigInit option defined anywhere? in the .uproject? In an additional module? If not, then you will need to, simplest solution is to add it to the uproject. Best solution is separate out the Compute Shader class into another module that has PostConfigInit set and execute the Compute Shader from your main module as per normal. Thats how my Fluid Surface plugin is setup.

I do have it defined in the uproject. As I said, its really weird that it only crashes when I compile from the editor, because it DOES run. I have tried doing it in a separate module and setting it as PostConfigInit, but still didn’t work.

Hmm… I will setup a test project over the weekend and see if I can repro this issue. I will keep you posted.

Oh, thanks! If you need any aditional information just let me know. :slight_smile:

I’m posting a minimalist sample project which is enough to demonstrate the crash.

A few notes:

  • Remember to place the .usf file in your engine shaders folder.
  • If it seems to compile fine, change something in the code and make it recompile FROM THE EDITOR (if done in Visual Studio it compiles fine). Then it should crash.

Many thanks in advance if someone has the patience to check this out. :slight_smile:

Okay this problem is because of hot reload. The module needs to be loaded prior to a specific point during startup, which does happen, but when a hot reload occurs after an in editor compile, the module is reloaded immediately, causing the engine to crash, this is correct behavior, in the sense that a module that needs to be loaded PostConfigInit to allow the shader type to be registered, which cannot happen during runtime of the editor, but maybe it shouldn’t crash but warn the user and not reload the shader or something along those lines.

I was able to get around this crash by creating a secondary module (CrashTestEngine in this case) and moved the CS class into that module, set that module to PostConfigInit and the main module to Default. When you change something in the main module and recompile from editor, it will work fine. This means if you want to change something in the secondary module, you will need to exit the editor and recompile from VS.

Yup, that worked! At least now I can compile from the editor, although when i compile from VS I would expect hot reload to work, but it crashes as before. I’m assuming that’s an error on my part, since I might have done something wrong when creating the module.

Thanks for everything Ehamloptiran!