opentk / LearnOpenTK

A port of learnopengl.com's tutorials to OpenTK and C#.
Creative Commons Attribution 4.0 International
458 stars 115 forks source link

OSX: Chapter 1/1 not working on OSX #57

Closed mrj001 closed 2 years ago

mrj001 commented 3 years ago

I've copied the code from the repo commit d71bd15.

I've tried both versions 4.6.4 and 5.0.0-pre.6.

I've modified Program.cs like this:

--- a/Program.cs
+++ b/Program.cs
@@ -12,6 +12,7 @@ namespace LearnOpenTK
          {
             Size = new Vector2i(800, 600),
             Title = "LearnOpenTK - Creating a Window",
+            APIVersion = new Version(3, 2)
          };

          // To create a new window, create a class that extends GameWindow, then call Run() on it.

I've tried versions 3.2, 3.3 and 4.1. The comments in the NativeWindowSettings.cs indicate these should all work on MacOS.

I'm consistently getting this exception:

Unhandled exception. OpenTK.Windowing.GraphicsLibraryFramework.GLFWException: NSGL: The targeted version of macOS only supports forward-compatible core profile contexts for OpenGL 3.2 and above at OpenTK.Windowing.Desktop.GLFWProvider.<>c.<.cctor>b__5_0(ErrorCode errorCode, String description) Abort trap: 6

Here is the output of my "dotnet --info"

.NET SDK (reflecting any global.json): Version: 5.0.102 Commit: 71365b4d42

Runtime Environment: OS Name: Mac OS X OS Version: 10.15 OS Platform: Darwin RID: osx.10.15-x64 Base Path: /usr/local/share/dotnet/sdk/5.0.102/

Host (useful for support): Version: 5.0.2 Commit: cb5f173b96

.NET SDKs installed: 3.1.201 [/usr/local/share/dotnet/sdk] 3.1.403 [/usr/local/share/dotnet/sdk] 3.1.404 [/usr/local/share/dotnet/sdk] 5.0.100 [/usr/local/share/dotnet/sdk] 5.0.101 [/usr/local/share/dotnet/sdk] 5.0.102 [/usr/local/share/dotnet/sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 3.1.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.10 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.23 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.10 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

deccer commented 3 years ago

When you squeeze this Profile = ContextProfile.Compatability, in between the other properties, will that fix the problem for you?

If yes, maybe we can wrap it with something like

#if MACOS
Profile = ContextProfile.Compatability,
#endif
deccer commented 3 years ago

Just noted the spelling error.. Compatability :D Isn't it Compatibility?

If this fix works, maybe you can provide a PR with both :) That also means we should modify the csproj files so that they can emit a symbol/definition "MACOS" when you are on that platform...

Maybe this works

    <PropertyGroup>
        <IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
        <IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
        <IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
    </PropertyGroup>
    <PropertyGroup Condition="'$(IsWindows)'=='true'">
        <DefineConstants>WINDOWS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition="'$(IsOSX)'=='true'">
        <DefineConstants>OSX</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition="'$(IsLinux)'=='true'">
        <DefineConstants>LINUX</DefineConstants>
    </PropertyGroup> 

in conjunction with

#if OSX
...
#endif
mrj001 commented 3 years ago

My "git diff" on Program.cs now looks like this (compared to the tutorial):

--- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ <U+FEFF>using System; using OpenTK.Mathematics; +using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop;

namespace LearnOpenTK @@ -12,6 +13,8 @@ namespace LearnOpenTK { Size = new Vector2i(800, 600), Title = "LearnOpenTK - Creating a Window",

  • Profile = ContextProfile.Compatability,
  • APIVersion = new Version(3, 2) };

      // To create a new window, create a class that extends GameWindow, then call Run() on it.

And, I'm still getting the same runtime exception:

Unhandled exception. OpenTK.Windowing.GraphicsLibraryFramework.GLFWException: NSGL: The targeted version of macOS only supports forward-compatible core profile contexts for OpenGL 3.2 and above at OpenTK.Windowing.Desktop.GLFWProvider.<>c.<.cctor>b__5_0(ErrorCode errorCode, String description)

This also happens with APIVersion set to 3.3 and 4.1. I only tried OpenTK version 5.0.0-pre.6 today.

NogginBops commented 3 years ago

What you need is not to enable compatibility context (macos doesn't support it), instead you need to set the forward-compatible flag (which removes compatibility context functions).

Flags = ContextFlags.ForwardCompatible,
mrj001 commented 3 years ago

@NogginBops, this worked, the current 'git diff' against Program.cs is:

--- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ <U+FEFF>using System; using OpenTK.Mathematics; +using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop;

namespace LearnOpenTK @@ -12,6 +13,8 @@ namespace LearnOpenTK { Size = new Vector2i(800, 600), Title = "LearnOpenTK - Creating a Window",

  • Flags = ContextFlags.ForwardCompatible,
  • APIVersion = new Version(4, 1) };

      // To create a new window, create a class that extends GameWindow, then call Run() on it.
mrj001 commented 3 years ago

@deccer, I think rather than a PR against the tutorials, that it would be better to change the value of the ContextFlags.Default on MacOS. See opentk/opentk#1307

deccer commented 3 years ago

I guess that's possible.