x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: UE 4.18
Tags:
more ▼

asked Dec 02 '17 at 10:24 PM in C++ Programming

avatar image

DanimalsOnParade
68 4 10 14

avatar image Sam54123 Oct 09 '18 at 03:39 AM

Did you ever figure this out?

avatar image DanimalsOnParade Oct 09 '18 at 04:09 AM

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.

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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);
     }
 }
more ▼

answered Feb 06 '19 at 02:04 PM

avatar image

p5.rawq
1 1 1

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question