private unsafe void UpdateMonitors() { if (p_sdl_GetNumVideoDisplays == null) { p_sdl_GetNumVideoDisplays = Sdl2Native.LoadFunction <SDL_GetNumVideoDisplays_t>("SDL_GetNumVideoDisplays"); } if (p_sdl_GetDisplayUsableBounds_t == null) { p_sdl_GetDisplayUsableBounds_t = Sdl2Native.LoadFunction <SDL_GetDisplayUsableBounds_t>("SDL_GetDisplayUsableBounds"); } ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); Marshal.FreeHGlobal(platformIO.NativePtr->Monitors.Data); int numMonitors = p_sdl_GetNumVideoDisplays(); IntPtr data = Marshal.AllocHGlobal(Unsafe.SizeOf <ImGuiPlatformMonitor>() * numMonitors); platformIO.NativePtr->Monitors = new ImVector(numMonitors, numMonitors, data); for (int i = 0; i < numMonitors; i++) { Rectangle r; p_sdl_GetDisplayUsableBounds_t(i, &r); ImGuiPlatformMonitorPtr monitor = platformIO.Monitors[i]; monitor.DpiScale = 1f; monitor.MainPos = new Vector2(r.X, r.Y); monitor.MainSize = new Vector2(r.Width, r.Height); monitor.WorkPos = new Vector2(r.X, r.Y); monitor.WorkSize = new Vector2(r.Width, r.Height); } }
/// <summary> /// Renders the ImGui draw list data. /// This method requires a <see cref="GraphicsDevice"/> because it may create new DeviceBuffers if the size of vertex /// or index data has increased beyond the capacity of the existing buffers. /// A <see cref="CommandList"/> is needed to submit drawing and resource update commands. /// </summary> public void Render(GraphicsDevice gd, CommandList cl) { if (_frameBegun) { _frameBegun = false; ImGui.Render(); RenderImDrawData(ImGui.GetDrawData(), gd, cl); // Update and Render additional Platform Windows if ((ImGui.GetIO().ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0) { ImGui.UpdatePlatformWindows(); ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); for (int i = 1; i < platformIO.Viewports.Size; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; VeldridImGuiWindow?window = (VeldridImGuiWindow?)GCHandle.FromIntPtr(vp.PlatformUserData).Target; if (window == null) { throw new NullReferenceException(); } if (window.Swapchain != null) { cl.SetFramebuffer(window.Swapchain.Framebuffer); RenderImDrawData(vp.DrawData, gd, cl); } } } } }
/// <summary> /// Renders the ImGui draw list data. /// This method requires a <see cref="GraphicsDevice"/> because it may create new DeviceBuffers if the size of vertex /// or index data has increased beyond the capacity of the existing buffers. /// A <see cref="CommandList"/> is needed to submit drawing and resource update commands. /// </summary> public void Render(GraphicsDevice gd, CommandList cl) { #if DEBUG using Profiler fullProfiler = new Profiler(GetType()); #endif if (frameBegun) { frameBegun = false; ImGui.Render(); if (!mainWindow.Minimized) { RenderImDrawData(ImGui.GetDrawData(), gd, cl); } // Update and Render additional Platform Windows if ((ImGui.GetIO().ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0) { ImGui.UpdatePlatformWindows(); ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); for (int i = 1; i < platformIO.Viewports.Size; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; if ((vp.Flags & ImGuiViewportFlags.Minimized) == 0) { WindowBase window = (WindowBase)GCHandle.FromIntPtr(vp.PlatformUserData).Target; cl.SetFramebuffer(window.Swapchain.Framebuffer); RenderImDrawData(vp.DrawData, gd, cl); } } } } }
public void SwapExtraWindows(GraphicsDevice gd) { ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); for (int i = 1; i < platformIO.Viewports.Size; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; gd.SwapBuffers(window.Swapchain); } }
private static void SetUpMonitorData() { ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); var platformMonitors = new List <ImGuiPlatformMonitor>(); { Monitor *[] monitors = GLFW.GetMonitors(); for (int i = 0; i < monitors.Length; i++) { Monitor * monitor = monitors[i]; ImGuiPlatformMonitor m = new ImGuiPlatformMonitor(); var vidMode = GLFW.GetVideoMode(monitor); GLFW.GetMonitorPos(monitor, out int x, out int y); m.MainPos = m.WorkPos = new Vector2(x, y); m.MainSize = m.WorkSize = new Vector2(vidMode->Width, vidMode->Height); GLFW.GetMonitorContentScale(monitor, out float xScale, out float yScale); m.DpiScale = xScale; platformMonitors.Add(m); } } _monitorData = platformMonitors.ToArray(); _monitorDataPin = GCHandle.Alloc(_monitorData, GCHandleType.Pinned); var platformMonitorsPtrs = new ImGuiPlatformMonitorPtr[_monitorData.Length]; for (int i = 0; i < _monitorData.Length; i++) { fixed(ImGuiPlatformMonitor *ptr = &_monitorData[i]) { platformMonitorsPtrs[i] = new ImGuiPlatformMonitorPtr(ptr); } } _monitorPtrData = platformMonitorsPtrs; _monitorPtrDataPin = GCHandle.Alloc(_monitorPtrData, GCHandleType.Pinned); _monitorsPtrVector = new ImVector <ImGuiPlatformMonitor>(_monitorPtrData.Length, _monitorPtrData.Length, _monitorDataPin.AddrOfPinnedObject()); { ImGuiPlatformIO *ptr = platformIO; ImVector v = Unsafe.As <ImVector <ImGuiPlatformMonitor>, ImVector>(ref _monitorsPtrVector); ptr->Monitors = v; } }
public void SwapExtraWindows(GraphicsDevice gd) { ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); for (int i = 1; i < platformIO.Viewports.Size; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; VeldridImGuiWindow?window = (VeldridImGuiWindow?)GCHandle.FromIntPtr(vp.PlatformUserData).Target; if (window == null) { throw new NullReferenceException(); } gd.SwapBuffers(window.Swapchain); } }
/// <summary> /// Swaps Buffers for additional windows created with imgui /// </summary> public void SwapBuffers(GraphicsDevice gd) { #if DEBUG using Profiler fullProfiler = new Profiler(GetType()); #endif if ((ImGui.GetIO().ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0) { ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); for (int i = 1; i < platformIO.Viewports.Size; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; WindowBase window = (WindowBase)GCHandle.FromIntPtr(vp.PlatformUserData).Target; gd.SwapBuffers(window.Swapchain); } } }
private void InitPlatformInterface() { ImGuiPlatformIOPtr ptr = ImGui.GetPlatformIO(); _createWindow = CreateWindow; _destroyWindow = DestroyWindow; _setWindowSize = SetWindowSize; _renderWindow = RenderWindow; _swapBuffers = SwapBuffers; ptr.Renderer_CreateWindow = Marshal.GetFunctionPointerForDelegate(_createWindow); ptr.Renderer_DestroyWindow = Marshal.GetFunctionPointerForDelegate(_destroyWindow); ptr.Renderer_SetWindowSize = Marshal.GetFunctionPointerForDelegate(_setWindowSize); ptr.Renderer_RenderWindow = Marshal.GetFunctionPointerForDelegate(_renderWindow); ptr.Renderer_SwapBuffers = Marshal.GetFunctionPointerForDelegate(_swapBuffers); }
/// <summary> /// Sets per-frame data based on the associated window. /// This is called by Update(float). /// </summary> private void SetPerFrameImGuiData(float deltaSeconds) { #if DEBUG using Profiler fullProfiler = new Profiler(GetType()); #endif ImGuiIOPtr io = ImGui.GetIO(); io.DisplaySize = new Vector2( windowWidth / scaleFactor.X, windowHeight / scaleFactor.Y); io.DisplayFramebufferScale = scaleFactor; io.DeltaTime = deltaSeconds; // DeltaTime is in seconds. ImGuiPlatformIOPtr plIo = ImGui.GetPlatformIO(); Sdl2Window mainSdlWindow = mainWindow.SdlWindow; plIo.MainViewport.Pos = new Vector2(mainSdlWindow.X, mainSdlWindow.Y); plIo.MainViewport.Size = new Vector2(mainSdlWindow.Width, mainSdlWindow.Height); }
private unsafe void SetupPlatformIo(Sdl2Window sdlWindow) { #if DEBUG using Profiler fullProfiler = new Profiler(GetType()); #endif ImGuiStylePtr style = ImGui.GetStyle(); style.WindowRounding = 0.0f; style.Colors[(int)ImGuiCol.WindowBg].W = 1.0f; createWindow = PlatformCreateWindow; destroyWindow = PlatformDestroyWindow; showWindow = PlatformShowWindow; getWindowPosition = PlatformGetWindowPosition; setWindowPosition = PlatformSetWindowPosition; getWindowSize = PlatformGetWindowSize; setWindowSize = PlatformSetWindowSize; setWindowTitle = PlatformSetWindowTitle; getWindowFocus = PlatformGetWindowFocus; setWindowFocus = PlatformSetWindowFocus; getWindowMinimized = PlatformGetWindowMinimized; setWindowAlpha = PlatformSetWindowAlpha; createVulkanSurface = PlatformCreateVkSurface; ImGuiPlatformIOPtr plIo = ImGui.GetPlatformIO(); plIo.NativePtr->Platform_CreateWindow = Marshal.GetFunctionPointerForDelegate(createWindow); plIo.NativePtr->Platform_DestroyWindow = Marshal.GetFunctionPointerForDelegate(destroyWindow); plIo.NativePtr->Platform_ShowWindow = Marshal.GetFunctionPointerForDelegate(showWindow); plIo.NativePtr->Platform_GetWindowPos = Marshal.GetFunctionPointerForDelegate(getWindowPosition); plIo.NativePtr->Platform_SetWindowPos = Marshal.GetFunctionPointerForDelegate(setWindowPosition); plIo.NativePtr->Platform_GetWindowSize = Marshal.GetFunctionPointerForDelegate(getWindowSize); plIo.NativePtr->Platform_SetWindowSize = Marshal.GetFunctionPointerForDelegate(setWindowSize); plIo.NativePtr->Platform_SetWindowTitle = Marshal.GetFunctionPointerForDelegate(setWindowTitle); plIo.NativePtr->Platform_GetWindowMinimized = Marshal.GetFunctionPointerForDelegate(getWindowMinimized); plIo.NativePtr->Platform_GetWindowFocus = Marshal.GetFunctionPointerForDelegate(getWindowFocus); plIo.NativePtr->Platform_SetWindowFocus = Marshal.GetFunctionPointerForDelegate(setWindowFocus); plIo.NativePtr->Platform_SetWindowAlpha = Marshal.GetFunctionPointerForDelegate(setWindowAlpha); plIo.NativePtr->Platform_CreateVkSurface = Marshal.GetFunctionPointerForDelegate(createVulkanSurface); UpdateMonitors(); ImGuiViewportPtr mainViewport = plIo.MainViewport; mainViewport.PlatformHandle = sdlWindow.Handle; mainViewport.PlatformUserData = (IntPtr)mainWindow.GcHandle; }
private unsafe static void UpdateMonitors() { #if DEBUG using Profiler fullProfiler = new Profiler(typeof(ImGuiController)); #endif ImGuiPlatformIOPtr plIo = ImGui.GetPlatformIO(); Marshal.FreeHGlobal(plIo.NativePtr->Monitors.Data); int numMonitors = Sdl_GetNumVideoDisplays(); IntPtr data = Marshal.AllocHGlobal(Unsafe.SizeOf <ImGuiPlatformMonitor>() * numMonitors); plIo.NativePtr->Monitors = new ImVector(numMonitors, numMonitors, data); for (int i = 0; i < numMonitors; i++) { Rectangle r; Sdl_GetDisplayUsableBounds(i, &r); ImGuiPlatformMonitorPtr monitor = plIo.Monitors[i]; monitor.DpiScale = 1f; monitor.MainPos = new Vector2(r.X, r.Y); monitor.MainSize = new Vector2(r.Width, r.Height); monitor.WorkPos = new Vector2(r.X, r.Y); monitor.WorkSize = new Vector2(r.Width, r.Height); } }
/// <summary> /// Constructs a new ImGuiController. /// </summary> public unsafe ImGuiController(GraphicsDevice gd, Sdl2Window window, OutputDescription outputDescription, int width, int height) { _gd = gd; _window = window; _windowWidth = width; _windowHeight = height; IntPtr context = ImGui.CreateContext(); ImGui.SetCurrentContext(context); ImGuiIOPtr io = ImGui.GetIO(); io.ConfigFlags |= ImGuiConfigFlags.DockingEnable; io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable; ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); ImGuiViewportPtr mainViewport = platformIO.MainViewport; mainViewport.PlatformHandle = window.Handle; _mainViewportWindow = new VeldridImGuiWindow(gd, mainViewport, _window); _createWindow = CreateWindow; _destroyWindow = DestroyWindow; _getWindowPos = GetWindowPos; _showWindow = ShowWindow; _setWindowPos = SetWindowPos; _setWindowSize = SetWindowSize; _getWindowSize = GetWindowSize; _setWindowFocus = SetWindowFocus; _getWindowFocus = GetWindowFocus; _getWindowMinimized = GetWindowMinimized; _setWindowTitle = SetWindowTitle; platformIO.Platform_CreateWindow = Marshal.GetFunctionPointerForDelegate(_createWindow); platformIO.Platform_DestroyWindow = Marshal.GetFunctionPointerForDelegate(_destroyWindow); platformIO.Platform_ShowWindow = Marshal.GetFunctionPointerForDelegate(_showWindow); platformIO.Platform_SetWindowPos = Marshal.GetFunctionPointerForDelegate(_setWindowPos); platformIO.Platform_SetWindowSize = Marshal.GetFunctionPointerForDelegate(_setWindowSize); platformIO.Platform_SetWindowFocus = Marshal.GetFunctionPointerForDelegate(_setWindowFocus); platformIO.Platform_GetWindowFocus = Marshal.GetFunctionPointerForDelegate(_getWindowFocus); platformIO.Platform_GetWindowMinimized = Marshal.GetFunctionPointerForDelegate(_getWindowMinimized); platformIO.Platform_SetWindowTitle = Marshal.GetFunctionPointerForDelegate(_setWindowTitle); ImGuiNative.ImGuiPlatformIO_Set_Platform_GetWindowPos(platformIO.NativePtr, Marshal.GetFunctionPointerForDelegate(_getWindowPos)); ImGuiNative.ImGuiPlatformIO_Set_Platform_GetWindowSize(platformIO.NativePtr, Marshal.GetFunctionPointerForDelegate(_getWindowSize)); unsafe { io.NativePtr->BackendPlatformName = (byte *)new FixedAsciiString("Veldrid.SDL2 Backend").DataPtr; } io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors; io.BackendFlags |= ImGuiBackendFlags.HasSetMousePos; io.BackendFlags |= ImGuiBackendFlags.PlatformHasViewports; io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports; io.Fonts.AddFontDefault(); CreateDeviceResources(gd, outputDescription); SetKeyMappings(); SetPerFrameImGuiData(1f / 60f); UpdateMonitors(); ImGui.NewFrame(); _frameBegun = true; }
/// <summary> /// Constructs a new ImGuiController. /// </summary> public unsafe ImGuiView(GraphicsDevice gd, Sdl2Window window, OutputDescription outputDescription, int width, int height) { _gd = gd; _window = window; _windowWidth = width; _windowHeight = height; IntPtr context = ImGui.CreateContext(); ImGui.SetCurrentContext(context); ImGuiIOPtr io = ImGui.GetIO(); io.ConfigFlags |= ImGuiConfigFlags.DockingEnable; io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable; ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); ImGuiViewportPtr mainViewport = platformIO.MainViewport; mainViewport.PlatformHandle = window.Handle; _mainViewportWindow = new VeldridImGuiWindow(gd, mainViewport, _window); _createWindow = CreateWindow; _destroyWindow = DestroyWindow; _getWindowPos = GetWindowPos; _showWindow = ShowWindow; _setWindowPos = SetWindowPos; _setWindowSize = SetWindowSize; _getWindowSize = GetWindowSize; _setWindowFocus = SetWindowFocus; _getWindowFocus = GetWindowFocus; _getWindowMinimized = GetWindowMinimized; _setWindowTitle = SetWindowTitle; platformIO.Platform_CreateWindow = Marshal.GetFunctionPointerForDelegate(_createWindow); platformIO.Platform_DestroyWindow = Marshal.GetFunctionPointerForDelegate(_destroyWindow); platformIO.Platform_ShowWindow = Marshal.GetFunctionPointerForDelegate(_showWindow); platformIO.Platform_SetWindowPos = Marshal.GetFunctionPointerForDelegate(_setWindowPos); platformIO.Platform_SetWindowSize = Marshal.GetFunctionPointerForDelegate(_setWindowSize); platformIO.Platform_SetWindowFocus = Marshal.GetFunctionPointerForDelegate(_setWindowFocus); platformIO.Platform_GetWindowFocus = Marshal.GetFunctionPointerForDelegate(_getWindowFocus); platformIO.Platform_GetWindowMinimized = Marshal.GetFunctionPointerForDelegate(_getWindowMinimized); platformIO.Platform_SetWindowTitle = Marshal.GetFunctionPointerForDelegate(_setWindowTitle); ImGuiNative.ImGuiPlatformIO_Set_Platform_GetWindowPos(platformIO.NativePtr, Marshal.GetFunctionPointerForDelegate(_getWindowPos)); ImGuiNative.ImGuiPlatformIO_Set_Platform_GetWindowSize(platformIO.NativePtr, Marshal.GetFunctionPointerForDelegate(_getWindowSize)); unsafe { io.NativePtr->BackendPlatformName = (byte *)new FixedAsciiString("Veldrid.SDL2 Backend").DataPtr; } io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors; io.BackendFlags |= ImGuiBackendFlags.HasSetMousePos; io.BackendFlags |= ImGuiBackendFlags.PlatformHasViewports; io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports; io.Fonts.AddFontDefault(); // Create general resources _gd = gd; ResourceFactory factory = gd.ResourceFactory; _vertexBuffer = factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic)); _vertexBuffer.Name = "ImGui.NET Vertex Buffer"; _indexBuffer = factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic)); _indexBuffer.Name = "ImGui.NET Index Buffer"; byte *pixels; int fwidth, fheight, bytesPerPixel; io.Fonts.GetTexDataAsRGBA32(out pixels, out fwidth, out fheight, out bytesPerPixel); // Store our identifier io.Fonts.SetTexID(_fontAtlasID); _fontTexture = gd.ResourceFactory.CreateTexture(TextureDescription.Texture2D( (uint)fwidth, (uint)fheight, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Sampled)); _fontTexture.Name = "ImGui.NET Font Texture"; gd.UpdateTexture( _fontTexture, (IntPtr)pixels, (uint)(bytesPerPixel * fwidth * fheight), 0, 0, 0, (uint)fwidth, (uint)fheight, 1, 0, 0); _fontTextureView = gd.ResourceFactory.CreateTextureView(_fontTexture); io.Fonts.ClearTexData(); _projMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _projMatrixBuffer.Name = "ImGui.NET Projection Buffer"; byte[] vertexShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex", ShaderStages.Vertex); byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment); _vertexShader = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, "main")); _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, "main")); VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("in_position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("in_texCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("in_color", VertexElementSemantic.Color, VertexElementFormat.Byte4_Norm)) }; _layout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("MainSampler", ResourceKind.Sampler, ShaderStages.Fragment))); _textureLayout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("MainTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment))); GraphicsPipelineDescription pd = new GraphicsPipelineDescription( BlendStateDescription.SingleAlphaBlend, new DepthStencilStateDescription(false, false, ComparisonKind.Always), new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, false, true), PrimitiveTopology.TriangleList, new ShaderSetDescription(vertexLayouts, new[] { _vertexShader, _fragmentShader }), new ResourceLayout[] { _layout, _textureLayout }, outputDescription); _pipeline = factory.CreateGraphicsPipeline(ref pd); _mainResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout, _projMatrixBuffer, gd.PointSampler)); _fontTextureResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_textureLayout, _fontTextureView)); SetKeyMappings(); SetPerFrameImGuiData(1f / 60f, width, height); UpdateMonitors(); ImGui.NewFrame(); _frameBegun = true; }