[swift-users] Design and performance of Vector2/3/4 and Matrix

Janosch Hildebrand jnosh at jnosh.com
Fri Dec 18 18:35:28 CST 2015

> On 19 Dec 2015, at 00:12, David Turnbull <dturnbull at gmail.com <mailto:dturnbull at gmail.com>> wrote:
> On Fri, Dec 18, 2015 at 2:31 PM, Janosch Hildebrand via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> You will also want to have this code in the same module that is using this type.
> If you're using these types from another module you're limited to unspecialized generics which are (unsurprisingly) very slow.
> This is becoming clear. Hopefully these patterns can be optimized across modules eventually. 

Agreed. I think we'll eventually want some way to "inline" dependencies with the package manager to treat them as one target for optimization purposes. The standard library is fancier even and can essentially specializes and inlines most of it's code into yours when you use it but I don't know if we'll get those abilities for our own modules at some point.

For now you can use @transparent (or @_transparent for Swift 2.2) to force-inline functions across module boundaries but that's it as far as I know.

> It's easy enough to write a pre-processor that expands the generics into four specializations. But it doesn't solve everything so not a priority.
> And since you probably need to drop generics anyway it might make sense to simply wrap the respective GLKit types on OS X and iOS for the GLFloat variants as they are already highly optimized. I have some wrappers for the GLKMatrix and GLKVector types lying around in my own OpenGL wrapper (incidentally also named SwiftGL ;-)) which might save you some typing if you're interested...
> I'm trailblazing cross-platform OpenGL in Swift. Given there's only one other platform, the key question is, "Does it work on Linux?"

If you only care about having a simple cross-platform library, doing a simple implementation by yourself is fine. Also Swift (well, LLVM) is also pretty good at auto-vectorization so you get decent results for custom vector/matrix types.

However OpenGL-related code is usually pretty performance sensitive and the math parts doubly so, so I'd recommend wrapping some appropriate libraries instead of writing your own...

Now if you find a nice cross-platform C library you could wrap that, C++ (for example http://glm.g-truc.net/0.9.7/api/index.html <http://glm.g-truc.net/0.9.7/api/index.html>) also works but then you'd also have to write a C wrapper first :-(

Now on the Apple side, the Accelerate, GLKit and simd modules contain vector and matrix types/functions but these are OS X and iOS only...
Using a single cross platform library is obviously simpler but using these on the Apple platforms would remove the need for a separate dependency there and they are likely well optimized for the target architectures.

> Is your SwiftGL online somewhere? A cursory search didn't yield anything. 

No. I mostly just wrapped the OpenGL symbols I needed and experimented with some other additions but as a whole it wasn't really in a state to be published. I don't know if it'd be much use to you but if you're interested l I can give you the code in whatever form is most convenient for you :-)

> -david (https://github.com/AE9RB/SwiftGL <https://github.com/AE9RB/SwiftGL>)

- Janosch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151219/6aa98989/attachment.html>

More information about the swift-users mailing list