I’m trying to use Godot’s Subviewport and ViewportTextures to do some computations for me. Basically I’m trying to use a regular shader as a sort of compute shader and was hoping that I could have my shader generate an image of data and then I could grab that data by reading the Viewport texture. However, I’m finding when I update the parameters of my shader and then get the VewiportTexture, often it has not updated.
I’ve added lines with await RenderingServer.frame_post_draw between setting my shader parameters and getting the viewport texture, but this does not seem to help. Is there any way force the viewport to render to the image and then get the image data so that I can consistently get the data I’m creating?
Is it not possible for you to use a compute shader?
Viewports always update exactly once per frame, so after await get_tree().process_frame the texture should be updated; I don’t know of a way to force it to update early though.
Compute shaders are complex and poorly documented. I’ve created a few, but the amount of code you have to write just to add a texture sampler or update a uniform is ridiculous. Most of this is not documented either (at least not any place that I can find, aside from this simple example: Using compute shaders — Godot Engine (latest) documentation in English), so I’ve had to make guesses based on the code of the few other programs using compute shaders I’ve been able to find.
Compute shaders are also not supported for web export, which is another disincentive.