-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReconstructionShader.frag
493 lines (428 loc) · 14.9 KB
/
ReconstructionShader.frag
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// uniform buffer
// core in 3.1
#extension GL_ARB_uniform_buffer_object : require
// sampler2DMS
// core in 3.2
#extension GL_ARB_texture_multisample : require
// layout(location = ...)
// core in 3.3
#extension GL_ARB_explicit_attrib_location : require
#ifdef VALIDATION
#extension GL_GOOGLE_include_directive : require
#include "ReconstructionOptions.h"
#define COLOR_OUTPUT_ATTRIBUTE_LOCATION 0
#endif
// References:
// Checkerboard Rendering for Real-Time Upscaling on Intel Integrated Graphics
// https://software.intel.com/en-us/articles/checkerboard-rendering-for-real-time-upscaling-on-intel-integrated-graphics
// - checkerboard pattern, viewport jitter
// - velocity pass, depth reprojection
// - initial reconstruction shader with depth-based occlusion test
// Rendering Rainbow Six Siege, Jalal El Mansouri
// https://twvideo01.ubm-us.net/o1/vault/gdc2016/Presentations/El_Mansouri_Jalal_Rendering_Rainbow_Six.pdf
// - color clamping and confidence blend
// - dilated velocity, preserves object silhouette (TODO)
// - use velocity of pixel in 3x3 neighboorhood that's closest to the camera
// - we need full-res depth here which requires the velocity pass to render the entire scene
// 4K Checkerboard in Battlefield 1 and Mass Effect, Graham Wihlidal
// http://frostbite-wp-prd.s3.amazonaws.com/wp-content/uploads/2017/03/04173623/GDC-Checkerboard.compressed.pdf
// - differential blend operator, removes artifacts around object edges
// - sharpen filter (TODO)
// Dynamic Temporal Antialiasing and Upsampling in Call of Duty, Jorge Jimenez
// https://www.activision.com/cdn/research/Dynamic_Temporal_Antialiasing_and_Upsampling_in_Call_of_Duty_v4.pdf
// - composite object velocity with camera velocity (TODO)
// - downsample to half-res closest velocity in same pass
// - reduces texture reads required, 2 gathers for velocity
// quarter-res 2X multisampled textures
// two layers: even / odd (jittered)
uniform sampler2DMSArray color;
uniform sampler2DMSArray depth;
// full-res screen-space velocity buffer
// .z is a mask for moving objects
uniform sampler2D velocity;
layout(std140) uniform OptionsBlock
{
mat4 prevViewProjection;
mat4 invViewProjection;
ivec2 viewport;
float near;
float far;
int currentFrame; // is the current frame even or odd? (-> index into color and depth array layers)
bool cameraParametersChanged;
int flags;
float depthTolerance;
};
#define OPTION_SET(OPT) ((flags & (OPTION_ ## OPT)) != 0)
#ifdef DEBUG
#define DEBUG_OPTION_SET(OPT) OPTION_SET(DEBUG_ ## OPT)
#else
#define DEBUG_OPTION_SET(OPT) (false)
#endif
layout(location = COLOR_OUTPUT_ATTRIBUTE_LOCATION) out vec4 fragColor;
/*
each quarter-res pixel corresponds to 4 pixels (quadrants) in the full-res output
each quarter-res pixel has two MSAA samples at fixed positions
quadrants:
+---+---+
| 2 | 3 |
+---+---+
| 0 | 1 |
+---+---+
sample positions:
+---+---+
| | 0 |
+---+---+
| 1 | |
+---+---+
the odd frames' viewport is jittered half a pixel (= one full-res pixel) to the right
so the sample positions overlap for full coverage across two frames
even:
+---+---+
| | 0 |
+---+---+
| 1 | |
+---+---+
odd:
+---+---+
| | A |
+---+---+
| B | |
+---+---+
combined:
+---+---+
| A | 0 |
+---+---+---+
| B | 1 | |
+---+---+---+
*/
int calculateQuadrant(ivec2 pixelCoords)
{
return (pixelCoords.x & 1) + (pixelCoords.y & 1) * 2;
}
vec4 fetchQuadrant(sampler2DMSArray tex, ivec2 coords, int quadrant)
{
switch(quadrant)
{
default:
case 0: // (x, y, even/odd), sample
return texelFetch(tex, ivec3(coords, 0), 1);
case 1:
return texelFetch(tex, ivec3(coords + ivec2(1, 0), 1), 1);
case 2:
return texelFetch(tex, ivec3(coords, 1), 0);
case 3:
return texelFetch(tex, ivec3(coords, 0), 0);
}
}
/*
quadrants to evaluate when averaging values around a quadrant:
0:
+---+---+
| 0 | |
---+---+---+
1 | X | 1 |
---+---+---+
| 0 |
1:
+---+---+
| | 0 |
+---+---+---
| 1 | X | 1
+---+---+---
| 0 |
2:
| 1 |
---+---+---+
0 | X | 0 |
---+---+---+
| 1 | |
+---+---+
3:
| 1 |
+---+---+---
| 0 | X | 0
+---+---+---
| | 1 |
+---+---+
*/
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
const ivec2 directionOffsets[4*4] = ivec2[4*4](
// quadrant 0
ivec2( 0, 0), // up
ivec2( 0, -1), // down
ivec2(-1, 0), // left
ivec2( 0, 0), // right
// quadrant 1
ivec2( 0, 0),
ivec2( 0, -1),
ivec2( 0, 0),
ivec2(+1, 0),
// quadrant 2
ivec2( 0, +1),
ivec2( 0, 0),
ivec2(-1, 0),
ivec2( 0, 0),
// quadrant 3
ivec2( 0, +1),
ivec2( 0, 0),
ivec2( 0, 0),
ivec2(+1, 0)
);
const ivec4 directionQuadrants[4] = ivec4[4](
// quadrant 0
ivec4(ivec2(2), ivec2(1)), // up/down, left/right
// quadrant 1
ivec4(ivec2(3), ivec2(0)),
// quadrant 2
ivec4(ivec2(0), ivec2(3)),
// quadrant 3
ivec4(ivec2(1), ivec2(2))
);
// tonemapping operator for combining HDR colors to prevent bright samples from dominating the result
// https://gpuopen.com/learn/optimized-reversible-tonemapper-for-resolve/
vec4 tonemap(vec4 color)
{
return color / (max(color.r, max(color.g, color.b)) + 1.0);
}
vec4 undoTonemap(vec4 color)
{
return color / (1.0 - max(color.r, max(color.g, color.b)));
}
struct ColorNeighborhood
{
// values are tonemapped!
// if you want to output any of these (or a linear combination of them) use undoTonemap
vec4 up;
vec4 down;
vec4 left;
vec4 right;
};
// differential blend operator
// look at vertical and horizontal color blend
// higher weight on whichever has the lowest color difference
// this greatly reduces checkerboard artifacts at color discontinuities and edges
float colorBlendWeight(vec4 a, vec4 b)
{
return 1.0 / max(length(a.rgb - b.rgb), 0.001);
}
vec4 differentialBlend(ColorNeighborhood neighbors)
{
float verticalWeight = colorBlendWeight(neighbors.up, neighbors.down);
float horizontalWeight = colorBlendWeight(neighbors.left, neighbors.right);
vec4 result = (neighbors.up + neighbors.down) * verticalWeight +
(neighbors.left + neighbors.right) * horizontalWeight;
return result * 0.5 * 1.0/(verticalWeight + horizontalWeight);
}
void fetchColorNeighborhood(ivec2 coords, int quadrant, out ColorNeighborhood neighbors)
{
int k = quadrant * 4;
neighbors.up = tonemap(fetchQuadrant(color, coords + directionOffsets[k + UP ], directionQuadrants[quadrant][UP ]));
neighbors.down = tonemap(fetchQuadrant(color, coords + directionOffsets[k + DOWN ], directionQuadrants[quadrant][DOWN ]));
neighbors.left = tonemap(fetchQuadrant(color, coords + directionOffsets[k + LEFT ], directionQuadrants[quadrant][LEFT ]));
neighbors.right = tonemap(fetchQuadrant(color, coords + directionOffsets[k + RIGHT], directionQuadrants[quadrant][RIGHT]));
}
vec4 colorAverage(ColorNeighborhood neighbors)
{
vec4 result;
if(OPTION_SET(DIFFERENTIAL_BLENDING))
result = differentialBlend(neighbors);
else
result = (neighbors.up + neighbors.down + neighbors.left + neighbors.right) * 0.25;
return undoTonemap(result);
}
vec4 colorClamp(ColorNeighborhood neighbors, vec4 color)
{
vec4 minColor = min(min(neighbors.up, neighbors.down), min(neighbors.left, neighbors.right));
vec4 maxColor = max(max(neighbors.up, neighbors.down), max(neighbors.left, neighbors.right));
return undoTonemap(clamp(tonemap(color), minColor, maxColor));
}
// undo depth projection
// assumes a projection transformation produced by Matrix4::perspectiveProjection with finite far plane
// solve for view space z: (z(n+f) + 2nf)/(-z(n-f)) = 2w-1
// w = window space depth [0;1]
// 2w-1 = NDC space depth [-1;1]
float screenToViewDepth(float depth)
{
return (far * near) / ((far * depth) - far - (near * depth));
}
// returns averaged depth in view space
// depth buffer values are non-linear, averaging those produces incorrect results
float fetchDepthAverage(ivec2 coords, int quadrant)
{
int k = quadrant * 4;
float result =
screenToViewDepth(fetchQuadrant(depth, coords + directionOffsets[k + UP ], directionQuadrants[quadrant][UP ]).x) +
screenToViewDepth(fetchQuadrant(depth, coords + directionOffsets[k + DOWN ], directionQuadrants[quadrant][DOWN ]).x) +
screenToViewDepth(fetchQuadrant(depth, coords + directionOffsets[k + LEFT ], directionQuadrants[quadrant][LEFT ]).x) +
screenToViewDepth(fetchQuadrant(depth, coords + directionOffsets[k + RIGHT], directionQuadrants[quadrant][RIGHT]).x);
return result * 0.25;
}
// get screen space velocity vector from fullscreen coordinates
// the z component is a mask for dynamic objects, if it's 0 no velocity was calculated at that coordinate
// and camera reprojection is necessary
vec3 fetchVelocity(ivec2 coords)
{
return texelFetch(velocity, coords, 0).xyz * vec3(viewport, 1.0);
}
// get old frame's pixel position based on camera movement
// unprojects world position from screen space depth, then projects into previous frame's screen space
ivec2 reprojectPixel(ivec2 coords, float depth)
{
vec2 screen = vec2(coords) + 0.5; // gl_FragCoord x/y are located at half-pixel centers, undo the flooring
vec3 ndc = vec3(screen / viewport, depth) * 2.0 - 1.0; // z: [0;1] -> [-1;1]
vec4 clip = vec4(ndc, 1.0);
vec4 world = invViewProjection * clip;
world /= world.w;
clip = prevViewProjection * world;
ndc = clip.xyz / clip.w;
screen = (ndc.xy * 0.5 + 0.5) * viewport;
coords = ivec2(floor(screen));
return coords;
}
void main()
{
ivec2 coords = ivec2(floor(gl_FragCoord.xy));
ivec2 halfCoords = coords >> 1;
int quadrant = calculateQuadrant(coords);
const ivec2 FRAME_QUADRANTS[2] = ivec2[](
ivec2(3, 0), // even
ivec2(2, 1) // odd
);
// debug output: velocity buffer
if(DEBUG_OPTION_SET(SHOW_VELOCITY))
{
vec2 vel = texelFetch(velocity, coords, 0).xy;
fragColor = vec4(abs(vel * 255.0), 0.0, 1.0);
return;
}
// debug output: checkered frame
if(DEBUG_OPTION_SET(SHOW_SAMPLES))
{
int sampleFrame = OPTION_SET(DEBUG_SHOW_EVEN_SAMPLES) ? 0 : 1;
ivec2 boardQuadrants = FRAME_QUADRANTS[sampleFrame];
if(any(equal(vec2(quadrant), boardQuadrants)))
fragColor = fetchQuadrant(color, halfCoords, quadrant);
else
fragColor = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
ivec2 currentQuadrants = FRAME_QUADRANTS[currentFrame];
// was this pixel rendered with the most recent frame?
// -> just use it
if(any(equal(ivec2(quadrant), currentQuadrants)))
{
fragColor = fetchQuadrant(color, halfCoords, quadrant);
return;
}
ColorNeighborhood neighbors;
fetchColorNeighborhood(halfCoords, quadrant, neighbors);
// we have no old data, use average
if(cameraParametersChanged)
{
fragColor = colorAverage(neighbors);
return;
}
bool possiblyOccluded = false;
// find pixel position in previous frame
ivec2 oldCoords = coords;
bool velocityFromDepth = true;
// for fully general results, sample from a velocity buffer
if(OPTION_SET(USE_VELOCITY_BUFFER))
{
vec3 velocity = fetchVelocity(coords);
// z is a mask for dynamic objects
if(velocity.z > 0.0)
{
oldCoords = ivec2(floor(gl_FragCoord.xy - velocity.xy));
velocityFromDepth = false;
}
else
{
// force occlusion check to prevent ghosting around previously
// occluded pixels
// if we only check for quarter-pixel movement, we'd see (0,0) movement in
// that case and directly use the old frame's, but we need to average
possiblyOccluded = true;
}
}
// if we're not using a velocity buffer or the object is static, reproject using the camera transformation
if(velocityFromDepth)
{
float z = fetchQuadrant(depth, halfCoords, quadrant).x;
oldCoords = reprojectPixel(coords, z);
}
ivec2 oldHalfCoords = oldCoords >> 1;
int oldQuadrant = calculateQuadrant(oldCoords);
// TODO
// this eliminates jitter, but breaks with smearing everywhere
// occlusion?
//fragColor = fetchQuadrant(color, oldHalfCoords, oldQuadrant);
//return;
// is the previous position outside the screen?
if(any(lessThan(oldCoords, ivec2(0, 0))) || any(greaterThanEqual(oldCoords, viewport)))
{
if(DEBUG_OPTION_SET(SHOW_COLORS))
fragColor = vec4(1.0, 1.0, 0.0, 1.0);
else
fragColor = colorAverage(neighbors);
return;
}
// is the previous position not in an old frame quadrant?
// this happens when any movement cancelled the jitter
// -> there's no shading information
ivec2 oldQuadrants = FRAME_QUADRANTS[1 - currentFrame];
if(!any(equal(ivec2(oldQuadrant), oldQuadrants)))
{
if(DEBUG_OPTION_SET(SHOW_COLORS))
fragColor = vec4(0.0, 1.0, 1.0, 1.0);
else
fragColor = colorAverage(neighbors);
return;
}
// check for occlusion if the old position was in a different quarter-res pixel
if(any(greaterThan(abs(oldHalfCoords - halfCoords), ivec2(0))))
{
possiblyOccluded = true;
}
// check for occlusion
bool occluded = false;
if(possiblyOccluded)
{
// simple variant: always assume occlusion
if(OPTION_SET(ASSUME_OCCLUSION))
{
occluded = true;
}
else // more correct heuristic: check depth against current depth average
{
float currentDepthAverage = fetchDepthAverage(halfCoords, quadrant);
float oldDepth = fetchQuadrant(depth, oldHalfCoords, oldQuadrant).x;
// fetchDepthAverage returns average of linear view space depth
oldDepth = screenToViewDepth(oldDepth);
occluded = abs(currentDepthAverage - oldDepth) >= depthTolerance;
}
}
if(occluded)
{
if(DEBUG_OPTION_SET(SHOW_COLORS))
fragColor = vec4(1.0, 0.0, 1.0, 1.0);
else
fragColor = colorAverage(neighbors);
return;
}
// clamp color based on neighbors
vec4 reprojectedColor = fetchQuadrant(color, oldHalfCoords, oldQuadrant);
vec4 clampedColor = colorClamp(neighbors, reprojectedColor);
// blend back towards reprojected result using confidence based on old depth
float currentDepthAverage = fetchDepthAverage(halfCoords, quadrant);
float oldDepth = screenToViewDepth(fetchQuadrant(depth, oldHalfCoords, oldQuadrant).x);
float diff = abs(currentDepthAverage - oldDepth);
// reuse depthTolerance to indicate 0 confidence cutoff
// square falloff
float deviation = diff - depthTolerance;
float confidence = clamp(deviation * deviation, 0.0, 1.0);
fragColor = mix(clampedColor, reprojectedColor, confidence);
}