-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathPatchAllocation.compute
More file actions
227 lines (195 loc) · 10.2 KB
/
PatchAllocation.compute
File metadata and controls
227 lines (195 loc) · 10.2 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
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
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal glcore ps5
#pragma kernel Allocate
#define PATCH_UTIL_USE_RW_IRRADIANCE_BUFFER
#define PATCH_UTIL_USE_RW_CELL_INDEX_BUFFER
#define PATCH_UTIL_USE_RW_CELL_ALLOCATION_MARK_BUFFER
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Sampling/Hashes.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Lighting/SurfaceCache/VectorLogic.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Lighting/SurfaceCache/IrradianceCompression.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Lighting/SurfaceCache/PatchUtil.hlsl"
RWStructuredBuffer<uint> _RingConfigBuffer;
RWStructuredBuffer<PatchUtil::PatchGeometry> _PatchGeometries;
RWStructuredBuffer<uint> _PatchCellIndices;
RWStructuredBuffer<PatchUtil::PatchCounterSet> _PatchCounterSets;
RWStructuredBuffer<SphericalHarmonics::RGBL1> _PatchIrradiances0;
RWStructuredBuffer<SphericalHarmonics::RGBL1> _PatchIrradiances1;
RWStructuredBuffer<uint> _CellAllocationMarks;
RWStructuredBuffer<uint> _CellPatchIndices;
StructuredBuffer<int3> _CascadeOffsets;
Texture2D<float> _CurrentFullResScreenDepths;
Texture2D<float3> _CurrentFullResScreenFlatNormals;
Texture2D<float2> _CurrentFullResScreenMotionVectors;
Texture2D<float3> _PreviousLowResScreenIrradiancesL0;
Texture2D<float3> _PreviousLowResScreenIrradiancesL10;
Texture2D<float3> _PreviousLowResScreenIrradiancesL11;
Texture2D<float3> _PreviousLowResScreenIrradiancesL12;
Texture2D<float> _PreviousLowResScreenNdcDepths;
uint _FrameIdx;
uint _GridSize;
float _VoxelMinSize;
uint _CascadeCount;
uint _RingConfigOffset;
float4x4 _CurrentClipToWorldTransform;
float4x4 _PreviousClipToWorldTransform;
float3 _GridTargetPos;
uint2 _LowResScreenSize;
uint2 _FullResPixelOffset;
int _UseMotionVectorSeeding;
uint CountLeadingZeroes(uint x)
{
// Note that this assumes firstbithigh(0) = -1.
return 31 - firstbithigh(x);
}
// https://andrew-helmer.github.io/permute/
// https://graphics.pixar.com/library/MultiJitteredSampling/
uint RandomPermute(uint idx, uint len, uint seed)
{
const uint mask = 0xFFFFFFFF >> CountLeadingZeroes(len - 1);
// This max guard is required and it is not clear why.
// https://jira.unity3d.com/browse/GFXLIGHT-1616
const uint iterMax = 16;
uint iterCount = 0;
do {
idx ^= seed;
idx *= 0xe170893d;
idx ^= seed >> 16;
idx ^= (idx & mask) >> 4;
idx ^= seed >> 8;
idx *= 0x0929eb3f;
idx ^= seed >> 23;
idx ^= (idx & mask) >> 1;
idx *= 1 | seed >> 27;
idx *= 0x6935fa69;
idx ^= (idx & mask) >> 11;
idx *= 0x74dcb303;
idx ^= (idx & mask) >> 2;
idx *= 0x9e501cc3;
idx ^= (idx & mask) >> 2;
idx *= 0xc860a3df;
idx &= mask;
idx ^= idx >> 5;
iterCount++;
} while (idx >= len && iterCount < iterMax);
return idx;
}
#define GROUP_SIZE 8
[numthreads(GROUP_SIZE, GROUP_SIZE, 1)]
void Allocate(uint2 lowResPixelPos : SV_DispatchThreadID)
{
if (any(lowResPixelPos >= _LowResScreenSize))
return;
const uint lowResThreadPosIndex = lowResPixelPos.y * _LowResScreenSize.x + lowResPixelPos.x;
const uint shuffleHash = LowBiasHash32(_FrameIdx, 1); // We pass a seed of 1 to avoid the degenerate zero case.
const uint shuffledLowResThreadPosIndex = RandomPermute(lowResThreadPosIndex, _LowResScreenSize.x * _LowResScreenSize.y, shuffleHash);
lowResPixelPos = uint2(shuffledLowResThreadPosIndex % _LowResScreenSize.x, shuffledLowResThreadPosIndex / _LowResScreenSize.x);
const uint2 fullResPixelPos = lowResPixelPos * lowResScreenScaling + _FullResPixelOffset;
uint2 fullResScreenSize;
_CurrentFullResScreenDepths.GetDimensions(fullResScreenSize.x, fullResScreenSize.y);
if (any(fullResPixelPos >= fullResScreenSize))
return;
const float ndcDepth = LoadNdcDepth(_CurrentFullResScreenDepths, fullResPixelPos);
if (ndcDepth == invalidNdcDepth)
return;
const float2 rcpFullResScreenSize = rcp(float2(fullResScreenSize));
const float2 sourceUvPos = PixelPosToUvPos(fullResPixelPos, rcpFullResScreenSize);
const float3 sourceWorldPosition = ComputeWorldSpacePosition(sourceUvPos, ndcDepth, _CurrentClipToWorldTransform);
const float3 sourceWorldFlatNormal = _CurrentFullResScreenFlatNormals[fullResPixelPos];
PatchUtil::VolumePositionResolution patchPosResolution = PatchUtil::ResolveVolumePosition(sourceWorldPosition, _GridTargetPos, _GridSize, _CascadeOffsets, _CascadeCount, _VoxelMinSize);
if (patchPosResolution.isValid())
{
const uint directionIdx = PatchUtil::GetDirectionIndex(sourceWorldFlatNormal, PatchUtil::gridCellAngularResolution);
const uint3 positionStorageSpace = PatchUtil::ConvertGridSpaceToStorageSpace(patchPosResolution.positionGridSpace, _GridSize, _CascadeOffsets[patchPosResolution.cascadeIdx]);
const uint cellIdx = PatchUtil::GetCellIndex(patchPosResolution.cascadeIdx, positionStorageSpace, directionIdx, _GridSize, PatchUtil::gridCellAngularResolution);
PatchUtil::PatchIndexResolutionResult resolutionResult = PatchUtil::ResolvePatchIndex(
_RingConfigBuffer,
_RingConfigOffset,
_CellPatchIndices,
_CellAllocationMarks,
cellIdx);
if (resolutionResult.code != PatchUtil::patchIndexResolutionCodeAllocationFailure)
{
PatchUtil::PatchGeometry geo;
geo.position = sourceWorldPosition;
geo.normal = sourceWorldFlatNormal;
_PatchGeometries[resolutionResult.patchIdx] = geo;
}
if (resolutionResult.code == PatchUtil::patchIndexResolutionCodeAllocationSuccess)
{
_PatchCellIndices[resolutionResult.patchIdx] = cellIdx;
PatchUtil::PatchCounterSet counterSet;
PatchUtil::Reset(counterSet);
PatchUtil::SetLastAccessFrame(counterSet, _FrameIdx);
SphericalHarmonics::RGBL1 irradianceSeed = (SphericalHarmonics::RGBL1)0;
if (_FrameIdx != 0)
{
if (_UseMotionVectorSeeding)
{
const float2 threadUvPos = PixelPosToUvPos(fullResPixelPos, rcpFullResScreenSize);
float2 reprojectedThreadUvPos = threadUvPos - _CurrentFullResScreenMotionVectors[fullResPixelPos];
if (all(VECTOR_LOGIC_AND(0.0f < reprojectedThreadUvPos, reprojectedThreadUvPos < 1.0f)))
{
const uint2 reprojectedThreadPixelPos = reprojectedThreadUvPos * fullResScreenSize;
const uint2 seedLowResPixelPos = reprojectedThreadPixelPos / lowResScreenScaling;
const float seedNdcDepth = _PreviousLowResScreenNdcDepths[seedLowResPixelPos];
if (seedNdcDepth != invalidNdcDepth)
{
const float2 seedFullResUvPos = PixelPosToUvPos(seedLowResPixelPos * lowResScreenScaling, rcpFullResScreenSize);
const float3 seedWorldPosition = ComputeWorldSpacePosition(seedFullResUvPos, seedNdcDepth, _PreviousClipToWorldTransform);
const float voxelSize = PatchUtil::GetVoxelSize(_VoxelMinSize, patchPosResolution.cascadeIdx);
const float patchPlaneToSeedPositionDistance = dot(sourceWorldFlatNormal, seedWorldPosition - sourceWorldPosition);
if (patchPlaneToSeedPositionDistance < voxelSize)
{
irradianceSeed = IrradianceCompression::LoadAndDecompress(
_PreviousLowResScreenIrradiancesL0,
_PreviousLowResScreenIrradiancesL10,
_PreviousLowResScreenIrradiancesL11,
_PreviousLowResScreenIrradiancesL12,
seedLowResPixelPos);
if (PatchUtil::IsValid(irradianceSeed))
PatchUtil::SetUpdateCount(counterSet, PatchUtil::updateMax / 2);
#ifdef SEED_DEBUGGING
else
{
irradianceSeed = (SphericalHarmonics::RGBL1)0;
PatchUtil::SetUpdateCount(counterSet, PatchUtil::updateMax);
irradianceSeed.l0 = float3(10, 0, 0);
}
#endif
}
#ifdef SEED_DEBUGGING
else
{
PatchUtil::SetUpdateCount(counterSet, PatchUtil::updateMax);
irradianceSeed.l0 = float3(0, 0, 10);
}
#endif
}
}
}
else
{
irradianceSeed = IrradianceCompression::LoadAndDecompress(
_PreviousLowResScreenIrradiancesL0,
_PreviousLowResScreenIrradiancesL10,
_PreviousLowResScreenIrradiancesL11,
_PreviousLowResScreenIrradiancesL12,
lowResPixelPos);
if (PatchUtil::IsValid(irradianceSeed))
PatchUtil::SetUpdateCount(counterSet, PatchUtil::updateMax / 2);
}
#ifdef SEED_DEBUGGING
// For debugging: Add color to unseeded patches.
if (PatchUtil::GetUpdateCount(counterSet) == 0)
{
PatchUtil::SetUpdateCount(counterSet, PatchUtil::updateMax);
irradianceSeed.l0 = float3(0, 10, 0);
}
#endif
}
_PatchIrradiances0[resolutionResult.patchIdx] = irradianceSeed;
_PatchIrradiances1[resolutionResult.patchIdx] = irradianceSeed;
_PatchCounterSets[resolutionResult.patchIdx] = counterSet;
}
}
}