forked from NVIDIAGameWorks/FaceWorks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.cpp
275 lines (241 loc) · 9.22 KB
/
runtime.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//----------------------------------------------------------------------------------
// File: FaceWorks/src/runtime.cpp
// SDK Version: v1.0
// Email: [email protected]
// Site: http://developer.nvidia.com/
//
// Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//----------------------------------------------------------------------------------
#include "internal.h"
#include <cassert>
// ======================================================================================
// Runtime API for SSS
// ======================================================================================
GFSDK_FaceWorks_Result ValidateSSSConfig(
const GFSDK_FaceWorks_SSSConfig * pConfig,
GFSDK_FaceWorks_ErrorBlob * pErrorBlobOut)
{
if (!pConfig)
{
ErrPrintf("pConfig is null\n");
return GFSDK_FaceWorks_InvalidArgument;
}
if (pConfig->m_diffusionRadius <= 0.0f)
{
WarnPrintf(
"pConfig->m_diffusionRadius is %g; should be greater than 0.0\n",
pConfig->m_diffusionRadius);
}
if (pConfig->m_diffusionRadiusLUT <= 0.0f)
{
WarnPrintf(
"pConfig->m_diffusionRadiusLUT is %g; should be greater than 0.0\n",
pConfig->m_diffusionRadius);
}
if (pConfig->m_curvatureRadiusMinLUT <= 0.0f)
{
WarnPrintf(
"pConfig->m_curvatureRadiusMinLUT is %g; should be greater than 0.0\n",
pConfig->m_curvatureRadiusMinLUT);
}
if (pConfig->m_curvatureRadiusMaxLUT <= 0.0f)
{
WarnPrintf(
"pConfig->m_curvatureRadiusMaxLUT is %g; should be greater than 0.0\n",
pConfig->m_curvatureRadiusMaxLUT);
}
if (pConfig->m_curvatureRadiusMaxLUT < pConfig->m_curvatureRadiusMinLUT)
{
WarnPrintf(
"pConfig->m_curvatureRadiusMaxLUT (%g) is less than pConfig->m_curvatureRadiusMinLUT (%g)\n",
pConfig->m_curvatureRadiusMaxLUT, pConfig->m_curvatureRadiusMinLUT);
}
if (pConfig->m_shadowWidthMinLUT <= 0.0f)
{
WarnPrintf(
"pConfig->m_shadowWidthMinLUT is %g; should be greater than 0.0\n",
pConfig->m_shadowWidthMinLUT);
}
if (pConfig->m_shadowWidthMaxLUT <= 0.0f)
{
WarnPrintf(
"pConfig->m_shadowWidthMaxLUT is %g; should be greater than 0.0\n",
pConfig->m_shadowWidthMaxLUT);
}
if (pConfig->m_shadowWidthMaxLUT < pConfig->m_shadowWidthMinLUT)
{
WarnPrintf(
"pConfig->m_shadowWidthMaxLUT (%g) is less than pConfig->m_shadowWidthMinLUT (%g)\n",
pConfig->m_shadowWidthMaxLUT, pConfig->m_shadowWidthMinLUT);
}
if (pConfig->m_shadowFilterWidth <= 0.0f)
{
WarnPrintf(
"pConfig->m_shadowFilterWidth is %g; should be greater than 0.0\n",
pConfig->m_shadowFilterWidth);
}
if (pConfig->m_normalMapSize < 0)
{
WarnPrintf(
"pConfig->m_normalMapSize is %d; should be at least 0\n",
pConfig->m_normalMapSize);
}
if (pConfig->m_averageUVScale <= 0.0f)
{
WarnPrintf(
"pConfig->m_averageUVScale is %g; should be greater than 0.0\n",
pConfig->m_averageUVScale);
}
return GFSDK_FaceWorks_OK;
}
GFSDK_FACEWORKS_API GFSDK_FaceWorks_Result GFSDK_FACEWORKS_CALLCONV GFSDK_FaceWorks_WriteCBDataForSSS(
const GFSDK_FaceWorks_SSSConfig * pConfig,
GFSDK_FaceWorks_CBData * pCBDataOut,
GFSDK_FaceWorks_ErrorBlob * pErrorBlobOut)
{
// Validate params
GFSDK_FaceWorks_Result res = ValidateSSSConfig(pConfig, pErrorBlobOut);
if (res != GFSDK_FaceWorks_OK)
return res;
if (!pCBDataOut)
{
ErrPrintf("GFSDK_FaceWorks_WriteCBDataForSSS: pCBDataOut is null\n");
return GFSDK_FaceWorks_InvalidArgument;
}
// The LUTs are built assuming a particular scattering radius, so the
// curvatures and penumbra widths need to be scaled to match the
// scattering radius set at runtime.
float diffusionRadiusFactor = pConfig->m_diffusionRadiusLUT /
pConfig->m_diffusionRadius;
// Set up the constant buffer
float curvatureMin = diffusionRadiusFactor / pConfig->m_curvatureRadiusMaxLUT;
float curvatureMax = diffusionRadiusFactor / pConfig->m_curvatureRadiusMinLUT;
float curvatureScale = 1.0f / (curvatureMax - curvatureMin);
float curvatureBias = -curvatureMin * curvatureScale;
float shadowRcpWidthMin = diffusionRadiusFactor / pConfig->m_shadowWidthMaxLUT;
float shadowRcpWidthMax = diffusionRadiusFactor / pConfig->m_shadowWidthMinLUT;
float shadowScale = 1.0f / (shadowRcpWidthMax - shadowRcpWidthMin);
float shadowBias = -shadowRcpWidthMin * shadowScale;
float minLevelForBlurredNormal = log2(max(
pConfig->m_normalMapSize * pConfig->m_diffusionRadius,
pConfig->m_averageUVScale)
/ pConfig->m_averageUVScale);
// Output to user buffer
pCBDataOut->data[0].x = curvatureScale;
pCBDataOut->data[0].y = curvatureBias;
pCBDataOut->data[0].z = shadowScale / pConfig->m_shadowFilterWidth;
pCBDataOut->data[0].w = shadowBias;
pCBDataOut->data[1].x = minLevelForBlurredNormal;
return GFSDK_FaceWorks_OK;
}
// ======================================================================================
// Runtime API for deep scatter
// ======================================================================================
GFSDK_FaceWorks_Result ValidateDeepScatterConfig(
const GFSDK_FaceWorks_DeepScatterConfig * pConfig,
GFSDK_FaceWorks_ErrorBlob * pErrorBlobOut)
{
if (!pConfig)
{
ErrPrintf("pConfig is null\n");
return GFSDK_FaceWorks_InvalidArgument;
}
if (pConfig->m_radius <= 0.0f)
{
WarnPrintf(
"pConfig->m_radius is %g; should be greater than 0.0\n",
pConfig->m_radius);
}
if (pConfig->m_shadowProjType != GFSDK_FaceWorks_NoProjection &&
pConfig->m_shadowProjType != GFSDK_FaceWorks_ParallelProjection &&
pConfig->m_shadowProjType != GFSDK_FaceWorks_PerspectiveProjection)
{
ErrPrintf(
"pConfig->m_shadowProjType is %d; not a valid GFSDK_FaceWorks_ProjectionType enum value\n",
pConfig->m_shadowProjType);
return GFSDK_FaceWorks_InvalidArgument;
}
if (pConfig->m_shadowProjType != GFSDK_FaceWorks_NoProjection)
{
// Error checking for shadow parameters is done only if enabled by the projection type
if (pConfig->m_shadowFilterRadius < 0.0f)
{
WarnPrintf(
"pConfig->m_shadowFilterRadius is %g; should be at least 0.0\n",
pConfig->m_shadowFilterRadius);
}
// Should we check that m_shadowProjMatrix is of the expected form
// (e.g. has zeros in the expected places)?
}
return GFSDK_FaceWorks_OK;
}
GFSDK_FACEWORKS_API GFSDK_FaceWorks_Result GFSDK_FACEWORKS_CALLCONV GFSDK_FaceWorks_WriteCBDataForDeepScatter(
const GFSDK_FaceWorks_DeepScatterConfig * pConfig,
GFSDK_FaceWorks_CBData * pCBDataOut,
GFSDK_FaceWorks_ErrorBlob * pErrorBlobOut)
{
// Validate params
GFSDK_FaceWorks_Result res = ValidateDeepScatterConfig(pConfig, pErrorBlobOut);
if (res != GFSDK_FaceWorks_OK)
return res;
if (!pCBDataOut)
{
ErrPrintf("pCBDataOut is null\n");
return GFSDK_FaceWorks_InvalidArgument;
}
// Set up the constant buffer
// -0.7213475f = -1 / (2 * ln(2)) - this conversion lets us write this in the shader:
// exp2(deepScatterFalloff * thickness^2)
// instead of this:
// exp(-thickness^2 / (2 * radius^2))
float deepScatterFalloff = -0.7213475f / (pConfig->m_radius * pConfig->m_radius);
// Calculate depth-decoding parameters from the projection matrix
float decodeDepthScale = 0.0f;
float decodeDepthBias = 0.0f;
switch (pConfig->m_shadowProjType)
{
case GFSDK_FaceWorks_NoProjection:
break; // Nothing to do
case GFSDK_FaceWorks_ParallelProjection:
decodeDepthScale = -1.0f / pConfig->m_shadowProjMatrix._33;
break;
case GFSDK_FaceWorks_PerspectiveProjection:
decodeDepthScale = pConfig->m_shadowProjMatrix._34 / pConfig->m_shadowProjMatrix._43;
decodeDepthBias = -pConfig->m_shadowProjMatrix._33 / pConfig->m_shadowProjMatrix._43;
break;
default:
assert(false);
return GFSDK_FaceWorks_InvalidArgument;
}
// Output to user buffer
pCBDataOut->data[1].y = deepScatterFalloff;
pCBDataOut->data[1].z = pConfig->m_shadowFilterRadius;
pCBDataOut->data[1].w = decodeDepthScale;
pCBDataOut->data[2].x = decodeDepthBias;
return GFSDK_FaceWorks_OK;
}