Skip to content

Commit

Permalink
Merge pull request #1 from frenzibyte/fix-metal-ios
Browse files Browse the repository at this point in the history
Work around mono-level crash on iOS with interpreter
  • Loading branch information
peppy authored Mar 23, 2023
2 parents 0c2e8e2 + 75f6690 commit b6bb757
Show file tree
Hide file tree
Showing 10 changed files with 640 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Veldrid.MetalBindings/MTLBlitCommandEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
using static Veldrid.MetalBindings.ObjectiveCRuntime;

namespace Veldrid.MetalBindings
Expand Down Expand Up @@ -29,9 +30,9 @@ public void copyFromBuffer(
UIntPtr destinationSlice,
UIntPtr destinationLevel,
MTLOrigin destinationOrigin)
=> objc_msgSend(
{
copyFromBuffer(
NativePtr,
sel_copyFromBuffer1,
sourceBuffer.NativePtr,
sourceOffset,
sourceBytesPerRow,
Expand All @@ -40,7 +41,25 @@ public void copyFromBuffer(
destinationTexture.NativePtr,
destinationSlice,
destinationLevel,
destinationOrigin);
destinationOrigin.x,
destinationOrigin.y,
destinationOrigin.z);
}

[DllImport("@rpath/metal_mono_workaround.framework/metal_mono_workaround")]
private static extern void copyFromBuffer(
IntPtr encoder,
IntPtr sourceBuffer,
UIntPtr sourceOffset,
UIntPtr sourceBytesPerRow,
UIntPtr sourceBytesPerImage,
MTLSize sourceSize,
IntPtr destinationTexture,
UIntPtr destinationSlice,
UIntPtr destinationLevel,
UIntPtr destinationOriginX,
UIntPtr destinationOriginY,
UIntPtr destinationOriginZ);

public void copyTextureToBuffer(
MTLTexture sourceTexture,
Expand Down Expand Up @@ -106,4 +125,4 @@ public void copyFromTexture(
private static readonly Selector sel_synchronizeResource = "synchronizeResource:";
private static readonly Selector sel_endEncoding = "endEncoding";
}
}
}
12 changes: 12 additions & 0 deletions src/Veldrid.MetalBindings/metal-mono-workaround/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Metal Mono Workaround

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.

This library should be referenced by any project that uses Veldrid.

To generate the library, build the Xcode project on both iPhone and iPhone Simulator architectures, and execute the following to generate the XCFramework:
```bash
xcodebuild -create-xcframework -framework ./Release-iphoneos/metal_mono_workaround.framework -framework ./Release-iphonesimulator/metal_mono_workaround.framework -output metal-mono-workaround.xcframework
```

Afterwards, reference the XCFramework in your project and it should work correctly.
Loading

0 comments on commit b6bb757

Please sign in to comment.