[SIMD] Implement Simd double types (VectorDouble3/MatrixDouble4x4). - #2632
Conversation
…uble4x4). The following types will be used by ModelIO bindings
rolfbjarne
left a comment
There was a problem hiding this comment.
Just one minor thing, otherwise it would have been perfect 😄
| Assert.AreEqual (expected, actual, message + " (M)"); | ||
| } | ||
|
|
||
| public static void AreEqual (double expected, double actual, float delta, string message) |
There was a problem hiding this comment.
I think delta should be double too (and there are more cases below too).
migueldeicaza
left a comment
There was a problem hiding this comment.
Why are we introducing new vector/matrix data types when we already have perfectly suitable data types for for Vecto2d, Vector3d, Vector4d and Matrix4x4d in OpenTK?
The OpenTK data types are already baked into Xamarin.iOS.dll, and we already consume them in many places in our Xamarin.iOS API.
And even if we had a good case (say, the internal layout is incompatible), then in that case, we should copy the code from OpenTK which has many existing capabilities and comes with inline API documentation.
|
Build success |
|
@migueldeicaza The issue is better explained here #2571 this is just an extension from PR #2622 |
|
Build success |
|
@migueldeicaza For the matrix types, the internal layout is incompatible, so we don't have much choice (technically we can convert matrices when going from native to managed (and vice versa), but this will complicate a lot of code, it won't be ready for iOS 11, and it will be slow). Regarding copying the OpenTK types, I looked at the ones we have, and found that:
So I decided to implement the matrices with only a few simple operations, but correctly and well-tested. We can add more operations later, and in any case it's a lossless (explicit) conversion to and from the OpenTK matrices, so the operations there can still be used. At the time it didn't occur to me to look to the upstream OpenTK types (https://github.com/opentk/opentk/tree/develop/src/OpenTK/Math), and it looks like some of my concerns are addressed (the API is consistent (Row0..RowX), there's less obsolete stuff (but still some)), but the rest of the points still stand. If we still want to copy the OpenTK types, a minimum effort would be:
Regarding the vector types: the OpenTK.Vector3 does not have the size it should have (it's 12 bytes, when it should be 16 bytes due to padding). While this is also technically fixable when we go from managed to native (and vice versa), it's not at all trivial to get it right (most of the time we get it wrong the first time), and it's also slow (if an API takes an array of Vector3, we can't just pass a reference to the managed array, we have to copy every single vector element into a new array, converting them, and pass that to native code - and now I just looked at Apple's API to see if any of it takes a |
|
Excellent, so we have to go with the secondary part of the proposal, which is given that the layout is different, we should adjust the layout. If we happen to fix bugs in the process we should fix those clearly. We can certainly try the approach of just using what we have and later fill the gaps for the case of Matrix (given the risk of swapping the fields), I would like us to avoid having two gratuitously different APIs nor resort to "Cast to the OpenTK and operate there and convert back" - because that is 128 byte copies per Matrix4d. I think we can wait on that part. That said, I do not think the risk aversion applies to the Vector types. Things that are gratuitously different from OpenTK (and previous efforts that have copied those, SceneKit and Urho):
|
|
@migueldeicaza I'm not sure what is the secondary part of the proposal but the main issue with we should adjust the layout was that OpenTK structures are There's also an (alignment) issue with Vector3, but afaik the other vector types are fine. We can special case only this one - that implies more mixing of OpenTK and new Simd types. The names can be changed, I think the major issue was avoiding identically named types that would confuse the compiler between different namespaces (a |
|
The matrices in the System.Numerics.Vectors namespaces are 1-based: https://msdn.microsoft.com/en-us/library/system.numerics.matrix4x4(v=vs.111).aspx#Anchor_5 But either way works for me, this is easy to change. Regarding the naming, I avoided using the same name as OpenTK on purpose, because it becomes annoying to have two types with the same name from different namespaces when writing code (and I felt it made most sense to choose something close to how Apple named the types). |
|
In fact some of the OpenTK's types are 1-based too (but inconsistently so), which is where I got it from:
Whatever we choose we should at least be consistent... |
|
For the type names I think we can do a prefix, e.g. |
|
@spouliot The suggestion was to take the existing code and replace the names and adjust the types to match what we need (for layout and size), so the @rolfbjarne I agree that it is undesirable to have the same type names, because you need to resolve them with the namespace. What we could do is prefix them (like we did for SceneKit - It is a shame that the public constructors in OpenTK do not match the property names in the matrix type themselves, we should correct that. But what I do not want to have is a scenario where our public properties are different, and we already have both the original OpenTK and the forked versions for SceneKit that are at least the same, we should not have one that is gratuitously different. I would take what we have, because that is what we are consistent with. Sadly, the upstream OpenTK was sort of a fork based on ours where they threw away backwards compatibility away. I rather be consistent with our existing data types, than with OpenTK's updated one (scenario: cast this new Matrix4d to our OpenTK Matrix4d to perform operations, but the value of M11 is different depending on which one you use). I take it from the comments that we agree that the Vectors types should just then be the adjusted/ported versions of OpenTk at this point. |
|
@spouliot / @migueldeicaza: OK, I think I have a plan now:
If we want to prefix the new types with In all cases I'll:
Other notes:
Please confirm if this is OK. |
|
@rolfbjarne sounds good to me |
|
I am ok with this plan, but I am also OK with the lower-impact plan which was to keep the code that is already part of this pull request, but: (a) Adjust the public properties to match OpenTK ones (b) Adjust the names to follow the convention. The only issue with using "Simd" in the prefix is that these types are not SIMD accelerated at all from C#, perhaps we can use the MIO prefix, if this is just for ModelIO interop? |
|
Apple use those types in ARKit, AVFoundation, ModelIO... they are not specific to a framework |
|
OK, keeping the existing types as I implemented them, and just changing the type name and field/property names is much simpler. I'll do that instead. Regarding the name: I've used the |
|
We could prefix them |
|
We have agreed to go with |
|
I've completed the requested changes for this PR locally, and I'll push them once PR #2668 is merged. |
This also means removing the new Vector2 and Vector4 variants (but not Vector3).
|
@migueldeicaza / @spouliot: This PR has now been updated with the new NMatrix/NVector names. @dalexsoto: Please update PR #2651 accordingly. |
|
Build failure |
|
Build success |
The following types will be used by ModelIO bindings, this is based on Rolf's commit :)