-
Notifications
You must be signed in to change notification settings - Fork 606
Expand file tree
/
Copy pathGBufferRaster.3d.slang
More file actions
178 lines (156 loc) · 6.66 KB
/
GBufferRaster.3d.slang
File metadata and controls
178 lines (156 loc) · 6.66 KB
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
/***************************************************************************
# Copyright (c) 2015-24, 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 "Scene/VertexAttrib.slangh"
import Scene.Raster;
import Utils.Math.MathHelpers;
import GBufferHelpers;
import Rendering.Materials.TexLODHelpers;
cbuffer PerFrameCB
{
uint2 gFrameDim;
};
// GBuffer channels
struct GBufferPSOut
{
float4 posW : SV_TARGET0;
float4 normW : SV_TARGET1;
float4 tangentW : SV_TARGET2;
float4 faceNormalW : SV_TARGET3;
float2 texC : SV_TARGET4;
float4 texGrads : SV_TARGET5;
float2 mvec : SV_TARGET6;
uint4 mtlData : SV_TARGET7;
};
// GBufferRaster channels
RasterizerOrderedTexture2D<PackedHitInfo> gVBuffer;
RasterizerOrderedTexture2D<float4> gGuideNormalW;
RasterizerOrderedTexture2D<float4> gDiffOpacity;
RasterizerOrderedTexture2D<float4> gSpecRough;
RasterizerOrderedTexture2D<float4> gEmissive;
RasterizerOrderedTexture2D<float4> gViewW;
RasterizerOrderedTexture2D<float2> gPosNormalFwidth;
RasterizerOrderedTexture2D<float2> gLinearZAndDeriv;
RasterizerOrderedTexture2D<float> gMask;
#define is_valid(name) (is_valid_##name != 0)
VSOut vsMain(VSIn vsIn)
{
return defaultVS(vsIn);
}
float2 computeMotionVector(const VSOut vsOut, const int2 ipos)
{
// Current sample in pixel coords.
float2 pixelPos = ipos + float2(0.5, 0.5);
// Sample in previous frame in clip space coords, no jittering applied.
float4 prevPosH = vsOut.prevPosH;
// Remove camera jitter from motion vector
return calcMotionVector(pixelPos, prevPosH, gFrameDim) + float2(gScene.camera.data.jitterX, -gScene.camera.data.jitterY);
}
[earlydepthstencil]
GBufferPSOut psMain(VSOut vsOut, uint triangleIndex: SV_PrimitiveID, float3 barycentrics: SV_Barycentrics)
{
// Using vOut.posH.xy as pixel coordinate since it has the SV_Position semantic.
int2 ipos = int2(vsOut.posH.xy);
float3 faceNormal = gScene.getFaceNormalW(vsOut.instanceID, triangleIndex);
VertexData v = prepareVertexData(vsOut, faceNormal);
let lod = ImplicitLodTextureSampler();
#if USE_ALPHA_TEST
if (gScene.materials.alphaTest(v, vsOut.materialID, lod))
discard;
#endif
const float3 viewDir = normalize(gScene.camera.getPosition() - v.posW);
ShadingData sd = gScene.materials.prepareShadingData(v, vsOut.materialID, viewDir);
uint hints = 0;
#if ADJUST_SHADING_NORMALS
hints |= (uint)MaterialInstanceHints::AdjustShadingNormal;
#endif
// Create material instance and query its properties.
let mi = gScene.materials.getMaterialInstance(sd, lod, hints);
let bsdfProperties = mi.getProperties(sd);
const GBufferData gbuf = prepareGBufferData(sd, v, mi, bsdfProperties);
const float4 texGrads = float4(ddx(sd.uv), ddy(sd.uv));
const float2 mvec = computeMotionVector(vsOut, ipos);
GBufferPSOut psOut = {};
// Store render target outputs.
if (is_valid(gPosW))
psOut.posW = gbuf.posW;
if (is_valid(gNormW))
psOut.normW = gbuf.normW;
if (is_valid(gTangentW))
psOut.tangentW = gbuf.tangentW;
if (is_valid(gFaceNormalW))
psOut.faceNormalW = gbuf.faceNormalW;
if (is_valid(gTexC))
psOut.texC = gbuf.texC;
if (is_valid(gTexGrads))
psOut.texGrads = texGrads;
if (is_valid(gMotionVector))
psOut.mvec = mvec;
if (is_valid(gMaterialData))
psOut.mtlData = gbuf.mtlData;
// Store UAV outputs.
if (is_valid(gViewW))
gViewW[ipos] = float4(sd.V, 0.f);
if (is_valid(gGuideNormalW))
gGuideNormalW[ipos] = gbuf.guideNormalW;
if (is_valid(gDiffOpacity))
gDiffOpacity[ipos] = gbuf.diffuseOpacity;
if (is_valid(gSpecRough))
gSpecRough[ipos] = gbuf.specRough;
if (is_valid(gEmissive))
gEmissive[ipos] = gbuf.emissive;
if (is_valid(gMask))
gMask[ipos] = 1.f;
// Length of derivatives of position and guide normal.
// This is a feature guide for the SVGF denoiser pass.
if (is_valid(gPosNormalFwidth))
{
gPosNormalFwidth[ipos] = float2(length(fwidth(sd.posW)), length(fwidth(bsdfProperties.guideNormal)));
}
// Linear z and its derivative
if (is_valid(gLinearZAndDeriv))
{
const float linearZ = vsOut.posH.z * vsOut.posH.w;
gLinearZAndDeriv[ipos] = float2(linearZ, max(abs(ddx(linearZ)), abs(ddy(linearZ))));
}
// Note on barycentrics:
// The barycentric weights provided to pixel shader correspond to vertices A, B, C of the rasterized triangle.
// For triangle strips, every odd primitive has the order for vertices B and C flipped. We don't handle triangle
// strips as DXR does not support them and Falcor uses triangle lists exclusively in its scene representation.
// DXR intersection attributes store barycentric weights in a float2 for triangle vertices B and C.
// This is what we store in the hit info.
// Store hit information.
if (is_valid(gVBuffer))
{
TriangleHit triangleHit;
triangleHit.instanceID = vsOut.instanceID;
triangleHit.primitiveIndex = triangleIndex;
triangleHit.barycentrics = barycentrics.yz;
gVBuffer[ipos] = triangleHit.pack();
}
return psOut;
}