UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
ICommandList.h
1#pragma once
2#include <UnCompute/Backend/BaseTypes.h>
3#include <UnCompute/Backend/IDeviceObject.h>
4#include <UnCompute/Memory/Ptr.h>
5
6namespace UN
7{
9 enum class CommandListState
10 {
11 Initial,
12 Recording,
13 Executable,
14 Pending,
15 Invalid
16 };
17
19 inline const char* CommandListStateToString(CommandListState state)
20 {
21 switch (state)
22 {
23 // clang-format off
24 case CommandListState::Initial: return "CommandListState::Initial";
25 case CommandListState::Recording: return "CommandListState::Recording";
26 case CommandListState::Executable: return "CommandListState::Executable";
27 case CommandListState::Pending: return "CommandListState::Pending";
28 case CommandListState::Invalid: return "CommandListState::Invalid";
29 // clang-format on
30 default:
31 assert(false && "CommandListState was unknown");
32 return "CommandListState::<Unknown>";
33 }
34 }
35
37 enum class CommandListFlags
38 {
39 None = 0,
40 OneTimeSubmit = UN_BIT(1)
41 };
42
43 UN_ENUM_OPERATORS(CommandListFlags);
44
47 {
48 const char* Name = nullptr;
49 HardwareQueueKindFlags QueueKindFlags = HardwareQueueKindFlags::Compute;
50 CommandListFlags Flags = CommandListFlags::None;
51
52 inline CommandListDesc() = default;
53
54 inline CommandListDesc(const char* name, HardwareQueueKindFlags queueKindFlags,
55 CommandListFlags flags = CommandListFlags::None)
56 : Name(name)
57 , QueueKindFlags(queueKindFlags)
58 , Flags(flags)
59 {
60 }
61 };
62
65 {
66 UInt64 Size = 0;
67 UInt32 SourceOffset = 0;
68 UInt32 DestOffset = 0;
69
70 inline BufferCopyRegion() = default;
71
72 inline explicit BufferCopyRegion(UInt64 size)
73 : Size(size)
74 , SourceOffset(0)
75 , DestOffset(0)
76 {
77 }
78
79 inline BufferCopyRegion(UInt32 sourceOffset, UInt32 destOffset, UInt64 size)
80 : Size(size)
81 , SourceOffset(sourceOffset)
82 , DestOffset(destOffset)
83 {
84 }
85 };
86
88 enum class AccessFlags
89 {
90 None = UN_BIT(0),
91 KernelRead = UN_BIT(1),
92 KernelWrite = UN_BIT(2),
93 TransferRead = UN_BIT(3),
94 TransferWrite = UN_BIT(4),
95 HostRead = UN_BIT(5),
96 HostWrite = UN_BIT(6)
97 };
98
99 UN_ENUM_OPERATORS(AccessFlags);
100
101 class IBuffer;
102
105 {
106 AccessFlags SourceAccess = AccessFlags::None;
107 AccessFlags DestAccess = AccessFlags::None;
108
109 HardwareQueueKindFlags SourceQueueKind = HardwareQueueKindFlags::None;
110 HardwareQueueKindFlags DestQueueKind = HardwareQueueKindFlags::None;
111
112 inline MemoryBarrierDesc() = default;
113
114 inline MemoryBarrierDesc(AccessFlags sourceAccess, AccessFlags destAccess,
115 HardwareQueueKindFlags sourceQueueKind = HardwareQueueKindFlags::None,
116 HardwareQueueKindFlags destQueueKind = HardwareQueueKindFlags::None)
117 : SourceAccess(sourceAccess)
118 , DestAccess(destAccess)
119 , SourceQueueKind(sourceQueueKind)
120 , DestQueueKind(destQueueKind)
121 {
122 }
123 };
124
125 class IFence;
126 class ICommandList;
127 class IKernel;
128
131 {
132 ICommandList* m_pCommandList;
133
134 public:
135 explicit CommandListBuilder(ICommandList* pCommandList);
137
138 inline CommandListBuilder(CommandListBuilder&& other) noexcept
139 : m_pCommandList(other.m_pCommandList)
140 {
141 other.m_pCommandList = nullptr;
142 }
143
144 inline CommandListBuilder& operator=(CommandListBuilder&& other)
145 {
146 m_pCommandList = other.m_pCommandList;
147 other.m_pCommandList = nullptr;
148 return *this;
149 }
150
152 void End();
153
158 void MemoryBarrier(IBuffer* pBuffer, const MemoryBarrierDesc& barrierDesc);
159
165 void Copy(IBuffer* pSource, IBuffer* pDestination, const BufferCopyRegion& region);
166
173 void Dispatch(IKernel* pKernel, Int32 x, Int32 y, Int32 z);
174
175 explicit operator bool();
176 };
177
180 {
181 friend class CommandListBuilder;
182
183 protected:
184 virtual void End() = 0;
185
186 virtual void CmdMemoryBarrier(IBuffer* pBuffer, const MemoryBarrierDesc& barrierDesc) = 0;
187 virtual void CmdCopy(IBuffer* pSource, IBuffer* pDestination, const BufferCopyRegion& region) = 0;
188 virtual void CmdDispatch(IKernel* pKernel, Int32 x, Int32 y, Int32 z) = 0;
189
190 public:
192
193 [[nodiscard]] virtual const DescriptorType& GetDesc() const = 0;
194
195 virtual ResultCode Init(const DescriptorType& desc) = 0;
196
198 virtual IFence* GetFence() = 0;
199
201 [[nodiscard]] virtual CommandListState GetState() = 0;
202
205
207 virtual void ResetState() = 0;
208
210 virtual ResultCode Submit() = 0;
211 };
212
213 inline CommandListBuilder::CommandListBuilder(ICommandList* pCommandList)
214 : m_pCommandList(pCommandList)
215 {
216 }
217
219 {
220 if (m_pCommandList)
221 {
222 m_pCommandList->End();
223 m_pCommandList = nullptr;
224 }
225 }
226
227 inline void CommandListBuilder::MemoryBarrier(IBuffer* pBuffer, const MemoryBarrierDesc& barrierDesc)
228 {
229 m_pCommandList->CmdMemoryBarrier(pBuffer, barrierDesc);
230 }
231
232 inline void CommandListBuilder::Copy(IBuffer* pSource, IBuffer* pDestination, const BufferCopyRegion& region)
233 {
234 m_pCommandList->CmdCopy(pSource, pDestination, region);
235 }
236
237 inline void CommandListBuilder::Dispatch(IKernel* pKernel, Int32 x, Int32 y, Int32 z)
238 {
239 m_pCommandList->CmdDispatch(pKernel, x, y, z);
240 }
241
242 inline CommandListBuilder::operator bool()
243 {
244 return m_pCommandList != nullptr;
245 }
246
247 inline CommandListBuilder::~CommandListBuilder()
248 {
249 End();
250 }
251} // namespace UN
252
253template<>
254struct fmt::formatter<UN::CommandListState> : fmt::formatter<std::string_view>
255{
256 template<typename FormatContext>
257 auto format(const UN::CommandListState& state, FormatContext& ctx) const -> decltype(ctx.out())
258 {
259 return fmt::format_to(ctx.out(), "{}", UN::CommandListStateToString(state));
260 }
261};
Command list builder, used for device command recording.
Definition: ICommandList.h:131
void Dispatch(IKernel *pKernel, Int32 x, Int32 y, Int32 z)
Dispatch a compute kernel to execute on the device.
Definition: ICommandList.h:237
void Copy(IBuffer *pSource, IBuffer *pDestination, const BufferCopyRegion &region)
Copy a region of the source buffer to the destination buffer.
Definition: ICommandList.h:232
void MemoryBarrier(IBuffer *pBuffer, const MemoryBarrierDesc &barrierDesc)
Insert a memory dependency.
Definition: ICommandList.h:227
void End()
Set the command list state to CommandListState::Executable.
Definition: ICommandList.h:218
An interface for backend-specific buffers that store the data on the device.
Definition: IBuffer.h:26
An interface for command lists that record commands to be executed by the backend.
Definition: ICommandList.h:180
virtual ResultCode Submit()=0
Submit the command list and set the state to CommandListState::Pending.
virtual void ResetState()=0
Set the command list state to CommandListState::Initial.
virtual CommandListBuilder Begin()=0
Set the command list state to CommandListState::Recording.
virtual IFence * GetFence()=0
Get the fence that is signaled after submit operation is complete.
virtual CommandListState GetState()=0
Get command list state.
Base interface for all compute backend objects.
Definition: IDeviceObject.h:30
An interface for fences - synchronization primitives that can be either signaled or reset.
Definition: IFence.h:31
An interface for compute kernel - a program running on the device.
Definition: IKernel.h:29
Region for buffer copy command.
Definition: ICommandList.h:65
UInt64 Size
Size of the copy region.
Definition: ICommandList.h:66
UInt32 DestOffset
Offset in the destination buffer.
Definition: ICommandList.h:68
UInt32 SourceOffset
Offset in the source buffer.
Definition: ICommandList.h:67
Command list descriptor.
Definition: ICommandList.h:47
const char * Name
Command list debug name.
Definition: ICommandList.h:48
HardwareQueueKindFlags QueueKindFlags
Command queue kind flags.
Definition: ICommandList.h:49
CommandListFlags Flags
Command list flags.
Definition: ICommandList.h:50
Memory barrier descriptor.
Definition: ICommandList.h:105
HardwareQueueKindFlags DestQueueKind
Destination command queue kind.
Definition: ICommandList.h:110
AccessFlags SourceAccess
Source access mask.
Definition: ICommandList.h:106
AccessFlags DestAccess
Destination access mask.
Definition: ICommandList.h:107
HardwareQueueKindFlags SourceQueueKind
Source command queue kind.
Definition: ICommandList.h:109