nightshade 0.8.0

A cross-platform data-oriented game engine.
Documentation
struct DownsampleParams {
    mip_level: u32,
    _padding0: u32,
    _padding1: u32,
    _padding2: u32,
}

@group(0) @binding(0) var source_texture: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> params: DownsampleParams;

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
}

@vertex
fn vertex_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
    let uv = vec2<f32>(
        f32((vertex_index << 1u) & 2u),
        f32(vertex_index & 2u)
    );
    let clip_position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);

    var out: VertexOutput;
    out.clip_position = clip_position;
    out.uv = vec2<f32>(uv.x, 1.0 - uv.y);
    return out;
}

fn rgb_to_luminance(col: vec3<f32>) -> f32 {
    return dot(col, vec3<f32>(0.2126, 0.7152, 0.0722));
}

fn karis_average(col: vec3<f32>) -> f32 {
    let luma = rgb_to_luminance(col) * 0.25;
    return 1.0 / (1.0 + luma);
}

@fragment
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let texture_size = vec2<f32>(textureDimensions(source_texture));
    let texel_size = 1.0 / texture_size;
    let x = texel_size.x;
    let y = texel_size.y;

    // Take 13 samples around current texel 'e':
    // a - b - c
    // - j - k -
    // d - e - f
    // - l - m -
    // g - h - i
    let a = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(-2.0*x, 2.0*y)).rgb;
    let b = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(0.0, 2.0*y)).rgb;
    let c = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(2.0*x, 2.0*y)).rgb;

    let d = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(-2.0*x, 0.0)).rgb;
    let e = textureSample(source_texture, source_sampler, in.uv).rgb;
    let f = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(2.0*x, 0.0)).rgb;

    let g = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(-2.0*x, -2.0*y)).rgb;
    let h = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(0.0, -2.0*y)).rgb;
    let i = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(2.0*x, -2.0*y)).rgb;

    let j = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(-x, y)).rgb;
    let k = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(x, y)).rgb;
    let l = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(-x, -y)).rgb;
    let m = textureSample(source_texture, source_sampler, in.uv + vec2<f32>(x, -y)).rgb;

    var downsample: vec3<f32>;

    if params.mip_level == 0u {
        // First mip: apply Karis average to prevent fireflies from overly bright subpixels
        var groups: array<vec3<f32>, 5>;
        groups[0] = (a + b + d + e) * (0.125 / 4.0);
        groups[1] = (b + c + e + f) * (0.125 / 4.0);
        groups[2] = (d + e + g + h) * (0.125 / 4.0);
        groups[3] = (e + f + h + i) * (0.125 / 4.0);
        groups[4] = (j + k + l + m) * (0.5 / 4.0);
        groups[0] *= karis_average(groups[0]);
        groups[1] *= karis_average(groups[1]);
        groups[2] *= karis_average(groups[2]);
        groups[3] *= karis_average(groups[3]);
        groups[4] *= karis_average(groups[4]);
        downsample = groups[0] + groups[1] + groups[2] + groups[3] + groups[4];
    } else {
        // Subsequent mips: standard weighted distribution
        downsample = e * 0.125;
        downsample += (a + c + g + i) * 0.03125;
        downsample += (b + d + f + h) * 0.0625;
        downsample += (j + k + l + m) * 0.125;
    }

    // Clamp to prevent black boxes from multiplications yielding 0
    downsample = max(downsample, vec3<f32>(0.0001));

    return vec4<f32>(downsample, 1.0);
}