Skip to content

Commit

Permalink
feat: Provider base class, folder structure and lazy done
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelwan committed May 29, 2024
1 parent def6ba6 commit 61b4548
Show file tree
Hide file tree
Showing 14 changed files with 516 additions and 129 deletions.
1 change: 1 addition & 0 deletions rt_entt_codegen/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ _CORE_CODEGEN_METHODS = {
"//rt_entt_codegen/shared:system_util",
"//rt_entt_codegen/shared:parallel",
"//rt_entt_codegen/core/sys_exec",
"//rt_entt_codegen/core/system_provider:lazy",
"@entt//:entt",
"@ecsact_rt_entt//:lib",
],
Expand Down
149 changes: 38 additions & 111 deletions rt_entt_codegen/core/print_sys_exec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "rt_entt_codegen/shared/system_util.hh"
#include "rt_entt_codegen/shared/parallel.hh"
#include "rt_entt_codegen/core/sys_exec/sys_exec.hh"
#include "rt_entt_codegen/core/system_provider/lazy/lazy.hh"
#include "system_provider/lazy/lazy.hh"
#include "system_provider/system_provider.hh"

using capability_t =
std::unordered_map<ecsact_component_like_id, ecsact_system_capability>;
Expand All @@ -45,9 +48,10 @@ static auto print_sys_exec_ctx_action(
.parameter("void*", "out_action_data")
.return_type("void final");

if(options.is_action()) {
auto action_name =
cpp_identifier(decl_full_name(options.get_sys_like_id()));
if(options.sys_like_id_variant.is_action()) {
auto action_name = cpp_identifier(
decl_full_name(options.sys_like_id_variant.get_sys_like_id())
);

ctx.write(
"*static_cast<",
Expand Down Expand Up @@ -673,17 +677,29 @@ static auto print_execute_systems(
using ecsact::cpp_codegen_plugin_util::block;
using ecsact::meta::decl_full_name;
using ecsact::rt_entt_codegen::ecsact_entt_system_details;
using ecsact::rt_entt_codegen::core::provider::lazy;
using ecsact::rt_entt_codegen::system_util::create_context_struct_name;
using ecsact::rt_entt_codegen::system_util::create_context_var_name;
using ecsact::rt_entt_codegen::system_util::is_notify_system;
using ecsact::rt_entt_codegen::system_util::print_system_notify_views;
using ecsact::rt_entt_codegen::util::method_printer;

auto sys_caps = ecsact::meta::system_capabilities(options.get_sys_like_id());
auto lazy_provider = lazy{
ctx,
sys_details,
options.sys_like_id_variant,
options.registry_var_name
};

auto sys_caps = ecsact::meta::system_capabilities(
options.sys_like_id_variant.get_sys_like_id()
);
auto lazy_iteration_rate = 0;

auto exec_start_label_name =
std::format("exec_start_{}_", static_cast<int>(options.get_sys_like_id()));
auto exec_start_label_name = std::format(
"exec_start_{}_",
static_cast<int>(options.sys_like_id_variant.get_sys_like_id())
);

auto pending_lazy_exec_struct = std::format(
"::ecsact::entt::detail::pending_lazy_execution<::{}>",
Expand All @@ -697,37 +713,23 @@ static auto print_execute_systems(

auto additional_view_components = std::vector<std::string>{};

if(options.is_system()) {
lazy_iteration_rate = ecsact_meta_get_lazy_iteration_rate(
static_cast<ecsact_system_id>(options.get_sys_like_id())
);
}

if(lazy_iteration_rate > 0) {
ctx.write(
"constexpr auto lazy_iteration_rate_ = ",
lazy_iteration_rate,
";\n\n"
);
ctx.write("auto iteration_count_ = 0;\n\n");
ctx.write(exec_start_label_name, ":\n");
additional_view_components.push_back(pending_lazy_exec_struct);
if(options.sys_like_id_variant.is_system()) {
lazy_iteration_rate =
ecsact_meta_get_lazy_iteration_rate(static_cast<ecsact_system_id>(
options.sys_like_id_variant.get_sys_like_id()
));
}

if(options.is_system()) {
if(system_needs_sorted_entities(options.as_system())) {
additional_view_components.push_back(system_sorting_struct_name);
}
}
lazy_provider.before_make_view_or_group(additional_view_components);

if(is_notify_system(options.get_sys_like_id())) {
if(is_notify_system(options.sys_like_id_variant.get_sys_like_id())) {
additional_view_components.push_back(
std::format("ecsact::entt::detail::run_system<{}>", options.system_name)
);
print_system_notify_views(
ctx,
sys_details,
options.get_sys_like_id(),
options.sys_like_id_variant.get_sys_like_id(),
options.registry_var_name
);
}
Expand All @@ -742,8 +744,8 @@ static auto print_execute_systems(

ctx.write("using view_t = decltype(view);\n");

if(options.is_system()) {
if(system_needs_sorted_entities(options.as_system())) {
if(options.sys_like_id_variant.is_system()) {
if(system_needs_sorted_entities(options.sys_like_id_variant.as_system())) {
ctx.write("view.use<", system_sorting_struct_name, ">();\n");
}
}
Expand Down Expand Up @@ -790,19 +792,7 @@ static auto print_execute_systems(
auto other_view_names = print_other_contexts(ctx, sys_details, options);

block(ctx, "for(ecsact::entt::entity_id entity : view)", [&] {
if(lazy_iteration_rate > 0) {
block(ctx, "if(iteration_count_ == lazy_iteration_rate_)", [&] {
ctx.write("break;\n");
});

ctx.write("++iteration_count_;\n");
ctx.write(
options.registry_var_name,
".erase<",
pending_lazy_exec_struct,
">(entity);\n"
);
}
lazy_provider.pre_exec_system_impl();

// value = comp var name
auto components_with_entity_fields =
Expand Down Expand Up @@ -894,75 +884,12 @@ static auto print_execute_systems(
ctx.write("\n");
});

if(lazy_iteration_rate > 0) {
ctx.write(
"// If this assertion triggers that's a ecsact_rt_entt codegen "
"failure\n"
);
ctx.write("assert(iteration_count_ <= lazy_iteration_rate_);\n");
block(ctx, "if(iteration_count_ < lazy_iteration_rate_)", [&] {
ctx.write(
"_recalc_sorting_hash<",
options.system_name,
">(",
options.registry_var_name,
");\n"
);
ctx.write(
options.registry_var_name,
".sort<",
system_sorting_struct_name,
">([](const auto& a, const auto& b) { return a.hash < b.hash; });\n"
);

ecsact::rt_entt_codegen::util::make_view(
ctx,
"view_no_pending_lazy_",
options.registry_var_name,
sys_details
);

ctx.write("auto view_no_pending_lazy_count_ = 0;\n");

block(
ctx,
"for(ecsact::entt::entity_id entity : view_no_pending_lazy_)",
[&] {
ctx.write(
"// If this assertion triggers this is an indicator of a codegen "
"failure.\n"
"// Please report to "
"https://github.com/ecsact-dev/ecsact_rt_entt\n"
);
ctx.write(
"assert(",
options.registry_var_name,
".all_of<",
system_sorting_struct_name,
">(entity));\n"
);
ctx.write("view_no_pending_lazy_count_ += 1;\n");
ctx.write(
options.registry_var_name,
".emplace<",
pending_lazy_exec_struct,
">(entity);\n"
);
}
);

block(
ctx,
"if(view_no_pending_lazy_count_ >= lazy_iteration_rate_)",
[&] { ctx.write("goto ", exec_start_label_name, ";\n"); }
);
});
}
lazy_provider.post_exec_system_impl();

print_apply_pendings(
ctx,
sys_details,
options.get_sys_like_id(),
options.sys_like_id_variant.get_sys_like_id(),
options.registry_var_name
);
}
Expand Down Expand Up @@ -1030,7 +957,7 @@ static auto print_execute_system_template_specialization(
using ecsact::cc_lang_support::cpp_identifier;
using ecsact::cpp_codegen_plugin_util::block;
using ecsact::meta::decl_full_name;
using ecsact::rt_entt_codegen::core::system_like_id_variant_t;
using ecsact::rt_entt_codegen::core::system_like_id_variant;
using ecsact::rt_entt_codegen::system_util::is_trivial_system;

using ecsact::rt_entt_codegen::util::method_printer;
Expand Down Expand Up @@ -1066,7 +993,7 @@ static auto print_execute_system_template_specialization(
details,
sys_details,
{
.sys_like_id = system_id,
.sys_like_id_variant = system_id,
.system_name = system_name,
.registry_var_name = "registry",
.parent_context_var_name = "parent_context",
Expand Down Expand Up @@ -1129,7 +1056,7 @@ static auto print_execute_actions_template_specialization(
details,
sys_details,
{
.sys_like_id = action_id,
.sys_like_id_variant = action_id,
.system_name = cpp_system_name,
.registry_var_name = "registry",
.parent_context_var_name = "nullptr",
Expand Down
8 changes: 5 additions & 3 deletions rt_entt_codegen/core/sys_exec/sys_exec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ auto ecsact::rt_entt_codegen::core::print_child_systems(
using ecsact::meta::get_child_system_ids;
using ecsact::rt_entt_codegen::ecsact_entt_system_details;

auto child_system_ids =
ecsact::meta::get_child_system_ids(options.get_sys_like_id());
auto child_system_ids = ecsact::meta::get_child_system_ids(
options.sys_like_id_variant.get_sys_like_id()
);

std::vector<ecsact_system_like_id> child_system_like_ids{};

Expand All @@ -34,7 +35,8 @@ auto ecsact::rt_entt_codegen::core::print_child_systems(
// TODO(Kelwan): Make use case system agnostic when we support
// nested Action systems
// Issue: https://github.com/ecsact-dev/ecsact_parse/issues/154
for(auto child_sys_id : get_child_system_ids(options.get_sys_like_id())) {
for(auto child_sys_id :
get_child_system_ids(options.sys_like_id_variant.get_sys_like_id())) {
auto child_details = ecsact_entt_system_details::from_system_like(
ecsact_id_cast<ecsact_system_like_id>(child_sys_id)
);
Expand Down
32 changes: 17 additions & 15 deletions rt_entt_codegen/core/sys_exec/sys_exec.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,43 @@

namespace ecsact::rt_entt_codegen::core {

using system_like_id_variant_t =
std::variant<ecsact_system_id, ecsact_action_id>;

struct print_execute_systems_options {
system_like_id_variant_t sys_like_id;
std::string system_name;
std::string registry_var_name;
std::string parent_context_var_name;
/// only set if system is an action
std::optional<std::string> action_var_name;
struct system_like_id_variant
: std::variant<ecsact_system_id, ecsact_action_id> {
using variant::variant;

auto as_system() const -> ecsact_system_id {
return std::get<ecsact_system_id>(sys_like_id);
return std::get<ecsact_system_id>(*this);
}

auto as_action() const -> ecsact_action_id {
return std::get<ecsact_action_id>(sys_like_id);
return std::get<ecsact_action_id>(*this);
}

auto is_system() const -> bool {
return std::holds_alternative<ecsact_system_id>(sys_like_id);
return std::holds_alternative<ecsact_system_id>(*this);
}

auto is_action() const -> bool {
return std::holds_alternative<ecsact_action_id>(sys_like_id);
return std::holds_alternative<ecsact_action_id>(*this);
}

auto get_sys_like_id() const -> ecsact_system_like_id {
return std::visit(
[](auto&& arg) { return static_cast<ecsact_system_like_id>(arg); },
sys_like_id
*this
);
}
};

struct print_execute_systems_options {
system_like_id_variant sys_like_id_variant;
std::string system_name;
std::string registry_var_name;
std::string parent_context_var_name;
/// only set if system is an action
std::optional<std::string> action_var_name;
};

auto print_child_systems(
ecsact::codegen_plugin_context& ctx,
const ecsact::rt_entt_codegen::ecsact_entt_details& details,
Expand Down
54 changes: 54 additions & 0 deletions rt_entt_codegen/core/system_provider/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//bazel:copts.bzl", "copts")

cc_library(
name = "system_provider",
hdrs = ["system_provider.hh"],
copts = copts,
deps = [
"//rt_entt_codegen/shared:ecsact_entt_details",
],
)

cc_library(
name = "association",
srcs = ["association/association.cc"],
hdrs = ["association/association.hh"],
copts = copts,
deps = [
":system_provider",
"//rt_entt_codegen/core/sys_exec",
"//rt_entt_codegen/shared:ecsact_entt_details",
"//rt_entt_codegen/shared:system_util",
],
)

cc_library(
name = "basic",
hdrs = ["basic/basic.hh"],
copts = copts,
)

cc_library(
name = "notify",
hdrs = ["notify/notify.hh"],
copts = copts,
deps = [
":system_provider",
"//rt_entt_codegen/shared:ecsact_entt_details",
],
)

cc_library(
name = "lazy",
srcs = ["lazy/lazy.cc"],
hdrs = ["lazy/lazy.hh"],
copts = copts,
visibility = ["//rt_entt_codegen/core:__pkg__"],
deps = [
":system_provider",
"//rt_entt_codegen/core/sys_exec",
"//rt_entt_codegen/shared:ecsact_entt_details",
"//rt_entt_codegen/shared:sorting",
],
)
Loading

0 comments on commit 61b4548

Please sign in to comment.