Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a vertex buffer to a uniform array in Gfx-rs?

Tags:

I am trying to pass a list of Uniforms to a vertex shader using gfx-rs. The data is defined as follows

gfx_defines! {
    vertex Vertex { ... }

    constant MyConst {
        valoo: i32 = "my_val",
    }

    pipeline pipe {
        my_const: gfx::ConstantBuffer<MyConst> = "my_const",
        vbuf: gfx::VertexBuffer<Vertex> = (),
        out: gfx::RenderTarget<ColorFormat> = "Target0",
    }
}

The vertex shader is as follows:

#version 150 core

struct MyConst
{
    uint my_val;
};

in vec2 a_Pos;
in vec3 a_Color;
uniform MyConst my_const[];
out vec4 v_Color;

void main() {
    MyConst cc = my_const[0];
    v_Color = vec4(a_Color, 1.0);
    gl_Position = vec4(a_Pos, 0.0, 1.0);
}

When I introduce the first line in main(), the application crashes with the error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: DescriptorInit(GlobalConstant("my_const[0].my_val", None))'

Full code

like image 479
Mercurious Avatar asked Jul 01 '19 20:07

Mercurious


1 Answers

How to bind a vertex buffer to a uniform array [...]

In OpenGL a vertex buffer cannot be "bound" to a uniform array.

A vertex attribute can address a named vertex buffer object (stored in the state vector of the Vertex Array Object), but a uniform can't. See Vertex Specification.

If you want to bind some kind of buffer to some kind of uniform, then you've to use a Uniform Buffer Object which is available since OpenGL 3.1 respectively GLSL version 1.40.

Or you can use a Shader Storage Buffer Object, where the last element of the buffer can be an array of variable size. SSBOs are available since OpenGL 4.3 (GLSL version 4.30) respectively by the extension ARB_shader_storage_buffer_object.

e.g.:

layout(std430, binding = 0) buffer MyConst
{
    uint my_const[];
};

See also Using Shader Storage Buffer Objects (SSBOs)

like image 54
Rabbid76 Avatar answered Oct 02 '22 15:10

Rabbid76