Hello! I am trying to implement a procedural runtime masking via runtime unwrapping and render target capture like this https://www.raywenderlich.com/6817-d…nreal-engine-4 in C++. The issue arises when I try to switch materials after performing scene capture. From debugging, I believe that occasionally material swap is performed while the scene capture is happening (or some other related rendering process) and that causes something to become nulltptr and crash. Hence I am trying to find a solution for controlling when the material swap should happen after the scene capture. Doing timed delay does not really work and stopping game thread while waiting for render thread to catch up does not sound like a good idea either, So, my question is there an option to “listen” for when the scene capture happens and then perform the material swap. Here is code in the function responsible for it (if needed):
m_pMesh->SetMaterial(0, m_pUnwrapMaterialInstance);
m_pUnwrapMaterialInstance->SetVectorParameterValue("HitLocation", FLinearColor(hitLoc));
m_pUnwrapMaterialInstance->SetVectorParameterValue("UnwrapLocation", FLinearColor(GetActorLocation() + FVector(0.f, 0.f, 1000.f)));
m_pUnwrapMaterialInstance->SetScalarParameterValue("BrushRadius", 5.f);
m_pSceneCapture2D->SetRelativeRotation(FRotator::MakeFromEuler(FVector(0.f, -90.f, -90.f)));
m_pSceneCapture2D->SetRelativeLocation(FVector(0.f, 0.f, 1200.f));
m_pSceneCapture2D->CaptureScene();
m_pMesh->SetMaterial(0, m_pEnemyMaterialInstance); <-- this seems to be the issue, as without this reset - crash does not occur
I was also looking into trying to wrap this code with ENQUEUE_UNIQUE_RENDER_COMMAND_XXXPARAMETER but underlying functions in CaptureScene() are required to be called from game thread and I assume this means that this macro would not work and from testing it did not solve the issue either (only increased nr of issues).
The crash specifically occurs in void FMaterialRenderProxy::InvalidateUniformExpressionCache(bool bRecreateUniformBuffer), on line
if (HasVirtualTextureCallbacks). (Render thread), while Main/Game thread is executing FMaterialRelevance UMaterialInterface::GetRelevance_Concurrent(ERHIFeatureLevel::Type InFeatureLevel) const, line const UMaterial* Material = GetMaterial_Concurrent();