Uninitialized value in Material PreshaderBuffer

We noticed some inconsistencies when toggling static switches of materials in the Material Constant Editor after updating to 5.6. The issue would manifest in the form of the preview material disappearing or having the wrong branch compiled.

I tracked it down to uninitialized values in the PreshaderBuffer. The following one line in MaterialUniformExpressions.cpp fixes the issue. Sharing it here as I haven’t seen it reported elsewhere. The attached images show the buffer contents when correct and when they are uninitialized.

  			// Copy the value to the target position
 			CopyValueToUniformBuffer(ParameterShaderValue, (float*)BufferCursor, Evaluation.BufferOffset);
 		}
  
 		// Dump preshader results into buffer.
 		float* const PreshaderBuffer = (float*)BufferCursor;
+ 		FMemory::Memzero(PreshaderBuffer, UniformPreshaderBufferSize * 4 * sizeof(float));
 		FPreshaderStack PreshaderStack;
 		FPreshaderDataContext PreshaderBaseContext(UniformPreshaderData);
 		for (const FMaterialUniformPreshaderHeader& Preshader : UniformPreshaders)
 		{
 			FPreshaderDataContext PreshaderContext(PreshaderBaseContext, Preshader.OpcodeOffset, Preshader.OpcodeSize);
 			const FPreshaderValue Result = EvaluatePreshader(this, MaterialRenderContext, PreshaderStack, PreshaderContext);



Hi Sakib,

Thanks for calling that out. Could you please send a small repro project or the exact repro steps for this issue? That way, I can expedite this fix through the dev team.

We had bugs with static bool parameters in 5.6 and this fixed it for us: https://github.com/EpicGames/UnrealEngine/commit/2a10d813bb619e5466a6c3c777c167e5fbe994db

Hi folks, it sounds like David’s suggestion resolved the issue. I have marked it as the best answer in case others encounter the problem.

Thank you! Just tested that change and that fixes the issue as well. I’ll cherry-pick that change. I suppose there is no harm in default initializing the memory but not required anymore.

Based on the checkin comment, I believe that is why it was difficult for us to create a repro in vanilla and we couldn’t export our material because of custom version.