Add index16/index32 FieldTypes;

Similar to u16/u32 but are 1-indexed when written using tables.
This commit is contained in:
bjorn 2022-07-10 12:18:31 -07:00
parent 72dd7ee0f2
commit f7d7281e70
4 changed files with 19 additions and 4 deletions

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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,

View File

@ -138,7 +138,9 @@ typedef enum {
FIELD_F32x4,
FIELD_MAT2,
FIELD_MAT3,
FIELD_MAT4
FIELD_MAT4,
FIELD_INDEX16,
FIELD_INDEX32
} FieldType;
typedef struct {