Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around mono-level crash on iOS with interpreter #1

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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