Skip to content

Commit 20992dc

Browse files
committed
feat: add command queue mtl
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent 5e47459 commit 20992dc

File tree

9 files changed

+168
-75
lines changed

9 files changed

+168
-75
lines changed

Include/.DS_Store

-6 KB
Binary file not shown.

Include/Extensions/NRIWrapperMTL.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@
77
NRI_NAMESPACE_BEGIN
88

99

10+
NRI_ENUM
11+
(
12+
MTLGPUFamily, uint8_t,
13+
14+
Apple1,
15+
Apple2,
16+
Apple3,
17+
Apple4,
18+
Apple5,
19+
Apple6,
20+
Apple7,
21+
Mac1,
22+
Mac2,
23+
24+
MAX_APPLE_FAMILY_NUM
25+
);
1026

1127

1228
NRI_STRUCT(DeviceCreationMTLDesc)

Source/Metal/CommandQueueMTL.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// © 2021 NVIDIA Corporation
2+
3+
#pragma once
4+
5+
6+
namespace nri {
7+
8+
struct DeviceMTL;
9+
10+
struct CommandQueueMTL {
11+
12+
inline CommandQueueMTL(DeviceMTL& device)
13+
: m_Device(device) {
14+
}
15+
16+
~CommandQueueMTL();
17+
18+
inline operator id<MTLCommandQueue>() const {
19+
return m_Handle;
20+
}
21+
22+
inline DeviceMTL& GetDevice() const {
23+
return m_Device;
24+
}
25+
26+
inline uint32_t GetFamilyIndex() const {
27+
return m_FamilyIndex;
28+
}
29+
30+
inline CommandQueueType GetType() const {
31+
return m_Type;
32+
}
33+
34+
inline Lock& GetLock() {
35+
return m_Lock;
36+
}
37+
38+
void SetDebugName(const char* name);
39+
Result Create(CommandQueueType type, uint32_t familyIndex, id<MTLCommandQueue> handle);
40+
private:
41+
DeviceMTL& m_Device;
42+
uint32_t m_FamilyIndex = INVALID_FAMILY_INDEX;
43+
CommandQueueType m_Type = CommandQueueType(-1);
44+
id<MTLCommandQueue> m_Handle;
45+
Lock m_Lock;
46+
};
47+
48+
}
49+
50+

Source/Metal/CommandQueueMTL.mm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// © 2021 NVIDIA Corporation
2+
3+
#include "CommandQueueMTL.h"
4+
5+
using namespace nri;
6+
7+
8+
CommandQueueMTL::~CommandQueueMTL() {
9+
m_CommandQueue = nil;
10+
}
11+
12+
Result CommandQueueMTL::Create(CommandQueueType type, uint32_t familyIndex, id<MTLCommandQueue> handle) {
13+
m_Type = type;
14+
m_FamilyIndex = familyIndex;
15+
m_Handle = handle;
16+
return Result::SUCCESS;
17+
}
18+
19+
void CommandQueueMTL::SetDebugName(const char* name) {
20+
21+
}

Source/Metal/DeviceMTL.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ struct DeviceMTl final : public DeviceBase {
77
~BufferMetal();
88

99

10+
11+
Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue);
12+
1013
//================================================================================================================
1114
// DeviceBase
1215
//================================================================================================================
@@ -30,6 +33,7 @@ struct DeviceMTl final : public DeviceBase {
3033
private:
3134
DeviceDesc m_Desc = {};
3235
id<MTLDevice> m_Device;
36+
std::array<CommandQueueMTL*, (uint32_t)CommandQueueType::MAX_NUM> m_CommandQueues = {};
3337
DeviceDesc m_Desc = {};
3438
MTLGPUFamily m_Family;
3539
bool m_OwnsNativeObjects = true;

Source/Metal/DeviceMTL.mm

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
using namespace nri;
44

5+
6+
static inline bool FindMTLGpuFamily(id<MTLDevice> device, MTLGPUFamily *family);
7+
8+
59
BufferMetal::BufferMetal(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator)
610
: DeviceBase(callbacks, stdAllocator) {
711
m_Desc.graphicsAPI = GraphicsAPI::VK;
@@ -23,42 +27,6 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa
2327

2428
}
2529

26-
//ResultOrError<MTLGPUFamily> GetMTLGPUFamily(id<MTLDevice> device) {
27-
// // https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc
28-
//
29-
// if (@available(macOS 10.15, iOS 10.13, *)) {
30-
// if ([device supportsFamily:MTLGPUFamilyApple7]) {
31-
// return MTLGPUFamily::Apple7;
32-
// }
33-
// if ([device supportsFamily:MTLGPUFamilyApple6]) {
34-
// return MTLGPUFamily::Apple6;
35-
// }
36-
// if ([device supportsFamily:MTLGPUFamilyApple5]) {
37-
// return MTLGPUFamily::Apple5;
38-
// }
39-
// if ([device supportsFamily:MTLGPUFamilyApple4]) {
40-
// return MTLGPUFamily::Apple4;
41-
// }
42-
// if ([device supportsFamily:MTLGPUFamilyApple3]) {
43-
// return MTLGPUFamily::Apple3;
44-
// }
45-
// if ([device supportsFamily:MTLGPUFamilyApple2]) {
46-
// return MTLGPUFamily::Apple2;
47-
// }
48-
// if ([device supportsFamily:MTLGPUFamilyApple1]) {
49-
// return MTLGPUFamily::Apple1;
50-
// }
51-
//
52-
// // This family is no longer supported in the macOS 10.15 SDK but still exists so
53-
// // default to it.
54-
// return MTLGPUFamily::Mac1;
55-
// }
56-
//
57-
// return DAWN_INTERNAL_ERROR("Unsupported Metal device");
58-
//}
59-
//
60-
//} // anonymous namespace
61-
6230
FormatSupportBits DeviceMTL::GetFormatSupport(const Device& device, Format format) {
6331
int currentFamily = HIGHEST_GPU_FAMILY;
6432
for (; currentFamily >= (int)MTLGPUFamilyApple1; currentFamily--) {
@@ -69,42 +37,14 @@ Result CreateDeviceMTL(const DeviceCreationMTLDesc& deviceCreationDesc, DeviceBa
6937
}
7038
}
7139

72-
static inline bool FindMTLGpuFamily(id<MTLDevice> device, MTLGPUFamily* family) {
73-
//https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc
74-
if (@available(macOS 10.15, iOS 10.13, *)) {
75-
if ([device supportsFamily:MTLGPUFamilyApple7]) {
76-
return MTLGPUFamily::Apple7;
77-
}
78-
if ([device supportsFamily:MTLGPUFamilyApple6]) {
79-
return MTLGPUFamily::Apple6;
80-
}
81-
if ([device supportsFamily:MTLGPUFamilyApple5]) {
82-
return MTLGPUFamily::Apple5;
83-
}
84-
if ([device supportsFamily:MTLGPUFamilyApple4]) {
85-
return MTLGPUFamily::Apple4;
86-
}
87-
if ([device supportsFamily:MTLGPUFamilyApple3]) {
88-
return MTLGPUFamily::Apple3;
89-
}
90-
if ([device supportsFamily:MTLGPUFamilyApple2]) {
91-
return MTLGPUFamily::Apple2;
92-
}
93-
if ([device supportsFamily:MTLGPUFamilyApple1]) {
94-
return MTLGPUFamily::Apple1;
95-
}
9640

97-
// This family is no longer supported in the macOS 10.15 SDK but still exists so
98-
// default to it.
99-
//return MTLGPUFamily::Mac1;
100-
(*family) = MTLGPUFamily::Mac1;
101-
return true;
102-
}
41+
Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) {
10342

104-
return false;
10543
}
10644

10745

46+
47+
10848
Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) {
10949
m_OwnsNativeObjects = !isWrapper;
11050

@@ -134,9 +74,66 @@ static inline bool FindMTLGpuFamily(id<MTLDevice> device, MTLGPUFamily* family)
13474
}
13575
}
13676

77+
MTLGPUFamily family;
78+
if(!FindMTLGpuFamily(m_Device, family)) {
79+
return Result::UNSUPPORTED;
80+
}
81+
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
82+
//TODO: fill desc
83+
switch(family) {
84+
case MTLGPUFamily::Apple1:
85+
break;
86+
default:
87+
break;
88+
}
89+
13790
m_Desc.adapterDesc.luid = 0;
13891
m_Desc.adapterDesc.videoMemorySize = 0;
13992
m_Desc.adapterDesc.systemMemorySize = 0;
14093
return Result::SUCCESS;
14194

14295
}
96+
97+
98+
static inline bool FindMTLGpuFamily(id<MTLDevice> device,
99+
MTLGPUFamily *family) {
100+
// https://developer.apple.com/documentation/metal/mtldevice/detecting_gpu_features_and_metal_software_versions?language=objc
101+
if (@available(macOS 10.15, iOS 10.13, *)) {
102+
if ([device supportsFamily:MTLGPUFamilyApple7]) {
103+
(*family) = MTLGPUFamily::Apple7;
104+
return true;
105+
}
106+
if ([device supportsFamily:MTLGPUFamilyApple6]) {
107+
(*family) = MTLGPUFamily::Apple6;
108+
return true;
109+
}
110+
if ([device supportsFamily:MTLGPUFamilyApple5]) {
111+
(*family) = MTLGPUFamily::Apple5;
112+
return true;
113+
}
114+
if ([device supportsFamily:MTLGPUFamilyApple4]) {
115+
(*family) = MTLGPUFamily::Apple4;
116+
return true;
117+
}
118+
if ([device supportsFamily:MTLGPUFamilyApple3]) {
119+
(*family) = MTLGPUFamily::Apple3;
120+
return true;
121+
}
122+
if ([device supportsFamily:MTLGPUFamilyApple2]) {
123+
(*family) = MTLGPUFamily::Apple2;
124+
return true;
125+
}
126+
if ([device supportsFamily:MTLGPUFamilyApple1]) {
127+
(*family) = MTLGPUFamily::Apple1;
128+
return true;
129+
}
130+
131+
// This family is no longer supported in the macOS 10.15 SDK but still
132+
// exists so default to it.
133+
// return MTLGPUFamily::Mac1;
134+
(*family) = MTLGPUFamily::Mac1;
135+
return true;
136+
}
137+
return false;
138+
}
139+

Source/Metal/TextureMTL.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#pragma once
44

5-
65
namespace nri {
76

87
struct DeviceMTL;

Source/VK/CommandQueueVK.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ Result CommandQueueVK::Create(CommandQueueType type, uint32_t familyIndex, VkQue
1818
return Result::SUCCESS;
1919
}
2020

21-
//================================================================================================================
22-
// NRI
23-
//================================================================================================================
24-
2521
inline void CommandQueueVK::SetDebugName(const char* name) {
26-
m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_QUEUE, (uint64_t)m_Handle, name);
22+
[m_Handle setLabel:[NSString stringWithUTF8String:name]];
23+
}
24+
25+
26+
Result CommandQueueVK::WaitForIdle() {
27+
28+
id<MTLCommandBuffer> waitCmdBuf = [m_Handle commandBufferWithUnretainedReferences];
29+
30+
[waitCmdBuf commit];
31+
[waitCmdBuf waitUntilCompleted];
32+
33+
waitCmdBuf = nil;
2734
}
2835

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

90-
#include "CommandQueueVK.hpp"
97+
#include "CommandQueueVK.hpp"

Source/VK/CommandQueueVK.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ struct CommandQueueVK {
3939

4040
void SetDebugName(const char* name);
4141
void Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain);
42-
Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum);
4342
Result WaitForIdle();
4443

4544
private:

0 commit comments

Comments
 (0)