- Home /
How do I store a fragment shader result to give it back as input later?
I am using one fragment shader to compute some (reads a lot) of position by using floating point textures. This technique uses Graphics.Blit and RenderTexture to call a specific pass of the shader.
Here's, in short, what I'm doing:
RenderTexture RT_positions;
[...]
void Start()
{
RT_positions = new RenderTexture(nX, nY, 0, RenderTextureFormat.ARGBFloat); // Floating point texture. nX and nY are with and height of the moving samples/boids/whatever
[...]
}
void Update()
{
Graphics.Blit(null, RT_positions, kernelMaterial, 0); // compute RT_positions values using the first pass in the shader (held by kernelMaterial)
[...]
}
Later in the Update, RT_positions is uploaded to another vertex/surface shader which uses it to move the vertices.
I now need to smooth out the position and I would like to know what is the best known way to give back the fragment result as input.
As a side note: I tried to simply give RT_positions back into a shader's uniform and it didn't work as I expected but it's likely that I am doing something wrong.
Answer by tanoshimi · Mar 04, 2016 at 10:13 AM
I'm not sure what you mean that the result is "uploaded to another shader", but otherwise the process you describe is exactly how I do iterative shaders - by blitting the result to a texture, and then using that same texture as input to the next pass.
Broadly speaking, if your shader is assigned to material then you'd use code something like this:
public RenderTexture texture;
public Material material;
private RenderTexture buffer;
public void Pass()
{
// Pass the source texture to the shader, saving the output in buffer texture
Graphics.Blit(texture, buffer, material);
// Now copy the output back to the source
Graphics.Blit(buffer, texture);
}
Then call Pass() repeatedly to process the texture. Or have I misunderstood?