How can I read an information from the previous iteration of fragment() execution

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dest0re

Basically, I want to see behavior similar to this:
Description
But ironically, the attachment was captured while an error in code, when everything works all wrong.
Can I get something like this with working shader code?

What I want exactly, is to read a data from a previous shader iteration for each separate pixel (last state of the current pixel).

My code isn’t complicated at all. I thought it should move the picture down and to the left every frame, but in fact it just deforms the image like shown below.


shader_type canvas_item;

void fragment() {
	COLOR.xyz = textureLod(SCREEN_TEXTURE, SCREEN_UV + vec2(0.1, 0.1), 0.0).xyz;
}

enter image description here

Help me please!

I was trying to achieve this for quite some time, and I got to conclusion, that shaders don’t work like this. It is possible, that GPU has no option to remember previous iteration. Notice, that it is impossible to even freeze ViewportTexture. If You want to stop it for one iteration You actually have to create image from it.
However most of the times it is possible to cheese it and I think your shader idea may not require previous iterations of fragment(). Main question is - what do You want to leave trace on screen ? Sprite against background ?

Inces | 2021-12-06 09:19

Thank you for your answer. I know that functionality like this may be achieved in Unity with thing named CustomRenderTexture, which uses double-buffering to get a result of the previous iteration. In Godot, we also have some sort of double-buffering, the node named BackBufferCopy.
It’s possible that I just don’t truly understand how’s Unity CustomRenderTexture works.

Globally, I’m trying to adapt shaders for thing they are probably not supposed to do. It is pixel-perfect simulations like Game of Life or sand physics.

Dest0re | 2021-12-06 10:07

Then definetely it is impossible for shaders. Even if it was possible it would be highly unoptimal to force shaders to do it instead of CPU. Game of life is basically a tilemap, no shader.

But maybe You can use something simpler :
2D Motion Blur - Godot Shaders

Inces | 2021-12-06 10:28