Skip to content

Commit 336023f

Browse files
committed
Other context added, lots of cleanup. Testing on example
1 parent b8c0888 commit 336023f

File tree

13 files changed

+362
-193
lines changed

13 files changed

+362
-193
lines changed

ecsact/entt/execution.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct actions_map {
6868
}
6969

7070
template<typename Action>
71-
auto as_action_span() const -> std::span<const Action*> {
71+
auto as_action_span() const -> const std::span<const Action* const> {
7272
ecsact_action_id action_id = Action::id;
7373

7474
if(!raw_value.contains(action_id)) {
@@ -78,8 +78,8 @@ struct actions_map {
7878
auto& action_data_list = raw_value.at(action_id);
7979
auto action_list_data_ptr = action_data_list.data();
8080

81-
return std::span<const Action*>{
82-
reinterpret_cast<const Action**>(action_list_data_ptr),
81+
return std::span<const Action* const>{
82+
reinterpret_cast<const Action* const*>(action_list_data_ptr),
8383
action_data_list.size(),
8484
};
8585
}

ecsact/entt/wrapper/core.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ inline auto prepare_component(ecsact_registry_id registry_id) -> void {
411411
}
412412
}
413413

414+
template<typename S>
415+
inline auto prepare_system(ecsact_registry_id registry_id) -> void {
416+
using namespace ecsact::entt::detail;
417+
auto& reg = ecsact::entt::get_registry(registry_id);
418+
419+
reg.template storage<system_sorted<S>>();
420+
reg.template storage<pending_lazy_execution<S>>();
421+
}
422+
414423
template<typename C, typename V>
415424
auto has_component_changed(entt::entity_id entity, V& view) -> bool {
416425
using detail::exec_itr_beforechange_storage;

rt_entt_codegen/core/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ cc_library(
1111

1212
# keep sorted
1313
_CORE_CODEGEN_METHODS = {
14-
"execute_systems": [],
14+
"execute_systems": [
15+
"//rt_entt_codegen/shared:parallel",
16+
],
1517
"create_registry": [],
1618
"entity_matches": [
1719
"//rt_entt_codegen/shared:sorting",
@@ -22,6 +24,7 @@ _CORE_CODEGEN_METHODS = {
2224
"//rt_entt_codegen/shared:comps_with_caps",
2325
"//rt_entt_codegen/shared:sorting",
2426
"//rt_entt_codegen/shared:system_util",
27+
"//rt_entt_codegen/shared:parallel",
2528
"@entt//:entt",
2629
"@ecsact_rt_entt//:lib",
2730
],

rt_entt_codegen/core/check_error.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static auto print_check_action_error_template_specialization(
116116
auto printer = //
117117
method_printer{ctx, method_name}
118118
.parameter("::entt::registry&", "registry")
119-
.parameter("const ecsact::entt::actions_map &", "actions")
119+
.parameter(cpp_action_ident + " const&", "action")
120120
.return_type("ecsact_execute_systems_error");
121121

122122
ctx.write("auto err = ECSACT_EXEC_SYS_OK;\n");

rt_entt_codegen/core/core.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace ecsact::rt_entt_codegen::core {
88

9+
auto print_parallel_system_execute(
10+
codegen_plugin_context& ctx,
11+
const ecsact_entt_details& details
12+
) -> void;
13+
914
auto print_execute_systems( //
1015
codegen_plugin_context& ctx,
1116
const ecsact_entt_details& details

rt_entt_codegen/core/execute_systems.cc

Lines changed: 31 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,42 @@
11
#include "core.hh"
22

3-
#include <set>
4-
3+
#include "rt_entt_codegen/shared/parallel.hh"
54
#include "ecsact/lang-support/lang-cc.hh"
65
#include "rt_entt_codegen/shared/util.hh"
76
#include "ecsact/cpp_codegen_plugin_util.hh"
87

98
#include <iostream>
109

1110
constexpr auto METHOD_BODY_TOP = R"(
12-
auto& reg = ecsact::entt::get_registry(registry_id);
13-
auto actions = ecsact::entt::actions_map{};
11+
auto& registry = ecsact::entt::get_registry(registry_id);
12+
auto actions_map = ecsact::entt::actions_map{};
1413
)";
1514

16-
auto is_capability_safe(ecsact_system_capability capability) -> bool {
17-
// List of components capabilities that can't be executed in parallel
18-
if(capability == ECSACT_SYS_CAP_ADDS ||
19-
capability == ECSACT_SYS_CAP_REMOVES ||
20-
capability == ECSACT_SYS_CAP_READWRITE ||
21-
capability == ECSACT_SYS_CAP_WRITEONLY ||
22-
capability == ECSACT_SYS_CAP_OPTIONAL_WRITEONLY ||
23-
capability == ECSACT_SYS_CAP_OPTIONAL_READWRITE) {
24-
return false;
25-
}
26-
return true;
27-
}
28-
29-
auto manage_component_safety(
30-
const ecsact_component_id unsafe_comp_id,
31-
std::set<ecsact_component_id>& unsafe_comps
32-
) -> void {
33-
if(!unsafe_comps.contains(unsafe_comp_id)) {
34-
unsafe_comps.insert(unsafe_comp_id);
35-
return;
36-
}
37-
}
38-
39-
auto loop_iterator(
40-
std::vector<ecsact_system_like_id>& system_list,
41-
const std::vector<ecsact_system_like_id>::iterator begin,
42-
std::vector<std::vector<ecsact_system_like_id>>& parallel_system_cluster
43-
) -> void {
44-
std::cout << "loop iterator run" << std::endl;
45-
std::vector<ecsact_system_like_id> parallel_system_list;
46-
auto unsafe_comps = std::set<ecsact_component_like_id>{};
47-
48-
for(auto iterator = begin; iterator != system_list.end(); iterator++) {
49-
auto sys_like_id = *iterator;
50-
auto capabilities = ecsact::meta::system_capabilities(sys_like_id);
51-
52-
for(const auto [comp_id, capability] : capabilities) {
53-
if(!is_capability_safe(capability)) {
54-
if(!unsafe_comps.contains(comp_id)) {
55-
std::cout << "Unsafe comp inserted" << std::endl;
56-
unsafe_comps.insert(comp_id);
57-
} else {
58-
// We already have it. Uh-oh!
59-
std::cout << "Conflict found" << std::endl;
60-
parallel_system_cluster.push_back(parallel_system_list);
61-
parallel_system_list.clear();
62-
unsafe_comps.clear();
63-
loop_iterator(system_list, iterator, parallel_system_cluster);
64-
}
65-
}
66-
}
67-
68-
// Okay it's all good, check has passed. Now let's check the child systems
69-
auto child_systems = ecsact::meta::get_child_system_ids(sys_like_id);
70-
71-
for(const auto child_sys_id : child_systems) {
72-
auto child_capabilities = ecsact::meta::system_capabilities(child_sys_id);
73-
for(const auto [child_comp_id, child_capability] : child_capabilities) {
74-
if(!is_capability_safe(child_capability)) {
75-
if(!unsafe_comps.contains(child_comp_id)) {
76-
std::cout << "Unsafe comp inserted in child" << std::endl;
77-
unsafe_comps.insert(child_comp_id);
78-
} else {
79-
// We already have it. Uh-oh!
80-
std::cout << "Conflict found in child" << std::endl;
81-
parallel_system_cluster.push_back(parallel_system_list);
82-
parallel_system_list.clear();
83-
unsafe_comps.clear();
84-
loop_iterator(system_list, iterator, parallel_system_cluster);
85-
}
86-
}
87-
}
88-
}
89-
90-
std::cout << "System added to list" << std::endl;
91-
parallel_system_list.push_back(sys_like_id);
92-
}
93-
parallel_system_cluster.push_back(parallel_system_list);
94-
}
95-
96-
auto print_parallel_execution(
97-
ecsact::codegen_plugin_context& ctx,
98-
const ecsact::rt_entt_codegen::ecsact_entt_details& details
15+
auto ecsact::rt_entt_codegen::core::print_parallel_system_execute(
16+
codegen_plugin_context& ctx,
17+
const ecsact_entt_details& details
9918
) -> void {
19+
using ecsact::cc_lang_support::cpp_identifier;
10020
using ecsact::cpp_codegen_plugin_util::block;
21+
using ecsact::rt_entt_codegen::util::method_printer;
10122

102-
auto parallel_system_cluster =
103-
std::vector<std::vector<ecsact_system_like_id>>{};
104-
105-
auto top_systems_copy = std::vector<ecsact_system_like_id>{
106-
details.top_execution_order.begin(),
107-
details.top_execution_order.end()
108-
};
109-
110-
loop_iterator(
111-
top_systems_copy,
112-
top_systems_copy.begin(),
113-
parallel_system_cluster
23+
auto printer = //
24+
method_printer{ctx, "ecsact_execute_parallel_cluster"} //
25+
.parameter("::entt::registry&", "registry")
26+
.parameter("ecsact_system_execution_context*", "parent_context")
27+
.parameter("auto", "system_arr")
28+
.return_type("void");
29+
30+
ctx.write(
31+
"std::for_each(std::execution::par_unseq\n, system_arr.begin()\n, "
32+
"system_arr.end()\n, "
11433
);
115-
116-
std::cout << "post loop iterator" << std::endl;
117-
118-
for(const auto& systems_to_parallel : parallel_system_cluster) {
119-
std::cout << "Parallel system cluster" << std::endl;
120-
using ecsact::cc_lang_support::cpp_identifier;
121-
block(ctx, "", [&] {
122-
block(ctx, "auto system_bank = std::array\n", [&] {
123-
for(const auto system_like_id : systems_to_parallel) {
124-
std::cout << "Systems to parallel" << std::endl;
125-
auto cpp_decl_name =
126-
cpp_identifier(ecsact::meta::decl_full_name(system_like_id));
127-
128-
if(details.is_action(system_like_id)) {
129-
ctx.write(
130-
"std::pair{&ecsact::entt::execute_actions<",
131-
cpp_decl_name,
132-
">, actions},\n"
133-
);
134-
} else if(details.is_system(system_like_id)) {
135-
ctx.write(
136-
"std::pair{&ecsact::entt::execute_system<",
137-
cpp_decl_name,
138-
">, actions},\n"
139-
);
140-
} else {
141-
ctx.write("// ??? unhandled ??? ", cpp_decl_name, "\n");
142-
}
143-
}
144-
});
145-
ctx.write(";\n");
146-
147-
ctx.write(
148-
"std::for_each(std::execution::par_unseq\n, system_bank.begin()\n, "
149-
"system_bank.end()\n, "
150-
);
151-
block(ctx, "[&](auto pair)", [&]() {
152-
ctx.write("auto fn_ptr = pair.first;\n");
153-
ctx.write("auto actions_map = pair.second;\n");
154-
ctx.write("fn_ptr(reg, actions_map);");
155-
});
156-
ctx.write(");\n");
157-
});
158-
}
34+
block(ctx, "[&](auto pair)", [&]() {
35+
ctx.write("auto fn_ptr = pair.first;\n");
36+
ctx.write("auto actions_map = pair.second;\n");
37+
ctx.write("fn_ptr(registry, nullptr, actions_map);");
38+
});
39+
ctx.write(");\n");
15940
}
16041

16142
auto ecsact::rt_entt_codegen::core::print_execute_systems( //
@@ -182,7 +63,8 @@ auto ecsact::rt_entt_codegen::core::print_execute_systems( //
18263
ctx.indentation += 1;
18364
ctx.write("\n");
18465

185-
ctx.write("actions.collect(i, execution_count, execution_options_list);\n");
66+
ctx.write("actions_map.collect(i, execution_count, execution_options_list);\n"
67+
);
18668

18769
block(ctx, "if(execution_options_list != nullptr)", [&] {
18870
ctx.write(
@@ -194,7 +76,11 @@ auto ecsact::rt_entt_codegen::core::print_execute_systems( //
19476
});
19577
});
19678

197-
print_parallel_execution(ctx, details);
79+
ecsact::rt_entt_codegen::parallel::print_parallel_system_executions(
80+
ctx,
81+
details,
82+
details.top_execution_order
83+
);
19884

19985
ctx.write("\nupdate_all_beforechange_storage(registry_id);\n");
20086
ctx.write("cleanup_system_notifies(registry_id);\n");

rt_entt_codegen/core/init_registry_storage.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ auto ecsact::rt_entt_codegen::core::print_init_registry_storage(
1919
for(auto comp_id : details.all_components) {
2020
auto cpp_comp_name = cpp_identifier(decl_full_name(comp_id));
2121

22-
ctx.write(
23-
"ecsact::entt::wrapper::core::prepare_component<",
24-
cpp_comp_name,
25-
">(registry_id)"
26-
);
27-
ctx.write(";\n");
22+
ctx.write(std::format(
23+
"ecsact::entt::wrapper::core::prepare_component<{}>(registry_id);\n",
24+
cpp_comp_name
25+
));
26+
}
27+
28+
for(auto system_id : details.all_systems) {
29+
auto cpp_sys_name = cpp_identifier(decl_full_name(system_id));
30+
31+
ctx.write(std::format(
32+
"ecsact::entt::wrapper::core::prepare_system<{}>(registry_id);\n",
33+
cpp_sys_name
34+
));
2835
}
2936
}

0 commit comments

Comments
 (0)