GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Task.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_EXECUTION_TASK_HPP
5#define GREM_EXECUTION_TASK_HPP
6
8
15
16#include <utility> // std::move
17
18namespace grem::execution {
19
20#ifdef _MSC_VER
21#pragma warning(push)
22#pragma warning(disable : 4324)
23#endif
30class alignas(64) Task {
31public:
33 using ParallelIndex = uint16_t;
34
37
39 using Function = void (*)(void* context, byte* taskSharedMemory, ParallelIndex parallelIndex, ParallelCount parallelism);
40
43
46
48 using SharedMemoryOffset = uint32_t;
49
51 using SharedMemorySize = uint32_t;
52
55
57 static constexpr size_t MAX_GRAPH_SIZE = size_t{Limits<Count>::MAX};
58
75 Task(Function function, SharedMemoryOffset sharedMemoryOffset, ParallelIndex parallelIndex, ParallelCount parallelism, DependencyIndices dependencyIndices,
76 UniquePointer<char[]> name) noexcept
77 : function(function)
78 , sharedMemoryOffset(sharedMemoryOffset)
79 , parallelIndex(parallelIndex)
80 , parallelism(parallelism)
81 , dependencyIndices(std::move(dependencyIndices))
82 , name(std::move(name)) {}
83
96 void execute(void* context, byte* sharedMemory) const {
97 byte* const taskSharedMemory = sharedMemory + sharedMemoryOffset;
98 function(context, taskSharedMemory, parallelIndex, parallelism);
99 }
100
107 [[nodiscard]] SharedMemoryOffset getSharedMemoryOffset() const noexcept {
108 return sharedMemoryOffset;
109 }
110
116 [[nodiscard]] Span<const GraphIndex> getDependencyIndices() const noexcept {
117 return dependencyIndices;
118 }
119
126 [[nodiscard]] CStringView getName() const noexcept {
127 return (name) ? CStringView{name.get()} : CStringView{};
128 }
129
130private:
131 Function function;
132 SharedMemoryOffset sharedMemoryOffset;
133 ParallelIndex parallelIndex;
134 ParallelCount parallelism;
135 DependencyIndices dependencyIndices;
137};
138#ifdef _MSC_VER
139#pragma warning(pop)
140#endif
141
146public:
148 using Function = void (*)(void* subTaskContext, byte* taskSharedMemory, Task::ParallelIndex subTaskIndex, Task::ParallelCount subTaskCount);
149
156 ParallelTask(Function function) noexcept
157 : function(function) {}
158
174 void execute(void* subTaskContext, byte* taskSharedMemory, Task::ParallelIndex subTaskIndex, Task::ParallelCount subTaskCount) const {
175 function(subTaskContext, taskSharedMemory, subTaskIndex, subTaskCount);
176 }
177
178private:
179 Function function;
180};
181
182} // namespace grem::execution
183
184#endif
Definition SmallArrayList.hpp:26
Definition Span.hpp:146
Nullable RAII data pointer with exclusive ownership of an object that is automatically destroyed on p...
Definition UniquePointer.hpp:77
ParallelTask(Function function) noexcept
Construct a parallel task.
Definition Task.hpp:156
void(*)(void *subTaskContext, byte *taskSharedMemory, Task::ParallelIndex subTaskIndex, Task::ParallelCount subTaskCount) Function
Callback function pointer that executes a chunk of a parallel task.
Definition Task.hpp:148
void execute(void *subTaskContext, byte *taskSharedMemory, Task::ParallelIndex subTaskIndex, Task::ParallelCount subTaskCount) const
Execute a chunk of the parallel task.
Definition Task.hpp:174
uint32_t SharedMemorySize
Size, in bytes, of shared memory allocated for a task.
Definition Task.hpp:51
ParallelIndex ParallelCount
Number of a parallel sub-tasks.
Definition Task.hpp:36
void execute(void *context, byte *sharedMemory) const
Execute the task.
Definition Task.hpp:96
ParallelIndex GraphIndex
Index of a task in a task graph.
Definition Task.hpp:42
uint16_t ParallelIndex
Index of a parallel sub-task.
Definition Task.hpp:33
uint32_t SharedMemoryOffset
Byte offset into the shared memory allocated for a task.
Definition Task.hpp:48
SharedMemoryOffset getSharedMemoryOffset() const noexcept
Get the expected offset of this task's reserved memory range in the total shared task memory allocate...
Definition Task.hpp:107
ParallelCount Count
Number of tasks in a task graph.
Definition Task.hpp:45
SmallArrayList< GraphIndex, 12 > DependencyIndices
List of indices of previous tasks that a task depends on.
Definition Task.hpp:54
void(*)(void *context, byte *taskSharedMemory, ParallelIndex parallelIndex, ParallelCount parallelism) Function
Callback function pointer that executes a task.
Definition Task.hpp:39
CStringView getName() const noexcept
Get the human-readable name of the task.
Definition Task.hpp:126
Span< const GraphIndex > getDependencyIndices() const noexcept
Get the task indices of this task's dependencies in the task graph.
Definition Task.hpp:116
static constexpr size_t MAX_GRAPH_SIZE
The maximum supported number of tasks in a task graph.
Definition Task.hpp:57
Task(Function function, SharedMemoryOffset sharedMemoryOffset, ParallelIndex parallelIndex, ParallelCount parallelism, DependencyIndices dependencyIndices, UniquePointer< char[]> name) noexcept
Construct a task.
Definition Task.hpp:75
Definition Chunk.hpp:19
std::function< Signature > Function
Definition Function.hpp:14
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
static constexpr T MAX
Definition fundamentals.hpp:29