1//! A library containing a 1D Gaussian blur kernel.2//!3//! This is used by depth of field, but you can also use it in custom4//! postprocessing passes.6/// Performs a single direction of the separable Gaussian blur kernel.7///8/// * `color_texture` is the texture to blur.9///10/// * `color_texture_sampler` is a sampler to sample the texture. It must have11/// linear filtering for both minification and magnification.12///13/// * `frag_coord` is the screen-space pixel coordinate of the fragment (i.e. the14/// `position` input to the fragment).15///16/// * `coc` is the diameter (not the radius) of the circle of confusion for this17/// fragment.18///19/// * `frag_offset` is the vector, in screen-space units, from one sample to the20/// next. For a horizontal blur this will be `vec2(1.0, 0.0)`; for a vertical21/// blur this will be `vec2(0.0, 1.0)`.22///23/// Returns the resulting color of the fragment.24fn gaussian_blur(color_texture: texture_2d<f32>, color_texture_sampler: sampler, frag_coord: vec4<f32>, coc: f32, frag_offset: vec2<f32>) -> vec4<f32> {25 // Usually σ (the standard deviation) is half the radius, and the radius is26 // half the CoC. So we multiply by 0.25.27 let sigma = coc * 0.25;29 // 1.5σ is a good, somewhat aggressive default for support—the number of30 // texels on each side of the center that we process.31 let support = i32(ceil(sigma * 1.5));32 let uv = frag_coord.xy / vec2<f32>(textureDimensions(color_texture));33 let offset = frag_offset / vec2<f32>(textureDimensions(color_texture));35 // The probability density function of the Gaussian blur is (up to constant factors) `exp(-1 / 2σ² *36 // x²). We precalculate the constant factor here to avoid having to37 // calculate it in the inner loop.38 let exp_factor = -1.0 / (2.0 * sigma * sigma);40 // Accumulate samples on both sides of the current texel. Go two at a time,41 // taking advantage of bilinear filtering.42 var sum = textureSampleLevel(color_texture, color_texture_sampler, uv, 0.0).rgb;43 var weight_sum = 1.0;44 for (var i = 1; i <= support; i += 2) {45 // This is a well-known trick to reduce the number of needed texture46 // samples by a factor of two. We seek to accumulate two adjacent47 // samples c₀ and c₁ with weights w₀ and w₁ respectively, with a single48 // texture sample at a carefully chosen location. Observe that:49 //50 // k ⋅ lerp(c₀, c₁, t) = w₀⋅c₀ + w₁⋅c₁51 //52 // w₁53 // if k = w₀ + w₁ and t = ───────54 // w₀ + w₁55 //56 // Therefore, if we sample at a distance of t = w₁ / (w₀ + w₁) texels in57 // between the two texel centers and scale by k = w₀ + w₁ afterward, we58 // effectively evaluate w₀⋅c₀ + w₁⋅c₁ with a single texture lookup.59 let w0 = exp(exp_factor * f32(i) * f32(i));60 let w1 = exp(exp_factor * f32(i + 1) * f32(i + 1));61 let uv_offset = offset * (f32(i) + w1 / (w0 + w1));62 let weight = w0 + w1;64 sum += (65 textureSampleLevel(color_texture, color_texture_sampler, uv + uv_offset, 0.0).rgb +66 textureSampleLevel(color_texture, color_texture_sampler, uv - uv_offset, 0.0).rgb67 ) * weight;68 weight_sum += weight * 2.0;69 }71 return vec4(sum / weight_sum, 1.0);72}