glium / glium

Safe OpenGL wrapper for the Rust language.
Apache License 2.0
3.51k stars 406 forks source link

Cannot implement a transform feedback shader that receives and writes to a VertexBuffer with same vertex type #1565

Open ColonelThirtyTwo opened 7 years ago

ColonelThirtyTwo commented 7 years ago

I can't figure out how to write and use a transform feedback enabled vertex shader that takes in as well as writes to a VertexBuffer of the same type.

The crux of the issue is that glium matches shader vertex attribute variables as well as transform feedback output variables with vertex buffer attributes based on their names; i.e. for a simple vertex structure struct Vertex { position: (f32, f32, f32, f32) }, position would be matched to the shader vertex attribute variable in vec4 position;

This is an issue when using transform feedback, as for glium to properly bind the shader variables correctly, the shader must have an in vec4 position; variable for the vertex attribute coming in, as well as a out vec4 position; variable for the transform feedback outputs. This is obviously impossible, since it creates a name conflict in the shader. And the developer cannot rename either of the variables, since glium will not be able correctly bind them.

What can I do to correct this issue, and use transform feedback where both the input and the output buffers have the same type?

My best guess would be to create another vertex type that has the exact same structure as the real vertex type, but has different field names, and unsafely cast the VertexBuffer to the fake type, effectively renaming the fields, but I can't figure out how to do that.

(My use case is a double-buffered particle system; the transform feedback shader would read a vertex buffer containing the current particle states, do a step of the simulation, and write the new particle state to another vertex buffer, which would be read by the same shader the next frame.)

unknownue commented 5 years ago

The same problem occurs to me when I try to implement a particle example using transform feedback feature. This problem does make transform feedback hard to use. I spent hours to find solution to it and finally gave up.