I 'm trying to Copy an UAV Texture to the render target of Mobile forward pipleline:
TRefCountPtr<IPooledRenderTarget> MyOutput = nullptr;
//custome shader, the .usf code just simply make the input UAV red
class MyCS : public FGlobalShader
{
DECLARE_SHADER_TYPE(MyCS, Global);
SHADER_USE_PARAMETER_STRUCT(MyCS, FGlobalShader);
public:
// The number of texels on each axis processed by a single thread group.
static const FIntPoint TexelsPerThreadGroup;
static const uint32 ThreadGroupSizeX = 16;
static const uint32 ThreadGroupSizeY = 16;
static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters)
{
return IsMobilePlatform(Parameters.Platform);
}
static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment)
{
OutEnvironment.SetDefine(TEXT("THREADGROUP_SIZEX"), ThreadGroupSizeX);
OutEnvironment.SetDefine(TEXT("THREADGROUP_SIZEY"), ThreadGroupSizeY);
}
BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
SHADER_PARAMETER_RDG_TEXTURE_UAV(RWTexture2D<float4>, OutputTexture)
END_SHADER_PARAMETER_STRUCT()
};
const FIntPoint MyCS::TexelsPerThreadGroup(ThreadGroupSizeX, ThreadGroupSizeY);
IMPLEMENT_SHADER_TYPE(, MyCS, TEXT("/Engine/Private/MyCS.usf"), TEXT("mainCS"), SF_Compute);
//Function used to add my pass and setup shaderparameters
void FMobileSceneRenderer::MyPass(FRHICommandListImmediate& RHICmdList, FRDGBuilder& GraphBuilder, const FViewInfo& View, TRefCountPtr<IPooledRenderTarget>& OutputTexture) {
TShaderMapRef<MyCS> ComputeShader(View.ShaderMap);
MyCS::FParameters* CSShaderParameters = GraphBuilder.AllocParameters<MyCS::FParameters>();
FRDGTextureRef Output = GraphBuilder.CreateTexture(FRDGTextureDesc::Create2DDesc(
OutputTexture->GetDesc().Extent,
OutputTexture->GetDesc().Format,
OutputTexture->GetDesc().ClearValue,
OutputTexture->GetDesc().Flags,
OutputTexture->GetDesc().TargetableFlags | TexCreate_UAV,
false),
TEXT("MyOutput"));
CSShaderParameters->OutputTexture = GraphBuilder.CreateUAV(Output);
FComputeShaderUtils::AddPass(
GraphBuilder,
RDG_EVENT_NAME("MyPass(CS)"),
ERDGPassFlags::Compute,
ComputeShader,
CSShaderParameters,
FComputeShaderUtils::GetGroupCount(View.ViewRect.Size(),
MyCS::TexelsPerThreadGroup)
);
GraphBuilder.QueueTextureExtraction(Output, &OutputTexture);
};
//And finally I add my pass after the SceneColorRendering pass is done
FRHITexture* FMobileSceneRenderer::RenderForward(FRHICommandListImmediate& RHICmdList, const TArrayView<const FViewInfo*> ViewList){
//...unrelated code.../
FPooledRenderTargetDesc DescOut = SceneContext.GetSceneColor()->GetDesc();
if (!MyOutput.GetReference()) {
GRenderTargetPool.FindFreeElement(RHICmdList, DescOut, MyOutput,
TEXT("MyOutput"));
}
//...unrelated code.../
FRDGBuilder GraphBuilder(RHICmdList);
MyPass(RHICmdList, GraphBuilder, View, MyOutput);
GraphBuilder.Execute();
RHICmdList.EndRenderPass();
//Copy
//SceneColor is the rendertarget used in the RenderPassInfo
FRHICopyTextureInfo CopyInfo;
RHICmdList.CopyTexture(MyOutput->GetRenderTargetItem().TargetableTexture, SceneColor,
CopyInfo);
return SceneColorResolve ? SceneColorResolve : SceneColor;
}
I have captured a frame in RenderDoc and found the copy call as showed below:
But the output is not as expected (it should be red in my expection), it’s just how it look likes before doing the copy:
Does anyone have solusions? Many thanks