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

[MetalPerforanceShaders] Implement Xcode 16.0 beta 1-6 changes. #21155

Merged
merged 9 commits into from
Sep 12, 2024
130 changes: 130 additions & 0 deletions src/MetalPerformanceShaders/MPSDefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,134 @@ public struct MPSMatrixOffset {
public uint RowOffset;
public uint ColumnOffset;
}

[StructLayout (LayoutKind.Sequential)]
public struct MPSNDArrayOffsets {
// NSInteger dimensions[16];
nint dimension0;
nint dimension1;
nint dimension2;
nint dimension3;
nint dimension4;
nint dimension5;
nint dimension6;
nint dimension7;
nint dimension8;
nint dimension9;
nint dimension10;
nint dimension11;
nint dimension12;
nint dimension13;
nint dimension14;
nint dimension15;

public nint [] Dimensions {
get => new nint [] {
dimension0,
dimension1,
dimension2,
dimension3,
dimension4,
dimension5,
dimension6,
dimension7,
dimension8,
dimension9,
dimension10,
dimension11,
dimension12,
dimension13,
dimension14,
dimension15,
};
set {
if (value is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (value));
if (value.Length != 16)
ObjCRuntime.ThrowHelper.ThrowArgumentOutOfRangeException (nameof (value), "Length must be 16.");

dimension0 = value [0];
dimension1 = value [1];
dimension2 = value [2];
dimension3 = value [3];
dimension4 = value [4];
dimension5 = value [5];
dimension6 = value [6];
dimension7 = value [7];
dimension8 = value [8];
dimension9 = value [9];
dimension10 = value [10];
dimension11 = value [11];
dimension12 = value [12];
dimension13 = value [13];
dimension14 = value [14];
dimension15 = value [15];
}
}
}

[StructLayout (LayoutKind.Sequential)]
public struct MPSNDArraySizes {
// NSUInteger dimensions[16];
nuint dimension0;
nuint dimension1;
nuint dimension2;
nuint dimension3;
nuint dimension4;
nuint dimension5;
nuint dimension6;
nuint dimension7;
nuint dimension8;
nuint dimension9;
nuint dimension10;
nuint dimension11;
nuint dimension12;
nuint dimension13;
nuint dimension14;
nuint dimension15;

public nuint [] Dimensions {
get => new nuint [] {
dimension0,
dimension1,
dimension2,
dimension3,
dimension4,
dimension5,
dimension6,
dimension7,
dimension8,
dimension9,
dimension10,
dimension11,
dimension12,
dimension13,
dimension14,
dimension15,
};
set {
if (value is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (value));
if (value.Length != 16)
ObjCRuntime.ThrowHelper.ThrowArgumentOutOfRangeException (nameof (value), "Length must be 16.");

dimension0 = value [0];
dimension1 = value [1];
dimension2 = value [2];
dimension3 = value [3];
dimension4 = value [4];
dimension5 = value [5];
dimension6 = value [6];
dimension7 = value [7];
dimension8 = value [8];
dimension9 = value [9];
dimension10 = value [10];
dimension11 = value [11];
dimension12 = value [12];
dimension13 = value [13];
dimension14 = value [14];
dimension15 = value [15];
}
}
}
}
4 changes: 4 additions & 0 deletions src/MetalPerformanceShaders/MPSEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public enum MPSDataType : uint { // uint32_t
Float32 = FloatBit | 32,

SignedBit = 0x20000000,
[TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
Int4 = SignedBit | 4,
Int8 = SignedBit | 8,
Int16 = SignedBit | 16,
Int32 = SignedBit | 32,
Expand All @@ -72,6 +74,8 @@ public enum MPSDataType : uint { // uint32_t
[MacCatalyst (14, 1)]
Int64 = SignedBit | 64,

[TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
UInt4 = 4,
UInt8 = 8,
UInt16 = 16,
UInt32 = 32,
Expand Down
35 changes: 35 additions & 0 deletions src/MetalPerformanceShaders/MPSNDArray.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#nullable enable

using System;

using Metal;
using Foundation;
using ObjCRuntime;

namespace MetalPerformanceShaders {
public partial class MPSNDArray {
Expand Down Expand Up @@ -92,5 +94,38 @@ public unsafe void Read (Span<float> values)
ReadBytes ((IntPtr) p, strideBytesPerDimension: IntPtr.Zero);
}
}

#if NET
[SupportedOSPlatform ("ios18.0")]
[SupportedOSPlatform ("maccatalyst18.0")]
[SupportedOSPlatform ("macos15.0")]
[SupportedOSPlatform ("tvos18.0")]
#else
[TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
#endif
public MPSNDArray? Create (nuint numberOfDimensions, nuint [] dimensionSizes, nuint [] dimStrides)
{
if (dimensionSizes is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (dimensionSizes));

if (dimStrides is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (dimStrides));

if (dimensionSizes.Length != (int) numberOfDimensions)
ObjCRuntime.ThrowHelper.ThrowArgumentOutOfRangeException (nameof (dimensionSizes), $"Length must be equal to 'numberOfDimensions'.");

if (dimStrides.Length != (int) numberOfDimensions)
ObjCRuntime.ThrowHelper.ThrowArgumentOutOfRangeException (nameof (dimStrides), $"Length must be equal to 'numberOfDimensions'.");

MPSNDArray? rv;
unsafe {
fixed (nuint* dimensionSizesPtr = dimensionSizes) {
fixed (nuint* dimStridesPtr = dimStrides) {
rv = _Create (numberOfDimensions, (IntPtr) dimensionSizesPtr, (IntPtr) dimStridesPtr);
}
}
}
return rv;
}
}
}
35 changes: 35 additions & 0 deletions src/MetalPerformanceShaders/MPSNDArrayDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#nullable enable

using System;

using Metal;
using Foundation;
using ObjCRuntime;

namespace MetalPerformanceShaders {
public partial class MPSNDArrayDescriptor {

#if NET
[SupportedOSPlatform ("ios18.0")]
[SupportedOSPlatform ("maccatalyst18.0")]
[SupportedOSPlatform ("macos15.0")]
[SupportedOSPlatform ("tvos18.0")]
#else
[TV (18, 0), Mac (15, 0), iOS (18, 0), MacCatalyst (18, 0)]
#endif
public void PermuteWithDimensionOrder (nuint [] dimensionOrder)
{
if (dimensionOrder is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (dimensionOrder));

if (dimensionOrder.Length != (int) NumberOfDimensions)
ObjCRuntime.ThrowHelper.ThrowArgumentOutOfRangeException (nameof (dimensionOrder), $"Length must be equal to 'NumberOfDimensions'.");

unsafe {
fixed (nuint* ptr = dimensionOrder) {
_PermuteWithDimensionOrder ((IntPtr) ptr);
}
}
}
}
}
32 changes: 32 additions & 0 deletions src/MetalPerformanceShaders/MPSNDArrayIdentity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#nullable enable

using System;
using Metal;
using Foundation;
using ObjCRuntime;

namespace MetalPerformanceShaders {
public partial class MPSNDArrayIdentity {
public MPSNDArray? Reshape (IMTLCommandBuffer? commandBuffer, MPSNDArray sourceArray, nuint [] dimensionSizes, MPSNDArray? destinationArray)
{
MPSNDArray? rv;
unsafe {
fixed (nuint* dimensionsPtr = dimensionSizes) {
rv = _Reshape (commandBuffer, sourceArray, (nuint) dimensionSizes.Length, (IntPtr) dimensionsPtr, destinationArray);
}
}
return rv;
}

public MPSNDArray? Reshape (IMTLComputeCommandEncoder? encoder, IMTLCommandBuffer? commandBuffer, MPSNDArray sourceArray, nuint [] dimensionSizes, MPSNDArray? destinationArray)
{
MPSNDArray? rv;
unsafe {
fixed (nuint* dimensionsPtr = dimensionSizes) {
rv = _Reshape (encoder, commandBuffer, sourceArray, (nuint) dimensionSizes.Length, (IntPtr) dimensionsPtr, destinationArray);
}
}
return rv;
}
}
}
2 changes: 2 additions & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,8 @@ METALPERFORMANCESHADERS_SOURCES = \
MetalPerformanceShaders/MPSCnnKernel.cs \
MetalPerformanceShaders/MPSMatrixDescriptor.cs \
MetalPerformanceShaders/MPSNDArray.cs \
MetalPerformanceShaders/MPSNDArrayDescriptor.cs \
MetalPerformanceShaders/MPSNDArrayIdentity.cs \
MetalPerformanceShaders/MPSNNGraph.cs \
MetalPerformanceShaders/MPSImageHistogram.cs \
MetalPerformanceShaders/MPSStateBatch.cs \
Expand Down
Loading
Loading