Skip to content

Commit

Permalink
Fixed: Archetype matching
Browse files Browse the repository at this point in the history
  • Loading branch information
richardbiely committed Sep 30, 2024
1 parent 2d79133 commit 8adc9fd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
1 change: 1 addition & 0 deletions include/gaia/ecs/query_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ namespace gaia {
//! Index of the last checked archetype in the component-to-archetype map
QueryArchetypeCacheIndexMap lastMatchedArchetypeIdx_All;
QueryArchetypeCacheIndexMap lastMatchedArchetypeIdx_Any;
QueryArchetypeCacheIndexMap lastMatchedArchetypeIdx_Not;
//! Mapping of the original indices to the new ones after sorting
QueryRemappingArray remapping;
//! Array of filtered components
Expand Down
1 change: 1 addition & 0 deletions include/gaia/ecs/query_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ namespace gaia {
ctx.pMatchesSet = &s_tmpArchetypeMatchesSet;
ctx.pLastMatchedArchetypeIdx_All = &data.lastMatchedArchetypeIdx_All;
ctx.pLastMatchedArchetypeIdx_Any = &data.lastMatchedArchetypeIdx_Any;
ctx.pLastMatchedArchetypeIdx_Not = &data.lastMatchedArchetypeIdx_Not;
ctx.as_mask_0 = data.as_mask_0;
ctx.as_mask_1 = data.as_mask_1;
m_vm.exec(ctx);
Expand Down
38 changes: 28 additions & 10 deletions include/gaia/ecs/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ namespace gaia {
QueryArchetypeCacheIndexMap* pLastMatchedArchetypeIdx_All;
//! Idx of the last matched archetype against the ANY opcode
QueryArchetypeCacheIndexMap* pLastMatchedArchetypeIdx_Any;
//! Idx of the last matched archetype against the NOT opcode
QueryArchetypeCacheIndexMap* pLastMatchedArchetypeIdx_Not;
//! Mask for items with Is relationship pair.
//! If the id is a pair, the first part (id) is written here.
uint32_t as_mask_0;
Expand Down Expand Up @@ -113,6 +115,19 @@ namespace gaia {
cnt::sarr_ext<Entity, MAX_ITEMS_IN_QUERY> ids_not;
};

inline uint32_t handle_last_archetype_match(
QueryArchetypeCacheIndexMap* pCont, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
const auto cache_it = pCont->find(entityKey);
uint32_t lastMatchedIdx = 0;
if (cache_it == pCont->end())
pCont->emplace(entityKey, pSrcArchetype->size());
else {
lastMatchedIdx = cache_it->second;
cache_it->second = pSrcArchetype->size();
}
return lastMatchedIdx;
}

// Operator ALL (used by query::all)
struct OpAll {
static bool can_continue(bool hasMatch) {
Expand All @@ -121,6 +136,10 @@ namespace gaia {
static bool eval(uint32_t expectedMatches, uint32_t totalMatches) {
return expectedMatches == totalMatches;
}
static uint32_t
handle_last_match(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
return handle_last_archetype_match(ctx.pLastMatchedArchetypeIdx_All, entityKey, pSrcArchetype);
}
};
// Operator OR (used by query::any)
struct OpAny {
Expand All @@ -131,6 +150,10 @@ namespace gaia {
(void)expectedMatches;
return totalMatches > 0;
}
static uint32_t
handle_last_match(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
return handle_last_archetype_match(ctx.pLastMatchedArchetypeIdx_Any, entityKey, pSrcArchetype);
}
};
// Operator NOT (used by query::no)
struct OpNo {
Expand All @@ -141,6 +164,10 @@ namespace gaia {
(void)expectedMatches;
return totalMatches == 0;
}
static uint32_t
handle_last_match(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
return handle_last_archetype_match(ctx.pLastMatchedArchetypeIdx_Not, entityKey, pSrcArchetype);
}
};

template <typename Op>
Expand Down Expand Up @@ -426,19 +453,10 @@ namespace gaia {
inline void
match_archetype_inter(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetypes) {
const auto& archetypes = *pSrcArchetypes;

auto& matchesArr = *ctx.pMatchesArr;
auto& matchesSet = *ctx.pMatchesSet;

const auto cache_it = ctx.pLastMatchedArchetypeIdx_All->find(entityKey);
uint32_t lastMatchedIdx = 0;
if (cache_it == ctx.pLastMatchedArchetypeIdx_All->end())
ctx.pLastMatchedArchetypeIdx_All->emplace(entityKey, archetypes.size());
else {
lastMatchedIdx = cache_it->second;
cache_it->second = archetypes.size();
}

uint32_t lastMatchedIdx = OpKind::handle_last_match(ctx, entityKey, pSrcArchetypes);
if (lastMatchedIdx >= archetypes.size())
return;

Expand Down
40 changes: 30 additions & 10 deletions single_include/gaia.h
Original file line number Diff line number Diff line change
Expand Up @@ -21495,6 +21495,7 @@ namespace gaia {
//! Index of the last checked archetype in the component-to-archetype map
QueryArchetypeCacheIndexMap lastMatchedArchetypeIdx_All;
QueryArchetypeCacheIndexMap lastMatchedArchetypeIdx_Any;
QueryArchetypeCacheIndexMap lastMatchedArchetypeIdx_Not;
//! Mapping of the original indices to the new ones after sorting
QueryRemappingArray remapping;
//! Array of filtered components
Expand Down Expand Up @@ -22101,6 +22102,8 @@ namespace gaia {
QueryArchetypeCacheIndexMap* pLastMatchedArchetypeIdx_All;
//! Idx of the last matched archetype against the ANY opcode
QueryArchetypeCacheIndexMap* pLastMatchedArchetypeIdx_Any;
//! Idx of the last matched archetype against the NOT opcode
QueryArchetypeCacheIndexMap* pLastMatchedArchetypeIdx_Not;
//! Mask for items with Is relationship pair.
//! If the id is a pair, the first part (id) is written here.
uint32_t as_mask_0;
Expand Down Expand Up @@ -22167,6 +22170,19 @@ namespace gaia {
cnt::sarr_ext<Entity, MAX_ITEMS_IN_QUERY> ids_not;
};

inline uint32_t handle_last_archetype_match(
QueryArchetypeCacheIndexMap* pCont, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
const auto cache_it = pCont->find(entityKey);
uint32_t lastMatchedIdx = 0;
if (cache_it == pCont->end())
pCont->emplace(entityKey, pSrcArchetype->size());
else {
lastMatchedIdx = cache_it->second;
cache_it->second = pSrcArchetype->size();
}
return lastMatchedIdx;
}

// Operator ALL (used by query::all)
struct OpAll {
static bool can_continue(bool hasMatch) {
Expand All @@ -22175,6 +22191,10 @@ namespace gaia {
static bool eval(uint32_t expectedMatches, uint32_t totalMatches) {
return expectedMatches == totalMatches;
}
static uint32_t
handle_last_match(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
return handle_last_archetype_match(ctx.pLastMatchedArchetypeIdx_All, entityKey, pSrcArchetype);
}
};
// Operator OR (used by query::any)
struct OpAny {
Expand All @@ -22185,6 +22205,10 @@ namespace gaia {
(void)expectedMatches;
return totalMatches > 0;
}
static uint32_t
handle_last_match(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
return handle_last_archetype_match(ctx.pLastMatchedArchetypeIdx_Any, entityKey, pSrcArchetype);
}
};
// Operator NOT (used by query::no)
struct OpNo {
Expand All @@ -22195,6 +22219,10 @@ namespace gaia {
(void)expectedMatches;
return totalMatches == 0;
}
static uint32_t
handle_last_match(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetype) {
return handle_last_archetype_match(ctx.pLastMatchedArchetypeIdx_Not, entityKey, pSrcArchetype);
}
};

template <typename Op>
Expand Down Expand Up @@ -22480,19 +22508,10 @@ namespace gaia {
inline void
match_archetype_inter(MatchingCtx& ctx, EntityLookupKey entityKey, const ArchetypeDArray* pSrcArchetypes) {
const auto& archetypes = *pSrcArchetypes;

auto& matchesArr = *ctx.pMatchesArr;
auto& matchesSet = *ctx.pMatchesSet;

const auto cache_it = ctx.pLastMatchedArchetypeIdx_All->find(entityKey);
uint32_t lastMatchedIdx = 0;
if (cache_it == ctx.pLastMatchedArchetypeIdx_All->end())
ctx.pLastMatchedArchetypeIdx_All->emplace(entityKey, archetypes.size());
else {
lastMatchedIdx = cache_it->second;
cache_it->second = archetypes.size();
}

uint32_t lastMatchedIdx = OpKind::handle_last_match(ctx, entityKey, pSrcArchetypes);
if (lastMatchedIdx >= archetypes.size())
return;

Expand Down Expand Up @@ -23104,6 +23123,7 @@ namespace gaia {
ctx.pMatchesSet = &s_tmpArchetypeMatchesSet;
ctx.pLastMatchedArchetypeIdx_All = &data.lastMatchedArchetypeIdx_All;
ctx.pLastMatchedArchetypeIdx_Any = &data.lastMatchedArchetypeIdx_Any;
ctx.pLastMatchedArchetypeIdx_Not = &data.lastMatchedArchetypeIdx_Not;
ctx.as_mask_0 = data.as_mask_0;
ctx.as_mask_1 = data.as_mask_1;
m_vm.exec(ctx);
Expand Down

0 comments on commit 8adc9fd

Please sign in to comment.