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

Unable to define constant vertices array in WGSL for usage in GLSL #1638

Closed
maxammann opened this issue Jan 2, 2022 · 1 comment · Fixed by #1644
Closed

Unable to define constant vertices array in WGSL for usage in GLSL #1638

maxammann opened this issue Jan 2, 2022 · 1 comment · Fixed by #1644
Labels
area: back-end Outputs of shader conversion kind: bug Something isn't working lang: GLSL OpenGL Shading Language

Comments

@maxammann
Copy link
Contributor

I'm drawing several instances of vertices which are statically defined in a vertex shader. The shader below works perfectly with Vulkan and WebGPU (with Vulkan) but not with OpenGL.

struct CameraUniform {
    view_proj: mat4x4<f32>;
    view_position: vec4<f32>;
};

struct GlobalsUniform {
    camera: CameraUniform;
};

[[group(0), binding(0)]] var<uniform> globals: GlobalsUniform;

struct VertexOutput {
    [[location(0)]] v_color: vec4<f32>;
    [[builtin(position)]] position: vec4<f32>;
};

var<private> VERTICES: array<vec2<f32>, 6> = array<vec2<f32>, 6>(
    vec2<f32>(0.0, 0.0),
    vec2<f32>(0.0, EXTENT),
    vec2<f32>(EXTENT, 0.0),
    vec2<f32>(EXTENT, 0.0),
    vec2<f32>(0.0, EXTENT),
    vec2<f32>(EXTENT, EXTENT)
);

[[stage(vertex)]]
fn main(
    //[[location(0)]] a_position: vec2<f32>,
    [[location(4)]] mask_offset: vec2<f32>,
    [[location(5)]] target_width: f32,
    [[location(6)]] target_height: f32,
    [[location(7)]] debug_color: vec4<f32>,
    [[builtin(vertex_index)]] vertex_idx: u32,
    [[builtin(instance_index)]] instance_idx: u32 // instance_index is used when we have multiple instances of the same "object"
) -> VertexOutput {
    let a_position = VERTICES[vertex_idx];

    let scaling: mat3x3<f32> = mat3x3<f32>(
            vec3<f32>(target_width,   0.0,            0.0),
            vec3<f32>(0.0,            target_height,  0.0),
            vec3<f32>(0.0,            0.0,            1.0)
    );

    let z = 0.0;

    let world_pos_3d = vec3<f32>(a_position + mask_offset, z);
    let world_pos = scaling * world_pos_3d;

    let position = globals.camera.view_proj * vec4<f32>(world_pos, 1.0);

    return VertexOutput(debug_color, position);
}

This translates to GLSL to the following which is missing the vertices:

#version 320 es
#extension GL_EXT_texture_shadow_lod : require

precision highp float;
precision highp int;

struct CameraUniform {
    mat4x4 view_proj;
    vec4 view_position;
};
struct GlobalsUniform {
    CameraUniform camera;
};
struct VertexOutput {
    vec4 v_color;
    vec4 position;
};
layout(std140, binding = 0) uniform GlobalsUniform_block_0Vertex { GlobalsUniform _group_0_binding_0; };

vec2 VERTICES[6];

layout(location = 4) in vec2 _p2vs_location4;
layout(location = 5) in float _p2vs_location5;
layout(location = 6) in float _p2vs_location6;
layout(location = 7) in vec4 _p2vs_location7;
layout(location = 0) smooth out vec4 _vs2fs_location0;

void main() {
    vec2 mask_offset = _p2vs_location4;
    float target_width = _p2vs_location5;
    float target_height = _p2vs_location6;
    vec4 debug_color = _p2vs_location7;
    uint vertex_idx = uint(gl_VertexID);
    uint instance_idx = uint(gl_InstanceID);
    vec2 a_position = VERTICES[vertex_idx];
    mat3x3 scaling = mat3x3(vec3(target_width, 0.0, 0.0), vec3(0.0, target_height, 0.0), vec3(0.0, 0.0, 1.0));
    vec3 world_pos_3d = vec3((a_position + mask_offset), 0.0);
    vec3 world_pos = (scaling * world_pos_3d);
    mat4x4 _e28 = _group_0_binding_0.camera.view_proj;
    vec4 position = (_e28 * vec4(world_pos, 1.0));
    VertexOutput _tmp_return = VertexOutput(debug_color, position);
    _vs2fs_location0 = _tmp_return.v_color;
    gl_Position = _tmp_return.position;
    gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);
    return;
}

Instead of var<private>´ I also tried letwhich did not work becausevertex_idxinVERTICES[vertex_idx];` would be requred to be a const. I'm opening this issue because the behavior is different.

maxammann added a commit to maplibre/maplibre-rs that referenced this issue Jan 3, 2022
@kvark kvark added area: back-end Outputs of shader conversion kind: bug Something isn't working lang: GLSL OpenGL Shading Language labels Jan 3, 2022
@maxammann
Copy link
Contributor Author

Thank you :)

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: GLSL OpenGL Shading Language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants