# How do you copy a RenderTarget2D?

**URL:** <https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223>\
**Category:** Platform & Builds\
**Tags:** rendertarget, copy, HTML5, unreal-engine, render-target, CPP, question\
**Created:** [December 2, 2017, 10:24pm UTC](https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223 "2017-12-02T22:24:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanimalsOnParade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DanimalsOnParade](https://forums.unrealengine.com/u/DanimalsOnParade)\
**Post date:** [December 2, 2017, 10:24pm UTC](https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223/1 "2017-12-02T22:24:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sam54123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sam54123](https://forums.unrealengine.com/u/Sam54123)\
**Post date:** [October 9, 2018, 3:39am UTC](https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223/2 "2018-10-09T03:39:14Z")

</div>

Did you ever figure this out?

---

<div class="post-metadata">

**Author:** ![DanimalsOnParade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DanimalsOnParade](https://forums.unrealengine.com/u/DanimalsOnParade)\
**Post date:** [October 9, 2018, 4:09am UTC](https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223/3 "2018-10-09T04:09:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![p5.rawq](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@p5.rawq](https://forums.unrealengine.com/u/p5.rawq)\
**Post date:** [February 6, 2019, 2:04pm UTC](https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223/4 "2019-02-06T14:04:06Z")

</div>

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);
	}
}
```

---

<div class="post-metadata">

**Author:** ![wsdxyhh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@wsdxyhh](https://forums.unrealengine.com/u/wsdxyhh)\
**Post date:** [April 12, 2021, 8:18am UTC](https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223/5 "2021-04-12T08:18:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![VinnyBlood](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@VinnyBlood](https://forums.unrealengine.com/u/VinnyBlood)\
**Post date:** [February 18, 2022, 3:59pm UTC](https://forums.unrealengine.com/t/how-do-you-copy-a-rendertarget2d/411223/6 "2022-02-18T15:59:44Z")

</div>

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

```auto
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);
        });
}

```
