Skip to content

Commit

Permalink
Added: Support for fixed entity targets for queries
Browse files Browse the repository at this point in the history
Tweaked: Enum/struct naming in CommandBuffer
  • Loading branch information
richardbiely committed Dec 30, 2023
1 parent 6f5d29f commit eefc190
Show file tree
Hide file tree
Showing 9 changed files with 599 additions and 335 deletions.
6 changes: 1 addition & 5 deletions include/gaia/core/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ namespace gaia {
};

//----------------------------------------------------------------------
// Compile - time for each
// Looping
//----------------------------------------------------------------------

namespace detail {
Expand Down Expand Up @@ -384,10 +384,6 @@ namespace gaia {
}
} // namespace detail

//----------------------------------------------------------------------
// Looping
//----------------------------------------------------------------------

//! Compile-time for loop. Performs \tparam Iters iterations.
//!
//! Example 1 (index argument):
Expand Down
72 changes: 36 additions & 36 deletions include/gaia/ecs/command_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace gaia {
}
};

enum CommandBufferCmd : uint8_t {
enum CommandBufferCmdType : uint8_t {
CREATE_ENTITY,
CREATE_ENTITY_FROM_ARCHETYPE,
CREATE_ENTITY_FROM_ENTITY,
Expand All @@ -58,12 +58,12 @@ namespace gaia {
REMOVE_COMPONENT
};

struct CommandBufferCmd_t {
CommandBufferCmd id;
struct CommandBufferCmd {
CommandBufferCmdType id;
};

struct CREATE_ENTITY_t: CommandBufferCmd_t {};
struct CREATE_ENTITY_FROM_ARCHETYPE_t: CommandBufferCmd_t {
struct CreateEntityCmd: CommandBufferCmd {};
struct CreateEntityFromArchetypeCmd: CommandBufferCmd {
uint64_t archetypePtr;

void commit(CommandBufferCtx& ctx) const {
Expand All @@ -73,22 +73,22 @@ namespace gaia {
GAIA_ASSERT(res.second);
}
};
struct CREATE_ENTITY_FROM_ENTITY_t: CommandBufferCmd_t {
struct CreateEntityFromEntityCmd: CommandBufferCmd {
Entity entity;

void commit(CommandBufferCtx& ctx) const {
[[maybe_unused]] const auto res = ctx.entityMap.try_emplace(ctx.entities++, ctx.world.copy(entity));
GAIA_ASSERT(res.second);
}
};
struct DELETE_ENTITY_t: CommandBufferCmd_t {
struct DeleteEntityCmd: CommandBufferCmd {
Entity entity;

void commit(CommandBufferCtx& ctx) const {
ctx.world.del(entity);
}
};
struct ADD_COMPONENT_t: CommandBufferCmd_t {
struct AddComponentCmd: CommandBufferCmd {
Entity entity;
Entity object;

Expand All @@ -102,7 +102,7 @@ namespace gaia {
#endif
}
};
struct ADD_COMPONENT_DATA_t: CommandBufferCmd_t {
struct AddComponentWithDataCmd: CommandBufferCmd {
Entity entity;
Entity object;

Expand All @@ -122,7 +122,7 @@ namespace gaia {
ctx.load_comp(pComponentData, object);
}
};
struct ADD_COMPONENT_TO_TEMPENTITY_t: CommandBufferCmd_t {
struct AddComponentToTempEntityCmd: CommandBufferCmd {
TempEntity tempEntity;
Entity object;

Expand All @@ -143,7 +143,7 @@ namespace gaia {
#endif
}
};
struct ADD_COMPONENT_TO_TEMPENTITY_DATA_t: CommandBufferCmd_t {
struct AddComponentWithDataToTempEntityCmd: CommandBufferCmd {
TempEntity tempEntity;
Entity object;

Expand All @@ -170,7 +170,7 @@ namespace gaia {
ctx.load_comp(pComponentData, object);
}
};
struct SET_COMPONENT_t: CommandBufferCmd_t {
struct SetComponentCmd: CommandBufferCmd {
Entity entity;
Entity object;

Expand All @@ -185,7 +185,7 @@ namespace gaia {
ctx.load_comp(pComponentData, object);
}
};
struct SET_COMPONENT_FOR_TEMPENTITY_t: CommandBufferCmd_t {
struct SetComponentOnTempEntityCmd: CommandBufferCmd {
TempEntity tempEntity;
Entity object;

Expand All @@ -208,7 +208,7 @@ namespace gaia {
ctx.load_comp(pComponentData, object);
}
};
struct REMOVE_COMPONENT_t: CommandBufferCmd_t {
struct RemoveComponentCmd: CommandBufferCmd {
Entity entity;
Entity object;

Expand All @@ -230,7 +230,7 @@ namespace gaia {
GAIA_NODISCARD TempEntity add(Archetype& archetype) {
m_ctx.save(CREATE_ENTITY_FROM_ARCHETYPE);

CREATE_ENTITY_FROM_ARCHETYPE_t cmd;
CreateEntityFromArchetypeCmd cmd;
cmd.archetypePtr = (uintptr_t)&archetype;
ser::save(m_ctx, cmd);

Expand Down Expand Up @@ -265,7 +265,7 @@ namespace gaia {
GAIA_NODISCARD TempEntity copy(Entity entityFrom) {
m_ctx.save(CREATE_ENTITY_FROM_ENTITY);

CREATE_ENTITY_FROM_ENTITY_t cmd;
CreateEntityFromEntityCmd cmd;
cmd.entity = entityFrom;
ser::save(m_ctx, cmd);

Expand All @@ -278,7 +278,7 @@ namespace gaia {
void del(Entity entity) {
m_ctx.save(DELETE_ENTITY);

DELETE_ENTITY_t cmd;
DeleteEntityCmd cmd;
cmd.entity = entity;
ser::save(m_ctx, cmd);
}
Expand All @@ -295,7 +295,7 @@ namespace gaia {

m_ctx.save(ADD_COMPONENT);

ADD_COMPONENT_t cmd;
AddComponentCmd cmd;
cmd.entity = entity;
cmd.object = desc.entity;
ser::save(m_ctx, cmd);
Expand All @@ -313,7 +313,7 @@ namespace gaia {

m_ctx.save(ADD_COMPONENT_TO_TEMPENTITY);

ADD_COMPONENT_TO_TEMPENTITY_t cmd;
AddComponentToTempEntityCmd cmd;
cmd.tempEntity = entity;
cmd.object = desc.entity;
ser::save(m_ctx, cmd);
Expand All @@ -331,7 +331,7 @@ namespace gaia {

m_ctx.save(ADD_COMPONENT_DATA);

ADD_COMPONENT_DATA_t cmd;
AddComponentWithDataCmd cmd;
cmd.entity = entity;
cmd.object = desc.entity;
ser::save(m_ctx, cmd);
Expand All @@ -350,7 +350,7 @@ namespace gaia {

m_ctx.save(ADD_COMPONENT_TO_TEMPENTITY_DATA);

ADD_COMPONENT_TO_TEMPENTITY_t cmd;
AddComponentToTempEntityCmd cmd;
cmd.tempEntity = entity;
cmd.object = desc.entity;
ser::save(m_ctx, cmd);
Expand All @@ -369,7 +369,7 @@ namespace gaia {

m_ctx.save(SET_COMPONENT);

SET_COMPONENT_t cmd;
SetComponentCmd cmd;
cmd.entity = entity;
cmd.object = desc.entity;
ser::save(m_ctx, cmd);
Expand All @@ -389,7 +389,7 @@ namespace gaia {

m_ctx.save(SET_COMPONENT_FOR_TEMPENTITY);

SET_COMPONENT_FOR_TEMPENTITY_t cmd;
SetComponentOnTempEntityCmd cmd;
cmd.tempEntity = entity;
cmd.object = desc.entity;
ser::save(m_ctx, cmd);
Expand All @@ -408,7 +408,7 @@ namespace gaia {

m_ctx.save(REMOVE_COMPONENT);

REMOVE_COMPONENT_t cmd;
RemoveComponentCmd cmd;
cmd.entity = entity;
cmd.object = desc.entity;
ser::save(m_ctx, cmd);
Expand All @@ -424,61 +424,61 @@ namespace gaia {
},
// CREATE_ENTITY_FROM_ARCHETYPE
[](CommandBufferCtx& ctx) {
CREATE_ENTITY_FROM_ARCHETYPE_t cmd;
CreateEntityFromArchetypeCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// CREATE_ENTITY_FROM_ENTITY
[](CommandBufferCtx& ctx) {
CREATE_ENTITY_FROM_ENTITY_t cmd;
CreateEntityFromEntityCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// DELETE_ENTITY
[](CommandBufferCtx& ctx) {
DELETE_ENTITY_t cmd;
DeleteEntityCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// ADD_COMPONENT
[](CommandBufferCtx& ctx) {
ADD_COMPONENT_t cmd;
AddComponentCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// ADD_COMPONENT_DATA
[](CommandBufferCtx& ctx) {
ADD_COMPONENT_DATA_t cmd;
AddComponentWithDataCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// ADD_COMPONENT_TO_TEMPENTITY
[](CommandBufferCtx& ctx) {
ADD_COMPONENT_TO_TEMPENTITY_t cmd;
AddComponentToTempEntityCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// ADD_COMPONENT_TO_TEMPENTITY_DATA
[](CommandBufferCtx& ctx) {
ADD_COMPONENT_TO_TEMPENTITY_DATA_t cmd;
AddComponentWithDataToTempEntityCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// SET_COMPONENT
[](CommandBufferCtx& ctx) {
SET_COMPONENT_t cmd;
SetComponentCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// SET_COMPONENT_FOR_TEMPENTITY
[](CommandBufferCtx& ctx) {
SET_COMPONENT_FOR_TEMPENTITY_t cmd;
SetComponentOnTempEntityCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
},
// REMOVE_COMPONENT
[](CommandBufferCtx& ctx) {
REMOVE_COMPONENT_t cmd;
RemoveComponentCmd cmd;
ser::load(ctx, cmd);
cmd.commit(ctx);
}};
Expand All @@ -494,14 +494,14 @@ namespace gaia {
// Extract data from the buffer
m_ctx.seek(0);
while (m_ctx.tell() < m_ctx.bytes()) {
CommandBufferCmd id{};
CommandBufferCmdType id{};
m_ctx.load(id);
CommandBufferRead[id](m_ctx);
}

m_entities = 0;
m_ctx.reset();
} // namespace ecs
}; // namespace gaia
};
} // namespace ecs
} // namespace gaia
24 changes: 16 additions & 8 deletions include/gaia/ecs/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ namespace gaia {

private:
//! Command buffer command type
enum CommandBufferCmd : uint8_t { ADD_ITEM, ADD_FILTER };
enum CommandBufferCmdType : uint8_t { ADD_ITEM, ADD_FILTER };

struct Command_AddItem {
static constexpr CommandBufferCmd Id = CommandBufferCmd::ADD_ITEM;
static constexpr CommandBufferCmdType Id = CommandBufferCmdType::ADD_ITEM;

QueryItem item;

Expand All @@ -86,15 +86,16 @@ namespace gaia {

const uint8_t isReadWrite = item.access == QueryAccess::Write;
data.readWriteMask |= (isReadWrite << (uint8_t)ids.size());
data.remapping.push_back(data.remapping.size());

ids.push_back(item.id);
pairs.push_back({item.op, item.id});
pairs.push_back({item.id, item.src, nullptr, item.op});
lastMatchedArchetypeIdx.push_back(0);
}
};

struct Command_Filter {
static constexpr CommandBufferCmd Id = CommandBufferCmd::ADD_FILTER;
static constexpr CommandBufferCmdType Id = CommandBufferCmdType::ADD_FILTER;

Entity comp;

Expand Down Expand Up @@ -191,7 +192,7 @@ namespace gaia {

// No queryId is set which means QueryInfo needs to be created
QueryCtx ctx;
ctx.cc = &comp_cache_mut(*m_world);
ctx.init(m_world);
commit(ctx);
auto& queryInfo = m_storage.m_queryCache->add(GAIA_MOV(ctx));
m_storage.m_queryId = queryInfo.id();
Expand All @@ -200,7 +201,7 @@ namespace gaia {
} else {
if GAIA_UNLIKELY (m_storage.m_queryInfo.id() == QueryIdBad) {
QueryCtx ctx;
ctx.cc = &comp_cache_mut(*m_world);
ctx.init(m_world);
commit(ctx);
m_storage.m_queryInfo = QueryInfo::create(QueryId{}, GAIA_MOV(ctx));
}
Expand Down Expand Up @@ -288,7 +289,7 @@ namespace gaia {
// Read data from buffer and execute the command stored in it
m_serBuffer.seek(0);
while (m_serBuffer.tell() < m_serBuffer.bytes()) {
CommandBufferCmd id{};
CommandBufferCmdType id{};
ser::load(m_serBuffer, id);
CommandBufferRead[id](m_serBuffer, ctx);
}
Expand Down Expand Up @@ -652,7 +653,6 @@ namespace gaia {
//!
//! \param str Null-terminated string with the query expression
QueryImpl& add(const char* str, ...) {

GAIA_ASSERT(str != nullptr);
if (str == nullptr)
return *this;
Expand Down Expand Up @@ -725,6 +725,14 @@ namespace gaia {
return *this;
}

QueryImpl& all(Entity entity, Entity src, bool isReadWrite = false) {
if (entity.pair())
add({entity, QueryOp::All, QueryAccess::None, src});
else
add({entity, QueryOp::All, isReadWrite ? QueryAccess::Write : QueryAccess::Read, src});
return *this;
}

template <typename... T>
QueryImpl& all() {
// Add commands to the command buffer
Expand Down
Loading

0 comments on commit eefc190

Please sign in to comment.