From f7d7281e7074bc477556cffd619062a556aa2753 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 10 Jul 2022 12:18:31 -0700 Subject: [PATCH] Add index16/index32 FieldTypes; Similar to u16/u32 but are 1-indexed when written using tables. --- src/api/l_graphics.c | 10 +++++++++- src/api/l_graphics_buffer.c | 6 +++++- src/modules/graphics/graphics.c | 3 ++- src/modules/graphics/graphics.h | 4 +++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index 08cbdf67..7bb085dd 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -106,6 +106,8 @@ StringEntry lovrFieldType[] = { [FIELD_MAT2] = ENTRY("mat2"), [FIELD_MAT3] = ENTRY("mat3"), [FIELD_MAT4] = ENTRY("mat4"), + [FIELD_INDEX16] = ENTRY("index16"), + [FIELD_INDEX32] = ENTRY("index32"), { 0 } }; @@ -245,7 +247,9 @@ static struct { uint32_t size, scalarAlign, baseAlign, components; } fieldInfo[] [FIELD_F32x4] = { 16, 4, 16, 4 }, [FIELD_MAT2] = { 16, 4, 8, 4 }, [FIELD_MAT3] = { 64, 4, 16, 9 }, - [FIELD_MAT4] = { 64, 4, 16, 16 } + [FIELD_MAT4] = { 64, 4, 16, 16 }, + [FIELD_INDEX16] = { 2, 2, 2, 1 }, + [FIELD_INDEX32] = { 4, 4, 4, 1 } }; static uint32_t luax_checkfieldtype(lua_State* L, int index, uint32_t* nameHash) { @@ -296,6 +300,10 @@ static uint32_t luax_checkfieldtype(lua_State* L, int index, uint32_t* nameHash) return FIELD_UN8x4; } + if (length == 6 && !memcmp(string, "index", length)) { + return FIELD_INDEX32; + } + for (int i = 0; lovrFieldType[i].length; i++) { if (length == lovrFieldType[i].length && !memcmp(string, lovrFieldType[i].string, length)) { return i; diff --git a/src/api/l_graphics_buffer.c b/src/api/l_graphics_buffer.c index cd34b8c7..ef67b148 100644 --- a/src/api/l_graphics_buffer.c +++ b/src/api/l_graphics_buffer.c @@ -47,7 +47,9 @@ static const uint32_t fieldComponents[] = { [FIELD_F32x4] = 4, [FIELD_MAT2] = 4, [FIELD_MAT3] = 9, - [FIELD_MAT4] = 16 + [FIELD_MAT4] = 16, + [FIELD_INDEX16] = 1, + [FIELD_INDEX32] = 1 }; typedef union { @@ -131,6 +133,8 @@ void luax_readbufferfield(lua_State* L, int index, int type, void* data) { case FIELD_MAT2: p.f32[i] = (float) x; break; case FIELD_MAT3: p.f32[i] = (float) x; break; case FIELD_MAT4: p.f32[i] = (float) x; break; + case FIELD_INDEX16: p.u16[i] = (uint16_t) x - 1; break; + case FIELD_INDEX32: p.u32[i] = (uint32_t) x - 1; break; default: lovrUnreachable(); } } diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index b7e43bfe..d389ee6d 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -2342,7 +2342,7 @@ Model* lovrModelCreate(ModelInfo* info) { .length = data->indexCount, .stride = indexSize, .fieldCount = 1, - .fields[0] = { 0, 0, data->indexType == U32 ? FIELD_U32 : FIELD_I32, 0 } + .fields[0] = { 0, 0, data->indexType == U32 ? FIELD_INDEX32 : FIELD_INDEX16, 0 } }, (void**) &indices); } @@ -3435,6 +3435,7 @@ static void flushPipeline(Pass* pass, Draw* draw, Shader* shader) { bool found = false; for (uint32_t j = 0; j < draw->vertex.buffer->info.fieldCount; j++) { BufferField field = draw->vertex.buffer->info.fields[j]; + lovrCheck(field.type < FIELD_MAT2, "Currently, matrix and index types can not be used in vertex buffers"); if (field.hash ? (field.hash == attribute->hash) : (field.location == attribute->location)) { pipeline->info.vertex.attributes[i] = (gpu_attribute) { .buffer = 0, diff --git a/src/modules/graphics/graphics.h b/src/modules/graphics/graphics.h index fa807624..964f5a1d 100644 --- a/src/modules/graphics/graphics.h +++ b/src/modules/graphics/graphics.h @@ -138,7 +138,9 @@ typedef enum { FIELD_F32x4, FIELD_MAT2, FIELD_MAT3, - FIELD_MAT4 + FIELD_MAT4, + FIELD_INDEX16, + FIELD_INDEX32 } FieldType; typedef struct {