Skip to content

Commit 36def81

Browse files
committed
feat: add graphics queu
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent ed75b99 commit 36def81

File tree

7 files changed

+103
-41
lines changed

7 files changed

+103
-41
lines changed

Source/Metal/CommandQueueMTL.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ struct CommandQueueMTL {
2222
inline DeviceMTL& GetDevice() const {
2323
return m_Device;
2424
}
25-
26-
inline uint32_t GetFamilyIndex() const {
27-
return m_FamilyIndex;
28-
}
2925

3026
inline CommandQueueType GetType() const {
3127
return m_Type;
@@ -38,10 +34,9 @@ struct CommandQueueMTL {
3834
Result WaitForIdle();
3935

4036
void SetDebugName(const char* name);
41-
Result Create(CommandQueueType type, uint32_t familyIndex, id<MTLCommandQueue> handle);
37+
Result Create(CommandQueueType type);
4238
private:
4339
DeviceMTL& m_Device;
44-
uint32_t m_FamilyIndex = 0;
4540
CommandQueueType m_Type = CommandQueueType(-1);
4641
id<MTLCommandQueue> m_Handle;
4742
Lock m_Lock;

Source/Metal/CommandQueueMTL.mm

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
m_Handle = nil;
1111
}
1212

13-
Result CommandQueueMTL::Create(CommandQueueType type, uint32_t familyIndex, id<MTLCommandQueue> handle) {
13+
Result CommandQueueMTL::Create(CommandQueueType type) {
1414
m_Type = type;
15-
m_FamilyIndex = familyIndex;
16-
m_Handle = handle;
15+
const char* queueNames[] = {
16+
"GRAPHICS_QUEUE", // GRAPHICS
17+
"TRANSFER_QUEUE", // TRANSFER
18+
"COMPUTE_QUEUE" // COMPUTE
19+
};
20+
21+
m_Handle = [m_Device newCommandQueueWithMaxCommandBufferCount:512];
22+
SetDebugName(queueNames[(uint32_t)type]);
23+
1724
return Result::SUCCESS;
1825
}
1926

Source/Metal/DeviceMTL.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,23 @@ struct DeviceMTL final : public DeviceBase {
2121
return m_Desc;
2222
}
2323

24-
//FormatSupportBits GetFormatSupport(const Device& device, Format format);
24+
void Destruct() override;
2525

26-
//void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const;
27-
// void FillCreateInfo(const TextureDesc& bufferDesc, MTLTextureDescriptor* info) const;
26+
template <typename Implementation, typename Interface, typename... Args>
27+
inline Result CreateImplementation(Interface*& entity, const Args&... args) {
28+
Implementation* impl = Allocate<Implementation>(GetStdAllocator(), *this);
29+
Result result = impl->Create(args...);
2830

31+
if (result != Result::SUCCESS) {
32+
Destroy(GetStdAllocator(), impl);
33+
entity = nullptr;
34+
} else
35+
entity = (Interface*)impl;
36+
37+
return result;
38+
}
39+
40+
2941
Result FillFunctionTable(CoreInterface& table) const;
3042
Result FillFunctionTable(HelperInterface& table) const;
3143
Result FillFunctionTable(LowLatencyInterface& table) const;
@@ -37,6 +49,7 @@ struct DeviceMTL final : public DeviceBase {
3749

3850
Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper);
3951
private:
52+
Lock m_Lock;
4053
id<MTLDevice> m_Device;
4154
std::array<CommandQueueMTL*, (uint32_t)CommandQueueType::MAX_NUM> m_CommandQueues = {};
4255
DeviceDesc m_Desc = {};

Source/Metal/DeviceMTL.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,39 @@ Result DeviceMetal::FillFunctionTable(CoreInterface& table) const {
214214

215215
#pragma endregion
216216

217-
218217
Result DeviceMetal::FillFunctionTable(HelperInterface& table) const {
219-
218+
table = {};
219+
return ResVult::UNSUPPORTED;
220220
}
221221

222222
Result DeviceMetal::FillFunctionTable(LowLatencyInterface& table) const {
223+
table = {};
224+
return ResVult::UNSUPPORTED;
223225
}
224226

225227
Result DeviceMetal::FillFunctionTable(MeshShaderInterface& table) const {
228+
table = {};
229+
return ResVult::UNSUPPORTED;
226230
}
227231

228232
Result DeviceMetal::FillFunctionTable(RayTracingInterface& table) const {
233+
table = {};
234+
return ResVult::UNSUPPORTED;
229235
}
236+
230237
Result DeviceMetal::FillFunctionTable(StreamerInterface& table) const {
238+
table = {};
239+
return ResVult::UNSUPPORTED;
231240
}
241+
232242
Result DeviceMetal::FillFunctionTable(SwapChainInterface& table) const {
243+
table = {};
244+
return ResVult::UNSUPPORTED;
233245
}
246+
234247
Result DeviceMetal::FillFunctionTable(ResourceAllocatorInterface& table) const {
248+
table = {};
249+
return ResVult::UNSUPPORTED;
235250
}
236251

237252
Define_Core_Device_PartiallyFillFunctionTable(MTL);

Source/Metal/DeviceMTL.mm

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "SharedMTL.h"
22

3+
#include "CommandQueueMTL.h"
4+
35
#include "DeviceMTL.h"
46

57
using namespace nri;
@@ -48,15 +50,18 @@ static bool FindMTLGpuFamily(id<MTLDevice> device,
4850

4951

5052

51-
52-
DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator)
53-
: DeviceBase(callbacks, stdAllocator) {
53+
DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator) : DeviceBase(callbacks, stdAllocator) {
5454
m_Desc.graphicsAPI = GraphicsAPI::VK;
5555
m_Desc.nriVersionMajor = NRI_VERSION_MAJOR;
5656
m_Desc.nriVersionMinor = NRI_VERSION_MINOR;
5757

5858
}
5959

60+
61+
void DeviceMTL::Destruct() {
62+
Destroy(GetStdAllocator(), this);
63+
}
64+
6065
DeviceMTL::~DeviceMTL() {
6166

6267
}
@@ -74,7 +79,31 @@ static bool FindMTLGpuFamily(id<MTLDevice> device,
7479

7580

7681
Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) {
77-
return Result::SUCCESS;
82+
ExclusiveScope lock(m_Lock);
83+
84+
// Check if already created (or wrapped)
85+
uint32_t index = (uint32_t)commandQueueType;
86+
if (m_CommandQueues[index]) {
87+
commandQueue = (CommandQueue*)m_CommandQueues[index];
88+
return Result::SUCCESS;
89+
}
90+
91+
// Check if supported
92+
//uint32_t queueFamilyIndex = m_QueueFamilyIndices[index];
93+
//if (queueFamilyIndex == INVALID_FAMILY_INDEX) {
94+
// commandQueue = nullptr;
95+
// return Result::UNSUPPORTED;
96+
//}
97+
98+
// Create
99+
//VkQueue handle = VK_NULL_HANDLE;
100+
//m_VK.GetDeviceQueue(m_Device, queueFamilyIndex, 0, &handle);
101+
102+
Result result = CreateImplementation<CommandQueueMTL>(commandQueue, commandQueueType);
103+
if (result == Result::SUCCESS)
104+
m_CommandQueues[index] = (CommandQueueMTL*)commandQueue;
105+
106+
return result;
78107
}
79108

80109
//void DeviceMTL::FillCreateInfo(const TextureDesc& textureDesc, MTLTextureDescriptor* info) const {
@@ -90,36 +119,36 @@ static bool FindMTLGpuFamily(id<MTLDevice> device,
90119

91120
Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) {
92121
m_OwnsNativeObjects = !isWrapper;
93-
122+
94123
if(isWrapper) {
95-
// m_Device = deviceCreationMTLDesc.MtlDevice;
124+
// m_Device = deviceCreationMTLDesc.MtlDevice;
96125
}
97-
126+
98127
strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name));
99128
// No vendor id, device id for Apple GPUs
100129
if (strstr(m_Desc.adapterDesc.name, "Apple")) {
101130
m_Desc.adapterDesc.vendor = nri::Vendor::APPLE;
102-
}
103-
131+
}
132+
104133
const uint64_t regID = [m_Device registryID];
105134
if (regID)
106135
{
107-
// IORef<io_registry_entry_t> entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID)));
108-
// if (entry)
109-
// {
110-
// That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device.
111-
// IORef<io_registry_entry_t> deviceEntry;
112-
// if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess)
113-
// {
114-
// m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id")));
115-
// m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id"));
116-
// }
117-
// }
136+
// IORef<io_registry_entry_t> entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID)));
137+
// if (entry)
138+
// {
139+
// That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device.
140+
// IORef<io_registry_entry_t> deviceEntry;
141+
// if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess)
142+
// {
143+
// m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id")));
144+
// m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id"));
145+
// }
146+
// }
118147
}
119-
148+
120149
MTLGPUFamily family;
121-
// if(!FindMTLGpuFamily(m_Device, family)) {
122-
// return Result::UNSUPPORTED;
150+
// if(!FindMTLGpuFamily(m_Device, family)) {
151+
// return Result::UNSUPPORTED;
123152
//}
124153
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
125154
//TODO: fill desc

Source/Metal/TextureMTL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct TextureMTL {
2626
return m_Desc;
2727
}
2828

29-
Result Create(const TextureDesc& textureDesc);
29+
Result CreateFromTextureDesc(const TextureDesc& textureDesc);
3030
//Result Create(const TextureMTLDesc& textureDesc);
3131

3232
private:

Source/Metal/TextureMTL.mm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414
// return Result::SUCCESS;
1515
//}
1616

17-
Result TextureMTL::Create(const TextureDesc& textureDesc) {
17+
Result TextureMTL::CreateFromTextureDesc(const TextureDesc& textureDesc) {
1818
MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init];
1919
info.textureType = ::GetImageTypeMTL(textureDesc.type);
2020
info.pixelFormat = ::GetFormatMTL(textureDesc.format, true);
2121
info.width = textureDesc.width;
2222
info.height = textureDesc.height;
2323
info.depth = textureDesc.depth;
2424
info.mipmapLevelCount = textureDesc.mipNum;
25-
// info.sampleCount = textureDesc->mSampleCount;
26-
// info.arrayLength = textureDesc->mArraySize;
25+
info.sampleCount = textureDesc.sampleNum;
26+
info.arrayLength = textureDesc.layerNum;
27+
28+
m_Handle = [m_Device newTextureWithDescriptor:info];
29+
m_Desc = textureDesc;
2730

2831
//m_Handle = [m_Device newTextureWithDescriptor:textureDesc];
2932
return Result::SUCCESS;

0 commit comments

Comments
 (0)