Wrong UAV Resource Barrier For Render Textere After Niagara Write

Hey there,

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:

  1. Niagara SIM is the only thing (as far as RDG is concerned) using this texture, specifically as a UAV
  2. RDG issues a transition to UAV which happens at the start of the frame.
  3. BasePass binds the texture, expecting a SRV, but it is instead, the last transition is a UAV.
  4. 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?

Many thanks,

Alister

Steps to Reproduce

As a hack, something like this appears to work (validated in PIX):

BEGIN_SHADER_PARAMETER_STRUCT(FDummySRVPass, )
	RDG_TEXTURE_ACCESS(SrcTexture, ERHIAccess::SRVGraphics)
END_SHADER_PARAMETER_STRUCT()
 
static void AddDummySRVPass(FRDGBuilder& GraphBuilder, FRDGTextureRef Texture)
{
	FDummySRVPass* PassParameters = GraphBuilder.AllocParameters<FDummySRVPass>();
	PassParameters->SrcTexture = Texture;
	GraphBuilder.AddPass(	RDG_EVENT_NAME("DummySRV"),
							PassParameters,
							ERDGPassFlags::Raster | ERDGPassFlags::SkipRenderPass | ERDGPassFlags::NeverCull | ERDGPassFlags::NeverParallel,
							[](FRHICommandList&) {});
}
 
void UNiagaraDataInterfaceRenderTarget2D::SetShaderParameters(const FNiagaraDataInterfaceSetShaderParametersContext& Context) const
{
	FNiagaraDataInterfaceProxyRenderTarget2DProxy& DIProxy = Context.GetProxy<FNiagaraDataInterfaceProxyRenderTarget2DProxy>();
	FRenderTarget2DRWInstanceData_RenderThread* InstanceData_RT = DIProxy.SystemInstancesToProxyData_RT.Find(Context.GetSystemInstanceID());
	check(InstanceData_RT);
 
	FRDGBuilder& GraphBuilder = Context.GetGraphBuilder();
 
	// Ensure RDG resources are ready to use
	if (InstanceData_RT->TransientRDGTexture == nullptr && InstanceData_RT->TextureRHI)
	{
		InstanceData_RT->TransientRDGTexture = GraphBuilder.FindExternalTexture(InstanceData_RT->TextureRHI);
		if (InstanceData_RT->TransientRDGTexture == nullptr)
		{
			InstanceData_RT->TransientRDGTexture = GraphBuilder.RegisterExternalTexture(CreateRenderTarget(InstanceData_RT->TextureRHI, TEXT("NiagaraRenderTarget2D")));
		}
 
		AddDummySRVPass(GraphBuilder, InstanceData_RT->TransientRDGTexture); //// <<<<<<<<<<<<<<<<

Which isn’t great, but does support my initial suspicions about what is going on.

Hello,

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?

Hey Alex,

It does change things back to SRV access back as you said, both with and without mipmapping enabled.

Our Niagara sim is executed AFTER the basepass (it’s tick stage is PostOpaqueRender).

The problem isn’t that it doesn’t transition things to SRV, the problem is that RDG will transition it back to a UAV.

Here is a breakdown in PIX of what’s happening, I think it helps illustrate the issue a bit better.

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.

Here is with no mipmaps, where you can see the same problem happens.

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.

We don’t have a fix yet for this use-case, but I’ve created an issue for tracking which should be visible publicly soon https://issues.unrealengine.com/issue/UE\-405964