aboutsummaryrefslogtreecommitdiff
path: root/samples/OpenGL/compute_shader/positiongen.c
diff options
context:
space:
mode:
Diffstat (limited to 'samples/OpenGL/compute_shader/positiongen.c')
-rw-r--r--samples/OpenGL/compute_shader/positiongen.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/samples/OpenGL/compute_shader/positiongen.c b/samples/OpenGL/compute_shader/positiongen.c
new file mode 100644
index 0000000..3349ff6
--- /dev/null
+++ b/samples/OpenGL/compute_shader/positiongen.c
@@ -0,0 +1,51 @@
+#include <GL/glew.h>
+#include "positiongen.h"
+
+#include "shaderloader/shaderloader.h"
+#include "debuggl/debuggl.h"
+
+#include <stdio.h>
+
+static struct {
+ GLuint program;
+ GLint w_output;
+ GLint u_time;
+} positiongen;
+
+int positiongen_load(void)
+{
+ debuggl_check( "pre positiongen_load check" );
+
+ char const * const paths[] = { "positiongen.glsl", 0 };
+ shader_program_sources const sources[] = { {GL_COMPUTE_SHADER, paths}, {0,0} };
+
+ if( positiongen.program ) {
+ debuggl_check( glDeleteProgram(positiongen.program) );
+ positiongen.program = 0;
+ }
+
+ positiongen.program = shader_program_load_from_files(sources);
+ if( !positiongen.program ) {
+ fprintf(stderr, "%s failed\n", __func__);
+ return -2;
+ }
+
+ debuggl_check( positiongen.w_output = glGetProgramResourceIndex(positiongen.program, GL_SHADER_STORAGE_BLOCK, "w_output") );
+ debuggl_check( positiongen.u_time = glGetProgramResourceLocation(positiongen.program, GL_UNIFORM, "u_time") );
+
+ return 0;
+}
+
+int positiongen_launch(
+ float t,
+ GLuint vbo,
+ int width,
+ int height )
+{
+ debuggl_check( glUseProgram(positiongen.program) );
+ debuggl_check( glUniform1f(positiongen.u_time, t) );
+ debuggl_check( glBindBufferBase(GL_SHADER_STORAGE_BUFFER, positiongen.w_output, vbo) );
+ debuggl_check( glDispatchCompute(width/16, height/16, 1) );
+ debuggl_check( glBindBufferBase(GL_SHADER_STORAGE_BUFFER, positiongen.w_output, 0) );
+ debuggl_check( glUseProgram(0) );
+}