SOURCE DUMP v1

This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit fb20299e9d
4 changed files with 768360 additions and 0 deletions

View file

@ -0,0 +1,16 @@
using Brutal.OpenGlApi;
namespace Brutal.ImGuiApi.Backends;
public partial class OpenGlBackend
{
internal static class Helpers
{
public static unsafe int GetIntegerv(GlEnum name)
{
var value = 0;
OpenGl.GetIntegerv(name, new Span<int>(&value, 1));
return value;
}
}
}

View file

@ -0,0 +1,605 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Brutal.Numerics;
using Brutal.OpenGlApi;
using Brutal.Pointers;
using Brutal.Strings;
namespace Brutal.ImGuiApi.Backends;
using static OpenGlBackend.Helpers;
/**
* Brutal port based on the default imgui_impl_opengl3 C++ code
* Key variations:
* - Assumes version 4.1+
* - Haven't added multi-context support, assumes we can just GetContext()
* - Doesn't support forward compatibility using GL_CONTEXT_COMPATIBILITY_PROFILE_BIT
*/
public partial class OpenGlBackend : IRendererBackend
{
private static readonly Ptr<byte> Name = "Brutal.ImGuiApi.OpenGlBackend"u8;
public unsafe OpenGlBackend()
{
var io = ImGui.GetIO();
io.BackendRendererUserData = (nint)GCHandle.Alloc(this);
io.BackendRendererName = Name;
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; // We can create multi-viewports on the Renderer side (optional)
var versionMaj = GetIntegerv(OpenGl.GL_MAJOR_VERSION);
var versionMin = GetIntegerv(OpenGl.GL_MINOR_VERSION);
_glVersion = versionMaj * 100 + versionMin;
_hasClipOrigin = _glVersion >= 450;
if (!_hasClipOrigin)
{
var numExtensions = GetIntegerv(OpenGl.GL_NUM_EXTENSIONS);
for (uint i = 0; i < numExtensions; i++)
{
var extension = OpenGl.GetStringi(OpenGl.GL_EXTENSIONS, i);
if (extension is not null)
{
var utf8String = Utf8.GetNullTerminatedSpan(extension);
if (utf8String.SequenceEqual("GL_ARB_clip_control"u8))
_hasClipOrigin = true;
}
}
}
InitMultiViewportSupport();
// Note: original C++ code does this on NewFrame if not already loaded, couldn't see a good reason to wait though
CreateDeviceObjects(out _shader, out _vertexBuffer, out _indexBuffer);
}
public void NewFrame() { }
public unsafe void RenderDrawData(ImDrawDataPtr drawData)
{
var width = (int)(drawData.DisplaySize.X * drawData.FramebufferScale.X);
var height = (int)(drawData.DisplaySize.Y * drawData.FramebufferScale.Y);
if (width <= 0 || height <= 0)
return;
if (drawData.Textures.IsNotNull())
{
for (var i = 0; i < drawData.Textures.Value.Count; i++)
{
var tex = drawData.Textures.Value[i];
if (tex.Status != ImTextureStatus.OK)
UpdateTexture(tex);
}
}
using var backupState = new GlStateBackup();
var vertexArrayObject = OpenGl.GenVertexArray();
SetupRenderState(drawData, width, height, vertexArrayObject);
// Will project scissor/clipping rectangles into framebuffer space
var clipOff = drawData.DisplayPos; // (0,0) unless using multi-viewports
var clipScale = drawData.FramebufferScale; // (1,1) unless using retina display which are often (2,2)
// Render command lists
for (var n = 0; n < drawData.CmdListsCount; n++)
{
var drawList = drawData.CmdLists[n];
OpenGl.BufferData(
GlBindTarget.ArrayBuffer,
new Span<ImDrawVert>(drawList.VtxBuffer.Data, drawList.VtxBuffer.Count),
GlBufferUsage.StreamDraw
);
OpenGl.BufferData(
GlBindTarget.ElementArrayBuffer,
new Span<ushort>(drawList.IdxBuffer.Data, drawList.IdxBuffer.Count),
GlBufferUsage.StreamDraw
);
for (var i = 0; i < drawList.CmdBuffer.Count; i++)
{
var command = drawList.CmdBuffer[i];
// Project scissor/clipping rectangles into framebuffer space
var clipMin = new float2(
(command.ClipRect.X - clipOff.X) * clipScale.X,
(command.ClipRect.Y - clipOff.Y) * clipScale.Y
);
var clipMax = new float2(
(command.ClipRect.Z - clipOff.X) * clipScale.X,
(command.ClipRect.W - clipOff.Y) * clipScale.Y
);
if (clipMax.X <= clipMin.X || clipMax.Y <= clipMin.Y)
continue;
// Apply scissor/clipping rectangle (Y is inverted in OpenGL)
OpenGl.Scissor(
(int)clipMin.X,
(int)(height - clipMax.Y),
(int)(clipMax.X - clipMin.X),
(int)(clipMax.Y - clipMin.Y)
);
// Bind texture, Draw
OpenGl.BindTexture(OpenGl.GL_TEXTURE_2D, (uint)command.GetTexID().Value);
OpenGl.DrawIndexed(
GlTopology.Triangles,
GlIndexType.UShort,
indexCount: (int)command.ElemCount,
instanceCount: 1,
firstIndex: (int)command.IdxOffset * sizeof(ushort),
vertexOffset: (int)command.VtxOffset,
firstInstance: 0
);
OpenGl.Scissor(0, 0, width, height); // Reset scissor to full viewport;
}
}
OpenGl.DeleteVertexArrays([vertexArrayObject]);
}
private unsafe void SetupRenderState(ImDrawDataPtr drawData, int width, int height, GlVertexArray vertexArrayObject)
{
OpenGl.Enable(GlCapability.Blend);
OpenGl.BlendEquation(OpenGl.GL_FUNC_ADD);
OpenGl.BlendFuncSeparate(
OpenGl.GL_SRC_ALPHA,
OpenGl.GL_ONE_MINUS_SRC_ALPHA,
OpenGl.GL_ONE,
OpenGl.GL_ONE_MINUS_SRC_ALPHA
);
OpenGl.Disable(GlCapability.CullFace);
OpenGl.Disable(GlCapability.DepthTest);
OpenGl.Disable(GlCapability.StencilTest);
OpenGl.Enable(GlCapability.ScissorTest);
OpenGl.Disable(GlCapability.PrimitiveRestart);
OpenGl.PolygonMode(GlFace.FrontAndBack, OpenGl.GL_FILL);
var clipOriginLowerLeft = true;
if (_hasClipOrigin)
{
if (GetIntegerv(OpenGl.GL_CLIP_ORIGIN) == OpenGl.GL_UPPER_LEFT)
clipOriginLowerLeft = false;
}
// Setup viewport, orthographic projection matrix
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
// DisplayPos is (0,0) for single viewport apps.
OpenGl.Viewport(0, 0, width, height);
var left = drawData.DisplayPos.X;
var right = drawData.DisplayPos.X + drawData.DisplaySize.X;
var top = drawData.DisplayPos.Y;
var bottom = drawData.DisplayPos.Y + drawData.DisplaySize.Y;
// Swap top and bottom if origin is upper left
if (!clipOriginLowerLeft)
{
(top, bottom) = (bottom, top);
}
_shader.Use();
OpenGl.Uniform(_shader.Locations.Tex, 0);
OpenGl.Uniform(
_shader.Locations.Projection,
float4x4.CreateOrthographicOffCenter(left, right, bottom, top, 0.0f, 1.0f)
);
// We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise.
OpenGl.BindSampler(0, default);
// Bind vertex/index buffers and setup attributes for ImDrawVert
vertexArrayObject.Bind();
_vertexBuffer.Bind(GlBindTarget.ArrayBuffer);
_indexBuffer.Bind(GlBindTarget.ElementArrayBuffer);
OpenGl.EnableVertexAttribArray((uint)_shader.Locations.Position);
OpenGl.EnableVertexAttribArray((uint)_shader.Locations.Uv);
OpenGl.EnableVertexAttribArray((uint)_shader.Locations.Color);
OpenGl.VertexAttribute(
new GlAttribute(
(uint)_shader.Locations.Position,
2,
OpenGl.GL_FLOAT,
OpenGl.GL_FALSE,
sizeof(ImDrawVert),
0 // Offset of pos
)
);
OpenGl.VertexAttribute(
new GlAttribute(
(uint)_shader.Locations.Uv,
2,
OpenGl.GL_FLOAT,
OpenGl.GL_FALSE,
sizeof(ImDrawVert),
8 // Offset of uv
)
);
OpenGl.VertexAttribute(
new GlAttribute(
(uint)_shader.Locations.Color,
4,
OpenGl.GL_UNSIGNED_BYTE,
GlScaling.Normalized, // TODO: This is supposed to be true but API seems wrong...
sizeof(ImDrawVert),
16 // Offset of color
)
);
}
public unsafe void Dispose()
{
var io = ImGui.GetIO();
GCHandle.FromIntPtr(io.BackendRendererUserData).Free();
io.BackendRendererUserData = IntPtr.Zero;
io.BackendRendererName = null;
io.BackendFlags &= ~(
ImGuiBackendFlags.RendererHasVtxOffset
| ImGuiBackendFlags.RendererHasTextures
| ImGuiBackendFlags.RendererHasViewports
);
ShutdownMultiViewportSupport();
DestroyDeviceObjects();
}
private static unsafe void UpdateTexture(ImTextureDataPtr tex)
{
if (tex.Status is ImTextureStatus.WantCreate or ImTextureStatus.WantUpdates)
{
OpenGl.PixelStorei(OpenGl.GL_UNPACK_ROW_LENGTH, 0);
OpenGl.PixelStorei(OpenGl.GL_UNPACK_ALIGNMENT, 1);
}
if (tex.Status is ImTextureStatus.WantCreate)
{
// Create and upload new texture to graphics system
ImGui.Assert(tex.TexID.Value == 0 && tex.BackendUserData is 0);
ImGui.Assert(tex.Format is ImTextureFormat.RGBA32);
var pixels = tex.GetPixels();
// Upload texture to graphics system
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
GlTexture glTextureId = default;
var lastGlTextureId = GetIntegerv(OpenGl.GL_TEXTURE_BINDING_2D);
OpenGl.GenTextures(new ReadOnlySpan<GlTexture>(&glTextureId, 1));
OpenGl.BindTexture(OpenGl.GL_TEXTURE_2D, glTextureId.Id);
OpenGl.TexParameterInt(OpenGl.GL_TEXTURE_2D, OpenGl.GL_TEXTURE_MIN_FILTER, [(int)OpenGl.GL_LINEAR]);
OpenGl.TexParameterInt(OpenGl.GL_TEXTURE_2D, OpenGl.GL_TEXTURE_MAG_FILTER, [(int)OpenGl.GL_LINEAR]);
OpenGl.TexParameterInt(OpenGl.GL_TEXTURE_2D, OpenGl.GL_TEXTURE_WRAP_S, [(int)OpenGl.GL_CLAMP_TO_EDGE]);
OpenGl.TexParameterInt(OpenGl.GL_TEXTURE_2D, OpenGl.GL_TEXTURE_WRAP_T, [(int)OpenGl.GL_CLAMP_TO_EDGE]);
OpenGl.TexImage2D(
OpenGl.GL_TEXTURE_2D,
0,
(int)OpenGl.GL_RGBA,
tex.Width,
tex.Height,
0,
OpenGl.GL_RGBA,
OpenGl.GL_UNSIGNED_BYTE,
(void*)pixels
);
// Store identifiers
tex.SetTexID(new ImTextureID((nint)glTextureId.Id));
tex.SetStatus(ImTextureStatus.OK);
// Restore state
OpenGl.BindTexture(OpenGl.GL_TEXTURE_2D, (uint)lastGlTextureId);
}
else if (tex.Status == ImTextureStatus.WantUpdates)
{
// Update selected blocks. We only ever write to textures regions which have never been used before!
// This backend choose to use tex.Updates[] but you can use tex.UpdateRect to upload a single region.
var lastGlTextureId = GetIntegerv(OpenGl.GL_TEXTURE_BINDING_2D);
OpenGl.BindTexture(OpenGl.GL_TEXTURE_2D, (uint)tex.TexID.Value);
OpenGl.PixelStorei(OpenGl.GL_UNPACK_ROW_LENGTH, tex.Width);
for (var i = 0; i < tex.Updates.Count; i++)
{
var r = tex.Updates[i];
OpenGl.TexSubImage2D(
OpenGl.GL_TEXTURE_2D,
0,
r.x,
r.y,
r.w,
r.h,
(int)OpenGl.GL_RGBA,
OpenGl.GL_UNSIGNED_BYTE,
(void*)tex.GetPixelsAt(r.x, r.y)
);
}
OpenGl.PixelStorei(OpenGl.GL_UNPACK_ROW_LENGTH, 0);
tex.SetStatus(ImTextureStatus.OK);
OpenGl.BindTexture(OpenGl.GL_TEXTURE_2D, (uint)lastGlTextureId); // Restore state
}
else if (tex is { Status: ImTextureStatus.WantDestroy, UnusedFrames: > 0 })
DestroyTexture(tex);
}
private void CreateDeviceObjects(out Shader shader, out GlBuffer vertexBuffer, out GlBuffer indexBuffer)
{
// Note: at this point the C++ backend does some gl state backup/restore.
// It didn't appear that any of it was actually required, so skipped here...
shader = new Shader();
ReadOnlySpan<GlBuffer> buffers = stackalloc GlBuffer[2];
OpenGl.GenBuffers(buffers);
vertexBuffer = buffers[0];
indexBuffer = buffers[1];
}
private void DestroyDeviceObjects()
{
_shader.Dispose();
OpenGl.DeleteBuffers([_vertexBuffer, _indexBuffer]);
// Destroy all textures
var textures = ImGui.GetPlatformIO().Textures;
foreach (var tex in textures)
{
if (tex.RefCount is 1)
DestroyTexture(tex);
}
}
private static void DestroyTexture(ImTextureDataPtr tex)
{
var glTextureId = new GlTexture { Id = (uint)tex.TexID.Value };
OpenGl.DeleteTextures([glTextureId]);
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
tex.SetTexID(ImTextureID.Invalid);
tex.SetStatus(ImTextureStatus.Destroyed);
}
private Shader _shader;
private readonly GlBuffer _vertexBuffer;
private readonly GlBuffer _indexBuffer;
private int _glVersion;
private bool _hasClipOrigin;
private unsafe struct GlStateBackup : IDisposable
{
private readonly int _lastActiveTexture;
private readonly uint _lastProgram;
private readonly uint _lastTexture;
private readonly uint _lastSampler;
private readonly uint _lastArrayBuffer;
private readonly uint _lastVertexArrayObject;
private fixed int _lastPolygonMode[2];
private fixed int _lastViewport[4];
private fixed int _lastScissorBox[4];
private readonly int _lastBlendSrcRgb;
private readonly int _lastBlendDstRgb;
private readonly int _lastBlendSrcAlpha;
private readonly int _lastBlendDstAlpha;
private readonly int _lastBlendEquationRgb;
private readonly int _lastBlendEquationAlpha;
private readonly bool _lastEnableBlend;
private readonly bool _lastEnableCullFace;
private readonly bool _lastEnableDepthTest;
private readonly bool _lastEnableStencilTest;
private readonly bool _lastEnableScissorTest;
private readonly bool _lastEnablePrimitiveRestart;
public GlStateBackup()
{
// Backup GL state
_lastActiveTexture = GetIntegerv(OpenGl.GL_ACTIVE_TEXTURE);
OpenGl.ActiveTexture(OpenGl.GL_TEXTURE0);
_lastProgram = (uint)GetIntegerv(OpenGl.GL_CURRENT_PROGRAM);
_lastTexture = (uint)GetIntegerv(OpenGl.GL_TEXTURE_BINDING_2D);
_lastSampler = (uint)GetIntegerv(OpenGl.GL_SAMPLER_BINDING);
_lastArrayBuffer = (uint)GetIntegerv(OpenGl.GL_ARRAY_BUFFER_BINDING);
_lastVertexArrayObject = (uint)GetIntegerv(OpenGl.GL_VERTEX_ARRAY_BINDING);
fixed (int* lastPolygonMode = _lastPolygonMode)
OpenGl.GetIntegerv(OpenGl.GL_POLYGON_MODE, new ReadOnlySpan<int>(lastPolygonMode, 2));
fixed (int* lastViewport = _lastViewport)
OpenGl.GetIntegerv(OpenGl.GL_VIEWPORT, new ReadOnlySpan<int>(lastViewport, 4));
fixed (int* lastScissorBox = _lastScissorBox)
OpenGl.GetIntegerv(OpenGl.GL_SCISSOR_BOX, new ReadOnlySpan<int>(lastScissorBox, 4));
_lastBlendSrcRgb = GetIntegerv(OpenGl.GL_BLEND_SRC_RGB);
_lastBlendDstRgb = GetIntegerv(OpenGl.GL_BLEND_DST_RGB);
_lastBlendSrcAlpha = GetIntegerv(OpenGl.GL_BLEND_SRC_ALPHA);
_lastBlendDstAlpha = GetIntegerv(OpenGl.GL_BLEND_DST_ALPHA);
_lastBlendEquationRgb = GetIntegerv(OpenGl.GL_BLEND_EQUATION_RGB);
_lastBlendEquationAlpha = GetIntegerv(OpenGl.GL_BLEND_EQUATION_ALPHA);
_lastEnableBlend = OpenGl.IsEnabled(OpenGl.GL_BLEND);
_lastEnableCullFace = OpenGl.IsEnabled(OpenGl.GL_CULL_FACE);
_lastEnableDepthTest = OpenGl.IsEnabled(OpenGl.GL_DEPTH_TEST);
_lastEnableStencilTest = OpenGl.IsEnabled(OpenGl.GL_STENCIL_TEST);
_lastEnableScissorTest = OpenGl.IsEnabled(OpenGl.GL_SCISSOR_TEST);
_lastEnablePrimitiveRestart = OpenGl.IsEnabled(OpenGl.GL_PRIMITIVE_RESTART);
}
public void Dispose()
{
// Restore modified GL state
// This "glIsProgram()" check is required because if the program is "pending deletion" at the time of binding backup, it will have been deleted by now and will cause an OpenGL error. See #6220.
if (_lastProgram is 0 || OpenGl.IsProgram(new GlProgram(_lastProgram)))
new GlProgram(_lastProgram).Use();
OpenGl.BindTexture(OpenGl.GL_TEXTURE_2D, _lastTexture);
OpenGl.BindSampler(0, new GlSampler(_lastSampler));
OpenGl.ActiveTexture((uint)_lastActiveTexture);
new GlVertexArray(_lastVertexArrayObject).Bind();
new GlBuffer(_lastArrayBuffer).Bind(GlBindTarget.ArrayBuffer);
OpenGl.BlendEquationSeparate((uint)_lastBlendEquationRgb, (uint)_lastBlendEquationAlpha);
OpenGl.BlendFuncSeparate(
(uint)_lastBlendSrcRgb,
(uint)_lastBlendDstRgb,
(uint)_lastBlendSrcAlpha,
(uint)_lastBlendDstAlpha
);
if (_lastEnableBlend)
OpenGl.Enable(GlCapability.Blend);
else
OpenGl.Disable(GlCapability.Blend);
if (_lastEnableCullFace)
OpenGl.Enable(GlCapability.CullFace);
else
OpenGl.Disable(GlCapability.CullFace);
if (_lastEnableDepthTest)
OpenGl.Enable(GlCapability.DepthTest);
else
OpenGl.Disable(GlCapability.DepthTest);
if (_lastEnableStencilTest)
OpenGl.Enable(GlCapability.StencilTest);
else
OpenGl.Disable(GlCapability.StencilTest);
if (_lastEnableScissorTest)
OpenGl.Enable(GlCapability.ScissorTest);
else
OpenGl.Disable(GlCapability.ScissorTest);
if (_lastEnablePrimitiveRestart)
OpenGl.Enable(GlCapability.PrimitiveRestart);
else
OpenGl.Disable(GlCapability.PrimitiveRestart);
OpenGl.PolygonMode(GlFace.FrontAndBack, (uint)_lastPolygonMode[0]);
OpenGl.Viewport(_lastViewport[0], _lastViewport[1], _lastViewport[2], _lastViewport[3]);
OpenGl.Scissor(_lastScissorBox[0], _lastScissorBox[1], _lastScissorBox[2], _lastScissorBox[3]);
}
}
private struct Shader : IDisposable
{
public Shader()
{
Program = BuildProgram();
Locations.Tex = Program.GetUniformLocation("Texture"u8);
Locations.Projection = Program.GetUniformLocation("Projection"u8);
Locations.Position = OpenGl.GetAttribLocation(Program, "Position"u8);
Locations.Uv = OpenGl.GetAttribLocation(Program, "UV"u8);
Locations.Color = OpenGl.GetAttribLocation(Program, "Color"u8);
}
public void Use()
{
Program.Use();
}
public readonly GlProgram Program;
public readonly AttributeLocations Locations;
public struct AttributeLocations
{
public int Tex;
public int Projection;
public int Position;
public int Uv;
public int Color;
}
private static GlProgram BuildProgram()
{
using var vertShaderHandle = OpenGl
.CreateShader(GlShaderType.Vertex)
.SetSource(ShaderSource.Vert)
.Compile();
using var fragShaderHandle = OpenGl
.CreateShader(GlShaderType.Fragment)
.SetSource(ShaderSource.Frag)
.Compile();
var shaderHandle = OpenGl.CreateProgram().Attach(vertShaderHandle, fragShaderHandle).Link();
OpenGl.DetachShader(shaderHandle, vertShaderHandle);
OpenGl.DetachShader(shaderHandle, fragShaderHandle);
return shaderHandle;
}
public void Dispose()
{
Program.Dispose();
}
}
private static class ShaderSource
{
public const string Vert = """
#version 410 core
layout (location = 0) in vec2 Position;
layout (location = 1) in vec2 UV;
layout (location = 2) in vec4 Color;
uniform mat4 Projection;
out vec2 Frag_UV;
out vec4 Frag_Color;
void main()
{
Frag_UV = UV;
Frag_Color = Color;
gl_Position = Projection * vec4(Position.xy,0,1);
}
""";
public const string Frag = """
#version 410 core
in vec2 Frag_UV;
in vec4 Frag_Color;
uniform sampler2D Texture;
layout (location = 0) out vec4 Out_Color;
void main()
{
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
}
""";
}
//--------------------------------------------------------------------------------------------------------
// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
//--------------------------------------------------------------------------------------------------------
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static unsafe void RenderWindow(ImGuiViewport* viewport)
{
if (!viewport->Flags.HasFlag(ImGuiViewportFlags.NoRendererClear))
{
OpenGl.ClearColor(new float4(float3.Zero, 1.0f));
OpenGl.Clear(GlClearBit.Color);
}
var handle = GCHandle.FromIntPtr(ImGui.GetIO().BackendRendererUserData);
if (handle.Target is not OpenGlBackend backend)
throw new Exception("Failed to retrieve OpenGL backend");
backend.RenderDrawData(viewport->DrawData);
}
private static unsafe void InitMultiViewportSupport()
{
var platformIo = ImGui.GetPlatformIO();
delegate* unmanaged[Cdecl]<ImGuiViewport*, void> renderWindowFunction = &RenderWindow;
platformIo.Renderer_RenderWindow = (nint)renderWindowFunction;
}
private static void ShutdownMultiViewportSupport()
{
ImGui.DestroyPlatformWindows();
}
}

View file

@ -0,0 +1,424 @@
using Brutal.VulkanApi;
using Brutal.VulkanApi.Abstractions;
namespace Brutal.ImGuiApi.Backends;
public partial class VulkanBackend
{
private static class Helpers
{
public static unsafe VkSurfaceFormatKHR SelectSurfaceFormat(
PhysicalDevice physicalDevice,
VkSurfaceKHR surface,
ReadOnlySpan<VkFormat> requestFormats,
VkColorSpaceKHR requestColorSpace
)
{
Assert(!requestFormats.IsEmpty);
// Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
// Assuming that the default behavior is without setting this bit, there is no need for separate Swapchain image and image view format
// Additionally several new color spaces were introduced with Vulkan Spec v1.0.40,
// hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used.
var surfaceFormatCount = physicalDevice.GetSurfaceFormatsKHR_Count(surface);
Span<VkSurfaceFormatKHR> formats = stackalloc VkSurfaceFormatKHR[surfaceFormatCount];
physicalDevice.GetSurfaceFormatsKHR_Write(surface, formats);
// First check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
if (formats.Length is 1)
{
if (formats[0].Format is VkFormat.Undefined)
return new VkSurfaceFormatKHR { Format = requestFormats[0], ColorSpace = requestColorSpace };
// No point in searching another format
return formats[0];
}
// Request several formats, the first found will be used
foreach (var t in requestFormats)
foreach (var format in formats)
if (format.Format == t && format.ColorSpace == requestColorSpace)
return format;
// If none of the requested image formats could be found, use the first available
return formats[0];
}
public static unsafe VkPresentModeKHR SelectPresentMode(
PhysicalDevice physicalDevice,
VkSurfaceKHR surface,
ReadOnlySpan<VkPresentModeKHR> requestModes
)
{
Assert(!requestModes.IsEmpty);
// Request a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
var numModes = physicalDevice.GetSurfacePresentModesKHR_Count(surface);
Span<VkPresentModeKHR> modes = stackalloc VkPresentModeKHR[numModes];
physicalDevice.GetSurfacePresentModesKHR_Write(surface, modes);
foreach (var requestMode in requestModes)
foreach (var mode in modes)
if (requestMode == mode)
return requestMode;
return VkPresentModeKHR.FifoKHR; // Always available
}
// TODO: Can we pass command pool?
public static unsafe void CreateOrResizeWindow(
PhysicalDevice physicalDevice,
Device device,
WindowImpl* window,
Queue queue,
int queueFamily,
int width,
int height,
int minImageCount
)
{
CreateWindowSwapChain(physicalDevice, device, window, width, height, minImageCount);
CreateWindowCommandBuffers(physicalDevice, device, window, queueFamily);
// FIXME: to submit the command buffer, we need a queue. In the examples folder, the ImGui_ImplVulkanH_CreateOrResizeWindow function is called
// before the ImGui_ImplVulkan_Init function, so we don't have access to the queue yet. Here we have the queue_family that we can use to grab
// a queue from the device and submit the command buffer. It would be better to have access to the queue as suggested in the FIXME below.
var commandPool = device.CreateCommandPool(
new VkCommandPoolCreateInfo
{
QueueFamilyIndex = queueFamily,
Flags = VkCommandPoolCreateFlags.ResetCommandBufferBit,
},
null
);
var fence = device.CreateFence(new VkFenceCreateInfo { Flags = 0 }, null);
var commandBuffer = device.AllocateCommandBuffer(
new VkCommandBufferAllocateInfo { CommandPool = commandPool, Level = VkCommandBufferLevel.Primary }
);
commandBuffer.Begin(new VkCommandBufferBeginInfo { Flags = VkCommandBufferUsageFlags.OneTimeSubmitBit });
// Transition the images to the correct layout for rendering
for (var i = 0; i < window->ImageCount; i++)
{
var barrier = new VkImageMemoryBarrier
{
Image = window->Frames[i].Backbuffer,
OldLayout = VkImageLayout.Undefined,
NewLayout = VkImageLayout.PresentSrcKHR,
SrcQueueFamilyIndex = VK.QUEUE_FAMILY_IGNORED,
DstQueueFamilyIndex = VK.QUEUE_FAMILY_IGNORED,
SubresourceRange = new VkImageSubresourceRange
{
AspectMask = VkImageAspectFlags.ColorBit,
LevelCount = 1,
LayerCount = 1,
},
};
commandBuffer.PipelineBarrier(
VkPipelineStageFlags.BottomOfPipeBit,
VkPipelineStageFlags.ColorAttachmentOutputBit,
VkDependencyFlags.None,
[],
[],
[barrier]
);
}
commandBuffer.End();
queue.Submit([], [], [commandBuffer], [], fence);
device.WaitForFences([fence], true, -1);
// Destroy command buffer and fence and command pool
device.DestroyCommandPool(commandPool, null);
device.DestroyFence(fence, null);
}
public static unsafe void DestroyWindow(Instance instance, Device device, WindowImpl* window)
{
// TODO: ???
device.WaitIdle(); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals)
//vkQueueWaitIdle(bd->Queue);
for (var i = 0; i < window->ImageCount; i++)
DestroyFrame(device, &window->Frames.DataRaw[i]);
for (var i = 0; i < window->SemaphoreCount; i++)
DestroyFrameSemaphores(device, &window->FrameSemaphores.DataRaw[i]);
window->Frames.Clear();
window->FrameSemaphores.Clear();
device.DestroyRenderPass(window->RenderPass, null);
device.DestroySwapchainKHR(window->Swapchain, null);
instance.DestroySurfaceKHR(window->Surface, null);
*window = default;
}
// Also destroy old swap chain and in-flight frames data, if any.
private static unsafe void CreateWindowSwapChain(
PhysicalDevice physicalDevice,
Device device,
WindowImpl* window,
int width,
int height,
int minImageCount
)
{
var oldSwapchain = window->Swapchain;
window->Swapchain = default;
// TODO: NOOOOOOO
device.WaitIdle();
// We don't use ImGui_ImplVulkanH_DestroyWindow() because we want to preserve the old swapchain to create the new one.
// Destroy old Framebuffer
for (var i = 0; i < window->ImageCount; i++)
DestroyFrame(device, &window->Frames.DataRaw[i]);
for (var i = 0; i < window->SemaphoreCount; i++)
DestroyFrameSemaphores(device, &window->FrameSemaphores.DataRaw[i]);
window->Frames.Clear();
window->FrameSemaphores.Clear();
window->ImageCount = 0;
if (window->RenderPass.IsNotNull())
device.DestroyRenderPass(window->RenderPass, null);
// If min image count was not specified, request different count of images dependent on selected present mode
if (minImageCount is 0)
minImageCount = GetMinImageCountFromPresentMode(window->PresentMode);
// Create Swapchain
{
var cap = physicalDevice.GetSurfaceCapabilitiesKHR(window->Surface);
var info = new VkSwapchainCreateInfoKHR
{
Surface = window->Surface,
MinImageCount = minImageCount,
ImageFormat = window->SurfaceFormat.Format,
ImageColorSpace = window->SurfaceFormat.ColorSpace,
ImageArrayLayers = 1,
ImageUsage = VkImageUsageFlags.ColorAttachmentBit,
ImageSharingMode = VkSharingMode.Exclusive, // Assume that graphics family == present family
PreTransform = cap.SupportedTransforms.HasFlag(VkSurfaceTransformFlagsKHR.IdentityBitKHR)
? VkSurfaceTransformFlagsKHR.IdentityBitKHR
: cap.CurrentTransform,
CompositeAlpha = VkCompositeAlphaFlagsKHR.OpaqueBitKHR,
PresentMode = window->PresentMode,
Clipped = true,
OldSwapchain = oldSwapchain,
};
if (info.MinImageCount < cap.MinImageCount)
info.MinImageCount = cap.MinImageCount;
else if (cap.MaxImageCount != 0 && info.MinImageCount > cap.MaxImageCount)
info.MinImageCount = cap.MaxImageCount;
if (cap.CurrentExtent.Width == -1)
{
window->Width = width;
window->Height = height;
info.ImageExtent.Width = width;
info.ImageExtent.Height = height;
}
else
{
window->Width = cap.CurrentExtent.Width;
window->Height = cap.CurrentExtent.Height;
info.ImageExtent.Width = cap.CurrentExtent.Width;
info.ImageExtent.Height = cap.CurrentExtent.Height;
}
window->Swapchain = device.CreateSwapchainKHR(info, null);
window->ImageCount = device.GetSwapchainImagesKHR_Count(window->Swapchain);
Span<VkImage> backBuffers = stackalloc VkImage[window->ImageCount];
Assert(window->ImageCount >= minImageCount);
device.GetSwapchainImagesKHR_Write(window->Swapchain, backBuffers);
window->SemaphoreCount = window->ImageCount + 1;
window->Frames.Count = window->ImageCount;
window->FrameSemaphores.Count = window->SemaphoreCount;
// TODO: Do we need to zero out this memory?
// memset(window->Frames.Data, 0, window->Frames.size_in_bytes());
// memset(window->FrameSemaphores.Data, 0, window->FrameSemaphores.size_in_bytes());
for (var i = 0; i < window->ImageCount; i++)
window->Frames.Data[i].Backbuffer = backBuffers[i];
}
if (oldSwapchain.IsNotNull())
device.DestroySwapchainKHR(oldSwapchain, null);
// Create the Render Pass
{
var attachment = new VkAttachmentDescription
{
Format = window->SurfaceFormat.Format,
Samples = VkSampleCountFlags._1Bit,
LoadOp = window->ClearEnable ? VkAttachmentLoadOp.Clear : VkAttachmentLoadOp.DontCare,
StoreOp = VkAttachmentStoreOp.Store,
StencilLoadOp = VkAttachmentLoadOp.DontCare,
StencilStoreOp = VkAttachmentStoreOp.DontCare,
InitialLayout = VkImageLayout.Undefined,
FinalLayout = VkImageLayout.PresentSrcKHR,
};
var colorAttachment = new VkAttachmentReference
{
Attachment = 0,
Layout = VkImageLayout.ColorAttachmentOptimal,
};
var subPass = new VkSubpassDescription
{
PipelineBindPoint = VkPipelineBindPoint.Graphics,
ColorAttachmentCount = 1,
ColorAttachments = &colorAttachment,
};
var dependency = new VkSubpassDependency
{
SrcSubpass = VK.SUBPASS_EXTERNAL,
DstSubpass = 0,
SrcStageMask = VkPipelineStageFlags.ColorAttachmentOutputBit,
DstStageMask = VkPipelineStageFlags.ColorAttachmentOutputBit,
SrcAccessMask = 0,
DstAccessMask = VkAccessFlags.ColorAttachmentWriteBit,
};
window->RenderPass = device.CreateRenderPass(
new VkRenderPassCreateInfo
{
AttachmentCount = 1,
Attachments = &attachment,
SubpassCount = 1,
Subpasses = &subPass,
DependencyCount = 1,
Dependencies = &dependency,
},
null
);
// We do not create a pipeline by default as this is also used by examples' main.cpp,
// but secondary viewport in multi-viewport mode may want to create one with:
//ImGui_ImplVulkan_CreatePipeline(device, allocator, VK_NULL_HANDLE, window->RenderPass, VK_SAMPLE_COUNT_1_BIT, &window->Pipeline, v->Subpass);
}
// Create The Image Views
{
var info = new VkImageViewCreateInfo
{
ViewType = VkImageViewType._2D,
Format = window->SurfaceFormat.Format,
Components = new VkComponentMapping
{
R = VkComponentSwizzle.R,
G = VkComponentSwizzle.G,
B = VkComponentSwizzle.B,
A = VkComponentSwizzle.A,
},
SubresourceRange = new VkImageSubresourceRange
{
AspectMask = VkImageAspectFlags.ColorBit,
LevelCount = 1,
LayerCount = 1,
},
};
for (var i = 0; i < window->ImageCount; i++)
{
var frame = &window->Frames.DataRaw[i];
info.Image = frame->Backbuffer;
frame->BackbufferView = device.CreateImageView(info, null);
}
}
// Create Framebuffer
{
VkImageView attachment;
var info = new VkFramebufferCreateInfo
{
RenderPass = window->RenderPass,
AttachmentCount = 1,
Attachments = &attachment,
Width = window->Width,
Height = window->Height,
Layers = 1,
};
for (var i = 0; i < window->ImageCount; i++)
{
var frame = &window->Frames.DataRaw[i];
attachment = frame->BackbufferView;
frame->Framebuffer = device.CreateFramebuffer(info, null);
}
}
}
private static int GetMinImageCountFromPresentMode(VkPresentModeKHR presentMode)
{
return presentMode switch
{
VkPresentModeKHR.MailboxKHR => 3,
VkPresentModeKHR.FifoKHR => 2,
VkPresentModeKHR.FifoRelaxedKHR => 2,
VkPresentModeKHR.ImmediateKHR => 1,
_ => 1,
};
}
private static unsafe void CreateWindowCommandBuffers(
PhysicalDevice physicalDevice,
Device device,
WindowImpl* window,
int queueFamily
)
{
Assert(physicalDevice.IsNotNull() && device.IsNotNull());
// Create Command Buffers
for (var i = 0; i < window->ImageCount; i++)
{
var frame = &window->Frames.DataRaw[i];
// TODO: Seriously, each frame has its own command pool?
frame->CommandPool = device.CreateCommandPool(
new VkCommandPoolCreateInfo { QueueFamilyIndex = queueFamily },
null
);
frame->CommandBuffer = device.AllocateCommandBuffer(
new VkCommandBufferAllocateInfo
{
CommandPool = frame->CommandPool,
Level = VkCommandBufferLevel.Primary,
}
);
frame->Fence = device.CreateFence(
new VkFenceCreateInfo { Flags = VkFenceCreateFlags.SignaledBit },
null
);
}
for (var i = 0; i < window->SemaphoreCount; i++)
{
var semaphores = &window->FrameSemaphores.DataRaw[i];
{
var createInfo = new VkSemaphoreCreateInfo();
semaphores->ImageAcquiredSemaphore = device.CreateSemaphore(createInfo, null);
semaphores->RenderCompleteSemaphore = device.CreateSemaphore(createInfo, null);
}
}
}
private static unsafe void DestroyFrame(Device device, Frame* frame)
{
device.DestroyFence(frame->Fence, null);
device.DestroyCommandPool(frame->CommandPool, null);
frame->Fence = default;
frame->CommandBuffer = default;
frame->CommandPool = default;
device.DestroyImageView(frame->BackbufferView, null);
device.DestroyFramebuffer(frame->Framebuffer, null);
}
private static unsafe void DestroyFrameSemaphores(Device device, FrameSemaphores* semaphores)
{
device.DestroySemaphore(semaphores->ImageAcquiredSemaphore, null);
device.DestroySemaphore(semaphores->RenderCompleteSemaphore, null);
semaphores->ImageAcquiredSemaphore = semaphores->RenderCompleteSemaphore = default;
}
}
}

File diff suppressed because it is too large Load diff