Using OpenGL from Swift

(Eben Up) #1

I'm interested in using Swift on GNU/Linux specifically accessing OpenGL or perhaps Vulkan.
Most likely I'll want to run the same code on MacOS at some point, where I gather OpenGL is deprecated but still available.
Is there anything preventing the use of OpenGL at least on Linux at this point?

(^) #2

nope, used it many times before, and it works quite well with GLFW too. you might be interested in some bare bones bindings from OpenGL to Swift.

(Thomas Roughton) #3

As another alternative, I’ll throw in mention of my SwiftFrameGraph project ( if you instead wanted to target Vulkan on Linux and Metal on macOS. It’s fairly poorly documented at the moment, but having other people use it would be reason to start improving that and I’d be happy to help.

If you are interested in using it, I should also say that the Vulkan backend hasn’t been tested/optimised nearly as thoroughly as the Metal one, but it does work for basic rendering, and will be getting more attention in a few months time.

Otherwise, as @taylorswift said, OpenGL works fine from Swift (and if you’re using a wrapper with argument labels is often much clearer than the corresponding C code).

EDIT: the other thing to be aware of is the last version of OpenGL that'll work on macOS is 4.1. That means no compute shaders, no modern resource binding, and a host of other limitations.

(John McCall) #4

Moved to the Using Swift category.


Interesting. Is it run on iOS?

(Thomas Roughton) #6

As of the past three days, yes. I just got around to adding support.

(Spencer Kohan) #7

There's nothing preventing you from using OpenGL or Vulkan from Swift. Both have C APIs, and Swift interoperates quite well. If you're using the package manager, you can include either library as a system module.

Most of the pain points calling the OpenGL API from swift have to do with types: occasionally you'll need to cast things to make everything work correctly. There's a pretty nice wrapper library ( which rounds off a lot of these rough edges.