internal static bool IsFormatSupported(PixelFormat format, TextureUsage usage, MTLFeatureSupport metalFeatures) { switch (format) { case PixelFormat.BC1_Rgb_UNorm: case PixelFormat.BC1_Rgb_UNorm_SRgb: case PixelFormat.BC1_Rgba_UNorm: case PixelFormat.BC1_Rgba_UNorm_SRgb: case PixelFormat.BC2_UNorm: case PixelFormat.BC2_UNorm_SRgb: case PixelFormat.BC3_UNorm: case PixelFormat.BC3_UNorm_SRgb: case PixelFormat.BC4_UNorm: case PixelFormat.BC4_SNorm: case PixelFormat.BC5_UNorm: case PixelFormat.BC5_SNorm: case PixelFormat.BC7_UNorm: case PixelFormat.BC7_UNorm_SRgb: return(metalFeatures.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v1) || metalFeatures.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v2) || metalFeatures.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v3)); case PixelFormat.ETC2_R8_G8_B8_UNorm: case PixelFormat.ETC2_R8_G8_B8_A1_UNorm: case PixelFormat.ETC2_R8_G8_B8_A8_UNorm: return(metalFeatures.IsSupported(MTLFeatureSet.iOS_GPUFamily1_v1) || metalFeatures.IsSupported(MTLFeatureSet.iOS_GPUFamily2_v1) || metalFeatures.IsSupported(MTLFeatureSet.iOS_GPUFamily3_v1) || metalFeatures.IsSupported(MTLFeatureSet.iOS_GPUFamily4_v1)); case PixelFormat.R16_UNorm: return(((usage & TextureUsage.DepthStencil) == 0) || metalFeatures.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v2) || metalFeatures.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v3)); default: return(true); } }
public MTLGraphicsDevice( GraphicsDeviceOptions options, SwapchainDescription?swapchainDesc) { _device = MTLDevice.MTLCreateSystemDefaultDevice(); MetalFeatures = new MTLFeatureSupport(_device); Features = new GraphicsDeviceFeatures( computeShader: true, geometryShader: false, tessellationShaders: false, multipleViewports: MetalFeatures.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v3), samplerLodBias: false, drawBaseVertex: true, drawBaseInstance: true, drawIndirect: true, drawIndirectBaseInstance: true, fillModeWireframe: true, samplerAnisotropy: true, depthClipDisable: true, texture1D: true, // TODO: Should be macOS 10.11+ and iOS 11.0+. independentBlend: true, structuredBuffer: true, subsetTextureView: true, commandListDebugMarkers: true, bufferRangeBinding: true); ResourceBindingModel = options.ResourceBindingModel; _libSystem = new NativeLibrary("libSystem.dylib"); _concreteGlobalBlock = _libSystem.LoadFunction <IntPtr>("_NSConcreteGlobalBlock"); if (MetalFeatures.IsMacOS) { _completionHandler = OnCommandBufferCompleted; } else { _completionHandler = OnCommandBufferCompleted_Static; } _completionHandlerFuncPtr = Marshal.GetFunctionPointerForDelegate <MTLCommandBufferHandler>(_completionHandler); _completionBlockDescriptor = Marshal.AllocHGlobal(Unsafe.SizeOf <BlockDescriptor>()); BlockDescriptor *descriptorPtr = (BlockDescriptor *)_completionBlockDescriptor; descriptorPtr->reserved = 0; descriptorPtr->Block_size = (ulong)Unsafe.SizeOf <BlockDescriptor>(); _completionBlockLiteral = Marshal.AllocHGlobal(Unsafe.SizeOf <BlockLiteral>()); BlockLiteral *blockPtr = (BlockLiteral *)_completionBlockLiteral; blockPtr->isa = _concreteGlobalBlock; blockPtr->flags = 1 << 28 | 1 << 29; blockPtr->invoke = _completionHandlerFuncPtr; blockPtr->descriptor = descriptorPtr; if (!MetalFeatures.IsMacOS) { lock (s_aotRegisteredBlocks) { s_aotRegisteredBlocks.Add(_completionBlockLiteral, this); } } ResourceFactory = new MTLResourceFactory(this); _commandQueue = _device.newCommandQueue(); TextureSampleCount[] allSampleCounts = (TextureSampleCount[])Enum.GetValues(typeof(TextureSampleCount)); _supportedSampleCounts = new bool[allSampleCounts.Length]; for (int i = 0; i < allSampleCounts.Length; i++) { TextureSampleCount count = allSampleCounts[i]; uint uintValue = FormatHelpers.GetSampleCountUInt32(count); if (_device.supportsTextureSampleCount((UIntPtr)uintValue)) { _supportedSampleCounts[i] = true; } } if (swapchainDesc != null) { SwapchainDescription desc = swapchainDesc.Value; _mainSwapchain = new MTLSwapchain(this, ref desc); } PostDeviceCreated(); }