From c77d329d13172d4bc0388e42295af0216293084a Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Fri, 12 May 2017 12:06:02 -0400 Subject: add instanced draw with uniform buffer --- main.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index b26de0d..e6aded0 100644 --- a/main.c +++ b/main.c @@ -42,16 +42,29 @@ 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" + "};\n" + "\n" "layout (location = 0) in mediump vec3 pos;\n" "\n" "void main() {\n" - " gl_Position = vec4(pos.x, pos.y, pos.z, 1);\n" + " mediump float ofs = 0.002 * float(gl_InstanceID - 500);\n" + " gl_Position = vec4(pos.x - ofs, pos.y, pos.z, 1);\n" "}\n"; 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" + "};\n" + "\n" "out mediump vec4 color;\n" "\n" "uniform mediump float time;\n" @@ -222,7 +235,7 @@ ctx_init(context_t * const ctx) { } // init times ring buffer - memset(ctx->times, 0, MAX_TIMES * sizeof(uint32_t) * 2); + // memset(ctx->times, 0, sizeof(ctx->times)); ctx->times_ofs = 0; // init flags @@ -288,7 +301,7 @@ handle_events( if (ev.user.code == EVENT_CODE_TIMER) { uint32_t sum = 0; - #pragma omp parallel for reduction(+:sum) + // #pragma omp parallel for reduction(+:sum) for (int i = 0; i < MAX_TIMES; i++) { sum += ctx->times[i].draw_end - ctx->times[i].draw_start; } @@ -339,6 +352,16 @@ 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); + + // bind uniform buffer to slot 0 + glBindBufferBase(GL_UNIFORM_BUFFER, 2, ubo); + // generate vertex buffer GLuint vbo; glGenBuffers(1, &vbo); @@ -373,7 +396,7 @@ int main(int argc, char *argv[]) { // draw glBindVertexArray(vao); - glDrawArrays(GL_TRIANGLES, 0, 3); + glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1000); glBindVertexArray(0); // swap buffer -- cgit v1.2.3