diff --git a/.gitmodules b/.gitmodules index 7d9204d9..c3bd4f78 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,7 @@ [submodule "External/vulkan"] path = External/vulkan url = https://github.com/KhronosGroup/Vulkan-Headers.git - branch = origin/vulkan-sdk-1.3.283 + branch = origin/vulkan-sdk-1.3.290 update = merge [submodule "External/nvapi"] path = External/nvapi diff --git a/External/vulkan b/External/vulkan index eaa319da..b379292b 160000 --- a/External/vulkan +++ b/External/vulkan @@ -1 +1 @@ -Subproject commit eaa319dade959cb61ed2229c8ea42e307cc8f8b3 +Subproject commit b379292b2ab6df5771ba9870d53cf8b2c9295daf diff --git a/Include/Extensions/NRIHelper.h b/Include/Extensions/NRIHelper.h index a7e6862f..82054189 100644 --- a/Include/Extensions/NRIHelper.h +++ b/Include/Extensions/NRIHelper.h @@ -41,6 +41,7 @@ NRI_STRUCT(ResourceGroupDesc) uint32_t textureNum; NRI_NAME(Buffer)* const* buffers; uint32_t bufferNum; + uint64_t preferredMemorySize; // desired chunk size (but can be greater if a resource doesn't fit), 256 Mb if 0 }; NRI_STRUCT(FormatProps) diff --git a/Include/NRI.h b/Include/NRI.h index 5c8c5638..e239af62 100644 --- a/Include/NRI.h +++ b/Include/NRI.h @@ -25,8 +25,8 @@ Non-goals: #include #define NRI_VERSION_MAJOR 1 -#define NRI_VERSION_MINOR 138 -#define NRI_VERSION_DATE "22 July 2024" +#define NRI_VERSION_MINOR 139 +#define NRI_VERSION_DATE "31 July 2024" #ifdef _WIN32 #define NRI_CALL __fastcall @@ -90,6 +90,10 @@ NRI_STRUCT(CoreInterface) void (NRI_CALL *DestroyFence)(NRI_NAME_REF(Fence) fence); // Memory + // - use "GetBufferMemoryDesc" (or "GetTextureMemoryDesc") to get "MemoryDesc" ("usageBits" and "MemoryLocation" affect returned "MemoryType") + // - (optional) group returned "MemoryDesc"s by "MemoryType", but do not group if "mustBeDedicated = true" + // - call "BindBufferMemory" (or "BindTextureMemory") to bind resources to "Memory" objects + // => "CalculateAllocationNumber" and "AllocateAndBindMemory" simplify this process for static allocations NRI_NAME(Result) (NRI_CALL *AllocateMemory)(NRI_NAME_REF(Device) device, NRI_NAME(MemoryType) memoryType, uint64_t size, NRI_NAME_REF(Memory*) memory); NRI_NAME(Result) (NRI_CALL *BindBufferMemory)(NRI_NAME_REF(Device) device, const NRI_NAME(BufferMemoryBindingDesc)* memoryBindingDescs, uint32_t memoryBindingDescNum); NRI_NAME(Result) (NRI_CALL *BindTextureMemory)(NRI_NAME_REF(Device) device, const NRI_NAME(TextureMemoryBindingDesc)* memoryBindingDescs, uint32_t memoryBindingDescNum); diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index 8c660f4f..3e814d23 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -1212,6 +1212,14 @@ NRI_STRUCT(QueueSubmitDesc) }; // Memory +NRI_STRUCT(MemoryDesc) +{ + uint64_t size; + uint32_t alignment; + NRI_NAME(MemoryType) type; + bool mustBeDedicated; +}; + NRI_STRUCT(BufferMemoryBindingDesc) { NRI_NAME(Memory)* memory; @@ -1226,14 +1234,6 @@ NRI_STRUCT(TextureMemoryBindingDesc) uint64_t offset; }; -NRI_STRUCT(MemoryDesc) -{ - uint64_t size; - uint32_t alignment; - NRI_NAME(MemoryType) type; - bool mustBeDedicated; -}; - // Clear storage NRI_STRUCT(ClearStorageBufferDesc) { diff --git a/Resources/Version.h b/Resources/Version.h index a03ac51b..8a84b851 100644 --- a/Resources/Version.h +++ b/Resources/Version.h @@ -4,7 +4,7 @@ #define STR(x) STR_HELPER(x) #define VERSION_MAJOR 1 -#define VERSION_MINOR 138 +#define VERSION_MINOR 139 #define VERSION_BUILD 0 #define VERSION_REVISION 0 diff --git a/Source/D3D11/PipelineD3D11.cpp b/Source/D3D11/PipelineD3D11.cpp index 6b0fc6fe..67640f36 100644 --- a/Source/D3D11/PipelineD3D11.cpp +++ b/Source/D3D11/PipelineD3D11.cpp @@ -73,7 +73,7 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) { m_InputAssemplyStrides[stream.bindingSlot] = stream.stride; }; - assert(vertexShader != nullptr); + CHECK(vertexShader != nullptr, "VS can't be NULL"); hr = m_Device->CreateInputLayout(&inputElements[0], vi.attributeNum, vertexShader->bytecode, (size_t)vertexShader->size, &m_InputLayout); RETURN_ON_BAD_HRESULT(&m_Device, hr, "ID3D11Device::CreateInputLayout()"); } diff --git a/Source/D3D12/DeviceD3D12.cpp b/Source/D3D12/DeviceD3D12.cpp index d8f028d1..43d4663f 100644 --- a/Source/D3D12/DeviceD3D12.cpp +++ b/Source/D3D12/DeviceD3D12.cpp @@ -54,7 +54,7 @@ static uint8_t QueryLatestDevice(ComPtr& in, ComPtrGetResourceAllocationInfo(NRI_NODE_MASK, 1, &resourceDesc); MemoryType memoryType = ConstructMemoryType(heapType, heapFlags); - memoryDesc.size = (uint64_t)resourceAllocationInfo.SizeInBytes; + memoryDesc.size = resourceAllocationInfo.SizeInBytes; memoryDesc.alignment = (uint32_t)resourceAllocationInfo.Alignment; memoryDesc.type = memoryType; memoryDesc.mustBeDedicated = IsDedicated(memoryType); @@ -949,7 +949,7 @@ inline void DeviceD3D12::DestroyQueryPool(QueryPool& queryPool) { Deallocate(GetStdAllocator(), (QueryPoolD3D12*)&queryPool); } -inline Result DeviceD3D12::AllocateMemory(const MemoryType memoryType, uint64_t size, Memory*& memory) { +inline Result DeviceD3D12::AllocateMemory(MemoryType memoryType, uint64_t size, Memory*& memory) { return CreateImplementation(memory, memoryType, size); } diff --git a/Source/D3D12/DeviceD3D12.h b/Source/D3D12/DeviceD3D12.h index 7802cb96..8bf7f06b 100644 --- a/Source/D3D12/DeviceD3D12.h +++ b/Source/D3D12/DeviceD3D12.h @@ -119,7 +119,7 @@ struct DeviceD3D12 final : public DeviceBase { void DestroyPipeline(Pipeline& pipeline); void DestroyFence(Fence& queueSemaphore); void DestroyQueryPool(QueryPool& queryPool); - Result AllocateMemory(const MemoryType memoryType, uint64_t size, Memory*& memory); + Result AllocateMemory(MemoryType memoryType, uint64_t size, Memory*& memory); Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); void FreeMemory(Memory& memory); diff --git a/Source/D3D12/MemoryD3D12.cpp b/Source/D3D12/MemoryD3D12.cpp index 7466a83a..7b774eb4 100644 --- a/Source/D3D12/MemoryD3D12.cpp +++ b/Source/D3D12/MemoryD3D12.cpp @@ -10,7 +10,7 @@ static inline D3D12_HEAP_TYPE GetHeapType(MemoryType memoryType) { return (D3D12_HEAP_TYPE)(memoryType >> 16); } -Result MemoryD3D12::Create(const MemoryType memoryType, uint64_t size) { +Result MemoryD3D12::Create(MemoryType memoryType, uint64_t size) { D3D12_HEAP_DESC heapDesc = {}; heapDesc.SizeInBytes = size; heapDesc.Properties.Type = GetHeapType(memoryType); diff --git a/Source/D3D12/MemoryD3D12.h b/Source/D3D12/MemoryD3D12.h index db0eca7f..42da646c 100644 --- a/Source/D3D12/MemoryD3D12.h +++ b/Source/D3D12/MemoryD3D12.h @@ -29,7 +29,7 @@ struct MemoryD3D12 { return m_Device; } - Result Create(const MemoryType memoryType, uint64_t size); + Result Create(MemoryType memoryType, uint64_t size); Result Create(const MemoryD3D12Desc& memoryDesc); //================================================================================================================ diff --git a/Source/Shared/HelperDeviceMemoryAllocator.cpp b/Source/Shared/HelperDeviceMemoryAllocator.cpp index 30efd355..01d6e91f 100644 --- a/Source/Shared/HelperDeviceMemoryAllocator.cpp +++ b/Source/Shared/HelperDeviceMemoryAllocator.cpp @@ -4,14 +4,14 @@ using namespace nri; -HelperDeviceMemoryAllocator::MemoryTypeGroup::MemoryTypeGroup(const StdAllocator& stdAllocator) : - buffers(stdAllocator), bufferOffsets(stdAllocator), textures(stdAllocator), textureOffsets(stdAllocator), memoryOffset(0) { +HelperDeviceMemoryAllocator::MemoryHeap::MemoryHeap(MemoryType memoryType, const StdAllocator& stdAllocator) : + buffers(stdAllocator), bufferOffsets(stdAllocator), textures(stdAllocator), textureOffsets(stdAllocator), size(0), type(memoryType) { } HelperDeviceMemoryAllocator::HelperDeviceMemoryAllocator(const CoreInterface& NRI, Device& device) : m_NRI(NRI), m_Device(device), - m_Map(((DeviceBase&)device).GetStdAllocator()), + m_Heaps(((DeviceBase&)device).GetStdAllocator()), m_DedicatedBuffers(((DeviceBase&)device).GetStdAllocator()), m_DedicatedTextures(((DeviceBase&)device).GetStdAllocator()), m_BufferBindingDescs(((DeviceBase&)device).GetStdAllocator()), @@ -19,25 +19,16 @@ HelperDeviceMemoryAllocator::HelperDeviceMemoryAllocator(const CoreInterface& NR } uint32_t HelperDeviceMemoryAllocator::CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) { - m_Map.clear(); - m_DedicatedBuffers.clear(); - m_DedicatedTextures.clear(); + GroupByMemoryType(resourceGroupDesc.memoryLocation, resourceGroupDesc); - GroupByMemoryType(resourceGroupDesc.memoryLocation, resourceGroupDesc.buffers, resourceGroupDesc.bufferNum); - GroupByMemoryType(resourceGroupDesc.memoryLocation, resourceGroupDesc.textures, resourceGroupDesc.textureNum); + size_t allocationNum = m_Heaps.size() + m_DedicatedBuffers.size() + m_DedicatedTextures.size(); - return uint32_t(m_Map.size()) + uint32_t(m_DedicatedBuffers.size()) + uint32_t(m_DedicatedTextures.size()); + return (uint32_t)allocationNum; } Result HelperDeviceMemoryAllocator::AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) { - m_Map.clear(); - m_DedicatedBuffers.clear(); - m_DedicatedTextures.clear(); - m_BufferBindingDescs.clear(); - m_TextureBindingDescs.clear(); - size_t allocationNum = 0; - const Result result = TryToAllocateAndBindMemory(resourceGroupDesc, allocations, allocationNum); + Result result = TryToAllocateAndBindMemory(resourceGroupDesc, allocations, allocationNum); if (result != Result::SUCCESS) { for (size_t i = 0; i < allocationNum; i++) { @@ -50,24 +41,26 @@ Result HelperDeviceMemoryAllocator::AllocateAndBindMemory(const ResourceGroupDes } Result HelperDeviceMemoryAllocator::TryToAllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations, size_t& allocationNum) { - GroupByMemoryType(resourceGroupDesc.memoryLocation, resourceGroupDesc.buffers, resourceGroupDesc.bufferNum); - GroupByMemoryType(resourceGroupDesc.memoryLocation, resourceGroupDesc.textures, resourceGroupDesc.textureNum); + GroupByMemoryType(resourceGroupDesc.memoryLocation, resourceGroupDesc); - Result result = Result::SUCCESS; + for (MemoryHeap& heap : m_Heaps) { + Memory*& memory = allocations[allocationNum]; - for (auto it = m_Map.begin(); it != m_Map.end() && result == Result::SUCCESS; ++it) - result = ProcessMemoryTypeGroup(it->first, it->second, allocations, allocationNum); + Result result = m_NRI.AllocateMemory(m_Device, heap.type, heap.size, memory); + if (result != Result::SUCCESS) + return result; - if (result != Result::SUCCESS) - return result; + FillMemoryBindingDescs(heap.buffers.data(), heap.bufferOffsets.data(), (uint32_t)heap.buffers.size(), *memory); + FillMemoryBindingDescs(heap.textures.data(), heap.textureOffsets.data(), (uint32_t)heap.textures.size(), *memory); - result = ProcessDedicatedResources(resourceGroupDesc.memoryLocation, allocations, allocationNum); + allocationNum++; + } + Result result = ProcessDedicatedResources(resourceGroupDesc.memoryLocation, allocations, allocationNum); if (result != Result::SUCCESS) return result; result = m_NRI.BindBufferMemory(m_Device, m_BufferBindingDescs.data(), (uint32_t)m_BufferBindingDescs.size()); - if (result != Result::SUCCESS) return result; @@ -76,22 +69,6 @@ Result HelperDeviceMemoryAllocator::TryToAllocateAndBindMemory(const ResourceGro return result; } -Result HelperDeviceMemoryAllocator::ProcessMemoryTypeGroup(MemoryType memoryType, MemoryTypeGroup& group, Memory** allocations, size_t& allocationNum) { - Memory*& memory = allocations[allocationNum]; - - const uint64_t allocationSize = group.memoryOffset; - - const Result result = m_NRI.AllocateMemory(m_Device, memoryType, allocationSize, memory); - if (result != Result::SUCCESS) - return result; - - FillMemoryBindingDescs(group.buffers.data(), group.bufferOffsets.data(), (uint32_t)group.buffers.size(), *memory); - FillMemoryBindingDescs(group.textures.data(), group.textureOffsets.data(), (uint32_t)group.textures.size(), *memory); - allocationNum++; - - return Result::SUCCESS; -} - Result HelperDeviceMemoryAllocator::ProcessDedicatedResources(MemoryLocation memoryLocation, Memory** allocations, size_t& allocationNum) { constexpr uint64_t zeroOffset = 0; MemoryDesc memoryDesc = {}; @@ -102,11 +79,12 @@ Result HelperDeviceMemoryAllocator::ProcessDedicatedResources(MemoryLocation mem Memory*& memory = allocations[allocationNum]; - const Result result = m_NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, memory); + Result result = m_NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, memory); if (result != Result::SUCCESS) return result; FillMemoryBindingDescs(m_DedicatedBuffers.data() + i, &zeroOffset, 1, *memory); + allocationNum++; } @@ -116,62 +94,82 @@ Result HelperDeviceMemoryAllocator::ProcessDedicatedResources(MemoryLocation mem Memory*& memory = allocations[allocationNum]; - const Result result = m_NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, memory); + Result result = m_NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, memory); if (result != Result::SUCCESS) return result; FillMemoryBindingDescs(m_DedicatedTextures.data() + i, &zeroOffset, 1, *memory); + allocationNum++; } return Result::SUCCESS; } -void HelperDeviceMemoryAllocator::GroupByMemoryType(MemoryLocation memoryLocation, Buffer* const* buffers, uint32_t bufferNum) { - MemoryDesc memoryDesc = {}; +HelperDeviceMemoryAllocator::MemoryHeap& HelperDeviceMemoryAllocator::FindOrCreateHeap(nri::MemoryDesc& memoryDesc, uint64_t preferredMemorySize) { + if (preferredMemorySize == 0) + preferredMemorySize = 256 * 1024 * 1024; - for (uint32_t i = 0; i < bufferNum; i++) { - Buffer* buffer = buffers[i]; + size_t j = 0; + for (; j < m_Heaps.size(); j++) { + const MemoryHeap& heap = m_Heaps[j]; + + uint64_t offset = Align(heap.size, memoryDesc.alignment); + uint64_t newSize = offset + memoryDesc.size; + + if (heap.type == memoryDesc.type && newSize <= preferredMemorySize) + break; + } + + if (j == m_Heaps.size()) + m_Heaps.push_back(MemoryHeap(memoryDesc.type, ((DeviceBase&)m_Device).GetStdAllocator())); + + return m_Heaps[j]; +} + +void HelperDeviceMemoryAllocator::GroupByMemoryType(MemoryLocation memoryLocation, const nri::ResourceGroupDesc& resourceGroupDesc) { + for (uint32_t i = 0; i < resourceGroupDesc.bufferNum; i++) { + Buffer* buffer = resourceGroupDesc.buffers[i]; const BufferDesc& bufferDesc = m_NRI.GetBufferDesc(*buffer); + + MemoryDesc memoryDesc = {}; m_NRI.GetBufferMemoryDesc(m_Device, bufferDesc, memoryLocation, memoryDesc); if (memoryDesc.mustBeDedicated) m_DedicatedBuffers.push_back(buffer); else { - MemoryTypeGroup& group = m_Map.try_emplace(memoryDesc.type, ((DeviceBase&)m_Device).GetStdAllocator()).first->second; + MemoryHeap& heap = FindOrCreateHeap(memoryDesc, resourceGroupDesc.preferredMemorySize); - uint64_t offset = Align(group.memoryOffset, memoryDesc.alignment); + uint64_t offset = Align(heap.size, memoryDesc.alignment); - group.buffers.push_back(buffer); - group.bufferOffsets.push_back(offset); - group.memoryOffset = offset + memoryDesc.size; + heap.buffers.push_back(buffer); + heap.bufferOffsets.push_back(offset); + heap.size = offset + memoryDesc.size; } } -} - -void HelperDeviceMemoryAllocator::GroupByMemoryType(MemoryLocation memoryLocation, Texture* const* textures, uint32_t textureNum) { - const DeviceDesc& deviceDesc = m_NRI.GetDeviceDesc(m_Device); - MemoryDesc memoryDesc = {}; - - for (uint32_t i = 0; i < textureNum; i++) { - Texture* texture = textures[i]; + for (uint32_t i = 0; i < resourceGroupDesc.textureNum; i++) { + Texture* texture = resourceGroupDesc.textures[i]; const TextureDesc& textureDesc = m_NRI.GetTextureDesc(*texture); + + MemoryDesc memoryDesc = {}; m_NRI.GetTextureMemoryDesc(m_Device, textureDesc, memoryLocation, memoryDesc); if (memoryDesc.mustBeDedicated) m_DedicatedTextures.push_back(texture); else { - MemoryTypeGroup& group = m_Map.try_emplace(memoryDesc.type, ((DeviceBase&)m_Device).GetStdAllocator()).first->second; + MemoryHeap& heap = FindOrCreateHeap(memoryDesc, resourceGroupDesc.preferredMemorySize); - if (group.textures.empty() && group.memoryOffset > 0) - group.memoryOffset = Align(group.memoryOffset, deviceDesc.bufferTextureGranularity); + if (heap.textures.empty()) { + const DeviceDesc& deviceDesc = m_NRI.GetDeviceDesc(m_Device); + heap.size = Align(heap.size, deviceDesc.bufferTextureGranularity); + } - uint64_t offset = Align(group.memoryOffset, memoryDesc.alignment); + uint64_t offset = Align(heap.size, memoryDesc.alignment); - group.textures.push_back(texture); - group.textureOffsets.push_back(offset); - group.memoryOffset = offset + memoryDesc.size; + heap.textures.push_back(texture); + heap.textureOffsets.push_back(offset); + heap.size = offset + memoryDesc.size; } } } diff --git a/Source/Shared/HelperDeviceMemoryAllocator.h b/Source/Shared/HelperDeviceMemoryAllocator.h index e80ba303..27a8935d 100644 --- a/Source/Shared/HelperDeviceMemoryAllocator.h +++ b/Source/Shared/HelperDeviceMemoryAllocator.h @@ -10,30 +10,28 @@ struct HelperDeviceMemoryAllocator { nri::Result AllocateAndBindMemory(const nri::ResourceGroupDesc& resourceGroupDesc, nri::Memory** allocations); private: - struct MemoryTypeGroup; - - nri::Result TryToAllocateAndBindMemory(const nri::ResourceGroupDesc& resourceGroupDesc, nri::Memory** allocations, size_t& allocationNum); - nri::Result ProcessMemoryTypeGroup(nri::MemoryType memoryType, MemoryTypeGroup& group, nri::Memory** allocations, size_t& allocationNum); - nri::Result ProcessDedicatedResources(nri::MemoryLocation memoryLocation, nri::Memory** allocations, size_t& allocationNum); - void GroupByMemoryType(nri::MemoryLocation memoryLocation, nri::Buffer* const* buffers, uint32_t bufferNum); - void GroupByMemoryType(nri::MemoryLocation memoryLocation, nri::Texture* const* textures, uint32_t textureNum); - void FillMemoryBindingDescs(nri::Buffer* const* buffers, const uint64_t* bufferOffsets, uint32_t bufferNum, nri::Memory& memory); - void FillMemoryBindingDescs(nri::Texture* const* texture, const uint64_t* textureOffsets, uint32_t textureNum, nri::Memory& memory); - - struct MemoryTypeGroup { - MemoryTypeGroup(const StdAllocator& stdAllocator); + struct MemoryHeap { + MemoryHeap(nri::MemoryType memoryType, const StdAllocator& stdAllocator); Vector buffers; Vector bufferOffsets; Vector textures; Vector textureOffsets; - uint64_t memoryOffset; + uint64_t size; + nri::MemoryType type; }; + nri::Result TryToAllocateAndBindMemory(const nri::ResourceGroupDesc& resourceGroupDesc, nri::Memory** allocations, size_t& allocationNum); + nri::Result ProcessDedicatedResources(nri::MemoryLocation memoryLocation, nri::Memory** allocations, size_t& allocationNum); + MemoryHeap& FindOrCreateHeap(nri::MemoryDesc& memoryDesc, uint64_t preferredMemorySize); + void GroupByMemoryType(nri::MemoryLocation memoryLocation, const nri::ResourceGroupDesc& resourceGroupDesc); + void FillMemoryBindingDescs(nri::Buffer* const* buffers, const uint64_t* bufferOffsets, uint32_t bufferNum, nri::Memory& memory); + void FillMemoryBindingDescs(nri::Texture* const* texture, const uint64_t* textureOffsets, uint32_t textureNum, nri::Memory& memory); + const nri::CoreInterface& m_NRI; nri::Device& m_Device; - Map m_Map; + Vector m_Heaps; Vector m_DedicatedBuffers; Vector m_DedicatedTextures; Vector m_BufferBindingDescs; diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index 935f3caf..72d2e6a2 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -51,7 +51,7 @@ typedef uint32_t DXGI_FORMAT; #define REPORT_WARNING(deviceBase, format, ...) (deviceBase)->ReportMessage(nri::Message::TYPE_WARNING, __FILE__, __LINE__, format, ##__VA_ARGS__) #define REPORT_ERROR(deviceBase, format, ...) (deviceBase)->ReportMessage(nri::Message::TYPE_ERROR, __FILE__, __LINE__, format, ##__VA_ARGS__) -#define CHECK(condition, message) assert(condition&& message) +#define CHECK(condition, message) assert((condition) && message) #define SET_D3D_DEBUG_OBJECT_NAME(obj, name) \ if (obj) \ diff --git a/Source/VK/AccelerationStructureVK.cpp b/Source/VK/AccelerationStructureVK.cpp index e87b062b..822c31e9 100644 --- a/Source/VK/AccelerationStructureVK.cpp +++ b/Source/VK/AccelerationStructureVK.cpp @@ -154,9 +154,16 @@ inline void AccelerationStructureVK::GetMemoryDesc(MemoryDesc& memoryDesc) const } inline Result AccelerationStructureVK::CreateDescriptor(Descriptor*& descriptor) const { - DescriptorVK& descriptorImpl = *Allocate(m_Device.GetStdAllocator(), m_Device); - descriptorImpl.Create(m_Handle); - descriptor = (Descriptor*)&descriptorImpl; + DescriptorVK* descriptorImpl = Allocate(m_Device.GetStdAllocator(), m_Device); + + Result result = descriptorImpl->Create(m_Handle); + + if (result == Result::SUCCESS) { + descriptor = (Descriptor*)descriptorImpl; + return Result::SUCCESS; + } + + Deallocate(m_Device.GetStdAllocator(), descriptorImpl); return Result::SUCCESS; } diff --git a/Source/VK/DeviceVK.cpp b/Source/VK/DeviceVK.cpp index 4b97d764..74c248bd 100644 --- a/Source/VK/DeviceVK.cpp +++ b/Source/VK/DeviceVK.cpp @@ -326,7 +326,7 @@ void DeviceVK::ProcessDeviceExtensions(Vector& desiredDeviceExts, b template Result DeviceVK::CreateImplementation(Interface*& entity, const Args&... args) { Implementation* implementation = Allocate(GetStdAllocator(), *this); - const Result result = implementation->Create(args...); + Result result = implementation->Create(args...); if (result == Result::SUCCESS) { entity = (Interface*)implementation; @@ -1713,21 +1713,23 @@ inline Result DeviceVK::CreateAccelerationStructure(const AccelerationStructureD inline Result DeviceVK::CreateCommandQueue(const CommandQueueVKDesc& commandQueueVKDesc, CommandQueue*& commandQueue) { ExclusiveScope lock(m_Lock); - const uint32_t commandQueueTypeIndex = (uint32_t)commandQueueVKDesc.commandQueueType; - const bool isFamilyIndexSame = m_FamilyIndices[commandQueueTypeIndex] == commandQueueVKDesc.familyIndex; - const bool isQueueSame = (VkQueue)m_Queues[commandQueueTypeIndex] == (VkQueue)commandQueueVKDesc.vkQueue; + uint32_t commandQueueTypeIndex = (uint32_t)commandQueueVKDesc.commandQueueType; + bool isFamilyIndexSame = m_FamilyIndices[commandQueueTypeIndex] == commandQueueVKDesc.familyIndex; + bool isQueueSame = (VkQueue)m_Queues[commandQueueTypeIndex] == (VkQueue)commandQueueVKDesc.vkQueue; if (isFamilyIndexSame && isQueueSame) { commandQueue = (CommandQueue*)m_Queues[commandQueueTypeIndex]; return Result::SUCCESS; } - CreateImplementation(commandQueue, commandQueueVKDesc); - Deallocate(GetStdAllocator(), m_Queues[commandQueueTypeIndex]); + Result result = CreateImplementation(commandQueue, commandQueueVKDesc); + if (result == Result::SUCCESS) { + Deallocate(GetStdAllocator(), m_Queues[commandQueueTypeIndex]); - m_FamilyIndices[commandQueueTypeIndex] = commandQueueVKDesc.familyIndex; - m_Queues[commandQueueTypeIndex] = (CommandQueueVK*)commandQueue; + m_FamilyIndices[commandQueueTypeIndex] = commandQueueVKDesc.familyIndex; + m_Queues[commandQueueTypeIndex] = (CommandQueueVK*)commandQueue; + } - return Result::SUCCESS; + return result; } inline Result DeviceVK::CreateCommandAllocator(const CommandAllocatorVKDesc& commandAllocatorVKDesc, CommandAllocator*& commandAllocator) { @@ -1756,11 +1758,11 @@ inline Result DeviceVK::CreateMemory(const MemoryVKDesc& memoryVKDesc, Memory*& inline Result DeviceVK::CreateGraphicsPipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline) { PipelineVK* implementation = Allocate(GetStdAllocator(), *this); - const Result result = implementation->CreateGraphics(vkPipeline); + Result result = implementation->CreateGraphics(vkPipeline); - if (result != Result::SUCCESS) { + if (result == Result::SUCCESS) { pipeline = (Pipeline*)implementation; - return result; + return Result::SUCCESS; } Deallocate(GetStdAllocator(), implementation); @@ -1770,11 +1772,11 @@ inline Result DeviceVK::CreateGraphicsPipeline(NRIVkPipeline vkPipeline, Pipelin inline Result DeviceVK::CreateComputePipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline) { PipelineVK* implementation = Allocate(GetStdAllocator(), *this); - const Result result = implementation->CreateCompute(vkPipeline); + Result result = implementation->CreateCompute(vkPipeline); - if (result != Result::SUCCESS) { + if (result == Result::SUCCESS) { pipeline = (Pipeline*)implementation; - return result; + return Result::SUCCESS; } Deallocate(GetStdAllocator(), implementation); diff --git a/Source/VK/MemoryVK.cpp b/Source/VK/MemoryVK.cpp index 97a1025e..e2598ef7 100644 --- a/Source/VK/MemoryVK.cpp +++ b/Source/VK/MemoryVK.cpp @@ -15,13 +15,34 @@ MemoryVK::~MemoryVK() { vk.FreeMemory(m_Device, m_Handle, m_Device.GetAllocationCallbacks()); } +Result MemoryVK::Create(const MemoryVKDesc& memoryDesc) { + m_OwnsNativeObjects = false; + + MemoryTypeUnion memoryType = {}; + + bool found = m_Device.GetMemoryTypeByIndex(memoryDesc.memoryTypeIndex, memoryType.unpacked); + RETURN_ON_FAILURE(&m_Device, found, Result::INVALID_ARGUMENT, "Can't find memory by index"); + + m_Handle = (VkDeviceMemory)memoryDesc.vkDeviceMemory; + m_MappedMemory = (uint8_t*)memoryDesc.vkMappedMemory; + m_Type = memoryType.packed; + + const auto& vk = m_Device.GetDispatchTable(); + if (!m_MappedMemory && IsHostVisibleMemory(memoryType.unpacked.location)) { + VkResult result = vk.MapMemory(m_Device, m_Handle, 0, memoryDesc.size, 0, (void**)&m_MappedMemory); + RETURN_ON_FAILURE(&m_Device, result == VK_SUCCESS, GetReturnCode(result), "vkMapMemory returned %d", (int32_t)result); + } + + return Result::SUCCESS; +} + Result MemoryVK::Create(MemoryType type, uint64_t size) { m_OwnsNativeObjects = true; m_Type = type; - MemoryTypeUnion memoryType = {type}; + MemoryTypeUnion memoryType = {type}; if (memoryType.unpacked.isDedicated) - return Result::SUCCESS; // Dedicated allocation occurs on memory binding + return Result::SUCCESS; // dedicated allocation occurs on memory binding VkMemoryAllocateFlagsInfo flagsInfo = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; flagsInfo.flags = (m_Device.m_IsDeviceAddressSupported ? VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT : 0) | VK_MEMORY_ALLOCATE_DEVICE_MASK_BIT; @@ -44,34 +65,11 @@ Result MemoryVK::Create(MemoryType type, uint64_t size) { return Result::SUCCESS; } -Result MemoryVK::Create(const MemoryVKDesc& memoryDesc) { - m_OwnsNativeObjects = false; - - MemoryTypeUnion memoryType = {}; - - bool found = m_Device.GetMemoryTypeByIndex(memoryDesc.memoryTypeIndex, memoryType.unpacked); - RETURN_ON_FAILURE(&m_Device, found, Result::INVALID_ARGUMENT, "Can't find memory by index"); - - m_Handle = (VkDeviceMemory)memoryDesc.vkDeviceMemory; - m_MappedMemory = (uint8_t*)memoryDesc.vkMappedMemory; - m_Type = memoryType.packed; - - const auto& vk = m_Device.GetDispatchTable(); - if (!m_MappedMemory && IsHostVisibleMemory(memoryType.unpacked.location)) { - VkResult result = vk.MapMemory(m_Device, m_Handle, 0, memoryDesc.size, 0, (void**)&m_MappedMemory); - RETURN_ON_FAILURE(&m_Device, result == VK_SUCCESS, GetReturnCode(result), "vkMapMemory returned %d", (int32_t)result); - } - - return Result::SUCCESS; -} - -Result MemoryVK::CreateDedicated(BufferVK& buffer) { +Result MemoryVK::CreateDedicated(const BufferVK& buffer) { m_OwnsNativeObjects = true; - RETURN_ON_FAILURE(&m_Device, m_Type != std::numeric_limits::max(), Result::FAILURE, "Can't allocate a dedicated memory: memory type is invalid."); - MemoryTypeUnion memoryType = {m_Type}; - RETURN_ON_FAILURE(&m_Device, memoryType.unpacked.isDedicated, Result::FAILURE, "Can't allocate a dedicated memory: memory type is not dedicated."); + CHECK(m_Type != std::numeric_limits::max() && memoryType.unpacked.isDedicated, "Shouldn't be there"); MemoryDesc memoryDesc = {}; m_Device.GetMemoryDesc(buffer.GetDesc(), memoryType.unpacked.location, memoryDesc); @@ -89,7 +87,6 @@ Result MemoryVK::CreateDedicated(BufferVK& buffer) { memoryInfo.allocationSize = memoryDesc.size; memoryInfo.memoryTypeIndex = memoryType.unpacked.index; - // No need to allocate more than one instance of host memory const auto& vk = m_Device.GetDispatchTable(); VkResult result = vk.AllocateMemory(m_Device, &memoryInfo, m_Device.GetAllocationCallbacks(), &m_Handle); RETURN_ON_FAILURE(&m_Device, result == VK_SUCCESS, GetReturnCode(result), "vkAllocateMemory returned %d", (int32_t)result); @@ -102,13 +99,11 @@ Result MemoryVK::CreateDedicated(BufferVK& buffer) { return Result::SUCCESS; } -Result MemoryVK::CreateDedicated(TextureVK& texture) { +Result MemoryVK::CreateDedicated(const TextureVK& texture) { m_OwnsNativeObjects = true; - RETURN_ON_FAILURE(&m_Device, m_Type != std::numeric_limits::max(), Result::FAILURE, "Can't allocate a dedicated memory: invalid memory type."); - const MemoryTypeUnion memoryType = {m_Type}; - RETURN_ON_FAILURE(&m_Device, memoryType.unpacked.isDedicated, Result::FAILURE, "Can't allocate a dedicated memory: the memory type is not dedicated."); + CHECK(m_Type != std::numeric_limits::max() && memoryType.unpacked.isDedicated, "Shouldn't be there"); MemoryDesc memoryDesc = {}; m_Device.GetMemoryDesc(texture.GetDesc(), memoryType.unpacked.location, memoryDesc); diff --git a/Source/VK/MemoryVK.h b/Source/VK/MemoryVK.h index dabb6547..c1831b94 100644 --- a/Source/VK/MemoryVK.h +++ b/Source/VK/MemoryVK.h @@ -36,10 +36,10 @@ struct MemoryVK { ~MemoryVK(); - Result Create(MemoryType type, uint64_t size); Result Create(const MemoryVKDesc& memoryDesc); - Result CreateDedicated(BufferVK& buffer); - Result CreateDedicated(TextureVK& texture); + Result Create(MemoryType type, uint64_t size); + Result CreateDedicated(const BufferVK& buffer); + Result CreateDedicated(const TextureVK& texture); //================================================================================================================ // NRI diff --git a/Source/VK/PipelineVK.cpp b/Source/VK/PipelineVK.cpp index db8289be..f151eb21 100644 --- a/Source/VK/PipelineVK.cpp +++ b/Source/VK/PipelineVK.cpp @@ -381,6 +381,9 @@ Result PipelineVK::Create(const RayTracingPipelineDesc& rayTracingPipelineDesc) } Result PipelineVK::CreateGraphics(NRIVkPipeline vkPipeline) { + if (!vkPipeline) + return Result::INVALID_ARGUMENT; + m_OwnsNativeObjects = false; m_Handle = (VkPipeline)vkPipeline; m_BindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; @@ -389,6 +392,9 @@ Result PipelineVK::CreateGraphics(NRIVkPipeline vkPipeline) { } Result PipelineVK::CreateCompute(NRIVkPipeline vkPipeline) { + if (!vkPipeline) + return Result::INVALID_ARGUMENT; + m_OwnsNativeObjects = false; m_Handle = (VkPipeline)vkPipeline; m_BindPoint = VK_PIPELINE_BIND_POINT_COMPUTE;