From 055c0c3072d3bf732e76c3ef3edfea2b3bcb360b Mon Sep 17 00:00:00 2001 From: John Parton Date: Fri, 16 Jan 2026 12:37:11 -0600 Subject: [PATCH] Fix Windows OpenGL: Fail immediately when pixel format not available Previously, if the requested pixel format (including sRGB) was not available on Windows, wglChoosePixelFormatARB would return num_formats=0, but the code would proceed with an invalid pixel_format value of 0, leading to undefined behavior. This commit adds proper validation to match Linux behavior: - Check if wglChoosePixelFormatARB found a matching format (num_formats <= 0) - Check if pixel_format is invalid (== 0) - Return GlError::CreationFailed immediately if validation fails - Properly clean up (ReleaseDC) before returning error - Add debug message indicating the failure and whether sRGB was requested This makes Windows behave consistently with the Linux/GLX implementation, which also fails immediately with InvalidFBConfig if the requested framebuffer configuration is not supported. --- src/gl/win.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/gl/win.rs b/src/gl/win.rs index 097eb092..29847a70 100644 --- a/src/gl/win.rs +++ b/src/gl/win.rs @@ -233,6 +233,12 @@ impl GlContext { &mut num_formats, ); + if num_formats <= 0 || pixel_format == 0 { + eprintln!("Error: Failed to find matching pixel format (sRGB requested: {})", config.srgb); + ReleaseDC(hwnd, hdc); + return Err(GlError::CreationFailed(())); + } + let mut pfd: PIXELFORMATDESCRIPTOR = std::mem::zeroed(); DescribePixelFormat( hdc,