Skip to content

Commit da314e4

Browse files
committed
feat: entities running parallel on systems
1 parent d324920 commit da314e4

File tree

16 files changed

+346
-84
lines changed

16 files changed

+346
-84
lines changed

rt_entt_codegen/core/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ _CORE_CODEGEN_METHODS = {
3131
"//rt_entt_codegen/core/system_provider:association",
3232
"//rt_entt_codegen/core/system_provider:notify",
3333
"//rt_entt_codegen/core/system_provider:basic",
34+
"//rt_entt_codegen/core/system_provider:parallel",
3435
"//rt_entt_codegen/core/system_provider",
3536
"@entt//:entt",
3637
"@ecsact_rt_entt//:lib",

rt_entt_codegen/core/print_sys_exec.cc

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
#include "rt_entt_codegen/shared/comps_with_caps.hh"
1717
#include "rt_entt_codegen/shared/system_util.hh"
1818
#include "rt_entt_codegen/core/sys_exec/sys_exec.hh"
19+
#include "rt_entt_codegen/shared/parallel.hh"
1920
#include "system_provider/system_provider.hh"
2021
#include "rt_entt_codegen/core/system_provider/lazy/lazy.hh"
2122
#include "system_provider/lazy/lazy.hh"
2223
#include "system_provider/association/association.hh"
2324
#include "system_provider/notify/notify.hh"
2425
#include "system_provider/basic/basic.hh"
26+
#include "system_provider/parallel/parallel.hh"
2527

2628
using capability_t =
2729
std::unordered_map<ecsact_component_like_id, ecsact_system_capability>;
@@ -285,9 +287,15 @@ static auto print_system_execution_context(
285287
system_like_id_variant sys_like_id,
286288
const common_vars names,
287289
system_provider_t system_providers
288-
) -> void {
290+
) -> std::string {
289291
auto system_name = cpp_identifier(decl_full_name(sys_like_id));
290292

293+
auto context_name =
294+
ecsact::rt_entt_codegen::system_util::create_context_var_name(sys_like_id);
295+
296+
auto context_header =
297+
std::format("struct {}: ecsact_system_execution_context ", context_name);
298+
291299
block(ctx, "struct : ecsact_system_execution_context ", [&] {
292300
ctx.write("view_t* view;\n");
293301

@@ -320,6 +328,8 @@ static auto print_system_execution_context(
320328
);
321329
ctx.write("context.parent_ctx = ", names.parent_context_var_name, ";\n");
322330
ctx.write("context.view = &view;\n\n");
331+
332+
return context_name;
323333
}
324334

325335
static auto setup_system_providers(system_like_id_variant sys_like_id
@@ -328,6 +338,8 @@ static auto setup_system_providers(system_like_id_variant sys_like_id
328338
using ecsact::rt_entt_codegen::core::provider::basic;
329339
using ecsact::rt_entt_codegen::core::provider::lazy;
330340
using ecsact::rt_entt_codegen::core::provider::notify;
341+
using ecsact::rt_entt_codegen::core::provider::parallel;
342+
using ecsact::rt_entt_codegen::parallel::can_entities_parallel;
331343
using ecsact::rt_entt_codegen::system_util::is_notify_system;
332344

333345
assert(sys_like_id != system_like_id_variant{});
@@ -355,8 +367,16 @@ static auto setup_system_providers(system_like_id_variant sys_like_id
355367
system_providers.push_back(std::make_shared<association>(sys_like_id));
356368
}
357369

370+
if(can_entities_parallel(sys_like_id)) {
371+
system_providers.push_back(std::make_shared<parallel>(sys_like_id));
372+
}
373+
358374
system_providers.push_back(std::make_shared<basic>(sys_like_id));
359375

376+
// NOTE: The basic provider must always be last. It's the only
377+
// guaranteed provider and has fallbacks for exclusive functions
378+
assert(dynamic_cast<basic*>(system_providers.back().get()) != nullptr);
379+
360380
return system_providers;
361381
}
362382

@@ -389,7 +409,8 @@ static auto print_execute_systems(
389409

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

392-
print_system_execution_context(ctx, sys_like_id, names, system_providers);
412+
auto context_name =
413+
print_system_execution_context(ctx, sys_like_id, names, system_providers);
393414

394415
for(const auto& provider : system_providers) {
395416
provider->after_make_view_or_group(ctx, names);
@@ -399,30 +420,39 @@ static auto print_execute_systems(
399420
provider->pre_entity_iteration(ctx, names);
400421
}
401422

402-
block(ctx, "for(ecsact::entt::entity_id entity : view)", [&] {
403-
ctx.write("context.entity = entity;\n");
423+
for(auto provider : system_providers) {
424+
auto result = provider->entity_iteration(ctx, names, [&] {
425+
ctx.write("context.entity = entity;\n");
404426

405-
ecsact::rt_entt_codegen::core::print_child_systems(ctx, names, sys_like_id);
427+
ecsact::rt_entt_codegen::core::print_child_systems(
428+
ctx,
429+
names,
430+
sys_like_id
431+
);
406432

407-
for(const auto& provider : system_providers) {
408-
provider->pre_exec_system_impl(ctx, names);
409-
}
433+
for(const auto& provider : system_providers) {
434+
provider->pre_exec_system_impl(ctx, names);
435+
}
410436

411-
auto result = std::ranges::find_if(system_providers, [&](auto provider) {
412-
return provider->system_impl(ctx, names) ==
413-
handle_exclusive_provide::HANDLED;
414-
});
437+
auto result = std::ranges::find_if(system_providers, [&](auto provider) {
438+
return provider->system_impl(ctx, names) ==
439+
handle_exclusive_provide::HANDLED;
440+
});
415441

416-
if(result == system_providers.end()) {
417-
throw std::logic_error("system_impl was not handled by providers");
418-
}
442+
if(result == system_providers.end()) {
443+
throw std::logic_error("system_impl was not handled by providers");
444+
}
419445

420-
for(const auto& provider : system_providers) {
421-
provider->post_exec_system_impl(ctx, names);
422-
}
446+
for(const auto& provider : system_providers) {
447+
provider->post_exec_system_impl(ctx, names);
448+
}
423449

424-
ctx.write("\n");
425-
});
450+
ctx.write("\n");
451+
});
452+
if(result == handle_exclusive_provide::HANDLED) {
453+
break;
454+
}
455+
}
426456

427457
for(const auto& provider : system_providers) {
428458
provider->post_iteration(ctx, names);

rt_entt_codegen/core/system_provider/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,18 @@ cc_library(
8383
"//rt_entt_codegen/shared:system_variant",
8484
],
8585
)
86+
87+
cc_library(
88+
name = "parallel",
89+
srcs = ["parallel/parallel.cc"],
90+
hdrs = ["parallel/parallel.hh"],
91+
copts = copts,
92+
visibility = ["//rt_entt_codegen/core:__pkg__"],
93+
deps = [
94+
":system_provider",
95+
"//rt_entt_codegen/core/sys_exec",
96+
"//rt_entt_codegen/shared:ecsact_entt_details",
97+
"//rt_entt_codegen/shared:parallel",
98+
"//rt_entt_codegen/shared:system_variant",
99+
],
100+
)

rt_entt_codegen/core/system_provider/basic/basic.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "basic.hh"
22

33
#include "ecsact/runtime/meta.hh"
4+
#include "ecsact/cpp_codegen_plugin_util.hh"
45
#include "rt_entt_codegen/core/system_provider/system_ctx_functions.hh"
56

67
using namespace ecsact::rt_entt_codegen::core;
@@ -106,3 +107,16 @@ auto provider::basic::system_impl(
106107
ctx.write("system_impl(&context);\n");
107108
return HANDLED;
108109
}
110+
111+
auto provider::basic::entity_iteration(
112+
ecsact::codegen_plugin_context& ctx,
113+
const ecsact::rt_entt_codegen::core::common_vars& names,
114+
std::function<void()> iter_func
115+
) -> handle_exclusive_provide {
116+
using ecsact::cpp_codegen_plugin_util::block;
117+
118+
block(ctx, "for(ecsact::entt::entity_id entity : view)", [&] {
119+
iter_func();
120+
});
121+
return HANDLED;
122+
}

rt_entt_codegen/core/system_provider/basic/basic.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public:
6666
const common_vars& names
6767
) -> handle_exclusive_provide final;
6868

69+
auto entity_iteration(
70+
ecsact::codegen_plugin_context& ctx,
71+
const ecsact::rt_entt_codegen::core::common_vars& names,
72+
std::function<void()> iter_func
73+
) -> handle_exclusive_provide;
74+
6975
auto system_impl(
7076
ecsact::codegen_plugin_context& ctx,
7177
const common_vars& names
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "parallel.hh"
2+
3+
#include "ecsact/cpp_codegen_plugin_util.hh"
4+
5+
using namespace ecsact::rt_entt_codegen::core;
6+
7+
auto provider::parallel::entity_iteration(
8+
ecsact::codegen_plugin_context& ctx,
9+
const ecsact::rt_entt_codegen::core::common_vars& names,
10+
std::function<void()> iter_func
11+
) -> handle_exclusive_provide {
12+
using ecsact::cpp_codegen_plugin_util::block;
13+
14+
block(
15+
ctx,
16+
"std::for_each(std::execution::par_unseq, view.begin(), "
17+
"view.end(), [&](auto entity)",
18+
[&] { iter_func(); }
19+
);
20+
ctx.write(");\n");
21+
return HANDLED;
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "rt_entt_codegen/core/system_provider/system_provider.hh"
4+
#include "rt_entt_codegen/core/sys_exec/sys_exec.hh"
5+
6+
namespace ecsact::rt_entt_codegen::core::provider {
7+
class parallel final : public system_provider {
8+
public:
9+
using system_provider::system_provider;
10+
11+
auto entity_iteration(
12+
ecsact::codegen_plugin_context& ctx,
13+
const ecsact::rt_entt_codegen::core::common_vars& names,
14+
std::function<void()> iter_func
15+
) -> handle_exclusive_provide;
16+
};
17+
} // namespace ecsact::rt_entt_codegen::core::provider

rt_entt_codegen/core/system_provider/system_provider.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ auto system_provider::pre_entity_iteration(
105105
) -> void {
106106
}
107107

108+
auto system_provider::entity_iteration(
109+
ecsact::codegen_plugin_context& ctx,
110+
const ecsact::rt_entt_codegen::core::common_vars& names,
111+
std::function<void()> iter_func
112+
) -> handle_exclusive_provide {
113+
return NOT_HANDLED;
114+
}
115+
108116
auto system_provider::pre_exec_system_impl(
109117
ecsact::codegen_plugin_context& ctx,
110118
const common_vars& names

rt_entt_codegen/core/system_provider/system_provider.hh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <vector>
44
#include <string>
5-
#include <variant>
5+
#include <functional>
66

77
#include "rt_entt_codegen/core/sys_exec/sys_exec.hh"
88
#include "rt_entt_codegen/shared/system_variant.hh"
@@ -80,6 +80,11 @@ public:
8080
ecsact::codegen_plugin_context& ctx,
8181
const ecsact::rt_entt_codegen::core::common_vars& names
8282
) -> void;
83+
[[nodiscard]] virtual auto entity_iteration(
84+
ecsact::codegen_plugin_context& ctx,
85+
const ecsact::rt_entt_codegen::core::common_vars& names,
86+
std::function<void()> iter_func
87+
) -> handle_exclusive_provide;
8388
virtual auto pre_exec_system_impl(
8489
ecsact::codegen_plugin_context& ctx,
8590
const ecsact::rt_entt_codegen::core::common_vars& names

0 commit comments

Comments
 (0)