2015/08/04 | Simon Rodriguez | macos opengl swift

It can be useful during the development and debug of graphical code to check the OpenGL version or which extensions are available[1].

Swift being more type-sensitive than Objective-C, one sometimes needs to fiddle when combining it with OpenGL. First, enums defined in OpenGL headers will be considered as Int32, not as GLenum, so you'll have to cast them before calling any GL function.

Furthermore, when calling glGetString you have to manually handle the pointer, as glGetString returns a pointer to an array of UInt8 (ASCII string), whereas Swift necessarily expects a Int8 array when manipulating C-Strings. With no possible direct conversion from UnsafePointer<UInt8> to UnsafePointer<Int8>, you'll have to extract each character.

var extensions = glGetString(GLenum(GL_EXTENSIONS)) 
                as UnsafePointer<UInt8>
var array : [Int8] = []

while (extensions[0] != UInt8(ascii:"\0")){
    extensions = extensions.advancedBy(1)
if let extensionsString = String.fromCString(array) {
    println("Available extensions :\n--------------------------")
    println(extensionsString.stringByReplacingOccurrencesOfString(" ", withString: "\n"))

This snippet displays all the extensions enabled on the testing device. (Pointer manipulation in Swift is not as straightforward as in C derived languages, but seems safer.)

  1. for instance, the list of GL extensions for iOS devices with PowerVR GPUs in the documentation is not exhaustive.