From f7a718003eb0f1720dbae5d24240272ae7cdac3c Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Mon, 14 Oct 2024 20:19:18 +0300 Subject: [PATCH] send vertex data packed --- src/rendering/twn_gl_15_rendering.c | 170 +++++++++++++++------------- 1 file changed, 94 insertions(+), 76 deletions(-) diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index 894c1ac..badefcf 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -9,71 +9,89 @@ #include +typedef struct { + float x, y; +} Vec2Packed; + + +typedef struct { + int16_t x, y; +} Vec2ShortPacked; + + +typedef struct { + uint8_t r, g, b, a; +} ColorPacked; + + +#define m_vec2_packed_from(p_vec2) ((Vec2Packed){ (p_vec2).x, (p_vec2).y }) +#define m_vec2_short_packed_from(p_vec2) ((Vec2ShortPacked){ (uint16_t)((p_vec2).x * 32768 * 2), (uint16_t)((p_vec2).y * 32768 * 2) }) +#define m_color_packed_from(p_color) ((ColorPacked){ (p_color).r, (p_color).g, (p_color).b, (p_color).a }) + + +/* TODO: try using the fact we utilize edge coloring and step virtual color attributes to bogus points */ +/* this is only doable is we take out color attribute to separate array or a portion of it */ /* interleaved vertex array data */ -/* TODO: use int16_t for uvs */ -/* TODO: use packed types? */ -/* TODO: int16_t could be used for positioning, but we would need to have more CPU calcs */ typedef struct ElementIndexedQuad { /* upper-left */ - Vec2 v0; - Vec2 uv0; - Color c0; + Vec2Packed v0; + Vec2Packed uv0; + ColorPacked c0; /* bottom-left */ - Vec2 v1; - Vec2 uv1; - Color c1; + Vec2Packed v1; + Vec2Packed uv1; + ColorPacked c1; /* bottom-right */ - Vec2 v2; - Vec2 uv2; - Color c2; + Vec2Packed v2; + Vec2Packed uv2; + ColorPacked c2; /* upper-right */ - Vec2 v3; - Vec2 uv3; - Color c3; + Vec2Packed v3; + Vec2Packed uv3; + ColorPacked c3; } ElementIndexedQuad; - typedef struct ElementIndexedQuadWithoutColor { /* upper-left */ - Vec2 v0; - Vec2 uv0; + Vec2Packed v0; + Vec2Packed uv0; /* bottom-left */ - Vec2 v1; - Vec2 uv1; + Vec2Packed v1; + Vec2Packed uv1; /* bottom-right */ - Vec2 v2; - Vec2 uv2; + Vec2Packed v2; + Vec2Packed uv2; /* upper-right */ - Vec2 v3; - Vec2 uv3; + Vec2Packed v3; + Vec2Packed uv3; } ElementIndexedQuadWithoutColor; typedef struct ElementIndexedQuadWithoutTexture { /* upper-left */ - Vec2 v0; - Color c0; + Vec2Packed v0; + ColorPacked c0; /* bottom-left */ - Vec2 v1; - Color c1; + Vec2Packed v1; + ColorPacked c1; /* bottom-right */ - Vec2 v2; - Color c2; + Vec2Packed v2; + ColorPacked c2; /* upper-right */ - Vec2 v3; - Color c3; + Vec2Packed v3; + ColorPacked c3; } ElementIndexedQuadWithoutTexture; typedef struct ElementIndexedQuadWithoutColorWithoutTexture { /* upper-left */ - Vec2 v0; + Vec2Packed v0; /* bottom-left */ - Vec2 v1; + Vec2Packed v1; /* bottom-right */ - Vec2 v2; + Vec2Packed v2; /* upper-right */ - Vec2 v3; + Vec2Packed v3; } ElementIndexedQuadWithoutColorWithoutTexture; @@ -388,62 +406,62 @@ bool push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, { if (!batch.constant_colored && batch.textured) { ElementIndexedQuad const buffer_element = { - .v0 = v0, - .v1 = v1, - .v2 = v2, - .v3 = v3, + .v0 = m_vec2_packed_from(v0), + .v1 = m_vec2_packed_from(v1), + .v2 = m_vec2_packed_from(v2), + .v3 = m_vec2_packed_from(v3), - .uv0 = uv0, - .uv1 = uv1, - .uv2 = uv2, - .uv3 = uv3, + .uv0 = m_vec2_packed_from(uv0), + .uv1 = m_vec2_packed_from(uv1), + .uv2 = m_vec2_packed_from(uv2), + .uv3 = m_vec2_packed_from(uv3), /* equal for all (flat shaded) */ - .c0 = color, - .c1 = color, - .c2 = color, - .c3 = color, + .c0 = m_color_packed_from(color), + .c1 = m_color_packed_from(color), + .c2 = m_color_packed_from(color), + .c3 = m_color_packed_from(color), }; return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); } else if (batch.constant_colored && batch.textured) { ElementIndexedQuadWithoutColor const buffer_element = { - .v0 = v0, - .v1 = v1, - .v2 = v2, - .v3 = v3, + .v0 = m_vec2_packed_from(v0), + .v1 = m_vec2_packed_from(v1), + .v2 = m_vec2_packed_from(v2), + .v3 = m_vec2_packed_from(v3), - .uv0 = uv0, - .uv1 = uv1, - .uv2 = uv2, - .uv3 = uv3, + .uv0 = m_vec2_packed_from(uv0), + .uv1 = m_vec2_packed_from(uv1), + .uv2 = m_vec2_packed_from(uv2), + .uv3 = m_vec2_packed_from(uv3), }; return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); } else if (!batch.constant_colored && !batch.textured) { ElementIndexedQuadWithoutTexture const buffer_element = { - .v0 = v0, - .v1 = v1, - .v2 = v2, - .v3 = v3, + .v0 = m_vec2_packed_from(v0), + .v1 = m_vec2_packed_from(v1), + .v2 = m_vec2_packed_from(v2), + .v3 = m_vec2_packed_from(v3), /* equal for all (flat shaded) */ - .c0 = color, - .c1 = color, - .c2 = color, - .c3 = color, + .c0 = m_color_packed_from(color), + .c1 = m_color_packed_from(color), + .c2 = m_color_packed_from(color), + .c3 = m_color_packed_from(color), }; return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); } else if (batch.constant_colored && !batch.textured) { ElementIndexedQuadWithoutColorWithoutTexture const buffer_element = { - .v0 = v0, - .v1 = v1, - .v2 = v2, - .v3 = v3, + .v0 = m_vec2_packed_from(v0), + .v1 = m_vec2_packed_from(v1), + .v2 = m_vec2_packed_from(v2), + .v3 = m_vec2_packed_from(v3), }; return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); @@ -499,15 +517,15 @@ bool push_text_payload_to_vertex_buffer_builder(FontData const *font_data, (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 }, + .v0 = (Vec2Packed){ quad.x0, quad.y0 }, + .v1 = (Vec2Packed){ quad.x1, quad.y0 }, + .v2 = (Vec2Packed){ quad.x1, quad.y1 }, + .v3 = (Vec2Packed){ 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 }, + .uv0 = (Vec2Packed){ quad.s0, quad.t0 }, + .uv1 = (Vec2Packed){ quad.s1, quad.t0 }, + .uv2 = (Vec2Packed){ quad.s1, quad.t1 }, + .uv3 = (Vec2Packed){ quad.s0, quad.t1 }, }; return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element);