Skip to content

Commit

Permalink
[Metal] Add a real support check for Metal.
Browse files Browse the repository at this point in the history
  • Loading branch information
mellinoe committed Mar 20, 2018
1 parent 0058c68 commit 8a7ccd3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Veldrid.MetalBindings/MTLDevice.cs
Expand Up @@ -126,6 +126,9 @@ public Bool8 isDepth24Stencil8PixelFormatSupported
[DllImport(MetalFramework)]
public static extern MTLDevice MTLCreateSystemDefaultDevice();

[DllImport(MetalFramework)]
public static extern NSArray MTLCopyAllDevices();

private static readonly Selector sel_name = "name";
private static readonly Selector sel_maxThreadsPerThreadgroup = "maxThreadsPerThreadgroup";
private static readonly Selector sel_newLibraryWithSource = "newLibraryWithSource:options:error:";
Expand Down
14 changes: 14 additions & 0 deletions src/Veldrid.MetalBindings/NSArray.cs
@@ -0,0 +1,14 @@
using System;
using static Veldrid.MetalBindings.ObjectiveCRuntime;

namespace Veldrid.MetalBindings
{
public struct NSArray
{
public readonly IntPtr NativePtr;
public NSArray(IntPtr ptr) => NativePtr = ptr;

public UIntPtr count => UIntPtr_objc_msgSend(NativePtr, sel_count);
private static readonly Selector sel_count = "count";
}
}
2 changes: 1 addition & 1 deletion src/Veldrid/GraphicsDevice.cs
Expand Up @@ -595,7 +595,7 @@ public static bool IsBackendSupported(GraphicsBackend backend)
#endif
case GraphicsBackend.Metal:
#if !EXCLUDE_METAL_BACKEND
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
return MTL.MTLGraphicsDevice.IsSupported();
#else
return false;
#endif
Expand Down
36 changes: 36 additions & 0 deletions src/Veldrid/MTL/MTLGraphicsDevice.cs
Expand Up @@ -11,6 +11,8 @@ namespace Veldrid.MTL
{
internal unsafe class MTLGraphicsDevice : GraphicsDevice
{
private static readonly Lazy<bool> s_isSupported = new Lazy<bool>(GetIsSupported);

private readonly MTLDevice _device;
private readonly MTLCommandQueue _commandQueue;
private readonly MTLSwapchain _mainSwapchain;
Expand Down Expand Up @@ -307,6 +309,40 @@ public override void ResetFence(Fence fence)
Util.AssertSubtype<Fence, MTLFence>(fence).Reset();
}

internal static bool IsSupported() => s_isSupported.Value;

private static bool GetIsSupported()
{
bool result = false;
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
if (RuntimeInformation.OSDescription.Contains("Darwin"))
{
NSArray allDevices = MTLDevice.MTLCopyAllDevices();
result |= (ulong)allDevices.count > 0;
ObjectiveCRuntime.release(allDevices.NativePtr);
}
else
{
MTLDevice defaultDevice = MTLDevice.MTLCreateSystemDefaultDevice();
if (defaultDevice.NativePtr != IntPtr.Zero)
{
result = true;
ObjectiveCRuntime.release(defaultDevice.NativePtr);
}
}
}
}
catch
{
result = false;
}

return result;
}

internal MTLComputePipelineState GetUnalignedBufferCopyPipeline()
{
lock (_unalignedBufferCopyPipelineLock)
Expand Down

0 comments on commit 8a7ccd3

Please sign in to comment.