Commit 9c5ff7de by Philipp Adolf

Execute vertex shader

parent 1c7fdb0d
......@@ -320,6 +320,18 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) {
void Subdivision::runShader(Input input, Tables &tables) {
qDebug()<<"Running compute shader";
// Create an input buffer for the vertex indices
GLuint vertex_indices_handle;
f->glGenBuffers(1, &vertex_indices_handle);
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, vertex_indices_handle);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, tables.vertex_indices.size() * sizeof(GLuint), tables.vertex_indices.data(), GL_DYNAMIC_DRAW);
// Create an input buffer for the vertex offsets
GLuint vertex_offsets_handle;
f->glGenBuffers(1, &vertex_offsets_handle);
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, vertex_offsets_handle);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, tables.vertex_offsets.size() * sizeof(GLuint), tables.vertex_offsets.data(), GL_DYNAMIC_DRAW);
// Create an input buffer for the edge indices
GLuint edge_indices_handle;
f->glGenBuffers(1, &edge_indices_handle);
......@@ -334,6 +346,7 @@ void Subdivision::runShader(Input input, Tables &tables) {
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
runVertexShader(input.vertex_buffer.size(), input.vb_handle, vertex_indices_handle, vertex_offsets_handle, output_handle);
int edgeOffset = input.vertex_buffer.size();
runEdgeShader(tables.edge_indices.size() / 4, input.vb_handle, edge_indices_handle, output_handle, edgeOffset);
......@@ -341,16 +354,46 @@ void Subdivision::runShader(Input input, Tables &tables) {
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_handle);
Vertex *ptr;
ptr = (Vertex *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
qDebug() << "New vertices:";
for (int i = 0; i < input.vertex_buffer.size(); i++) {
qDebug() << ptr[i].pos;
}
qDebug() << "New edge points:";
for (int i = 0; i < tables.edge_indices.size() / 4; i++) {
qDebug() << ptr[edgeOffset + i].pos;
}
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
// Delete the buffers the free the resources
f->glDeleteBuffers(1, &vertex_indices_handle);
f->glDeleteBuffers(1, &vertex_offsets_handle);
f->glDeleteBuffers(1, &edge_indices_handle);
f->glDeleteBuffers(1, &output_handle);
}
void Subdivision::runVertexShader(GLuint size, GLuint vb_handle, GLuint vertex_indices_handle, GLuint vertex_offsets_handle, GLuint output_handle) {
vertexShader->bind();
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vb_handle);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vertex_indices_handle);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, vertex_offsets_handle);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, output_handle);
// Run the shader
f->glDispatchCompute(size, 1, 1);
// Wait for the shader to complete and the data to be written back to the global memory
f->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
// Unbind the buffers from the shader
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, 0);
vertexShader->release();
}
void Subdivision::runEdgeShader(GLuint size, GLuint vb_handle, GLuint edge_indices_handle, GLuint output_handle, GLuint offset) {
edgeShader->bind();
......
......@@ -38,6 +38,7 @@ private:
QOpenGLShaderProgram *initComputeShaderProgram(QString &source);
Tables precomputeTables(Input input);
void runShader(Input input, Tables &tables);
void runVertexShader(GLuint size, GLuint vb_handle, GLuint vertex_indices_handle, GLuint vertex_offsets_handle, GLuint output_handle);
void runEdgeShader(GLuint size, GLuint vb_handle, GLuint edge_indices_handle, GLuint output_handle, GLuint offset);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment