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

Naga doesnt compile wgsl statically sized arrays properly to hlsl #2419

Closed
FishArmy100 opened this issue Jul 31, 2023 · 3 comments
Closed

Naga doesnt compile wgsl statically sized arrays properly to hlsl #2419

FishArmy100 opened this issue Jul 31, 2023 · 3 comments
Labels
area: back-end Outputs of shader conversion kind: bug Something isn't working lang: HLSL High-Level Shading Language

Comments

@FishArmy100
Copy link

Compiled Errors:

  • Generated array constructor functions are declared after they are used
  • Generated arrays are possibly not in proper format? (I toyed around in Shader Playgroud with the generated hlsl code)

Code:

wgsl code:

// Vertex shader

struct VertexInput {
    @location(0) index: u32,
    @location(1) color: vec4<f32>,
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(1) color: vec4<f32>,
};

struct InstanceInput {
    @location(2) position: vec3<u32>,
    @location(3) id: u32,
    @location(4) face_index: u32
};

struct CameraUniform {
    view_proj: mat4x4<f32>
}

struct ModelUniform {
    model: mat4x4<f32>
}

struct VoxelRenderData {
    color: vec4<f32>
}

struct VoxelRenderDataUniform {
    data: array<VoxelRenderData, 4>
}

@group(0) @binding(0)
var<uniform> camera: CameraUniform;

@group(1) @binding(0)
var<uniform> model: ModelUniform;

@group(2) @binding(0)
var<uniform> render_data: VoxelRenderDataUniform;

const voxel_south_face_position_array = array<vec3<f32>, 4>(
    vec3<f32>(0.0, 1.0, 1.0),
    vec3<f32>(1.0, 1.0, 1.0),
    vec3<f32>(0.0, 0.0, 1.0),
    vec3<f32>(1.0, 0.0, 1.0),
);

const voxel_north_face_position_array = array<vec3<f32>, 4>(
    vec3<f32>(0.0, 0.0, 0.0),
    vec3<f32>(1.0, 0.0, 0.0),
    vec3<f32>(0.0, 1.0, 0.0),
    vec3<f32>(1.0, 1.0, 0.0),
);

const voxel_up_face_position_array = array<vec3<f32>, 4>(
    vec3<f32>(0.0, 1.0, 0.0),
    vec3<f32>(1.0, 1.0, 0.0),
    vec3<f32>(0.0, 1.0, 1.0),
    vec3<f32>(1.0, 1.0, 1.0),
);

const voxel_down_face_position_array = array<vec3<f32>, 4>(
    vec3<f32>(0.0, 0.0, 1.0),
    vec3<f32>(1.0, 0.0, 1.0),
    vec3<f32>(0.0, 0.0, 0.0),
    vec3<f32>(1.0, 0.0, 0.0),
);

const voxel_east_face_position_array = array<vec3<f32>, 4>(
    vec3<f32>(1.0, 1.0, 1.0),
    vec3<f32>(1.0, 1.0, 0.0),
    vec3<f32>(1.0, 0.0, 1.0),
    vec3<f32>(1.0, 0.0, 0.0),
);

const voxel_west_face_position_array = array<vec3<f32>, 4>(
    vec3<f32>(0.0, 1.0, 0.0),
    vec3<f32>(0.0, 1.0, 1.0),
    vec3<f32>(0.0, 0.0, 0.0),
    vec3<f32>(0.0, 0.0, 1.0),
);

const voxel_face_array = array<array<vec3<f32>, 4>, 6>(
    voxel_up_face_position_array,
    voxel_down_face_position_array,
    voxel_north_face_position_array,
    voxel_south_face_position_array,
    voxel_east_face_position_array,
    voxel_west_face_position_array
);

struct FaceArrayIndirect {
    arr: array<array<vec3<f32>, 4>, 6>
}

@vertex
fn vs_main(vertex: VertexInput, instance: InstanceInput) -> VertexOutput {
    var face_array: FaceArrayIndirect;
    face_array.arr = voxel_face_array;

    var out: VertexOutput;
    out.color = render_data.data[instance.id].color;

    var vert_pos = face_array.arr[instance.face_index][vertex.index];
    vert_pos.x += f32(instance.position.x);
    vert_pos.y += f32(instance.position.y);
    vert_pos.z += f32(instance.position.z);

    out.clip_position = camera.view_proj * model.model * vec4<f32>(vert_pos, 1.0);

    return out;
}

// Fragment shader

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    return in.color;
}

Generated HLSL code:

struct NagaConstants {
        int base_vertex;
        int base_instance;
        uint other;
    };
    ConstantBuffer<NagaConstants> _NagaConstants: register(b3);
    static const float3 voxel_south_face_position_array = Constructarray4_float3_(float3(0.0, 1.0, 1.0), float3(1.0, 1.0, 1.0), float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0));
    static const float3 voxel_north_face_position_array = Constructarray4_float3_(float3(0.0, 0.0, 0.0), float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), float3(1.0, 1.0, 0.0));
    static const float3 voxel_up_face_position_array = Constructarray4_float3_(float3(0.0, 1.0, 0.0), float3(1.0, 1.0, 0.0), float3(0.0, 1.0, 1.0), float3(1.0, 1.0, 1.0));
    static const float3 voxel_down_face_position_array = Constructarray4_float3_(float3(0.0, 
0.0, 1.0), float3(1.0, 0.0, 1.0), float3(0.0, 0.0, 0.0), float3(1.0, 0.0, 0.0));
    static const float3 voxel_east_face_position_array = Constructarray4_float3_(float3(1.0, 
1.0, 1.0), float3(1.0, 1.0, 0.0), float3(1.0, 0.0, 1.0), float3(1.0, 0.0, 0.0));
    static const float3 voxel_west_face_position_array = Constructarray4_float3_(float3(0.0, 
1.0, 0.0), float3(0.0, 1.0, 1.0), float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 1.0));
    static const float3 voxel_face_array = Constructarray6_array4_float3__(Constructarray4_float3_(float3(0.0, 1.0, 0.0), float3(1.0, 1.0, 0.0), float3(0.0, 1.0, 1.0), float3(1.0, 1.0, 1.0)), Constructarray4_float3_(float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0), float3(0.0, 0.0, 
0.0), float3(1.0, 0.0, 0.0)), Constructarray4_float3_(float3(0.0, 0.0, 0.0), float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), float3(1.0, 1.0, 0.0)), Constructarray4_float3_(float3(0.0, 1.0, 1.0), float3(1.0, 1.0, 1.0), float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0)), Constructarray4_float3_(float3(1.0, 1.0, 1.0), float3(1.0, 1.0, 0.0), float3(1.0, 0.0, 1.0), float3(1.0, 0.0, 0.0)), Constructarray4_float3_(float3(0.0, 1.0, 0.0), float3(0.0, 1.0, 1.0), float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 1.0)));

    struct VertexInput {
        nointerpolation uint index : LOC0;
        float4 color : LOC1;
    };

    struct VertexOutput {
        float4 clip_position : SV_Position;
        float4 color : LOC1;
    };

    struct InstanceInput {
        nointerpolation uint3 position : LOC2;
        nointerpolation uint id : LOC3;
        nointerpolation uint face_index : LOC4;
    };

    struct CameraUniform {
        row_major float4x4 view_proj;
    };

    struct ModelUniform {
        row_major float4x4 model;
    };

    struct VoxelRenderData {
        float4 color;
    };

    struct VoxelRenderDataUniform {
        VoxelRenderData data[4];
    };

    struct FaceArrayIndirect {
        float3 arr[6][4];
        int _end_pad_0;
    };

    typedef float3 ret_Constructarray4_float3_[4];
    ret_Constructarray4_float3_ Constructarray4_float3_(float3 arg0, float3 arg1, float3 arg2, float3 arg3) {
        float3 ret[4] = { arg0, arg1, arg2, arg3 };
        return ret;
    }

    typedef float3 ret_Constructarray6_array4_float3__[6][4];
    ret_Constructarray6_array4_float3__ Constructarray6_array4_float3__(float3 arg0[4], float3 arg1[4], float3 arg2[4], float3 arg3[4], float3 arg4[4], float3 arg5[4]) {
        float3 ret[6][4] = { arg0, arg1, arg2, arg3, arg4, arg5 };
        return ret;
    }

    cbuffer camera : register(b0) { CameraUniform camera; }
    cbuffer model : register(b1) { ModelUniform model; }
    cbuffer render_data : register(b2) { VoxelRenderDataUniform render_data; }

    struct VertexOutput_vs_main {
        float4 color : LOC1;
        float4 clip_position : SV_Position;
    };

    struct FragmentInput_fs_main {
        float4 color_1 : LOC1;
        float4 clip_position_1 : SV_Position;
    };

    VertexOutput_vs_main vs_main(VertexInput vertex, InstanceInput instance)
    {
        FaceArrayIndirect face_array = (FaceArrayIndirect)0;
        VertexOutput out_ = (VertexOutput)0;
        float3 vert_pos = (float3)0;

        face_array.arr = Constructarray6_array4_float3__(Constructarray4_float3_(float3(0.0, 
1.0, 0.0), float3(1.0, 1.0, 0.0), float3(0.0, 1.0, 1.0), float3(1.0, 1.0, 1.0)), Constructarray4_float3_(float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0), float3(0.0, 0.0, 0.0), float3(1.0, 
0.0, 0.0)), Constructarray4_float3_(float3(0.0, 0.0, 0.0), float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), float3(1.0, 1.0, 0.0)), Constructarray4_float3_(float3(0.0, 1.0, 1.0), float3(1.0, 1.0, 1.0), float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0)), Constructarray4_float3_(float3(1.0, 1.0, 1.0), float3(1.0, 1.0, 0.0), float3(1.0, 0.0, 1.0), float3(1.0, 0.0, 0.0)), Constructarray4_float3_(float3(0.0, 1.0, 0.0), float3(0.0, 1.0, 1.0), float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 1.0)));
        float4 _expr12 = render_data.data[instance.id].color;
        out_.color = _expr12;
        float3 _expr18 = face_array.arr[instance.face_index][vertex.index];
        vert_pos = _expr18;
        float _expr24 = vert_pos.x;
        vert_pos.x = (_expr24 + float(instance.position.x));
        float _expr30 = vert_pos.y;
        vert_pos.y = (_expr30 + float(instance.position.y));
        float _expr36 = vert_pos.z;
        vert_pos.z = (_expr36 + float(instance.position.z));
        float4x4 _expr41 = camera.view_proj;
        float4x4 _expr44 = model.model;
        float3 _expr46 = vert_pos;
        out_.clip_position = mul(float4(_expr46, 1.0), mul(_expr44, _expr41));
        VertexOutput _expr50 = out_;
        const VertexOutput vertexoutput = _expr50;
        const VertexOutput_vs_main vertexoutput_1 = { vertexoutput.color, vertexoutput.clip_position };
        return vertexoutput_1;
    }

    float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
    {
        VertexOutput in_ = { fragmentinput_fs_main.clip_position_1, fragmentinput_fs_main.color_1 };
        return in_.color;
    }

Notes:

  • I am using wgpu, although all the errors are coming from the shader compilation
  • Im using a Microsoft surface, which only has dx12 drivers
@teoxoy
Copy link
Member

teoxoy commented Aug 1, 2023

Thanks for the report!

This has been recently fixed by #2266 which is part of the latest wgpu and naga release.

@teoxoy teoxoy closed this as completed Aug 1, 2023
@teoxoy teoxoy added kind: bug Something isn't working area: back-end Outputs of shader conversion lang: HLSL High-Level Shading Language labels Aug 1, 2023
@teoxoy teoxoy added this to the WGSL Specification V1 milestone Aug 1, 2023
@FishArmy100
Copy link
Author

In the latest coming release, or 0.17.0?

@cwfitzgerald
Copy link
Member

Both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: back-end Outputs of shader conversion kind: bug Something isn't working lang: HLSL High-Level Shading Language
Projects
None yet
Development

No branches or pull requests

3 participants