forked from veldrid/veldrid
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MTLGraphicsDevice.cs
668 lines (578 loc) · 25.3 KB
/
MTLGraphicsDevice.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using NativeLibrary = NativeLibraryLoader.NativeLibrary;
using Veldrid.MetalBindings;
namespace Veldrid.MTL
{
internal unsafe class MTLGraphicsDevice : GraphicsDevice
{
private static readonly Lazy<bool> s_isSupported = new Lazy<bool>(GetIsSupported);
private static readonly Dictionary<IntPtr, MTLGraphicsDevice> s_aotRegisteredBlocks
= new Dictionary<IntPtr, MTLGraphicsDevice>();
private readonly MTLDevice _device;
private readonly string _deviceName;
private readonly GraphicsApiVersion _apiVersion;
private readonly MTLCommandQueue _commandQueue;
private readonly MTLSwapchain _mainSwapchain;
private readonly bool[] _supportedSampleCounts;
private BackendInfoMetal _metalInfo;
private readonly object _submittedCommandsLock = new object();
private readonly Dictionary<MTLCommandBuffer, MTLCommandList> _submittedCLs = new Dictionary<MTLCommandBuffer, MTLCommandList>();
private MTLCommandBuffer _latestSubmittedCB;
private readonly object _resetEventsLock = new object();
private readonly List<ManualResetEvent[]> _resetEvents = new List<ManualResetEvent[]>();
private const string UnalignedBufferCopyPipelineMacOSName = "MTL_UnalignedBufferCopy_macOS";
private const string UnalignedBufferCopyPipelineiOSName = "MTL_UnalignedBufferCopy_iOS";
private readonly object _unalignedBufferCopyPipelineLock = new object();
private readonly NativeLibrary _libSystem;
private readonly IntPtr _concreteGlobalBlock;
private MTLShader _unalignedBufferCopyShader;
private MTLComputePipelineState _unalignedBufferCopyPipeline;
private MTLCommandBufferHandler _completionHandler;
private readonly IntPtr _completionHandlerFuncPtr;
private readonly IntPtr _completionBlockDescriptor;
private readonly IntPtr _completionBlockLiteral;
private readonly IMTLDisplayLink _displayLink;
private readonly AutoResetEvent _nextFrameReadyEvent;
private readonly EventWaitHandle _frameEndedEvent = new EventWaitHandle(true, EventResetMode.ManualReset);
public MTLDevice Device => _device;
public MTLCommandQueue CommandQueue => _commandQueue;
public MTLFeatureSupport MetalFeatures { get; }
public ResourceBindingModel ResourceBindingModel { get; }
public bool PreferMemorylessDepthTargets { get; }
public MTLGraphicsDevice(GraphicsDeviceOptions options, SwapchainDescription? swapchainDesc)
: this(options, swapchainDesc, new MetalDeviceOptions())
{
}
public override void UpdateActiveDisplay(int x, int y, int w, int h)
{
if (_displayLink != null)
{
_displayLink.UpdateActiveDisplay(x, y, w, h);
}
}
public override double GetActualRefreshPeriod()
{
if (_displayLink != null)
{
return _displayLink.GetActualOutputVideoRefreshPeriod();
}
return -1.0f;
}
public MTLGraphicsDevice(
GraphicsDeviceOptions options,
SwapchainDescription? swapchainDesc,
MetalDeviceOptions metalOptions)
{
_device = MTLDevice.MTLCreateSystemDefaultDevice();
_deviceName = _device.name;
MetalFeatures = new MTLFeatureSupport(_device);
int major = (int)MetalFeatures.MaxFeatureSet / 10000;
int minor = (int)MetalFeatures.MaxFeatureSet % 10000;
_apiVersion = new GraphicsApiVersion(major, minor, 0, 0);
Features = new GraphicsDeviceFeatures(
computeShader: true,
geometryShader: false,
tessellationShaders: false,
multipleViewports: MetalFeatures.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v3),
samplerLodBias: false,
drawBaseVertex: MetalFeatures.IsDrawBaseVertexInstanceSupported(),
drawBaseInstance: MetalFeatures.IsDrawBaseVertexInstanceSupported(),
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,
shaderFloat64: false);
ResourceBindingModel = options.ResourceBindingModel;
PreferMemorylessDepthTargets = metalOptions.PreferMemorylessDepthTargets;
if (MetalFeatures.IsMacOS)
{
_libSystem = new NativeLibrary("libSystem.dylib");
_concreteGlobalBlock = _libSystem.LoadFunction("_NSConcreteGlobalBlock");
_completionHandler = OnCommandBufferCompleted;
_displayLink = new MTLCVDisplayLink();
}
else
{
_concreteGlobalBlock = IntPtr.Zero;
_completionHandler = OnCommandBufferCompleted_Static;
}
if (_displayLink != null)
{
_nextFrameReadyEvent = new AutoResetEvent(true);
_displayLink.Callback += OnDisplayLinkCallback;
}
_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);
}
_metalInfo = new BackendInfoMetal(this);
PostDeviceCreated();
}
public override string DeviceName => _deviceName;
public override string VendorName => "Apple";
public override GraphicsApiVersion ApiVersion => _apiVersion;
public override GraphicsBackend BackendType => GraphicsBackend.Metal;
public override bool IsUvOriginTopLeft => true;
public override bool IsDepthRangeZeroToOne => true;
public override bool IsClipSpaceYInverted => false;
public override ResourceFactory ResourceFactory { get; }
public override Swapchain MainSwapchain => _mainSwapchain;
public override GraphicsDeviceFeatures Features { get; }
private void OnCommandBufferCompleted(IntPtr block, MTLCommandBuffer cb)
{
lock (_submittedCommandsLock)
{
MTLCommandList cl = _submittedCLs[cb];
_submittedCLs.Remove(cb);
cl.OnCompleted(cb);
if (_latestSubmittedCB.NativePtr == cb.NativePtr)
{
_latestSubmittedCB = default(MTLCommandBuffer);
}
}
ObjectiveCRuntime.release(cb.NativePtr);
}
// Xamarin AOT requires native callbacks be static.
[MonoPInvokeCallback(typeof(MTLCommandBufferHandler))]
private static void OnCommandBufferCompleted_Static(IntPtr block, MTLCommandBuffer cb)
{
lock (s_aotRegisteredBlocks)
{
if (s_aotRegisteredBlocks.TryGetValue(block, out MTLGraphicsDevice gd))
{
gd.OnCommandBufferCompleted(block, cb);
}
}
}
private protected override void SubmitCommandsCore(CommandList commandList, Fence fence)
{
MTLCommandList mtlCL = Util.AssertSubtype<CommandList, MTLCommandList>(commandList);
mtlCL.CommandBuffer.addCompletedHandler(_completionBlockLiteral);
lock (_submittedCommandsLock)
{
if (fence != null)
{
mtlCL.SetCompletionFence(mtlCL.CommandBuffer, Util.AssertSubtype<Fence, MTLFence>(fence));
}
_submittedCLs.Add(mtlCL.CommandBuffer, mtlCL);
_latestSubmittedCB = mtlCL.Commit();
}
}
private protected override void WaitForNextFrameReadyCore()
{
_frameEndedEvent.Reset();
_nextFrameReadyEvent?.WaitOne(TimeSpan.FromSeconds(1)); // Should never time out.
// in iOS, if one frame takes longer than the next V-Sync request, the next frame will be processed immediately rather than being delayed to a subsequent V-Sync request,
// therefore we will request the next drawable here as a method of waiting until we're ready to draw the next frame.
if (!MetalFeatures.IsMacOS)
{
MTLSwapchainFramebuffer mtlSwapchainFramebuffer = Util.AssertSubtype<Framebuffer, MTLSwapchainFramebuffer>(_mainSwapchain.Framebuffer);
mtlSwapchainFramebuffer.EnsureDrawableAvailable();
}
}
private void OnDisplayLinkCallback()
{
_nextFrameReadyEvent.Set();
_frameEndedEvent.WaitOne();
}
public override TextureSampleCount GetSampleCountLimit(PixelFormat format, bool depthFormat)
{
for (int i = _supportedSampleCounts.Length - 1; i >= 0; i--)
{
if (_supportedSampleCounts[i])
{
return (TextureSampleCount)i;
}
}
return TextureSampleCount.Count1;
}
private protected override bool GetPixelFormatSupportCore(
PixelFormat format,
TextureType type,
TextureUsage usage,
out PixelFormatProperties properties)
{
if (!MTLFormats.IsFormatSupported(format, usage, MetalFeatures))
{
properties = default(PixelFormatProperties);
return false;
}
uint sampleCounts = 0;
for (int i = 0; i < _supportedSampleCounts.Length; i++)
{
if (_supportedSampleCounts[i])
{
sampleCounts |= (uint)(1 << i);
}
}
MTLFeatureSet maxFeatureSet = MetalFeatures.MaxFeatureSet;
uint maxArrayLayer = MTLFormats.GetMaxTextureVolume(maxFeatureSet);
uint maxWidth;
uint maxHeight;
uint maxDepth;
if (type == TextureType.Texture1D)
{
maxWidth = MTLFormats.GetMaxTexture1DWidth(maxFeatureSet);
maxHeight = 1;
maxDepth = 1;
}
else if (type == TextureType.Texture2D)
{
uint maxDimensions;
if ((usage & TextureUsage.Cubemap) != 0)
{
maxDimensions = MTLFormats.GetMaxTextureCubeDimensions(maxFeatureSet);
}
else
{
maxDimensions = MTLFormats.GetMaxTexture2DDimensions(maxFeatureSet);
}
maxWidth = maxDimensions;
maxHeight = maxDimensions;
maxDepth = 1;
}
else if (type == TextureType.Texture3D)
{
maxWidth = maxArrayLayer;
maxHeight = maxArrayLayer;
maxDepth = maxArrayLayer;
maxArrayLayer = 1;
}
else
{
throw Illegal.Value<TextureType>();
}
properties = new PixelFormatProperties(
maxWidth,
maxHeight,
maxDepth,
uint.MaxValue,
maxArrayLayer,
sampleCounts);
return true;
}
private protected override void SwapBuffersCore(Swapchain swapchain)
{
MTLSwapchain mtlSC = Util.AssertSubtype<Swapchain, MTLSwapchain>(swapchain);
IntPtr currentDrawablePtr = mtlSC.CurrentDrawable.NativePtr;
if (currentDrawablePtr != IntPtr.Zero)
{
using (NSAutoreleasePool.Begin())
{
MTLCommandBuffer submitCB = _commandQueue.commandBuffer();
submitCB.presentDrawable(currentDrawablePtr);
submitCB.commit();
}
mtlSC.InvalidateDrawable();
}
_frameEndedEvent.Set();
}
private protected override void UpdateBufferCore(DeviceBuffer buffer, uint bufferOffsetInBytes, IntPtr source, uint sizeInBytes)
{
var mtlBuffer = Util.AssertSubtype<DeviceBuffer, MTLBuffer>(buffer);
void* destPtr = mtlBuffer.Pointer;
byte* destOffsetPtr = (byte*)destPtr + bufferOffsetInBytes;
if (destPtr == null)
throw new VeldridException("Attempting to write to a MTLBuffer that is inaccessible from a CPU.");
Unsafe.CopyBlock(destOffsetPtr, source.ToPointer(), sizeInBytes);
}
private protected override void UpdateTextureCore(
Texture texture,
IntPtr source,
uint sizeInBytes,
uint x,
uint y,
uint z,
uint width,
uint height,
uint depth,
uint mipLevel,
uint arrayLayer)
{
MTLTexture mtlTex = Util.AssertSubtype<Texture, MTLTexture>(texture);
if (mtlTex.StagingBuffer.IsNull)
{
Texture stagingTex = ResourceFactory.CreateTexture(new TextureDescription(
width, height, depth, 1, 1, texture.Format, TextureUsage.Staging, texture.Type));
UpdateTexture(stagingTex, source, sizeInBytes, 0, 0, 0, width, height, depth, 0, 0);
CommandList cl = ResourceFactory.CreateCommandList();
cl.Begin();
cl.CopyTexture(
stagingTex, 0, 0, 0, 0, 0,
texture, x, y, z, mipLevel, arrayLayer,
width, height, depth, 1);
cl.End();
SubmitCommands(cl);
cl.Dispose();
stagingTex.Dispose();
}
else
{
mtlTex.GetSubresourceLayout(mipLevel, arrayLayer, out uint dstRowPitch, out uint dstDepthPitch);
ulong dstOffset = Util.ComputeSubresourceOffset(mtlTex, mipLevel, arrayLayer);
uint srcRowPitch = FormatHelpers.GetRowPitch(width, texture.Format);
uint srcDepthPitch = FormatHelpers.GetDepthPitch(srcRowPitch, height, texture.Format);
Util.CopyTextureRegion(
source.ToPointer(),
0, 0, 0,
srcRowPitch, srcDepthPitch,
(byte*)mtlTex.StagingBufferPointer + dstOffset,
x, y, z,
dstRowPitch, dstDepthPitch,
width, height, depth,
texture.Format);
}
}
private protected override void WaitForIdleCore()
{
MTLCommandBuffer lastCB = default(MTLCommandBuffer);
lock (_submittedCommandsLock)
{
lastCB = _latestSubmittedCB;
ObjectiveCRuntime.retain(lastCB.NativePtr);
}
if (lastCB.NativePtr != IntPtr.Zero && lastCB.status != MTLCommandBufferStatus.Completed)
{
lastCB.waitUntilCompleted();
}
ObjectiveCRuntime.release(lastCB.NativePtr);
}
protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource)
{
if (resource is MTLBuffer buffer)
{
return MapBuffer(buffer, mode);
}
else
{
MTLTexture texture = Util.AssertSubtype<MappableResource, MTLTexture>(resource);
return MapTexture(texture, mode, subresource);
}
}
private MappedResource MapBuffer(MTLBuffer buffer, MapMode mode)
{
return new MappedResource(
buffer,
mode,
(IntPtr)buffer.Pointer,
buffer.SizeInBytes,
0,
buffer.SizeInBytes,
buffer.SizeInBytes);
}
private MappedResource MapTexture(MTLTexture texture, MapMode mode, uint subresource)
{
Debug.Assert(!texture.StagingBuffer.IsNull);
void* data = texture.StagingBufferPointer;
Util.GetMipLevelAndArrayLayer(texture, subresource, out uint mipLevel, out uint arrayLayer);
Util.GetMipDimensions(texture, mipLevel, out uint width, out uint height, out uint depth);
uint subresourceSize = texture.GetSubresourceSize(mipLevel, arrayLayer);
texture.GetSubresourceLayout(mipLevel, arrayLayer, out uint rowPitch, out uint depthPitch);
ulong offset = Util.ComputeSubresourceOffset(texture, mipLevel, arrayLayer);
byte* offsetPtr = (byte*)data + offset;
return new MappedResource(texture, mode, (IntPtr)offsetPtr, subresourceSize, subresource, rowPitch, depthPitch);
}
protected override void PlatformDispose()
{
WaitForIdle();
if (!_unalignedBufferCopyPipeline.IsNull)
{
_unalignedBufferCopyShader.Dispose();
ObjectiveCRuntime.release(_unalignedBufferCopyPipeline.NativePtr);
}
_mainSwapchain?.Dispose();
ObjectiveCRuntime.release(_commandQueue.NativePtr);
ObjectiveCRuntime.release(_device.NativePtr);
lock (s_aotRegisteredBlocks)
{
s_aotRegisteredBlocks.Remove(_completionBlockLiteral);
}
_libSystem?.Dispose();
Marshal.FreeHGlobal(_completionBlockDescriptor);
Marshal.FreeHGlobal(_completionBlockLiteral);
_displayLink?.Dispose();
}
public override bool GetMetalInfo(out BackendInfoMetal info)
{
info = _metalInfo;
return true;
}
protected override void UnmapCore(MappableResource resource, uint subresource)
{
}
public override bool WaitForFence(Fence fence, ulong nanosecondTimeout)
{
return Util.AssertSubtype<Fence, MTLFence>(fence).Wait(nanosecondTimeout);
}
public override bool WaitForFences(Fence[] fences, bool waitAll, ulong nanosecondTimeout)
{
int msTimeout;
if (nanosecondTimeout == ulong.MaxValue)
{
msTimeout = -1;
}
else
{
msTimeout = (int)Math.Min(nanosecondTimeout / 1_000_000, int.MaxValue);
}
ManualResetEvent[] events = GetResetEventArray(fences.Length);
for (int i = 0; i < fences.Length; i++)
{
events[i] = Util.AssertSubtype<Fence, MTLFence>(fences[i]).ResetEvent;
}
bool result;
if (waitAll)
{
result = WaitHandle.WaitAll(events, msTimeout);
}
else
{
int index = WaitHandle.WaitAny(events, msTimeout);
result = index != WaitHandle.WaitTimeout;
}
ReturnResetEventArray(events);
return result;
}
private ManualResetEvent[] GetResetEventArray(int length)
{
lock (_resetEventsLock)
{
for (int i = _resetEvents.Count - 1; i > 0; i--)
{
ManualResetEvent[] array = _resetEvents[i];
if (array.Length == length)
{
_resetEvents.RemoveAt(i);
return array;
}
}
}
ManualResetEvent[] newArray = new ManualResetEvent[length];
return newArray;
}
private void ReturnResetEventArray(ManualResetEvent[] array)
{
lock (_resetEventsLock)
{
_resetEvents.Add(array);
}
}
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)
{
if (_unalignedBufferCopyPipeline.IsNull)
{
MTLComputePipelineDescriptor descriptor = MTLUtil.AllocInit<MTLComputePipelineDescriptor>(
nameof(MTLComputePipelineDescriptor));
MTLPipelineBufferDescriptor buffer0 = descriptor.buffers[0];
buffer0.mutability = MTLMutability.Mutable;
MTLPipelineBufferDescriptor buffer1 = descriptor.buffers[1];
buffer0.mutability = MTLMutability.Mutable;
Debug.Assert(_unalignedBufferCopyShader == null);
string name = MetalFeatures.IsMacOS ? UnalignedBufferCopyPipelineMacOSName : UnalignedBufferCopyPipelineiOSName;
using (Stream resourceStream = typeof(MTLGraphicsDevice).Assembly.GetManifestResourceStream(name))
{
byte[] data = new byte[resourceStream.Length];
using (MemoryStream ms = new MemoryStream(data))
{
resourceStream.CopyTo(ms);
ShaderDescription shaderDesc = new ShaderDescription(ShaderStages.Compute, data, "copy_bytes");
_unalignedBufferCopyShader = new MTLShader(ref shaderDesc, this);
}
}
descriptor.computeFunction = _unalignedBufferCopyShader.Function;
_unalignedBufferCopyPipeline = _device.newComputePipelineStateWithDescriptor(descriptor);
ObjectiveCRuntime.release(descriptor.NativePtr);
}
return _unalignedBufferCopyPipeline;
}
}
internal override uint GetUniformBufferMinOffsetAlignmentCore() => MetalFeatures.IsMacOS ? 16u : 256u;
internal override uint GetStructuredBufferMinOffsetAlignmentCore() => 16u;
}
internal sealed class MonoPInvokeCallbackAttribute : Attribute
{
public MonoPInvokeCallbackAttribute(Type t) { }
}
}