简体   繁体   English

如何将顶点缓冲区绑定到 Gfx-rs 中的统一数组?

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

I am trying to pass a list of Uniform s to a vertex shader using gfx-rs.我正在尝试使用 gfx-rs 将Uniform列表传递给顶点着色器。 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:当我在main()引入第一行时,应用程序崩溃并显示错误:

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

Full code 完整代码

How to bind a vertex buffer to a uniform array [...]如何将顶点缓冲区绑定到统一数组 [...]

In OpenGL a vertex buffer cannot be "bound" to a uniform array.在 OpenGL 中,顶点缓冲区不能“绑定”到统一数组。

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.一个顶点属性可以寻址一个命名的顶点缓冲区对象(存储在顶点数组对象的状态向量中),但一个uniform 不能。 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.如果您想将某种缓冲区绑定到某种统一,那么您必须使用统一缓冲区对象,该对象从 OpenGL 3.1 和 GLSL 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.或者您可以使用Shader Storage Buffer Object ,其中缓冲区的最后一个元素可以是可变大小的数组。 SSBOs are available since OpenGL 4.3 (GLSL version 4.30) respectively by the extension ARB_shader_storage_buffer_object .自 OpenGL 4.3(GLSL 版本 4.30)起, ARB_shader_storage_buffer_object分别通过扩展ARB_shader_storage_buffer_object

eg:例如:

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

See also Using Shader Storage Buffer Objects (SSBOs)另请参阅使用着色器存储缓冲区对象 (SSBO)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM