xamarin/xamarin-macios

.NET: Clear the OpenTK namespace

rolfbjarne opened this issue · 2 comments

Types in the OpenTK namespace

We want to remove types from the OpenTK namespace, and these types fall in three categories:

  1. Types we can replace with an existing .NET type
  2. Types that don't have an equivalent .NET type, so we have to come up with an alternative type.
  3. Types we don't need, so we can remove them.

One important question is which namespace to use for 2. Options:

  1. Keep using OpenTK. This is not an option, because that's exactly what we're trying to move away from.
  2. ObjCRuntime: not really very math-like, and this namespace shouldn't become a catch-all for everything that doesn't fit elsewhere either.
  3. CoreNumerics: A mix of Apple's Core* frameworks with System.Numerics.
  4. CoreMath: same as 3.
  5. CoreGraphics: existing Apple namespace. Con: might end up with a name clash in the future depending on what Apple does.
  6. Something else?

Ref: #2571
Ref: https://bugzilla.xamarin.com/show_bug.cgi?id=58599

Different type

The following types have an equivalent in .NET, so the proposal is to use that type instead:

OpenTK type .NET type
OpenTK.Matrix4 System.Numerics.Matrix4x4
OpenTK.Quaternion System.Numerics.Quaternion
OpenTK.Vector2 System.Numerics.Vector2
OpenTK.Vector2d System.Numerics.Vector<double>
OpenTK.Vector2i System.Numerics.Vector<int>
OpenTK.Vector3 System.Numerics.Vector3
OpenTK.Vector3d System.Numerics.Vector<double>
OpenTK.Vector3i System.Numerics.Vector<int>
OpenTK.Vector4 System.Numerics.Vector4
OpenTK.Vector4d System.Numerics.Vector<double>
OpenTK.Vector4i System.Numerics.Vector<int>

Different namespace

The following OpenTK types have no equivalent in .NET, so the proposal is to copy the OpenTK implementation, but use a different namespace.

  • OpenTK.Matrix2
  • OpenTK.Matrix3
  • OpenTK.Matrix4d
  • OpenTK.Quaterniond

The following are types that we created ourselves, but put in the OpenTK namespace.

  • OpenTK.NMatrix4d
  • OpenTK.NMatrix2
  • OpenTK.NMatrix3
  • OpenTK.NMatrix4x3
  • OpenTK.NMatrix4
  • OpenTK.NVector3d
  • OpenTK.NVector3

Types to remove

The following types don't seem to be used by any other API, so they can be removed

  • OpenTK.Box2
  • OpenTK.Functions
  • OpenTK.Half
  • OpenTK.Vector2h
  • OpenTK.Vector3h
  • OpenTK.Vector4h
  • OpenTK.BezierCurve
  • OpenTK.BezierCurveCubic
  • OpenTK.BezierCurveQuadric

Don't know yet

  • ❓OpenTK.MathHelper

Ref: #13087

For OpenTK.Matrix2, OpenTK.Matrix3 and OpenTK.Matrix4d I wonder if you could use ValueTuple<Vector3, Vector3, Vector3> etc. instead.

For OpenTK.Matrix2, OpenTK.Matrix3 and OpenTK.Matrix4d I wonder if you could use ValueTuple<Vector3, Vector3, Vector3> etc. instead.

Sadly not, value tuples have auto layout rather than sequential: not suitable for interop...


System.Numerics.Vector

Note: Vector isn't quite akin to Vector2, namely it's variably sized and actually might hold more than 2 Ts in some cases. Because of this, the memory layout probably isn't suitable for your needs (interop, presumably?)

There's a proposal for generic "dimensional" vectors (dotnet/runtime#24168), but these have been postponed to .NET 7. The best solution is probably to whip up your own vectors in a Xamarin namespace. When .NET 7 generic vectors become available, either implicit casts can be added or throw those types out if you still feel in the mood to break things between major releases.


For your reference:

We've been down this road and made our own maths library which fills these holes for us (i.e. MathHelper, called Scalar in our library; and generic vector types like Vector2D). It's possibly a bit overkill for what Xamarin needs, but figured I'd drop it here for your reference (please change the namespace if you use anything): https://github.com/dotnet/Silk.NET/tree/main/src/Maths/Silk.NET.Maths


One more thing to add is please don't call anything VectorN or MatrixNxN regardless of namespace as these will conflict with System.Numerics if ever they get those types as well if users are using both namespaces - this is why the above link uses VectorND for its vectors and a capital X for its matrices.