[ruamoko] Switch to 64-bit handles for scene objects

The scene id is in the lower 32-bits for all objects (upper 32-bits are
0 for actual scene objects) and entity/transform ids are in the upper
32-bits. Saves having to pass around a second parameter in progs code.
This commit is contained in:
Bill Currie 2022-04-26 17:40:06 +09:00
parent 709a0a338d
commit 4ed9fc6820

View file

@ -76,9 +76,13 @@ rua_scene_free (rua_scene_resources_t *res, rua_scene_t *scene)
}
static rua_scene_t * __attribute__((pure))
rua__scene_get (rua_scene_resources_t *res, int id, const char *name)
rua__scene_get (rua_scene_resources_t *res, pr_ulong_t id, const char *name)
{
rua_scene_t *scene = PR_RESGET (res->scene_map, id);
rua_scene_t *scene = 0;
if (id <= 0xffffffffu) {
scene = PR_RESGET (res->scene_map, (pr_int_t) id);
}
// scene->prev will be null if the handle is unallocated
if (!scene || !scene->prev) {
@ -89,29 +93,42 @@ rua__scene_get (rua_scene_resources_t *res, int id, const char *name)
#define rua_scene_get(res, id) rua__scene_get(res, id, __FUNCTION__)
static entity_t * __attribute__((pure))
rua__entity_get (progs_t *pr, rua_scene_t *scene, int id, const char *name)
rua__entity_get (rua_scene_resources_t *res, pr_ulong_t id, const char *name)
{
entity_t *ent = Scene_GetEntity (scene->scene, id);
pr_ulong_t scene_id = id & 0xffffffff;
entity_t *ent = 0;
rua_scene_t *scene = rua__scene_get (res, scene_id, name);
if (scene) {
pr_int_t entity_id = id >> 32;
ent = Scene_GetEntity (scene->scene, entity_id);
}
if (!ent) {
PR_RunError (pr, "invalid entity passed to %s", name + 3);
PR_RunError (res->pr, "invalid entity passed to %s", name + 3);
}
return ent;
}
#define rua_entity_get(pr, scene, id) rua__entity_get(pr, scene, id, __FUNCTION__)
#define rua_entity_get(res, id) rua__entity_get(res, id, __FUNCTION__)
static transform_t * __attribute__((pure))
rua__transform_get (progs_t *pr, rua_scene_t *scene, int id, const char *name)
rua__transform_get (rua_scene_resources_t *res, pr_ulong_t id, const char *name)
{
transform_t *transform = Scene_GetTransform (scene->scene, id);
pr_ulong_t scene_id = id & 0xffffffff;
transform_t *transform = 0;
rua_scene_t *scene = rua_scene_get (res, scene_id);
if (scene) {
pr_int_t transform_id = id >> 32;
transform = Scene_GetTransform (scene->scene, transform_id);
}
if (!transform) {
PR_RunError (pr, "invalid transform passed to %s", name + 3);
PR_RunError (res->pr, "invalid transform passed to %s", name + 3);
}
return transform;
}
#define rua_transform_get(pr, scene, id) \
rua__transform_get(pr, scene, id, __FUNCTION__)
#define rua_transform_get(res, id) rua__transform_get(res, id, __FUNCTION__)
static int __attribute__((pure))
rua_scene_index (rua_scene_resources_t *res, rua_scene_t *scene)
@ -119,6 +136,9 @@ rua_scene_index (rua_scene_resources_t *res, rua_scene_t *scene)
return PR_RESINDEX (res->scene_map, scene);
}
#define MAKE_ID(id, sc_id) ((((pr_ulong_t) (id)) << 32) \
| ((sc_id) & 0xffffffff))
static void
bi_Scene_NewScene (progs_t *pr, void *_res)
{
@ -135,7 +155,9 @@ bi_Scene_NewScene (progs_t *pr, void *_res)
scene->prev = &res->scenes;
res->scenes = scene;
R_INT (pr) = rua_scene_index (res, scene);
// scene id in lower 32-bits for all handles
// zero upper 32-bits zero means scene, otherwise transform or entity
R_ULONG (pr) = MAKE_ID (0, rua_scene_index (res, scene));
}
static void
@ -149,7 +171,7 @@ static void
bi_Scene_DeleteScene (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
rua_scene_t *scene = rua_scene_get (res, P_ULONG (pr, 0));
rua_delete_scene (res, scene);
}
@ -158,17 +180,21 @@ static void
bi_Scene_CreateEntity (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
pr_ulong_t scene_id = P_ULONG (pr, 0);
rua_scene_t *scene = rua_scene_get (res, scene_id);
entity_t *ent = Scene_CreateEntity (scene->scene);
R_INT (pr) = ent->id;
R_ULONG (pr) = MAKE_ID (ent->id, scene_id);
}
static void
bi_Scene_DestroyEntity (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
entity_t *ent = rua_entity_get (pr, scene, P_INT (pr, 1));
pr_ulong_t id = P_ULONG (pr, 0);
entity_t *ent = rua_entity_get (res, id);
pr_ulong_t scene_id = id & 0xffffffff;
// bad scene caught above
rua_scene_t *scene = rua_scene_get (res, scene_id);
Scene_DestroyEntity (scene->scene, ent);
}
@ -176,18 +202,18 @@ static void
bi_Entity_GetTransform (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
entity_t *ent = rua_entity_get (pr, scene, P_INT (pr, 1));
pr_ulong_t ent_id = P_ULONG (pr, 0);
entity_t *ent = rua_entity_get (res, ent_id);
R_INT (pr) = ent->transform->id;
// ent_id contains scene id
R_ULONG (pr) = MAKE_ID (ent->transform->id, ent_id);
}
static void
bi_Transform_ChildCount (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_UINT (pr) = Transform_ChildCount (transform);
}
@ -196,8 +222,7 @@ static void
bi_Transform_GetChild (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
transform_t *child = Transform_GetChild (transform, P_UINT (pr, 2));
R_UINT (pr) = child ? child->id : 0;
@ -207,9 +232,8 @@ static void
bi_Transform_SetParent (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *parent = rua_transform_get (pr, scene, P_INT (pr, 2));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
transform_t *parent = rua_transform_get (res, P_ULONG (pr, 1));
Transform_SetParent (transform, parent);
}
@ -218,19 +242,19 @@ static void
bi_Transform_GetParent (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
pr_ulong_t transform_id = P_ULONG (pr, 0);
transform_t *transform = rua_transform_get (res, transform_id);
transform_t *parent = Transform_GetParent (transform);
R_INT (pr) = parent ? parent->id : 0;
// transform_id contains scene id
R_ULONG (pr) = parent ? MAKE_ID (parent->id, transform_id) : 0;
}
static void
bi_Transform_SetTag (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
pr_uint_t tag = P_UINT (pr, 2);
Transform_SetTag (transform, tag);
}
@ -239,8 +263,7 @@ static void
bi_Transform_GetTag (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_UINT (pr) = Transform_GetTag (transform);
}
@ -249,8 +272,7 @@ static void
bi_Transform_GetLocalMatrix (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_GetLocalMatrix (transform, &R_PACKED (pr, pr_vec4_t));
}
@ -258,8 +280,7 @@ static void
bi_Transform_GetLocalInverse (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_GetLocalInverse (transform, &R_PACKED (pr, pr_vec4_t));
}
@ -267,8 +288,7 @@ static void
bi_Transform_GetWorldMatrix (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_GetWorldMatrix (transform, &R_PACKED (pr, pr_vec4_t));
}
@ -276,8 +296,7 @@ static void
bi_Transform_GetWorldInverse (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_GetWorldInverse (transform, &R_PACKED (pr, pr_vec4_t));
}
@ -285,17 +304,15 @@ static void
bi_Transform_SetLocalPosition (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
Transform_SetLocalPosition (transform, P_PACKED (pr, pr_vec4_t, 2));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_SetLocalPosition (transform, P_PACKED (pr, pr_vec4_t, 1));
}
static void
bi_Transform_GetLocalPosition (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_GetLocalPosition (transform);
}
@ -303,17 +320,15 @@ static void
bi_Transform_SetLocalRotation (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
Transform_SetLocalRotation (transform, P_PACKED (pr, pr_vec4_t, 2));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_SetLocalRotation (transform, P_PACKED (pr, pr_vec4_t, 1));
}
static void
bi_Transform_GetLocalRotation (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_GetLocalRotation (transform);
}
@ -321,17 +336,15 @@ static void
bi_Transform_SetLocalScale (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
Transform_SetLocalScale (transform, P_PACKED (pr, pr_vec4_t, 2));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_SetLocalScale (transform, P_PACKED (pr, pr_vec4_t, 1));
}
static void
bi_Transform_GetLocalScale (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_GetLocalScale (transform);
}
@ -339,17 +352,15 @@ static void
bi_Transform_SetWorldPosition (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
Transform_SetWorldPosition (transform, P_PACKED (pr, pr_vec4_t, 2));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_SetWorldPosition (transform, P_PACKED (pr, pr_vec4_t, 1));
}
static void
bi_Transform_GetWorldPosition (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_GetWorldPosition (transform);
}
@ -357,17 +368,15 @@ static void
bi_Transform_SetWorldRotation (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
Transform_SetWorldRotation (transform, P_PACKED (pr, pr_vec4_t, 2));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_SetWorldRotation (transform, P_PACKED (pr, pr_vec4_t, 1));
}
static void
bi_Transform_GetWorldRotation (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_GetWorldRotation (transform);
}
@ -375,8 +384,7 @@ static void
bi_Transform_GetWorldScale (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_GetWorldScale (transform);
}
@ -384,18 +392,16 @@ static void
bi_Transform_SetLocalTransform (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
Transform_SetLocalTransform (transform, P_PACKED (pr, pr_vec4_t, 2),
P_PACKED (pr, pr_vec4_t, 3), P_PACKED (pr, pr_vec4_t, 4));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
Transform_SetLocalTransform (transform, P_PACKED (pr, pr_vec4_t, 1),
P_PACKED (pr, pr_vec4_t, 2), P_PACKED (pr, pr_vec4_t, 3));
}
static void
bi_Transform_Forward (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_Forward (transform);
}
@ -403,8 +409,7 @@ static void
bi_Transform_Right (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_Right (transform);
}
@ -412,8 +417,7 @@ static void
bi_Transform_Up (progs_t *pr, void *_res)
{
rua_scene_resources_t *res = _res;
rua_scene_t *scene = rua_scene_get (res, P_INT (pr, 0));
transform_t *transform = rua_transform_get (pr, scene, P_INT (pr, 1));
transform_t *transform = rua_transform_get (res, P_ULONG (pr, 0));
R_PACKED (pr, pr_vec4_t) = Transform_Up (transform);
}
@ -421,44 +425,43 @@ bi_Transform_Up (progs_t *pr, void *_res)
#define P(a, s) { .size = (s), .alignment = BITOP_LOG2 (a), }
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
static builtin_t builtins[] = {
bi(Scene_NewScene, 0),
bi(Scene_DeleteScene, 1, p(ptr)),
bi(Scene_CreateEntity, 1, p(ptr)),
bi(Scene_DestroyEntity, 2, p(ptr), p(ptr)),
bi(Scene_NewScene, 0),
bi(Scene_DeleteScene, 1, p(ulong)),
bi(Scene_CreateEntity, 1, p(ulong)),
bi(Scene_DestroyEntity, 1, p(ulong)),
bi(Entity_GetTransform, 2, p(ptr), p(ptr)),
bi(Entity_GetTransform, 1, p(ulong)),
bi(Transform_ChildCount, 2, p(ptr), p(ptr)),
bi(Transform_GetChild, 3, p(ptr), p(ptr), p(int)),
bi(Transform_SetParent, 3, p(ptr), p(ptr), p(ptr)),
bi(Transform_GetParent, 2, p(ptr), p(ptr)),
bi(Transform_ChildCount, 1, p(ulong)),
bi(Transform_GetChild, 2, p(ulong), p(int)),
bi(Transform_SetParent, 2, p(ulong), p(ulong)),
bi(Transform_GetParent, 1, p(ulong)),
bi(Transform_SetTag, 3, p(ptr), p(ptr), p(uint)),
bi(Transform_GetTag, 2, p(ptr), p(ptr)),
bi(Transform_SetTag, 2, p(ulong), p(uint)),
bi(Transform_GetTag, 1, p(ulong)),
bi(Transform_GetLocalMatrix, 2, p(ptr), p(ptr)),
bi(Transform_GetLocalInverse, 2, p(ptr), p(ptr)),
bi(Transform_GetWorldMatrix, 2, p(ptr), p(ptr)),
bi(Transform_GetWorldInverse, 2, p(ptr), p(ptr)),
bi(Transform_GetLocalMatrix, 1, p(ulong)),
bi(Transform_GetLocalInverse, 1, p(ulong)),
bi(Transform_GetWorldMatrix, 1, p(ulong)),
bi(Transform_GetWorldInverse, 1, p(ulong)),
bi(Transform_SetLocalPosition, 3, p(ptr), p(ptr), p(vec4)),
bi(Transform_GetLocalPosition, 2, p(ptr), p(ptr)),
bi(Transform_SetLocalRotation, 3, p(ptr), p(ptr), p(vec4)),
bi(Transform_GetLocalRotation, 2, p(ptr), p(ptr)),
bi(Transform_SetLocalScale, 3, p(ptr), p(ptr), p(vec4)),
bi(Transform_GetLocalScale, 2, p(ptr), p(ptr)),
bi(Transform_SetLocalPosition, 2, p(ulong), p(vec4)),
bi(Transform_GetLocalPosition, 1, p(ulong)),
bi(Transform_SetLocalRotation, 2, p(ulong), p(vec4)),
bi(Transform_GetLocalRotation, 1, p(ulong)),
bi(Transform_SetLocalScale, 2, p(ulong), p(vec4)),
bi(Transform_GetLocalScale, 1, p(ulong)),
bi(Transform_SetWorldPosition, 3, p(ptr), p(ptr), p(vec4)),
bi(Transform_GetWorldPosition, 2, p(ptr), p(ptr)),
bi(Transform_SetWorldRotation, 3, p(ptr), p(ptr), p(vec4)),
bi(Transform_GetWorldRotation, 2, p(ptr), p(ptr)),
bi(Transform_GetWorldScale, 2, p(ptr), p(ptr)),
bi(Transform_SetWorldPosition, 2, p(ulong), p(vec4)),
bi(Transform_GetWorldPosition, 1, p(ulong)),
bi(Transform_SetWorldRotation, 2, p(ulong), p(vec4)),
bi(Transform_GetWorldRotation, 1, p(ulong)),
bi(Transform_GetWorldScale, 1, p(ulong)),
bi(Transform_SetLocalTransform, 5, p(ptr), p(ptr),
p(vec4), p(vec4), p(vec4)),
bi(Transform_Forward, 2, p(ptr), p(ptr)),
bi(Transform_Right, 2, p(ptr), p(ptr)),
bi(Transform_Up, 2, p(ptr), p(ptr)),
bi(Transform_SetLocalTransform, 4, p(ulong), p(vec4), p(vec4), p(vec4)),
bi(Transform_Forward, 1, p(ulong)),
bi(Transform_Right, 1, p(ulong)),
bi(Transform_Up, 1, p(ulong)),
{0}
};