Skip to content

Commit

Permalink
feat: add graphics queu
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Sep 2, 2024
1 parent ed75b99 commit 36def81
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 41 deletions.
7 changes: 1 addition & 6 deletions Source/Metal/CommandQueueMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ struct CommandQueueMTL {
inline DeviceMTL& GetDevice() const {
return m_Device;
}

inline uint32_t GetFamilyIndex() const {
return m_FamilyIndex;
}

inline CommandQueueType GetType() const {
return m_Type;
Expand All @@ -38,10 +34,9 @@ struct CommandQueueMTL {
Result WaitForIdle();

void SetDebugName(const char* name);
Result Create(CommandQueueType type, uint32_t familyIndex, id<MTLCommandQueue> handle);
Result Create(CommandQueueType type);
private:
DeviceMTL& m_Device;
uint32_t m_FamilyIndex = 0;
CommandQueueType m_Type = CommandQueueType(-1);
id<MTLCommandQueue> m_Handle;
Lock m_Lock;
Expand Down
13 changes: 10 additions & 3 deletions Source/Metal/CommandQueueMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
m_Handle = nil;
}

Result CommandQueueMTL::Create(CommandQueueType type, uint32_t familyIndex, id<MTLCommandQueue> handle) {
Result CommandQueueMTL::Create(CommandQueueType type) {
m_Type = type;
m_FamilyIndex = familyIndex;
m_Handle = handle;
const char* queueNames[] = {
"GRAPHICS_QUEUE", // GRAPHICS
"TRANSFER_QUEUE", // TRANSFER
"COMPUTE_QUEUE" // COMPUTE
};

m_Handle = [m_Device newCommandQueueWithMaxCommandBufferCount:512];
SetDebugName(queueNames[(uint32_t)type]);

return Result::SUCCESS;
}

Expand Down
19 changes: 16 additions & 3 deletions Source/Metal/DeviceMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ struct DeviceMTL final : public DeviceBase {
return m_Desc;
}

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

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

if (result != Result::SUCCESS) {
Destroy(GetStdAllocator(), impl);
entity = nullptr;
} else
entity = (Interface*)impl;

return result;
}


Result FillFunctionTable(CoreInterface& table) const;
Result FillFunctionTable(HelperInterface& table) const;
Result FillFunctionTable(LowLatencyInterface& table) const;
Expand All @@ -37,6 +49,7 @@ struct DeviceMTL final : public DeviceBase {

Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper);
private:
Lock m_Lock;
id<MTLDevice> m_Device;
std::array<CommandQueueMTL*, (uint32_t)CommandQueueType::MAX_NUM> m_CommandQueues = {};
DeviceDesc m_Desc = {};
Expand Down
19 changes: 17 additions & 2 deletions Source/Metal/DeviceMTL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,39 @@ Result DeviceMetal::FillFunctionTable(CoreInterface& table) const {

#pragma endregion


Result DeviceMetal::FillFunctionTable(HelperInterface& table) const {

table = {};
return ResVult::UNSUPPORTED;
}

Result DeviceMetal::FillFunctionTable(LowLatencyInterface& table) const {
table = {};
return ResVult::UNSUPPORTED;
}

Result DeviceMetal::FillFunctionTable(MeshShaderInterface& table) const {
table = {};
return ResVult::UNSUPPORTED;
}

Result DeviceMetal::FillFunctionTable(RayTracingInterface& table) const {
table = {};
return ResVult::UNSUPPORTED;
}

Result DeviceMetal::FillFunctionTable(StreamerInterface& table) const {
table = {};
return ResVult::UNSUPPORTED;
}

Result DeviceMetal::FillFunctionTable(SwapChainInterface& table) const {
table = {};
return ResVult::UNSUPPORTED;
}

Result DeviceMetal::FillFunctionTable(ResourceAllocatorInterface& table) const {
table = {};
return ResVult::UNSUPPORTED;
}

Define_Core_Device_PartiallyFillFunctionTable(MTL);
Expand Down
75 changes: 52 additions & 23 deletions Source/Metal/DeviceMTL.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "SharedMTL.h"

#include "CommandQueueMTL.h"

#include "DeviceMTL.h"

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




DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator)
: DeviceBase(callbacks, stdAllocator) {
DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator) : DeviceBase(callbacks, stdAllocator) {
m_Desc.graphicsAPI = GraphicsAPI::VK;
m_Desc.nriVersionMajor = NRI_VERSION_MAJOR;
m_Desc.nriVersionMinor = NRI_VERSION_MINOR;

}


void DeviceMTL::Destruct() {
Destroy(GetStdAllocator(), this);
}

DeviceMTL::~DeviceMTL() {

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


Result DeviceMTL::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) {
return Result::SUCCESS;
ExclusiveScope lock(m_Lock);

// Check if already created (or wrapped)
uint32_t index = (uint32_t)commandQueueType;
if (m_CommandQueues[index]) {
commandQueue = (CommandQueue*)m_CommandQueues[index];
return Result::SUCCESS;
}

// Check if supported
//uint32_t queueFamilyIndex = m_QueueFamilyIndices[index];
//if (queueFamilyIndex == INVALID_FAMILY_INDEX) {
// commandQueue = nullptr;
// return Result::UNSUPPORTED;
//}

// Create
//VkQueue handle = VK_NULL_HANDLE;
//m_VK.GetDeviceQueue(m_Device, queueFamilyIndex, 0, &handle);

Result result = CreateImplementation<CommandQueueMTL>(commandQueue, commandQueueType);
if (result == Result::SUCCESS)
m_CommandQueues[index] = (CommandQueueMTL*)commandQueue;

return result;
}

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

Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) {
m_OwnsNativeObjects = !isWrapper;

if(isWrapper) {
// m_Device = deviceCreationMTLDesc.MtlDevice;
// m_Device = deviceCreationMTLDesc.MtlDevice;
}

strncpy(m_Desc.adapterDesc.name, [m_Device.name UTF8String], sizeof(m_Desc.adapterDesc.name));
// No vendor id, device id for Apple GPUs
if (strstr(m_Desc.adapterDesc.name, "Apple")) {
m_Desc.adapterDesc.vendor = nri::Vendor::APPLE;
}

}
const uint64_t regID = [m_Device registryID];
if (regID)
{
// IORef<io_registry_entry_t> entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID)));
// if (entry)
// {
// That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device.
// IORef<io_registry_entry_t> deviceEntry;
// if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess)
// {
// m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id")));
// m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id"));
// }
// }
// IORef<io_registry_entry_t> entry =AcquireIORef(IOServiceGetMatchingService(kIOMasterPortDefault, IORegistryEntryIDMatching(regID)));
// if (entry)
// {
// That returned the IOGraphicsAccelerator nub. Its parent, then, is the actual PCI device.
// IORef<io_registry_entry_t> deviceEntry;
// if (IORegistryEntryGetParentEntry(entry, kIOServicePlane, &deviceEntry) == kIOReturnSuccess)
// {
// m_Desc.adapterDesc.vendor = GetVendorFromID(GetEntryProperty(deviceEntry, CFSTR("vendor-id")));
// m_Desc.adapterDesc.deviceId = GetEntryProperty(deviceEntry, CFSTR("device-id"));
// }
// }
}

MTLGPUFamily family;
// if(!FindMTLGpuFamily(m_Device, family)) {
// return Result::UNSUPPORTED;
// if(!FindMTLGpuFamily(m_Device, family)) {
// return Result::UNSUPPORTED;
//}
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
//TODO: fill desc
Expand Down
2 changes: 1 addition & 1 deletion Source/Metal/TextureMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct TextureMTL {
return m_Desc;
}

Result Create(const TextureDesc& textureDesc);
Result CreateFromTextureDesc(const TextureDesc& textureDesc);
//Result Create(const TextureMTLDesc& textureDesc);

private:
Expand Down
9 changes: 6 additions & 3 deletions Source/Metal/TextureMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
// return Result::SUCCESS;
//}

Result TextureMTL::Create(const TextureDesc& textureDesc) {
Result TextureMTL::CreateFromTextureDesc(const TextureDesc& textureDesc) {
MTLTextureDescriptor* info = [[MTLTextureDescriptor alloc] init];
info.textureType = ::GetImageTypeMTL(textureDesc.type);
info.pixelFormat = ::GetFormatMTL(textureDesc.format, true);
info.width = textureDesc.width;
info.height = textureDesc.height;
info.depth = textureDesc.depth;
info.mipmapLevelCount = textureDesc.mipNum;
// info.sampleCount = textureDesc->mSampleCount;
// info.arrayLength = textureDesc->mArraySize;
info.sampleCount = textureDesc.sampleNum;
info.arrayLength = textureDesc.layerNum;

m_Handle = [m_Device newTextureWithDescriptor:info];
m_Desc = textureDesc;

//m_Handle = [m_Device newTextureWithDescriptor:textureDesc];
return Result::SUCCESS;
Expand Down

0 comments on commit 36def81

Please sign in to comment.