Graphics Reference
In-Depth Information
3.2.2
GL ARM shader framebuffer fetch depth stencil
This extension enables reading of the existing framebuffer depth and stencil val-
ues. This enables use-cases such as programmable depth/stencil testing, soft
particles, and modulating shadows. It also offers applications a very convenient
method of reconstructing 3D positions of any pixel on the screen.
The extension is enabled by adding
#
extension GL ARM shader framebuffer fetch depth stencil : enable
to the very start of a fragment shader.
See Table 3.2 for a list of the built-in variables added by the extension and
Listings 3.2 and 3.3 for example use-cases for this extension.
#extension GL ARM shader framebuffer fetch depth stencil : enable
precision mediump float
;
uniform float
uTwoXNear
;
// 2.0
near
uniform float
uFarPlusNear
;
// far + near
uniform float
uFarMinusNear
;
// far
−
near
uniform float
uInvParticleSize
;
uniform sampler2D
uParticleTexture
;
varying float
vLinearDepth
;
varying vec2
vTexCoord
;
void
main
(
void
)
{
vec4
ParticleColor
=
texture2D
(
uParticleTexture
,
vTexCoord
);
// convert from exponential depth to linear
float
LinearDepth
=
uTwoXNear
/(
uFarPlusNear
−←
gl_LastFragDepthARM
uFarMinusNear
);
// compute blend weight by subtracting current fragment depth
←
with depth in depth buffer
float
Weight
=
clamp
((
LinearDepth
−
vLinearDepth
)
←
uInvParticleSize
,0.0,1.0);
// modulate with particle alpha
ParticleColor
.
w
=
Weight
;
gl FragColor
=
ParticleColor
;
}
Listing 3.2.
Sample that uses depth read-back to render soft particles.