-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathGlassPass.hlsl
273 lines (213 loc) · 9.16 KB
/
GlassPass.hlsl
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
/***************************************************************************
# Copyright (c) 2020-2023, 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.
**************************************************************************/
#pragma pack_matrix(row_major)
#include <donut/shaders/brdf.hlsli>
#include "ShaderParameters.h"
#include "SceneGeometry.hlsli"
ConstantBuffer<GlassConstants> g_Const : register(b0);
VK_PUSH_CONSTANT ConstantBuffer<PerPassConstants> g_PerPassConstants : register(b1);
RWTexture2D<float4> u_CompositedColor : register(u0);
RWBuffer<uint> u_RayCountBuffer : register(u1);
RaytracingAccelerationStructure SceneBVH : register(t0);
StructuredBuffer<InstanceData> t_InstanceData : register(t1);
StructuredBuffer<GeometryData> t_GeometryData : register(t2);
StructuredBuffer<MaterialConstants> t_MaterialConstants : register(t3);
Texture2D<float4> t_Emissive : register(t4);
SamplerState s_MaterialSampler : register(s0);
SamplerState s_EnvironmentSampler : register(s1);
RayDesc setupPrimaryRay(uint2 pixelPosition, PlanarViewConstants view, float TMax)
{
float2 uv = (float2(pixelPosition) + 0.5) * view.viewportSizeInv;
float4 clipPos = float4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.5, 1);
float4 worldPos = mul(clipPos, view.matClipToWorld);
worldPos.xyz /= worldPos.w;
RayDesc ray;
ray.Origin = view.cameraDirectionOrPosition.xyz;
ray.Direction = normalize(worldPos.xyz - ray.Origin);
ray.TMin = 0;
ray.TMax = TMax;
return ray;
}
struct RayPayload
{
float committedRayT;
uint instanceID;
uint geometryIndex;
uint primitiveIndex;
float2 barycentrics;
};
#if !USE_RAY_QUERY
struct RayAttributes
{
float2 uv;
};
[shader("miss")]
void Miss(inout RayPayload payload : SV_RayPayload)
{
}
[shader("closesthit")]
void ClosestHit(inout RayPayload payload : SV_RayPayload, in RayAttributes attrib : SV_IntersectionAttributes)
{
payload.committedRayT = RayTCurrent();
payload.instanceID = InstanceID();
payload.primitiveIndex = PrimitiveIndex();
payload.barycentrics = attrib.uv;
}
[shader("anyhit")]
void AnyHit(inout RayPayload payload : SV_RayPayload, in RayAttributes attrib : SV_IntersectionAttributes)
{
}
#endif
void tracePrimaryRay(inout RayPayload payload, RayDesc ray)
{
#if USE_RAY_QUERY
RayQuery<RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES | RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_CULL_BACK_FACING_TRIANGLES > rayQuery;
rayQuery.TraceRayInline(SceneBVH, RAY_FLAG_NONE, INSTANCE_MASK_TRANSPARENT, ray);
rayQuery.Proceed();
if (rayQuery.CommittedStatus() == COMMITTED_TRIANGLE_HIT)
{
payload.instanceID = rayQuery.CommittedInstanceID();
payload.primitiveIndex = rayQuery.CommittedPrimitiveIndex();
payload.barycentrics = rayQuery.CommittedTriangleBarycentrics();
payload.committedRayT = rayQuery.CommittedRayT();
}
#else
TraceRay(SceneBVH, RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES | RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_CULL_BACK_FACING_TRIANGLES, INSTANCE_MASK_TRANSPARENT, 0, 0, 0, ray, payload);
#endif
REPORT_RAY(payload.instanceID != ~0u);
}
float3 getSecondaryRadiance(float3 surfacePosition, float3 reflectedDirection)
{
RayDesc ray;
ray.Origin = surfacePosition;
ray.Direction = reflectedDirection;
ray.TMin = 0.001;
ray.TMax = 1000;
RayPayload payload = (RayPayload)0;
payload.instanceID = ~0u;
#if USE_RAY_QUERY
RayQuery<RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES | RAY_FLAG_FORCE_OPAQUE> rayQuery;
rayQuery.TraceRayInline(SceneBVH, RAY_FLAG_NONE, INSTANCE_MASK_OPAQUE, ray);
rayQuery.Proceed();
if (rayQuery.CommittedStatus() == COMMITTED_TRIANGLE_HIT)
{
payload.instanceID = rayQuery.CommittedInstanceID();
payload.primitiveIndex = rayQuery.CommittedPrimitiveIndex();
payload.barycentrics = rayQuery.CommittedTriangleBarycentrics();
payload.committedRayT = rayQuery.CommittedRayT();
}
#else
TraceRay(SceneBVH, RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES | RAY_FLAG_FORCE_OPAQUE, INSTANCE_MASK_OPAQUE, 0, 0, 0, ray, payload);
#endif
REPORT_RAY(payload.instanceID != ~0u);
if (payload.instanceID == ~0u)
{
if (g_Const.enableEnvironmentMap)
{
Texture2D environmentLatLongMap = t_BindlessTextures[g_Const.environmentMapTextureIndex];
float2 uv = directionToEquirectUV(reflectedDirection);
uv.x -= g_Const.environmentRotation;
float3 environmentColor = environmentLatLongMap.SampleLevel(s_EnvironmentSampler, uv, 0).rgb;
environmentColor *= g_Const.environmentScale;
return environmentColor;
}
else
return 0;
}
GeometrySample gs = getGeometryFromHit(
payload.instanceID,
payload.geometryIndex,
payload.primitiveIndex,
payload.barycentrics,
GeomAttr_TexCoord | GeomAttr_Normal,
t_InstanceData, t_GeometryData, t_MaterialConstants);
MaterialSample ms = sampleGeometryMaterial(gs, 0, 0, 0,
MatAttr_BaseColor | MatAttr_Emissive, s_MaterialSampler);
float3 ambient = float3(0.05, 0.045, 0.03); // very basic ambient, works OK for this sample
return ms.emissiveColor + ms.diffuseAlbedo * ambient;
}
#if USE_RAY_QUERY
[numthreads(16, 16, 1)]
void main(uint2 pixelPosition : SV_DispatchThreadID)
#else
[shader("raygeneration")]
void RayGen()
#endif
{
#if !USE_RAY_QUERY
uint2 pixelPosition = DispatchRaysIndex().xy;
#endif
float maxGlassHitT = t_Emissive[pixelPosition].a;
if(maxGlassHitT <= 0)
return;
float3 throughput = 1.0;
float3 overlay = 0.0;
RayDesc ray = setupPrimaryRay(pixelPosition, g_Const.view, maxGlassHitT + 0.01);
for (uint surfaceIndex = 0; surfaceIndex < 8; surfaceIndex++)
{
RayPayload payload = (RayPayload)0;
payload.instanceID = ~0u;
tracePrimaryRay(payload, ray);
if (payload.instanceID == ~0u)
break;
float3 surfacePosition = ray.Origin + ray.Direction * payload.committedRayT;
ray.Origin = surfacePosition + ray.Direction * 0.001; // for the next ray
ray.TMax -= payload.committedRayT;
GeometrySample gs = getGeometryFromHit(
payload.instanceID,
payload.geometryIndex,
payload.primitiveIndex,
payload.barycentrics,
GeomAttr_TexCoord | GeomAttr_Normal | GeomAttr_Tangents,
t_InstanceData, t_GeometryData, t_MaterialConstants);
MaterialSample ms = sampleGeometryMaterial(gs, 0, 0, 0, MatAttr_BaseColor | MatAttr_Normal | MatAttr_Transmission | MatAttr_Emissive,
s_MaterialSampler, g_Const.normalMapScale);
if (surfaceIndex == 0 && all(g_Const.materialReadbackPosition == int2(pixelPosition)))
{
u_RayCountBuffer[g_Const.materialReadbackBufferIndex] = gs.geometry.materialIndex + 1;
}
bool alphaMask = ms.opacity >= gs.material.alphaCutoff;
if (gs.material.domain == MaterialDomain_Transmissive ||
(gs.material.domain == MaterialDomain_TransmissiveAlphaTested && alphaMask) ||
gs.material.domain == MaterialDomain_TransmissiveAlphaBlended)
{
float3 surfaceNormal = ms.shadingNormal;
if (dot(surfaceNormal, ray.Direction) > 0)
surfaceNormal = -surfaceNormal;
// TODO: use the surface params provided by donut
float3 F0 = lerp(0.04, ms.baseColor, ms.metalness);
float3 fresnel = Schlick_Fresnel(F0, abs(dot(surfaceNormal, ray.Direction)));
float3 reflectedDirection = reflect(ray.Direction, surfaceNormal);
float3 secondaryRadiance = getSecondaryRadiance(surfacePosition, reflectedDirection);
float3 contribution = secondaryRadiance * fresnel + ms.emissiveColor;
float3 thisSurfaceThroughput = ms.transmission;
if ((gs.material.flags & MaterialFlags_UseSpecularGlossModel) == 0)
thisSurfaceThroughput *= (1.0 - ms.metalness) * ms.baseColor;
if (gs.material.domain == MaterialDomain_TransmissiveAlphaBlended)
{
contribution *= ms.opacity;
thisSurfaceThroughput *= (1.0 - ms.opacity);
}
thisSurfaceThroughput *= (1.0 - fresnel) * gs.material.baseOrDiffuseColor.rgb;
if (all(thisSurfaceThroughput == 0)) // this is a transparent-turned-opaque surface, it should be in the G-buffer
break;
overlay += contribution * throughput;
throughput *= thisSurfaceThroughput;
if (calcLuminance(throughput) < 0.01)
break;
}
}
if (any(throughput < 1.0) || any(overlay > 0.0))
{
float4 previousColor = u_CompositedColor[pixelPosition];
float3 newColor = previousColor.rgb * throughput + overlay;
u_CompositedColor[pixelPosition] = float4(newColor.rgb, previousColor.a);
}
}