declare const _default: "\n #include \"sampleCatmullRomPS\"\n #include \"reversibleTonemapPS\"\n #include \"screenDepthPS\"\n\n var sourceTexture: texture_2d;\n var sourceTextureSampler: sampler;\n var historyTexture: texture_2d;\n var historyTextureSampler: sampler;\n uniform matrix_viewProjectionPrevious: mat4x4f;\n uniform matrix_viewProjectionInverse: mat4x4f;\n uniform taaExposure: f32; // the exposure the scene is displayed with\n uniform jitters: vec4f; // xy: current frame, zw: previous frame\n uniform textureSize: vec2f;\n uniform historySize: vec2f;\n\n varying uv0: vec2f;\n\n // the most of the current frame taken while the camera moves the surface of a pixel across the\n // screen, and the motion from which it is taken, in pixels of the history\n const TAA_MOTION_BLEND: f32 = 0.5;\n const TAA_MOTION_PIXELS: f32 = 32.0;\n\n fn reproject(uv_in: vec2f, depth: f32) -> vec2f {\n\n // uv was Y-flipped by getImageEffectUV for texture sampling,\n // un-flip to reconstruct correct NDC (viewProj matrices use standard Y convention)\n var uv = vec2f(uv_in.x, 1.0 - uv_in.y);\n\n var ndc = vec4f(uv * 2.0 - 1.0, depth, 1.0);\n\n // remove jitter from the current frame\n ndc = vec4f(ndc.xy - uniform.jitters.xy, ndc.zw);\n\n // Transform NDC to world space of the current frame\n var worldPosition = uniform.matrix_viewProjectionInverse * ndc;\n worldPosition = worldPosition / worldPosition.w;\n\n // world position to screen space of the previous frame\n let screenPrevious = uniform.matrix_viewProjectionPrevious * worldPosition;\n\n // remove the jitter of the previous frame, as the history holds the image without it, and\n // flip the result back to texture sampling convention\n var result = (screenPrevious.xy / screenPrevious.w + uniform.jitters.zw) * 0.5 + 0.5;\n result.y = 1.0 - result.y;\n\n return result;\n }\n\n fn colorClampPremul(uv: vec2f, historyPremul: vec3f) -> vec3f {\n\n var minPremul = vec3f(9999.0);\n var maxPremul = vec3f(-9999.0);\n\n // 3x3 neighborhood in premultiplied space \u2014 same domain as the temporal mix (see fragmentMain).\n for (var ix: i32 = -1; ix <= 1; ix = ix + 1) {\n for (var iy: i32 = -1; iy <= 1; iy = iy + 1) {\n let s = textureSample(sourceTexture, sourceTextureSampler, uv + vec2f(f32(ix), f32(iy)) / uniform.textureSize);\n let premul = s.rgb * s.a;\n minPremul = min(minPremul, premul);\n maxPremul = max(maxPremul, premul);\n }\n }\n\n return clamp(historyPremul, minPremul, maxPremul);\n }\n\n #ifdef TAA_UPSCALE\n\n struct UpscaleSample {\n color: vec4f,\n weight: f32,\n minPremul: vec3f,\n maxPremul: vec3f\n };\n\n // The current frame renders at a lower resolution than the history. Each of its texels holds\n // the image at its center moved by the jitter of the frame, so from frame to frame its sample\n // lands at a different position within the pixels of the history. The current color at this\n // pixel is reconstructed from the texels around it, weighted by how close their samples are\n // to it, and the weight of the nearest decides how much of it the history takes - over the\n // jitter sequence, each pixel of the history accumulates the samples landing on it.\n fn upscaleCurrent(uv: vec2f) -> UpscaleSample {\n\n // this pixel and the jitter, in texels of the current frame - uv is Y-flipped for\n // texture sampling, and so is the jitter\n let position = uv * uniform.textureSize;\n let jitter = vec2f(uniform.jitters.x, -uniform.jitters.y) * 0.5 * uniform.textureSize;\n\n // the texel whose sample is nearest to this pixel\n let nearest = floor(position - jitter);\n\n // pixels of the history per texel of the current frame\n let scale = uniform.historySize.x / uniform.textureSize.x;\n\n var result: UpscaleSample;\n var sum = vec4f(0.0);\n var sumWeight: f32 = 0.0;\n var nearestColor = vec4f(0.0);\n result.weight = 0.0;\n result.minPremul = vec3f(9999.0);\n result.maxPremul = vec3f(-9999.0);\n for (var ix: i32 = -1; ix <= 1; ix = ix + 1) {\n for (var iy: i32 = -1; iy <= 1; iy = iy + 1) {\n let texel = nearest + vec2f(f32(ix), f32(iy));\n let s = textureSampleLevel(sourceTexture, sourceTextureSampler, (texel + 0.5) / uniform.textureSize, 0.0);\n let premul = s.rgb * s.a;\n result.minPremul = min(result.minPremul, premul);\n result.maxPremul = max(result.maxPremul, premul);\n if (ix == 0 && iy == 0) {\n nearestColor = s;\n }\n\n // the distance of its sample to this pixel, in pixels of the history\n let d = (texel + 0.5 + jitter - position) * scale;\n let w = exp(-2.29 * dot(d, d));\n sum = sum + s * w;\n sumWeight = sumWeight + w;\n result.weight = max(result.weight, w);\n }\n }\n result.color = select(nearestColor, sum / max(sumWeight, 1e-6), sumWeight > 1e-4);\n return result;\n }\n\n #endif\n\n @fragment\n fn fragmentMain(input: FragmentInput) -> FragmentOutput {\n var output: FragmentOutput;\n\n // current frame\n #ifdef TAA_UPSCALE\n let upscaled = upscaleCurrent(uv0);\n let srcColor = upscaled.color;\n #else\n let srcColor = textureSample(sourceTexture, sourceTextureSampler, uv0);\n #endif\n\n // current depth is in linear space, convert it to non-linear space\n let linearDepth = getLinearScreenDepth(uv0);\n let depth = delinearizeDepth(linearDepth);\n\n // previous frame\n let historyUv = reproject(uv0, depth);\n\n #ifdef QUALITY_HIGH\n\n var historySample: vec4f = SampleTextureCatmullRom(historyTexture, historyTextureSampler, historyUv, uniform.historySize);\n\n #else\n\n var historySample: vec4f = textureSample(historyTexture, historyTextureSampler, historyUv);\n\n #endif\n\n // Premultiplied (rgb * a) is the coverage-correct space for TAA: straight RGB interpolates\n // uncorrelated color and opacity at edges, which causes colored fringes and alpha-related\n // ghosting when history is blended or clamped against a 3x3 neighborhood. We clamp and mix\n // in premultiplied space, then un-premultiply once using the current frame's alpha only\n // (alpha is not temporally filtered \u2014 output stays tied to this frame's coverage).\n let historyPremul = historySample.rgb * historySample.a;\n let srcPremul = srcColor.rgb * srcColor.a;\n\n #ifdef TAA_UPSCALE\n // clamped to the range of the texels around this pixel, and taking more of the current\n // frame the closer its nearest sample is to this pixel\n let historyPremulClamped = clamp(historyPremul, upscaled.minPremul, upscaled.maxPremul);\n let baseBlend = 0.1 * upscaled.weight;\n #else\n let historyPremulClamped = colorClampPremul(uv0, historyPremul);\n let baseBlend: f32 = 0.05;\n #endif\n\n // The further the history of this pixel was reprojected from, the faster the camera moves its\n // surface across the screen: the history has been resampled further, which blurs it, and it is\n // more likely not to hold this surface at all. The current frame is then taken more of, up to\n // TAA_MOTION_BLEND from a motion of TAA_MOTION_PIXELS pixels of the history - while the camera\n // is still, the history accumulates as before.\n let motion = length((uv0 - historyUv) * uniform.historySize);\n let blend = mix(baseBlend, max(baseBlend, TAA_MOTION_BLEND), clamp(motion / TAA_MOTION_PIXELS, 0.0, 1.0));\n\n let mixFactor_condition = historyUv.x < 0.0 || historyUv.x > 1.0 || historyUv.y < 0.0 || historyUv.y > 1.0;\n let mixFactor = select(blend, 1.0, mixFactor_condition);\n\n // The history and the current frame are blended compressed by a reversible tone mapping of\n // their exposed values. Blended in linear HDR, the samples of a bright surface partially\n // covering a pixel dominate its average, which the tone mapping then displays nearly as\n // bright as the surface - the edges of bright geometry would stay thick and aliased.\n let e = max(uniform.taaExposure, 1e-6);\n let mixedPremul = reversibleTonemapInverse(mix(reversibleTonemap(historyPremulClamped * e), reversibleTonemap(srcPremul * e), mixFactor)) / e;\n let a = srcColor.a;\n // UNORM8 alpha step \u2014 avoid un-premul in the lowest band (quantization / noise).\n let UNPREMUL_EPS = 1.0 / 255.0;\n let rgbStraight = select(srcColor.rgb, mixedPremul / a, a > UNPREMUL_EPS);\n output.color = vec4f(rgbStraight, a);\n return output;\n }\n"; export default _default;