# Help with multithreading and accessing render target images

**URL:** <https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 6, 2017, 8:07pm UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403 "2017-04-06T20:07:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saihv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@saihv](https://forums.unrealengine.com/u/saihv)\
**Post date:** [April 6, 2017, 8:07pm UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403/1 "2017-04-06T20:07:14Z")

</div>

I am working on an application where I need to record images seen from an in-game camera. Initially, the method was to set up a TextureRenderTargetResource from the in-game capture component, and subsequently call ReadPixels to access the frame data and save that data as an image. (code flow below)

```auto

 bool saveImage() {
      USceneCaptureComponent2D* capture = getCaptureComponent(camera_type, true);
      FTextureRenderTargetResource* RenderResource = capture->TextureTarget->GameThread_GetRenderTargetResource();
      width = capture->TextureTarget->GetSurfaceWidth();
	  height = capture->TextureTarget->GetSurfaceHeight();

	  TArray<FColor> imageColor;
	  imageColor.AddUninitialized(width * height);
	  RenderResource->ReadPixels(bmp);
    }

```

This approach was slowing the game thread down significantly, so I am trying to move the screenshot saving procedure to its own thread. My approach now is inspired by the article at [A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums](https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4). I access the \*AActor object from this separate thread and subsequently its camera, and then I call the saveImage() function from the thread: but this is resulting in a crash with no additional information (just “_UE4Editor.exe has triggered an exception_”) at the call to ReadPixels(). So essentially, calling saveImage() and thereby ReadPixels() from the game thread works perfectly, and it fails when I call it from a different thread. Am I doing something wrong in my process here? Looking for some suggestions as to how to better debug this.

EDIT: Visual Studio’s output log says :

“Assertion failed: IsInGameThread() [File: D:\Build++UE4+Release-4.15+Compile\Sync\Engine\Source\Runtime\RenderCore\Private\RenderingThread.cpp]”

So it looks like it doesn’t like the fact that it’s being called from a non-game thread… is there a way to fix this?

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [April 6, 2017, 9:20pm UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403/2 "2017-04-06T21:20:36Z")

</div>

Are you sure RenderResource isn’t null when you call it from that other thread? Right now you’re just blindly accessing it.

---

<div class="post-metadata">

**Author:** ![saihv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@saihv](https://forums.unrealengine.com/u/saihv)\
**Post date:** [April 6, 2017, 11:09pm UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403/3 "2017-04-06T23:09:18Z")

</div>

Yes, there are checks that return false is RenderResource or capture is Null. Sorry I just didn’t include them in my snippet because I was just trying to indicate the important steps.

---

<div class="post-metadata">

**Author:** ![saihv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@saihv](https://forums.unrealengine.com/u/saihv)\
**Post date:** [April 10, 2017, 8:49pm UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403/4 "2017-04-10T20:49:52Z")

</div>

Figured out the problem here: The default ReadPixels() implementation has a function call to FlushRenderingCommands(), which should be called only from a game thread. A rewrite of ReadPixels() in my own implementation was needed to fix this.

---

<div class="post-metadata">

**Author:** ![ericrius1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ericrius1](https://forums.unrealengine.com/u/ericrius1)\
**Post date:** [March 16, 2019, 9:08am UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403/5 "2019-03-16T09:08:40Z")

</div>

> [@saihv](#):
>
> Figured out the problem here: The default ReadPixels() implementation has a function call to FlushRenderingCommands(), which should be called only from a game thread. A rewrite of ReadPixels() in my own implementation was needed to fix this.

@sahiven - I’m wanting to move screenshot saving to another thread as well- we’re rendering out 8K 360 equirectangular images and when user takes screenshot game will freeze for a few seconds. We’re doing a VR game too so the freeze is extra horrible. Would you mind posting or messaging your implementation of ReadPixels ?

---

<div class="post-metadata">

**Author:** ![Pinknoise](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pinknoise](https://forums.unrealengine.com/u/Pinknoise)\
**Post date:** [April 8, 2020, 6:14pm UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403/6 "2020-04-08T18:14:57Z")

</div>

@saihv I am also interested in knowing what was your ReadPixels() implementation if you have some time to post it. I am currently trying the same thing in a Tick method and it is really slowing down the game. Obviously I am doing something wrong here.

---

<div class="post-metadata">

**Author:** ![thadweldon5](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@thadweldon5](https://forums.unrealengine.com/u/thadweldon5)\
**Post date:** [May 22, 2024, 5:47pm UTC](https://forums.unrealengine.com/t/help-with-multithreading-and-accessing-render-target-images/89403/7 "2024-05-22T17:47:07Z")

</div>

plz anyone have a code snip i need pls
