diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2021-10-19 18:40:10 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2021-10-24 17:13:04 +0300 |
commit | 3db3006d4c22917cf25c12ca137d33594e323344 (patch) | |
tree | bfda4c4e37ffcd9040d2ba307bc56b2d65537056 /source/blender/draw/engines/eevee/shaders | |
parent | 008fb3eed9b8c318a8b50dc5a7e84e9cfb030656 (diff) |
EEVEE: Port back HiZbuffer
Pretty much identical.
Texture format is now always `GPU_R32F` to remove some workarounds.
Diffstat (limited to 'source/blender/draw/engines/eevee/shaders')
4 files changed, 68 insertions, 5 deletions
diff --git a/source/blender/draw/engines/eevee/shaders/eevee_deferred_direct_frag.glsl b/source/blender/draw/engines/eevee/shaders/eevee_deferred_direct_frag.glsl index 9335970ba4d..dd4d0ba4f20 100644 --- a/source/blender/draw/engines/eevee/shaders/eevee_deferred_direct_frag.glsl +++ b/source/blender/draw/engines/eevee/shaders/eevee_deferred_direct_frag.glsl @@ -46,7 +46,7 @@ layout(std140) uniform lightprobes_info_block LightProbeInfoData probes_info; }; -uniform sampler2D depth_tx; +uniform sampler2D hiz_tx; uniform sampler2D emission_data_tx; uniform sampler2D transmit_color_tx; uniform sampler2D transmit_normal_tx; @@ -84,7 +84,7 @@ vec3 lightprobe_cubemap_eval(vec3 P, vec3 R, float roughness, float random_thres void main(void) { - float gbuffer_depth = texture(depth_tx, uvcoordsvar.xy).r; + float gbuffer_depth = texelFetch(hiz_tx, ivec2(gl_FragCoord.xy), 0).r; vec3 vP = get_view_space_from_depth(uvcoordsvar.xy, gbuffer_depth); vec3 P = point_view_to_world(vP); vec3 V = cameraVec(P); diff --git a/source/blender/draw/engines/eevee/shaders/eevee_hiz_copy_frag.glsl b/source/blender/draw/engines/eevee/shaders/eevee_hiz_copy_frag.glsl new file mode 100644 index 00000000000..f0ee9f515c9 --- /dev/null +++ b/source/blender/draw/engines/eevee/shaders/eevee_hiz_copy_frag.glsl @@ -0,0 +1,17 @@ +/** + * Copy input depth texture to lower left corner of the destination, filling any padding with + * clamped texture extrapolation. + */ + +#pragma BLENDER_REQUIRE(common_view_lib.glsl) + +uniform sampler2D depth_tx; + +layout(location = 0) out float out_depth; + +void main() +{ + vec2 uv = gl_FragCoord.xy / vec2(textureSize(depth_tx, 0).xy); + + out_depth = texture(depth_tx, uv).r; +} diff --git a/source/blender/draw/engines/eevee/shaders/eevee_hiz_downsample_frag.glsl b/source/blender/draw/engines/eevee/shaders/eevee_hiz_downsample_frag.glsl new file mode 100644 index 00000000000..9a095d960b2 --- /dev/null +++ b/source/blender/draw/engines/eevee/shaders/eevee_hiz_downsample_frag.glsl @@ -0,0 +1,40 @@ +/** + * Shader that down-sample depth buffer, creating a Hierarchical-Z buffer. + * Saves max value of each 2x2 texel in the mipmap above the one we are rendering to. + * Adapted from http://rastergrid.com/blog/2010/10/hierarchical-z-map-based-occlusion-culling/ + * + * Major simplification has been made since we pad the buffer to always be bigger than input to + * avoid mipmapping misalignement. + */ + +#pragma BLENDER_REQUIRE(common_math_lib.glsl) + +uniform sampler2D depth_tx; + +uniform vec2 texel_size; + +layout(location = 0) out float out_depth; + +#ifndef GPU_ARB_texture_gather +vec4 texGather(sampler2D tex, vec2 uv) +{ + vec4 ofs = vec2(0.5, 0.5, -0.5, -0.5) * texel_size.xyxy; + return vec4(texture(tex, uv + ofs.zw).r, + texture(tex, uv + ofs.zy).r, + texture(tex, uv + ofs.xw).r, + texture(tex, uv + ofs.xy).r); +} +#else +# define texGather(a, b) textureGather(a, b) +#endif + +void main() +{ + /* NOTE(@fclem): textureSize() does not work the same on all implementations + * when changing the min and max texture levels. Use uniform instead (see T87801). */ + vec2 uv = gl_FragCoord.xy * texel_size; + + vec4 samp = texGather(depth_tx, uv); + + out_depth = max_v4(samp); +} diff --git a/source/blender/draw/engines/eevee/shaders/eevee_subsurface_eval_frag.glsl b/source/blender/draw/engines/eevee/shaders/eevee_subsurface_eval_frag.glsl index 50ec88b1990..e24c2dccdbf 100644 --- a/source/blender/draw/engines/eevee/shaders/eevee_subsurface_eval_frag.glsl +++ b/source/blender/draw/engines/eevee/shaders/eevee_subsurface_eval_frag.glsl @@ -23,7 +23,12 @@ layout(std140) uniform subsurface_block SubsurfaceData sss; }; -uniform sampler2D depth_tx; +layout(std140) uniform hiz_block +{ + HiZData hiz; +}; + +uniform sampler2D hiz_tx; uniform sampler2D radiance_tx; uniform sampler2D transmit_color_tx; uniform sampler2D transmit_normal_tx; @@ -63,7 +68,8 @@ void main(void) { vec2 center_uv = uvcoordsvar.xy; - vec3 vP = get_view_space_from_depth(center_uv, texture(depth_tx, center_uv).r); + float gbuffer_depth = texelFetch(hiz_tx, ivec2(gl_FragCoord.xy), 0).r; + vec3 vP = get_view_space_from_depth(center_uv, gbuffer_depth); vec4 tra_col_in = texture(transmit_color_tx, center_uv); vec4 tra_nor_in = texture(transmit_normal_tx, center_uv); vec4 tra_dat_in = texture(transmit_data_tx, center_uv); @@ -111,7 +117,7 @@ void main(void) vec2 sample_uv = center_uv + sample_space * sss.samples[i].xy; float pdf = sss.samples[i].z; - float sample_depth = texture(depth_tx, sample_uv).r; + float sample_depth = texture(hiz_tx, sample_uv * hiz.uv_scale).r; vec3 sample_vP = get_view_space_from_depth(sample_uv, sample_depth); vec4 sample_data = texture(radiance_tx, sample_uv); |