Skip to content

Commit

Permalink
Fix wglGetExtensionsStringARB definition and enforce to realloc resul…
Browse files Browse the repository at this point in the history
…t of GetExtensionsString

This fix a crash on AMD drivers on Windows caused by the result being in the .rodata, this fixes a deallocation made by .NET managed code
  • Loading branch information
Mary committed Apr 8, 2021
1 parent e6923b2 commit 2ea2a68
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions SPB/Platform/WGL/WGLHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public static class WGLHelper

private static string[] Extensions;

private delegate string wglGetExtensionsString();
private delegate IntPtr wglGetExtensionsStringARB(IntPtr hdc);
private delegate IntPtr wglGetExtensionsStringEXT();

private static wglGetExtensionsString GetExtensionsString;
private static wglGetExtensionsStringEXT GetExtensionsStringEXT;
private static wglGetExtensionsStringARB GetExtensionsStringARB;

[UnmanagedFunctionPointer(CallingConvention.Winapi, SetLastError = true)]
private delegate bool wglChoosePixelFormatARBDelegate(IntPtr hdc, int[] piAttribIList, float[] pfAttribFList, int nMaxFormats, int[] piFormats, out int nNumFormats);
Expand All @@ -47,6 +49,23 @@ public static class WGLHelper

public static wglGetSwapIntervalEXTDelegate GetSwapInterval { get; private set; }


private static string GetExtensionsString()
{
IntPtr stringPtr;

if (GetExtensionsStringARB != null)
{
stringPtr = GetExtensionsStringARB(WGL.GetCurrentDC());
}
else
{
stringPtr = GetExtensionsStringEXT();
}

return Marshal.PtrToStringAnsi(stringPtr);
}

public static IntPtr GetProcAddress(string procName)
{
IntPtr result = WGL.GetProcAddress(procName);
Expand Down Expand Up @@ -122,16 +141,8 @@ private static void EnsureInit()
throw new PlatformException($"WGL.MakeCurrent failed for dummy context: {Marshal.GetLastWin32Error()}");
}

// Now that we have a context, query everything we need.

IntPtr getExtensionsPtr = WGL.GetProcAddress("wglGetExtensionsStringEXT");

if (getExtensionsPtr == IntPtr.Zero)
{
getExtensionsPtr = WGL.GetProcAddress("wglGetExtensionsStringARB");
}

GetExtensionsString = Marshal.GetDelegateForFunctionPointer<wglGetExtensionsString>(getExtensionsPtr);
GetExtensionsStringARB = Marshal.GetDelegateForFunctionPointer<wglGetExtensionsStringARB>(WGL.GetProcAddress("wglGetExtensionsStringARB"));
GetExtensionsStringEXT = Marshal.GetDelegateForFunctionPointer<wglGetExtensionsStringEXT>(WGL.GetProcAddress("wglGetExtensionsStringEXT"));

Extensions = GetExtensionsString().Split(" ");

Expand Down

0 comments on commit 2ea2a68

Please sign in to comment.