From c49789f1f42c707827dd98ac8f539b4d0467f1f3 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sat, 19 Oct 2024 20:02:39 +0300 Subject: [PATCH] make deferred space triangles work --- src/rendering/twn_gl_15_rendering.c | 79 ++++++++++++++++------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index b548772..187e971 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -104,6 +104,9 @@ typedef struct { GLsizei element_count; GLsizei range_start, range_end; + /* could be either `element_count` with supplied `element_buffer`, or this, but not both */ + GLsizei primitive_count; + double depth_range_low, depth_range_high; } DeferredCommandDraw; @@ -228,8 +231,7 @@ static void issue_deferred_draw_commands(void) { /* TODO: don't assume a single vertex array ? */ SDL_assert(command.vertices.arity != 0); SDL_assert(command.vertices.buffer); - SDL_assert(command.element_count != 0); - SDL_assert(command.element_buffer); + SDL_assert((command.element_buffer && command.element_count != 0) || command.primitive_count != 0); glBindBuffer(GL_ARRAY_BUFFER, command.vertices.buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.element_buffer); @@ -274,15 +276,21 @@ static void issue_deferred_draw_commands(void) { textures_bind(&ctx.texture_cache, command.texture_key); } - if (command.range_start == command.range_end) - glDrawElements(GL_TRIANGLES, command.element_count, GL_UNSIGNED_SHORT, NULL); - else - glDrawRangeElements(GL_TRIANGLES, - command.range_start, - command.range_end, - command.element_count, - GL_UNSIGNED_SHORT, - NULL); + if (command.element_buffer) { + SDL_assert(command.element_count != 0); + if (command.range_start == command.range_end) + glDrawElements(GL_TRIANGLES, command.element_count, GL_UNSIGNED_SHORT, NULL); + else + glDrawRangeElements(GL_TRIANGLES, + command.range_start, + command.range_end, + command.element_count, + GL_UNSIGNED_SHORT, + NULL); + } else { + SDL_assert(command.primitive_count != 0); + glDrawArrays(GL_TRIANGLES, 0, command.primitive_count); + } /* state clearing */ @@ -735,37 +743,36 @@ void finally_draw_uncolored_space_traingle_batch(const MeshBatch *batch, const TextureKey texture_key, const VertexBuffer buffer) { - const size_t primitives_len = arrlenu(batch->primitives); + DeferredCommandDraw command = {0}; - textures_bind(&ctx.texture_cache, texture_key); + command.vertices = (AttributeArrayPointer) { + .arity = 3, + .type = GL_FLOAT, + .stride = offsetof(struct UncoloredSpaceTrianglePayload, v1), + .offset = offsetof(struct UncoloredSpaceTrianglePayload, v0), + .buffer = buffer + }; - use_texture_mode(textures_get_mode(&ctx.texture_cache, texture_key)); + command.texcoords = (AttributeArrayPointer) { + .arity = 2, + .type = GL_FLOAT, + .stride = offsetof(struct UncoloredSpaceTrianglePayload, v1), + .offset = offsetof(struct UncoloredSpaceTrianglePayload, uv0), + .buffer = buffer + }; - glBindBuffer(GL_ARRAY_BUFFER, buffer); + command.textured = true; + command.texture_key = texture_key; - /* vertex specification*/ - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(3, - GL_FLOAT, - offsetof(struct UncoloredSpaceTrianglePayload, v1), - (void *)offsetof(struct UncoloredSpaceTrianglePayload, v0)); + const GLuint primitives_len = arrlenu(batch->primitives); + command.primitive_count = 3 * primitives_len; - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glClientActiveTexture(GL_TEXTURE0); - glTexCoordPointer(2, - GL_FLOAT, - offsetof(struct UncoloredSpaceTrianglePayload, v1), - (void *)offsetof(struct UncoloredSpaceTrianglePayload, uv0)); + DeferredCommand final_command = { + .type = DEFERRED_COMMAND_TYPE_DRAW, + .draw = command + }; - /* commit for drawing */ - glDrawArrays(GL_TRIANGLES, 0, 3 * (GLint)primitives_len); - - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - /* invalidate the buffer immediately */ - glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + arrpush(deferred_commands, final_command); }