Commit c7e5ff41 by Philipp Adolf

Add a simple compute shader with in- and output

parent 2aaf8beb
#version 430 #version 430 core
layout (local_size_x = 1) in; layout (local_size_x = 2) in;
void main() {
layout(std430, binding=0) readonly buffer Input {
float offset;
};
layout(std430, binding=1) writeonly buffer Output {
float data[];
};
void main(){
data[gl_GlobalInvocationID.x] = 100 * gl_GlobalInvocationID.x + gl_LocalInvocationID.x + offset;
} }
...@@ -95,4 +95,52 @@ void Subdivision::precomputeTables(Mesh *mesh) { ...@@ -95,4 +95,52 @@ void Subdivision::precomputeTables(Mesh *mesh) {
} }
void Subdivision::runShader(Mesh *mesh) { void Subdivision::runShader(Mesh *mesh) {
/*
* 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";
int num = 6 * 2; // must be a multiple of two as we do not handle the other case
GLfloat offset = 3.0f;
shader->bind();
// Create an input buffer and set it to the value of offset
GLuint offsetBufferID;
f->glGenBuffers(1, &offsetBufferID);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, offsetBufferID);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLfloat), &offset, GL_DYNAMIC_DRAW);
// Create an output buffer
GLuint bufferID;
f->glGenBuffers(1, &bufferID);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, bufferID);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLfloat) * num, NULL, GL_DYNAMIC_DRAW);
// Run the shader
f->glDispatchCompute(num/2, 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);
// Map the output buffer so we can read the results
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID);
GLfloat *ptr;
ptr = (GLfloat *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
for (int i = 0; i < num; i++) {
qDebug()<<i<<ptr[i];
}
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
// Delete the buffers the free the resources
f->glDeleteBuffers(1, &offsetBufferID);
f->glDeleteBuffers(1, &bufferID);
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