Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WGSL validation issue when creating mat3x3<f32> as constant #2110

Closed
redwarp opened this issue Oct 30, 2022 · 2 comments
Closed

WGSL validation issue when creating mat3x3<f32> as constant #2110

redwarp opened this issue Oct 30, 2022 · 2 comments
Labels
area: front-end Input formats for conversion help wanted Extra attention is needed kind: bug Something isn't working lang: WGSL WebGPU shading language

Comments

@redwarp
Copy link

redwarp commented Oct 30, 2022

I've been writing some WGSL shader that creates a mat3x3<f32> using the mat3x3(e1,...,e9): mat3x3<T>

It works well if I define my matrix inside a function:

fn rgb_to_xyz(rgb: vec4<f32>) -> vec4<f32> {
    var r = rgb.r;
    var g = rgb.g;
    var b = rgb.b;

    if (r > 0.04045) {
        r = pow(((r + 0.055) / 1.055), 2.4);
    } else {
        r =  r / 12.92;
    }
    if (g > 0.04045) {
        g = pow(((g + 0.055) / 1.055), 2.4);
    } else {
        g =  g / 12.92;
    }
    if (b > 0.04045) {
        b = pow(((b + 0.055) / 1.055), 2.4);
    } else {
        b =  b / 12.92;
    }
    r = r * 100.0;
    g = g * 100.0;
    b = b * 100.0;
        
    let m = mat3x3<f32>(
        0.4124, 0.2126, 0.0193,
        0.3576, 0.7152, 0.1192,
        0.1805, 0.0722, 0.9505
    );

    var xyz = m * vec3<f32>(r, g, b);
    return vec4<f32>(xyz, 1.0);
}

When moving m outside of the function to make it a constant, naga complains with:

error: 
  ┌─ rgb_to_lab.wgsl:4:19
  │
4 │       let m = mat3x3<f32>(
  │ ╭───────────────────^
5 │ │         0.4124, 0.2126, 0.0193,
6 │ │         0.3576, 0.7152, 0.1192,
7 │ │         0.1805, 0.0722, 0.9505
8 │ │     );
  │ ╰─────^ naga::Constant [10]

Constant [10] 'm' is invalid:
        Composing expects 3 components but 9 were given

Full shader code that is not validated by naga:

@group(0) @binding(0) var input_texture : texture_2d<f32>;
@group(0) @binding(1) var output_texture : texture_storage_2d<rgba32float, write>;

let m = mat3x3<f32>(
    0.4124, 0.2126, 0.0193,
    0.3576, 0.7152, 0.1192,
    0.1805, 0.0722, 0.9505
);

fn rgb_to_xyz(rgb: vec4<f32>) -> vec4<f32> {
    var r = rgb.r;
    var g = rgb.g;
    var b = rgb.b;

    if (r > 0.04045) {
        r = pow(((r + 0.055) / 1.055), 2.4);
    } else {
        r =  r / 12.92;
    }
    if (g > 0.04045) {
        g = pow(((g + 0.055) / 1.055), 2.4);
    } else {
        g =  g / 12.92;
    }
    if (b > 0.04045) {
        b = pow(((b + 0.055) / 1.055), 2.4);
    } else {
        b =  b / 12.92;
    }
    r = r * 100.0;
    g = g * 100.0;
    b = b * 100.0;
    
    var xyz = m * vec3<f32>(r, g, b);
    return vec4<f32>(xyz, 1.0);
}

fn xyz_to_lab(xyz: vec4<f32>) -> vec4<f32> {
    var x = xyz.x / 95.0489;
    var y = xyz.y / 100.0;
    var z = xyz.z / 108.8840;

    if (x > 0.008856) {
        x = pow(x, 1.0/3.0);
    } else {
        x = (7.787 * x) + (16.0 / 116.0);
    }
    if (y > 0.008856) {
        y = pow(y, 1.0/3.0);
    } else {
        y = (7.787 * y) + (16.0 / 116.0);
    }
    if (z > 0.008856) {
        z = pow(z, 1.0/3.0);
    } else {
        z = (7.787 * z) + (16.0 / 116.0);
    }
    let l = (116.0 * y) - 16.0;
    let a = 500.0 * (x - y);
    let b = 200.0 * (y - z);

    return vec4<f32>(l, a, b, 1.0);
}

@compute
@workgroup_size(16, 16)
fn main(
    @builtin(global_invocation_id) global_id : vec3<u32>,
) {
    let dimensions = textureDimensions(output_texture);
    let coords = vec2<i32>(global_id.xy);

    if(coords.x >= dimensions.x || coords.y >= dimensions.y) {
        return;
    }

    let lab = xyz_to_lab(rgb_to_xyz(textureLoad(input_texture, coords, 0)));
    textureStore(output_texture, coords, lab);
}

Full shader that is validated by naga, no problem:

@group(0) @binding(0) var input_texture : texture_2d<f32>;
@group(0) @binding(1) var output_texture : texture_storage_2d<rgba32float, write>;


fn rgb_to_xyz(rgb: vec4<f32>) -> vec4<f32> {
    var r = rgb.r;
    var g = rgb.g;
    var b = rgb.b;

    if (r > 0.04045) {
        r = pow(((r + 0.055) / 1.055), 2.4);
    } else {
        r =  r / 12.92;
    }
    if (g > 0.04045) {
        g = pow(((g + 0.055) / 1.055), 2.4);
    } else {
        g =  g / 12.92;
    }
    if (b > 0.04045) {
        b = pow(((b + 0.055) / 1.055), 2.4);
    } else {
        b =  b / 12.92;
    }
    r = r * 100.0;
    g = g * 100.0;
    b = b * 100.0;
    
    let m = mat3x3<f32>(
        0.4124, 0.2126, 0.0193,
        0.3576, 0.7152, 0.1192,
        0.1805, 0.0722, 0.9505
    );

    var xyz = m * vec3<f32>(r, g, b);
    return vec4<f32>(xyz, 1.0);
}

fn xyz_to_lab(xyz: vec4<f32>) -> vec4<f32> {
    var x = xyz.x / 95.0489;
    var y = xyz.y / 100.0;
    var z = xyz.z / 108.8840;

    if (x > 0.008856) {
        x = pow(x, 1.0/3.0);
    } else {
        x = (7.787 * x) + (16.0 / 116.0);
    }
    if (y > 0.008856) {
        y = pow(y, 1.0/3.0);
    } else {
        y = (7.787 * y) + (16.0 / 116.0);
    }
    if (z > 0.008856) {
        z = pow(z, 1.0/3.0);
    } else {
        z = (7.787 * z) + (16.0 / 116.0);
    }
    let l = (116.0 * y) - 16.0;
    let a = 500.0 * (x - y);
    let b = 200.0 * (y - z);

    return vec4<f32>(l, a, b, 1.0);
}

@compute
@workgroup_size(16, 16)
fn main(
    @builtin(global_invocation_id) global_id : vec3<u32>,
) {
    let dimensions = textureDimensions(output_texture);
    let coords = vec2<i32>(global_id.xy);

    if(coords.x >= dimensions.x || coords.y >= dimensions.y) {
        return;
    }

    let lab = xyz_to_lab(rgb_to_xyz(textureLoad(input_texture, coords, 0)));
    textureStore(output_texture, coords, lab);
}
@redwarp
Copy link
Author

redwarp commented Oct 30, 2022

Note: I'm testing using the latest master, hash e7fc8e6

@cwfitzgerald cwfitzgerald added area: front-end Input formats for conversion kind: bug Something isn't working lang: WGSL WebGPU shading language help wanted Extra attention is needed labels Oct 30, 2022
@teoxoy
Copy link
Member

teoxoy commented Oct 30, 2022

Thanks for the report!
This seems to be a duplicate of #1956

@teoxoy teoxoy closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: front-end Input formats for conversion help wanted Extra attention is needed kind: bug Something isn't working lang: WGSL WebGPU shading language
Projects
None yet
Development

No branches or pull requests

3 participants