Write Texture2DRHI or UAV to Render Target

Based on some code from the Fluid Surface Plugin and the UE4 Shader Plugin Demo I’ve written a compute shader which writes to an Unordered Access View that is part of an FTexture2DRHIRef instance.

I need to write the output from the compute shader to a UTextureRenderTarget2D, or just a UTexture2D. What is the most efficient way of achieving this? The Shader Plugin Demo uses a pixel shader to render the UAV output to the Render Target, while the Fluid Surface merely sends the RHIRef to the Vertex Factory; the former seems overkill and not very optimized, while the latter doesn’t apply here as I need a texture and not a mesh. One other alternative I’ve seen is to copy the raw data from the UAV into the Render Target using Memcpy, but this requires me to lock the RT, access both data and copy it, then unlock the RT and call UpdateResource on it.

The goal is to send a reference to a Render Target from an Actor (which is set in a Dynamic Material) to a class that executes the compute shader and writes the output to that Render Target to update the material.

Ever found a solution?

I did find a solution not long after asking, but later I no longer needed it and it has been two versions back since I last used it. Unfortunately, I no longer have the project and I cannot remember how I solved it. What I do remember is that it did involve creating a second RT just for the shader, executing the shader, and then copy the data from that RT to the actual RT used in the material (either created at runtime in code or Blueprint, or as an asset).

For the ones who are still struggling on this issue:

FTexture2DRHIRef Texture = RHICreateTexture2D(SizeX, SizeY, PF_A32B32G32R32F, 1, 1, TexCreate_ShaderResource | TexCreate_UAV, CreateInfo);
	FTexture2DRHIRef OriginalRT = OutputTargetResource->GetRenderTargetTexture();
	FUnorderedAccessViewRHIRef TextureUAV = RHICreateUnorderedAccessView(Texture);

	ComputeShader->SetParameters(RHICmdList, TextureUAV);
	DispatchComputeShader(RHICmdList, *ComputeShader, SizeX / 32, SizeY / 32, 1);
	// Now parameter Texture saves the execute result
	FRHICopyTextureInfo CopyInfo(SizeX,SizeY);
	RHICmdList.CopyTexture(Texture, OriginalRT, CopyInfo);

This Key is to make the FRHICopyTextureInfo struct and then CopyTexture function works, OriginalRT then works for material use