ImGuiNet
This commit is contained in:
+120
-183
@@ -1,6 +1,6 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hexa.NET.ImGui;
|
||||
using ImGuiNET;
|
||||
using SDL3;
|
||||
|
||||
namespace SDL3_TestingSuite.SDL3;
|
||||
@@ -11,12 +11,6 @@ namespace SDL3_TestingSuite.SDL3;
|
||||
/// </summary>
|
||||
public unsafe static class ImGuiSDL3Renderer
|
||||
{
|
||||
private sealed class TextureState
|
||||
{
|
||||
public ImTextureDataPtr Source;
|
||||
public readonly Dictionary<nint, nint> RendererTextures = new();
|
||||
}
|
||||
|
||||
public class RendererData
|
||||
{
|
||||
public nint Renderer; // Main viewport's renderer
|
||||
@@ -59,7 +53,9 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
|
||||
private static readonly RendererSwapBuffersFn RendererSwapBuffersDelegate = RendererSwapBuffers;
|
||||
|
||||
public static RendererData Data => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData)! : null!;
|
||||
public static RendererData Data => ImGui.GetCurrentContext() != nint.Zero ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData)! : null!;
|
||||
|
||||
private static nint _fontTexture = nint.Zero;
|
||||
|
||||
public static bool Init(nint renderer)
|
||||
{
|
||||
@@ -67,19 +63,17 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
|
||||
RendererData bd = new RendererData();
|
||||
io.BackendRendererUserData = ImGuiUserData<RendererData>.Store(bd);
|
||||
io.BackendRendererName = (byte*)Marshal.StringToHGlobalAnsi("NepImGuiSDL3Renderer");
|
||||
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;
|
||||
|
||||
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||
platformIO.RendererCreateWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererCreateWindowDelegate);
|
||||
platformIO.RendererDestroyWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererDestroyWindowDelegate);
|
||||
platformIO.RendererSetWindowSize = (void*)Marshal.GetFunctionPointerForDelegate(RendererSetWindowSizeDelegate);
|
||||
platformIO.RendererRenderWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererRenderWindowDelegate);
|
||||
platformIO.RendererSwapBuffers = (void*)Marshal.GetFunctionPointerForDelegate(RendererSwapBuffersDelegate);
|
||||
platformIO.Renderer_CreateWindow = Marshal.GetFunctionPointerForDelegate(RendererCreateWindowDelegate);
|
||||
platformIO.Renderer_DestroyWindow = Marshal.GetFunctionPointerForDelegate(RendererDestroyWindowDelegate);
|
||||
platformIO.Renderer_SetWindowSize = Marshal.GetFunctionPointerForDelegate(RendererSetWindowSizeDelegate);
|
||||
platformIO.Renderer_RenderWindow = Marshal.GetFunctionPointerForDelegate(RendererRenderWindowDelegate);
|
||||
platformIO.Renderer_SwapBuffers = Marshal.GetFunctionPointerForDelegate(RendererSwapBuffersDelegate);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -90,21 +84,24 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
var platformIO = ImGui.GetPlatformIO();
|
||||
|
||||
DestroyDeviceObjects();
|
||||
|
||||
io.BackendRendererName = null;
|
||||
io.BackendRendererUserData = null;
|
||||
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures);
|
||||
platformIO.RendererTextureMaxWidth = 0;
|
||||
platformIO.RendererTextureMaxHeight = 0;
|
||||
platformIO.RendererRenderState = null;
|
||||
platformIO.RendererCreateWindow = null;
|
||||
platformIO.RendererDestroyWindow = null;
|
||||
platformIO.RendererSetWindowSize = null;
|
||||
platformIO.RendererRenderWindow = null;
|
||||
platformIO.RendererSwapBuffers = null;
|
||||
|
||||
io.BackendRendererUserData = nint.Zero;
|
||||
io.BackendFlags &= ~ImGuiBackendFlags.RendererHasVtxOffset;
|
||||
platformIO.Renderer_RenderState = nint.Zero;
|
||||
platformIO.Renderer_CreateWindow = nint.Zero;
|
||||
platformIO.Renderer_DestroyWindow = nint.Zero;
|
||||
platformIO.Renderer_SetWindowSize = nint.Zero;
|
||||
platformIO.Renderer_RenderWindow = nint.Zero;
|
||||
platformIO.Renderer_SwapBuffers = nint.Zero;
|
||||
}
|
||||
|
||||
public static void NewFrame() { }
|
||||
public static void NewFrame()
|
||||
{
|
||||
if (_fontTexture == nint.Zero)
|
||||
{
|
||||
CreateDeviceObjects();
|
||||
}
|
||||
}
|
||||
|
||||
private static void RendererCreateWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
@@ -171,7 +168,7 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
public static void RenderDrawData(ImDrawDataPtr drawData, nint renderer)
|
||||
{
|
||||
// Skip if no data to render
|
||||
if (drawData.Handle == null || drawData.CmdListsCount == 0)
|
||||
if (drawData.NativePtr == null || drawData.CmdListsCount == 0)
|
||||
return;
|
||||
|
||||
SDL.GetRenderScale(renderer, out float renderScaleX, out float renderScaleY);
|
||||
@@ -184,12 +181,6 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
if (fbWidth <= 0 || fbHeight <= 0)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < drawData.Textures.Size; i++)
|
||||
{
|
||||
var texture = drawData.Textures[i];
|
||||
UpdateTexture(texture, renderer);
|
||||
}
|
||||
|
||||
// Backup SDL renderer state
|
||||
BackupSDLRendererState old = new BackupSDLRendererState
|
||||
{
|
||||
@@ -207,7 +198,7 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
|
||||
// Set render state in platform IO
|
||||
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
|
||||
platformIo.RendererRenderState = (void*)renderer;
|
||||
platformIo.Renderer_RenderState = renderer;
|
||||
|
||||
Vector2 clipOffset = drawData.DisplayPos;
|
||||
|
||||
@@ -218,157 +209,54 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
|
||||
for (int cmdIndex = 0; cmdIndex < cmdList.CmdBuffer.Size; cmdIndex++)
|
||||
{
|
||||
ImDrawCmd cmd = cmdList.CmdBuffer[cmdIndex];
|
||||
ImDrawCmdPtr cmd = cmdList.CmdBuffer[cmdIndex];
|
||||
|
||||
if (cmd.UserCallback != null)
|
||||
if (cmd.UserCallback != nint.Zero)
|
||||
{
|
||||
continue; // User callback not implemented
|
||||
}
|
||||
else
|
||||
|
||||
// Apply clipping rectangle
|
||||
Vector4 clipRect = cmd.ClipRect;
|
||||
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;
|
||||
|
||||
SDL.Rect r = new SDL.Rect
|
||||
{
|
||||
// Apply clipping rectangle
|
||||
Vector4 clipRect = cmd.ClipRect;
|
||||
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);
|
||||
X = (int)clipMin.X,
|
||||
Y = (int)clipMin.Y,
|
||||
W = (int)(clipMax.X - clipMin.X),
|
||||
H = (int)(clipMax.Y - clipMin.Y)
|
||||
};
|
||||
SDL.SetRenderClipRect(renderer, r);
|
||||
|
||||
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;
|
||||
// Get texture
|
||||
nint texId = cmd.GetTexID();
|
||||
|
||||
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()}");
|
||||
}
|
||||
// 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
|
||||
platformIo.RendererRenderState = null;
|
||||
platformIo.Renderer_RenderState = nint.Zero;
|
||||
|
||||
// Restore renderer state
|
||||
SDL.SetRenderViewport(renderer, old.ViewportEnabled ? old.Viewport : new SDL.Rect());
|
||||
SDL.SetRenderClipRect(renderer, old.ClipEnabled ? old.ClipRect : new SDL.Rect());
|
||||
}
|
||||
|
||||
private static nint ResolveTextureId(ImTextureID texId, nint renderer)
|
||||
{
|
||||
if (texId == ImTextureID.Null)
|
||||
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
|
||||
{
|
||||
SDL.UpdateTexture(sdlTexture, nint.Zero, (nint)tex.GetPixels(), tex.GetPitch());
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateTexture(ImTextureDataPtr tex, nint renderer)
|
||||
{
|
||||
TextureState? state = null;
|
||||
if ((nint)tex.BackendUserData != nint.Zero)
|
||||
state = ImGuiUserData<TextureState>.Get(tex.BackendUserData);
|
||||
|
||||
if (state == null)
|
||||
{
|
||||
state = new TextureState();
|
||||
tex.BackendUserData = ImGuiUserData<TextureState>.Store(state);
|
||||
}
|
||||
|
||||
state.Source = tex;
|
||||
tex.SetTexID((nint)tex.BackendUserData);
|
||||
|
||||
if (tex.Status == ImTextureStatus.WantDestroy)
|
||||
{
|
||||
foreach (nint rendererTexture in state.RendererTextures.Values)
|
||||
{
|
||||
if (rendererTexture != nint.Zero)
|
||||
SDL.DestroyTexture(rendererTexture);
|
||||
}
|
||||
|
||||
state.RendererTextures.Clear();
|
||||
ImGuiUserData<TextureState>.Free(tex.BackendUserData);
|
||||
tex.BackendUserData = (void*)nint.Zero;
|
||||
tex.SetTexID(ImTextureID.Null);
|
||||
tex.SetStatus(ImTextureStatus.Destroyed);
|
||||
return;
|
||||
}
|
||||
|
||||
bool hasRendererTexture = state.RendererTextures.TryGetValue(renderer, out nint sdlTexture) && sdlTexture != nint.Zero;
|
||||
if (!hasRendererTexture)
|
||||
{
|
||||
sdlTexture = CreateRendererTexture(state, renderer);
|
||||
state.RendererTextures[renderer] = sdlTexture;
|
||||
}
|
||||
|
||||
if (sdlTexture != nint.Zero && (tex.Status == ImTextureStatus.WantCreate || tex.Status == ImTextureStatus.WantUpdates || !hasRendererTexture))
|
||||
{
|
||||
UploadRendererTexture(tex, renderer, sdlTexture);
|
||||
tex.SetStatus(ImTextureStatus.Ok);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static bool RenderDrawCommand(ImDrawListPtr drawList, ImDrawCmd cmd, nint renderer, nint texId, Vector2 scale)
|
||||
private static bool RenderDrawCommand(ImDrawListPtr drawList, ImDrawCmdPtr cmd, nint renderer, nint texId, Vector2 scale)
|
||||
{
|
||||
uint indexOffset = cmd.IdxOffset;
|
||||
uint vertexOffset = cmd.VtxOffset;
|
||||
@@ -382,9 +270,9 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
ushort idx = drawList.IdxBuffer[(int)indexOffset + i];
|
||||
int vertIdx = (int)(vertexOffset + idx);
|
||||
|
||||
ImDrawVert srcVert = drawList.VtxBuffer[vertIdx];
|
||||
ImDrawVertPtr srcVert = drawList.VtxBuffer[vertIdx];
|
||||
|
||||
uint col = srcVert.Col;
|
||||
uint col = srcVert.col;
|
||||
|
||||
byte r = (byte)((col >> 0) & 0xFF);
|
||||
byte g = (byte)((col >> 8) & 0xFF);
|
||||
@@ -395,8 +283,8 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
{
|
||||
Position = new SDL.FPoint()
|
||||
{
|
||||
X = srcVert.Pos.X * scale.X,
|
||||
Y = srcVert.Pos.Y * scale.Y
|
||||
X = srcVert.pos.X * scale.X,
|
||||
Y = srcVert.pos.Y * scale.Y
|
||||
},
|
||||
Color = new SDL.FColor()
|
||||
{
|
||||
@@ -407,8 +295,8 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
},
|
||||
TexCoord = new SDL.FPoint()
|
||||
{
|
||||
X = srcVert.Uv.X,
|
||||
Y = srcVert.Uv.Y
|
||||
X = srcVert.uv.X,
|
||||
Y = srcVert.uv.Y
|
||||
}
|
||||
};
|
||||
|
||||
@@ -418,16 +306,65 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
return SDL.RenderGeometry(renderer, texId, vertices, vertices.Length, indices, indices.Length);
|
||||
}
|
||||
|
||||
public static void CreateDeviceObjects() { }
|
||||
|
||||
public static void DestroyDeviceObjects()
|
||||
public static void CreateDeviceObjects()
|
||||
{
|
||||
var texures = ImGui.GetPlatformIO().Textures;
|
||||
for (int i = 0; i < texures.Size; i++)
|
||||
ImGuiIOPtr io = ImGui.GetIO();
|
||||
var data = Data;
|
||||
|
||||
io.Fonts.AddFontDefault();
|
||||
|
||||
// TODO: Load custom fonts from "Fonts" directory
|
||||
//string fontsPath = Path.Combine(Plugin.AssemblyDirectory, "Fonts");
|
||||
//if (Path.Exists(fontsPath))
|
||||
//{
|
||||
// string[] fonts = Directory.GetFiles(fontsPath, "*.ttf");
|
||||
// if (fonts.Length > 0)
|
||||
// {
|
||||
// foreach (string font in fonts)
|
||||
// {
|
||||
// io.Fonts.AddFontFromFileTTF(font, 20f);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
// Build texture atlas
|
||||
io.Fonts.GetTexDataAsRGBA32(out byte* pixels, out int width, out int height);
|
||||
|
||||
// Create surface from pixel data
|
||||
nint surface = SDL.CreateSurfaceFrom(width, height, SDL.PixelFormat.RGBA8888, (nint)pixels, width * 4);
|
||||
if (surface == nint.Zero)
|
||||
{
|
||||
var texture = texures[i];
|
||||
texture.Status = ImTextureStatus.WantDestroy;
|
||||
UpdateTexture(texture, nint.Zero);
|
||||
SDL.LogError(SDL.LogCategory.Application, $"Failed to create font surface: {SDL.GetError()}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create texture
|
||||
_fontTexture = SDL.CreateTextureFromSurface(data.Renderer, surface);
|
||||
if (_fontTexture == nint.Zero)
|
||||
{
|
||||
SDL.LogError(SDL.LogCategory.Application, $"Failed to create font texture: {SDL.GetError()}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Update texture directly without converting pixel format
|
||||
if (!SDL.UpdateTexture(_fontTexture, nint.Zero, (nint)pixels, width * 4))
|
||||
{
|
||||
SDL.LogError(SDL.LogCategory.Application, $"Failed to update font texture: {SDL.GetError()}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure proper blending for font rendering
|
||||
SDL.SetTextureBlendMode(_fontTexture, SDL.BlendMode.Blend);
|
||||
|
||||
// Use nearest neighbor filtering for crisp font rendering at small sizes
|
||||
SDL.SetTextureScaleMode(_fontTexture, SDL.ScaleMode.Linear);
|
||||
|
||||
// Store our identifier
|
||||
io.Fonts.SetTexID(_fontTexture);
|
||||
|
||||
SDL.DestroySurface(surface);
|
||||
io.Fonts.ClearTexData();
|
||||
}
|
||||
|
||||
public static void DestroyDeviceObjects() { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user