-
Notifications
You must be signed in to change notification settings - Fork 774
/
RaytracingIndirectDiffuse.compute
464 lines (370 loc) · 18.9 KB
/
RaytracingIndirectDiffuse.compute
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#pragma kernel RaytracingIndirectDiffuseHalfRes
#pragma kernel RaytracingIndirectDiffuseFullRes
#pragma kernel IndirectDiffuseIntegrationUpscaleHalfRes
#pragma kernel IndirectDiffuseIntegrationUpscaleFullRes
#pragma kernel AdjustIndirectDiffuseWeight
#pragma only_renderers d3d11
// #pragma enable_d3d11_debug_symbols
// Include and define the shader pass
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl"
#define SHADERPASS SHADERPASS_RAYTRACING
// HDRP generic includes
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceLighting.hlsl"
// Raytracing includes
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/ShaderVariablesRaytracing.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingSampling.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingCommon.hlsl"
// #define WITHOUT_LDS
// Tile size of this compute
#define RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE 8
TEXTURE2D_X(_DepthTexture);
RW_TEXTURE2D_X(float4, _RaytracingDirectionBuffer);
// Flag value that defines if a given pixel recieves reflections or not
TEXTURE2D_X_UINT2(_StencilTexture);
int _SsrStencilBit;
[numthreads(RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, 1)]
void RaytracingIndirectDiffuseHalfRes(uint3 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
// Compute the pixel position to process
uint2 halfResCoord = groupId * RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE + groupThreadId;
// Pixel where we will store the result of the raytracing
uint2 outputCoord = halfResCoord * 2;
// Pixel coordinate in full res of the pixel that we will be using for our computation
uint2 sourceCoord = ComputeSourceCoordinates(halfResCoord, _RaytracingFrameIndex);
// Initialize the buffer with invalid values
_RaytracingDirectionBuffer[COORD_TEXTURE2D_X(outputCoord)] = float4(0.0, 0.0, 0.0, -1.0f);
// Read the depth value
float depthValue = LOAD_TEXTURE2D_X(_DepthTexture, sourceCoord).r;
// This point is part of the background, we don't really care
if (depthValue == UNITY_RAW_FAR_CLIP_VALUE)
return;
// Convert this to a world space position
PositionInputs posInput = GetPositionInput(sourceCoord, _ScreenSize.zw, depthValue, UNITY_MATRIX_I_VP, GetWorldToViewMatrix(), 0);
// Compute the view in world space
const float3 viewWS = GetWorldSpaceNormalizeViewDir(posInput.positionWS);
// Decode the world space normal
NormalData normalData;
DecodeFromNormalBuffer(sourceCoord, normalData);
// Generate the new sample (follwing values of the sequence)
float2 newSample;
newSample.x = GetBNDSequenceSample(halfResCoord, _RaytracingFrameIndex, 0);
newSample.y = GetBNDSequenceSample(halfResCoord, _RaytracingFrameIndex, 1);
// Importance sample with a cosine lobe
float3 sampleDir = SampleHemisphereCosine(newSample.x, newSample.y, normalData.normalWS);
// PDF is the cosine
float samplePDF = dot(sampleDir, normalData.normalWS);
// In the second texture, we store the sampled direction and the invPDF of the sample
_RaytracingDirectionBuffer[COORD_TEXTURE2D_X(outputCoord)] = float4(sampleDir, 1.0 / samplePDF);
}
[numthreads(RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, 1)]
void RaytracingIndirectDiffuseFullRes(uint3 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
// Compute the pixel position to process
uint2 currentCoord = groupId * RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE + groupThreadId;
// Clear the output color texture
_RaytracingDirectionBuffer[COORD_TEXTURE2D_X(currentCoord)] = float4(0.0, 0.0, 0.0, -1.0f);
// Read the depth value
float depthValue = LOAD_TEXTURE2D_X(_DepthTexture, currentCoord).x;
// This point is part of the background, we don't really care
if (depthValue == UNITY_RAW_FAR_CLIP_VALUE)
return;
// Convert this to a world space position
PositionInputs posInput = GetPositionInput(currentCoord, _ScreenSize.zw, depthValue, UNITY_MATRIX_I_VP, GetWorldToViewMatrix(), 0);
// Compute the view in world space
const float3 viewWS = GetWorldSpaceNormalizeViewDir(posInput.positionWS);
// Decode the world space normal
NormalData normalData;
DecodeFromNormalBuffer(currentCoord, normalData);
// Generate the new sample (following values of the sequence)
float2 theSample;
theSample.x = GetBNDSequenceSample(currentCoord, _RaytracingFrameIndex, 0);
theSample.y = GetBNDSequenceSample(currentCoord, _RaytracingFrameIndex, 1);
// Importance sample with a cosine lobe
float3 sampleDir = SampleHemisphereCosine(theSample.x, theSample.y, normalData.normalWS);
// PDF is the cosine
float samplePDF = dot(sampleDir, normalData.normalWS);
// Write the output ray data
_RaytracingDirectionBuffer[COORD_TEXTURE2D_X(currentCoord)] = float4(sampleDir, 1.0 / samplePDF);
}
// Input textures for the spatial filtering
Texture2DArray<float> _BlueNoiseTexture;
// Output Textures for the spatial filtering
RW_TEXTURE2D_X(float4, _UpscaledIndirectDiffuseTextureRW);
#define NORMAL_REJECTION_THRESHOLD 0.5
struct NeighborTapData
{
float3 lighting;
float linearDepth;
float3 normalWS;
};
float EvaluateNeighborWeight(in NeighborTapData neighborData, float3 normalWS, float linearDepth)
{
// Initially the weight of this pixel is evaluate using the depth
float weight = 1.0f;
// If the candidate pixel is a background pixel, we cannot use it
if(neighborData.linearDepth == 1.0)
weight = 0.0f;
// If the normals of both pixels are too different, we cannot use it
if(dot(neighborData.normalWS, normalWS) < NORMAL_REJECTION_THRESHOLD)
weight *= 0.1f;
// Weight the sample by it's depth similarity
weight *= lerp(1.0, 0.0, saturate(abs(neighborData.linearDepth - linearDepth) / (linearDepth * 0.2)));
// Return the final weight
return weight;
}
#define HALF_RES_PER_THREAD_TAP_COUNT 2
#define HALF_RES_OUT_REGION_SIZE 3
#define HALF_RES_REGION_SIZE (HALF_RES_OUT_REGION_SIZE + 4 + HALF_RES_OUT_REGION_SIZE)
#define HALF_RES_REGION_SIZE_2 (HALF_RES_REGION_SIZE * HALF_RES_REGION_SIZE)
// LDS used to pre-fetch the neighborhood data (in Half Res) (10x10 region)
groupshared uint gs_cacheLighting_HR[HALF_RES_REGION_SIZE_2];
groupshared float gs_cacheDepth_HR[HALF_RES_REGION_SIZE_2];
groupshared uint gs_cacheNormal_HR[HALF_RES_REGION_SIZE_2];
void FillUpscaleNeighborhoodDataLDS_Half(uint groupIndex, uint2 groupOrigin)
{
// The initial position of the access
int2 originXY = groupOrigin / 2 - int2(HALF_RES_OUT_REGION_SIZE, HALF_RES_OUT_REGION_SIZE);
for (int i = 0; i < HALF_RES_PER_THREAD_TAP_COUNT; ++i)
{
uint sampleID = i + (groupIndex * HALF_RES_PER_THREAD_TAP_COUNT);
int offsetX = sampleID % HALF_RES_REGION_SIZE;
int offsetY = sampleID / HALF_RES_REGION_SIZE;
// Compute the full res sampling coordinate
int2 halfResCoord = int2(originXY.x + offsetX, originXY.y + offsetY);
int2 sampleCoord = int2(clamp(halfResCoord.x * 2, 0, _ScreenSize.x - 1), clamp(halfResCoord.y * 2, 0, _ScreenSize.y - 1));
// Read all the values for tap
float3 lighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, sampleCoord).xyz;
float depthValue = LOAD_TEXTURE2D_X(_DepthTexture, sampleCoord).x;
float2 octNormalWS = Unpack888ToFloat2(LOAD_TEXTURE2D_X(_NormalBufferTexture, sampleCoord).xyz);
// Pack and store into the LDS
int LDSIndex = offsetX + offsetY * HALF_RES_REGION_SIZE;
gs_cacheLighting_HR[LDSIndex] = PackToR11G11B10f(lighting);
gs_cacheDepth_HR[LDSIndex] = Linear01Depth(depthValue, _ZBufferParams);
gs_cacheNormal_HR[LDSIndex] = f32tof16(octNormalWS.x) | f32tof16(octNormalWS.y) << 16;
}
}
NeighborTapData GetNeighborTapDataSample_HR(uint index)
{
NeighborTapData outVal;
outVal.lighting = UnpackFromR11G11B10f(gs_cacheLighting_HR[index]);
outVal.linearDepth = gs_cacheDepth_HR[index];
// Grab and unpack
uint packedNormal = gs_cacheNormal_HR[index];
float2 unpackedNormal = float2(f16tof32(packedNormal), f16tof32(packedNormal >> 16));
outVal.normalWS = UnpackNormalOctQuadEncode(unpackedNormal * 2.0 - 1.0);
return outVal;
}
uint OffsetToLDSAdress_HR(uint2 groupThreadId, int2 offset)
{
// Compute the tap coordinate in the 10x10 grid
uint2 tapAddress = (uint2)((int2)(groupThreadId / 2 + HALF_RES_OUT_REGION_SIZE) + offset);
return clamp((uint)(tapAddress.x) + tapAddress.y * HALF_RES_REGION_SIZE, 0, HALF_RES_REGION_SIZE_2 - 1);
}
NeighborTapData GetNeighborTapDataSample_HR(uint2 groupThreadId, int2 offset)
{
return GetNeighborTapDataSample_HR(OffsetToLDSAdress_HR(groupThreadId, offset));
}
NeighborTapData GetNeighborTapDataSample_HR_NOLDS(uint2 fulLResCoord, int2 offset)
{
int2 tapCoord = (fulLResCoord / 2 + offset) * 2;
NeighborTapData outVal;
outVal.lighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, tapCoord).xyz;
outVal.linearDepth = Linear01Depth(LOAD_TEXTURE2D_X(_DepthTexture, tapCoord).x, _ZBufferParams);
float4 normalBuffer = LOAD_TEXTURE2D_X(_NormalBufferTexture, tapCoord);
NormalData normalData;
DecodeFromNormalBuffer(normalBuffer, normalData);
outVal.normalWS = normalData.normalWS;
return outVal;
}
[numthreads(RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, 1)]
void IndirectDiffuseIntegrationUpscaleHalfRes(uint3 dispatchThreadId : SV_DispatchThreadID,
int groupIndex : SV_GroupIndex,
uint2 groupThreadId : SV_GroupThreadID,
uint2 groupId : SV_GroupID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
#ifndef WITHOUT_LDS
// Only the 50 first workers will so something
// Load 2 value per thread
if (groupIndex < 50)
FillUpscaleNeighborhoodDataLDS_Half(groupIndex, groupId * 8);
// Make sure all values are loaded in LDS by now.
GroupMemoryBarrierWithGroupSync();
#endif
// Compute the half res coordinate that we shall be using for our effect
uint2 targetCoord = dispatchThreadId.xy;
uint2 sourceCoord = targetCoord / 2;
// Fetch the depth
float depth = LOAD_TEXTURE2D_X(_DepthTexture, targetCoord).x;
float linearDepth = Linear01Depth(depth, _ZBufferParams);
// Fetch the current normal data
NormalData normalData;
DecodeFromNormalBuffer(targetCoord, normalData);
// If this is a background pixel, we are done
if (depth == UNITY_RAW_FAR_CLIP_VALUE)
return;
// Initialize the output pixels
float3 lightingSum = float3(0.0 ,0.0, 0.0);
float weightSum = 0;
for(int y = -HALF_RES_OUT_REGION_SIZE; y < HALF_RES_OUT_REGION_SIZE; ++y)
{
for(int x = -HALF_RES_OUT_REGION_SIZE; x < HALF_RES_OUT_REGION_SIZE; ++x)
{
#ifndef WITHOUT_LDS
// Grab the neighbor data
NeighborTapData neighborData = GetNeighborTapDataSample_HR(groupThreadId, int2(x,y));
#else
NeighborTapData neighborData = GetNeighborTapDataSample_HR_NOLDS(targetCoord, int2(x,y));
#endif
// Evaluate the weight of this neighbor
float weight = EvaluateNeighborWeight(neighborData, normalData.normalWS, linearDepth);
// Contribute to all the output values
lightingSum += neighborData.lighting * weight;
weightSum += weight;
}
}
// Compute the full res coordinate
if(weightSum == 0.0f)
{
_UpscaledIndirectDiffuseTextureRW[COORD_TEXTURE2D_X(targetCoord)] = float4(0.0f, 0.0f, 0.0f, 0.0f);
}
else
{
_UpscaledIndirectDiffuseTextureRW[COORD_TEXTURE2D_X(targetCoord)] = float4(lightingSum / weightSum, 1.0);
}
}
#define FULL_RES_PER_THREAD_TAP_COUNT 4
#define FULL_RES_OUT_REGION_SIZE 4
#define FULL_RES_REGION_SIZE (FULL_RES_OUT_REGION_SIZE + 8 + FULL_RES_OUT_REGION_SIZE)
#define FULL_RES_REGION_SIZE_2 (FULL_RES_REGION_SIZE * FULL_RES_REGION_SIZE)
// LDS used to pre-fetch the neighborhood data (in Half Res) (10x10 region)
groupshared uint gs_cacheLighting_FR[FULL_RES_REGION_SIZE_2];
groupshared float gs_cacheDepth_FR[FULL_RES_REGION_SIZE_2];
groupshared uint gs_cacheNormal_FR[FULL_RES_REGION_SIZE_2];
void FillUpscaleNeighborhoodDataLDS_Full(uint groupIndex, uint2 groupOrigin)
{
// The initial position of the access
int2 originXY = groupOrigin - int2(FULL_RES_OUT_REGION_SIZE, FULL_RES_OUT_REGION_SIZE);
for (int i = 0; i < FULL_RES_PER_THREAD_TAP_COUNT; ++i)
{
uint sampleID = i + groupIndex * FULL_RES_PER_THREAD_TAP_COUNT;
int offsetX = sampleID % FULL_RES_REGION_SIZE;
int offsetY = sampleID / FULL_RES_REGION_SIZE;
int2 targetCoord = int2(originXY.x + offsetX, originXY.y + offsetY);
int2 sampleCoord = int2(clamp(targetCoord.x, 0, _ScreenSize.x - 1), clamp(targetCoord.y, 0, _ScreenSize.y - 1));
// Read all the values for tap
float3 lighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, sampleCoord).xyz;
float depthValue = LOAD_TEXTURE2D_X(_DepthTexture, sampleCoord).x;
float2 octNormalWS = Unpack888ToFloat2(LOAD_TEXTURE2D_X(_NormalBufferTexture, sampleCoord).xyz);
int LDSIndex = offsetX + offsetY * FULL_RES_REGION_SIZE;
gs_cacheLighting_FR[LDSIndex] = PackToR11G11B10f(lighting);
gs_cacheDepth_FR[LDSIndex] = Linear01Depth(depthValue, _ZBufferParams);
gs_cacheNormal_FR[LDSIndex] = f32tof16(octNormalWS.x) | f32tof16(octNormalWS.y) << 16;
}
}
NeighborTapData GetNeighborTapDataSample_FR(uint index)
{
NeighborTapData outVal;
outVal.lighting = UnpackFromR11G11B10f(gs_cacheLighting_FR[index]);
outVal.linearDepth = gs_cacheDepth_FR[index];
// Grab the packed normal
uint packedNormal = gs_cacheNormal_FR[index];
// Unpack it to float2
float2 unpackedNormal = float2(f16tof32(packedNormal), f16tof32(packedNormal >> 16));
// Unpack it to world space normal
outVal.normalWS = UnpackNormalOctQuadEncode(unpackedNormal * 2.0 - 1.0);
return outVal;
}
uint OffsetToLDSAdress_FR(uint2 groupThreadId, int2 offset)
{
// Compute the tap coordinate in the 16x16 grid
uint2 tapAddress = (uint2)((int2)(groupThreadId + FULL_RES_OUT_REGION_SIZE) + offset);
return clamp((uint)(tapAddress.x) + tapAddress.y * FULL_RES_REGION_SIZE, 0, FULL_RES_REGION_SIZE_2 - 1);
}
NeighborTapData GetNeighborTapDataSample_FR(uint2 groupThreadId, int2 offset)
{
return GetNeighborTapDataSample_FR(OffsetToLDSAdress_FR(groupThreadId, offset));
}
NeighborTapData GetNeighborTapDataSample_FR_NOLDS(uint2 fulLResCoord, int2 offset)
{
int2 tapCoord = fulLResCoord + offset;
NeighborTapData outVal;
outVal.lighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, tapCoord).xyz;
outVal.linearDepth = Linear01Depth(LOAD_TEXTURE2D_X(_DepthTexture, tapCoord).x, _ZBufferParams);
float4 normalBuffer = LOAD_TEXTURE2D_X(_NormalBufferTexture, tapCoord);
NormalData normalData;
DecodeFromNormalBuffer(normalBuffer, normalData);
outVal.normalWS = normalData.normalWS;
return outVal;
}
[numthreads(RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, 1)]
void IndirectDiffuseIntegrationUpscaleFullRes(uint3 dispatchThreadId : SV_DispatchThreadID,
int groupIndex : SV_GroupIndex,
uint2 groupThreadId : SV_GroupThreadID,
uint2 groupId : SV_GroupID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
#ifndef WITHOUT_LDS
// Load 4 value per thread
FillUpscaleNeighborhoodDataLDS_Full(groupIndex, groupId * 8);
// Make sure all values are loaded in LDS by now.
GroupMemoryBarrierWithGroupSync();
#endif
uint2 targetCoord = dispatchThreadId.xy;
uint2 sourceCoord = targetCoord;
// Fetch the depth
float depth = LOAD_TEXTURE2D_X(_DepthTexture, targetCoord).x;
float linearDepth = Linear01Depth(depth, _ZBufferParams);
NormalData normalData;
DecodeFromNormalBuffer(targetCoord, normalData);
if (depth == UNITY_RAW_FAR_CLIP_VALUE)
return;
// Initialize the output pixels
float3 lightingSum = 0.0;
float weightSum = 0;
for(int y = -FULL_RES_OUT_REGION_SIZE; y < FULL_RES_OUT_REGION_SIZE; ++y)
{
for(int x = -FULL_RES_OUT_REGION_SIZE; x < FULL_RES_OUT_REGION_SIZE; ++x)
{
#ifndef WITHOUT_LDS
// Grab the neighbor data
NeighborTapData neighborData = GetNeighborTapDataSample_FR(groupThreadId, int2(x,y));
#else
NeighborTapData neighborData = GetNeighborTapDataSample_FR_NOLDS(targetCoord, int2(x,y));
#endif
// Evaluate the weight of this neighbor
float weight = EvaluateNeighborWeight(neighborData, normalData.normalWS, linearDepth);
// Contribute to all the output values
lightingSum += neighborData.lighting * weight;
weightSum += weight;
}
}
_UpscaledIndirectDiffuseTextureRW[COORD_TEXTURE2D_X(targetCoord)] = float4(lightingSum / weightSum, 1.0);
}
RW_TEXTURE2D_X(float4, _IndirectDiffuseTextureRW);
[numthreads(RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, RAYTRACING_INDIRECT_DIFFUSE_TILE_SIZE, 1)]
void AdjustIndirectDiffuseWeight(uint3 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
uint2 sourceCoord = dispatchThreadId.xy;
// Fetch the depth
float depth = LOAD_TEXTURE2D_X(_DepthTexture, sourceCoord).x;
// If this is a background pixel, there is nothing to do
if (depth == UNITY_RAW_FAR_CLIP_VALUE)
return;
float4 previousGIValue = _IndirectDiffuseTextureRW[COORD_TEXTURE2D_X(sourceCoord)];
// Nullify the weight if the surface is not supposed to recieve SSGI
uint stencilValue = GetStencilValue(LOAD_TEXTURE2D_X(_StencilTexture, sourceCoord));
if ((stencilValue & _SsrStencilBit) == 0)
previousGIValue.w = 0.0f;
else
previousGIValue.w = 1.0f;
_IndirectDiffuseTextureRW[COORD_TEXTURE2D_X(sourceCoord)] = previousGIValue;
}