<p>In general you can't safely read from neighboring pixels in the render target you're currently writing to. You can sample from them, but there's no guarantees about how other pixels are scheduled on the GPU, so it's a race condition—you might get the neighboring pixel value either before or after modification, and it might change frame to frame due to unpredictable scheduling differences.</p>
<p>You can read from the same pixel you're about to write to, but only if your geometry is set up to ensure that nothing is overlapping in screen space (e.g. a full-screen pass). If other triangles in the same render pass are overlapping the same area on screen, then you have a race condition again, similar to the neighboring-pixels case. (You can use fragment_shader_interlock or equivalent functionality to address this case, but it is not universally supported across all GPU vendors, and it can also be slow.)</p>
<p>The depth and stencil buffers, as well as fixed-function blending (additive, alpha, etc) can get around this because they have special-purpose hardware that sorts the fragments in submission order in order to apply those simple fixed operations.</p>
<p>I believe the way stochastic transparency usually works is to randomize the set of pixels output and use spatial/temporal filtering to clean up the noise afterward. So you wouldn't have such fine-grained control like &quot;within each 2x2 block, there will be N pixels of this, and M pixels of that&quot;. It would be more probablistic, like &quot;on average, X% of the pixels will be this, and Y% of the pixels will be that&quot;, with the exact outputs being randomly chosen each frame.</p>

CC BY-SA 4.0
2021-03-14T21:55:33.340Z
Thank you for your answer. I guess I do not need any more than the details you have provided.
And regarding using noise for the stochastic transparency: Yes that is correct, however I have not managed to create a filter that can tidy up the image sufficiently, so I hoped I might get around that by coming up with something like the 2x2 block thing that could work.
(I will now move on to plan B: using some kind of spatial hashing function for the noise so that the sample distribution becomes more favorable for the filtering process.)
MrDropC
CC BY-SA 4.0
2021-03-14T18:34:22.383Z
2021-03-14T18:34:22.383Z
Nathan Reed
<p>I need something to be cleared up for me, and I hope this is the right place to ask my question:</p>
<p>I intend to attempt stochastic transparency, where I will draw glass panes and smoke particles onto the g-buffer for deferred shading.
For every 2x2 cell of pixels, I will allow up to 3 of the 4 pixels to be used for glass or smoke. The last pixel is always the opaque background. But say we have this situation:</p>
<ul>
<li>Camera</li>
<li>(Near clip plane)</li>
<li>Smoke particle</li>
<li>Glass pane</li>
<li>Solid wall</li>
<li>(Far clip plane)</li>
</ul>
<p>The smoke particle is in front of the glass pane, and would occupy all 3 pixels. This I do not want, and therefore I will need to check whether neighbouring pixels in that 2x2 cell already have 'smoke' or 'glass' written to them, rather than just the simple Z-check.</p>
<p>I therefore wish to sample neighbouring pixels from the same g-buffer that I'm currently writing to, but this seems not possible, am I right? And why do the depth &amp; stencil buffers seem to be exceptions to this, or does this have something to do with only reading the exact pixel you are about to write to?</p>
<p>I hope the way I formulated the question(s) make sense, and is sufficiently detailed.</p>

<rendering>
Reading neighbouring pixels during object draw call
MrDropC
# Reading neighbouring pixels during object draw call

(Asked by: username_0 on 2021-03-14)

I need something to be cleared up for me, and I hope this is the right place to ask my question:

I intend to attempt stochastic transparency, where I will draw glass panes and smoke particles onto the g-buffer for deferred shading.
For every 2x2 cell of pixels, I will allow up to 3 of the 4 pixels to be used for glass or smoke. The last pixel is always the opaque background. But say we have this situation:

* Camera
* (Near clip plane)
* Smoke particle
* Glass pane
* Solid wall
* (Far clip plane)

The smoke particle is in front of the glass pane, and would occupy all 3 pixels. This I do not want, and therefore I will need to check whether neighbouring pixels in that 2x2 cell already have 'smoke' or 'glass' written to them, rather than just the simple Z-check.

I therefore wish to sample neighbouring pixels from the same g-buffer that I'm currently writing to, but this seems not possible, am I right? And why do the depth & stencil buffers seem to be exceptions to this, or does this have something to do with only reading the exact pixel you are about to write to?

I hope the way I formulated the question(s) make sense, and is sufficiently detailed.

**Tags:** rendering


---

## 1 answers

### Answer by username_1 on 2021-03-14. Score: 3 | Accepted answer

In general you can't safely read from neighboring pixels in the render target you're currently writing to. You can sample from them, but there's no guarantees about how other pixels are scheduled on the GPU, so it's a race condition—you might get the neighboring pixel value either before or after modification, and it might change frame to frame due to unpredictable scheduling differences.

You can read from the same pixel you're about to write to, but only if your geometry is set up to ensure that nothing is overlapping in screen space (e.g. a full-screen pass). If other triangles in the same render pass are overlapping the same area on screen, then you have a race condition again, similar to the neighboring-pixels case. (You can use fragment\_shader\_interlock or equivalent functionality to address this case, but it is not universally supported across all GPU vendors, and it can also be slow.)

The depth and stencil buffers, as well as fixed-function blending (additive, alpha, etc) can get around this because they have special-purpose hardware that sorts the fragments in submission order in order to apply those simple fixed operations.

I believe the way stochastic transparency usually works is to randomize the set of pixels output and use spatial/temporal filtering to clean up the noise afterward. So you wouldn't have such fine-grained control like "within each 2x2 block, there will be N pixels of this, and M pixels of that". It would be more probablistic, like "on average, X% of the pixels will be this, and Y% of the pixels will be that", with the exact outputs being randomly chosen each frame.

> **Comments:**
> **username_0**: Thank you for your answer. I guess I do not need any more than the details you have provided. And regarding using noise for the stochastic transparency: Yes that is correct, however I have not managed to create a filter that can tidy up the image sufficiently, so I hoped I might get around that by coming up with something like the 2x2 block thing that could work. (I will now move on to plan B: using some kind of spatial hashing function for the noise so that the sample distribution becomes more favorable for the filtering process.)  

---

