Skip to content

Commit b6bb757

Browse files
authored
Merge pull request #1 from frenzibyte/fix-metal-ios
Work around mono-level crash on iOS with interpreter
2 parents 0c2e8e2 + 75f6690 commit b6bb757

File tree

10 files changed

+640
-4
lines changed

10 files changed

+640
-4
lines changed

src/Veldrid.MetalBindings/MTLBlitCommandEncoder.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using static Veldrid.MetalBindings.ObjectiveCRuntime;
34

45
namespace Veldrid.MetalBindings
@@ -29,9 +30,9 @@ public void copyFromBuffer(
2930
UIntPtr destinationSlice,
3031
UIntPtr destinationLevel,
3132
MTLOrigin destinationOrigin)
32-
=> objc_msgSend(
33+
{
34+
copyFromBuffer(
3335
NativePtr,
34-
sel_copyFromBuffer1,
3536
sourceBuffer.NativePtr,
3637
sourceOffset,
3738
sourceBytesPerRow,
@@ -40,7 +41,25 @@ public void copyFromBuffer(
4041
destinationTexture.NativePtr,
4142
destinationSlice,
4243
destinationLevel,
43-
destinationOrigin);
44+
destinationOrigin.x,
45+
destinationOrigin.y,
46+
destinationOrigin.z);
47+
}
48+
49+
[DllImport("@rpath/metal_mono_workaround.framework/metal_mono_workaround")]
50+
private static extern void copyFromBuffer(
51+
IntPtr encoder,
52+
IntPtr sourceBuffer,
53+
UIntPtr sourceOffset,
54+
UIntPtr sourceBytesPerRow,
55+
UIntPtr sourceBytesPerImage,
56+
MTLSize sourceSize,
57+
IntPtr destinationTexture,
58+
UIntPtr destinationSlice,
59+
UIntPtr destinationLevel,
60+
UIntPtr destinationOriginX,
61+
UIntPtr destinationOriginY,
62+
UIntPtr destinationOriginZ);
4463

4564
public void copyTextureToBuffer(
4665
MTLTexture sourceTexture,
@@ -106,4 +125,4 @@ public void copyFromTexture(
106125
private static readonly Selector sel_synchronizeResource = "synchronizeResource:";
107126
private static readonly Selector sel_endEncoding = "endEncoding";
108127
}
109-
}
128+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Metal Mono Workaround
2+
3+
On iOS, simply defining a P/Invoke method for `MTLBlitCommandEncoder.copyFromBuffer` and invoking it blows up the application. To work around that, we're defining a method that has a different signature that's friendly with Mono, which redirects back to the original method.
4+
5+
This library should be referenced by any project that uses Veldrid.
6+
7+
To generate the library, build the Xcode project on both iPhone and iPhone Simulator architectures, and execute the following to generate the XCFramework:
8+
```bash
9+
xcodebuild -create-xcframework -framework ./Release-iphoneos/metal_mono_workaround.framework -framework ./Release-iphonesimulator/metal_mono_workaround.framework -output metal-mono-workaround.xcframework
10+
```
11+
12+
Afterwards, reference the XCFramework in your project and it should work correctly.

0 commit comments

Comments
 (0)