Last active
October 22, 2016 09:02
-
-
Save reinsteam/51f054bcf4ee27fdb8d041984260b4e8 to your computer and use it in GitHub Desktop.
An HLSL function for sampling a depth texture and computing min and max of 4x4 grid of texels using 4 fetches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| float Min3(float a, float b, float c) | |
| { | |
| return min(a, min(b, c)); | |
| } | |
| float Max3(float a, float b, float c) | |
| { | |
| return max(a, max(b, c)); | |
| } | |
| // Samples depth texture and computes min and max per 4x4 grid (use 4 fetches) | |
| float2 Sample4x4MinMaxZ(in Texture2D<float> DepthTexture, in SamplerState PointClampSampler, in float2 Uv, in float2 InvTexSize) | |
| { | |
| // We are going to sample 4x4 grid of texels using 4 2x2 fetches. Do do this we offset input uv by 2 texels | |
| const float u0 = Uv.x; | |
| const float v0 = Uv.y; | |
| const float u1 = Uv.x + 2.0 * InvTexSize.x; | |
| const float v1 = Uv.y + 2.0 * InvTexSize.y; | |
| // Sample 4x4 grid of texels | |
| const float4 Depth03 = DepthTexture.GatherRed(PointClampSampler, float2(u0, v0)); | |
| const float4 Depth47 = DepthTexture.GatherRed(PointClampSampler, float2(u1, v0)); | |
| const float4 Depth8B = DepthTexture.GatherRed(PointClampSampler, float2(u0, v1)); | |
| const float4 DepthCF = DepthTexture.GatherRed(PointClampSampler, float2(u1, v1)); | |
| // Compute min and max of 16 texels | |
| const float Min02 = Min3(Depth03.x, Depth03.y, Depth03.z); | |
| const float Max02 = Max3(Depth03.x, Depth03.y, Depth03.z); | |
| const float Min35 = Min3(Depth03.w, Depth47.x, Depth47.y); | |
| const float Max35 = Max3(Depth03.w, Depth47.x, Depth47.y); | |
| const float Min68 = Min3(Depth47.z, Depth47.w, Depth8B.x); | |
| const float Max68 = Max3(Depth47.z, Depth47.w, Depth8B.x); | |
| const float Min9B = Min3(Depth8B.y, Depth8B.z, Depth8B.w); | |
| const float Max9B = Max3(Depth8B.y, Depth8B.z, Depth8B.w); | |
| const float MinCE = Min3(DepthCF.x, DepthCF.y, DepthCF.z); | |
| const float MaxCE = Max3(DepthCF.x, DepthCF.y, DepthCF.z); | |
| const float Min08 = Min3(Min02, Min35, Min68); | |
| const float Max08 = Max3(Max02, Max35, Max68); | |
| const float Min9F = Min3(Min9B, MinCE, DepthCF.w); | |
| const float Max9F = Max3(Max9B, MaxCE, DepthCF.w); | |
| return float2(min(Min08, Min9F), max(Max08, Max9F)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment