-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathPrepareLights.hlsl
210 lines (170 loc) · 8.6 KB
/
PrepareLights.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
/***************************************************************************
# 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/bindless.h>
#include <donut/shaders/binding_helpers.hlsli>
#include <donut/shaders/packing.hlsli>
#include <Rtxdi/Utils/Math.hlsli>
#include "ShaderParameters.h"
VK_PUSH_CONSTANT ConstantBuffer<PrepareLightsConstants> g_Const : register(b0);
RWStructuredBuffer<PolymorphicLightInfo> u_LightDataBuffer : register(u0);
RWBuffer<uint> u_LightIndexMappingBuffer : register(u1);
RWTexture2D<float> u_LocalLightPdfTexture : register(u2);
StructuredBuffer<PrepareLightsTask> t_TaskBuffer : register(t0);
StructuredBuffer<PolymorphicLightInfo> t_PrimitiveLights : register(t1);
StructuredBuffer<InstanceData> t_InstanceData : register(t2);
StructuredBuffer<GeometryData> t_GeometryData : register(t3);
StructuredBuffer<MaterialConstants> t_MaterialConstants : register(t4);
SamplerState s_MaterialSampler : register(s0);
VK_BINDING(0, 1) ByteAddressBuffer t_BindlessBuffers[] : register(t0, space1);
VK_BINDING(1, 1) Texture2D t_BindlessTextures[] : register(t0, space2);
#define ENVIRONMENT_SAMPLER s_MaterialSampler // doesn't matter in this pass
#define IES_SAMPLER s_MaterialSampler
#include "PolymorphicLight.hlsli"
bool FindTask(uint dispatchThreadId, out PrepareLightsTask task)
{
// Use binary search to find the task that contains the current thread's output index:
// task.lightBufferOffset <= dispatchThreadId < (task.lightBufferOffset + task.triangleCount)
int left = 0;
int right = int(g_Const.numTasks) - 1;
while (right >= left)
{
int middle = (left + right) / 2;
task = t_TaskBuffer[middle];
int tri = int(dispatchThreadId) - int(task.lightBufferOffset); // signed
if (tri < 0)
{
// Go left
right = middle - 1;
}
else if (tri < task.triangleCount)
{
// Found it!
return true;
}
else
{
// Go right
left = middle + 1;
}
}
return false;
}
[numthreads(256, 1, 1)]
void main(uint dispatchThreadId : SV_DispatchThreadID, uint groupThreadId : SV_GroupThreadID)
{
PrepareLightsTask task = (PrepareLightsTask)0;
if (!FindTask(dispatchThreadId, task))
return;
uint triangleIdx = dispatchThreadId - task.lightBufferOffset;
bool isPrimitiveLight = (task.instanceAndGeometryIndex & TASK_PRIMITIVE_LIGHT_BIT) != 0;
PolymorphicLightInfo lightInfo = (PolymorphicLightInfo)0;
if (!isPrimitiveLight)
{
InstanceData instance = t_InstanceData[task.instanceAndGeometryIndex >> 12];
GeometryData geometry = t_GeometryData[instance.firstGeometryIndex + task.instanceAndGeometryIndex & 0xfff];
MaterialConstants material = t_MaterialConstants[geometry.materialIndex];
ByteAddressBuffer indexBuffer = t_BindlessBuffers[NonUniformResourceIndex(geometry.indexBufferIndex)];
ByteAddressBuffer vertexBuffer = t_BindlessBuffers[NonUniformResourceIndex(geometry.vertexBufferIndex)];
uint3 indices = indexBuffer.Load3(geometry.indexOffset + triangleIdx * c_SizeOfTriangleIndices);
float3 positions[3];
positions[0] = asfloat(vertexBuffer.Load3(geometry.positionOffset + indices[0] * c_SizeOfPosition));
positions[1] = asfloat(vertexBuffer.Load3(geometry.positionOffset + indices[1] * c_SizeOfPosition));
positions[2] = asfloat(vertexBuffer.Load3(geometry.positionOffset + indices[2] * c_SizeOfPosition));
positions[0] = mul(instance.transform, float4(positions[0], 1)).xyz;
positions[1] = mul(instance.transform, float4(positions[1], 1)).xyz;
positions[2] = mul(instance.transform, float4(positions[2], 1)).xyz;
float3 radiance = material.emissiveColor;
if (material.emissiveTextureIndex >= 0 && geometry.texCoord1Offset != ~0u && (material.flags & MaterialFlags_UseEmissiveTexture) != 0)
{
Texture2D emissiveTexture = t_BindlessTextures[NonUniformResourceIndex(material.emissiveTextureIndex)];
// Load the vertex UVs
float2 uvs[3];
uvs[0] = asfloat(vertexBuffer.Load2(geometry.texCoord1Offset + indices[0] * c_SizeOfTexcoord));
uvs[1] = asfloat(vertexBuffer.Load2(geometry.texCoord1Offset + indices[1] * c_SizeOfTexcoord));
uvs[2] = asfloat(vertexBuffer.Load2(geometry.texCoord1Offset + indices[2] * c_SizeOfTexcoord));
// Calculate the triangle edges and edge lengths in UV space
float2 edges[3];
edges[0] = uvs[1] - uvs[0];
edges[1] = uvs[2] - uvs[1];
edges[2] = uvs[0] - uvs[2];
float3 edgeLengths;
edgeLengths[0] = length(edges[0]);
edgeLengths[1] = length(edges[1]);
edgeLengths[2] = length(edges[2]);
// Find the shortest edge and the other two (longer) edges
float2 shortEdge;
float2 longEdge1;
float2 longEdge2;
if (edgeLengths[0] < edgeLengths[1] && edgeLengths[0] < edgeLengths[2])
{
shortEdge = edges[0];
longEdge1 = edges[1];
longEdge2 = edges[2];
}
else if (edgeLengths[1] < edgeLengths[2])
{
shortEdge = edges[1];
longEdge1 = edges[2];
longEdge2 = edges[0];
}
else
{
shortEdge = edges[2];
longEdge1 = edges[0];
longEdge2 = edges[1];
}
// Use anisotropic sampling with the sample ellipse axes parallel to the short edge
// and the median from the opposite vertex to the short edge.
// This ellipse is roughly inscribed into the triangle and approximates long or skinny
// triangles with highly anisotropic sampling, and is mostly round for usual triangles.
float2 shortGradient = shortEdge * (2.0 / 3.0);
float2 longGradient = (longEdge1 + longEdge2) / 3.0;
// Sample
float2 centerUV = (uvs[0] + uvs[1] + uvs[2]) / 3.0;
float3 emissiveMask = emissiveTexture.SampleGrad(s_MaterialSampler, centerUV, shortGradient, longGradient).rgb;
radiance *= emissiveMask;
}
radiance.rgb = max(0, radiance.rgb);
TriangleLight triLight;
triLight.base = positions[0];
triLight.edge1 = positions[1] - positions[0];
triLight.edge2 = positions[2] - positions[0];
triLight.radiance = radiance;
lightInfo = triLight.Store();
}
else
{
uint primitiveLightIndex = task.instanceAndGeometryIndex & ~TASK_PRIMITIVE_LIGHT_BIT;
lightInfo = t_PrimitiveLights[primitiveLightIndex];
}
uint lightBufferPtr = task.lightBufferOffset + triangleIdx;
u_LightDataBuffer[g_Const.currentFrameLightOffset + lightBufferPtr] = lightInfo;
// If this light has existed on the previous frame, write the index mapping information
// so that temporal resampling can be applied to the light correctly when it changes
// the index inside the light buffer.
if (task.previousLightBufferOffset >= 0)
{
uint prevBufferPtr = task.previousLightBufferOffset + triangleIdx;
// Mapping buffer for the previous frame points at the current frame.
// Add one to indicate that this is a valid mapping, zero is invalid.
u_LightIndexMappingBuffer[g_Const.previousFrameLightOffset + prevBufferPtr] =
g_Const.currentFrameLightOffset + lightBufferPtr + 1;
// Mapping buffer for the current frame points at the previous frame.
// Add one to indicate that this is a valid mapping, zero is invalid.
u_LightIndexMappingBuffer[g_Const.currentFrameLightOffset + lightBufferPtr] =
g_Const.previousFrameLightOffset + prevBufferPtr + 1;
}
// Calculate the total flux
float emissiveFlux = PolymorphicLight::getPower(lightInfo);
// Write the flux into the PDF texture
uint2 pdfTexturePosition = RTXDI_LinearIndexToZCurve(lightBufferPtr);
u_LocalLightPdfTexture[pdfTexturePosition] = emissiveFlux;
}