To enable high DPI (HiDPI) display support in programs using the bgfx graphics rendering library, you need to ensure that your application properly handles high-resolution displays, especially on platforms like Windows, macOS, or Linux, where HiDPI scaling is common. Below are the steps and considerations to enable HiDPI support in bgfx-based applications:
1. Understand bgfx's Role in HiDPI Support
- bgfx is a cross-platform rendering library that abstracts graphics APIs (e.g., OpenGL, Direct3D, Vulkan, etc.). It does not directly handle window creation or HiDPI scaling; instead, it relies on the windowing system or platform abstraction layer (e.g., SDL, GLFW, or native platform APIs) to manage HiDPI settings.
- To enable HiDPI support, you need to configure the windowing system to be HiDPI-aware and ensure bgfx renders at the appropriate resolution.
2. Choose a Windowing Library
bgfx does not create windows itself; it relies on a windowing library or platform abstraction layer. Common choices include:
- GLFW: A popular cross-platform library for creating windows and handling input.
- SDL: Another cross-platform library for windowing and input.
- Native APIs: Platform-specific APIs (e.g., Win32 on Windows, Cocoa on macOS, X11/Wayland on Linux).
The steps to enable HiDPI support depend on the windowing library you use. Below, we cover the most common libraries (GLFW and SDL).
3. Enable HiDPI Support in GLFW
If your bgfx application uses GLFW for window creation, follow these steps:
a. Set GLFW to Be HiDPI-Aware
- Before creating a window, enable HiDPI support by calling
glfwWindowHint with the appropriate settings.
- Example code:
#include <GLFW/glfw3.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
int main()
{
// Initialize GLFW
if (!glfwInit())
return -1;
// Enable HiDPI support (important for macOS Retina and Windows scaling)
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); // Enable HiDPI scaling
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // bgfx handles the graphics API
// Create a window
GLFWwindow* window = glfwCreateWindow(1280, 720, "bgfx HiDPI Example", nullptr, nullptr);
if (!window)
{
glfwTerminate();
return -1;
}
// Get the framebuffer size (actual pixel resolution, accounting for HiDPI scaling)
int framebufferWidth, framebufferHeight;
glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHeight);
// Get the window size (logical size, in screen coordinates)
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
// Calculate the DPI scale factor
float dpiScaleX = (float)framebufferWidth / windowWidth;
float dpiScaleY = (float)framebufferHeight / windowHeight;
// Set up bgfx platform data
bgfx::PlatformData pd;
pd.nwh = glfwGetWin32Window(window); // For Windows; use appropriate native handle for other platforms
bgfx::setPlatformData(pd);
// Initialize bgfx
bgfx::Init init;
init.type = bgfx::RendererType::Count; // Auto-select renderer
init.resolution.width = framebufferWidth; // Use framebuffer size for rendering
init.resolution.height = framebufferHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(init))
return -1;
// Main loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
// Handle window resize (important for HiDPI scaling)
glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHeight);
bgfx::reset(framebufferWidth, framebufferHeight, BGFX_RESET_VSYNC);
bgfx::setViewRect(0, 0, 0, framebufferWidth, framebufferHeight);
// Clear the screen
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0);
bgfx::touch(0);
// Render frame
bgfx::frame();
}
// Cleanup
bgfx::shutdown();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
b. Key Points for GLFW
- Use
glfwGetFramebufferSize to get the actual pixel resolution of the window (which accounts for HiDPI scaling) instead of glfwGetWindowSize (which returns logical screen coordinates).
- Pass the framebuffer size to
bgfx::reset and bgfx::setViewRect to ensure bgfx renders at the full HiDPI resolution.
- On macOS, GLFW automatically handles Retina displays if
GLFW_SCALE_TO_MONITOR is enabled.
- On Windows, ensure your application is DPI-aware (see Step 5 below).
4. Enable HiDPI Support in SDL
If your bgfx application uses SDL for window creation, follow these steps:
a. Set SDL to Be HiDPI-Aware
- SDL provides functions to query the actual pixel resolution of the window, accounting for HiDPI scaling.
- Example code:
#include <SDL2/SDL.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
int main(int argc, char* argv[])
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return -1;
// Create an SDL window
SDL_Window* window = SDL_CreateWindow("bgfx HiDPI Example",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1280, 720,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if (!window)
{
SDL_Quit();
return -1;
}
// Get the drawable size (actual pixel resolution, accounting for HiDPI scaling)
int drawableWidth, drawableHeight;
SDL_GL_GetDrawableSize(window, &drawableWidth, &drawableHeight);
// Set up bgfx platform data
bgfx::PlatformData pd;
pd.nwh = SDL_SysWMinfo_GetWindowHandle(window); // Platform-specific handle
bgfx::setPlatformData(pd);
// Initialize bgfx
bgfx::Init init;
init.type = bgfx::RendererType::Count; // Auto-select renderer
init.resolution.width = drawableWidth; // Use drawable size for rendering
init.resolution.height = drawableHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(init))
return -1;
// Main loop
bool running = true;
while (running)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
running = false;
else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED)
{
// Handle window resize (important for HiDPI scaling)
SDL_GL_GetDrawableSize(window, &drawableWidth, &drawableHeight);
bgfx::reset(drawableWidth, drawableHeight, BGFX_RESET_VSYNC);
bgfx::setViewRect(0, 0, 0, drawableWidth, drawableHeight);
}
}
// Clear the screen
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0);
bgfx::touch(0);
// Render frame
bgfx::frame();
}
// Cleanup
bgfx::shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
b. Key Points for SDL
- Use
SDL_GL_GetDrawableSize to get the actual pixel resolution of the window (which accounts for HiDPI scaling) instead of SDL_GetWindowSize (which returns logical screen coordinates).
- Pass the drawable size to
bgfx::reset and bgfx::setViewRect to ensure bgfx renders at the full HiDPI resolution.
- On macOS, SDL automatically handles Retina displays if
SDL_WINDOW_ALLOW_HIGHDPI is set.
- On Windows, ensure your application is DPI-aware (see Step 5 below).
5. Ensure DPI Awareness on Windows
On Windows, applications must explicitly declare themselves as DPI-aware to avoid being scaled by the operating system, which can result in blurry rendering. This is especially important for HiDPI displays.
a. Set DPI Awareness Programmatically
b. Set DPI Awareness via Manifest File
6. Handle Dynamic DPI Changes
On platforms like Windows, users can change the DPI scaling factor while your application is running (e.g., by dragging the window to a monitor with a different DPI). To handle this:
- Listen for window resize events (e.g.,
GLFWwindowcontentscalefun in GLFW or SDL_WINDOWEVENT_RESIZED in SDL).
- Query the new framebuffer/drawable size and update bgfx's resolution using
bgfx::reset and bgfx::setViewRect.
7. Test on HiDPI Displays
- Test your application on HiDPI displays (e.g., Retina displays on macOS, 4K monitors on Windows with scaling enabled) to ensure rendering is sharp and not blurry.
- Verify that UI elements, text, and other graphics scale appropriately with the DPI.
8. Additional Considerations
- UI Scaling: If your application has a user interface (e.g., ImGui), ensure the UI library is also HiDPI-aware and scales its elements based on the DPI scale factor.
- Performance: Rendering at HiDPI resolutions requires more GPU resources. Optimize your rendering pipeline if needed.
- Cross-Platform Testing: HiDPI behavior varies across platforms, so test on Windows, macOS, and Linux to ensure consistent results.
By following these steps, you can enable high DPI display support in your bgfx-based application, ensuring sharp and properly scaled rendering on modern high-resolution displays.