Skip to content

Commit

Permalink
feat: apple support
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Aug 29, 2024
1 parent 1791bd8 commit 92fa0c6
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 3 deletions.
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ option (NRI_STATIC_LIBRARY "Build static library" OFF)

# Options: backends
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)

Expand Down Expand Up @@ -209,8 +210,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_VULKAN=1)

Expand Down Expand Up @@ -315,6 +327,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_NAME})

Expand Down
Binary file added Include/.DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions Include/Extensions/NRIWrapperMTL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// © 2021 NVIDIA Corporation

#pragma once

#include "NRIDeviceCreation.h"

NRI_NAMESPACE_BEGIN

NRI_STRUCT(DeviceCreationMTLDesc)
{
bool enableNRIValidation;
id<MTLDevice> 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

2 changes: 2 additions & 0 deletions Include/NRIDescs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@ NRI_ENUM
D3D11,
D3D12,
VK,
MTL,

MAX_NUM
);
Expand All @@ -1411,6 +1412,7 @@ NRI_ENUM
NVIDIA,
AMD,
INTEL,
APPLE,

MAX_NUM
);
Expand Down
23 changes: 23 additions & 0 deletions Source/Creation/Creation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Result CreateDeviceD3D12(const DeviceCreationDesc& deviceCreationDesc, DeviceBas
Result CreateDeviceD3D12(const DeviceCreationD3D12Desc& deviceCreationDesc, DeviceBase*& device);
#endif

#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);
Expand Down Expand Up @@ -188,6 +193,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;
Expand Down
14 changes: 14 additions & 0 deletions Source/Metal/BufferMetal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


#import <MetalKit/MetalKit.h>

struct BufferMetal {

private:
id<MTLBuffer> pBuffer;
//id<MTLIndirectCommandBuffer> pIndirectCommandBuffer;
uint64_t mOffset;
};



2 changes: 2 additions & 0 deletions Source/Metal/BufferMetal.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "BufferMetal.h"

36 changes: 36 additions & 0 deletions Source/Metal/DeviceMetal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

namespace nri {

struct DeviceMetal final : public DeviceBase {
BufferMetal(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& 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<MTLDevice> m_Device;
DeviceDesc m_Desc = {};
bool m_OwnsNativeObjects = true;
};
}; // namespace nri
17 changes: 17 additions & 0 deletions Source/Metal/DeviceMetal.hpp
Original file line number Diff line number Diff line change
@@ -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);
57 changes: 57 additions & 0 deletions Source/Metal/DeviceMetal.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "DeviceMetal.h"

using namespace nri;

BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& 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);
}
}
}


}
2 changes: 2 additions & 0 deletions Source/Shared/SharedExternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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;
Expand Down
2 changes: 1 addition & 1 deletion Source/VK/DeviceVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "DescriptorSetVK.h"
#include "DescriptorVK.h"
#include "FenceVK.h"
#include "HelperDeviceMemoryAllocator.h"
#include "HelperDeviceMemoryAllocator.h"Device
#include "MemoryVK.h"
#include "PipelineLayoutVK.h"
#include "PipelineVK.h"
Expand Down
3 changes: 2 additions & 1 deletion Source/VK/DeviceVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once


namespace nri {

struct CommandQueueVK;
Expand All @@ -11,7 +12,7 @@ struct DeviceVK final : public DeviceBase {
return m_Device;
}

inline operator VkPhysicalDevice() const {
inline operator VkPhysicalDevice() const {DeviceB
return m_PhysicalDevice;
}

Expand Down
5 changes: 5 additions & 0 deletions Source/Validation/DeviceVal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,11 @@ Result DeviceVal::CreateAccelerationStructure(const AccelerationStructureVKDesc&

#endif


#if NRI_USE_METAL

#endif

#if NRI_USE_D3D11

Result DeviceVal::CreateCommandBuffer(const CommandBufferD3D11Desc& commandBufferDesc, CommandBuffer*& commandBuffer) {
Expand Down

0 comments on commit 92fa0c6

Please sign in to comment.