How to quickly read data from a compute shader?

I want to read data from a compute shader in the CPU side. I created a StorageBuffer whose length was just one and passed it to the compute shader.

It will take about 20 ms to get the data which is too slow. Is there any way to shorten the time to 5 ms?

1 Like

Hello :slight_smile:

I had a look, and I can notice 2 things :


  1. Your data read here is somehow launched on the very first frame even before scene is returned, but if you actually run for example onAfterRenderObservable, it would take ~5ms :


  1. Also, after a look at the source code I found this :

You can add the param noDelay, which would avoid to run the read on theengine.onEndFrameObservable, and then I go down to ~2ms

5 Likes

You are great. Thank you so much! :+1:

Reading data to the CPU is known to be slow and occurs within a promise, so data won’t be available until the end of the current frame, at best. Unfortunately, there’s no way around this, so you should try as far as possible not to rely on getting data from the CPU side.

If you really need the data on the CPU, you should be prepared to get the data this frame, the next frame or even frame+n: there’s no guarantee when the data will be available (although in practice the data should be available a few milliseconds after the job has been submitted to the GPU)…

2 Likes

I want to peel all the depth of a mesh(or meshes) in the GPU and determine when to end the peeling. Do you have any better idea?

Unfortunately not. After looking into the problem a bit, it seems that people settle for a fixed number of steps (as we do in our own OIT implementation with deep peeling).

1 Like

Yes. I found the passCount is 5 in the source code.

It’s the default value, but you can change it.

1 Like