Skip to content

Commit 2e8eaf6

Browse files
authored
Improve ImGui fractional scaling support (#2614)
* Improve ImGui fractional scaling support * Update window size in SetPerFrameImGuiData, Resize not raised on Wayland
1 parent 57e0f86 commit 2e8eaf6

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/Input/Silk.NET.Input/Silk.NET.Input.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0-android</TargetFrameworks>
5+
<TargetFrameworks Condition="!$([MSBuild]::IsOsPlatform('Linux'))">$(TargetFrameworks);net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
56
<SilkMetapackage>true</SilkMetapackage>
67
</PropertyGroup>
78

src/OpenGL/Extensions/Silk.NET.OpenGL.Extensions.ImGui/ImGuiController.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,19 @@ public void Update(float deltaSeconds)
236236
private void SetPerFrameImGuiData(float deltaSeconds)
237237
{
238238
var io = ImGuiNET.ImGui.GetIO();
239+
var size = _view.Size;
240+
var framebufferSize = _view.FramebufferSize;
241+
242+
// Update here since Resize isn't raised on all platforms
243+
_windowWidth = size.X;
244+
_windowHeight = size.Y;
245+
239246
io.DisplaySize = new Vector2(_windowWidth, _windowHeight);
240247

241248
if (_windowWidth > 0 && _windowHeight > 0)
242249
{
243-
io.DisplayFramebufferScale = new Vector2(_view.FramebufferSize.X / _windowWidth,
244-
_view.FramebufferSize.Y / _windowHeight);
250+
io.DisplayFramebufferScale = new Vector2((float) framebufferSize.X / _windowWidth,
251+
(float) framebufferSize.Y / _windowHeight);
245252
}
246253

247254
io.DeltaTime = deltaSeconds; // DeltaTime is in seconds.
@@ -427,6 +434,9 @@ private unsafe void SetupRenderState(ImDrawDataPtr drawDataPtr, int framebufferW
427434
_gl.PolygonMode(GLEnum.FrontAndBack, GLEnum.Fill);
428435
#endif
429436

437+
// Setup viewport, orthographic projection matrix
438+
_gl.Viewport(0, 0, (uint) framebufferWidth, (uint) framebufferHeight);
439+
430440
float L = drawDataPtr.DisplayPos.X;
431441
float R = drawDataPtr.DisplayPos.X + drawDataPtr.DisplaySize.X;
432442
float T = drawDataPtr.DisplayPos.Y;
@@ -488,6 +498,8 @@ private unsafe void RenderImDrawData(ImDrawDataPtr drawDataPtr)
488498
_gl.GetInteger(GLEnum.PolygonMode, lastPolygonMode);
489499
#endif
490500

501+
Span<int> lastViewport = stackalloc int[4];
502+
_gl.GetInteger(GLEnum.Viewport, lastViewport);
491503
Span<int> lastScissorBox = stackalloc int[4];
492504
_gl.GetInteger(GLEnum.ScissorBox, lastScissorBox);
493505

@@ -637,6 +649,7 @@ private unsafe void RenderImDrawData(ImDrawDataPtr drawDataPtr)
637649
_gl.PolygonMode(GLEnum.FrontAndBack, (GLEnum) lastPolygonMode[0]);
638650
#endif
639651

652+
_gl.Viewport(lastViewport[0], lastViewport[1], (uint) lastViewport[2], (uint) lastViewport[3]);
640653
_gl.Scissor(lastScissorBox[0], lastScissorBox[1], (uint) lastScissorBox[2], (uint) lastScissorBox[3]);
641654
}
642655

src/Windowing/Silk.NET.GLFW/Glfw.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,38 @@ public unsafe partial WindowHandle* CreateWindow
20632063
/// </remarks>
20642064
public unsafe partial void GetFramebufferSize(WindowHandle* window, out int width, out int height);
20652065

2066+
/// <summary>
2067+
/// <para>
2068+
/// This function retrieves the content scale for the specified window.
2069+
/// </para>
2070+
/// <para>
2071+
/// The content scale is the ratio between the current DPI and the platform's default DPI.
2072+
/// </para>
2073+
/// <para>
2074+
/// If you scale all pixel dimensions by this scale then your content should appear at an appropriate size.
2075+
/// This is especially important for text and any UI elements.
2076+
/// </para>
2077+
/// <para>
2078+
/// On platforms where each monitor can have its own content scale, the window content scale
2079+
/// depends on which monitor the system considers the window to be on.
2080+
/// </para>
2081+
/// </summary>
2082+
/// <param name="window">The window to query.</param>
2083+
/// <param name="xscale">Where to store the x-axis content scale, or <c>out _</c>.</param>
2084+
/// <param name="yscale">Where to store the y-axis content scale, or <c>out _</c>.</param>
2085+
/// <remarks>
2086+
/// <para>
2087+
/// This function must only be called from the main thread.
2088+
/// </para>
2089+
/// <para>
2090+
/// Possible errors include <see cref="ErrorCode.NotInitialized" /> and <see cref="ErrorCode.PlatformError" />.
2091+
/// </para>
2092+
/// </remarks>
2093+
/// <seealso cref="GetMonitorContentScale" />
2094+
// ReSharper disable IdentifierTypo
2095+
public unsafe partial void GetWindowContentScale(WindowHandle* window, out float xscale, out float yscale);
2096+
// ReSharper enable IdentifierTypo
2097+
20662098
/// <summary>
20672099
/// <para>
20682100
/// This function returns the value of an input option for the specified window.

0 commit comments

Comments
 (0)