Using OpenGL from Swift

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?

1 Like

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.

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.

Moved to the Using Swift category.

Interesting. Is it run on iOS?

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

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.

I realise this is an old topic, but thought I'd mention that I'm currently working on a generated Swift interface for Vulkan here.

It's still got a way to go, but I hope to have some working demos soon.


This looks amazing! I'm making heavy use of the Vulkan C API from Swift, and I would probably switch to this in a second when it's production ready.

One of the rough edges I've noticed in using Vulkan from Swift is the heavy use of pointers in Vulkan structs, since it's not super clean to bridge to pointers in Swift unless you're passing them as function arguments. How do you handle it if you don't mind me asking?

Thanks @Spencer_Kohan!

In terms of dealing with the pointers, it's usually a case of using the builtin with* methods (withUnsafePointer, withUnsafeBufferPointer, withCString etc.) to convert from Swift to C, and then making use of the C data within the closures. I've found fixed size C arrays slightly more tricky to deal with, since these are imported to Swift as tuples. The aim of the library is to abstract this though.

Maybe late with my code sample.
Swift and OpenGL on macOS, iOS, Linux, Android.

Terms of Service

Privacy Policy

Cookie Policy