There is a SCNNode
category named SCNNode(SIMD)
, which declares some properties like simdPosition
, simdRotation
and so on. It seems these are duplicated properties of the original/normal properties position
and rotation
.
@property(nonatomic) simd_float3 simdPosition API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0));
@property(nonatomic) simd_float4 simdRotation API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0));
What's the difference between position
and simdPosition
? What does the prefix "simd" mean exactly?
SIMD is a small library built on top of vector types that you can import from
<simd/simd.h>
. It allows for more expressive and more performant code.For instance using SIMD you can write
instead of
In Objective-C you can not overload methods. That is you can not have both
The new SIMD-based API needed a different name, and that's why SceneKit exposes
simdPosition
.SIMD: Single Instruction Multiple Data
SIMD instructions allow you to perform the same operation on multiple values at the same time.
Let's see an example
Serial Approach (NO SIMD)
We have these 4 Int32 values
Now we want to sum the 2
x
and the 2y
values, so we writeIn order to perform the 2 previous
sums
the CPU needs toSo the result is obtained with 2 operations.
I created some graphics to better show you the idea
SIMD
Let's see now how SIMD does work. First of all we need the input values stored in the proper SIMD format so
As you can see the previous
x
andy
are vectors. Infact bothx
andy
contain 2 components.Now we can write
Let's see what the CPU does in order to perform the previous operations
That's it!
Both components of
x
and both components ofy
are processed at the same time.Parallel Programming
We are NOT talking about concurrent programming, instead this is real parallel programming.
As you can imagine in certain operation the SIMD approach is way faster then the serial one.
Scene Kit
Let's see now an example in SceneKit
We want to add
10
to thex
,y
andz
components of all the direct descendants of the scene node.Using the classic serial approach we can write
Let's see now how we can convert the previous code in SIMD instructions
This code is much faster then the previous one. I am not sure whether 2x or 3x faster but, believe me, it's way better.
Wrap up
If you need to perform several times the same operation on different value, just use the SIMD properties :)