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

Drawing vertices without vertex buffer not possible with OpenGL #2342

Closed
maxammann opened this issue Jan 1, 2022 · 3 comments
Closed

Drawing vertices without vertex buffer not possible with OpenGL #2342

maxammann opened this issue Jan 1, 2022 · 3 comments

Comments

@maxammann
Copy link
Contributor

Description
I'm drawing several instances of vertices which are statically defined in a vertex shader. I'm drawing 7 instances of 6 vertices like in the following. The data for each instance is bound to slot 0.

​pass.set_vertex_buffer(0, self.instances.slice(..));
pass.draw(0..6, 0..7);

This works nicely on Vulkan. I get 7 squares with the vertex shader below. On OpenGL and WebGL nothing is drawn.

WGSL shader:

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);
}

Repro steps
None right now.

Expected vs observed behavior
Right now nothing is drawn with OpenGL. I would expect that drawing without a vertex buffer works with OpenGL.

Extra materials
None

Platform
wgpu: 0.12 (latest)
OpenGL: ES 3.2 Mesa 21.3.2

@maxammann
Copy link
Contributor Author

maxammann commented Jan 2, 2022

Here is a renderdoc log:
renderdoc.rdc.zip

And a spectorjs log:
spectorjs.json.zip

@maxammann
Copy link
Contributor Author

Alright, so I noticed that the issue is not the rendering code, but my shader. It works if I move the VERTICES array into the main():

[[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 {
var 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)
);

    var a_position = VERTICES[vertex_idx];

    // Encode a square within the shader

    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);
}

@maxammann maxammann reopened this Jan 2, 2022
@maxammann
Copy link
Contributor Author

Closed in favor of gfx-rs/naga#1638

cwfitzgerald pushed a commit that referenced this issue Oct 25, 2023
* error on param redefinition

* also fix #2312
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant