Skip to content

Commit

Permalink
Make ModelData lookups opaque;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed May 14, 2024
1 parent 3b1fe26 commit cd290f4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/api/l_data_modelData.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ uint32_t luax_checkanimationindex(lua_State* L, int index, ModelData* model) {
size_t length;
const char* name = lua_tolstring(L, index, &length);
uint64_t hash = hash64(name, length);
uint64_t entry = map_get(&model->animationMap, hash);
uint64_t entry = map_get(model->animationMap, hash);
lovrCheck(entry != MAP_NIL, "Model has no animation named '%s'", name);
return (uint32_t) entry;
}
Expand All @@ -33,7 +33,7 @@ uint32_t luax_checkmaterialindex(lua_State* L, int index, ModelData* model) {
size_t length;
const char* name = lua_tolstring(L, index, &length);
uint64_t hash = hash64(name, length);
uint64_t entry = map_get(&model->materialMap, hash);
uint64_t entry = map_get(model->materialMap, hash);
lovrCheck(entry != MAP_NIL, "Model has no material named '%s'", name);
return (uint32_t) entry;
}
Expand All @@ -52,7 +52,7 @@ uint32_t luax_checknodeindex(lua_State* L, int index, ModelData* model) {
size_t length;
const char* name = lua_tolstring(L, index, &length);
uint64_t hash = hash64(name, length);
uint64_t entry = map_get(&model->nodeMap, hash);
uint64_t entry = map_get(model->nodeMap, hash);
lovrCheck(entry != MAP_NIL, "Model has no node named '%s'", name);
return (uint32_t) entry;
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/l_graphics_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ uint32_t luax_checkblendshape(lua_State* L, int index, Model* model) {
size_t length;
const char* name = lua_tolstring(L, index, &length);
ModelData* data = lovrModelGetInfo(model)->data;
uint64_t blendShapeIndex = map_get(&data->blendShapeMap, hash64(name, length));
uint64_t blendShapeIndex = map_get(data->blendShapeMap, hash64(name, length));
lovrCheck(blendShapeIndex != MAP_NIL, "ModelData has no blend shape named '%s'", name);
return (uint32_t) blendShapeIndex;
}
Expand Down
1 change: 1 addition & 0 deletions src/api/l_headset.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "data/modelData.h"
#include "graphics/graphics.h"
#include "core/maf.h"
#include "util.h"
#include <stdlib.h>

StringEntry lovrHeadsetDriver[] = {
Expand Down
31 changes: 20 additions & 11 deletions src/modules/data/modelData.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "data/blob.h"
#include "data/image.h"
#include "core/maf.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -50,10 +51,10 @@ void lovrModelDataDestroy(void* ref) {
for (uint32_t i = 0; i < model->imageCount; i++) {
lovrRelease(model->images[i], lovrImageDestroy);
}
map_free(&model->blendShapeMap);
map_free(&model->animationMap);
map_free(&model->materialMap);
map_free(&model->nodeMap);
map_free(model->blendShapeMap);
map_free(model->animationMap);
map_free(model->materialMap);
map_free(model->nodeMap);
lovrFree(model->vertices);
lovrFree(model->indices);
lovrFree(model->metadata);
Expand All @@ -64,7 +65,7 @@ void lovrModelDataDestroy(void* ref) {
// Batches allocations for all the ModelData arrays
void lovrModelDataAllocate(ModelData* model) {
size_t totalSize = 0;
size_t sizes[15];
size_t sizes[19];
size_t alignment = 8;
totalSize += sizes[0] = ALIGN(model->blobCount * sizeof(Blob*), alignment);
totalSize += sizes[1] = ALIGN(model->bufferCount * sizeof(ModelBuffer), alignment);
Expand All @@ -80,7 +81,11 @@ void lovrModelDataAllocate(ModelData* model) {
totalSize += sizes[11] = ALIGN(model->blendDataCount * sizeof(ModelBlendData), alignment);
totalSize += sizes[12] = ALIGN(model->childCount * sizeof(uint32_t), alignment);
totalSize += sizes[13] = ALIGN(model->jointCount * sizeof(uint32_t), alignment);
totalSize += sizes[14] = model->charCount * sizeof(char);
totalSize += sizes[14] = ALIGN(model->charCount * sizeof(char), alignment);
totalSize += sizes[15] = ALIGN(sizeof(map_t), alignment);
totalSize += sizes[16] = ALIGN(sizeof(map_t), alignment);
totalSize += sizes[17] = ALIGN(sizeof(map_t), alignment);
totalSize += sizes[18] = ALIGN(sizeof(map_t), alignment);

size_t offset = 0;
char* p = model->data = lovrCalloc(totalSize);
Expand All @@ -99,11 +104,15 @@ void lovrModelDataAllocate(ModelData* model) {
model->children = (uint32_t*) (p + offset), offset += sizes[12];
model->joints = (uint32_t*) (p + offset), offset += sizes[13];
model->chars = (char*) (p + offset), offset += sizes[14];

map_init(&model->blendShapeMap, model->blendShapeCount);
map_init(&model->animationMap, model->animationCount);
map_init(&model->materialMap, model->materialCount);
map_init(&model->nodeMap, model->nodeCount);
model->blendShapeMap = (map_t*) (p + offset), offset += sizes[15];
model->animationMap = (map_t*) (p + offset), offset += sizes[16];
model->materialMap = (map_t*) (p + offset), offset += sizes[17];
model->nodeMap = (map_t*) (p + offset), offset += sizes[18];

map_init(model->blendShapeMap, model->blendShapeCount);
map_init(model->animationMap, model->animationCount);
map_init(model->materialMap, model->materialCount);
map_init(model->nodeMap, model->nodeCount);
}

void lovrModelDataFinalize(ModelData* model) {
Expand Down
9 changes: 4 additions & 5 deletions src/modules/data/modelData.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "util.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
Expand Down Expand Up @@ -234,10 +233,10 @@ typedef struct ModelData {

// Lookups

map_t blendShapeMap;
map_t animationMap;
map_t materialMap;
map_t nodeMap;
void* blendShapeMap;
void* animationMap;
void* materialMap;
void* nodeMap;
} ModelData;

typedef void* ModelDataIO(const char* filename, size_t* bytesRead);
Expand Down
11 changes: 6 additions & 5 deletions src/modules/data/modelData_gltf.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "data/modelData.h"
#include "data/blob.h"
#include "data/image.h"
#include "util.h"
#include "lib/jsmn/jsmn.h"
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -713,7 +714,7 @@ ModelData* lovrModelDataInitGltf(ModelData* model, Blob* source, ModelDataIO* io
token += NOM_VALUE(json, token);
} else if (STR_EQ(key, "name")) {
gltfString name = NOM_STR(json, token);
map_set(&model->animationMap, hash64(name.data, name.length), animation - model->animations);
map_set(model->animationMap, hash64(name.data, name.length), animation - model->animations);
memcpy(model->chars, name.data, name.length);
animation->name = model->chars;
model->chars += name.length + 1;
Expand Down Expand Up @@ -800,7 +801,7 @@ ModelData* lovrModelDataInitGltf(ModelData* model, Blob* source, ModelDataIO* io
material->alphaCutoff = NOM_FLOAT(json, token);
} else if (STR_EQ(key, "name")) {
gltfString name = NOM_STR(json, token);
map_set(&model->materialMap, hash64(name.data, name.length), material - model->materials);
map_set(model->materialMap, hash64(name.data, name.length), material - model->materials);
memcpy(model->chars, name.data, name.length);
material->name = model->chars;
model->chars += name.length + 1;
Expand Down Expand Up @@ -889,8 +890,8 @@ ModelData* lovrModelDataInitGltf(ModelData* model, Blob* source, ModelDataIO* io
for (int k3 = (token++)->size, index = mesh->blendShapeIndex; k3 > 0; k3--, index++) {
gltfString name = NOM_STR(json, token);
uint64_t hash = hash64(name.data, name.length);
if (map_get(&model->blendShapeMap, hash) == MAP_NIL) {
map_set(&model->blendShapeMap, hash, index);
if (map_get(model->blendShapeMap, hash) == MAP_NIL) {
map_set(model->blendShapeMap, hash, index);
}
memcpy(model->chars, name.data, name.length);
model->chars += name.length + 1;
Expand Down Expand Up @@ -968,7 +969,7 @@ ModelData* lovrModelDataInitGltf(ModelData* model, Blob* source, ModelDataIO* io
scale[2] = NOM_FLOAT(json, token);
} else if (STR_EQ(key, "name")) {
gltfString name = NOM_STR(json, token);
map_set(&model->nodeMap, hash64(name.data, name.length), node - model->nodes);
map_set(model->nodeMap, hash64(name.data, name.length), node - model->nodes);
memcpy(model->chars, name.data, name.length);
node->name = model->chars;
model->chars += name.length + 1;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/data/modelData_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source, ModelDataIO* io)

memcpy(model->images, images.data, model->imageCount * sizeof(Image*));
memcpy(model->materials, materials.data, model->materialCount * sizeof(ModelMaterial));
memcpy(model->materialMap.hashes, materialMap.hashes, materialMap.size * sizeof(uint64_t));
memcpy(model->materialMap.values, materialMap.values, materialMap.size * sizeof(uint64_t));
memcpy(((map_t*) model->materialMap)->hashes, materialMap.hashes, materialMap.size * sizeof(uint64_t));
memcpy(((map_t*) model->materialMap)->values, materialMap.values, materialMap.size * sizeof(uint64_t));

float min[4] = { FLT_MAX };
float max[4] = { FLT_MIN };
Expand Down

0 comments on commit cd290f4

Please sign in to comment.