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 34 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7b8f156
feat: apple support
pollend Aug 29, 2024
555ef0a
feat: add templte classes
pollend Aug 29, 2024
12332e5
feat: add command queue mtl
pollend Aug 30, 2024
b6f1b07
add ds_store
pollend Aug 30, 2024
e640750
feat: update commanQueuMTL
pollend Aug 30, 2024
b0da08f
feat: add conversion and start texture
pollend Aug 31, 2024
a5a013d
feat: add more to api
pollend Sep 1, 2024
29d0b72
feat: fix build
pollend Sep 2, 2024
874bfaf
feat: add graphics queu
pollend Sep 2, 2024
e100cb2
feat: add interface
pollend Sep 4, 2024
ebb6843
feat: add descriptor
pollend Sep 5, 2024
f2ef030
fix compiling errors
pollend Sep 5, 2024
5013f21
feat: start on command buffer logic
pollend Sep 6, 2024
b841503
add command buffer type
pollend Sep 6, 2024
07e558b
start working on command buffer
pollend Sep 7, 2024
322c8e9
feat: begin shader load logic
pollend Sep 8, 2024
8e3d1e1
fix compiling errors
pollend Oct 1, 2024
08cc6e2
feat: cleanup
pollend Oct 2, 2024
7cd0c47
feat: fix compiling errors
pollend Nov 14, 2024
2889d1d
feat: update desciprot and textureo
pollend Nov 19, 2024
c23acd8
feat: add command buffer impl progress
pollend Nov 21, 2024
55a0f41
feat: add memory mtl
pollend Nov 27, 2024
16e9e68
feat: more progress
pollend Nov 29, 2024
6e71837
feat: add descriptor binding logic
pollend Dec 1, 2024
4389b1d
feat: more progress with CommandBufferMTL
pollend Dec 25, 2024
b634abf
feat: progress command buffer logic
pollend Dec 26, 2024
cbe3d8e
feat: implement command buffer impl
pollend Dec 27, 2024
722501a
feat: add swapchain
pollend Dec 28, 2024
fdac99d
feat: add alloc Buffer Desc
pollend Dec 28, 2024
89637d6
feat: add more methods to implMTL
pollend Dec 29, 2024
ae69716
feat: add missing impl
pollend Dec 29, 2024
e757b6e
feat: add Creation logic
pollend Dec 30, 2024
645f67d
feat: fix formats
pollend Dec 31, 2024
2b43eb3
feat: address compiling issues
pollend Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -233,6 +235,25 @@ 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 IOKit"
"-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 @@ -301,8 +322,14 @@ file (GLOB NRI_EXTENSIONS "Include/Extensions/*.h" "Include/Extensions/*.hpp")
source_group ("Include/Extensions" FILES ${NRI_EXTENSIONS})

file (GLOB NRI_SOURCE "Source/Creation/*.cpp" "Source/Creation/*.h")
if (NRI_ENABLE_METAL_SUPPORT)
file (GLOB MTL_SOURCE "Source/Creation/*.mm")
list (APPEND NRI_SOURCE ${MTL_SOURCE})
endif()
source_group ("Sources" FILES ${NRI_SOURCE})



file (GLOB NRI_RESOURCES "Resources/*")
source_group ("Resources" FILES ${NRI_RESOURCES})

Expand Down Expand Up @@ -342,6 +369,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
62 changes: 62 additions & 0 deletions Include/Extensions/NRIWrapperMTL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// © 2021 NVIDIA Corporation

#pragma once

#include "NRIMacro.h"
#include "NRIDeviceCreation.h"


NriNamespaceBegin


typedef void* MTLHeap;
typedef void* MTLDeviceHandle; // id<MTLDevice>
typedef void* MTLBufferHandle; // id<MTLHeap>
typedef void* MTLTextureHandle;

typedef uint32_t MTLEnum;

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

NriStruct(CommandBufferMTLDesc)
{

};

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

NriStruct(TextureMTLDesc)
{
MTLTextureHandle mtlTexture;
MTLEnum textureType;
MTLEnum pixelFormat;
Nri(Dim_t) width;
Nri(Dim_t) height;
Nri(Dim_t) depth;
Nri(Mip_t) mipNum;
Nri(Dim_t) layerNum;
Nri(Sample_t) sampleNum;
//MTLTextureDescriptor* descriptor;
};

NriStruct(MemoryMTLDesc)
{
uint64_t size;
// MTLStorageMode storage;

//MTLResourceOptions options;
};

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

NriNamespaceEnd

19 changes: 15 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 @@ -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,
Expand Down Expand Up @@ -685,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;
Expand Down Expand Up @@ -1284,12 +1290,17 @@ NriStruct(PipelineStatisticsDesc) {
#pragma region [ Device desc ]
//============================================================================================================================================================================================

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

Comment on lines +1293 to +1295
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
1 change: 1 addition & 0 deletions Include/NRIMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__), \
}
Expand Down
41 changes: 41 additions & 0 deletions Source/Creation/Creation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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_VK
Result CreateDeviceVK(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device);
Result CreateDeviceVK(const DeviceCreationVKDesc& deviceDesc, DeviceBase*& device);
Expand Down Expand Up @@ -216,6 +221,11 @@ NRI_API Result NRI_CALL nriCreateDevice(const DeviceCreationDesc& deviceCreation
if (modifiedDeviceCreationDesc.graphicsAPI == GraphicsAPI::D3D12)
result = CreateDeviceD3D12(modifiedDeviceCreationDesc, deviceImpl);
#endif

#if NRI_USE_MTL
if (modifiedDeviceCreationDesc.graphicsAPI == GraphicsAPI::MTL)
result = CreateDeviceMTL(modifiedDeviceCreationDesc, deviceImpl);
#endif

#if NRI_USE_VK
if (modifiedDeviceCreationDesc.graphicsAPI == GraphicsAPI::VK)
Expand Down Expand Up @@ -284,6 +294,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 @@ -325,6 +353,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_VK
return NRIFormatToMTLFormat(format);
#else
return 0;
#endif
}


NRI_API uint32_t NRI_CALL nriConvertNRIFormatToVK(Format format) {
MaybeUnused(format);

Expand Down Expand Up @@ -443,6 +482,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
83 changes: 83 additions & 0 deletions Source/Creation/Creation.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#import <MetalKit/MetalKit.h>

#include "SharedExternal.h"

using namespace nri;

// 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<const uint32_t*>(CFDataGetBytePtr((CFDataRef)cfProp));
if (pValue) { value = *pValue; }
CFRelease(cfProp);
}

return value;
}


static int SortAdaptersByDedicatedVideoMemorySize(const void* pa, const void* pb) {
AdapterDesc* a = (AdapterDesc*)pa;
AdapterDesc* b = (AdapterDesc*)pb;

if (a->videoMemorySize > b->videoMemorySize)
return -1;

if (a->videoMemorySize < b->videoMemorySize)
return 1;

return 0;
}

NRI_API Result NRI_CALL nriEnumerateAdapters(AdapterDesc* adapterDescs, uint32_t& adapterDescNum) {

NSArray<id<MTLDevice>>* devices = MTLCopyAllDevices();
if(!adapterDescs) {
adapterDescNum = (uint32_t)devices.count;
return Result::SUCCESS;
}

AdapterDesc* adapterDescsSorted = (AdapterDesc*)alloca(sizeof(AdapterDesc) * devices.count);
for(size_t i = 0; i < devices.count; i++) {
NSString* name = [devices[i] name];
[name getCString:adapterDescsSorted[i].name maxLength: sizeof(adapterDescs[i].name) - 1 encoding: NSASCIIStringEncoding];
const uint64_t regID = [devices[i] registryID];
adapterDescsSorted[i].luid = regID;
if (regID)
{
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)
{
adapterDescsSorted[i].vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id"))) ;
adapterDescsSorted[i].deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id"));
}
}
} else {
adapterDescsSorted[i].vendor = nri::Vendor::APPLE;
}
adapterDescsSorted[i].videoMemorySize = [devices[i] recommendedMaxWorkingSetSize];
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
adapterDescsSorted[i].systemMemorySize = [pinfo physicalMemory];
}

// Sort by video memory size
qsort(adapterDescsSorted, devices.count, sizeof(adapterDescsSorted[0]), SortAdaptersByDedicatedVideoMemorySize);
for(size_t i = 0; i < MIN(devices.count, adapterDescNum); i++) {
adapterDescs[i] = adapterDescsSorted[i];
}
adapterDescNum = (uint32_t)MIN(devices.count, adapterDescNum);
return Result::SUCCESS;
}
Loading
Loading