From 10f1eedac7063d71529d014bbf5d861dcc8df920 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 28 Aug 2024 21:25:41 -0700 Subject: [PATCH 01/27] feat: apple support Signed-off-by: Michael Pollind --- CMakeLists.txt | 18 ++++++++- Include/.DS_Store | Bin 0 -> 6148 bytes Include/Extensions/NRIWrapperMTL.h | 24 ++++++++++++ Include/NRIDescs.h | 16 +++++++- Source/Creation/Creation.cpp | 25 ++++++++++++- Source/Metal/BufferMetal.h | 14 +++++++ Source/Metal/BufferMetal.mm | 2 + Source/Metal/DeviceMetal.h | 36 ++++++++++++++++++ Source/Metal/DeviceMetal.hpp | 17 +++++++++ Source/Metal/DeviceMetal.mm | 57 +++++++++++++++++++++++++++++ Source/Shared/SharedExternal.h | 2 + Source/VK/DeviceVK.h | 3 +- 12 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 Include/.DS_Store create mode 100644 Include/Extensions/NRIWrapperMTL.h create mode 100644 Source/Metal/BufferMetal.h create mode 100644 Source/Metal/BufferMetal.mm create mode 100644 Source/Metal/DeviceMetal.h create mode 100644 Source/Metal/DeviceMetal.hpp create mode 100644 Source/Metal/DeviceMetal.mm diff --git a/CMakeLists.txt b/CMakeLists.txt index c010c3d2..f54a52fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ option (NRI_ENABLE_NVTX_SUPPORT "Annotations for NVIDIA Nsight Systems" OFF) # Options: backends option (NRI_ENABLE_NONE_SUPPORT "Enable NONE backend" ON) +option (NRI_ENABLE_VK_SUPPORT "Enable VULKAN backend" ON) +option (NRI_ENABLE_METAL_SUPPORT "Enable METAL backend" ON) option (NRI_ENABLE_D3D11_SUPPORT "Enable D3D11 backend" ON) option (NRI_ENABLE_D3D12_SUPPORT "Enable D3D12 backend" ON) option (NRI_ENABLE_VK_SUPPORT "Enable VULKAN backend" ON) @@ -233,8 +235,19 @@ if (WIN32 AND NRI_ENABLE_D3D12_SUPPORT) endif () endif () +# METAL +if (NRI_ENABLE_METAL_SUPPORT AND APPLE) + message ("NRI adding backend: METAL") + set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_MTL=1) + file (GLOB MTL_SOURCE "Source/Metal/*.cpp" "Source/Metal/*.mm" "Source/Metal/*.h" "Source/Metal/*.hpp" ) + source_group ("" FILES ${MTL_SOURCE}) + add_library (NRI_MTL STATIC ${MTL_SOURCE}) + target_include_directories (NRI_MTL PRIVATE "Include" "Source/Shared" "External") + target_compile_definitions (NRI_MTL PRIVATE ${COMPILE_DEFINITIONS}) +endif() + # VK -if (NRI_ENABLE_VK_SUPPORT) +if (NRI_ENABLE_VK_SUPPORT AND NOT APPLE) message ("NRI adding backend: VK") set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_VK=1) @@ -342,6 +355,9 @@ endif () if (NRI_ENABLE_VK_SUPPORT) target_link_libraries (${PROJECT_NAME} PRIVATE NRI_VK) endif () +if (NRI_ENABLE_METAL_SUPPORT) + target_link_libraries (${PROJECT_NAME} PRIVATE NRI_MTL) +endif () set_property (TARGET ${PROJECT_NAME} PROPERTY FOLDER ${PROJECT_FOLDER}) diff --git a/Include/.DS_Store b/Include/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..dbf90847cd1bd865f676ed42ad220f31e71536bd GIT binary patch literal 6148 zcmeHKO>Yx15FNKE*`z|sfv*eFN?fB7Dis=W*{12C2RL*Q8~}ygB%&*}>nOV+MNy=^ z0zZZ$|Aha-3EtRKi6qT|3qp}c8o#l}&yHVYdrd^DGZ{Q0Y7mhJXROpwTw|PPU$HG| zsRET5Bcq%W(!}V@Dc}_N-xc7!Tc;Dcq=JTK-^%_yO?5v`b&Nb84oC34U&auv;l$H? zB%JT@C{6OB)%u~fwkta~s$SLG_1=c(dKwl{G08j8_%+vFXr0DaaTLEyhSOf-*0Iit zD9MInLr4Zgbb0eC$p(7b(UWYD8*XS5Ud^lZ8h2;2gZ6&YKX@{4`m^?atLZ;JY|rO4 z@AiX-PtSTE(u+)gHb0&ejw@p~1+U->f@|gY7>=`4XYY|`?hZYtGdiU`I$sj%8?t{2 zbw_$0<5;|)5k(L>p%|XR3;M)kAkMkIIF`Df*1;sSjV7UwvJ}d4`$$G{&!3 z<@{XYpz3V%6_8Ef?QZ^3-3pxoPJzEvfX@dX&KL-m7R}Lt!aMb!BmU*ZS~xa5nB&S~L_Cavht1ui|@fW61d& W00Y6&B3fYXL%_)33a7w7Rp2+N0+gKq literal 0 HcmV?d00001 diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h new file mode 100644 index 00000000..6d62a9bf --- /dev/null +++ b/Include/Extensions/NRIWrapperMTL.h @@ -0,0 +1,24 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +#include "NRIDeviceCreation.h" + +NRI_NAMESPACE_BEGIN + +NRI_STRUCT(DeviceCreationMTLDesc) +{ + bool enableNRIValidation; + id MtlDevice; +} + +NRI_STRUCT(CommandBufferMTLDesc) +{ + +} + + +NRI_API NRI_NAME(Result) NRI_CALL nriCreateDeviceFromMtlDevice(const NRI_NAME_REF(DeviceCreationMTLDesc) deviceDesc, NRI_NAME_REF(Device*) device); + +NRI_NAMESPACE_END + diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index 7f2f53d5..98ed5208 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -1,4 +1,4 @@ -// © 2021 NVIDIA Corporation +// © 2021 NVIDIA Corporation #pragma once @@ -1284,11 +1284,23 @@ NriStruct(PipelineStatisticsDesc) { #pragma region [ Device desc ] //============================================================================================================================================================================================ +NriEnum(GraphicsAPI, uint8_t, + D3D11, + D3D12, + VK, + MTL, + + MAX_NUM +); + NriEnum(Vendor, uint8_t, UNKNOWN, NVIDIA, AMD, - INTEL + INTEL, + APPLE, + + MAX_NUM ); NriStruct(AdapterDesc) { diff --git a/Source/Creation/Creation.cpp b/Source/Creation/Creation.cpp index 1d81fbb7..d9d8a23a 100644 --- a/Source/Creation/Creation.cpp +++ b/Source/Creation/Creation.cpp @@ -18,7 +18,12 @@ Result CreateDeviceD3D12(const DeviceCreationDesc& deviceCreationDesc, DeviceBas Result CreateDeviceD3D12(const DeviceCreationD3D12Desc& deviceCreationDesc, DeviceBase*& device); #endif -#if NRI_USE_VK +#if NRI_USE_MTL +Result CreateDeviceMTL(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device); +Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBase*& device); +#endif + +#if NRI_USE_VULKAN Result CreateDeviceVK(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device); Result CreateDeviceVK(const DeviceCreationVKDesc& deviceDesc, DeviceBase*& device); #endif @@ -284,6 +289,24 @@ NRI_API Result NRI_CALL nriCreateDeviceFromD3D12Device(const DeviceCreationD3D12 return FinalizeDeviceCreation(deviceCreationDesc, *deviceImpl, device); } +NRI_API Result NRI_CALL nriCreateDeviceFromMtlDevice(const DeviceCreationMTLDesc& deviceCreationMtlDesc, Device*& device) { + DeviceCreationDesc deviceCreationDesc = {}; + deviceCreationDesc.graphicsAPI = GraphicsAPI::MTL; + deviceCreationDesc.enableNRIValidation = deviceCreationMtlDesc.enableNRIValidation; + + Result result = Result::UNSUPPORTED; + DeviceBase* deviceImpl = nullptr; + +#if NRI_USE_MTL + result = CreateDeviceD3D12(tempDeviceCreationD3D12Desc, deviceImpl); +#endif + + if (result != Result::SUCCESS) + return result; + + return FinalizeDeviceCreation(deviceCreationDesc, *deviceImpl, device); +} + NRI_API Result NRI_CALL nriCreateDeviceFromVkDevice(const DeviceCreationVKDesc& deviceCreationVKDesc, Device*& device) { DeviceCreationDesc deviceCreationDesc = {}; deviceCreationDesc.callbackInterface = deviceCreationVKDesc.callbackInterface; diff --git a/Source/Metal/BufferMetal.h b/Source/Metal/BufferMetal.h new file mode 100644 index 00000000..6c906e22 --- /dev/null +++ b/Source/Metal/BufferMetal.h @@ -0,0 +1,14 @@ + + +#import + +struct BufferMetal { + +private: + id pBuffer; + //id pIndirectCommandBuffer; + uint64_t mOffset; +}; + + + diff --git a/Source/Metal/BufferMetal.mm b/Source/Metal/BufferMetal.mm new file mode 100644 index 00000000..f01f9abf --- /dev/null +++ b/Source/Metal/BufferMetal.mm @@ -0,0 +1,2 @@ +#include "BufferMetal.h" + diff --git a/Source/Metal/DeviceMetal.h b/Source/Metal/DeviceMetal.h new file mode 100644 index 00000000..b51a1df5 --- /dev/null +++ b/Source/Metal/DeviceMetal.h @@ -0,0 +1,36 @@ +#pragma once + +namespace nri { + +struct DeviceMetal final : public DeviceBase { + BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); + ~BufferMetal(); + + Result FillFunctionTable(CoreInterface& table) const; + Result FillFunctionTable(HelperInterface& table) const; + Result FillFunctionTable(LowLatencyInterface& table) const; + Result FillFunctionTable(MeshShaderInterface& table) const; + Result FillFunctionTable(RayTracingInterface& table) const; + Result FillFunctionTable(StreamerInterface& table) const; + Result FillFunctionTable(SwapChainInterface& table) const; + Result FillFunctionTable(ResourceAllocatorInterface& table) const; + Result FillFunctionTable(WrapperD3D11Interface& table) const; + Result FillFunctionTable(WrapperD3D12Interface& table) const; + Result FillFunctionTable(WrapperVKInterface& table) const; + + //================================================================================================================ + // DeviceBase + //================================================================================================================ + + inline const DeviceDesc& GetDesc() const { + return m_Desc; + } + + Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper); +private: + DeviceDesc m_Desc = {}; + id m_Device; + DeviceDesc m_Desc = {}; + bool m_OwnsNativeObjects = true; +}; +}; // namespace nri diff --git a/Source/Metal/DeviceMetal.hpp b/Source/Metal/DeviceMetal.hpp new file mode 100644 index 00000000..8a903496 --- /dev/null +++ b/Source/Metal/DeviceMetal.hpp @@ -0,0 +1,17 @@ +Declare_PartiallyFillFunctionTable_Functions(MTL); + +#pragma region[ Core ] + +static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { + return ((const DeviceMetal&)device).GetDesc(); +} + +#pragma endregion + + +Define_Core_Device_PartiallyFillFunctionTable(MTL); +Define_Helper_Device_PartiallyFillFunctionTable(MTL); +Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); +Define_Streamer_Device_PartiallyFillFunctionTable(MTL); +Define_SwapChain_Device_PartiallyFillFunctionTable(MTL); +Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL); diff --git a/Source/Metal/DeviceMetal.mm b/Source/Metal/DeviceMetal.mm new file mode 100644 index 00000000..4e7c837d --- /dev/null +++ b/Source/Metal/DeviceMetal.mm @@ -0,0 +1,57 @@ +#include "DeviceMetal.h" + +using namespace nri; + +BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) + : DeviceBase(callbacks, stdAllocator) { + m_Desc.graphicsAPI = GraphicsAPI::VK; + m_Desc.nriVersionMajor = NRI_VERSION_MAJOR; + m_Desc.nriVersionMinor = NRI_VERSION_MINOR; + +} +BufferMetal::~BufferMetal() { + +} + +Result CreateDeviceMTL(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) { + +} + +Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBase*& device) { + +} + +Result DeviceMetal::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { + m_OwnsNativeObjects = !isWrapper; + + if(isWrapper) { + m_Device = deviceCreationMTLDesc.MtlDevice; + } + + strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name)); + // No vendor id, device id for Apple GPUs + if (strstr(m_Desc.adapterDesc.name, "Apple")) + { + m_Desc.adapterDesc.vendor = nri::Vendor::APPLE; + } else { + const uint64_t regID = [m_Device registryID]; + if (regID) + { + io_registry_entry_t entry = IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID)); + if (entry) + { + // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. + io_registry_entry_t parent; + if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &parent) == kIOReturnSuccess) + { + m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(parent, CFSTR("vendor-id"))); + m_Desc.adapterDesc.deviceId = GetEntryProperty(parent, CFSTR("device-id")); + IOObjectRelease(parent); + } + IOObjectRelease(entry); + } + } + } + + +} diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index 1e432a86..d555d7cb 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -179,6 +179,8 @@ inline nri::Vendor GetVendorFromID(uint32_t vendorID) { return nri::Vendor::AMD; case 0x8086: return nri::Vendor::INTEL; + case 0x106b: + return nri::Vendor::APPLE; } return nri::Vendor::UNKNOWN; diff --git a/Source/VK/DeviceVK.h b/Source/VK/DeviceVK.h index d88a2c4c..160f3b99 100644 --- a/Source/VK/DeviceVK.h +++ b/Source/VK/DeviceVK.h @@ -2,6 +2,7 @@ #pragma once + namespace nri { struct CommandQueueVK; @@ -25,7 +26,7 @@ struct DeviceVK final : public DeviceBase { return m_Device; } - inline operator VkPhysicalDevice() const { + inline operator VkPhysicalDevice() const {DeviceB return m_PhysicalDevice; } From c1f03a2637afc1e5b3a36e04ea9513792d8b754a Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Thu, 29 Aug 2024 00:01:17 -0700 Subject: [PATCH 02/27] feat: add templte classes Signed-off-by: Michael Pollind --- Include/Extensions/NRIWrapperMTL.h | 17 ++ Source/Metal/BufferMTL.h | 48 ++++ Source/Metal/{BufferMetal.mm => BufferMTL.mm} | 0 Source/Metal/BufferMetal.h | 14 -- Source/Metal/{DeviceMetal.h => DeviceMTL.h} | 25 +- Source/Metal/DeviceMTL.hpp | 222 ++++++++++++++++++ Source/Metal/DeviceMTL.mm | 142 +++++++++++ Source/Metal/DeviceMetal.hpp | 17 -- Source/Metal/DeviceMetal.mm | 57 ----- Source/Metal/MemoryMTL.h | 57 +++++ Source/Metal/MemoryMTL.mm | 36 +++ Source/Metal/TextureMTL.h | 21 ++ Source/Metal/TextureMTL.mm | 0 13 files changed, 556 insertions(+), 100 deletions(-) create mode 100644 Source/Metal/BufferMTL.h rename Source/Metal/{BufferMetal.mm => BufferMTL.mm} (100%) delete mode 100644 Source/Metal/BufferMetal.h rename Source/Metal/{DeviceMetal.h => DeviceMTL.h} (84%) create mode 100644 Source/Metal/DeviceMTL.hpp create mode 100644 Source/Metal/DeviceMTL.mm delete mode 100644 Source/Metal/DeviceMetal.hpp delete mode 100644 Source/Metal/DeviceMetal.mm create mode 100644 Source/Metal/MemoryMTL.h create mode 100644 Source/Metal/MemoryMTL.mm create mode 100644 Source/Metal/TextureMTL.h create mode 100644 Source/Metal/TextureMTL.mm diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h index 6d62a9bf..b3dac1db 100644 --- a/Include/Extensions/NRIWrapperMTL.h +++ b/Include/Extensions/NRIWrapperMTL.h @@ -6,6 +6,9 @@ NRI_NAMESPACE_BEGIN + + + NRI_STRUCT(DeviceCreationMTLDesc) { bool enableNRIValidation; @@ -17,6 +20,20 @@ NRI_STRUCT(CommandBufferMTLDesc) } +NRI_STRUCT(BufferMTLDesc) +{ + id buffer; + void* mappedMemory; + MTLResourceOptions options; +} + + +NRI_STRUCT(MemoryMTLDesc) +{ + id buffer; + void* mappedMemory; + MTLResourceOptions options; +} NRI_API NRI_NAME(Result) NRI_CALL nriCreateDeviceFromMtlDevice(const NRI_NAME_REF(DeviceCreationMTLDesc) deviceDesc, NRI_NAME_REF(Device*) device); diff --git a/Source/Metal/BufferMTL.h b/Source/Metal/BufferMTL.h new file mode 100644 index 00000000..a71e7578 --- /dev/null +++ b/Source/Metal/BufferMTL.h @@ -0,0 +1,48 @@ + + +#import + +struct DeviceMTL; +struct MemoryMTL; + +struct BufferMTL { + + inline BufferMTL(DeviceMTL& device) + : m_Device(device) { + } + + inline id GetHandle() const { + return pBuffer; + } + + inline VkDeviceAddress GetDeviceAddress() const { + return m_DeviceAddress; + } + + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + inline const BufferDesc& GetDesc() const { + return m_Desc; + } + + ~BufferMTL(); + + Result Create(const BufferDesc& bufferDesc); + Result Create(const BufferVKDesc& bufferDesc); + Result Create(const AllocateBufferDesc& bufferDesc); + +private: + DeviceVK& m_Device; + id pBuffer; + uint8_t* m_MappedMemory = nullptr; + uint64_t m_MappedMemoryOffset = 0; + uint64_t m_MappedMemoryRangeSize = 0; + uint64_t m_MappedMemoryRangeOffset = 0; + BufferDesc m_Desc = {}; + bool m_OwnsNativeObjects = true; +}; + + + diff --git a/Source/Metal/BufferMetal.mm b/Source/Metal/BufferMTL.mm similarity index 100% rename from Source/Metal/BufferMetal.mm rename to Source/Metal/BufferMTL.mm diff --git a/Source/Metal/BufferMetal.h b/Source/Metal/BufferMetal.h deleted file mode 100644 index 6c906e22..00000000 --- a/Source/Metal/BufferMetal.h +++ /dev/null @@ -1,14 +0,0 @@ - - -#import - -struct BufferMetal { - -private: - id pBuffer; - //id pIndirectCommandBuffer; - uint64_t mOffset; -}; - - - diff --git a/Source/Metal/DeviceMetal.h b/Source/Metal/DeviceMTL.h similarity index 84% rename from Source/Metal/DeviceMetal.h rename to Source/Metal/DeviceMTL.h index b51a1df5..20567262 100644 --- a/Source/Metal/DeviceMetal.h +++ b/Source/Metal/DeviceMTL.h @@ -2,21 +2,10 @@ namespace nri { -struct DeviceMetal final : public DeviceBase { +struct DeviceMTl final : public DeviceBase { BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); ~BufferMetal(); - Result FillFunctionTable(CoreInterface& table) const; - Result FillFunctionTable(HelperInterface& table) const; - Result FillFunctionTable(LowLatencyInterface& table) const; - Result FillFunctionTable(MeshShaderInterface& table) const; - Result FillFunctionTable(RayTracingInterface& table) const; - Result FillFunctionTable(StreamerInterface& table) const; - Result FillFunctionTable(SwapChainInterface& table) const; - Result FillFunctionTable(ResourceAllocatorInterface& table) const; - Result FillFunctionTable(WrapperD3D11Interface& table) const; - Result FillFunctionTable(WrapperD3D12Interface& table) const; - Result FillFunctionTable(WrapperVKInterface& table) const; //================================================================================================================ // DeviceBase @@ -25,12 +14,24 @@ struct DeviceMetal final : public DeviceBase { inline const DeviceDesc& GetDesc() const { return m_Desc; } + + FormatSupportBits GetFormatSupport(const Device& device, Format format); + + Result FillFunctionTable(CoreInterface& table) const; + Result FillFunctionTable(HelperInterface& table) const; + Result FillFunctionTable(LowLatencyInterface& table) const; + Result FillFunctionTable(MeshShaderInterface& table) const; + Result FillFunctionTable(RayTracingInterface& table) const; + Result FillFunctionTable(StreamerInterface& table) const; + Result FillFunctionTable(SwapChainInterface& table) const; + Result FillFunctionTable(ResourceAllocatorInterface& table) const; Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper); private: DeviceDesc m_Desc = {}; id m_Device; DeviceDesc m_Desc = {}; + MTLGPUFamily m_Family; bool m_OwnsNativeObjects = true; }; }; // namespace nri diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp new file mode 100644 index 00000000..267e1f81 --- /dev/null +++ b/Source/Metal/DeviceMTL.hpp @@ -0,0 +1,222 @@ +Declare_PartiallyFillFunctionTable_Functions(MTL); + +#pragma region[ Core ] + +static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { + return ((const DeviceMTL&)device).GetDesc(); +} + +static const BufferDesc& NRI_CALL GetBufferDesc(const Buffer& buffer) { + return ((const BufferMTL&)buffer).GetDesc(); +} + +static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { + return ((const TextureMTL&)texture).GetDesc(); +} + +static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { + return ((const DeviceDesc&)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); +} + +static Result NRI_CALL CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator) { + DeviceD3D12& device = ((CommandQueueD3D12&)commandQueue).GetDevice(); + return device.CreateImplementation(commandAllocator, commandQueue); +} + +static Result NRI_CALL CreateDescriptorPool(Device& device, const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) { + return ((DeviceD3D12&)device).CreateImplementation(descriptorPool, descriptorPoolDesc); +} + +static Result NRI_CALL CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffer) { + return ((DeviceD3D12&)device).CreateImplementation(buffer, bufferDesc); +} + +static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& texture) { + return ((DeviceD3D12&)device).CreateImplementation(texture, textureDesc); +} + +static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { + DeviceD3D12& device = ((const BufferD3D12*)bufferViewDesc.buffer)->GetDevice(); + return device.CreateImplementation(bufferView, bufferViewDesc); +} + +static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) { + DeviceD3D12& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); + return device.CreateImplementation(textureView, textureViewDesc); +} + +static Result NRI_CALL CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) { + DeviceD3D12& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); + return device.CreateImplementation(textureView, textureViewDesc); +} + +static Result NRI_CALL CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) { + DeviceD3D12& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); + return device.CreateImplementation(textureView, textureViewDesc); +} + +static Result NRI_CALL CreateSampler(Device& device, const SamplerDesc& samplerDesc, Descriptor*& sampler) { + return ((DeviceD3D12&)device).CreateImplementation(sampler, samplerDesc); +} + +static Result NRI_CALL CreatePipelineLayout(Device& device, const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) { + return ((DeviceD3D12&)device).CreateImplementation(pipelineLayout, pipelineLayoutDesc); +} + +static Result NRI_CALL CreateGraphicsPipeline(Device& device, const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) { + return ((DeviceD3D12&)device).CreateImplementation(pipeline, graphicsPipelineDesc); +} + +static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) { + return ((DeviceD3D12&)device).CreateImplementation(pipeline, computePipelineDesc); +} + +static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) { + return ((DeviceD3D12&)device).CreateImplementation(fence, initialValue); +} + +static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) { + return ((DeviceD3D12&)device).CreateImplementation(queryPool, queryPoolDesc); +} + +static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) { + Destroy((CommandBufferD3D12*)&commandBuffer); +} + +static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) { + Destroy((CommandAllocatorD3D12*)&commandAllocator); +} + +static void NRI_CALL DestroyDescriptorPool(DescriptorPool& descriptorPool) { + Destroy((DescriptorPoolD3D12*)&descriptorPool); +} + +static void NRI_CALL DestroyBuffer(Buffer& buffer) { + Destroy((BufferD3D12*)&buffer); +} + +static void NRI_CALL DestroyTexture(Texture& texture) { + Destroy((TextureD3D12*)&texture); +} + +static void NRI_CALL DestroyDescriptor(Descriptor& descriptor) { + Destroy((DescriptorD3D12*)&descriptor); +} + +static void NRI_CALL DestroyPipelineLayout(PipelineLayout& pipelineLayout) { + Destroy((PipelineLayoutD3D12*)&pipelineLayout); +} + +static void NRI_CALL DestroyPipeline(Pipeline& pipeline) { + Destroy((PipelineD3D12*)&pipeline); +} + +static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) { + Destroy((QueryPoolD3D12*)&queryPool); +} + +static void NRI_CALL DestroyFence(Fence& fence) { + Destroy((FenceD3D12*)&fence); +} + +static Result NRI_CALL AllocateMemory(Device& device, const AllocateMemoryDesc& allocateMemoryDesc, Memory*& memory) { + return ((DeviceD3D12&)device).CreateImplementation(memory, allocateMemoryDesc); +} + +static Result NRI_CALL BindBufferMemory(Device& device, const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { + return ((DeviceD3D12&)device).BindBufferMemory(memoryBindingDescs, memoryBindingDescNum); +} + +static Result NRI_CALL BindTextureMemory(Device& device, const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { + return ((DeviceD3D12&)device).BindTextureMemory(memoryBindingDescs, memoryBindingDescNum); +} + +static void NRI_CALL FreeMemory(Memory& memory) { + Destroy((MemoryD3D12*)&memory); +} + +static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { + ((DeviceD3D12&)device).SetDebugName(name); +} + +static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) { + ((PipelineD3D12&)pipeline).SetDebugName(name); +} + +static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) { + ((PipelineLayoutD3D12&)pipelineLayout).SetDebugName(name); +} + +static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) { + ((MemoryMetal&)memory).SetDebugName(name); +} + +static void* NRI_CALL GetDeviceNativeObject(const Device& device) { + if (!(&device)) + return nullptr; + + return ((DeviceMetal&)device).GetNativeObject(); +} + +Result DeviceMetal::FillFunctionTable(CoreInterface& table) const { + table = {}; + Core_Device_PartiallyFillFunctionTableMTL(table); + Core_Buffer_PartiallyFillFunctionTableMTL(table); + Core_CommandAllocator_PartiallyFillFunctionTableMTL(table); + Core_CommandBuffer_PartiallyFillFunctionTableMTL(table); + Core_CommandQueue_PartiallyFillFunctionTableMTL(table); + Core_Descriptor_PartiallyFillFunctionTableMTL(table); + Core_DescriptorPool_PartiallyFillFunctionTableMTL(table); + Core_DescriptorSet_PartiallyFillFunctionTableMTL(table); + Core_Fence_PartiallyFillFunctionTableMTL(table); + Core_QueryPool_PartiallyFillFunctionTableMTL(table); + Core_Texture_PartiallyFillFunctionTableMTL(table); + return ValidateFunctionTable(table); +} + +#pragma endregion + + +Result DeviceMetal::FillFunctionTable(HelperInterface& table) const { + +} + +Result DeviceMetal::FillFunctionTable(LowLatencyInterface& table) const { +} + +Result DeviceMetal::FillFunctionTable(MeshShaderInterface& table) const { +} + +Result DeviceMetal::FillFunctionTable(RayTracingInterface& table) const { +} +Result DeviceMetal::FillFunctionTable(StreamerInterface& table) const { +} +Result DeviceMetal::FillFunctionTable(SwapChainInterface& table) const { +} +Result DeviceMetal::FillFunctionTable(ResourceAllocatorInterface& table) const { +} + +Define_Core_Device_PartiallyFillFunctionTable(MTL); +Define_Helper_Device_PartiallyFillFunctionTable(MTL); +Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); +Define_Streamer_Device_PartiallyFillFunctionTable(MTL); +Define_SwapChain_Device_PartiallyFillFunctionTable(MTL); +Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL); diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm new file mode 100644 index 00000000..aae2f501 --- /dev/null +++ b/Source/Metal/DeviceMTL.mm @@ -0,0 +1,142 @@ +#include "DeviceMetal.h" + +using namespace nri; + +BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) + : DeviceBase(callbacks, stdAllocator) { + m_Desc.graphicsAPI = GraphicsAPI::VK; + m_Desc.nriVersionMajor = NRI_VERSION_MAJOR; + m_Desc.nriVersionMinor = NRI_VERSION_MINOR; + +} + +BufferMetal::~BufferMetal() { + +} + +Result CreateDeviceMTL(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) { + +} + + +Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBase*& device) { + +} + +//ResultOrError GetMTLGPUFamily(id device) { +// // https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc +// +// if (@available(macOS 10.15, iOS 10.13, *)) { +// if ([device supportsFamily:MTLGPUFamilyApple7]) { +// return MTLGPUFamily::Apple7; +// } +// if ([device supportsFamily:MTLGPUFamilyApple6]) { +// return MTLGPUFamily::Apple6; +// } +// if ([device supportsFamily:MTLGPUFamilyApple5]) { +// return MTLGPUFamily::Apple5; +// } +// if ([device supportsFamily:MTLGPUFamilyApple4]) { +// return MTLGPUFamily::Apple4; +// } +// if ([device supportsFamily:MTLGPUFamilyApple3]) { +// return MTLGPUFamily::Apple3; +// } +// if ([device supportsFamily:MTLGPUFamilyApple2]) { +// return MTLGPUFamily::Apple2; +// } +// if ([device supportsFamily:MTLGPUFamilyApple1]) { +// return MTLGPUFamily::Apple1; +// } +// +// // This family is no longer supported in the macOS 10.15 SDK but still exists so +// // default to it. +// return MTLGPUFamily::Mac1; +// } +// +// return DAWN_INTERNAL_ERROR("Unsupported Metal device"); +//} +// +//} // anonymous namespace + +FormatSupportBits DeviceMTL::GetFormatSupport(const Device& device, Format format) { + int currentFamily = HIGHEST_GPU_FAMILY; + for (; currentFamily >= (int)MTLGPUFamilyApple1; currentFamily--) { + if ([m_device supportsFamily:(MTLGPUFamily)currentFamily]) { + highestAppleFamily = (MTLGPUFamily)currentFamily; + break; + } + } +} + +static inline bool FindMTLGpuFamily(id device, MTLGPUFamily* family) { + //https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc + if (@available(macOS 10.15, iOS 10.13, *)) { + if ([device supportsFamily:MTLGPUFamilyApple7]) { + return MTLGPUFamily::Apple7; + } + if ([device supportsFamily:MTLGPUFamilyApple6]) { + return MTLGPUFamily::Apple6; + } + if ([device supportsFamily:MTLGPUFamilyApple5]) { + return MTLGPUFamily::Apple5; + } + if ([device supportsFamily:MTLGPUFamilyApple4]) { + return MTLGPUFamily::Apple4; + } + if ([device supportsFamily:MTLGPUFamilyApple3]) { + return MTLGPUFamily::Apple3; + } + if ([device supportsFamily:MTLGPUFamilyApple2]) { + return MTLGPUFamily::Apple2; + } + if ([device supportsFamily:MTLGPUFamilyApple1]) { + return MTLGPUFamily::Apple1; + } + + // This family is no longer supported in the macOS 10.15 SDK but still exists so + // default to it. + //return MTLGPUFamily::Mac1; + (*family) = MTLGPUFamily::Mac1; + return true; + } + + return false; +} + + +Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { + m_OwnsNativeObjects = !isWrapper; + + if(isWrapper) { + m_Device = deviceCreationMTLDesc.MtlDevice; + } + + strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name)); + // No vendor id, device id for Apple GPUs + if (strstr(m_Desc.adapterDesc.name, "Apple")) { + m_Desc.adapterDesc.vendor = nri::Vendor::APPLE; + } + + const uint64_t regID = [m_Device registryID]; + if (regID) + { + IORef entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID))); + if (entry) + { + // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. + IORef deviceEntry; + if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess) + { + m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))); + m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id")); + } + } + } + + m_Desc.adapterDesc.luid = 0; + m_Desc.adapterDesc.videoMemorySize = 0; + m_Desc.adapterDesc.systemMemorySize = 0; + return Result::SUCCESS; + +} diff --git a/Source/Metal/DeviceMetal.hpp b/Source/Metal/DeviceMetal.hpp deleted file mode 100644 index 8a903496..00000000 --- a/Source/Metal/DeviceMetal.hpp +++ /dev/null @@ -1,17 +0,0 @@ -Declare_PartiallyFillFunctionTable_Functions(MTL); - -#pragma region[ Core ] - -static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { - return ((const DeviceMetal&)device).GetDesc(); -} - -#pragma endregion - - -Define_Core_Device_PartiallyFillFunctionTable(MTL); -Define_Helper_Device_PartiallyFillFunctionTable(MTL); -Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); -Define_Streamer_Device_PartiallyFillFunctionTable(MTL); -Define_SwapChain_Device_PartiallyFillFunctionTable(MTL); -Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL); diff --git a/Source/Metal/DeviceMetal.mm b/Source/Metal/DeviceMetal.mm deleted file mode 100644 index 4e7c837d..00000000 --- a/Source/Metal/DeviceMetal.mm +++ /dev/null @@ -1,57 +0,0 @@ -#include "DeviceMetal.h" - -using namespace nri; - -BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) - : DeviceBase(callbacks, stdAllocator) { - m_Desc.graphicsAPI = GraphicsAPI::VK; - m_Desc.nriVersionMajor = NRI_VERSION_MAJOR; - m_Desc.nriVersionMinor = NRI_VERSION_MINOR; - -} -BufferMetal::~BufferMetal() { - -} - -Result CreateDeviceMTL(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) { - -} - -Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBase*& device) { - -} - -Result DeviceMetal::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { - m_OwnsNativeObjects = !isWrapper; - - if(isWrapper) { - m_Device = deviceCreationMTLDesc.MtlDevice; - } - - strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name)); - // No vendor id, device id for Apple GPUs - if (strstr(m_Desc.adapterDesc.name, "Apple")) - { - m_Desc.adapterDesc.vendor = nri::Vendor::APPLE; - } else { - const uint64_t regID = [m_Device registryID]; - if (regID) - { - io_registry_entry_t entry = IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID)); - if (entry) - { - // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. - io_registry_entry_t parent; - if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &parent) == kIOReturnSuccess) - { - m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(parent, CFSTR("vendor-id"))); - m_Desc.adapterDesc.deviceId = GetEntryProperty(parent, CFSTR("device-id")); - IOObjectRelease(parent); - } - IOObjectRelease(entry); - } - } - } - - -} diff --git a/Source/Metal/MemoryMTL.h b/Source/Metal/MemoryMTL.h new file mode 100644 index 00000000..2374d2a8 --- /dev/null +++ b/Source/Metal/MemoryMTL.h @@ -0,0 +1,57 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +#include + +namespace nri { + +struct DeviceMTL; + +struct MemoryMTL { + + inline MemoryMTL (DeviceMTL& device) + : m_Device(device) { + } + + inline VkDeviceMemory GetHandle() const { + return m_Handle; + } + + inline DeviceVK& GetDevice() const { + return m_Device; + } + + inline MemoryType GetType() const { + return m_Type; + } + + inline uint8_t* GetMappedMemory() const { + return m_MappedMemory; + } + + ~MemoryMTL(); + + Result Create(const MemoryMTLDesc& memoryDesc); + Result Create(const AllocateMemoryDesc& allocateMemoryDesc); + //Result CreateDedicated(const BufferVK& buffer); + //Result CreateDedicated(const TextureVK& texture); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + +private: + DeviceMTL& m_Device; + id m_Buffer; + uint8_t* m_MappedMemory = nullptr; + MemoryType m_Type = std::numeric_limits::max(); + float m_Priority = 0.0f; + bool m_OwnsNativeObjects = true; +}; + + +} + diff --git a/Source/Metal/MemoryMTL.mm b/Source/Metal/MemoryMTL.mm new file mode 100644 index 00000000..528facdb --- /dev/null +++ b/Source/Metal/MemoryMTL.mm @@ -0,0 +1,36 @@ +// © 2021 NVIDIA Corporation + +#include "MemoryMTL.h" + +using namespace nri; + +MemoryMTL::~MemoryMTL() { + //if (m_OwnsNativeObjects) { + // const auto& vk = m_Device.GetDispatchTable(); + // vk.FreeMemory(m_Device, m_Handle, m_Device.GetAllocationCallbacks()); + //} +} + +Result MemoryMTL::Create(const MemoryMTLDesc& memoryDesc) { + if (!memoryDesc.vkDeviceMemory) + return Result::INVALID_ARGUMENT; + + MemoryTypeInfo memoryTypeInfo = {}; + + //bool found = m_Device.GetMemoryTypeByIndex(memoryDesc.memoryTypeIndex, memoryTypeInfo); + //RETURN_ON_FAILURE(&m_Device, found, Result::INVALID_ARGUMENT, "Can't find memory by index"); + + m_OwnsNativeObjects = false; + m_Buffer = memoryDesc.buffer; + m_MappedMemory = (uint8_t*)memoryDesc.mappedMemory; + m_Type = memoryDesc.options; + + const auto& mtl = m_Device.GetDispatchTable(); + //if (!m_MappedMemory && IsHostVisibleMemory(memoryTypeInfo.location)) { + // VkResult result = mtl.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; +} + diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h new file mode 100644 index 00000000..71d77181 --- /dev/null +++ b/Source/Metal/TextureMTL.h @@ -0,0 +1,21 @@ +// © 2021 NVIDIA Corporation + +#pragma once + + +namespace nri { + +struct DeviceMTL; + +struct TextureMTL { + + inline TextureMTL(DeviceMTL& device) + : m_Device(device) { + } +private: + DeviceMTL& m_Device; + +}; + +} + diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm new file mode 100644 index 00000000..e69de29b From c03dda8759a5cc4ec19692448b019a16b5c5e556 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Fri, 30 Aug 2024 08:01:24 -0700 Subject: [PATCH 03/27] feat: add command queue mtl Signed-off-by: Michael Pollind --- Include/.DS_Store | Bin 6148 -> 0 bytes Include/Extensions/NRIWrapperMTL.h | 16 ++++ Source/Metal/CommandQueueMTL.h | 50 +++++++++++ Source/Metal/CommandQueueMTL.mm | 21 +++++ Source/Metal/DeviceMTL.h | 4 + Source/Metal/DeviceMTL.mm | 131 ++++++++++++++--------------- Source/Metal/TextureMTL.h | 1 - Source/VK/CommandQueueVK.h | 1 - 8 files changed, 155 insertions(+), 69 deletions(-) delete mode 100644 Include/.DS_Store create mode 100644 Source/Metal/CommandQueueMTL.h create mode 100644 Source/Metal/CommandQueueMTL.mm diff --git a/Include/.DS_Store b/Include/.DS_Store deleted file mode 100644 index dbf90847cd1bd865f676ed42ad220f31e71536bd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKO>Yx15FNKE*`z|sfv*eFN?fB7Dis=W*{12C2RL*Q8~}ygB%&*}>nOV+MNy=^ z0zZZ$|Aha-3EtRKi6qT|3qp}c8o#l}&yHVYdrd^DGZ{Q0Y7mhJXROpwTw|PPU$HG| zsRET5Bcq%W(!}V@Dc}_N-xc7!Tc;Dcq=JTK-^%_yO?5v`b&Nb84oC34U&auv;l$H? zB%JT@C{6OB)%u~fwkta~s$SLG_1=c(dKwl{G08j8_%+vFXr0DaaTLEyhSOf-*0Iit zD9MInLr4Zgbb0eC$p(7b(UWYD8*XS5Ud^lZ8h2;2gZ6&YKX@{4`m^?atLZ;JY|rO4 z@AiX-PtSTE(u+)gHb0&ejw@p~1+U->f@|gY7>=`4XYY|`?hZYtGdiU`I$sj%8?t{2 zbw_$0<5;|)5k(L>p%|XR3;M)kAkMkIIF`Df*1;sSjV7UwvJ}d4`$$G{&!3 z<@{XYpz3V%6_8Ef?QZ^3-3pxoPJzEvfX@dX&KL-m7R}Lt!aMb!BmU*ZS~xa5nB&S~L_Cavht1ui|@fW61d& W00Y6&B3fYXL%_)33a7w7Rp2+N0+gKq diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h index b3dac1db..b57e6c0f 100644 --- a/Include/Extensions/NRIWrapperMTL.h +++ b/Include/Extensions/NRIWrapperMTL.h @@ -7,6 +7,22 @@ NRI_NAMESPACE_BEGIN +NRI_ENUM +( + MTLGPUFamily, uint8_t, + + Apple1, + Apple2, + Apple3, + Apple4, + Apple5, + Apple6, + Apple7, + Mac1, + Mac2, + + MAX_APPLE_FAMILY_NUM +); NRI_STRUCT(DeviceCreationMTLDesc) diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h new file mode 100644 index 00000000..85f46694 --- /dev/null +++ b/Source/Metal/CommandQueueMTL.h @@ -0,0 +1,50 @@ +// © 2021 NVIDIA Corporation + +#pragma once + + +namespace nri { + +struct DeviceMTL; + +struct CommandQueueMTL { + + inline CommandQueueMTL(DeviceMTL& device) + : m_Device(device) { + } + + ~CommandQueueMTL(); + + inline operator id() const { + return m_Handle; + } + + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + inline uint32_t GetFamilyIndex() const { + return m_FamilyIndex; + } + + inline CommandQueueType GetType() const { + return m_Type; + } + + inline Lock& GetLock() { + return m_Lock; + } + + void SetDebugName(const char* name); + Result Create(CommandQueueType type, uint32_t familyIndex, id handle); +private: + DeviceMTL& m_Device; + uint32_t m_FamilyIndex = INVALID_FAMILY_INDEX; + CommandQueueType m_Type = CommandQueueType(-1); + id m_Handle; + Lock m_Lock; +}; + +} + + diff --git a/Source/Metal/CommandQueueMTL.mm b/Source/Metal/CommandQueueMTL.mm new file mode 100644 index 00000000..2386982a --- /dev/null +++ b/Source/Metal/CommandQueueMTL.mm @@ -0,0 +1,21 @@ +// © 2021 NVIDIA Corporation + +#include "CommandQueueMTL.h" + +using namespace nri; + + +CommandQueueMTL::~CommandQueueMTL() { + m_CommandQueue = nil; +} + +Result CommandQueueMTL::Create(CommandQueueType type, uint32_t familyIndex, id handle) { + m_Type = type; + m_FamilyIndex = familyIndex; + m_Handle = handle; + return Result::SUCCESS; +} + +void CommandQueueMTL::SetDebugName(const char* name) { + +} diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index 20567262..5b244d49 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -7,6 +7,9 @@ struct DeviceMTl final : public DeviceBase { ~BufferMetal(); + + Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); + //================================================================================================================ // DeviceBase //================================================================================================================ @@ -30,6 +33,7 @@ struct DeviceMTl final : public DeviceBase { private: DeviceDesc m_Desc = {}; id m_Device; + std::array m_CommandQueues = {}; DeviceDesc m_Desc = {}; MTLGPUFamily m_Family; bool m_OwnsNativeObjects = true; diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index aae2f501..97b79920 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -2,6 +2,10 @@ using namespace nri; + +static inline bool FindMTLGpuFamily(id device, MTLGPUFamily *family); + + BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) : DeviceBase(callbacks, stdAllocator) { m_Desc.graphicsAPI = GraphicsAPI::VK; @@ -23,42 +27,6 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa } -//ResultOrError GetMTLGPUFamily(id device) { -// // https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc -// -// if (@available(macOS 10.15, iOS 10.13, *)) { -// if ([device supportsFamily:MTLGPUFamilyApple7]) { -// return MTLGPUFamily::Apple7; -// } -// if ([device supportsFamily:MTLGPUFamilyApple6]) { -// return MTLGPUFamily::Apple6; -// } -// if ([device supportsFamily:MTLGPUFamilyApple5]) { -// return MTLGPUFamily::Apple5; -// } -// if ([device supportsFamily:MTLGPUFamilyApple4]) { -// return MTLGPUFamily::Apple4; -// } -// if ([device supportsFamily:MTLGPUFamilyApple3]) { -// return MTLGPUFamily::Apple3; -// } -// if ([device supportsFamily:MTLGPUFamilyApple2]) { -// return MTLGPUFamily::Apple2; -// } -// if ([device supportsFamily:MTLGPUFamilyApple1]) { -// return MTLGPUFamily::Apple1; -// } -// -// // This family is no longer supported in the macOS 10.15 SDK but still exists so -// // default to it. -// return MTLGPUFamily::Mac1; -// } -// -// return DAWN_INTERNAL_ERROR("Unsupported Metal device"); -//} -// -//} // anonymous namespace - FormatSupportBits DeviceMTL::GetFormatSupport(const Device& device, Format format) { int currentFamily = HIGHEST_GPU_FAMILY; for (; currentFamily >= (int)MTLGPUFamilyApple1; currentFamily--) { @@ -69,42 +37,14 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa } } -static inline bool FindMTLGpuFamily(id device, MTLGPUFamily* family) { - //https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc - if (@available(macOS 10.15, iOS 10.13, *)) { - if ([device supportsFamily:MTLGPUFamilyApple7]) { - return MTLGPUFamily::Apple7; - } - if ([device supportsFamily:MTLGPUFamilyApple6]) { - return MTLGPUFamily::Apple6; - } - if ([device supportsFamily:MTLGPUFamilyApple5]) { - return MTLGPUFamily::Apple5; - } - if ([device supportsFamily:MTLGPUFamilyApple4]) { - return MTLGPUFamily::Apple4; - } - if ([device supportsFamily:MTLGPUFamilyApple3]) { - return MTLGPUFamily::Apple3; - } - if ([device supportsFamily:MTLGPUFamilyApple2]) { - return MTLGPUFamily::Apple2; - } - if ([device supportsFamily:MTLGPUFamilyApple1]) { - return MTLGPUFamily::Apple1; - } - // This family is no longer supported in the macOS 10.15 SDK but still exists so - // default to it. - //return MTLGPUFamily::Mac1; - (*family) = MTLGPUFamily::Mac1; - return true; - } +Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - return false; } + + Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { m_OwnsNativeObjects = !isWrapper; @@ -134,9 +74,66 @@ static inline bool FindMTLGpuFamily(id device, MTLGPUFamily* family) } } + MTLGPUFamily family; + if(!FindMTLGpuFamily(m_Device, family)) { + return Result::UNSUPPORTED; + } + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf + //TODO: fill desc + switch(family) { + case MTLGPUFamily::Apple1: + break; + default: + break; + } + m_Desc.adapterDesc.luid = 0; m_Desc.adapterDesc.videoMemorySize = 0; m_Desc.adapterDesc.systemMemorySize = 0; return Result::SUCCESS; } + + +static inline bool FindMTLGpuFamily(id device, + MTLGPUFamily *family) { + // https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc + if (@available(macOS 10.15, iOS 10.13, *)) { + if ([device supportsFamily:MTLGPUFamilyApple7]) { + (*family) = MTLGPUFamily::Apple7; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple6]) { + (*family) = MTLGPUFamily::Apple6; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple5]) { + (*family) = MTLGPUFamily::Apple5; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple4]) { + (*family) = MTLGPUFamily::Apple4; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple3]) { + (*family) = MTLGPUFamily::Apple3; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple2]) { + (*family) = MTLGPUFamily::Apple2; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple1]) { + (*family) = MTLGPUFamily::Apple1; + return true; + } + + // This family is no longer supported in the macOS 10.15 SDK but still + // exists so default to it. + // return MTLGPUFamily::Mac1; + (*family) = MTLGPUFamily::Mac1; + return true; + } + return false; +} + diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index 71d77181..0195ba8d 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -2,7 +2,6 @@ #pragma once - namespace nri { struct DeviceMTL; diff --git a/Source/VK/CommandQueueVK.h b/Source/VK/CommandQueueVK.h index cb8d847e..c374cb53 100644 --- a/Source/VK/CommandQueueVK.h +++ b/Source/VK/CommandQueueVK.h @@ -42,7 +42,6 @@ struct CommandQueueVK { void EndAnnotation(); void Annotation(const char* name, uint32_t bgra); void Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain); - Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); Result WaitForIdle(); private: From 52fc16a6365dc0fb72a37f07913cbb164200eb21 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Fri, 30 Aug 2024 08:05:34 -0700 Subject: [PATCH 04/27] add ds_store Signed-off-by: Michael Pollind --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ef35f6b5..42f09440 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# apple +.DS_Store + # cmake CMakeFiles/ .cmake/ From f7aad2bc4b91f2022c2a223fa6ad86bd092c6f82 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Fri, 30 Aug 2024 08:08:01 -0700 Subject: [PATCH 05/27] feat: update commanQueuMTL Signed-off-by: Michael Pollind --- Source/Metal/CommandQueueMTL.h | 2 -- Source/Metal/CommandQueueMTL.mm | 14 ++++++++++++-- Source/VK/CommandQueueVK.h | 1 + Source/VK/DeviceVK.h | 3 +-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h index 85f46694..0c5b35bf 100644 --- a/Source/Metal/CommandQueueMTL.h +++ b/Source/Metal/CommandQueueMTL.h @@ -1,8 +1,6 @@ // © 2021 NVIDIA Corporation - #pragma once - namespace nri { struct DeviceMTL; diff --git a/Source/Metal/CommandQueueMTL.mm b/Source/Metal/CommandQueueMTL.mm index 2386982a..6956aa25 100644 --- a/Source/Metal/CommandQueueMTL.mm +++ b/Source/Metal/CommandQueueMTL.mm @@ -15,7 +15,17 @@ m_Handle = handle; return Result::SUCCESS; } - -void CommandQueueMTL::SetDebugName(const char* name) { +inline void CommandQueueMTL::SetDebugName(const char* name) { + [m_Handle setLabel:[NSString stringWithUTF8String:name]]; } + +Result CommandQueueVK::WaitForIdle() { + + id waitCmdBuf = [m_Handle commandBufferWithUnretainedReferences]; + + [waitCmdBuf commit]; + [waitCmdBuf waitUntilCompleted]; + + waitCmdBuf = nil; +} \ No newline at end of file diff --git a/Source/VK/CommandQueueVK.h b/Source/VK/CommandQueueVK.h index c374cb53..cb8d847e 100644 --- a/Source/VK/CommandQueueVK.h +++ b/Source/VK/CommandQueueVK.h @@ -42,6 +42,7 @@ struct CommandQueueVK { void EndAnnotation(); void Annotation(const char* name, uint32_t bgra); void Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain); + Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); Result WaitForIdle(); private: diff --git a/Source/VK/DeviceVK.h b/Source/VK/DeviceVK.h index 160f3b99..d88a2c4c 100644 --- a/Source/VK/DeviceVK.h +++ b/Source/VK/DeviceVK.h @@ -2,7 +2,6 @@ #pragma once - namespace nri { struct CommandQueueVK; @@ -26,7 +25,7 @@ struct DeviceVK final : public DeviceBase { return m_Device; } - inline operator VkPhysicalDevice() const {DeviceB + inline operator VkPhysicalDevice() const { return m_PhysicalDevice; } From c5463d2002008713cd560ccf4bfeb571282dbbf6 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sat, 31 Aug 2024 09:24:04 -0700 Subject: [PATCH 06/27] feat: add conversion and start texture Signed-off-by: Michael Pollind --- CMakeLists.txt | 7 +++ Include/Extensions/NRIWrapperMTL.h | 8 ++- Source/Creation/Creation.cpp | 11 ++++ Source/Metal/ConversionMTL.h | 33 ++++++++++++ Source/Metal/ConversionMTL.mm | 82 ++++++++++++++++++++++++++++++ Source/Metal/DeviceMTL.h | 15 +++--- Source/Metal/DeviceMTL.mm | 12 ++++- Source/Metal/FencMTL.mm | 11 ++++ Source/Metal/FenceMTL.h | 28 ++++++++++ Source/Metal/MemoryMTL.h | 6 +-- Source/Metal/TextureMTL.h | 20 +++++++- Source/Metal/TextureMTL.mm | 24 +++++++++ Source/Shared/SharedExternal.h | 1 + 13 files changed, 243 insertions(+), 15 deletions(-) create mode 100644 Source/Metal/ConversionMTL.h create mode 100644 Source/Metal/ConversionMTL.mm create mode 100644 Source/Metal/FencMTL.mm create mode 100644 Source/Metal/FenceMTL.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f54a52fa..3b77741b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,6 +242,13 @@ if (NRI_ENABLE_METAL_SUPPORT AND APPLE) file (GLOB MTL_SOURCE "Source/Metal/*.cpp" "Source/Metal/*.mm" "Source/Metal/*.h" "Source/Metal/*.hpp" ) source_group ("" FILES ${MTL_SOURCE}) add_library (NRI_MTL STATIC ${MTL_SOURCE}) + target_link_libraries(NRI_MTL + "-framework Metal" + "-framework MetalKit" + "-framework AppKit" + "-framework Foundation" + "-framework QuartzCore" + ) target_include_directories (NRI_MTL PRIVATE "Include" "Source/Shared" "External") target_compile_definitions (NRI_MTL PRIVATE ${COMPILE_DEFINITIONS}) endif() diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h index b57e6c0f..5ecf69ea 100644 --- a/Include/Extensions/NRIWrapperMTL.h +++ b/Include/Extensions/NRIWrapperMTL.h @@ -42,7 +42,13 @@ NRI_STRUCT(BufferMTLDesc) void* mappedMemory; MTLResourceOptions options; } - + +NRI_STRUCT(TextureMTLDesc) +{ + id mtlTexture; + MTLTextureDescriptor* descriptor; +} + NRI_STRUCT(MemoryMTLDesc) { diff --git a/Source/Creation/Creation.cpp b/Source/Creation/Creation.cpp index d9d8a23a..10d3cf74 100644 --- a/Source/Creation/Creation.cpp +++ b/Source/Creation/Creation.cpp @@ -348,6 +348,17 @@ NRI_API Format NRI_CALL nriConvertDXGIFormatToNRI(uint32_t dxgiFormat) { return DXGIFormatToNRIFormat(dxgiFormat); } +NRI_API uint32_t NRI_CALL nriConvertNRIFormatToMTL(Format format) { + MaybeUnused(format); + +#if NRI_USE_VULKAN + return NRIFormatToMTLFormat(format); +#else + return 0; +#endif +} + + NRI_API uint32_t NRI_CALL nriConvertNRIFormatToVK(Format format) { MaybeUnused(format); diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h new file mode 100644 index 00000000..4150710c --- /dev/null +++ b/Source/Metal/ConversionMTL.h @@ -0,0 +1,33 @@ +#pragma once + +#import + +#include "NRIMacro.h" +#include "NRIDescs.h" +#include "Extensions/NRIWrapperVK.h" + +namespace nri { + + constexpr std::array IMAGE_TYPES = { + MTLTextureType1D, // TEXTURE_1D + MTLTextureType2D, // TEXTURE_2D + MTLTextureType3D, // TEXTURE_3D + }; + + constexpr MTLTextureType GetImageTypeMTL(TextureType type) { + return IMAGE_TYPES[(size_t)type]; + } + + + inline MTLTextureType GetFormatMTL(Format format, bool demoteSrgb = false) { + if (demoteSrgb) { + const FormatProps& formatProps = GetFormatProps(format); + if (formatProps.isSrgb) + format = (Format)((uint32_t)format - 1); + } + + return (MTLTextureType)NRIFormatToMTLFormat(format); + } + +}; + diff --git a/Source/Metal/ConversionMTL.mm b/Source/Metal/ConversionMTL.mm new file mode 100644 index 00000000..92d6ea5c --- /dev/null +++ b/Source/Metal/ConversionMTL.mm @@ -0,0 +1,82 @@ +#include "ConversionMTL.h" + +// Each depth/stencil format is only compatible with itself in VK +constexpr std::array MLT_FORMAT = { + MTLPixelFormatInvalid, // UNKNOWN + MTLPixelFormatR8Unorm, // R8_UNORM + MTLPixelFormatR8Snorm, // R8_SNORM + MTLPixelFormatR8Uint, // R8_UINT + MTLPixelFormatR8Sint, // R8_SINT + MTLPixelFormatRG8Unorm, // RG8_UNORM + MTLPixelFormatRG8Snorm, // RG8_SNORM + MTLPixelFormatRG8Uint, // RG8_UINT + MTLPixelFormatRG8Sint, // RG8_SINT + MTLPixelFormatBGRA8Unorm, // BGRA8_UNORM + MTLPixelFormatBGRA8Unorm_sRGB, // BGRA8_SRGB + MTLPixelFormatRGBA8Unorm, // RGBA8_UNORM + MTLPixelFormatRGBA8Unorm_sRGB, // RGBA8_SRGB + MTLPixelFormatRGBA8Snorm, // RGBA8_SNORM + MTLPixelFormatRGBA8Uint, // RGBA8_UINT + MTLPixelFormatRGBA8Sint, // RGBA8_SINT + MTLPixelFormatR16Unorm, // R16_UNORM + MTLPixelFormatR16Snorm, // R16_SNORM + MTLPixelFormatR16Uint, // R16_UINT + MTLPixelFormatR16Sint, // R16_SINT + MTLPixelFormatR16Float, // R16_SFLOAT + MTLPixelFormatRG16Unorm, // RG16_UNORM + MTLPixelFormatRG16Snorm, // RG16_SNORM + MTLPixelFormatRG16Uint, // RG16_UINT + MTLPixelFormatRG16Sint, // RG16_SINT + MTLPixelFormatRG16Float, // RG16_SFLOAT + MTLPixelFormatRGBA16Unorm, // RGBA16_UNORM + MTLPixelFormatRGBA16Snorm, // RGBA16_SNORM + MTLPixelFormatRGBA16Uint, // RGBA16_UINT + MTLPixelFormatRGBA16Sint, // RGBA16_SINT + MTLPixelFormatRGBA16Float, // RGBA16_SFLOAT + MTLPixelFormatR32Uint, // R32_UINT + MTLPixelFormatR32Sint, // R32_SINT + MTLPixelFormatR32Float, // R32_SFLOAT + MTLPixelFormatRG32Uint, // RG32_UINT + MTLPixelFormatRG32Sint, // RG32_SINT + MTLPixelFormatRG32Float, // RG32_SFLOAT + MTLPixelFormatRGB32Uint, // RGB32_UINT + MTLPixelFormatRGB32Sint, // RGB32_SINT + MTLPixelFormatRGB32Float, // RGB32_SFLOAT + MTLPixelFormatRGBA32Uint, // RGB32_UINT + MTLPixelFormatRGBA32Sint, // RGB32_SINT + MTLPixelFormatRGBA32Float, // RGB32_SFLOAT + MTLPixelFormatB5G6R5Unorm, // B5_G6_R5_UNORM + MTLPixelFormatA1BGR5Unorm, // B5_G5_R5_A1_UNORM + MTLPixelFormatABGR4Unorm, // B4_G4_R4_A4_UNORM + MTLPixelFormatRGB10A2Unorm, // R10_G10_B10_A2_UNORM + MTLPixelFormatRGB10A2Uint, // R10_G10_B10_A2_UINT + MTLPixelFormatRG11B10Float, // R11_G11_B10_UFLOAT + MTLPixelFormatRGB9E5Float, // R9_G9_B9_E5_UFLOAT + MTLPixelFormatBC1_RGBA, // BC1_RGBA_UNORM + MTLPixelFormatBC1_RGBA_sRGB, // BC1_RGBA_SRGB + MTLPixelFormatBC2_RGBA, // BC2_RGBA_UNORM + MTLPixelFormatBC2_RGBA_sRGB, // BC2_RGBA_SRGB + MTLPixelFormatBC3_RGBA, // BC3_RGBA_UNORM + MTLPixelFormatBC3_RGBA_sRGB, // BC3_RGBA_SRGB + MTLPixelFormatBC4_RUnorm, // BC4_R_UNORM + MTLPixelFormatBC4_RSnorm, // BC4_R_SNORM + MTLPixelFormatBC5_RGUnorm, // BC5_RG_UNORM + MTLPixelFormatBC5_RGSnorm, // BC5_RG_SNORM + MTLPixelFormatBC6H_RGBUfloat, // BC6H_RGB_UFLOAT + MTLPixelFormatBC6H_RGBFloat, // BC6H_RGB_SFLOAT + MTLPixelFormatBC7_RGBAUnorm, // BC7_RGBA_UNORM + MTLPixelFormatBC7_RGBAUnorm_sRGB, // BC7_RGBA_SRGB + MTLPixelFormatDepth16Unorm, // D16_UNORM + MTLPixelFormatDepth24Unorm_Stencil8, // D24_UNORM_S8_UINT + MTLPixelFormatDepth32Float, // D32_SFLOAT + MTLPixelFormatDepth32Float_Stencil8, // D32_SFLOAT_S8_UINT_X24 + MTLPixelFormatDepth24Unorm_Stencil8, // R24_UNORM_X8 + MTLPixelFormatDepth24Unorm_Stencil8, // X24_G8_UINT + MTLPixelFormatDepth32Float_Stencil8, // R32_SFLOAT_X8_X24 + MTLPixelFormatDepth32Float_Stencil8, // X32_G8_UINT_X24 +}; + +uint32_t NRIFormatToMTLFormat(Format format) { + return (uint32_t)VK_FORMAT[(uint32_t)format]; +} + diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index 5b244d49..a3997dc1 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -3,23 +3,24 @@ namespace nri { struct DeviceMTl final : public DeviceBase { - BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); - ~BufferMetal(); - + DeviceMTl(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); + ~DeviceMTl(); Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); - //================================================================================================================ - // DeviceBase - //================================================================================================================ + inline operator id&() const { + return m_Device; + } inline const DeviceDesc& GetDesc() const { return m_Desc; } FormatSupportBits GetFormatSupport(const Device& device, Format format); - + + void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const; + Result FillFunctionTable(CoreInterface& table) const; Result FillFunctionTable(HelperInterface& table) const; Result FillFunctionTable(LowLatencyInterface& table) const; diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index 97b79920..65bade1b 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -42,8 +42,16 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa } - - +void DeviceMTL::FillCreateInfo(const TextureDesc& textureDesc, MTLTextureDescriptor* info) const { + info.textureType = ::GetImageTypeMTL(textureDesc.type); + into.pixelFormat = ::GetFormatMTL(textureDesc.format, true); + into.width = pDesc->mWidth; + into.height = pDesc->mHeight; + into.depth = pDesc->mDepth; + into.mipmapLevelCount = mipLevels; + into.sampleCount = pDesc->mSampleCount; + into.arrayLength = pDesc->mArraySize; +} Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { m_OwnsNativeObjects = !isWrapper; diff --git a/Source/Metal/FencMTL.mm b/Source/Metal/FencMTL.mm new file mode 100644 index 00000000..f009671b --- /dev/null +++ b/Source/Metal/FencMTL.mm @@ -0,0 +1,11 @@ +#include "FenceMTL.h" + +using namespace nri; + +FenceMTL::~FenceMTL() { + +} + +Result FenceMTL::Create(uint64_t initialValue) { + m_Handle = dispatch_semaphore_create(initialValue); +} diff --git a/Source/Metal/FenceMTL.h b/Source/Metal/FenceMTL.h new file mode 100644 index 00000000..3aa3c29a --- /dev/null +++ b/Source/Metal/FenceMTL.h @@ -0,0 +1,28 @@ +// © 2021 NVIDIA Corporation +#pragma once + +namespace nri { + +struct DeviceMTL; + +struct FenceMTL { + inline FenceMTL(DeviceMTL& device) + : m_Device(device) { + } + ~FenceMTL(); + + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + Result Create(uint64_t initialValue); + + //void SetDebugName(const char* name); + //uint64_t GetFenceValue() const; + //void Wait(uint64_t value); +private: + dispatch_semaphore_t m_Handle; + DeviceMTL& m_Device; +}; + +} // namespace nri diff --git a/Source/Metal/MemoryMTL.h b/Source/Metal/MemoryMTL.h index 2374d2a8..72caa7a4 100644 --- a/Source/Metal/MemoryMTL.h +++ b/Source/Metal/MemoryMTL.h @@ -14,11 +14,11 @@ struct MemoryMTL { : m_Device(device) { } - inline VkDeviceMemory GetHandle() const { - return m_Handle; + inline id GetHandle() const { + return m_Buffer; } - inline DeviceVK& GetDevice() const { + inline DeviceMTL& GetDevice() const { return m_Device; } diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index 0195ba8d..b856e502 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -11,10 +11,26 @@ struct TextureMTL { inline TextureMTL(DeviceMTL& device) : m_Device(device) { } + + ~TextureMTL(); + + inline id& GetHandle() const { + return m_Handle; + } + + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + + Result Create(const TextureDesc& textureDesc); + Result Create(const TextureMTLDesc& textureDesc); +// Result Create(const AllocateTextureDesc& textureDesc); + private: DeviceMTL& m_Device; - + id m_Handle; + TextureDesc m_Desc = {}; }; - } diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm index e69de29b..876cc2d3 100644 --- a/Source/Metal/TextureMTL.mm +++ b/Source/Metal/TextureMTL.mm @@ -0,0 +1,24 @@ +#include "TextureMTL.h" + +TextureMTL::~TextureMTL() { + m_Handle = nil; +} + + +Result Create(const TextureMTLDesc& textureDesc) { + m_Handle = texturedesc.texture; + return Result::SUCCESS; +} + +Result Create(const TextureMTLDesc& textureDesc) { + return Result::SUCCESS; +} + +Result Create(const TextureDesc& textureDesc) { + MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init]; + m_Device.FillCreateInfo(textureDesc, info); + + m_Handle = [m_Device newTextureWithDescriptor:textureDesc]; + return Result::SUCCESS; +} + diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index d555d7cb..df7958c7 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -169,6 +169,7 @@ nri::Format VKFormatToNRIFormat(uint32_t vkFormat); uint32_t NRIFormatToDXGIFormat(nri::Format format); uint32_t NRIFormatToVKFormat(nri::Format format); +uint32_t NRIFormatToMTLFormat(nri::Format format); // Misc inline nri::Vendor GetVendorFromID(uint32_t vendorID) { From 57b025e407375adb9471530e734df37d060e0430 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sun, 1 Sep 2024 00:01:47 -0700 Subject: [PATCH 07/27] feat: add more to api Signed-off-by: Michael Pollind --- Source/Metal/BufferMTL.h | 2 - Source/Metal/BufferMTL.mm | 11 +++ Source/Metal/ConversionMTL.h | 159 +++++++++++++++++++++++++++--- Source/Metal/DeviceMTL.h | 6 +- Source/Metal/DeviceMTL.hpp | 77 +++++++++------ Source/Metal/DeviceMTL.mm | 2 +- Source/Metal/PipelineLayoutMTL.h | 54 ++++++++++ Source/Metal/PipelineLayoutMTL.mm | 0 Source/Metal/PipelineMTL.h | 20 ++++ Source/Metal/PipelineMTL.mm | 55 +++++++++++ Source/Metal/TextureMTL.h | 9 +- 11 files changed, 337 insertions(+), 58 deletions(-) create mode 100644 Source/Metal/PipelineLayoutMTL.h create mode 100644 Source/Metal/PipelineLayoutMTL.mm create mode 100644 Source/Metal/PipelineMTL.h create mode 100644 Source/Metal/PipelineMTL.mm diff --git a/Source/Metal/BufferMTL.h b/Source/Metal/BufferMTL.h index a71e7578..c7924f53 100644 --- a/Source/Metal/BufferMTL.h +++ b/Source/Metal/BufferMTL.h @@ -1,5 +1,3 @@ - - #import struct DeviceMTL; diff --git a/Source/Metal/BufferMTL.mm b/Source/Metal/BufferMTL.mm index f01f9abf..a3367227 100644 --- a/Source/Metal/BufferMTL.mm +++ b/Source/Metal/BufferMTL.mm @@ -1,2 +1,13 @@ #include "BufferMetal.h" +Result BufferMTL::Create(const BufferDesc& bufferDesc) { + return RESULT::SUCCESS; +} + +Result BufferMTL::Create(const BufferVKDesc& bufferDesc) { + return RESULT::SUCCESS; +} + +Result BufferMTL::Create(const AllocateBufferDesc& bufferDesc) { + return RESULT::SUCCESS; +} diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index 4150710c..b381dc37 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -8,26 +8,153 @@ namespace nri { - constexpr std::array IMAGE_TYPES = { - MTLTextureType1D, // TEXTURE_1D - MTLTextureType2D, // TEXTURE_2D - MTLTextureType3D, // TEXTURE_3D - }; - - constexpr MTLTextureType GetImageTypeMTL(TextureType type) { - return IMAGE_TYPES[(size_t)type]; - } +constexpr std::array IMAGE_TYPES = { + MTLTextureType1D, // TEXTURE_1D + MTLTextureType2D, // TEXTURE_2D + MTLTextureType3D, // TEXTURE_3D +}; + +constexpr MTLTextureType GetImageTypeMTL(TextureType type) { + return IMAGE_TYPES[(size_t)type]; +} + +constexpr std::array BLEND_OP = { + MTLBlendOperationAdd, // ADD + MTLBlendOperationSubtract, // SUBTRACT + MTLBlendOperationReverseSubtract, // REVERSE_SUBTRACT + MTLBlendOperationMin, // MIN + MTLBlendOperationMax // MAX +}; +constexpr MTLBlendOperation GetBlendOp(BlendFunc blendFunc) { + return BLEND_OP[(size_t)blendFunc]; +} + +constexpr std::array BLEND_FACTOR = { + MTLBlendFactorZero, // ZERO + MTLBlendFactorOne, // ONE + MTLBlendFactorSourceColor, // SRC_COLOR + MTLBlendFactorOneMinusSourceColor, // ONE_MINUS_SRC_COLOR + MTLBlendFactorDestinationColor, // DST_COLOR + MTLBlendFactorOneMinusDestinationColor, // ONE_MINUS_DST_COLOR + MTLBlendFactorSourceAlpha, // SRC_ALPHA + MTLBlendFactorOneMinusSourceAlpha, // ONE_MINUS_SRC_ALPHA + MTLBlendFactorDestinationAlpha, // DST_ALPHA + MTLBlendFactorOneMinusDestinationAlpha, // ONE_MINUS_DST_ALPHA + MTLBlendFactorBlendColor, // CONSTANT_COLOR + MTLBlendFactorOneMinusBlendColor, // ONE_MINUS_CONSTANT_COLOR + MTLBlendFactorBlendAlpha, // CONSTANT_ALPHA + MTLBlendFactorOneMinusBlendAlpha, // ONE_MINUS_CONSTANT_ALPHA + MTLBlendFactorSourceAlphaSaturated, // SRC_ALPHA_SATURATE + MTLBlendFactorSource1Color, // SRC1_COLOR + MTLBlendFactorOneMinusSource1Color, // ONE_MINUS_SRC1_COLOR + MTLBlendFactorSource1Alpha, // SRC1_ALPHA + MTLBlendFactorOneMinusSource1Alpha, // ONE_MINUS_SRC1_ALPHA +}; + + + +constexpr MTLColorWriteMask GetColorComponent(ColorWriteBits colorWriteMask) { + return MTLColorWriteMask(colorWriteMask) & MTLColorWriteMaskAll; +} + + +constexpr MTLBlendFactor GetBlendFactor(BlendFactor blendFactor) { + return BLEND_FACTOR[(size_t)blendFactor]; +} + +constexpr std::array COMPARE_OP = { + MTLCompareFunctionNever, // NONE + MTLCompareFunctionAlways, // ALWAYS + MTLCompareFunctionNever, // NEVER + MTLCompareFunctionEqual, // EQUAL + MTLCompareFunctionNotEqual, // NOT_EQUAL + MTLCompareFunctionLess, // LESS + MTLCompareFunctionLessEqual, // LESS_EQUAL + MTLCompareFunctionGreater, // GREATER + MTLCompareFunctionGreaterEqual, // GREATER_EQUAL +}; + +constexpr MTLCompareFunction GetCompareOp(CompareFunc compareFunc) { + return COMPARE_OP[(size_t)compareFunc]; +} + +constexpr std::array STENCIL_OP = { + MTLStencilOperationKeep, // KEEP + MTLStencilOperationZero, // ZERO + MTLStencilOperationReplace, // REPLACE + MTLStencilOperationIncrementClamp, // INCREMENT_AND_CLAMP + MTLStencilOperationDecrementClamp, // DECREMENT_AND_CLAMP + MTLStencilOperationInvert, // INVERT + MTLStencilOperationIncrementWrap, // INCREMENT_AND_WRAP + MTLStencilOperationDecrementWrap // DECREMENT_AND_WRAP +}; + +constexpr MTLStencilOperation GetStencilOp(StencilFunc stencilFunc) { + return STENCIL_OP[(size_t)stencilFunc]; +} + + +constexpr std::array CULL_MODES = { + MTLCullModeNone, // NONE + MTLCullModeFront, // FRONT + MTLCullModeBack // BACK +}; + +constexpr MTLCullMode GetCullMode(CullMode cullMode) { + return CULL_MODES[(size_t)cullMode]; +} + +constexpr std::array POLYGON_MODES = { + MTLTriangleFillModeFill, // SOLID + MTLTriangleFillModeLines // WIREFRAME +}; + +constexpr MTLTriangleFillMode GetPolygonMode(FillMode fillMode) { + return POLYGON_MODES[(size_t)fillMode]; +} + +constexpr std::array SAMPLER_ADDRESS_MODE = { + MTLSamplerAddressModeRepeat, // REPEAT + MTLSamplerAddressModeMirrorRepeat, // MIRRORED_REPEAT + MTLSamplerAddressModeClampToEdge, // CLAMP_TO_EDGE + MTLSamplerAddressModeClampToBorder // CLAMP_TO_BORDER +}; + +constexpr MTLSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) { + return SAMPLER_ADDRESS_MODE[(size_t)addressMode]; +} + + + +constexpr std::array TOPOLOGIES = { + MTLPrimitiveTopologyClassPoint, // POINT_LIST + MTLPrimitiveTopologyClassLine, // LINE_LIST + MTLPrimitiveTopologyClassLine, // LINE_STRIP + MTLPrimitiveTopologyClassTriangle, // TRIANGLE_LIST + MTLPrimitiveTopologyClassTriangle, // TRIANGLE_STRIP + MTLPrimitiveTopologyClassLine, // LINE_LIST_WITH_ADJACENCY + MTLPrimitiveTopologyClassLine, // LINE_STRIP_WITH_ADJACENCY + MTLPrimitiveTopologyClassTriangle, // TRIANGLE_LIST_WITH_ADJACENCY + MTLPrimitiveTopologyClassTriangle, // TRIANGLE_STRIP_WITH_ADJACENCY + MTLPrimitiveTopologyClassTriangle // PATCH_LIST TODO: not sure +}; + + +constexpr MTLPrimitiveTopologyClass GetTopologyMTL(Topology topology) { + return TOPOLOGIES[(size_t)topology]; +} - inline MTLTextureType GetFormatMTL(Format format, bool demoteSrgb = false) { - if (demoteSrgb) { - const FormatProps& formatProps = GetFormatProps(format); - if (formatProps.isSrgb) - format = (Format)((uint32_t)format - 1); - } - return (MTLTextureType)NRIFormatToMTLFormat(format); +inline MTLTextureType GetFormatMTL(Format format, bool demoteSrgb = false) { + if (demoteSrgb) { + const FormatProps& formatProps = GetFormatProps(format); + if (formatProps.isSrgb) + format = (Format)((uint32_t)format - 1); } + + return (MTLTextureType)NRIFormatToMTLFormat(format); +} }; diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index a3997dc1..11aa325a 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -2,9 +2,9 @@ namespace nri { -struct DeviceMTl final : public DeviceBase { - DeviceMTl(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); - ~DeviceMTl(); +struct DeviceMTL final : public DeviceBase { + DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); + ~DeviceMTL(); Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp index 267e1f81..8e2b3b4c 100644 --- a/Source/Metal/DeviceMTL.hpp +++ b/Source/Metal/DeviceMTL.hpp @@ -1,3 +1,5 @@ +// © 2021 NVIDIA Corporation + Declare_PartiallyFillFunctionTable_Functions(MTL); #pragma region[ Core ] @@ -19,90 +21,101 @@ static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format } static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - D3D12_RESOURCE_DESC desc = {}; - GetResourceDesc(&desc, bufferDesc); + //D3D12_RESOURCE_DESC desc = {}; + //GetResourceDesc(&desc, bufferDesc); - ((const DeviceD3D12&)device).GetMemoryDesc(memoryLocation, desc, memoryDesc); + //((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); + //D3D12_RESOURCE_DESC desc = {}; + //GetResourceDesc(&desc, textureDesc); - ((const DeviceD3D12&)device).GetMemoryDesc(memoryLocation, desc, memoryDesc); + //((const DeviceD3D12&)device).GetMemoryDesc(memoryLocation, desc, memoryDesc); } static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - return ((DeviceD3D12&)device).GetCommandQueue(commandQueueType, commandQueue); + return ((DeviceMTL&)device).GetCommandQueue(commandQueueType, commandQueue); } static Result NRI_CALL CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator) { - DeviceD3D12& device = ((CommandQueueD3D12&)commandQueue).GetDevice(); - return device.CreateImplementation(commandAllocator, commandQueue); + //DeviceMTL& device = ((CommandQueueD3D12&)commandQueue).GetDevice(); + //return device.CreateImplementation(commandAllocator, commandQueue); + return Result::SUCCESS; } static Result NRI_CALL CreateDescriptorPool(Device& device, const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) { - return ((DeviceD3D12&)device).CreateImplementation(descriptorPool, descriptorPoolDesc); + //return ((DeviceMTL&)device).CreateImplementation(descriptorPool, descriptorPoolDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffer) { - return ((DeviceD3D12&)device).CreateImplementation(buffer, bufferDesc); + //return ((DeviceMTL&)device).CreateImplementation(buffer, bufferDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& texture) { - return ((DeviceD3D12&)device).CreateImplementation(texture, textureDesc); + //return ((DeviceMTL&)device).CreateImplementation(texture, textureDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { - DeviceD3D12& device = ((const BufferD3D12*)bufferViewDesc.buffer)->GetDevice(); - return device.CreateImplementation(bufferView, bufferViewDesc); + //DeviceMTL& device = ((const BufferD3D12*)bufferViewDesc.buffer)->GetDevice(); + //return device.CreateImplementation(bufferView, bufferViewDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) { - DeviceD3D12& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); - return device.CreateImplementation(textureView, textureViewDesc); + //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); + //return device.CreateImplementation(textureView, textureViewDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) { - DeviceD3D12& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); - return device.CreateImplementation(textureView, textureViewDesc); + //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); + //return device.CreateImplementation(textureView, textureViewDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) { - DeviceD3D12& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); - return device.CreateImplementation(textureView, textureViewDesc); + //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); + //return device.CreateImplementation(textureView, textureViewDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateSampler(Device& device, const SamplerDesc& samplerDesc, Descriptor*& sampler) { - return ((DeviceD3D12&)device).CreateImplementation(sampler, samplerDesc); + //return ((DeviceMTL&)device).CreateImplementation(sampler, samplerDesc); + return Result::SUCCESS; } static Result NRI_CALL CreatePipelineLayout(Device& device, const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) { - return ((DeviceD3D12&)device).CreateImplementation(pipelineLayout, pipelineLayoutDesc); + //return ((DeviceMTL&)device).CreateImplementation(pipelineLayout, pipelineLayoutDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateGraphicsPipeline(Device& device, const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) { - return ((DeviceD3D12&)device).CreateImplementation(pipeline, graphicsPipelineDesc); + //return ((DeviceMTL&)device).CreateImplementation(pipeline, graphicsPipelineDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) { - return ((DeviceD3D12&)device).CreateImplementation(pipeline, computePipelineDesc); + return ((DeviceMTL&)device).CreateImplementation(pipeline, computePipelineDesc); } static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) { - return ((DeviceD3D12&)device).CreateImplementation(fence, initialValue); + return ((DeviceMTL&)device).CreateImplementation(fence, initialValue); } static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) { - return ((DeviceD3D12&)device).CreateImplementation(queryPool, queryPoolDesc); + return ((DeviceMTL&)device).CreateImplementation(queryPool, queryPoolDesc); } static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) { - Destroy((CommandBufferD3D12*)&commandBuffer); + //Destroy((CommandBufferD3D12*)&commandBuffer); } static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) { - Destroy((CommandAllocatorD3D12*)&commandAllocator); + //Destroy((CommandAllocatorD3D12*)&commandAllocator); } static void NRI_CALL DestroyDescriptorPool(DescriptorPool& descriptorPool) { @@ -138,15 +151,15 @@ static void NRI_CALL DestroyFence(Fence& fence) { } static Result NRI_CALL AllocateMemory(Device& device, const AllocateMemoryDesc& allocateMemoryDesc, Memory*& memory) { - return ((DeviceD3D12&)device).CreateImplementation(memory, allocateMemoryDesc); + return ((DeviceMTL&)device).CreateImplementation(memory, allocateMemoryDesc); } static Result NRI_CALL BindBufferMemory(Device& device, const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { - return ((DeviceD3D12&)device).BindBufferMemory(memoryBindingDescs, memoryBindingDescNum); + return ((DeviceMTL&)device).BindBufferMemory(memoryBindingDescs, memoryBindingDescNum); } static Result NRI_CALL BindTextureMemory(Device& device, const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { - return ((DeviceD3D12&)device).BindTextureMemory(memoryBindingDescs, memoryBindingDescNum); + return ((DeviceMTL&)device).BindTextureMemory(memoryBindingDescs, memoryBindingDescNum); } static void NRI_CALL FreeMemory(Memory& memory) { @@ -154,7 +167,7 @@ static void NRI_CALL FreeMemory(Memory& memory) { } static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { - ((DeviceD3D12&)device).SetDebugName(name); + ((DeviceMTL&)device).SetDebugName(name); } static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) { diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index 65bade1b..dc200912 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -39,7 +39,7 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - + return Result::SUCCESS; } void DeviceMTL::FillCreateInfo(const TextureDesc& textureDesc, MTLTextureDescriptor* info) const { diff --git a/Source/Metal/PipelineLayoutMTL.h b/Source/Metal/PipelineLayoutMTL.h new file mode 100644 index 00000000..464c31c0 --- /dev/null +++ b/Source/Metal/PipelineLayoutMTL.h @@ -0,0 +1,54 @@ +#pragma once + +namespace nri { + +struct DeviceMTL; + +struct PipelineLayoutMTL { + inline PipelineLayoutMTL (DeviceMTL& device) + : m_Device(device) { + } + +// inline operator VkPipelineLayout() const { +// return m_Handle; +// } +// +// inline DeviceVK& GetDevice() const { +// return m_Device; +// } +// +// inline const RuntimeBindingInfo& GetRuntimeBindingInfo() const { +// return m_RuntimeBindingInfo; +// } +// +// inline VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t index) const { +// return m_DescriptorSetLayouts[index]; +// } +// +// inline VkPipelineBindPoint GetPipelineBindPoint() const { +// return m_PipelineBindPoint; +// } +// +// inline uint32_t GetDescriptorSetSpace(uint32_t setIndexInPipelineLayout) const { +// return m_DescriptorSetSpaces[setIndexInPipelineLayout]; +// } +// + ~PipelineLayoutMTL(); +// +// Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); +// +// //================================================================================================================ +// // NRI +// //================================================================================================================ +// +// void SetDebugName(const char* name); + + +private: + DeviceMTL& m_Device; + id m_PipelineDesc; + //Vector m_DescriptorSetLayouts; + +}; + +} diff --git a/Source/Metal/PipelineLayoutMTL.mm b/Source/Metal/PipelineLayoutMTL.mm new file mode 100644 index 00000000..e69de29b diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h new file mode 100644 index 00000000..61b43444 --- /dev/null +++ b/Source/Metal/PipelineMTL.h @@ -0,0 +1,20 @@ +#pragma once + +namespace nri { + +struct DeviceMTL; +struct PipelineLayoutMTL; + +struct PipelineMTL { + inline PipelineMTL(DeviceMTL& device) + : m_Device(device) { + } + ~PipelineMTL(); + + Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); +private: + DeviceMTL& m_Device; +}; + +} + diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm new file mode 100644 index 00000000..53493acb --- /dev/null +++ b/Source/Metal/PipelineMTL.mm @@ -0,0 +1,55 @@ +#include "PipelineMTL.h" +#import + +PipelineMTL::~PipelineMTL() { + +} + +Result PipelineMTL::Create(const GraphicsPipelineDesc& graphicsPipelineDesc) { + MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init]; + for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) { + } + + const VertexInputDesc *vi = graphicsPipelineDesc.vertexInput; + if (vi) { + VkVertexInputBindingDescription* streams const_cast(vertexInputState.pVertexBindingDescriptions); + for (uint32_t i = 0; i < vi->streamNum; i++) { + const VertexStreamDesc &stream = vi->streams[i]; + renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stride = stream.stride; + renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stepRate = 1; + } + } + + const InputAssemblyDesc& ia = graphicsPipelineDesc.inputAssembly; + renderPipelineDesc.inputPrimitiveTopology = GetTopologyMTL(ia.topology); + + if (graphicsPipelineDesc.multisample) { + // TODO: multisampling + } + + + // Blending + const OutputMergerDesc& om = graphicsPipelineDesc.outputMerger; + + // assign render target pixel format for all attachments + for (uint32_t i = 0; i < om.colorNum; i++) { + + MTLRenderPipelineColorAttachmentDescriptor& colorAtachment = &renderPipelineDesc.colorAttachments[i]; + const ColorAttachmentDesc& attachmentDesc = om.color[i]; + + colorAtachment.pixelFormat = GetFormatMTL(attachmentDesc.format, false); + + colorAtachment.blendingEnabled = attachmentDesc.blendEnabled; + colorAtachment.rgbBlendOperation = GetBlendOp(attachmentDesc.colorBlend.func); + colorAtachment.alphaBlendOperation = GetBlendOp(attachmentDesc.alphaBlend.func); + + colorAtachment.sourceRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.srcFactor); + colorAtachment.destinationRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.dstFactor); + colorAtachment.sourceAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.srcFactor); + colorAtachment.destinationAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.dstFactor); + + colorAtachment.writeMask = GetColorComponent(pDesc->mColorWriteMasks[blendDescIndex]); + + } + +} diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index b856e502..d10e9895 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -11,7 +11,6 @@ struct TextureMTL { inline TextureMTL(DeviceMTL& device) : m_Device(device) { } - ~TextureMTL(); inline id& GetHandle() const { @@ -22,15 +21,17 @@ struct TextureMTL { return m_Device; } + inline const TextureDesc& GetDesc() const { + return m_Desc; + } Result Create(const TextureDesc& textureDesc); Result Create(const TextureMTLDesc& textureDesc); -// Result Create(const AllocateTextureDesc& textureDesc); private: DeviceMTL& m_Device; - id m_Handle; TextureDesc m_Desc = {}; + id m_Handle; }; -} +} // namespace nri \ No newline at end of file From e63c3314768aa4a5543521d021b6c14b1a435d01 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sun, 1 Sep 2024 17:31:14 -0700 Subject: [PATCH 08/27] feat: fix build Signed-off-by: Michael Pollind --- CMakeLists.txt | 2 +- Include/Extensions/NRIWrapperMTL.h | 60 +++++----- Include/NRIDescs.h | 11 +- Source/Creation/Creation.cpp | 4 +- Source/Metal/BufferMTL.h | 28 ++--- Source/Metal/BufferMTL.mm | 13 ++- Source/Metal/CommandQueueMTL.h | 8 +- Source/Metal/CommandQueueMTL.mm | 9 +- Source/Metal/ConversionMTL.h | 43 +++----- Source/Metal/ConversionMTL.mm | 14 ++- Source/Metal/DeviceMTL.h | 14 ++- Source/Metal/DeviceMTL.hpp | 47 ++++---- Source/Metal/DeviceMTL.mm | 172 ++++++++++++++--------------- Source/Metal/FencMTL.mm | 2 + Source/Metal/FenceMTL.h | 3 + Source/Metal/MemoryMTL.h | 2 + Source/Metal/MemoryMTL.mm | 13 ++- Source/Metal/PipelineLayoutMTL.h | 5 +- Source/Metal/PipelineLayoutMTL.mm | 11 ++ Source/Metal/PipelineMTL.h | 2 + Source/Metal/PipelineMTL.mm | 37 ++++--- Source/Metal/SharedMTL.h | 8 ++ Source/Metal/TextureMTL.h | 15 +-- Source/Metal/TextureMTL.mm | 31 ++++-- Source/Shared/SharedExternal.h | 5 +- 25 files changed, 310 insertions(+), 249 deletions(-) create mode 100644 Source/Metal/SharedMTL.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b77741b..f5ee3422 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,7 +254,7 @@ if (NRI_ENABLE_METAL_SUPPORT AND APPLE) endif() # VK -if (NRI_ENABLE_VK_SUPPORT AND NOT APPLE) +if (NRI_ENABLE_VK_SUPPORT) message ("NRI adding backend: VK") set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_VK=1) diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h index 5ecf69ea..96a8fc0c 100644 --- a/Include/Extensions/NRIWrapperMTL.h +++ b/Include/Extensions/NRIWrapperMTL.h @@ -7,55 +7,57 @@ NRI_NAMESPACE_BEGIN -NRI_ENUM -( - MTLGPUFamily, uint8_t, - - Apple1, - Apple2, - Apple3, - Apple4, - Apple5, - Apple6, - Apple7, - Mac1, - Mac2, - - MAX_APPLE_FAMILY_NUM -); +typedef void* MTLBufferHandle; +typedef void* MTLTextureHandle; +typedef void* MTLDeviceHandle; + +//NRI_ENUM +//( +// MTLGPUFamily, uint8_t, + // Apple1, +// Apple2, +// Apple3, +// Apple4, +// Apple5, +// Apple6, +// Apple7, +// Mac1, +// Mac2, + +// MAX_APPLE_FAMILY_NUM +//); NRI_STRUCT(DeviceCreationMTLDesc) { bool enableNRIValidation; - id MtlDevice; -} + MTLDeviceHandle MtlDevice; +}; NRI_STRUCT(CommandBufferMTLDesc) { -} +}; NRI_STRUCT(BufferMTLDesc) { - id buffer; + MTLBufferHandle buffer; void* mappedMemory; - MTLResourceOptions options; -} + //MTLResourceOptions options; +}; NRI_STRUCT(TextureMTLDesc) { - id mtlTexture; - MTLTextureDescriptor* descriptor; -} - + //MTLTextureHandle mtlTexture; + //MTLTextureDescriptor* descriptor; +}; NRI_STRUCT(MemoryMTLDesc) { - id buffer; + MTLBufferHandle buffer; void* mappedMemory; - MTLResourceOptions options; -} + //MTLResourceOptions options; +}; NRI_API NRI_NAME(Result) NRI_CALL nriCreateDeviceFromMtlDevice(const NRI_NAME_REF(DeviceCreationMTLDesc) deviceDesc, NRI_NAME_REF(Device*) device); diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index 98ed5208..1c1a044b 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -1293,7 +1293,14 @@ NriEnum(GraphicsAPI, uint8_t, MAX_NUM ); -NriEnum(Vendor, uint8_t, +// defined in apple framework +#undef INTEL +#undef AMD + +NRI_ENUM +( + + Vendor, uint8_t, UNKNOWN, NVIDIA, AMD, @@ -1301,7 +1308,7 @@ NriEnum(Vendor, uint8_t, APPLE, MAX_NUM -); +); NriStruct(AdapterDesc) { char name[256]; diff --git a/Source/Creation/Creation.cpp b/Source/Creation/Creation.cpp index 10d3cf74..05c284a5 100644 --- a/Source/Creation/Creation.cpp +++ b/Source/Creation/Creation.cpp @@ -298,7 +298,7 @@ NRI_API Result NRI_CALL nriCreateDeviceFromMtlDevice(const DeviceCreationMTLDesc DeviceBase* deviceImpl = nullptr; #if NRI_USE_MTL - result = CreateDeviceD3D12(tempDeviceCreationD3D12Desc, deviceImpl); + //result = CreateDeviceD3D12(tempDeviceCreationD3D12Desc, deviceImpl); #endif if (result != Result::SUCCESS) @@ -477,6 +477,8 @@ NRI_API void NRI_CALL nriReportLiveObjects() { pDebug->ReportLiveObjects(DXGI_DEBUG_ALL, (DXGI_DEBUG_RLO_FLAGS)((uint32_t)DXGI_DEBUG_RLO_DETAIL | (uint32_t)DXGI_DEBUG_RLO_IGNORE_INTERNAL)); } +#elif __APPLE__ + #else # include # define GET_VK_FUNCTION(instance, name) \ diff --git a/Source/Metal/BufferMTL.h b/Source/Metal/BufferMTL.h index c7924f53..63be8c84 100644 --- a/Source/Metal/BufferMTL.h +++ b/Source/Metal/BufferMTL.h @@ -1,38 +1,40 @@ +// © 2021 NVIDIA Corporation +#pragma once + #import +namespace nri { + struct DeviceMTL; struct MemoryMTL; struct BufferMTL { - + inline BufferMTL(DeviceMTL& device) - : m_Device(device) { + : m_Device(device) { } - + inline id GetHandle() const { return pBuffer; } - - inline VkDeviceAddress GetDeviceAddress() const { - return m_DeviceAddress; - } - + + inline DeviceMTL& GetDevice() const { return m_Device; } - + inline const BufferDesc& GetDesc() const { return m_Desc; } - + ~BufferMTL(); Result Create(const BufferDesc& bufferDesc); Result Create(const BufferVKDesc& bufferDesc); Result Create(const AllocateBufferDesc& bufferDesc); - + private: - DeviceVK& m_Device; + DeviceMTL& m_Device; id pBuffer; uint8_t* m_MappedMemory = nullptr; uint64_t m_MappedMemoryOffset = 0; @@ -43,4 +45,4 @@ struct BufferMTL { }; - +}; diff --git a/Source/Metal/BufferMTL.mm b/Source/Metal/BufferMTL.mm index a3367227..38e4759e 100644 --- a/Source/Metal/BufferMTL.mm +++ b/Source/Metal/BufferMTL.mm @@ -1,13 +1,18 @@ -#include "BufferMetal.h" +#include "SharedMTL.h" + +#include "BufferMTL.h" + +using namespace nri; + Result BufferMTL::Create(const BufferDesc& bufferDesc) { - return RESULT::SUCCESS; + return Result::SUCCESS; } Result BufferMTL::Create(const BufferVKDesc& bufferDesc) { - return RESULT::SUCCESS; + return Result::SUCCESS; } Result BufferMTL::Create(const AllocateBufferDesc& bufferDesc) { - return RESULT::SUCCESS; + return Result::SUCCESS; } diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h index 0c5b35bf..e983c5ef 100644 --- a/Source/Metal/CommandQueueMTL.h +++ b/Source/Metal/CommandQueueMTL.h @@ -1,6 +1,8 @@ // © 2021 NVIDIA Corporation #pragma once +#import + namespace nri { struct DeviceMTL; @@ -32,12 +34,14 @@ struct CommandQueueMTL { inline Lock& GetLock() { return m_Lock; } - + + Result WaitForIdle(); + void SetDebugName(const char* name); Result Create(CommandQueueType type, uint32_t familyIndex, id handle); private: DeviceMTL& m_Device; - uint32_t m_FamilyIndex = INVALID_FAMILY_INDEX; + uint32_t m_FamilyIndex = 0; CommandQueueType m_Type = CommandQueueType(-1); id m_Handle; Lock m_Lock; diff --git a/Source/Metal/CommandQueueMTL.mm b/Source/Metal/CommandQueueMTL.mm index 6956aa25..dde2fbcd 100644 --- a/Source/Metal/CommandQueueMTL.mm +++ b/Source/Metal/CommandQueueMTL.mm @@ -1,12 +1,13 @@ // © 2021 NVIDIA Corporation +#include "SharedMTL.h" + #include "CommandQueueMTL.h" using namespace nri; - CommandQueueMTL::~CommandQueueMTL() { - m_CommandQueue = nil; + m_Handle = nil; } Result CommandQueueMTL::Create(CommandQueueType type, uint32_t familyIndex, id handle) { @@ -20,7 +21,7 @@ [m_Handle setLabel:[NSString stringWithUTF8String:name]]; } -Result CommandQueueVK::WaitForIdle() { +Result CommandQueueMTL::WaitForIdle() { id waitCmdBuf = [m_Handle commandBufferWithUnretainedReferences]; @@ -28,4 +29,4 @@ [waitCmdBuf waitUntilCompleted]; waitCmdBuf = nil; -} \ No newline at end of file +} diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index b381dc37..d1aa5b5e 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -1,23 +1,9 @@ #pragma once -#import - -#include "NRIMacro.h" -#include "NRIDescs.h" -#include "Extensions/NRIWrapperVK.h" +#include namespace nri { -constexpr std::array IMAGE_TYPES = { - MTLTextureType1D, // TEXTURE_1D - MTLTextureType2D, // TEXTURE_2D - MTLTextureType3D, // TEXTURE_3D -}; - -constexpr MTLTextureType GetImageTypeMTL(TextureType type) { - return IMAGE_TYPES[(size_t)type]; -} - constexpr std::array BLEND_OP = { MTLBlendOperationAdd, // ADD MTLBlendOperationSubtract, // SUBTRACT @@ -29,6 +15,16 @@ constexpr MTLBlendOperation GetBlendOp(BlendFunc blendFunc) { return BLEND_OP[(size_t)blendFunc]; } +constexpr std::array IMAGE_TYPES = { + MTLTextureType1D, // TEXTURE_1D + MTLTextureType2D, // TEXTURE_2D + MTLTextureType3D, // TEXTURE_3D +}; + +constexpr MTLTextureType GetImageTypeMTL(TextureType type) { + return IMAGE_TYPES[(size_t)type]; +} + constexpr std::array BLEND_FACTOR = { MTLBlendFactorZero, // ZERO MTLBlendFactorOne, // ONE @@ -51,17 +47,14 @@ constexpr std::array BLEND_FACTOR MTLBlendFactorOneMinusSource1Alpha, // ONE_MINUS_SRC1_ALPHA }; - +constexpr MTLBlendFactor GetBlendFactor(BlendFactor blendFactor) { + return BLEND_FACTOR[(size_t)blendFactor]; +} constexpr MTLColorWriteMask GetColorComponent(ColorWriteBits colorWriteMask) { return MTLColorWriteMask(colorWriteMask) & MTLColorWriteMaskAll; } - -constexpr MTLBlendFactor GetBlendFactor(BlendFactor blendFactor) { - return BLEND_FACTOR[(size_t)blendFactor]; -} - constexpr std::array COMPARE_OP = { MTLCompareFunctionNever, // NONE MTLCompareFunctionAlways, // ALWAYS @@ -117,7 +110,7 @@ constexpr std::array SAMPLE MTLSamplerAddressModeRepeat, // REPEAT MTLSamplerAddressModeMirrorRepeat, // MIRRORED_REPEAT MTLSamplerAddressModeClampToEdge, // CLAMP_TO_EDGE - MTLSamplerAddressModeClampToBorder // CLAMP_TO_BORDER + MTLSamplerAddressModeClampToZero // CLAMP_TO_BORDER }; constexpr MTLSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) { @@ -144,16 +137,14 @@ constexpr MTLPrimitiveTopologyClass GetTopologyMTL(Topology topology) { return TOPOLOGIES[(size_t)topology]; } - - -inline MTLTextureType GetFormatMTL(Format format, bool demoteSrgb = false) { +inline MTLPixelFormat GetFormatMTL(Format format, bool demoteSrgb = false) { if (demoteSrgb) { const FormatProps& formatProps = GetFormatProps(format); if (formatProps.isSrgb) format = (Format)((uint32_t)format - 1); } - return (MTLTextureType)NRIFormatToMTLFormat(format); + return (MTLPixelFormat)NRIFormatToMTLFormat(format); } }; diff --git a/Source/Metal/ConversionMTL.mm b/Source/Metal/ConversionMTL.mm index 92d6ea5c..8e13b6d1 100644 --- a/Source/Metal/ConversionMTL.mm +++ b/Source/Metal/ConversionMTL.mm @@ -1,7 +1,11 @@ +#include "SharedMTL.h" + #include "ConversionMTL.h" +using namespace nri; + // Each depth/stencil format is only compatible with itself in VK -constexpr std::array MLT_FORMAT = { +constexpr std::array MTL_FORMAT = { MTLPixelFormatInvalid, // UNKNOWN MTLPixelFormatR8Unorm, // R8_UNORM MTLPixelFormatR8Snorm, // R8_SNORM @@ -39,9 +43,9 @@ MTLPixelFormatRG32Uint, // RG32_UINT MTLPixelFormatRG32Sint, // RG32_SINT MTLPixelFormatRG32Float, // RG32_SFLOAT - MTLPixelFormatRGB32Uint, // RGB32_UINT - MTLPixelFormatRGB32Sint, // RGB32_SINT - MTLPixelFormatRGB32Float, // RGB32_SFLOAT + MTLPixelFormatInvalid, // RGB32_UINT //TODO: lookup correct values + MTLPixelFormatInvalid, // RGB32_SINT //TODO: lookup correct values + MTLPixelFormatInvalid, // RGB32_SFLOAT //TODO: lookup correct values MTLPixelFormatRGBA32Uint, // RGB32_UINT MTLPixelFormatRGBA32Sint, // RGB32_SINT MTLPixelFormatRGBA32Float, // RGB32_SFLOAT @@ -77,6 +81,6 @@ }; uint32_t NRIFormatToMTLFormat(Format format) { - return (uint32_t)VK_FORMAT[(uint32_t)format]; + return (uint32_t)MTL_FORMAT[(uint32_t)format]; } diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index 11aa325a..3edeb1de 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -1,7 +1,11 @@ #pragma once +#import + namespace nri { +struct CommandQueueMTL; + struct DeviceMTL final : public DeviceBase { DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); ~DeviceMTL(); @@ -9,7 +13,7 @@ struct DeviceMTL final : public DeviceBase { Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); - inline operator id&() const { + inline operator id() const { return m_Device; } @@ -17,14 +21,15 @@ struct DeviceMTL final : public DeviceBase { return m_Desc; } - FormatSupportBits GetFormatSupport(const Device& device, Format format); + //FormatSupportBits GetFormatSupport(const Device& device, Format format); - void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const; + //void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const; + // void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const; Result FillFunctionTable(CoreInterface& table) const; Result FillFunctionTable(HelperInterface& table) const; Result FillFunctionTable(LowLatencyInterface& table) const; - Result FillFunctionTable(MeshShaderInterface& table) const; + Result FillFnctionTable(MeshShaderInterface& table) const; Result FillFunctionTable(RayTracingInterface& table) const; Result FillFunctionTable(StreamerInterface& table) const; Result FillFunctionTable(SwapChainInterface& table) const; @@ -32,7 +37,6 @@ struct DeviceMTL final : public DeviceBase { Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper); private: - DeviceDesc m_Desc = {}; id m_Device; std::array m_CommandQueues = {}; DeviceDesc m_Desc = {}; diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp index 8e2b3b4c..43c43596 100644 --- a/Source/Metal/DeviceMTL.hpp +++ b/Source/Metal/DeviceMTL.hpp @@ -99,15 +99,18 @@ static Result NRI_CALL CreateGraphicsPipeline(Device& device, const GraphicsPipe } static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) { - return ((DeviceMTL&)device).CreateImplementation(pipeline, computePipelineDesc); + //return ((DeviceMTL&)device).CreateImplementation(pipeline, computePipelineDesc); + return Result::SUCCESS; } static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) { - return ((DeviceMTL&)device).CreateImplementation(fence, initialValue); + //return ((DeviceMTL&)device).CreateImplementation(fence, initialValue); + return Result::SUCCESS; } static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) { - return ((DeviceMTL&)device).CreateImplementation(queryPool, queryPoolDesc); + //return ((DeviceMTL&)device).CreateImplementation(queryPool, queryPoolDesc); + return Result::SUCCESS; } static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) { @@ -119,74 +122,78 @@ static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) } static void NRI_CALL DestroyDescriptorPool(DescriptorPool& descriptorPool) { - Destroy((DescriptorPoolD3D12*)&descriptorPool); + //Destroy((DescriptorPoolD3D12*)&descriptorPool); } static void NRI_CALL DestroyBuffer(Buffer& buffer) { - Destroy((BufferD3D12*)&buffer); + //Destroy((BufferD3D12*)&buffer); } static void NRI_CALL DestroyTexture(Texture& texture) { - Destroy((TextureD3D12*)&texture); + //Destroy((TextureD3D12*)&texture); } static void NRI_CALL DestroyDescriptor(Descriptor& descriptor) { - Destroy((DescriptorD3D12*)&descriptor); + //Destroy((DescriptorD3D12*)&descriptor); } static void NRI_CALL DestroyPipelineLayout(PipelineLayout& pipelineLayout) { - Destroy((PipelineLayoutD3D12*)&pipelineLayout); + //Destroy((PipelineLayoutD3D12*)&pipelineLayout); } static void NRI_CALL DestroyPipeline(Pipeline& pipeline) { - Destroy((PipelineD3D12*)&pipeline); + //Destroy((PipelineD3D12*)&pipeline); } static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) { - Destroy((QueryPoolD3D12*)&queryPool); + //Destroy((QueryPoolD3D12*)&queryPool); } static void NRI_CALL DestroyFence(Fence& fence) { - Destroy((FenceD3D12*)&fence); + //Destroy((FenceD3D12*)&fence); } static Result NRI_CALL AllocateMemory(Device& device, const AllocateMemoryDesc& allocateMemoryDesc, Memory*& memory) { - return ((DeviceMTL&)device).CreateImplementation(memory, allocateMemoryDesc); + return Result::SUCCESS; + //return ((DeviceMTL&)device).CreateImplementation(memory, allocateMemoryDesc); } static Result NRI_CALL BindBufferMemory(Device& device, const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { - return ((DeviceMTL&)device).BindBufferMemory(memoryBindingDescs, memoryBindingDescNum); + //return ((DeviceMTL&)device).BindBufferMemory(memoryBindingDescs, memoryBindingDescNum); + return Result::SUCCESS; } static Result NRI_CALL BindTextureMemory(Device& device, const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { - return ((DeviceMTL&)device).BindTextureMemory(memoryBindingDescs, memoryBindingDescNum); + //return ((DeviceMTL&)device).BindTextureMemory(memoryBindingDescs, memoryBindingDescNum); + return Result::SUCCESS; } static void NRI_CALL FreeMemory(Memory& memory) { - Destroy((MemoryD3D12*)&memory); +// Destroy((MemoryD3D12*)&memory); } static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { - ((DeviceMTL&)device).SetDebugName(name); +// ((DeviceMTL&)device).SetDebugName(name); } static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) { - ((PipelineD3D12&)pipeline).SetDebugName(name); +// ((PipelineD3D12&)pipeline).SetDebugName(name); } static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) { - ((PipelineLayoutD3D12&)pipelineLayout).SetDebugName(name); +// ((PipelineLayoutD3D12&)pipelineLayout).SetDebugName(name); } static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) { - ((MemoryMetal&)memory).SetDebugName(name); +// ((MemoryMetal&)memory).SetDebugName(name); } static void* NRI_CALL GetDeviceNativeObject(const Device& device) { if (!(&device)) return nullptr; + return (MTLDevice*)((DeviceMTL&)device); - return ((DeviceMetal&)device).GetNativeObject(); + //return ((DeviceMetal&)device).GetNativeObject(); } Result DeviceMetal::FillFunctionTable(CoreInterface& table) const { diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index dc200912..b6543c4e 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -1,12 +1,55 @@ -#include "DeviceMetal.h" +#include "SharedMTL.h" + +#include "DeviceMTL.h" using namespace nri; +static bool FindMTLGpuFamily(id device, + MTLGPUFamily *family) { + // https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc + if (@available(macOS 10.15, iOS 10.13, *)) { + if ([device supportsFamily:MTLGPUFamilyApple7]) { + (*family) = MTLGPUFamily::MTLGPUFamilyApple7; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple6]) { + (*family) = MTLGPUFamily::MTLGPUFamilyApple6; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple5]) { + (*family) = MTLGPUFamily::MTLGPUFamilyApple5; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple4]) { + (*family) = MTLGPUFamily::MTLGPUFamilyApple4; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple3]) { + (*family) = MTLGPUFamily::MTLGPUFamilyApple3; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple2]) { + (*family) = MTLGPUFamily::MTLGPUFamilyApple2; + return true; + } + if ([device supportsFamily:MTLGPUFamilyApple1]) { + (*family) = MTLGPUFamily::MTLGPUFamilyApple1; + return true; + } + + // This family is no longer supported in the macOS 10.15 SDK but still + // exists so default to it. + // return MTLGPUFamily::Mac1; + (*family) = MTLGPUFamily::MTLGPUFamilyMac1; + return true; + } + return false; +} + -static inline bool FindMTLGpuFamily(id device, MTLGPUFamily *family); -BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) +DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) : DeviceBase(callbacks, stdAllocator) { m_Desc.graphicsAPI = GraphicsAPI::VK; m_Desc.nriVersionMajor = NRI_VERSION_MAJOR; @@ -14,50 +57,42 @@ } -BufferMetal::~BufferMetal() { - -} +DeviceMTL::~DeviceMTL() { -Result CreateDeviceMTL(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) { - } -Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBase*& device) { - -} - -FormatSupportBits DeviceMTL::GetFormatSupport(const Device& device, Format format) { - int currentFamily = HIGHEST_GPU_FAMILY; - for (; currentFamily >= (int)MTLGPUFamilyApple1; currentFamily--) { - if ([m_device supportsFamily:(MTLGPUFamily)currentFamily]) { - highestAppleFamily = (MTLGPUFamily)currentFamily; - break; - } - } -} +//FormatSupportBits DeviceMTL::GetFormatSupport(const Device& device, Format format) { +// int currentFamily = HIGHEST_GPU_FAMILY; +// for (; currentFamily >= (int)MTLGPUFamilyApple1; currentFamily--) { +// if ([m_Device supportsFamily:(MTLGPUFamily)currentFamily]) { +// highestAppleFamily = (MTLGPUFamily)currentFamily; +// break; +// } +// } +//} Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { return Result::SUCCESS; } -void DeviceMTL::FillCreateInfo(const TextureDesc& textureDesc, MTLTextureDescriptor* info) const { - info.textureType = ::GetImageTypeMTL(textureDesc.type); - into.pixelFormat = ::GetFormatMTL(textureDesc.format, true); - into.width = pDesc->mWidth; - into.height = pDesc->mHeight; - into.depth = pDesc->mDepth; - into.mipmapLevelCount = mipLevels; - into.sampleCount = pDesc->mSampleCount; - into.arrayLength = pDesc->mArraySize; -} +//void DeviceMTL::FillCreateInfo(const TextureDesc& textureDesc, MTLTextureDescriptor* info) const { +// info.textureType = ::GetImageTypeMTL(textureDesc.type); +// //info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); +// //info.width = pDesc->mWidth; +// //info.height = pDesc->mHeight; +// //info.depth = pDesc->mDepth; +// //info.mipmapLevelCount = mipLevels; +// //info.sampleCount = pDesc->mSampleCount; +// //info.arrayLength = pDesc->mArraySize; +//} Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { m_OwnsNativeObjects = !isWrapper; if(isWrapper) { - m_Device = deviceCreationMTLDesc.MtlDevice; + // m_Device = deviceCreationMTLDesc.MtlDevice; } strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name)); @@ -69,28 +104,28 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa const uint64_t regID = [m_Device registryID]; if (regID) { - IORef entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID))); - if (entry) - { + // IORef entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID))); + // if (entry) + // { // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. - IORef deviceEntry; - if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess) - { - m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))); - m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id")); - } - } + // IORef deviceEntry; + // if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess) + // { + // m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))); + // m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id")); + // } + // } } MTLGPUFamily family; - if(!FindMTLGpuFamily(m_Device, family)) { - return Result::UNSUPPORTED; - } + // if(!FindMTLGpuFamily(m_Device, family)) { + // return Result::UNSUPPORTED; + //} // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf //TODO: fill desc switch(family) { - case MTLGPUFamily::Apple1: - break; + // case MTLGPUFamily::Apple1: + // break; default: break; } @@ -102,46 +137,3 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa } - -static inline bool FindMTLGpuFamily(id device, - MTLGPUFamily *family) { - // https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc - if (@available(macOS 10.15, iOS 10.13, *)) { - if ([device supportsFamily:MTLGPUFamilyApple7]) { - (*family) = MTLGPUFamily::Apple7; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple6]) { - (*family) = MTLGPUFamily::Apple6; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple5]) { - (*family) = MTLGPUFamily::Apple5; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple4]) { - (*family) = MTLGPUFamily::Apple4; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple3]) { - (*family) = MTLGPUFamily::Apple3; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple2]) { - (*family) = MTLGPUFamily::Apple2; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple1]) { - (*family) = MTLGPUFamily::Apple1; - return true; - } - - // This family is no longer supported in the macOS 10.15 SDK but still - // exists so default to it. - // return MTLGPUFamily::Mac1; - (*family) = MTLGPUFamily::Mac1; - return true; - } - return false; -} - diff --git a/Source/Metal/FencMTL.mm b/Source/Metal/FencMTL.mm index f009671b..d0ee5d55 100644 --- a/Source/Metal/FencMTL.mm +++ b/Source/Metal/FencMTL.mm @@ -1,3 +1,5 @@ +#include "SharedMTL.h" + #include "FenceMTL.h" using namespace nri; diff --git a/Source/Metal/FenceMTL.h b/Source/Metal/FenceMTL.h index 3aa3c29a..0531e63b 100644 --- a/Source/Metal/FenceMTL.h +++ b/Source/Metal/FenceMTL.h @@ -1,6 +1,9 @@ // © 2021 NVIDIA Corporation + #pragma once +#import + namespace nri { struct DeviceMTL; diff --git a/Source/Metal/MemoryMTL.h b/Source/Metal/MemoryMTL.h index 72caa7a4..9ab89d5f 100644 --- a/Source/Metal/MemoryMTL.h +++ b/Source/Metal/MemoryMTL.h @@ -4,6 +4,8 @@ #include +#import + namespace nri { struct DeviceMTL; diff --git a/Source/Metal/MemoryMTL.mm b/Source/Metal/MemoryMTL.mm index 528facdb..b94511cb 100644 --- a/Source/Metal/MemoryMTL.mm +++ b/Source/Metal/MemoryMTL.mm @@ -1,4 +1,5 @@ // © 2021 NVIDIA Corporation +#include "SharedMTL.h" #include "MemoryMTL.h" @@ -12,20 +13,20 @@ } Result MemoryMTL::Create(const MemoryMTLDesc& memoryDesc) { - if (!memoryDesc.vkDeviceMemory) - return Result::INVALID_ARGUMENT; + // if (!memoryDesc.vkDeviceMemory) + // return Result::INVALID_ARGUMENT; - MemoryTypeInfo memoryTypeInfo = {}; + // MemoryTypeInfo memoryTypeInfo = {}; //bool found = m_Device.GetMemoryTypeByIndex(memoryDesc.memoryTypeIndex, memoryTypeInfo); //RETURN_ON_FAILURE(&m_Device, found, Result::INVALID_ARGUMENT, "Can't find memory by index"); m_OwnsNativeObjects = false; - m_Buffer = memoryDesc.buffer; + // m_Buffer = memoryDesc.buffer; m_MappedMemory = (uint8_t*)memoryDesc.mappedMemory; - m_Type = memoryDesc.options; + // m_Type = memoryDesc.options; - const auto& mtl = m_Device.GetDispatchTable(); + // const auto& mtl = m_Device.GetDispatchTable(); //if (!m_MappedMemory && IsHostVisibleMemory(memoryTypeInfo.location)) { // VkResult result = mtl.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); diff --git a/Source/Metal/PipelineLayoutMTL.h b/Source/Metal/PipelineLayoutMTL.h index 464c31c0..e3c2a2b1 100644 --- a/Source/Metal/PipelineLayoutMTL.h +++ b/Source/Metal/PipelineLayoutMTL.h @@ -1,5 +1,7 @@ #pragma once +#import + namespace nri { struct DeviceMTL; @@ -30,7 +32,7 @@ struct PipelineLayoutMTL { // } // // inline uint32_t GetDescriptorSetSpace(uint32_t setIndexInPipelineLayout) const { -// return m_DescriptorSetSpaces[setIndexInPipelineLayout]; + // } // ~PipelineLayoutMTL(); @@ -46,7 +48,6 @@ struct PipelineLayoutMTL { private: DeviceMTL& m_Device; - id m_PipelineDesc; //Vector m_DescriptorSetLayouts; }; diff --git a/Source/Metal/PipelineLayoutMTL.mm b/Source/Metal/PipelineLayoutMTL.mm index e69de29b..7760b5f1 100644 --- a/Source/Metal/PipelineLayoutMTL.mm +++ b/Source/Metal/PipelineLayoutMTL.mm @@ -0,0 +1,11 @@ +#include "SharedMTL.h" + +#include "PipelineLayoutMTL.h" + +using namespace nri; + +PipelineLayoutMTL::~PipelineLayoutMTL() { + +} + + diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index 61b43444..ccb7e15b 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -1,5 +1,7 @@ #pragma once +#import + namespace nri { struct DeviceMTL; diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index 53493acb..3603b528 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -1,5 +1,8 @@ +#include "SharedMTL.h" + #include "PipelineMTL.h" -#import + +using namespace nri; PipelineMTL::~PipelineMTL() { @@ -12,12 +15,12 @@ const VertexInputDesc *vi = graphicsPipelineDesc.vertexInput; if (vi) { - VkVertexInputBindingDescription* streams const_cast(vertexInputState.pVertexBindingDescriptions); - for (uint32_t i = 0; i < vi->streamNum; i++) { - const VertexStreamDesc &stream = vi->streams[i]; - renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stride = stream.stride; - renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stepRate = 1; - } + // VkVertexInputBindingDescription* streams const_cast(vertexInputState.pVertexBindingDescriptions); + //for (uint32_t i = 0; i < vi->streamNum; i++) { + // const VertexStreamDesc &stream = vi->streams[i]; + // renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stride = stream.stride; + // renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stepRate = 1; + // } } const InputAssemblyDesc& ia = graphicsPipelineDesc.inputAssembly; @@ -34,21 +37,21 @@ // assign render target pixel format for all attachments for (uint32_t i = 0; i < om.colorNum; i++) { - MTLRenderPipelineColorAttachmentDescriptor& colorAtachment = &renderPipelineDesc.colorAttachments[i]; + // MTLRenderPipelineColorAttachmentDescriptor& colorAtachment = &renderPipelineDesc.colorAttachments[i]; const ColorAttachmentDesc& attachmentDesc = om.color[i]; - colorAtachment.pixelFormat = GetFormatMTL(attachmentDesc.format, false); + //colorAtachment.pixelFormat = GetFormatMTL(attachmentDesc.format, false); - colorAtachment.blendingEnabled = attachmentDesc.blendEnabled; - colorAtachment.rgbBlendOperation = GetBlendOp(attachmentDesc.colorBlend.func); - colorAtachment.alphaBlendOperation = GetBlendOp(attachmentDesc.alphaBlend.func); + //colorAtachment.blendingEnabled = attachmentDesc.blendEnabled; + //colorAtachment.rgbBlendOperation = GetBlendOp(attachmentDesc.colorBlend.func); + //colorAtachment.alphaBlendOperation = GetBlendOp(attachmentDesc.alphaBlend.func); - colorAtachment.sourceRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.srcFactor); - colorAtachment.destinationRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.dstFactor); - colorAtachment.sourceAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.srcFactor); - colorAtachment.destinationAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.dstFactor); + //colorAtachment.sourceRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.srcFactor); + //colorAtachment.destinationRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.dstFactor); + //colorAtachment.sourceAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.srcFactor); + //colorAtachment.destinationAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.dstFactor); - colorAtachment.writeMask = GetColorComponent(pDesc->mColorWriteMasks[blendDescIndex]); + //colorAtachment.writeMask = GetColorComponent(pDesc->mColorWriteMasks[blendDescIndex]); } diff --git a/Source/Metal/SharedMTL.h b/Source/Metal/SharedMTL.h new file mode 100644 index 00000000..58262f0f --- /dev/null +++ b/Source/Metal/SharedMTL.h @@ -0,0 +1,8 @@ +#import + +#include "SharedExternal.h" + +#include "ConversionMTL.h" +#include "DeviceMTL.h" + + diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index d10e9895..296b8049 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -1,10 +1,11 @@ // © 2021 NVIDIA Corporation - #pragma once +#import + namespace nri { -struct DeviceMTL; +struct DeviceMTL; struct TextureMTL { @@ -13,9 +14,9 @@ struct TextureMTL { } ~TextureMTL(); - inline id& GetHandle() const { - return m_Handle; - } + //inline id GetHandle() const { + // return m_Handle; + //} inline DeviceMTL& GetDevice() const { return m_Device; @@ -26,7 +27,7 @@ struct TextureMTL { } Result Create(const TextureDesc& textureDesc); - Result Create(const TextureMTLDesc& textureDesc); + //Result Create(const TextureMTLDesc& textureDesc); private: DeviceMTL& m_Device; @@ -34,4 +35,4 @@ struct TextureMTL { id m_Handle; }; -} // namespace nri \ No newline at end of file +} // namespace nri diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm index 876cc2d3..edbb8d92 100644 --- a/Source/Metal/TextureMTL.mm +++ b/Source/Metal/TextureMTL.mm @@ -1,24 +1,31 @@ +#include "SharedMTL.h" + #include "TextureMTL.h" +using namespace nri; + TextureMTL::~TextureMTL() { m_Handle = nil; } -Result Create(const TextureMTLDesc& textureDesc) { - m_Handle = texturedesc.texture; - return Result::SUCCESS; -} - -Result Create(const TextureMTLDesc& textureDesc) { - return Result::SUCCESS; -} +//Result TextureMTL::Create(const TextureMTLDesc& textureDesc) { +// m_Handle = texturedesc.texture; +// return Result::SUCCESS; +//} -Result Create(const TextureDesc& textureDesc) { +Result TextureMTL::Create(const TextureDesc& textureDesc) { MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init]; - m_Device.FillCreateInfo(textureDesc, info); - - m_Handle = [m_Device newTextureWithDescriptor:textureDesc]; + info.textureType = ::GetImageTypeMTL(textureDesc.type); + info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); + info.width = textureDesc.width; + info.height = textureDesc.height; + info.depth = textureDesc.depth; + info.mipmapLevelCount = textureDesc.mipNum; + // info.sampleCount = textureDesc->mSampleCount; + // info.arrayLength = textureDesc->mArraySize; + + //m_Handle = [m_Device newTextureWithDescriptor:textureDesc]; return Result::SUCCESS; } diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index df7958c7..583a3c83 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -1,4 +1,4 @@ -// © 2021 NVIDIA Corporation +// © 2021 NVIDIA Corporation #pragma once @@ -29,6 +29,7 @@ typedef uint32_t DXGI_FORMAT; #include "Extensions/NRIWrapperD3D11.h" #include "Extensions/NRIWrapperD3D12.h" #include "Extensions/NRIWrapperVK.h" +#include "Extensions/NRIWrapperMTL.h" #include "NRICompatibility.hlsli" @@ -180,8 +181,6 @@ inline nri::Vendor GetVendorFromID(uint32_t vendorID) { return nri::Vendor::AMD; case 0x8086: return nri::Vendor::INTEL; - case 0x106b: - return nri::Vendor::APPLE; } return nri::Vendor::UNKNOWN; From 9f465e7bb9cafef9ea2431d8221cdda3e0a163b9 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sun, 1 Sep 2024 21:30:31 -0700 Subject: [PATCH 09/27] feat: add graphics queu Signed-off-by: Michael Pollind --- Source/Metal/CommandQueueMTL.h | 7 +-- Source/Metal/CommandQueueMTL.mm | 13 ++++-- Source/Metal/DeviceMTL.h | 19 +++++++-- Source/Metal/DeviceMTL.hpp | 19 ++++++++- Source/Metal/DeviceMTL.mm | 75 +++++++++++++++++++++++---------- Source/Metal/TextureMTL.h | 2 +- Source/Metal/TextureMTL.mm | 9 ++-- 7 files changed, 103 insertions(+), 41 deletions(-) diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h index e983c5ef..7d48be94 100644 --- a/Source/Metal/CommandQueueMTL.h +++ b/Source/Metal/CommandQueueMTL.h @@ -22,10 +22,6 @@ struct CommandQueueMTL { inline DeviceMTL& GetDevice() const { return m_Device; } - - inline uint32_t GetFamilyIndex() const { - return m_FamilyIndex; - } inline CommandQueueType GetType() const { return m_Type; @@ -38,10 +34,9 @@ struct CommandQueueMTL { Result WaitForIdle(); void SetDebugName(const char* name); - Result Create(CommandQueueType type, uint32_t familyIndex, id handle); + Result Create(CommandQueueType type); private: DeviceMTL& m_Device; - uint32_t m_FamilyIndex = 0; CommandQueueType m_Type = CommandQueueType(-1); id m_Handle; Lock m_Lock; diff --git a/Source/Metal/CommandQueueMTL.mm b/Source/Metal/CommandQueueMTL.mm index dde2fbcd..1eb63051 100644 --- a/Source/Metal/CommandQueueMTL.mm +++ b/Source/Metal/CommandQueueMTL.mm @@ -10,10 +10,17 @@ m_Handle = nil; } -Result CommandQueueMTL::Create(CommandQueueType type, uint32_t familyIndex, id handle) { +Result CommandQueueMTL::Create(CommandQueueType type) { m_Type = type; - m_FamilyIndex = familyIndex; - m_Handle = handle; + const char* queueNames[] = { + "GRAPHICS_QUEUE", // GRAPHICS + "TRANSFER_QUEUE", // TRANSFER + "COMPUTE_QUEUE" // COMPUTE + }; + + m_Handle = [m_Device newCommandQueueWithMaxCommandBufferCount:512]; + SetDebugName(queueNames[(uint32_t)type]); + return Result::SUCCESS; } diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index 3edeb1de..de013697 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -21,11 +21,23 @@ struct DeviceMTL final : public DeviceBase { return m_Desc; } - //FormatSupportBits GetFormatSupport(const Device& device, Format format); + void Destruct() override; - //void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const; - // void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const; + template + inline Result CreateImplementation(Interface*& entity, const Args&... args) { + Implementation* impl = Allocate(GetStdAllocator(), *this); + Result result = impl->Create(args...); + if (result != Result::SUCCESS) { + Destroy(GetStdAllocator(), impl); + entity = nullptr; + } else + entity = (Interface*)impl; + + return result; + } + + Result FillFunctionTable(CoreInterface& table) const; Result FillFunctionTable(HelperInterface& table) const; Result FillFunctionTable(LowLatencyInterface& table) const; @@ -37,6 +49,7 @@ struct DeviceMTL final : public DeviceBase { Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper); private: + Lock m_Lock; id m_Device; std::array m_CommandQueues = {}; DeviceDesc m_Desc = {}; diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp index 43c43596..c05ec9a4 100644 --- a/Source/Metal/DeviceMTL.hpp +++ b/Source/Metal/DeviceMTL.hpp @@ -214,24 +214,39 @@ Result DeviceMetal::FillFunctionTable(CoreInterface& table) const { #pragma endregion - Result DeviceMetal::FillFunctionTable(HelperInterface& table) const { - + table = {}; + return ResVult::UNSUPPORTED; } Result DeviceMetal::FillFunctionTable(LowLatencyInterface& table) const { + table = {}; + return ResVult::UNSUPPORTED; } Result DeviceMetal::FillFunctionTable(MeshShaderInterface& table) const { + table = {}; + return ResVult::UNSUPPORTED; } Result DeviceMetal::FillFunctionTable(RayTracingInterface& table) const { + table = {}; + return ResVult::UNSUPPORTED; } + Result DeviceMetal::FillFunctionTable(StreamerInterface& table) const { + table = {}; + return ResVult::UNSUPPORTED; } + Result DeviceMetal::FillFunctionTable(SwapChainInterface& table) const { + table = {}; + return ResVult::UNSUPPORTED; } + Result DeviceMetal::FillFunctionTable(ResourceAllocatorInterface& table) const { + table = {}; + return ResVult::UNSUPPORTED; } Define_Core_Device_PartiallyFillFunctionTable(MTL); diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index b6543c4e..2bdcdbae 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -1,5 +1,7 @@ #include "SharedMTL.h" +#include "CommandQueueMTL.h" + #include "DeviceMTL.h" using namespace nri; @@ -48,15 +50,18 @@ static bool FindMTLGpuFamily(id device, - -DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) - : DeviceBase(callbacks, stdAllocator) { +DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) : DeviceBase(callbacks, stdAllocator) { m_Desc.graphicsAPI = GraphicsAPI::VK; m_Desc.nriVersionMajor = NRI_VERSION_MAJOR; m_Desc.nriVersionMinor = NRI_VERSION_MINOR; } + +void DeviceMTL::Destruct() { + Destroy(GetStdAllocator(), this); +} + DeviceMTL::~DeviceMTL() { } @@ -74,7 +79,31 @@ static bool FindMTLGpuFamily(id device, Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - return Result::SUCCESS; + ExclusiveScope lock(m_Lock); + + // Check if already created (or wrapped) + uint32_t index = (uint32_t)commandQueueType; + if (m_CommandQueues[index]) { + commandQueue = (CommandQueue*)m_CommandQueues[index]; + return Result::SUCCESS; + } + + // Check if supported + //uint32_t queueFamilyIndex = m_QueueFamilyIndices[index]; + //if (queueFamilyIndex == INVALID_FAMILY_INDEX) { + // commandQueue = nullptr; + // return Result::UNSUPPORTED; + //} + + // Create + //VkQueue handle = VK_NULL_HANDLE; + //m_VK.GetDeviceQueue(m_Device, queueFamilyIndex, 0, &handle); + + Result result = CreateImplementation(commandQueue, commandQueueType); + if (result == Result::SUCCESS) + m_CommandQueues[index] = (CommandQueueMTL*)commandQueue; + + return result; } //void DeviceMTL::FillCreateInfo(const TextureDesc& textureDesc, MTLTextureDescriptor* info) const { @@ -90,36 +119,36 @@ static bool FindMTLGpuFamily(id device, Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { m_OwnsNativeObjects = !isWrapper; - + if(isWrapper) { - // m_Device = deviceCreationMTLDesc.MtlDevice; + // m_Device = deviceCreationMTLDesc.MtlDevice; } - + strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name)); // No vendor id, device id for Apple GPUs if (strstr(m_Desc.adapterDesc.name, "Apple")) { m_Desc.adapterDesc.vendor = nri::Vendor::APPLE; - } - + } + const uint64_t regID = [m_Device registryID]; if (regID) { - // IORef entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID))); - // if (entry) - // { - // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. - // IORef deviceEntry; - // if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess) - // { - // m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))); - // m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id")); - // } - // } + // IORef entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID))); + // if (entry) + // { + // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. + // IORef deviceEntry; + // if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess) + // { + // m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))); + // m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id")); + // } + // } } - + MTLGPUFamily family; - // if(!FindMTLGpuFamily(m_Device, family)) { - // return Result::UNSUPPORTED; + // if(!FindMTLGpuFamily(m_Device, family)) { + // return Result::UNSUPPORTED; //} // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf //TODO: fill desc diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index 296b8049..f4f5841f 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -26,7 +26,7 @@ struct TextureMTL { return m_Desc; } - Result Create(const TextureDesc& textureDesc); + Result CreateFromTextureDesc(const TextureDesc& textureDesc); //Result Create(const TextureMTLDesc& textureDesc); private: diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm index edbb8d92..3d4cf67e 100644 --- a/Source/Metal/TextureMTL.mm +++ b/Source/Metal/TextureMTL.mm @@ -14,7 +14,7 @@ // return Result::SUCCESS; //} -Result TextureMTL::Create(const TextureDesc& textureDesc) { +Result TextureMTL::CreateFromTextureDesc(const TextureDesc& textureDesc) { MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init]; info.textureType = ::GetImageTypeMTL(textureDesc.type); info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); @@ -22,8 +22,11 @@ info.height = textureDesc.height; info.depth = textureDesc.depth; info.mipmapLevelCount = textureDesc.mipNum; - // info.sampleCount = textureDesc->mSampleCount; - // info.arrayLength = textureDesc->mArraySize; + info.sampleCount = textureDesc.sampleNum; + info.arrayLength = textureDesc.layerNum; + + m_Handle = [m_Device newTextureWithDescriptor:info]; + m_Desc = textureDesc; //m_Handle = [m_Device newTextureWithDescriptor:textureDesc]; return Result::SUCCESS; From 6c050b0a71baca5f652946acc72b7f3919abd5ee Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Tue, 3 Sep 2024 21:36:02 -0700 Subject: [PATCH 10/27] feat: add interface Signed-off-by: Michael Pollind --- Source/Metal/ConversionMTL.h | 16 ++++++++ Source/Metal/DescriptorMTL.h | 0 Source/Metal/DeviceMTL.hpp | 64 ++++++++++++++++--------------- Source/Metal/DeviceMTL.mm | 17 ++++++++ Source/Metal/PipelineLayoutMTL.h | 43 +++++---------------- Source/Metal/PipelineLayoutMTL.mm | 26 +++++++++++++ Source/Metal/PipelineMTL.mm | 28 +++++++------- Source/Metal/QueryMTL.h | 0 Source/Metal/QueryMTL.mm | 0 Source/Metal/SwapChainMTL.h | 23 +++++++++++ Source/Metal/SwapChainMTL.mm | 0 Source/Metal/TextureMTL.h | 4 +- Source/Metal/TextureMTL.mm | 3 +- 13 files changed, 146 insertions(+), 78 deletions(-) create mode 100644 Source/Metal/DescriptorMTL.h create mode 100644 Source/Metal/QueryMTL.h create mode 100644 Source/Metal/QueryMTL.mm create mode 100644 Source/Metal/SwapChainMTL.h create mode 100644 Source/Metal/SwapChainMTL.mm diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index d1aa5b5e..a0c53d3b 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -55,6 +55,22 @@ constexpr MTLColorWriteMask GetColorComponent(ColorWriteBits colorWriteMask) { return MTLColorWriteMask(colorWriteMask) & MTLColorWriteMaskAll; } +constexpr std::array DESCRIPTOR_TYPES = { + MTLDataTypeSampler, // SAMPLER + MTLDataTypeNone, // CONSTANT_BUFFER + MTLDataTypeTexture, // TEXTURE + MTLDataTypeNone, // STORAGE_TEXTURE + MTLDataTypeStruct, // BUFFER + MTLDataTypeStruct, // STORAGE_BUFFER + MTLDataTypeArray, // STRUCTURED_BUFFER + MTLDataTypeStruct, // STORAGE_STRUCTURED_BUFFER + MTLDataTypePrimitiveAccelerationStructure // ACCELERATION_STRUCTURE +}; + +constexpr MTLDataType GetDescriptorType(DescriptorType type) { + return DESCRIPTOR_TYPES[(size_t)type]; +} + constexpr std::array COMPARE_OP = { MTLCompareFunctionNever, // NONE MTLCompareFunctionAlways, // ALWAYS diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h new file mode 100644 index 00000000..e69de29b diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp index c05ec9a4..011c3bc7 100644 --- a/Source/Metal/DeviceMTL.hpp +++ b/Source/Metal/DeviceMTL.hpp @@ -17,7 +17,8 @@ static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { } static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { - return ((const DeviceDesc&)device).GetFormatSupport(format); + return (FormatSupportBits)0; + //return ((const DeviceDesc&)device).GetFormatSupport(format); } static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { @@ -45,18 +46,16 @@ static Result NRI_CALL CreateCommandAllocator(const CommandQueue& commandQueue, } static Result NRI_CALL CreateDescriptorPool(Device& device, const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) { - //return ((DeviceMTL&)device).CreateImplementation(descriptorPool, descriptorPoolDesc); + //return ((DeviceMTL&)device).CreateImplementation(descriptorPool, descriptorPoolDesc); return Result::SUCCESS; } static Result NRI_CALL CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffer) { - //return ((DeviceMTL&)device).CreateImplementation(buffer, bufferDesc); - return Result::SUCCESS; + return ((DeviceMTL&)device).CreateImplementation(buffer, bufferDesc); } static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& texture) { - //return ((DeviceMTL&)device).CreateImplementation(texture, textureDesc); - return Result::SUCCESS; + return ((DeviceMTL&)device).CreateImplementation(texture, textureDesc); } static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { @@ -65,7 +64,12 @@ static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, De return Result::SUCCESS; } + static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) { + //DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice(); + //return device.CreateImplementation(textureView, textureViewDesc); + + //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); //return device.CreateImplementation(textureView, textureViewDesc); return Result::SUCCESS; @@ -150,7 +154,7 @@ static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) { } static void NRI_CALL DestroyFence(Fence& fence) { - //Destroy((FenceD3D12*)&fence); + Destroy((FenceMTL*)&fence); } static Result NRI_CALL AllocateMemory(Device& device, const AllocateMemoryDesc& allocateMemoryDesc, Memory*& memory) { @@ -191,12 +195,12 @@ static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) { static void* NRI_CALL GetDeviceNativeObject(const Device& device) { if (!(&device)) return nullptr; - return (MTLDevice*)((DeviceMTL&)device); + return (DeviceMTL*)&((DeviceMTL&)device); //return ((DeviceMetal&)device).GetNativeObject(); } -Result DeviceMetal::FillFunctionTable(CoreInterface& table) const { +Result DeviceMTL::FillFunctionTable(CoreInterface& table) const { table = {}; Core_Device_PartiallyFillFunctionTableMTL(table); Core_Buffer_PartiallyFillFunctionTableMTL(table); @@ -214,44 +218,44 @@ Result DeviceMetal::FillFunctionTable(CoreInterface& table) const { #pragma endregion -Result DeviceMetal::FillFunctionTable(HelperInterface& table) const { +Result DeviceMTL::FillFunctionTable(HelperInterface& table) const { table = {}; - return ResVult::UNSUPPORTED; + return Result::UNSUPPORTED; } -Result DeviceMetal::FillFunctionTable(LowLatencyInterface& table) const { +Result DeviceMTL::FillFunctionTable(LowLatencyInterface& table) const { table = {}; - return ResVult::UNSUPPORTED; + return Result::UNSUPPORTED; } -Result DeviceMetal::FillFunctionTable(MeshShaderInterface& table) const { - table = {}; - return ResVult::UNSUPPORTED; -} +//Result DeviceMTL::FillFunctionTable(MeshShaderInterface& table) const { +// table = {}; +// return Result::UNSUPPORTED; +//} -Result DeviceMetal::FillFunctionTable(RayTracingInterface& table) const { +Result DeviceMTL::FillFunctionTable(RayTracingInterface& table) const { table = {}; - return ResVult::UNSUPPORTED; + return Result::UNSUPPORTED; } -Result DeviceMetal::FillFunctionTable(StreamerInterface& table) const { +Result DeviceMTL::FillFunctionTable(StreamerInterface& table) const { table = {}; - return ResVult::UNSUPPORTED; + return Result::UNSUPPORTED; } -Result DeviceMetal::FillFunctionTable(SwapChainInterface& table) const { +Result DeviceMTL::FillFunctionTable(SwapChainInterface& table) const { table = {}; - return ResVult::UNSUPPORTED; + return Result::UNSUPPORTED; } -Result DeviceMetal::FillFunctionTable(ResourceAllocatorInterface& table) const { +Result DeviceMTL::FillFunctionTable(ResourceAllocatorInterface& table) const { table = {}; - return ResVult::UNSUPPORTED; + return Result::UNSUPPORTED; } Define_Core_Device_PartiallyFillFunctionTable(MTL); -Define_Helper_Device_PartiallyFillFunctionTable(MTL); -Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); -Define_Streamer_Device_PartiallyFillFunctionTable(MTL); -Define_SwapChain_Device_PartiallyFillFunctionTable(MTL); -Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL); +//Define_Helper_Device_PartiallyFillFunctionTable(MTL); +//Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); +//Define_Streamer_Device_PartiallyFillFunctionTable(MTL); +//Define_SwapChain_Device_PartiallyFillFunctionTable(MTL); +//Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL); diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index 2bdcdbae..b2960f5b 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -4,6 +4,20 @@ #include "DeviceMTL.h" +//#include "AccelerationStructureVK.h" +#include "BufferMTL.h" +//#include "CommandAllocatorVK.h" +//#include "CommandBufferMTL.h" +#include "CommandQueueMTL.h" +//#include "DescriptorPoolMTL.h" +#include "DescriptorSetMTL.h" +#include "TextureMTL.h" +#include "FenceMTL.h" +#include "MemoryMTL.h" +#include "PipelineLayoutMTL.h" +#include "PipelineMTL.h" + + using namespace nri; static bool FindMTLGpuFamily(id device, @@ -130,6 +144,7 @@ static bool FindMTLGpuFamily(id device, m_Desc.adapterDesc.vendor = nri::Vendor::APPLE; } + const uint64_t regID = [m_Device registryID]; if (regID) { @@ -166,3 +181,5 @@ static bool FindMTLGpuFamily(id device, } +#include "DeviceMTL.hpp" + diff --git a/Source/Metal/PipelineLayoutMTL.h b/Source/Metal/PipelineLayoutMTL.h index e3c2a2b1..3a575b86 100644 --- a/Source/Metal/PipelineLayoutMTL.h +++ b/Source/Metal/PipelineLayoutMTL.h @@ -11,45 +11,22 @@ struct PipelineLayoutMTL { : m_Device(device) { } -// inline operator VkPipelineLayout() const { -// return m_Handle; -// } -// -// inline DeviceVK& GetDevice() const { -// return m_Device; -// } -// -// inline const RuntimeBindingInfo& GetRuntimeBindingInfo() const { -// return m_RuntimeBindingInfo; -// } -// -// inline VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t index) const { -// return m_DescriptorSetLayouts[index]; -// } -// -// inline VkPipelineBindPoint GetPipelineBindPoint() const { -// return m_PipelineBindPoint; -// } -// -// inline uint32_t GetDescriptorSetSpace(uint32_t setIndexInPipelineLayout) const { - -// } -// ~PipelineLayoutMTL(); // -// Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); -// -// //================================================================================================================ -// // NRI -// //================================================================================================================ -// -// void SetDebugName(const char* name); + inline Result Create(const PipelineLayoutDesc& pipelineLayoutDesc) { + return CreateDesc(pipelineLayoutDesc); + } + + struct PipelineDescriptorSet { + NSMutableArray* m_ArgumentDescriptors; + }; private: + Result CreateDesc(const PipelineLayoutDesc& pipelineLayoutDesc); DeviceMTL& m_Device; - //Vector m_DescriptorSetLayouts; - + + std::vector m_DescriptorSets; }; } diff --git a/Source/Metal/PipelineLayoutMTL.mm b/Source/Metal/PipelineLayoutMTL.mm index 7760b5f1..7a507671 100644 --- a/Source/Metal/PipelineLayoutMTL.mm +++ b/Source/Metal/PipelineLayoutMTL.mm @@ -8,4 +8,30 @@ } +Result PipelineLayoutMTL::CreateDesc(const PipelineLayoutDesc& pipelineLayoutDesc) { + m_DescriptorSets.resize(pipelineLayoutDesc.descriptorSetNum); + for (uint32_t i = 0; i < pipelineLayoutDesc.descriptorSetNum; i++) { + const DescriptorSetDesc& descriptorSetDesc = pipelineLayoutDesc.descriptorSets[i]; + + NSMutableArray* argumentDescriptors = [[NSMutableArray alloc] init]; + MTLArgumentDescriptor* argDescriptor = [MTLArgumentDescriptor argumentDescriptor]; + + for(size_t r = 0; r < descriptorSetDesc.rangeNum; r++) { + + } + + + //argDescriptor.access = memberDescriptor.mAccessType; + //argDescriptor.arrayLength = memberDescriptor.mArrayLength; + //argDescriptor.constantBlockAlignment = memberDescriptor.mAlignment; + //argDescriptor.dataType = memberDescriptor.mDataType; + //argDescriptor.index = memberDescriptor.mArgumentIndex; + //argDescriptor.textureType = memberDescriptor.mTextureType; + + [argumentDescriptors addObject:argDescriptor]; + m_DescriptorSets[i].m_ArgumentDescriptors = argumentDescriptors; + + } + return Result::SUCCESS; +} diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index 3603b528..39b574cc 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -12,9 +12,14 @@ MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init]; for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) { } + // Depth-stencil + const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth; + const StencilAttachmentDesc& sa = graphicsPipelineDesc.outputMerger.stencil; + const PipelineLayout *pl = graphicsPipelineDesc.pipelineLayout; const VertexInputDesc *vi = graphicsPipelineDesc.vertexInput; if (vi) { + // VkVertexInputBindingDescription* streams const_cast(vertexInputState.pVertexBindingDescriptions); //for (uint32_t i = 0; i < vi->streamNum; i++) { // const VertexStreamDesc &stream = vi->streams[i]; @@ -30,28 +35,25 @@ // TODO: multisampling } - // Blending - const OutputMergerDesc& om = graphicsPipelineDesc.outputMerger; // assign render target pixel format for all attachments + const OutputMergerDesc& om = graphicsPipelineDesc.outputMerger; for (uint32_t i = 0; i < om.colorNum; i++) { - // MTLRenderPipelineColorAttachmentDescriptor& colorAtachment = &renderPipelineDesc.colorAttachments[i]; const ColorAttachmentDesc& attachmentDesc = om.color[i]; - - //colorAtachment.pixelFormat = GetFormatMTL(attachmentDesc.format, false); + renderPipelineDesc.colorAttachments[i].pixelFormat = GetFormatMTL(attachmentDesc.format, false); - //colorAtachment.blendingEnabled = attachmentDesc.blendEnabled; - //colorAtachment.rgbBlendOperation = GetBlendOp(attachmentDesc.colorBlend.func); - //colorAtachment.alphaBlendOperation = GetBlendOp(attachmentDesc.alphaBlend.func); + renderPipelineDesc.colorAttachments[i].blendingEnabled = attachmentDesc.blendEnabled; + renderPipelineDesc.colorAttachments[i].rgbBlendOperation = GetBlendOp(attachmentDesc.colorBlend.func); + renderPipelineDesc.colorAttachments[i].alphaBlendOperation = GetBlendOp(attachmentDesc.alphaBlend.func); - //colorAtachment.sourceRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.srcFactor); - //colorAtachment.destinationRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.dstFactor); - //colorAtachment.sourceAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.srcFactor); - //colorAtachment.destinationAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.dstFactor); + renderPipelineDesc.colorAttachments[i].sourceRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.srcFactor); + renderPipelineDesc.colorAttachments[i].destinationRGBBlendFactor = GetBlendFactor(attachmentDesc.colorBlend.dstFactor); + renderPipelineDesc.colorAttachments[i].sourceAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.srcFactor); + renderPipelineDesc.colorAttachments[i].destinationAlphaBlendFactor = GetBlendFactor(attachmentDesc.alphaBlend.dstFactor); - //colorAtachment.writeMask = GetColorComponent(pDesc->mColorWriteMasks[blendDescIndex]); + renderPipelineDesc.colorAttachments[i].writeMask = GetColorComponent(attachmentDesc.colorWriteMask); } diff --git a/Source/Metal/QueryMTL.h b/Source/Metal/QueryMTL.h new file mode 100644 index 00000000..e69de29b diff --git a/Source/Metal/QueryMTL.mm b/Source/Metal/QueryMTL.mm new file mode 100644 index 00000000..e69de29b diff --git a/Source/Metal/SwapChainMTL.h b/Source/Metal/SwapChainMTL.h new file mode 100644 index 00000000..b2de4e68 --- /dev/null +++ b/Source/Metal/SwapChainMTL.h @@ -0,0 +1,23 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +namespace nri { + +// Let's keep things simple and hide it under the hood +constexpr uint32_t MAX_NUMBER_OF_FRAMES_IN_FLIGHT = 8; + +struct SwapChainMTL: public DisplayDescHelper { + SwapChainVK(DeviceMTL& device); + ~SwapChainVK(); + + Result Create(const SwapChainDesc& swapChainDesc); + +private: + DeviceMTL& m_Device; + uint64_t m_PresentId = 0; + uint32_t m_TextureIndex = 0; + uint8_t m_FrameIndex = 0; // in flight, not global +}; + +}; diff --git a/Source/Metal/SwapChainMTL.mm b/Source/Metal/SwapChainMTL.mm new file mode 100644 index 00000000..e69de29b diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index f4f5841f..a6a4f1ea 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -26,10 +26,12 @@ struct TextureMTL { return m_Desc; } - Result CreateFromTextureDesc(const TextureDesc& textureDesc); + Result Create(const TextureDesc& textureDesc); //Result Create(const TextureMTLDesc& textureDesc); private: + // Result CreateFromTextureDesc(const TextureDesc& textureDesc); + DeviceMTL& m_Device; TextureDesc m_Desc = {}; id m_Handle; diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm index 3d4cf67e..b66fcb08 100644 --- a/Source/Metal/TextureMTL.mm +++ b/Source/Metal/TextureMTL.mm @@ -14,7 +14,8 @@ // return Result::SUCCESS; //} -Result TextureMTL::CreateFromTextureDesc(const TextureDesc& textureDesc) { + +Result TextureMTL::Create(const TextureDesc& textureDesc) { MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init]; info.textureType = ::GetImageTypeMTL(textureDesc.type); info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); From b13bedcb068deb5dbbab4cd213a251fb864d80f7 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 4 Sep 2024 21:46:56 -0700 Subject: [PATCH 11/27] feat: add descriptor Signed-off-by: Michael Pollind --- Source/Metal/DescriptorMTL.h | 26 ++++++++++++++++++++++++++ Source/Metal/DescriptorMTL.mm | 0 Source/Metal/DeviceMTL.mm | 1 - 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Source/Metal/DescriptorMTL.mm diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index e69de29b..0cce5fea 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -0,0 +1,26 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +namespace nri { + +struct DeviceMTL; + + +struct DescriptorMTL { + + inline DescriptorMTL (DeviceMTL& device) + : m_Device(device) { + } + + inline Result Create(const Texture1DViewDesc& textureViewDesc) {} + inline Result Create(const Texture2DViewDesc& textureViewDesc){} + inline Result Create(const Texture3DViewDesc& textureViewDesc){} + inline Result Create(const SamplerDesc& samplerDesc){} + +private: + DeviceMTL& m_Device; + +}; + +} // namespace nri diff --git a/Source/Metal/DescriptorMTL.mm b/Source/Metal/DescriptorMTL.mm new file mode 100644 index 00000000..e69de29b diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index b2960f5b..81174bb2 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -10,7 +10,6 @@ //#include "CommandBufferMTL.h" #include "CommandQueueMTL.h" //#include "DescriptorPoolMTL.h" -#include "DescriptorSetMTL.h" #include "TextureMTL.h" #include "FenceMTL.h" #include "MemoryMTL.h" From d4621d41e650117951cab8a4d1f1ed389e67a34c Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 4 Sep 2024 21:59:30 -0700 Subject: [PATCH 12/27] fix compiling errors Signed-off-by: Michael Pollind --- Include/Extensions/NRIWrapperMTL.h | 34 +++++++----------------------- Include/NRIDescs.h | 13 +++--------- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h index 96a8fc0c..3425a15e 100644 --- a/Include/Extensions/NRIWrapperMTL.h +++ b/Include/Extensions/NRIWrapperMTL.h @@ -4,62 +4,44 @@ #include "NRIDeviceCreation.h" -NRI_NAMESPACE_BEGIN - +NriNamespaceBegin typedef void* MTLBufferHandle; typedef void* MTLTextureHandle; typedef void* MTLDeviceHandle; -//NRI_ENUM -//( -// MTLGPUFamily, uint8_t, - - // Apple1, -// Apple2, -// Apple3, -// Apple4, -// Apple5, -// Apple6, -// Apple7, -// Mac1, -// Mac2, - -// MAX_APPLE_FAMILY_NUM -//); - -NRI_STRUCT(DeviceCreationMTLDesc) +NriStruct(DeviceCreationMTLDesc) { bool enableNRIValidation; MTLDeviceHandle MtlDevice; }; -NRI_STRUCT(CommandBufferMTLDesc) +NriStruct(CommandBufferMTLDesc) { }; -NRI_STRUCT(BufferMTLDesc) +NriStruct(BufferMTLDesc) { MTLBufferHandle buffer; void* mappedMemory; //MTLResourceOptions options; }; -NRI_STRUCT(TextureMTLDesc) +NriStruct(TextureMTLDesc) { //MTLTextureHandle mtlTexture; //MTLTextureDescriptor* descriptor; }; -NRI_STRUCT(MemoryMTLDesc) +NriStruct(MemoryMTLDesc) { MTLBufferHandle buffer; void* mappedMemory; //MTLResourceOptions options; }; -NRI_API NRI_NAME(Result) NRI_CALL nriCreateDeviceFromMtlDevice(const NRI_NAME_REF(DeviceCreationMTLDesc) deviceDesc, NRI_NAME_REF(Device*) device); +NRI_API Nri(Result) NRI_CALL nriCreateDeviceFromMtlDevice(const NriRef(DeviceCreationMTLDesc) deviceDesc, NriRef(Device*) device); -NRI_NAMESPACE_END +NriNamespaceEnd diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index 1c1a044b..dfb9d641 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -1288,26 +1288,19 @@ NriEnum(GraphicsAPI, uint8_t, D3D11, D3D12, VK, - MTL, - - MAX_NUM + MTL ); // defined in apple framework #undef INTEL #undef AMD -NRI_ENUM -( - - Vendor, uint8_t, +NriEnum(Vendor, uint8_t, UNKNOWN, NVIDIA, AMD, INTEL, - APPLE, - - MAX_NUM + APPLE ); NriStruct(AdapterDesc) { From 5d98970cf0ed4ad7c5e4eae23fd3283cd456c0c9 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Thu, 5 Sep 2024 22:18:10 -0700 Subject: [PATCH 13/27] feat: start on command buffer logic Signed-off-by: Michael Pollind --- Source/Metal/CommandBufferMTL.h | 100 +++++++++++++++++ Source/Metal/CommandBufferMTL.hpp | 179 ++++++++++++++++++++++++++++++ Source/Metal/CommandBufferMTL.mm | 119 ++++++++++++++++++++ Source/Metal/ConversionMTL.h | 22 ++-- Source/Metal/DeviceMTL.h | 5 +- Source/Metal/DeviceMTL.hpp | 6 +- Source/Metal/DeviceMTL.mm | 12 ++ Source/Metal/PipelineLayoutMTL.h | 8 +- Source/Metal/PipelineLayoutMTL.mm | 2 +- Source/Metal/PipelineMTL.h | 21 ++++ Source/Metal/PipelineMTL.mm | 34 +++++- 11 files changed, 484 insertions(+), 24 deletions(-) create mode 100644 Source/Metal/CommandBufferMTL.h create mode 100644 Source/Metal/CommandBufferMTL.hpp create mode 100644 Source/Metal/CommandBufferMTL.mm diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h new file mode 100644 index 00000000..c6fd1707 --- /dev/null +++ b/Source/Metal/CommandBufferMTL.h @@ -0,0 +1,100 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +#import + +namespace nri { + +struct DeviceMTL; +struct PipelineMTL; +struct BufferMTL; +struct PipelineLayoutMTL; +struct TextureMTL; +struct DescriptorMTL; + +struct CommandBufferMTL { + inline CommandBufferMTL(DeviceMTL& device) + : m_Device(device) { + } + + // inline operator VkCommandBuffer() const { + // return m_Handle; + // } + + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + void SetDebugName(const char* name); + Result Begin(const DescriptorPool* descriptorPool); + Result End(); + void SetPipeline(const Pipeline& pipeline); + void SetPipelineLayout(const PipelineLayout& pipelineLayout); + void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); + void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); + void SetDescriptorPool(const DescriptorPool& descriptorPool); + void Barrier(const BarrierGroupDesc& barrierGroupDesc); + void BeginRendering(const AttachmentsDesc& attachmentsDesc); + void EndRendering(); + void SetViewports(const Viewport* viewports, uint32_t viewportNum); + void SetScissors(const Rect* rects, uint32_t rectNum); + void SetDepthBounds(float boundsMin, float boundsMax); + void SetStencilReference(uint8_t frontRef, uint8_t backRef); + void SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum); + void SetBlendConstants(const Color32f& color); + void SetShadingRate(const ShadingRateDesc& shadingRateDesc); + void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); + void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); + void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); + void Draw(const DrawDesc& drawDesc); + void DrawIndexed(const DrawIndexedDesc& drawIndexedDesc); + void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset); + void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset); + void Dispatch(const DispatchDesc& dispatchDesc); + void DispatchIndirect(const Buffer& buffer, uint64_t offset); + void BeginQuery(const QueryPool& queryPool, uint32_t offset); + void EndQuery(const QueryPool& queryPool, uint32_t offset); + void BeginAnnotation(const char* name); + void EndAnnotation(); + void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); + void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); + void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); + void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); + void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); + void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); + void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); + void ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num); + void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); + void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset); + void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); + void DispatchRaysIndirect(const Buffer& buffer, uint64_t offset); + void DrawMeshTasks(const DrawMeshTasksDesc& drawMeshTasksDesc); + void DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + + ~CommandBufferMTL(); + + struct CmdIndexBuffer { + size_t m_Offset; + MTLIndexType m_Type; + struct BufferMTL* m_Buffer; + }; + + +private: + DeviceMTL& m_Device; + struct PipelineMTL* m_CurrentPipeline = nullptr; + + struct CmdIndexBuffer m_CurrentIndexCmd; + id m_Handle; + + id m_encoder; + + //id renderEncoder; +}; +}; + diff --git a/Source/Metal/CommandBufferMTL.hpp b/Source/Metal/CommandBufferMTL.hpp new file mode 100644 index 00000000..7aa867b4 --- /dev/null +++ b/Source/Metal/CommandBufferMTL.hpp @@ -0,0 +1,179 @@ + +#pragma region[ Core ] + +static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { + ((CommandBufferMTL&)commandBuffer).SetDebugName(name); +} + +static Result NRI_CALL BeginCommandBuffer(CommandBuffer& commandBuffer, const DescriptorPool* descriptorPool) { + return ((CommandBufferMTL&)commandBuffer).Begin(descriptorPool); +} + +static Result NRI_CALL EndCommandBuffer(CommandBuffer& commandBuffer) { + return ((CommandBufferMTL&)commandBuffer).End(); +} + +static void NRI_CALL CmdSetPipelineLayout(CommandBuffer& commandBuffer, const PipelineLayout& pipelineLayout) { + ((CommandBufferMTL&)commandBuffer).SetPipelineLayout(pipelineLayout); +} + +static void NRI_CALL CmdSetPipeline(CommandBuffer& commandBuffer, const Pipeline& pipeline) { + ((CommandBufferMTL&)commandBuffer).SetPipeline(pipeline); +} + +static void NRI_CALL CmdBarrier(CommandBuffer& commandBuffer, const BarrierGroupDesc& barrierGroupDesc) { + ((CommandBufferMTL&)commandBuffer).Barrier(barrierGroupDesc); +} + +static void NRI_CALL CmdSetDescriptorPool(CommandBuffer& commandBuffer, const DescriptorPool& descriptorPool) { + ((CommandBufferMTL&)commandBuffer).SetDescriptorPool(descriptorPool); +} + +static void NRI_CALL CmdSetDescriptorSet(CommandBuffer& commandBuffer, uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { + ((CommandBufferMTL&)commandBuffer).SetDescriptorSet(setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); +} + +static void NRI_CALL CmdSetConstants(CommandBuffer& commandBuffer, uint32_t pushConstantIndex, const void* data, uint32_t size) { + ((CommandBufferMTL&)commandBuffer).SetConstants(pushConstantIndex, data, size); +} + +static void NRI_CALL CmdBeginRendering(CommandBuffer& commandBuffer, const AttachmentsDesc& attachmentsDesc) { + ((CommandBufferMTL&)commandBuffer).BeginRendering(attachmentsDesc); +} + +static void NRI_CALL CmdEndRendering(CommandBuffer& commandBuffer) { + ((CommandBufferMTL&)commandBuffer).EndRendering(); +} + +static void NRI_CALL CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) { + ((CommandBufferMTL&)commandBuffer).SetViewports(viewports, viewportNum); +} + +static void NRI_CALL CmdSetScissors(CommandBuffer& commandBuffer, const nri::Rect* rects, uint32_t rectNum) { + ((CommandBufferMTL&)commandBuffer).SetScissors(rects, rectNum); +} + +static void NRI_CALL CmdSetDepthBounds(CommandBuffer& commandBuffer, float boundsMin, float boundsMax) { + ((CommandBufferMTL&)commandBuffer).SetDepthBounds(boundsMin, boundsMax); +} + +static void NRI_CALL CmdSetStencilReference(CommandBuffer& commandBuffer, uint8_t frontRef, uint8_t backRef) { + ((CommandBufferMTL&)commandBuffer).SetStencilReference(frontRef, backRef); +} + +static void NRI_CALL CmdSetSamplePositions(CommandBuffer& commandBuffer, const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) { + ((CommandBufferMTL&)commandBuffer).SetSamplePositions(positions, positionNum, sampleNum); +} + +static void NRI_CALL CmdSetBlendConstants(CommandBuffer& commandBuffer, const Color32f& color) { + ((CommandBufferMTL&)commandBuffer).SetBlendConstants(color); +} + +static void NRI_CALL CmdSetShadingRate(CommandBuffer& commandBuffer, const ShadingRateDesc& shadingRateDesc) { + ((CommandBufferMTL&)commandBuffer).SetShadingRate(shadingRateDesc); +} + +static void NRI_CALL CmdSetDepthBias(CommandBuffer& commandBuffer, const DepthBiasDesc& depthBiasDesc) { + //((CommandBufferMTL&)commandBuffer).SetDepthBias(depthBiasDesc); +} + +static void NRI_CALL CmdClearAttachments(CommandBuffer& commandBuffer, const ClearDesc* clearDescs, uint32_t clearDescNum, const nri::Rect* rects, uint32_t rectNum) { + ((CommandBufferMTL&)commandBuffer).ClearAttachments(clearDescs, clearDescNum, rects, rectNum); +} + +static void NRI_CALL CmdSetIndexBuffer(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, IndexType indexType) { + ((CommandBufferMTL&)commandBuffer).SetIndexBuffer(buffer, offset, indexType); +} + +static void NRI_CALL CmdSetVertexBuffers(CommandBuffer& commandBuffer, uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { + ((CommandBufferMTL&)commandBuffer).SetVertexBuffers(baseSlot, bufferNum, buffers, offsets); +} + +static void NRI_CALL CmdDraw(CommandBuffer& commandBuffer, const DrawDesc& drawDesc) { + ((CommandBufferMTL&)commandBuffer).Draw(drawDesc); +} + +static void NRI_CALL CmdDrawIndexed(CommandBuffer& commandBuffer, const DrawIndexedDesc& drawIndexedDesc) { + ((CommandBufferMTL&)commandBuffer).DrawIndexed(drawIndexedDesc); +} + +static void NRI_CALL CmdDrawIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + ((CommandBufferMTL&)commandBuffer).DrawIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); +} + +static void NRI_CALL CmdDrawIndexedIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + ((CommandBufferMTL&)commandBuffer).DrawIndexedIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); +} + +static void NRI_CALL CmdDispatch(CommandBuffer& commandBuffer, const DispatchDesc& dispatchDesc) { + ((CommandBufferMTL&)commandBuffer).Dispatch(dispatchDesc); +} + +static void NRI_CALL CmdDispatchIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset) { + ((CommandBufferMTL&)commandBuffer).DispatchIndirect(buffer, offset); +} + +static void NRI_CALL CmdBeginQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { + ((CommandBufferMTL&)commandBuffer).BeginQuery(queryPool, offset); +} + +static void NRI_CALL CmdEndQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { + ((CommandBufferMTL&)commandBuffer).EndQuery(queryPool, offset); +} + +static void NRI_CALL CmdBeginAnnotation(CommandBuffer& commandBuffer, const char* name) { + ((CommandBufferMTL&)commandBuffer).BeginAnnotation(name); +} + +static void NRI_CALL CmdEndAnnotation(CommandBuffer& commandBuffer) { + ((CommandBufferMTL&)commandBuffer).EndAnnotation(); +} + +static void NRI_CALL CmdClearStorageBuffer(CommandBuffer& commandBuffer, const ClearStorageBufferDesc& clearDesc) { + ((CommandBufferMTL&)commandBuffer).ClearStorageBuffer(clearDesc); +} + +static void NRI_CALL CmdClearStorageTexture(CommandBuffer& commandBuffer, const ClearStorageTextureDesc& clearDesc) { + ((CommandBufferMTL&)commandBuffer).ClearStorageTexture(clearDesc); +} + +static void NRI_CALL CmdCopyBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) { + ((CommandBufferMTL&)commandBuffer).CopyBuffer(dstBuffer, dstOffset, srcBuffer, srcOffset, size); +} + +static void NRI_CALL CmdCopyTexture( + CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) { + ((CommandBufferMTL&)commandBuffer).CopyTexture(dstTexture, dstRegionDesc, srcTexture, srcRegionDesc); +} + +static void NRI_CALL CmdUploadBufferToTexture(CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { + ((CommandBufferMTL&)commandBuffer).UploadBufferToTexture(dstTexture, dstRegionDesc, srcBuffer, srcDataLayoutDesc); +} + +static void NRI_CALL CmdReadbackTextureToBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, const TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { + //((CommandBufferMTL&)commandBuffer).ReadbackTextureToBuffer(dstBuffer, dstDataLayoutDesc, srcTexture, srcRegionDesc); +} + +static void NRI_CALL CmdCopyQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) { + ((CommandBufferMTL&)commandBuffer).CopyQueries(queryPool, offset, num, dstBuffer, dstOffset); +} + +static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num) { + ((CommandBufferMTL&)commandBuffer).ResetQueries(queryPool, offset, num); +} + +static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandBuffer) { + if (!(&commandBuffer)) + return nullptr; + + return nullptr; + // return (CommandBufferMTL)((CommandBufferMTL&)commandBuffer); +} + +#pragma endregion + + + + +Define_Core_CommandBuffer_PartiallyFillFunctionTable(VK); + diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm new file mode 100644 index 00000000..92ee9911 --- /dev/null +++ b/Source/Metal/CommandBufferMTL.mm @@ -0,0 +1,119 @@ +#include "SharedMTL.h" + +#include "CommandBufferMTL.h" + +//#include "AccelerationStructureVK.h" +#include "BufferMTL.h" +#include "CommandBufferMTL.h" +#include "CommandQueueMTL.h" +//#include "DescriptorSetMTL.h" +#include "DescriptorMTL.h" +#include "PipelineLayoutMTL.h" +#include "PipelineMTL.h" +//#include "QueryPoolMTL.h" +#include "TextureMTL.h" + +#include + +using namespace nri; + +CommandBufferMTL::~CommandBufferMTL() { + +} + +void CommandBufferMTL::SetDebugName(const char* name) { + [m_Handle setLabel:[NSString stringWithUTF8String:name]]; +} + +Result CommandBufferMTL::Begin(const DescriptorPool* descriptorPool) { + +} +Result CommandBufferMTL::End() { + +} +void CommandBufferMTL::SetPipeline(const Pipeline& pipeline) { + if (m_CurrentPipeline == (PipelineMTL*)&pipeline) + return; + PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline; + m_CurrentPipeline = &pipelineImpl; + + +} +void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {} +void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) {} +void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) {} +void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) {} +void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {} +void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { + m_encoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; + +} +void CommandBufferMTL::EndRendering() {} +void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {} +void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) {} +void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) {} +void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) {} +void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) {} +void CommandBufferMTL::SetBlendConstants(const Color32f& color) {} +void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) {} +void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) {} +void CommandBufferMTL::SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType) { + m_CurrentIndexCmd.m_Buffer = &(BufferMTL&)buffer; + switch(indexType) { + case IndexType::UINT16: + m_CurrentIndexCmd.m_Type = MTLIndexType::MTLIndexTypeUInt16; + break; + default: + case IndexType::UINT32: + m_CurrentIndexCmd.m_Type = MTLIndexType::MTLIndexTypeUInt32; + break; + } + m_CurrentIndexCmd.m_Offset = offset; +} +void CommandBufferMTL::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { +} +void CommandBufferMTL::Draw(const DrawDesc& drawDesc) { + + [m_encoder drawPrimitives: m_CurrentPipeline->m_graphics.m_primitiveType + vertexStart:drawDesc.baseVertex + vertexCount:drawDesc.vertexNum + instanceCount:drawDesc.instanceNum + baseInstance:0]; +} +void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) { + id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); + [m_encoder drawIndexedPrimitives: m_CurrentPipeline->m_graphics.m_primitiveType + indexCount:drawIndexedDesc.indexNum + indexType: m_CurrentIndexCmd.m_Type + indexBuffer: indexBuffer + indexBufferOffset: m_CurrentIndexCmd.m_Offset]; +} +void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) {} +void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) {} +void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) {} +void CommandBufferMTL::DispatchIndirect(const Buffer& buffer, uint64_t offset) {} +void CommandBufferMTL::BeginQuery(const QueryPool& queryPool, uint32_t offset) {} +void CommandBufferMTL::EndQuery(const QueryPool& queryPool, uint32_t offset) {} +void CommandBufferMTL::BeginAnnotation(const char* name) {} +void CommandBufferMTL::EndAnnotation() {} +void CommandBufferMTL::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) {} +void CommandBufferMTL::ClearStorageTexture(const ClearStorageTextureDesc& clearDesc) {} +void CommandBufferMTL::CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) {} +void CommandBufferMTL::CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) {} +void CommandBufferMTL::UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) {} +void CommandBufferMTL::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) {} +void CommandBufferMTL::CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) {} +void CommandBufferMTL::ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num) {} +void CommandBufferMTL::BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) {} +void CommandBufferMTL::BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) {} +void CommandBufferMTL::UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) {} +void CommandBufferMTL::UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) {} +void CommandBufferMTL::CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode) {} +void CommandBufferMTL::WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset) {} +void CommandBufferMTL::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) {} +void CommandBufferMTL::DispatchRaysIndirect(const Buffer& buffer, uint64_t offset) {} +void CommandBufferMTL::DrawMeshTasks(const DrawMeshTasksDesc& drawMeshTasksDesc) {} +void CommandBufferMTL::DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) {} + +#include "CommandBufferMTL.hpp" + diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index a0c53d3b..1079bbc8 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -56,15 +56,15 @@ constexpr MTLColorWriteMask GetColorComponent(ColorWriteBits colorWriteMask) { } constexpr std::array DESCRIPTOR_TYPES = { - MTLDataTypeSampler, // SAMPLER - MTLDataTypeNone, // CONSTANT_BUFFER - MTLDataTypeTexture, // TEXTURE - MTLDataTypeNone, // STORAGE_TEXTURE - MTLDataTypeStruct, // BUFFER - MTLDataTypeStruct, // STORAGE_BUFFER - MTLDataTypeArray, // STRUCTURED_BUFFER - MTLDataTypeStruct, // STORAGE_STRUCTURED_BUFFER - MTLDataTypePrimitiveAccelerationStructure // ACCELERATION_STRUCTURE + MTLDataTypeSampler, // SAMPLER + MTLDataTypeNone, // CONSTANT_BUFFER + MTLDataTypeTexture, // TEXTURE + MTLDataTypeNone, // STORAGE_TEXTURE + MTLDataTypeStruct, // BUFFER + MTLDataTypeStruct, // STORAGE_BUFFER + MTLDataTypeArray, // STRUCTURED_BUFFER + MTLDataTypeStruct, // STORAGE_STRUCTURED_BUFFER + MTLDataTypePrimitiveAccelerationStructure // ACCELERATION_STRUCTURE }; constexpr MTLDataType GetDescriptorType(DescriptorType type) { @@ -135,7 +135,7 @@ constexpr MTLSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) { -constexpr std::array TOPOLOGIES = { +constexpr std::array TOPOLOGIES_CLASSES = { MTLPrimitiveTopologyClassPoint, // POINT_LIST MTLPrimitiveTopologyClassLine, // LINE_LIST MTLPrimitiveTopologyClassLine, // LINE_STRIP @@ -150,7 +150,7 @@ constexpr std::array TOPOL constexpr MTLPrimitiveTopologyClass GetTopologyMTL(Topology topology) { - return TOPOLOGIES[(size_t)topology]; + return TOPOLOGIES_CLASSES [(size_t)topology]; } inline MTLPixelFormat GetFormatMTL(Format format, bool demoteSrgb = false) { diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index de013697..b6027113 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -37,7 +37,10 @@ struct DeviceMTL final : public DeviceBase { return result; } - + void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); + void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); + void GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); + Result FillFunctionTable(CoreInterface& table) const; Result FillFunctionTable(HelperInterface& table) const; Result FillFunctionTable(LowLatencyInterface& table) const; diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp index 011c3bc7..16770b3d 100644 --- a/Source/Metal/DeviceMTL.hpp +++ b/Source/Metal/DeviceMTL.hpp @@ -29,10 +29,8 @@ static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& } 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); + //return ((const DeviceMTL&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc); + } static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index 81174bb2..39877b00 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -90,6 +90,18 @@ static bool FindMTLGpuFamily(id device, // } //} +void DeviceMTL::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + +} + +void DeviceMTL::GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + +} + +void DeviceMTL::GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + +} + Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { ExclusiveScope lock(m_Lock); diff --git a/Source/Metal/PipelineLayoutMTL.h b/Source/Metal/PipelineLayoutMTL.h index 3a575b86..2a4b080a 100644 --- a/Source/Metal/PipelineLayoutMTL.h +++ b/Source/Metal/PipelineLayoutMTL.h @@ -12,18 +12,14 @@ struct PipelineLayoutMTL { } ~PipelineLayoutMTL(); -// - inline Result Create(const PipelineLayoutDesc& pipelineLayoutDesc) { - return CreateDesc(pipelineLayoutDesc); - } - + Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); + struct PipelineDescriptorSet { NSMutableArray* m_ArgumentDescriptors; }; private: - Result CreateDesc(const PipelineLayoutDesc& pipelineLayoutDesc); DeviceMTL& m_Device; std::vector m_DescriptorSets; diff --git a/Source/Metal/PipelineLayoutMTL.mm b/Source/Metal/PipelineLayoutMTL.mm index 7a507671..33b4c764 100644 --- a/Source/Metal/PipelineLayoutMTL.mm +++ b/Source/Metal/PipelineLayoutMTL.mm @@ -8,7 +8,7 @@ } -Result PipelineLayoutMTL::CreateDesc(const PipelineLayoutDesc& pipelineLayoutDesc) { +Result PipelineLayoutMTL::Create(const PipelineLayoutDesc& pipelineLayoutDesc) { m_DescriptorSets.resize(pipelineLayoutDesc.descriptorSetNum); for (uint32_t i = 0; i < pipelineLayoutDesc.descriptorSetNum; i++) { const DescriptorSetDesc& descriptorSetDesc = pipelineLayoutDesc.descriptorSets[i]; diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index ccb7e15b..5c9b3224 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -8,12 +8,33 @@ struct DeviceMTL; struct PipelineLayoutMTL; struct PipelineMTL { + enum PiplineType { + None, + Graphics, + Compute, + Raytracing + }; + inline PipelineMTL(DeviceMTL& device) : m_Device(device) { } ~PipelineMTL(); Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); + Result Create(const ComputePipelineDesc& computePipelineDesc); + Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); + + enum PiplineType m_type = None; + union { + struct { + MTLPrimitiveTopologyClass m_topologyClass; + MTLPrimitiveType m_primitiveType; + } m_graphics; + struct { + + } m_compute; + }; + private: DeviceMTL& m_Device; }; diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index 39b574cc..f5ef7f45 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -8,8 +8,40 @@ } +Result PipelineMTL::Create(const ComputePipelineDesc& computePipelineDesc) { + return Result::SUCCESS; +} + +Result PipelineMTL::Create(const RayTracingPipelineDesc& rayTracingPipelineDesc) { + return Result::SUCCESS; +} + Result PipelineMTL::Create(const GraphicsPipelineDesc& graphicsPipelineDesc) { MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init]; + + m_type = PipelineMTL::Graphics; + const InputAssemblyDesc& ia = graphicsPipelineDesc.inputAssembly; + switch (ia.topology) { + case Topology::POINT_LIST: + m_graphics.m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypePoint; + break; + case Topology::LINE_LIST: + case Topology::LINE_STRIP: + case Topology::LINE_LIST_WITH_ADJACENCY: + case Topology::LINE_STRIP_WITH_ADJACENCY: + m_graphics.m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypeLine; + break; + case Topology::TRIANGLE_LIST: + case Topology::TRIANGLE_STRIP: + case Topology::TRIANGLE_LIST_WITH_ADJACENCY: + case Topology::TRIANGLE_STRIP_WITH_ADJACENCY: + m_graphics.m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypeTriangle; + break; + case Topology::PATCH_LIST: + default: + break; + } + for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) { } // Depth-stencil @@ -28,7 +60,6 @@ // } } - const InputAssemblyDesc& ia = graphicsPipelineDesc.inputAssembly; renderPipelineDesc.inputPrimitiveTopology = GetTopologyMTL(ia.topology); if (graphicsPipelineDesc.multisample) { @@ -56,5 +87,6 @@ renderPipelineDesc.colorAttachments[i].writeMask = GetColorComponent(attachmentDesc.colorWriteMask); } + return Result::SUCCESS; } From 81ea614bb0d73c3b84062b84ed4b3163e7491d7d Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Fri, 6 Sep 2024 08:02:58 -0700 Subject: [PATCH 14/27] add command buffer type --- Source/Metal/CommandBufferMTL.h | 4 ---- Source/Metal/CommandBufferMTL.mm | 27 ++++++++++++++++------- Source/Metal/PipelineMTL.h | 15 +++---------- Source/Metal/PipelineMTL.mm | 38 ++++++++++++++++---------------- 4 files changed, 41 insertions(+), 43 deletions(-) diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index c6fd1707..ce670850 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -83,15 +83,11 @@ struct CommandBufferMTL { MTLIndexType m_Type; struct BufferMTL* m_Buffer; }; - - private: DeviceMTL& m_Device; struct PipelineMTL* m_CurrentPipeline = nullptr; - struct CmdIndexBuffer m_CurrentIndexCmd; id m_Handle; - id m_encoder; //id renderEncoder; diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 92ee9911..794126ef 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -46,9 +46,10 @@ void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {} void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { m_encoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; - } -void CommandBufferMTL::EndRendering() {} +void CommandBufferMTL::EndRendering() { + [m_encoder endEncoding]; +} void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {} void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) {} void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) {} @@ -71,25 +72,35 @@ m_CurrentIndexCmd.m_Offset = offset; } void CommandBufferMTL::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { + } void CommandBufferMTL::Draw(const DrawDesc& drawDesc) { - - [m_encoder drawPrimitives: m_CurrentPipeline->m_graphics.m_primitiveType + [m_encoder drawPrimitives: m_CurrentPipeline->m_primitiveType vertexStart:drawDesc.baseVertex vertexCount:drawDesc.vertexNum instanceCount:drawDesc.instanceNum - baseInstance:0]; + baseInstance:0]; } void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) { id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); - [m_encoder drawIndexedPrimitives: m_CurrentPipeline->m_graphics.m_primitiveType + [m_encoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType indexCount:drawIndexedDesc.indexNum indexType: m_CurrentIndexCmd.m_Type indexBuffer: indexBuffer indexBufferOffset: m_CurrentIndexCmd.m_Offset]; } -void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) {} -void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) {} + +void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + NSCAssert(!countBuffer, @"count buffer not supported"); + [m_encoder + drawPrimitives: m_CurrentPipeline->m_primitiveType + indirectBuffer:((BufferMTL&)buffer).GetHandle() + indirectBufferOffset: offset]; + +} +void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + +} void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) {} void CommandBufferMTL::DispatchIndirect(const Buffer& buffer, uint64_t offset) {} void CommandBufferMTL::BeginQuery(const QueryPool& queryPool, uint32_t offset) {} diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index 5c9b3224..b274bc49 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -23,18 +23,9 @@ struct PipelineMTL { Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); Result Create(const ComputePipelineDesc& computePipelineDesc); Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); - - enum PiplineType m_type = None; - union { - struct { - MTLPrimitiveTopologyClass m_topologyClass; - MTLPrimitiveType m_primitiveType; - } m_graphics; - struct { - - } m_compute; - }; - + + MTLPrimitiveTopologyClass m_topologyClass; + MTLPrimitiveType m_primitiveType; private: DeviceMTL& m_Device; }; diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index f5ef7f45..77418aab 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -19,27 +19,27 @@ Result PipelineMTL::Create(const GraphicsPipelineDesc& graphicsPipelineDesc) { MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init]; - m_type = PipelineMTL::Graphics; + //m_type = PipelineMTL::Graphics; const InputAssemblyDesc& ia = graphicsPipelineDesc.inputAssembly; switch (ia.topology) { - case Topology::POINT_LIST: - m_graphics.m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypePoint; - break; - case Topology::LINE_LIST: - case Topology::LINE_STRIP: - case Topology::LINE_LIST_WITH_ADJACENCY: - case Topology::LINE_STRIP_WITH_ADJACENCY: - m_graphics.m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypeLine; - break; - case Topology::TRIANGLE_LIST: - case Topology::TRIANGLE_STRIP: - case Topology::TRIANGLE_LIST_WITH_ADJACENCY: - case Topology::TRIANGLE_STRIP_WITH_ADJACENCY: - m_graphics.m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypeTriangle; - break; - case Topology::PATCH_LIST: - default: - break; + case Topology::POINT_LIST: + m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypePoint; + break; + case Topology::LINE_LIST: + case Topology::LINE_STRIP: + case Topology::LINE_LIST_WITH_ADJACENCY: + case Topology::LINE_STRIP_WITH_ADJACENCY: + m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypeLine; + break; + case Topology::TRIANGLE_LIST: + case Topology::TRIANGLE_STRIP: + case Topology::TRIANGLE_LIST_WITH_ADJACENCY: + case Topology::TRIANGLE_STRIP_WITH_ADJACENCY: + m_primitiveType = MTLPrimitiveType::MTLPrimitiveTypeTriangle; + break; + case Topology::PATCH_LIST: + default: + break; } for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) { From a5f2448ce3c00ce13a5ae1680da38e0aee7e9b61 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Fri, 6 Sep 2024 22:56:17 -0700 Subject: [PATCH 15/27] start working on command buffer --- Source/Metal/CommandAllocatorMTL.h | 36 ++++++++++++ Source/Metal/CommandAllocatorMTL.hpp | 20 +++++++ Source/Metal/CommandAllocatorMTL.mm | 28 +++++++++ Source/Metal/CommandBufferMTL.h | 27 ++++++--- Source/Metal/CommandBufferMTL.mm | 86 ++++++++++++++++++++++------ Source/Metal/PipelineMTL.h | 8 +-- 6 files changed, 177 insertions(+), 28 deletions(-) create mode 100644 Source/Metal/CommandAllocatorMTL.h create mode 100644 Source/Metal/CommandAllocatorMTL.hpp create mode 100644 Source/Metal/CommandAllocatorMTL.mm diff --git a/Source/Metal/CommandAllocatorMTL.h b/Source/Metal/CommandAllocatorMTL.h new file mode 100644 index 00000000..d10a47a7 --- /dev/null +++ b/Source/Metal/CommandAllocatorMTL.h @@ -0,0 +1,36 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +namespace nri { + +struct DeviceMTL; +struct CommandQueueMTL; + +struct CommandAllocatorMTL { + inline CommandAllocatorMTL(DeviceMTL& device) + : m_Device(device) { + } + + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + ~CommandAllocatorMTL(); + + + Result Create(const CommandQueue& commandQueue); + + //================================================================================================================ + // NRI + //================================================================================================================ + + Result CreateCommandBuffer(CommandBuffer*& commandBuffer); + +private: + struct CommandQueueMTL* m_CommandQueue; + DeviceMTL& m_Device; +}; + +} + diff --git a/Source/Metal/CommandAllocatorMTL.hpp b/Source/Metal/CommandAllocatorMTL.hpp new file mode 100644 index 00000000..bbad79eb --- /dev/null +++ b/Source/Metal/CommandAllocatorMTL.hpp @@ -0,0 +1,20 @@ +// © 2021 NVIDIA Corporation + +#pragma region[ Core ] + +static void NRI_CALL SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) { + //((CommandAllocatorVK&)commandAllocator).SetDebugName(name); +} + +static Result NRI_CALL CreateCommandBuffer(CommandAllocator& commandAllocator, CommandBuffer*& commandBuffer) { + return ((CommandAllocatorMTL&)commandAllocator).CreateCommandBuffer(commandBuffer); +} + +static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) { + //((CommandAllocatorVK&)commandAllocator).Reset(); +} + +#pragma endregion + +Define_Core_CommandAllocator_PartiallyFillFunctionTable(VK); + diff --git a/Source/Metal/CommandAllocatorMTL.mm b/Source/Metal/CommandAllocatorMTL.mm new file mode 100644 index 00000000..9532664f --- /dev/null +++ b/Source/Metal/CommandAllocatorMTL.mm @@ -0,0 +1,28 @@ +#include "SharedMTL.h" + +#include "CommandAllocatorMTL.h" + +#include "CommandQueueMTL.h" + +using namespace nri; + +CommandAllocatorMTL::~CommandAllocatorMTL() { + +} + +Result CommandAllocatorMTL::Create(const CommandQueue& commandQueue) { + m_CommandQueue = &(CommandQueueMTL&)commandQueue; + return Result::SUCCESS; +} + +//================================================================================================================ +// NRI +//================================================================================================================ + +Result CommandAllocatorMTL::CreateCommandBuffer(CommandBuffer*& commandBuffer) { + + + return Result::SUCCESS; +} + +#include "CommandAllocatorMTL.hpp" diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index ce670850..a11de62e 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -2,6 +2,7 @@ #pragma once +#include #import namespace nri { @@ -13,15 +14,18 @@ struct PipelineLayoutMTL; struct TextureMTL; struct DescriptorMTL; + +NriBits(CommandBufferDirtyBits, uint8_t, + NONE = 0, + CMD_DIRTY_STENCIL = NriBit(0) +); + struct CommandBufferMTL { + inline CommandBufferMTL(DeviceMTL& device) : m_Device(device) { } - - // inline operator VkCommandBuffer() const { - // return m_Handle; - // } - + inline DeviceMTL& GetDevice() const { return m_Device; } @@ -77,20 +81,27 @@ struct CommandBufferMTL { void DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); ~CommandBufferMTL(); - + + void Create(id cmd); + struct CmdIndexBuffer { size_t m_Offset; MTLIndexType m_Type; struct BufferMTL* m_Buffer; }; + private: + + void updateCommandBufferState(); + DeviceMTL& m_Device; struct PipelineMTL* m_CurrentPipeline = nullptr; struct CmdIndexBuffer m_CurrentIndexCmd; id m_Handle; - id m_encoder; + id m_RendererEncoder = nil; + id m_ComputeEncoder = nil; - //id renderEncoder; + CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE; }; }; diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 794126ef..7cf41d30 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -26,9 +26,11 @@ } Result CommandBufferMTL::Begin(const DescriptorPool* descriptorPool) { - + [m_Handle computeCommandEncoderWithDescriptor: NULL]; } Result CommandBufferMTL::End() { + m_ComputeEncoder = nil; + m_RendererEncoder = nil; } void CommandBufferMTL::SetPipeline(const Pipeline& pipeline) { @@ -36,7 +38,14 @@ return; PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline; m_CurrentPipeline = &pipelineImpl; - + + switch(m_CurrentPipeline->m_pipelineType) { + case nri::PipelineMTL::Compute: + m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL]; + break; + default: + break; + } } void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {} @@ -45,19 +54,46 @@ void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) {} void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {} void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { - m_encoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; + m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; } void CommandBufferMTL::EndRendering() { - [m_encoder endEncoding]; + [m_RendererEncoder endEncoding]; + m_RendererEncoder = nil; + m_ComputeEncoder = nil; } void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {} -void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) {} -void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) {} -void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) {} -void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) {} -void CommandBufferMTL::SetBlendConstants(const Color32f& color) {} -void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) {} -void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) {} +void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) { + NSCAssert(m_RendererEncoder, @"encoder set"); + MTLScissorRect rect; + rect.x = rects[rectNum].x; + rect.y = rects[rectNum].y; + rect.width = rects[rectNum].width; + rect.height = rects[rectNum].height; + [m_RendererEncoder setScissorRect:rect]; +} +void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) { +} +void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) { + [m_RendererEncoder setStencilFrontReferenceValue: frontRef backReferenceValue:backRef]; +} +void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) { + +} +void CommandBufferMTL::SetBlendConstants(const Color32f& color) { + [m_RendererEncoder + setBlendColorRed:color.x + green:color.y + blue:color.z + alpha:color.w + ]; +} +void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) { + +} + +void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { +} + void CommandBufferMTL::SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType) { m_CurrentIndexCmd.m_Buffer = &(BufferMTL&)buffer; switch(indexType) { @@ -71,19 +107,28 @@ } m_CurrentIndexCmd.m_Offset = offset; } + void CommandBufferMTL::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { + for(size_t i = 0; i < bufferNum; i++) { + BufferMTL* mtlBuffer = (BufferMTL*)buffers[i]; + [m_RendererEncoder setVertexBuffer: mtlBuffer->GetHandle() + offset: offsets[i] + atIndex: i + baseSlot]; + } } + void CommandBufferMTL::Draw(const DrawDesc& drawDesc) { - [m_encoder drawPrimitives: m_CurrentPipeline->m_primitiveType + [m_RendererEncoder drawPrimitives: m_CurrentPipeline->m_primitiveType vertexStart:drawDesc.baseVertex vertexCount:drawDesc.vertexNum instanceCount:drawDesc.instanceNum baseInstance:0]; } + void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) { id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); - [m_encoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType + [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType indexCount:drawIndexedDesc.indexNum indexType: m_CurrentIndexCmd.m_Type indexBuffer: indexBuffer @@ -91,8 +136,9 @@ } void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + // TODO: implement count Buffer NSCAssert(!countBuffer, @"count buffer not supported"); - [m_encoder + [m_RendererEncoder drawPrimitives: m_CurrentPipeline->m_primitiveType indirectBuffer:((BufferMTL&)buffer).GetHandle() indirectBufferOffset: offset]; @@ -101,7 +147,9 @@ void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { } -void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) {} +void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) { + +} void CommandBufferMTL::DispatchIndirect(const Buffer& buffer, uint64_t offset) {} void CommandBufferMTL::BeginQuery(const QueryPool& queryPool, uint32_t offset) {} void CommandBufferMTL::EndQuery(const QueryPool& queryPool, uint32_t offset) {} @@ -124,7 +172,13 @@ void CommandBufferMTL::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) {} void CommandBufferMTL::DispatchRaysIndirect(const Buffer& buffer, uint64_t offset) {} void CommandBufferMTL::DrawMeshTasks(const DrawMeshTasksDesc& drawMeshTasksDesc) {} -void CommandBufferMTL::DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) {} +void CommandBufferMTL::DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) { + +} + +void Create(id cmd); + + #include "CommandBufferMTL.hpp" diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index b274bc49..2d2d3d92 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -8,10 +8,9 @@ struct DeviceMTL; struct PipelineLayoutMTL; struct PipelineMTL { - enum PiplineType { - None, - Graphics, + enum PipelineType { Compute, + Graphics, Raytracing }; @@ -23,7 +22,8 @@ struct PipelineMTL { Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); Result Create(const ComputePipelineDesc& computePipelineDesc); Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); - + + PipelineType m_pipelineType; MTLPrimitiveTopologyClass m_topologyClass; MTLPrimitiveType m_primitiveType; private: From 1da2b24336f079802c8f1dea16b14fc3c82cb704 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sun, 8 Sep 2024 10:52:08 -0700 Subject: [PATCH 16/27] feat: begin shader load logic Signed-off-by: Michael Pollind --- Source/Metal/CommandBufferMTL.mm | 67 ++++++++++++++++++++++++++------ Source/Metal/CommandQueueMTL.h | 10 +++++ Source/Metal/PipelineMTL.h | 12 +++--- Source/Metal/PipelineMTL.mm | 14 +++++++ 4 files changed, 86 insertions(+), 17 deletions(-) diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 7cf41d30..2810030f 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -39,20 +39,59 @@ PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline; m_CurrentPipeline = &pipelineImpl; - switch(m_CurrentPipeline->m_pipelineType) { - case nri::PipelineMTL::Compute: - m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL]; - break; - default: - break; - } +// if(m_CurrentPipeline->m_pipelineType == nri::PipelineMTL::Compute) { + // m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL]; + // } + +} +void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) { + +} +void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { + +} +void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) { + //if (pDesc->mUsedStages & SHADER_STAGE_VERT) + //{ + // [m_RendererEncoder setVertexBytes:data length:size atIndex:pushConstantIndex]; + //} + + //if (pDesc->mUsedStages & SHADER_STAGE_FRAG) + //{ + // [m_RendererEncoder setFragmentBytes:data length:size atIndex:pushConstantIndex]; + //} + + //if (pDesc->mUsedStages & SHADER_STAGE_COMP) + //{ + // [m_RendererEncoder setBytes:data length:size atIndex:pushConstantIndex]; + //} } -void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) {} -void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) {} -void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) {} void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) {} -void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) {} +void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) { + //if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_BUFFERS) + { + [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers + afterStages:MTLRenderStageFragment + beforeStages:MTLRenderStageVertex]; + } + + //if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_TEXTURES) + { + [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeTextures + afterStages:MTLRenderStageFragment + beforeStages:MTLRenderStageVertex]; + } + + //if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_RENDERTARGETS) + { + [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeRenderTargets + afterStages:MTLRenderStageFragment + beforeStages:MTLRenderStageVertex]; + } + + +} void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; } @@ -61,7 +100,11 @@ m_RendererEncoder = nil; m_ComputeEncoder = nil; } -void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) {} +void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) { + MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum); + + // [m_RendererEncoder setViewports:<#(const MTLViewport * _Nonnull)#> count:<#(NSUInteger)#> +} void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) { NSCAssert(m_RendererEncoder, @"encoder set"); MTLScissorRect rect; diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h index 7d48be94..3be48381 100644 --- a/Source/Metal/CommandQueueMTL.h +++ b/Source/Metal/CommandQueueMTL.h @@ -7,6 +7,13 @@ namespace nri { struct DeviceMTL; +NriBits(QueueBarrierBits, uint8_t, + NONE = 0, + BARRIER_FLAG_BUFFERS = NriBit(0), + BARRIER_FLAG_TEXTURES = NriBit(1), + BARRIER_FLAG_RENDERTARGETS = NriBit(2), + BARRIER_FLAG_FENCE = NriBit(3)); + struct CommandQueueMTL { inline CommandQueueMTL(DeviceMTL& device) @@ -35,11 +42,14 @@ struct CommandQueueMTL { void SetDebugName(const char* name); Result Create(CommandQueueType type); + QueueBarrierBits m_BarrierBits = QueueBarrierBits::NONE; + private: DeviceMTL& m_Device; CommandQueueType m_Type = CommandQueueType(-1); id m_Handle; Lock m_Lock; + }; } diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index 2d2d3d92..785f948e 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -7,12 +7,13 @@ namespace nri { struct DeviceMTL; struct PipelineLayoutMTL; +NriEnum(PipelineType, uint8_t, + Compute, + Graphics, + Raytracing +); + struct PipelineMTL { - enum PipelineType { - Compute, - Graphics, - Raytracing - }; inline PipelineMTL(DeviceMTL& device) : m_Device(device) { @@ -26,6 +27,7 @@ struct PipelineMTL { PipelineType m_pipelineType; MTLPrimitiveTopologyClass m_topologyClass; MTLPrimitiveType m_primitiveType; + StageBits m_usedBits; private: DeviceMTL& m_Device; }; diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index 77418aab..49d9876f 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -43,6 +43,20 @@ } for (uint32_t i = 0; i < graphicsPipelineDesc.shaderNum; i++) { + const ShaderDesc& shader = graphicsPipelineDesc.shaders[i]; + m_usedBits |= shader.stage; + + dispatch_data_t byteCode = + dispatch_data_create(shader.bytecode, shader.size, nil, + DISPATCH_DATA_DESTRUCTOR_DEFAULT); + id lib = [m_Device newLibraryWithData:byteCode error:nil]; + + // Create a MTLFunction from the loaded MTLLibrary. + NSString *entryPointNStr = [lib functionNames][0]; + if (shader.entryPointName) { + entryPointNStr = + [[NSString alloc] initWithUTF8String:shader.entryPointName]; + } } // Depth-stencil const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth; From c67577bcb75a7dde132f4036eb5d6b5711b1bfc3 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Tue, 1 Oct 2024 13:03:07 -0700 Subject: [PATCH 17/27] fix compiling errors --- Include/NRIDescs.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index dfb9d641..87a6948b 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -76,7 +76,8 @@ NriEnum(GraphicsAPI, uint8_t, NONE, // Supports everything, does nothing, returns dummy non-NULL objects and ~0-filled descs, available if "NRI_ENABLE_NONE_SUPPORT = ON" in CMake D3D11, // Direct3D 11 (feature set 11.1), available if "NRI_ENABLE_D3D11_SUPPORT = ON" in CMake D3D12, // Direct3D 12 (feature set 11.1+), available if "NRI_ENABLE_D3D12_SUPPORT = ON" in CMake - VK // Vulkan 1.3 or 1.2+ (can be used on MacOS via MoltenVK), available if "NRI_ENABLE_VK_SUPPORT = ON" in CMake + VK, // Vulkan 1.3 or 1.2+ (can be used on MacOS via MoltenVK), available if "NRI_ENABLE_VK_SUPPORT = ON" in CMake + MTL ); NriEnum(Result, uint8_t, @@ -1284,13 +1285,6 @@ NriStruct(PipelineStatisticsDesc) { #pragma region [ Device desc ] //============================================================================================================================================================================================ -NriEnum(GraphicsAPI, uint8_t, - D3D11, - D3D12, - VK, - MTL -); - // defined in apple framework #undef INTEL #undef AMD From 55f887a2f0038f290e8b7bf5bc74045773c71250 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Tue, 1 Oct 2024 20:20:47 -0700 Subject: [PATCH 18/27] feat: cleanup Signed-off-by: Michael Pollind --- Source/Metal/CommandBufferMTL.mm | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 2810030f..4dbadb66 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -2,15 +2,12 @@ #include "CommandBufferMTL.h" -//#include "AccelerationStructureVK.h" #include "BufferMTL.h" #include "CommandBufferMTL.h" #include "CommandQueueMTL.h" -//#include "DescriptorSetMTL.h" #include "DescriptorMTL.h" #include "PipelineLayoutMTL.h" #include "PipelineMTL.h" -//#include "QueryPoolMTL.h" #include "TextureMTL.h" #include @@ -28,11 +25,13 @@ Result CommandBufferMTL::Begin(const DescriptorPool* descriptorPool) { [m_Handle computeCommandEncoderWithDescriptor: NULL]; } + Result CommandBufferMTL::End() { m_ComputeEncoder = nil; m_RendererEncoder = nil; } + void CommandBufferMTL::SetPipeline(const Pipeline& pipeline) { if (m_CurrentPipeline == (PipelineMTL*)&pipeline) return; @@ -93,6 +92,15 @@ } void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { + MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; + for(uint32_t i = 0; i < attachmentsDesc.colorNum; i++) { + + } + + //renderPassDescriptor.colorAttachments[ + + //renderPassDescriptor.colorAttachments + m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; } void CommandBufferMTL::EndRendering() { @@ -182,10 +190,9 @@ // TODO: implement count Buffer NSCAssert(!countBuffer, @"count buffer not supported"); [m_RendererEncoder - drawPrimitives: m_CurrentPipeline->m_primitiveType - indirectBuffer:((BufferMTL&)buffer).GetHandle() + drawPrimitives: m_CurrentPipeline->m_primitiveType + indirectBuffer:((BufferMTL&)buffer).GetHandle() indirectBufferOffset: offset]; - } void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { From 9c16da7166e5b73a9a527df0cb7e2c276f0f4432 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 13 Nov 2024 22:09:00 -0800 Subject: [PATCH 19/27] feat: fix compiling errors Signed-off-by: Michael Pollind --- Include/NRIDescs.h | 5 + Source/Creation/Creation.cpp | 4 +- Source/Metal/CommandAllocatorMTL.hpp | 1 - Source/Metal/CommandBufferMTL.h | 2 +- Source/Metal/CommandBufferMTL.hpp | 354 +++++++++++++-------------- Source/Metal/CommandBufferMTL.mm | 7 +- Source/Metal/ConversionMTL.h | 82 +++++++ Source/Metal/ConversionMTL.mm | 5 +- Source/Metal/DeviceMTL.hpp | 29 +-- Source/Metal/ImplMTL.mm | 0 Source/Metal/PipelineMTL.mm | 42 ++-- 11 files changed, 316 insertions(+), 215 deletions(-) create mode 100644 Source/Metal/ImplMTL.mm diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index 87a6948b..7f9d39c5 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -686,9 +686,14 @@ NriStruct(VertexAttributeVK) { uint32_t location; }; +NriStruct(VertexAttributeMTL) { + uint32_t location; +}; + NriStruct(VertexAttributeDesc) { Nri(VertexAttributeD3D) d3d; Nri(VertexAttributeVK) vk; + Nri(VertexAttributeMTL) mtl; uint32_t offset; Nri(Format) format; uint16_t streamIndex; diff --git a/Source/Creation/Creation.cpp b/Source/Creation/Creation.cpp index 05c284a5..93173844 100644 --- a/Source/Creation/Creation.cpp +++ b/Source/Creation/Creation.cpp @@ -23,7 +23,7 @@ Result CreateDeviceMTL(const DeviceCreationDesc& deviceCreationDesc, DeviceBase* Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBase*& device); #endif -#if NRI_USE_VULKAN +#if NRI_USE_VK Result CreateDeviceVK(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device); Result CreateDeviceVK(const DeviceCreationVKDesc& deviceDesc, DeviceBase*& device); #endif @@ -351,7 +351,7 @@ NRI_API Format NRI_CALL nriConvertDXGIFormatToNRI(uint32_t dxgiFormat) { NRI_API uint32_t NRI_CALL nriConvertNRIFormatToMTL(Format format) { MaybeUnused(format); -#if NRI_USE_VULKAN +#if NRI_USE_VK return NRIFormatToMTLFormat(format); #else return 0; diff --git a/Source/Metal/CommandAllocatorMTL.hpp b/Source/Metal/CommandAllocatorMTL.hpp index bbad79eb..0239d982 100644 --- a/Source/Metal/CommandAllocatorMTL.hpp +++ b/Source/Metal/CommandAllocatorMTL.hpp @@ -16,5 +16,4 @@ static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) { #pragma endregion -Define_Core_CommandAllocator_PartiallyFillFunctionTable(VK); diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index a11de62e..ac450a64 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -45,7 +45,7 @@ struct CommandBufferMTL { void SetScissors(const Rect* rects, uint32_t rectNum); void SetDepthBounds(float boundsMin, float boundsMax); void SetStencilReference(uint8_t frontRef, uint8_t backRef); - void SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum); + // void SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum); void SetBlendConstants(const Color32f& color); void SetShadingRate(const ShadingRateDesc& shadingRateDesc); void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); diff --git a/Source/Metal/CommandBufferMTL.hpp b/Source/Metal/CommandBufferMTL.hpp index 7aa867b4..17f4af40 100644 --- a/Source/Metal/CommandBufferMTL.hpp +++ b/Source/Metal/CommandBufferMTL.hpp @@ -1,179 +1,179 @@ -#pragma region[ Core ] - -static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { - ((CommandBufferMTL&)commandBuffer).SetDebugName(name); -} - -static Result NRI_CALL BeginCommandBuffer(CommandBuffer& commandBuffer, const DescriptorPool* descriptorPool) { - return ((CommandBufferMTL&)commandBuffer).Begin(descriptorPool); -} - -static Result NRI_CALL EndCommandBuffer(CommandBuffer& commandBuffer) { - return ((CommandBufferMTL&)commandBuffer).End(); -} - -static void NRI_CALL CmdSetPipelineLayout(CommandBuffer& commandBuffer, const PipelineLayout& pipelineLayout) { - ((CommandBufferMTL&)commandBuffer).SetPipelineLayout(pipelineLayout); -} - -static void NRI_CALL CmdSetPipeline(CommandBuffer& commandBuffer, const Pipeline& pipeline) { - ((CommandBufferMTL&)commandBuffer).SetPipeline(pipeline); -} - -static void NRI_CALL CmdBarrier(CommandBuffer& commandBuffer, const BarrierGroupDesc& barrierGroupDesc) { - ((CommandBufferMTL&)commandBuffer).Barrier(barrierGroupDesc); -} - -static void NRI_CALL CmdSetDescriptorPool(CommandBuffer& commandBuffer, const DescriptorPool& descriptorPool) { - ((CommandBufferMTL&)commandBuffer).SetDescriptorPool(descriptorPool); -} - -static void NRI_CALL CmdSetDescriptorSet(CommandBuffer& commandBuffer, uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { - ((CommandBufferMTL&)commandBuffer).SetDescriptorSet(setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); -} - -static void NRI_CALL CmdSetConstants(CommandBuffer& commandBuffer, uint32_t pushConstantIndex, const void* data, uint32_t size) { - ((CommandBufferMTL&)commandBuffer).SetConstants(pushConstantIndex, data, size); -} - -static void NRI_CALL CmdBeginRendering(CommandBuffer& commandBuffer, const AttachmentsDesc& attachmentsDesc) { - ((CommandBufferMTL&)commandBuffer).BeginRendering(attachmentsDesc); -} - -static void NRI_CALL CmdEndRendering(CommandBuffer& commandBuffer) { - ((CommandBufferMTL&)commandBuffer).EndRendering(); -} - -static void NRI_CALL CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) { - ((CommandBufferMTL&)commandBuffer).SetViewports(viewports, viewportNum); -} - -static void NRI_CALL CmdSetScissors(CommandBuffer& commandBuffer, const nri::Rect* rects, uint32_t rectNum) { - ((CommandBufferMTL&)commandBuffer).SetScissors(rects, rectNum); -} - -static void NRI_CALL CmdSetDepthBounds(CommandBuffer& commandBuffer, float boundsMin, float boundsMax) { - ((CommandBufferMTL&)commandBuffer).SetDepthBounds(boundsMin, boundsMax); -} - -static void NRI_CALL CmdSetStencilReference(CommandBuffer& commandBuffer, uint8_t frontRef, uint8_t backRef) { - ((CommandBufferMTL&)commandBuffer).SetStencilReference(frontRef, backRef); -} - -static void NRI_CALL CmdSetSamplePositions(CommandBuffer& commandBuffer, const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) { - ((CommandBufferMTL&)commandBuffer).SetSamplePositions(positions, positionNum, sampleNum); -} - -static void NRI_CALL CmdSetBlendConstants(CommandBuffer& commandBuffer, const Color32f& color) { - ((CommandBufferMTL&)commandBuffer).SetBlendConstants(color); -} - -static void NRI_CALL CmdSetShadingRate(CommandBuffer& commandBuffer, const ShadingRateDesc& shadingRateDesc) { - ((CommandBufferMTL&)commandBuffer).SetShadingRate(shadingRateDesc); -} - -static void NRI_CALL CmdSetDepthBias(CommandBuffer& commandBuffer, const DepthBiasDesc& depthBiasDesc) { - //((CommandBufferMTL&)commandBuffer).SetDepthBias(depthBiasDesc); -} - -static void NRI_CALL CmdClearAttachments(CommandBuffer& commandBuffer, const ClearDesc* clearDescs, uint32_t clearDescNum, const nri::Rect* rects, uint32_t rectNum) { - ((CommandBufferMTL&)commandBuffer).ClearAttachments(clearDescs, clearDescNum, rects, rectNum); -} - -static void NRI_CALL CmdSetIndexBuffer(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, IndexType indexType) { - ((CommandBufferMTL&)commandBuffer).SetIndexBuffer(buffer, offset, indexType); -} - -static void NRI_CALL CmdSetVertexBuffers(CommandBuffer& commandBuffer, uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { - ((CommandBufferMTL&)commandBuffer).SetVertexBuffers(baseSlot, bufferNum, buffers, offsets); -} - -static void NRI_CALL CmdDraw(CommandBuffer& commandBuffer, const DrawDesc& drawDesc) { - ((CommandBufferMTL&)commandBuffer).Draw(drawDesc); -} - -static void NRI_CALL CmdDrawIndexed(CommandBuffer& commandBuffer, const DrawIndexedDesc& drawIndexedDesc) { - ((CommandBufferMTL&)commandBuffer).DrawIndexed(drawIndexedDesc); -} - -static void NRI_CALL CmdDrawIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { - ((CommandBufferMTL&)commandBuffer).DrawIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); -} - -static void NRI_CALL CmdDrawIndexedIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { - ((CommandBufferMTL&)commandBuffer).DrawIndexedIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); -} - -static void NRI_CALL CmdDispatch(CommandBuffer& commandBuffer, const DispatchDesc& dispatchDesc) { - ((CommandBufferMTL&)commandBuffer).Dispatch(dispatchDesc); -} - -static void NRI_CALL CmdDispatchIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset) { - ((CommandBufferMTL&)commandBuffer).DispatchIndirect(buffer, offset); -} - -static void NRI_CALL CmdBeginQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { - ((CommandBufferMTL&)commandBuffer).BeginQuery(queryPool, offset); -} - -static void NRI_CALL CmdEndQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { - ((CommandBufferMTL&)commandBuffer).EndQuery(queryPool, offset); -} - -static void NRI_CALL CmdBeginAnnotation(CommandBuffer& commandBuffer, const char* name) { - ((CommandBufferMTL&)commandBuffer).BeginAnnotation(name); -} - -static void NRI_CALL CmdEndAnnotation(CommandBuffer& commandBuffer) { - ((CommandBufferMTL&)commandBuffer).EndAnnotation(); -} - -static void NRI_CALL CmdClearStorageBuffer(CommandBuffer& commandBuffer, const ClearStorageBufferDesc& clearDesc) { - ((CommandBufferMTL&)commandBuffer).ClearStorageBuffer(clearDesc); -} - -static void NRI_CALL CmdClearStorageTexture(CommandBuffer& commandBuffer, const ClearStorageTextureDesc& clearDesc) { - ((CommandBufferMTL&)commandBuffer).ClearStorageTexture(clearDesc); -} - -static void NRI_CALL CmdCopyBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) { - ((CommandBufferMTL&)commandBuffer).CopyBuffer(dstBuffer, dstOffset, srcBuffer, srcOffset, size); -} - -static void NRI_CALL CmdCopyTexture( - CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) { - ((CommandBufferMTL&)commandBuffer).CopyTexture(dstTexture, dstRegionDesc, srcTexture, srcRegionDesc); -} - -static void NRI_CALL CmdUploadBufferToTexture(CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { - ((CommandBufferMTL&)commandBuffer).UploadBufferToTexture(dstTexture, dstRegionDesc, srcBuffer, srcDataLayoutDesc); -} - -static void NRI_CALL CmdReadbackTextureToBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, const TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { - //((CommandBufferMTL&)commandBuffer).ReadbackTextureToBuffer(dstBuffer, dstDataLayoutDesc, srcTexture, srcRegionDesc); -} - -static void NRI_CALL CmdCopyQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) { - ((CommandBufferMTL&)commandBuffer).CopyQueries(queryPool, offset, num, dstBuffer, dstOffset); -} - -static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num) { - ((CommandBufferMTL&)commandBuffer).ResetQueries(queryPool, offset, num); -} - -static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandBuffer) { - if (!(&commandBuffer)) - return nullptr; - - return nullptr; - // return (CommandBufferMTL)((CommandBufferMTL&)commandBuffer); -} - -#pragma endregion - - - - -Define_Core_CommandBuffer_PartiallyFillFunctionTable(VK); +//#pragma region[ Core ] +// +//static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { +// ((CommandBufferMTL&)commandBuffer).SetDebugName(name); +//} +// +//static Result NRI_CALL BeginCommandBuffer(CommandBuffer& commandBuffer, const DescriptorPool* descriptorPool) { +// return ((CommandBufferMTL&)commandBuffer).Begin(descriptorPool); +//} +// +//static Result NRI_CALL EndCommandBuffer(CommandBuffer& commandBuffer) { +// return ((CommandBufferMTL&)commandBuffer).End(); +//} +// +//static void NRI_CALL CmdSetPipelineLayout(CommandBuffer& commandBuffer, const PipelineLayout& pipelineLayout) { +// ((CommandBufferMTL&)commandBuffer).SetPipelineLayout(pipelineLayout); +//} +// +//static void NRI_CALL CmdSetPipeline(CommandBuffer& commandBuffer, const Pipeline& pipeline) { +// ((CommandBufferMTL&)commandBuffer).SetPipeline(pipeline); +//} +// +//static void NRI_CALL CmdBarrier(CommandBuffer& commandBuffer, const BarrierGroupDesc& barrierGroupDesc) { +// ((CommandBufferMTL&)commandBuffer).Barrier(barrierGroupDesc); +//} +// +//static void NRI_CALL CmdSetDescriptorPool(CommandBuffer& commandBuffer, const DescriptorPool& descriptorPool) { +// ((CommandBufferMTL&)commandBuffer).SetDescriptorPool(descriptorPool); +//} +// +//static void NRI_CALL CmdSetDescriptorSet(CommandBuffer& commandBuffer, uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { +// ((CommandBufferMTL&)commandBuffer).SetDescriptorSet(setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); +//} +// +//static void NRI_CALL CmdSetConstants(CommandBuffer& commandBuffer, uint32_t pushConstantIndex, const void* data, uint32_t size) { +// ((CommandBufferMTL&)commandBuffer).SetConstants(pushConstantIndex, data, size); +//} +// +//static void NRI_CALL CmdBeginRendering(CommandBuffer& commandBuffer, const AttachmentsDesc& attachmentsDesc) { +// ((CommandBufferMTL&)commandBuffer).BeginRendering(attachmentsDesc); +//} +// +//static void NRI_CALL CmdEndRendering(CommandBuffer& commandBuffer) { +// ((CommandBufferMTL&)commandBuffer).EndRendering(); +//} +// +//static void NRI_CALL CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) { +// ((CommandBufferMTL&)commandBuffer).SetViewports(viewports, viewportNum); +//} +// +//static void NRI_CALL CmdSetScissors(CommandBuffer& commandBuffer, const nri::Rect* rects, uint32_t rectNum) { +// ((CommandBufferMTL&)commandBuffer).SetScissors(rects, rectNum); +//} +// +//static void NRI_CALL CmdSetDepthBounds(CommandBuffer& commandBuffer, float boundsMin, float boundsMax) { +// ((CommandBufferMTL&)commandBuffer).SetDepthBounds(boundsMin, boundsMax); +//} +// +//static void NRI_CALL CmdSetStencilReference(CommandBuffer& commandBuffer, uint8_t frontRef, uint8_t backRef) { +// ((CommandBufferMTL&)commandBuffer).SetStencilReference(frontRef, backRef); +//} +// +//static void NRI_CALL CmdSetSamplePositions(CommandBuffer& commandBuffer, const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) { +// ((CommandBufferMTL&)commandBuffer).SetSamplePositions(positions, positionNum, sampleNum); +//} +// +//static void NRI_CALL CmdSetBlendConstants(CommandBuffer& commandBuffer, const Color32f& color) { +// ((CommandBufferMTL&)commandBuffer).SetBlendConstants(color); +//} +// +//static void NRI_CALL CmdSetShadingRate(CommandBuffer& commandBuffer, const ShadingRateDesc& shadingRateDesc) { +// ((CommandBufferMTL&)commandBuffer).SetShadingRate(shadingRateDesc); +//} +// +//static void NRI_CALL CmdSetDepthBias(CommandBuffer& commandBuffer, const DepthBiasDesc& depthBiasDesc) { +// //((CommandBufferMTL&)commandBuffer).SetDepthBias(depthBiasDesc); +//} +// +//static void NRI_CALL CmdClearAttachments(CommandBuffer& commandBuffer, const ClearDesc* clearDescs, uint32_t clearDescNum, const nri::Rect* rects, uint32_t rectNum) { +// ((CommandBufferMTL&)commandBuffer).ClearAttachments(clearDescs, clearDescNum, rects, rectNum); +//} +// +//static void NRI_CALL CmdSetIndexBuffer(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, IndexType indexType) { +// ((CommandBufferMTL&)commandBuffer).SetIndexBuffer(buffer, offset, indexType); +//} +// +//static void NRI_CALL CmdSetVertexBuffers(CommandBuffer& commandBuffer, uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { +// ((CommandBufferMTL&)commandBuffer).SetVertexBuffers(baseSlot, bufferNum, buffers, offsets); +//} +// +//static void NRI_CALL CmdDraw(CommandBuffer& commandBuffer, const DrawDesc& drawDesc) { +// ((CommandBufferMTL&)commandBuffer).Draw(drawDesc); +//} +// +//static void NRI_CALL CmdDrawIndexed(CommandBuffer& commandBuffer, const DrawIndexedDesc& drawIndexedDesc) { +// ((CommandBufferMTL&)commandBuffer).DrawIndexed(drawIndexedDesc); +//} +// +//static void NRI_CALL CmdDrawIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { +// ((CommandBufferMTL&)commandBuffer).DrawIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); +//} +// +//static void NRI_CALL CmdDrawIndexedIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { +// ((CommandBufferMTL&)commandBuffer).DrawIndexedIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); +//} +// +//static void NRI_CALL CmdDispatch(CommandBuffer& commandBuffer, const DispatchDesc& dispatchDesc) { +// ((CommandBufferMTL&)commandBuffer).Dispatch(dispatchDesc); +//} +// +//static void NRI_CALL CmdDispatchIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset) { +// ((CommandBufferMTL&)commandBuffer).DispatchIndirect(buffer, offset); +//} +// +//static void NRI_CALL CmdBeginQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { +// ((CommandBufferMTL&)commandBuffer).BeginQuery(queryPool, offset); +//} +// +//static void NRI_CALL CmdEndQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { +// ((CommandBufferMTL&)commandBuffer).EndQuery(queryPool, offset); +//} +// +//static void NRI_CALL CmdBeginAnnotation(CommandBuffer& commandBuffer, const char* name) { +// ((CommandBufferMTL&)commandBuffer).BeginAnnotation(name); +//} +// +//static void NRI_CALL CmdEndAnnotation(CommandBuffer& commandBuffer) { +// ((CommandBufferMTL&)commandBuffer).EndAnnotation(); +//} +// +//static void NRI_CALL CmdClearStorageBuffer(CommandBuffer& commandBuffer, const ClearStorageBufferDesc& clearDesc) { +// ((CommandBufferMTL&)commandBuffer).ClearStorageBuffer(clearDesc); +//} +// +//static void NRI_CALL CmdClearStorageTexture(CommandBuffer& commandBuffer, const ClearStorageTextureDesc& clearDesc) { +// ((CommandBufferMTL&)commandBuffer).ClearStorageTexture(clearDesc); +//} +// +//static void NRI_CALL CmdCopyBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) { +// ((CommandBufferMTL&)commandBuffer).CopyBuffer(dstBuffer, dstOffset, srcBuffer, srcOffset, size); +//} +// +//static void NRI_CALL CmdCopyTexture( +// CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) { +// ((CommandBufferMTL&)commandBuffer).CopyTexture(dstTexture, dstRegionDesc, srcTexture, srcRegionDesc); +//} +// +//static void NRI_CALL CmdUploadBufferToTexture(CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { +// ((CommandBufferMTL&)commandBuffer).UploadBufferToTexture(dstTexture, dstRegionDesc, srcBuffer, srcDataLayoutDesc); +//} +// +//static void NRI_CALL CmdReadbackTextureToBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, const TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { +// //((CommandBufferMTL&)commandBuffer).ReadbackTextureToBuffer(dstBuffer, dstDataLayoutDesc, srcTexture, srcRegionDesc); +//} +// +//static void NRI_CALL CmdCopyQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) { +// ((CommandBufferMTL&)commandBuffer).CopyQueries(queryPool, offset, num, dstBuffer, dstOffset); +//} +// +//static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num) { +// ((CommandBufferMTL&)commandBuffer).ResetQueries(queryPool, offset, num); +//} +// +//static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandBuffer) { +// if (!(&commandBuffer)) +// return nullptr; +// +// return nullptr; +// // return (CommandBufferMTL)((CommandBufferMTL&)commandBuffer); +//} +// +//#pragma endregion +// +// +// +// +//Define_Core_CommandBuffer_PartiallyFillFunctionTable(VK); diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 4dbadb66..8cbfb0ab 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -109,7 +109,7 @@ m_ComputeEncoder = nil; } void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) { - MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum); + //MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum); // [m_RendererEncoder setViewports:<#(const MTLViewport * _Nonnull)#> count:<#(NSUInteger)#> } @@ -127,9 +127,8 @@ void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) { [m_RendererEncoder setStencilFrontReferenceValue: frontRef backReferenceValue:backRef]; } -void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) { - -} +//void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) { +//} void CommandBufferMTL::SetBlendConstants(const Color32f& color) { [m_RendererEncoder setBlendColorRed:color.x diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index 1079bbc8..7d29b471 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -4,6 +4,81 @@ namespace nri { +constexpr std::array MTL_NRI_FORMAT_TO_VERTEX_FORMAT = { + MTLVertexFormatInvalid, // UNKNOWN + MTLVertexFormatUCharNormalized, // R8_UNORM + MTLVertexFormatCharNormalized, // R8_SNORM + MTLVertexFormatUChar, // R8_UINT + MTLVertexFormatChar, // R8_SINT + MTLVertexFormatUChar2Normalized, // RG8_UNORM + MTLVertexFormatChar2Normalized, // RG8_SNORM + MTLVertexFormatUChar2, // RG8_UINT + MTLVertexFormatChar2, // RG8_SINT + MTLVertexFormatUChar4Normalized, // BGRA8_UNORM + MTLVertexFormatInvalid, // BGRA8_SRGB + MTLVertexFormatUChar4Normalized, // RGBA8_UNORM + MTLVertexFormatInvalid, // RGBA8_SRGB + MTLVertexFormatChar4Normalized, // RGBA8_SNORM + MTLVertexFormatUChar4, // RGBA8_UINT + MTLVertexFormatChar4, // RGBA8_SINT + MTLVertexFormatUShortNormalized, // R16_UNORM + MTLVertexFormatShortNormalized, // R16_SNORM + MTLVertexFormatUShort, // R16_UINT + MTLVertexFormatShort, // R16_SINT + MTLVertexFormatHalf, // R16_SFLOAT + MTLVertexFormatUShort2Normalized, // RG16_UNORM + MTLVertexFormatShort2Normalized, // RG16_SNORM + MTLVertexFormatUShort2, // RG16_UINT + MTLVertexFormatShort2, // RG16_SINT + MTLVertexFormatHalf2, // RG16_SFLOAT + MTLVertexFormatUShort4Normalized, // RGBA16_UNORM + MTLVertexFormatShort4Normalized, // RGBA16_SNORM + MTLVertexFormatUShort4, // RGBA16_UINT + MTLVertexFormatShort4, // RGBA16_SINT + MTLVertexFormatHalf4, // RGBA16_SFLOAT + MTLVertexFormatUInt, // R32_UINT + MTLVertexFormatInt, // R32_SINT + MTLVertexFormatFloat, // R32_SFLOAT + MTLVertexFormatUInt2, // RG32_UINT + MTLVertexFormatInt2, // RG32_SINT + MTLVertexFormatFloat2, // RG32_SFLOAT + MTLVertexFormatInvalid, // RGB32_UINT //TODO: lookup correct values + MTLVertexFormatInvalid, // RGB32_SINT //TODO: lookup correct values + MTLVertexFormatInvalid, // RGB32_SFLOAT //TODO: lookup correct values + MTLVertexFormatUInt4, // RGBA32_UINT + MTLVertexFormatInt4, // RGBA32_SINT + MTLVertexFormatFloat4, // RGBA32_SFLOAT + MTLVertexFormatInvalid, // B5_G6_R5_UNORM + MTLVertexFormatInvalid, // B5_G5_R5_A1_UNORM + MTLVertexFormatInvalid, // B4_G4_R4_A4_UNORM + MTLVertexFormatInvalid, // R10_G10_B10_A2_UNORM + MTLVertexFormatInvalid, // R10_G10_B10_A2_UINT + MTLVertexFormatInvalid, // R11_G11_B10_UFLOAT + MTLVertexFormatInvalid, // R9_G9_B9_E5_UFLOAT + MTLVertexFormatInvalid, // BC1_RGBA_UNORM + MTLVertexFormatInvalid, // BC1_RGBA_SRGB + MTLVertexFormatInvalid, // BC2_RGBA_UNORM + MTLVertexFormatInvalid, // BC2_RGBA_SRGB + MTLVertexFormatInvalid, // BC3_RGBA_UNORM + MTLVertexFormatInvalid, // BC3_RGBA_SRGB + MTLVertexFormatInvalid, // BC4_R_UNORM + MTLVertexFormatInvalid, // BC4_R_SNORM + MTLVertexFormatInvalid, // BC5_RG_UNORM + MTLVertexFormatInvalid, // BC5_RG_SNORM + MTLVertexFormatInvalid, // BC6H_RGB_UFLOAT + MTLVertexFormatInvalid, // BC6H_RGB_SFLOAT + MTLVertexFormatInvalid, // BC7_RGBA_UNORM + MTLVertexFormatInvalid, // BC7_RGBA_SRGB + MTLVertexFormatInvalid, // D16_UNORM + MTLVertexFormatInvalid, // D24_UNORM_S8_UINT + MTLVertexFormatInvalid, // D32_SFLOAT + MTLVertexFormatInvalid, // D32_SFLOAT_S8_UINT_X24 + MTLVertexFormatInvalid, // R24_UNORM_X8 + MTLVertexFormatInvalid, // X24_G8_UINT + MTLVertexFormatInvalid, // R32_SFLOAT_X8_X24 + MTLVertexFormatInvalid, // X32_G8_UINT_X24 +}; + constexpr std::array BLEND_OP = { MTLBlendOperationAdd, // ADD MTLBlendOperationSubtract, // SUBTRACT @@ -153,6 +228,13 @@ constexpr MTLPrimitiveTopologyClass GetTopologyMTL(Topology topology) { return TOPOLOGIES_CLASSES [(size_t)topology]; } + +inline MTLVertexFormat GetVertexFormatMTL(Format format) { + return (MTLVertexFormat)MTL_NRI_FORMAT_TO_VERTEX_FORMAT[(size_t)format]; +} + + + inline MTLPixelFormat GetFormatMTL(Format format, bool demoteSrgb = false) { if (demoteSrgb) { const FormatProps& formatProps = GetFormatProps(format); diff --git a/Source/Metal/ConversionMTL.mm b/Source/Metal/ConversionMTL.mm index 8e13b6d1..ef200713 100644 --- a/Source/Metal/ConversionMTL.mm +++ b/Source/Metal/ConversionMTL.mm @@ -5,7 +5,7 @@ using namespace nri; // Each depth/stencil format is only compatible with itself in VK -constexpr std::array MTL_FORMAT = { +constexpr std::array MTL_PIXEL_FORMAT = { MTLPixelFormatInvalid, // UNKNOWN MTLPixelFormatR8Unorm, // R8_UNORM MTLPixelFormatR8Snorm, // R8_SNORM @@ -80,7 +80,8 @@ MTLPixelFormatDepth32Float_Stencil8, // X32_G8_UINT_X24 }; + uint32_t NRIFormatToMTLFormat(Format format) { - return (uint32_t)MTL_FORMAT[(uint32_t)format]; + return (uint32_t)MTL_PIXEL_FORMAT[(uint32_t)format]; } diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp index 16770b3d..cb6d4977 100644 --- a/Source/Metal/DeviceMTL.hpp +++ b/Source/Metal/DeviceMTL.hpp @@ -1,6 +1,6 @@ // © 2021 NVIDIA Corporation -Declare_PartiallyFillFunctionTable_Functions(MTL); +//Declare_PartiallyFillFunctionTable_Functions(MTL); #pragma region[ Core ] @@ -200,18 +200,19 @@ static void* NRI_CALL GetDeviceNativeObject(const Device& device) { Result DeviceMTL::FillFunctionTable(CoreInterface& table) const { table = {}; - Core_Device_PartiallyFillFunctionTableMTL(table); - Core_Buffer_PartiallyFillFunctionTableMTL(table); - Core_CommandAllocator_PartiallyFillFunctionTableMTL(table); - Core_CommandBuffer_PartiallyFillFunctionTableMTL(table); - Core_CommandQueue_PartiallyFillFunctionTableMTL(table); - Core_Descriptor_PartiallyFillFunctionTableMTL(table); - Core_DescriptorPool_PartiallyFillFunctionTableMTL(table); - Core_DescriptorSet_PartiallyFillFunctionTableMTL(table); - Core_Fence_PartiallyFillFunctionTableMTL(table); - Core_QueryPool_PartiallyFillFunctionTableMTL(table); - Core_Texture_PartiallyFillFunctionTableMTL(table); - return ValidateFunctionTable(table); + // Core_Device_PartiallyFillFunctionTableMTL(table); + // Core_Buffer_PartiallyFillFunctionTableMTL(table); + // Core_CommandAllocator_PartiallyFillFunctionTableMTL(table); + // Core_CommandBuffer_PartiallyFillFunctionTableMTL(table); + // Core_CommandQueue_PartiallyFillFunctionTableMTL(table); + // Core_Descriptor_PartiallyFillFunctionTableMTL(table); + // Core_DescriptorPool_PartiallyFillFunctionTableMTL(table); + // Core_DescriptorSet_PartiallyFillFunctionTableMTL(table); + // Core_Fence_PartiallyFillFunctionTableMTL(table); + // Core_QueryPool_PartiallyFillFunctionTableMTL(table); + // Core_Texture_PartiallyFillFunctionTableMTL(table); + // return ValidateFunctionTable(table); + return Result::UNSUPPORTED; } #pragma endregion @@ -251,7 +252,7 @@ Result DeviceMTL::FillFunctionTable(ResourceAllocatorInterface& table) const { return Result::UNSUPPORTED; } -Define_Core_Device_PartiallyFillFunctionTable(MTL); +//Dfine_Core_Device_PartiallyFillFunctionTable(MTL); //Define_Helper_Device_PartiallyFillFunctionTable(MTL); //Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); //Define_Streamer_Device_PartiallyFillFunctionTable(MTL); diff --git a/Source/Metal/ImplMTL.mm b/Source/Metal/ImplMTL.mm new file mode 100644 index 00000000..e69de29b diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index 49d9876f..f5002f7e 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -46,17 +46,24 @@ const ShaderDesc& shader = graphicsPipelineDesc.shaders[i]; m_usedBits |= shader.stage; - dispatch_data_t byteCode = - dispatch_data_create(shader.bytecode, shader.size, nil, - DISPATCH_DATA_DESTRUCTOR_DEFAULT); - id lib = [m_Device newLibraryWithData:byteCode error:nil]; - + dispatch_data_t byteCode = dispatch_data_create(shader.bytecode, shader.size, nil, DISPATCH_DATA_DESTRUCTOR_DEFAULT); + NSError *error; + id lib = [m_Device newLibraryWithData:byteCode error:&error]; + NSCAssert(lib, @"Failed to load Metal shader library %@", error); // not sure how to correctly report this + RETURN_ON_FAILURE(&m_Device, lib, Result::FAILURE, "Failed to Load Metal shader library"); + // Create a MTLFunction from the loaded MTLLibrary. NSString *entryPointNStr = [lib functionNames][0]; if (shader.entryPointName) { - entryPointNStr = - [[NSString alloc] initWithUTF8String:shader.entryPointName]; + entryPointNStr = [[NSString alloc] initWithUTF8String:shader.entryPointName]; + } + id entryPointFunc = [lib newFunctionWithName:entryPointNStr]; + if(shader.stage & nri::StageBits::VERTEX_SHADER) { + renderPipelineDesc.vertexFunction = entryPointFunc; + } else if(shader.stage & nri::StageBits::FRAGMENT_SHADER) { + renderPipelineDesc.fragmentFunction = entryPointFunc; } + } // Depth-stencil const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth; @@ -65,13 +72,20 @@ const PipelineLayout *pl = graphicsPipelineDesc.pipelineLayout; const VertexInputDesc *vi = graphicsPipelineDesc.vertexInput; if (vi) { + MTLVertexDescriptor *vertexDescriptor = [MTLVertexDescriptor new]; + for(size_t attIdx = 0; attIdx < vi->attributeNum; attIdx++) { + const nri::VertexAttributeDesc* attrib = &vi->attributes[attIdx]; + vertexDescriptor.attributes[attrib->mtl.location].offset = attrib->offset; + vertexDescriptor.attributes[attrib->mtl.location].format = GetVertexFormatMTL(attrib->format); + vertexDescriptor.attributes[attrib->mtl.location].bufferIndex = attrib->streamIndex; + } - // VkVertexInputBindingDescription* streams const_cast(vertexInputState.pVertexBindingDescriptions); - //for (uint32_t i = 0; i < vi->streamNum; i++) { - // const VertexStreamDesc &stream = vi->streams[i]; - // renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stride = stream.stride; - // renderPipelineDesc.vertexDescriptor.layouts[attribute_desc.streamIndex].stepRate = 1; - // } + for(size_t layoutIdx = 0; layoutIdx < vi->streamNum; layoutIdx++) { + const nri::VertexStreamDesc* stream = &vi->streams[layoutIdx]; + vertexDescriptor.layouts[stream->bindingSlot].stride = stream->stride; + vertexDescriptor.layouts[stream->bindingSlot].stepRate = static_cast(stream->stepRate); + } + renderPipelineDesc.vertexDescriptor = vertexDescriptor; } renderPipelineDesc.inputPrimitiveTopology = GetTopologyMTL(ia.topology); @@ -86,7 +100,7 @@ const OutputMergerDesc& om = graphicsPipelineDesc.outputMerger; for (uint32_t i = 0; i < om.colorNum; i++) { - const ColorAttachmentDesc& attachmentDesc = om.color[i]; + const ColorAttachmentDesc& attachmentDesc = om.colors[i]; renderPipelineDesc.colorAttachments[i].pixelFormat = GetFormatMTL(attachmentDesc.format, false); renderPipelineDesc.colorAttachments[i].blendingEnabled = attachmentDesc.blendEnabled; From b8df84fe5dbd578baf672b7298cc9238aca514ff Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Mon, 18 Nov 2024 21:05:03 -0800 Subject: [PATCH 20/27] feat: update desciprot and textureo Signed-off-by: Michael Pollind --- Source/Metal/CommandAllocatorMTL.h | 4 +++- Source/Metal/CommandAllocatorMTL.mm | 5 +++-- Source/Metal/CommandBufferMTL.mm | 26 +++++++++++++++++++++--- Source/Metal/DescriptorMTL.h | 26 +++++++++++++++++------- Source/Metal/DescriptorMTL.mm | 31 +++++++++++++++++++++++++++++ Source/Metal/PipelineMTL.mm | 5 ++++- Source/Metal/TextureMTL.h | 7 +++---- 7 files changed, 86 insertions(+), 18 deletions(-) diff --git a/Source/Metal/CommandAllocatorMTL.h b/Source/Metal/CommandAllocatorMTL.h index d10a47a7..127c7adf 100644 --- a/Source/Metal/CommandAllocatorMTL.h +++ b/Source/Metal/CommandAllocatorMTL.h @@ -25,11 +25,13 @@ struct CommandAllocatorMTL { // NRI //================================================================================================================ + void SetDebugName(const char* name); Result CreateCommandBuffer(CommandBuffer*& commandBuffer); + void Reset(); private: - struct CommandQueueMTL* m_CommandQueue; DeviceMTL& m_Device; + struct CommandQueueMTL* m_CommandQueue; }; } diff --git a/Source/Metal/CommandAllocatorMTL.mm b/Source/Metal/CommandAllocatorMTL.mm index 9532664f..9fc54bde 100644 --- a/Source/Metal/CommandAllocatorMTL.mm +++ b/Source/Metal/CommandAllocatorMTL.mm @@ -1,7 +1,7 @@ #include "SharedMTL.h" #include "CommandAllocatorMTL.h" - +#include "CommandBufferMTL.h" #include "CommandQueueMTL.h" using namespace nri; @@ -20,8 +20,9 @@ //================================================================================================================ Result CommandAllocatorMTL::CreateCommandBuffer(CommandBuffer*& commandBuffer) { - + CommandBufferMTL* commandBufferImpl = Allocate(m_Device.GetStdAllocator(), m_Device); + commandBuffer = (CommandBuffer*)commandBufferImpl; return Result::SUCCESS; } diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 8cbfb0ab..5cf14718 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -23,7 +23,7 @@ } Result CommandBufferMTL::Begin(const DescriptorPool* descriptorPool) { - [m_Handle computeCommandEncoderWithDescriptor: NULL]; + m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL]; } Result CommandBufferMTL::End() { @@ -93,15 +93,32 @@ } void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; + for(uint32_t i = 0; i < attachmentsDesc.colorNum; i++) { + DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.colors[i]; + + renderPassDescriptor.colorAttachments[i].texture = descriptorMTL.GetImageView() ; + renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); + renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionClear; + renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; + + } + + if(attachmentsDesc.depthStencil) { + DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.depthStencil; + renderPassDescriptor.depthAttachment.texture = descriptorMTL.GetImageView(); + // renderPassDescriptor.depthAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); + renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; + renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; + } //renderPassDescriptor.colorAttachments[ //renderPassDescriptor.colorAttachments - m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; + m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDescriptor]; } void CommandBufferMTL::EndRendering() { [m_RendererEncoder endEncoding]; @@ -110,8 +127,11 @@ } void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) { //MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum); + + Scratch mtlViewports = AllocateScratch(m_Device, MTLViewport, viewportNum); + - // [m_RendererEncoder setViewports:<#(const MTLViewport * _Nonnull)#> count:<#(NSUInteger)#> + [m_RendererEncoder setViewports:mtlViewports count:viewportNum]; } void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) { NSCAssert(m_RendererEncoder, @"encoder set"); diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index 0cce5fea..0977fb38 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -6,20 +6,32 @@ namespace nri { struct DeviceMTL; +enum class DescriptorTypeMTL { + NONE, + IMAGE_VIEW +}; struct DescriptorMTL { - +public: inline DescriptorMTL (DeviceMTL& device) : m_Device(device) { } - - inline Result Create(const Texture1DViewDesc& textureViewDesc) {} - inline Result Create(const Texture2DViewDesc& textureViewDesc){} - inline Result Create(const Texture3DViewDesc& textureViewDesc){} - inline Result Create(const SamplerDesc& samplerDesc){} - + + inline id GetImageView() { + return m_texture; + } + + + Result Create(const BufferViewDesc& bufferViewDesc); + Result Create(const Texture1DViewDesc& textureViewDesc); + Result Create(const Texture2DViewDesc& textureViewDesc); + Result Create(const Texture3DViewDesc& textureViewDesc); + Result Create(const SamplerDesc& samplerDesc); private: DeviceMTL& m_Device; + + id m_texture; + DescriptorTypeMTL m_type = DescriptorTypeMTL::NONE; }; diff --git a/Source/Metal/DescriptorMTL.mm b/Source/Metal/DescriptorMTL.mm index e69de29b..595a5d39 100644 --- a/Source/Metal/DescriptorMTL.mm +++ b/Source/Metal/DescriptorMTL.mm @@ -0,0 +1,31 @@ +#include "SharedMTL.h" + +#include "DescriptorMTL.h" + +#include "BufferMTL.h" + +namespace nri { + +Result DescriptorMTL::Create(const BufferViewDesc& bufferViewDesc) { + // m_Type = DescriptorTypeVK::BUFFER_VIEW; + const BufferMTL& buffer = *(const BufferMTL*)bufferViewDesc.buffer; + + return Result::SUCCESS; + +} +Result DescriptorMTL::Create(const Texture1DViewDesc& textureViewDesc) { + return Result::SUCCESS; + +} +Result DescriptorMTL::Create(const Texture2DViewDesc& textureViewDesc) { + + return Result::SUCCESS; +} +Result DescriptorMTL::Create(const Texture3DViewDesc& textureViewDesc){ + return Result::SUCCESS; +} +Result DescriptorMTL::Create(const SamplerDesc& samplerDesc){ + return Result::SUCCESS; +} + +} diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index f5002f7e..9a74abc0 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -63,7 +63,6 @@ } else if(shader.stage & nri::StageBits::FRAGMENT_SHADER) { renderPipelineDesc.fragmentFunction = entryPointFunc; } - } // Depth-stencil const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth; @@ -92,7 +91,11 @@ if (graphicsPipelineDesc.multisample) { // TODO: multisampling + // } + + // renderPipelineDesc.rasterSampleCount = pCreateInfo->pMultisampleState->pSampleMask[0]; + // Blending diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index a6a4f1ea..a88c981d 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -14,9 +14,9 @@ struct TextureMTL { } ~TextureMTL(); - //inline id GetHandle() const { - // return m_Handle; - //} + inline id GetHandle() const { + return m_Handle; + } inline DeviceMTL& GetDevice() const { return m_Device; @@ -27,7 +27,6 @@ struct TextureMTL { } Result Create(const TextureDesc& textureDesc); - //Result Create(const TextureMTLDesc& textureDesc); private: // Result CreateFromTextureDesc(const TextureDesc& textureDesc); From 78d762be4bbfe915ffc493987a357dc59c0b8bf2 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 20 Nov 2024 21:25:31 -0800 Subject: [PATCH 21/27] feat: add command buffer impl progress Signed-off-by: Michael Pollind --- Source/Metal/BufferMTL.h | 5 +- Source/Metal/BufferMTL.mm | 19 ++ Source/Metal/CommandBufferMTL.h | 5 + Source/Metal/CommandBufferMTL.mm | 12 +- Source/Metal/CommandQueueMTL.h | 4 +- Source/Metal/CommandQueueMTL.mm | 16 +- Source/Metal/DeviceMTL.h | 28 +-- Source/Metal/ImplMTL.mm | 380 +++++++++++++++++++++++++++++++ 8 files changed, 449 insertions(+), 20 deletions(-) diff --git a/Source/Metal/BufferMTL.h b/Source/Metal/BufferMTL.h index 63be8c84..dac53dd0 100644 --- a/Source/Metal/BufferMTL.h +++ b/Source/Metal/BufferMTL.h @@ -18,7 +18,6 @@ struct BufferMTL { return pBuffer; } - inline DeviceMTL& GetDevice() const { return m_Device; } @@ -27,6 +26,10 @@ struct BufferMTL { return m_Desc; } + void* Map(uint64_t offset, uint64_t size); + void Unmap(); + void SetDebugName(const char* name); + ~BufferMTL(); Result Create(const BufferDesc& bufferDesc); diff --git a/Source/Metal/BufferMTL.mm b/Source/Metal/BufferMTL.mm index 38e4759e..70d623e4 100644 --- a/Source/Metal/BufferMTL.mm +++ b/Source/Metal/BufferMTL.mm @@ -13,6 +13,25 @@ return Result::SUCCESS; } + +void* BufferMTL::Map(uint64_t offset, uint64_t size) { + return (uint8_t*)[pBuffer contents] + offset; +} + +void BufferMTL::Unmap() { + +} + + Result BufferMTL::Create(const AllocateBufferDesc& bufferDesc) { return Result::SUCCESS; } + +void BufferMTL::SetDebugName(const char* name) { + + NSString* str = [NSString stringWithUTF8String:name]; + + + //[pBuffer addDebugMarker:name range:] + +} diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index ac450a64..7034aa92 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -30,6 +30,11 @@ struct CommandBufferMTL { return m_Device; } + inline operator id() const { + return m_Handle; + } + + void SetDebugName(const char* name); Result Begin(const DescriptorPool* descriptorPool); Result End(); diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 5cf14718..cffe549f 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -121,15 +121,21 @@ m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDescriptor]; } void CommandBufferMTL::EndRendering() { + NSCAssert(m_RendererEncoder, @"Renderer Encoderer Not Set"); [m_RendererEncoder endEncoding]; m_RendererEncoder = nil; m_ComputeEncoder = nil; } void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) { - //MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum); - Scratch mtlViewports = AllocateScratch(m_Device, MTLViewport, viewportNum); - + for(size_t i = 0; i < viewportNum; i++) { + mtlViewports[i].originX = viewports[i].x; + mtlViewports[i].originY = viewports[i].y; + mtlViewports[i].width = viewports[i].width; + mtlViewports[i].height = viewports[i].height; + mtlViewports[i].znear = viewports[i].depthMin; + mtlViewports[i].zfar = viewports[i].depthMax; + } [m_RendererEncoder setViewports:mtlViewports count:viewportNum]; } diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h index 3be48381..0486e732 100644 --- a/Source/Metal/CommandQueueMTL.h +++ b/Source/Metal/CommandQueueMTL.h @@ -38,9 +38,11 @@ struct CommandQueueMTL { return m_Lock; } - Result WaitForIdle(); void SetDebugName(const char* name); + void Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain); + Result WaitForIdle(); + Result Create(CommandQueueType type); QueueBarrierBits m_BarrierBits = QueueBarrierBits::NONE; diff --git a/Source/Metal/CommandQueueMTL.mm b/Source/Metal/CommandQueueMTL.mm index 1eb63051..6ad24150 100644 --- a/Source/Metal/CommandQueueMTL.mm +++ b/Source/Metal/CommandQueueMTL.mm @@ -3,6 +3,7 @@ #include "SharedMTL.h" #include "CommandQueueMTL.h" +#include "CommandBufferMTL.h" using namespace nri; @@ -24,7 +25,20 @@ return Result::SUCCESS; } -inline void CommandQueueMTL::SetDebugName(const char* name) { + +void CommandQueueMTL::Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain) { + + for(uint32_t i = 0; i < queueSubmitDesc.commandBufferNum; i++) { + id cmd = *(struct CommandBufferMTL*)queueSubmitDesc.commandBuffers[i]; + [cmd commit]; + + } + + +} + + +void CommandQueueMTL::SetDebugName(const char* name) { [m_Handle setLabel:[NSString stringWithUTF8String:name]]; } diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index b6027113..67f3d347 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -16,13 +16,7 @@ struct DeviceMTL final : public DeviceBase { inline operator id() const { return m_Device; } - - inline const DeviceDesc& GetDesc() const { - return m_Desc; - } - void Destruct() override; - template inline Result CreateImplementation(Interface*& entity, const Args&... args) { Implementation* impl = Allocate(GetStdAllocator(), *this); @@ -41,14 +35,20 @@ struct DeviceMTL final : public DeviceBase { void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); void GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); - Result FillFunctionTable(CoreInterface& table) const; - Result FillFunctionTable(HelperInterface& table) const; - Result FillFunctionTable(LowLatencyInterface& table) const; - Result FillFnctionTable(MeshShaderInterface& table) const; - Result FillFunctionTable(RayTracingInterface& table) const; - Result FillFunctionTable(StreamerInterface& table) const; - Result FillFunctionTable(SwapChainInterface& table) const; - Result FillFunctionTable(ResourceAllocatorInterface& table) const; + + const DeviceDesc& GetDesc() const override { + return m_Desc; + } + void Destruct() override; + + Result FillFunctionTable(CoreInterface& table) const override; + Result FillFunctionTable(HelperInterface& table) const override; + Result FillFunctionTable(LowLatencyInterface& table) const override; + Result FillFunctionTable(MeshShaderInterface& table) const override; + Result FillFunctionTable(RayTracingInterface& table) const override; + Result FillFunctionTable(StreamerInterface& table) const override; + Result FillFunctionTable(SwapChainInterface& table) const override; + Result FillFunctionTable(ResourceAllocatorInterface& table) const override; Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper); private: diff --git a/Source/Metal/ImplMTL.mm b/Source/Metal/ImplMTL.mm index e69de29b..cdb30b9a 100644 --- a/Source/Metal/ImplMTL.mm +++ b/Source/Metal/ImplMTL.mm @@ -0,0 +1,380 @@ +// © 2021 NVIDIA Corporation + +#include "SharedMTL.h" + +using namespace nri; + +#include "BufferMTL.h" +#include "CommandAllocatorMTL.h" +#include "CommandBufferMTL.h" +#include "DeviceMTL.h" + +Result CreateDeviceMTL(const DeviceCreationDesc& desc, DeviceBase*& device) { + StdAllocator allocator(desc.allocationCallbacks); + DeviceMTL* impl = Allocate(allocator, desc.callbackInterface, allocator); + Result result = impl->Create(desc, {}, false); + + if (result != Result::SUCCESS) { + Destroy(allocator, impl); + device = nullptr; + } else + device = (DeviceBase*)impl; + return result; +} + + +Result CreateDeviceMTL(const DeviceCreationMTLDesc& desc, DeviceBase*& device) { + return Result::SUCCESS; +} +//============================================================================================================================================================================================ +#pragma region[ Core ] + + +static void NRI_CALL SetBufferDebugName(Buffer& buffer, const char* name) { + ((BufferMTL&)buffer).SetDebugName(name); +} + +static uint64_t NRI_CALL GetBufferNativeObject(const Buffer& buffer) { + if (!(&buffer)) + return 0; + return uint64_t(((BufferMTL&)buffer).GetHandle()); +} + + +static void* NRI_CALL MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) { + return ((BufferMTL&)buffer).Map(offset, size); +} + +static void NRI_CALL UnmapBuffer(Buffer& buffer) { + ((BufferMTL&)buffer).Unmap(); +} + +static void NRI_CALL SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) { + // ((CommandAllocatorVK&)commandAllocator).SetDebugName(name); +} + +static Result NRI_CALL CreateCommandBuffer(CommandAllocator& commandAllocator, CommandBuffer*& commandBuffer) { + return ((CommandAllocatorMTL&)commandAllocator).CreateCommandBuffer(commandBuffer); +} + +static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) { + // ((CommandAllocatorVK&)commandAllocator).Reset(); +} + +static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { + // ((CommandBufferVK&)commandBuffer).SetDebugName(name); +} + +static Result NRI_CALL BeginCommandBuffer(CommandBuffer& commandBuffer, const DescriptorPool* descriptorPool) { + return ((CommandBufferMTL&)commandBuffer).Begin(descriptorPool); +} + +static Result NRI_CALL EndCommandBuffer(CommandBuffer& commandBuffer) { + return ((CommandBufferMTL&)commandBuffer).End(); +} + +static void NRI_CALL CmdSetPipelineLayout(CommandBuffer& commandBuffer, const PipelineLayout& pipelineLayout) { + ((CommandBufferMTL&)commandBuffer).SetPipelineLayout(pipelineLayout); +} + +static void NRI_CALL CmdSetPipeline(CommandBuffer& commandBuffer, const Pipeline& pipeline) { + ((CommandBufferMTL&)commandBuffer).SetPipeline(pipeline); +} + +static void NRI_CALL CmdBarrier(CommandBuffer& commandBuffer, const BarrierGroupDesc& barrierGroupDesc) { + ((CommandBufferMTL&)commandBuffer).Barrier(barrierGroupDesc); +} + +static void NRI_CALL CmdSetDescriptorPool(CommandBuffer& commandBuffer, const DescriptorPool& descriptorPool) { + ((CommandBufferMTL&)commandBuffer).SetDescriptorPool(descriptorPool); +} + +static void NRI_CALL CmdSetDescriptorSet(CommandBuffer& commandBuffer, uint32_t setIndex, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { + ((CommandBufferMTL&)commandBuffer).SetDescriptorSet(setIndex, descriptorSet, dynamicConstantBufferOffsets); +} + +static void NRI_CALL CmdSetRootConstants(CommandBuffer& commandBuffer, uint32_t rootConstantIndex, const void* data, uint32_t size) { + //((CommandBufferMTL&)commandBuffer).SetRootConstants(rootConstantIndex, data, size); +} + +static void NRI_CALL CmdSetRootDescriptor(CommandBuffer& commandBuffer, uint32_t rootDescriptorIndex, Descriptor& descriptor) { + //((CommandBufferMTL&)commandBuffer).SetRootDescriptor(rootDescriptorIndex, descriptor); +} + +static void NRI_CALL CmdBeginRendering(CommandBuffer& commandBuffer, const AttachmentsDesc& attachmentsDesc) { + ((CommandBufferMTL&)commandBuffer).BeginRendering(attachmentsDesc); +} + +static void NRI_CALL CmdEndRendering(CommandBuffer& commandBuffer) { + ((CommandBufferMTL&)commandBuffer).EndRendering(); +} + +static void NRI_CALL CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) { + ((CommandBufferMTL&)commandBuffer).SetViewports(viewports, viewportNum); +} + +static void NRI_CALL CmdSetScissors(CommandBuffer& commandBuffer, const nri::Rect* rects, uint32_t rectNum) { + ((CommandBufferMTL&)commandBuffer).SetScissors(rects, rectNum); +} + +static void NRI_CALL CmdSetDepthBounds(CommandBuffer& commandBuffer, float boundsMin, float boundsMax) { + ((CommandBufferMTL&)commandBuffer).SetDepthBounds(boundsMin, boundsMax); +} + +static void NRI_CALL CmdSetStencilReference(CommandBuffer& commandBuffer, uint8_t frontRef, uint8_t backRef) { + ((CommandBufferMTL&)commandBuffer).SetStencilReference(frontRef, backRef); +} + +static void NRI_CALL CmdSetSampleLocations(CommandBuffer& commandBuffer, const SampleLocation* locations, Sample_t locationNum, Sample_t sampleNum) { + //((CommandBufferMTL&)commandBuffer).SetSampleLocations(locations, locationNum, sampleNum); +} + +static void NRI_CALL CmdSetBlendConstants(CommandBuffer& commandBuffer, const Color32f& color) { + ((CommandBufferMTL&)commandBuffer).SetBlendConstants(color); +} + +static void NRI_CALL CmdSetShadingRate(CommandBuffer& commandBuffer, const ShadingRateDesc& shadingRateDesc) { + ((CommandBufferMTL&)commandBuffer).SetShadingRate(shadingRateDesc); +} + +static void NRI_CALL CmdSetDepthBias(CommandBuffer& commandBuffer, const DepthBiasDesc& depthBiasDesc) { + // ((CommandBufferMTL&)commandBuffer).SetDepthBias(depthBiasDesc); +} + +static void NRI_CALL CmdClearAttachments(CommandBuffer& commandBuffer, const ClearDesc* clearDescs, uint32_t clearDescNum, const nri::Rect* rects, uint32_t rectNum) { + //((CommandBufferMTL&)commandBuffer).ClearAttachments(clearDescs, clearDescNum, rects, rectNum); +} + +static void NRI_CALL CmdSetIndexBuffer(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, IndexType indexType) { + ((CommandBufferMTL&)commandBuffer).SetIndexBuffer(buffer, offset, indexType); +} + +static void NRI_CALL CmdSetVertexBuffers(CommandBuffer& commandBuffer, uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { + ((CommandBufferMTL&)commandBuffer).SetVertexBuffers(baseSlot, bufferNum, buffers, offsets); +} + +static void NRI_CALL CmdDraw(CommandBuffer& commandBuffer, const DrawDesc& drawDesc) { + ((CommandBufferMTL&)commandBuffer).Draw(drawDesc); +} + +static void NRI_CALL CmdDrawIndexed(CommandBuffer& commandBuffer, const DrawIndexedDesc& drawIndexedDesc) { + ((CommandBufferMTL&)commandBuffer).DrawIndexed(drawIndexedDesc); +} + +static void NRI_CALL CmdDrawIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + ((CommandBufferMTL&)commandBuffer).DrawIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); +} + +static void NRI_CALL CmdDrawIndexedIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + ((CommandBufferMTL&)commandBuffer).DrawIndexedIndirect(buffer, offset, drawNum, stride, countBuffer, countBufferOffset); +} + +static void NRI_CALL CmdDispatch(CommandBuffer& commandBuffer, const DispatchDesc& dispatchDesc) { + ((CommandBufferMTL&)commandBuffer).Dispatch(dispatchDesc); +} + +static void NRI_CALL CmdDispatchIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset) { + ((CommandBufferMTL&)commandBuffer).DispatchIndirect(buffer, offset); +} + +static void NRI_CALL CmdBeginQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { + ((CommandBufferMTL&)commandBuffer).BeginQuery(queryPool, offset); +} + +static void NRI_CALL CmdEndQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { + ((CommandBufferMTL&)commandBuffer).EndQuery(queryPool, offset); +} + +static void NRI_CALL CmdBeginAnnotation(CommandBuffer& commandBuffer, const char* name) { + ((CommandBufferMTL&)commandBuffer).BeginAnnotation(name); +} + +static void NRI_CALL CmdEndAnnotation(CommandBuffer& commandBuffer) { + ((CommandBufferMTL&)commandBuffer).EndAnnotation(); +} + +static void NRI_CALL CmdClearStorageBuffer(CommandBuffer& commandBuffer, const ClearStorageBufferDesc& clearDesc) { + ((CommandBufferMTL&)commandBuffer).ClearStorageBuffer(clearDesc); +} + +static void NRI_CALL CmdClearStorageTexture(CommandBuffer& commandBuffer, const ClearStorageTextureDesc& clearDesc) { + ((CommandBufferMTL&)commandBuffer).ClearStorageTexture(clearDesc); +} + +static void NRI_CALL CmdResolveTexture(CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) { + //((CommandBufferMTL&)commandBuffer).ResolveTexture(dstTexture, dstRegionDesc, srcTexture, srcRegionDesc); +} + +static void NRI_CALL CmdCopyBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) { + ((CommandBufferMTL&)commandBuffer).CopyBuffer(dstBuffer, dstOffset, srcBuffer, srcOffset, size); +} + +static void NRI_CALL CmdCopyTexture( + CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) { + ((CommandBufferMTL&)commandBuffer).CopyTexture(dstTexture, dstRegionDesc, srcTexture, srcRegionDesc); +} + +static void NRI_CALL CmdUploadBufferToTexture(CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { + ((CommandBufferMTL&)commandBuffer).UploadBufferToTexture(dstTexture, dstRegionDesc, srcBuffer, srcDataLayoutDesc); +} + +static void NRI_CALL CmdReadbackTextureToBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, const TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { + //((CommandBufferMTL&)commandBuffer).ReadbackTextureToBuffer(dstBuffer, dstDataLayoutDesc, srcTexture, srcRegionDesc); +} + +static void NRI_CALL CmdCopyQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) { + ((CommandBufferMTL&)commandBuffer).CopyQueries(queryPool, offset, num, dstBuffer, dstOffset); +} + +static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num) { + ((CommandBufferMTL&)commandBuffer).ResetQueries(queryPool, offset, num); +} + +static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { + return ((DeviceMTL&)device).GetDesc(); +} + +//static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& workSubmissionDesc) { +// ((CommandQueueMTL&)commandQueue).Submit(workSubmissionDesc, nullptr); +//} + +Result DeviceMTL::FillFunctionTable(CoreInterface& table) const { + table.GetDeviceDesc = ::GetDeviceDesc; + //table.GetBufferDesc = ::GetBufferDesc; + //table.GetTextureDesc = ::GetTextureDesc; + //table.GetFormatSupport = ::GetFormatSupport; + //table.GetQuerySize = ::GetQuerySize; + //table.GetBufferMemoryDesc = ::GetBufferMemoryDesc; + //table.GetTextureMemoryDesc = ::GetTextureMemoryDesc; + //table.GetCommandQueue = ::GetCommandQueue; + //table.CreateCommandAllocator = ::CreateCommandAllocator; + //table.CreateCommandBuffer = ::CreateCommandBuffer; + //table.CreateDescriptorPool = ::CreateDescriptorPool; + //table.CreateBuffer = ::CreateBuffer; + //table.CreateTexture = ::CreateTexture; + //table.CreateBufferView = ::CreateBufferView; + //table.CreateTexture1DView = ::CreateTexture1DView; + //table.CreateTexture2DView = ::CreateTexture2DView; + //table.CreateTexture3DView = ::CreateTexture3DView; + //table.CreateSampler = ::CreateSampler; + //table.CreatePipelineLayout = ::CreatePipelineLayout; + //table.CreateGraphicsPipeline = ::CreateGraphicsPipeline; +// table.CreateComputePipeline = ::CreateComputePipeline; +// table.CreateQueryPool = ::CreateQueryPool; +// table.CreateFence = ::CreateFence; +// table.DestroyCommandAllocator = ::DestroyCommandAllocator; +// table.DestroyCommandBuffer = ::DestroyCommandBuffer; +// table.DestroyDescriptorPool = ::DestroyDescriptorPool; +// table.DestroyBuffer = ::DestroyBuffer; +// table.DestroyTexture = ::DestroyTexture; +// table.DestroyDescriptor = ::DestroyDescriptor; +// table.DestroyPipelineLayout = ::DestroyPipelineLayout; +// table.DestroyPipeline = ::DestroyPipeline; +// table.DestroyQueryPool = ::DestroyQueryPool; +// table.DestroyFence = ::DestroyFence; +// table.AllocateMemory = ::AllocateMemory; +// table.BindBufferMemory = ::BindBufferMemory; +// table.BindTextureMemory = ::BindTextureMemory; +// table.FreeMemory = ::FreeMemory; + table.BeginCommandBuffer = ::BeginCommandBuffer; + table.CmdSetDescriptorPool = ::CmdSetDescriptorPool; + table.CmdSetDescriptorSet = ::CmdSetDescriptorSet; + table.CmdSetPipelineLayout = ::CmdSetPipelineLayout; + table.CmdSetPipeline = ::CmdSetPipeline; + table.CmdSetRootConstants = ::CmdSetRootConstants; + table.CmdSetRootDescriptor = ::CmdSetRootDescriptor; + table.CmdBarrier = ::CmdBarrier; + table.CmdSetIndexBuffer = ::CmdSetIndexBuffer; + table.CmdSetVertexBuffers = ::CmdSetVertexBuffers; + table.CmdSetViewports = ::CmdSetViewports; + table.CmdSetScissors = ::CmdSetScissors; + table.CmdSetStencilReference = ::CmdSetStencilReference; + table.CmdSetDepthBounds = ::CmdSetDepthBounds; + table.CmdSetBlendConstants = ::CmdSetBlendConstants; + table.CmdSetSampleLocations = ::CmdSetSampleLocations; + table.CmdSetShadingRate = ::CmdSetShadingRate; + table.CmdSetDepthBias = ::CmdSetDepthBias; + table.CmdBeginRendering = ::CmdBeginRendering; + table.CmdClearAttachments = ::CmdClearAttachments; + table.CmdDraw = ::CmdDraw; + table.CmdDrawIndexed = ::CmdDrawIndexed; + table.CmdDrawIndirect = ::CmdDrawIndirect; + table.CmdDrawIndexedIndirect = ::CmdDrawIndexedIndirect; + table.CmdEndRendering = ::CmdEndRendering; + table.CmdDispatch = ::CmdDispatch; + table.CmdDispatchIndirect = ::CmdDispatchIndirect; + table.CmdCopyBuffer = ::CmdCopyBuffer; + table.CmdCopyTexture = ::CmdCopyTexture; + table.CmdUploadBufferToTexture = ::CmdUploadBufferToTexture; + table.CmdReadbackTextureToBuffer = ::CmdReadbackTextureToBuffer; + table.CmdClearStorageBuffer = ::CmdClearStorageBuffer; + table.CmdClearStorageTexture = ::CmdClearStorageTexture; + table.CmdResolveTexture = ::CmdResolveTexture; + table.CmdResetQueries = ::CmdResetQueries; + table.CmdBeginQuery = ::CmdBeginQuery; + table.CmdEndQuery = ::CmdEndQuery; + table.CmdCopyQueries = ::CmdCopyQueries; + table.CmdBeginAnnotation = ::CmdBeginAnnotation; + table.CmdEndAnnotation = ::CmdEndAnnotation; + table.EndCommandBuffer = ::EndCommandBuffer; +// table.QueueSubmit = ::QueueSubmit; +// table.Wait = ::Wait; +// table.GetFenceValue = ::GetFenceValue; +// table.UpdateDescriptorRanges = ::UpdateDescriptorRanges; +// table.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers; +// table.CopyDescriptorSet = ::CopyDescriptorSet; +// table.AllocateDescriptorSets = ::AllocateDescriptorSets; +// table.ResetDescriptorPool = ::ResetDescriptorPool; + table.ResetCommandAllocator = ::ResetCommandAllocator; + table.MapBuffer = ::MapBuffer; + table.UnmapBuffer = ::UnmapBuffer; +// table.SetDeviceDebugName = ::SetDeviceDebugName; +// table.SetFenceDebugName = ::SetFenceDebugName; +// table.SetDescriptorDebugName = ::SetDescriptorDebugName; +// table.SetPipelineDebugName = ::SetPipelineDebugName; + table.SetCommandBufferDebugName = ::SetCommandBufferDebugName; + table.SetBufferDebugName = ::SetBufferDebugName; +// table.SetTextureDebugName = ::SetTextureDebugName; +// table.SetCommandQueueDebugName = ::SetCommandQueueDebugName; + table.SetCommandAllocatorDebugName = ::SetCommandAllocatorDebugName; +// table.SetDescriptorPoolDebugName = ::SetDescriptorPoolDebugName; +// table.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; +// table.SetQueryPoolDebugName = ::SetQueryPoolDebugName; +// table.SetDescriptorSetDebugName = ::SetDescriptorSetDebugName; +// table.SetMemoryDebugName = ::SetMemoryDebugName; +// table.GetDeviceNativeObject = ::GetDeviceNativeObject; +// table.GetCommandBufferNativeObject = ::GetCommandBufferNativeObject; + table.GetBufferNativeObject = ::GetBufferNativeObject; +// table.GetTextureNativeObject = ::GetTextureNativeObject; +// table.GetDescriptorNativeObject = ::GetDescriptorNativeObject; + + return Result::SUCCESS; +} + + +#pragma endregion + +Result DeviceMTL::FillFunctionTable(HelperInterface& table) const { + return Result::SUCCESS; +} +Result DeviceMTL::FillFunctionTable(LowLatencyInterface& table) const { + + return Result::SUCCESS; +} +Result DeviceMTL::FillFunctionTable(MeshShaderInterface& table) const { + + return Result::SUCCESS; +} +Result DeviceMTL::FillFunctionTable(RayTracingInterface& table) const { + return Result::SUCCESS; +} +Result DeviceMTL::FillFunctionTable(StreamerInterface& table) const { + return Result::SUCCESS; +} +Result DeviceMTL::FillFunctionTable(SwapChainInterface& table) const { + return Result::SUCCESS; +} +Result DeviceMTL::FillFunctionTable(ResourceAllocatorInterface& table) const { + + return Result::SUCCESS; +} From de9ca9c15ca2791b245625973247ec8acf5ed959 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Tue, 26 Nov 2024 21:14:48 -0800 Subject: [PATCH 22/27] feat: add memory mtl Signed-off-by: Michael Pollind --- Include/Extensions/NRIWrapperMTL.h | 14 +- Source/Metal/ConversionMTL.h | 26 +- Source/Metal/DescriptorMTL.h | 2 + Source/Metal/DescriptorMTL.mm | 4 + Source/Metal/DeviceMTL.h | 7 +- Source/Metal/DeviceMTL.hpp | 512 ++++++++++++++--------------- Source/Metal/DeviceMTL.mm | 27 +- Source/Metal/ImplMTL.mm | 26 ++ Source/Metal/MemoryMTL.h | 9 +- Source/Metal/MemoryMTL.mm | 15 +- Source/Metal/SharedMTL.h | 11 +- Source/Metal/SwapChainMTL.h | 11 +- Source/Metal/SwapChainMTL.mm | 15 + Source/Metal/TextureMTL.h | 4 +- Source/Metal/TextureMTL.mm | 28 +- 15 files changed, 410 insertions(+), 301 deletions(-) diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h index 3425a15e..a560999d 100644 --- a/Include/Extensions/NRIWrapperMTL.h +++ b/Include/Extensions/NRIWrapperMTL.h @@ -2,12 +2,17 @@ #pragma once +#include "NRIMacro.h" #include "NRIDeviceCreation.h" + NriNamespaceBegin -typedef void* MTLBufferHandle; -typedef void* MTLTextureHandle; + +NonNriForwardStruct(MTLHeap); + +typedef void* MTLBufferHandle; // id +typedef void* MTLTextureHandle; typedef void* MTLDeviceHandle; NriStruct(DeviceCreationMTLDesc) @@ -36,8 +41,9 @@ NriStruct(TextureMTLDesc) NriStruct(MemoryMTLDesc) { - MTLBufferHandle buffer; - void* mappedMemory; + uint64_t size; + // MTLStorageMode storage; + //MTLResourceOptions options; }; diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index 7d29b471..64a45c68 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -4,6 +4,28 @@ namespace nri { +constexpr std::array DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION ={ + MTLResourceStorageModePrivate, // DEVICE + MTLResourceStorageModeShared | MTLResourceCPUCacheModeWriteCombined, // DEVICE_UPLOAD + MTLResourceStorageModeShared | MTLResourceCPUCacheModeWriteCombined, // HOST_UPLOAD + MTLResourceStorageModeShared | MTLResourceCPUCacheModeDefaultCache // HOST_READBACK +}; + +constexpr std::array DEFAULT_CACHE_MODE_MEMORY_LOCATION ={ + MTLCPUCacheModeDefaultCache, // DEVICE + MTLCPUCacheModeWriteCombined, // DEVICE_UPLOAD + MTLCPUCacheModeWriteCombined, // HOST_UPLOAD + MTLCPUCacheModeDefaultCache // HOST_READBACK +}; + +constexpr std::array DEFAULT_STORAGE_MODE_MEMORY_LOCATION ={ + MTLStorageModePrivate, // DEVICE + MTLStorageModeShared, // DEVICE_UPLOAD + MTLStorageModeShared, // HOST_UPLOAD + MTLStorageModeShared // HOST_READBACK +}; + + constexpr std::array MTL_NRI_FORMAT_TO_VERTEX_FORMAT = { MTLVertexFormatInvalid, // UNKNOWN MTLVertexFormatUCharNormalized, // R8_UNORM @@ -208,8 +230,6 @@ constexpr MTLSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) { return SAMPLER_ADDRESS_MODE[(size_t)addressMode]; } - - constexpr std::array TOPOLOGIES_CLASSES = { MTLPrimitiveTopologyClassPoint, // POINT_LIST MTLPrimitiveTopologyClassLine, // LINE_LIST @@ -233,8 +253,6 @@ inline MTLVertexFormat GetVertexFormatMTL(Format format) { return (MTLVertexFormat)MTL_NRI_FORMAT_TO_VERTEX_FORMAT[(size_t)format]; } - - inline MTLPixelFormat GetFormatMTL(Format format, bool demoteSrgb = false) { if (demoteSrgb) { const FormatProps& formatProps = GetFormatProps(format); diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index 0977fb38..5ffe9a1d 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -17,6 +17,8 @@ struct DescriptorMTL { : m_Device(device) { } + ~DescriptorMTL(); + inline id GetImageView() { return m_texture; } diff --git a/Source/Metal/DescriptorMTL.mm b/Source/Metal/DescriptorMTL.mm index 595a5d39..7fabc058 100644 --- a/Source/Metal/DescriptorMTL.mm +++ b/Source/Metal/DescriptorMTL.mm @@ -6,6 +6,10 @@ namespace nri { +DescriptorMTL::~DescriptorMTL() { + +} + Result DescriptorMTL::Create(const BufferViewDesc& bufferViewDesc) { // m_Type = DescriptorTypeVK::BUFFER_VIEW; const BufferMTL& buffer = *(const BufferMTL*)bufferViewDesc.buffer; diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index 67f3d347..e99738f9 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -30,11 +30,12 @@ struct DeviceMTL final : public DeviceBase { return result; } - + + //void GetMemoryTypeInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); void GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); - + //bool GetMemoryTypeInfo(MemoryLocation memoryLocation, MemoryTypeInfo& memoryTypeInfo) const; const DeviceDesc& GetDesc() const override { return m_Desc; @@ -52,7 +53,7 @@ struct DeviceMTL final : public DeviceBase { Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper); private: - Lock m_Lock; + //Lock m_Lock; id m_Device; std::array m_CommandQueues = {}; DeviceDesc m_Desc = {}; diff --git a/Source/Metal/DeviceMTL.hpp b/Source/Metal/DeviceMTL.hpp index cb6d4977..c6dd5d91 100644 --- a/Source/Metal/DeviceMTL.hpp +++ b/Source/Metal/DeviceMTL.hpp @@ -1,260 +1,260 @@ // © 2021 NVIDIA Corporation //Declare_PartiallyFillFunctionTable_Functions(MTL); - -#pragma region[ Core ] - -static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { - return ((const DeviceMTL&)device).GetDesc(); -} - -static const BufferDesc& NRI_CALL GetBufferDesc(const Buffer& buffer) { - return ((const BufferMTL&)buffer).GetDesc(); -} - -static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { - return ((const TextureMTL&)texture).GetDesc(); -} - -static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { - return (FormatSupportBits)0; - //return ((const DeviceDesc&)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) { - //return ((const DeviceMTL&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc); - -} - -static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - return ((DeviceMTL&)device).GetCommandQueue(commandQueueType, commandQueue); -} - -static Result NRI_CALL CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator) { - //DeviceMTL& device = ((CommandQueueD3D12&)commandQueue).GetDevice(); - //return device.CreateImplementation(commandAllocator, commandQueue); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateDescriptorPool(Device& device, const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) { - //return ((DeviceMTL&)device).CreateImplementation(descriptorPool, descriptorPoolDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffer) { - return ((DeviceMTL&)device).CreateImplementation(buffer, bufferDesc); -} - -static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& texture) { - return ((DeviceMTL&)device).CreateImplementation(texture, textureDesc); -} - -static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { - //DeviceMTL& device = ((const BufferD3D12*)bufferViewDesc.buffer)->GetDevice(); - //return device.CreateImplementation(bufferView, bufferViewDesc); - return Result::SUCCESS; -} - - -static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) { - //DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice(); - //return device.CreateImplementation(textureView, textureViewDesc); - - - //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); - //return device.CreateImplementation(textureView, textureViewDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) { - //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); - //return device.CreateImplementation(textureView, textureViewDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) { - //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); - //return device.CreateImplementation(textureView, textureViewDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateSampler(Device& device, const SamplerDesc& samplerDesc, Descriptor*& sampler) { - //return ((DeviceMTL&)device).CreateImplementation(sampler, samplerDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreatePipelineLayout(Device& device, const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) { - //return ((DeviceMTL&)device).CreateImplementation(pipelineLayout, pipelineLayoutDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateGraphicsPipeline(Device& device, const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) { - //return ((DeviceMTL&)device).CreateImplementation(pipeline, graphicsPipelineDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) { - //return ((DeviceMTL&)device).CreateImplementation(pipeline, computePipelineDesc); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) { - //return ((DeviceMTL&)device).CreateImplementation(fence, initialValue); - return Result::SUCCESS; -} - -static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) { - //return ((DeviceMTL&)device).CreateImplementation(queryPool, queryPoolDesc); - return Result::SUCCESS; -} - -static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) { - //Destroy((CommandBufferD3D12*)&commandBuffer); -} - -static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) { - //Destroy((CommandAllocatorD3D12*)&commandAllocator); -} - -static void NRI_CALL DestroyDescriptorPool(DescriptorPool& descriptorPool) { - //Destroy((DescriptorPoolD3D12*)&descriptorPool); -} - -static void NRI_CALL DestroyBuffer(Buffer& buffer) { - //Destroy((BufferD3D12*)&buffer); -} - -static void NRI_CALL DestroyTexture(Texture& texture) { - //Destroy((TextureD3D12*)&texture); -} - -static void NRI_CALL DestroyDescriptor(Descriptor& descriptor) { - //Destroy((DescriptorD3D12*)&descriptor); -} - -static void NRI_CALL DestroyPipelineLayout(PipelineLayout& pipelineLayout) { - //Destroy((PipelineLayoutD3D12*)&pipelineLayout); -} - -static void NRI_CALL DestroyPipeline(Pipeline& pipeline) { - //Destroy((PipelineD3D12*)&pipeline); -} - -static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) { - //Destroy((QueryPoolD3D12*)&queryPool); -} - -static void NRI_CALL DestroyFence(Fence& fence) { - Destroy((FenceMTL*)&fence); -} - -static Result NRI_CALL AllocateMemory(Device& device, const AllocateMemoryDesc& allocateMemoryDesc, Memory*& memory) { - return Result::SUCCESS; - //return ((DeviceMTL&)device).CreateImplementation(memory, allocateMemoryDesc); -} - -static Result NRI_CALL BindBufferMemory(Device& device, const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { - //return ((DeviceMTL&)device).BindBufferMemory(memoryBindingDescs, memoryBindingDescNum); - return Result::SUCCESS; -} - -static Result NRI_CALL BindTextureMemory(Device& device, const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { - //return ((DeviceMTL&)device).BindTextureMemory(memoryBindingDescs, memoryBindingDescNum); - return Result::SUCCESS; -} - -static void NRI_CALL FreeMemory(Memory& memory) { -// Destroy((MemoryD3D12*)&memory); -} - -static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { -// ((DeviceMTL&)device).SetDebugName(name); -} - -static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) { -// ((PipelineD3D12&)pipeline).SetDebugName(name); -} - -static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) { -// ((PipelineLayoutD3D12&)pipelineLayout).SetDebugName(name); -} - -static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) { -// ((MemoryMetal&)memory).SetDebugName(name); -} - -static void* NRI_CALL GetDeviceNativeObject(const Device& device) { - if (!(&device)) - return nullptr; - return (DeviceMTL*)&((DeviceMTL&)device); - - //return ((DeviceMetal&)device).GetNativeObject(); -} - -Result DeviceMTL::FillFunctionTable(CoreInterface& table) const { - table = {}; - // Core_Device_PartiallyFillFunctionTableMTL(table); - // Core_Buffer_PartiallyFillFunctionTableMTL(table); - // Core_CommandAllocator_PartiallyFillFunctionTableMTL(table); - // Core_CommandBuffer_PartiallyFillFunctionTableMTL(table); - // Core_CommandQueue_PartiallyFillFunctionTableMTL(table); - // Core_Descriptor_PartiallyFillFunctionTableMTL(table); - // Core_DescriptorPool_PartiallyFillFunctionTableMTL(table); - // Core_DescriptorSet_PartiallyFillFunctionTableMTL(table); - // Core_Fence_PartiallyFillFunctionTableMTL(table); - // Core_QueryPool_PartiallyFillFunctionTableMTL(table); - // Core_Texture_PartiallyFillFunctionTableMTL(table); - // return ValidateFunctionTable(table); - return Result::UNSUPPORTED; -} - -#pragma endregion - -Result DeviceMTL::FillFunctionTable(HelperInterface& table) const { - table = {}; - return Result::UNSUPPORTED; -} - -Result DeviceMTL::FillFunctionTable(LowLatencyInterface& table) const { - table = {}; - return Result::UNSUPPORTED; -} - -//Result DeviceMTL::FillFunctionTable(MeshShaderInterface& table) const { -// table = {}; -// return Result::UNSUPPORTED; +// +//#pragma region[ Core ] +// +//static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { +// return ((const DeviceMTL&)device).GetDesc(); //} - -Result DeviceMTL::FillFunctionTable(RayTracingInterface& table) const { - table = {}; - return Result::UNSUPPORTED; -} - -Result DeviceMTL::FillFunctionTable(StreamerInterface& table) const { - table = {}; - return Result::UNSUPPORTED; -} - -Result DeviceMTL::FillFunctionTable(SwapChainInterface& table) const { - table = {}; - return Result::UNSUPPORTED; -} - -Result DeviceMTL::FillFunctionTable(ResourceAllocatorInterface& table) const { - table = {}; - return Result::UNSUPPORTED; -} - -//Dfine_Core_Device_PartiallyFillFunctionTable(MTL); -//Define_Helper_Device_PartiallyFillFunctionTable(MTL); -//Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); -//Define_Streamer_Device_PartiallyFillFunctionTable(MTL); -//Define_SwapChain_Device_PartiallyFillFunctionTable(MTL); -//Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL); +// +//static const BufferDesc& NRI_CALL GetBufferDesc(const Buffer& buffer) { +// return ((const BufferMTL&)buffer).GetDesc(); +//} +// +//static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { +// return ((const TextureMTL&)texture).GetDesc(); +//} +// +//static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) { +// return (FormatSupportBits)0; +// //return ((const DeviceDesc&)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) { +// //return ((const DeviceMTL&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc); +// +//} +// +//static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) { +// return ((DeviceMTL&)device).GetCommandQueue(commandQueueType, commandQueue); +//} +// +//static Result NRI_CALL CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator) { +// //DeviceMTL& device = ((CommandQueueD3D12&)commandQueue).GetDevice(); +// //return device.CreateImplementation(commandAllocator, commandQueue); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateDescriptorPool(Device& device, const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) { +// //return ((DeviceMTL&)device).CreateImplementation(descriptorPool, descriptorPoolDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffer) { +// return ((DeviceMTL&)device).CreateImplementation(buffer, bufferDesc); +//} +// +//static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& texture) { +// return ((DeviceMTL&)device).CreateImplementation(texture, textureDesc); +//} +// +//static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { +// //DeviceMTL& device = ((const BufferD3D12*)bufferViewDesc.buffer)->GetDevice(); +// //return device.CreateImplementation(bufferView, bufferViewDesc); +// return Result::SUCCESS; +//} +// +// +//static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) { +// //DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice(); +// //return device.CreateImplementation(textureView, textureViewDesc); +// +// +// //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); +// //return device.CreateImplementation(textureView, textureViewDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) { +// //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); +// //return device.CreateImplementation(textureView, textureViewDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) { +// //DeviceMTL& device = ((const TextureD3D12*)textureViewDesc.texture)->GetDevice(); +// //return device.CreateImplementation(textureView, textureViewDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateSampler(Device& device, const SamplerDesc& samplerDesc, Descriptor*& sampler) { +// //return ((DeviceMTL&)device).CreateImplementation(sampler, samplerDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreatePipelineLayout(Device& device, const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) { +// //return ((DeviceMTL&)device).CreateImplementation(pipelineLayout, pipelineLayoutDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateGraphicsPipeline(Device& device, const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) { +// //return ((DeviceMTL&)device).CreateImplementation(pipeline, graphicsPipelineDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) { +// //return ((DeviceMTL&)device).CreateImplementation(pipeline, computePipelineDesc); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) { +// //return ((DeviceMTL&)device).CreateImplementation(fence, initialValue); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) { +// //return ((DeviceMTL&)device).CreateImplementation(queryPool, queryPoolDesc); +// return Result::SUCCESS; +//} +// +//static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) { +// //Destroy((CommandBufferD3D12*)&commandBuffer); +//} +// +//static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) { +// //Destroy((CommandAllocatorD3D12*)&commandAllocator); +//} +// +//static void NRI_CALL DestroyDescriptorPool(DescriptorPool& descriptorPool) { +// //Destroy((DescriptorPoolD3D12*)&descriptorPool); +//} +// +//static void NRI_CALL DestroyBuffer(Buffer& buffer) { +// //Destroy((BufferD3D12*)&buffer); +//} +// +//static void NRI_CALL DestroyTexture(Texture& texture) { +// //Destroy((TextureD3D12*)&texture); +//} +// +//static void NRI_CALL DestroyDescriptor(Descriptor& descriptor) { +// //Destroy((DescriptorD3D12*)&descriptor); +//} +// +//static void NRI_CALL DestroyPipelineLayout(PipelineLayout& pipelineLayout) { +// //Destroy((PipelineLayoutD3D12*)&pipelineLayout); +//} +// +//static void NRI_CALL DestroyPipeline(Pipeline& pipeline) { +// //Destroy((PipelineD3D12*)&pipeline); +//} +// +//static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) { +// //Destroy((QueryPoolD3D12*)&queryPool); +//} +// +//static void NRI_CALL DestroyFence(Fence& fence) { +// Destroy((FenceMTL*)&fence); +//} +// +//static Result NRI_CALL AllocateMemory(Device& device, const AllocateMemoryDesc& allocateMemoryDesc, Memory*& memory) { +// return Result::SUCCESS; +// //return ((DeviceMTL&)device).CreateImplementation(memory, allocateMemoryDesc); +//} +// +//static Result NRI_CALL BindBufferMemory(Device& device, const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { +// //return ((DeviceMTL&)device).BindBufferMemory(memoryBindingDescs, memoryBindingDescNum); +// return Result::SUCCESS; +//} +// +//static Result NRI_CALL BindTextureMemory(Device& device, const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { +// //return ((DeviceMTL&)device).BindTextureMemory(memoryBindingDescs, memoryBindingDescNum); +// return Result::SUCCESS; +//} +// +//static void NRI_CALL FreeMemory(Memory& memory) { +//// Destroy((MemoryD3D12*)&memory); +//} +// +//static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) { +//// ((DeviceMTL&)device).SetDebugName(name); +//} +// +//static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) { +//// ((PipelineD3D12&)pipeline).SetDebugName(name); +//} +// +//static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) { +//// ((PipelineLayoutD3D12&)pipelineLayout).SetDebugName(name); +//} +// +//static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) { +//// ((MemoryMetal&)memory).SetDebugName(name); +//} +// +//static void* NRI_CALL GetDeviceNativeObject(const Device& device) { +// if (!(&device)) +// return nullptr; +// return (DeviceMTL*)&((DeviceMTL&)device); +// +// //return ((DeviceMetal&)device).GetNativeObject(); +//} +// +////Result DeviceMTL::FillFunctionTable(CoreInterface& table) const { +//// table = {}; +//// // Core_Device_PartiallyFillFunctionTableMTL(table); +//// // Core_Buffer_PartiallyFillFunctionTableMTL(table); +//// // Core_CommandAllocator_PartiallyFillFunctionTableMTL(table); +//// // Core_CommandBuffer_PartiallyFillFunctionTableMTL(table); +//// // Core_CommandQueue_PartiallyFillFunctionTableMTL(table); +//// // Core_Descriptor_PartiallyFillFunctionTableMTL(table); +//// // Core_DescriptorPool_PartiallyFillFunctionTableMTL(table); +//// // Core_DescriptorSet_PartiallyFillFunctionTableMTL(table); +//// // Core_Fence_PartiallyFillFunctionTableMTL(table); +//// // Core_QueryPool_PartiallyFillFunctionTableMTL(table); +//// // Core_Texture_PartiallyFillFunctionTableMTL(table); +//// // return ValidateFunctionTable(table); +//// return Result::UNSUPPORTED; +////} +// +//#pragma endregion +// +////Result DeviceMTL::FillFunctionTable(HelperInterface& table) const { +//// table = {}; +//// return Result::UNSUPPORTED; +////} +//// +////Result DeviceMTL::FillFunctionTable(LowLatencyInterface& table) const { +//// table = {}; +//// return Result::UNSUPPORTED; +////} +// +////Result DeviceMTL::FillFunctionTable(MeshShaderInterface& table) const { +//// table = {}; +//// return Result::UNSUPPORTED; +////} +// +////Result DeviceMTL::FillFunctionTable(RayTracingInterface& table) const { +//// table = {}; +//// return Result::UNSUPPORTED; +////} +//// +////Result DeviceMTL::FillFunctionTable(StreamerInterface& table) const { +//// table = {}; +//// return Result::UNSUPPORTED; +////} +//// +////Result DeviceMTL::FillFunctionTable(SwapChainInterface& table) const { +//// table = {}; +//// return Result::UNSUPPORTED; +////} +//// +////Result DeviceMTL::FillFunctionTable(ResourceAllocatorInterface& table) const { +//// table = {}; +//// return Result::UNSUPPORTED; +////} +// +////Dfine_Core_Device_PartiallyFillFunctionTable(MTL); +////Define_Helper_Device_PartiallyFillFunctionTable(MTL); +////Define_RayTracing_Device_PartiallyFillFunctionTable(MTL); +////Define_Streamer_Device_PartiallyFillFunctionTable(MTL); +////Define_SwapChain_Device_PartiallyFillFunctionTable(MTL); +////Define_ResourceAllocator_Device_PartiallyFillFunctionTable(MTL); diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index 39877b00..0b13e897 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -63,6 +63,7 @@ static bool FindMTLGpuFamily(id device, + DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) : DeviceBase(callbacks, stdAllocator) { m_Desc.graphicsAPI = GraphicsAPI::VK; m_Desc.nriVersionMajor = NRI_VERSION_MAJOR; @@ -90,12 +91,24 @@ static bool FindMTLGpuFamily(id device, // } //} + void DeviceMTL::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { } void DeviceMTL::GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + MTLTextureDescriptor* mtlTextureDesc = [[MTLTextureDescriptor alloc] init]; + + MemoryTypeInfo memoryTypeInfo; + memoryTypeInfo.options = DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION[(size_t)memoryLocation]; + memoryTypeInfo.cacheMode = DEFAULT_CACHE_MODE_MEMORY_LOCATION[(size_t)memoryLocation]; + memoryTypeInfo.storageMode = DEFAULT_STORAGE_MODE_MEMORY_LOCATION[(size_t)memoryLocation]; + nri::fillMTLTextureDescriptor(textureDesc, mtlTextureDesc); + const MTLSizeAndAlign sizeAlign = [m_Device heapTextureSizeAndAlignWithDescriptor: mtlTextureDesc]; + memoryDesc.size = sizeAlign.size; + memoryDesc.alignment = sizeAlign.align; + memoryDesc.type = memoryTypeInfo.value; } void DeviceMTL::GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { @@ -104,7 +117,7 @@ static bool FindMTLGpuFamily(id device, Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - ExclusiveScope lock(m_Lock); + // ExclusiveScope lock(m_Lock); // Check if already created (or wrapped) uint32_t index = (uint32_t)commandQueueType; @@ -112,18 +125,6 @@ static bool FindMTLGpuFamily(id device, commandQueue = (CommandQueue*)m_CommandQueues[index]; return Result::SUCCESS; } - - // Check if supported - //uint32_t queueFamilyIndex = m_QueueFamilyIndices[index]; - //if (queueFamilyIndex == INVALID_FAMILY_INDEX) { - // commandQueue = nullptr; - // return Result::UNSUPPORTED; - //} - - // Create - //VkQueue handle = VK_NULL_HANDLE; - //m_VK.GetDeviceQueue(m_Device, queueFamilyIndex, 0, &handle); - Result result = CreateImplementation(commandQueue, commandQueueType); if (result == Result::SUCCESS) m_CommandQueues[index] = (CommandQueueMTL*)commandQueue; diff --git a/Source/Metal/ImplMTL.mm b/Source/Metal/ImplMTL.mm index cdb30b9a..33c119f0 100644 --- a/Source/Metal/ImplMTL.mm +++ b/Source/Metal/ImplMTL.mm @@ -8,12 +8,17 @@ #include "CommandAllocatorMTL.h" #include "CommandBufferMTL.h" #include "DeviceMTL.h" +#include "DescriptorMTL.h" +#include "TextureMTL.h" Result CreateDeviceMTL(const DeviceCreationDesc& desc, DeviceBase*& device) { StdAllocator allocator(desc.allocationCallbacks); DeviceMTL* impl = Allocate(allocator, desc.callbackInterface, allocator); Result result = impl->Create(desc, {}, false); + + MTLPurgeableState a; + MTLStorageMode mode; if (result != Result::SUCCESS) { Destroy(allocator, impl); device = nullptr; @@ -234,6 +239,27 @@ static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPo return ((DeviceMTL&)device).GetDesc(); } +//static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { +// DeviceMTL& device = ((const BufferMTL*)bufferViewDesc.buffer)->GetDevice(); +// return device.CreateImplementation(bufferView, bufferViewDesc); +//} +// +//static Result NRI_CALL CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) { +// DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice(); +// return device.CreateImplementation(textureView, textureViewDesc); +//} +// +//static Result NRI_CALL CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) { +// DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice(); +// return device.CreateImplementation(textureView, textureViewDesc); +//} +// +//static Result NRI_CALL CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) { +// DeviceMTL& device = ((const TextureMTL*)textureViewDesc.texture)->GetDevice(); +// return device.CreateImplementation(textureView, textureViewDesc); +//} + + //static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& workSubmissionDesc) { // ((CommandQueueMTL&)commandQueue).Submit(workSubmissionDesc, nullptr); //} diff --git a/Source/Metal/MemoryMTL.h b/Source/Metal/MemoryMTL.h index 9ab89d5f..a8deeb2b 100644 --- a/Source/Metal/MemoryMTL.h +++ b/Source/Metal/MemoryMTL.h @@ -16,8 +16,8 @@ struct MemoryMTL { : m_Device(device) { } - inline id GetHandle() const { - return m_Buffer; + inline id GetHandle() const { + return m_Handle; } inline DeviceMTL& GetDevice() const { @@ -33,7 +33,7 @@ struct MemoryMTL { } ~MemoryMTL(); - + Result Create(const MemoryMTLDesc& memoryDesc); Result Create(const AllocateMemoryDesc& allocateMemoryDesc); //Result CreateDedicated(const BufferVK& buffer); @@ -42,12 +42,11 @@ struct MemoryMTL { //================================================================================================================ // NRI //================================================================================================================ - void SetDebugName(const char* name); private: DeviceMTL& m_Device; - id m_Buffer; + id m_Handle; uint8_t* m_MappedMemory = nullptr; MemoryType m_Type = std::numeric_limits::max(); float m_Priority = 0.0f; diff --git a/Source/Metal/MemoryMTL.mm b/Source/Metal/MemoryMTL.mm index b94511cb..18aa0524 100644 --- a/Source/Metal/MemoryMTL.mm +++ b/Source/Metal/MemoryMTL.mm @@ -12,7 +12,18 @@ //} } +Result MemoryMTL::Create(const AllocateMemoryDesc& allocateMemoryDesc) { + MTLHeapDescriptor* heapDescriptor = [[MTLHeapDescriptor alloc] init]; + [heapDescriptor setSize:allocateMemoryDesc.size]; + +} + Result MemoryMTL::Create(const MemoryMTLDesc& memoryDesc) { + MTLHeapDescriptor* heapDescriptor = [[MTLHeapDescriptor alloc] init]; + [heapDescriptor setSize:memoryDesc.size]; + + // [heapDescriptor setStorageMode:memoryDesc.storage]; + // if (!memoryDesc.vkDeviceMemory) // return Result::INVALID_ARGUMENT; @@ -21,9 +32,9 @@ //bool found = m_Device.GetMemoryTypeByIndex(memoryDesc.memoryTypeIndex, memoryTypeInfo); //RETURN_ON_FAILURE(&m_Device, found, Result::INVALID_ARGUMENT, "Can't find memory by index"); - m_OwnsNativeObjects = false; +// m_OwnsNativeObjects = false; // m_Buffer = memoryDesc.buffer; - m_MappedMemory = (uint8_t*)memoryDesc.mappedMemory; +// m_MappedMemory = (uint8_t*)memoryDesc.mappedMemory; // m_Type = memoryDesc.options; // const auto& mtl = m_Device.GetDispatchTable(); diff --git a/Source/Metal/SharedMTL.h b/Source/Metal/SharedMTL.h index 58262f0f..4c236110 100644 --- a/Source/Metal/SharedMTL.h +++ b/Source/Metal/SharedMTL.h @@ -5,4 +5,13 @@ #include "ConversionMTL.h" #include "DeviceMTL.h" - +struct MemoryTypeInfo { + union { + uint32_t value; + struct { + uint32_t options: 16; // MTLResourceOptions + uint32_t storageMode: 4; // MTLStorageMode + uint32_t cacheMode: 2; // MTLCPUCacheMode + }; + }; +}; diff --git a/Source/Metal/SwapChainMTL.h b/Source/Metal/SwapChainMTL.h index b2de4e68..567d03b2 100644 --- a/Source/Metal/SwapChainMTL.h +++ b/Source/Metal/SwapChainMTL.h @@ -1,5 +1,3 @@ -// © 2021 NVIDIA Corporation - #pragma once namespace nri { @@ -8,12 +6,17 @@ namespace nri { constexpr uint32_t MAX_NUMBER_OF_FRAMES_IN_FLIGHT = 8; struct SwapChainMTL: public DisplayDescHelper { - SwapChainVK(DeviceMTL& device); - ~SwapChainVK(); + SwapChainMTL(DeviceMTL& device) + : m_Device(device) { + + } + ~SwapChainMTL(); Result Create(const SwapChainDesc& swapChainDesc); private: + CAMetalLayer* m_MetalLayer; + DeviceMTL& m_Device; uint64_t m_PresentId = 0; uint32_t m_TextureIndex = 0; diff --git a/Source/Metal/SwapChainMTL.mm b/Source/Metal/SwapChainMTL.mm index e69de29b..f2bad956 100644 --- a/Source/Metal/SwapChainMTL.mm +++ b/Source/Metal/SwapChainMTL.mm @@ -0,0 +1,15 @@ +#include "SharedMTL.h" + +#include "SwapChainMTL.h" + +using namespace nri; + + +SwapChainMTL::~SwapChainMTL() { + +} + +Result SwapChainMTL::Create(const SwapChainDesc& swapChainDesc) { + m_MetalLayer = (CAMetalLayer*)swapChainDesc.window.metal.caMetalLayer; + +} diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index a88c981d..7f60993b 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -7,8 +7,10 @@ namespace nri { struct DeviceMTL; -struct TextureMTL { +void fillMTLTextureDescriptor(const TextureDesc& textureDesc, MTLTextureDescriptor* mtlDescriptor); +struct TextureMTL { +public: inline TextureMTL(DeviceMTL& device) : m_Device(device) { } diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm index b66fcb08..ed6ec638 100644 --- a/Source/Metal/TextureMTL.mm +++ b/Source/Metal/TextureMTL.mm @@ -8,6 +8,16 @@ m_Handle = nil; } +void nri::fillMTLTextureDescriptor(const TextureDesc& textureDesc, MTLTextureDescriptor* info) { + info.textureType = ::GetImageTypeMTL(textureDesc.type); + info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); + info.width = textureDesc.width; + info.height = textureDesc.height; + info.depth = textureDesc.depth; + info.mipmapLevelCount = textureDesc.mipNum; + info.sampleCount = textureDesc.sampleNum; + info.arrayLength = textureDesc.layerNum; +} //Result TextureMTL::Create(const TextureMTLDesc& textureDesc) { // m_Handle = texturedesc.texture; @@ -17,15 +27,17 @@ Result TextureMTL::Create(const TextureDesc& textureDesc) { MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init]; - info.textureType = ::GetImageTypeMTL(textureDesc.type); - info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); - info.width = textureDesc.width; - info.height = textureDesc.height; - info.depth = textureDesc.depth; - info.mipmapLevelCount = textureDesc.mipNum; - info.sampleCount = textureDesc.sampleNum; - info.arrayLength = textureDesc.layerNum; + fillMTLTextureDescriptor(textureDesc, info); +// info.textureType = ::GetImageTypeMTL(textureDesc.type); +// info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); +// info.width = textureDesc.width; +// info.height = textureDesc.height; +// info.depth = textureDesc.depth; +// info.mipmapLevelCount = textureDesc.mipNum; +// info.sampleCount = textureDesc.sampleNum; +// info.arrayLength = textureDesc.layerNum; +// m_Handle = [m_Device newTextureWithDescriptor:info]; m_Desc = textureDesc; From 8c274b5f71d6e36fcff7a27b39bb389a81014885 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Thu, 28 Nov 2024 21:02:53 -0800 Subject: [PATCH 23/27] feat: more progress Signed-off-by: Michael Pollind --- Include/Extensions/NRIWrapperMTL.h | 5 +- Source/Metal/BufferMTL.h | 21 +- Source/Metal/BufferMTL.mm | 41 ++-- Source/Metal/CommandBufferMTL.h | 2 + Source/Metal/CommandBufferMTL.mm | 35 +-- Source/Metal/ConversionMTL.h | 18 +- Source/Metal/DescriptorMTL.h | 42 +++- Source/Metal/DescriptorMTL.mm | 68 +++++- Source/Metal/DescriptorPoolMTL.h | 47 ++++ Source/Metal/DescriptorPoolMTL.mm | 29 +++ Source/Metal/DescriptorSetMTL.h | 29 +++ Source/Metal/DescriptorSetMTL.mm | 36 +++ Source/Metal/DeviceMTL.h | 15 +- Source/Metal/DeviceMTL.mm | 338 +++++++++++++++++++++-------- Source/Metal/ImplMTL.mm | 113 ++++++++-- Source/Metal/MemoryMTL.h | 15 +- Source/Metal/MemoryMTL.mm | 68 +++--- Source/Metal/PipelineMTL.h | 13 +- Source/Metal/PipelineMTL.mm | 45 ++++ Source/Metal/TextureMTL.h | 15 +- Source/Metal/TextureMTL.mm | 41 ++-- Source/Shared/SharedExternal.h | 2 + 22 files changed, 818 insertions(+), 220 deletions(-) create mode 100644 Source/Metal/DescriptorPoolMTL.h create mode 100644 Source/Metal/DescriptorPoolMTL.mm create mode 100644 Source/Metal/DescriptorSetMTL.h create mode 100644 Source/Metal/DescriptorSetMTL.mm diff --git a/Include/Extensions/NRIWrapperMTL.h b/Include/Extensions/NRIWrapperMTL.h index a560999d..277be623 100644 --- a/Include/Extensions/NRIWrapperMTL.h +++ b/Include/Extensions/NRIWrapperMTL.h @@ -9,11 +9,10 @@ NriNamespaceBegin -NonNriForwardStruct(MTLHeap); - +typedef void* MTLHeap; +typedef void* MTLDeviceHandle; // id typedef void* MTLBufferHandle; // id typedef void* MTLTextureHandle; -typedef void* MTLDeviceHandle; NriStruct(DeviceCreationMTLDesc) { diff --git a/Source/Metal/BufferMTL.h b/Source/Metal/BufferMTL.h index dac53dd0..9604cef2 100644 --- a/Source/Metal/BufferMTL.h +++ b/Source/Metal/BufferMTL.h @@ -14,8 +14,10 @@ struct BufferMTL { : m_Device(device) { } + ~BufferMTL(); + inline id GetHandle() const { - return pBuffer; + return m_Handle; } inline DeviceMTL& GetDevice() const { @@ -28,17 +30,22 @@ struct BufferMTL { void* Map(uint64_t offset, uint64_t size); void Unmap(); - void SetDebugName(const char* name); - - ~BufferMTL(); + void FinishMemoryBinding(MemoryMTL& memory, uint64_t memoryOffset); Result Create(const BufferDesc& bufferDesc); - Result Create(const BufferVKDesc& bufferDesc); - Result Create(const AllocateBufferDesc& bufferDesc); + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + private: + void UpdateLabel(); + + NSString* m_Label = nullptr; DeviceMTL& m_Device; - id pBuffer; + id m_Handle; uint8_t* m_MappedMemory = nullptr; uint64_t m_MappedMemoryOffset = 0; uint64_t m_MappedMemoryRangeSize = 0; diff --git a/Source/Metal/BufferMTL.mm b/Source/Metal/BufferMTL.mm index 70d623e4..2d7ae578 100644 --- a/Source/Metal/BufferMTL.mm +++ b/Source/Metal/BufferMTL.mm @@ -1,37 +1,44 @@ #include "SharedMTL.h" #include "BufferMTL.h" +#include "MemoryMTL.h" using namespace nri; - -Result BufferMTL::Create(const BufferDesc& bufferDesc) { - return Result::SUCCESS; -} - -Result BufferMTL::Create(const BufferVKDesc& bufferDesc) { - return Result::SUCCESS; +void* BufferMTL::Map(uint64_t offset, uint64_t size) { + return (uint8_t*)[m_Handle contents] + offset; } - -void* BufferMTL::Map(uint64_t offset, uint64_t size) { - return (uint8_t*)[pBuffer contents] + offset; +BufferMTL::~BufferMTL() { + [m_Label release]; } void BufferMTL::Unmap() { } +void BufferMTL::FinishMemoryBinding(MemoryMTL& memory, uint64_t memoryOffset) { + m_Handle = [memory.GetHandle() + newBufferWithLength: m_Desc.size + options: MTLResourceCPUCacheModeDefaultCache + offset: memoryOffset]; + UpdateLabel(); +} -Result BufferMTL::Create(const AllocateBufferDesc& bufferDesc) { - return Result::SUCCESS; +void BufferMTL::UpdateLabel() { + if(m_Handle && m_Label) { + [m_Handle setLabel: m_Label]; + } } -void BufferMTL::SetDebugName(const char* name) { - - NSString* str = [NSString stringWithUTF8String:name]; - //[pBuffer addDebugMarker:name range:] - +Result BufferMTL::Create(const BufferDesc& bufferDesc) { + m_Desc = bufferDesc; +} + +void BufferMTL::SetDebugName(const char* name) { + m_Label = [NSString stringWithUTF8String:name]; + [m_Label retain]; + UpdateLabel(); } diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index 7034aa92..f4f03498 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -106,6 +106,8 @@ struct CommandBufferMTL { id m_RendererEncoder = nil; id m_ComputeEncoder = nil; + //id + CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE; }; }; diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index cffe549f..66bd664a 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -97,7 +97,7 @@ for(uint32_t i = 0; i < attachmentsDesc.colorNum; i++) { DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.colors[i]; - renderPassDescriptor.colorAttachments[i].texture = descriptorMTL.GetImageView() ; + renderPassDescriptor.colorAttachments[i].texture = descriptorMTL.GetTextureHandle(); renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionClear; renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; @@ -107,17 +107,13 @@ if(attachmentsDesc.depthStencil) { DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.depthStencil; - renderPassDescriptor.depthAttachment.texture = descriptorMTL.GetImageView(); + renderPassDescriptor.depthAttachment.texture = descriptorMTL.GetTextureHandle(); // renderPassDescriptor.depthAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; } - - //renderPassDescriptor.colorAttachments[ - - //renderPassDescriptor.colorAttachments - + m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDescriptor]; } void CommandBufferMTL::EndRendering() { @@ -136,8 +132,7 @@ mtlViewports[i].znear = viewports[i].depthMin; mtlViewports[i].zfar = viewports[i].depthMax; } - - [m_RendererEncoder setViewports:mtlViewports count:viewportNum]; + [m_RendererEncoder setViewports: mtlViewports count: viewportNum]; } void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) { NSCAssert(m_RendererEncoder, @"encoder set"); @@ -146,9 +141,10 @@ rect.y = rects[rectNum].y; rect.width = rects[rectNum].width; rect.height = rects[rectNum].height; - [m_RendererEncoder setScissorRect:rect]; + [m_RendererEncoder setScissorRect: rect]; } void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) { + } void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) { [m_RendererEncoder setStencilFrontReferenceValue: frontRef backReferenceValue:backRef]; @@ -205,7 +201,7 @@ void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) { id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType - indexCount:drawIndexedDesc.indexNum + indexCount: drawIndexedDesc.indexNum indexType: m_CurrentIndexCmd.m_Type indexBuffer: indexBuffer indexBufferOffset: m_CurrentIndexCmd.m_Offset]; @@ -214,13 +210,24 @@ void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { // TODO: implement count Buffer NSCAssert(!countBuffer, @"count buffer not supported"); + [m_RendererEncoder - drawPrimitives: m_CurrentPipeline->m_primitiveType - indirectBuffer:((BufferMTL&)buffer).GetHandle() - indirectBufferOffset: offset]; + drawPrimitives: m_CurrentPipeline->m_primitiveType + indirectBuffer:((BufferMTL&)buffer).GetHandle() + indirectBufferOffset: offset]; } void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { + // m_CurrentPipeline-> + id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); + const BufferMTL& bufferImpl = (const BufferMTL&)buffer; + //const BufferMTL& bufferImpl = (const BufferMTL&)buffer; +// [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType +// indexCount: drawIndexedDesc.indexNum +// indexType: m_CurrentIndexCmd.m_Type +// indexBuffer: indexBuffer +// indexBufferOffset: m_CurrentIndexCmd.m_Offset +// ]; } void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) { diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index 64a45c68..f8d1e8b2 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -220,12 +220,22 @@ constexpr MTLTriangleFillMode GetPolygonMode(FillMode fillMode) { } constexpr std::array SAMPLER_ADDRESS_MODE = { - MTLSamplerAddressModeRepeat, // REPEAT - MTLSamplerAddressModeMirrorRepeat, // MIRRORED_REPEAT - MTLSamplerAddressModeClampToEdge, // CLAMP_TO_EDGE - MTLSamplerAddressModeClampToZero // CLAMP_TO_BORDER + MTLSamplerAddressModeRepeat, // REPEAT, + MTLSamplerAddressModeMirrorRepeat, // MIRRORED_REPEAT, + MTLSamplerAddressModeClampToEdge, // CLAMP_TO_EDGE, + MTLSamplerAddressModeClampToBorderColor, // CLAMP_TO_BORDER, + MTLSamplerAddressModeClampToEdge // MIRROR_CLAMP_TO_EDGE }; +constexpr std::array MIN_MAG_FILTER = { + MTLSamplerMinMagFilterNearest, // NEAREST + MTLSamplerMinMagFilterLinear // LINEAR +}; + +constexpr MTLSamplerMinMagFilter GetFilter(Filter filter) { + return MIN_MAG_FILTER[(size_t)filter]; +} + constexpr MTLSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) { return SAMPLER_ADDRESS_MODE[(size_t)addressMode]; } diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index 5ffe9a1d..d0af5c8c 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -6,11 +6,20 @@ namespace nri { struct DeviceMTL; + +struct DescriptorView { +}; + enum class DescriptorTypeMTL { NONE, - IMAGE_VIEW + IMAGE_VIEW_1D, + IMAGE_VIEW_2D, + IMAGE_VIEW_3D, + SAMPLER, + BUFFER_VIEW }; + struct DescriptorMTL { public: inline DescriptorMTL (DeviceMTL& device) @@ -18,22 +27,43 @@ struct DescriptorMTL { } ~DescriptorMTL(); - - inline id GetImageView() { - return m_texture; + inline DescriptorTypeMTL GetType() { + return m_Type; + } + inline id GetTextureHandle() { + return m_Texture; + } + inline id GetBufferHandle() { + return m_Buffer; + } + inline id GetSamplerStateHandler() { + return m_SamplerState; } + inline struct BufferViewDesc& BufferView() { + return m_BufferViewDesc; + } Result Create(const BufferViewDesc& bufferViewDesc); Result Create(const Texture1DViewDesc& textureViewDesc); Result Create(const Texture2DViewDesc& textureViewDesc); Result Create(const Texture3DViewDesc& textureViewDesc); Result Create(const SamplerDesc& samplerDesc); + + private: + DeviceMTL& m_Device; + DescriptorTypeMTL m_Type = DescriptorTypeMTL::NONE; + id m_Texture; + id m_Buffer; + id m_SamplerState; + + union { + BufferViewDesc m_BufferViewDesc; + SamplerDesc m_SamplerViewDesc; + }; - id m_texture; - DescriptorTypeMTL m_type = DescriptorTypeMTL::NONE; }; diff --git a/Source/Metal/DescriptorMTL.mm b/Source/Metal/DescriptorMTL.mm index 7fabc058..fe9c189f 100644 --- a/Source/Metal/DescriptorMTL.mm +++ b/Source/Metal/DescriptorMTL.mm @@ -3,6 +3,9 @@ #include "DescriptorMTL.h" #include "BufferMTL.h" +#include "TextureMTL.h" + +#include "ConversionMTL.h" namespace nri { @@ -10,25 +13,88 @@ } + Result DescriptorMTL::Create(const BufferViewDesc& bufferViewDesc) { - // m_Type = DescriptorTypeVK::BUFFER_VIEW; + m_Type = DescriptorTypeMTL::BUFFER_VIEW; const BufferMTL& buffer = *(const BufferMTL*)bufferViewDesc.buffer; return Result::SUCCESS; } + + Result DescriptorMTL::Create(const Texture1DViewDesc& textureViewDesc) { + TextureMTL& texture = *(TextureMTL*)textureViewDesc.texture; + const Mip_t remainingMips = textureViewDesc.mipNum - textureViewDesc.mipOffset; + const Dim_t remainingLayers = textureViewDesc.layerNum - textureViewDesc.layerOffset; + + NSRange level; + level.location = textureViewDesc.mipOffset; + level.length = textureViewDesc.mipNum == REMAINING_MIPS ? remainingMips: textureViewDesc.mipNum; + NSRange slices; + slices.location = textureViewDesc.layerNum; + slices.length = textureViewDesc.layerNum == REMAINING_LAYERS ? remainingLayers : textureViewDesc.layerNum; + m_Texture = [texture.GetHandle() + newTextureViewWithPixelFormat: GetFormatMTL(textureViewDesc.format) + textureType: MTLTextureType1D + levels:level + slices:slices]; + return Result::SUCCESS; } Result DescriptorMTL::Create(const Texture2DViewDesc& textureViewDesc) { + TextureMTL& texture = *(TextureMTL*)textureViewDesc.texture; + const Mip_t remainingMips = textureViewDesc.mipNum - textureViewDesc.mipOffset; + const Dim_t remainingLayers = textureViewDesc.layerNum - textureViewDesc.layerOffset; + + NSRange level; + level.location = textureViewDesc.mipOffset; + level.length = textureViewDesc.mipNum == REMAINING_MIPS ? remainingMips: textureViewDesc.mipNum; + NSRange slices; + slices.location = textureViewDesc.layerNum; + slices.length = textureViewDesc.layerNum == REMAINING_LAYERS ? remainingLayers : textureViewDesc.layerNum; + m_Texture = [texture.GetHandle() + newTextureViewWithPixelFormat: GetFormatMTL(textureViewDesc.format) + textureType: MTLTextureType2D + levels:level + slices:slices]; return Result::SUCCESS; } Result DescriptorMTL::Create(const Texture3DViewDesc& textureViewDesc){ + TextureMTL& texture = *(TextureMTL*)textureViewDesc.texture; + const Mip_t remainingMips = textureViewDesc.mipNum - textureViewDesc.mipOffset; + const Dim_t remainingLayers = textureViewDesc.sliceNum - textureViewDesc.sliceOffset; + + NSRange level; + level.location = textureViewDesc.mipOffset; + level.length = textureViewDesc.mipNum == REMAINING_MIPS ? remainingMips: textureViewDesc.mipNum; + NSRange slices; + slices.location = textureViewDesc.sliceOffset; + slices.length = textureViewDesc.sliceNum == REMAINING_LAYERS ? remainingLayers : textureViewDesc.sliceOffset; + m_Texture = [texture.GetHandle() + newTextureViewWithPixelFormat: GetFormatMTL(textureViewDesc.format) + textureType: MTLTextureType3D + levels:level + slices:slices]; + return Result::SUCCESS; } + Result DescriptorMTL::Create(const SamplerDesc& samplerDesc){ + MTLSamplerDescriptor* mtlDesc = [[MTLSamplerDescriptor alloc] init]; + [mtlDesc setMagFilter: GetFilter(samplerDesc.filters.mag)]; + [mtlDesc setMinFilter: GetFilter(samplerDesc.filters.min)]; + [mtlDesc setRAddressMode: GetSamplerAddressMode(samplerDesc.addressModes.w)]; + [mtlDesc setSAddressMode: GetSamplerAddressMode(samplerDesc.addressModes.u)]; + [mtlDesc setTAddressMode: GetSamplerAddressMode(samplerDesc.addressModes.v)]; + [mtlDesc setMaxAnisotropy: samplerDesc.anisotropy]; + [mtlDesc setCompareFunction: GetCompareOp(samplerDesc.compareFunc)]; + [mtlDesc setLodMinClamp: samplerDesc.mipMin]; + [mtlDesc setLodMaxClamp: samplerDesc.mipMax]; + m_SamplerState = [m_Device newSamplerStateWithDescriptor: mtlDesc]; + return Result::SUCCESS; } diff --git a/Source/Metal/DescriptorPoolMTL.h b/Source/Metal/DescriptorPoolMTL.h new file mode 100644 index 00000000..060fad1f --- /dev/null +++ b/Source/Metal/DescriptorPoolMTL.h @@ -0,0 +1,47 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +#import + +namespace nri { + +struct DeviceMTL; +struct DescriptorSetMTL; + +struct DescriptorPoolMTL { + inline DescriptorPoolMTL(DeviceMTL& device) + : m_Device(device) + , m_AllocatedSets(device.GetStdAllocator()) { + m_AllocatedSets.reserve(64); + } +// +// inline operator VkDescriptorPool() const { +// return m_Handle; +// } +// + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + ~DescriptorPoolMTL(); + + Result Create(const DescriptorPoolDesc& descriptorPoolDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + void Reset(); + Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndex, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum); + +private: + DeviceMTL& m_Device; + Vector m_AllocatedSets; + //VkDescriptorPool m_Handle = VK_NULL_HANDLE; + uint32_t m_UsedSets = 0; + bool m_OwnsNativeObjects = true; +}; + +} // namespace nri diff --git a/Source/Metal/DescriptorPoolMTL.mm b/Source/Metal/DescriptorPoolMTL.mm new file mode 100644 index 00000000..05dd2d53 --- /dev/null +++ b/Source/Metal/DescriptorPoolMTL.mm @@ -0,0 +1,29 @@ + +#include "SharedMTL.h" +#include "DescriptorPoolMTL.h" + +using namespace nri; + + +DescriptorPoolMTL::~DescriptorPoolMTL() { + +} + +Result DescriptorPoolMTL::Create(const DescriptorPoolDesc& descriptorPoolDesc) { + +} + +//================================================================================================================ +// NRI +//================================================================================================================ + +void DescriptorPoolMTL::SetDebugName(const char* name) { + +} +void DescriptorPoolMTL::Reset() { + +} +Result DescriptorPoolMTL::AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndex, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum) { + return Result::SUCCESS; +} + diff --git a/Source/Metal/DescriptorSetMTL.h b/Source/Metal/DescriptorSetMTL.h new file mode 100644 index 00000000..61cce70d --- /dev/null +++ b/Source/Metal/DescriptorSetMTL.h @@ -0,0 +1,29 @@ +// © 2021 NVIDIA Corporation + +#pragma once + +namespace nri { + +struct DeviceVK; +struct DescriptorSetMTL { +public: + inline DescriptorSetMTL (DeviceMTL& device) + : m_Device(device) { + } + + + void UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); + + inline id GetArgumentHandle() { + return m_ArgumentEncoder; + } + +private: + DeviceMTL& m_Device; + id m_ArgumentEncoder; + +}; + + +} // namespace nri + diff --git a/Source/Metal/DescriptorSetMTL.mm b/Source/Metal/DescriptorSetMTL.mm new file mode 100644 index 00000000..5c5b2cdd --- /dev/null +++ b/Source/Metal/DescriptorSetMTL.mm @@ -0,0 +1,36 @@ + +#include "SharedMTL.h" + +#include "DescriptorSetMTL.h" +#include "DescriptorMTL.h" + +using namespace nri; + + +void DescriptorSetMTL::UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs) { + + for(size_t j = 0; j < rangeNum; j++) { + const DescriptorRangeUpdateDesc& update = rangeUpdateDescs[j]; + +// uint32_t offset = update.baseDescriptor + descriptorOffset; + + for(size_t descIdx = 0; descIdx < update.descriptorNum; descIdx++) { + + DescriptorMTL& descriptorImpl = *(DescriptorMTL*)&update.descriptors[descIdx]; + switch(descriptorImpl.GetType()) { + case DescriptorTypeMTL::IMAGE_VIEW_1D: + [m_ArgumentEncoder setTexture: descriptorImpl.GetTextureHandle() atIndex:0]; +// [m_ArgumentEncoder setTextures:<#(id _Nullable const * _Nonnull)#> withRange:<#(NSRange)#>] + break; + case DescriptorTypeMTL::IMAGE_VIEW_2D: + [m_ArgumentEncoder setTexture: descriptorImpl.GetTextureHandle() atIndex:0]; + break; + case DescriptorTypeMTL::BUFFER_VIEW: +// [m_ArgumentEncoder setBuffer: offset:<#(NSUInteger)#> atIndex:<#(NSUInteger)#>] + break; + } + +// update.descriptors[descIdx]; + } + } +} diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index e99738f9..6839d001 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -7,6 +7,7 @@ namespace nri { struct CommandQueueMTL; struct DeviceMTL final : public DeviceBase { +public: DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator); ~DeviceMTL(); @@ -17,6 +18,10 @@ struct DeviceMTL final : public DeviceBase { return m_Device; } + inline id GetHandle() const { + return m_Device; + } + template inline Result CreateImplementation(Interface*& entity, const Args&... args) { Implementation* impl = Allocate(GetStdAllocator(), *this); @@ -32,16 +37,17 @@ struct DeviceMTL final : public DeviceBase { } //void GetMemoryTypeInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); - void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); - void GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc); + void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; //bool GetMemoryTypeInfo(MemoryLocation memoryLocation, MemoryTypeInfo& memoryTypeInfo) const; const DeviceDesc& GetDesc() const override { return m_Desc; } + + void Destruct() override; - Result FillFunctionTable(CoreInterface& table) const override; Result FillFunctionTable(HelperInterface& table) const override; Result FillFunctionTable(LowLatencyInterface& table) const override; @@ -59,5 +65,6 @@ struct DeviceMTL final : public DeviceBase { DeviceDesc m_Desc = {}; MTLGPUFamily m_Family; bool m_OwnsNativeObjects = true; + Lock m_Lock; }; }; // namespace nri diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index 0b13e897..244faeaa 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -1,74 +1,64 @@ + + #include "SharedMTL.h" #include "CommandQueueMTL.h" - #include "DeviceMTL.h" - -//#include "AccelerationStructureVK.h" #include "BufferMTL.h" -//#include "CommandAllocatorVK.h" -//#include "CommandBufferMTL.h" #include "CommandQueueMTL.h" -//#include "DescriptorPoolMTL.h" #include "TextureMTL.h" #include "FenceMTL.h" #include "MemoryMTL.h" #include "PipelineLayoutMTL.h" #include "PipelineMTL.h" - using namespace nri; + static bool FindMTLGpuFamily(id device, - MTLGPUFamily *family) { - // https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc - if (@available(macOS 10.15, iOS 10.13, *)) { - if ([device supportsFamily:MTLGPUFamilyApple7]) { - (*family) = MTLGPUFamily::MTLGPUFamilyApple7; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple6]) { - (*family) = MTLGPUFamily::MTLGPUFamilyApple6; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple5]) { - (*family) = MTLGPUFamily::MTLGPUFamilyApple5; + const MTLGPUFamily *families, size_t len, + MTLGPUFamily* current) { + for(size_t i = 0; i < len; i++) { + if( [device supportsFamily: families[i]]) { + *current = families[i]; return true; } - if ([device supportsFamily:MTLGPUFamilyApple4]) { - (*family) = MTLGPUFamily::MTLGPUFamilyApple4; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple3]) { - (*family) = MTLGPUFamily::MTLGPUFamilyApple3; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple2]) { - (*family) = MTLGPUFamily::MTLGPUFamilyApple2; - return true; - } - if ([device supportsFamily:MTLGPUFamilyApple1]) { - (*family) = MTLGPUFamily::MTLGPUFamilyApple1; - return true; - } - - // This family is no longer supported in the macOS 10.15 SDK but still - // exists so default to it. - // return MTLGPUFamily::Mac1; - (*family) = MTLGPUFamily::MTLGPUFamilyMac1; - return true; } return false; } +// referenced from Molten VK +static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef propertyName) { + uint32_t value = 0; + CFTypeRef cfProp = IORegistryEntrySearchCFProperty(entry, + kIOServicePlane, + propertyName, + kCFAllocatorDefault, + kIORegistryIterateRecursively | + kIORegistryIterateParents); + if (cfProp) { + const uint32_t* pValue = reinterpret_cast(CFDataGetBytePtr((CFDataRef)cfProp)); + if (pValue) { value = *pValue; } + CFRelease(cfProp); + } -DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) : DeviceBase(callbacks, stdAllocator) { - m_Desc.graphicsAPI = GraphicsAPI::VK; + return value; +} + + + + +DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator& stdAllocator) + : DeviceBase(callbacks, stdAllocator) { + + for (uint32_t i = 0; i < m_CommandQueues.size(); i++) + Destroy(GetStdAllocator(), m_CommandQueues[i]); + + m_Desc.graphicsAPI = GraphicsAPI::MTL; m_Desc.nriVersionMajor = NRI_VERSION_MAJOR; m_Desc.nriVersionMinor = NRI_VERSION_MINOR; - } @@ -92,11 +82,21 @@ static bool FindMTLGpuFamily(id device, //} -void DeviceMTL::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { +void DeviceMTL::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { + MemoryTypeInfo memoryTypeInfo; + memoryTypeInfo.options = DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION[(size_t)memoryLocation]; + memoryTypeInfo.cacheMode = DEFAULT_CACHE_MODE_MEMORY_LOCATION[(size_t)memoryLocation]; + memoryTypeInfo.storageMode = DEFAULT_STORAGE_MODE_MEMORY_LOCATION[(size_t)memoryLocation]; + MTLTextureDescriptor* mtlTextureDesc = [[MTLTextureDescriptor alloc] init]; + MTLSizeAndAlign sizeAlign = [m_Device heapBufferSizeAndAlignWithLength: bufferDesc.size options: (MTLResourceOptions)memoryTypeInfo.options]; + + memoryDesc.size = sizeAlign.size; + memoryDesc.alignment = sizeAlign.align; + memoryDesc.type = memoryTypeInfo.value; } -void DeviceMTL::GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { +void DeviceMTL::GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { MTLTextureDescriptor* mtlTextureDesc = [[MTLTextureDescriptor alloc] init]; MemoryTypeInfo memoryTypeInfo; @@ -111,13 +111,17 @@ static bool FindMTLGpuFamily(id device, memoryDesc.type = memoryTypeInfo.value; } -void DeviceMTL::GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { +void DeviceMTL::GetMemoryDesc(const AccelerationStructureDesc& accelerationStructureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { +// MTLAccelerationStructureDescriptor* mtlAcceleration = [[MTLAccelerationStructureDescriptor alloc] init]; +// [mtlAcceleration set] + // [m_Device heapAccelerationStructureSizeAndAlignWithSize: accelerationStructureDesc.geometryObjects] + // TODO: need to add ray traced } Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - // ExclusiveScope lock(m_Lock); + ExclusiveScope lock(m_Lock); // Check if already created (or wrapped) uint32_t index = (uint32_t)commandQueueType; @@ -132,22 +136,11 @@ static bool FindMTLGpuFamily(id device, return result; } -//void DeviceMTL::FillCreateInfo(const TextureDesc& textureDesc, MTLTextureDescriptor* info) const { -// info.textureType = ::GetImageTypeMTL(textureDesc.type); -// //info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); -// //info.width = pDesc->mWidth; -// //info.height = pDesc->mHeight; -// //info.depth = pDesc->mDepth; -// //info.mipmapLevelCount = mipLevels; -// //info.sampleCount = pDesc->mSampleCount; -// //info.arrayLength = pDesc->mArraySize; -//} Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) { m_OwnsNativeObjects = !isWrapper; - if(isWrapper) { - // m_Device = deviceCreationMTLDesc.MtlDevice; + m_Device = *(id*)&deviceCreationMTLDesc.MtlDevice; } strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name)); @@ -156,39 +149,210 @@ static bool FindMTLGpuFamily(id device, m_Desc.adapterDesc.vendor = nri::Vendor::APPLE; } - const uint64_t regID = [m_Device registryID]; if (regID) { - // IORef entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID))); - // if (entry) - // { - // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. - // IORef deviceEntry; - // if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess) - // { - // m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))); - // m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id")); - // } - // } + io_registry_entry_t entry = IOServiceGetMatchingService(MACH_PORT_NULL, IORegistryEntryIDMatching(regID)); + if (entry) + { + // That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device. + io_registry_entry_t deviceEntry; + if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess) + { + m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))) ; + m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id")); + } + } + } else { + m_Desc.adapterDesc.vendor = nri::Vendor::APPLE; } - MTLGPUFamily family; - // if(!FindMTLGpuFamily(m_Device, family)) { - // return Result::UNSUPPORTED; - //} - // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf - //TODO: fill desc - switch(family) { - // case MTLGPUFamily::Apple1: - // break; - default: - break; + + MTLArgumentBuffersTier argumentBufferTier = MTLArgumentBuffersTier1; + if ([m_Device respondsToSelector: @selector(argumentBuffersSupport)]) { + argumentBufferTier = [m_Device argumentBuffersSupport]; } + + for (uint32_t sc = 1; sc <= 64; sc <<= 1) { + if([m_Device supportsTextureSampleCount: sc]) { + m_Desc.colorSampleMaxNum |= sc; + m_Desc.depthSampleMaxNum |= sc; + m_Desc.stencilSampleMaxNum |= sc; + m_Desc.zeroAttachmentsSampleMaxNum |= sc; + m_Desc.textureColorSampleMaxNum |= sc; + m_Desc.textureIntegerSampleMaxNum |= sc; + m_Desc.textureDepthSampleMaxNum |= sc; + m_Desc.textureStencilSampleMaxNum |= sc; + m_Desc.storageTextureSampleMaxNum |= sc; + } + } + + + m_Desc.viewportMaxNum = 16; + m_Desc.viewportBoundsRange[0] = -32768; + m_Desc.viewportBoundsRange[1] = 32767; + + m_Desc.attachmentMaxDim = 16384; + m_Desc.attachmentLayerMaxNum = 2048; + m_Desc.colorAttachmentMaxNum = 8; + + m_Desc.colorSampleMaxNum = 32; + m_Desc.depthSampleMaxNum = 32; + m_Desc.stencilSampleMaxNum = 32; + m_Desc.zeroAttachmentsSampleMaxNum = 32; + m_Desc.textureColorSampleMaxNum = 32; + m_Desc.textureIntegerSampleMaxNum = 32; + m_Desc.textureDepthSampleMaxNum = 32; + m_Desc.textureStencilSampleMaxNum = 32; + m_Desc.storageTextureSampleMaxNum = 32; + + m_Desc.textureArrayLayerMaxNum = 16384; + m_Desc.typedBufferMaxDim = uint32_t(-1); + + m_Desc.deviceUploadHeapSize = 256 * 1024 * 1024; + m_Desc.memoryAllocationMaxNum = uint32_t(-1); + m_Desc.samplerAllocationMaxNum = 4096; + m_Desc.constantBufferMaxRange = 64 * 1024; + m_Desc.storageBufferMaxRange = uint32_t(-1); + m_Desc.bufferTextureGranularity = 1; + m_Desc.bufferMaxSize = [m_Device maxBufferLength]; + + m_Desc.uploadBufferTextureRowAlignment = 1; + m_Desc.uploadBufferTextureSliceAlignment = 1; + m_Desc.bufferShaderResourceOffsetAlignment = 1; + m_Desc.constantBufferOffsetAlignment = 1; + m_Desc.shaderBindingTableAlignment = 1; + m_Desc.scratchBufferOffsetAlignment = 1; + + m_Desc.pipelineLayoutDescriptorSetMaxNum = 64; + m_Desc.pipelineLayoutRootConstantMaxSize = 256; + m_Desc.pipelineLayoutRootDescriptorMaxNum = 64; + + m_Desc.perStageDescriptorSamplerMaxNum = 1000000; + m_Desc.perStageDescriptorConstantBufferMaxNum = 1000000; + m_Desc.perStageDescriptorStorageBufferMaxNum = 1000000; + m_Desc.perStageDescriptorTextureMaxNum = 1000000; + m_Desc.perStageDescriptorStorageTextureMaxNum = 1000000; + m_Desc.perStageResourceMaxNum = 1000000; + + m_Desc.descriptorSetSamplerMaxNum = m_Desc.perStageDescriptorSamplerMaxNum; + m_Desc.descriptorSetConstantBufferMaxNum = m_Desc.perStageDescriptorConstantBufferMaxNum; + m_Desc.descriptorSetStorageBufferMaxNum = m_Desc.perStageDescriptorStorageBufferMaxNum; + m_Desc.descriptorSetTextureMaxNum = m_Desc.perStageDescriptorTextureMaxNum; + m_Desc.descriptorSetStorageTextureMaxNum = m_Desc.perStageDescriptorStorageTextureMaxNum; + + m_Desc.vertexShaderAttributeMaxNum = 32; + m_Desc.vertexShaderStreamMaxNum = 32; + m_Desc.vertexShaderOutputComponentMaxNum = 128; - m_Desc.adapterDesc.luid = 0; - m_Desc.adapterDesc.videoMemorySize = 0; - m_Desc.adapterDesc.systemMemorySize = 0; + m_Desc.tessControlShaderGenerationMaxLevel = 64.0f; + m_Desc.tessControlShaderPatchPointMaxNum = 32; + m_Desc.tessControlShaderPerVertexInputComponentMaxNum = 128; + m_Desc.tessControlShaderPerVertexOutputComponentMaxNum = 128; + m_Desc.tessControlShaderPerPatchOutputComponentMaxNum = 128; + m_Desc.tessControlShaderTotalOutputComponentMaxNum = m_Desc.tessControlShaderPatchPointMaxNum * m_Desc.tessControlShaderPerVertexOutputComponentMaxNum + m_Desc.tessControlShaderPerPatchOutputComponentMaxNum; + + m_Desc.tessEvaluationShaderInputComponentMaxNum = 128; + m_Desc.tessEvaluationShaderOutputComponentMaxNum = 128; + + m_Desc.geometryShaderInvocationMaxNum = 32; + m_Desc.geometryShaderInputComponentMaxNum = 128; + m_Desc.geometryShaderOutputComponentMaxNum = 128; + m_Desc.geometryShaderOutputVertexMaxNum = 1024; + m_Desc.geometryShaderTotalOutputComponentMaxNum = 1024; + + m_Desc.fragmentShaderInputComponentMaxNum = 128; + m_Desc.fragmentShaderOutputAttachmentMaxNum = 8; + m_Desc.fragmentShaderDualSourceAttachmentMaxNum = 1; + + +// m_Desc.computeShaderSharedMemoryMaxSize = [m_Device maxThreadgroupMemoryLength]; +// const MTLSize threadGroupSize = [m_Device maxThreadsPerThreadgroup]; +// //familiesm_Desc.computeShaderWorkGroupMaxNum = threadGroupSize.width; +// m_Desc.computeShaderWorkGroupMaxNum[0] = threadGroupSize.width; +// m_Desc.computeShaderWorkGroupMaxNum[1] = threadGroupSize.height; +// m_Desc.computeShaderWorkGroupMaxNum[2] = threadGroupSize.depth; + m_Desc.computeShaderSharedMemoryMaxSize = 64 * 1024; + m_Desc.computeShaderWorkGroupMaxNum[0] = 64 * 1024; + m_Desc.computeShaderWorkGroupMaxNum[1] = 64 * 1024; + m_Desc.computeShaderWorkGroupMaxNum[2] = 64 * 1024; + m_Desc.computeShaderWorkGroupInvocationMaxNum = 64 * 1024; + m_Desc.computeShaderWorkGroupMaxDim[0] = 64 * 1024; + m_Desc.computeShaderWorkGroupMaxDim[1] = 64 * 1024; + m_Desc.computeShaderWorkGroupMaxDim[2] = 64 * 1024; + + m_Desc.rayTracingShaderGroupIdentifierSize = 32; + m_Desc.rayTracingShaderTableMaxStride = (uint32_t)(-1); + m_Desc.rayTracingShaderRecursionMaxDepth = 31; + m_Desc.rayTracingGeometryObjectMaxNum = (uint32_t)(-1); + + m_Desc.meshControlSharedMemoryMaxSize = 64 * 1024; + m_Desc.meshControlWorkGroupInvocationMaxNum = 128; + m_Desc.meshControlPayloadMaxSize = 64 * 1024; + m_Desc.meshEvaluationOutputVerticesMaxNum = 256; + m_Desc.meshEvaluationOutputPrimitiveMaxNum = 256; + m_Desc.meshEvaluationOutputComponentMaxNum = 128; + m_Desc.meshEvaluationSharedMemoryMaxSize = 64 * 1024; + m_Desc.meshEvaluationWorkGroupInvocationMaxNum = 128; + + m_Desc.viewportPrecisionBits = 8; + m_Desc.subPixelPrecisionBits = 8; + m_Desc.subTexelPrecisionBits = 8; + m_Desc.mipmapPrecisionBits = 8; + + m_Desc.drawIndirectMaxNum = uint32_t(-1); + m_Desc.samplerLodBiasMin = -16.0f; + m_Desc.samplerLodBiasMax = 16.0f; + m_Desc.samplerAnisotropyMax = 16; + m_Desc.texelOffsetMin = -8; + m_Desc.texelOffsetMax = 7; + m_Desc.texelGatherOffsetMin = -8; + m_Desc.texelGatherOffsetMax = 7; + m_Desc.clipDistanceMaxNum = 8; + m_Desc.cullDistanceMaxNum = 8; + m_Desc.combinedClipAndCullDistanceMaxNum = 8; + m_Desc.shadingRateAttachmentTileSize = 16; + m_Desc.shaderModel = 69; + + m_Desc.conservativeRasterTier = 3; + m_Desc.sampleLocationsTier = 2; + m_Desc.shadingRateTier = 2; + m_Desc.bindlessTier = 2; + m_Desc.bindlessTier = 2; + + m_Desc.isRayTracingSupported = [m_Device supportsRaytracing]; + + const MTLGPUFamily appleFamilyGPUs[] = { + MTLGPUFamilyApple9, + MTLGPUFamilyApple8, + MTLGPUFamilyApple7, + MTLGPUFamilyApple6, + MTLGPUFamilyApple5, + MTLGPUFamilyApple4, + MTLGPUFamilyApple3, + MTLGPUFamilyApple2, + MTLGPUFamilyApple1 + }; + + const MTLGPUFamily macFamilyGPUs[] = { + MTLGPUFamilyMac2 + }; + + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf + MTLGPUFamily maxSelectedGPU; + m_Desc.texture3DMaxDim = 2048; + if(FindMTLGpuFamily(m_Device, appleFamilyGPUs, GetCountOf(appleFamilyGPUs), &maxSelectedGPU)) { + m_Desc.texture2DMaxDim = (maxSelectedGPU >= MTLGPUFamilyApple3) ? 16384 : 8192; + m_Desc.texture1DMaxDim = (maxSelectedGPU >= MTLGPUFamilyApple3) ? 16384 : 8192; + + } else if(FindMTLGpuFamily(m_Device, macFamilyGPUs, GetCountOf(macFamilyGPUs), &maxSelectedGPU)) { + m_Desc.texture2DMaxDim = 16384; + m_Desc.texture1DMaxDim = 16384; + } else { + return Result::UNSUPPORTED; + } + + return Result::SUCCESS; } diff --git a/Source/Metal/ImplMTL.mm b/Source/Metal/ImplMTL.mm index 33c119f0..bca2a337 100644 --- a/Source/Metal/ImplMTL.mm +++ b/Source/Metal/ImplMTL.mm @@ -10,6 +10,10 @@ #include "DeviceMTL.h" #include "DescriptorMTL.h" #include "TextureMTL.h" +#include "PipelineLayoutMTL.h" +#include "PipelineMTL.h" +#include "DescriptorMTL.h" +#include "MemoryMTL.h" Result CreateDeviceMTL(const DeviceCreationDesc& desc, DeviceBase*& device) { StdAllocator allocator(desc.allocationCallbacks); @@ -67,7 +71,7 @@ static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) { } static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { - // ((CommandBufferVK&)commandBuffer).SetDebugName(name); + ((CommandBufferMTL&)commandBuffer).SetDebugName(name); } static Result NRI_CALL BeginCommandBuffer(CommandBuffer& commandBuffer, const DescriptorPool* descriptorPool) { @@ -264,14 +268,87 @@ static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPo // ((CommandQueueMTL&)commandQueue).Submit(workSubmissionDesc, nullptr); //} + +static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) { + Destroy((CommandBufferMTL*)&commandBuffer); +} + +static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) { + Destroy((CommandAllocatorMTL*)&commandAllocator); +} + +static void NRI_CALL DestroyDescriptorPool(DescriptorPool& descriptorPool) { + //Destroy((DescriptorPoolM*)&descriptorPool); +} + +static void NRI_CALL DestroyBuffer(Buffer& buffer) { + Destroy((BufferMTL*)&buffer); +} + +static void NRI_CALL DestroyTexture(Texture& texture) { + Destroy((TextureMTL*)&texture); +} + +static void NRI_CALL DestroyDescriptor(Descriptor& descriptor) { + // Destroy((DescriptorMTL*)&descriptor); +} + +static void NRI_CALL DestroyPipelineLayout(PipelineLayout& pipelineLayout) { + // Destroy((PipelineLayoutMTL*)&pipelineLayout); +} + +static void NRI_CALL DestroyPipeline(Pipeline& pipeline) { + //Destroy((PipelineMTL*)&pipeline); +} + +static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) { + //Destroy((QueryPoolVK*)&queryPool); +} + +static void NRI_CALL DestroyFence(Fence& fence) { + // Destroy((Fenc*)&fence); +} + +static void NRI_CALL FreeMemory(Memory& memory) { + Destroy((MemoryMTL*)&memory); +} + +static const BufferDesc& NRI_CALL GetBufferDesc(const Buffer& buffer) { + return ((const BufferMTL&)buffer).GetDesc(); +} + +static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) { + return ((const TextureMTL&)texture).GetDesc(); +} + + +static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((const DeviceMTL&)device).GetMemoryDesc(bufferDesc, memoryLocation, memoryDesc); +} + +static void NRI_CALL GetTextureMemoryDesc(const Device& device, const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { + ((const DeviceMTL&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc); +} + +static Result NRI_CALL CreateGraphicsPipeline(Device& device, const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) { + return ((DeviceMTL&)device).CreateImplementation(pipeline, graphicsPipelineDesc); +} + +static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, const PipelineLayout& pipelineLayout, uint32_t setIndex, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum) { + return Result::SUCCESS; + //return ((DescriptorPoolMTL&)descriptorPool).AllocateDescriptorSets(pipelineLayout, setIndex, descriptorSets, instanceNum, variableDescriptorNum); +} + + + Result DeviceMTL::FillFunctionTable(CoreInterface& table) const { table.GetDeviceDesc = ::GetDeviceDesc; - //table.GetBufferDesc = ::GetBufferDesc; - //table.GetTextureDesc = ::GetTextureDesc; + table.GetBufferDesc = ::GetBufferDesc; + table.GetTextureDesc = ::GetTextureDesc; //table.GetFormatSupport = ::GetFormatSupport; //table.GetQuerySize = ::GetQuerySize; - //table.GetBufferMemoryDesc = ::GetBufferMemoryDesc; - //table.GetTextureMemoryDesc = ::GetTextureMemoryDesc; + table.GetBufferMemoryDesc = ::GetBufferMemoryDesc; + table.GetTextureMemoryDesc = ::GetTextureMemoryDesc; //table.GetCommandQueue = ::GetCommandQueue; //table.CreateCommandAllocator = ::CreateCommandAllocator; //table.CreateCommandBuffer = ::CreateCommandBuffer; @@ -284,24 +361,24 @@ static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPo //table.CreateTexture3DView = ::CreateTexture3DView; //table.CreateSampler = ::CreateSampler; //table.CreatePipelineLayout = ::CreatePipelineLayout; - //table.CreateGraphicsPipeline = ::CreateGraphicsPipeline; + table.CreateGraphicsPipeline = ::CreateGraphicsPipeline; // table.CreateComputePipeline = ::CreateComputePipeline; // table.CreateQueryPool = ::CreateQueryPool; // table.CreateFence = ::CreateFence; -// table.DestroyCommandAllocator = ::DestroyCommandAllocator; -// table.DestroyCommandBuffer = ::DestroyCommandBuffer; -// table.DestroyDescriptorPool = ::DestroyDescriptorPool; -// table.DestroyBuffer = ::DestroyBuffer; -// table.DestroyTexture = ::DestroyTexture; -// table.DestroyDescriptor = ::DestroyDescriptor; -// table.DestroyPipelineLayout = ::DestroyPipelineLayout; -// table.DestroyPipeline = ::DestroyPipeline; -// table.DestroyQueryPool = ::DestroyQueryPool; -// table.DestroyFence = ::DestroyFence; + table.DestroyCommandAllocator = ::DestroyCommandAllocator; + table.DestroyCommandBuffer = ::DestroyCommandBuffer; + table.DestroyDescriptorPool = ::DestroyDescriptorPool; + table.DestroyBuffer = ::DestroyBuffer; + table.DestroyTexture = ::DestroyTexture; + table.DestroyDescriptor = ::DestroyDescriptor; + table.DestroyPipelineLayout = ::DestroyPipelineLayout; + table.DestroyPipeline = ::DestroyPipeline; + table.DestroyQueryPool = ::DestroyQueryPool; + table.DestroyFence = ::DestroyFence; // table.AllocateMemory = ::AllocateMemory; // table.BindBufferMemory = ::BindBufferMemory; // table.BindTextureMemory = ::BindTextureMemory; -// table.FreeMemory = ::FreeMemory; + table.FreeMemory = ::FreeMemory; table.BeginCommandBuffer = ::BeginCommandBuffer; table.CmdSetDescriptorPool = ::CmdSetDescriptorPool; table.CmdSetDescriptorSet = ::CmdSetDescriptorSet; @@ -349,7 +426,7 @@ static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPo // table.UpdateDescriptorRanges = ::UpdateDescriptorRanges; // table.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers; // table.CopyDescriptorSet = ::CopyDescriptorSet; -// table.AllocateDescriptorSets = ::AllocateDescriptorSets; + table.AllocateDescriptorSets = ::AllocateDescriptorSets; // table.ResetDescriptorPool = ::ResetDescriptorPool; table.ResetCommandAllocator = ::ResetCommandAllocator; table.MapBuffer = ::MapBuffer; diff --git a/Source/Metal/MemoryMTL.h b/Source/Metal/MemoryMTL.h index a8deeb2b..db196a91 100644 --- a/Source/Metal/MemoryMTL.h +++ b/Source/Metal/MemoryMTL.h @@ -28,16 +28,16 @@ struct MemoryMTL { return m_Type; } - inline uint8_t* GetMappedMemory() const { - return m_MappedMemory; - } +// inline uint8_t* GetMappedMemory() const { +// return m_MappedMemory; +// } ~MemoryMTL(); - Result Create(const MemoryMTLDesc& memoryDesc); + // Result Create(const MemoryMTLDesc& memoryDesc); Result Create(const AllocateMemoryDesc& allocateMemoryDesc); - //Result CreateDedicated(const BufferVK& buffer); - //Result CreateDedicated(const TextureVK& texture); + // Result CreateDedicated(const BufferVK& buffer); + // Result CreateDedicated(const TextureVK& texture); //================================================================================================================ // NRI @@ -47,8 +47,7 @@ struct MemoryMTL { private: DeviceMTL& m_Device; id m_Handle; - uint8_t* m_MappedMemory = nullptr; - MemoryType m_Type = std::numeric_limits::max(); + MemoryType m_Type; float m_Priority = 0.0f; bool m_OwnsNativeObjects = true; }; diff --git a/Source/Metal/MemoryMTL.mm b/Source/Metal/MemoryMTL.mm index 18aa0524..00accab2 100644 --- a/Source/Metal/MemoryMTL.mm +++ b/Source/Metal/MemoryMTL.mm @@ -14,35 +14,49 @@ Result MemoryMTL::Create(const AllocateMemoryDesc& allocateMemoryDesc) { MTLHeapDescriptor* heapDescriptor = [[MTLHeapDescriptor alloc] init]; - [heapDescriptor setSize:allocateMemoryDesc.size]; - + MemoryTypeInfo typeInfo; + typeInfo.value = allocateMemoryDesc.type; + [heapDescriptor setCpuCacheMode: (MTLCPUCacheMode)typeInfo.cacheMode]; + [heapDescriptor setStorageMode: (MTLStorageMode)typeInfo.storageMode]; + [heapDescriptor setResourceOptions: (MTLResourceOptions)typeInfo.options]; + [heapDescriptor setSize: allocateMemoryDesc.size]; + m_Handle = [m_Device newHeapWithDescriptor: heapDescriptor]; + m_Type = allocateMemoryDesc.type; + m_Priority = allocateMemoryDesc.priority; } -Result MemoryMTL::Create(const MemoryMTLDesc& memoryDesc) { - MTLHeapDescriptor* heapDescriptor = [[MTLHeapDescriptor alloc] init]; - [heapDescriptor setSize:memoryDesc.size]; - - // [heapDescriptor setStorageMode:memoryDesc.storage]; - - // if (!memoryDesc.vkDeviceMemory) - // return Result::INVALID_ARGUMENT; - - // MemoryTypeInfo memoryTypeInfo = {}; - - //bool found = m_Device.GetMemoryTypeByIndex(memoryDesc.memoryTypeIndex, memoryTypeInfo); - //RETURN_ON_FAILURE(&m_Device, found, Result::INVALID_ARGUMENT, "Can't find memory by index"); - -// m_OwnsNativeObjects = false; - // m_Buffer = memoryDesc.buffer; -// m_MappedMemory = (uint8_t*)memoryDesc.mappedMemory; - // m_Type = memoryDesc.options; +void MemoryMTL::SetDebugName(const char* name) { + NSString* str = [NSString stringWithUTF8String:name]; + [m_Handle setLabel: str]; +} - // const auto& mtl = m_Device.GetDispatchTable(); - //if (!m_MappedMemory && IsHostVisibleMemory(memoryTypeInfo.location)) { - // VkResult result = mtl.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 MemoryMTL::Create(const MemoryMTLDesc& memoryDesc) { +// MTLHeapDescriptor* heapDescriptor = [[MTLHeapDescriptor alloc] init]; +// +// [heapDescriptor setSize:memoryDesc.size]; +// +// // [heapDescriptor setStorageMode:memoryDesc.storage]; +// +// // if (!memoryDesc.vkDeviceMemory) +// // return Result::INVALID_ARGUMENT; +// +// // MemoryTypeInfo memoryTypeInfo = {}; +// +// //bool found = m_Device.GetMemoryTypeByIndex(memoryDesc.memoryTypeIndex, memoryTypeInfo); +// //RETURN_ON_FAILURE(&m_Device, found, Result::INVALID_ARGUMENT, "Can't find memory by index"); +// +//// m_OwnsNativeObjects = false; +// // m_Buffer = memoryDesc.buffer; +//// m_MappedMemory = (uint8_t*)memoryDesc.mappedMemory; +// // m_Type = memoryDesc.options; +// +// // const auto& mtl = m_Device.GetDispatchTable(); +// //if (!m_MappedMemory && IsHostVisibleMemory(memoryTypeInfo.location)) { +// // VkResult result = mtl.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; +//} diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index 785f948e..43518867 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -14,21 +14,30 @@ NriEnum(PipelineType, uint8_t, ); struct PipelineMTL { - +public: inline PipelineMTL(DeviceMTL& device) : m_Device(device) { } ~PipelineMTL(); + + inline PipelineType GetPipelineType() { + return m_PipelineType; + } Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); Result Create(const ComputePipelineDesc& computePipelineDesc); Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); - PipelineType m_pipelineType; MTLPrimitiveTopologyClass m_topologyClass; MTLPrimitiveType m_primitiveType; StageBits m_usedBits; + private: + PipelineType m_PipelineType; + union{ + id m_ComputePipeline = nil; + id m_GraphicsPipeline; + }; DeviceMTL& m_Device; }; diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index 9a74abc0..61d72f48 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -1,14 +1,54 @@ #include "SharedMTL.h" #include "PipelineMTL.h" +#include "PipelineLayoutMTL.h" using namespace nri; PipelineMTL::~PipelineMTL() { + switch(m_PipelineType) { + case PipelineType::Graphics: + m_GraphicsPipeline = nil; + break; + case PipelineType::Compute: + m_ComputePipeline = nil; + break; + case PipelineType::Raytracing: + break; + default: + break; + } } Result PipelineMTL::Create(const ComputePipelineDesc& computePipelineDesc) { + MTLComputePipelineDescriptor* pipelineDesc = [[MTLComputePipelineDescriptor alloc] init]; + NSError* error = nil; + + dispatch_data_t byteCode = dispatch_data_create( + computePipelineDesc.shader.bytecode, + computePipelineDesc.shader.size, nil, DISPATCH_DATA_DESTRUCTOR_DEFAULT); + + id lib = [m_Device newLibraryWithData: byteCode error:&error]; + NSCAssert(lib, @"Failed to load Metal shader library %@", error); // not sure how to correctly report this + RETURN_ON_FAILURE(&m_Device, lib, Result::FAILURE, "Failed to Load Metal shader library"); + // Create a MTLFunction from the loaded MTLLibrary. + NSString *entryPointNStr = [lib functionNames][0]; + if (computePipelineDesc.shader.entryPointName) { + entryPointNStr = [[NSString alloc] initWithUTF8String:computePipelineDesc.shader.entryPointName]; + } + id entryPointFunc = [lib newFunctionWithName:entryPointNStr]; + + m_PipelineType = PipelineType::Compute; + pipelineDesc.computeFunction = entryPointFunc; + NSCAssert(pipelineDesc.computeFunction, @"Failed to create Metal kernel function %@: %@", entryPointNStr, error); + + + m_ComputePipeline = [m_Device newComputePipelineStateWithDescriptor: pipelineDesc + options: MTLPipelineOptionNone + reflection: nil + error:&error]; + NSCAssert(m_ComputePipeline, @"Failed to create pipeline state: %@", error); return Result::SUCCESS; } @@ -118,6 +158,11 @@ renderPipelineDesc.colorAttachments[i].writeMask = GetColorComponent(attachmentDesc.colorWriteMask); } + NSError* error = nil; + m_GraphicsPipeline = [m_Device newRenderPipelineStateWithDescriptor:renderPipelineDesc error: &error]; + NSCAssert(m_GraphicsPipeline, @"Failed to create pipeline state: %@", error); + + return Result::SUCCESS; } diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index 7f60993b..834c2583 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -5,7 +5,9 @@ namespace nri { -struct DeviceMTL; +struct DeviceMTL; + +class MemoryMTL; void fillMTLTextureDescriptor(const TextureDesc& textureDesc, MTLTextureDescriptor* mtlDescriptor); @@ -29,13 +31,22 @@ struct TextureMTL { } Result Create(const TextureDesc& textureDesc); + void FinishMemoryBinding(MemoryMTL& memory, uint64_t memoryOffset); + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + private: - // Result CreateFromTextureDesc(const TextureDesc& textureDesc); + void UpdateLabel(); + NSString* m_Label = nullptr; DeviceMTL& m_Device; TextureDesc m_Desc = {}; id m_Handle; + NSString* m_label; }; } // namespace nri diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm index ed6ec638..b6f7fe12 100644 --- a/Source/Metal/TextureMTL.mm +++ b/Source/Metal/TextureMTL.mm @@ -1,11 +1,13 @@ #include "SharedMTL.h" #include "TextureMTL.h" +#include "MemoryMTL.h" using namespace nri; TextureMTL::~TextureMTL() { m_Handle = nil; + [m_label release]; } void nri::fillMTLTextureDescriptor(const TextureDesc& textureDesc, MTLTextureDescriptor* info) { @@ -19,29 +21,28 @@ info.arrayLength = textureDesc.layerNum; } -//Result TextureMTL::Create(const TextureMTLDesc& textureDesc) { -// m_Handle = texturedesc.texture; -// return Result::SUCCESS; -//} +Result TextureMTL::Create(const TextureDesc& textureDesc) { + m_Desc = textureDesc; + + return Result::SUCCESS; +} -Result TextureMTL::Create(const TextureDesc& textureDesc) { +void TextureMTL::FinishMemoryBinding(MemoryMTL& memory, uint64_t memoryOffset) { MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init]; - fillMTLTextureDescriptor(textureDesc, info); - -// info.textureType = ::GetImageTypeMTL(textureDesc.type); -// info.pixelFormat = ::GetFormatMTL(textureDesc.format, true); -// info.width = textureDesc.width; -// info.height = textureDesc.height; -// info.depth = textureDesc.depth; -// info.mipmapLevelCount = textureDesc.mipNum; -// info.sampleCount = textureDesc.sampleNum; -// info.arrayLength = textureDesc.layerNum; -// - m_Handle = [m_Device newTextureWithDescriptor:info]; - m_Desc = textureDesc; + fillMTLTextureDescriptor(m_Desc, info); + m_Handle = [memory.GetHandle() newTextureWithDescriptor:info offset:memoryOffset]; + UpdateLabel(); +} - //m_Handle = [m_Device newTextureWithDescriptor:textureDesc]; - return Result::SUCCESS; +void TextureMTL::UpdateLabel() { + if(m_Handle && m_Label) { + [m_Handle setLabel: m_Label]; + } } +void TextureMTL::SetDebugName(const char* name) { + m_label = [NSString stringWithUTF8String:name]; + [m_label retain]; + UpdateLabel(); +} diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index 583a3c83..2755c78a 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -181,6 +181,8 @@ inline nri::Vendor GetVendorFromID(uint32_t vendorID) { return nri::Vendor::AMD; case 0x8086: return nri::Vendor::INTEL; + case 0x106b: + return nri::Vendor::APPLE; } return nri::Vendor::UNKNOWN; From a84371e369ab12adeb920d6615e4ecea58b85427 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sat, 30 Nov 2024 21:11:41 -0800 Subject: [PATCH 24/27] feat: add descriptor binding logic Signed-off-by: Michael Pollind --- Source/Metal/DescriptorMTL.h | 1 - Source/Metal/DescriptorPoolMTL.h | 14 +++--- Source/Metal/DescriptorPoolMTL.mm | 45 +++++++++++++++++++- Source/Metal/DescriptorSetMTL.h | 12 +++--- Source/Metal/DescriptorSetMTL.mm | 41 ++++++++++++------ Source/Metal/PipelineLayoutMTL.h | 48 ++++++++++++++++++--- Source/Metal/PipelineLayoutMTL.mm | 71 +++++++++++++++++++++++++------ 7 files changed, 184 insertions(+), 48 deletions(-) diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index d0af5c8c..903fb9c7 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -50,7 +50,6 @@ struct DescriptorMTL { Result Create(const Texture3DViewDesc& textureViewDesc); Result Create(const SamplerDesc& samplerDesc); - private: DeviceMTL& m_Device; diff --git a/Source/Metal/DescriptorPoolMTL.h b/Source/Metal/DescriptorPoolMTL.h index 060fad1f..c21b0fbd 100644 --- a/Source/Metal/DescriptorPoolMTL.h +++ b/Source/Metal/DescriptorPoolMTL.h @@ -15,31 +15,29 @@ struct DescriptorPoolMTL { , m_AllocatedSets(device.GetStdAllocator()) { m_AllocatedSets.reserve(64); } -// -// inline operator VkDescriptorPool() const { -// return m_Handle; -// } -// + inline DeviceMTL& GetDevice() const { return m_Device; } ~DescriptorPoolMTL(); - Result Create(const DescriptorPoolDesc& descriptorPoolDesc); //================================================================================================================ // NRI //================================================================================================================ - + +// size_t GetNumberOfArugmentsAlloc(); void SetDebugName(const char* name); void Reset(); Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndex, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum); private: DeviceMTL& m_Device; + size_t m_ArgumentOffset = 0; Vector m_AllocatedSets; - //VkDescriptorPool m_Handle = VK_NULL_HANDLE; + id m_ArgumentBuffer; + uint32_t m_UsedSets = 0; bool m_OwnsNativeObjects = true; }; diff --git a/Source/Metal/DescriptorPoolMTL.mm b/Source/Metal/DescriptorPoolMTL.mm index 05dd2d53..adb64880 100644 --- a/Source/Metal/DescriptorPoolMTL.mm +++ b/Source/Metal/DescriptorPoolMTL.mm @@ -1,6 +1,8 @@ #include "SharedMTL.h" #include "DescriptorPoolMTL.h" +#include "DescriptorSetMTL.h" +#include "PipelineLayoutMTL.h" using namespace nri; @@ -10,9 +12,25 @@ } Result DescriptorPoolMTL::Create(const DescriptorPoolDesc& descriptorPoolDesc) { - + size_t numArgs = descriptorPoolDesc.samplerMaxNum + + descriptorPoolDesc.constantBufferMaxNum + + descriptorPoolDesc.dynamicConstantBufferMaxNum + + descriptorPoolDesc.textureMaxNum + + descriptorPoolDesc.storageTextureMaxNum + + descriptorPoolDesc.bufferMaxNum + + descriptorPoolDesc.storageBufferMaxNum + + descriptorPoolDesc.structuredBufferMaxNum + + descriptorPoolDesc.accelerationStructureMaxNum; + + m_ArgumentBuffer = [m_Device + newBufferWithLength: numArgs * sizeof(uint32_t) options:MTLResourceStorageModeShared]; } + +//size_t DescriptorPoolMTL::GetNumberOfArugmentsAlloc() { +// return [m_ArgumentBuffer length] / sizeof(uint32_t); +//} + //================================================================================================================ // NRI //================================================================================================================ @@ -23,7 +41,32 @@ void DescriptorPoolMTL::Reset() { } + Result DescriptorPoolMTL::AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndex, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum) { + PipelineLayoutMTL* pipelineLayoutMTL = (PipelineLayoutMTL*)&pipelineLayout; + + uint32_t freeSetNum = (uint32_t)m_AllocatedSets.size() - m_UsedSets; + if (freeSetNum < instanceNum) { + uint32_t newSetNum = instanceNum - freeSetNum; + uint32_t prevSetNum = (uint32_t)m_AllocatedSets.size(); + m_AllocatedSets.resize(prevSetNum + newSetNum); + for (size_t i = 0; i < newSetNum; i++) { + Construct(m_AllocatedSets[prevSetNum + i], 1, m_Device); + } + } + + struct DescriptorSetLayout* setLayoutMTL = pipelineLayoutMTL->GetDescriptorSetLayout(setIndex); + for(uint32_t i = 0; i < instanceNum; i++) { + descriptorSets[i] = (DescriptorSet*)m_AllocatedSets[m_UsedSets++]; + ((DescriptorSetMTL*)descriptorSets[i])->Create( + m_ArgumentOffset, + m_ArgumentBuffer, + setLayoutMTL->m_ArgumentDescriptors, + &setLayoutMTL->m_DescriptorSetDesc); + m_ArgumentOffset += ((DescriptorSetMTL*)descriptorSets[i])->getDescriptorLength(); + } + + return Result::SUCCESS; } diff --git a/Source/Metal/DescriptorSetMTL.h b/Source/Metal/DescriptorSetMTL.h index 61cce70d..dfb520d3 100644 --- a/Source/Metal/DescriptorSetMTL.h +++ b/Source/Metal/DescriptorSetMTL.h @@ -11,19 +11,21 @@ struct DescriptorSetMTL { : m_Device(device) { } - + void Create(size_t argumentBufferOffset, id argumentBuffer, NSArray* argDesc, const struct DescriptorSetDesc* desc); void UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); - inline id GetArgumentHandle() { return m_ArgumentEncoder; } + size_t getDescriptorLength(); private: DeviceMTL& m_Device; - id m_ArgumentEncoder; - + id m_ArgumentEncoder; + id m_ArgumentBuffer; + size_t m_ArgumentBufferOffset; + NSArray* m_ArgumentDescriptor; + const DescriptorSetDesc* m_Desc = nullptr; }; - } // namespace nri diff --git a/Source/Metal/DescriptorSetMTL.mm b/Source/Metal/DescriptorSetMTL.mm index 5c5b2cdd..9ae3d478 100644 --- a/Source/Metal/DescriptorSetMTL.mm +++ b/Source/Metal/DescriptorSetMTL.mm @@ -7,30 +7,45 @@ using namespace nri; +void DescriptorSetMTL::Create(size_t argumentBufferOffset, id argumentBuffer, NSArray* argDesc, const struct DescriptorSetDesc* desc) { + m_ArgumentDescriptor = argDesc; + m_ArgumentBuffer = argumentBuffer; + m_ArgumentBufferOffset = argumentBufferOffset; + m_Desc = desc; + m_ArgumentEncoder = [m_Device newArgumentEncoderWithArguments: argDesc]; + [m_ArgumentEncoder setArgumentBuffer:m_ArgumentBuffer offset:argumentBufferOffset]; +} + +size_t DescriptorSetMTL::getDescriptorLength() { + return [m_ArgumentEncoder encodedLength]; +} + void DescriptorSetMTL::UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs) { for(size_t j = 0; j < rangeNum; j++) { const DescriptorRangeUpdateDesc& update = rangeUpdateDescs[j]; - -// uint32_t offset = update.baseDescriptor + descriptorOffset; - for(size_t descIdx = 0; descIdx < update.descriptorNum; descIdx++) { + DescriptorMTL* descriptorImpl = (DescriptorMTL*)&update.descriptors[descIdx]; + const DescriptorRangeDesc& rangeDesc = m_Desc->ranges[rangeOffset + j]; - DescriptorMTL& descriptorImpl = *(DescriptorMTL*)&update.descriptors[descIdx]; - switch(descriptorImpl.GetType()) { + switch(descriptorImpl->GetType()) { case DescriptorTypeMTL::IMAGE_VIEW_1D: - [m_ArgumentEncoder setTexture: descriptorImpl.GetTextureHandle() atIndex:0]; -// [m_ArgumentEncoder setTextures:<#(id _Nullable const * _Nonnull)#> withRange:<#(NSRange)#>] - break; case DescriptorTypeMTL::IMAGE_VIEW_2D: - [m_ArgumentEncoder setTexture: descriptorImpl.GetTextureHandle() atIndex:0]; + case DescriptorTypeMTL::IMAGE_VIEW_3D: + [m_ArgumentEncoder setTexture: descriptorImpl->GetTextureHandle() atIndex: rangeDesc.baseRegisterIndex + descIdx]; break; - case DescriptorTypeMTL::BUFFER_VIEW: -// [m_ArgumentEncoder setBuffer: offset:<#(NSUInteger)#> atIndex:<#(NSUInteger)#>] + case DescriptorTypeMTL::SAMPLER: + [m_ArgumentEncoder setSamplerState: descriptorImpl->GetSamplerStateHandler() atIndex:rangeDesc.baseRegisterIndex + descIdx]; // not sure if this is correct + break; + case DescriptorTypeMTL::BUFFER_VIEW: { + BufferViewDesc* view = &descriptorImpl->BufferView(); + [m_ArgumentEncoder setBuffer: descriptorImpl->GetBufferHandle() offset: view->offset atIndex: rangeDesc.baseRegisterIndex + descIdx]; + break; + } + default: break; } - -// update.descriptors[descIdx]; + } } } diff --git a/Source/Metal/PipelineLayoutMTL.h b/Source/Metal/PipelineLayoutMTL.h index 2a4b080a..436549d3 100644 --- a/Source/Metal/PipelineLayoutMTL.h +++ b/Source/Metal/PipelineLayoutMTL.h @@ -6,23 +6,57 @@ namespace nri { struct DeviceMTL; + +// +//struct BindingInfo { +// BindingInfo(StdAllocator& allocator); +//// Vector pushConstantBindings; +//// Vector pushDescriptorBindings; +//}; + +struct DescriptorSetLayout { + DescriptorSetDesc m_DescriptorSetDesc; + NSMutableArray* m_ArgumentDescriptors; +}; + struct PipelineLayoutMTL { + inline PipelineLayoutMTL (DeviceMTL& device) - : m_Device(device) { + : m_Device(device) + , m_HasVariableDescriptorNum(device.GetStdAllocator()) + , m_DescriptorSetRangeDescs(device.GetStdAllocator()) + , m_DynamicConstantBufferDescs(device.GetStdAllocator()) + , m_DescriptorSets(device.GetStdAllocator()) + { } ~PipelineLayoutMTL(); - - Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); - struct PipelineDescriptorSet { - NSMutableArray* m_ArgumentDescriptors; - }; + inline DeviceMTL& GetDevice() const { + return m_Device; + } + + inline struct DescriptorSetLayout* GetDescriptorSetLayout(uint32_t setIndex) { + return &m_DescriptorSets[setIndex]; + } + +// inline struct DescriptorSetDesc* GetDescriptorSetDesc(uint32_t setIndex) { +// return &m_DescriptorSetDesc[setIndex]; +// } + + Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); + private: DeviceMTL& m_Device; - std::vector m_DescriptorSets; + Vector m_HasVariableDescriptorNum; + Vector m_DescriptorSetRangeDescs; + Vector m_DynamicConstantBufferDescs; +// Vector m_DescriptorSetDesc; + Vector m_DescriptorSets; + +// BindingInfo m_BindingInfo; }; } diff --git a/Source/Metal/PipelineLayoutMTL.mm b/Source/Metal/PipelineLayoutMTL.mm index 33b4c764..aefa22ce 100644 --- a/Source/Metal/PipelineLayoutMTL.mm +++ b/Source/Metal/PipelineLayoutMTL.mm @@ -4,31 +4,76 @@ using namespace nri; +// +//BindingInfo::BindingInfo(StdAllocator& allocator) +// : hasVariableDescriptorNum(allocator) +// , descriptorSetRangeDescs(allocator) +// , dynamicConstantBufferDescs(allocator) +// , descriptorSetDescs(allocator) { +// +//} +// + PipelineLayoutMTL::~PipelineLayoutMTL() { } Result PipelineLayoutMTL::Create(const PipelineLayoutDesc& pipelineLayoutDesc) { + + + size_t rangeNum = 0; + size_t dynamicConstantBufferNum = 0; + for (uint32_t i = 0; i < pipelineLayoutDesc.descriptorSetNum; i++) { + rangeNum += pipelineLayoutDesc.descriptorSets[i].rangeNum; + dynamicConstantBufferNum += pipelineLayoutDesc.descriptorSets[i].dynamicConstantBufferNum; + } + m_DescriptorSets.resize(pipelineLayoutDesc.descriptorSetNum); + m_HasVariableDescriptorNum.resize(pipelineLayoutDesc.descriptorSetNum); + m_DescriptorSetRangeDescs.reserve(rangeNum); + m_DynamicConstantBufferDescs.reserve(dynamicConstantBufferNum); + for (uint32_t i = 0; i < pipelineLayoutDesc.descriptorSetNum; i++) { const DescriptorSetDesc& descriptorSetDesc = pipelineLayoutDesc.descriptorSets[i]; - NSMutableArray* argumentDescriptors = [[NSMutableArray alloc] init]; - MTLArgumentDescriptor* argDescriptor = [MTLArgumentDescriptor argumentDescriptor]; + // Binding info + m_HasVariableDescriptorNum[i] = false; + m_DescriptorSets[i].m_DescriptorSetDesc = descriptorSetDesc; + m_DescriptorSets[i].m_DescriptorSetDesc.ranges = m_DescriptorSetRangeDescs.data() +m_DescriptorSetRangeDescs.size(); + m_DescriptorSets[i].m_DescriptorSetDesc.dynamicConstantBuffers = m_DynamicConstantBufferDescs.data() + m_DynamicConstantBufferDescs.size(); + m_DescriptorSetRangeDescs.insert(m_DescriptorSetRangeDescs.end(), descriptorSetDesc.ranges, descriptorSetDesc.ranges + descriptorSetDesc.rangeNum); + m_DynamicConstantBufferDescs.insert(m_DynamicConstantBufferDescs.end(), descriptorSetDesc.dynamicConstantBuffers, descriptorSetDesc.dynamicConstantBuffers + descriptorSetDesc.dynamicConstantBufferNum); + NSMutableArray* argumentDescriptors = [[NSMutableArray alloc] init]; for(size_t r = 0; r < descriptorSetDesc.rangeNum; r++) { - + MTLArgumentDescriptor* argDescriptor = [MTLArgumentDescriptor argumentDescriptor]; + const DescriptorRangeDesc* range = &descriptorSetDesc.ranges[r]; + argDescriptor.arrayLength = range->descriptorNum; + argDescriptor.access = MTLBindingAccessReadWrite; + argDescriptor.index = range->baseRegisterIndex; + switch(range->descriptorType) { + case DescriptorType::TEXTURE: + argDescriptor.dataType = MTLDataTypeTexture; + argDescriptor.textureType = MTLTextureType2D; // descriptor type does not have this + break; + case DescriptorType::SAMPLER: + argDescriptor.dataType = MTLDataTypeSampler; + break; + case DescriptorType::CONSTANT_BUFFER: + case DescriptorType::STORAGE_TEXTURE: + case DescriptorType::BUFFER: + case DescriptorType::STORAGE_BUFFER: + case DescriptorType::STRUCTURED_BUFFER: + case DescriptorType::STORAGE_STRUCTURED_BUFFER: + argDescriptor.dataType = MTLDataTypeStruct; + break; + case DescriptorType::ACCELERATION_STRUCTURE: + argDescriptor.dataType = MTLDataTypePrimitiveAccelerationStructure; + break; + default: + break; + } } - - - //argDescriptor.access = memberDescriptor.mAccessType; - //argDescriptor.arrayLength = memberDescriptor.mArrayLength; - //argDescriptor.constantBlockAlignment = memberDescriptor.mAlignment; - //argDescriptor.dataType = memberDescriptor.mDataType; - //argDescriptor.index = memberDescriptor.mArgumentIndex; - //argDescriptor.textureType = memberDescriptor.mTextureType; - - [argumentDescriptors addObject:argDescriptor]; m_DescriptorSets[i].m_ArgumentDescriptors = argumentDescriptors; } From 0eaa1a5541a50983ab70bcc5a37304f35cdd1130 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Tue, 24 Dec 2024 23:27:38 -0800 Subject: [PATCH 25/27] feat: more progress with CommandBufferMTL Signed-off-by: Michael Pollind --- Source/Metal/CommandBufferMTL.h | 24 ++--- Source/Metal/CommandBufferMTL.mm | 164 ++++++++++++++++++++++++++---- Source/Metal/DescriptorMTL.h | 3 - Source/Metal/DescriptorMTL.mm | 10 +- Source/Metal/DescriptorPoolMTL.mm | 2 +- Source/Metal/DescriptorSetMTL.h | 8 ++ Source/Metal/DescriptorSetMTL.mm | 2 +- Source/Metal/DeviceMTL.h | 3 +- Source/Metal/FencMTL.mm | 5 + Source/Metal/FenceMTL.h | 2 +- Source/Metal/ImplMTL.mm | 26 +++-- Source/Metal/PipelineLayoutMTL.h | 26 ++--- Source/Metal/PipelineLayoutMTL.mm | 28 ++--- Source/Metal/PipelineMTL.mm | 7 +- Source/Metal/TextureMTL.h | 5 + Source/Metal/TextureMTL.mm | 1 - 16 files changed, 222 insertions(+), 94 deletions(-) diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index f4f03498..405dccc5 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -16,16 +16,16 @@ struct DescriptorMTL; NriBits(CommandBufferDirtyBits, uint8_t, - NONE = 0, - CMD_DIRTY_STENCIL = NriBit(0) -); + NONE = 0, + CMD_DIRTY_STENCIL = NriBit(0) + ); struct CommandBufferMTL { - + inline CommandBufferMTL(DeviceMTL& device) : m_Device(device) { } - + inline DeviceMTL& GetDevice() const { return m_Device; } @@ -33,7 +33,7 @@ struct CommandBufferMTL { inline operator id() const { return m_Handle; } - + void SetDebugName(const char* name); Result Begin(const DescriptorPool* descriptorPool); @@ -50,7 +50,7 @@ struct CommandBufferMTL { void SetScissors(const Rect* rects, uint32_t rectNum); void SetDepthBounds(float boundsMin, float boundsMax); void SetStencilReference(uint8_t frontRef, uint8_t backRef); - // void SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum); + // void SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum); void SetBlendConstants(const Color32f& color); void SetShadingRate(const ShadingRateDesc& shadingRateDesc); void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); @@ -86,15 +86,15 @@ struct CommandBufferMTL { void DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); ~CommandBufferMTL(); - + void Create(id cmd); - + struct CmdIndexBuffer { size_t m_Offset; MTLIndexType m_Type; struct BufferMTL* m_Buffer; }; - + private: void updateCommandBufferState(); @@ -102,11 +102,11 @@ struct CommandBufferMTL { DeviceMTL& m_Device; struct PipelineMTL* m_CurrentPipeline = nullptr; struct CmdIndexBuffer m_CurrentIndexCmd; + PipelineLayoutMTL* m_CurrentPipelineLayout = nullptr; id m_Handle; id m_RendererEncoder = nil; id m_ComputeEncoder = nil; - - //id + id m_BlitEncoder = nil; CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE; }; diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 66bd664a..47f17c82 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -6,8 +6,10 @@ #include "CommandBufferMTL.h" #include "CommandQueueMTL.h" #include "DescriptorMTL.h" +#include "DescriptorSetMTL.h" #include "PipelineLayoutMTL.h" #include "PipelineMTL.h" +#include "PipelineLayoutMTL.h" #include "TextureMTL.h" #include @@ -37,17 +39,49 @@ return; PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline; m_CurrentPipeline = &pipelineImpl; - -// if(m_CurrentPipeline->m_pipelineType == nri::PipelineMTL::Compute) { - // m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL]; - // } - } -void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) { +void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) { + PipelineLayoutMTL& pipelineLayoutMTL = (PipelineLayoutMTL&)pipelineLayout; + m_CurrentPipelineLayout = &pipelineLayoutMTL; } + void CommandBufferMTL::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { + const DescriptorSetMTL& descriptorSetImpl = (DescriptorSetMTL&)descriptorSet; + auto* layout = m_CurrentPipelineLayout->GetDescriptorSetLayout(setIndexInPipelineLayout); + const uint32_t space = layout->m_DescriptorSetDesc.registerSpace; + bool setFragmentBuffer = false; + bool setVertexBuffer = false; + + for(size_t i = 0; i < layout->m_DescriptorSetDesc.rangeNum; i++) { + if(layout->m_DescriptorSetDesc.ranges[i].shaderStages == StageBits::ALL) { + setFragmentBuffer = true; + setVertexBuffer = true; + break; + } + if(layout->m_DescriptorSetDesc.ranges[i].shaderStages & StageBits::VERTEX_SHADER) { + setVertexBuffer = true; + break; + } + if(layout->m_DescriptorSetDesc.ranges[i].shaderStages & StageBits::GRAPHICS_SHADERS) { + setFragmentBuffer = true; + break; + } + } + if(setFragmentBuffer) { + [m_RendererEncoder setFragmentBuffer: descriptorSetImpl.GetArgumentBuffer() + offset: descriptorSetImpl.GetArugmentBufferOffset() + atIndex: space]; + } + if(setVertexBuffer) { + [m_RendererEncoder setVertexBuffer: descriptorSetImpl.GetArgumentBuffer() + offset: descriptorSetImpl.GetArugmentBufferOffset() + atIndex: space]; + + } + + } void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) { //if (pDesc->mUsedStages & SHADER_STAGE_VERT) @@ -66,8 +100,13 @@ //} } -void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) {} + +void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) { + +} + void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) { + //if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_BUFFERS) { [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers @@ -101,7 +140,6 @@ renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionClear; renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; - } if(attachmentsDesc.depthStencil) { @@ -144,13 +182,12 @@ [m_RendererEncoder setScissorRect: rect]; } void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) { - + //[m_RendererEncoder set] } void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) { [m_RendererEncoder setStencilFrontReferenceValue: frontRef backReferenceValue:backRef]; } -//void CommandBufferMTL::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) { -//} + void CommandBufferMTL::SetBlendConstants(const Color32f& color) { [m_RendererEncoder setBlendColorRed:color.x @@ -160,10 +197,12 @@ ]; } void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) { - + //[m_RendererEncoder sha] } void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { + + } void CommandBufferMTL::SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType) { @@ -195,7 +234,7 @@ vertexStart:drawDesc.baseVertex vertexCount:drawDesc.vertexNum instanceCount:drawDesc.instanceNum - baseInstance:0]; + baseInstance: 0]; } void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) { @@ -237,12 +276,99 @@ void CommandBufferMTL::EndQuery(const QueryPool& queryPool, uint32_t offset) {} void CommandBufferMTL::BeginAnnotation(const char* name) {} void CommandBufferMTL::EndAnnotation() {} -void CommandBufferMTL::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) {} -void CommandBufferMTL::ClearStorageTexture(const ClearStorageTextureDesc& clearDesc) {} -void CommandBufferMTL::CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) {} -void CommandBufferMTL::CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) {} -void CommandBufferMTL::UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) {} -void CommandBufferMTL::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) {} +void CommandBufferMTL::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) { + +} +void CommandBufferMTL::ClearStorageTexture(const ClearStorageTextureDesc& clearDesc) { + +} + +void CommandBufferMTL::CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) { + const BufferMTL& src = (const BufferMTL&)srcBuffer; + const BufferMTL& dst = (const BufferMTL&)dstBuffer; + [m_BlitEncoder + copyFromBuffer:src.GetHandle() + sourceOffset:srcOffset + toBuffer: dst.GetHandle() + destinationOffset:dstOffset + size:size]; +} + +void CommandBufferMTL::CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) { + const TextureMTL& src = (const TextureMTL&)srcTexture; + const TextureMTL& dst = (const TextureMTL&)dstTexture; + bool isWholeResource = !dstRegionDesc && !srcRegionDesc; + if (isWholeResource) { + [m_BlitEncoder + copyFromTexture: src.GetHandle() + toTexture: dst.GetHandle()]; + } else { + TextureRegionDesc wholeResource = {}; + if (!srcRegionDesc) + srcRegionDesc = &wholeResource; + if (!dstRegionDesc) + dstRegionDesc = &wholeResource; + const MTLSize sourceSize = MTLSizeMake( + (srcRegionDesc->width == WHOLE_SIZE) ? src.GetSize(0, srcRegionDesc->mipOffset) : srcRegionDesc->width, + (srcRegionDesc->height == WHOLE_SIZE) ? src.GetSize(1, srcRegionDesc->mipOffset) : srcRegionDesc->height, + (srcRegionDesc->depth == WHOLE_SIZE) ? src.GetSize(2, srcRegionDesc->mipOffset) : srcRegionDesc->depth + ); + + // Copy to the texture's final subresource. + [m_BlitEncoder copyFromTexture: src.GetHandle() + sourceSlice: srcRegionDesc->layerOffset + sourceLevel: srcRegionDesc->mipOffset + sourceOrigin: MTLOriginMake(srcRegionDesc->x, srcRegionDesc->y, srcRegionDesc->z) + sourceSize: sourceSize + toTexture: dst.GetHandle() + destinationSlice: dstRegionDesc->layerOffset + destinationLevel: dstRegionDesc->mipOffset + destinationOrigin: MTLOriginMake(dstRegionDesc->x, dstRegionDesc->y, dstRegionDesc->z) + ]; + } +} +void CommandBufferMTL::UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { + const BufferMTL& src = (const BufferMTL&)srcBuffer; + const TextureMTL& dst = (const TextureMTL&)dstTexture; + + const MTLSize sourceSize = MTLSizeMake( + (dstRegionDesc.width == WHOLE_SIZE) ? dst.GetSize(0, dstRegionDesc.mipOffset) : dstRegionDesc.width, + (dstRegionDesc.height == WHOLE_SIZE) ? dst.GetSize(1, dstRegionDesc.mipOffset) : dstRegionDesc.height, + (dstRegionDesc.depth == WHOLE_SIZE) ? dst.GetSize(2, dstRegionDesc.mipOffset) : dstRegionDesc.depth + ); + [m_BlitEncoder + copyFromBuffer: src.GetHandle() + sourceOffset: srcDataLayoutDesc.offset + sourceBytesPerRow: srcDataLayoutDesc.rowPitch + sourceBytesPerImage: srcDataLayoutDesc.slicePitch + sourceSize: sourceSize + toTexture: dst.GetHandle() + destinationSlice: dstRegionDesc.layerOffset + destinationLevel: dstRegionDesc.mipOffset + destinationOrigin: MTLOriginMake(dstRegionDesc.x, dstRegionDesc.y, dstRegionDesc.z) + options: MTLBlitOptionNone]; +} +void CommandBufferMTL::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { + const TextureMTL& src = (const TextureMTL&)srcTexture; + const BufferMTL& dst = (const BufferMTL&)dstBuffer; + + + const MTLSize sourceSize = MTLSizeMake( + (srcRegionDesc.width == WHOLE_SIZE) ? src.GetSize(0, srcRegionDesc.mipOffset) : srcRegionDesc.width, + (srcRegionDesc.height == WHOLE_SIZE) ? src.GetSize(1, srcRegionDesc.mipOffset) : srcRegionDesc.height, + (srcRegionDesc.depth == WHOLE_SIZE) ? src.GetSize(2, srcRegionDesc.mipOffset) : srcRegionDesc.depth + ); + [m_BlitEncoder copyFromTexture: src.GetHandle() + sourceSlice: srcRegionDesc.layerOffset + sourceLevel: srcRegionDesc.mipOffset + sourceOrigin: MTLOriginMake(srcRegionDesc.x, srcRegionDesc.y, srcRegionDesc.z) + sourceSize: sourceSize + toBuffer: dst.GetHandle() + destinationOffset: dstDataLayoutDesc.offset + destinationBytesPerRow: dstDataLayoutDesc.rowPitch + destinationBytesPerImage: dstDataLayoutDesc.slicePitch + options: MTLBlitOptionNone]; +} void CommandBufferMTL::CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) {} void CommandBufferMTL::ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num) {} void CommandBufferMTL::BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) {} diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index 903fb9c7..b5a7447c 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -57,13 +57,10 @@ struct DescriptorMTL { id m_Texture; id m_Buffer; id m_SamplerState; - union { BufferViewDesc m_BufferViewDesc; SamplerDesc m_SamplerViewDesc; }; - - }; } // namespace nri diff --git a/Source/Metal/DescriptorMTL.mm b/Source/Metal/DescriptorMTL.mm index fe9c189f..36a1c8cc 100644 --- a/Source/Metal/DescriptorMTL.mm +++ b/Source/Metal/DescriptorMTL.mm @@ -13,16 +13,13 @@ } - Result DescriptorMTL::Create(const BufferViewDesc& bufferViewDesc) { m_Type = DescriptorTypeMTL::BUFFER_VIEW; const BufferMTL& buffer = *(const BufferMTL*)bufferViewDesc.buffer; - + m_Buffer = buffer.GetHandle(); return Result::SUCCESS; - } - Result DescriptorMTL::Create(const Texture1DViewDesc& textureViewDesc) { TextureMTL& texture = *(TextureMTL*)textureViewDesc.texture; const Mip_t remainingMips = textureViewDesc.mipNum - textureViewDesc.mipOffset; @@ -39,6 +36,7 @@ textureType: MTLTextureType1D levels:level slices:slices]; + m_Type = DescriptorTypeMTL::IMAGE_VIEW_1D; return Result::SUCCESS; @@ -59,6 +57,7 @@ textureType: MTLTextureType2D levels:level slices:slices]; + m_Type = DescriptorTypeMTL::IMAGE_VIEW_2D; return Result::SUCCESS; } @@ -78,6 +77,7 @@ textureType: MTLTextureType3D levels:level slices:slices]; + m_Type = DescriptorTypeMTL::IMAGE_VIEW_3D; return Result::SUCCESS; } @@ -94,7 +94,7 @@ [mtlDesc setLodMinClamp: samplerDesc.mipMin]; [mtlDesc setLodMaxClamp: samplerDesc.mipMax]; m_SamplerState = [m_Device newSamplerStateWithDescriptor: mtlDesc]; - + m_Type = DescriptorTypeMTL::SAMPLER; return Result::SUCCESS; } diff --git a/Source/Metal/DescriptorPoolMTL.mm b/Source/Metal/DescriptorPoolMTL.mm index adb64880..254f3cb6 100644 --- a/Source/Metal/DescriptorPoolMTL.mm +++ b/Source/Metal/DescriptorPoolMTL.mm @@ -55,7 +55,7 @@ } } - struct DescriptorSetLayout* setLayoutMTL = pipelineLayoutMTL->GetDescriptorSetLayout(setIndex); + const struct DescriptorSetLayout* setLayoutMTL = pipelineLayoutMTL->GetDescriptorSetLayout(setIndex); for(uint32_t i = 0; i < instanceNum; i++) { descriptorSets[i] = (DescriptorSet*)m_AllocatedSets[m_UsedSets++]; ((DescriptorSetMTL*)descriptorSets[i])->Create( diff --git a/Source/Metal/DescriptorSetMTL.h b/Source/Metal/DescriptorSetMTL.h index dfb520d3..06405e42 100644 --- a/Source/Metal/DescriptorSetMTL.h +++ b/Source/Metal/DescriptorSetMTL.h @@ -18,6 +18,14 @@ struct DescriptorSetMTL { } size_t getDescriptorLength(); + inline id GetArgumentBuffer() const { + return m_ArgumentBuffer; + } + + inline size_t GetArugmentBufferOffset() const { + return m_ArgumentBufferOffset; + } + private: DeviceMTL& m_Device; id m_ArgumentEncoder; diff --git a/Source/Metal/DescriptorSetMTL.mm b/Source/Metal/DescriptorSetMTL.mm index 9ae3d478..6e96909f 100644 --- a/Source/Metal/DescriptorSetMTL.mm +++ b/Source/Metal/DescriptorSetMTL.mm @@ -13,7 +13,7 @@ m_ArgumentBufferOffset = argumentBufferOffset; m_Desc = desc; m_ArgumentEncoder = [m_Device newArgumentEncoderWithArguments: argDesc]; - [m_ArgumentEncoder setArgumentBuffer:m_ArgumentBuffer offset:argumentBufferOffset]; + [m_ArgumentEncoder setArgumentBuffer: m_ArgumentBuffer offset:argumentBufferOffset]; } size_t DescriptorSetMTL::getDescriptorLength() { diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index 6839d001..eac2c876 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -30,8 +30,9 @@ struct DeviceMTL final : public DeviceBase { if (result != Result::SUCCESS) { Destroy(GetStdAllocator(), impl); entity = nullptr; - } else + } else { entity = (Interface*)impl; + } return result; } diff --git a/Source/Metal/FencMTL.mm b/Source/Metal/FencMTL.mm index d0ee5d55..e6a86204 100644 --- a/Source/Metal/FencMTL.mm +++ b/Source/Metal/FencMTL.mm @@ -11,3 +11,8 @@ Result FenceMTL::Create(uint64_t initialValue) { m_Handle = dispatch_semaphore_create(initialValue); } + + +void FenceMTL::Wait(uint64_t value) { + dispatch_semaphore_wait(m_Handle, DISPATCH_TIME_FOREVER); +} diff --git a/Source/Metal/FenceMTL.h b/Source/Metal/FenceMTL.h index 0531e63b..24b7cbe7 100644 --- a/Source/Metal/FenceMTL.h +++ b/Source/Metal/FenceMTL.h @@ -22,7 +22,7 @@ struct FenceMTL { //void SetDebugName(const char* name); //uint64_t GetFenceValue() const; - //void Wait(uint64_t value); + void Wait(uint64_t value); private: dispatch_semaphore_t m_Handle; DeviceMTL& m_Device; diff --git a/Source/Metal/ImplMTL.mm b/Source/Metal/ImplMTL.mm index bca2a337..0c8b3111 100644 --- a/Source/Metal/ImplMTL.mm +++ b/Source/Metal/ImplMTL.mm @@ -243,6 +243,11 @@ static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPo return ((DeviceMTL&)device).GetDesc(); } +static Result NRI_CALL CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& texture) { + return ((DeviceMTL&)device).CreateImplementation(texture, textureDesc); +} + + //static Result NRI_CALL CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { // DeviceMTL& device = ((const BufferMTL*)bufferViewDesc.buffer)->GetDevice(); // return device.CreateImplementation(bufferView, bufferViewDesc); @@ -321,7 +326,6 @@ static void NRI_CALL FreeMemory(Memory& memory) { return ((const TextureMTL&)texture).GetDesc(); } - static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { ((const DeviceMTL&)device).GetMemoryDesc(bufferDesc, memoryLocation, memoryDesc); } @@ -334,13 +338,16 @@ static Result NRI_CALL CreateGraphicsPipeline(Device& device, const GraphicsPipe return ((DeviceMTL&)device).CreateImplementation(pipeline, graphicsPipelineDesc); } +static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) { + return ((DeviceMTL&)device).CreateImplementation(pipeline, computePipelineDesc); +} + + static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, const PipelineLayout& pipelineLayout, uint32_t setIndex, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum) { return Result::SUCCESS; //return ((DescriptorPoolMTL&)descriptorPool).AllocateDescriptorSets(pipelineLayout, setIndex, descriptorSets, instanceNum, variableDescriptorNum); } - - Result DeviceMTL::FillFunctionTable(CoreInterface& table) const { table.GetDeviceDesc = ::GetDeviceDesc; table.GetBufferDesc = ::GetBufferDesc; @@ -354,7 +361,7 @@ static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, co //table.CreateCommandBuffer = ::CreateCommandBuffer; //table.CreateDescriptorPool = ::CreateDescriptorPool; //table.CreateBuffer = ::CreateBuffer; - //table.CreateTexture = ::CreateTexture; + table.CreateTexture = ::CreateTexture; //table.CreateBufferView = ::CreateBufferView; //table.CreateTexture1DView = ::CreateTexture1DView; //table.CreateTexture2DView = ::CreateTexture2DView; @@ -362,7 +369,8 @@ static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, co //table.CreateSampler = ::CreateSampler; //table.CreatePipelineLayout = ::CreatePipelineLayout; table.CreateGraphicsPipeline = ::CreateGraphicsPipeline; -// table.CreateComputePipeline = ::CreateComputePipeline; + table.CreateComputePipeline = ::CreateComputePipeline; + // table.CreateQueryPool = ::CreateQueryPool; // table.CreateFence = ::CreateFence; table.DestroyCommandAllocator = ::DestroyCommandAllocator; @@ -413,11 +421,11 @@ static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, co table.CmdClearStorageBuffer = ::CmdClearStorageBuffer; table.CmdClearStorageTexture = ::CmdClearStorageTexture; table.CmdResolveTexture = ::CmdResolveTexture; - table.CmdResetQueries = ::CmdResetQueries; - table.CmdBeginQuery = ::CmdBeginQuery; - table.CmdEndQuery = ::CmdEndQuery; + //table.CmdResetQueries = ::CmdResetQueries; + //table.CmdBeginQuery = ::CmdBeginQuery; + //table.CmdEndQuery = ::CmdEndQuery; table.CmdCopyQueries = ::CmdCopyQueries; - table.CmdBeginAnnotation = ::CmdBeginAnnotation; + //table.CmdBeginAnnotation = ::CmdBeginAnnotation; table.CmdEndAnnotation = ::CmdEndAnnotation; table.EndCommandBuffer = ::EndCommandBuffer; // table.QueueSubmit = ::QueueSubmit; diff --git a/Source/Metal/PipelineLayoutMTL.h b/Source/Metal/PipelineLayoutMTL.h index 436549d3..a783db6b 100644 --- a/Source/Metal/PipelineLayoutMTL.h +++ b/Source/Metal/PipelineLayoutMTL.h @@ -6,14 +6,6 @@ namespace nri { struct DeviceMTL; - -// -//struct BindingInfo { -// BindingInfo(StdAllocator& allocator); -//// Vector pushConstantBindings; -//// Vector pushDescriptorBindings; -//}; - struct DescriptorSetLayout { DescriptorSetDesc m_DescriptorSetDesc; NSMutableArray* m_ArgumentDescriptors; @@ -26,7 +18,7 @@ struct PipelineLayoutMTL { , m_HasVariableDescriptorNum(device.GetStdAllocator()) , m_DescriptorSetRangeDescs(device.GetStdAllocator()) , m_DynamicConstantBufferDescs(device.GetStdAllocator()) - , m_DescriptorSets(device.GetStdAllocator()) + , m_DescriptorSetLayouts(device.GetStdAllocator()) { } @@ -36,14 +28,10 @@ struct PipelineLayoutMTL { return m_Device; } - inline struct DescriptorSetLayout* GetDescriptorSetLayout(uint32_t setIndex) { - return &m_DescriptorSets[setIndex]; + const inline struct DescriptorSetLayout* GetDescriptorSetLayout(uint32_t setIndex) const { + return &m_DescriptorSetLayouts[setIndex]; } - -// inline struct DescriptorSetDesc* GetDescriptorSetDesc(uint32_t setIndex) { -// return &m_DescriptorSetDesc[setIndex]; -// } - + Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); @@ -53,10 +41,8 @@ struct PipelineLayoutMTL { Vector m_HasVariableDescriptorNum; Vector m_DescriptorSetRangeDescs; Vector m_DynamicConstantBufferDescs; -// Vector m_DescriptorSetDesc; - Vector m_DescriptorSets; - -// BindingInfo m_BindingInfo; + Vector m_DescriptorSetLayouts; + }; } diff --git a/Source/Metal/PipelineLayoutMTL.mm b/Source/Metal/PipelineLayoutMTL.mm index aefa22ce..ebb62874 100644 --- a/Source/Metal/PipelineLayoutMTL.mm +++ b/Source/Metal/PipelineLayoutMTL.mm @@ -4,23 +4,12 @@ using namespace nri; -// -//BindingInfo::BindingInfo(StdAllocator& allocator) -// : hasVariableDescriptorNum(allocator) -// , descriptorSetRangeDescs(allocator) -// , dynamicConstantBufferDescs(allocator) -// , descriptorSetDescs(allocator) { -// -//} -// - PipelineLayoutMTL::~PipelineLayoutMTL() { } Result PipelineLayoutMTL::Create(const PipelineLayoutDesc& pipelineLayoutDesc) { - size_t rangeNum = 0; size_t dynamicConstantBufferNum = 0; for (uint32_t i = 0; i < pipelineLayoutDesc.descriptorSetNum; i++) { @@ -28,8 +17,9 @@ dynamicConstantBufferNum += pipelineLayoutDesc.descriptorSets[i].dynamicConstantBufferNum; } - m_DescriptorSets.resize(pipelineLayoutDesc.descriptorSetNum); - m_HasVariableDescriptorNum.resize(pipelineLayoutDesc.descriptorSetNum); + //m_DescriptorSetDesc.insert(m_DescriptorSetDesc.begin(), pipelineLayoutDesc.descriptorSets, pipelineLayoutDesc.descriptorSets + pipelineLayoutDesc.descriptorSetNum); + m_DescriptorSetLayouts.reserve(pipelineLayoutDesc.descriptorSetNum); + m_HasVariableDescriptorNum.reserve(pipelineLayoutDesc.descriptorSetNum); m_DescriptorSetRangeDescs.reserve(rangeNum); m_DynamicConstantBufferDescs.reserve(dynamicConstantBufferNum); @@ -38,9 +28,9 @@ // Binding info m_HasVariableDescriptorNum[i] = false; - m_DescriptorSets[i].m_DescriptorSetDesc = descriptorSetDesc; - m_DescriptorSets[i].m_DescriptorSetDesc.ranges = m_DescriptorSetRangeDescs.data() +m_DescriptorSetRangeDescs.size(); - m_DescriptorSets[i].m_DescriptorSetDesc.dynamicConstantBuffers = m_DynamicConstantBufferDescs.data() + m_DynamicConstantBufferDescs.size(); + m_DescriptorSetLayouts[i].m_DescriptorSetDesc = descriptorSetDesc; + m_DescriptorSetLayouts[i].m_DescriptorSetDesc.ranges = m_DescriptorSetRangeDescs.data() + m_DescriptorSetRangeDescs.size(); + m_DescriptorSetLayouts[i].m_DescriptorSetDesc.dynamicConstantBuffers = m_DynamicConstantBufferDescs.data() + m_DynamicConstantBufferDescs.size(); m_DescriptorSetRangeDescs.insert(m_DescriptorSetRangeDescs.end(), descriptorSetDesc.ranges, descriptorSetDesc.ranges + descriptorSetDesc.rangeNum); m_DynamicConstantBufferDescs.insert(m_DynamicConstantBufferDescs.end(), descriptorSetDesc.dynamicConstantBuffers, descriptorSetDesc.dynamicConstantBuffers + descriptorSetDesc.dynamicConstantBufferNum); @@ -73,8 +63,12 @@ default: break; } + if(range->flags & DescriptorRangeBits::VARIABLE_SIZED_ARRAY) + m_HasVariableDescriptorNum[r] = true; + [argumentDescriptors addObject: argDescriptor]; } - m_DescriptorSets[i].m_ArgumentDescriptors = argumentDescriptors; + + m_DescriptorSetLayouts[i].m_ArgumentDescriptors = argumentDescriptors; } return Result::SUCCESS; diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index 61d72f48..0bf08a71 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -26,9 +26,9 @@ NSError* error = nil; dispatch_data_t byteCode = dispatch_data_create( - computePipelineDesc.shader.bytecode, - computePipelineDesc.shader.size, nil, DISPATCH_DATA_DESTRUCTOR_DEFAULT); - + computePipelineDesc.shader.bytecode, + computePipelineDesc.shader.size, nil, DISPATCH_DATA_DESTRUCTOR_DEFAULT); + id lib = [m_Device newLibraryWithData: byteCode error:&error]; NSCAssert(lib, @"Failed to load Metal shader library %@", error); // not sure how to correctly report this RETURN_ON_FAILURE(&m_Device, lib, Result::FAILURE, "Failed to Load Metal shader library"); @@ -43,7 +43,6 @@ pipelineDesc.computeFunction = entryPointFunc; NSCAssert(pipelineDesc.computeFunction, @"Failed to create Metal kernel function %@: %@", entryPointNStr, error); - m_ComputePipeline = [m_Device newComputePipelineStateWithDescriptor: pipelineDesc options: MTLPipelineOptionNone reflection: nil diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index 834c2583..8efb5a74 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -33,6 +33,11 @@ struct TextureMTL { Result Create(const TextureDesc& textureDesc); void FinishMemoryBinding(MemoryMTL& memory, uint64_t memoryOffset); + inline Dim_t GetSize(Dim_t dimensionIndex, Mip_t mip = 0) const { + return GetDimension(GraphicsAPI::MTL, m_Desc, dimensionIndex, mip); + } + + //================================================================================================================ // NRI //================================================================================================================ diff --git a/Source/Metal/TextureMTL.mm b/Source/Metal/TextureMTL.mm index b6f7fe12..5529daeb 100644 --- a/Source/Metal/TextureMTL.mm +++ b/Source/Metal/TextureMTL.mm @@ -23,7 +23,6 @@ Result TextureMTL::Create(const TextureDesc& textureDesc) { m_Desc = textureDesc; - return Result::SUCCESS; } From a9a31212ff575063932abc105003d4ffbbc807be Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 25 Dec 2024 20:49:38 -0800 Subject: [PATCH 26/27] feat: progress command buffer logic Signed-off-by: Michael Pollind --- Source/Metal/CommandAllocatorMTL.h | 2 +- Source/Metal/CommandAllocatorMTL.mm | 4 +- Source/Metal/CommandBufferMTL.h | 29 +++- Source/Metal/CommandBufferMTL.mm | 249 +++++++++++++++++++--------- Source/Metal/CommandQueueMTL.h | 17 +- Source/Metal/CommandQueueMTL.mm | 7 +- Source/Metal/ConversionMTL.h | 2 + Source/Metal/DescriptorMTL.h | 3 +- Source/Metal/DeviceMTL.h | 3 + Source/Metal/DeviceMTL.mm | 5 + Source/Metal/ImplMTL.mm | 3 +- Source/Metal/PipelineLayoutMTL.h | 6 + Source/Metal/PipelineLayoutMTL.mm | 8 + Source/Metal/PipelineMTL.h | 26 ++- 14 files changed, 252 insertions(+), 112 deletions(-) diff --git a/Source/Metal/CommandAllocatorMTL.h b/Source/Metal/CommandAllocatorMTL.h index 127c7adf..b0071a67 100644 --- a/Source/Metal/CommandAllocatorMTL.h +++ b/Source/Metal/CommandAllocatorMTL.h @@ -31,7 +31,7 @@ struct CommandAllocatorMTL { private: DeviceMTL& m_Device; - struct CommandQueueMTL* m_CommandQueue; + const struct CommandQueueMTL* m_CommandQueue; }; } diff --git a/Source/Metal/CommandAllocatorMTL.mm b/Source/Metal/CommandAllocatorMTL.mm index 9fc54bde..913a5233 100644 --- a/Source/Metal/CommandAllocatorMTL.mm +++ b/Source/Metal/CommandAllocatorMTL.mm @@ -11,7 +11,8 @@ } Result CommandAllocatorMTL::Create(const CommandQueue& commandQueue) { - m_CommandQueue = &(CommandQueueMTL&)commandQueue; + const CommandQueueMTL& commandQueueImpl = (CommandQueueMTL&)commandQueue; + m_CommandQueue = &commandQueueImpl; return Result::SUCCESS; } @@ -22,6 +23,7 @@ Result CommandAllocatorMTL::CreateCommandBuffer(CommandBuffer*& commandBuffer) { CommandBufferMTL* commandBufferImpl = Allocate(m_Device.GetStdAllocator(), m_Device); + commandBufferImpl->Create(m_CommandQueue); commandBuffer = (CommandBuffer*)commandBufferImpl; return Result::SUCCESS; } diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index 405dccc5..18dd25b1 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -14,6 +14,13 @@ struct PipelineLayoutMTL; struct TextureMTL; struct DescriptorMTL; +NriBits(BarrierBits, uint8_t, + NONE = 0, + BARRIER_FLAG_BUFFERS = NriBit(0), + BARRIER_FLAG_TEXTURES = NriBit(1), + BARRIER_FLAG_RENDERTARGETS = NriBit(2), + BARRIER_FLAG_FENCE = NriBit(3)); + NriBits(CommandBufferDirtyBits, uint8_t, NONE = 0, @@ -29,12 +36,11 @@ struct CommandBufferMTL { inline DeviceMTL& GetDevice() const { return m_Device; } - - inline operator id() const { + + inline id GetHandle() const { return m_Handle; } - void SetDebugName(const char* name); Result Begin(const DescriptorPool* descriptorPool); Result End(); @@ -87,7 +93,7 @@ struct CommandBufferMTL { ~CommandBufferMTL(); - void Create(id cmd); + void Create(const struct CommandQueueMTL* queue); struct CmdIndexBuffer { size_t m_Offset; @@ -95,18 +101,31 @@ struct CommandBufferMTL { struct BufferMTL* m_Buffer; }; + struct CmdVertexBuffer { + size_t m_Offset; + struct BufferMTL* m_Buffer; + }; + private: + void Restore(); void updateCommandBufferState(); + void EndCurrentEncoders(); DeviceMTL& m_Device; struct PipelineMTL* m_CurrentPipeline = nullptr; - struct CmdIndexBuffer m_CurrentIndexCmd; PipelineLayoutMTL* m_CurrentPipelineLayout = nullptr; id m_Handle; id m_RendererEncoder = nil; id m_ComputeEncoder = nil; id m_BlitEncoder = nil; + const struct CommandQueueMTL* m_CommandQueue = nullptr; + + struct CmdIndexBuffer m_CurrentIndexCmd; + uint32_t m_dirtyVertexBufferBits = 0; + struct CmdVertexBuffer m_CurrentVertexCmd[32]; + id m_GraphicsPipelineState; + CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE; }; diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 47f17c82..13e4d9d8 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -25,13 +25,10 @@ } Result CommandBufferMTL::Begin(const DescriptorPool* descriptorPool) { - m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL]; } Result CommandBufferMTL::End() { - m_ComputeEncoder = nil; - m_RendererEncoder = nil; - + EndCurrentEncoders(); } void CommandBufferMTL::SetPipeline(const Pipeline& pipeline) { @@ -39,6 +36,25 @@ return; PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline; m_CurrentPipeline = &pipelineImpl; + + switch(m_CurrentPipeline->GetPipelineType()) { + case PipelineType::Compute: { + if(!m_ComputeEncoder) { + MTLComputePassDescriptor* computePassDescriptor = [MTLComputePassDescriptor computePassDescriptor]; + m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor:computePassDescriptor]; + } + [m_ComputeEncoder setComputePipelineState: m_CurrentPipeline->GetComputePipeline()]; + break; + } + case PipelineType::Graphics: { + [m_RendererEncoder setRenderPipelineState: m_CurrentPipeline->GetGraphicsPipeline()]; + m_GraphicsPipelineState = m_CurrentPipeline->GetGraphicsPipeline(); + break; + } + case PipelineType::Raytracing: { + break; + } + } } void CommandBufferMTL::SetPipelineLayout(const PipelineLayout& pipelineLayout) { @@ -51,54 +67,57 @@ auto* layout = m_CurrentPipelineLayout->GetDescriptorSetLayout(setIndexInPipelineLayout); const uint32_t space = layout->m_DescriptorSetDesc.registerSpace; - bool setFragmentBuffer = false; - bool setVertexBuffer = false; - + StageBits stageBits = (StageBits)0; for(size_t i = 0; i < layout->m_DescriptorSetDesc.rangeNum; i++) { - if(layout->m_DescriptorSetDesc.ranges[i].shaderStages == StageBits::ALL) { - setFragmentBuffer = true; - setVertexBuffer = true; - break; - } - if(layout->m_DescriptorSetDesc.ranges[i].shaderStages & StageBits::VERTEX_SHADER) { - setVertexBuffer = true; - break; - } - if(layout->m_DescriptorSetDesc.ranges[i].shaderStages & StageBits::GRAPHICS_SHADERS) { - setFragmentBuffer = true; - break; - } + if(layout->m_DescriptorSetDesc.ranges[i].shaderStages == StageBits::NONE) + continue; + stageBits |= layout->m_DescriptorSetDesc.ranges[i].shaderStages ; } - if(setFragmentBuffer) { + if(stageBits == StageBits::ALL || + stageBits & StageBits::FRAGMENT_SHADER) { [m_RendererEncoder setFragmentBuffer: descriptorSetImpl.GetArgumentBuffer() offset: descriptorSetImpl.GetArugmentBufferOffset() atIndex: space]; } - if(setVertexBuffer) { + if(stageBits == StageBits::ALL || + stageBits & StageBits::VERTEX_SHADER) { [m_RendererEncoder setVertexBuffer: descriptorSetImpl.GetArgumentBuffer() offset: descriptorSetImpl.GetArugmentBufferOffset() atIndex: space]; } - + if(stageBits == StageBits::ALL || + stageBits & StageBits::COMPUTE_SHADER) { + [m_ComputeEncoder setBuffer: descriptorSetImpl.GetArgumentBuffer() + offset: descriptorSetImpl.GetArugmentBufferOffset() + atIndex: space]; + + } + } -void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) { - //if (pDesc->mUsedStages & SHADER_STAGE_VERT) - //{ - // [m_RendererEncoder setVertexBytes:data length:size atIndex:pushConstantIndex]; - //} - - //if (pDesc->mUsedStages & SHADER_STAGE_FRAG) - //{ - // [m_RendererEncoder setFragmentBytes:data length:size atIndex:pushConstantIndex]; - //} - - //if (pDesc->mUsedStages & SHADER_STAGE_COMP) - //{ - // [m_RendererEncoder setBytes:data length:size atIndex:pushConstantIndex]; - //} +void CommandBufferMTL::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) { + const struct RootConstantDesc* pDesc = m_CurrentPipelineLayout->GetPushBinding(pushConstantIndex); + + + if (pDesc->shaderStages == StageBits::ALL || + pDesc->shaderStages & StageBits::VERTEX_SHADER) + { + [m_RendererEncoder setVertexBytes:data length:size atIndex:pDesc->registerIndex]; + } + + if (pDesc->shaderStages == StageBits::ALL || + pDesc->shaderStages & StageBits::FRAGMENT_SHADER ) + { + [m_RendererEncoder setFragmentBytes:data length:size atIndex:pDesc->registerIndex]; + } + + if (pDesc->shaderStages == StageBits::ALL || + pDesc->shaderStages & StageBits::COMPUTE_SHADER ) + { + [m_ComputeEncoder setBytes:data length:size atIndex:pDesc->registerIndex]; + } } void CommandBufferMTL::SetDescriptorPool(const DescriptorPool& descriptorPool) { @@ -107,59 +126,54 @@ void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) { - //if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_BUFFERS) - { - [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers - afterStages:MTLRenderStageFragment - beforeStages:MTLRenderStageVertex]; - } - - //if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_TEXTURES) - { - [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeTextures - afterStages:MTLRenderStageFragment - beforeStages:MTLRenderStageVertex]; + const size_t totalResources = barrierGroupDesc.bufferNum + barrierGroupDesc.textureNum; + + Scratch> resourceBarrier = AllocateScratch(m_Device, id, totalResources); + size_t barrierCount = 0; + for(size_t i = 0; i < barrierGroupDesc.bufferNum; i++) { + const BufferBarrierDesc& in = barrierGroupDesc.buffers[i]; + const BufferMTL& bufferImpl = *(const BufferMTL*)in.buffer; + resourceBarrier[barrierCount++] = bufferImpl.GetHandle(); } - - //if (pCmd->pQueue->mBarrierFlags & BARRIER_FLAG_RENDERTARGETS) - { - [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeRenderTargets - afterStages:MTLRenderStageFragment - beforeStages:MTLRenderStageVertex]; + + for(size_t i = 0; i < barrierGroupDesc.textureNum; i++) { + const TextureBarrierDesc& in = barrierGroupDesc.textures[i]; + const TextureMTL& textureImpl = *(const TextureMTL*)in.texture; + resourceBarrier[barrierCount++] = textureImpl.GetHandle(); } - + [m_RendererEncoder memoryBarrierWithResources: resourceBarrier + count: barrierCount + afterStages: MTLRenderStageFragment + beforeStages: MTLRenderStageVertex]; } void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { - MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; + MTLRenderPassDescriptor* renderPassDesc = [MTLRenderPassDescriptor renderPassDescriptor]; for(uint32_t i = 0; i < attachmentsDesc.colorNum; i++) { DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.colors[i]; - renderPassDescriptor.colorAttachments[i].texture = descriptorMTL.GetTextureHandle(); - renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); - renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionClear; - renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; + renderPassDesc.colorAttachments[i].texture = descriptorMTL.GetTextureHandle(); + renderPassDesc.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); + renderPassDesc.colorAttachments[i].loadAction = MTLLoadActionLoad; + renderPassDesc.colorAttachments[i].storeAction = MTLStoreActionStore; } if(attachmentsDesc.depthStencil) { - DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.depthStencil; - renderPassDescriptor.depthAttachment.texture = descriptorMTL.GetTextureHandle(); - // renderPassDescriptor.depthAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); - renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; - renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; + renderPassDesc.depthAttachment.texture = descriptorMTL.GetTextureHandle(); + renderPassDesc.depthAttachment.loadAction = MTLLoadActionLoad; + renderPassDesc.depthAttachment.storeAction = MTLStoreActionStore; } - - m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDescriptor]; + //m_RenderPassDescriptor = [renderPassDescriptor copy]; + m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDesc]; } + void CommandBufferMTL::EndRendering() { - NSCAssert(m_RendererEncoder, @"Renderer Encoderer Not Set"); - [m_RendererEncoder endEncoding]; - m_RendererEncoder = nil; - m_ComputeEncoder = nil; + EndCurrentEncoders(); } + void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) { Scratch mtlViewports = AllocateScratch(m_Device, MTLViewport, viewportNum); for(size_t i = 0; i < viewportNum; i++) { @@ -172,6 +186,8 @@ } [m_RendererEncoder setViewports: mtlViewports count: viewportNum]; } + + void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) { NSCAssert(m_RendererEncoder, @"encoder set"); MTLScissorRect rect; @@ -197,12 +213,32 @@ ]; } void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) { + //[m_RendererEncoder sha] } void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { + + + Restore(); +} + +void CommandBufferMTL::EndCurrentEncoders() { + if(m_RendererEncoder) { + [m_RendererEncoder endEncoding]; + m_RendererEncoder = nil; + } + + if(m_ComputeEncoder) { + [m_ComputeEncoder endEncoding]; + m_ComputeEncoder = nil; + } + if(m_BlitEncoder) { + [m_BlitEncoder endEncoding]; + m_BlitEncoder = nil; + } } void CommandBufferMTL::SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType) { @@ -219,9 +255,26 @@ m_CurrentIndexCmd.m_Offset = offset; } + +void CommandBufferMTL::Restore() { + [m_RendererEncoder setRenderPipelineState: m_GraphicsPipelineState]; + uint32_t vertexSlot = 0; + for( uint32_t attr = m_dirtyVertexBufferBits; attr > 0; attr = ( attr >> 1 ), vertexSlot++ ) { + [m_RendererEncoder setVertexBuffer: m_CurrentVertexCmd[vertexSlot].m_Buffer->GetHandle() + offset: m_CurrentVertexCmd[vertexSlot].m_Offset + atIndex: vertexSlot]; + } +} + void CommandBufferMTL::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { for(size_t i = 0; i < bufferNum; i++) { BufferMTL* mtlBuffer = (BufferMTL*)buffers[i]; + + const size_t slotIndex = i + baseSlot; + m_CurrentVertexCmd[slotIndex].m_Offset = offsets[i]; + m_CurrentVertexCmd[slotIndex].m_Buffer = mtlBuffer; + m_dirtyVertexBufferBits |= (1 << slotIndex); + [m_RendererEncoder setVertexBuffer: mtlBuffer->GetHandle() offset: offsets[i] atIndex: i + baseSlot]; @@ -230,16 +283,20 @@ } void CommandBufferMTL::Draw(const DrawDesc& drawDesc) { - [m_RendererEncoder drawPrimitives: m_CurrentPipeline->m_primitiveType + //m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: m_renderPassDescriptor]; + + [m_RendererEncoder drawPrimitives: m_CurrentPipeline->GetPrimitiveType() vertexStart:drawDesc.baseVertex vertexCount:drawDesc.vertexNum instanceCount:drawDesc.instanceNum baseInstance: 0]; + + [m_RendererEncoder endEncoding]; } void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) { id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); - [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType + [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->GetPrimitiveType() indexCount: drawIndexedDesc.indexNum indexType: m_CurrentIndexCmd.m_Type indexBuffer: indexBuffer @@ -251,7 +308,7 @@ NSCAssert(!countBuffer, @"count buffer not supported"); [m_RendererEncoder - drawPrimitives: m_CurrentPipeline->m_primitiveType + drawPrimitives: m_CurrentPipeline->GetPrimitiveType() indirectBuffer:((BufferMTL&)buffer).GetHandle() indirectBufferOffset: offset]; } @@ -272,10 +329,16 @@ } void CommandBufferMTL::DispatchIndirect(const Buffer& buffer, uint64_t offset) {} -void CommandBufferMTL::BeginQuery(const QueryPool& queryPool, uint32_t offset) {} +void CommandBufferMTL::BeginQuery(const QueryPool& queryPool, uint32_t offset) { + +} void CommandBufferMTL::EndQuery(const QueryPool& queryPool, uint32_t offset) {} -void CommandBufferMTL::BeginAnnotation(const char* name) {} -void CommandBufferMTL::EndAnnotation() {} +void CommandBufferMTL::BeginAnnotation(const char* name) { + +} +void CommandBufferMTL::EndAnnotation() { + +} void CommandBufferMTL::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) { } @@ -286,6 +349,12 @@ void CommandBufferMTL::CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) { const BufferMTL& src = (const BufferMTL&)srcBuffer; const BufferMTL& dst = (const BufferMTL&)dstBuffer; + + if(!m_BlitEncoder) { + EndCurrentEncoders(); + m_BlitEncoder = [m_Handle blitCommandEncoder]; + } + [m_BlitEncoder copyFromBuffer:src.GetHandle() sourceOffset:srcOffset @@ -297,6 +366,11 @@ void CommandBufferMTL::CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc) { const TextureMTL& src = (const TextureMTL&)srcTexture; const TextureMTL& dst = (const TextureMTL&)dstTexture; + if(!m_BlitEncoder) { + EndCurrentEncoders(); + m_BlitEncoder = [m_Handle blitCommandEncoder]; + } + bool isWholeResource = !dstRegionDesc && !srcRegionDesc; if (isWholeResource) { [m_BlitEncoder @@ -331,6 +405,11 @@ const BufferMTL& src = (const BufferMTL&)srcBuffer; const TextureMTL& dst = (const TextureMTL&)dstTexture; + if(!m_BlitEncoder) { + EndCurrentEncoders(); + m_BlitEncoder = [m_Handle blitCommandEncoder]; + } + const MTLSize sourceSize = MTLSizeMake( (dstRegionDesc.width == WHOLE_SIZE) ? dst.GetSize(0, dstRegionDesc.mipOffset) : dstRegionDesc.width, (dstRegionDesc.height == WHOLE_SIZE) ? dst.GetSize(1, dstRegionDesc.mipOffset) : dstRegionDesc.height, @@ -352,6 +431,10 @@ const TextureMTL& src = (const TextureMTL&)srcTexture; const BufferMTL& dst = (const BufferMTL&)dstBuffer; + if(!m_BlitEncoder) { + EndCurrentEncoders(); + m_BlitEncoder = [m_Handle blitCommandEncoder]; + } const MTLSize sourceSize = MTLSizeMake( (srcRegionDesc.width == WHOLE_SIZE) ? src.GetSize(0, srcRegionDesc.mipOffset) : srcRegionDesc.width, @@ -384,8 +467,14 @@ } -void Create(id cmd); +void CommandBufferMTL::Create(const struct CommandQueueMTL* queue) { + m_CommandQueue = queue; + MTLCommandBufferDescriptor* pDesc = [[MTLCommandBufferDescriptor alloc] init]; + pDesc.errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus; + m_Handle = [m_CommandQueue->GetHandle() commandBufferWithDescriptor: pDesc]; + +} #include "CommandBufferMTL.hpp" diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h index 0486e732..8e0206d3 100644 --- a/Source/Metal/CommandQueueMTL.h +++ b/Source/Metal/CommandQueueMTL.h @@ -7,12 +7,6 @@ namespace nri { struct DeviceMTL; -NriBits(QueueBarrierBits, uint8_t, - NONE = 0, - BARRIER_FLAG_BUFFERS = NriBit(0), - BARRIER_FLAG_TEXTURES = NriBit(1), - BARRIER_FLAG_RENDERTARGETS = NriBit(2), - BARRIER_FLAG_FENCE = NriBit(3)); struct CommandQueueMTL { @@ -21,10 +15,6 @@ struct CommandQueueMTL { } ~CommandQueueMTL(); - - inline operator id() const { - return m_Handle; - } inline DeviceMTL& GetDevice() const { return m_Device; @@ -38,14 +28,17 @@ struct CommandQueueMTL { return m_Lock; } + inline id GetHandle() const { + return m_Handle; + } + void SetDebugName(const char* name); void Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain); Result WaitForIdle(); Result Create(CommandQueueType type); - QueueBarrierBits m_BarrierBits = QueueBarrierBits::NONE; - + private: DeviceMTL& m_Device; CommandQueueType m_Type = CommandQueueType(-1); diff --git a/Source/Metal/CommandQueueMTL.mm b/Source/Metal/CommandQueueMTL.mm index 6ad24150..47065bc8 100644 --- a/Source/Metal/CommandQueueMTL.mm +++ b/Source/Metal/CommandQueueMTL.mm @@ -29,12 +29,9 @@ void CommandQueueMTL::Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain) { for(uint32_t i = 0; i < queueSubmitDesc.commandBufferNum; i++) { - id cmd = *(struct CommandBufferMTL*)queueSubmitDesc.commandBuffers[i]; - [cmd commit]; - + const struct CommandBufferMTL& cmd = (const CommandBufferMTL&)queueSubmitDesc.commandBuffers[i]; + [cmd.GetHandle() commit]; } - - } diff --git a/Source/Metal/ConversionMTL.h b/Source/Metal/ConversionMTL.h index f8d1e8b2..e321bcde 100644 --- a/Source/Metal/ConversionMTL.h +++ b/Source/Metal/ConversionMTL.h @@ -168,6 +168,8 @@ constexpr MTLDataType GetDescriptorType(DescriptorType type) { return DESCRIPTOR_TYPES[(size_t)type]; } + + constexpr std::array COMPARE_OP = { MTLCompareFunctionNever, // NONE MTLCompareFunctionAlways, // ALWAYS diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index b5a7447c..c7594f33 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -19,7 +19,6 @@ enum class DescriptorTypeMTL { BUFFER_VIEW }; - struct DescriptorMTL { public: inline DescriptorMTL (DeviceMTL& device) @@ -52,6 +51,8 @@ struct DescriptorMTL { private: + void EndCurrentEncoders(bool forceBarrier); + DeviceMTL& m_Device; DescriptorTypeMTL m_Type = DescriptorTypeMTL::NONE; id m_Texture; diff --git a/Source/Metal/DeviceMTL.h b/Source/Metal/DeviceMTL.h index eac2c876..2a5b307b 100644 --- a/Source/Metal/DeviceMTL.h +++ b/Source/Metal/DeviceMTL.h @@ -48,6 +48,9 @@ struct DeviceMTL final : public DeviceBase { } + id GetClearPipeline(); + + void Destruct() override; Result FillFunctionTable(CoreInterface& table) const override; Result FillFunctionTable(HelperInterface& table) const override; diff --git a/Source/Metal/DeviceMTL.mm b/Source/Metal/DeviceMTL.mm index 244faeaa..fc359a74 100644 --- a/Source/Metal/DeviceMTL.mm +++ b/Source/Metal/DeviceMTL.mm @@ -82,6 +82,11 @@ static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef property //} +id DeviceMTL::GetClearPipeline() { + +} + + void DeviceMTL::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { MemoryTypeInfo memoryTypeInfo; memoryTypeInfo.options = DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION[(size_t)memoryLocation]; diff --git a/Source/Metal/ImplMTL.mm b/Source/Metal/ImplMTL.mm index 0c8b3111..384cb0cb 100644 --- a/Source/Metal/ImplMTL.mm +++ b/Source/Metal/ImplMTL.mm @@ -68,6 +68,7 @@ static Result NRI_CALL CreateCommandBuffer(CommandAllocator& commandAllocator, C static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) { // ((CommandAllocatorVK&)commandAllocator).Reset(); + } static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { @@ -358,7 +359,7 @@ static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, co table.GetTextureMemoryDesc = ::GetTextureMemoryDesc; //table.GetCommandQueue = ::GetCommandQueue; //table.CreateCommandAllocator = ::CreateCommandAllocator; - //table.CreateCommandBuffer = ::CreateCommandBuffer; + table.CreateCommandBuffer = ::CreateCommandBuffer; //table.CreateDescriptorPool = ::CreateDescriptorPool; //table.CreateBuffer = ::CreateBuffer; table.CreateTexture = ::CreateTexture; diff --git a/Source/Metal/PipelineLayoutMTL.h b/Source/Metal/PipelineLayoutMTL.h index a783db6b..e6416b5f 100644 --- a/Source/Metal/PipelineLayoutMTL.h +++ b/Source/Metal/PipelineLayoutMTL.h @@ -19,6 +19,7 @@ struct PipelineLayoutMTL { , m_DescriptorSetRangeDescs(device.GetStdAllocator()) , m_DynamicConstantBufferDescs(device.GetStdAllocator()) , m_DescriptorSetLayouts(device.GetStdAllocator()) + , m_PushBindingConstants(device.GetStdAllocator()) { } @@ -32,6 +33,10 @@ struct PipelineLayoutMTL { return &m_DescriptorSetLayouts[setIndex]; } + const inline struct RootConstantDesc* GetPushBinding(uint32_t index) { + return &m_PushBindingConstants[index]; + } + Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); @@ -42,6 +47,7 @@ struct PipelineLayoutMTL { Vector m_DescriptorSetRangeDescs; Vector m_DynamicConstantBufferDescs; Vector m_DescriptorSetLayouts; + Vector m_PushBindingConstants; }; diff --git a/Source/Metal/PipelineLayoutMTL.mm b/Source/Metal/PipelineLayoutMTL.mm index ebb62874..fa88fad9 100644 --- a/Source/Metal/PipelineLayoutMTL.mm +++ b/Source/Metal/PipelineLayoutMTL.mm @@ -22,6 +22,7 @@ m_HasVariableDescriptorNum.reserve(pipelineLayoutDesc.descriptorSetNum); m_DescriptorSetRangeDescs.reserve(rangeNum); m_DynamicConstantBufferDescs.reserve(dynamicConstantBufferNum); + m_PushBindingConstants.resize(pipelineLayoutDesc.rootConstantNum); for (uint32_t i = 0; i < pipelineLayoutDesc.descriptorSetNum; i++) { const DescriptorSetDesc& descriptorSetDesc = pipelineLayoutDesc.descriptorSets[i]; @@ -71,6 +72,13 @@ m_DescriptorSetLayouts[i].m_ArgumentDescriptors = argumentDescriptors; } + + + for (uint32_t i = 0; i < pipelineLayoutDesc.rootConstantNum; i++) { + const RootConstantDesc& pushConstantDesc = pipelineLayoutDesc.rootConstants[i]; + m_PushBindingConstants[i] = pushConstantDesc; + } + return Result::SUCCESS; } diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index 43518867..e6480fd3 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -20,20 +20,34 @@ struct PipelineMTL { } ~PipelineMTL(); - inline PipelineType GetPipelineType() { - return m_PipelineType; - } - Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); Result Create(const ComputePipelineDesc& computePipelineDesc); Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); + inline MTLPrimitiveType GetPrimitiveType() const { + return m_primitiveType; + } + + inline PipelineType GetPipelineType() const { + return m_PipelineType; + } + + inline id GetComputePipeline() const { + return m_ComputePipeline; + } + + + inline id GetGraphicsPipeline() const { + return m_GraphicsPipeline; + } + + +private: + PipelineType m_PipelineType; MTLPrimitiveTopologyClass m_topologyClass; MTLPrimitiveType m_primitiveType; StageBits m_usedBits; -private: - PipelineType m_PipelineType; union{ id m_ComputePipeline = nil; id m_GraphicsPipeline; From 15d093d2623c3dd7adce69c1c43b89e567c5ef69 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Thu, 26 Dec 2024 22:38:45 -0800 Subject: [PATCH 27/27] feat: implement command buffer impl Signed-off-by: Michael Pollind --- Include/NRIMacro.h | 1 + Source/Metal/CommandBufferMTL.h | 47 ++--- Source/Metal/CommandBufferMTL.mm | 339 ++++++++++++++++++++++--------- Source/Metal/CommandQueueMTL.h | 1 - Source/Metal/CommandQueueMTL.mm | 2 +- Source/Metal/PipelineMTL.h | 9 +- Source/Metal/SharedMTL.h | 24 +++ 7 files changed, 288 insertions(+), 135 deletions(-) diff --git a/Include/NRIMacro.h b/Include/NRIMacro.h index 42c343aa..7ae83833 100644 --- a/Include/NRIMacro.h +++ b/Include/NRIMacro.h @@ -202,6 +202,7 @@ constexpr name operator | (name val0, name val1) { return (name)((type)val0 | (type)val1); } \ constexpr name& operator &= (name& val0, name val1) { val0 = (name)(val0 & val1); return val0; } \ constexpr name& operator |= (name& val0, name val1) { val0 = (name)(val0 | val1); return val0; } \ + constexpr name operator ~(name val0) { return (name)(~(type)val0); } \ enum class name : type { \ _NRI_ENUM_EXPAND(__VA_ARGS__), \ } diff --git a/Source/Metal/CommandBufferMTL.h b/Source/Metal/CommandBufferMTL.h index 18dd25b1..8e70c777 100644 --- a/Source/Metal/CommandBufferMTL.h +++ b/Source/Metal/CommandBufferMTL.h @@ -2,9 +2,10 @@ #pragma once -#include #import +#include "SharedMTL.h" + namespace nri { struct DeviceMTL; @@ -14,29 +15,13 @@ struct PipelineLayoutMTL; struct TextureMTL; struct DescriptorMTL; -NriBits(BarrierBits, uint8_t, - NONE = 0, - BARRIER_FLAG_BUFFERS = NriBit(0), - BARRIER_FLAG_TEXTURES = NriBit(1), - BARRIER_FLAG_RENDERTARGETS = NriBit(2), - BARRIER_FLAG_FENCE = NriBit(3)); - - -NriBits(CommandBufferDirtyBits, uint8_t, - NONE = 0, - CMD_DIRTY_STENCIL = NriBit(0) - ); - struct CommandBufferMTL { - inline CommandBufferMTL(DeviceMTL& device) - : m_Device(device) { - } - + CommandBufferMTL(DeviceMTL& device); inline DeviceMTL& GetDevice() const { return m_Device; } - + inline id GetHandle() const { return m_Handle; } @@ -100,7 +85,6 @@ struct CommandBufferMTL { MTLIndexType m_Type; struct BufferMTL* m_Buffer; }; - struct CmdVertexBuffer { size_t m_Offset; struct BufferMTL* m_Buffer; @@ -108,9 +92,9 @@ struct CommandBufferMTL { private: - void Restore(); void updateCommandBufferState(); void EndCurrentEncoders(); + void InsertBarriers(); DeviceMTL& m_Device; struct PipelineMTL* m_CurrentPipeline = nullptr; @@ -120,14 +104,25 @@ struct CommandBufferMTL { id m_ComputeEncoder = nil; id m_BlitEncoder = nil; const struct CommandQueueMTL* m_CommandQueue = nullptr; + + BarrierBits m_barrierFlags = BarrierBits::NONE; - struct CmdIndexBuffer m_CurrentIndexCmd; + MTLRenderPassDescriptor* m_renderPassDescriptor = nil; + CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE; + struct CmdIndexBuffer m_indexBuffer; uint32_t m_dirtyVertexBufferBits = 0; - struct CmdVertexBuffer m_CurrentVertexCmd[32]; - id m_GraphicsPipelineState; + struct CmdVertexBuffer m_vertexBuffers[32]; + struct ShadingRateDesc m_shadingRateDesc; + uint16_t m_numViewports = 0; + uint16_t m_numScissors = 0; + MTLViewport m_viewports[16]; + MTLScissorRect m_Scissors[16]; + Color32f m_BlendColor; + uint8_t m_StencilFront; + uint8_t m_StencilBack; - - CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE; + NSMutableArray* m_Annotations; + }; }; diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 13e4d9d8..c32219ef 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -12,14 +12,17 @@ #include "PipelineLayoutMTL.h" #include "TextureMTL.h" -#include - using namespace nri; CommandBufferMTL::~CommandBufferMTL() { } + +CommandBufferMTL::CommandBufferMTL(DeviceMTL& device): m_Device(device) { + m_Annotations = [NSMutableArray alloc]; +} + void CommandBufferMTL::SetDebugName(const char* name) { [m_Handle setLabel:[NSString stringWithUTF8String:name]]; } @@ -28,27 +31,71 @@ } Result CommandBufferMTL::End() { + InsertBarriers(); EndCurrentEncoders(); } void CommandBufferMTL::SetPipeline(const Pipeline& pipeline) { if (m_CurrentPipeline == (PipelineMTL*)&pipeline) return; + PipelineMTL& pipelineImpl = (PipelineMTL&)pipeline; m_CurrentPipeline = &pipelineImpl; - switch(m_CurrentPipeline->GetPipelineType()) { case PipelineType::Compute: { if(!m_ComputeEncoder) { MTLComputePassDescriptor* computePassDescriptor = [MTLComputePassDescriptor computePassDescriptor]; m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor:computePassDescriptor]; + + while(m_Annotations.count > 0) { + NSObject* obj = m_Annotations[m_Annotations.count - 1]; + if([obj isKindOfClass: [NSNull class]]) { + [m_ComputeEncoder popDebugGroup]; + } else { + [m_ComputeEncoder insertDebugSignpost: (NSString*)obj]; + } + [m_Annotations removeLastObject]; + } + } [m_ComputeEncoder setComputePipelineState: m_CurrentPipeline->GetComputePipeline()]; break; } case PipelineType::Graphics: { + if(!m_RendererEncoder) { + m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: m_renderPassDescriptor]; + + uint32_t vertexSlot = 0; + for( uint32_t attr = m_dirtyVertexBufferBits; attr > 0; attr = ( attr >> 1 ), vertexSlot++ ) + [m_RendererEncoder setVertexBuffer: m_vertexBuffers[vertexSlot].m_Buffer->GetHandle() + offset: m_vertexBuffers[vertexSlot].m_Offset + atIndex: vertexSlot]; + + while(m_Annotations.count > 0) { + NSObject* obj = m_Annotations[m_Annotations.count - 1]; + if([obj isKindOfClass: [NSNull class]]) { + [m_RendererEncoder popDebugGroup]; + } else { + [m_RendererEncoder insertDebugSignpost: (NSString*)obj]; + } + [m_Annotations removeLastObject]; + } + if(m_numViewports > 0) + [m_RendererEncoder setViewports: m_viewports count: m_numViewports]; + if(m_numScissors > 0) + [m_RendererEncoder setScissorRects: m_Scissors count: m_numScissors]; + if(m_DirtyBits & CommandBufferDirtyBits::CMD_DIRTY_BLEND_CONSTANT) + [m_RendererEncoder setBlendColorRed: m_BlendColor.x + green: m_BlendColor.y + blue: m_BlendColor.z + alpha: m_BlendColor.w]; + if(m_DirtyBits & CommandBufferDirtyBits::CMD_DIRTY_STENCIL) + [m_RendererEncoder + setStencilFrontReferenceValue: m_StencilFront + backReferenceValue: m_StencilBack]; + } + [m_RendererEncoder setRenderPipelineState: m_CurrentPipeline->GetGraphicsPipeline()]; - m_GraphicsPipelineState = m_CurrentPipeline->GetGraphicsPipeline(); break; } case PipelineType::Raytracing: { @@ -124,103 +171,158 @@ } -void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) { - - const size_t totalResources = barrierGroupDesc.bufferNum + barrierGroupDesc.textureNum; + +void CommandBufferMTL::InsertBarriers() { + if(m_RendererEncoder) { + if (m_barrierFlags & BarrierBits::BARRIER_FLAG_BUFFERS) + { + [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers + afterStages:MTLRenderStageFragment + beforeStages:MTLRenderStageVertex]; + } + + if (m_barrierFlags & BarrierBits::BARRIER_FLAG_TEXTURES) + { + [m_RendererEncoder memoryBarrierWithScope:MTLBarrierScopeTextures + afterStages:MTLRenderStageFragment + beforeStages:MTLRenderStageVertex]; + } + } - Scratch> resourceBarrier = AllocateScratch(m_Device, id, totalResources); - size_t barrierCount = 0; - for(size_t i = 0; i < barrierGroupDesc.bufferNum; i++) { - const BufferBarrierDesc& in = barrierGroupDesc.buffers[i]; - const BufferMTL& bufferImpl = *(const BufferMTL*)in.buffer; - resourceBarrier[barrierCount++] = bufferImpl.GetHandle(); + if(m_ComputeEncoder) { + if (m_barrierFlags & BarrierBits::BARRIER_FLAG_BUFFERS) + { + [m_ComputeEncoder memoryBarrierWithScope:MTLBarrierScopeBuffers]; + } + + if (m_barrierFlags & BarrierBits::BARRIER_FLAG_TEXTURES) + { + [m_ComputeEncoder memoryBarrierWithScope:MTLBarrierScopeTextures]; + } + } - for(size_t i = 0; i < barrierGroupDesc.textureNum; i++) { - const TextureBarrierDesc& in = barrierGroupDesc.textures[i]; - const TextureMTL& textureImpl = *(const TextureMTL*)in.texture; - resourceBarrier[barrierCount++] = textureImpl.GetHandle(); + m_barrierFlags = BarrierBits::NONE; +} + +void CommandBufferMTL::Barrier(const BarrierGroupDesc& barrierGroupDesc) { + + if(barrierGroupDesc.bufferNum) { + m_barrierFlags |= BarrierBits::BARRIER_FLAG_BUFFERS; } - [m_RendererEncoder memoryBarrierWithResources: resourceBarrier - count: barrierCount - afterStages: MTLRenderStageFragment - beforeStages: MTLRenderStageVertex]; + if(barrierGroupDesc.textureNum) { + m_barrierFlags |= BarrierBits::BARRIER_FLAG_TEXTURES; + } +// +// const size_t totalResources = barrierGroupDesc.bufferNum + barrierGroupDesc.textureNum; +// Scratch> resourceBarrier = AllocateScratch(m_Device, id, totalResources); +// size_t barrierCount = 0; +// for(size_t i = 0; i < barrierGroupDesc.bufferNum; i++) { +// const BufferBarrierDesc& in = barrierGroupDesc.buffers[i]; +// const BufferMTL& bufferImpl = *(const BufferMTL*)in.buffer; +// resourceBarrier[barrierCount++] = bufferImpl.GetHandle(); +// } +// +// for(size_t i = 0; i < barrierGroupDesc.textureNum; i++) { +// const TextureBarrierDesc& in = barrierGroupDesc.textures[i]; +// const TextureMTL& textureImpl = *(const TextureMTL*)in.texture; +// resourceBarrier[barrierCount++] = textureImpl.GetHandle(); +// } + +// [m_RendererEncoder memoryBarrierWithResources: resourceBarrier +// count: barrierCount +// afterStages: MTLRenderStageFragment +// beforeStages: MTLRenderStageVertex]; } void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { - MTLRenderPassDescriptor* renderPassDesc = [MTLRenderPassDescriptor renderPassDescriptor]; + m_renderPassDescriptor = [MTLRenderPassDescriptor alloc]; for(uint32_t i = 0; i < attachmentsDesc.colorNum; i++) { DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.colors[i]; - renderPassDesc.colorAttachments[i].texture = descriptorMTL.GetTextureHandle(); - renderPassDesc.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); - renderPassDesc.colorAttachments[i].loadAction = MTLLoadActionLoad; - renderPassDesc.colorAttachments[i].storeAction = MTLStoreActionStore; + m_renderPassDescriptor.colorAttachments[i].texture = descriptorMTL.GetTextureHandle(); + m_renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); + m_renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionLoad; + m_renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; } if(attachmentsDesc.depthStencil) { DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.depthStencil; - renderPassDesc.depthAttachment.texture = descriptorMTL.GetTextureHandle(); - renderPassDesc.depthAttachment.loadAction = MTLLoadActionLoad; - renderPassDesc.depthAttachment.storeAction = MTLStoreActionStore; - + m_renderPassDescriptor.depthAttachment.texture = descriptorMTL.GetTextureHandle(); + m_renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionLoad; + m_renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; } - //m_RenderPassDescriptor = [renderPassDescriptor copy]; - m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDesc]; } void CommandBufferMTL::EndRendering() { EndCurrentEncoders(); + m_numScissors = 0; + m_numViewports = 0; + m_DirtyBits = CommandBufferDirtyBits::NONE; } void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) { - Scratch mtlViewports = AllocateScratch(m_Device, MTLViewport, viewportNum); for(size_t i = 0; i < viewportNum; i++) { - mtlViewports[i].originX = viewports[i].x; - mtlViewports[i].originY = viewports[i].y; - mtlViewports[i].width = viewports[i].width; - mtlViewports[i].height = viewports[i].height; - mtlViewports[i].znear = viewports[i].depthMin; - mtlViewports[i].zfar = viewports[i].depthMax; + m_viewports[i].originX = viewports[i].x; + m_viewports[i].originY = viewports[i].y; + m_viewports[i].width = viewports[i].width; + m_viewports[i].height = viewports[i].height; + m_viewports[i].znear = viewports[i].depthMin; + m_viewports[i].zfar = viewports[i].depthMax; } - [m_RendererEncoder setViewports: mtlViewports count: viewportNum]; + m_numViewports = viewportNum; + if(m_RendererEncoder && m_numViewports > 0) + [m_RendererEncoder + setViewports: m_viewports + count: m_numViewports]; } void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) { - NSCAssert(m_RendererEncoder, @"encoder set"); - MTLScissorRect rect; - rect.x = rects[rectNum].x; - rect.y = rects[rectNum].y; - rect.width = rects[rectNum].width; - rect.height = rects[rectNum].height; - [m_RendererEncoder setScissorRect: rect]; + for(size_t i = 0; i < rectNum; i++) { + m_Scissors[i].x = rects[i].x; + m_Scissors[i].y = rects[i].y; + m_Scissors[i].width = rects[i].width; + m_Scissors[i].height = rects[i].height; + } + m_numScissors = rectNum; + if(m_RendererEncoder && m_numScissors > 0) + [m_RendererEncoder + setScissorRects: m_Scissors + count: m_numScissors]; + } void CommandBufferMTL::SetDepthBounds(float boundsMin, float boundsMax) { //[m_RendererEncoder set] } void CommandBufferMTL::SetStencilReference(uint8_t frontRef, uint8_t backRef) { - [m_RendererEncoder setStencilFrontReferenceValue: frontRef backReferenceValue:backRef]; + m_DirtyBits |= CommandBufferDirtyBits::CMD_DIRTY_STENCIL; + m_StencilFront = frontRef; + m_StencilBack = backRef; + if (m_RendererEncoder) + [m_RendererEncoder + setStencilFrontReferenceValue: frontRef + backReferenceValue: backRef]; } void CommandBufferMTL::SetBlendConstants(const Color32f& color) { - [m_RendererEncoder - setBlendColorRed:color.x - green:color.y - blue:color.z - alpha:color.w - ]; + m_BlendColor = color; + m_DirtyBits |= CommandBufferDirtyBits::CMD_DIRTY_BLEND_CONSTANT; + if (m_RendererEncoder) + [m_RendererEncoder setBlendColorRed: m_BlendColor.x + green: m_BlendColor.y + blue: m_BlendColor.z + alpha: m_BlendColor.w]; + + } void CommandBufferMTL::SetShadingRate(const ShadingRateDesc& shadingRateDesc) { - - //[m_RendererEncoder sha] + m_shadingRateDesc = shadingRateDesc; + m_DirtyBits |= CommandBufferDirtyBits::CMD_DIRTY_SHADING_RATE; } void CommandBufferMTL::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { - - - Restore(); } void CommandBufferMTL::EndCurrentEncoders() { @@ -242,65 +344,53 @@ } void CommandBufferMTL::SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType) { - m_CurrentIndexCmd.m_Buffer = &(BufferMTL&)buffer; + m_indexBuffer.m_Buffer = &(BufferMTL&)buffer; switch(indexType) { case IndexType::UINT16: - m_CurrentIndexCmd.m_Type = MTLIndexType::MTLIndexTypeUInt16; + m_indexBuffer.m_Type = MTLIndexType::MTLIndexTypeUInt16; break; default: case IndexType::UINT32: - m_CurrentIndexCmd.m_Type = MTLIndexType::MTLIndexTypeUInt32; + m_indexBuffer.m_Type = MTLIndexType::MTLIndexTypeUInt32; break; } - m_CurrentIndexCmd.m_Offset = offset; + m_indexBuffer.m_Offset = offset; } -void CommandBufferMTL::Restore() { - [m_RendererEncoder setRenderPipelineState: m_GraphicsPipelineState]; - uint32_t vertexSlot = 0; - for( uint32_t attr = m_dirtyVertexBufferBits; attr > 0; attr = ( attr >> 1 ), vertexSlot++ ) { - [m_RendererEncoder setVertexBuffer: m_CurrentVertexCmd[vertexSlot].m_Buffer->GetHandle() - offset: m_CurrentVertexCmd[vertexSlot].m_Offset - atIndex: vertexSlot]; - } -} - void CommandBufferMTL::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { for(size_t i = 0; i < bufferNum; i++) { BufferMTL* mtlBuffer = (BufferMTL*)buffers[i]; const size_t slotIndex = i + baseSlot; - m_CurrentVertexCmd[slotIndex].m_Offset = offsets[i]; - m_CurrentVertexCmd[slotIndex].m_Buffer = mtlBuffer; + m_vertexBuffers[slotIndex].m_Offset = offsets[i]; + m_vertexBuffers[slotIndex].m_Buffer = mtlBuffer; m_dirtyVertexBufferBits |= (1 << slotIndex); - [m_RendererEncoder setVertexBuffer: mtlBuffer->GetHandle() - offset: offsets[i] - atIndex: i + baseSlot]; + if(m_RendererEncoder) + [m_RendererEncoder setVertexBuffer: mtlBuffer->GetHandle() + offset: offsets[i] + atIndex: i + baseSlot]; } } void CommandBufferMTL::Draw(const DrawDesc& drawDesc) { - //m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: m_renderPassDescriptor]; - [m_RendererEncoder drawPrimitives: m_CurrentPipeline->GetPrimitiveType() - vertexStart:drawDesc.baseVertex - vertexCount:drawDesc.vertexNum - instanceCount:drawDesc.instanceNum - baseInstance: 0]; + vertexStart:drawDesc.baseVertex + vertexCount:drawDesc.vertexNum + instanceCount:drawDesc.instanceNum + baseInstance: drawDesc.baseInstance]; - [m_RendererEncoder endEncoding]; } void CommandBufferMTL::DrawIndexed(const DrawIndexedDesc& drawIndexedDesc) { - id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); + id indexBuffer = m_indexBuffer.m_Buffer->GetHandle(); [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->GetPrimitiveType() indexCount: drawIndexedDesc.indexNum - indexType: m_CurrentIndexCmd.m_Type + indexType: m_indexBuffer.m_Type indexBuffer: indexBuffer - indexBufferOffset: m_CurrentIndexCmd.m_Offset]; + indexBufferOffset: m_indexBuffer.m_Offset]; } void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { @@ -313,31 +403,81 @@ indirectBufferOffset: offset]; } void CommandBufferMTL::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) { - // m_CurrentPipeline-> + InsertBarriers(); - id indexBuffer = m_CurrentIndexCmd.m_Buffer->GetHandle(); + id indexBuffer = m_indexBuffer.m_Buffer->GetHandle(); const BufferMTL& bufferImpl = (const BufferMTL&)buffer; - //const BufferMTL& bufferImpl = (const BufferMTL&)buffer; -// [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->m_primitiveType -// indexCount: drawIndexedDesc.indexNum -// indexType: m_CurrentIndexCmd.m_Type -// indexBuffer: indexBuffer -// indexBufferOffset: m_CurrentIndexCmd.m_Offset -// ]; + [m_RendererEncoder drawIndexedPrimitives: m_CurrentPipeline->GetPrimitiveType() + indexCount: drawNum + indexType: m_indexBuffer.m_Type + indexBuffer: indexBuffer + indexBufferOffset: m_indexBuffer.m_Offset + ]; } void CommandBufferMTL::Dispatch(const DispatchDesc& dispatchDesc) { + InsertBarriers(); + MTLSize threadgroupCount = MTLSizeMake(dispatchDesc.x, dispatchDesc.y, dispatchDesc.z); +// MTLSize threadPerThreadGroup = MTLSizeMake(dispatchDesc.x, dispatchDesc.y, dispatchDesc.z); +// [m_ComputeEncoder +// dispatchThreadgroups: threadgroupCount +// threadsPerThreadgroup: threadPerThreadGroup]; +} +void CommandBufferMTL::DispatchIndirect(const Buffer& buffer, uint64_t offset) { + InsertBarriers(); + + const BufferMTL& bufferImpl = (const BufferMTL&)buffer; + id dispatchBuffer = bufferImpl.GetHandle(); +// MTLSize threadPerThreadGroup = MTLSizeMake(0, 0, 0); +// [m_ComputeEncoder +// dispatchThreadgroupsWithIndirectBuffer: dispatchBuffer +// indirectBufferOffset: offset +// threadsPerThreadgroup: threadPerThreadGroup]; } -void CommandBufferMTL::DispatchIndirect(const Buffer& buffer, uint64_t offset) {} void CommandBufferMTL::BeginQuery(const QueryPool& queryPool, uint32_t offset) { } -void CommandBufferMTL::EndQuery(const QueryPool& queryPool, uint32_t offset) {} +void CommandBufferMTL::EndQuery(const QueryPool& queryPool, uint32_t offset) { + +} + + void CommandBufferMTL::BeginAnnotation(const char* name) { + if(m_RendererEncoder) { + [m_RendererEncoder insertDebugSignpost: [NSString stringWithUTF8String:name]]; + return; + } + + if(m_ComputeEncoder) { + [m_ComputeEncoder insertDebugSignpost: [NSString stringWithUTF8String:name]]; + return; + } + + if(m_BlitEncoder) { + [m_BlitEncoder insertDebugSignpost: [NSString stringWithUTF8String:name]]; + return; + } + + [m_Annotations addObject: [NSString stringWithUTF8String:name]]; } void CommandBufferMTL::EndAnnotation() { + if(m_RendererEncoder) { + [m_RendererEncoder popDebugGroup]; + return; + } + if(m_ComputeEncoder) { + [m_ComputeEncoder popDebugGroup]; + return; + } + + if(m_BlitEncoder) { + [m_BlitEncoder popDebugGroup]; + return; + } + [m_Annotations addObject: [NSNull null]]; +// m_DirtyBits |= CommandBufferDirtyBits::CMD_DIRTY_END_ANNOTATION; } void CommandBufferMTL::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) { @@ -476,6 +616,5 @@ } - #include "CommandBufferMTL.hpp" diff --git a/Source/Metal/CommandQueueMTL.h b/Source/Metal/CommandQueueMTL.h index 8e0206d3..dd4f1de5 100644 --- a/Source/Metal/CommandQueueMTL.h +++ b/Source/Metal/CommandQueueMTL.h @@ -7,7 +7,6 @@ namespace nri { struct DeviceMTL; - struct CommandQueueMTL { inline CommandQueueMTL(DeviceMTL& device) diff --git a/Source/Metal/CommandQueueMTL.mm b/Source/Metal/CommandQueueMTL.mm index 47065bc8..7db82365 100644 --- a/Source/Metal/CommandQueueMTL.mm +++ b/Source/Metal/CommandQueueMTL.mm @@ -18,7 +18,7 @@ "TRANSFER_QUEUE", // TRANSFER "COMPUTE_QUEUE" // COMPUTE }; - + m_Handle = [m_Device newCommandQueueWithMaxCommandBufferCount:512]; SetDebugName(queueNames[(uint32_t)type]); diff --git a/Source/Metal/PipelineMTL.h b/Source/Metal/PipelineMTL.h index e6480fd3..c1da4ecd 100644 --- a/Source/Metal/PipelineMTL.h +++ b/Source/Metal/PipelineMTL.h @@ -2,17 +2,13 @@ #import +#include "SharedMTL.h" + namespace nri { struct DeviceMTL; struct PipelineLayoutMTL; -NriEnum(PipelineType, uint8_t, - Compute, - Graphics, - Raytracing -); - struct PipelineMTL { public: inline PipelineMTL(DeviceMTL& device) @@ -47,7 +43,6 @@ struct PipelineMTL { MTLPrimitiveTopologyClass m_topologyClass; MTLPrimitiveType m_primitiveType; StageBits m_usedBits; - union{ id m_ComputePipeline = nil; id m_GraphicsPipeline; diff --git a/Source/Metal/SharedMTL.h b/Source/Metal/SharedMTL.h index 4c236110..770f7870 100644 --- a/Source/Metal/SharedMTL.h +++ b/Source/Metal/SharedMTL.h @@ -5,6 +5,30 @@ #include "ConversionMTL.h" #include "DeviceMTL.h" +#pragma once + +NriEnum(PipelineType, uint8_t, + Compute, + Graphics, + Raytracing +); + +NriBits(BarrierBits, uint8_t, + NONE = 0, + BARRIER_FLAG_BUFFERS = NriBit(0), + BARRIER_FLAG_TEXTURES = NriBit(1), + BARRIER_FLAG_RENDERTARGETS = NriBit(2), + BARRIER_FLAG_FENCE = NriBit(3)); + + +NriBits(CommandBufferDirtyBits, uint8_t, + NONE = 0, + CMD_DIRTY_STENCIL = NriBit(0), + CMD_DIRTY_SHADING_RATE = NriBit(1), + CMD_DIRTY_BLEND_CONSTANT = NriBit(2) + ); + + struct MemoryTypeInfo { union { uint32_t value;