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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
// This code contains NVIDIA Confidential Information and is disclosed
// under the Mutual Non-Disclosure Agreement.
//
// Notice
// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
//
// NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. No third party distribution is allowed unless
// expressly authorized by NVIDIA. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2003 - 2016 NVIDIA Corporation. All rights reserved.
//
// NVIDIA Corporation and its licensors retain all intellectual property and proprietary
// rights in and to this software and 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.
//
/*==============================================================================
NvVolumetricLighting.h
================================================================================
NVIDIA Volumetric Lighting
-----------------------------------------
Gameworks Volumetric Lighting provides dynamic, physically-based light
scattering based on application-provided media properties and existing
shadowing information.
ENGINEERING CONTACT
Nathan Hoobler (NVIDIA Devtech)
[email protected]
==============================================================================*/
#ifndef NVVOLUMETRICLIGHTING_H
#define NVVOLUMETRICLIGHTING_H
////////////////////////////////////////////////////////////////////////////////
/*==============================================================================
NvFoundation Includes
==============================================================================*/
#include <Nv.h>
#include <NvPreprocessor.h>
#include <NvCTypes.h>
/*==============================================================================
Forward Declarations
==============================================================================*/
#if defined(NV_PLATFORM_D3D11)
struct ID3D11Device;
struct ID3D11DeviceContext;
struct ID3D11RenderTargetView;
struct ID3D11DepthStencilView;
struct ID3D11ShaderResourceView;
#endif
// [Add other platforms here]
////////////////////////////////////////////////////////////////////////////////
#if (!NV_DOXYGEN)
namespace Nv {
using namespace nvidia;
namespace VolumetricLighting {
#endif
////////////////////////////////////////////////////////////////////////////////
/*==============================================================================
Library Version
==============================================================================*/
//! Describes the library version
struct VersionDesc
{
uint32_t Major; //!< Major version of the product, changed manually with every product release with a large new feature set. API refactoring. Breaks backwards compatibility
uint32_t Minor; //!< Minor version of the product, changed manually with every minor product release containing some features. Minor API changes
uint32_t Build; //!< Very minor version of the product, mostly for bug fixing. No API changes, serialization compatible.
uint32_t Revision; //!< Latest Perforce revision of the codebase used for this build.
};
//! Current library version number
const VersionDesc LIBRARY_VERSION={1,0,0,0/*VERSIONCHANGELISTREPLACETOKEN*/};
/*==============================================================================
Platform-dependent wrappers
==============================================================================*/
//! Rendering context information for platform
union PlatformRenderCtx
{
#if defined(NV_PLATFORM_D3D11)
ID3D11DeviceContext* d3d11;
PlatformRenderCtx(ID3D11DeviceContext* x) : d3d11(x){};
operator ID3D11DeviceContext*() { return d3d11; }
#endif
// [Add other platforms here]
};
//! Render target information for platform
union PlatformRenderTarget
{
#if defined(NV_PLATFORM_D3D11)
ID3D11RenderTargetView* d3d11;
PlatformRenderTarget(ID3D11RenderTargetView* x) : d3d11(x){};
operator ID3D11RenderTargetView*() { return d3d11; }
#endif
// [Add other platforms here]
};
//! Depth-Stencil target information for platform
union PlatformDepthStencilTarget
{
#if defined(NV_PLATFORM_D3D11)
ID3D11DepthStencilView* d3d11;
PlatformDepthStencilTarget(ID3D11DepthStencilView* x) : d3d11(x){};
operator ID3D11DepthStencilView*() { return d3d11; }
#endif
// [Add other platforms here]
};
//! Surface shader resource information for platform
union PlatformShaderResource
{
#if defined(NV_PLATFORM_D3D11)
ID3D11ShaderResourceView* d3d11;
PlatformShaderResource(ID3D11ShaderResourceView* x) : d3d11(x){};
operator ID3D11ShaderResourceView*() { return d3d11; }
#endif
// [Add other platforms here]
};
/*==============================================================================
API Types and Enums
==============================================================================*/
//! Context used for an instance of the library used for rendering
typedef void * Context;
//! Return codes returned by library API functions
enum class Status
{
OK = 0, //!< Success
FAIL = -1, //!< Unspecified Failure
INVALID_VERSION = -2, //!< Mismatch between header and dll
UNINITIALIZED = -3, //!< API call made before the library has been properly initialized
UNIMPLEMENTED = -4, //!< Call not implemented for platform
INVALID_PARAMETER = -5, //!< One or more invalid parameters
UNSUPPORTED_DEVICE = -6, //!< Device doesn't support necessary features
RESOURCE_FAILURE = -7, //!< Failed to allocate a resource
API_ERROR = -8, //!< The platform API returned an error to the library
};
//! Platform/API IDs
enum class PlatformName
{
UNKNOWN = -1,
#if defined(NV_PLATFORM_D3D11)
D3D11, //!< Direct3D 11
#endif
// [Add other platforms here]
COUNT
};
//! Debug mode constants (bit flags)
enum class DebugFlags
{
NONE = 0x00000000, //!< No debug visualizations
WIREFRAME = 0x00000001, //!< Render volume as wireframe
NO_BLENDING = 0x00000002 //!< Don't blend scene into output
};
//! Specifies the godrays buffer resolution relative to framebuffer
enum class DownsampleMode
{
UNKNOWN = -1,
FULL, //!< Same resolution as framebuffer
HALF, //!< Half dimensions of framebuffer (1x downsample)
QUARTER, //!< Quarter dimensions of framebuffer (2x downsample)
COUNT
};
//! Specifies the godrays buffer sample rate
enum class MultisampleMode
{
UNKNOWN = -1,
SINGLE, //!< Single-sample
MSAA2, //!< 2x MSAA
MSAA4, //!< 4x MSAA
COUNT
};
enum class FilterMode
{
UNKNOWN = -1,
NONE, //!< No post-processing filter
TEMPORAL, //!< Temporal AA on post-process output
};
//! Phase function to use for this media term
enum class PhaseFunctionType
{
UNKNOWN = -1,
ISOTROPIC, //!< Isotropic scattering (equivalent to HG with 0 eccentricity, but more efficient)
RAYLEIGH, //!< Rayleigh scattering term (air/small molecules)
HENYEYGREENSTEIN, //!< Scattering term with variable anisotropy
MIE_HAZY, //!< Slightly forward-scattering
MIE_MURKY, //!< Densely forward-scattering
COUNT
};
//! Specifies the geometric mapping of the shadow map
enum class ShadowMapLayout
{
UNKNOWN = -1,
SIMPLE, //!< Simple frustum depth texture
CASCADE_ATLAS, //!< Multiple depth views combined into one texture
CASCADE_ARRAY, //!< Multiple depth views as texture array slices
PARABOLOID, //!< Depth mapped using paraboloid warping
COUNT
};
//! Specifies the encoding of shadow map samples
enum class ShadowMapFormat
{
UNKNOWN = -1,
DEPTH, //!< Simple depth-buffer output
COUNT
};
//! Specifies the class of light source
enum class LightType
{
UNKNOWN = -1,
DIRECTIONAL, //!< Simple analytic directional light (like the sun)
SPOTLIGHT, //!< Spotlight with frustum shadow map and angular falloff
OMNI, //!< Omni-directional local light source
COUNT
};
//! Specifies the type of distance attenuation applied to the light
enum class AttenuationMode
{
UNKNOWN = -1,
NONE, //!< No attenuation
POLYNOMIAL, //!< f(x) = 1-(A+Bx+Cx^2)
INV_POLYNOMIAL, //!< f(x) = 1/(A+Bx+Cx^2)+D
COUNT
};
//! Specifies the type of angular falloff to apply to the spotlight
enum class SpotlightFalloffMode
{
UNKNOWN = -1,
NONE, //!< No falloff (constant brightness across cone cross-section)
FIXED, //!< A_fixed(vL, vP) = (dot(vL, vP) - theta_max)/(1 - theta_max)
CUSTOM, //!< A_custom(vL, vP) = (A_fixed(vL, vP))^n
COUNT
};
//! Amount of tessellation to use
enum class TessellationQuality
{
UNKNOWN = -1,
LOW, //!< Low amount of tessellation (16x)
MEDIUM, //!< Medium amount of tessellation (32x)
HIGH, //!< High amount of tessellation (64x)
COUNT
};
//! Quality of upsampling
enum class UpsampleQuality
{
UNKNOWN = -1,
POINT, //!< Point sampling (no filter)
BILINEAR, //!< Bilinear Filtering
BILATERAL, //!< Bilateral Filtering (using depth)
COUNT
};
//! Platform-specific parameters
struct PlatformDesc
{
PlatformName platform; //!< Platform identifier
union
{
#if defined(NV_PLATFORM_D3D11) || defined(GFSDK_PLATFORM_DURANGO)
struct
{
ID3D11Device *pDevice; //!< D3D11 Device to use for context
} d3d11;
#endif
// [Add other platforms here]
};
};
//! Context description
struct ContextDesc
{
struct
{
uint32_t uWidth; //!< Width of output/depth surface
uint32_t uHeight; //!< Height of output/depth surface
uint32_t uSamples; //!< Sample rate of output/depth surface
} framebuffer;
DownsampleMode eDownsampleMode; //!< Target resolution of internal buffer
MultisampleMode eInternalSampleMode; //!< Target sample rate of internal buffer
FilterMode eFilterMode; //!< Type of filtering to do on the output
};
//! Viewer Camera/Framebuffer Description
struct ViewerDesc
{
NvcMat44 mProj; //!< Camera projection transform
NvcMat44 mViewProj; //!< Camera view-proj transform
NvcVec3 vEyePosition; //!< Camera position in world-space
uint32_t uViewportWidth; //!< Viewport Width (may differ from framebuffer)
uint32_t uViewportHeight; //!< Viewport Height (may differ from framebuffer)
};
//! Describes one component of the phase function
struct PhaseTerm
{
PhaseFunctionType ePhaseFunc; //!< Phase function this term uses
NvcVec3 vDensity; //!< Optical density in [R,G,B]
float fEccentricity; //!< Degree/direction of anisotropy (-1, 1) (HG only)
};
//! Maximum number of phase terms in a medium
const uint32_t MAX_PHASE_TERMS = 4;
//! Volume Medium Description
struct MediumDesc
{
NvcVec3 vAbsorption; //!< Absorpsive component of the medium
uint32_t uNumPhaseTerms; //!< Number of valid phase terms
//! Phase term definitions
PhaseTerm PhaseTerms[MAX_PHASE_TERMS];
};
//! Describes an individual slice in a shadow map cascade
struct ShadowMapElementDesc
{
NvcMat44 mViewProj; //!< View-Proj transform for cascade
uint32_t uOffsetX; //!< X-offset within texture
uint32_t uOffsetY; //!< Y-offset within texture
uint32_t uWidth; //!< Footprint width within texture
uint32_t uHeight; //!< Footprint height within texture
uint32_t mArrayIndex; //!< Texture array index for this element (if used)
};
//! Maximum number of sub-elements in a shadow map set
const uint32_t MAX_SHADOWMAP_ELEMENTS = 4;
//! Shadow Map Structural Description
struct ShadowMapDesc
{
ShadowMapLayout eType; //!< Shadow map structure type
uint32_t uWidth; //!< Shadow map texture width
uint32_t uHeight; //!< Shadow map texture height
uint32_t uElementCount; //!< Number of sub-elements in the shadow map
//! Individual cascade descriptions
ShadowMapElementDesc Elements[MAX_SHADOWMAP_ELEMENTS];
};
//! Light Source Description
struct LightDesc
{
LightType eType; //!< Type of light source
NvcMat44 mLightToWorld; //!< Light clip-space to world-space transform
NvcVec3 vIntensity; //!< Color of light
union
{
//! LightType = Directional
struct {
NvcVec3 vDirection; //!< Normalized light direction
} Directional;
//! LightType = Spotlight
struct {
NvcVec3 vDirection; //!< Normalized light direction
NvcVec3 vPosition; //!< Light position in world-space
float fZNear; //!< World-space distance to near view plane
float fZFar; //!< World-space distance to far view plane
SpotlightFalloffMode eFalloffMode; //!< Equation to use for angular falloff
float fFalloff_CosTheta; //!< Spotlight falloff angle
float fFalloff_Power; //!< Spotlight power
AttenuationMode eAttenuationMode; //!< Light falloff equation
float fAttenuationFactors[4]; //!< Factors in the attenuation equation
} Spotlight;
//! LightType = Omni
struct {
NvcVec3 vPosition; //!< Light position in world-space
float fZNear; //!< World-space distance to near view plane
float fZFar; //!< World-space distance to far view plane
AttenuationMode eAttenuationMode; //!< Light falloff equation
float fAttenuationFactors[4]; //!< Factors in the attenuation equation
} Omni;
};
};
//! Parameters for Volume Generation
struct VolumeDesc
{
float fTargetRayResolution; //!< Target minimum ray width in pixels
uint32_t uMaxMeshResolution; //!< Maximum geometric resolution of the mesh. Accounts for requested tessellation quality.
float fDepthBias; //!< Amount to bias ray geometry depth
TessellationQuality eTessQuality; //!< Quality level of tessellation to use
};
//! Post-Processing Behavior Description
struct PostprocessDesc
{
NvcMat44 mUnjitteredViewProj; //!< Camera view projection without jitter
float fTemporalFactor; //!< Weight of pixel history smoothing (0.0 for off)
float fFilterThreshold; //!< Threshold of frame movement to use temporal history
UpsampleQuality eUpsampleQuality; //!< Quality of upsampling to use
NvcVec3 vFogLight; //!< Light to use as "faked" multiscattering
float fMultiscatter; //<! strength of faked multiscatter effect
bool bDoFog; //!< Apply fogging based on scattering
bool bIgnoreSkyFog; //!< Ignore depth values of (1.0f) for fogging
float fBlendfactor; //!< Blend factor to use for compositing
};
/*==============================================================================
API Definition
==============================================================================*/
#define NV_VOLUMETRICLIGHTING_API(r) NV_DLL_EXPORT r NV_CALL_CONV
//! Load the library and initialize global state
NV_VOLUMETRICLIGHTING_API(Status) OpenLibrary(
NvAllocatorCallback * allocator = nullptr, //!< (opt) Memory management handler for library
NvAssertHandler * assert_handler = nullptr, //!< (opt) Assertion handler for library
const VersionDesc & link_version = LIBRARY_VERSION //!< Requested library version (do not set)
);
//! Release the library and resources, and uninitialize all global state
NV_VOLUMETRICLIGHTING_API(Status) CloseLibrary();
//! Create a new rendering interface
NV_VOLUMETRICLIGHTING_API(Status) CreateContext(
Context & out_ctx, //!< Pointer to contain newly created context
const PlatformDesc * pPlatformDesc, //!< Platform-specific data
const ContextDesc * pContextDesc //!< Context description
);
//! Release the context and any associated resources
NV_VOLUMETRICLIGHTING_API(Status) ReleaseContext(
Context & ctx //!< Library context to release
);
//! Begin accumulation of lighting volumes for a view
NV_VOLUMETRICLIGHTING_API(Status) BeginAccumulation(
Context ctx, //!< Library context to operate on
PlatformRenderCtx renderCtx, //!< Context to use for rendering
PlatformShaderResource sceneDepth, //!< Scene Depth-Buffer
ViewerDesc const* pViewerDesc, //!< Description of camera space
MediumDesc const* pMediumDesc, //!< Description of medium
DebugFlags debugFlags = DebugFlags::NONE //!< Debug flags to apply for this pass
);
//! Add a lighting volume to the accumulated results
NV_VOLUMETRICLIGHTING_API(Status) RenderVolume(
Context ctx, //!< Library context to operate on
PlatformRenderCtx renderCtx, //!< Context to use for rendering
PlatformShaderResource shadowMap, //!< Shadow map resource
ShadowMapDesc const* pShadowMapDesc, //!< Shadow map layout description
LightDesc const* pLightDesc, //!< Light source description
VolumeDesc const* pVolumeDesc //!< Parameters for volume generation
);
//! Finish accumulation of lighting volumes
NV_VOLUMETRICLIGHTING_API(Status) EndAccumulation(
Context ctx, //!< Library context to operate on
PlatformRenderCtx renderCtx //!< Context to use for rendering
);
//! Resolve the results and composite into the provided scene
NV_VOLUMETRICLIGHTING_API(Status) ApplyLighting(
Context ctx, //!< Library context to operate on
PlatformRenderCtx renderCtx, //!< Context to use for rendering
PlatformRenderTarget sceneTarget, //!< Render target to composite into
PlatformShaderResource sceneDepth, //!< Depth buffer for scene
PostprocessDesc const* pPostprocessDesc //!< Options for how to perform the resolve and composite
);
////////////////////////////////////////////////////////////////////////////////
#if (!NV_DOXYGEN)
}; /*namespace VolumetricLighting*/
namespace Vl = VolumetricLighting;
}; /*namespace Nv*/
namespace NvVl = Nv::VolumetricLighting;
#endif
////////////////////////////////////////////////////////////////////////////////
#endif // NVVOLUMETRICLIGHTING_H
|