Commit 44d0a2d5 by Philipp Adolf

Pass vertex buffer and edge table to shader

parent 1884395d
#version 430 core #version 430 core
layout (local_size_x = 2) in; layout (local_size_x = 1) in;
layout(std430, binding=0) readonly buffer Input { struct Vertex {
float offset; vec3 pos;
vec3 norm;
vec2 uv;
}; };
layout(std430, binding=1) writeonly buffer Output { layout(std430, binding=0) readonly buffer Vertices {
Vertex vertices[];
};
layout(std430, binding=1) readonly buffer EdgeIndices {
unsigned int indices[][4];
};
layout(std430, binding=2) writeonly buffer Output {
float data[]; float data[];
}; };
void main(){ void main(){
data[gl_GlobalInvocationID.x] = 100 * gl_GlobalInvocationID.x + gl_LocalInvocationID.x + offset; data[gl_GlobalInvocationID.x] = vertices[gl_GlobalInvocationID.x].pos.x;
} }
...@@ -266,32 +266,47 @@ void Subdivision::precomputeTables(Mesh *mesh, QVector<QVector<unsigned int> > & ...@@ -266,32 +266,47 @@ void Subdivision::precomputeTables(Mesh *mesh, QVector<QVector<unsigned int> > &
} }
void Subdivision::runShader(Mesh *mesh, QVector<QVector<unsigned int> > &edgeIndices) { void Subdivision::runShader(Mesh *mesh, QVector<QVector<unsigned int> > &edgeIndices) {
/*
* This shader will set the output buffer to its global ID times hundred + the local id + offset.
*
* So, for offset = 0 we will get 0, 101, 200, 301 and so on as there's two threads in a local group.
*/
qDebug()<<"Running compute shader"; qDebug()<<"Running compute shader";
int num = 6 * 2; // must be a multiple of two as we do not handle the other case
GLfloat offset = 3.0f; Mesh::Node root = mesh->getRootNode();
int firstMeshEntryIndex = -1;
bool meshExists = root.getFirstMeshIndex(firstMeshEntryIndex);
if (!meshExists){
qDebug()<<"No Mesh found. Abort shader execution.";
return;
}
qDebug()<<"Mesh found. Index:"<<firstMeshEntryIndex;
Mesh::MeshEntry *firstMeshEntry = mesh->getMeshEntry(firstMeshEntryIndex);
GLuint vb_handle = firstMeshEntry->VB_handle;
QVector<Vertex> vb = firstMeshEntry->getVertexBuffer();
shader->bind(); shader->bind();
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vb_handle);
GLuint *edgeIDs = new GLuint[edgeIndices.size() * 4];
for (uint i = 0; i < edgeIndices.size(); i++) {
for (uint j = 0; j < 4; j++) {
edgeIDs[4 * i + j] = edgeIndices[i][j];
}
}
// Create an input buffer and set it to the value of offset // Create an input buffer and set it to the value of offset
GLuint offsetBufferID; GLuint indicesID;
f->glGenBuffers(1, &offsetBufferID); f->glGenBuffers(1, &indicesID);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, offsetBufferID); f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, indicesID);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLfloat), &offset, GL_DYNAMIC_DRAW); f->glBufferData(GL_SHADER_STORAGE_BUFFER, edgeIndices.size() * sizeof(GLuint) * 4, &edgeIDs[0], GL_DYNAMIC_DRAW);
delete edgeIDs;
// Create an output buffer // Create an output buffer
GLuint bufferID; GLuint bufferID;
f->glGenBuffers(1, &bufferID); f->glGenBuffers(1, &bufferID);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, bufferID); f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bufferID);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLfloat) * num, NULL, GL_DYNAMIC_DRAW); f->glBufferData(GL_SHADER_STORAGE_BUFFER, vb.size() * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
// Run the shader // Run the shader
f->glDispatchCompute(num/2, 1, 1); f->glDispatchCompute(vb.size(), 1, 1);
// Wait for the shader to complete and the data to be written back to the global memory // Wait for the shader to complete and the data to be written back to the global memory
f->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); f->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
...@@ -299,18 +314,19 @@ void Subdivision::runShader(Mesh *mesh, QVector<QVector<unsigned int> > &edgeInd ...@@ -299,18 +314,19 @@ void Subdivision::runShader(Mesh *mesh, QVector<QVector<unsigned int> > &edgeInd
// Unbind the buffers from the shader // Unbind the buffers from the shader
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0); f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0); f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);
// Map the output buffer so we can read the results // Map the output buffer so we can read the results
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID); f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID);
GLfloat *ptr; GLfloat *ptr;
ptr = (GLfloat *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY); ptr = (GLfloat *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
for (int i = 0; i < num; i++) { for (int i = 0; i < vb.size(); i++) {
qDebug()<<i<<ptr[i]; qDebug() << ptr[i] << vb[i].pos.x();
} }
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
// Delete the buffers the free the resources // Delete the buffers the free the resources
f->glDeleteBuffers(1, &offsetBufferID); f->glDeleteBuffers(1, &indicesID);
f->glDeleteBuffers(1, &bufferID); f->glDeleteBuffers(1, &bufferID);
shader->release(); shader->release();
......
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