aboutsummaryrefslogtreecommitdiff
path: root/src/ConstantBuffers.cpp
blob: 57f51ab02de197a0ece2a6cfc2110df62ef70b4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* 
* Copyright (c) 2008-2016, NVIDIA CORPORATION. All rights reserved. 
* 
* NVIDIA CORPORATION and its licensors retain all intellectual property 
* and proprietary rights in and to this software, related documentation 
* and any modifications thereto. Any use, reproduction, disclosure or 
* distribution of this software and related documentation without an express 
* license agreement from NVIDIA CORPORATION is strictly prohibited. 
*/

#include "ConstantBuffers.h"
#include "MatrixView.h"

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetBlurConstants(const GFSDK_SSAO_BlurParameters& Params, const GFSDK::SSAO::InputDepthInfo& InputDepth)
{
    float BaseSharpness = Max(Params.Sharpness, 0.f);
    BaseSharpness /= InputDepth.MetersToViewSpaceUnits;

    if (Params.SharpnessProfile.Enable)
    {
        m_Data.fBlurViewDepth0 = Max(Params.SharpnessProfile.ForegroundViewDepth, 0.f);
        m_Data.fBlurViewDepth1 = Max(Params.SharpnessProfile.BackgroundViewDepth, m_Data.fBlurViewDepth0 + EPSILON);
        m_Data.fBlurSharpness0 = BaseSharpness * Max(Params.SharpnessProfile.ForegroundSharpnessScale, 0.f);
        m_Data.fBlurSharpness1 = BaseSharpness;
    }
    else
    {
        m_Data.fBlurSharpness0 = BaseSharpness;
        m_Data.fBlurSharpness1 = BaseSharpness;
        m_Data.fBlurViewDepth0 = 0.f;
        m_Data.fBlurViewDepth1 = 1.f;
    }
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetDepthThresholdConstants(const GFSDK_SSAO_DepthThreshold& Params)
{
    if (Params.Enable)
    {
        m_Data.fViewDepthThresholdNegInv = -1.f / Max(Params.MaxViewDepth, EPSILON);
        m_Data.fViewDepthThresholdSharpness = Max(Params.Sharpness, 0.f);
    }
    else
    {
        m_Data.fViewDepthThresholdNegInv = 0.f;
        m_Data.fViewDepthThresholdSharpness = 1.f;
    }
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetAORadiusConstants(const GFSDK_SSAO_Parameters& Params, const GFSDK::SSAO::InputDepthInfo& InputDepth)
{
    const float RadiusInMeters = Max(Params.Radius, EPSILON);
    const float R = RadiusInMeters * InputDepth.MetersToViewSpaceUnits;
    m_Data.fR2 = R * R;
    m_Data.fNegInvR2 = -1.f / m_Data.fR2;

    const float TanHalfFovy = InputDepth.ProjectionMatrixInfo.GetTanHalfFovY();
    m_Data.fRadiusToScreen = R * 0.5f / TanHalfFovy * InputDepth.Viewport.Height;

    const float BackgroundViewDepth = Max(Params.BackgroundAO.BackgroundViewDepth, EPSILON);
    m_Data.fBackgroundAORadiusPixels = m_Data.fRadiusToScreen / BackgroundViewDepth;

    const float ForegroundViewDepth = Max(Params.ForegroundAO.ForegroundViewDepth, EPSILON);
    m_Data.fForegroundAORadiusPixels = m_Data.fRadiusToScreen / ForegroundViewDepth;
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetAOParameters(const GFSDK_SSAO_Parameters& Params, const GFSDK::SSAO::InputDepthInfo& InputDepth)
{
    SetAORadiusConstants(Params, InputDepth);
    SetBlurConstants(Params.Blur, InputDepth);
    SetDepthThresholdConstants(Params.DepthThreshold);

    m_Data.fPowExponent = Clamp(Params.PowerExponent, 1.f, 8.f);
    m_Data.fNDotVBias = Clamp(Params.Bias, 0.0f, 0.5f);

    const float AOAmountScaleFactor = 1.f / (1.f - m_Data.fNDotVBias);
    m_Data.fNearAOAmount = Clamp(Params.NearAO, 1.f, 4.f) * AOAmountScaleFactor * 2.f;
    m_Data.fFarAOAmount = Clamp(Params.FarAO, 1.f, 4.f) * AOAmountScaleFactor;
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetRenderMask(GFSDK_SSAO_RenderMask RenderMask)
{
    switch (RenderMask)
    {
    case GFSDK_SSAO_RENDER_DEBUG_NORMAL_X:
        m_Data.iDebugNormalComponent = 0;
        break;
    case GFSDK_SSAO_RENDER_DEBUG_NORMAL_Y:
        m_Data.iDebugNormalComponent = 1;
        break;
    case GFSDK_SSAO_RENDER_DEBUG_NORMAL_Z:
        m_Data.iDebugNormalComponent = 2;
        break;
    default:
        m_Data.iDebugNormalComponent = 3;
        break;
    }
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetDepthData(const GFSDK::SSAO::InputDepthInfo& InputDepth)
{
    SetDepthLinearizationConstants(InputDepth);
    SetViewportConstants(InputDepth);
    SetProjectionConstants(InputDepth);
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetResolutionConstants(const GFSDK::SSAO::Viewports &Viewports)
{
    m_Data.f2InvFullResolution.X = 1.f / Viewports.FullRes.Width;
    m_Data.f2InvFullResolution.Y = 1.f / Viewports.FullRes.Height;
    m_Data.f2InvQuarterResolution.X = 1.f / Viewports.QuarterRes.Width;
    m_Data.f2InvQuarterResolution.Y = 1.f / Viewports.QuarterRes.Height;
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetProjectionConstants(const GFSDK::SSAO::InputDepthInfo& InputDepth)
{
    const float InvFocalLenX  = InputDepth.ProjectionMatrixInfo.GetTanHalfFovX();
    const float InvFocalLenY  = InputDepth.ProjectionMatrixInfo.GetTanHalfFovY();
    m_Data.f2UVToViewA.X =  2.f * InvFocalLenX;
    m_Data.f2UVToViewA.Y = -2.f * InvFocalLenY;
    m_Data.f2UVToViewB.X = -1.f * InvFocalLenX;
    m_Data.f2UVToViewB.Y =  1.f * InvFocalLenY;
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetViewportConstants(const GFSDK::SSAO::InputDepthInfo& InputDepth)
{
    // In Shaders/Src/LinearizeDepth_Common.hlsl:
    // float NormalizedDepth = saturate(g_fInverseDepthRangeA * HardwareDepth + g_fInverseDepthRangeB);

    if (InputDepth.DepthTextureType == GFSDK_SSAO_HARDWARE_DEPTHS_SUB_RANGE)
    {
        // Inverse viewport depth range from [MinZ,MaxZ] to [0,1]
        // Z = (HardwareZ - MinZ) / (MaxZ - MinZ)
        const float MinZ  = InputDepth.Viewport.MinDepth;
        const float MaxZ  = InputDepth.Viewport.MaxDepth;
        m_Data.fInverseDepthRangeA = 1.f / (MaxZ - MinZ);
        m_Data.fInverseDepthRangeB = -MinZ * m_Data.fInverseDepthRangeA;
    }
    else
    {
        m_Data.fInverseDepthRangeA = 1.f;
        m_Data.fInverseDepthRangeB = 0.f;
    }

    m_Data.f2InputViewportTopLeft.X = InputDepth.Viewport.TopLeftX;
    m_Data.f2InputViewportTopLeft.Y = InputDepth.Viewport.TopLeftY;
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetDepthLinearizationConstants(const GFSDK::SSAO::InputDepthInfo& InputDepth)
{
    // In Shaders/Src/LinearizeDepth_Common.hlsl:
    // float ViewDepth = 1.0 / (NormalizedDepth * g_fLinearizeDepthA + g_fLinearizeDepthB);

    // Inverse projection from [0,1] to [ZNear,ZFar]
    // W = 1 / [(1/ZFar - 1/ZNear) * Z + 1/ZNear]
    const float InverseZNear = InputDepth.ProjectionMatrixInfo.GetInverseZNear();
    const float InverseZFar  = InputDepth.ProjectionMatrixInfo.GetInverseZFar();
    m_Data.fLinearizeDepthA = InverseZFar - InverseZNear;
    m_Data.fLinearizeDepthB = InverseZNear;

    ASSERT((0.f * m_Data.fLinearizeDepthA + m_Data.fLinearizeDepthB) != 0.f);
    ASSERT((1.f * m_Data.fLinearizeDepthA + m_Data.fLinearizeDepthB) != 0.f);
}

//--------------------------------------------------------------------------------
void GFSDK::SSAO::GlobalConstants::SetNormalData(const GFSDK_SSAO_InputNormalData& NormalData)
{
    MatrixView WorldToView(NormalData.WorldToViewMatrix);

    for (UINT Row = 0; Row < 3; ++Row)
    {
        for (UINT Col = 0; Col < 3; ++Col)
        {
            m_Data.f44NormalMatrix.Data[Row * 4 + Col] = WorldToView(Row, Col);
        }
    }

    m_Data.fNormalDecodeScale = NormalData.DecodeScale;
    m_Data.fNormalDecodeBias = NormalData.DecodeBias;
}