To enable high DPI display support for applications using the bgfx graphics library, follow these steps. The approach varies by platform and involves both OS-level settings and code adjustments to handle DPI scaling correctly.
1. Platform-Specific Setup
Windows
- Declare DPI Awareness (Choose one method):
- Application Manifest (Recommended):
<!-- Add to your .exe manifest -->
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
</assembly>
- Programmatic Declaration (Early in
main()):
#include <ShellScalingAPI.h>
// ...
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); // Windows 8.1+
// OR for Windows 10 1607+
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
macOS
Linux
- No universal method; depends on the toolkit (e.g., GDK_SCALE for GTK). Ensure your window manager honors DPI settings.
2. Windowing System Integration
bgfx relies on the windowing library (e.g., GLFW, SDL) to report correct framebuffer sizes. Configure your windowing library for high DPI:
GLFW
- Enable DPI Scaling:
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); // GLFW 3.3+
// macOS specific (if needed):
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_TRUE);
- Use Framebuffer Size (not window size):
int width, height;
glfwGetFramebufferSize(window, &width, &height); // Always use this!
bgfx::reset(width, height, ...);
- Handle Resize Events:
glfwSetFramebufferSizeCallback(window, [](GLFWwindow* win, int w, int h) {
bgfx::reset(w, h, ...);
});
SDL
- Enable High DPI:
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0"); // Enable high DPI
- Use Drawable Size:
int drawW, drawH;
SDL_GL_GetDrawableSize(window, &drawW, &drawH); // Actual pixel size
bgfx::reset(drawW, drawH, ...);
- Resize Handling:
// In event loop for SDL_WINDOWEVENT_SIZE_CHANGED:
SDL_GL_GetDrawableSize(window, &drawW, &drawH);
bgfx::reset(drawW, drawH, ...);
3. bgfx Initialization
Pass the actual framebuffer size (pixels) to bgfx:
// Example: GLFW
int fbWidth, fbHeight;
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
bgfx::Init init;
init.resolution.width = fbWidth; // Use framebuffer size!
init.resolution.height = fbHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
bgfx::init(init);
4. Handle DPI Scaling in UI (If Applicable)
If rendering UI (e.g., ImGui), scale elements based on DPI:
// Get DPI scale factor (example for GLFW)
float xscale, yscale;
glfwGetWindowContentScale(window, &xscale, &yscale);
const float dpiScale = xscale; // Use for UI scaling
// In ImGui:
ImGui::GetIO().FontGlobalScale = dpiScale;
ImGui::GetStyle().ScaleAllSizes(dpiScale);
5. Testing
- Windows: Move the window between monitors with different DPIs.
- macOS: Test on a Retina display.
- Verify the framebuffer size (from
glfwGetFramebufferSize/SDL_GL_GetDrawableSize) matches the physical resolution.
Troubleshooting
- Blurry Text/Graphics:
Ensure you’re using the framebuffer size (pixels), not window size (logical coordinates).
- Incorrect Window Size:
Handle WM_DPICHANGED on Windows or SDL_WINDOWEVENT_SIZE_CHANGED in SDL to resize properly.
- Initialization Order:
Set DPI awareness before creating the window.
By following these steps, your bgfx application will correctly render at high DPI resolutions. Key takeaways:
- Declare DPI awareness at the OS level.
- Use framebuffer sizes (not window sizes) for bgfx.
- Configure your windowing library for high DPI support.