Skip to content

Fix viewport scaling for high DPI displays - geometry now properly fitted and centered#55

Draft
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-viewport-high-dpi-issues
Draft

Fix viewport scaling for high DPI displays - geometry now properly fitted and centered#55
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-viewport-high-dpi-issues

Conversation

Copy link
Contributor

Copilot AI commented Oct 13, 2025

Problem

On high DPI displays (1.5x, 2x scaling, etc.), the viewport was not correctly fitting or centering geometry. Objects would appear smaller than expected, and zoom-to-extents would fail to properly frame the content. This made the viewport difficult to use on modern high-resolution displays.

Root Cause

The viewport code was inconsistently mixing physical pixel dimensions (RenderWidth/RenderHeight) with logical dimensions (Width/Height). On high DPI displays, these differ significantly:

  • Logical dimensions (Width/Height): Control size in logical pixels (e.g., 800×600)
  • Physical dimensions (RenderWidth/RenderHeight): Actual framebuffer size scaled by DPI (e.g., 1600×1200 on 2×)

The orthographic projection, zoom calculations, and coordinate conversions were using physical dimensions, causing:

  1. View frustum to be 2× too large in world space, making objects appear 2× smaller
  2. Zoom-to-extents to divide by the wrong dimensions, resulting in incorrect zoom levels
  3. Mouse coordinates to be incorrectly transformed, breaking interaction

Solution

Changed the viewport to consistently use logical dimensions throughout the rendering pipeline:

Files Modified

  • VeldridDriver_Draw.cs: Orthographic projection, grid, and axes rendering now use Width/Height
  • VeldridDriver_Public.cs: zoomExtents() zoom calculation now uses logical dimensions
  • VeldridDriver_Conversions.cs: WorldToScreen() and ScreenToWorld() conversions use logical dimensions
  • VeldridDriver_Handlers.cs: Mouse coordinates no longer scaled by LogicalPixelSize

Mathematical Example

For world space geometry with bounds 0-1000 units:

Before (2× DPI):

zoomLevel = 1000 / 1600 = 0.625  // Too zoomed out!

After (2× DPI):

zoomLevel = 1000 / 800 = 1.25    // Correct!

Key Insight

The framebuffer size should only be used when creating GPU resources (swapchain, buffers). For all viewport calculations—projection matrix, zoom levels, coordinate transformations—we must use logical dimensions to maintain consistent behavior across different DPI settings.

Testing

  • ✅ All 437 existing unit tests pass
  • ✅ No compilation errors or warnings introduced
  • ✅ Consistent behavior validated mathematically for 1×, 1.5×, and 2× DPI

Documentation

Added comprehensive documentation:

  • VIEWPORT_DPI_FIX.md: Detailed explanation of the problem, solution, and technical implementation
  • DPI_SCALING_VALIDATION.md: Mathematical validation with concrete examples for different DPI scenarios

This fix ensures geometry is properly fitted and centered regardless of display DPI scaling, making the viewport usable on modern high-resolution displays.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • invalid.nonexistent.host
    • Triggering command: /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/DesignLibs_GPL/DesignLibs_GPL/artifacts/bin/UnitTests/Debug/net8.0/UnitTests.runtimeconfig.json --depsfile /home/REDACTED/work/DesignLibs_GPL/DesignLibs_GPL/artifacts/bin/UnitTests/Debug/net8.0/UnitTests.deps.json /home/REDACTED/work/DesignLibs_GPL/DesignLibs_GPL/artifacts/bin/UnitTests/Debug/net8.0/testhost.dll --port 45589 --endpoint 127.0.0.1:045589 --role client --parentprocessid 4026 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/DesignLibs_GPL/DesignLibs_GPL/artifacts/bin/UnitTests/Debug/net8.0/UnitTests.runtimeconfig.json --depsfile /home/REDACTED/work/DesignLibs_GPL/DesignLibs_GPL/artifacts/bin/UnitTests/Debug/net8.0/UnitTests.deps.json /home/REDACTED/work/DesignLibs_GPL/DesignLibs_GPL/artifacts/bin/UnitTests/Debug/net8.0/testhost.dll --port 36073 --endpoint 127.0.0.1:036073 --role client --parentprocessid 5001 --telemetryoptedin false (dns block)
  • t1.symcb.com
    • Triggering command: dotnet restore (dns block)
  • t2.symcb.com
    • Triggering command: dotnet restore (dns block)
  • tl.symcb.com
    • Triggering command: dotnet restore (dns block)
  • tl.symcd.com
    • Triggering command: dotnet restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Something still isn't quite right about the viewport on scaled high DPI. The geometry is not fitted correctly and the view is not centered.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits October 13, 2025 19:31
Changed viewport to use logical dimensions (Width/Height) instead of physical pixels (RenderWidth/RenderHeight) throughout:
- Draw() orthographic projection now uses logical size
- zoomExtents() zoom calculation uses logical size
- WorldToScreen and ScreenToWorld conversions use logical size
- Grid and axes rendering use logical size
- Mouse coordinate handling no longer scales by LogicalPixelSize

This ensures geometry is properly fitted and centered regardless of DPI scaling.

Co-authored-by: philstopford <1983851+philstopford@users.noreply.github.com>
Co-authored-by: philstopford <1983851+philstopford@users.noreply.github.com>
Co-authored-by: philstopford <1983851+philstopford@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix viewport scaling issues on high DPI displays Fix viewport scaling for high DPI displays - geometry now properly fitted and centered Oct 13, 2025
Copilot AI requested a review from philstopford October 13, 2025 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants