|
1 | 1 | #include "core.hh"
|
2 | 2 |
|
| 3 | +#include <set> |
| 4 | + |
3 | 5 | #include "ecsact/lang-support/lang-cc.hh"
|
4 | 6 | #include "rt_entt_codegen/shared/util.hh"
|
5 | 7 | #include "ecsact/cpp_codegen_plugin_util.hh"
|
6 | 8 |
|
| 9 | +#include <iostream> |
| 10 | + |
7 | 11 | constexpr auto METHOD_BODY_TOP = R"(
|
8 | 12 | auto& reg = ecsact::entt::get_registry(registry_id);
|
9 | 13 | auto actions = ecsact::entt::actions_map{};
|
10 | 14 | )";
|
11 | 15 |
|
12 |
| -auto determine_parallel_execution( |
| 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( |
13 | 97 | ecsact::codegen_plugin_context& ctx,
|
14 | 98 | const ecsact::rt_entt_codegen::ecsact_entt_details& details
|
15 | 99 | ) -> void {
|
16 |
| - for(auto sys_like : details.top_execution_order) { |
| 100 | + using ecsact::cpp_codegen_plugin_util::block; |
| 101 | + |
| 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 |
| 114 | + ); |
| 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 | + }); |
17 | 158 | }
|
18 | 159 | }
|
19 | 160 |
|
@@ -53,27 +194,7 @@ auto ecsact::rt_entt_codegen::core::print_execute_systems( //
|
53 | 194 | });
|
54 | 195 | });
|
55 | 196 |
|
56 |
| - for(auto sys_like : details.top_execution_order) { |
57 |
| - auto cpp_decl_name = cpp_identifier(ecsact::meta::decl_full_name(sys_like)); |
58 |
| - |
59 |
| - if(details.is_action(sys_like)) { |
60 |
| - ctx.write( |
61 |
| - "ecsact::entt::execute_actions<", |
62 |
| - cpp_decl_name, |
63 |
| - ">(reg, actions.as_action_span<", |
64 |
| - cpp_decl_name, |
65 |
| - ">());\n" |
66 |
| - ); |
67 |
| - } else if(details.is_system(sys_like)) { |
68 |
| - ctx.write( |
69 |
| - "ecsact::entt::execute_system<", |
70 |
| - cpp_decl_name, |
71 |
| - ">(reg, nullptr);\n" |
72 |
| - ); |
73 |
| - } else { |
74 |
| - ctx.write("// ??? unhandled ??? ", cpp_decl_name, "\n"); |
75 |
| - } |
76 |
| - } |
| 197 | + print_parallel_execution(ctx, details); |
77 | 198 |
|
78 | 199 | ctx.write("\nupdate_all_beforechange_storage(registry_id);\n");
|
79 | 200 | ctx.write("cleanup_system_notifies(registry_id);\n");
|
|
0 commit comments