2#include <UnCompute/Backend/IBuffer.h>
3#include <UnCompute/Backend/MemoryKindFlags.h>
4#include <UnCompute/Base/Flags.h>
5#include <UnCompute/Containers/ArraySlice.h>
13 const char*
Name =
nullptr;
15 MemoryKindFlags
Flags = MemoryKindFlags::None;
36 inline static constexpr UInt64
WholeSize = std::numeric_limits<UInt64>::max();
56 virtual ResultCode
Map(UInt64 byteOffset, UInt64 byteSize,
void** ppData) = 0;
91 UInt64 m_ByteOffset = 0;
92 UInt64 m_ByteSize = 0;
106 m_ByteOffset = byteOffset;
107 m_ByteSize = std::min(byteSize, pMemory->GetDesc().
Size - byteOffset);
109 "Invalid memory slice size");
135 [[nodiscard]]
inline ResultCode
Map(
void** ppData)
const
137 return m_pMemory->Map(m_ByteOffset, m_ByteSize, ppData);
143 [[nodiscard]]
inline void*
Map()
const
146 if (
auto result = m_pMemory->Map(m_ByteOffset, m_ByteSize, &pResult); Failed(result))
148 UN_Error(
false,
"Couldn't map memory, IDeviceMemory::Map returned {}", result);
162 [[nodiscard]]
inline ResultCode
Map(UInt64 byteOffset, UInt64 byteSize,
void** ppData)
const
165 "Invalid memory map size");
167 return m_pMemory->Map(
168 m_ByteOffset + byteOffset, std::min(byteSize, m_pMemory->GetDesc().Size - byteOffset - m_ByteOffset), ppData);
180 "Invalid memory map size");
183 if (Succeeded(m_pMemory->Map(m_ByteOffset + byteOffset,
184 std::min(byteSize, m_pMemory->GetDesc().Size - byteOffset - m_ByteOffset),
190 UN_Error(
false,
"Couldn't map memory");
213 return m_pMemory->IsCompatible(pObject, m_ByteSize);
227 : m_MemorySlice(memorySlice)
234 : m_MemorySlice(other.m_MemorySlice)
237 other.m_MemorySlice = {};
238 other.m_Map =
nullptr;
245 m_MemorySlice.
Unmap();
249 inline T& operator[](USize index)
251 UN_Assert(
IsValid(),
"MemoryMapHelper was in invalid state");
252 UN_Assert(index *
sizeof(T) < m_MemorySlice.
GetByteSize(),
"Index out of range in MemoryMapHelper");
257 [[nodiscard]]
inline UInt64
Length()
const
259 UN_Assert(
IsValid(),
"MemoryMapHelper was in invalid state");
266 UN_Assert(
IsValid(),
"MemoryMapHelper was in invalid state");
277 inline explicit operator bool()
const
283 [[nodiscard]]
inline T*
Data()
const
285 UN_Assert(
IsValid(),
"MemoryMapHelper was in invalid state");
307 auto result = memorySlice.
Map(&map);
308 if (Succeeded(result))
341 UN_Assert(
IsValid(),
"MemoryMapHelper was in invalid state");
347 UN_Assert(
IsValid(),
"MemoryMapHelper was in invalid state");
This class represents a non-owning slice of contiguously stored elements.
Definition: ArraySlice.h:12
A slice of device memory.
Definition: IDeviceMemory.h:89
void Unmap() const
Unmap the mapped memory.
Definition: IDeviceMemory.h:197
bool IsCompatible(IDeviceObject *pObject) const
Check if the memory is compatible with an object.
Definition: IDeviceMemory.h:211
ResultCode Map(UInt64 byteOffset, UInt64 byteSize, void **ppData) const
Map the part of device memory represented by this slice.
Definition: IDeviceMemory.h:162
ResultCode Map(void **ppData) const
Map the part of device memory represented by this slice.
Definition: IDeviceMemory.h:135
IDeviceMemory * GetDeviceMemory() const
Get the underlying device memory object.
Definition: IDeviceMemory.h:113
void * Map(UInt64 byteOffset, UInt64 byteSize=IDeviceMemory::WholeSize) const
Map the part of device memory represented by this slice.
Definition: IDeviceMemory.h:177
UInt64 GetByteSize() const
Get slice size in bytes.
Definition: IDeviceMemory.h:125
DeviceMemorySlice(IDeviceMemory *pMemory, UInt64 byteOffset=0, UInt64 byteSize=IDeviceMemory::WholeSize)
Create a device memory slice.
Definition: IDeviceMemory.h:102
void * Map() const
Map the part of device memory represented by this slice.
Definition: IDeviceMemory.h:143
UInt64 GetByteOffset() const
Get slice offset in bytes.
Definition: IDeviceMemory.h:119
This class holds a handle to backend-specific memory.
Definition: IDeviceMemory.h:31
static constexpr UInt64 WholeSize
Special constant that represents the whole memory size.
Definition: IDeviceMemory.h:36
virtual ResultCode Map(UInt64 byteOffset, UInt64 byteSize, void **ppData)=0
Map the device memory to access it from the host.
virtual ResultCode Init(const DescriptorType &desc)=0
Allocate memory with specified descriptor.
virtual void Unmap()=0
Unmap the mapped memory.
virtual bool IsCompatible(IDeviceObject *pObject)=0
Check if the memory is compatible with an object.
virtual bool IsCompatible(IDeviceObject *pObject, UInt64 sizeLimit)=0
Check if the memory is compatible with an object.
Base interface for all compute backend objects.
Definition: IDeviceObject.h:30
Helper class for memory mapping, implements indexing operators and bound checking.
Definition: IDeviceMemory.h:220
UInt64 Length() const
Get size of the mapped data as a number of elements of type T.
Definition: IDeviceMemory.h:257
bool IsValid() const
Check if an instance of the class is in valid state.
Definition: IDeviceMemory.h:271
static MemoryMapHelper Map(IDeviceMemory *pMemory, UInt64 byteOffset=0, UInt64 byteSize=IDeviceMemory::WholeSize)
Map the device memory.
Definition: IDeviceMemory.h:333
T * Data() const
Get a pointer to the mapped memory.
Definition: IDeviceMemory.h:283
UInt64 ByteSize() const
Get size of the mapped data in bytes.
Definition: IDeviceMemory.h:264
static MemoryMapHelper Map(const DeviceMemorySlice &memorySlice)
Map the device memory slice.
Definition: IDeviceMemory.h:304
Device memory descriptor.
Definition: IDeviceMemory.h:12
const char * Name
Device memory debug name.
Definition: IDeviceMemory.h:13
UInt64 Size
Memory size in bytes.
Definition: IDeviceMemory.h:14
MemoryKindFlags Flags
Memory kind flags.
Definition: IDeviceMemory.h:15
ArraySlice< IDeviceObject * > Objects
Resource objects that the memory must be compatible with.
Definition: IDeviceMemory.h:16