1
0
Fork 0

HDR: Use a reversed depth buffer to increase precision and avoid Z-fighting

This commit is contained in:
Fernando García Liñán 2021-08-17 20:58:30 +02:00
parent 8304298146
commit f5bc6df9fb
13 changed files with 60 additions and 65 deletions

View file

@ -589,6 +589,7 @@
<type>scene</type>
<effect-scheme>hdr-geometry</effect-scheme>
<clear-mask>depth</clear-mask>
<clear-depth>0.0</clear-depth>
<cull-mask>0xfffff7ff</cull-mask>
<attachment>
<component>color0</component>

View file

@ -4,6 +4,12 @@
<technique n="110">
<scheme>hdr-geometry</scheme>
<pass>
<!-- Reverse floating point depth buffer -->
<depth>
<function>gequal</function>
<near>1.0</near>
<far>0.0</far>
</depth>
<texture-unit>
<unit>0</unit>
<type><use>texture[0]/type</use></type>

View file

@ -16,12 +16,7 @@ please see Docs/README.model-combined.eff for documentation
<normalmap-dds type="int">0</normalmap-dds>
<normalmap-tiling type="float">1.0</normalmap-tiling>
<texture n="2">
<image>Aircraft/Generic/Effects/null_bumpspec.png</image>
<type>2d</type>
<filter>linear-mipmap-linear</filter>
<wrap-s>clamp</wrap-s>
<wrap-t>clamp</wrap-t>
<internal-format>normalized</internal-format>
<type>null-normalmap</type>
</texture>
<!-- Light Map -->
<lightmap-enabled type="int">0</lightmap-enabled>
@ -1297,6 +1292,12 @@ please see Docs/README.model-combined.eff for documentation
<technique n="7">
<scheme>hdr-geometry</scheme>
<pass>
<!-- Reverse floating point depth buffer -->
<depth>
<function>gequal</function>
<near>1.0</near>
<far>0.0</far>
</depth>
<texture-unit>
<unit>0</unit>
<type><use>texture[0]/type</use></type>

View file

@ -651,33 +651,4 @@
</pass>
</technique>
<technique n="109">
<scheme>hdr-geometry</scheme>
<pass>
<cull-face><use>cull-face</use></cull-face>
<rendering-hint>opaque</rendering-hint>
<texture-unit>
<unit>0</unit>
<type><use>texture[0]/type</use></type>
<image><use>texture[0]/image</use></image>
<filter><use>texture[0]/filter</use></filter>
<wrap-s><use>texture[0]/wrap-s</use></wrap-s>
<wrap-t><use>texture[0]/wrap-t</use></wrap-t>
<internal-format><use>texture[0]/internal-format</use></internal-format>
</texture-unit>
<vertex-program-two-side>
<use>vertex-program-two-side</use>
</vertex-program-two-side>
<program>
<vertex-shader>Shaders/HDR/geometry.vert</vertex-shader>
<fragment-shader>Shaders/HDR/geometry.frag</fragment-shader>
<fragment-shader>Shaders/HDR/gbuffer-include.frag</fragment-shader>
</program>
<uniform>
<name>color_tex</name>
<type>sampler-2d</type>
<value type="int">0</value>
</uniform>
</pass>
</technique>
</PropertyList>

View file

@ -47,6 +47,12 @@
<technique n="20">
<scheme>hdr-geometry</scheme>
<pass>
<!-- Reverse floating point depth buffer -->
<depth>
<function>gequal</function>
<near>1.0</near>
<far>0.0</far>
</depth>
<texture-unit>
<unit>0</unit>
<type><use>texture[0]/type</use></type>

View file

@ -303,13 +303,13 @@
<technique n="129">
<scheme>hdr-forward</scheme>
<pass>
<cull-face>back</cull-face>
<!-- Reverse floating point depth buffer -->
<depth>
<function>lequal</function>
<near>0.0</near>
<far>1.0</far>
<write-mask>true</write-mask>
<function>greater</function>
<near>1.0</near>
<far>0.0</far>
</depth>
<cull-face>back</cull-face>
<program>
<vertex-shader>Shaders/HDR/skydome.vert</vertex-shader>
<fragment-shader>Shaders/HDR/skydome.frag</fragment-shader>
@ -335,12 +335,6 @@
<scheme>hdr-envmap</scheme>
<pass>
<cull-face>back</cull-face>
<depth>
<function>lequal</function>
<near>0.0</near>
<far>1.0</far>
<write-mask>true</write-mask>
</depth>
<program>
<vertex-shader>Shaders/HDR/skydome.vert</vertex-shader>
<fragment-shader>Shaders/HDR/skydome.frag</fragment-shader>

View file

@ -674,6 +674,13 @@
<technique n="109">
<scheme>hdr-geometry</scheme>
<pass>
<!-- Reverse floating point depth buffer -->
<depth>
<function>gequal</function>
<near>1.0</near>
<far>0.0</far>
</depth>
<blend><use>transparent</use></blend>
<alpha-test><use>transparent</use></alpha-test>
<shade-model>smooth</shade-model>

View file

@ -118,6 +118,13 @@
<technique n="109">
<scheme>hdr-geometry</scheme>
<pass>
<!-- Reverse floating point depth buffer -->
<depth>
<function>gequal</function>
<near>1.0</near>
<far>0.0</far>
</depth>
<blend><use>transparent</use></blend>
<alpha-test><use>transparent</use></alpha-test>
<shade-model>smooth</shade-model>

View file

@ -31,8 +31,8 @@ float rand(vec2 co) {
float sampleAO(vec3 fragPos, vec3 normal, vec2 coords)
{
float sampleDepth = texture(depth_tex, coords).r * 2.0 - 1.0;
vec3 samplePoint = positionFromDepth(coords * 2.0 - 1.0, sampleDepth);
float sampleDepth = texture(depth_tex, coords).r;
vec3 samplePoint = positionFromDepth(coords, sampleDepth);
vec3 diff = samplePoint - fragPos;
float l = length(diff);
@ -47,7 +47,7 @@ float sampleAO(vec3 fragPos, vec3 normal, vec2 coords)
void main()
{
float fragDepth = texture(depth_tex, texCoord).r;
vec3 fragPos = positionFromDepth(texCoord * 2.0 - 1.0, fragDepth * 2.0 - 1.0);
vec3 fragPos = positionFromDepth(texCoord, fragDepth);
vec3 normal = normalize(decodeNormal(texture(normal_tex, texCoord).rg));

View file

@ -56,7 +56,7 @@ void main()
w *= w;
float depth = w * DEPTH_RANGE;
vec3 fragPos = positionFromDepth(coord * 2.0 - 1.0, 0.0);
vec3 fragPos = positionFromDepth(coord, 1.0);
vec3 rayDir = vec4(fg_ViewMatrixInverse * vec4(normalize(fragPos), 0.0)).xyz;
float cameraHeight = length(fg_CameraPositionCart);

View file

@ -23,20 +23,26 @@ vec3 decodeNormal(vec2 enc)
return n;
}
// Given a position in clip space (values in the range [-1,1]), return
// the view space position.
// Given a 2D coordinate in the range [0,1] and a depth value from a depth
// buffer, also in the [0,1] range, return the view space position.
vec3 positionFromDepth(vec2 pos, float depth)
{
vec4 p = fg_ProjectionMatrixInverse * vec4(pos, depth, 1.0);
p.xyz /= p.w;
return p.xyz;
// We are using a reversed depth buffer. 1.0 corresponds to the near plane
// and 0.0 to the far plane. We convert this back to clip space by doing
// 1.0 - depth to undo the depth reversal
// 2.0 * depth - 1.0 to transform it to clip space [-1,1]
vec4 clipSpacePos = vec4(pos * 2.0 - 1.0, 1.0 - depth * 2.0, 1.0);
vec4 viewSpacePos = fg_ProjectionMatrixInverse * clipSpacePos;
viewSpacePos.xyz /= viewSpacePos.w;
return viewSpacePos.xyz;
}
// http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffer-in-glsl/
float linearizeDepth(float depth)
{
return (2.0 * fg_NearFar.x) / (
fg_NearFar.y + fg_NearFar.x - depth * (fg_NearFar.y - fg_NearFar.x));
float z = 1.0 - depth; // Undo the depth reversal
return 2.0 * fg_NearFar.x
/ (fg_NearFar.y + fg_NearFar.x - z * (fg_NearFar.y - fg_NearFar.x));
}
vec3 decodeSRGB(vec3 screenRGB)

View file

@ -383,17 +383,15 @@ vec3 getSunIlluminance()
void main()
{
float depth = texture(depth_tex, texCoord).r;
if (depth == 1.0) {
fragHdrColor = vec3(0.0);
return;
if (depth == 0.0) {
discard;
}
vec4 gbuffer0 = texture(gbuffer0_tex, texCoord);
vec2 gbuffer1 = texture(gbuffer1_tex, texCoord).rg;
vec4 gbuffer2 = texture(gbuffer2_tex, texCoord);
float ao = texture(ao_tex, texCoord).r;
vec3 pos = positionFromDepth(texCoord * 2.0 - 1.0, depth * 2.0 - 1.0);
vec3 pos = positionFromDepth(texCoord, depth);
vec3 v = normalize(-pos);
vec3 n = decodeNormal(gbuffer1);

View file

@ -46,11 +46,9 @@ void main()
vec3 texel = texture(atlas, vec3(st, lc)).rgb;
gbuffer0.rgb = decodeSRGB(texel) * color.rgb;
gbuffer0.rgb = decodeSRGB(texel);
gbuffer0.a = 1.0;
gbuffer1 = encodeNormal(normalVS);
float specularity = clamp(dot(specular.rgb, vec3(0.333)), 0.0, 1.0);
gbuffer2 = vec4(0.0, 1.0-specularity, 0.0, 0.0);
}