Question

Is it possible for a pixel shader to see the current state of the depth/color/stencil buffer?

Was it helpful?

Solution

A fragment shader is not given the current buffer values for the fragment it is working on. Attempts to read these values, by using those buffers as textures, will not in the general case produce reasonable results. It's "undefined behavior."

There are certain specific cases where it can work.

First, you can use texture barriers. That is technically an NVIDIA extension, but ATI supports it widely as well. A barrier is basically a cache flush. It allows you to bind the current render targets as textures and read from them: exactly once. You can do one read, and after that, you're back to "undefined behavior" unless you use another barrier to flush the cache.

Direct image read/writes is a feature unique to GL 4.x-class hardware (aka: DX11). It allows you to arbitrarily read from and write to bound images. To do this however, you have to synchronize access between different shader instances. There are a lot of caveats to this approach, so you should read up on how to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top