Skip to content

Commit

Permalink
feat: add command queue mtl
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Aug 30, 2024
1 parent 5e47459 commit 20992dc
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 75 deletions.
Binary file removed Include/.DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions Include/Extensions/NRIWrapperMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions Source/Metal/CommandQueueMTL.h
Original file line number Diff line number Diff line change
@@ -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<MTLCommandQueue>() 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<MTLCommandQueue> handle);
private:
DeviceMTL& m_Device;
uint32_t m_FamilyIndex = INVALID_FAMILY_INDEX;
CommandQueueType m_Type = CommandQueueType(-1);
id<MTLCommandQueue> m_Handle;
Lock m_Lock;
};

}


21 changes: 21 additions & 0 deletions Source/Metal/CommandQueueMTL.mm
Original file line number Diff line number Diff line change
@@ -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<MTLCommandQueue> handle) {
m_Type = type;
m_FamilyIndex = familyIndex;
m_Handle = handle;
return Result::SUCCESS;
}

void CommandQueueMTL::SetDebugName(const char* name) {

}
4 changes: 4 additions & 0 deletions Source/Metal/DeviceMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ struct DeviceMTl final : public DeviceBase {
~BufferMetal();



Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue);

//================================================================================================================
// DeviceBase
//================================================================================================================
Expand All @@ -30,6 +33,7 @@ struct DeviceMTl final : public DeviceBase {
private:
DeviceDesc m_Desc = {};
id<MTLDevice> m_Device;
std::array<CommandQueueMTL*, (uint32_t)CommandQueueType::MAX_NUM> m_CommandQueues = {};
DeviceDesc m_Desc = {};
MTLGPUFamily m_Family;
bool m_OwnsNativeObjects = true;
Expand Down
131 changes: 64 additions & 67 deletions Source/Metal/DeviceMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

using namespace nri;


static inline bool FindMTLGpuFamily(id<MTLDevice> device, MTLGPUFamily *family);


BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator)
: DeviceBase(callbacks, stdAllocator) {
m_Desc.graphicsAPI = GraphicsAPI::VK;
Expand All @@ -23,42 +27,6 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa

}

//ResultOrError<MTLGPUFamily> GetMTLGPUFamily(id<MTLDevice> 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--) {
Expand All @@ -69,42 +37,14 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa
}
}

static inline bool FindMTLGpuFamily(id<MTLDevice> 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;

Expand Down Expand Up @@ -134,9 +74,66 @@ static inline bool FindMTLGpuFamily(id<MTLDevice> 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<MTLDevice> 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;
}

1 change: 0 additions & 1 deletion Source/Metal/TextureMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#pragma once


namespace nri {

struct DeviceMTL;
Expand Down
19 changes: 13 additions & 6 deletions Source/VK/CommandQueueVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ Result CommandQueueVK::Create(CommandQueueType type, uint32_t familyIndex, VkQue
return Result::SUCCESS;
}

//================================================================================================================
// NRI
//================================================================================================================

inline void CommandQueueVK::SetDebugName(const char* name) {
m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_QUEUE, (uint64_t)m_Handle, name);
[m_Handle setLabel:[NSString stringWithUTF8String:name]];
}


Result CommandQueueVK::WaitForIdle() {

id<MTLCommandBuffer> waitCmdBuf = [m_Handle commandBufferWithUnretainedReferences];

[waitCmdBuf commit];
[waitCmdBuf waitUntilCompleted];

waitCmdBuf = nil;
}

inline void CommandQueueVK::Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain) {
Expand Down Expand Up @@ -87,4 +94,4 @@ inline Result CommandQueueVK::WaitForIdle() {
return Result::SUCCESS;
}

#include "CommandQueueVK.hpp"
#include "CommandQueueVK.hpp"
1 change: 0 additions & 1 deletion Source/VK/CommandQueueVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct CommandQueueVK {

void SetDebugName(const char* name);
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:
Expand Down

0 comments on commit 20992dc

Please sign in to comment.