Rendering via subviewport causing one frame delay

Godot Version

4.3.stable

Question

I’m rendering some 2D content into a SubViewport, and then using this viewport texture to draw in the main viewport. However, I’ve noticed that when drawing the content this way there seems to be a 1 frame delay, where the updated subviewport texture is shown on screen the next frame instead of the same frame that drawing was done. This is noticable since the updated drawn content follows the mouse cursor, and this causes it to drag behind ever so slightly. If I instead draw the same content directly in a control on the main viewport this 1 frame delay does not occur.

I’m guessing this is due to some issue with the rendering or updating order of the viewports. What I’d like is for the content in the subviewport to be rendered first, updating the viewport texture, so that this is then used in the same frame and rendered in the main viewport.

But it appears that either the viewport is rendered after, or the viewport texture is updated only at the end of the frame, causing the previous frame viewport texture to be used when rendering the main viewport? Or perhaps the update of the viewport texture is not correctly marking the control where it’s rendered as needing to redraw the same frame, causing the delay?

How can I set this up correctly to have the viewport texture be rendered the same frame as the rendering is done in the subviewport?

1 Like

Did you ever find a solution to this problem? I’m having the same issue: I have a camera capturing select visibility layers around the character into a subviewport that sits on top of the character, in order to create a “punch-through” hole effect in structures the character walks behind. But the main camera and it’s main subviewport seem to be processing first, which causes the punch-through subviewport to “jitter” since it’s then out of sync with the rest of the visuals by 1 frame.

I’m hoping there’s a way to set the order in which the subviewports process that isn’t just “move them in below each other in the scene tree” because that often isn’t a reasonable possibility in complex scenes.

No, unfortunately not. I managed to get the viewports to render in sync, but only by having them all be lagging one frame behind.