使用SSBO作为下一次绘制调用的VBO输入

时间:2017-07-18 18:26:19

标签: opengl synchronization shader vbo

我在着色器中将一些顶点数据写入SSBO。然后我想将写入SSBO的数据用作VBO。这些将用于下一次绘制调用。怎么办呢?

这是我现在如何做的,但它仍然是段错误的:

int new_vertex_count = …;
int new_index_count = …;
int* new_indices = …;

GLuint ssbo[3];
glGenBuffers(3, ssbo);


glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo[1]);
glBufferData(GL_SHADER_STORAGE_BUFFER, new_vertex_count * 3 * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo[1]);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo[2]);
glBufferData(GL_SHADER_STORAGE_BUFFER, new_vertex_count * 2 * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbo[2]);

glBindVertexArray(vao); //bind the original VAO
glPatchParameteri(GL_PATCH_VERTICES, 16);
glEnable(GL_RASTERIZER_DISCARD); //disable displaying
glDrawElements(GL_PATCHES, index_count, GL_UNSIGNED_INT, 0); //don't draw, just 
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); //sync writing
glFinish();
glDisable(GL_RASTERIZER_DISCARD); //reanable displaying for next draw call
glBindVertexArray(0); //unbind original VAO in order to use new VBOs


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ssbo[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count * sizeof(uint), indices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, ssbo[1]); //bind SSBO as VBO, is this even possible?
//or should I use new VBOs and copy? How would I copy then?
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, ssbo[2]);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);

glDrawElements(GL_PATCHES, new_index_count, new_indices, GL_UNSIGNED_INT, 0); //here the real drawing

1 个答案:

答案 0 :(得分:5)

没有“SSBO”或“VBO”这样的东西。只有缓冲对象。存储块和顶点数组用于缓冲区对象,但特定缓冲区本身并不与特定用途相关联。没有什么可以阻止你通过存储块写入缓冲区,然后在渲染操作中从中读取缓冲区。

当然,只要你遵守incoherent memory access的规则就可以了。除非您明确提供,否则通过存储块写入不可用于读取操作。您可以使用import {App} from '../' 。而memory barrier works的方式是枚举器指定您希望能够看到所有的结果的操作。

您希望将写入的数据作为顶点属性数组读取。所以你使用:

glMemoryBarrier