Foundation
Loading...
Searching...
No Matches
JoltCommon.hpp
Go to the documentation of this file.
1#pragma once
2#include <Core/JobSystem.hpp>
3#include <Jolt/Jolt.h>
4#include <Jolt/Core/JobSystemWithBarrier.h>
5#include <cassert>
6
8{
9 class FoundationJoltJobSystem final : public JPH::JobSystemWithBarrier
10 {
11 using JoltJob = JPH::JobSystem::Job;
12
13 struct QueuedJob
14 {
16
17 explicit QueuedJob(JoltJob* value) : job(value) { job->AddRef(); }
18 QueuedJob(QueuedJob const&) = delete;
19 QueuedJob& operator=(QueuedJob const&) = delete;
20 QueuedJob(QueuedJob&& other) noexcept : job(std::exchange(other.job, nullptr)) {}
23 {
24 if (job)
25 job->Release();
26 }
27
28 void operator()() const { job->Execute(); }
29 };
30
32 size_t mMaxJobs;
37
38 JoltJob* JobAt(uint32_t index) const noexcept
39 {
40 auto* memory = static_cast<std::byte*>(mJobArena.arena.memory);
41 return reinterpret_cast<JoltJob*>(memory + static_cast<size_t>(index) * sizeof(JoltJob));
42 }
43
44 public:
45 FoundationJoltJobSystem(Core::JobSystem* jobs, uint32_t maxJobs, uint32_t maxBarriers,
46 Core::Allocator* allocator = GLOBAL_ALLOC) :
47 JPH::JobSystemWithBarrier(maxBarriers),
48 mAllocator(allocator),
49 mMaxJobs(maxJobs),
50 mJobArena(mAllocator, static_cast<size_t>(maxJobs) * sizeof(JoltJob), alignof(JoltJob)),
52 mJobs(jobs)
53 {
54 CHECK(mJobs != nullptr);
56 mFreelist.reserve(maxJobs);
57 for (uint32_t i = maxJobs; i-- > 0;)
58 mFreelist.push_back(i);
59 }
60
61 ~FoundationJoltJobSystem() override { assert(mFreelist.size() == mMaxJobs); }
62
63 int GetMaxConcurrency() const override { return static_cast<int>(mJobs->GetMaxConcurrency()); }
64
65 JPH::JobHandle CreateJob(char const* name, JPH::ColorArg color, JobFunction const& function,
66 JPH::uint32 dependencyCount = 0) override
67 {
68 uint32_t index;
69 {
70 std::lock_guard lock(mFreelistMutex);
71 CHECK(!mFreelist.empty());
72 index = mFreelist.back();
73 mFreelist.pop_back();
74 }
75
76 JoltJob* job = std::construct_at(JobAt(index), name, color, this, function, dependencyCount);
77 JPH::JobHandle handle(job);
78 if (dependencyCount == 0)
79 QueueJob(job);
80 return handle;
81 }
82
83 void WaitForJobs(Barrier* barrier) override
84 {
85 JPH::JobSystemWithBarrier::WaitForJobs(barrier);
86 }
87
88 protected:
89 void QueueJob(JoltJob* job) override
90 {
91 char const* name = "Jolt";
92#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
93 name = job->GetName();
94#endif
96 }
97
98 void QueueJobs(JoltJob** jobs, JPH::uint count) override
99 {
100 for (JPH::uint i = 0; i < count; ++i)
101 QueueJob(jobs[i]);
102 }
103
104 void FreeJob(JoltJob* job) override
105 {
106 auto* begin = static_cast<std::byte*>(mJobArena.arena.memory);
107 auto* pointer = reinterpret_cast<std::byte*>(job);
108 size_t const byteOffset = static_cast<size_t>(pointer - begin);
109 uint32_t const index = static_cast<uint32_t>(byteOffset / sizeof(JoltJob));
110 job->~JoltJob();
111 std::lock_guard lock(mFreelistMutex);
112 mFreelist.push_back(index);
113 }
114 };
115} // namespace Foundation::Examples
#define GLOBAL_ALLOC
Definition Allocator.hpp:240
#define CHECK(expr)
Definition Logging.hpp:92
Allocator interface (noexcept)
Definition Allocator.hpp:29
Definition JobSystem.hpp:135
size_t GetMaxConcurrency() const noexcept
Definition JobSystem.hpp:184
JobHandle CreateJob(StringView name, JobPriority priority, Fn &&function, uint32_t dependencyCount=0)
Definition JobSystem.hpp:187
size_t mMaxJobs
Definition JoltCommon.hpp:32
void WaitForJobs(Barrier *barrier) override
Definition JoltCommon.hpp:83
JoltJob * JobAt(uint32_t index) const noexcept
Definition JoltCommon.hpp:38
Core::Vector< uint32_t > mFreelist
Definition JoltCommon.hpp:34
Core::ScopedArena mJobArena
Definition JoltCommon.hpp:33
void QueueJob(JoltJob *job) override
Definition JoltCommon.hpp:89
int GetMaxConcurrency() const override
Definition JoltCommon.hpp:63
void FreeJob(JoltJob *job) override
Definition JoltCommon.hpp:104
~FoundationJoltJobSystem() override
Definition JoltCommon.hpp:61
Core::Mutex mFreelistMutex
Definition JoltCommon.hpp:35
void QueueJobs(JoltJob **jobs, JPH::uint count) override
Definition JoltCommon.hpp:98
Core::JobSystem * mJobs
Definition JoltCommon.hpp:36
FoundationJoltJobSystem(Core::JobSystem *jobs, uint32_t maxJobs, uint32_t maxBarriers, Core::Allocator *allocator=GLOBAL_ALLOC)
Definition JoltCommon.hpp:45
JPH::JobHandle CreateJob(char const *name, JPH::ColorArg color, JobFunction const &function, JPH::uint32 dependencyCount=0) override
Definition JoltCommon.hpp:65
Core::Allocator * mAllocator
Definition JoltCommon.hpp:31
JPH::JobSystem::Job JoltJob
Definition JoltCommon.hpp:11
std::vector< T, StlAllocator< T > > Vector
std::vector with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:149
std::mutex Mutex
Definition Thread.hpp:10
Definition JoltCommon.hpp:8
pointer memory
Definition Allocator.hpp:22
RAII wrapper for an arena allocated from an Allocator.
Definition Allocator.hpp:49
Arena arena
Definition Allocator.hpp:51
QueuedJob(QueuedJob &&other) noexcept
Definition JoltCommon.hpp:20
QueuedJob(JoltJob *value)
Definition JoltCommon.hpp:17
JoltJob * job
Definition JoltCommon.hpp:15
QueuedJob & operator=(QueuedJob const &)=delete
void operator()() const
Definition JoltCommon.hpp:28