简体   繁体   English

将数据传递给 GLSL 顶点着色器

[英]Passing data to a GLSL Vertex Shader

I'm trying to convert a program written in C using legacy OpenGL Fixed Pipeline commands.我正在尝试使用旧版 OpenGL 固定管道命令转换用 C 编写的程序。

I'm stuck trying to pass some data into a Vertex Shader.我一直试图将一些数据传递到顶点着色器中。 I'm trying to use the latest 4.5 commands and I have managed to pass in my array of vertex coordinates "vertices[]" to the Vertex Shader using我正在尝试使用最新的 4.5 命令,并且我设法将我的顶点坐标数组“vertices []”传递给顶点着色器使用

glCreateVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);

glCreateBuffers(1, &vertex_buffer);
glNamedBufferStorage(vertex_buffer, sizeof(verticies), verticies, GL_DYNAMIC_STORAGE_BIT);


glVertexArrayVertexBuffer(vertex_array_object, 0, vertex_buffer, 0, sizeof(float)*3);
glVertexArrayAttribFormat(vertex_array_object,0, 3, GL_FLOAT, GL_FALSE, 0);

glVertexArrayAttribBinding(vertex_array_object,0,0);
glEnableVertexArrayAttrib(vertex_array_object,0);

This all works fine and I can render the vertices as points.这一切都很好,我可以将顶点渲染为点。

As well as passing the vertices I also need to pass an additional block of 4 values for each vertex which I then want to pass from the Vertex Shader to a Geometry Shader.除了传递顶点之外,我还需要为每个顶点传递一个额外的 4 个值块,然后我想将其从顶点着色器传递到几何着色器。 The values I need to pass are in an array of structures (1 per vertex) where the structure is defined as我需要传递的值位于结构数组(每个顶点 1 个)中,其中结构定义为

typedef struct {        /* Vertex vector data structure */

unsigned char  v;           /* Scaled magnitude value     "    */
char           x;           /* X Component of direction cosine */
char           y;           /* Y     "      "     "       "    */
char           z;           /* Z     "      "     "       "    */

} NVD;

I can't easily change this structure as it's used in lots of other places in the code.我不能轻易更改此结构,因为它在代码中的许多其他地方都使用过。

Inside the vertex shader I need the 4 values as integers in the ranges在顶点着色器中,我需要 4 个值作为范围内的整数

 v (0->255)
 x,y,z (-127 > 127)

I can't easily change this structure as it's used in lots of other places in the code.我不能轻易更改此结构,因为它在代码中的许多其他地方都使用过。

Well, you're going to have to because you can't use struct s as interface variables between shader stages.好吧,您将不得不这样做,因为您不能使用struct作为着色器阶段之间的接口变量。 You can pass the data along as a single ivec4 , with each component storing the value you want.您可以将数据作为单个ivec4 ,每个组件都存储您想要的值。 Though really, you should just pass the floating-point values you compute in the shader;虽然真的,你应该只传递你在着色器中计算的浮点值; it's going to be 128-bits per-vertex either way, so no point in taking the time to quantize the data.无论哪种方式,每个顶点都将是 128 位,因此花时间量化数据是没有意义的。

If the size of this data is shown through profiling to be an actual problem (and using a GS at all is far more likely to be the performance problem), you can encode the data in a single uint for passing to the GS, then unpacking it on the other end.如果通过分析显示此数据的大小是一个实际问题(并且根本使用 GS 更有可能是性能问题),您可以将数据编码为单个uint以传递给 GS,然后解包它在另一端。

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

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