Skip to content

Commit 2b10650

Browse files
committed
Added "E_MEMORY_ALLOCATE_FLAGS" and necessary validations and deductions.
1 parent 95b6570 commit 2b10650

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

include/nbl/video/IDriverMemoryAllocation.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ class IDriverMemoryAllocation : public virtual core::IReferenceCounted
8989
EMCF_CACHED=0x08u, ///< whether mapping is cached, i.e. if cpu reads go through cache, this is relevant to Vulkan only and is transparent to program operation.
9090
};
9191

92+
//! Memory allocate flags
93+
enum E_MEMORY_ALLOCATE_FLAGS
94+
{
95+
EMAF_NONE = 0x00000000,
96+
EMAF_DEVICE_MASK_BIT = 0x00000001,
97+
EMAF_DEVICE_ADDRESS_BIT = 0x00000002,
98+
EMAF_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT = 0x00000004,
99+
};
100+
92101
E_API_TYPE getAPIType() const;
93102

94103
//! Where the memory was actually allocated
@@ -117,6 +126,9 @@ class IDriverMemoryAllocation : public virtual core::IReferenceCounted
117126

118127
//!
119128
inline E_MAPPING_CPU_ACCESS_FLAG getCurrentMappingCaps() const {return currentMappingAccess;}
129+
130+
//!
131+
inline core::bitflag<E_MEMORY_ALLOCATE_FLAGS> getAllocateFlags() const {return allocateFlags;}
120132

121133
inline bool isCurrentlyMapped() const { return mappedPtr != nullptr; }
122134

@@ -143,15 +155,16 @@ class IDriverMemoryAllocation : public virtual core::IReferenceCounted
143155
currentMappingAccess = access;
144156
}
145157

146-
IDriverMemoryAllocation(const ILogicalDevice* originDevice)
158+
IDriverMemoryAllocation(const ILogicalDevice* originDevice, core::bitflag<E_MEMORY_ALLOCATE_FLAGS> flags = E_MEMORY_ALLOCATE_FLAGS::EMAF_NONE)
147159
: m_originDevice(originDevice), mappedPtr(nullptr), mappedRange(0,0),
148-
currentMappingAccess(EMCAF_NO_MAPPING_ACCESS)
160+
currentMappingAccess(EMCAF_NO_MAPPING_ACCESS), allocateFlags(flags)
149161
{}
150162

151163
const ILogicalDevice* m_originDevice = nullptr;
152164
uint8_t* mappedPtr;
153165
MemoryRange mappedRange;
154166
E_MAPPING_CPU_ACCESS_FLAG currentMappingAccess;
167+
core::bitflag<E_MEMORY_ALLOCATE_FLAGS> allocateFlags;
155168
};
156169

157170
} // end namespace nbl::video

include/nbl/video/ILogicalDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class ILogicalDevice : public core::IReferenceCounted
248248
//! Should be just as fast to play around with on the CPU as regular malloc'ed memory, but slowest to access with GPU
249249
virtual core::smart_refctd_ptr<IDriverMemoryAllocation> allocateCPUSideGPUVisibleMemory(const IDriverMemoryBacked::SDriverMemoryRequirements& additionalReqs) { return nullptr; }
250250

251-
virtual core::smart_refctd_ptr<IDriverMemoryAllocation> allocateGPUMemory(const IDriverMemoryBacked::SDriverMemoryRequirements& reqs) { return nullptr; }
251+
virtual core::smart_refctd_ptr<IDriverMemoryAllocation> allocateGPUMemory(const IDriverMemoryBacked::SDriverMemoryRequirements& reqs, core::bitflag<IDriverMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS> allocateFlags = IDriverMemoryAllocation::EMAF_NONE) { return nullptr; }
252252

253253
//! For memory allocations without the video::IDriverMemoryAllocation::EMCF_COHERENT mapping capability flag you need to call this for the CPU writes to become GPU visible
254254
void flushMappedMemoryRanges(uint32_t memoryRangeCount, const video::IDriverMemoryAllocation::MappedMemoryRange* pMemoryRanges)

src/nbl/video/CVulkanLogicalDevice.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ core::smart_refctd_ptr<IDriverMemoryAllocation> CVulkanLogicalDevice::allocateCP
146146
}
147147

148148
core::smart_refctd_ptr<IDriverMemoryAllocation> CVulkanLogicalDevice::allocateGPUMemory(
149-
const IDriverMemoryBacked::SDriverMemoryRequirements& reqs)
149+
const IDriverMemoryBacked::SDriverMemoryRequirements& reqs,
150+
core::bitflag<IDriverMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS> allocateFlags)
150151
{
151152
VkMemoryPropertyFlags desiredMemoryProperties = static_cast<VkMemoryPropertyFlags>(0u);
152153

@@ -182,17 +183,25 @@ core::smart_refctd_ptr<IDriverMemoryAllocation> CVulkanLogicalDevice::allocateGP
182183
for (uint32_t i = 0u; i < compatibleMemoryTypeCount; ++i)
183184
{
184185
// Todo(achal): Make use of requiresDedicatedAllocation and prefersDedicatedAllocation
186+
VkMemoryAllocateFlagsInfo vk_allocateFlags = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, nullptr };
187+
if (allocateFlags.hasValue(IDriverMemoryAllocation::EMAF_DEVICE_MASK_BIT))
188+
vk_allocateFlags.flags |= VK_MEMORY_ALLOCATE_DEVICE_MASK_BIT;
189+
else if(allocateFlags.hasValue(IDriverMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT))
190+
vk_allocateFlags.flags |= VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
191+
else if (allocateFlags.hasValue(IDriverMemoryAllocation::EMAF_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT))
192+
vk_allocateFlags.flags |= VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT;
193+
vk_allocateFlags.deviceMask = 0u; // unused
185194

186195
VkMemoryAllocateInfo vk_allocateInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
187-
vk_allocateInfo.pNext = nullptr; // No extensions for now
196+
vk_allocateInfo.pNext = &vk_allocateFlags; // No extensions for now
188197
vk_allocateInfo.allocationSize = reqs.vulkanReqs.size;
189198
vk_allocateInfo.memoryTypeIndex = compatibleMemoryTypeIndices[i];
190199

191200
VkDeviceMemory vk_deviceMemory;
192201
if (m_devf.vk.vkAllocateMemory(m_vkdev, &vk_allocateInfo, nullptr, &vk_deviceMemory) == VK_SUCCESS)
193202
{
194203
// Todo(achal): Change dedicate to not always be false
195-
return core::make_smart_refctd_ptr<CVulkanMemoryAllocation>(this, reqs.vulkanReqs.size, false, vk_deviceMemory);
204+
return core::make_smart_refctd_ptr<CVulkanMemoryAllocation>(this, reqs.vulkanReqs.size, false, vk_deviceMemory, allocateFlags);
196205
}
197206
}
198207

src/nbl/video/CVulkanLogicalDevice.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,17 @@ class CVulkanLogicalDevice final : public ILogicalDevice
514514
if ((bindInfo.buffer->getAPIType() != EAT_VULKAN) || (bindInfo.memory->getAPIType() != EAT_VULKAN))
515515
continue;
516516

517+
if (bindInfo.buffer->getCachedCreationParams().usage.hasValue(asset::IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT))
518+
{
519+
if(!bindInfo.memory->getAllocateFlags().hasValue(IDriverMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT))
520+
{
521+
// TODO(erfan): Log-> if buffer was created with EUF_SHADER_DEVICE_ADDRESS_BIT set, memory must have been allocated with the EMAF_DEVICE_ADDRESS_BIT bit.
522+
_NBL_DEBUG_BREAK_IF(false);
523+
anyFailed = true;
524+
continue;
525+
}
526+
}
527+
517528
CVulkanBuffer* vulkanBuffer = static_cast<CVulkanBuffer*>(bindInfo.buffer);
518529
vulkanBuffer->setMemoryAndOffset(
519530
core::smart_refctd_ptr<IDriverMemoryAllocation>(bindInfo.memory), bindInfo.offset);
@@ -523,6 +534,7 @@ class CVulkanLogicalDevice final : public ILogicalDevice
523534
if (m_devf.vk.vkBindBufferMemory(m_vkdev, vk_buffer, vk_memory, static_cast<VkDeviceSize>(pBindInfos[i].offset)) != VK_SUCCESS)
524535
{
525536
// Todo(achal): Log which one failed
537+
_NBL_DEBUG_BREAK_IF(false);
526538
anyFailed = true;
527539
}
528540
}
@@ -591,8 +603,13 @@ class CVulkanLogicalDevice final : public ILogicalDevice
591603
memoryReqs.memoryHeapLocation = additionalMemoryReqs.memoryHeapLocation;
592604
memoryReqs.mappingCapability = additionalMemoryReqs.mappingCapability;
593605

606+
core::bitflag<IDriverMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS> allocateFlags;
607+
608+
if(creationParams.usage.hasValue(asset::IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT))
609+
allocateFlags |= IDriverMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT;
610+
594611
core::smart_refctd_ptr<video::IDriverMemoryAllocation> bufferMemory =
595-
allocateGPUMemory(memoryReqs);
612+
allocateGPUMemory(memoryReqs, allocateFlags);
596613

597614
if (!bufferMemory)
598615
return nullptr;
@@ -910,7 +927,7 @@ class CVulkanLogicalDevice final : public ILogicalDevice
910927
const IDriverMemoryBacked::SDriverMemoryRequirements& additionalReqs) override;
911928

912929
core::smart_refctd_ptr<IDriverMemoryAllocation> allocateGPUMemory(
913-
const IDriverMemoryBacked::SDriverMemoryRequirements& reqs) override;
930+
const IDriverMemoryBacked::SDriverMemoryRequirements& reqs, core::bitflag<IDriverMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS> allocateFlags = IDriverMemoryAllocation::EMAF_NONE) override;
914931

915932
core::smart_refctd_ptr<IGPUSampler> createGPUSampler(const IGPUSampler::SParams& _params) override
916933
{

src/nbl/video/CVulkanMemoryAllocation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class ILogicalDevice;
1212
class CVulkanMemoryAllocation : public IDriverMemoryAllocation
1313
{
1414
public:
15-
CVulkanMemoryAllocation(ILogicalDevice* dev, size_t size, bool isDedicated, VkDeviceMemory deviceMemoryHandle)
16-
: IDriverMemoryAllocation(dev), m_size(size), m_isDedicated(isDedicated), m_deviceMemoryHandle(deviceMemoryHandle)
15+
CVulkanMemoryAllocation(ILogicalDevice* dev, size_t size, bool isDedicated, VkDeviceMemory deviceMemoryHandle, core::bitflag<IDriverMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS> flags = E_MEMORY_ALLOCATE_FLAGS::EMAF_NONE)
16+
: IDriverMemoryAllocation(dev, flags), m_size(size), m_isDedicated(isDedicated), m_deviceMemoryHandle(deviceMemoryHandle)
1717
{}
1818

1919
~CVulkanMemoryAllocation();

0 commit comments

Comments
 (0)