# An OpenGL math library in pure Swift

I've been working on a math library for SwiftGL. It's looking good.
Vector2, Vector3, Vector4, Matrix2x2, Matrix3x3, Matrix4x4 are implemented
with all arithmetic. You can even swizzle just like GLSL.

var myVec = vec4(1, 2, 3, 4)

myVec.ab = vec2(99, 98)

print(myVec) //=> (1, 2, 98, 99)

There's still a lot to do. I hope to have feature parity with GLSL done
this week. But it's ready to set free and get feedback.

And a couple questions. Is there any way to make import SwiftGL.Math work?
Note the dot. Also, is there anything reasonable I can do to improve the
compile time? 3.5 minutes for 2000 lines of code can't be right.

-David "Expression was too complex to be solved" Turnbull

I've been working on a math library for SwiftGL. It's looking good. Vector2, Vector3, Vector4, Matrix2x2, Matrix3x3, Matrix4x4 are implemented with all arithmetic. You can even swizzle just like GLSL.

var myVec = vec4(1, 2, 3, 4)
myVec.ab = vec2(99, 98)
print(myVec) //=> (1, 2, 98, 99)

This is pretty cool!

There's still a lot to do. I hope to have feature parity with GLSL done this week. But it's ready to set free and get feedback.

What all is mean by “feature parity” with GLSL, given that GLSL is for authoring shaders intended to be ran on device (texture units, interpolation, fuzzy floating point math semantics, etc) and your library presumably is running on host? Do you plan on synthesizing shaders (warning: this may be a little bit tricker than it seems)?

···

On Jan 4, 2016, at 7:11 PM, David Turnbull via swift-users <swift-users@swift.org> wrote:

GitHub - AE9RB/SwiftGL: This project has moved.

And a couple questions. Is there any way to make import SwiftGL.Math work? Note the dot. Also, is there anything reasonable I can do to improve the compile time? 3.5 minutes for 2000 lines of code can't be right.

-David "Expression was too complex to be solved" Turnbull

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Hey David —

FYI essentially all of this stuff is already present in the simd module (stdlib/public/SDK/simd/simd.swift.gyb or ‘import simd'), albeit without nice generics, and with some different stylistic choices because simd is at present a straight Swift port of a subset of the simd C/Obj-C/C++ library on OS X and iOS (the most important distinction is that the vector types are compatible with clang extended vectors in C languages).

Making the simd module more “swifty” is something that will need to happen eventually, and there’s a lot of other opportunities for other improvements, but you should be aware that it’s available.

– Steve

···

On Jan 4, 2016, at 10:11 PM, David Turnbull via swift-users <swift-users@swift.org> wrote:

I've been working on a math library for SwiftGL. It's looking good. Vector2, Vector3, Vector4, Matrix2x2, Matrix3x3, Matrix4x4 are implemented with all arithmetic. You can even swizzle just like GLSL.

var myVec = vec4(1, 2, 3, 4)
myVec.ab = vec2(99, 98)
print(myVec) //=> (1, 2, 98, 99)

There's still a lot to do. I hope to have feature parity with GLSL done this week. But it's ready to set free and get feedback.

GitHub - AE9RB/SwiftGL: This project has moved.

And a couple questions. Is there any way to make import SwiftGL.Math work? Note the dot. Also, is there anything reasonable I can do to improve the compile time? 3.5 minutes for 2000 lines of code can't be right.

-David "Expression was too complex to be solved" Turnbull

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

I've been working on a math library for SwiftGL. It's looking good. Vector2, Vector3, Vector4, Matrix2x2, Matrix3x3, Matrix4x4 are implemented with all arithmetic. You can even swizzle just like GLSL.

var myVec = vec4(1, 2, 3, 4)
myVec.ab = vec2(99, 98)
print(myVec) //=> (1, 2, 98, 99)

There's still a lot to do. I hope to have feature parity with GLSL done this week. But it's ready to set free and get feedback.

GitHub - AE9RB/SwiftGL: This project has moved.

And a couple questions. Is there any way to make import SwiftGL.Math work? Note the dot. Also, is there anything reasonable I can do to improve the compile time? 3.5 minutes for 2000 lines of code can't be right.

To find out which parts of your code are causing issues add `-Xfrontend -debug-time-function-bodies` in "Other Swift Flags".
When building Xcode will print the compile times of functions in the build log.

The main culprits seem to be the `*`, `inverse` and `determinant` functions of `Matrix3x3` and `Matrix4x4` where type inference probably runs amok. Breaking these calculation up into smaller statements should drastically reduce the compile times.

-David "Expression was too complex to be solved" Turnbull

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

- Janosch

···

On 05 Jan 2016, at 04:11, David Turnbull via swift-users <swift-users@swift.org> wrote:

Yeah, but...

1> import simd
repl.swift:1:8: error: no such module 'simd'

SwiftGLmath works today for everyone. It's not as fast as packed SIMD but
it's still really fast. As fast as C.

Finishing features is my priority right now. But perhaps someone else wants
to "ifdef" the simd module for Apple platforms.

-david

···

On Tue, Jan 5, 2016 at 8:59 AM, Stephen Canon wrote:

FYI essentially all of this stuff is already present in the simd module

The functions in GLSL like normalize(), cross(), dot(), distance(). The
idea is that you shouldn't have to remember two APIs to be productive. Like
what glm does for C++.

-david

···

On Tue, Jan 5, 2016 at 8:27 AM, Michael Ilseman <milseman@apple.com> wrote:

What all is mean by “feature parity” with GLSL

Any plans to make simd open-source? or available for Linux?

Best Regards,
Volodymyr Boichentsov

···

On 5 Jan 2016, at 16:59, Stephen Canon via swift-users <swift-users@swift.org> wrote:

Hey David —

FYI essentially all of this stuff is already present in the simd module (stdlib/public/SDK/simd/simd.swift.gyb or ‘import simd'), albeit without nice generics, and with some different stylistic choices because simd is at present a straight Swift port of a subset of the simd C/Obj-C/C++ library on OS X and iOS (the most important distinction is that the vector types are compatible with clang extended vectors in C languages).

Making the simd module more “swifty” is something that will need to happen eventually, and there’s a lot of other opportunities for other improvements, but you should be aware that it’s available.

– Steve

On Jan 4, 2016, at 10:11 PM, David Turnbull via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I've been working on a math library for SwiftGL. It's looking good. Vector2, Vector3, Vector4, Matrix2x2, Matrix3x3, Matrix4x4 are implemented with all arithmetic. You can even swizzle just like GLSL.

var myVec = vec4(1, 2, 3, 4)
myVec.ab = vec2(99, 98)
print(myVec) //=> (1, 2, 98, 99)

There's still a lot to do. I hope to have feature parity with GLSL done this week. But it's ready to set free and get feedback.

GitHub - AE9RB/SwiftGL: This project has moved.

And a couple questions. Is there any way to make import SwiftGL.Math work? Note the dot. Also, is there anything reasonable I can do to improve the compile time? 3.5 minutes for 2000 lines of code can't be right.

-David "Expression was too complex to be solved" Turnbull

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

There are three pieces of the simd module on OS X / iOS.

- There’s the SDK overlay, which is part of Swift.
- There are C/C++ headers /usr/include/simd/*.h, which comprise the bulk of the C implementation.
- A small number of routines require external calls, whose implementations are part of libSystem (to clarify for Linux folks, libSystem is a dynamic library that contains all the low-level runtime stuff: libc, libm, compiler-rt, etc).

The first is part of the Swift open-source project; the other two are not, so the simd interfaces are not available in open-source Swift. However, only a small subset of the Swift operations depend on the C/C++ headers and library for their implementation; most of the implementations for Swift are in the SDK overlay itself. It would be easy for someone to provide implementations for the remaining operations (inverse and determinant, IIRC) if they were so inclined. With that done, I don’t believe there would be any other obstacles to the use of the SDK overlay as a native Swift module.

– Steve

If you’re asking if the C library that Apple ships will be open-sourced, that’s an Apple product-planning question that’s out of scope for this list. We (Apple) will definitely take such a request into account, but please file a formal request (Radar) at bugreporter.apple.com. Thanks!

···

On Jan 5, 2016, at 2:45 PM, Volodymyr Boichentsov <sakristx@gmail.com> wrote:

Any plans to make simd open-source? or available for Linux?

Best Regards,
Volodymyr Boichentsov

On 5 Jan 2016, at 16:59, Stephen Canon via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hey David —

FYI essentially all of this stuff is already present in the simd module (stdlib/public/SDK/simd/simd.swift.gyb or ‘import simd'), albeit without nice generics, and with some different stylistic choices because simd is at present a straight Swift port of a subset of the simd C/Obj-C/C++ library on OS X and iOS (the most important distinction is that the vector types are compatible with clang extended vectors in C languages).

Making the simd module more “swifty” is something that will need to happen eventually, and there’s a lot of other opportunities for other improvements, but you should be aware that it’s available.

– Steve

On Jan 4, 2016, at 10:11 PM, David Turnbull via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I've been working on a math library for SwiftGL. It's looking good. Vector2, Vector3, Vector4, Matrix2x2, Matrix3x3, Matrix4x4 are implemented with all arithmetic. You can even swizzle just like GLSL.

var myVec = vec4(1, 2, 3, 4)
myVec.ab = vec2(99, 98)
print(myVec) //=> (1, 2, 98, 99)

There's still a lot to do. I hope to have feature parity with GLSL done this week. But it's ready to set free and get feedback.

GitHub - AE9RB/SwiftGL: This project has moved.

And a couple questions. Is there any way to make import SwiftGL.Math work? Note the dot. Also, is there anything reasonable I can do to improve the compile time? 3.5 minutes for 2000 lines of code can't be right.

-David "Expression was too complex to be solved" Turnbull

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

1 Like