summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2017-05-12 17:48:40 -0400
committerPaul Duncan <pabs@pablotron.org>2017-05-12 17:48:40 -0400
commitd3decc8d48e8007aae6e4efdeb98dc6166987768 (patch)
treee352da969dd9e15d1584181f0d3c6dadcd64b523
parentdb9386f3c59112867784dc448089d88b9e2186aa (diff)
downloadcompute-test-d3decc8d48e8007aae6e4efdeb98dc6166987768.tar.bz2
compute-test-d3decc8d48e8007aae6e4efdeb98dc6166987768.zip
add init_particles(), use SSBO
-rw-r--r--Makefile2
-rw-r--r--main.c85
2 files changed, 65 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index f0f7b3a..7ef6f8d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CFLAGS=-std=c11 -fopenmp -W -Wall -pedantic -O2
APP=compute-test
-LIBS=-fopenmp -lSDL2 -lGLEW -lGL
+LIBS=-fopenmp -lSDL2 -lGLEW -lGL -lcrypto
OBJS=main.o
.PHONY=all clean test
diff --git a/main.c b/main.c
index e112bfa..cc9ed66 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,7 @@
#include <stdbool.h>
#include <stdlib.h>
+#include <openssl/rand.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
@@ -37,36 +38,35 @@ typedef struct {
static const float
verts[] = {
- -0.5, 0.5, 0,
- 0.5, 0.5, 0,
- 0.0, -0.5, 0,
+ -1, 1, 0,
+ 1, 1, 0,
+ 0.0, -1, 0,
};
static const char
vs_src[] =
"#version 320 es\n"
"\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"
+ "layout (std430, binding = 1) buffer particles {\n"
+ " highp vec4 positions[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 velocities[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 colors[" S(NUM_PARTICLES) "];\n"
"};\n"
"\n"
"layout (location = 0) in mediump vec3 pos;\n"
"\n"
"void main() {\n"
- " mediump float ofs = 0.002 * float(gl_InstanceID - 500);\n"
- " gl_Position = vec4(pos.x - ofs, pos.y, pos.z, 1);\n"
+ " gl_Position = positions[gl_InstanceID] + 0.025 * vec4(pos, 1);\n"
"}\n";
static const char
fs_src[] =
"#version 320 es\n"
"\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"
+ "layout (std430, binding = 1) buffer particles {\n"
+ " highp vec4 positions[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 velocities[" S(NUM_PARTICLES) "];\n"
+ " highp vec4 colors[" S(NUM_PARTICLES) "];\n"
"};\n"
"\n"
"out mediump vec4 color;\n"
@@ -340,6 +340,56 @@ static Uint32 timer_cb(
return interval;
}
+static GLuint
+init_particles(void) {
+ // create shader storage buffer
+ GLuint ssbo;
+ glGenBuffers(1, &ssbo);
+
+ // generate seed data
+ uint8_t seeds[4 * NUM_PARTICLES];
+ if (!RAND_bytes(seeds, 4 * NUM_PARTICLES)) {
+ SDL_Log("RAND_bytes() failed");
+ exit(EXIT_FAILURE);
+ }
+
+ // populate data
+ float data[3 * 4 * NUM_PARTICLES];
+ #pragma omp parallel for
+ for (int i = 0; i < NUM_PARTICLES; i++) {
+ float x = 2.0 * seeds[4 * i + 0] / 0xFF - 1.0,
+ y = 2.0 * seeds[4 * i + 1] / 0xFF - 1.0;
+
+ // SDL_Log("%f,%f", x, y);
+
+ // populate position
+ data[4 * i + 0] = x;
+ data[4 * i + 1] = y;
+ data[4 * i + 2] = 0.0;
+ data[4 * i + 3] = 1.0;
+
+ // populate random velocity
+ data[4 * (NUM_PARTICLES + i) + 0] = 2.0 * seeds[4 * i + 2] / 0xFF - 1.0;
+ data[4 * (NUM_PARTICLES + i) + 1] = 2.0 * seeds[4 * i + 3] / 0xFF - 1.0;
+
+ // populate colors
+ data[4 * (2 * NUM_PARTICLES + i) + 0] = ((i % 3) == 0) ? 1.0 : 0.0;
+ data[4 * (2 * NUM_PARTICLES + i) + 1] = ((i % 3) == 1) ? 1.0 : 0.0;
+ data[4 * (2 * NUM_PARTICLES + i) + 2] = ((i % 3) == 2) ? 1.0 : 0.0;
+ data[4 * (2 * NUM_PARTICLES + i) + 3] = 1.0;
+ }
+
+ // populate buffer
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
+ glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
+
+ glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbo);
+
+ // return buffer
+ return ssbo;
+}
+
int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
@@ -357,14 +407,7 @@ int main(int argc, char *argv[]) {
glBindVertexArray(vao);
// 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);
+ GLuint ssbo = init_particles();
// generate vertex buffer
GLuint vbo;