Home > Software design >  OpenGL loading fails at seemingly random points
OpenGL loading fails at seemingly random points

Time:01-03

I am loading OpenGL in C# using LoadLibrary/GetProcAddress/wglGetProcAddress. For some reason, it fails to load the pointer for glBlendFunc, and other random ones after that point. I have been looking through my code for the better part of an hour now, and all these failing parts look completely fine and normal?

The code to load the function pointers and cast it to a delegate.

    public override T GetFunction<T>(string name)
    {
        IntPtr pointer = wglGetProcAddress(name);
        if (pointer == IntPtr.Zero)
            pointer = GetProcAddress(Handle, name);
        return Marshal.GetDelegateForFunctionPointer<T>(pointer);
    }

A sample of the code that calls GetFunction.

 BlendEquationSeparate = Loader.GetFunction<glBlendEquationSeparate>("glBlendEquationSeparate");
 BlendEquationSeparatei = Loader.GetFunction<glBlendEquationSeparatei>("glBlendEquationSeparatei");
 BlendFunc = Loader.GetFunction<glBlendFunc>("glBlendFunc");
 BlendFunci = Loader.GetFunction<glBlendFunci>("glBlendFunci");
 BlendFuncSeparate = Loader.GetFunction<glBlendFuncSeparate>("glBlendFuncSeparate");
 BlendFuncSeparatei = Loader.GetFunction<glBlendFuncSeparatei>("glBlendFuncSeparatei");

A sample of the delegate code.

public delegate void glBlendEquationSeparate(BlendEquationMode modeRGB, BlendEquationMode modeAlpha);
public static glBlendEquationSeparate BlendEquationSeparate;
public delegate void glBlendEquationSeparatei(UInt32 buf, BlendEquationMode modeRGB, BlendEquationMode modeAlpha);
public static glBlendEquationSeparatei BlendEquationSeparatei;
public delegate void glBlendFunc(BlendingFactorSrc sfactor, BlendingFactorDest dfactor);
public static glBlendFunc BlendFunc;
public delegate void glBlendFunci(UInt32 buf, BlendingFactorSrc sfactor, BlendingFactorDest dfactor);
public static glBlendFunci BlendFunci;
public delegate void glBlendFuncSeparate(BlendingFactorSrc srcRGB, BlendingFactorDest dstRGB, BlendingFactorSrc srcAlpha, BlendingFactorDest dstAlpha);
public static glBlendFuncSeparate BlendFuncSeparate;
public delegate void glBlendFuncSeparatei(UInt32 buf, BlendingFactorSrc srcRGB, BlendingFactorDest dstRGB, BlendingFactorSrc srcAlpha, BlendingFactorDest dstAlpha);
public static glBlendFuncSeparatei BlendFuncSeparatei;

The DllImports I am using

[DllImport("kernel32")]
public static extern IntPtr LoadLibrary(string fileName);

[DllImport("kernel32")]
public static extern IntPtr GetProcAddress(IntPtr module, string procName);

[DllImport("opengl32.dll", EntryPoint = "wglGetProcAddress", ExactSpelling = true)]
public static extern IntPtr wglGetProcAddress(string lpszProc);

All the enums are pretty standard, and need no context. I load the library like usual: LoadLibrary(opengl32.dll);. Can anyone explain to me where I am going wrong?

CodePudding user response:

I have found a somewhat strange solution. If I move the code to a static constructor so that it runs at the beginning of the program, it suddenly works. I have no idea why my first approach didn't work.

  • Related