diff --git a/Include/Extensions/NRIRayTracing.h b/Include/Extensions/NRIRayTracing.h index 86ae6140..20e14568 100644 --- a/Include/Extensions/NRIRayTracing.h +++ b/Include/Extensions/NRIRayTracing.h @@ -203,7 +203,7 @@ NRI_STRUCT(DispatchRaysIndirectDesc) NRI_STRUCT(RayTracingInterface) { // Get - void (NRI_CALL *GetAccelerationStructureMemoryInfo)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure, NRI_NAME_REF(MemoryDesc) memoryDesc); + void (NRI_CALL *GetAccelerationStructureMemoryDesc)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure, NRI_NAME_REF(MemoryDesc) memoryDesc); uint64_t (NRI_CALL *GetAccelerationStructureUpdateScratchBufferSize)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure); uint64_t (NRI_CALL *GetAccelerationStructureBuildScratchBufferSize)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure); uint64_t (NRI_CALL *GetAccelerationStructureHandle)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure); diff --git a/Include/NRI.h b/Include/NRI.h index 4c8ac70e..5c8c5638 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 137 -#define NRI_VERSION_DATE "21 July 2024" +#define NRI_VERSION_MINOR 138 +#define NRI_VERSION_DATE "22 July 2024" #ifdef _WIN32 #define NRI_CALL __fastcall @@ -56,8 +56,8 @@ NRI_STRUCT(CoreInterface) const NRI_NAME_REF(TextureDesc) (NRI_CALL *GetTextureDesc)(const NRI_NAME_REF(Texture) texture); NRI_NAME(FormatSupportBits) (NRI_CALL *GetFormatSupport)(const NRI_NAME_REF(Device) device, NRI_NAME(Format) format); uint32_t (NRI_CALL *GetQuerySize)(const NRI_NAME_REF(QueryPool) queryPool); - void (NRI_CALL *GetBufferMemoryInfo)(const NRI_NAME_REF(Buffer) buffer, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc); - void (NRI_CALL *GetTextureMemoryInfo)(const NRI_NAME_REF(Texture) texture, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc); + void (NRI_CALL *GetBufferMemoryDesc)(const NRI_NAME_REF(Device) device, const NRI_NAME_REF(BufferDesc) bufferDesc, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc); + void (NRI_CALL *GetTextureMemoryDesc)(const NRI_NAME_REF(Device) device, const NRI_NAME_REF(TextureDesc) textureDesc, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc); NRI_NAME(Result) (NRI_CALL *GetCommandQueue)(NRI_NAME_REF(Device) device, NRI_NAME(CommandQueueType) commandQueueType, NRI_NAME_REF(CommandQueue*) commandQueue); // Create diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index d9e05c57..8c660f4f 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -58,29 +58,6 @@ NRI_ENUM MAX_NUM ); -NRI_ENUM -( - Vendor, uint8_t, - - UNKNOWN, - NVIDIA, - AMD, - INTEL, - - MAX_NUM -); - -NRI_ENUM -( - GraphicsAPI, uint8_t, - - D3D11, - D3D12, - VULKAN, - - MAX_NUM -); - // left -> right : low -> high bits // Expected (but not guaranteed) "FormatSupportBits" are provided, but "GetFormatSupport" should be used for querying real HW support // To demote sRGB use the previous format, i.e. "format - 1" @@ -1404,6 +1381,40 @@ NRI_STRUCT(PipelineStatisticsDesc) //=============================================================================================================================== #pragma region [ Device desc ] +NRI_ENUM +( + GraphicsAPI, uint8_t, + + D3D11, + D3D12, + VULKAN, + + MAX_NUM +); + +NRI_ENUM +( + Vendor, uint8_t, + + UNKNOWN, + NVIDIA, + AMD, + INTEL, + + MAX_NUM +); + +// Provided for convinience, when a memory chunk needs to be allocated in advance and it's unclear which resource categories can be placed together +NRI_ENUM +( + MemoryTier, uint8_t, + + ONE, // A memory can only support resources from a single resource category (buffers, color and depth-stencil attachments, all other textures) + TWO, // A memory can support resources from all 3 categories + + MAX_NUM +); + NRI_STRUCT(AdapterDesc) { char description[128]; @@ -1464,6 +1475,7 @@ NRI_STRUCT(DeviceDesc) uint32_t bufferTextureGranularity; uint64_t bufferMaxSize; uint32_t pushConstantsMaxSize; + NRI_NAME(MemoryTier) memoryTier; // Shader resources uint32_t boundDescriptorSetMaxNum; diff --git a/Resources/Version.h b/Resources/Version.h index d73889ed..a03ac51b 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 137 +#define VERSION_MINOR 138 #define VERSION_BUILD 0 #define VERSION_REVISION 0 diff --git a/Source/D3D11/BufferD3D11.cpp b/Source/D3D11/BufferD3D11.cpp index 4fc71a94..c063c9f1 100644 --- a/Source/D3D11/BufferD3D11.cpp +++ b/Source/D3D11/BufferD3D11.cpp @@ -234,23 +234,6 @@ TextureD3D11& BufferD3D11::RecreateReadbackTexture(const TextureD3D11& srcTextur // NRI //================================================================================================================ -inline void BufferD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { - const bool isConstantBuffer = (m_Desc.usageMask & BufferUsageBits::CONSTANT_BUFFER) == (uint32_t)BufferUsageBits::CONSTANT_BUFFER; - - uint32_t alignment = 65536; - if (isConstantBuffer) - alignment = 256; - else if (m_Desc.size <= 4096) - alignment = 4096; - - uint64_t size = Align(m_Desc.size, alignment); - - memoryDesc.type = (MemoryType)memoryLocation; - memoryDesc.size = size; - memoryDesc.alignment = alignment; - memoryDesc.mustBeDedicated = false; -} - inline void* BufferD3D11::Map(uint64_t offset, uint64_t size) { MaybeUnused(size); diff --git a/Source/D3D11/BufferD3D11.h b/Source/D3D11/BufferD3D11.h index daa842df..c700b404 100644 --- a/Source/D3D11/BufferD3D11.h +++ b/Source/D3D11/BufferD3D11.h @@ -59,7 +59,6 @@ struct BufferD3D11 { SET_D3D_DEBUG_OBJECT_NAME(m_Buffer, name); } - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; void* Map(uint64_t offset, uint64_t size); void Unmap(); diff --git a/Source/D3D11/BufferD3D11.hpp b/Source/D3D11/BufferD3D11.hpp index d2190ae3..ece90bec 100644 --- a/Source/D3D11/BufferD3D11.hpp +++ b/Source/D3D11/BufferD3D11.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetBufferNativeObject(const Buffer& buffer) { return uint64_t((ID3D11Buffer*)((BufferD3D11&)buffer)); } -static void NRI_CALL GetBufferMemoryInfo(const Buffer& buffer, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((BufferD3D11&)buffer).GetMemoryInfo(memoryLocation, memoryDesc); -} - static void* NRI_CALL MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) { return ((BufferD3D11&)buffer).Map(offset, size); } diff --git a/Source/D3D11/DeviceD3D11.cpp b/Source/D3D11/DeviceD3D11.cpp index e99ea9e5..17810a91 100644 --- a/Source/D3D11/DeviceD3D11.cpp +++ b/Source/D3D11/DeviceD3D11.cpp @@ -335,6 +335,7 @@ void DeviceD3D11::FillDesc() { m_Desc.bufferTextureGranularity = 1; m_Desc.bufferMaxSize = D3D11_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_C_TERM * 1024ull * 1024ull; m_Desc.pushConstantsMaxSize = D3D11_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16; + m_Desc.memoryTier = MemoryTier::ONE; m_Desc.boundDescriptorSetMaxNum = D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; m_Desc.perStageDescriptorSamplerMaxNum = D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; @@ -446,6 +447,39 @@ Result DeviceD3D11::CreateImplementation(Interface*& entity, const Args&... args return result; } +void DeviceD3D11::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { + const bool isConstantBuffer = (bufferDesc.usageMask & BufferUsageBits::CONSTANT_BUFFER) == (uint32_t)BufferUsageBits::CONSTANT_BUFFER; + + uint32_t alignment = 65536; + if (isConstantBuffer) + alignment = 256; + else if (bufferDesc.size <= 4096) + alignment = 4096; + + memoryDesc.type = (MemoryType)memoryLocation; + memoryDesc.size = Align(bufferDesc.size, alignment); + memoryDesc.alignment = alignment; + memoryDesc.mustBeDedicated = false; +} + +void DeviceD3D11::GetMemoryDesc(const TextureDesc& textureDesc,MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { + bool isMultisampled = textureDesc.sampleNum > 1; + uint32_t size = TextureD3D11::GetMipmappedSize(textureDesc); + + uint32_t alignment = 65536; + if (isMultisampled) + alignment = 4194304; + else if (size <= 65536) + alignment = 65536; + + size = Align(size, alignment); + + memoryDesc.type = (MemoryType)memoryLocation; + memoryDesc.size = size; + memoryDesc.alignment = alignment; + memoryDesc.mustBeDedicated = false; +} + //================================================================================================================ // DeviceBase //================================================================================================================ diff --git a/Source/D3D11/DeviceD3D11.h b/Source/D3D11/DeviceD3D11.h index 07addd4a..1b50b419 100644 --- a/Source/D3D11/DeviceD3D11.h +++ b/Source/D3D11/DeviceD3D11.h @@ -49,8 +49,6 @@ struct DeviceD3D11 final : public DeviceBase { return m_IsDeferredContextEmulated; } - Result Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11Device* precreatedDevice, AGSContext* agsContext, bool isNVAPILoadedInApp); - inline void EnterCriticalSection() { if (m_Multithread) m_Multithread->Enter(); @@ -65,6 +63,10 @@ struct DeviceD3D11 final : public DeviceBase { ::LeaveCriticalSection(&m_CriticalSection); } + Result Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11Device* precreatedDevice, AGSContext* agsContext, bool isNVAPILoadedInApp); + void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + //================================================================================================================ // NRI //================================================================================================================ diff --git a/Source/D3D11/DeviceD3D11.hpp b/Source/D3D11/DeviceD3D11.hpp index ef1e1d47..64a5addd 100644 --- a/Source/D3D11/DeviceD3D11.hpp +++ b/Source/D3D11/DeviceD3D11.hpp @@ -16,6 +16,18 @@ static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { return ((const TextureD3D11&)texture).GetDesc(); } +static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { + return ((const DeviceD3D11&)device).GetFormatSupport(format); +} + +static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((const DeviceD3D11&)device).GetMemoryDesc(bufferDesc, memoryLocation, memoryDesc); +} + +static void NRI_CALL GetTextureMemoryDesc(const Device& device, const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((const DeviceD3D11&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc); +} + static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { return ((DeviceD3D11&)device).GetCommandQueue(commandQueueType, commandQueue); } @@ -173,10 +185,6 @@ static void NRI_CALL FreeMemory(Memory& memory) { device.FreeMemory(memory); } -static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { - return ((const DeviceD3D11&)device).GetFormatSupport(format); -} - static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { ((DeviceD3D11&)device).SetDebugName(name); } @@ -208,6 +216,8 @@ Result DeviceD3D11::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface.GetBufferDesc = ::GetBufferDesc; coreInterface.GetTextureDesc = ::GetTextureDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; + coreInterface.GetBufferMemoryDesc = ::GetBufferMemoryDesc; + coreInterface.GetTextureMemoryDesc = ::GetTextureMemoryDesc; coreInterface.GetCommandQueue = ::GetCommandQueue; coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; diff --git a/Source/D3D11/TextureD3D11.cpp b/Source/D3D11/TextureD3D11.cpp index 6e9d7327..cc35b01d 100644 --- a/Source/D3D11/TextureD3D11.cpp +++ b/Source/D3D11/TextureD3D11.cpp @@ -75,7 +75,7 @@ Result TextureD3D11::Create(const MemoryD3D11* memory) { RETURN_ON_BAD_HRESULT(&m_Device, hr, "ID3D11Device::CreateTextureXx()"); - uint64_t size = GetMipmappedSize(); + uint64_t size = GetMipmappedSize(m_Desc); uint32_t priority = memory ? memory->GetResidencyPriority(size) : 0; if (priority != 0) m_Texture->SetEvictionPriority(priority); @@ -94,22 +94,13 @@ Result TextureD3D11::Create(const TextureD3D11Desc& textureDesc) { return Result::SUCCESS; } -uint32_t TextureD3D11::GetMipmappedSize(uint32_t w, uint32_t h, uint32_t d, Mip_t mipNum, Mip_t mipOffset) const { - if (!mipNum) - mipNum = m_Desc.mipNum; - - bool isCompressed = m_Desc.format >= Format::BC1_RGBA_UNORM && m_Desc.format <= Format::BC7_RGBA_SRGB; - bool isCustom = w || h || d; - - if (!w) - w = GetSize(0, mipOffset); - - if (!h) - h = GetSize(1, mipOffset); - - if (!d) - d = GetSize(2, mipOffset); +uint32_t TextureD3D11::GetMipmappedSize(const TextureDesc& textureDesc) { + bool isCompressed = textureDesc.format >= Format::BC1_RGBA_UNORM && textureDesc.format <= Format::BC7_RGBA_SRGB; + uint32_t w = GetDimension(GraphicsAPI::D3D11, textureDesc, 0, 0); + uint32_t h = GetDimension(GraphicsAPI::D3D11, textureDesc, 1, 0); + uint32_t d = GetDimension(GraphicsAPI::D3D11, textureDesc, 2, 0); + uint32_t mipNum = textureDesc.mipNum; uint32_t size = 0; while (mipNum) { @@ -133,13 +124,10 @@ uint32_t TextureD3D11::GetMipmappedSize(uint32_t w, uint32_t h, uint32_t d, Mip_ mipNum--; } - const FormatProps& formatProps = GetFormatProps(m_Desc.format); + const FormatProps& formatProps = GetFormatProps(textureDesc.format); size *= formatProps.stride; - - if (!isCustom) { - size *= m_Desc.sampleNum; - size *= m_Desc.arraySize; - } + size *= std::max(textureDesc.sampleNum, (Sample_t)1); + size *= std::max(textureDesc.arraySize, (Dim_t)1); return size; } @@ -148,22 +136,4 @@ uint32_t TextureD3D11::GetMipmappedSize(uint32_t w, uint32_t h, uint32_t d, Mip_ // NRI //================================================================================================================ -inline void TextureD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { - bool isMultisampled = m_Desc.sampleNum > 1; - uint32_t size = GetMipmappedSize(); - - uint32_t alignment = 65536; - if (isMultisampled) - alignment = 4194304; - else if (size <= 65536) - alignment = 65536; - - size = Align(size, alignment); - - memoryDesc.type = (MemoryType)memoryLocation; - memoryDesc.size = size; - memoryDesc.alignment = alignment; - memoryDesc.mustBeDedicated = false; -} - #include "TextureD3D11.hpp" diff --git a/Source/D3D11/TextureD3D11.h b/Source/D3D11/TextureD3D11.h index 992c8a16..05ed8821 100644 --- a/Source/D3D11/TextureD3D11.h +++ b/Source/D3D11/TextureD3D11.h @@ -51,7 +51,8 @@ struct TextureD3D11 { Result Create(const MemoryD3D11* memory); Result Create(const TextureD3D11Desc& textureDesc); - uint32_t GetMipmappedSize(uint32_t w = 0, uint32_t h = 0, uint32_t d = 0, Mip_t mipNum = 0, Mip_t mipOffset = 0) const; + + static uint32_t GetMipmappedSize(const TextureDesc& textureDesc); //================================================================================================================ // NRI @@ -61,8 +62,6 @@ struct TextureD3D11 { SET_D3D_DEBUG_OBJECT_NAME(m_Texture, name); } - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - private: DeviceD3D11& m_Device; ComPtr m_Texture; diff --git a/Source/D3D11/TextureD3D11.hpp b/Source/D3D11/TextureD3D11.hpp index 0969f907..fc6f189c 100644 --- a/Source/D3D11/TextureD3D11.hpp +++ b/Source/D3D11/TextureD3D11.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetTextureNativeObject(const Texture& texture) { return uint64_t((ID3D11Resource*)((TextureD3D11&)texture)); } -static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((TextureD3D11&)texture).GetMemoryInfo(memoryLocation, memoryDesc); -} - #pragma endregion Define_Core_Texture_PartiallyFillFunctionTable(D3D11); diff --git a/Source/D3D12/AccelerationStructureD3D12.cpp b/Source/D3D12/AccelerationStructureD3D12.cpp index 3db54fec..90b40986 100644 --- a/Source/D3D12/AccelerationStructureD3D12.cpp +++ b/Source/D3D12/AccelerationStructureD3D12.cpp @@ -49,8 +49,8 @@ Result AccelerationStructureD3D12::Create(const AccelerationStructureDesc& accel return result; } -void AccelerationStructureD3D12::GetMemoryInfo(MemoryDesc& memoryDesc) const { - m_Device.GetMemoryInfoForAccelerationStructure(m_PrebuildInfo.ResultDataMaxSizeInBytes, memoryDesc); +void AccelerationStructureD3D12::GetMemoryDesc(MemoryDesc& memoryDesc) const { + m_Device.GetAccelerationStructureMemoryDesc(m_PrebuildInfo.ResultDataMaxSizeInBytes, memoryDesc); } uint64_t AccelerationStructureD3D12::GetUpdateScratchBufferSize() const { diff --git a/Source/D3D12/AccelerationStructureD3D12.h b/Source/D3D12/AccelerationStructureD3D12.h index 666e74db..18e8f26e 100644 --- a/Source/D3D12/AccelerationStructureD3D12.h +++ b/Source/D3D12/AccelerationStructureD3D12.h @@ -23,7 +23,7 @@ struct AccelerationStructureD3D12 { Result Create(const AccelerationStructureDesc& accelerationStructureDesc); Result Create(const AccelerationStructureD3D12Desc& accelerationStructureDesc); - void GetMemoryInfo(MemoryDesc& memoryDesc) const; + void GetMemoryDesc(MemoryDesc& memoryDesc) const; uint64_t GetUpdateScratchBufferSize() const; uint64_t GetBuildScratchBufferSize() const; Result BindMemory(Memory* memory, uint64_t offset); diff --git a/Source/D3D12/AccelerationStructureD3D12.hpp b/Source/D3D12/AccelerationStructureD3D12.hpp index ed4e657b..b550336e 100644 --- a/Source/D3D12/AccelerationStructureD3D12.hpp +++ b/Source/D3D12/AccelerationStructureD3D12.hpp @@ -6,8 +6,8 @@ static Result NRI_CALL CreateAccelerationStructureDescriptor(const AccelerationS return ((AccelerationStructureD3D12&)accelerationStructure).CreateDescriptor(descriptor); } -static void NRI_CALL GetAccelerationStructureMemoryInfo(const AccelerationStructure& accelerationStructure, MemoryDesc& memoryDesc) { - ((AccelerationStructureD3D12&)accelerationStructure).GetMemoryInfo(memoryDesc); +static void NRI_CALL GetAccelerationStructureMemoryDesc(const AccelerationStructure& accelerationStructure, MemoryDesc& memoryDesc) { + ((AccelerationStructureD3D12&)accelerationStructure).GetMemoryDesc(memoryDesc); } static uint64_t NRI_CALL GetAccelerationStructureUpdateScratchBufferSize(const AccelerationStructure& accelerationStructure) { diff --git a/Source/D3D12/BufferD3D12.cpp b/Source/D3D12/BufferD3D12.cpp index 17843d5c..42fb5d43 100644 --- a/Source/D3D12/BufferD3D12.cpp +++ b/Source/D3D12/BufferD3D12.cpp @@ -7,7 +7,7 @@ using namespace nri; -static inline void GetResourceDesc(D3D12_RESOURCE_DESC* desc, const BufferDesc& bufferDesc) { +void nri::GetResourceDesc(D3D12_RESOURCE_DESC* desc, const BufferDesc& bufferDesc) { desc->Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; desc->Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; // 64KB desc->Width = bufferDesc.size; @@ -96,13 +96,6 @@ Result BufferD3D12::BindMemory(const MemoryD3D12* memory, uint64_t offset, bool // NRI //================================================================================================================ -inline void BufferD3D12::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { - D3D12_RESOURCE_DESC desc = {}; - GetResourceDesc(&desc, m_Desc); - - m_Device.GetMemoryInfo(memoryLocation, desc, memoryDesc); -} - inline void* BufferD3D12::Map(uint64_t offset, uint64_t size) { uint8_t* data = nullptr; diff --git a/Source/D3D12/BufferD3D12.h b/Source/D3D12/BufferD3D12.h index 00031dd7..0a4ebefd 100644 --- a/Source/D3D12/BufferD3D12.h +++ b/Source/D3D12/BufferD3D12.h @@ -50,7 +50,6 @@ struct BufferD3D12 { SET_D3D_DEBUG_OBJECT_NAME(m_Buffer, name); } - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; void* Map(uint64_t offset, uint64_t size); void Unmap(); diff --git a/Source/D3D12/BufferD3D12.hpp b/Source/D3D12/BufferD3D12.hpp index b0c77e6c..512a1664 100644 --- a/Source/D3D12/BufferD3D12.hpp +++ b/Source/D3D12/BufferD3D12.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetBufferNativeObject(const Buffer& buffer) { return uint64_t((ID3D12Resource*)((BufferD3D12&)buffer)); } -static void NRI_CALL GetBufferMemoryInfo(const Buffer& buffer, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((BufferD3D12&)buffer).GetMemoryInfo(memoryLocation, memoryDesc); -} - static void* NRI_CALL MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) { return ((BufferD3D12&)buffer).Map(offset, size); } diff --git a/Source/D3D12/DeviceD3D12.cpp b/Source/D3D12/DeviceD3D12.cpp index 907b2f65..d8f028d1 100644 --- a/Source/D3D12/DeviceD3D12.cpp +++ b/Source/D3D12/DeviceD3D12.cpp @@ -287,7 +287,7 @@ void DeviceD3D12::FillDesc(bool enableDrawParametersEmulation) { hr = m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options)); if (FAILED(hr)) REPORT_WARNING(this, "ID3D12Device::CheckFeatureSupport(options) failed, result = 0x%08X!", hr); - m_IsResourceHeapTier2Supported = options.ResourceHeapTier == D3D12_RESOURCE_HEAP_TIER_2; + m_Desc.memoryTier = options.ResourceHeapTier == D3D12_RESOURCE_HEAP_TIER_2 ? MemoryTier::TWO : MemoryTier::ONE; D3D12_FEATURE_DATA_D3D12_OPTIONS1 options1 = {}; hr = m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &options1, sizeof(options1)); @@ -667,14 +667,14 @@ static inline MemoryType ConstructMemoryType(D3D12_HEAP_TYPE heapType, D3D12_HEA return ((uint32_t)heapFlags) | ((uint32_t)heapType << 16); } -void DeviceD3D12::GetMemoryInfo(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc, MemoryDesc& memoryDesc) const { +void DeviceD3D12::GetMemoryDesc(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc, MemoryDesc& memoryDesc) const { if (memoryLocation == MemoryLocation::DEVICE_UPLOAD && m_Desc.deviceUploadHeapSize == 0) memoryLocation = MemoryLocation::HOST_UPLOAD; D3D12_HEAP_TYPE heapType = HEAP_TYPES[(uint32_t)memoryLocation]; - D3D12_HEAP_FLAGS heapFlags = D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES; - if (!m_IsResourceHeapTier2Supported) { + D3D12_HEAP_FLAGS heapFlags = D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES; + if (m_Desc.memoryTier == MemoryTier::ONE) { if (resourceDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) heapFlags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS; else if (resourceDesc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) @@ -692,8 +692,8 @@ void DeviceD3D12::GetMemoryInfo(MemoryLocation memoryLocation, const D3D12_RESOU memoryDesc.mustBeDedicated = IsDedicated(memoryType); } -void DeviceD3D12::GetMemoryInfoForAccelerationStructure(uint64_t size, MemoryDesc& memoryDesc) const { - D3D12_HEAP_FLAGS heapFlags = m_IsResourceHeapTier2Supported ? D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES : D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS; +void DeviceD3D12::GetAccelerationStructureMemoryDesc(uint64_t size, MemoryDesc& memoryDesc) const { + D3D12_HEAP_FLAGS heapFlags = m_Desc.memoryTier == MemoryTier::TWO ? D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES : D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS; memoryDesc.size = size; memoryDesc.alignment = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT; @@ -705,7 +705,7 @@ bool DeviceD3D12::IsDedicated(MemoryType memoryType) const { D3D12_HEAP_FLAGS heapFlags = GetHeapFlags(memoryType); bool isRtDs = (heapFlags & D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES) == D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES; - return !m_IsResourceHeapTier2Supported && isRtDs; + return m_Desc.memoryTier == MemoryTier::ONE && isRtDs; } ComPtr DeviceD3D12::CreateCommandSignature( diff --git a/Source/D3D12/DeviceD3D12.h b/Source/D3D12/DeviceD3D12.h index 56a2be8c..7802cb96 100644 --- a/Source/D3D12/DeviceD3D12.h +++ b/Source/D3D12/DeviceD3D12.h @@ -64,8 +64,8 @@ struct DeviceD3D12 final : public DeviceBase { Result CreateCpuOnlyVisibleDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE type); Result GetDescriptorHandle(D3D12_DESCRIPTOR_HEAP_TYPE type, DescriptorHandle& descriptorHandle); DescriptorPointerCPU GetDescriptorPointerCPU(const DescriptorHandle& descriptorHandle); - void GetMemoryInfo(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc, MemoryDesc& memoryDesc) const; - void GetMemoryInfoForAccelerationStructure(uint64_t size, MemoryDesc& memoryDesc) const; + void GetMemoryDesc(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc, MemoryDesc& memoryDesc) const; + void GetAccelerationStructureMemoryDesc(uint64_t size, MemoryDesc& memoryDesc) const; bool IsDedicated(MemoryType memoryType) const; ID3D12CommandSignature* GetDrawCommandSignature(uint32_t stride, ID3D12RootSignature* rootSignature); @@ -170,7 +170,6 @@ struct DeviceD3D12 final : public DeviceBase { CoreInterface m_CoreInterface = {}; uint8_t m_Version = 0; bool m_IsWrapped = false; - bool m_IsResourceHeapTier2Supported = false; std::array m_FreeDescriptorLocks; Lock m_DescriptorHeapLock; Lock m_QueueLock; diff --git a/Source/D3D12/DeviceD3D12.hpp b/Source/D3D12/DeviceD3D12.hpp index ddba7c2c..319e3649 100644 --- a/Source/D3D12/DeviceD3D12.hpp +++ b/Source/D3D12/DeviceD3D12.hpp @@ -16,6 +16,24 @@ static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { return ((const TextureD3D12&)texture).GetDesc(); } +static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { + return ((const DeviceD3D12&)device).GetFormatSupport(format); +} + +static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + D3D12_RESOURCE_DESC desc = {}; + GetResourceDesc(&desc, bufferDesc); + + ((const DeviceD3D12&)device).GetMemoryDesc(memoryLocation, desc, memoryDesc); +} + +static void NRI_CALL GetTextureMemoryDesc(const Device& device, const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + D3D12_RESOURCE_DESC desc = {}; + GetResourceDesc(&desc, textureDesc); + + ((const DeviceD3D12&)device).GetMemoryDesc(memoryLocation, desc, memoryDesc); +} + static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { return ((DeviceD3D12&)device).GetCommandQueue(commandQueueType, commandQueue); } @@ -173,10 +191,6 @@ static void NRI_CALL FreeMemory(Memory& memory) { device.FreeMemory(memory); } -static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { - return ((const DeviceD3D12&)device).GetFormatSupport(format); -} - static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { ((DeviceD3D12&)device).SetDebugName(name); } @@ -206,6 +220,8 @@ Result DeviceD3D12::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface.GetBufferDesc = ::GetBufferDesc; coreInterface.GetTextureDesc = ::GetTextureDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; + coreInterface.GetBufferMemoryDesc = ::GetBufferMemoryDesc; + coreInterface.GetTextureMemoryDesc = ::GetTextureMemoryDesc; coreInterface.GetCommandQueue = ::GetCommandQueue; coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; diff --git a/Source/D3D12/SharedD3D12.h b/Source/D3D12/SharedD3D12.h index 089eb256..9b55aa3b 100644 --- a/Source/D3D12/SharedD3D12.h +++ b/Source/D3D12/SharedD3D12.h @@ -36,6 +36,9 @@ D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE GetAccelerationStructureType(Accele D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS GetAccelerationStructureBuildFlags(AccelerationStructureBuildBits accelerationStructureBuildFlags); D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE GetCopyMode(CopyMode copyMode); +void GetResourceDesc(D3D12_RESOURCE_DESC* desc, const BufferDesc& bufferDesc); +void GetResourceDesc(D3D12_RESOURCE_DESC* desc, const TextureDesc& textureDesc); + D3D12_RESOURCE_FLAGS GetBufferFlags(BufferUsageBits bufferUsageMask); D3D12_RESOURCE_FLAGS GetTextureFlags(TextureUsageBits textureUsageMask); diff --git a/Source/D3D12/TextureD3D12.cpp b/Source/D3D12/TextureD3D12.cpp index 1990bc6a..ffeafff3 100644 --- a/Source/D3D12/TextureD3D12.cpp +++ b/Source/D3D12/TextureD3D12.cpp @@ -7,7 +7,7 @@ using namespace nri; -static inline void GetResourceDesc(D3D12_RESOURCE_DESC* desc, const TextureDesc& textureDesc) { +void nri::GetResourceDesc(D3D12_RESOURCE_DESC* desc, const TextureDesc& textureDesc) { uint16_t blockWidth = (uint16_t)GetFormatProps(textureDesc.format).blockWidth; desc->Dimension = GetResourceDimension(textureDesc.type); @@ -94,11 +94,4 @@ Result TextureD3D12::BindMemory(const MemoryD3D12* memory, uint64_t offset) { // NRI //================================================================================================================ -inline void TextureD3D12::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { - D3D12_RESOURCE_DESC desc = {}; - GetResourceDesc(&desc, m_Desc); - - m_Device.GetMemoryInfo(memoryLocation, desc, memoryDesc); -} - #include "TextureD3D12.hpp" diff --git a/Source/D3D12/TextureD3D12.h b/Source/D3D12/TextureD3D12.h index 0e109d6d..445e4ace 100644 --- a/Source/D3D12/TextureD3D12.h +++ b/Source/D3D12/TextureD3D12.h @@ -56,8 +56,6 @@ struct TextureD3D12 { SET_D3D_DEBUG_OBJECT_NAME(m_Texture, name); } - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - private: DeviceD3D12& m_Device; TextureDesc m_Desc = {}; diff --git a/Source/D3D12/TextureD3D12.hpp b/Source/D3D12/TextureD3D12.hpp index 87d7ae22..46d9e1b3 100644 --- a/Source/D3D12/TextureD3D12.hpp +++ b/Source/D3D12/TextureD3D12.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetTextureNativeObject(const Texture& texture) { return uint64_t((ID3D12Resource*)((TextureD3D12&)texture)); } -static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((TextureD3D12&)texture).GetMemoryInfo(memoryLocation, memoryDesc); -} - #pragma endregion Define_Core_Texture_PartiallyFillFunctionTable(D3D12); diff --git a/Source/Shared/DeviceBase.h b/Source/Shared/DeviceBase.h index 08208a23..c8c9fa48 100644 --- a/Source/Shared/DeviceBase.h +++ b/Source/Shared/DeviceBase.h @@ -112,7 +112,6 @@ struct DeviceBase { void Core_Buffer_PartiallyFillFunctionTable##API(CoreInterface& table) { \ table.SetBufferDebugName = ::SetBufferDebugName; \ table.GetBufferNativeObject = ::GetBufferNativeObject; \ - table.GetBufferMemoryInfo = ::GetBufferMemoryInfo; \ table.MapBuffer = ::MapBuffer; \ table.UnmapBuffer = ::UnmapBuffer; \ } @@ -212,7 +211,6 @@ struct DeviceBase { void Core_Texture_PartiallyFillFunctionTable##API(CoreInterface& table) { \ table.SetTextureDebugName = ::SetTextureDebugName; \ table.GetTextureNativeObject = ::GetTextureNativeObject; \ - table.GetTextureMemoryInfo = ::GetTextureMemoryInfo; \ } #define Define_SwapChain_PartiallyFillFunctionTable(API) \ @@ -240,7 +238,7 @@ struct DeviceBase { #define Define_RayTracing_AccelerationStructure_PartiallyFillFunctionTable(API) \ void RayTracing_AccelerationStructure_PartiallyFillFunctionTable##API(RayTracingInterface& table) { \ table.CreateAccelerationStructureDescriptor = ::CreateAccelerationStructureDescriptor; \ - table.GetAccelerationStructureMemoryInfo = ::GetAccelerationStructureMemoryInfo; \ + table.GetAccelerationStructureMemoryDesc = ::GetAccelerationStructureMemoryDesc; \ table.GetAccelerationStructureUpdateScratchBufferSize = ::GetAccelerationStructureUpdateScratchBufferSize; \ table.GetAccelerationStructureBuildScratchBufferSize = ::GetAccelerationStructureBuildScratchBufferSize; \ table.GetAccelerationStructureHandle = ::GetAccelerationStructureHandle; \ diff --git a/Source/Shared/HelperDataUpload.cpp b/Source/Shared/HelperDataUpload.cpp index 80cf249f..ab7208a3 100644 --- a/Source/Shared/HelperDataUpload.cpp +++ b/Source/Shared/HelperDataUpload.cpp @@ -101,12 +101,13 @@ Result HelperDataUpload::UploadData( Result HelperDataUpload::Create() { BufferDesc bufferDesc = {}; bufferDesc.size = m_UploadBufferSize; + Result result = NRI.CreateBuffer(m_Device, bufferDesc, m_UploadBuffer); if (result != Result::SUCCESS) return result; MemoryDesc memoryDesc = {}; - NRI.GetBufferMemoryInfo(*m_UploadBuffer, MemoryLocation::HOST_UPLOAD, memoryDesc); + NRI.GetBufferMemoryDesc(m_Device, bufferDesc, MemoryLocation::HOST_UPLOAD, memoryDesc); result = NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, m_UploadBufferMemory); if (result != Result::SUCCESS) diff --git a/Source/Shared/HelperDeviceMemoryAllocator.cpp b/Source/Shared/HelperDeviceMemoryAllocator.cpp index 17d79d4d..30efd355 100644 --- a/Source/Shared/HelperDeviceMemoryAllocator.cpp +++ b/Source/Shared/HelperDeviceMemoryAllocator.cpp @@ -97,7 +97,8 @@ Result HelperDeviceMemoryAllocator::ProcessDedicatedResources(MemoryLocation mem MemoryDesc memoryDesc = {}; for (size_t i = 0; i < m_DedicatedBuffers.size(); i++) { - m_NRI.GetBufferMemoryInfo(*m_DedicatedBuffers[i], memoryLocation, memoryDesc); + const BufferDesc& bufferDesc = m_NRI.GetBufferDesc(*m_DedicatedBuffers[i]); + m_NRI.GetBufferMemoryDesc(m_Device, bufferDesc, memoryLocation, memoryDesc); Memory*& memory = allocations[allocationNum]; @@ -110,7 +111,8 @@ Result HelperDeviceMemoryAllocator::ProcessDedicatedResources(MemoryLocation mem } for (size_t i = 0; i < m_DedicatedTextures.size(); i++) { - m_NRI.GetTextureMemoryInfo(*m_DedicatedTextures[i], memoryLocation, memoryDesc); + const TextureDesc& textureDesc = m_NRI.GetTextureDesc(*m_DedicatedTextures[i]); + m_NRI.GetTextureMemoryDesc(m_Device, textureDesc, memoryLocation, memoryDesc); Memory*& memory = allocations[allocationNum]; @@ -130,7 +132,8 @@ void HelperDeviceMemoryAllocator::GroupByMemoryType(MemoryLocation memoryLocatio for (uint32_t i = 0; i < bufferNum; i++) { Buffer* buffer = buffers[i]; - m_NRI.GetBufferMemoryInfo(*buffer, memoryLocation, memoryDesc); + const BufferDesc& bufferDesc = m_NRI.GetBufferDesc(*buffer); + m_NRI.GetBufferMemoryDesc(m_Device, bufferDesc, memoryLocation, memoryDesc); if (memoryDesc.mustBeDedicated) m_DedicatedBuffers.push_back(buffer); @@ -153,7 +156,8 @@ void HelperDeviceMemoryAllocator::GroupByMemoryType(MemoryLocation memoryLocatio for (uint32_t i = 0; i < textureNum; i++) { Texture* texture = textures[i]; - m_NRI.GetTextureMemoryInfo(*texture, memoryLocation, memoryDesc); + const TextureDesc& textureDesc = m_NRI.GetTextureDesc(*texture); + m_NRI.GetTextureMemoryDesc(m_Device, textureDesc, memoryLocation, memoryDesc); if (memoryDesc.mustBeDedicated) m_DedicatedTextures.push_back(texture); diff --git a/Source/Shared/Streamer.cpp b/Source/Shared/Streamer.cpp index ca095268..b2df572a 100644 --- a/Source/Shared/Streamer.cpp +++ b/Source/Shared/Streamer.cpp @@ -32,7 +32,7 @@ Result StreamerImpl::Create(const StreamerDesc& desc) { // Allocate memory MemoryDesc memoryDesc = {}; - m_NRI.GetBufferMemoryInfo(*m_ConstantBuffer, desc.constantBufferMemoryLocation, memoryDesc); + m_NRI.GetBufferMemoryDesc(m_Device, bufferDesc, desc.constantBufferMemoryLocation, memoryDesc); result = m_NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, m_ConstantBufferMemory); if (result != Result::SUCCESS) @@ -131,7 +131,7 @@ Result StreamerImpl::CopyStreamerUpdateRequests() { if (m_DynamicBuffer) m_GarbageInFlight.push_back({m_DynamicBuffer, m_DynamicBufferMemory, 0}); - { // Create new dynamic buffer + { // Create new dynamic buffer & allocate memory BufferDesc bufferDesc = {}; bufferDesc.size = m_DynamicBufferSize; bufferDesc.usageMask = m_Desc.dynamicBufferUsageBits; @@ -139,13 +139,11 @@ Result StreamerImpl::CopyStreamerUpdateRequests() { Result result = m_NRI.CreateBuffer(m_Device, bufferDesc, m_DynamicBuffer); if (result != Result::SUCCESS) return result; - } - { // Allocate memory MemoryDesc memoryDesc = {}; - m_NRI.GetBufferMemoryInfo(*m_DynamicBuffer, m_Desc.dynamicBufferMemoryLocation, memoryDesc); + m_NRI.GetBufferMemoryDesc(m_Device, bufferDesc, m_Desc.dynamicBufferMemoryLocation, memoryDesc); - Result result = m_NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, m_DynamicBufferMemory); + result = m_NRI.AllocateMemory(m_Device, memoryDesc.type, memoryDesc.size, m_DynamicBufferMemory); if (result != Result::SUCCESS) return result; } diff --git a/Source/VK/AccelerationStructureVK.cpp b/Source/VK/AccelerationStructureVK.cpp index ff2316c7..e87b062b 100644 --- a/Source/VK/AccelerationStructureVK.cpp +++ b/Source/VK/AccelerationStructureVK.cpp @@ -149,8 +149,8 @@ inline void AccelerationStructureVK::SetDebugName(const char* name) { m_Buffer->SetDebugName(name); } -inline void AccelerationStructureVK::GetMemoryInfo(MemoryDesc& memoryDesc) const { - m_Buffer->GetMemoryInfo(MemoryLocation::DEVICE, memoryDesc); +inline void AccelerationStructureVK::GetMemoryDesc(MemoryDesc& memoryDesc) const { + m_Device.GetMemoryDesc(m_Buffer->GetDesc(), MemoryLocation::DEVICE, memoryDesc); } inline Result AccelerationStructureVK::CreateDescriptor(Descriptor*& descriptor) const { diff --git a/Source/VK/AccelerationStructureVK.h b/Source/VK/AccelerationStructureVK.h index 98f50496..e800b4f7 100644 --- a/Source/VK/AccelerationStructureVK.h +++ b/Source/VK/AccelerationStructureVK.h @@ -50,7 +50,7 @@ struct AccelerationStructureVK { } void SetDebugName(const char* name); - void GetMemoryInfo(MemoryDesc& memoryDesc) const; + void GetMemoryDesc(MemoryDesc& memoryDesc) const; Result CreateDescriptor(Descriptor*& descriptor) const; private: diff --git a/Source/VK/AcceleratrionStructureVK.hpp b/Source/VK/AcceleratrionStructureVK.hpp index 34e5d21d..3ec44d34 100644 --- a/Source/VK/AcceleratrionStructureVK.hpp +++ b/Source/VK/AcceleratrionStructureVK.hpp @@ -2,8 +2,8 @@ #pragma region[ RayTracing ] -static void NRI_CALL GetAccelerationStructureMemoryInfo(const AccelerationStructure& accelerationStructure, MemoryDesc& memoryDesc) { - ((AccelerationStructureVK&)accelerationStructure).GetMemoryInfo(memoryDesc); +static void NRI_CALL GetAccelerationStructureMemoryDesc(const AccelerationStructure& accelerationStructure, MemoryDesc& memoryDesc) { + ((AccelerationStructureVK&)accelerationStructure).GetMemoryDesc(memoryDesc); } static uint64_t NRI_CALL GetAccelerationStructureUpdateScratchBufferSize(const AccelerationStructure& accelerationStructure) { diff --git a/Source/VK/BufferVK.cpp b/Source/VK/BufferVK.cpp index 8ce1a745..898667b9 100644 --- a/Source/VK/BufferVK.cpp +++ b/Source/VK/BufferVK.cpp @@ -8,44 +8,6 @@ using namespace nri; -constexpr VkBufferUsageFlags GetBufferUsageFlags(BufferUsageBits bufferUsageBits, uint32_t structureStride) { - VkBufferUsageFlags flags = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; - - if (bufferUsageBits & BufferUsageBits::VERTEX_BUFFER) - flags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; - - if (bufferUsageBits & BufferUsageBits::INDEX_BUFFER) - flags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT; - - if (bufferUsageBits & BufferUsageBits::CONSTANT_BUFFER) - flags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; - - if (bufferUsageBits & BufferUsageBits::ARGUMENT_BUFFER) - flags |= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT; - - if (bufferUsageBits & BufferUsageBits::RAY_TRACING_BUFFER) // TODO: add more usage bits? - flags |= VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; - - if (bufferUsageBits & BufferUsageBits::ACCELERATION_STRUCTURE_BUILD_READ) - flags |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR; - - if (bufferUsageBits & BufferUsageBits::SHADER_RESOURCE) { - if (structureStride == 0) - flags |= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT; - else - flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; - } - - if (bufferUsageBits & BufferUsageBits::SHADER_RESOURCE_STORAGE) { - if (structureStride == 0 && (bufferUsageBits & BufferUsageBits::RAY_TRACING_BUFFER) == 0) - flags |= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; - else - flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; - } - - return flags; -} - BufferVK::~BufferVK() { const auto& vk = m_Device.GetDispatchTable(); @@ -57,18 +19,8 @@ Result BufferVK::Create(const BufferDesc& bufferDesc) { m_OwnsNativeObjects = true; m_Desc = bufferDesc; - const VkSharingMode sharingMode = m_Device.IsConcurrentSharingModeEnabledForBuffers() ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE; - const Vector& queueIndices = m_Device.GetConcurrentSharingModeQueueIndices(); - VkBufferCreateInfo info = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; - info.size = bufferDesc.size; - info.usage = GetBufferUsageFlags(bufferDesc.usageMask, bufferDesc.structureStride); - info.sharingMode = sharingMode; - info.queueFamilyIndexCount = (uint32_t)queueIndices.size(); - info.pQueueFamilyIndices = queueIndices.data(); - - if (m_Device.m_IsDeviceAddressSupported) - info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; + m_Device.FillCreateInfo(bufferDesc, info); const auto& vk = m_Device.GetDispatchTable(); VkResult result = vk.CreateBuffer(m_Device, &info, m_Device.GetAllocationCallbacks(), &m_Handle); @@ -115,30 +67,6 @@ void BufferVK::SetDebugName(const char* name) { m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_BUFFER, (uint64_t)m_Handle, name); } -void BufferVK::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { - VkMemoryDedicatedRequirements dedicatedRequirements = {VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS}; - - VkMemoryRequirements2 requirements = {VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2}; - requirements.pNext = &dedicatedRequirements; - - VkBufferMemoryRequirementsInfo2 info = {VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2}; - info.buffer = m_Handle; - - const auto& vk = m_Device.GetDispatchTable(); - vk.GetBufferMemoryRequirements2(m_Device, &info, &requirements); - - MemoryTypeUnion memoryType = {}; - memoryType.unpacked.isDedicated = dedicatedRequirements.requiresDedicatedAllocation; - - bool found = m_Device.GetMemoryType(memoryLocation, requirements.memoryRequirements.memoryTypeBits, memoryType.unpacked); - RETURN_ON_FAILURE(&m_Device, found, ReturnVoid(), "Can't find suitable memory type"); - - memoryDesc.size = requirements.memoryRequirements.size; - memoryDesc.alignment = (uint32_t)requirements.memoryRequirements.alignment; - memoryDesc.type = memoryType.packed; - memoryDesc.mustBeDedicated = dedicatedRequirements.requiresDedicatedAllocation; -} - inline void* BufferVK::Map(uint64_t offset, uint64_t size) { CHECK(m_Memory != nullptr, "The buffer does not support memory mapping."); diff --git a/Source/VK/BufferVK.h b/Source/VK/BufferVK.h index 831d606c..debcd6d4 100644 --- a/Source/VK/BufferVK.h +++ b/Source/VK/BufferVK.h @@ -43,7 +43,6 @@ struct BufferVK { //================================================================================================================ void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; void* Map(uint64_t offset, uint64_t size); void Unmap(); diff --git a/Source/VK/BufferVK.hpp b/Source/VK/BufferVK.hpp index 814884d2..e8651284 100644 --- a/Source/VK/BufferVK.hpp +++ b/Source/VK/BufferVK.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetBufferNativeObject(const Buffer& buffer) { return uint64_t(((BufferVK&)buffer).GetHandle()); } -static void NRI_CALL GetBufferMemoryInfo(const Buffer& buffer, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((BufferVK&)buffer).GetMemoryInfo(memoryLocation, memoryDesc); -} - static void* NRI_CALL MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) { return ((BufferVK&)buffer).Map(offset, size); } diff --git a/Source/VK/ConversionVK.h b/Source/VK/ConversionVK.h index e611aaac..5c6e13f3 100644 --- a/Source/VK/ConversionVK.h +++ b/Source/VK/ConversionVK.h @@ -317,24 +317,6 @@ constexpr VkImageType GetImageType(TextureType type) { return IMAGE_TYPES[(uint32_t)type]; } -constexpr VkImageUsageFlags GetImageUsageFlags(TextureUsageBits textureUsageBits) { - VkImageUsageFlags flags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; - - if (textureUsageBits & TextureUsageBits::SHADER_RESOURCE) - flags |= VK_IMAGE_USAGE_SAMPLED_BIT; - - if (textureUsageBits & TextureUsageBits::SHADER_RESOURCE_STORAGE) - flags |= VK_IMAGE_USAGE_STORAGE_BIT; - - if (textureUsageBits & TextureUsageBits::COLOR_ATTACHMENT) - flags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - - if (textureUsageBits & TextureUsageBits::DEPTH_STENCIL_ATTACHMENT) - flags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; - - return flags; -} - constexpr VkImageAspectFlags GetImageAspectFlags(Format format) { switch (format) { case Format::D16_UNORM: diff --git a/Source/VK/DeviceVK.cpp b/Source/VK/DeviceVK.cpp index d2d390d1..4b97d764 100644 --- a/Source/VK/DeviceVK.cpp +++ b/Source/VK/DeviceVK.cpp @@ -26,6 +26,62 @@ constexpr uint32_t INVALID_FAMILY_INDEX = uint32_t(-1); using namespace nri; +constexpr VkBufferUsageFlags GetBufferUsageFlags(BufferUsageBits bufferUsageBits, uint32_t structureStride) { + VkBufferUsageFlags flags = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; + + if (bufferUsageBits & BufferUsageBits::VERTEX_BUFFER) + flags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; + + if (bufferUsageBits & BufferUsageBits::INDEX_BUFFER) + flags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT; + + if (bufferUsageBits & BufferUsageBits::CONSTANT_BUFFER) + flags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; + + if (bufferUsageBits & BufferUsageBits::ARGUMENT_BUFFER) + flags |= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT; + + if (bufferUsageBits & BufferUsageBits::RAY_TRACING_BUFFER) // TODO: add more usage bits? + flags |= VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; + + if (bufferUsageBits & BufferUsageBits::ACCELERATION_STRUCTURE_BUILD_READ) + flags |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR; + + if (bufferUsageBits & BufferUsageBits::SHADER_RESOURCE) { + if (structureStride == 0) + flags |= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT; + else + flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; + } + + if (bufferUsageBits & BufferUsageBits::SHADER_RESOURCE_STORAGE) { + if (structureStride == 0 && (bufferUsageBits & BufferUsageBits::RAY_TRACING_BUFFER) == 0) + flags |= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; + else + flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; + } + + return flags; +} + +constexpr VkImageUsageFlags GetImageUsageFlags(TextureUsageBits textureUsageBits) { + VkImageUsageFlags flags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; + + if (textureUsageBits & TextureUsageBits::SHADER_RESOURCE) + flags |= VK_IMAGE_USAGE_SAMPLED_BIT; + + if (textureUsageBits & TextureUsageBits::SHADER_RESOURCE_STORAGE) + flags |= VK_IMAGE_USAGE_STORAGE_BIT; + + if (textureUsageBits & TextureUsageBits::COLOR_ATTACHMENT) + flags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + + if (textureUsageBits & TextureUsageBits::DEPTH_STENCIL_ATTACHMENT) + flags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; + + return flags; +} + Result CreateDeviceVK(const DeviceCreationDesc& desc, DeviceBase*& device) { StdAllocator allocator(desc.memoryAllocatorInterface); DeviceVK* implementation = Allocate(allocator, desc.callbackInterface, allocator); @@ -721,6 +777,7 @@ Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc, const Devi m_Desc.bufferMaxSize = props13.maxBufferSize; m_Desc.bufferTextureGranularity = (uint32_t)limits.bufferImageGranularity; m_Desc.pushConstantsMaxSize = limits.maxPushConstantsSize; + m_Desc.memoryTier = MemoryTier::TWO; // TODO: seems to be the best match m_Desc.boundDescriptorSetMaxNum = limits.maxBoundDescriptorSets; m_Desc.perStageDescriptorSamplerMaxNum = limits.maxPerStageDescriptorSamplers; @@ -848,6 +905,107 @@ Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc, const Devi return FillFunctionTable(m_CoreInterface); } +void DeviceVK::FillCreateInfo(const BufferDesc& bufferDesc, VkBufferCreateInfo& info) const { + const VkSharingMode sharingMode = IsConcurrentSharingModeEnabledForBuffers() ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE; + const Vector& queueIndices = GetConcurrentSharingModeQueueIndices(); + + info.size = bufferDesc.size; + info.usage = GetBufferUsageFlags(bufferDesc.usageMask, bufferDesc.structureStride); + info.sharingMode = sharingMode; + info.queueFamilyIndexCount = (uint32_t)queueIndices.size(); + info.pQueueFamilyIndices = queueIndices.data(); + + if (m_IsDeviceAddressSupported) + info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; +} + +void DeviceVK::FillCreateInfo(const TextureDesc& textureDesc, VkImageCreateInfo& info) const { + const VkImageType imageType = ::GetImageType(textureDesc.type); + const VkSharingMode sharingMode = IsConcurrentSharingModeEnabledForImages() ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE; + const Vector& queueIndices = GetConcurrentSharingModeQueueIndices(); + + VkImageCreateFlags flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; // typeless + const FormatProps& formatProps = GetFormatProps(textureDesc.format); + if (formatProps.blockWidth > 1) + flags |= VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT; // format can be used to create a view with an uncompressed format (1 texel covers 1 block) + if (textureDesc.arraySize >= 6 && textureDesc.width == textureDesc.height) + flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT; // allow cube maps + if (textureDesc.type == nri::TextureType::TEXTURE_3D) + flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT; // allow 3D demotion to a set of layers // TODO: hook up "VK_EXT_image_2d_view_of_3d"? + if (m_Desc.programmableSampleLocationsTier && textureDesc.format >= Format::D16_UNORM) + flags |= VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT; + + info.flags = flags; + info.imageType = imageType; + info.format = ::GetVkFormat(textureDesc.format, true); + info.extent.width = textureDesc.width; + info.extent.height = textureDesc.height; + info.extent.depth = textureDesc.depth; + info.mipLevels = textureDesc.mipNum; + info.arrayLayers = textureDesc.arraySize; + info.samples = (VkSampleCountFlagBits)textureDesc.sampleNum; + info.tiling = VK_IMAGE_TILING_OPTIMAL; + info.usage = GetImageUsageFlags(textureDesc.usageMask); + info.sharingMode = sharingMode; + info.queueFamilyIndexCount = (uint32_t)queueIndices.size(); + info.pQueueFamilyIndices = queueIndices.data(); + info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; +} + +void DeviceVK::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { + VkBufferCreateInfo createInfo = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; + FillCreateInfo(bufferDesc, createInfo); + + VkMemoryDedicatedRequirements dedicatedRequirements = {VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS}; + + VkMemoryRequirements2 requirements = {VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2}; + requirements.pNext = &dedicatedRequirements; + + VkDeviceBufferMemoryRequirements bufferMemoryRequirements = {VK_STRUCTURE_TYPE_DEVICE_BUFFER_MEMORY_REQUIREMENTS}; + bufferMemoryRequirements.pCreateInfo = &createInfo; + + const auto& vk = GetDispatchTable(); + vk.GetDeviceBufferMemoryRequirements(m_Device, &bufferMemoryRequirements, &requirements); + + MemoryTypeUnion memoryType = {}; + memoryType.unpacked.isDedicated = dedicatedRequirements.requiresDedicatedAllocation; + + bool found = GetMemoryType(memoryLocation, requirements.memoryRequirements.memoryTypeBits, memoryType.unpacked); + RETURN_ON_FAILURE(this, found, ReturnVoid(), "Can't find suitable memory type"); + + memoryDesc.size = requirements.memoryRequirements.size; + memoryDesc.alignment = (uint32_t)requirements.memoryRequirements.alignment; + memoryDesc.type = memoryType.packed; + memoryDesc.mustBeDedicated = dedicatedRequirements.requiresDedicatedAllocation; +} + +void DeviceVK::GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { + VkImageCreateInfo createInfo = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; + FillCreateInfo(textureDesc, createInfo); + + VkMemoryDedicatedRequirements dedicatedRequirements = {VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS}; + + VkMemoryRequirements2 requirements = {VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2}; + requirements.pNext = &dedicatedRequirements; + + VkDeviceImageMemoryRequirements imageMemoryRequirements = {VK_STRUCTURE_TYPE_DEVICE_IMAGE_MEMORY_REQUIREMENTS}; + imageMemoryRequirements.pCreateInfo = &createInfo; + + const auto& vk = GetDispatchTable(); + vk.GetDeviceImageMemoryRequirements(m_Device, &imageMemoryRequirements, &requirements); + + MemoryTypeUnion memoryType = {}; + memoryType.unpacked.isDedicated = dedicatedRequirements.requiresDedicatedAllocation; + + bool found = GetMemoryType(memoryLocation, requirements.memoryRequirements.memoryTypeBits, memoryType.unpacked); + RETURN_ON_FAILURE(this, found, ReturnVoid(), "Can't find suitable memory type"); + + memoryDesc.size = requirements.memoryRequirements.size; + memoryDesc.alignment = (uint32_t)requirements.memoryRequirements.alignment; + memoryDesc.type = memoryType.packed; + memoryDesc.mustBeDedicated = dedicatedRequirements.requiresDedicatedAllocation; +} + bool DeviceVK::GetMemoryType(MemoryLocation memoryLocation, uint32_t memoryTypeMask, MemoryTypeInfo& memoryTypeInfo) const { VkMemoryPropertyFlags neededFlags = 0; // must have VkMemoryPropertyFlags undesiredFlags = 0; // have higher priority than desired @@ -1365,8 +1523,8 @@ Result DeviceVK::ResolveDispatchTable(const Vector& desiredDeviceEx GET_DEVICE_CORE_OR_KHR_PROC(UpdateDescriptorSets); GET_DEVICE_CORE_OR_KHR_PROC(BindBufferMemory2); GET_DEVICE_CORE_OR_KHR_PROC(BindImageMemory2); - GET_DEVICE_CORE_OR_KHR_PROC(GetBufferMemoryRequirements2); - GET_DEVICE_CORE_OR_KHR_PROC(GetImageMemoryRequirements2); + GET_DEVICE_CORE_OR_KHR_PROC(GetDeviceBufferMemoryRequirements); + GET_DEVICE_CORE_OR_KHR_PROC(GetDeviceImageMemoryRequirements); GET_DEVICE_CORE_OR_KHR_PROC(BeginCommandBuffer); GET_DEVICE_CORE_OR_KHR_PROC(CmdSetViewport); GET_DEVICE_CORE_OR_KHR_PROC(CmdSetScissor); diff --git a/Source/VK/DeviceVK.h b/Source/VK/DeviceVK.h index b0b5963f..6355c35c 100644 --- a/Source/VK/DeviceVK.h +++ b/Source/VK/DeviceVK.h @@ -55,6 +55,10 @@ struct DeviceVK final : public DeviceBase { ~DeviceVK(); Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationVKDesc& deviceCreationVKDesc, bool isWrapper); + void FillCreateInfo(const BufferDesc& bufferDesc, VkBufferCreateInfo& info) const; + void FillCreateInfo(const TextureDesc& bufferDesc, VkImageCreateInfo& info) const; + void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; bool GetMemoryType(MemoryLocation memoryLocation, uint32_t memoryTypeMask, MemoryTypeInfo& memoryTypeInfo) const; bool GetMemoryTypeByIndex(uint32_t index, MemoryTypeInfo& memoryTypeInfo) const; void SetDebugNameToTrivialObject(VkObjectType objectType, uint64_t handle, const char* name); diff --git a/Source/VK/DeviceVK.hpp b/Source/VK/DeviceVK.hpp index dff7417e..1ab2a841 100644 --- a/Source/VK/DeviceVK.hpp +++ b/Source/VK/DeviceVK.hpp @@ -16,6 +16,18 @@ static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { return ((const TextureVK&)texture).GetDesc(); } +static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { + return ((const DeviceVK&)device).GetFormatSupport(format); +} + +static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((const DeviceVK&)device).GetMemoryDesc(bufferDesc, memoryLocation, memoryDesc); +} + +static void NRI_CALL GetTextureMemoryDesc(const Device& device, const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((const DeviceVK&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc); +} + static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { return ((DeviceVK&)device).GetCommandQueue(commandQueueType, commandQueue); } @@ -163,10 +175,6 @@ static void NRI_CALL FreeMemory(Memory& memory) { ((MemoryVK&)memory).GetDevice().FreeMemory(memory); } -static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { - return ((const DeviceVK&)device).GetFormatSupport(format); -} - static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { ((DeviceVK&)device).SetDebugName(name); } @@ -196,6 +204,8 @@ Result DeviceVK::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface.GetBufferDesc = ::GetBufferDesc; coreInterface.GetTextureDesc = ::GetTextureDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; + coreInterface.GetBufferMemoryDesc = ::GetBufferMemoryDesc; + coreInterface.GetTextureMemoryDesc = ::GetTextureMemoryDesc; coreInterface.GetCommandQueue = ::GetCommandQueue; coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; diff --git a/Source/VK/DispatchTable.h b/Source/VK/DispatchTable.h index 65edc08e..9f482349 100644 --- a/Source/VK/DispatchTable.h +++ b/Source/VK/DispatchTable.h @@ -103,8 +103,8 @@ struct DispatchTable { VULKAN_FUNCTION(UpdateDescriptorSets); VULKAN_FUNCTION(BindBufferMemory2); VULKAN_FUNCTION(BindImageMemory2); - VULKAN_FUNCTION(GetBufferMemoryRequirements2); - VULKAN_FUNCTION(GetImageMemoryRequirements2); + VULKAN_FUNCTION(GetDeviceBufferMemoryRequirements); + VULKAN_FUNCTION(GetDeviceImageMemoryRequirements); VULKAN_FUNCTION(GetBufferDeviceAddress); VULKAN_FUNCTION(BeginCommandBuffer); VULKAN_FUNCTION(CmdSetViewport); diff --git a/Source/VK/MemoryVK.cpp b/Source/VK/MemoryVK.cpp index 848f86cb..97a1025e 100644 --- a/Source/VK/MemoryVK.cpp +++ b/Source/VK/MemoryVK.cpp @@ -74,7 +74,7 @@ Result MemoryVK::CreateDedicated(BufferVK& buffer) { RETURN_ON_FAILURE(&m_Device, memoryType.unpacked.isDedicated, Result::FAILURE, "Can't allocate a dedicated memory: memory type is not dedicated."); MemoryDesc memoryDesc = {}; - buffer.GetMemoryInfo(memoryType.unpacked.location, memoryDesc); + m_Device.GetMemoryDesc(buffer.GetDesc(), memoryType.unpacked.location, memoryDesc); 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; @@ -111,7 +111,7 @@ Result MemoryVK::CreateDedicated(TextureVK& texture) { RETURN_ON_FAILURE(&m_Device, memoryType.unpacked.isDedicated, Result::FAILURE, "Can't allocate a dedicated memory: the memory type is not dedicated."); MemoryDesc memoryDesc = {}; - texture.GetMemoryInfo(memoryType.unpacked.location, memoryDesc); + m_Device.GetMemoryDesc(texture.GetDesc(), memoryType.unpacked.location, memoryDesc); 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; diff --git a/Source/VK/TextureVK.cpp b/Source/VK/TextureVK.cpp index a8e59499..950578d9 100644 --- a/Source/VK/TextureVK.cpp +++ b/Source/VK/TextureVK.cpp @@ -19,37 +19,8 @@ Result TextureVK::Create(const TextureDesc& textureDesc) { m_ImageAspectFlags = ::GetImageAspectFlags(textureDesc.format); m_Desc = textureDesc; - const VkImageType imageType = ::GetImageType(textureDesc.type); - const VkSharingMode sharingMode = m_Device.IsConcurrentSharingModeEnabledForImages() ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE; - const Vector& queueIndices = m_Device.GetConcurrentSharingModeQueueIndices(); - - VkImageCreateFlags flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; // typeless - const FormatProps& formatProps = GetFormatProps(textureDesc.format); - if (formatProps.blockWidth > 1) - flags |= VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT; // format can be used to create a view with an uncompressed format (1 texel covers 1 block) - if (textureDesc.arraySize >= 6 && textureDesc.width == textureDesc.height) - flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT; // allow cube maps - if (textureDesc.type == nri::TextureType::TEXTURE_3D) - flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT; // allow 3D demotion to a set of layers // TODO: hook up "VK_EXT_image_2d_view_of_3d"? - if (m_Device.GetDesc().programmableSampleLocationsTier && textureDesc.format >= Format::D16_UNORM) - flags |= VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT; - VkImageCreateInfo info = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; - info.flags = flags; - info.imageType = imageType; - info.format = ::GetVkFormat(textureDesc.format, true); - info.extent.width = textureDesc.width; - info.extent.height = textureDesc.height; - info.extent.depth = textureDesc.depth; - info.mipLevels = textureDesc.mipNum; - info.arrayLayers = textureDesc.arraySize; - info.samples = (VkSampleCountFlagBits)textureDesc.sampleNum; - info.tiling = VK_IMAGE_TILING_OPTIMAL; - info.usage = GetImageUsageFlags(textureDesc.usageMask); - info.sharingMode = sharingMode; - info.queueFamilyIndexCount = (uint32_t)queueIndices.size(); - info.pQueueFamilyIndices = queueIndices.data(); - info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + m_Device.FillCreateInfo(textureDesc, info); const auto& vk = m_Device.GetDispatchTable(); VkResult result = vk.CreateImage(m_Device, &info, m_Device.GetAllocationCallbacks(), &m_Handle); @@ -87,28 +58,4 @@ inline void TextureVK::SetDebugName(const char* name) { m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_IMAGE, (uint64_t)m_Handle, name); } -void TextureVK::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { - VkMemoryDedicatedRequirements dedicatedRequirements = {VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS}; - - VkMemoryRequirements2 requirements = {VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2}; - requirements.pNext = &dedicatedRequirements; - - VkImageMemoryRequirementsInfo2 info = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2}; - info.image = m_Handle; - - const auto& vk = m_Device.GetDispatchTable(); - vk.GetImageMemoryRequirements2(m_Device, &info, &requirements); - - MemoryTypeUnion memoryType = {}; - memoryType.unpacked.isDedicated = dedicatedRequirements.requiresDedicatedAllocation; - - bool found = m_Device.GetMemoryType(memoryLocation, requirements.memoryRequirements.memoryTypeBits, memoryType.unpacked); - RETURN_ON_FAILURE(&m_Device, found, ReturnVoid(), "Can't find suitable memory type"); - - memoryDesc.size = requirements.memoryRequirements.size; - memoryDesc.alignment = (uint32_t)requirements.memoryRequirements.alignment; - memoryDesc.type = memoryType.packed; - memoryDesc.mustBeDedicated = dedicatedRequirements.requiresDedicatedAllocation; -} - #include "TextureVK.hpp" diff --git a/Source/VK/TextureVK.h b/Source/VK/TextureVK.h index d8629721..c45109b6 100644 --- a/Source/VK/TextureVK.h +++ b/Source/VK/TextureVK.h @@ -48,7 +48,6 @@ struct TextureVK { //================================================================================================================ void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; private: DeviceVK& m_Device; diff --git a/Source/VK/TextureVK.hpp b/Source/VK/TextureVK.hpp index 9a2167e7..707670e7 100644 --- a/Source/VK/TextureVK.hpp +++ b/Source/VK/TextureVK.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetTextureNativeObject(const Texture& texture) { return uint64_t(((TextureVK&)texture).GetHandle()); } -static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((TextureVK&)texture).GetMemoryInfo(memoryLocation, memoryDesc); -} - #pragma endregion Define_Core_Texture_PartiallyFillFunctionTable(VK); diff --git a/Source/Validation/AccelerationStructureVal.cpp b/Source/Validation/AccelerationStructureVal.cpp index 97231b08..ea161e36 100644 --- a/Source/Validation/AccelerationStructureVal.cpp +++ b/Source/Validation/AccelerationStructureVal.cpp @@ -17,8 +17,8 @@ AccelerationStructureVal::~AccelerationStructureVal() { GetRayTracingInterface().DestroyAccelerationStructure(*GetImpl()); } -void AccelerationStructureVal::GetMemoryInfo(MemoryDesc& memoryDesc) const { - GetRayTracingInterface().GetAccelerationStructureMemoryInfo(*GetImpl(), memoryDesc); +void AccelerationStructureVal::GetMemoryDesc(MemoryDesc& memoryDesc) const { + GetRayTracingInterface().GetAccelerationStructureMemoryDesc(*GetImpl(), memoryDesc); m_Device.RegisterMemoryType(memoryDesc.type, MemoryLocation::DEVICE); } diff --git a/Source/Validation/AccelerationStructureVal.h b/Source/Validation/AccelerationStructureVal.h index 6149eb8c..82f9da0b 100644 --- a/Source/Validation/AccelerationStructureVal.h +++ b/Source/Validation/AccelerationStructureVal.h @@ -25,7 +25,7 @@ struct AccelerationStructureVal final : public DeviceObjectVal { // NRI //================================================================================================================ void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; void* Map(uint64_t offset, uint64_t size); void Unmap(); diff --git a/Source/Validation/BufferVal.hpp b/Source/Validation/BufferVal.hpp index e90840d2..d945350a 100644 --- a/Source/Validation/BufferVal.hpp +++ b/Source/Validation/BufferVal.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetBufferNativeObject(const Buffer& buffer) { return ((BufferVal&)buffer).GetNativeObject(); } -static void NRI_CALL GetBufferMemoryInfo(const Buffer& buffer, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((BufferVal&)buffer).GetMemoryInfo(memoryLocation, memoryDesc); -} - static void* NRI_CALL MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) { return ((BufferVal&)buffer).Map(offset, size); } diff --git a/Source/Validation/DeviceVal.cpp b/Source/Validation/DeviceVal.cpp index 6a9c158f..f992a3c1 100644 --- a/Source/Validation/DeviceVal.cpp +++ b/Source/Validation/DeviceVal.cpp @@ -83,6 +83,16 @@ void DeviceVal::RegisterMemoryType(MemoryType memoryType, MemoryLocation memoryL m_MemoryTypeMap[memoryType] = memoryLocation; } +void DeviceVal::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + GetCoreInterface().GetBufferMemoryDesc(GetImpl(), bufferDesc, memoryLocation, memoryDesc); + RegisterMemoryType(memoryDesc.type, memoryLocation); +} + +void DeviceVal::GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + GetCoreInterface().GetTextureMemoryDesc(GetImpl(), textureDesc, memoryLocation, memoryDesc); + RegisterMemoryType(memoryDesc.type, memoryLocation); +} + Result DeviceVal::CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) { RETURN_ON_FAILURE(this, swapChainDesc.commandQueue != nullptr, Result::INVALID_ARGUMENT, "CreateSwapChain: 'swapChainDesc.commandQueue' is NULL"); RETURN_ON_FAILURE(this, swapChainDesc.width != 0, Result::INVALID_ARGUMENT, "CreateSwapChain: 'swapChainDesc.width' is 0"); @@ -644,7 +654,7 @@ Result DeviceVal::BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingD continue; MemoryDesc memoryDesc = {}; - buffer.GetMemoryInfo(memory.GetMemoryLocation(), memoryDesc); + GetMemoryDesc(buffer.GetDesc(), memory.GetMemoryLocation(), memoryDesc); RETURN_ON_FAILURE(this, !memoryDesc.mustBeDedicated || srcDesc.offset == 0, Result::INVALID_ARGUMENT, "BindBufferMemory: 'memoryBindingDescs[%u].offset' must be zero for dedicated allocation", i); @@ -696,7 +706,7 @@ Result DeviceVal::BindTextureMemory(const TextureMemoryBindingDesc* memoryBindin continue; MemoryDesc memoryDesc = {}; - texture.GetMemoryInfo(memory.GetMemoryLocation(), memoryDesc); + GetMemoryDesc(texture.GetDesc(), memory.GetMemoryLocation(), memoryDesc); RETURN_ON_FAILURE(this, !memoryDesc.mustBeDedicated || srcDesc.offset == 0, Result::INVALID_ARGUMENT, "BindTextureMemory: 'memoryBindingDescs[%u].offset' must be zero for dedicated allocation", i); @@ -1248,7 +1258,7 @@ Result DeviceVal::BindAccelerationStructureMemory(const AccelerationStructureMem "BindAccelerationStructureMemory: 'memoryBindingDescs[%u].accelerationStructure' is already bound to memory", i); MemoryDesc memoryDesc = {}; - accelerationStructure.GetMemoryInfo(memoryDesc); + accelerationStructure.GetMemoryDesc(memoryDesc); RETURN_ON_FAILURE(this, !memoryDesc.mustBeDedicated || srcDesc.offset == 0, Result::INVALID_ARGUMENT, "BindAccelerationStructureMemory: 'memoryBindingDescs[%u].offset' must be 0 for dedicated allocation", i); diff --git a/Source/Validation/DeviceVal.h b/Source/Validation/DeviceVal.h index 8c9195ec..5e318d99 100644 --- a/Source/Validation/DeviceVal.h +++ b/Source/Validation/DeviceVal.h @@ -10,9 +10,6 @@ struct DeviceVal final : public DeviceBase { DeviceVal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator, DeviceBase& device); ~DeviceVal(); - bool Create(); - void RegisterMemoryType(MemoryType memoryType, MemoryLocation memoryLocation); - inline Device& GetImpl() const { return m_Device; } @@ -65,6 +62,11 @@ struct DeviceVal final : public DeviceBase { return m_Lock; } + bool Create(); + void RegisterMemoryType(MemoryType memoryType, MemoryLocation memoryLocation); + void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); + void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); + //================================================================================================================ // NRI //================================================================================================================ diff --git a/Source/Validation/DeviceVal.hpp b/Source/Validation/DeviceVal.hpp index a356a6b9..45cefd92 100644 --- a/Source/Validation/DeviceVal.hpp +++ b/Source/Validation/DeviceVal.hpp @@ -16,6 +16,18 @@ static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { return ((const TextureVal&)texture).GetDesc(); } +static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { + return ((const DeviceVal&)device).GetFormatSupport(format); +} + +static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((DeviceVal&)device).GetMemoryDesc(bufferDesc, memoryLocation, memoryDesc); +} + +static void NRI_CALL GetTextureMemoryDesc(const Device& device, const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((DeviceVal&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc); +} + static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { return ((DeviceVal&)device).GetCommandQueue(commandQueueType, commandQueue); } @@ -166,10 +178,6 @@ static void NRI_CALL FreeMemory(Memory& memory) { GetDeviceVal(memory).FreeMemory(memory); } -static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { - return ((const DeviceVal&)device).GetFormatSupport(format); -} - static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { ((DeviceVal&)device).SetDebugName(name); } @@ -199,6 +207,8 @@ Result DeviceVal::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface.GetBufferDesc = ::GetBufferDesc; coreInterface.GetTextureDesc = ::GetTextureDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; + coreInterface.GetBufferMemoryDesc = ::GetBufferMemoryDesc; + coreInterface.GetTextureMemoryDesc = ::GetTextureMemoryDesc; coreInterface.GetCommandQueue = ::GetCommandQueue; coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; diff --git a/Source/Validation/TextureVal.cpp b/Source/Validation/TextureVal.cpp index e9d7ad46..68fc743d 100644 --- a/Source/Validation/TextureVal.cpp +++ b/Source/Validation/TextureVal.cpp @@ -18,9 +18,4 @@ void TextureVal::SetDebugName(const char* name) { GetCoreInterface().SetTextureDebugName(*GetImpl(), name); } -void TextureVal::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { - GetCoreInterface().GetTextureMemoryInfo(*GetImpl(), memoryLocation, memoryDesc); - m_Device.RegisterMemoryType(memoryDesc.type, memoryLocation); -} - #include "TextureVal.hpp" diff --git a/Source/Validation/TextureVal.h b/Source/Validation/TextureVal.h index c8551263..154d7fc1 100644 --- a/Source/Validation/TextureVal.h +++ b/Source/Validation/TextureVal.h @@ -37,7 +37,6 @@ struct TextureVal : public DeviceObjectVal { // NRI //================================================================================================================ void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; private: MemoryVal* m_Memory = nullptr; diff --git a/Source/Validation/TextureVal.hpp b/Source/Validation/TextureVal.hpp index 570cef55..70d2f0fd 100644 --- a/Source/Validation/TextureVal.hpp +++ b/Source/Validation/TextureVal.hpp @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetTextureNativeObject(const Texture& texture) { return ((TextureVal&)texture).GetNativeObject(); } -static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((TextureVal&)texture).GetMemoryInfo(memoryLocation, memoryDesc); -} - #pragma endregion Define_Core_Texture_PartiallyFillFunctionTable(Val);