MTLDevice.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using static Veldrid.MetalBindings.ObjectiveCRuntime;
  4. namespace Veldrid.MetalBindings
  5. {
  6. public unsafe struct MTLDevice
  7. {
  8. private const string MetalFramework = "/System/Library/Frameworks/Metal.framework/Metal";
  9. public readonly IntPtr NativePtr;
  10. public static implicit operator IntPtr(MTLDevice device) => device.NativePtr;
  11. public MTLDevice(IntPtr nativePtr) => NativePtr = nativePtr;
  12. public string name => string_objc_msgSend(NativePtr, sel_name);
  13. public MTLSize maxThreadsPerThreadgroup
  14. {
  15. get
  16. {
  17. if (UseStret<MTLSize>())
  18. {
  19. return objc_msgSend_stret<MTLSize>(this, sel_maxThreadsPerThreadgroup);
  20. }
  21. else
  22. {
  23. return MTLSize_objc_msgSend(this, sel_maxThreadsPerThreadgroup);
  24. }
  25. }
  26. }
  27. public MTLLibrary newLibraryWithSource(string source, MTLCompileOptions options)
  28. {
  29. NSString sourceNSS = NSString.New(source);
  30. IntPtr library = IntPtr_objc_msgSend(NativePtr, sel_newLibraryWithSource,
  31. sourceNSS,
  32. options,
  33. out NSError error);
  34. release(sourceNSS.NativePtr);
  35. if (library == IntPtr.Zero)
  36. {
  37. throw new Exception("Shader compilation failed: " + error.localizedDescription);
  38. }
  39. return new MTLLibrary(library);
  40. }
  41. public MTLLibrary newLibraryWithData(DispatchData data)
  42. {
  43. IntPtr library = IntPtr_objc_msgSend(NativePtr, sel_newLibraryWithData, data.NativePtr, out NSError error);
  44. if (library == IntPtr.Zero)
  45. {
  46. throw new Exception("Unable to load Metal library: " + error.localizedDescription);
  47. }
  48. return new MTLLibrary(library);
  49. }
  50. public MTLRenderPipelineState newRenderPipelineStateWithDescriptor(MTLRenderPipelineDescriptor desc)
  51. {
  52. IntPtr ret = IntPtr_objc_msgSend(NativePtr, sel_newRenderPipelineStateWithDescriptor,
  53. desc.NativePtr,
  54. out NSError error);
  55. if (error.NativePtr != IntPtr.Zero)
  56. {
  57. throw new Exception("Failed to create new MTLRenderPipelineState: " + error.localizedDescription);
  58. }
  59. return new MTLRenderPipelineState(ret);
  60. }
  61. public MTLComputePipelineState newComputePipelineStateWithDescriptor(
  62. MTLComputePipelineDescriptor descriptor)
  63. {
  64. IntPtr ret = IntPtr_objc_msgSend(NativePtr, sel_newComputePipelineStateWithDescriptor,
  65. descriptor,
  66. 0,
  67. IntPtr.Zero,
  68. out NSError error);
  69. if (error.NativePtr != IntPtr.Zero)
  70. {
  71. throw new Exception("Failed to create new MTLRenderPipelineState: " + error.localizedDescription);
  72. }
  73. return new MTLComputePipelineState(ret);
  74. }
  75. public MTLCommandQueue newCommandQueue() => objc_msgSend<MTLCommandQueue>(NativePtr, sel_newCommandQueue);
  76. public MTLBuffer newBuffer(void* pointer, UIntPtr length, MTLResourceOptions options)
  77. {
  78. IntPtr buffer = IntPtr_objc_msgSend(NativePtr, sel_newBufferWithBytes,
  79. pointer,
  80. length,
  81. options);
  82. return new MTLBuffer(buffer);
  83. }
  84. public MTLBuffer newBufferWithLengthOptions(UIntPtr length, MTLResourceOptions options)
  85. {
  86. IntPtr buffer = IntPtr_objc_msgSend(NativePtr, sel_newBufferWithLength, length, options);
  87. return new MTLBuffer(buffer);
  88. }
  89. public MTLTexture newTextureWithDescriptor(MTLTextureDescriptor descriptor)
  90. => objc_msgSend<MTLTexture>(NativePtr, sel_newTextureWithDescriptor, descriptor.NativePtr);
  91. public MTLSamplerState newSamplerStateWithDescriptor(MTLSamplerDescriptor descriptor)
  92. => objc_msgSend<MTLSamplerState>(NativePtr, sel_newSamplerStateWithDescriptor, descriptor.NativePtr);
  93. public MTLDepthStencilState newDepthStencilStateWithDescriptor(MTLDepthStencilDescriptor descriptor)
  94. => objc_msgSend<MTLDepthStencilState>(NativePtr, sel_newDepthStencilStateWithDescriptor, descriptor.NativePtr);
  95. public Bool8 supportsTextureSampleCount(UIntPtr sampleCount)
  96. => bool8_objc_msgSend(NativePtr, sel_supportsTextureSampleCount, sampleCount);
  97. public Bool8 supportsFeatureSet(MTLFeatureSet featureSet)
  98. => bool8_objc_msgSend(NativePtr, sel_supportsFeatureSet, (uint)featureSet);
  99. public Bool8 isDepth24Stencil8PixelFormatSupported
  100. => bool8_objc_msgSend(NativePtr, sel_isDepth24Stencil8PixelFormatSupported);
  101. [DllImport(MetalFramework)]
  102. public static extern MTLDevice MTLCreateSystemDefaultDevice();
  103. [DllImport(MetalFramework)]
  104. public static extern NSArray MTLCopyAllDevices();
  105. private static readonly Selector sel_name = "name";
  106. private static readonly Selector sel_maxThreadsPerThreadgroup = "maxThreadsPerThreadgroup";
  107. private static readonly Selector sel_newLibraryWithSource = "newLibraryWithSource:options:error:";
  108. private static readonly Selector sel_newLibraryWithData = "newLibraryWithData:error:";
  109. private static readonly Selector sel_newRenderPipelineStateWithDescriptor = "newRenderPipelineStateWithDescriptor:error:";
  110. private static readonly Selector sel_newComputePipelineStateWithDescriptor = "newComputePipelineStateWithDescriptor:options:reflection:error:";
  111. private static readonly Selector sel_newCommandQueue = "newCommandQueue";
  112. private static readonly Selector sel_newBufferWithBytes = "newBufferWithBytes:length:options:";
  113. private static readonly Selector sel_newBufferWithLength = "newBufferWithLength:options:";
  114. private static readonly Selector sel_newTextureWithDescriptor = "newTextureWithDescriptor:";
  115. private static readonly Selector sel_newSamplerStateWithDescriptor = "newSamplerStateWithDescriptor:";
  116. private static readonly Selector sel_newDepthStencilStateWithDescriptor = "newDepthStencilStateWithDescriptor:";
  117. private static readonly Selector sel_supportsTextureSampleCount = "supportsTextureSampleCount:";
  118. private static readonly Selector sel_supportsFeatureSet = "supportsFeatureSet:";
  119. private static readonly Selector sel_isDepth24Stencil8PixelFormatSupported = "isDepth24Stencil8PixelFormatSupported";
  120. }
  121. }