summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2017-05-12 12:49:33 -0400
committerPaul Duncan <pabs@pablotron.org>2017-05-12 12:49:33 -0400
commitdb9386f3c59112867784dc448089d88b9e2186aa (patch)
tree8dc06b4d25d0f29723628a3e5fa1d06daa0c7755
parentc77d329d13172d4bc0388e42295af0216293084a (diff)
downloadcompute-test-db9386f3c59112867784dc448089d88b9e2186aa.tar.bz2
compute-test-db9386f3c59112867784dc448089d88b9e2186aa.zip
use shader storage instead of uniform buffer, remove hard-coded particle count
-rw-r--r--main.c44
1 files changed, 24 insertions, 20 deletions
diff --git a/main.c b/main.c
index e6aded0..e112bfa 100644
--- a/main.c
+++ b/main.c
@@ -12,6 +12,10 @@
// custom SDL_USEREVENT code for timer
#define EVENT_CODE_TIMER 0
+#define NUM_PARTICLES 1000
+#define SS(v) #v
+#define S(v) SS(v)
+
typedef struct {
const GLuint type;
const char *src;
@@ -42,10 +46,10 @@ static const char
vs_src[] =
"#version 320 es\n"
"\n"
- "layout (std140, binding = 0) uniform particles {\n"
- " mediump vec4 positions[1000];\n"
- " mediump vec4 velocities[1000];\n"
- " mediump vec4 colors[1000];\n"
+ "layout (std140, binding = 0) buffer particles {\n"
+ " mediump vec4 positions[" S(NUM_PARTICLES) "];\n"
+ " mediump vec4 velocities[" S(NUM_PARTICLES) "];\n"
+ " mediump vec4 colors[" S(NUM_PARTICLES) "];\n"
"};\n"
"\n"
"layout (location = 0) in mediump vec3 pos;\n"
@@ -59,10 +63,10 @@ static const char
fs_src[] =
"#version 320 es\n"
"\n"
- "layout (std140, binding = 0) uniform particles {\n"
- " mediump vec4 positions[1000];\n"
- " mediump vec4 velocities[1000];\n"
- " mediump vec4 colors[1000];\n"
+ "layout (std140, binding = 0) buffer particles {\n"
+ " mediump vec4 positions[" S(NUM_PARTICLES) "];\n"
+ " mediump vec4 velocities[" S(NUM_PARTICLES) "];\n"
+ " mediump vec4 colors[" S(NUM_PARTICLES) "];\n"
"};\n"
"\n"
"out mediump vec4 color;\n"
@@ -352,15 +356,15 @@ int main(int argc, char *argv[]) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
- // generate uniform buffer
- GLuint ubo;
- glGenBuffers(1, &ubo);
- glBindBuffer(GL_UNIFORM_BUFFER, ubo);
- glBufferData(GL_UNIFORM_BUFFER, 1000 * 3 * 4 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
+ // generate shader storage buffer
+ GLuint ssbo;
+ glGenBuffers(1, &ssbo);
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
+ glBufferData(GL_SHADER_STORAGE_BUFFER, NUM_PARTICLES * 3 * 4 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
// bind uniform buffer to slot 0
- glBindBufferBase(GL_UNIFORM_BUFFER, 2, ubo);
+ // glBindBufferBase(GL_UNIFORM_BUFFER, 2, ubo);
// generate vertex buffer
GLuint vbo;
@@ -375,8 +379,8 @@ int main(int argc, char *argv[]) {
glBindVertexArray(0);
// link program, get uniforms
- GLuint prog = link_program(2, RENDER_SHADERS);
- GLint u_time = glGetUniformLocation(prog, "time");
+ GLuint render_prog = link_program(2, RENDER_SHADERS);
+ GLint u_time = glGetUniformLocation(render_prog, "time");
// main loop
while (!ctx.done) {
@@ -390,13 +394,13 @@ int main(int argc, char *argv[]) {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
- // use program, set uniform
- glUseProgram(prog);
+ // use render program, set uniform
+ glUseProgram(render_prog);
glUniform1f(u_time, now / 1000.0);
// draw
glBindVertexArray(vao);
- glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1000);
+ glDrawArraysInstanced(GL_TRIANGLES, 0, 3, NUM_PARTICLES);
glBindVertexArray(0);
// swap buffer