From 8a5d639f95fde8a01e5d65dfde4b2afaf665c749 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Fri, 17 Jan 2025 22:48:35 +0300 Subject: [PATCH] remove indirection in vertex builder --- src/rendering/twn_billboards.c | 4 ++- src/rendering/twn_draw_c.h | 16 +++--------- src/rendering/twn_gl_15_rendering.c | 25 +++++------------- src/rendering/twn_gl_any_rendering.c | 30 ++++++++++----------- src/rendering/twn_quads.c | 20 +++++++------- src/rendering/twn_rects.c | 2 +- src/rendering/twn_sprites.c | 2 +- src/rendering/twn_text.c | 39 +++++++++++----------------- 8 files changed, 53 insertions(+), 85 deletions(-) diff --git a/src/rendering/twn_billboards.c b/src/rendering/twn_billboards.c index eba0364..d2dde13 100644 --- a/src/rendering/twn_billboards.c +++ b/src/rendering/twn_billboards.c @@ -125,9 +125,11 @@ void finally_draw_billboard_batch(struct MeshBatch const *batch, .v3 = vec3_add(billboard.position, a), }; - push_to_vertex_buffer_builder(&builder, &payload, sizeof (payload)); + ((struct ElementIndexedBillboard *)builder.base)[i] = payload; } + finish_vertex_builder(&builder); + /* commit to drawing */ DeferredCommandDraw command = {0}; diff --git a/src/rendering/twn_draw_c.h b/src/rendering/twn_draw_c.h index 1ee4d1f..171b6fd 100644 --- a/src/rendering/twn_draw_c.h +++ b/src/rendering/twn_draw_c.h @@ -37,9 +37,7 @@ enum { typedef uint32_t VertexBuffer; typedef struct VertexBufferBuilder { - size_t bytes_left; size_t size; - void *mapping; void *base; } VertexBufferBuilder; @@ -271,10 +269,7 @@ void specify_vertex_buffer(VertexBuffer buffer, void const *data, size_t bytes); /* uses present in 1.5 buffer mapping feature */ VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes); -/* collects bytes for sending to the gpu until all is pushed, which is when false is returned */ -bool push_to_vertex_buffer_builder(VertexBufferBuilder *builder, - void const *bytes, - size_t size); +void finish_vertex_builder(VertexBufferBuilder *builder); /* state */ @@ -301,7 +296,8 @@ void finally_render_quads(Primitive2D const primitives[], size_t get_quad_payload_size(struct QuadBatch batch); -bool push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, +void push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, + size_t index, VertexBufferBuilder *builder, Vec2 v0, Vec2 v1, Vec2 v2, Vec2 v3, Vec2 uv0, Vec2 uv1, Vec2 uv2, Vec2 uv3, @@ -313,12 +309,6 @@ void finally_draw_uncolored_space_traingle_batch(MeshBatch const *batch, void finally_draw_billboard_batch(MeshBatch const *batch, TextureKey texture_key); -size_t get_text_payload_size(void); - -bool push_text_payload_to_vertex_buffer_builder(FontData const *font_data, - VertexBufferBuilder *builder, - stbtt_aligned_quad quad); - void finally_draw_text(FontData const *font_data, size_t len, Color color, diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index 807f225..35e8913 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -218,29 +218,18 @@ VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) { CRY("build_vertex_buffer", "Error mapping a vertex array buffer"); return (VertexBufferBuilder) { - .mapping = mapping, - .bytes_left = bytes, + .base = mapping, + .size = bytes, }; } -bool push_to_vertex_buffer_builder(VertexBufferBuilder *builder, - void const *bytes, size_t size) { - if (builder->bytes_left == 0) - return false; +void finish_vertex_builder(VertexBufferBuilder *builder) { + glUnmapBuffer(GL_ARRAY_BUFFER); + glBindBuffer(GL_ARRAY_BUFFER, 0); - memcpy(builder->mapping, bytes, size); - builder->bytes_left -= size; - - /* trigger data send */ - if (builder->bytes_left == 0) { - glUnmapBuffer(GL_ARRAY_BUFFER); - return false; - } - - builder->mapping = (void *)((uintptr_t)builder->mapping + size); - - return true; + builder->base = 0; + builder->size = 0; } diff --git a/src/rendering/twn_gl_any_rendering.c b/src/rendering/twn_gl_any_rendering.c index 8010d6f..9c1f771 100644 --- a/src/rendering/twn_gl_any_rendering.c +++ b/src/rendering/twn_gl_any_rendering.c @@ -44,16 +44,15 @@ VertexBuffer get_quad_element_buffer(void) { VertexBufferBuilder builder = build_vertex_buffer(buffer, sizeof (GLshort) * QUAD_ELEMENT_BUFFER_LENGTH * 6 ); for (size_t i = 0; i < QUAD_ELEMENT_BUFFER_LENGTH; ++i) { - GLshort indices[6]; - indices[0] = (GLshort)(i * 4 + 0); - indices[1] = (GLshort)(i * 4 + 1); - indices[2] = (GLshort)(i * 4 + 2); - indices[3] = (GLshort)(i * 4 + 2); - indices[4] = (GLshort)(i * 4 + 3); - indices[5] = (GLshort)(i * 4 + 0); - - push_to_vertex_buffer_builder(&builder, indices, sizeof indices); + ((GLshort *)builder.base)[i * 6 + 0] = (GLshort)(i * 4 + 0); + ((GLshort *)builder.base)[i * 6 + 1] = (GLshort)(i * 4 + 1); + ((GLshort *)builder.base)[i * 6 + 2] = (GLshort)(i * 4 + 2); + ((GLshort *)builder.base)[i * 6 + 3] = (GLshort)(i * 4 + 2); + ((GLshort *)builder.base)[i * 6 + 4] = (GLshort)(i * 4 + 3); + ((GLshort *)builder.base)[i * 6 + 5] = (GLshort)(i * 4 + 0); } + + finish_vertex_builder(&builder); } SDL_assert_always(buffer); @@ -71,17 +70,14 @@ VertexBuffer get_circle_element_buffer(void) { for (size_t i = 1; i < CIRCLE_VERTICES_MAX - 1; ++i) { /* first one is center point index, always zero */ - GLshort indices[3]; - - indices[0] = 0; + ((GLshort *)builder.base)[(i - 1) * 3 + 0] = 0; /* generated point index */ - indices[1] = (GLshort)i; - - indices[2] = (GLshort)i + 1; - - push_to_vertex_buffer_builder(&builder, indices, sizeof indices); + ((GLshort *)builder.base)[(i - 1) * 3 + 1] = (GLshort)i; + ((GLshort *)builder.base)[(i - 1) * 3 + 2] = (GLshort)i + 1; } + + finish_vertex_builder(&builder); } SDL_assert_always(buffer); diff --git a/src/rendering/twn_quads.c b/src/rendering/twn_quads.c index 79306b7..38ce6a8 100644 --- a/src/rendering/twn_quads.c +++ b/src/rendering/twn_quads.c @@ -101,14 +101,15 @@ size_t get_quad_payload_size(struct QuadBatch batch) { } -bool push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, +void push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, + size_t index, VertexBufferBuilder *builder, Vec2 v0, Vec2 v1, Vec2 v2, Vec2 v3, Vec2 uv0, Vec2 uv1, Vec2 uv2, Vec2 uv3, Color color) { if (!batch.constant_colored && batch.textured) { - ElementIndexedQuad const buffer_element = { + ElementIndexedQuad const payload = { .v0 = v0, .v1 = v1, .v2 = v2, @@ -126,10 +127,10 @@ bool push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, // .c3 = color, }; - return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); + ((ElementIndexedQuad *)builder->base)[index] = payload; } else if (batch.constant_colored && batch.textured) { - ElementIndexedQuadWithoutColor const buffer_element = { + ElementIndexedQuadWithoutColor const payload = { .v0 = v0, .v1 = v1, .v2 = v2, @@ -141,10 +142,10 @@ bool push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, .uv3 = uv3, }; - return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); + ((ElementIndexedQuadWithoutColor *)builder->base)[index] = payload; } else if (!batch.constant_colored && !batch.textured) { - ElementIndexedQuadWithoutTexture const buffer_element = { + ElementIndexedQuadWithoutTexture const payload = { .v0 = v0, .v1 = v1, .v2 = v2, @@ -157,19 +158,18 @@ bool push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, // .c3 = color, }; - return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); + ((ElementIndexedQuadWithoutTexture *)builder->base)[index] = payload; } else if (batch.constant_colored && !batch.textured) { - ElementIndexedQuadWithoutColorWithoutTexture const buffer_element = { + ElementIndexedQuadWithoutColorWithoutTexture const payload = { .v0 = v0, .v1 = v1, .v2 = v2, .v3 = v3, }; - return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); + ((ElementIndexedQuadWithoutColorWithoutTexture *)builder->base)[index] = payload; } SDL_assert(false); - return false; } diff --git a/src/rendering/twn_rects.c b/src/rendering/twn_rects.c index cf0acad..b82797d 100644 --- a/src/rendering/twn_rects.c +++ b/src/rendering/twn_rects.c @@ -85,7 +85,7 @@ void render_rect_batch(const Primitive2D primitives[], Vec2 v3 = { rect.rect.x + rect.rect.w, rect.rect.y }; push_quad_payload_to_vertex_buffer_builder( - batch, &payload, + batch, i, &payload, v0, v1, v2, v3, (Vec2){0}, (Vec2){0}, (Vec2){0}, (Vec2){0}, rect.color); diff --git a/src/rendering/twn_sprites.c b/src/rendering/twn_sprites.c index 23c2580..750d987 100644 --- a/src/rendering/twn_sprites.c +++ b/src/rendering/twn_sprites.c @@ -242,7 +242,7 @@ void render_sprite_batch(const Primitive2D primitives[], v3 = (Vec2){ c.x + t.x * +h.x - t.y * -h.y, c.y + t.y * +h.x + t.x * -h.y }; } - push_quad_payload_to_vertex_buffer_builder(batch, &payload, v0, v1, v2, v3, uv0, uv1, uv2, uv3, sprite.color); + push_quad_payload_to_vertex_buffer_builder(batch, i, &payload, v0, v1, v2, v3, uv0, uv1, uv2, uv3, sprite.color); } } diff --git a/src/rendering/twn_text.c b/src/rendering/twn_text.c index 2239285..6d8b2ef 100644 --- a/src/rendering/twn_text.c +++ b/src/rendering/twn_text.c @@ -175,7 +175,7 @@ static void text_draw_with(FontData* font_data, char* text, Vec2 position, Color const size_t len = SDL_strlen(text); - VertexBufferBuilder payload = build_vertex_buffer(vertex_array, sizeof (ElementIndexedQuadWithoutColor) * len); + VertexBufferBuilder builder = build_vertex_buffer(vertex_array, sizeof (ElementIndexedQuadWithoutColor) * len); for (size_t i = 0; i < len; ++i) { const char c = text[i]; @@ -204,9 +204,22 @@ static void text_draw_with(FontData* font_data, char* text, Vec2 position, Color quad.y0 += (float)font_data->ascent; quad.y1 += (float)font_data->ascent; - push_text_payload_to_vertex_buffer_builder(font_data, &payload, quad); + ElementIndexedQuadWithoutColor const payload = { + .v0 = (Vec2){ quad.x0, quad.y0 }, + .v1 = (Vec2){ quad.x1, quad.y0 }, + .v2 = (Vec2){ quad.x1, quad.y1 }, + .v3 = (Vec2){ quad.x0, quad.y1 }, + + .uv0 = (Vec2){ quad.s0, quad.t0 }, + .uv1 = (Vec2){ quad.s1, quad.t0 }, + .uv2 = (Vec2){ quad.s1, quad.t1 }, + .uv3 = (Vec2){ quad.s0, quad.t1 }, + }; + + ((ElementIndexedQuadWithoutColor *)builder.base)[i] = payload; } + finish_vertex_builder(&builder); finally_draw_text(font_data, len, color, vertex_array); } @@ -320,28 +333,6 @@ float draw_text_width(const char *string, float height, const char *font) { } -bool push_text_payload_to_vertex_buffer_builder(FontData const *font_data, - VertexBufferBuilder *builder, - stbtt_aligned_quad quad) -{ - (void)font_data; - - ElementIndexedQuadWithoutColor buffer_element = { - .v0 = (Vec2){ quad.x0, quad.y0 }, - .v1 = (Vec2){ quad.x1, quad.y0 }, - .v2 = (Vec2){ quad.x1, quad.y1 }, - .v3 = (Vec2){ quad.x0, quad.y1 }, - - .uv0 = (Vec2){ quad.s0, quad.t0 }, - .uv1 = (Vec2){ quad.s1, quad.t0 }, - .uv2 = (Vec2){ quad.s1, quad.t1 }, - .uv3 = (Vec2){ quad.s0, quad.t1 }, - }; - - return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); -} - - void finally_draw_text(FontData const *font_data, size_t len, Color color,