2021-03-09 02:39:41 +00:00
|
|
|
/*
|
|
|
|
hierarchy.c
|
|
|
|
|
2022-12-13 04:36:51 +00:00
|
|
|
ECS hierarchy handling
|
2021-03-09 02:39:41 +00:00
|
|
|
|
|
|
|
Copyright (C) 2021 Bill Currke
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2022-10-26 06:22:09 +00:00
|
|
|
#include "QF/sys.h"
|
2021-03-09 02:39:41 +00:00
|
|
|
|
2022-12-13 04:36:51 +00:00
|
|
|
#include "QF/ecs.h"
|
2022-02-14 11:01:36 +00:00
|
|
|
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
static component_t ent_component = { .size = sizeof (uint32_t) };
|
2022-10-09 17:00:33 +00:00
|
|
|
static component_t childCount_component = { .size = sizeof (uint32_t) };
|
|
|
|
static component_t childIndex_component = { .size = sizeof (uint32_t) };
|
|
|
|
static component_t parentIndex_component = { .size = sizeof (uint32_t) };
|
|
|
|
|
2021-03-09 02:39:41 +00:00
|
|
|
static void
|
|
|
|
hierarchy_UpdateTransformIndices (hierarchy_t *hierarchy, uint32_t start,
|
|
|
|
int offset)
|
|
|
|
{
|
2022-10-26 06:22:09 +00:00
|
|
|
ecs_registry_t *reg = hierarchy->reg;
|
2022-12-11 15:20:20 +00:00
|
|
|
uint32_t href = hierarchy->href_comp;
|
2022-10-09 17:00:33 +00:00
|
|
|
for (size_t i = start; i < hierarchy->num_objects; i++) {
|
2022-10-27 06:35:29 +00:00
|
|
|
if (ECS_EntValid (hierarchy->ent[i], reg)) {
|
|
|
|
hierref_t *ref = Ent_GetComponent (hierarchy->ent[i], href, reg);
|
|
|
|
ref->index += offset;
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 12:29:26 +00:00
|
|
|
static void
|
|
|
|
hierarchy_InvalidateReferences (hierarchy_t *hierarchy, uint32_t start,
|
|
|
|
uint32_t count)
|
|
|
|
{
|
|
|
|
ecs_registry_t *reg = hierarchy->reg;
|
|
|
|
uint32_t href = hierarchy->href_comp;
|
|
|
|
for (size_t i = start; count-- > 0; i++) {
|
|
|
|
if (ECS_EntValid (hierarchy->ent[i], reg)) {
|
|
|
|
hierref_t *ref = Ent_GetComponent (hierarchy->ent[i], href, reg);
|
|
|
|
ref->hierarchy = 0;
|
|
|
|
ref->index = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 02:39:41 +00:00
|
|
|
static void
|
|
|
|
hierarchy_UpdateChildIndices (hierarchy_t *hierarchy, uint32_t start,
|
|
|
|
int offset)
|
|
|
|
{
|
2022-10-09 17:00:33 +00:00
|
|
|
for (size_t i = start; i < hierarchy->num_objects; i++) {
|
|
|
|
hierarchy->childIndex[i] += offset;
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hierarchy_UpdateParentIndices (hierarchy_t *hierarchy, uint32_t start,
|
|
|
|
int offset)
|
|
|
|
{
|
2022-10-09 17:00:33 +00:00
|
|
|
for (size_t i = start; i < hierarchy->num_objects; i++) {
|
|
|
|
hierarchy->parentIndex[i] += offset;
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-10-09 17:00:33 +00:00
|
|
|
Hierarchy_Reserve (hierarchy_t *hierarchy, uint32_t count)
|
2021-03-09 02:39:41 +00:00
|
|
|
{
|
2022-10-09 17:00:33 +00:00
|
|
|
if (hierarchy->num_objects + count > hierarchy->max_objects) {
|
|
|
|
uint32_t new_max = hierarchy->num_objects + count;
|
|
|
|
new_max += 15;
|
|
|
|
new_max &= ~15;
|
|
|
|
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
Component_ResizeArray (&ent_component,
|
|
|
|
(void **) &hierarchy->ent, new_max);
|
2022-10-09 17:00:33 +00:00
|
|
|
Component_ResizeArray (&childCount_component,
|
|
|
|
(void **) &hierarchy->childCount, new_max);
|
|
|
|
Component_ResizeArray (&childIndex_component,
|
|
|
|
(void **) &hierarchy->childIndex, new_max);
|
|
|
|
Component_ResizeArray (&parentIndex_component,
|
|
|
|
(void **) &hierarchy->parentIndex, new_max);
|
|
|
|
|
2022-11-05 12:26:47 +00:00
|
|
|
if (hierarchy->type) {
|
|
|
|
for (uint32_t i = 0; i < hierarchy->type->num_components; i++) {
|
|
|
|
Component_ResizeArray (&hierarchy->type->components[i],
|
|
|
|
&hierarchy->components[i], new_max);
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
2022-10-09 17:00:33 +00:00
|
|
|
hierarchy->max_objects = new_max;
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hierarchy_open (hierarchy_t *hierarchy, uint32_t index, uint32_t count)
|
|
|
|
{
|
2022-10-09 17:00:33 +00:00
|
|
|
Hierarchy_Reserve (hierarchy, count);
|
|
|
|
|
|
|
|
hierarchy->num_objects += count;
|
|
|
|
uint32_t dstIndex = index + count;
|
|
|
|
count = hierarchy->num_objects - index - count;
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
Component_MoveElements (&ent_component,
|
|
|
|
hierarchy->ent, dstIndex, index, count);
|
2022-10-09 17:00:33 +00:00
|
|
|
Component_MoveElements (&childCount_component,
|
|
|
|
hierarchy->childCount, dstIndex, index, count);
|
|
|
|
Component_MoveElements (&childIndex_component,
|
|
|
|
hierarchy->childIndex, dstIndex, index, count);
|
|
|
|
Component_MoveElements (&parentIndex_component,
|
|
|
|
hierarchy->parentIndex, dstIndex, index, count);
|
2022-11-05 12:26:47 +00:00
|
|
|
if (hierarchy->type) {
|
|
|
|
for (uint32_t i = 0; i < hierarchy->type->num_components; i++) {
|
|
|
|
Component_MoveElements (&hierarchy->type->components[i],
|
|
|
|
hierarchy->components[i],
|
|
|
|
dstIndex, index, count);
|
|
|
|
}
|
2022-10-09 17:00:33 +00:00
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hierarchy_close (hierarchy_t *hierarchy, uint32_t index, uint32_t count)
|
|
|
|
{
|
2022-10-09 17:00:33 +00:00
|
|
|
if (!count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hierarchy->num_objects -= count;
|
|
|
|
uint32_t srcIndex = index + count;
|
|
|
|
count = hierarchy->num_objects - index;
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
Component_MoveElements (&ent_component,
|
|
|
|
hierarchy->ent, index, srcIndex, count);
|
2022-10-09 17:00:33 +00:00
|
|
|
Component_MoveElements (&childCount_component,
|
|
|
|
hierarchy->childCount, index, srcIndex, count);
|
|
|
|
Component_MoveElements (&childIndex_component,
|
|
|
|
hierarchy->childIndex, index, srcIndex, count);
|
|
|
|
Component_MoveElements (&parentIndex_component,
|
|
|
|
hierarchy->parentIndex, index, srcIndex, count);
|
2022-11-05 12:26:47 +00:00
|
|
|
if (hierarchy->type) {
|
|
|
|
for (uint32_t i = 0; i < hierarchy->type->num_components; i++) {
|
|
|
|
Component_MoveElements (&hierarchy->type->components[i],
|
|
|
|
hierarchy->components[i],
|
|
|
|
index, srcIndex, count);
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hierarchy_move (hierarchy_t *dst, const hierarchy_t *src,
|
|
|
|
uint32_t dstIndex, uint32_t srcIndex, uint32_t count)
|
|
|
|
{
|
2022-10-26 06:22:09 +00:00
|
|
|
ecs_registry_t *reg = dst->reg;
|
2022-12-11 15:20:20 +00:00
|
|
|
uint32_t href = dst->href_comp;
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
Component_CopyElements (&ent_component,
|
|
|
|
dst->ent, dstIndex,
|
|
|
|
src->ent, srcIndex, count);
|
2022-10-09 17:00:33 +00:00
|
|
|
// Actually move (as in C++ move semantics) source hierarchy object
|
|
|
|
// references so that their indices do not get updated when the objects
|
2023-03-05 12:29:26 +00:00
|
|
|
// are removed from the source hierarchy
|
2022-10-27 06:35:29 +00:00
|
|
|
memset (&src->ent[srcIndex], nullent, count * sizeof(dst->ent[0]));
|
2021-03-09 02:39:41 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2022-11-06 08:36:49 +00:00
|
|
|
if (dst->ent[dstIndex + i] != nullent) {
|
|
|
|
uint32_t ent = dst->ent[dstIndex + i];
|
|
|
|
hierref_t *ref = Ent_GetComponent (ent, href, reg);
|
|
|
|
ref->hierarchy = dst;
|
|
|
|
ref->index = dstIndex + i;
|
|
|
|
}
|
2022-10-09 17:00:33 +00:00
|
|
|
}
|
2022-11-05 12:26:47 +00:00
|
|
|
if (dst->type) {
|
|
|
|
for (uint32_t i = 0; i < dst->type->num_components; i++) {
|
|
|
|
Component_CopyElements (&dst->type->components[i],
|
|
|
|
dst->components[i], dstIndex,
|
|
|
|
src->components[i], srcIndex, count);
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hierarchy_init (hierarchy_t *dst, uint32_t index,
|
|
|
|
uint32_t parentIndex, uint32_t childIndex, uint32_t count)
|
|
|
|
{
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
memset (&dst->ent[index], nullent, count * sizeof(uint32_t));
|
2021-03-09 02:39:41 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2022-10-09 17:00:33 +00:00
|
|
|
dst->parentIndex[index + i] = parentIndex;
|
|
|
|
dst->childCount[index + i] = 0;
|
|
|
|
dst->childIndex[index + i] = childIndex;
|
|
|
|
}
|
2022-11-05 12:26:47 +00:00
|
|
|
if (dst->type) {
|
|
|
|
for (uint32_t i = 0; i < dst->type->num_components; i++) {
|
|
|
|
Component_CreateElements (&dst->type->components[i],
|
|
|
|
dst->components[i], index, count);
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
hierarchy_insert (hierarchy_t *dst, const hierarchy_t *src,
|
2022-11-06 08:36:49 +00:00
|
|
|
uint32_t dstParent, uint32_t *srcRoot, uint32_t count)
|
2021-03-09 02:39:41 +00:00
|
|
|
{
|
2022-10-09 17:00:33 +00:00
|
|
|
uint32_t insertIndex; // where the objects will be inserted
|
|
|
|
uint32_t childIndex; // where the objects' children will inserted
|
|
|
|
|
|
|
|
// The newly added objects are always last children of the parent
|
|
|
|
// object
|
|
|
|
insertIndex = dst->childIndex[dstParent] + dst->childCount[dstParent];
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
// By design, all of an object's children are in one contiguous block,
|
2022-10-09 17:00:33 +00:00
|
|
|
// and the blocks of children for each object are ordered by their
|
|
|
|
// parents. Thus the child index of each object increases monotonically
|
2021-03-09 02:39:41 +00:00
|
|
|
// for each child index in the array, regardless of the level of the owning
|
2022-10-09 17:00:33 +00:00
|
|
|
// object (higher levels always come before lower levels).
|
2021-03-09 02:39:41 +00:00
|
|
|
uint32_t neighbor = insertIndex - 1; // insertIndex never zero
|
2022-10-09 17:00:33 +00:00
|
|
|
childIndex = dst->childIndex[neighbor] + dst->childCount[neighbor];
|
2021-03-09 02:39:41 +00:00
|
|
|
|
2022-10-09 17:00:33 +00:00
|
|
|
// Any objects that come after the inserted objects need to have
|
2021-03-09 02:39:41 +00:00
|
|
|
// thier indices adjusted.
|
|
|
|
hierarchy_UpdateTransformIndices (dst, insertIndex, count);
|
2022-10-09 17:00:33 +00:00
|
|
|
// The parent object's child index is not affected, but the child
|
|
|
|
// indices of all objects immediately after the parent object are.
|
2021-03-09 02:39:41 +00:00
|
|
|
hierarchy_UpdateChildIndices (dst, dstParent + 1, count);
|
|
|
|
hierarchy_UpdateParentIndices (dst, childIndex, count);
|
|
|
|
|
2022-10-09 17:00:33 +00:00
|
|
|
// The beginning of the block of children for the new objects was
|
|
|
|
// computed from the pre-insert indices of the related objects, thus
|
|
|
|
// the index must be updated by the number of objects being inserted
|
2021-03-09 02:39:41 +00:00
|
|
|
// (it would have been updated thusly if the insert was done before
|
2022-10-09 17:00:33 +00:00
|
|
|
// updating the indices of the other objects).
|
2021-03-09 02:39:41 +00:00
|
|
|
childIndex += count;
|
|
|
|
|
|
|
|
hierarchy_open (dst, insertIndex, count);
|
2022-11-06 08:36:49 +00:00
|
|
|
if (dst == src && insertIndex <= *srcRoot) {
|
|
|
|
*srcRoot += count;
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
if (src) {
|
2022-11-06 08:36:49 +00:00
|
|
|
hierarchy_move (dst, src, insertIndex, *srcRoot, count);
|
2021-03-09 02:39:41 +00:00
|
|
|
} else {
|
|
|
|
hierarchy_init (dst, insertIndex, dstParent, childIndex, count);
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2022-10-09 17:00:33 +00:00
|
|
|
dst->parentIndex[insertIndex + i] = dstParent;
|
|
|
|
dst->childIndex[insertIndex + i] = childIndex;
|
|
|
|
dst->childCount[insertIndex + i] = 0;
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 17:00:33 +00:00
|
|
|
dst->childCount[dstParent] += count;
|
2021-03-09 02:39:41 +00:00
|
|
|
return insertIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hierarchy_insert_children (hierarchy_t *dst, const hierarchy_t *src,
|
2022-11-06 08:36:49 +00:00
|
|
|
uint32_t dstParent, uint32_t *srcRoot)
|
2021-03-09 02:39:41 +00:00
|
|
|
{
|
|
|
|
uint32_t insertIndex;
|
2022-11-06 08:36:49 +00:00
|
|
|
uint32_t childIndex = src->childIndex[*srcRoot];
|
|
|
|
uint32_t childCount = src->childCount[*srcRoot];
|
2021-03-09 02:39:41 +00:00
|
|
|
|
|
|
|
if (childCount) {
|
|
|
|
insertIndex = hierarchy_insert (dst, src, dstParent,
|
2022-11-06 08:36:49 +00:00
|
|
|
&childIndex, childCount);
|
|
|
|
if (dst == src && insertIndex <= *srcRoot) {
|
|
|
|
*srcRoot += childCount;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < childCount; i++, childIndex++) {
|
|
|
|
hierarchy_insert_children (dst, src, insertIndex + i, &childIndex);
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 08:36:49 +00:00
|
|
|
static uint32_t
|
|
|
|
hierarchy_insertHierarchy (hierarchy_t *dst, const hierarchy_t *src,
|
|
|
|
uint32_t dstParent, uint32_t *srcRoot)
|
2021-03-09 02:39:41 +00:00
|
|
|
{
|
|
|
|
uint32_t insertIndex;
|
|
|
|
|
2022-10-22 04:04:07 +00:00
|
|
|
if (dstParent == nullent) {
|
2022-10-09 17:00:33 +00:00
|
|
|
if (dst->num_objects) {
|
2021-03-09 02:39:41 +00:00
|
|
|
Sys_Error ("attempt to insert root in non-empty hierarchy");
|
|
|
|
}
|
|
|
|
hierarchy_open (dst, 0, 1);
|
2022-10-27 10:46:28 +00:00
|
|
|
if (src) {
|
2022-11-06 08:36:49 +00:00
|
|
|
hierarchy_move (dst, src, 0, *srcRoot, 1);
|
2022-10-27 10:46:28 +00:00
|
|
|
}
|
2022-10-22 04:04:07 +00:00
|
|
|
dst->parentIndex[0] = nullent;
|
2022-10-09 17:00:33 +00:00
|
|
|
dst->childIndex[0] = 1;
|
|
|
|
dst->childCount[0] = 0;
|
2021-03-09 02:39:41 +00:00
|
|
|
insertIndex = 0;
|
|
|
|
} else {
|
2022-10-09 17:00:33 +00:00
|
|
|
if (!dst->num_objects) {
|
2021-03-09 02:39:41 +00:00
|
|
|
Sys_Error ("attempt to insert non-root in empty hierarchy");
|
|
|
|
}
|
|
|
|
insertIndex = hierarchy_insert (dst, src, dstParent, srcRoot, 1);
|
|
|
|
}
|
2022-10-09 17:00:33 +00:00
|
|
|
// if src is null, then inserting a new object which has no children
|
2021-03-09 02:39:41 +00:00
|
|
|
if (src) {
|
|
|
|
hierarchy_insert_children (dst, src, insertIndex, srcRoot);
|
|
|
|
}
|
|
|
|
return insertIndex;
|
|
|
|
}
|
|
|
|
|
2022-11-06 08:36:49 +00:00
|
|
|
uint32_t
|
|
|
|
Hierarchy_InsertHierarchy (hierarchy_t *dst, const hierarchy_t *src,
|
|
|
|
uint32_t dstParent, uint32_t srcRoot)
|
|
|
|
{
|
|
|
|
return hierarchy_insertHierarchy (dst, src, dstParent, &srcRoot);
|
|
|
|
}
|
|
|
|
|
2021-03-09 02:39:41 +00:00
|
|
|
static void
|
2022-11-07 15:16:29 +00:00
|
|
|
hierarchy_remove_children (hierarchy_t *hierarchy, uint32_t index,
|
|
|
|
int delEntities)
|
2021-03-09 02:39:41 +00:00
|
|
|
{
|
2022-10-09 17:00:33 +00:00
|
|
|
uint32_t childIndex = hierarchy->childIndex[index];
|
|
|
|
uint32_t childCount = hierarchy->childCount[index];
|
2021-03-09 02:39:41 +00:00
|
|
|
|
|
|
|
for (uint32_t i = childCount; i-- > 0; ) {
|
2022-11-07 15:16:29 +00:00
|
|
|
hierarchy_remove_children (hierarchy, childIndex + i, delEntities);
|
|
|
|
}
|
|
|
|
if (delEntities) {
|
2023-03-05 12:29:26 +00:00
|
|
|
hierarchy_InvalidateReferences (hierarchy, childIndex, childCount);
|
2022-11-07 15:16:29 +00:00
|
|
|
for (uint32_t i = 0; i < childCount; i++) {
|
|
|
|
ECS_DelEntity (hierarchy->reg, hierarchy->ent[childIndex + i]);
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
hierarchy_close (hierarchy, childIndex, childCount);
|
2022-10-09 17:00:33 +00:00
|
|
|
hierarchy->childCount[index] = 0;
|
2021-03-09 02:39:41 +00:00
|
|
|
|
|
|
|
if (childCount) {
|
|
|
|
hierarchy_UpdateTransformIndices (hierarchy, childIndex, -childCount);
|
|
|
|
hierarchy_UpdateChildIndices (hierarchy, index, -childCount);
|
2022-11-06 14:04:40 +00:00
|
|
|
}
|
|
|
|
if (childIndex < hierarchy->num_objects) {
|
|
|
|
hierarchy_UpdateParentIndices (hierarchy, childIndex, -1);
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 15:16:29 +00:00
|
|
|
Hierarchy_RemoveHierarchy (hierarchy_t *hierarchy, uint32_t index,
|
|
|
|
int delEntities)
|
2021-03-09 02:39:41 +00:00
|
|
|
{
|
2022-11-05 17:38:59 +00:00
|
|
|
uint32_t parentIndex = hierarchy->parentIndex[index];
|
|
|
|
|
2022-11-07 15:16:29 +00:00
|
|
|
hierarchy_remove_children (hierarchy, index, delEntities);
|
|
|
|
if (delEntities) {
|
2023-03-05 12:29:26 +00:00
|
|
|
hierarchy_InvalidateReferences (hierarchy, index, 1);
|
2022-11-07 15:16:29 +00:00
|
|
|
ECS_DelEntity (hierarchy->reg, hierarchy->ent[index]);
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
hierarchy_close (hierarchy, index, 1);
|
2022-11-05 17:38:59 +00:00
|
|
|
|
|
|
|
hierarchy_UpdateTransformIndices (hierarchy, index, -1);
|
|
|
|
if (parentIndex != nullent) {
|
|
|
|
hierarchy_UpdateChildIndices (hierarchy, parentIndex + 1, -1);
|
|
|
|
hierarchy->childCount[parentIndex] -= 1;
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hierarchy_t *
|
2022-12-11 15:20:20 +00:00
|
|
|
Hierarchy_New (ecs_registry_t *reg, uint32_t href_comp,
|
|
|
|
const hierarchy_type_t *type, int createRoot)
|
2021-03-09 02:39:41 +00:00
|
|
|
{
|
2022-10-26 06:22:09 +00:00
|
|
|
hierarchy_t *hierarchy = PR_RESNEW (reg->hierarchies);
|
|
|
|
hierarchy->reg = reg;
|
2022-12-11 15:20:20 +00:00
|
|
|
hierarchy->href_comp = href_comp;
|
2022-03-03 21:43:30 +00:00
|
|
|
|
2022-11-05 12:26:47 +00:00
|
|
|
hierarchy->components = 0;
|
2022-10-09 17:00:33 +00:00
|
|
|
hierarchy->type = type;
|
2022-11-05 12:26:47 +00:00
|
|
|
if (type) {
|
|
|
|
hierarchy->components = calloc (hierarchy->type->num_components,
|
|
|
|
sizeof (void *));
|
|
|
|
}
|
2021-03-09 02:39:41 +00:00
|
|
|
|
|
|
|
if (createRoot) {
|
|
|
|
hierarchy_open (hierarchy, 0, 1);
|
2022-10-22 04:04:07 +00:00
|
|
|
hierarchy_init (hierarchy, 0, nullent, 1, 1);
|
2021-03-09 02:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hierarchy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Hierarchy_Delete (hierarchy_t *hierarchy)
|
|
|
|
{
|
2023-03-05 12:29:26 +00:00
|
|
|
hierarchy_InvalidateReferences (hierarchy, 0, hierarchy->num_objects);
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
free (hierarchy->ent);
|
2022-10-09 17:00:33 +00:00
|
|
|
free (hierarchy->childCount);
|
|
|
|
free (hierarchy->childIndex);
|
|
|
|
free (hierarchy->parentIndex);
|
2022-11-05 12:26:47 +00:00
|
|
|
if (hierarchy->type) {
|
|
|
|
for (uint32_t i = 0; i < hierarchy->type->num_components; i++) {
|
|
|
|
free (hierarchy->components[i]);
|
|
|
|
}
|
|
|
|
free (hierarchy->components);
|
2022-10-09 17:00:33 +00:00
|
|
|
}
|
2022-05-08 02:14:00 +00:00
|
|
|
|
2022-10-26 06:22:09 +00:00
|
|
|
ecs_registry_t *reg = hierarchy->reg;
|
|
|
|
PR_RESFREE (reg->hierarchies, hierarchy);
|
2022-05-08 02:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hierarchy_t *
|
2022-12-11 15:20:20 +00:00
|
|
|
Hierarchy_Copy (ecs_registry_t *dstReg, uint32_t href_comp,
|
|
|
|
const hierarchy_t *src)
|
2022-05-08 02:14:00 +00:00
|
|
|
{
|
2022-12-11 15:20:20 +00:00
|
|
|
hierarchy_t *dst = Hierarchy_New (dstReg, href_comp, src->type, 0);
|
2022-10-09 17:00:33 +00:00
|
|
|
size_t count = src->num_objects;
|
|
|
|
|
|
|
|
Hierarchy_Reserve (dst, count);
|
2022-05-08 02:14:00 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
dst->ent[i] = ECS_NewEntity (dstReg);
|
2022-12-11 15:20:20 +00:00
|
|
|
hierref_t *ref = Ent_AddComponent (dst->ent[i], href_comp, dstReg);
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
ref->hierarchy = dst;
|
|
|
|
ref->index = i;
|
2022-05-08 02:14:00 +00:00
|
|
|
}
|
2022-10-09 17:00:33 +00:00
|
|
|
|
|
|
|
Component_CopyElements (&childCount_component,
|
|
|
|
dst->childCount, 0, src->childCount, 0, count);
|
|
|
|
Component_CopyElements (&childIndex_component,
|
|
|
|
dst->childIndex, 0, src->childIndex, 0, count);
|
|
|
|
Component_CopyElements (&parentIndex_component,
|
|
|
|
dst->parentIndex, 0, src->parentIndex, 0, count);
|
2022-11-05 12:26:47 +00:00
|
|
|
if (dst->type) {
|
|
|
|
for (uint32_t i = 0; i < dst->type->num_components; i++) {
|
|
|
|
Component_CopyElements (&dst->type->components[i],
|
|
|
|
dst->components[i], 0,
|
|
|
|
src->components[i], 0, count);
|
|
|
|
}
|
2022-05-08 02:14:00 +00:00
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
2022-11-05 08:38:14 +00:00
|
|
|
|
|
|
|
hierref_t
|
|
|
|
Hierarchy_SetParent (hierarchy_t *dst, uint32_t dstParent,
|
|
|
|
hierarchy_t *src, uint32_t srcRoot)
|
|
|
|
{
|
|
|
|
hierref_t r = {};
|
|
|
|
if (dst && dstParent != nullent) {
|
|
|
|
if (dst->type != src->type) {
|
2023-03-05 12:29:26 +00:00
|
|
|
Sys_Error ("Can't set parent in hierarchy of different type");
|
2022-11-05 08:38:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!srcRoot) {
|
|
|
|
r.hierarchy = src;
|
|
|
|
r.index = 0;
|
|
|
|
return r;
|
|
|
|
}
|
2022-12-11 15:20:20 +00:00
|
|
|
dst = Hierarchy_New (src->reg, src->href_comp, src->type, 0);
|
2022-11-05 08:38:14 +00:00
|
|
|
}
|
|
|
|
r.hierarchy = dst;
|
2022-11-06 08:36:49 +00:00
|
|
|
r.index = hierarchy_insertHierarchy (dst, src, dstParent, &srcRoot);
|
2022-11-07 15:16:29 +00:00
|
|
|
Hierarchy_RemoveHierarchy (src, srcRoot, 0);
|
2022-11-05 08:38:14 +00:00
|
|
|
if (!src->num_objects) {
|
|
|
|
Hierarchy_Delete (src);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2023-03-05 12:29:26 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Hierref_DestroyComponent (void *href)
|
|
|
|
{
|
|
|
|
hierref_t ref = *(hierref_t *) href;
|
|
|
|
if (ref.hierarchy) {
|
2023-03-06 17:00:19 +00:00
|
|
|
ref.hierarchy->ent[ref.index] = -1;
|
2023-03-05 12:29:26 +00:00
|
|
|
Hierarchy_RemoveHierarchy (ref.hierarchy, ref.index, 1);
|
|
|
|
if (!ref.hierarchy->num_objects) {
|
|
|
|
Hierarchy_Delete (ref.hierarchy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|