We have a UTextureRenderTarget2D which has `bSupportsUAV` and `bAutoGenerateMips` set to true, this is written too in a Niagara sim, with the intent being that it can be read from a Material.
When it reaches the BasePass, where it should have had a transition to SRV, it’s wrongly a UAV (this can be validated by looking in PIX or turning on D3D12 validation layers).
From what I can tell there is no RDG tracking for base pass inputs so what appears to be happening:
Niagara SIM is the only thing (as far as RDG is concerned) using this texture, specifically as a UAV
RDG issues a transition to UAV which happens at the start of the frame.
BasePass binds the texture, expecting a SRV, but it is instead, the last transition is a UAV.
Niagara SIM uses the texture after the BasePass as a UAV.
I am not sure what the correct way to address this is, it seems like an oversight somewhere, or are we using this in the incorrect way?
The RDG should be setting the UTextureRenderTarget2D back into the external access mode in Engine/Plugins/FX/Niagara/Source/Niagara/Private/NiagaraGpuComputeDispatch.cpp[Content removed] where CurrentPassExternalAccessQueue.Submit(GraphBuilder); is called at the end of FNiagaraGpuComputeDispatch::ExecuteTicks().
It’s possible the code for transitioning to SRV later is missing from the mip gen pass - could you try testing without bAutoGeneratedMips to see if that has any effect?
Because RDG isn’t used for tracking material texture inputs, they have to be externally synchronised (which Niagara is doing).
But if that resource is used by the same RDG graph, such as this, and is first accessed AFTER the BasePass, RDG will undo the synchronisation done on the previous frame as part of it’s pre-amble.
Arguably there should be some amount of tracking for material inputs which are UTextureRenderTarget2D.
As things stand, it would also mean if the Niagara sim is executed on the async queue and the render target hasn’t marked as shared, there would be no transition handover.
Thanks for providing the details, it makes sense what the issue is now and we’re looking into how to address this.
Is the simulation using the default tick stage (Post Render Opaque)? I’m wondering if changing the tick stage has an effect - though that is not possible for simulations that need depth or distance fields.
I don’t pretend to be well versed in Niagara and the sim in question is quite complicated, but I believe this sim does need distance fields.
In another test performed by a colleague, which was executing at PostInit, everything was behaving as you would hope.
Locally though, I have a “fix”, which was namely:
////
// Engine/Plugins/FX/Niagara/Source/Niagara/Private/NiagaraGpuComputeDispatch.cpp
////
void FNiagaraGpuComputeDispatch::ExecuteTicks(FRDGBuilder& GraphBuilder, TConstStridedView<FSceneView> Views, ENiagaraGpuComputeTickStage::Type TickStage)
{
// CHANGE BEGIN
if (TickStage == ENiagaraGpuComputeTickStage::PreInitViews)
{
for (
int32 InitStage = static_cast<int32>(ENiagaraGpuComputeTickStage::First);
InitStage < static_cast<int32>(ENiagaraGpuComputeTickStage::Max);
++InitStage)
{
FNiagaraGpuDispatchList& DispatchList = DispatchListPerStage[InitStage];
for (const FNiagaraGpuDispatchGroup& DispatchGroup : DispatchList.DispatchGroups)
{
for (const FNiagaraGpuDispatchInstance& DispatchInstance : DispatchGroup.DispatchInstances)
{
for (FNiagaraDataInterfaceProxy* Proxy : DispatchInstance.InstanceData.DataInterfaceProxies)
{
Proxy->PerFrameInit(GraphBuilder, static_cast<ENiagaraGpuComputeTickStage::Type>(InitStage));
}
}
}
}
}
// CHANGE END
////
// Engine/Plugins/FX/Niagara/Source/Niagara/Private/NiagaraDataInterfaceRenderTarget2D.cpp
////
// CHANGE BEGIN
void FNiagaraDataInterfaceProxyRenderTarget2DProxy::PerFrameInit(FRDGBuilder& GraphBuilder, ENiagaraGpuComputeTickStage::Type ExecutedTickStage)
{
// The render texture may be used in the BasePass, which has no tracking associated with it.
// If this is rendered into after the BasePass, RDG will transition it into a UAV state.
// So until need, we mark it as externally accessed and needing to be SRV readable on the Graphics queue.
// During `SetShaderParameters`, it will be moved back into the internal queue.
// For more context: [Content removed]
if (ExecutedTickStage > ENiagaraGpuComputeTickStage::PostInitViews)
{
for (const auto& TextureMapping : SystemInstancesToProxyData_RT)
{
const FRenderTarget2DRWInstanceData_RenderThread& Proxy_RT = TextureMapping.Value;
if (!Proxy_RT.TextureRHI)
{
continue;
}
FRDGTextureRef TransientRDGTexture = Proxy_RT.TransientRDGTexture;
if (!TransientRDGTexture)
{
TransientRDGTexture = GraphBuilder.FindExternalTexture(Proxy_RT.TextureRHI);
if (!TransientRDGTexture)
{
TransientRDGTexture = GraphBuilder.RegisterExternalTexture(CreateRenderTarget(Proxy_RT.TextureRHI, TEXT("NiagaraRenderTarget2D")));
if (!TransientRDGTexture)
{
continue;
}
}
}
GraphBuilder.UseExternalAccessMode(TransientRDGTexture, ERHIAccess::SRVMask);
}
}
}
// CHANGE END
It was a bit too difficult to get the Texture RHI manually, but the overall intention is the same (basically call `GraphBuilder.UseExternalAccessMode` before the basepass).
The other “correct” alternative I suspect would be to take the texture out of the RDG tracking and manually transition things.