# \[BUG\] Render Target-\>ReadPixels Android

**URL:** <https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662>\
**Category:** Mobile\
**Tags:** unreal-engine, Android, bug-report, question\
**Created:** [October 27, 2016, 8:27pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662 "2016-10-27T20:27:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bassist66](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bassist66](https://forums.unrealengine.com/u/Bassist66)\
**Post date:** [October 27, 2016, 8:27pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/1 "2016-10-27T20:27:43Z")

</div>

FTextureRenderTargetResource-\>ReadPixels returns garbage data on Android(working in editor). Does anyone know another way to in the pixel information of a render target texture in order to save it as a thumbnail for a saved game?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [October 31, 2016, 6:35pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/2 "2016-10-31T18:35:51Z")

</div>

Hey -

I’m unable to find a reference to ReadPixels in the FTextureRenderTargetResource class. Are you referring to FTextureRenderTargetCubeResource instead? If you’re able to reproduce the bad data in a fresh project, can you provide the steps used to help me test this issue on my end?

---

<div class="post-metadata">

**Author:** ![Bassist66](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bassist66](https://forums.unrealengine.com/u/Bassist66)\
**Post date:** [October 31, 2016, 8:30pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/3 "2016-10-31T20:30:51Z")

</div>

Created C++ actor with a blueprint callable function that takes in a USceneCaptureComponent2D, then get a reference to the FTextureTargetResource using USceneCaptureComponent2D-\>TextureTarget-\>GameThread\_GetRenderTargetResource(). I then call readPixels from that which is in the resources base class FRenderTarget. Currently I am using hte imagewrapper to create and save a png file. Then packaged/deployed for Android(ASTC).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [November 1, 2016, 3:15pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/4 "2016-11-01T15:15:18Z")

</div>

I tried following the steps you mentioned and used the following code in my test:

```
void AMyActor::PixelTest(USceneCaptureComponent2D* Something)
{
	FReadSurfaceDataFlags InFlags;
	FIntRect InRect;
	bool things = Something->TextureTarget->GameThread_GetRenderTargetResource()->ReadPixels(OutImageData, InFlags, InRect);
	if (GEngine)
	{
		if(things)
		GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Magenta, TEXT("True"));
		else
		GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Magenta, TEXT("False"));
	}
}

```

The OutImageData array was populated with four colors. When this function is called during runtime I get “True” printed to my screen as expected. Can you explain the garbage data you’re getting when you call ReadPixels()? If possible, could you provide a sample project that shows the issue?

---

<div class="post-metadata">

**Author:** ![Bassist66](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bassist66](https://forums.unrealengine.com/u/Bassist66)\
**Post date:** [November 1, 2016, 5:10pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/5 "2016-11-01T17:10:27Z")

</div>

Currently the outImageData is filled with RGB values of 0.link text

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [November 1, 2016, 7:57pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/6 "2016-11-01T19:57:27Z")

</div>

Hey -

It appers you are referring to how the FColor array is being set rather than the boolean that is returned from ReadPixels. After reviewing the project you sent I was able to reproduce the output you were seeing and have entered a report for the issue here [Unreal Engine Issues and Bug Tracker (UE-38086)](https://issues.unrealengine.com/issue/UE-38086) . You can track the report’s status as the issue is reviewed by our development staff.

Cheers

---

<div class="post-metadata">

**Author:** ![Bassist66](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bassist66](https://forums.unrealengine.com/u/Bassist66)\
**Post date:** [November 3, 2016, 3:57pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/7 "2016-11-03T15:57:22Z")

</div>

Thank you for submitting the bug report , I appreciate it.

---

<div class="post-metadata">

**Author:** ![theonecalledtom](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@theonecalledtom](https://forums.unrealengine.com/u/theonecalledtom)\
**Post date:** [October 4, 2017, 4:31am UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/8 "2017-10-04T04:31:18Z")

</div>

Any workarounds for this?

On iOS it was fixed for me with the following code (called before GameThread\_GetRenderTargetResource) but this doesn’t seem to help Android:

```
#if PLATFORM_IOS | PLATFORM_ANDROID
    ENQUEUE_UNIQUE_RENDER_COMMAND(WaitUntilIdle,
    {
        GRHICommandList.GetImmediateCommandList().BlockUntilGPUIdle();
    });
    FlushRenderingCommands();
#endif

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2b03d27c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2b03d27c](https://forums.unrealengine.com/u/anonymous_user_2b03d27c)\
**Post date:** [October 4, 2017, 12:25pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/9 "2017-10-04T12:25:43Z")

</div>

Hey -

Unfortunately there is no known workaround at the moment for this issue.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ce9164e3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ce9164e3](https://forums.unrealengine.com/u/anonymous_user_ce9164e3)\
**Post date:** [October 4, 2017, 2:09pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/10 "2017-10-04T14:09:30Z")

</div>

I have no issues reading out a rendertarget on android by doing it the following way:  
pre-warning: i did not test out the following psuedocode, but its basicly a bare minimal version of the code that i’m using.

```
struct FGetTexture
{
	FUpdateCaptureCubemap(int32 TextureWidthHeightInPixels,FRenderTarget* pSrcRenderTargetResource)
	{
		srcRenderTargetResource = pSrcRenderTargetResource);
		TextureSize= TextureWidthHeightInPixels;
		dstBuffer.AddUninitialized(TextureSize*TextureSize);
	}
	TArray<FColor> dstBuffer;
	FRenderTarget* srcRenderTargetResource;
	int32 TextureSize;
};

FGetTexture *pGetTexture = new FGetTexture(TextureWidthHeightInPixels,RenderTarget->GameThread_GetRenderTargetResource());
  
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(GetTexture, FGetTexture *, pGetTexture, pGetTexture,
{
		FReadSurfaceDataFlags DefaultFlags(RCM_UNorm, CubeFace_MAX);
		RHICmdList.ReadSurfaceData(pGetTexture->srcRenderTargetResource->GetRenderTargetTexture(),FIntRect(0, 0, pGetTexture->TextureSize,pGetTexture->TextureSize),pGetTexture->dstBuffer,DefaultFlags);

		// Now do something with pGetTexture->DstBuffer either/and 
		// - upload it to a texture
		// - memcpy it to a different (make it Threadsafe) (allocated in your class) buffer
		// - start your next task here with the temp buffer as input, with the CreateTask function

		// do not forget to delete your temp buffer
		delete pGetTexture;
});

```

---

<div class="post-metadata">

**Author:** ![theonecalledtom](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@theonecalledtom](https://forums.unrealengine.com/u/theonecalledtom)\
**Post date:** [October 4, 2017, 6:19pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/11 "2017-10-04T18:19:32Z")

</div>

Thanks . I implemented a version of that but still all 0’s in the result.

I’m not sure if the problem is getting the surface or the previous rendering to that surface is failing though I have other render to surface points that are working (using the render target as a texture) and have reduced my test render to clearing the surface to one color and drawing a rectangle to another.

The RenderTarget setup is pretty straightforward:

```
    RenderTexture = PCIP.CreateDefaultSubobject<UTextureRenderTarget2D>(this, TEXT("RenderTexture"));
	RenderTexture->ClearColor = FLinearColor::Black;
	RenderTexture->SRGB = false;
	RenderTexture->bNeedsTwoCopies = false;
	RenderTexture->InitAutoFormat(iWidth, iHeight);
	RenderTexture->UpdateResourceImmediate();

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ce9164e3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ce9164e3](https://forums.unrealengine.com/u/anonymous_user_ce9164e3)\
**Post date:** [October 5, 2017, 1:13pm UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/12 "2017-10-05T13:13:31Z")

</div>

If you can use the rendertarget correctly as a source texture, then i assume that the render itself does work. Maybe its your setup code for the RenderTarget, i have (minimized, not tested) the following:

In your initialize function

```
RenderTarget = NewObject<UTextureRenderTarget2D>(GetTransientPackage(), NAME_None, RF_Transient);
check(RenderTarget);
RenderTarget->ClearColor = FLinearColor(0.0, 0.0, 0.0);
RenderTarget->InitCustomFormat(TextureSizeWidthHeight, TextureSizeWidthHeight, PF_B8G8R8A8,true);
RenderTarget->UpdateResourceImmediate();

// then i use this RenderTarget in the SceneCaptureComponent
CaptureComp2D= NewObject<USceneCaptureComponent2D>(GetOwner());
check(CaptureComp2D);

CaptureComp2D->TextureTarget = RenderTarget;

// i call the update in the tick function myself
CaptureComp2D->bCaptureEveryFrame = false;

CaptureComp2D->MaxViewDistanceOverride = MaxViewDistance;
CaptureComp2D->SetAbsolute(false, false, true);

// This seemed to be important to get it working for mobile (in my case, i want linear colors), after they updated the Scenecapture component in 4.14.
// Also in 4.14 my target textures got flipped vertically, so i unflip them manually and reupload to my custom target texture in the ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER
CaptureComp2D->CaptureSource = SCS_SceneColorHDRNoAlpha;

CaptureComp2D->AttachToComponent(pOwner->GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
CaptureComp2D->RegisterComponent();

// As no frame has been rendered yet.
FrameRendered = -1;

```

In the Tick function

```
// Here we retrieve the render that we did in the last tick
if (FrameRendered > -1)
{
	ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER .....
}
// Here we set the location/rotation of capturecomp2d and then kick of a Render
CaptureComp2D->CaptureScene();
if(FrameRendered < 0)
	FrameRendered = 0;
else
	FrameRendered++;

```

---

<div class="post-metadata">

**Author:** ![patrickreece](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@patrickreece](https://forums.unrealengine.com/u/patrickreece)\
**Post date:** [November 23, 2020, 6:08am UTC](https://forums.unrealengine.com/t/bug-render-target-readpixels-android/374662/13 "2020-11-23T06:08:58Z")

</div>

I get the same problem in UE 4.25: reading a render target’s pixels on Android gives all 0s, no matter the format or timing of reading. I would love to know if this is supposed to be solved, or should be fixed in a later version, or will never be fixed?
