Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 96e90a09d9 | |||
| 519b06367a | |||
| 2970fdc3c6 | |||
| 221cfa5e9c | |||
| 7167d98cb6 |
+48
-18
@@ -2,15 +2,14 @@
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using Hexa.NET.ImGui;
|
using Hexa.NET.ImGui;
|
||||||
using Hexa.NET.ImPlot;
|
using Hexa.NET.ImPlot;
|
||||||
using Hexa.NET.ImPlot3D;
|
|
||||||
using SDL3_TestingSuite.SDL3;
|
using SDL3_TestingSuite.SDL3;
|
||||||
using SDL3;
|
|
||||||
|
|
||||||
namespace SDL3_TestingSuite;
|
namespace SDL3_TestingSuite;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
private static bool _demoWindowVisible = true;
|
private static bool _demoWindowVisible = true;
|
||||||
|
private static bool _imPlotDemoVisible;
|
||||||
private static bool _fontStuff;
|
private static bool _fontStuff;
|
||||||
private static readonly Dictionary<string, List<uint>?> GlyphsByName = new Dictionary<string, List<uint>?>();
|
private static readonly Dictionary<string, List<uint>?> GlyphsByName = new Dictionary<string, List<uint>?>();
|
||||||
private static bool _initialized;
|
private static bool _initialized;
|
||||||
@@ -19,17 +18,16 @@ public class Program
|
|||||||
|
|
||||||
public static void Main()
|
public static void Main()
|
||||||
{
|
{
|
||||||
const SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity | SDL.WindowFlags.Transparent;
|
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720);
|
||||||
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720, flags);
|
window.RenderCallback += () =>
|
||||||
window.ClearColor.W = 0f;
|
|
||||||
window.RenderCallback = () =>
|
|
||||||
{
|
{
|
||||||
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
|
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
|
||||||
ImGui.SetNextWindowPos(viewport.WorkPos);
|
ImGui.SetNextWindowPos(viewport.WorkPos);
|
||||||
ImGui.SetNextWindowSize(viewport.WorkSize);
|
ImGui.SetNextWindowSize(viewport.WorkSize);
|
||||||
|
ImGui.SetNextWindowViewport(viewport.ID);
|
||||||
|
|
||||||
ImGui.Begin("MainWindow", ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoBringToFrontOnFocus);
|
if (ImGui.Begin("MainWindow", ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoBringToFrontOnFocus))
|
||||||
|
{
|
||||||
if (ImGui.BeginMenuBar())
|
if (ImGui.BeginMenuBar())
|
||||||
{
|
{
|
||||||
if (ImGui.BeginMenu("Stuff"))
|
if (ImGui.BeginMenu("Stuff"))
|
||||||
@@ -44,12 +42,15 @@ public class Program
|
|||||||
ImGui.Spacing();
|
ImGui.Spacing();
|
||||||
ImGui.MenuItem("Demo Window", "", ref _demoWindowVisible);
|
ImGui.MenuItem("Demo Window", "", ref _demoWindowVisible);
|
||||||
ImGui.Spacing();
|
ImGui.Spacing();
|
||||||
|
ImGui.MenuItem("ImPlot Demo Window", "", ref _imPlotDemoVisible);
|
||||||
|
ImGui.Spacing();
|
||||||
ImGui.MenuItem("Font Stuff", "", ref _fontStuff);
|
ImGui.MenuItem("Font Stuff", "", ref _fontStuff);
|
||||||
|
|
||||||
ImGui.EndMenuBar();
|
ImGui.EndMenuBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui.End();
|
ImGui.End();
|
||||||
|
}
|
||||||
|
|
||||||
if (_fontStuff)
|
if (_fontStuff)
|
||||||
{
|
{
|
||||||
@@ -69,7 +70,7 @@ public class Program
|
|||||||
fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
|
fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
|
||||||
if (!_initialized || change)
|
if (!_initialized || change)
|
||||||
{
|
{
|
||||||
if (!GlyphsByName.TryGetValue(fontName, out List<uint> glyphs))
|
if (!GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs))
|
||||||
{
|
{
|
||||||
glyphs = new List<uint>();
|
glyphs = new List<uint>();
|
||||||
|
|
||||||
@@ -96,12 +97,12 @@ public class Program
|
|||||||
|
|
||||||
ImGui.PushFont(null, 24);
|
ImGui.PushFont(null, 24);
|
||||||
|
|
||||||
if (GlyphsByName.TryGetValue(fontName, out List<uint> glyphs2))
|
if (GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs2))
|
||||||
{
|
{
|
||||||
float cursorXStart = ImGui.GetCursorPosX();
|
float cursorXStart = ImGui.GetCursorPosX();
|
||||||
float maxWidth = ImGui.GetContentRegionAvail().X;
|
float maxWidth = ImGui.GetContentRegionAvail().X;
|
||||||
|
|
||||||
foreach (uint codepoint in glyphs2)
|
foreach (uint codepoint in glyphs2!)
|
||||||
{
|
{
|
||||||
string text = char.ConvertFromUtf32((int)codepoint);
|
string text = char.ConvertFromUtf32((int)codepoint);
|
||||||
float glyphWidth = ImGui.CalcTextSize(text).X;
|
float glyphWidth = ImGui.CalcTextSize(text).X;
|
||||||
@@ -124,6 +125,8 @@ public class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ImGui.Begin("Thing"))
|
||||||
|
{
|
||||||
float time = (float)ImGui.GetTime() * 5;
|
float time = (float)ImGui.GetTime() * 5;
|
||||||
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
|
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
|
||||||
{
|
{
|
||||||
@@ -161,14 +164,24 @@ public class Program
|
|||||||
|
|
||||||
ImPlot.EndPlot();
|
ImPlot.EndPlot();
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
|
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
|
||||||
|
|
||||||
|
ImGui.End();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ImGui.Begin("ImageThing"))
|
||||||
|
{
|
||||||
|
|
||||||
|
ImGui.End();
|
||||||
|
}
|
||||||
|
|
||||||
if (_demoWindowVisible)
|
if (_demoWindowVisible)
|
||||||
{
|
{
|
||||||
ImGui.ShowDemoWindow(ref _demoWindowVisible);
|
ImGui.ShowDemoWindow(ref _demoWindowVisible);
|
||||||
ImPlot.ShowDemoWindow(ref _demoWindowVisible);
|
}
|
||||||
// ImPlot3D.ShowDemoWindow(ref _demoWindowVisible);
|
if (_imPlotDemoVisible)
|
||||||
|
{
|
||||||
|
ImPlot.ShowDemoWindow(ref _imPlotDemoVisible);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -177,13 +190,30 @@ public class Program
|
|||||||
|
|
||||||
public static class Logger
|
public static class Logger
|
||||||
{
|
{
|
||||||
public static void Log(string? str, [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
|
private const int MessageWidth = 80;
|
||||||
|
private const int CallerWidth = 35;
|
||||||
|
|
||||||
|
public static void Log(object? value, [CallerFilePath] string path = "", [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{caller}({line}): {str}");
|
Write(value?.ToString(), path, caller, line);
|
||||||
}
|
}
|
||||||
public static void Log(object? str, [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
|
|
||||||
|
public static void Log(object?[] values, [CallerFilePath] string path = "", [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
|
||||||
{
|
{
|
||||||
Log(str?.ToString(), caller, line);
|
if (values == null)
|
||||||
|
{
|
||||||
|
Write("null", path, caller, line);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Write(string.Join(" ", values), path, caller, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Write(string? str, string path, string caller, int line)
|
||||||
|
{
|
||||||
|
string message = (str ?? string.Empty).PadRight(MessageWidth);
|
||||||
|
string method = caller.PadRight(CallerWidth);
|
||||||
|
|
||||||
|
Console.WriteLine($"{message} | {method} | {path}:{line}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
<PackageReference Include="Hexa.NET.ImGui" Version="2.2.9" />
|
<PackageReference Include="Hexa.NET.ImGui" Version="2.2.9" />
|
||||||
<PackageReference Include="Hexa.NET.ImGui.Widgets" Version="1.2.18" />
|
<PackageReference Include="Hexa.NET.ImGui.Widgets" Version="1.2.18" />
|
||||||
<PackageReference Include="Hexa.NET.ImPlot" Version="2.2.9" />
|
<PackageReference Include="Hexa.NET.ImPlot" Version="2.2.9" />
|
||||||
<PackageReference Include="Hexa.NET.ImPlot3D" Version="2.2.9" />
|
<PackageReference Include="Hexa.NET.OpenGL3" Version="1.1.0" />
|
||||||
<PackageReference Include="Hexa.NET.Utilities" Version="2.2.12" />
|
<PackageReference Include="Hexa.NET.Utilities" Version="2.2.12" />
|
||||||
<PackageReference Include="SDL3-CS" Version="3.2.18" />
|
<PackageReference Include="SDL3-CS" Version="3.2.18" />
|
||||||
<PackageReference Include="SDL3-CS.Native" Version="3.2.18" />
|
<PackageReference Include="SDL3-CS.Native" Version="3.2.18" />
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace SDL3_TestingSuite.SDL3;
|
||||||
|
|
||||||
|
public unsafe static class DelegateHelpers
|
||||||
|
{
|
||||||
|
public static void* GetPtrForDelegate<TDelegate>(this TDelegate _delegate) where TDelegate : notnull => (void*)Marshal.GetFunctionPointerForDelegate(_delegate);
|
||||||
|
}
|
||||||
+4
-6
@@ -29,8 +29,8 @@ public static class FontFind
|
|||||||
FileInfo info = new FileInfo(fontPath);
|
FileInfo info = new FileInfo(fontPath);
|
||||||
if (!info.Exists || info.Length <= 0) return null;
|
if (!info.Exists || info.Length <= 0) return null;
|
||||||
|
|
||||||
FontMetrics metrics = FontFind.ReadFontMetrics(fontPath);
|
FontMetrics metrics = ReadFontMetrics(fontPath);
|
||||||
float size = FontFind.GetRecommendedPixelSize(metrics);
|
float size = GetRecommendedPixelSize(metrics);
|
||||||
|
|
||||||
ImFontConfigPtr config = ImGui.ImFontConfig();
|
ImFontConfigPtr config = ImGui.ImFontConfig();
|
||||||
config.FontLoaderFlags |= (uint)ImGuiFreeTypeLoaderFlags.LoadColor;
|
config.FontLoaderFlags |= (uint)ImGuiFreeTypeLoaderFlags.LoadColor;
|
||||||
@@ -110,11 +110,9 @@ public static class FontFind
|
|||||||
metrics.WinDescent = ReadS16Be(os2Offset + 76);
|
metrics.WinDescent = ReadS16Be(os2Offset + 76);
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics.TypoLineHeight =
|
metrics.TypoLineHeight = metrics.TypoAscender - metrics.TypoDescender + metrics.TypoLineGap;
|
||||||
metrics.TypoAscender - metrics.TypoDescender + metrics.TypoLineGap;
|
|
||||||
|
|
||||||
metrics.WinLineHeight =
|
metrics.WinLineHeight = metrics.WinAscent + metrics.WinDescent;
|
||||||
metrics.WinAscent + metrics.WinDescent;
|
|
||||||
|
|
||||||
return metrics;
|
return metrics;
|
||||||
|
|
||||||
|
|||||||
+568
-566
File diff suppressed because it is too large
Load Diff
+564
-323
@@ -1,433 +1,674 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using Hexa.NET.ImGui;
|
using Hexa.NET.ImGui;
|
||||||
using SDL3;
|
using Hexa.NET.OpenGL;
|
||||||
|
|
||||||
namespace SDL3_TestingSuite.SDL3;
|
namespace SDL3_TestingSuite.SDL3;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Implementation of ImGui for SDL3 Renderer backend.
|
|
||||||
/// https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_sdlrenderer3.h
|
|
||||||
/// </summary>
|
|
||||||
public unsafe static class ImGuiSDL3Renderer
|
public unsafe static class ImGuiSDL3Renderer
|
||||||
{
|
{
|
||||||
private sealed class TextureState
|
|
||||||
{
|
|
||||||
public ImTextureDataPtr Source;
|
|
||||||
public readonly Dictionary<nint, nint> RendererTextures = new();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class RendererData
|
public class RendererData
|
||||||
{
|
{
|
||||||
public nint Renderer; // Main viewport's renderer
|
public uint GlVersion; // e.g. 320 for GL 3.2
|
||||||
public ImVector<SDL.FColor> ColorBuffer;
|
public string GlslVersionString = ""; // e.g. "#version 330 core\n"
|
||||||
|
public bool GlProfileIsES2;
|
||||||
|
public bool GlProfileIsES3;
|
||||||
|
public bool GlProfileIsCompat;
|
||||||
|
public int GlProfileMask;
|
||||||
|
public int MaxTextureSize;
|
||||||
|
|
||||||
// Render State
|
public uint ShaderHandle;
|
||||||
public SDL.ScaleMode CurrentScaleMode;
|
public int AttribLocationTex;
|
||||||
|
public int AttribLocationProjMtx;
|
||||||
|
public uint AttribLocationVtxPos;
|
||||||
|
public uint AttribLocationVtxUV;
|
||||||
|
public uint AttribLocationVtxColor;
|
||||||
|
public uint VboHandle;
|
||||||
|
public uint ElementsHandle;
|
||||||
|
|
||||||
|
public bool HasPolygonMode;
|
||||||
|
public bool HasBindSampler;
|
||||||
|
public bool HasClipOrigin;
|
||||||
|
|
||||||
|
public uint[] TexSamplers = new uint[2]; // [0]=linear, [1]=nearest
|
||||||
|
|
||||||
|
public bool UseTexParameterToSetSampler;
|
||||||
|
public int NextSampler; // GL_LINEAR or GL_NEAREST
|
||||||
|
|
||||||
|
public GL Gl = null!;
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct BackupSDLRendererState
|
public static RendererData? GetRendererData() => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData) : null;
|
||||||
{
|
|
||||||
public SDL.Rect Viewport;
|
|
||||||
public bool ViewportEnabled;
|
|
||||||
public bool ClipEnabled;
|
|
||||||
public SDL.Rect ClipRect;
|
|
||||||
}
|
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
public static bool Init(GL gl, string? glslVersion = null)
|
||||||
private delegate void RendererCreateWindowFn(ImGuiViewportPtr viewport);
|
|
||||||
|
|
||||||
private static readonly RendererCreateWindowFn RendererCreateWindowDelegate = RendererCreateWindow;
|
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
|
||||||
private delegate void RendererDestroyWindowFn(ImGuiViewportPtr viewport);
|
|
||||||
|
|
||||||
private static readonly RendererDestroyWindowFn RendererDestroyWindowDelegate = RendererDestroyWindow;
|
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
|
||||||
private delegate void RendererSetWindowSizeFn(ImGuiViewportPtr viewport, Vector2 size);
|
|
||||||
|
|
||||||
private static readonly RendererSetWindowSizeFn RendererSetWindowSizeDelegate = RendererSetWindowSize;
|
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
|
||||||
private delegate void RendererRenderWindowFn(ImGuiViewportPtr viewport);
|
|
||||||
|
|
||||||
private static readonly RendererRenderWindowFn RendererRenderWindowDelegate = RendererRenderWindow;
|
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
|
||||||
private delegate void RendererSwapBuffersFn(ImGuiViewportPtr viewport);
|
|
||||||
|
|
||||||
private static readonly RendererSwapBuffersFn RendererSwapBuffersDelegate = RendererSwapBuffers;
|
|
||||||
|
|
||||||
public static RendererData Data => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData)! : null!;
|
|
||||||
|
|
||||||
public static bool Init(nint renderer)
|
|
||||||
{
|
{
|
||||||
ImGuiIOPtr io = ImGui.GetIO();
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
|
Debug.Assert(io.BackendRendererUserData == null, "Already initialised a renderer backend!");
|
||||||
|
|
||||||
RendererData bd = new RendererData();
|
RendererData bd = new RendererData { Gl = gl };
|
||||||
io.BackendRendererUserData = ImGuiUserData<RendererData>.Store(bd);
|
io.BackendRendererUserData = ImGuiUserData<RendererData>.Store(bd);
|
||||||
io.BackendRendererName = (byte*)Marshal.StringToHGlobalAnsi("NepImGuiSDL3Renderer");
|
io.BackendRendererName = (byte*)Marshal.StringToHGlobalAnsi("imgui_impl_opengl3_cs");
|
||||||
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset; // We can honor the ImDrawCmd.VtxOffset field, allowing for large meshes.
|
|
||||||
io.BackendFlags |= ImGuiBackendFlags.RendererHasTextures; // We can honor ImGuiPlatformIO.Textures[] requests during render.
|
|
||||||
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports;
|
|
||||||
|
|
||||||
bd.Renderer = renderer;
|
string? glVersionStr = Marshal.PtrToStringAnsi((nint)gl.GetString(GLStringName.Version));
|
||||||
|
|
||||||
|
if (glVersionStr != null && glVersionStr.StartsWith("OpenGL ES 2", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
bd.GlVersion = 200;
|
||||||
|
bd.GlProfileIsES2 = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gl.GetIntegerv(GLGetPName.MajorVersion, out int major);
|
||||||
|
gl.GetIntegerv(GLGetPName.MinorVersion, out int minor);
|
||||||
|
if (major == 0 && minor == 0 && glVersionStr != null)
|
||||||
|
{
|
||||||
|
string[] parts = glVersionStr.Split('.');
|
||||||
|
if (parts.Length >= 2)
|
||||||
|
{
|
||||||
|
int.TryParse(parts[0], out major);
|
||||||
|
int.TryParse(new string(parts[1].TakeWhile(char.IsDigit).ToArray()), out minor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bd.GlVersion = (uint)(major * 100 + minor * 10);
|
||||||
|
|
||||||
|
if (glVersionStr != null && glVersionStr.StartsWith("OpenGL ES 3", StringComparison.Ordinal))
|
||||||
|
bd.GlProfileIsES3 = true;
|
||||||
|
|
||||||
|
gl.GetIntegerv(GLGetPName.MaxTextureSize, out bd.MaxTextureSize);
|
||||||
|
|
||||||
|
if (!bd.GlProfileIsES3 && bd.GlVersion >= 320)
|
||||||
|
{
|
||||||
|
gl.GetIntegerv(GLGetPName.ContextProfileMask, out bd.GlProfileMask);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bd.GlVersion >= 320)
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset; // large meshes
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags.RendererHasTextures; // dynamic font atlas
|
||||||
|
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports; // multi-viewport
|
||||||
|
|
||||||
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||||
platformIO.RendererCreateWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererCreateWindowDelegate);
|
platformIO.RendererTextureMaxWidth = bd.MaxTextureSize;
|
||||||
platformIO.RendererDestroyWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererDestroyWindowDelegate);
|
platformIO.RendererTextureMaxHeight = bd.MaxTextureSize;
|
||||||
platformIO.RendererSetWindowSize = (void*)Marshal.GetFunctionPointerForDelegate(RendererSetWindowSizeDelegate);
|
|
||||||
platformIO.RendererRenderWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererRenderWindowDelegate);
|
if (glslVersion == null)
|
||||||
platformIO.RendererSwapBuffers = (void*)Marshal.GetFunctionPointerForDelegate(RendererSwapBuffersDelegate);
|
{
|
||||||
|
if (bd.GlProfileIsES2) glslVersion = "#version 100";
|
||||||
|
else if (bd.GlProfileIsES3) glslVersion = "#version 300 es";
|
||||||
|
else if (OperatingSystem.IsMacOS()) glslVersion = "#version 150";
|
||||||
|
else glslVersion = "#version 130";
|
||||||
|
}
|
||||||
|
bd.GlslVersionString = glslVersion + "\n";
|
||||||
|
|
||||||
|
bd.HasPolygonMode = !bd.GlProfileIsES2 && !bd.GlProfileIsES3;
|
||||||
|
bd.HasBindSampler = bd.GlVersion >= 330 || bd.GlProfileIsES3;
|
||||||
|
bd.HasClipOrigin = bd.GlVersion >= 450;
|
||||||
|
|
||||||
|
if (!bd.HasClipOrigin && !bd.GlProfileIsES2 && !bd.GlProfileIsES3)
|
||||||
|
{
|
||||||
|
gl.GetIntegerv(GLGetPName.NumExtensions, out int numExt);
|
||||||
|
for (uint i = 0; i < numExt; i++)
|
||||||
|
{
|
||||||
|
string? ext = Marshal.PtrToStringAnsi((nint)gl.GetStringi(GLStringName.Extensions, i));
|
||||||
|
if (ext == "GL_ARB_clip_control")
|
||||||
|
{
|
||||||
|
bd.HasClipOrigin = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InitMultiViewportSupport();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Dispose()
|
public static void Dispose()
|
||||||
{
|
{
|
||||||
var io = ImGui.GetIO();
|
RendererData? bd = GetRendererData();
|
||||||
var platformIO = ImGui.GetPlatformIO();
|
Debug.Assert(bd != null, "No renderer backend to shut down, or already shut down?");
|
||||||
|
|
||||||
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
|
|
||||||
|
ShutdownMultiViewportSupport();
|
||||||
DestroyDeviceObjects();
|
DestroyDeviceObjects();
|
||||||
|
|
||||||
|
if (io.BackendRendererName != null)
|
||||||
|
Marshal.FreeHGlobal((nint)io.BackendRendererName);
|
||||||
|
|
||||||
|
ImGuiUserData<RendererData>.Free(io.BackendRendererUserData);
|
||||||
io.BackendRendererName = null;
|
io.BackendRendererName = null;
|
||||||
io.BackendRendererUserData = null;
|
io.BackendRendererUserData = null;
|
||||||
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures);
|
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures | ImGuiBackendFlags.RendererHasViewports);
|
||||||
platformIO.RendererTextureMaxWidth = 0;
|
|
||||||
platformIO.RendererTextureMaxHeight = 0;
|
|
||||||
platformIO.RendererRenderState = null;
|
|
||||||
platformIO.RendererCreateWindow = null;
|
|
||||||
platformIO.RendererDestroyWindow = null;
|
|
||||||
platformIO.RendererSetWindowSize = null;
|
|
||||||
platformIO.RendererRenderWindow = null;
|
|
||||||
platformIO.RendererSwapBuffers = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void NewFrame() { }
|
public static void NewFrame()
|
||||||
|
|
||||||
private static void RendererCreateWindow(ImGuiViewportPtr viewport)
|
|
||||||
{
|
{
|
||||||
Program.Logger.Log(null);
|
RendererData? bd = GetRendererData();
|
||||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
Debug.Assert(bd != null, "Context or backend not initialised! Did you call ImGuiOpenGL3Renderer.Init()?");
|
||||||
if (vd == null || vd.Window == nint.Zero)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (vd.Renderer == nint.Zero)
|
if (bd.ShaderHandle == 0)
|
||||||
{
|
{
|
||||||
vd.Renderer = SDL.CreateRenderer(vd.Window, (string?)null);
|
bool ok = CreateDeviceObjects();
|
||||||
vd.RendererOwned = true;
|
Debug.Assert(ok, "ImGuiOpenGL3Renderer.CreateDeviceObjects() failed!");
|
||||||
}
|
}
|
||||||
Program.Logger.Log(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RendererDestroyWindow(ImGuiViewportPtr viewport)
|
public static void RenderDrawData(ImDrawDataPtr drawData)
|
||||||
{
|
{
|
||||||
Program.Logger.Log(null);
|
if (drawData.Handle == null) return;
|
||||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
|
||||||
if (vd == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (vd.RendererOwned && vd.Renderer != nint.Zero)
|
int fbWidth = (int)(drawData.DisplaySize.X * drawData.FramebufferScale.X);
|
||||||
|
int fbHeight = (int)(drawData.DisplaySize.Y * drawData.FramebufferScale.Y);
|
||||||
|
if (fbWidth <= 0 || fbHeight <= 0) return;
|
||||||
|
|
||||||
|
RendererData bd = GetRendererData()!;
|
||||||
|
GL gl = bd.Gl;
|
||||||
|
|
||||||
|
if (drawData.Textures.Size > 0)
|
||||||
{
|
{
|
||||||
SDL.DestroyRenderer(vd.Renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
vd.Renderer = nint.Zero;
|
|
||||||
vd.RendererOwned = false;
|
|
||||||
Program.Logger.Log(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void RendererSetWindowSize(ImGuiViewportPtr viewport, Vector2 size)
|
|
||||||
{
|
|
||||||
// SDL renderer windows track size through the platform window callback.
|
|
||||||
Program.Logger.Log(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void RendererRenderWindow(ImGuiViewportPtr viewport)
|
|
||||||
{
|
|
||||||
Program.Logger.Log(null);
|
|
||||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
|
||||||
if (vd == null || vd.Renderer == nint.Zero)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderDrawData(viewport.DrawData, vd.Renderer);
|
|
||||||
Program.Logger.Log(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void RendererSwapBuffers(ImGuiViewportPtr viewport)
|
|
||||||
{
|
|
||||||
Program.Logger.Log(null);
|
|
||||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
|
||||||
if (vd == null || vd.Renderer == nint.Zero)
|
|
||||||
return;
|
|
||||||
|
|
||||||
SDL.RenderPresent(vd.Renderer);
|
|
||||||
Program.Logger.Log(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void RenderDrawData(ImDrawDataPtr drawData, nint renderer)
|
|
||||||
{
|
|
||||||
// Skip if no data to render
|
|
||||||
if (drawData.Handle == null || drawData.CmdListsCount == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
SDL.GetRenderScale(renderer, out float renderScaleX, out float renderScaleY);
|
|
||||||
Vector2 renderScale = new Vector2(
|
|
||||||
renderScaleX == 1.0f ? drawData.FramebufferScale.X : 1.0f,
|
|
||||||
renderScaleY == 1.0f ? drawData.FramebufferScale.Y : 1.0f);
|
|
||||||
|
|
||||||
int fbWidth = (int)(drawData.DisplaySize.X * renderScale.X);
|
|
||||||
int fbHeight = (int)(drawData.DisplaySize.Y * renderScale.Y);
|
|
||||||
if (fbWidth <= 0 || fbHeight <= 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
for (int i = 0; i < drawData.Textures.Size; i++)
|
for (int i = 0; i < drawData.Textures.Size; i++)
|
||||||
{
|
{
|
||||||
var texture = drawData.Textures[i];
|
ImTextureDataPtr tex = drawData.Textures[i];
|
||||||
UpdateTexture(texture, renderer);
|
if (tex.Status != ImTextureStatus.Ok)
|
||||||
|
UpdateTexture(tex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backup SDL renderer state
|
gl.GetIntegerv(GLGetPName.ActiveTexture, out int lastActiveTexture);
|
||||||
BackupSDLRendererState old = new BackupSDLRendererState
|
gl.ActiveTexture(GLTextureUnit.Texture0);
|
||||||
|
gl.GetIntegerv(GLGetPName.CurrentProgram, out int lastProgram);
|
||||||
|
gl.GetIntegerv(GLGetPName.TextureBinding2D, out int lastTexture);
|
||||||
|
gl.GetIntegerv(GLGetPName.SamplerBinding, out int lastSampler);
|
||||||
|
gl.GetIntegerv(GLGetPName.ArrayBufferBinding, out int lastArrayBuffer);
|
||||||
|
gl.GetIntegerv(GLGetPName.VertexArrayBinding, out int lastVertexArray);
|
||||||
|
int[] lastPolygonMode = new int[2];
|
||||||
|
if (bd.HasPolygonMode) gl.GetIntegerv(GLGetPName.PolygonMode, lastPolygonMode);
|
||||||
|
int[] lastViewport = new int[4];
|
||||||
|
gl.GetIntegerv(GLGetPName.Viewport, lastViewport);
|
||||||
|
int[] lastScissorBox = new int[4];
|
||||||
|
gl.GetIntegerv(GLGetPName.ScissorBox, lastScissorBox);
|
||||||
|
|
||||||
|
gl.GetIntegerv(GLGetPName.BlendSrcRgb, out int lastBlendSrcRgb);
|
||||||
|
gl.GetIntegerv(GLGetPName.BlendDstRgb, out int lastBlendDstRgb);
|
||||||
|
gl.GetIntegerv(GLGetPName.BlendSrcAlpha, out int lastBlendSrcAlpha);
|
||||||
|
gl.GetIntegerv(GLGetPName.BlendDstAlpha, out int lastBlendDstAlpha);
|
||||||
|
gl.GetIntegerv(GLGetPName.BlendEquationRgb, out int lastBlendEqRgb);
|
||||||
|
gl.GetIntegerv(GLGetPName.BlendEquationAlpha, out int lastBlendEqAlpha);
|
||||||
|
|
||||||
|
bool lastBlend = gl.IsEnabled(GLEnableCap.Blend);
|
||||||
|
bool lastCullFace = gl.IsEnabled(GLEnableCap.CullFace);
|
||||||
|
bool lastDepthTest = gl.IsEnabled(GLEnableCap.DepthTest);
|
||||||
|
bool lastStencilTest = gl.IsEnabled(GLEnableCap.StencilTest);
|
||||||
|
bool lastScissorTest = gl.IsEnabled(GLEnableCap.ScissorTest);
|
||||||
|
bool lastPrimRestart = bd.GlVersion >= 310 && !bd.GlProfileIsES3 && gl.IsEnabled(GLEnableCap.PrimitiveRestart);
|
||||||
|
|
||||||
|
uint vao = 0;
|
||||||
|
gl.GenVertexArrays(1, ref vao);
|
||||||
|
SetupRenderState(drawData, fbWidth, fbHeight, vao);
|
||||||
|
|
||||||
|
Vector2 clipOff = drawData.DisplayPos;
|
||||||
|
Vector2 clipScale = drawData.FramebufferScale;
|
||||||
|
|
||||||
|
for (int listIdx = 0; listIdx < drawData.CmdListsCount; listIdx++)
|
||||||
{
|
{
|
||||||
ViewportEnabled = SDL.RenderViewportSet(renderer),
|
ImDrawListPtr drawList = drawData.CmdLists[listIdx];
|
||||||
ClipEnabled = SDL.RenderClipEnabled(renderer)
|
|
||||||
};
|
|
||||||
SDL.GetRenderViewport(renderer, out var oldViewport);
|
|
||||||
old.Viewport = oldViewport;
|
|
||||||
SDL.GetRenderClipRect(renderer, out var oldClipRect);
|
|
||||||
old.ClipRect = oldClipRect;
|
|
||||||
|
|
||||||
// Set up render state
|
int vtxSize = drawList.VtxBuffer.Size * sizeof(ImDrawVert);
|
||||||
SDL.SetRenderViewport(renderer, 0);
|
int idxSize = drawList.IdxBuffer.Size * sizeof(ushort);
|
||||||
SDL.SetRenderClipRect(renderer, nint.Zero);
|
|
||||||
|
|
||||||
// Set render state in platform IO
|
gl.BufferData(GLBufferTargetARB.ArrayBuffer, vtxSize, drawList.VtxBuffer.Data, GLBufferUsageARB.StreamDraw);
|
||||||
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
|
|
||||||
platformIo.RendererRenderState = (void*)renderer;
|
|
||||||
|
|
||||||
Vector2 clipOffset = drawData.DisplayPos;
|
gl.BufferData(GLBufferTargetARB.ElementArrayBuffer, idxSize, drawList.IdxBuffer.Data, GLBufferUsageARB.StreamDraw);
|
||||||
|
|
||||||
// Render command lists
|
for (int cmdIdx = 0; cmdIdx < drawList.CmdBuffer.Size; cmdIdx++)
|
||||||
for (int n = 0; n < drawData.CmdListsCount; n++)
|
|
||||||
{
|
{
|
||||||
ImDrawListPtr cmdList = drawData.CmdLists[n];
|
ImDrawCmd cmd = drawList.CmdBuffer[cmdIdx];
|
||||||
|
|
||||||
for (int cmdIndex = 0; cmdIndex < cmdList.CmdBuffer.Size; cmdIndex++)
|
|
||||||
{
|
|
||||||
ImDrawCmd cmd = cmdList.CmdBuffer[cmdIndex];
|
|
||||||
|
|
||||||
if (cmd.UserCallback != null)
|
if (cmd.UserCallback != null)
|
||||||
{
|
{
|
||||||
continue; // User callback not implemented
|
|
||||||
}
|
nint cbPtr = (nint)cmd.UserCallback;
|
||||||
|
if (cbPtr == (nint)DelegateStorage.DrawCallbackResetRenderStateDelegate.GetPtrForDelegate())
|
||||||
|
SetupRenderState(drawData, fbWidth, fbHeight, vao);
|
||||||
|
else if (cbPtr == (nint)DelegateStorage.DrawCallbackSetSamplerLinearDelegate.GetPtrForDelegate())
|
||||||
|
ApplySamplerLinear(bd);
|
||||||
|
else if (cbPtr == (nint)DelegateStorage.DrawCallbackSetSamplerNearestDelegate.GetPtrForDelegate())
|
||||||
|
ApplySamplerNearest(bd);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Apply clipping rectangle
|
ImDrawCmdPtr cmdPtr = new ImDrawCmdPtr { Handle = &cmd };
|
||||||
Vector4 clipRect = cmd.ClipRect;
|
((delegate* unmanaged[Cdecl]<ImDrawListPtr, ImDrawCmdPtr, void>)cmd.UserCallback)(drawList, cmdPtr);
|
||||||
Vector2 clipMin = new Vector2((clipRect.X - clipOffset.X) * renderScale.X, (clipRect.Y - clipOffset.Y) * renderScale.Y);
|
}
|
||||||
Vector2 clipMax = new Vector2((clipRect.Z - clipOffset.X) * renderScale.X, (clipRect.W - clipOffset.Y) * renderScale.Y);
|
|
||||||
|
|
||||||
clipMin.X = Math.Max(0, clipMin.X);
|
|
||||||
clipMin.Y = Math.Max(0, clipMin.Y);
|
|
||||||
clipMax.X = Math.Min(fbWidth, clipMax.X);
|
|
||||||
clipMax.Y = Math.Min(fbHeight, clipMax.Y);
|
|
||||||
if (clipMax.X <= clipMin.X || clipMax.Y <= clipMin.Y)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
SDL.Rect r = new SDL.Rect
|
|
||||||
{
|
|
||||||
X = (int)clipMin.X,
|
|
||||||
Y = (int)clipMin.Y,
|
|
||||||
W = (int)(clipMax.X - clipMin.X),
|
|
||||||
H = (int)(clipMax.Y - clipMin.Y)
|
|
||||||
};
|
|
||||||
SDL.SetRenderClipRect(renderer, r);
|
|
||||||
|
|
||||||
// Get texture
|
|
||||||
nint texId = ResolveTextureId(cmd.GetTexID(), renderer);
|
|
||||||
|
|
||||||
// Convert ImGui vertices to SDL vertices
|
|
||||||
if (!RenderDrawCommand(cmdList, cmd, renderer, texId, renderScale))
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Failed to render ImGui draw command: {SDL.GetError()}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset render state
|
float clipMinX = (cmd.ClipRect.X - clipOff.X) * clipScale.X;
|
||||||
platformIo.RendererRenderState = null;
|
float clipMinY = (cmd.ClipRect.Y - clipOff.Y) * clipScale.Y;
|
||||||
|
float clipMaxX = (cmd.ClipRect.Z - clipOff.X) * clipScale.X;
|
||||||
|
float clipMaxY = (cmd.ClipRect.W - clipOff.Y) * clipScale.Y;
|
||||||
|
if (clipMaxX <= clipMinX || clipMaxY <= clipMinY) continue;
|
||||||
|
|
||||||
// Restore renderer state
|
gl.Scissor((int)clipMinX, fbHeight - (int)clipMaxY, (int)(clipMaxX - clipMinX), (int)(clipMaxY - clipMinY));
|
||||||
SDL.SetRenderViewport(renderer, old.ViewportEnabled ? old.Viewport : new SDL.Rect());
|
|
||||||
SDL.SetRenderClipRect(renderer, old.ClipEnabled ? old.ClipRect : new SDL.Rect());
|
gl.BindTexture(GLTextureTarget.Texture2D, (uint)(nint)cmd.GetTexID());
|
||||||
|
|
||||||
|
if (!bd.HasBindSampler && bd.UseTexParameterToSetSampler)
|
||||||
|
{
|
||||||
|
gl.TexParameterf(GLTextureTarget.Texture2D, GLTextureParameterName.MinFilter, bd.NextSampler);
|
||||||
|
gl.TexParameterf(GLTextureTarget.Texture2D, GLTextureParameterName.MagFilter, bd.NextSampler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static nint ResolveTextureId(ImTextureID texId, nint renderer)
|
if (bd.GlVersion >= 320)
|
||||||
{
|
{
|
||||||
if (texId == ImTextureID.Null)
|
gl.DrawElementsBaseVertex(GLPrimitiveType.Triangles, (int)cmd.ElemCount, GLDrawElementsType.UnsignedShort, (void*)(cmd.IdxOffset * sizeof(ushort)), (int)cmd.VtxOffset);
|
||||||
return nint.Zero;
|
|
||||||
|
|
||||||
TextureState? state = ImGuiUserData<TextureState>.Get((void*)(nint)texId);
|
|
||||||
if (state == null)
|
|
||||||
return (nint)texId;
|
|
||||||
|
|
||||||
if (!state.RendererTextures.TryGetValue(renderer, out nint rendererTexture) || rendererTexture == nint.Zero)
|
|
||||||
{
|
|
||||||
rendererTexture = CreateRendererTexture(state, renderer);
|
|
||||||
state.RendererTextures[renderer] = rendererTexture;
|
|
||||||
}
|
|
||||||
|
|
||||||
return rendererTexture;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static nint CreateRendererTexture(TextureState state, nint renderer)
|
|
||||||
{
|
|
||||||
ImTextureDataPtr tex = state.Source;
|
|
||||||
// We keep ARGB8888 here because this project already relies on that upload path.
|
|
||||||
nint sdlTexture = SDL.CreateTexture(renderer, SDL.PixelFormat.ARGB8888, SDL.TextureAccess.Static, tex.Width, tex.Height);
|
|
||||||
if (sdlTexture == nint.Zero)
|
|
||||||
return nint.Zero;
|
|
||||||
|
|
||||||
SDL.UpdateTexture(sdlTexture, nint.Zero, (nint)tex.GetPixels(), tex.GetPitch());
|
|
||||||
SDL.SetTextureBlendMode(sdlTexture, SDL.BlendMode.Blend);
|
|
||||||
SDL.SetTextureScaleMode(sdlTexture, SDL.ScaleMode.Linear);
|
|
||||||
return sdlTexture;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void UploadRendererTexture(ImTextureDataPtr tex, nint renderer, nint sdlTexture)
|
|
||||||
{
|
|
||||||
if (tex.Status == ImTextureStatus.WantUpdates)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < tex.Updates.Size; i++)
|
|
||||||
{
|
|
||||||
var r = tex.Updates[i];
|
|
||||||
SDL.Rect rect = new SDL.Rect
|
|
||||||
{
|
|
||||||
X = r.X,
|
|
||||||
Y = r.Y,
|
|
||||||
W = r.W,
|
|
||||||
H = r.H
|
|
||||||
};
|
|
||||||
SDL.UpdateTexture(sdlTexture, rect, (nint)tex.GetPixelsAt(r.X, r.Y), tex.GetPitch());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SDL.UpdateTexture(sdlTexture, nint.Zero, (nint)tex.GetPixels(), tex.GetPitch());
|
gl.DrawElements(GLPrimitiveType.Triangles, (int)cmd.ElemCount, GLDrawElementsType.UnsignedShort, (void*)(cmd.IdxOffset * sizeof(ushort)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void UpdateTexture(ImTextureDataPtr tex, nint renderer)
|
gl.DeleteVertexArray(vao);
|
||||||
|
|
||||||
|
if (lastProgram == 0 || gl.IsProgram((uint)lastProgram))
|
||||||
|
gl.UseProgram((uint)lastProgram);
|
||||||
|
gl.BindTexture(GLTextureTarget.Texture2D, (uint)lastTexture);
|
||||||
|
if (bd.HasBindSampler) gl.BindSampler(0, (uint)lastSampler);
|
||||||
|
gl.ActiveTexture((GLTextureUnit)lastActiveTexture);
|
||||||
|
gl.BindVertexArray((uint)lastVertexArray);
|
||||||
|
gl.BindBuffer(GLBufferTargetARB.ArrayBuffer, (uint)lastArrayBuffer);
|
||||||
|
gl.BlendEquationSeparate((GLBlendEquationModeEXT)lastBlendEqRgb, (GLBlendEquationModeEXT)lastBlendEqAlpha);
|
||||||
|
gl.BlendFuncSeparate((GLBlendingFactor)lastBlendSrcRgb, (GLBlendingFactor)lastBlendDstRgb, (GLBlendingFactor)lastBlendSrcAlpha, (GLBlendingFactor)lastBlendDstAlpha);
|
||||||
|
|
||||||
|
SetCap(gl, GLEnableCap.Blend, lastBlend);
|
||||||
|
SetCap(gl, GLEnableCap.CullFace, lastCullFace);
|
||||||
|
SetCap(gl, GLEnableCap.DepthTest, lastDepthTest);
|
||||||
|
SetCap(gl, GLEnableCap.StencilTest, lastStencilTest);
|
||||||
|
SetCap(gl, GLEnableCap.ScissorTest, lastScissorTest);
|
||||||
|
if (bd.GlVersion >= 310 && !bd.GlProfileIsES3)
|
||||||
|
SetCap(gl, GLEnableCap.PrimitiveRestart, lastPrimRestart);
|
||||||
|
|
||||||
|
if (bd.HasPolygonMode)
|
||||||
{
|
{
|
||||||
TextureState? state = null;
|
if (bd.GlVersion <= 310 || bd.GlProfileIsCompat)
|
||||||
if ((nint)tex.BackendUserData != nint.Zero)
|
|
||||||
state = ImGuiUserData<TextureState>.Get(tex.BackendUserData);
|
|
||||||
|
|
||||||
if (state == null)
|
|
||||||
{
|
{
|
||||||
state = new TextureState();
|
gl.PolygonMode(GLTriangleFace.Front, (GLPolygonMode)lastPolygonMode[0]);
|
||||||
tex.BackendUserData = ImGuiUserData<TextureState>.Store(state);
|
gl.PolygonMode(GLTriangleFace.Back, (GLPolygonMode)lastPolygonMode[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gl.PolygonMode(GLTriangleFace.FrontAndBack, (GLPolygonMode)lastPolygonMode[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state.Source = tex;
|
gl.Viewport(lastViewport[0], lastViewport[1], lastViewport[2], lastViewport[3]);
|
||||||
tex.SetTexID((nint)tex.BackendUserData);
|
gl.Scissor(lastScissorBox[0], lastScissorBox[1], lastScissorBox[2], lastScissorBox[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void SetupRenderState(ImDrawDataPtr drawData, int fbWidth, int fbHeight, uint vao)
|
||||||
|
{
|
||||||
|
RendererData bd = GetRendererData()!;
|
||||||
|
GL gl = bd.Gl;
|
||||||
|
|
||||||
|
gl.Enable(GLEnableCap.Blend);
|
||||||
|
gl.BlendEquation(GLBlendEquationModeEXT.FuncAdd);
|
||||||
|
gl.BlendFuncSeparate(GLBlendingFactor.SrcAlpha, GLBlendingFactor.OneMinusSrcAlpha, GLBlendingFactor.One, GLBlendingFactor.OneMinusSrcAlpha);
|
||||||
|
gl.Disable(GLEnableCap.CullFace);
|
||||||
|
gl.Disable(GLEnableCap.DepthTest);
|
||||||
|
gl.Disable(GLEnableCap.StencilTest);
|
||||||
|
gl.Enable(GLEnableCap.ScissorTest);
|
||||||
|
|
||||||
|
if (bd.GlVersion >= 310 && !bd.GlProfileIsES3)
|
||||||
|
gl.Disable(GLEnableCap.PrimitiveRestart);
|
||||||
|
|
||||||
|
if (bd.HasPolygonMode)
|
||||||
|
gl.PolygonMode(GLTriangleFace.FrontAndBack, GLPolygonMode.Fill);
|
||||||
|
|
||||||
|
gl.Viewport(0, 0, fbWidth, fbHeight);
|
||||||
|
|
||||||
|
float L = drawData.DisplayPos.X;
|
||||||
|
float R = drawData.DisplayPos.X + drawData.DisplaySize.X;
|
||||||
|
float T = drawData.DisplayPos.Y;
|
||||||
|
float B = drawData.DisplayPos.Y + drawData.DisplaySize.Y;
|
||||||
|
|
||||||
|
if (bd.HasClipOrigin)
|
||||||
|
{
|
||||||
|
gl.GetIntegerv(GLGetPName.ClipPlane0, out int clipOrigin);
|
||||||
|
if (clipOrigin == (int)GLEnum.UpperLeft)
|
||||||
|
{
|
||||||
|
(T, B) = (B, T);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float[] ortho =
|
||||||
|
{
|
||||||
|
2.0f / (R - L), 0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 2.0f / (T - B), 0.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, -1.0f, 0.0f,
|
||||||
|
(R + L) / (L - R), (T + B) / (B - T), 0.0f, 1.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
gl.UseProgram(bd.ShaderHandle);
|
||||||
|
gl.Uniform1f(bd.AttribLocationTex, 0);
|
||||||
|
gl.UniformMatrix4fv(bd.AttribLocationProjMtx, 1, false, ortho);
|
||||||
|
|
||||||
|
if (bd.HasBindSampler)
|
||||||
|
gl.BindSampler(0, bd.TexSamplers[0]); // linear
|
||||||
|
|
||||||
|
gl.BindVertexArray(vao);
|
||||||
|
gl.BindBuffer(GLBufferTargetARB.ArrayBuffer, bd.VboHandle);
|
||||||
|
gl.BindBuffer(GLBufferTargetARB.ElementArrayBuffer, bd.ElementsHandle);
|
||||||
|
|
||||||
|
gl.EnableVertexAttribArray(bd.AttribLocationVtxPos);
|
||||||
|
gl.VertexAttribPointer(bd.AttribLocationVtxPos, 2, GLVertexAttribPointerType.Float, false, sizeof(ImDrawVert), (void*)0);
|
||||||
|
|
||||||
|
gl.EnableVertexAttribArray(bd.AttribLocationVtxUV);
|
||||||
|
gl.VertexAttribPointer(bd.AttribLocationVtxUV, 2, GLVertexAttribPointerType.Float, false, sizeof(ImDrawVert), (void*)8);
|
||||||
|
|
||||||
|
gl.EnableVertexAttribArray(bd.AttribLocationVtxColor);
|
||||||
|
gl.VertexAttribPointer(bd.AttribLocationVtxColor, 4, GLVertexAttribPointerType.UnsignedByte, true, sizeof(ImDrawVert), (void*)16);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void UpdateTexture(ImTextureDataPtr tex)
|
||||||
|
{
|
||||||
|
RendererData bd = GetRendererData()!;
|
||||||
|
GL gl = bd.Gl;
|
||||||
|
|
||||||
if (tex.Status == ImTextureStatus.WantDestroy)
|
if (tex.Status == ImTextureStatus.WantDestroy)
|
||||||
{
|
{
|
||||||
foreach (nint rendererTexture in state.RendererTextures.Values)
|
if (tex.TexID != ImTextureID.Null)
|
||||||
{
|
{
|
||||||
if (rendererTexture != nint.Zero)
|
uint glId = (uint)(nint)tex.TexID;
|
||||||
SDL.DestroyTexture(rendererTexture);
|
gl.DeleteTexture(glId);
|
||||||
}
|
}
|
||||||
|
|
||||||
state.RendererTextures.Clear();
|
|
||||||
ImGuiUserData<TextureState>.Free(tex.BackendUserData);
|
|
||||||
tex.BackendUserData = (void*)nint.Zero;
|
|
||||||
tex.SetTexID(ImTextureID.Null);
|
tex.SetTexID(ImTextureID.Null);
|
||||||
tex.SetStatus(ImTextureStatus.Destroyed);
|
tex.SetStatus(ImTextureStatus.Destroyed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasRendererTexture = state.RendererTextures.TryGetValue(renderer, out nint sdlTexture) && sdlTexture != nint.Zero;
|
gl.PixelStoref(GLPixelStoreParameter.UnpackRowLength, 0);
|
||||||
if (!hasRendererTexture)
|
gl.PixelStoref(GLPixelStoreParameter.UnpackAlignment, 1);
|
||||||
{
|
|
||||||
sdlTexture = CreateRendererTexture(state, renderer);
|
|
||||||
state.RendererTextures[renderer] = sdlTexture;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sdlTexture != nint.Zero && (tex.Status == ImTextureStatus.WantCreate || tex.Status == ImTextureStatus.WantUpdates || !hasRendererTexture))
|
if (tex.Status == ImTextureStatus.WantCreate)
|
||||||
{
|
{
|
||||||
UploadRendererTexture(tex, renderer, sdlTexture);
|
Debug.Assert(tex.Format == ImTextureFormat.Rgba32);
|
||||||
|
|
||||||
|
gl.GetIntegerv(GLGetPName.TextureBinding2D, out int lastTex);
|
||||||
|
uint glTexId = 0;
|
||||||
|
gl.GenTextures(1, ref glTexId);
|
||||||
|
gl.BindTexture(GLTextureTarget.Texture2D, glTexId);
|
||||||
|
gl.TexParameterf(GLTextureTarget.Texture2D, GLTextureParameterName.MinFilter, (int)GLEnum.Linear);
|
||||||
|
gl.TexParameterf(GLTextureTarget.Texture2D, GLTextureParameterName.MagFilter, (int)GLEnum.Linear);
|
||||||
|
gl.TexParameterf(GLTextureTarget.Texture2D, GLTextureParameterName.WrapS, (int)GLEnum.ClampToEdge);
|
||||||
|
gl.TexParameterf(GLTextureTarget.Texture2D, GLTextureParameterName.WrapT, (int)GLEnum.ClampToEdge);
|
||||||
|
gl.TexImage2D(GLTextureTarget.Texture2D, 0, GLInternalFormat.Rgba, tex.Width, tex.Height, 0, GLPixelFormat.Rgba, GLPixelType.UnsignedByte, tex.GetPixels());
|
||||||
|
|
||||||
|
tex.SetTexID((nint)glTexId);
|
||||||
tex.SetStatus(ImTextureStatus.Ok);
|
tex.SetStatus(ImTextureStatus.Ok);
|
||||||
|
gl.BindTexture(GLTextureTarget.Texture2D, (uint)lastTex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (tex.Status == ImTextureStatus.WantUpdates)
|
||||||
|
{
|
||||||
|
gl.GetIntegerv(GLGetPName.TextureBinding2D, out int lastTex);
|
||||||
|
uint glTexId = (uint)(nint)tex.TexID;
|
||||||
|
gl.BindTexture(GLTextureTarget.Texture2D, glTexId);
|
||||||
|
|
||||||
|
for (int i = 0; i < tex.Updates.Size; i++)
|
||||||
|
{
|
||||||
|
ImTextureRect r = tex.Updates[i];
|
||||||
|
gl.PixelStoref(GLPixelStoreParameter.UnpackRowLength, tex.Width);
|
||||||
|
gl.TexSubImage2D(GLTextureTarget.Texture2D, 0, r.X, r.Y, r.W, r.H, GLPixelFormat.Rgba, GLPixelType.UnsignedByte, tex.GetPixelsAt(r.X, r.Y));
|
||||||
|
}
|
||||||
|
gl.PixelStoref(GLPixelStoreParameter.UnpackRowLength, 0);
|
||||||
|
|
||||||
|
tex.SetStatus(ImTextureStatus.Ok);
|
||||||
|
gl.BindTexture(GLTextureTarget.Texture2D, (uint)lastTex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool RenderDrawCommand(ImDrawListPtr drawList, ImDrawCmd cmd, nint renderer, nint texId, Vector2 scale)
|
|
||||||
{
|
|
||||||
uint indexOffset = cmd.IdxOffset;
|
|
||||||
uint vertexOffset = cmd.VtxOffset;
|
|
||||||
uint elemCount = cmd.ElemCount;
|
|
||||||
|
|
||||||
SDL.Vertex[] vertices = new SDL.Vertex[elemCount];
|
public static bool CreateDeviceObjects()
|
||||||
int[] indices = new int[elemCount];
|
|
||||||
|
|
||||||
for (int i = 0; i < elemCount; i++)
|
|
||||||
{
|
{
|
||||||
ushort idx = drawList.IdxBuffer[(int)indexOffset + i];
|
RendererData bd = GetRendererData()!;
|
||||||
int vertIdx = (int)(vertexOffset + idx);
|
GL gl = bd.Gl;
|
||||||
|
|
||||||
ImDrawVert srcVert = drawList.VtxBuffer[vertIdx];
|
gl.GetIntegerv(GLGetPName.TextureBinding2D, out int lastTexture);
|
||||||
|
gl.GetIntegerv(GLGetPName.ArrayBufferBinding, out int lastArrayBuffer);
|
||||||
|
gl.GetIntegerv(GLGetPName.VertexArrayBinding, out int lastVertexArray);
|
||||||
|
|
||||||
uint col = srcVert.Col;
|
int glslVersion = 130;
|
||||||
|
|
||||||
byte r = (byte)((col >> 0) & 0xFF);
|
|
||||||
byte g = (byte)((col >> 8) & 0xFF);
|
|
||||||
byte b = (byte)((col >> 16) & 0xFF);
|
|
||||||
byte a = (byte)((col >> 24) & 0xFF);
|
|
||||||
|
|
||||||
vertices[i] = new SDL.Vertex()
|
|
||||||
{
|
{
|
||||||
Position = new SDL.FPoint()
|
string vstr = bd.GlslVersionString.TrimStart().TrimStart('#');
|
||||||
{
|
if (vstr.StartsWith("version "))
|
||||||
X = srcVert.Pos.X * scale.X,
|
int.TryParse(vstr["version ".Length..].Split(' ')[0], out glslVersion);
|
||||||
Y = srcVert.Pos.Y * scale.Y
|
|
||||||
},
|
|
||||||
Color = new SDL.FColor()
|
|
||||||
{
|
|
||||||
R = r / 255f,
|
|
||||||
G = g / 255f,
|
|
||||||
B = b / 255f,
|
|
||||||
A = a / 255f
|
|
||||||
},
|
|
||||||
TexCoord = new SDL.FPoint()
|
|
||||||
{
|
|
||||||
X = srcVert.Uv.X,
|
|
||||||
Y = srcVert.Uv.Y
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const string vertSrc120 = "uniform mat4 ProjMtx;\n" + "attribute vec2 Position;\n" + "attribute vec2 UV;\n" + "attribute vec4 Color;\n" + "varying vec2 Frag_UV;\n" + "varying vec4 Frag_Color;\n" + "void main() {\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" + "}\n";
|
||||||
|
const string fragSrc120 = "#ifdef GL_ES\n precision mediump float;\n#endif\n" + "uniform sampler2D Texture;\n" + "varying vec2 Frag_UV;\n" + "varying vec4 Frag_Color;\n" + "void main() {\n" + " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n" + "}\n";
|
||||||
|
const string vertSrc130 = "uniform mat4 ProjMtx;\n" + "in vec2 Position;\nin vec2 UV;\nin vec4 Color;\n" + "out vec2 Frag_UV;\nout vec4 Frag_Color;\n" + "void main() {\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" + "}\n";
|
||||||
|
const string fragSrc130 = "uniform sampler2D Texture;\n" + "in vec2 Frag_UV;\nin vec4 Frag_Color;\n" + "out vec4 Out_Color;\n" + "void main() {\n" + " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" + "}\n";
|
||||||
|
const string vertSrc300es = "precision highp float;\n" + "layout (location = 0) in vec2 Position;\n" + "layout (location = 1) in vec2 UV;\n" + "layout (location = 2) in vec4 Color;\n" + "uniform mat4 ProjMtx;\n" + "out vec2 Frag_UV;\nout vec4 Frag_Color;\n" + "void main() {\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" + "}\n";
|
||||||
|
const string fragSrc300es = "precision mediump float;\n" + "uniform sampler2D Texture;\n" + "in vec2 Frag_UV;\nin vec4 Frag_Color;\n" + "layout (location = 0) out vec4 Out_Color;\n" + "void main() {\n" + " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" + "}\n";
|
||||||
|
const string vertSrc410 = "layout (location = 0) in vec2 Position;\n" + "layout (location = 1) in vec2 UV;\n" + "layout (location = 2) in vec4 Color;\n" + "uniform mat4 ProjMtx;\n" + "out vec2 Frag_UV;\nout vec4 Frag_Color;\n" + "void main() {\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" + "}\n";
|
||||||
|
const string fragSrc410 = "in vec2 Frag_UV;\nin vec4 Frag_Color;\n" + "uniform sampler2D Texture;\n" + "layout (location = 0) out vec4 Out_Color;\n" + "void main() {\n" + " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" + "}\n";
|
||||||
|
|
||||||
|
(string vertSrc, string fragSrc) = glslVersion switch
|
||||||
|
{
|
||||||
|
< 130 => (vertSrc120, fragSrc120),
|
||||||
|
300 => (vertSrc300es, fragSrc300es),
|
||||||
|
>= 410 => (vertSrc410, fragSrc410),
|
||||||
|
_ => (vertSrc130, fragSrc130),
|
||||||
};
|
};
|
||||||
|
|
||||||
indices[i] = i;
|
string fullVert = bd.GlslVersionString + vertSrc;
|
||||||
|
string fullFrag = bd.GlslVersionString + fragSrc;
|
||||||
|
|
||||||
|
uint vert = gl.CreateShader(GLShaderType.VertexShader);
|
||||||
|
gl.ShaderSource(vert, fullVert);
|
||||||
|
gl.CompileShader(vert);
|
||||||
|
if (!CheckShader(gl, vert, "vertex shader")) return false;
|
||||||
|
|
||||||
|
uint frag = gl.CreateShader(GLShaderType.FragmentShader);
|
||||||
|
gl.ShaderSource(frag, fullFrag);
|
||||||
|
gl.CompileShader(frag);
|
||||||
|
if (!CheckShader(gl, frag, "fragment shader")) return false;
|
||||||
|
|
||||||
|
bd.ShaderHandle = gl.CreateProgram();
|
||||||
|
gl.AttachShader(bd.ShaderHandle, vert);
|
||||||
|
gl.AttachShader(bd.ShaderHandle, frag);
|
||||||
|
gl.LinkProgram(bd.ShaderHandle);
|
||||||
|
if (!CheckProgram(gl, bd.ShaderHandle, "shader program")) return false;
|
||||||
|
|
||||||
|
gl.DetachShader(bd.ShaderHandle, vert);
|
||||||
|
gl.DetachShader(bd.ShaderHandle, frag);
|
||||||
|
gl.DeleteShader(vert);
|
||||||
|
gl.DeleteShader(frag);
|
||||||
|
|
||||||
|
bd.AttribLocationTex = gl.GetUniformLocation(bd.ShaderHandle, "Texture");
|
||||||
|
bd.AttribLocationProjMtx = gl.GetUniformLocation(bd.ShaderHandle, "ProjMtx");
|
||||||
|
bd.AttribLocationVtxPos = (uint)gl.GetAttribLocation(bd.ShaderHandle, "Position");
|
||||||
|
bd.AttribLocationVtxUV = (uint)gl.GetAttribLocation(bd.ShaderHandle, "UV");
|
||||||
|
bd.AttribLocationVtxColor = (uint)gl.GetAttribLocation(bd.ShaderHandle, "Color");
|
||||||
|
|
||||||
|
gl.GenBuffers(1, ref bd.VboHandle);
|
||||||
|
gl.GenBuffers(1, ref bd.ElementsHandle);
|
||||||
|
|
||||||
|
if (bd.HasBindSampler)
|
||||||
|
{
|
||||||
|
gl.GenSamplers(2, bd.TexSamplers);
|
||||||
|
for (int i = 0; i < 2; i++)
|
||||||
|
{
|
||||||
|
int filter = i == 0 ? (int)GLEnum.Linear : (int)GLEnum.Nearest;
|
||||||
|
gl.SamplerParameteri(bd.TexSamplers[i], GLSamplerParameterI.TextureMinFilter, filter);
|
||||||
|
gl.SamplerParameteri(bd.TexSamplers[i], GLSamplerParameterI.TextureMagFilter, filter);
|
||||||
|
gl.SamplerParameteri(bd.TexSamplers[i], GLSamplerParameterI.TextureWrapS, (int)GLEnum.ClampToEdge);
|
||||||
|
gl.SamplerParameteri(bd.TexSamplers[i], GLSamplerParameterI.TextureWrapT, (int)GLEnum.ClampToEdge);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return SDL.RenderGeometry(renderer, texId, vertices, vertices.Length, indices, indices.Length);
|
gl.BindTexture(GLTextureTarget.Texture2D, (uint)lastTexture);
|
||||||
}
|
gl.BindBuffer(GLBufferTargetARB.ArrayBuffer, (uint)lastArrayBuffer);
|
||||||
|
gl.BindVertexArray((uint)lastVertexArray);
|
||||||
|
|
||||||
public static void CreateDeviceObjects() { }
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static void DestroyDeviceObjects()
|
public static void DestroyDeviceObjects()
|
||||||
{
|
{
|
||||||
var texures = ImGui.GetPlatformIO().Textures;
|
RendererData? bd = GetRendererData();
|
||||||
for (int i = 0; i < texures.Size; i++)
|
if (bd == null) return;
|
||||||
|
GL gl = bd.Gl;
|
||||||
|
|
||||||
|
if (bd.HasBindSampler && bd.TexSamplers[0] != 0)
|
||||||
{
|
{
|
||||||
var texture = texures[i];
|
gl.DeleteSamplers(2, bd.TexSamplers);
|
||||||
texture.Status = ImTextureStatus.WantDestroy;
|
bd.TexSamplers[0] = bd.TexSamplers[1] = 0;
|
||||||
UpdateTexture(texture, nint.Zero);
|
}
|
||||||
|
|
||||||
|
if (bd.VboHandle != 0)
|
||||||
|
{
|
||||||
|
gl.DeleteBuffer(bd.VboHandle);
|
||||||
|
bd.VboHandle = 0;
|
||||||
|
}
|
||||||
|
if (bd.ElementsHandle != 0)
|
||||||
|
{
|
||||||
|
gl.DeleteBuffer(bd.ElementsHandle);
|
||||||
|
bd.ElementsHandle = 0;
|
||||||
|
}
|
||||||
|
if (bd.ShaderHandle != 0)
|
||||||
|
{
|
||||||
|
gl.DeleteProgram(bd.ShaderHandle);
|
||||||
|
bd.ShaderHandle = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImVector<ImTextureDataPtr> textures = ImGui.GetPlatformIO().Textures;
|
||||||
|
for (int i = 0; i < textures.Size; i++)
|
||||||
|
{
|
||||||
|
ImTextureDataPtr tex = textures[i];
|
||||||
|
if (tex.RefCount == 1)
|
||||||
|
{
|
||||||
|
tex.SetStatus(ImTextureStatus.WantDestroy);
|
||||||
|
UpdateTexture(tex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void InitMultiViewportSupport()
|
||||||
|
{
|
||||||
|
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||||
|
platformIO.RendererRenderWindow = DelegateStorage.RendererRenderWindowDelegate.GetPtrForDelegate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ShutdownMultiViewportSupport()
|
||||||
|
{
|
||||||
|
ImGui.DestroyPlatformWindows();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ApplySamplerLinear(RendererData bd)
|
||||||
|
{
|
||||||
|
if (bd.HasBindSampler) bd.Gl.BindSampler(0, bd.TexSamplers[0]);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bd.UseTexParameterToSetSampler = true;
|
||||||
|
bd.NextSampler = (int)GLEnum.Linear;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ApplySamplerNearest(RendererData bd)
|
||||||
|
{
|
||||||
|
if (bd.HasBindSampler) bd.Gl.BindSampler(0, bd.TexSamplers[1]);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bd.UseTexParameterToSetSampler = true;
|
||||||
|
bd.NextSampler = (int)GLEnum.Nearest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static bool CheckShader(GL gl, uint handle, string desc)
|
||||||
|
{
|
||||||
|
gl.GetShaderiv(handle, GLShaderParameterName.CompileStatus, out int status);
|
||||||
|
gl.GetShaderiv(handle, GLShaderParameterName.InfoLogLength, out int logLen);
|
||||||
|
if (status == 0)
|
||||||
|
{
|
||||||
|
string log = logLen > 1 ? gl.GetShaderInfoLog(handle) : "";
|
||||||
|
Console.Error.WriteLine($"[ImGui OpenGL3] Failed to compile {desc}:\n{log}");
|
||||||
|
}
|
||||||
|
return status != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool CheckProgram(GL gl, uint handle, string desc)
|
||||||
|
{
|
||||||
|
gl.GetProgramiv(handle, GLProgramPropertyARB.LinkStatus, out int status);
|
||||||
|
gl.GetProgramiv(handle, GLProgramPropertyARB.InfoLogLength, out int logLen);
|
||||||
|
if (status == 0)
|
||||||
|
{
|
||||||
|
string log = logLen > 1 ? gl.GetProgramInfoLog(handle) : "";
|
||||||
|
Console.Error.WriteLine($"[ImGui OpenGL3] Failed to link {desc}:\n{log}");
|
||||||
|
}
|
||||||
|
return status != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void SetCap(GL gl, GLEnableCap cap, bool enabled)
|
||||||
|
{
|
||||||
|
if (enabled) gl.Enable(cap);
|
||||||
|
else gl.Disable(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class DelegateStorage
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
|
internal delegate void DrawCallbackFn(ImDrawListPtr drawList, ImDrawCmdPtr cmd);
|
||||||
|
|
||||||
|
internal static readonly DrawCallbackFn DrawCallbackResetRenderStateDelegate = DrawCallbackResetRenderState;
|
||||||
|
private static void DrawCallbackResetRenderState(ImDrawListPtr _, ImDrawCmdPtr __) { } // Intentionally empty.
|
||||||
|
|
||||||
|
internal static readonly DrawCallbackFn DrawCallbackSetSamplerLinearDelegate = DrawCallbackSetSamplerLinear;
|
||||||
|
private static void DrawCallbackSetSamplerLinear(ImDrawListPtr _, ImDrawCmdPtr __)
|
||||||
|
{
|
||||||
|
RendererData? bd = GetRendererData();
|
||||||
|
if (bd != null) ApplySamplerLinear(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static readonly DrawCallbackFn DrawCallbackSetSamplerNearestDelegate = DrawCallbackSetSamplerNearest;
|
||||||
|
private static void DrawCallbackSetSamplerNearest(ImDrawListPtr _, ImDrawCmdPtr __)
|
||||||
|
{
|
||||||
|
RendererData? bd = GetRendererData();
|
||||||
|
if (bd != null) ApplySamplerNearest(bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
|
internal delegate void RendererRenderWindowFn(ImGuiViewportPtr viewport, void* renderArg);
|
||||||
|
|
||||||
|
internal static readonly RendererRenderWindowFn RendererRenderWindowDelegate = RendererRenderWindow;
|
||||||
|
private static void RendererRenderWindow(ImGuiViewportPtr viewport, void* renderArg)
|
||||||
|
{
|
||||||
|
if ((viewport.Flags & ImGuiViewportFlags.NoRendererClear) == 0)
|
||||||
|
{
|
||||||
|
RendererData? bd = GetRendererData();
|
||||||
|
if (bd != null)
|
||||||
|
{
|
||||||
|
bd.Gl.ClearColor(0f, 0f, 0f, 1f);
|
||||||
|
bd.Gl.Clear(GLClearBufferMask.ColorBufferBit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RenderDrawData(viewport.DrawData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace SDL3_TestingSuite.SDL3;
|
||||||
|
|
||||||
|
public unsafe static class ImGuiUserData<T> where T : class
|
||||||
|
{
|
||||||
|
public static void* Store(T value)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
GCHandle handle = GCHandle.Alloc(value, GCHandleType.Normal);
|
||||||
|
return (void*)GCHandle.ToIntPtr(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T? Get(void* ptr)
|
||||||
|
{
|
||||||
|
if (ptr == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
|
||||||
|
return (T?)handle.Target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Free(void* ptr)
|
||||||
|
{
|
||||||
|
if (ptr == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
|
||||||
|
|
||||||
|
if (handle.IsAllocated)
|
||||||
|
handle.Free();
|
||||||
|
}
|
||||||
|
}
|
||||||
+139
-66
@@ -1,12 +1,9 @@
|
|||||||
using System;
|
using System.Numerics;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Numerics;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using Hexa.NET.ImGui;
|
using Hexa.NET.ImGui;
|
||||||
using Hexa.NET.ImGui.Utilities;
|
|
||||||
using Hexa.NET.ImGuizmo;
|
using Hexa.NET.ImGuizmo;
|
||||||
using Hexa.NET.ImPlot;
|
using Hexa.NET.ImPlot;
|
||||||
using Hexa.NET.ImPlot3D;
|
using Hexa.NET.OpenGL;
|
||||||
|
using HexaGen.Runtime;
|
||||||
using SDL3;
|
using SDL3;
|
||||||
|
|
||||||
namespace SDL3_TestingSuite.SDL3;
|
namespace SDL3_TestingSuite.SDL3;
|
||||||
@@ -14,49 +11,70 @@ namespace SDL3_TestingSuite.SDL3;
|
|||||||
public sealed unsafe class SDL3Window : IDisposable
|
public sealed unsafe class SDL3Window : IDisposable
|
||||||
{
|
{
|
||||||
public readonly nint Window;
|
public readonly nint Window;
|
||||||
public readonly nint Renderer;
|
|
||||||
|
|
||||||
internal Action? RenderCallback { get; set; }
|
public readonly Vector4 DefaultClearColor = new Vector4(0.06f, 0.06f, 0.06f, 1f);
|
||||||
|
|
||||||
public Vector4 ClearColor = new Vector4(0.06f, 0.05882353f, 0.05882353f, 1f);
|
private Vector4? _clearColor;
|
||||||
|
public Vector4 ClearColor
|
||||||
|
{
|
||||||
|
get => _clearColor ?? DefaultClearColor;
|
||||||
|
set => _clearColor = value;
|
||||||
|
}
|
||||||
|
|
||||||
private readonly Stopwatch _timer = Stopwatch.StartNew();
|
public event Action? RenderCallback;
|
||||||
private TimeSpan _time = TimeSpan.Zero;
|
|
||||||
|
|
||||||
private SDL.Rect _screenClipRect;
|
|
||||||
private ImGuiContextPtr _imGuiContext;
|
private ImGuiContextPtr _imGuiContext;
|
||||||
|
private ImPlotContextPtr _imPlotContext;
|
||||||
|
|
||||||
public bool Disposed => _disposed;
|
public bool Disposed => _disposed;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
|
||||||
private FileSystemWatcher? _watcher;
|
private readonly FileSystemWatcher? _watcher;
|
||||||
|
|
||||||
|
private readonly GL _gl;
|
||||||
|
|
||||||
public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity)
|
public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity)
|
||||||
{
|
{
|
||||||
if (!SDL.Init(SDL.InitFlags.Events | SDL.InitFlags.Video | SDL.InitFlags.Gamepad))
|
if (!SDL.Init(SDL.InitFlags.Events | SDL.InitFlags.Video | SDL.InitFlags.Gamepad))
|
||||||
throw new Exception($"SDL_Init failed: {SDL.GetError()}");
|
throw new Exception($"SDL_Init failed: {SDL.GetError()}");
|
||||||
|
|
||||||
|
SDL.GLSetAttribute(SDL.GLAttr.ContextFlags, (int)SDL.GLContextFlag.ForwardCompatible);
|
||||||
|
SDL.GLSetAttribute(SDL.GLAttr.ContextProfileMask, (int)SDL.GLProfile.Core);
|
||||||
|
SDL.GLSetAttribute(SDL.GLAttr.ContextMajorVersion, 3);
|
||||||
|
SDL.GLSetAttribute(SDL.GLAttr.ContextMinorVersion, 3);
|
||||||
|
|
||||||
|
SDL.SetHint(SDL.Hints.IMEImplementedUI, "1");
|
||||||
|
|
||||||
// Create window & renderer
|
// Create window & renderer
|
||||||
if (!SDL.CreateWindowAndRenderer(name, width, height, flags, out Window, out Renderer))
|
SDL.GLSetAttribute(SDL.GLAttr.DoubleBuffer, 1);
|
||||||
|
SDL.GLSetAttribute(SDL.GLAttr.DepthSize, 24);
|
||||||
|
SDL.GLSetAttribute(SDL.GLAttr.StencilSize, 8);
|
||||||
|
Window = SDL.CreateWindow(name, width, height, flags | SDL.WindowFlags.OpenGL);
|
||||||
|
if (Window == nint.Zero)
|
||||||
throw new Exception($"SDL_CreateWindowAndRenderer failed: {SDL.GetError()}");
|
throw new Exception($"SDL_CreateWindowAndRenderer failed: {SDL.GetError()}");
|
||||||
|
|
||||||
|
nint glContext = SDL.GLCreateContext(Window);
|
||||||
|
if (glContext == nint.Zero)
|
||||||
|
throw new Exception($"SDL_GL_CreateContext failed: {SDL.GetError()}");
|
||||||
|
|
||||||
|
SDL.GLMakeCurrent(Window, glContext);
|
||||||
|
SDL.GLSetSwapInterval(1);
|
||||||
|
|
||||||
SDL.SetWindowPosition(Window, posX, posY);
|
SDL.SetWindowPosition(Window, posX, posY);
|
||||||
SDL.SetRenderVSync(Renderer, 1);
|
// SDL.SetRenderVSync(Renderer, 1);
|
||||||
SDL.ShowWindow(Window);
|
SDL.ShowWindow(Window);
|
||||||
|
|
||||||
// Create ImGui context
|
// Create ImGui context
|
||||||
var context = ImGui.CreateContext();
|
_imGuiContext = ImGui.CreateContext();
|
||||||
ImPlot.CreateContext();
|
_imPlotContext = ImPlot.CreateContext();
|
||||||
ImPlot.SetImGuiContext(context);
|
ImPlot.SetImGuiContext(_imGuiContext);
|
||||||
ImGuizmo.SetImGuiContext(context);
|
ImGuizmo.SetImGuiContext(_imGuiContext);
|
||||||
// ImPlot3D.SetImGuiContext(context);
|
|
||||||
// ImPlot3D.CreateContext();
|
|
||||||
|
|
||||||
ImGuiIOPtr io = ImGui.GetIO();
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad | ImGuiConfigFlags.DockingEnable;
|
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad | ImGuiConfigFlags.DockingEnable;
|
||||||
// io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
|
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
|
||||||
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
|
|
||||||
|
|
||||||
|
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
|
||||||
io.Fonts.AddFontDefault();
|
io.Fonts.AddFontDefault();
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -66,7 +84,7 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
|
|
||||||
_watcher = new FileSystemWatcher(fontsPath);
|
_watcher = new FileSystemWatcher(fontsPath);
|
||||||
_watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime;
|
_watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime;
|
||||||
_watcher.Created += (a, b) =>
|
_watcher.Created += (_, b) =>
|
||||||
{
|
{
|
||||||
if (!File.Exists(b.FullPath)) return;
|
if (!File.Exists(b.FullPath)) return;
|
||||||
|
|
||||||
@@ -74,7 +92,7 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
|
|
||||||
ImGui.GetIO().AddFont(b.FullPath);
|
ImGui.GetIO().AddFont(b.FullPath);
|
||||||
};
|
};
|
||||||
_watcher.Deleted += (a, b) =>
|
_watcher.Deleted += (_, b) =>
|
||||||
{
|
{
|
||||||
if (Path.GetExtension(b.FullPath) != ".ttf") return;
|
if (Path.GetExtension(b.FullPath) != ".ttf") return;
|
||||||
|
|
||||||
@@ -95,41 +113,21 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_gl = new GL(new BindingsContext(Window, glContext));
|
||||||
|
|
||||||
// Init platform and renderer
|
// Init platform and renderer
|
||||||
ImGuiSDL3Platform.Init(Window, Renderer);
|
ImGuiSDL3Platform.Init(_gl, glContext, Window, this);
|
||||||
ImGuiSDL3Renderer.Init(Renderer);
|
ImGuiSDL3Renderer.Init(_gl, "#version 330");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool ShouldClose;
|
||||||
|
|
||||||
public void Run()
|
public void Run()
|
||||||
{
|
{
|
||||||
while (!_disposed)
|
while (!_disposed)
|
||||||
{
|
{
|
||||||
ImGui.GetIO().DeltaTime = (float)(_timer.Elapsed - _time).TotalSeconds;
|
SDL.PumpEvents();
|
||||||
_time = _timer.Elapsed;
|
|
||||||
|
|
||||||
PollEvents();
|
|
||||||
|
|
||||||
Update();
|
|
||||||
|
|
||||||
RenderCallback?.Invoke();
|
|
||||||
|
|
||||||
Render();
|
|
||||||
|
|
||||||
if (ShouldClose)
|
|
||||||
{
|
|
||||||
Dispose();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_disposed)
|
|
||||||
Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool ShouldClose;
|
|
||||||
|
|
||||||
private void PollEvents()
|
|
||||||
{
|
|
||||||
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
|
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
|
||||||
SDL.StartTextInput(Window);
|
SDL.StartTextInput(Window);
|
||||||
else if (!ImGui.GetIO().WantTextInput && SDL.TextInputActive(Window))
|
else if (!ImGui.GetIO().WantTextInput && SDL.TextInputActive(Window))
|
||||||
@@ -148,24 +146,24 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void Update()
|
_gl.MakeCurrent();
|
||||||
{
|
_gl.ClearColor(ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
|
||||||
|
_gl.Clear(GLClearBufferMask.ColorBufferBit);
|
||||||
|
|
||||||
ImGuiSDL3Platform.NewFrame();
|
ImGuiSDL3Platform.NewFrame();
|
||||||
ImGuiSDL3Renderer.NewFrame();
|
ImGuiSDL3Renderer.NewFrame();
|
||||||
ImGui.NewFrame();
|
ImGui.NewFrame();
|
||||||
}
|
|
||||||
|
|
||||||
private void Render()
|
RenderCallback?.Invoke();
|
||||||
{
|
|
||||||
ImGuiIOPtr io = ImGui.GetIO();
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
|
|
||||||
ImGui.Render();
|
ImGui.Render();
|
||||||
SDL.SetRenderScale(Renderer, io.DisplayFramebufferScale.X, io.DisplayFramebufferScale.Y);
|
|
||||||
SDL.SetRenderDrawColorFloat(Renderer, ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
|
_gl.MakeCurrent();
|
||||||
SDL.RenderClear(Renderer);
|
|
||||||
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData(), Renderer);
|
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData());
|
||||||
|
|
||||||
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
|
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
|
||||||
{
|
{
|
||||||
@@ -173,7 +171,18 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
ImGui.RenderPlatformWindowsDefault();
|
ImGui.RenderPlatformWindowsDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL.RenderPresent(Renderer);
|
_gl.MakeCurrent();
|
||||||
|
_gl.SwapBuffers();
|
||||||
|
|
||||||
|
if (ShouldClose)
|
||||||
|
{
|
||||||
|
Dispose();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_disposed)
|
||||||
|
Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
~SDL3Window()
|
~SDL3Window()
|
||||||
@@ -194,6 +203,15 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
|
|
||||||
if (disposing)
|
if (disposing)
|
||||||
{
|
{
|
||||||
|
if (_imGuiContext.Handle != null)
|
||||||
|
{
|
||||||
|
ImGui.SetCurrentContext(_imGuiContext);
|
||||||
|
}
|
||||||
|
if (_imPlotContext.Handle != null)
|
||||||
|
{
|
||||||
|
ImPlot.SetCurrentContext(_imPlotContext);
|
||||||
|
}
|
||||||
|
|
||||||
ImGuiSDL3Renderer.Dispose();
|
ImGuiSDL3Renderer.Dispose();
|
||||||
ImGuiSDL3Platform.Dispose();
|
ImGuiSDL3Platform.Dispose();
|
||||||
|
|
||||||
@@ -206,12 +224,18 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
ImGui.DestroyContext(_imGuiContext);
|
ImGui.DestroyContext(_imGuiContext);
|
||||||
_imGuiContext = null;
|
_imGuiContext = null;
|
||||||
}
|
}
|
||||||
|
if (_imPlotContext.Handle != null)
|
||||||
if (Renderer != nint.Zero)
|
|
||||||
{
|
{
|
||||||
SDL.DestroyRenderer(Renderer);
|
ImPlot.SetCurrentContext(null);
|
||||||
|
ImPlot.DestroyContext(_imPlotContext);
|
||||||
|
_imPlotContext = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (Renderer != nint.Zero)
|
||||||
|
// {
|
||||||
|
// SDL.DestroyRenderer(Renderer);
|
||||||
|
// }
|
||||||
|
|
||||||
if (Window != nint.Zero)
|
if (Window != nint.Zero)
|
||||||
{
|
{
|
||||||
SDL.DestroyWindow(Window);
|
SDL.DestroyWindow(Window);
|
||||||
@@ -219,3 +243,52 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public unsafe class BindingsContext : IGLContext
|
||||||
|
{
|
||||||
|
private readonly nint window;
|
||||||
|
private readonly nint context;
|
||||||
|
|
||||||
|
public BindingsContext(nint window, nint context)
|
||||||
|
{
|
||||||
|
this.window = window;
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public nint Handle => window;
|
||||||
|
|
||||||
|
public bool IsCurrent => SDL.GLGetCurrentContext() == context;
|
||||||
|
|
||||||
|
public void Dispose() { }
|
||||||
|
|
||||||
|
public nint GetProcAddress(string procName)
|
||||||
|
{
|
||||||
|
return (nint)SDL.GLGetProcAddress(procName).GetPtrForDelegate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsExtensionSupported(string extensionName)
|
||||||
|
{
|
||||||
|
return SDL.GLExtensionSupported(extensionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MakeCurrent()
|
||||||
|
{
|
||||||
|
SDL.GLMakeCurrent(window, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SwapBuffers()
|
||||||
|
{
|
||||||
|
SDL.GLSwapWindow(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SwapInterval(int interval)
|
||||||
|
{
|
||||||
|
SDL.GLSetSwapInterval(interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetProcAddress(string procName, out nint procAddress)
|
||||||
|
{
|
||||||
|
procAddress = (nint)SDL.GLGetProcAddress(procName).GetPtrForDelegate();
|
||||||
|
return procAddress != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user