4
\$\begingroup\$

I am using Unity but this might concerns all type of shaders. I would like to know if this is possible to share values between different shader pass.
Let's imagine that I am computing something in the first pass and I would like to use this value in a second pass, is it possible ?

If this is possible, how can I do that ?

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Absolutely, But not as simple as taking a return value of a normal function.

First I assume your question isn't about sharing variables between two shader stages (vertex, fragment..etc) but actually between two shader (programs).

Shaders are part of the rendering pipeline, every input/output should pass through the rendering pipeline and its memory is allocated on the GPU.

Regardless of each APIs terminology, you need to write to an intermediate buffer and pass that buffer to the next shader program. A widely used technique is to render to a texture and pass the texture into the next shader. Most of the advanced effects are implemented using a variation of this technique most; notably all of the deferred renders use this to generate of what is called a G-Buffer that contains several info about the geometry in the scene (Position, Normals, Tangents) all those are output of the geometry pass of the deffered render and encoded in buffer.

enter image description here

This G-Buffer is read by the deffered lighting stage, where all the values are read from the G-Buffer to perform what ever lighting calculations you need to perform. Other notable effects include reflections, where geometry is rendered for an offscreen buffer, and then assigned as texture with usually generated texCoords on the reflectors.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .