/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #include #include #include #include #include ShaderResourceGroup PassSrg : SRG_PerPass { Texture2DMS m_normal; // RGB10 = Normal (Encoded), A2 = Flags Texture2DMS m_depth; Sampler LinearSampler { MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = Clamp; AddressV = Clamp; AddressW = Clamp; }; // scale multiplier of the downsampled size to the fullscreen size (e.g., 4) uint m_outputImageScale; } #include // Vertex Shader VSOutput MainVS(VSInput input) { VSOutput OUT; float4 posTex = GetVertexPositionAndTexCoords(input.m_vertexID); OUT.m_texCoord = float2(posTex.z, posTex.w); OUT.m_position = float4(posTex.x, posTex.y, 0.0, 1.0); return OUT; } struct PSOutput { float m_depth : SV_Depth; float4 m_normal : SV_Target0; }; // Pixel Shader PSOutput MainPS(VSOutput IN, in uint sampleIndex : SV_SampleIndex) { uint2 screenCoords = IN.m_position.xy * PassSrg::m_outputImageScale; float downsampledDepth = 0; float4 downsampledEncodedNormal; for (uint y = 0; y < PassSrg::m_outputImageScale; ++y) { for (uint x = 0; x < PassSrg::m_outputImageScale; ++x) { float depth = PassSrg::m_depth.Load(screenCoords + int2(x, y), sampleIndex).r; float4 encodedNormal = PassSrg::m_normal.Load(screenCoords + int2(x, y), sampleIndex); // take the closest depth sample to ensure we're getting the normal closest to the viewer // (larger depth value due to reverse depth) if (depth > downsampledDepth) { downsampledDepth = depth; downsampledEncodedNormal = encodedNormal; } } } float3 downsampledNormal = DecodeNormalSignedOctahedron(downsampledEncodedNormal.rgb); PSOutput OUT; OUT.m_depth = downsampledDepth; OUT.m_normal = float4(downsampledNormal * 0.5f + 0.5f, 0.0f); return OUT; }