godot: `RenderingDevice` - `texture` functions are no longer allowed in background threads due to thread guards.

Tested versions

  • Reproducible in 4.4 (all dev versions)
  • Works reliable in 4.3

System information

Godot v4.4.dev5 - Windows 10.0.19045 - Single-window, 2 monitors - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4080 (NVIDIA; 32.0.15.6094) - AMD Ryzen 7 3700X 8-Core Processor (16 threads)

Issue description

A small intro: I tried talking to @clayjohn and @BastiaanOlij about this behaviour, but so far I couldn’t find out anything that enables me to move forward with it.

The setup in the MRP is what I essentially use in Engraving in different parts of the game to create an Image from a viewport texture in a asynchronous way, without stalling the game like a viewport.get_texture().get_image() would. For a lot of things it’s not critical that the image is instantly available but it’s more important to have a smooth game experience.

With the release of 4.4 this approach does not work at all anymore and keeps generating error messages, it works reliable in 4.3 stable.

My game is heavily depening on having a smooth way of creating an image from the players drawings to further process this image in real-time, so this change keeps me from updating the version and doing further testing on 4.4 (or even release in 4.4+).

If it is not possible to continue with this approach it would be greatly appreciated to find an alternative way of achieving async image generation from a viewport - which is also useful in a lot of other scenarios (vfx using viewports as textures/buffers).

Steps to reproduce

Checking out the MRP would be the easiest 😃

Minimal reproduction project (MRP)

rd_copy_texture.zip

EDIT

After the discussions in this issue, the main challenge appeared to be getting texture / image data from the GPU to the CPU without a stall. It’s not a regression per se - how I used to do what I needed to seemed to be a very flawed approach. The best solution would be a new function, like suggested here by Dario or here by myself.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 22 (22 by maintainers)

Most upvoted comments

An udpate from my side: I tested #100110 and it absolutely solves the challenge I had in my project in a very graceful way. I’m not sure if we should keep this issue open as there are still discussion going on.

We could probably split those other regressions I noticed into new bug reports so they can be tracked separately since the main reason for this report is now fixed.

@darksylinc I think we can make things quite simple for Godot because the current API just simply returns a PackedByteArray (which is problematic already because it requires two texture copies instead of one).

Right now you call texture_get_data(RID) and Godot will block until the data is ready, then copy it into a PackedByteArray and return it.

Instead we can add texture_get_data_async(RID, callback) which returns immediately, but calls the callback once the data is ready. The callback would always be a function with the structure func some_name(PackedByteArray) where the user can then do what they will with that array, the same as if they had called texture_get_data().

This implementation should be familiar to Godot users and very simple for us to implement. Later, we could even add an option to issue a fence or simple wait frame_count frames (could be an optional argument to texture_get_data_async()).

The downside of this approach is that it maintains our current approach of texture_get_data() copying into an intermediate buffer, then handing that buffer to the user to (potentially) copy into yet another buffer.

@clayjohn On the risk of going slightly off topic: Since Dario’s change broke multiple projects*; is it worth naming next version 4.4? But rather 5.0?

The amount of users who are direct users of RenderingDevice along with background threads is far too low to consider such a version change and we’ve done a lot worse breaking changes API-wise over the 4.x versions, so I’d say we’re fine. Even on this particular case we’ve found the answer to the problem wasn’t the fact that RenderingDevice has this new restriction but rather that the API was lacking a solution to download the image in the proper way. The OP’s methodology has a ton of performance implications we wouldn’t want to support in the long term anyway, as it effectively nullifies any amount of work that goes into optimizing our GPU throughput.

This would indeed need a new API.

In OgreNext we have AsyncTextureTicket which is conceptually similar to a Javascript promise: the user asks the engine it wants to download GPU data, and the engine returns a ticket (i.e. a handle).

The user now with that ticket can either chose to do a blocking download (which will stall), or get notified when the data is ready, receiving a callback, or poll the ticket until it returns ready.

Edit: Probably the only issue is how to determine that the ticket is no longer needed and can be thrown away (reference counted? user calls a free() function? the ticket becomes invalid after data has been obtained?).

Edit 2: In OgreNext tickets have 2 accuracy modes: inaccurate and accurate. Inaccurate means you have no problem always waiting a few frames (thus the engine can use the per frame fences to know if the download is ready). Accurate means we issue a fence to know when the download is ready. Useful when you need the data ASAP (but the user may still end up getting the data a few frames later if the GPU is too far behind the CPU, which increases the variance of waiting based on GPU load).

No, I’d suggest however making the title a bit clearer and update it with the solution we’ve discussed for any other maintainers who might see it and be unaware.

Alright, I’m not super aware about the details on how to implement something like this, but the idea has floated around long enough in our heads that it’d be useful to extend these methods to support this type of workflow. It seems 4.4’s changes warrant they’re basically a necessity at this point. CC @clayjohn

I don’t think it’d take much work to get this done in time for 4.4 if it’d provide a solution to users who relied on this functionality to work around the lack of the function in the first place.

cc @DarioSamo , you might have some insight to this? I haven’t had time to fully dive into what is going on here but I imagine we plugged some holes here especially when rendering runs on a separate thread.

Yes, the main change that has been made, for thread-safety and correctness reasons, is that a lot of RenderingDevice calls are no longer allowed. The main reason is that RenderingDevice used to do a ton of concurrent work by just doing a Mutex basically everywhere and hoping for the best. This also meant that even if you had what looked like it was asynchronous generation working, it was in fact not asynchronous at all and merely slotting in the recording of the current frame wherever it could and getting done in the current frame, making your frame actually take longer than it should.

True asynchronous GPU work takes advantage of the fact that the hardware offers multiple queues to execute the work and has a much lesser impact on the work produced on the current frame. This is why 4.4 introduces the use of transfer queues that actually run in parallel to the game, which gave a huge improvement to loading speeds in V-Sync’d scenarios.

With the release of 4.4 this approach does not work at all anymore and keeps generating error messages, it works reliable in 4.3 stable.

The problem here is this gives the false appearance that it was working reliably, while in fact it was doing it in a way that introduces a ton of possible validation errors that it is practically impossible for us to assess. GPU Validation is a bad area to risk having problems because the problems are not reliably reproduced and might show up differently on different hardware.

A major change that 4.4 brings is the possibility to actually use different command queues that the driver offers, but RenderingDevice has no API to expose this at the moment. The closest we have is creating a local rendering device, but sadly that does not allow you to share resources with the main one as it does create an entirely new different driver in the background.

I realize this might be perceived as a regression in the eyes of users, but rolling back to the old design would keep hiding away the problem instead of addressing it properly. It was an explicit request by @reduz to introduce thread guards, so I think he should weigh in suggesting the API we should use to provide some functionality like this again to do it properly.

As for unblocking your approach in 4.4 in some way even if it’s not the best solution, my suggestion would be to use the mechanism GDScript provides to defer calls to the main render thread. As I said, it was never actually processing the images in an asynchronous way anyway, as all it did was throw the processing that you requested into the main render queue for the current frame (which introduced several race conditions), so you can very much operate with a similar logic that just does deferred calls to the render thread and checks on the data on the next frame. This is what I mean by the fact that it wasn’t truly asynchronous anyway, and it was pretty evident because assets loaded in background threads were never actually loaded and had to wait for V-Sync all the time as they’d just get thrown in whatever current frame was active and had to wait it out. The same problem was probably happening with the effect you mention.

cc @DarioSamo , you might have some insight to this? I haven’t had time to fully dive into what is going on here but I imagine we plugged some holes here especially when rendering runs on a separate thread.