Files
stgsharp/源代码/StgSharp/Graphics/GL/GLloader.cs
T

96 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics
{
public unsafe partial class Form
{
internal GLcontext* glContext;
private int missingAPIcount = 0;
private int problemAPIcount = 0;
public unsafe IntPtr LoadGLfunc(string name)
{
string log = $"Attempt to load ";
IntPtr ret = InternalIO.glfw.GetProcAddress(name);
if (ret == default)
{
InternalIO.InternalAppendLog(
$"Cannot load OpenGL api called {name}, this api may not exist or supported.");
missingAPIcount++;
return ret;
}
return ret;
}
public unsafe void LoadGlapi()
{
InternalIO.InternalLoaderLoadGLapi(glContext, LoadGLfunc);
//check out if all api is converted correctely.
FieldInfo[] target = typeof(GLcontext).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
string problemLog = "Have problem in converting the fallowing GL API to C# delegates:\n";
foreach (FieldInfo item in target)
{
if (item.FieldType.Name.Contains("PFNG"))
{
if (item.GetValue(*glContext) == null)
{
problemLog += item.Name + ",";
problemAPIcount++;
}
}
}
InternalIO.InternalWriteLog(problemLog, LogType.Error);
if (problemAPIcount > missingAPIcount)
{
InternalIO.InternalWriteLog("Attempt to reload GL APIs", LogType.Info);
//use IntPtr as interface
TypedReference contextref = __makeref(*glContext);
GLcontextIO* iopack = (GLcontextIO*)glContext;
TypedReference iosource = __makeref(*iopack);
foreach (FieldInfo item in target)
{
if (item.FieldType.IsSubclassOf(typeof(Delegate))
&& item.GetValueDirect(contextref) == null)
{
IntPtr proc = default;
proc = LoadGLfunc("gl" + item.Name);
if (proc != IntPtr.Zero)
{
try
{
Delegate dele = Marshal.GetDelegateForFunctionPointer(proc, item.FieldType);
item.SetValueDirect(contextref, dele);
}
catch (Exception ex)
{
InternalIO.InternalWriteLog(
$"{ex.Message}",
LogType.Error);
}
}
}
}
}
GL.gl = glContext;
}
}
}