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