Here's a multi-platform OpenGL loader for Swift.


(David Turnbull) #1

If you tried using OpenGL with something other than Apple's framework then
you probably didn't get very far. The header files of GLEW and Epoxy aren't
fully understood by Swift. So I wrote some Swift code that generates some
Swift code using the XML file from Khronos. Now everything you usually get
from the headers is Swift code.

https://github.com/AE9RB/SwiftGL

The Demo folder contains an OpenGL equivalent of hello world. This is also
a good example to look at if you want to learn more about C bindings.

https://github.com/AE9RB/SwiftGL/tree/master/Demo

-david


(Joe Groff) #2

Nice! Having done OpenGL bindings for other languages, I know this isn't a trivial task. Giving the functions argument labels is a nice touch.

-Joe

···

On Dec 10, 2015, at 9:22 PM, David Turnbull via swift-users <swift-users@swift.org> wrote:

If you tried using OpenGL with something other than Apple's framework then you probably didn't get very far. The header files of GLEW and Epoxy aren't fully understood by Swift. So I wrote some Swift code that generates some Swift code using the XML file from Khronos. Now everything you usually get from the headers is Swift code.

https://github.com/AE9RB/SwiftGL

The Demo folder contains an OpenGL equivalent of hello world. This is also a good example to look at if you want to learn more about C bindings.

https://github.com/AE9RB/SwiftGL/tree/master/Demo

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


(David Turnbull) #3

It's looking like the Epoxy requirement for SwiftGL is going away real soon
now. I was able to lookup the OpenGL symbols from pure Swift code. No
compiled C code is needed at all. What follows is roughly how I will do it.
It lazily loads the pointer the first time you use it. The Linux version is
only slightly different.

Does anyone know a way to do this without the unsafeBitCast?

import Darwin

let handle = dlopen(
"/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL",
RTLD_LAZY)

func DLOADglGetIntegerv(a:GLenum, _ b:UnsafeMutablePointer<GLint>) -> Void {

    glGetIntegerv = unsafeBitCast(dlsym(handle, "glGetIntegerv"),
glGetIntegerv.dynamicType)

    glGetIntegerv(a,b)

}

public var glGetIntegerv:@convention(c) (GLenum, UnsafeMutablePointer<GLint>)
-> Void = { DLOADglGetIntegerv($0,$1) }

···

On Thu, Dec 10, 2015 at 9:22 PM, David Turnbull <dturnbull@gmail.com> wrote:

If you tried using OpenGL with something other than Apple's framework then
you probably didn't get very far. The header files of GLEW and Epoxy aren't
fully understood by Swift. So I wrote some Swift code that generates some
Swift code using the XML file from Khronos. Now everything you usually get
from the headers is Swift code.

https://github.com/AE9RB/SwiftGL

The Demo folder contains an OpenGL equivalent of hello world. This is also
a good example to look at if you want to learn more about C bindings.

https://github.com/AE9RB/SwiftGL/tree/master/Demo

-david


(Harlan Haskins) #4

This is a great use case for multiline string literals! :sweat_smile:

···

On Dec 11, 2015, at 10:56 PM, Joe Groff via swift-users <swift-users@swift.org> wrote:

Nice! Having done OpenGL bindings for other languages, I know this isn't a trivial task. Giving the functions argument labels is a nice touch.

-Joe

On Dec 10, 2015, at 9:22 PM, David Turnbull via swift-users <swift-users@swift.org> wrote:

If you tried using OpenGL with something other than Apple's framework then you probably didn't get very far. The header files of GLEW and Epoxy aren't fully understood by Swift. So I wrote some Swift code that generates some Swift code using the XML file from Khronos. Now everything you usually get from the headers is Swift code.

https://github.com/AE9RB/SwiftGL

The Demo folder contains an OpenGL equivalent of hello world. This is also a good example to look at if you want to learn more about C bindings.

https://github.com/AE9RB/SwiftGL/tree/master/Demo

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

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


(Joe Groff) #5

I think the unsafeBitCast is fundamentally necessary here to coerce the void pointer you get from dlsym to a C function pointer. I don't think it's a problem. This is pretty awesome!

-Joe

···

On Dec 12, 2015, at 1:39 PM, David Turnbull via swift-users <swift-users@swift.org> wrote:

It's looking like the Epoxy requirement for SwiftGL is going away real soon now. I was able to lookup the OpenGL symbols from pure Swift code. No compiled C code is needed at all. What follows is roughly how I will do it. It lazily loads the pointer the first time you use it. The Linux version is only slightly different.

Does anyone know a way to do this without the unsafeBitCast?

import Darwin

let handle = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY)

func DLOADglGetIntegerv(a:GLenum, _ b:UnsafeMutablePointer<GLint>) -> Void {
    glGetIntegerv = unsafeBitCast(dlsym(handle, "glGetIntegerv"), glGetIntegerv.dynamicType)
    glGetIntegerv(a,b)
}

public var glGetIntegerv:@convention(c) (GLenum, UnsafeMutablePointer<GLint>) -> Void = { DLOADglGetIntegerv($0,$1) }


(David Turnbull) #6

It's all generated code from a canonical XML source so really not at all
unsafe. Certainly no more unsafe than fixing up a C header file that
doesn't quite work with Swift.

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

···

On Mon, Dec 14, 2015 at 9:22 AM, Joe Groff <jgroff@apple.com> wrote:

I think the unsafeBitCast is fundamentally necessary here