lewisenator's picture

OpenGL 3+ w/ SDL2 on Mac?

Hi Everyone!

I've been struggling with getting anything above OpenGL 2.1 working with OpenTK on my Mac using OS X 10.9. I know it is supported on my hardware. Browsing through the source on Github, I followed the major and minor version constructor parameters from GameWindow to GraphicsContext to MacOSFactory where they are completely ignored. Eventually I found a thread in the forums which implies that OpenGL 3+ with OpenTK will not work on a Mac (http://www.opentk.com/node/3171).

Is this still the case with the new SDL2 backend in OpenTK 1.1 beta1? If not, how can I switch my project to use the new SDL2 backend? I tried copying both SDL2.dll and libSDL2.dylib from the Dependencies folder to /Library/Frameworks. I also tried the SDL2.framework from the DMG on libsdl.org. Am I missing something?

Thanks,

Kenneth


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

From Dependencies/Readme.txt:

Quote:

[SDL2.dll]
[libSDL2.dylib]

The Simple DirectMedia Library compiled for Windows and Mac OS X.
Copy these to your application directory in order to use the SDL2
platform backend for OpenTK.

Make sure your OpenTK.dll.config is up to date (copy it from the latest release zip if not certain.)

Edit: Apple does not allow Carbon applications to use OpenGL 3.x. You need the SDL2 backend for that.

lewisenator's picture

Thanks for the quick response!

Copying those files to my application directory didn't seem to help. I added SDL2.dll, libSDL2.dylib, and an up-to-date OpenTK.dll.config to my project and set them to "Copy to Output Directory." I verified that all three end up in my project's bin/Release and bin/Debug directories alongside the executable. It didn't seem to matter whether I started my app from Xamarin Studio or from the command line.

I did end up getting this working with the SDL2 backend. I grabbed the SDL2.framework from libsdl.org and placed it in /Library/Frameworks. Then I changed the following line in the updated OpenTK.dll.config from this

<dllmap os="osx" dll="SDL2.dll" target="libSDL2.dylib"/>

to this

<dllmap os="osx" dll="SDL2.dll" target="/Library/Frameworks/SDL2.framework/SDL2"/>

and that seemed to do the trick.

Oddly, if I request OpenGL version 3.3 or higher through the GameWindow constructor, I get an OpenTK.Graphics.GraphicsContextException with the message "OpenGL > 3.2 is not supported on this platform." If I request version 3.2, everything works fine and GL.GetString(StringName.Version) returns "4.1 NVIDIA-8.18.22 310.40.05f01", much higher than the reported limit of 3.2.

Thanks for the very timely update! I just started up a new project using OpenTK and reached a point where I wanted to use an OpenGL version that was previously not possible on a Mac with the framework. I've used OpenTK in several desktop and iOS projects so far. It has never let me down. Great work.

the Fiddler's picture

Interesting, I can reproduce the context version issue. This is a bug in SDL, I have reported it upstream at https://bugzilla.libsdl.org/show_bug.cgi?id=2283

If you don't mind installing with admin privileges, the SDL framework from libsdl.org works fine.

With libSDL2.dylib, you need to copy the correct version (x86 vs x64), or use a custom dll.config according to the instructions here. Note that Mono (and MonoMac) only supports x86 on the Mac; for x64, you need the very latest Xamarin.Mac.

Cireon's picture

I have been trying to get this to work as well, but I can't seem to get it work. I have copied the dll.config, libSDL2.dylib (tried both x64 and x86) and SDL2.dll (again, both x64 and x86 tried) to the application directory and tried to run it. Outputting OpenTK.Configuration.RunningOnSdl2 yields False. Looking at the source code, I should see for example an error and at least a line whether SDL2 is supported or not in my application output, but I only see a single empty line.
I have also tried changing the dllmap to refer to the SDL2.framework like lewisenator described above, but to no avail. I am really clueless on this and I am not sure what I am missing.

the Fiddler's picture

Which version of Mac OS X are you using? I have only tested on 10.8 and 10.9, and I can verify that SDL2 and OpenGL 3 and 4 respectively are working correctly. You need to use the x86 version of SDL2, until a x64 version of mono becomes available on Mac OS X.

To get diagnostic messages, compile and run a debug version of OpenTK.dll. If SDL fails to load there should be a message explaining why.

winterhell's picture

Is SDL2 now required for all versions of OpenGL under OS X or is it just for a subset of 3.X ?

the Fiddler's picture

SDL2 is strictly optional. If SDL2 is not available, OpenTK will use Carbon/AGL instead. Carbon is unfortunately limited to OpenGL 2.1.

Adding a Cocoa backend is a long-term goal, but for now SDL2 is the only way to access OpenGL 3.x on a Mac. Unless someone volunteers to help, I do not see this changing in the near future.

Cireon's picture

Sorry for the long wait. During Christmas I haven't worked on this project, so I only now went back to working on this. Changing to the x86 version already did wonders. I read about it before, but I must have completely forgotten. I am also working on a debug version of OpenTK now and got the debug output there, so thanks for that tip as well.

Anyhow, by using the x86 version now I have been able to get SDL2 to work. However, I do not get OpenGL 3 or higher to work. I have tried changing the dllmap to use the locally installed SDL2 framework, but the result is not different from using the dylib. I first considered that maybe it used my intel graphics card, as I am still using a Macbook Pro with two graphics cards, but according to the Apple capabilities even those should support up to OpenGL 4.1. Below is the output generated by OpenGL, followed by four output lines I added myself. The GL version string, shader language version string and the values of "RunningOnSdl2" and "RunningOnMacOS" from the OpenTK.Configuration class.

[0:] 
[0:] GraphicsMode.Default = Index: , Color: 32 (8888), Depth: 16, Stencil: 0, Samples: 0, Accum: 0 (0000), Buffers: 2, Stereo: False
 
[0:] 
[0:] Size: 2304
 
[0:] 
[0:] System:
 
[0:]     
[0:] Darwin
 
[0:]     
[0:] Toms-MacBook-Pro.local
 
[0:]     
[0:] 13.0.0
 
[0:]     
[0:] Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64
 
[0:]     
[0:] x86_64
 
[0:] 
[0:] SDL2 is supported
 
[0:] 
[0:] Detected configuration: MacOS / Mono
 
[0:] 
[0:] Creating GraphicsContext.
 
[0:]     
[0:] GraphicsMode: Index: , Color: 32 (8888), Depth: 16, Stencil: 0, Samples: 0, Accum: 0 (0000), Buffers: 2, Stereo: False
 
[0:]     
[0:] IWindowInfo: OpenTK.Platform.SDL2.Sdl2WindowInfo
 
[0:]     
[0:] GraphicsContextFlags: Default
 
[0:]     
[0:] Requested version: 3.2
 
[0:]     
[0:] SDL2 created GraphicsContext (handle: 2083428864)
 
[0:]     
[0:]     GraphicsMode: Index: , Color: 32 (8888), Depth: 16, Stencil: 0, Samples: 0, Accum: 0 (0000), Buffers: 2, Stereo: False
 
[0:]     
[0:]     GraphicsContextFlags: Default
 
[0:] 
[0:] Bindings loaded in 198.5998 ms.
 
2.1 NVIDIA-8.18.22 310.40.05f01
1.20
running on sdl2: True
running on macos: True

I too, get the error described above when requesting OpenGL version 3.3 or higher.

the Fiddler's picture

I just tested on a rMBP and I can get a 3.2 context with SDL2 if (and only if) I specify GraphicsContextFlags.ForwardCompatible in the constructor.

I will forward this to the SDL developers and add a workaround in OpenTK. Can you please file a bug report at https://github.com/opentk/opentk/issues?

Cireon's picture

Yes! This seems to work now. Thanks a lot for you quick answer. I have filed the bug on GitHub.