Skip to content

Commit

Permalink
Work around copyFromTexture not working on iOS with Mono interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Apr 16, 2023
1 parent 2bd55e7 commit f1f8dc0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
59 changes: 48 additions & 11 deletions src/Veldrid.MetalBindings/MTLBlitCommandEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,54 @@ public void copyFromTexture(
MTLTexture destinationTexture,
UIntPtr destinationSlice,
UIntPtr destinationLevel,
MTLOrigin destinationOrigin)
=> objc_msgSend(NativePtr, sel_copyFromTexture1,
sourceTexture,
sourceSlice,
sourceLevel,
sourceOrigin,
sourceSize,
destinationTexture,
destinationSlice,
destinationLevel,
destinationOrigin);
MTLOrigin destinationOrigin,
bool isMacOS)
{
if (!isMacOS)
{
copyFromTexture_iOS(
NativePtr,
sourceTexture.NativePtr,
sourceSlice,
sourceLevel,
sourceOrigin,
sourceSize,
destinationTexture.NativePtr,
destinationSlice,
destinationLevel,
destinationOrigin.x,
destinationOrigin.y,
destinationOrigin.z);
}
else
{
objc_msgSend(NativePtr, sel_copyFromTexture1,
sourceTexture,
sourceSlice,
sourceLevel,
sourceOrigin,
sourceSize,
destinationTexture,
destinationSlice,
destinationLevel,
destinationOrigin);
}
}

[DllImport("@rpath/metal_mono_workaround.framework/metal_mono_workaround", EntryPoint = "copyFromTexture")]
private static extern void copyFromTexture_iOS(
IntPtr encoder,
IntPtr sourceTexture,
UIntPtr sourceSlice,
UIntPtr sourceLevel,
MTLOrigin sourceOrigin,
MTLSize sourceSize,
IntPtr destinationTexture,
UIntPtr destinationSlice,
UIntPtr destinationLevel,
UIntPtr destinationOriginX,
UIntPtr destinationOriginY,
UIntPtr destinationOriginZ);

private static readonly Selector sel_copyFromBuffer0 = "copyFromBuffer:sourceOffset:toBuffer:destinationOffset:size:";
private static readonly Selector sel_copyFromBuffer1 = "copyFromBuffer:sourceOffset:sourceBytesPerRow:sourceBytesPerImage:sourceSize:toTexture:destinationSlice:destinationLevel:destinationOrigin:";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ void copyFromBuffer(id<MTLBlitCommandEncoder> encoder,
NSUInteger destinationOriginX,
NSUInteger destinationOriginY,
NSUInteger destinationOriginZ);

void copyFromTexture(id<MTLBlitCommandEncoder> encoder,
id<MTLTexture> sourceTexture,
NSUInteger sourceSlice,
NSUInteger sourceLevel,
MTLOrigin sourceOrigin,
MTLSize sourceSize,
id<MTLTexture> destinationTexture,
NSUInteger destinationSlice,
NSUInteger destinationLevel,
NSUInteger destinationOriginX,
NSUInteger destinationOriginY,
NSUInteger destinationOriginZ);
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,27 @@ void copyFromBuffer(id<MTLBlitCommandEncoder> encoder,
destinationLevel:destinationLevel
destinationOrigin:MTLOriginMake(destinationOriginX, destinationOriginY, destinationOriginZ)];
}

void copyFromTexture(id<MTLBlitCommandEncoder> encoder,
id<MTLTexture> sourceTexture,
NSUInteger sourceSlice,
NSUInteger sourceLevel,
MTLOrigin sourceOrigin,
MTLSize sourceSize,
id<MTLTexture> destinationTexture,
NSUInteger destinationSlice,
NSUInteger destinationLevel,
NSUInteger destinationOriginX,
NSUInteger destinationOriginY,
NSUInteger destinationOriginZ)
{
[encoder copyFromTexture:sourceTexture
sourceSlice:sourceSlice
sourceLevel:sourceLevel
sourceOrigin:sourceOrigin
sourceSize:sourceSize
toTexture:destinationTexture
destinationSlice:destinationSlice
destinationLevel:destinationLevel
destinationOrigin:MTLOriginMake(destinationOriginX, destinationOriginY, destinationOriginZ)];
}
3 changes: 2 additions & 1 deletion src/Veldrid/MTL/MTLCommandList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ protected override void CopyTextureCore(
dstMTLTexture.DeviceTexture,
(UIntPtr)(dstBaseArrayLayer + layer),
(UIntPtr)dstMipLevel,
new MTLOrigin(dstX, dstY, dstZ));
new MTLOrigin(dstX, dstY, dstZ),
_gd.MetalFeatures.IsMacOS);
}
}
}
Expand Down

0 comments on commit f1f8dc0

Please sign in to comment.