Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
RenderThread.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include <cstdint>
6#include <functional>
7
8#ifdef ES_RENDER_THREADS
9#include <condition_variable>
10#include <deque>
11#include <mutex>
12#include <optional>
13#include <thread>
14#include <type_traits>
15#ifdef __EMSCRIPTEN__
16#include <emscripten/proxying.h>
17#endif
18#endif
19
20namespace esengine {
21
22// Single game-side producer. A fence completes after the command returns on the
23// render CPU, independently of when the graphics driver finishes the GPU work.
25public:
26 using Fence = std::uint64_t;
27#ifdef ES_RENDER_THREADS
28 RenderThread() = default;
29 RenderThread(const RenderThread&) = delete;
30 RenderThread& operator=(const RenderThread&) = delete;
32 bool start(const char* canvas = nullptr);
33 void stop();
34 bool enabled() const { return started_; }
35 bool isOwner() const { return current_ == this; }
36 Fence enqueue(std::function<void()> work);
37 Fence fence();
38 bool complete(Fence value) const;
39 void wait(Fence value);
40 void finishFrame(std::function<void()> end);
41 void flush();
42
43 // Borrowed arguments and return storage stay with the blocked caller.
44 template<class F> auto invoke(F&& fn) -> std::invoke_result_t<F> {
45 using Result = std::invoke_result_t<F>;
46 if (!enabled() || isOwner()) return fn();
47 if constexpr (std::is_void_v<Result>) {
48 invokeChecked([&fn] { fn(); });
49 } else {
50 std::optional<Result> result;
51 invokeChecked([&] { result.emplace(fn()); });
52 return std::move(*result);
53 }
54 }
55
56private:
57 struct Command { Fence serial; std::function<void()> execute; };
58 static thread_local RenderThread* current_;
59 mutable std::mutex mutex_;
60 std::condition_variable changed_;
61 std::deque<Command> pending_;
62 Fence submitted_ = 0;
63 Fence completed_ = 0;
64 Fence frames_[2]{};
65 unsigned frameSlot_ = 0;
66 bool started_ = false;
67 bool stopping_ = false;
68 void run(Command command);
69 void invokeChecked(std::function<void()> work);
70#ifdef __EMSCRIPTEN__
71 pthread_t worker_{};
72 em_proxying_queue* proxy_ = nullptr;
73 static void* webEntry(void* arg);
74 static void drainWeb(void* arg);
75#else
76 std::thread worker_;
77#endif
78#else
79 bool start(const char* = nullptr) { return false; }
80 void stop() {}
81 bool enabled() const { return false; }
82 bool isOwner() const { return false; }
83 Fence enqueue(std::function<void()> fn) { fn(); return 0; }
84 Fence fence() { return 0; }
85 bool complete(Fence value) const { return value == 0; }
86 void wait(Fence) {}
87 void finishFrame(std::function<void()> fn) { fn(); }
88 void flush() {}
89 template<class F> decltype(auto) invoke(F&& fn) { return fn(); }
90#endif
91};
92
93} // namespace esengine
decltype(auto) invoke(F &&fn)
bool complete(Fence value) const
Fence enqueue(std::function< void()> fn)
void finishFrame(std::function< void()> fn)
bool start(const char *=nullptr)
std::span< const EsCmd > run(SystemFn fn, std::span< const std::uint32_t > fallback, std::span< const QuerySpec > queries, std::span< void *const > resources, CallArena &arena, WorldStamp world={})
Definition AotHost.hpp:181