-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
render-pass-taa.js
219 lines (174 loc) · 6.83 KB
/
render-pass-taa.js
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
import {
FILTER_LINEAR,
ADDRESS_CLAMP_TO_EDGE
} from '../../platform/graphics/constants.js';
import { Texture } from '../../platform/graphics/texture.js';
import { shaderChunks } from '../../scene/shader-lib/chunks/chunks.js';
import { RenderPassShaderQuad } from '../../scene/graphics/render-pass-shader-quad.js';
import { RenderTarget } from '../../platform/graphics/render-target.js';
const fs = /* glsl */ `
uniform highp sampler2D uSceneDepthMap;
uniform sampler2D sourceTexture;
uniform sampler2D historyTexture;
uniform mat4 matrix_viewProjectionPrevious;
uniform mat4 matrix_viewProjectionInverse;
uniform vec4 jitters; // xy: current frame, zw: previous frame
uniform vec2 textureSize;
varying vec2 uv0;
vec2 reproject(vec2 uv, float depth) {
// fragment NDC
#ifndef WEBGPU
depth = depth * 2.0 - 1.0;
#endif
vec4 ndc = vec4(uv * 2.0 - 1.0, depth, 1.0);
// remove jitter from the current frame
ndc.xy -= jitters.xy;
// Transform NDC to world space of the current frame
vec4 worldPosition = matrix_viewProjectionInverse * ndc;
worldPosition /= worldPosition.w;
// world position to screen space of the previous frame
vec4 screenPrevious = matrix_viewProjectionPrevious * worldPosition;
return (screenPrevious.xy / screenPrevious.w) * 0.5 + 0.5;
}
vec4 colorClamp(vec2 uv, vec4 historyColor) {
// out of range numbers
vec3 minColor = vec3(9999.0);
vec3 maxColor = vec3(-9999.0);
// sample a 3x3 neighborhood to create a box in color space
for(float x = -1.0; x <= 1.0; ++x)
{
for(float y = -1.0; y <= 1.0; ++y)
{
vec3 color = texture2D(sourceTexture, uv + vec2(x, y) / textureSize).rgb;
minColor = min(minColor, color);
maxColor = max(maxColor, color);
}
}
// clamp the history color to min/max bounding box
vec3 clamped = clamp(historyColor.rgb, minColor, maxColor);
return vec4(clamped, historyColor.a);
}
void main()
{
vec2 uv = uv0;
#ifdef WEBGPU
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// This hack is needed on webgpu, which makes TAA work but the resulting image is upside-down.
// We could flip the image in the following pass, but ideally a better solution should be found.
uv.y = 1.0 - uv.y;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#endif
// current frame
vec4 srcColor = texture2D(sourceTexture, uv);
// current depth
float depth = texture2DLodEXT(uSceneDepthMap, uv, 0.0).r;
// previous frame
vec2 historyUv = reproject(uv0, depth);
#ifdef QUALITY_HIGH
// high quality history, sharper result
vec4 historyColor = SampleTextureCatmullRom(TEXTURE_PASS(historyTexture), historyUv, textureSize);
#else
// single sample history, more blurry result
vec4 historyColor = texture2D(historyTexture, historyUv);
#endif
// handle disocclusion by clamping the history color
vec4 historyColorClamped = colorClamp(uv, historyColor);
// handle history buffer outside of the frame
float mixFactor = (historyUv.x < 0.0 || historyUv.x > 1.0 || historyUv.y < 0.0 || historyUv.y > 1.0) ?
1.0 : 0.05;
gl_FragColor = mix(historyColorClamped, srcColor, mixFactor);
}
`;
/**
* @category Graphics
*/
class RenderPassTAA extends RenderPassShaderQuad {
/**
* The index of the history texture to render to.
*
* @type {number}
*/
historyIndex = 0;
/**
* @type {Texture}
*/
historyTexture = null;
/**
* @type {Texture[]}
*/
historyTextures = [];
/**
* @type {RenderTarget[]}
*/
historyRenderTargets = [];
constructor(device, sourceTexture, cameraComponent) {
super(device);
this.sourceTexture = sourceTexture;
this.cameraComponent = cameraComponent;
const defines = /* glsl */`
#define QUALITY_HIGH
`;
const fsChunks = shaderChunks.sampleCatmullRomPS;
this.shader = this.createQuadShader('TaaResolveShader', defines + fsChunks + fs);
const { scope } = device;
this.sourceTextureId = scope.resolve('sourceTexture');
this.textureSizeId = scope.resolve('textureSize');
this.textureSize = new Float32Array(2);
this.historyTextureId = scope.resolve('historyTexture');
this.viewProjPrevId = scope.resolve('matrix_viewProjectionPrevious');
this.viewProjInvId = scope.resolve('matrix_viewProjectionInverse');
this.jittersId = scope.resolve('jitters');
this.setup();
}
destroy() {
if (this.renderTarget) {
this.renderTarget.destroyTextureBuffers();
this.renderTarget.destroy();
this.renderTarget = null;
}
}
setup() {
// double buffered history render target
for (let i = 0; i < 2; ++i) {
this.historyTextures[i] = new Texture(this.device, {
name: `TAA-History-${i}`,
width: 4,
height: 4,
format: this.sourceTexture.format,
mipmaps: false,
minFilter: FILTER_LINEAR,
magFilter: FILTER_LINEAR,
addressU: ADDRESS_CLAMP_TO_EDGE,
addressV: ADDRESS_CLAMP_TO_EDGE
});
this.historyRenderTargets[i] = new RenderTarget({
colorBuffer: this.historyTextures[i],
depth: false
});
}
this.historyTexture = this.historyTextures[0];
this.init(this.historyRenderTargets[0], {
resizeSource: this.sourceTexture
});
}
before() {
this.sourceTextureId.setValue(this.sourceTexture);
this.historyTextureId.setValue(this.historyTextures[1 - this.historyIndex]);
this.textureSize[0] = this.sourceTexture.width;
this.textureSize[1] = this.sourceTexture.height;
this.textureSizeId.setValue(this.textureSize);
const camera = this.cameraComponent.camera;
this.viewProjPrevId.setValue(camera._viewProjPrevious.data);
this.viewProjInvId.setValue(camera._viewProjInverse.data);
this.jittersId.setValue(camera._jitters);
}
// called when the parent render pass gets added to the frame graph
update() {
// swap source and destination history texture
this.historyIndex = 1 - this.historyIndex;
this.historyTexture = this.historyTextures[this.historyIndex];
this.renderTarget = this.historyRenderTargets[this.historyIndex];
return this.historyTexture;
}
}
export { RenderPassTAA };