send vertex data packed

This commit is contained in:
veclav talica 2024-10-14 20:19:18 +03:00
parent f087bf1f7f
commit f7a718003e

View File

@ -9,71 +9,89 @@
#include <stb_ds.h> #include <stb_ds.h>
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 */ /* 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 { typedef struct ElementIndexedQuad {
/* upper-left */ /* upper-left */
Vec2 v0; Vec2Packed v0;
Vec2 uv0; Vec2Packed uv0;
Color c0; ColorPacked c0;
/* bottom-left */ /* bottom-left */
Vec2 v1; Vec2Packed v1;
Vec2 uv1; Vec2Packed uv1;
Color c1; ColorPacked c1;
/* bottom-right */ /* bottom-right */
Vec2 v2; Vec2Packed v2;
Vec2 uv2; Vec2Packed uv2;
Color c2; ColorPacked c2;
/* upper-right */ /* upper-right */
Vec2 v3; Vec2Packed v3;
Vec2 uv3; Vec2Packed uv3;
Color c3; ColorPacked c3;
} ElementIndexedQuad; } ElementIndexedQuad;
typedef struct ElementIndexedQuadWithoutColor { typedef struct ElementIndexedQuadWithoutColor {
/* upper-left */ /* upper-left */
Vec2 v0; Vec2Packed v0;
Vec2 uv0; Vec2Packed uv0;
/* bottom-left */ /* bottom-left */
Vec2 v1; Vec2Packed v1;
Vec2 uv1; Vec2Packed uv1;
/* bottom-right */ /* bottom-right */
Vec2 v2; Vec2Packed v2;
Vec2 uv2; Vec2Packed uv2;
/* upper-right */ /* upper-right */
Vec2 v3; Vec2Packed v3;
Vec2 uv3; Vec2Packed uv3;
} ElementIndexedQuadWithoutColor; } ElementIndexedQuadWithoutColor;
typedef struct ElementIndexedQuadWithoutTexture { typedef struct ElementIndexedQuadWithoutTexture {
/* upper-left */ /* upper-left */
Vec2 v0; Vec2Packed v0;
Color c0; ColorPacked c0;
/* bottom-left */ /* bottom-left */
Vec2 v1; Vec2Packed v1;
Color c1; ColorPacked c1;
/* bottom-right */ /* bottom-right */
Vec2 v2; Vec2Packed v2;
Color c2; ColorPacked c2;
/* upper-right */ /* upper-right */
Vec2 v3; Vec2Packed v3;
Color c3; ColorPacked c3;
} ElementIndexedQuadWithoutTexture; } ElementIndexedQuadWithoutTexture;
typedef struct ElementIndexedQuadWithoutColorWithoutTexture { typedef struct ElementIndexedQuadWithoutColorWithoutTexture {
/* upper-left */ /* upper-left */
Vec2 v0; Vec2Packed v0;
/* bottom-left */ /* bottom-left */
Vec2 v1; Vec2Packed v1;
/* bottom-right */ /* bottom-right */
Vec2 v2; Vec2Packed v2;
/* upper-right */ /* upper-right */
Vec2 v3; Vec2Packed v3;
} ElementIndexedQuadWithoutColorWithoutTexture; } ElementIndexedQuadWithoutColorWithoutTexture;
@ -388,62 +406,62 @@ bool push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch,
{ {
if (!batch.constant_colored && batch.textured) { if (!batch.constant_colored && batch.textured) {
ElementIndexedQuad const buffer_element = { ElementIndexedQuad const buffer_element = {
.v0 = v0, .v0 = m_vec2_packed_from(v0),
.v1 = v1, .v1 = m_vec2_packed_from(v1),
.v2 = v2, .v2 = m_vec2_packed_from(v2),
.v3 = v3, .v3 = m_vec2_packed_from(v3),
.uv0 = uv0, .uv0 = m_vec2_packed_from(uv0),
.uv1 = uv1, .uv1 = m_vec2_packed_from(uv1),
.uv2 = uv2, .uv2 = m_vec2_packed_from(uv2),
.uv3 = uv3, .uv3 = m_vec2_packed_from(uv3),
/* equal for all (flat shaded) */ /* equal for all (flat shaded) */
.c0 = color, .c0 = m_color_packed_from(color),
.c1 = color, .c1 = m_color_packed_from(color),
.c2 = color, .c2 = m_color_packed_from(color),
.c3 = color, .c3 = m_color_packed_from(color),
}; };
return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element);
} else if (batch.constant_colored && batch.textured) { } else if (batch.constant_colored && batch.textured) {
ElementIndexedQuadWithoutColor const buffer_element = { ElementIndexedQuadWithoutColor const buffer_element = {
.v0 = v0, .v0 = m_vec2_packed_from(v0),
.v1 = v1, .v1 = m_vec2_packed_from(v1),
.v2 = v2, .v2 = m_vec2_packed_from(v2),
.v3 = v3, .v3 = m_vec2_packed_from(v3),
.uv0 = uv0, .uv0 = m_vec2_packed_from(uv0),
.uv1 = uv1, .uv1 = m_vec2_packed_from(uv1),
.uv2 = uv2, .uv2 = m_vec2_packed_from(uv2),
.uv3 = uv3, .uv3 = m_vec2_packed_from(uv3),
}; };
return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element);
} else if (!batch.constant_colored && !batch.textured) { } else if (!batch.constant_colored && !batch.textured) {
ElementIndexedQuadWithoutTexture const buffer_element = { ElementIndexedQuadWithoutTexture const buffer_element = {
.v0 = v0, .v0 = m_vec2_packed_from(v0),
.v1 = v1, .v1 = m_vec2_packed_from(v1),
.v2 = v2, .v2 = m_vec2_packed_from(v2),
.v3 = v3, .v3 = m_vec2_packed_from(v3),
/* equal for all (flat shaded) */ /* equal for all (flat shaded) */
.c0 = color, .c0 = m_color_packed_from(color),
.c1 = color, .c1 = m_color_packed_from(color),
.c2 = color, .c2 = m_color_packed_from(color),
.c3 = color, .c3 = m_color_packed_from(color),
}; };
return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element);
} else if (batch.constant_colored && !batch.textured) { } else if (batch.constant_colored && !batch.textured) {
ElementIndexedQuadWithoutColorWithoutTexture const buffer_element = { ElementIndexedQuadWithoutColorWithoutTexture const buffer_element = {
.v0 = v0, .v0 = m_vec2_packed_from(v0),
.v1 = v1, .v1 = m_vec2_packed_from(v1),
.v2 = v2, .v2 = m_vec2_packed_from(v2),
.v3 = v3, .v3 = m_vec2_packed_from(v3),
}; };
return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); 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; (void)font_data;
ElementIndexedQuadWithoutColor buffer_element = { ElementIndexedQuadWithoutColor buffer_element = {
.v0 = (Vec2){ quad.x0, quad.y0 }, .v0 = (Vec2Packed){ quad.x0, quad.y0 },
.v1 = (Vec2){ quad.x1, quad.y0 }, .v1 = (Vec2Packed){ quad.x1, quad.y0 },
.v2 = (Vec2){ quad.x1, quad.y1 }, .v2 = (Vec2Packed){ quad.x1, quad.y1 },
.v3 = (Vec2){ quad.x0, quad.y1 }, .v3 = (Vec2Packed){ quad.x0, quad.y1 },
.uv0 = (Vec2){ quad.s0, quad.t0 }, .uv0 = (Vec2Packed){ quad.s0, quad.t0 },
.uv1 = (Vec2){ quad.s1, quad.t0 }, .uv1 = (Vec2Packed){ quad.s1, quad.t0 },
.uv2 = (Vec2){ quad.s1, quad.t1 }, .uv2 = (Vec2Packed){ quad.s1, quad.t1 },
.uv3 = (Vec2){ quad.s0, quad.t1 }, .uv3 = (Vec2Packed){ quad.s0, quad.t1 },
}; };
return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element); return push_to_vertex_buffer_builder(builder, &buffer_element, sizeof buffer_element);