Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: apple support #89

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# apple
.DS_Store

# cmake
CMakeFiles/
.cmake/
Expand Down
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ option (NRI_STATIC_LIBRARY "Build static library" 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)
Expand Down Expand Up @@ -226,6 +228,24 @@ 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_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()

# VK
if (NRI_ENABLE_VK_SUPPORT)
message ("NRI adding backend: VK")
Expand Down Expand Up @@ -335,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})

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

#pragma once

#include "NRIDeviceCreation.h"

NriNamespaceBegin

typedef void* MTLBufferHandle;
typedef void* MTLTextureHandle;
typedef void* MTLDeviceHandle;

NriStruct(DeviceCreationMTLDesc)
{
bool enableNRIValidation;
MTLDeviceHandle MtlDevice;
};

NriStruct(CommandBufferMTLDesc)
{

};

NriStruct(BufferMTLDesc)
{
MTLBufferHandle buffer;
void* mappedMemory;
//MTLResourceOptions options;
};

NriStruct(TextureMTLDesc)
{
//MTLTextureHandle mtlTexture;
//MTLTextureDescriptor* descriptor;
};

NriStruct(MemoryMTLDesc)
{
MTLBufferHandle buffer;
void* mappedMemory;
//MTLResourceOptions options;
};

NRI_API Nri(Result) NRI_CALL nriCreateDeviceFromMtlDevice(const NriRef(DeviceCreationMTLDesc) deviceDesc, NriRef(Device*) device);

NriNamespaceEnd

14 changes: 10 additions & 4 deletions Include/NRIDescs.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// © 2021 NVIDIA Corporation
// © 2021 NVIDIA Corporation

#pragma once

Expand Down Expand Up @@ -75,7 +75,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,
Expand Down Expand Up @@ -1271,12 +1272,17 @@ NriStruct(PipelineStatisticsDesc) {
#pragma region [ Device desc ]
//============================================================================================================================================================================================

// defined in apple framework
#undef INTEL
#undef AMD

Comment on lines +1275 to +1277
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like these are #defined in apple framework.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind to add these undefs to the interface.

NriEnum(Vendor, uint8_t,
UNKNOWN,
NVIDIA,
AMD,
INTEL
);
INTEL,
APPLE
);

NriStruct(AdapterDesc) {
char name[256];
Expand Down
38 changes: 37 additions & 1 deletion Source/Creation/Creation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -209,6 +214,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 Expand Up @@ -250,6 +273,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);

Expand Down Expand Up @@ -368,6 +402,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 <vulkan/vulkan.h>
# define GET_VK_FUNCTION(instance, name) \
Expand Down
48 changes: 48 additions & 0 deletions Source/Metal/BufferMTL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// © 2021 NVIDIA Corporation
#pragma once

#import <MetalKit/MetalKit.h>

namespace nri {

struct DeviceMTL;
struct MemoryMTL;

struct BufferMTL {

inline BufferMTL(DeviceMTL& device)
: m_Device(device) {
}

inline id<MTLBuffer> GetHandle() const {
return pBuffer;
}


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:
DeviceMTL& m_Device;
id<MTLBuffer> 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;
};


};
18 changes: 18 additions & 0 deletions Source/Metal/BufferMTL.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "SharedMTL.h"

#include "BufferMTL.h"

using namespace nri;


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;
}
36 changes: 36 additions & 0 deletions Source/Metal/CommandAllocatorMTL.h
Original file line number Diff line number Diff line change
@@ -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;
};

}

20 changes: 20 additions & 0 deletions Source/Metal/CommandAllocatorMTL.hpp
Original file line number Diff line number Diff line change
@@ -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);

28 changes: 28 additions & 0 deletions Source/Metal/CommandAllocatorMTL.mm
Original file line number Diff line number Diff line change
@@ -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"
Loading
Loading