-
Notifications
You must be signed in to change notification settings - Fork 3
/
convolution_cs.glsl
400 lines (344 loc) · 11.8 KB
/
convolution_cs.glsl
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
#version 440
#extension GL_ARB_shader_group_vote: enable
// Includes
#include <Shaders/OpenGL/Common/common.glsl>
#include <Shaders/OpenGL/Aberration/TiledSplatBlur/common.glsl>
// Kernel size
layout(local_size_x = CONVOLUTION_GROUP_SIZE, local_size_y = CONVOLUTION_GROUP_SIZE, local_size_z = 1) in;
// Whether we want to use unpacked shared data or not
#if CONVOLUTION_GROUP_SIZE >= 32
#define USE_PACKED_SHARED_DATA 1
#define SHARED_DATA_TYPE FragmentDataPacked
#else
#define USE_PACKED_SHARED_DATA 0
#define SHARED_DATA_TYPE FragmentData
#endif
// Size of shared fragment arrays
#define SHARED_ARRAY_SIZE CONVOLUTION_GROUP_SIZE * CONVOLUTION_GROUP_SIZE
// Shared array for holding the per-tile fragments
shared SHARED_DATA_TYPE sTileFragments[SHARED_ARRAY_SIZE];
// Number of total fragments in the tile
uint uiNumFragments = 0;
// Result of the convolution
vec3 vResult = vec3(0.0);
// Total weight during convolution
vec3 vTotalWeight = vec3(0.0);
// Number of samples contributing to the result
int iNumSamples = 0;
// Various necessary coordinates and indices
#define DEFINE_COORDINATES_INDICES() \
const ivec2 fragmentCoord = ivec2(gl_GlobalInvocationID.xy); \
const ivec2 tileId = ivec2(gl_GlobalInvocationID.xy / TILE_SIZE); \
const ivec2 threadId = ivec2(gl_LocalInvocationID.xy); \
const uint threadIndex = gl_LocalInvocationID.y * CONVOLUTION_GROUP_SIZE + gl_LocalInvocationID.x; \
const uint arrayIndex = tileArrayIndex(tileId.xy); \
const uint countArrayIndex = tileCountArrayIndex(tileId.xy); \
const uint numTileFragments = CONVOLUTION_GROUP_SIZE * CONVOLUTION_GROUP_SIZE; \
const uint groupSize = CONVOLUTION_GROUP_SIZE * CONVOLUTION_GROUP_SIZE; \
const uint batchSize = SHARED_ARRAY_SIZE
// Unpacks the fragments in a given batch
uint unpackBatchFragments(const int batchId)
{
// Define the necessary coordinates
DEFINE_COORDINATES_INDICES();
// Batch start index
const uint batchStartIndex = batchId * batchSize;
// Number of fragments to fetch by this thread
const uint numFragmentsLeft = min(uiNumFragments - batchStartIndex, batchSize);
// Index into the sort index buffer
const uint sortElementIndex = arrayIndex + batchStartIndex + threadIndex;
// Index into the fragment buffer
const uint fragmentIndex = sTileSortBuffer[sortElementIndex].uiIndex;
// Extract the unpacked fragment data
#if USE_PACKED_SHARED_DATA
sTileFragments[threadIndex] = sFragmentBuffer[fragmentIndex];
#else
sTileFragments[threadIndex] = unpackFragmentData(sFragmentBuffer[fragmentIndex]);
#endif
// Return the number of extracted fragments
return numFragmentsLeft;
}
#if DEBUG_OUTPUT == 1
// Returns true if the current fragment corresponds to the center fragment
bool isCenterFragmentData(const vec2 fragmentCoord, const FragmentData fragmentData)
{
return distance(fragmentData.vScreenPosition, vec2(fragmentCoord)) <= calcFragmentSizeOffset(fragmentData.uiFragmentSize) + 0.01;
}
// Write out debug info using the current fragment
void debugVisualization(const FragmentData fragmentData)
{
// Define the necessary coordinates
DEFINE_COORDINATES_INDICES();
// Compute the blur radii for the 3 channels
vec3 blurRadius = vec3(0.0);
for (uint i = 0; i < sTiledSplatBlurData.uiRenderChannels; ++i)
{
const vec3 blurRadii = blurRadii(fragmentData.vPsfIndex, i);
blurRadius[i] = blurRadii[2];
}
for (uint i = sTiledSplatBlurData.uiRenderChannels; i < 3; ++i)
{
blurRadius[i] = blurRadius[sTiledSplatBlurData.uiRenderChannels - 1];
}
// Visualize the merged color
if (sTiledSplatBlurData.uiOutputMode == OutputMode_MergedColor)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
vResult = vec3(fragmentData.vColor);
vTotalWeight = vec3(1.0);
}
}
// Visualize the merged depth
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_MergedDepth)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
vResult = vec3(linearCamSpaceDepth(fragmentData.vPsfIndex.z));
vTotalWeight = vec3(1.0);
}
}
// Visualize alpha
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_Alpha)
{
vResult = vec3(vTotalWeight);
vTotalWeight = vec3(1.0);
}
// Visualize the tile size
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_TileBufferSize)
{
float ratio = uiNumFragments / float(sTiledSplatBlurData.uiTileBufferTotalSubentries - 1);
vResult = ratio > 1.0 ? vec3(1, 0, 0) : vec3(ratio);
vTotalWeight = vec3(1.0);
}
// Visualize the fragment size
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_FragmentSize)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
const float fragmentSize = fragmentData.uiFragmentSize;
vResult = vec3(fragmentSize / float(sTiledSplatBlurData.uiMergedFragmentSize));
vTotalWeight = vec3(1.0);
}
}
// Visualize the PSF id of the center fragment
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_PsfId)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
const vec3 psfIndex = vec3
(
floor(horizontalAngleIndex(fragmentData.vPsfIndex.x)),
floor(verticalAngleIndex(fragmentData.vPsfIndex.y)),
floor(objectDistanceIndex(fragmentData.vPsfIndex.z))
);
const vec3 psfIndexNorm = vec3
(
saturate(psfIndex.x / float(sTiledSplatBlurData.uiNumHorizontalAngles - 1)),
saturate(psfIndex.y / float(sTiledSplatBlurData.uiNumVerticalAngles - 1)),
saturate(psfIndex.z / float(sTiledSplatBlurData.uiNumObjectDistances - 1))
);
vResult = psfIndexNorm;
vTotalWeight = vec3(1.0);
}
}
// Visualize the PSF id of the center fragment
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_LerpFactor)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
const vec3 lerpFactor = vec3
(
fract(horizontalAngleIndex(fragmentData.vPsfIndex.x)),
fract(verticalAngleIndex(fragmentData.vPsfIndex.y)),
fract(objectDistanceIndex(fragmentData.vPsfIndex.z))
);
vResult = lerpFactor;
vTotalWeight = vec3(1.0);
}
}
// Visualize the blur radius of the center fragment
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_BlurRadiusCont ||
sTiledSplatBlurData.uiOutputMode == OutputMode_BlurRadius ||
sTiledSplatBlurData.uiOutputMode == OutputMode_BlurRadiusFract)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
if (sTiledSplatBlurData.uiOutputMode == OutputMode_BlurRadiusCont)
{
vResult = blurRadius / sTiledSplatBlurData.uiMaxBlurRadiusGlobal;
vTotalWeight = vec3(1.0);
}
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_BlurRadius)
{
vResult = floor(blurRadius) / sTiledSplatBlurData.uiMaxBlurRadiusGlobal;
vTotalWeight = vec3(1.0);
}
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_BlurRadiusFract)
{
vResult = fract(blurRadius);
vTotalWeight = vec3(1.0);
}
}
}
// Visualize the object depth borders
if (sTiledSplatBlurData.uiOverlayMode == OverlayMode_PsfBorder)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
const vec3 psfIndex = vec3
(
floor(horizontalAngleIndex(fragmentData.vPsfIndex.x)),
floor(verticalAngleIndex(fragmentData.vPsfIndex.y)),
floor(objectDistanceIndex(fragmentData.vPsfIndex.z))
);
if (fract(psfIndex[0]) < 0.08) { vResult.x = 1.0; vTotalWeight.x = 1.0; }
if (fract(psfIndex[1]) < 0.08) { vResult.y = 1.0; vTotalWeight.y = 1.0; }
if (fract(psfIndex[2]) < 0.08) { vResult.z = 1.0; vTotalWeight.z = 1.0; }
}
}
// Visualize the blur radius borders
else if (sTiledSplatBlurData.uiOverlayMode == OverlayMode_BlurRadiusBorder)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
for (int i = 0; i < 3; ++i)
{
if (fract(blurRadius[i]) < 0.08)
{
vResult[i] = 1.0;
vTotalWeight[i] = 1.0;
}
}
}
}
// Visualize the tile borders as an overlay
else if (sTiledSplatBlurData.uiOverlayMode == OverlayMode_TileBorder)
{
if
(
(gl_GlobalInvocationID.x % TILE_SIZE) == 0 ||
(gl_GlobalInvocationID.y % TILE_SIZE) == 0 ||
(gl_GlobalInvocationID.x % TILE_SIZE) == TILE_SIZE - 1 ||
(gl_GlobalInvocationID.y % TILE_SIZE) == TILE_SIZE - 1
)
{
vResult = vec3(1.0, 0.0, 0.0);
vTotalWeight = vec3(1.0);
}
}
// Visualize the object depth increments as an overlay
else if (sTiledSplatBlurData.uiOverlayMode == OverlayMode_ObjectDepthBorder)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
const float depthFract = fract(fragmentData.vPsfIndex.z);
const float smallDepthFract = fract(fragmentData.vPsfIndex.z * 10.0);
if (depthFract < 0.02 || depthFract > 0.998)
{
vResult = vec3(1.0, 0.0, 0.0);
vTotalWeight = vec3(1.0);
}
else if (fragmentData.vPsfIndex.z < 1.0 && (smallDepthFract < 0.02 || smallDepthFract > 0.998))
{
vResult = vec3(0.0, 1.0, 0.0);
vTotalWeight = vec3(1.0);
}
}
}
// Visualize the blur radius of the center fragment
else if (sTiledSplatBlurData.uiOutputMode == OutputMode_IncidentAngle)
{
if (isCenterFragmentData(fragmentCoord, fragmentData))
{
vResult.xy = abs(fragmentData.vPsfIndex.xy) / vec2(sTiledSplatBlurData.fHorizontalAnglesMax, sTiledSplatBlurData.fVerticalAnglesMax);
vResult.z = 0.0;
vTotalWeight = vec3(1.0);
}
}
}
#endif
// Perform the convolution process with the parameter fragment
void convolveFragment(const FragmentData fragmentData)
{
// Define the necessary coordinates
DEFINE_COORDINATES_INDICES();
// Sample the psf
const vec3 weight = samplePsf(vec2(fragmentCoord), fragmentData);
// Increment the number of samples
++iNumSamples;
// Front-to-back blending
if (sTiledSplatBlurData.uiAccumulationMethod == AccumulationMethod_FrontToBack)
{
vResult += (1.0 - vTotalWeight) * weight * fragmentData.vColor;
vTotalWeight = weight + (1.0 - weight) * vTotalWeight;
}
// Back-to-front blending
else if (sTiledSplatBlurData.uiAccumulationMethod == AccumulationMethod_BackToFront)
{
vResult = weight * fragmentData.vColor + (1.0 - weight) * vResult;
vTotalWeight = weight + (1.0 - weight) * vTotalWeight;
}
// Regular summation
else if (sTiledSplatBlurData.uiAccumulationMethod == AccumulationMethod_Sum)
{
vResult += weight * fragmentData.vColor;
vTotalWeight += weight;
}
}
// Perform the convolution with the current batch
void convolve(const uint numBatchFragments)
{
// Define the necessary coordinates
DEFINE_COORDINATES_INDICES();
// Process each fragment in the tile array
for (int elementIndex = 0; elementIndex < numBatchFragments; ++elementIndex)
{
// Extract the fragment's data
#if USE_PACKED_SHARED_DATA
const FragmentData fragmentData = unpackFragmentData(sTileFragments[elementIndex]);
#else
const FragmentData fragmentData = sTileFragments[elementIndex];
#endif
// Accumulate, if needed
#if DEBUG_OUTPUT == 1
if (sTiledSplatBlurData.uiOutputMode == OutputMode_Convolution ||
sTiledSplatBlurData.uiOutputMode == OutputMode_Alpha)
#endif
convolveFragment(fragmentData);
#if DEBUG_OUTPUT == 1
debugVisualization(fragmentData);
#endif
}
}
void main()
{
// Define the necessary coordinates
DEFINE_COORDINATES_INDICES();
// Extract the number of fragments to process
uiNumFragments = sTileParametersBuffer[countArrayIndex].uiNumFragmentsTotal;
// Number of batches to make
const uint numBatches = ROUNDED_DIV(uiNumFragments, batchSize);
// Init the PSF data
initPsfData();
// Fetch the current fragments
for (int batchId = 0; batchId < numBatches; ++batchId)
{
// Unpack the current batch to groupshared memory
const uint numBatchFragments = unpackBatchFragments(batchId);
memoryBarrierShared();
barrier();
// Perform convolution using the local data
convolve(numBatchFragments);
memoryBarrierShared();
barrier();
}
// Ignore non-existent pixels
if (any(greaterThanEqual(vec2(fragmentCoord), sTiledSplatBlurData.vResolution)))
return;
// Normalize back the result
if (sTiledSplatBlurData.bNormalizeResult == 1)
vResult /= vTotalWeight;
// Write out the convolution result
imageStore(sResult, ivec2(fragmentCoord), vec4(vResult, 1.0));
}