Skip to content

feat: parallel entities #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build_recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ sources:
outdir: include/ecsact/entt/detail
- path: ./ecsact/entt/detail/globals.hh
outdir: include/ecsact/entt/detail
- path: ./ecsact/entt/detail/registry.hh
outdir: include/ecsact/entt/detail
- path: ./ecsact/entt/detail/internal_markers.hh
outdir: include/ecsact/entt/detail
- path: ./ecsact/entt/detail/system_execution_context.hh
Expand Down
4 changes: 2 additions & 2 deletions ecsact/entt/detail/apply_pending.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace ecsact::entt::detail {

template<typename C>
auto apply_pending_add(::entt::registry& registry) -> void {
auto apply_pending_add(ecsact::entt::registry_t& registry) -> void {
if constexpr(std::is_empty_v<C>) {
registry.view<pending_add<C>>().each([&](auto entity) {
registry.emplace<C>(entity);
Expand All @@ -26,7 +26,7 @@ auto apply_pending_add(::entt::registry& registry) -> void {
}

template<typename C>
auto apply_pending_remove(::entt::registry& registry) -> void {
auto apply_pending_remove(ecsact::entt::registry_t& registry) -> void {
registry.view<pending_remove<C>>().each([&](auto entity) {
if constexpr(!std::is_empty_v<C>) {
registry.erase<exec_beforechange_storage<C>>(entity);
Expand Down
3 changes: 2 additions & 1 deletion ecsact/entt/detail/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ecsact/runtime/core.h"
#include "ecsact/runtime/dynamic.h"
#include "ecsact/entt/detail/system_execution_context.hh"
#include "ecsact/entt/detail/registry.hh"

/**
* A small set of globals expected to be available the ecsact_rt_entt_codegen
Expand All @@ -19,7 +20,7 @@ namespace ecsact::entt::detail::globals {
*/
extern std::unordered_map< //
ecsact_registry_id,
::entt::registry>
ecsact::entt::registry_t>
registries;

/**
Expand Down
6 changes: 3 additions & 3 deletions ecsact/entt/detail/internal_markers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ constexpr bool system_markers_unimplemented_by_codegen = false;

template<typename C>
auto add_system_markers_if_needed( //
::entt::registry&,
ecsact::entt::registry_t&,
ecsact::entt::entity_id
) -> void {
static_assert(system_markers_unimplemented_by_codegen<C>, R"(
Expand All @@ -101,7 +101,7 @@ auto add_system_markers_if_needed( //

template<typename C>
auto remove_system_markers_if_needed( //
::entt::registry&,
ecsact::entt::registry_t&,
ecsact::entt::entity_id
) -> void {
static_assert(system_markers_unimplemented_by_codegen<C>, R"(
Expand All @@ -115,7 +115,7 @@ auto remove_system_markers_if_needed( //

template<typename C>
auto add_exec_itr_beforechange_if_needed( //
::entt::registry&,
ecsact::entt::registry_t&,
ecsact::entt::entity_id
) -> void {
static_assert(system_markers_unimplemented_by_codegen<C>, R"(
Expand Down
8 changes: 8 additions & 0 deletions ecsact/entt/detail/registry.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "entt/entity/registry.hpp"

namespace ecsact::entt {

using registry_t = ::entt::basic_registry<::entt::entity>;
}
9 changes: 5 additions & 4 deletions ecsact/entt/detail/system_execution_context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ecsact/runtime/common.h"
#include "ecsact/entt/event_markers.hh"
#include "ecsact/entt/entity.hh"
#include "ecsact/entt/detail/registry.hh"

namespace ecsact::entt {
/**
Expand All @@ -30,10 +31,10 @@ constexpr auto underlying_assoc_index(assoc_index n) -> unsigned {
} // namespace ecsact::entt

struct ecsact_system_execution_context {
ecsact_system_like_id id;
ecsact::entt::entity_id entity;
::entt::registry* registry = nullptr;
const void* action_data = nullptr;
ecsact_system_like_id id;
ecsact::entt::entity_id entity;
ecsact::entt::registry_t* registry = nullptr;
const void* action_data = nullptr;

// pass in the context to this class that's a pointer
// context(ptr) = parent_ctx(ptr)
Expand Down
6 changes: 3 additions & 3 deletions ecsact/entt/error_check.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace ecsact::entt {

template<typename C>
auto check_add_component_error( //
::entt::registry&,
ecsact::entt::registry_t&,
::ecsact::entt::entity_id,
const C&
) -> ecsact_add_error {
Expand All @@ -34,7 +34,7 @@ auto check_add_component_error( //

template<typename C>
auto check_update_component_error( //
::entt::registry&,
ecsact::entt::registry_t&,
::ecsact::entt::entity_id,
const C&
) -> ecsact_update_error {
Expand All @@ -49,7 +49,7 @@ auto check_update_component_error( //

template<typename A>
auto check_action_error( //
::entt::registry&,
ecsact::entt::registry_t&,
const A&
) -> ecsact_execute_systems_error {
static_assert(detail::error_check_unimplemented_by_codegen<A>, R"(
Expand Down
6 changes: 0 additions & 6 deletions ecsact/entt/event_markers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ namespace ecsact::entt {
template<typename C>
struct component_added {};

/**
* Marker to indicate that a component has been changed during execution
*/
template<typename C>
struct component_updated {};

/**
* Marker to indicate that a component has been removed
*/
Expand Down
11 changes: 6 additions & 5 deletions ecsact/entt/execution.hh
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct actions_map {
*/
template<typename System>
auto execute_system( //
::entt::registry& registry,
ecsact::entt::registry_t& registry,
ecsact_system_execution_context* parent,
const ecsact::entt::actions_map& actions_map
) -> void {
Expand All @@ -115,7 +115,7 @@ auto execute_system( //
*/
template<typename Action>
auto execute_actions( //
::entt::registry& registry,
ecsact::entt::registry_t& registry,
ecsact_system_execution_context* parent,
const ecsact::entt::actions_map& actions_map
) -> void {
Expand All @@ -129,7 +129,7 @@ auto execute_actions( //
}

using execute_fn_t = void (*)(
::entt::registry& registry,
ecsact::entt::registry_t& registry,
ecsact_system_execution_context* parent,
const ecsact::entt::actions_map& actions_map
);
Expand All @@ -140,7 +140,7 @@ using execute_fn_t = void (*)(
* `ecsact_rt_entt_codegen`.
*/
template<typename SystemLike>
auto prepare_system_like(::entt::registry&) -> void {
auto prepare_system_like(ecsact::entt::registry_t&) -> void {
static_assert(detail::unimplemented_by_codegen<SystemLike>, R"(
-----------------------------------------------------------------------------
| (!) CODEGEN ERROR |
Expand All @@ -155,7 +155,8 @@ auto prepare_system_like(::entt::registry&) -> void {
* entity.
*/
template<typename SystemLike>
auto entity_matches_system(::entt::registry&, ecsact::entt::entity_id) -> bool {
auto entity_matches_system(ecsact::entt::registry_t&, ecsact::entt::entity_id)
-> bool {
static_assert(detail::unimplemented_by_codegen<SystemLike>, R"(
-----------------------------------------------------------------------------
| (!) CODEGEN ERROR |
Expand Down
5 changes: 3 additions & 2 deletions ecsact/entt/registry_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#include <cassert>
#include "entt/entity/registry.hpp"
#include "ecsact/entt/detail/globals.hh"
#include "ecsact/entt/detail/registry.hh"

namespace ecsact::entt {

inline auto get_registry( //
ecsact_registry_id id
) -> ::entt::registry& {
) -> ecsact::entt::registry_t& {
using ecsact::entt::detail::globals::registries;

// Check to make sure we're not trying to get a registry that doesn't exist
Expand All @@ -18,7 +19,7 @@ inline auto get_registry( //
}

inline auto create_registry()
-> std::tuple<ecsact_registry_id, ::entt::registry&> {
-> std::tuple<ecsact_registry_id, ecsact::entt::registry_t&> {
using ecsact::entt::detail::globals::last_registry_id;
using ecsact::entt::detail::globals::registries;

Expand Down
75 changes: 52 additions & 23 deletions ecsact/entt/wrapper/core.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "ecsact/entt/error_check.hh"
#include "ecsact/entt/detail/execution_events_collector.hh"

#include <iostream>

namespace ecsact::entt::wrapper::core {

template<typename C>
Expand Down Expand Up @@ -118,6 +120,8 @@ inline auto update_component( //
[[maybe_unused]] ecsact_component_id component_id,
const void* component_data
) -> ecsact_update_error {
using ecsact::entt::detail::exec_beforechange_storage;

auto& reg = ecsact::entt::get_registry(registry_id);
auto entity = ecsact::entt::entity_id{entity_id};
assert(C::id == component_id);
Expand All @@ -129,10 +133,21 @@ inline auto update_component( //
*static_cast<const C*>(component_data)
);

if(err == ECSACT_UPDATE_OK) {
reg.replace<C>(entity, *static_cast<const C*>(component_data));
if(err != ECSACT_UPDATE_OK) {
return err;
}

const auto& in_component = *static_cast<const C*>(component_data);
auto& beforechange = reg.template get<exec_beforechange_storage<C>>(entity);
auto& current_component = reg.template get<C>(entity);

if(!beforechange.has_update_occurred) {
beforechange.value = current_component;
beforechange.has_update_occurred = true;
}

current_component = in_component;

return err;
}

Expand All @@ -143,6 +158,8 @@ inline auto update_component_exec_options( //
[[maybe_unused]] ecsact_component_id component_id,
const void* component_data
) -> ecsact_update_error {
using ecsact::entt::detail::exec_beforechange_storage;

auto& reg = ecsact::entt::get_registry(registry_id);
auto entity = ecsact::entt::entity_id{entity_id};
assert(C::id == component_id);
Expand All @@ -154,11 +171,20 @@ inline auto update_component_exec_options( //
*static_cast<const C*>(component_data)
);

if(err == ECSACT_UPDATE_OK) {
reg.replace<C>(entity, *static_cast<const C*>(component_data));
reg.template emplace_or_replace<component_updated<C>>(entity);
if(err != ECSACT_UPDATE_OK) {
return err;
}

const auto& in_component = *static_cast<const C*>(component_data);
auto& beforechange = reg.template get<exec_beforechange_storage<C>>(entity);
auto& current_component = reg.template get<C>(entity);

if(!beforechange.has_update_occurred) {
beforechange.value = current_component;
beforechange.has_update_occurred = true;
}
current_component = in_component;

return err;
}

Expand All @@ -177,7 +203,6 @@ auto remove_component(
reg.remove<detail::exec_beforechange_storage<C>>(entity);
}
reg.template remove<component_added<C>>(entity);
reg.template remove<component_updated<C>>(entity);
reg.template emplace_or_replace<component_removed<C>>(entity);
ecsact::entt::detail::remove_system_markers_if_needed<C>(reg, entity);
}
Expand All @@ -203,7 +228,6 @@ auto remove_component_exec_options(

reg.template erase<C>(entity);
reg.template remove<component_added<C>>(entity);
reg.template remove<component_updated<C>>(entity);
reg.template emplace_or_replace<component_removed<C>>(entity);

if constexpr(!std::is_empty_v<C>) {
Expand Down Expand Up @@ -295,30 +319,30 @@ auto _trigger_update_component_event(
ecsact_registry_id registry_id,
ecsact::entt::detail::execution_events_collector& events_collector
) -> void {
using ecsact::entt::component_updated;
using ecsact::entt::detail::beforeremove_storage;
using ecsact::entt::detail::exec_beforechange_storage;

//

if(!events_collector.has_update_callback()) {
return;
}

auto& reg = ecsact::entt::get_registry(registry_id);
if constexpr(!C::transient && !std::is_empty_v<C>) {
::entt::basic_view changed_view{
reg.template storage<C>(),
reg.template storage<exec_beforechange_storage<C>>(),
reg.template storage<component_updated<C>>(),
};
auto comp_view = reg.view<C, exec_beforechange_storage<C>>( //
::entt::exclude<beforeremove_storage<C>>
);

for(ecsact::entt::entity_id entity : changed_view) {
for(ecsact::entt::entity_id entity : comp_view) {
auto& before =
changed_view.template get<exec_beforechange_storage<C>>(entity);
auto& current = changed_view.template get<C>(entity);
before.has_update_occurred = false;
comp_view.template get<exec_beforechange_storage<C>>(entity);
auto& current = comp_view.template get<C>(entity);

if(before.value != current) {
if(before.has_update_occurred && before.value != current) {
events_collector.invoke_update_callback<C>(entity, current);
}
before.has_update_occurred = false;
}
}
}
Expand Down Expand Up @@ -384,7 +408,6 @@ inline auto clear_component(ecsact_registry_id registry_id) -> void {
auto& reg = ecsact::entt::get_registry(registry_id);

reg.clear<ecsact::entt::component_added<C>>();
reg.clear<ecsact::entt::component_updated<C>>();
reg.clear<ecsact::entt::component_removed<C>>();
}

Expand All @@ -404,10 +427,13 @@ inline auto prepare_component(ecsact_registry_id registry_id) -> void {
reg.template storage<C>();
reg.template storage<component_added<C>>();
reg.template storage<component_removed<C>>();
reg.template storage<detail::pending_add<C>>();
reg.template storage<detail::pending_remove<C>>();
reg.template storage<detail::beforeremove_storage<C>>();

if constexpr(!std::is_empty_v<C>) {
reg.storage<detail::exec_beforechange_storage<C>>();
reg.template storage<component_updated<C>>();
reg.template storage<detail::exec_beforechange_storage<C>>();
reg.template storage<detail::exec_itr_beforechange_storage<C>>();
}
}

Expand All @@ -418,6 +444,7 @@ inline auto prepare_system(ecsact_registry_id registry_id) -> void {

reg.template storage<system_sorted<S>>();
reg.template storage<pending_lazy_execution<S>>();
reg.template storage<run_system<S>>();
}

template<typename C, typename V>
Expand All @@ -435,8 +462,10 @@ auto has_component_changed(entt::entity_id entity, V& view) -> bool {
}

template<typename C>
auto update_exec_itr_beforechange(entt::entity_id entity, ::entt::registry& reg)
-> void {
auto update_exec_itr_beforechange(
entt::entity_id entity,
ecsact::entt::registry_t& reg
) -> void {
auto comp = reg.get<C>(entity);
auto& beforechange_comp =
reg.get<detail::exec_itr_beforechange_storage<C>>(entity);
Expand Down
Loading