-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathDebug.compute
More file actions
179 lines (164 loc) · 7.34 KB
/
Debug.compute
File metadata and controls
179 lines (164 loc) · 7.34 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
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal glcore ps5
#pragma kernel Visualize
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/GBufferCommon.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Sampling/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Sampling/Hashes.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Lighting/SurfaceCache/PatchUtil.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Lighting/SurfaceCache/RingBuffer.hlsl"
StructuredBuffer<uint> _RingConfigBuffer;
StructuredBuffer<SphericalHarmonics::RGBL1> _PatchIrradiances;
StructuredBuffer<uint> _PatchCellIndices;
StructuredBuffer<PatchUtil::PatchGeometry> _PatchGeometries;
StructuredBuffer<PatchUtil::PatchStatisticsSet> _PatchStatistics;
StructuredBuffer<uint> _CellPatchIndices;
StructuredBuffer<int3> _CascadeOffsets;
RWStructuredBuffer<PatchUtil::PatchCounterSet> _PatchCounterSets;
RWTexture2D<float4> _Result;
Texture2D<float> _ScreenDepths;
Texture2D<float3> _ScreenShadedNormals;
Texture2D<float3> _ScreenFlatNormals;
uint _RingConfigOffset;
uint _GridSize;
uint _CascadeCount;
float _VoxelMinSize;
uint _ViewMode;
uint _ShowSamplePosition;
float4x4 _ClipToWorldTransform;
uint _FrameIdx;
float3 _GridTargetPos;
uint _DummyBlackbox; // By using a uniform we force the compiler to not make any assumptions.
float3 IsActuallyNan(float3 val) {
return isnan(asfloat(_DummyBlackbox ^ asuint(val)));
}
float3 IsActuallyInf(float3 val) {
return isinf(asfloat(_DummyBlackbox ^ asuint(val)));
}
[numthreads(8,8,1)]
void Visualize(uint2 threadIdx : SV_DispatchThreadID)
{
uint2 screenSize;
_ScreenDepths.GetDimensions(screenSize.x, screenSize.y);
float3 output;
const float statusBarHeight = 0.05f;
if (float(threadIdx.y) / screenSize.y < statusBarHeight)
{
const RingBuffer::Config ringConfig = RingBuffer::LoadConfig(_RingConfigBuffer, _RingConfigOffset);
const uint patchIdx = float(threadIdx.x) / screenSize.x * patchCapacity;
if (RingBuffer::IsPositionUnused(ringConfig, patchIdx))
{
output = float3(1.0f, 1.0f, 1.0f);
}
else
{
if (_PatchCellIndices[patchIdx] == PatchUtil::invalidCellIndex)
output = float3(1.0f, 0.0f, 0.0f);
else
output = float3(0.0f, 1.0f, 0.0f);
}
}
else
{
const float ndcDepth = LoadNdcDepth(_ScreenDepths, threadIdx);
if (ndcDepth == invalidNdcDepth)
return;
const float2 uv = (float2(threadIdx.xy) + 0.5f) / float2(screenSize);
const float3 worldPos = ComputeWorldSpacePosition(uv, ndcDepth, _ClipToWorldTransform);
const float3 worldShadedNormal = UnpackGBufferNormal(_ScreenShadedNormals[threadIdx.xy]);
const float3 worldFlatNormal = _ScreenFlatNormals[threadIdx.xy];
PatchUtil::VolumePositionResolution posResolution = PatchUtil::ResolveVolumePosition(worldPos, _GridTargetPos, _GridSize, _CascadeOffsets, _CascadeCount, _VoxelMinSize);
output = float3(1.0f, 0.0f, 1.0f);
if (posResolution.isValid())
{
const uint directionIdx = PatchUtil::GetDirectionIndex(worldFlatNormal, PatchUtil::gridCellAngularResolution);
const uint3 positionStorageSpace = PatchUtil::ConvertGridSpaceToStorageSpace(posResolution.positionGridSpace, _GridSize, _CascadeOffsets[posResolution.cascadeIdx]);
const uint cellIdx = PatchUtil::GetCellIndex(posResolution.cascadeIdx, positionStorageSpace, directionIdx, _GridSize, PatchUtil::gridCellAngularResolution);
const uint patchIdx = _CellPatchIndices[cellIdx];
const bool hasPatch = (patchIdx != PatchUtil::invalidPatchIndex);
SphericalHarmonics::RGBL1 patchIrradiance = (SphericalHarmonics::RGBL1)0;
float3 patchPos = 0; // initializing value only to silence shader warning
if (hasPatch)
{
patchIrradiance = _PatchIrradiances[patchIdx];
patchPos = _PatchGeometries[patchIdx].position;
PatchUtil::WriteLastFrameAccess(_PatchCounterSets, patchIdx, _FrameIdx);
}
if (_ShowSamplePosition == 1 && hasPatch && length(worldPos - patchPos) < 0.12f)
{
output = 30.0f;
}
else if (_ViewMode == 0)
{
output = float3(
UintToFloat01(LowBiasHash32(cellIdx, 0)),
UintToFloat01(LowBiasHash32(cellIdx, 1)),
UintToFloat01(LowBiasHash32(cellIdx, 2)));
}
else if (_ViewMode == 1)
{
if (hasPatch)
{
const float3 shEval = SphericalHarmonics::Eval(patchIrradiance, worldShadedNormal);
if (any(IsActuallyNan(shEval)))
output = float3(1, 0, 0);
else if (any(IsActuallyInf(shEval)))
output = float3(0, 1, 0);
else
output = max(0, shEval);
}
}
else if (_ViewMode == 2) // fast irradiance
{
if (hasPatch)
{
output = _PatchStatistics[patchIdx].mean * SphericalHarmonics::y0;
}
}
else if (_ViewMode == 3) // coefficient of variation
{
if (hasPatch)
{
const PatchUtil::PatchStatisticsSet stats = _PatchStatistics[patchIdx];
output = sqrt(stats.variance) / max(0.01f, stats.mean) / 20.0f;
}
}
else if (_ViewMode == 4)
{
if (hasPatch)
{
const PatchUtil::PatchStatisticsSet stats = _PatchStatistics[patchIdx];
float3 longIrradiance = _PatchIrradiances[patchIdx].l0;
float3 shortStdDev = sqrt(stats.variance);
float3 drift = abs(longIrradiance - stats.mean) / max(0.001f, shortStdDev);
output = drift * 0.25f;
}
}
else if (_ViewMode == 5)
{
if (hasPatch)
{
output = sqrt(_PatchStatistics[patchIdx].variance) * 0.1f;
}
}
else if (_ViewMode == 6)
{
if (hasPatch)
{
const float3 green = float3(0, 1, 0);
const float3 red = float3(1, 0, 0);
const PatchUtil::PatchCounterSet counterSet = _PatchCounterSets[patchIdx];
const uint updateCount = PatchUtil::GetUpdateCount(counterSet);
const float s = float(updateCount) / PatchUtil::updateMax;
const float3 redGreenMix = lerp(red, green, s);
const float3 checker = UintToFloat01(LowBiasHash32(cellIdx, 0)); // To make cells visible.
output = lerp(redGreenMix, checker, 0.25f);
}
}
else if (_ViewMode == 7)
{
output = worldFlatNormal * 0.5f + 0.5f;
}
}
}
_Result[threadIdx.xy] = float4(output, 1.0f);
}