-
Notifications
You must be signed in to change notification settings - Fork 867
Expand file tree
/
Copy pathPatchUtil.hlsl
More file actions
387 lines (333 loc) · 15.9 KB
/
PatchUtil.hlsl
File metadata and controls
387 lines (333 loc) · 15.9 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
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
#ifndef SURFACE_CACHE_PATCH_UTIL
#define SURFACE_CACHE_PATCH_UTIL
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl"
#include "VectorLogic.hlsl"
#include "Common.hlsl"
#include "RingBuffer.hlsl"
#if defined(PATCH_UTIL_USE_RW_IRRADIANCE_BUFFER)
#define IrradianceBufferType RWStructuredBuffer<SphericalHarmonics::RGBL1>
#else
#define IrradianceBufferType StructuredBuffer<SphericalHarmonics::RGBL1>
#endif
#if defined(PATCH_UTIL_USE_RW_CELL_INDEX_BUFFER)
#define CellPatchIndexBufferType RWStructuredBuffer<uint>
#else
#define CellPatchIndexBufferType StructuredBuffer<uint>
#endif
#if defined(PATCH_UTIL_USE_RW_CELL_ALLOCATION_MARK_BUFFER)
#define CellAllocationMarkBufferType RWStructuredBuffer<uint>
#else
#define CellAllocationMarkBufferType StructuredBuffer<uint>
#endif
namespace PatchUtil
{
static const uint invalidPatchIndex = UINT_MAX; // Must match C# side.
static const uint invalidCellIndex = UINT_MAX; // Must match C# side.
static const uint gridCellAngularResolution = 4; // Must match C# side.
static const float3 invalidIrradiance = float3(-1, -1, -1);
static const uint updateMax = 32;
struct PatchGeometry
{
float3 position;
float3 normal;
};
struct PatchStatisticsSet
{
float3 mean;
float3 variance;
};
struct PatchCounterSet
{
uint data;
};
void Reset(inout PatchCounterSet set)
{
set.data = 0;
}
uint GetUpdateCount(PatchCounterSet set)
{
return set.data & 0xFFFF;
}
uint GetLastAccessFrame(PatchCounterSet set)
{
return set.data >> 16;
}
void SetUpdateCount(inout PatchCounterSet set, uint updateCount)
{
set.data = updateCount | (set.data & 0xFFFF0000);
}
void SetLastAccessFrame(inout PatchCounterSet set, uint lastAccessFrame)
{
set.data = (lastAccessFrame << 16) | (set.data & 0xFFFF);
}
bool IsEqual(PatchCounterSet a, PatchCounterSet b)
{
return a.data == b.data;
}
void WriteLastFrameAccess(RWStructuredBuffer<PatchUtil::PatchCounterSet> counterSets, uint patchIdx, uint frameIdx)
{
PatchCounterSet counterSet = counterSets[patchIdx];
SetLastAccessFrame(counterSet, frameIdx);
counterSets[patchIdx] = counterSet;
}
float GetVoxelSize(float voxelMinSize, uint cascadeIdx)
{
return voxelMinSize * (1u << cascadeIdx);
}
float2 OctWrap(float2 v)
{
return (1.0 - abs(v.yx)) * VECTOR_LOGIC_SELECT(v.xy >= 0.0, 1.0, -1.0);
}
float2 SphereToSquare(float3 n)
{
n /= (abs(n.x) + abs(n.y) + abs(n.z));
n.xy = VECTOR_LOGIC_SELECT(n.z >= 0.0, n.xy, OctWrap(n.xy));
n.xy = n.xy * 0.5 + 0.5;
return n.xy;
}
struct VolumePositionResolution
{
uint cascadeIdx;
uint3 positionGridSpace;
void markInvalid()
{
positionGridSpace = UINT_MAX;
}
bool isValid()
{
return all(positionGridSpace != UINT_MAX);
}
};
uint GetCellIndex(uint cascadeIdx, uint3 positionStorageSpace, uint directionIndex, uint gridSize, uint angularResolution)
{
const uint angularResolutionSquared = angularResolution * angularResolution;
const uint gridSizeSquared = gridSize * gridSize;
const uint cellsPerCascade = gridSizeSquared * gridSize * angularResolutionSquared;
const uint withinCascadeIdx = angularResolutionSquared * (positionStorageSpace.x * gridSizeSquared + positionStorageSpace.y * gridSize + positionStorageSpace.z) + directionIndex;
return cellsPerCascade * cascadeIdx + withinCascadeIdx;
}
uint GetDirectionIndex(float3 direction, uint angularResolution)
{
// To avoid discontinuities near the cardinal axis directions, we apply an arbitrary rotation.
// This is based on the assumption that surfaces oriented along the cardinal axis directions
// are most likely in a scene compared to other directions.
const float3x3 arbitraryRotation = float3x3(
float3(0.34034f, -0.30925f, 0.888f),
float3(-0.30925f, 0.85502f, 0.41629f),
float3(-0.888f, -0.41629f, 0.19536f));
const float3 rotatedDirection = mul(arbitraryRotation, direction);
const uint2 angularSquarePos = min(uint2(3, 3), SphereToSquare(rotatedDirection) * angularResolution);
return angularSquarePos.y * angularResolution + angularSquarePos.x;
}
// Unlike the regular HLSL % operator where both operands must both be signed or unsigned,
// this function additionally supports the case where the first argument is negative and
// the second argument is positive.
uint3 SignedIntegerModulo(int3 x, uint modulus)
{
const uint3 remainder = uint3(abs(x)) % modulus;
return VECTOR_LOGIC_SELECT(VECTOR_LOGIC_AND(x < 0, remainder != 0), modulus - remainder, remainder);
}
uint3 ConvertGridSpaceToStorageSpace(uint3 posGridSpace, uint gridSize, int3 cascadeOffset)
{
return SignedIntegerModulo(int3(posGridSpace) + cascadeOffset, gridSize);
}
uint3 ConvertStorageSpaceToGridSpace(uint3 posStorageSpace, uint gridSize, int3 cascadeOffset)
{
return SignedIntegerModulo(int3(posStorageSpace) - cascadeOffset, gridSize);
}
bool IsInsideCascade(float3 gridTargetPos, float3 queryPos, float cascadeVoxelSize, uint gridSize)
{
const float3 dif = gridTargetPos - queryPos;
const float difSquaredLength = dot(dif, dif);
// We subtract 0.5 here to account for the fact that the Grid Target Pos can move up to
// 0.499... voxel sizes away from the cascade center in any dimension without causing the
// cascade to move.
const float threshold = cascadeVoxelSize * (float(gridSize) * 0.5f - 0.5f);
const float squaredThreshold = threshold * threshold;
return difSquaredLength < squaredThreshold;
}
VolumePositionResolution ResolveVolumePosition(float3 queryPos, float3 gridTargetPos, uint gridSize, StructuredBuffer<int3> cascadeOffsets, uint cascadeCount, float voxelMinSize, uint startCascadeIdx = 0)
{
VolumePositionResolution resolution = (VolumePositionResolution)0; // Zero initialization is strictly not required but this silences a shader compiler warning.
resolution.markInvalid();
const float halfGridSize = float(gridSize) * 0.5f;
for (uint cascadeIdx = startCascadeIdx; cascadeIdx < cascadeCount; ++cascadeIdx)
{
const float cascadeVoxelSize = GetVoxelSize(voxelMinSize, cascadeIdx);
if (IsInsideCascade(gridTargetPos, queryPos, cascadeVoxelSize, gridSize))
{
const int3 cascadeOffset = cascadeOffsets[cascadeIdx];
const float3 centerRelativePositionSpatialGridSpace = queryPos / cascadeVoxelSize - cascadeOffset;
resolution.positionGridSpace = centerRelativePositionSpatialGridSpace + halfGridSize;
resolution.cascadeIdx = cascadeIdx;
break;
}
}
return resolution;
}
int ResolveCascadeIndex(float3 gridTargetPos, float3 queryPos, uint gridSize, uint cascadeCount, float voxelMinSize)
{
int result = -1;
for (uint cascadeIdx = 0; cascadeIdx < cascadeCount; ++cascadeIdx)
{
const float cascadeVoxelSize = GetVoxelSize(voxelMinSize, cascadeIdx);
if (IsInsideCascade(gridTargetPos, queryPos, cascadeVoxelSize, gridSize))
{
result = cascadeIdx;
break;
}
}
return result;
}
static const uint patchIndexResolutionCodeLookup = 0;
static const uint patchIndexResolutionCodeAllocationSuccess = 1;
static const uint patchIndexResolutionCodeAllocationFailure = 2;
struct PatchIndexResolutionResult
{
uint code;
uint patchIdx;
};
PatchIndexResolutionResult ResolvePatchIndex(RWStructuredBuffer<uint> ringConfigBuffer, uint ringConfigOffset, RWStructuredBuffer<uint> cellPatchIndices, RWStructuredBuffer<uint> cellAllocationMarks, uint cellIdx)
{
PatchIndexResolutionResult result;
result.patchIdx = invalidPatchIndex;
uint existingPatchIndex = cellPatchIndices[cellIdx];
if (existingPatchIndex != invalidPatchIndex)
{
result.patchIdx = existingPatchIndex;
result.code = patchIndexResolutionCodeLookup;
}
else
{
result.code = patchIndexResolutionCodeAllocationFailure;
uint existingAllocationMark;
InterlockedExchange(cellAllocationMarks[cellIdx], 1, existingAllocationMark);
if (existingAllocationMark == 0)
{
uint countBeforeAllocation;
InterlockedAdd(ringConfigBuffer[ringConfigOffset + RingBuffer::countConfigIndex], 1, countBeforeAllocation);
if (countBeforeAllocation < patchCapacity)
{
uint newPatchIdx;
InterlockedAdd(ringConfigBuffer[ringConfigOffset + RingBuffer::endConfigIndex], 1, newPatchIdx);
newPatchIdx %= patchCapacity; // Here we exploit the requirement that UINT_MAX is a multiple of patchCapacity.
result.code = patchIndexResolutionCodeAllocationSuccess;
result.patchIdx = newPatchIdx;
cellPatchIndices[cellIdx] = newPatchIdx;
}
else
{
// Allocation failed, no room. Backing out.
ringConfigBuffer[ringConfigOffset + RingBuffer::countConfigIndex] = patchCapacity;
cellAllocationMarks[cellIdx] = 0;
}
}
}
return result;
}
bool ReadHemisphericalIrradiance(IrradianceBufferType patchIrradiances, CellPatchIndexBufferType cellPatchIndices, uint gridSize, uint cascadeIdx, uint3 gridSpacePosition, float3 worldNormal, out SphericalHarmonics::RGBL1 resultIrradiance)
{
const uint directionIdx = GetDirectionIndex(worldNormal, gridCellAngularResolution);
const uint cellIdx = GetCellIndex(cascadeIdx, gridSpacePosition, directionIdx, gridSize, gridCellAngularResolution);
bool resultBool = false;
const uint patchIdx = cellPatchIndices[cellIdx];
resultIrradiance = (SphericalHarmonics::RGBL1)0; // Setting value only to silence shader compilation warning.
if (patchIdx != invalidPatchIndex)
{
resultIrradiance = patchIrradiances[patchIdx];
resultBool = true;
}
return resultBool;
}
uint FindPatchIndex(float3 gridTargetPos, StructuredBuffer<uint> cellPatchIndices, uint gridSize, StructuredBuffer<int3> cascadeOffsets, uint cascadeCount, float voxelMinSize, float3 worldPosition, float3 worldNormal)
{
VolumePositionResolution posResolution = ResolveVolumePosition(worldPosition, gridTargetPos, gridSize, cascadeOffsets, cascadeCount, voxelMinSize);
if (posResolution.isValid())
{
const uint directionIdx = GetDirectionIndex(worldNormal, gridCellAngularResolution);
const uint3 positionStorageSpace = ConvertGridSpaceToStorageSpace(posResolution.positionGridSpace, gridSize, cascadeOffsets[posResolution.cascadeIdx]);
const uint cellIdx = GetCellIndex(posResolution.cascadeIdx, positionStorageSpace, directionIdx, gridSize, gridCellAngularResolution);
const uint patchIdx = cellPatchIndices[cellIdx];
if (patchIdx != invalidPatchIndex)
{
return patchIdx;
}
else
{
return invalidPatchIndex;
}
}
else
{
return invalidPatchIndex;
}
}
uint FindPatchIndexAndUpdateLastAccess(float3 gridTargetPos, StructuredBuffer<uint> cellPatchIndices, uint gridSize, StructuredBuffer<int3> cascadeOffsets, RWStructuredBuffer<PatchUtil::PatchCounterSet> patchCounterSets, uint cascadeCount, float voxelMinSize, float3 worldPosition, float3 worldNormal, uint frameIdx)
{
const uint patchIdx = FindPatchIndex(gridTargetPos, cellPatchIndices, gridSize, cascadeOffsets, cascadeCount, voxelMinSize,worldPosition, worldNormal);
if (patchIdx != invalidPatchIndex)
{
WriteLastFrameAccess(patchCounterSets, patchIdx, frameIdx);
}
return patchIdx;
}
bool ReadHemisphericalIrradiance(IrradianceBufferType patchIrradiances, CellPatchIndexBufferType cellPatchIndices, uint gridSize, StructuredBuffer<int3> cascadeOffsets, float3 cascadeFocusPos, uint cascadeCount, float voxelMinSize, float3 worldPosition, float3 worldNormal, uint startCascadeIdx, out SphericalHarmonics::RGBL1 resultIrradiance)
{
VolumePositionResolution posResolution = ResolveVolumePosition(worldPosition, cascadeFocusPos, gridSize, cascadeOffsets, cascadeCount, voxelMinSize, startCascadeIdx);
bool resultBool = false;
resultIrradiance = (SphericalHarmonics::RGBL1)0; // Theoretically not required but added to silence a shader compilation warning.
if (posResolution.isValid())
{
const uint3 positionStorageSpace = ConvertGridSpaceToStorageSpace(posResolution.positionGridSpace, gridSize, cascadeOffsets[posResolution.cascadeIdx]);
resultBool = ReadHemisphericalIrradiance(patchIrradiances, cellPatchIndices, gridSize, posResolution.cascadeIdx, positionStorageSpace, worldNormal, resultIrradiance);
}
return resultBool;
}
bool ReadHemisphericalIrradiance(IrradianceBufferType patchIrradiances, CellPatchIndexBufferType cellPatchIndices, uint gridSize, StructuredBuffer<int3> cascadeOffsets, float3 cascadeFocusPos, uint cascadeCount, float voxelMinSize, float3 worldPosition, float3 worldNormal, out SphericalHarmonics::RGBL1 resultIrradiance)
{
const uint conservativeStartCascadeIdx = 0;
return ReadHemisphericalIrradiance(
patchIrradiances,
cellPatchIndices,
gridSize,
cascadeOffsets,
cascadeFocusPos,
cascadeCount,
voxelMinSize,
worldPosition,
worldNormal,
conservativeStartCascadeIdx,
resultIrradiance);
}
float3 ReadPlanarIrradiance(IrradianceBufferType patchIrradiances, CellPatchIndexBufferType cellPatchIndices, uint gridSize, uint cascadeIdx, uint3 gridSpacePosition, float3 worldNormal)
{
SphericalHarmonics::RGBL1 resultIrradiance;
bool resultBool = ReadHemisphericalIrradiance(patchIrradiances, cellPatchIndices, gridSize, cascadeIdx, gridSpacePosition, worldNormal, resultIrradiance);
if (resultBool)
return max(0, SphericalHarmonics::Eval(resultIrradiance, worldNormal));
else
return invalidIrradiance;
}
float3 ReadPlanarIrradiance(float3 gridTargetPos, IrradianceBufferType patchIrradiances, CellPatchIndexBufferType cellPatchIndices, uint gridSize, StructuredBuffer<int3> cascadeOffsets, uint cascadeCount, float voxelMinSize, float3 worldPosition, float3 worldNormal)
{
VolumePositionResolution posResolution = ResolveVolumePosition(worldPosition, gridTargetPos, gridSize, cascadeOffsets, cascadeCount, voxelMinSize);
if (posResolution.isValid())
{
const uint3 positionStorageSpace = ConvertGridSpaceToStorageSpace(posResolution.positionGridSpace, gridSize, cascadeOffsets[posResolution.cascadeIdx]);
return ReadPlanarIrradiance(patchIrradiances, cellPatchIndices, gridSize, posResolution.cascadeIdx, positionStorageSpace, worldNormal);
}
else
{
return invalidIrradiance;
}
}
void MarkInvalid(inout SphericalHarmonics::RGBL1 irradiance)
{
irradiance.l0 = -1.0f;
}
bool IsValid(inout SphericalHarmonics::RGBL1 irradiance)
{
return all(irradiance.l0 != -1.0f);
}
}
#endif