How do you copy a RenderTarget2D?

How do you make an exact copy of a URenderTarget2D? Is there some function that will do it? Alternatively, I know I can get the pixels of a URenderTarget2D with:

TArray<FColor> RawColorArray;
auto RenderTarget = RT2D->GameThread_GetRenderTargetResource();
RenderTarget->ReadPixels(RawColorArray);

Is there a way to SetPixels(RawColorArray)?

I’m currently just feeding the URenderTarget2D into a UMaterial and calling UKismetRenderingLibrary::DrawMaterialToRenderTarget to write it to the copy URenderTarget2D, but this doesn’t work very well on the HTML5 platform where there’s an issue with the alpha washing out the image over multiple copies but it seems to be okay in PIE/Windows. A method of directly copying the pixels would be the best and most performant solution for me.

Any ideas?

Hi,

Unfortunately, no I did not. If I remember correctly, I was looking into writing to the RHI in a render command queue, but couldn’t get it to work. It was awhile ago so that may not be accurate. I ended up using the source RT as an input Texture2D to draw to the destination RT to make a “copy” and manually compensating for the linear color mismatch in the new image in the HTML platform by multiplicatively increasing or decreasing all the color values.

Hopefully someone more knowledgable can leave a working code snippet of the correct way to do it.

Did you ever figure this out?

Hi,
you can use CopyToResolveTarget on the Render Thread for this.
To make it a bit cleaner to read I was splitting it into two functions. The First will be called in the GameThread (the “normal” thread) and this will add a request for the Render Thread to call another function that will do the copy.
This way you can simply call CopyRT() within the GameThread.
In this case m_sourceRT and m_destRT are UTextureRenderTarget2D* pointer to a valid RT.

For example if UMyComponent is the class where your in (could also be an Actor)

void UMyComponent::CopyRT()
{
	ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
		RTCopyRunner,
		UMyComponent*, comp, this,
		{
			comp->CopyRT_RenderThread();
		}
	);
}

void UMyComponent::CopyRT_RenderThread()
{
	check(IsInRenderingThread());
	// Get global RHI command list 
	FRHICommandListImmediate& RHICmdList = GRHICommandList.GetImmediateCommandList();
	if (m_sourceRT && m_destRT)
	{
		FTexture2DRHIRef destTexture = m_destRT->GetRenderTargetResource()->GetRenderTargetTexture();
		FTexture2DRHIRef sourceTexture = m_sourceRT->GetRenderTargetResource()->GetRenderTargetTexture();

		FResolveParams param;
		RHICmdList.CopyToResolveTarget(sourceTexture, destTexture, param);
	}
}
1 Like

Hello,I try this method and it worked. But when I try copy game viewport, it not working.The result is black. The FViewport I get from GetWorld()->GetGameViewport()->Viewport. Do you know the reason for the command going wrong?

For anyone who needs that - I checked and it works with game viewport. I created RenderTarget asset (RGB10A2 format). And used this code:

void ARQ_GameMode::CopyViewoirtToTexture(UTextureRenderTarget2D* TextureTarget)
{
    UGameViewportClient* ViewportClient = this->GetWorld()->GetGameViewport();
    FSceneViewport* Viewport = ViewportClient->GetGameViewport();
    FTexture2DRHIRef Texture = Viewport->GetRenderTargetTexture();
    FRHITexture2D* TextureRHI = Texture->GetTexture2D();
    ENQUEUE_RENDER_COMMAND(ReadSurfaceCommand)(
        [Texture, TextureTarget](FRHICommandListImmediate& RHICmdList)
        {
        FTexture2DRHIRef destTexture = TextureTarget->GetRenderTargetResource()->GetRenderTargetTexture();
        FResolveParams param;
        RHICmdList.CopyToResolveTarget(Texture, destTexture, param);
        });
}
2 Likes