{"record":{"id":"b76d1711c08d8f19","repo":"wezterm/wezterm","slug":"setpixelformat-function-failed","errorCode":null,"errorMessage":"SetPixelFormat function failed: {}","messagePattern":"SetPixelFormat function failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"window/src/os/windows/wgl.rs","lineNumber":281,"sourceCode":"\n        let res = unsafe {\n            DescribePixelFormat(\n                hdc,\n                format_id,\n                std::mem::size_of::<PIXELFORMATDESCRIPTOR>() as _,\n                &mut pfd,\n            )\n        };\n        if res == 0 {\n            anyhow::bail!(\n                \"DescribePixelFormat function failed: {}\",\n                std::io::Error::last_os_error()\n            );\n        }\n\n        let res = unsafe { SetPixelFormat(hdc, format_id, &pfd) };\n        if res == 0 {\n            anyhow::bail!(\n                \"SetPixelFormat function failed: {}\",\n                std::io::Error::last_os_error()\n            );\n        }\n\n        let mut attribs = vec![\n            CONTEXT_MAJOR_VERSION_ARB as i32,\n            4,\n            CONTEXT_MINOR_VERSION_ARB as i32,\n            5,\n            CONTEXT_PROFILE_MASK_ARB as i32,\n            CONTEXT_CORE_PROFILE_BIT_ARB as i32,\n        ];\n\n        if has_extension(&extensions, \"WGL_ARB_create_context_robustness\") {\n            log::trace!(\"requesting robustness features\");\n            attribs.push(CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB as i32);\n            attribs.push(LOSE_CONTEXT_ON_RESET_ARB as i32);","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/wezterm/wezterm/blob/3ff7522b9617ec920f7fbdb22b57e93d42d93ee9/window/src/os/windows/wgl.rs#L263-L299","documentation":"SetPixelFormat returned 0 applying the chosen format to the window's DC. The classic cause: on Windows an HDC's pixel format can be set only once for the lifetime of the window — a second SetPixelFormat on the same HWND/DC fails. Also triggered by invalid hdc/format ids, or formats owned by a different driver.","triggerScenarios":"Calling enable_opengl a second time on the same window (re-init, reconnect, pane respawn) after the pixel format was already set; two code paths racing to initialize GL on one window; keeping a cached DC after the format was applied elsewhere.","commonSituations":"Recreating a GL context after context loss without creating a new window; multiple renderers attaching to one surface; teardown/reinit cycles in long-running apps.","solutions":["Before setting, call GetPixelFormat(hdc): if it returns nonzero, reuse that id (DescribePixelFormat on it) and skip SetPixelFormat entirely","Create a fresh HWND when a different pixel format is genuinely required","Ensure only one component owns GL initialization per window"],"exampleFix":"// before\nlet res = unsafe { SetPixelFormat(hdc, format_id, &pfd) };\nif res == 0 {\n    anyhow::bail!(\"SetPixelFormat function failed: {}\", std::io::Error::last_os_error());\n}\n\n// after\nlet existing = unsafe { GetPixelFormat(hdc) };\nif existing == 0 {\n    let res = unsafe { SetPixelFormat(hdc, format_id, &pfd) };\n    if res == 0 {\n        anyhow::bail!(\"SetPixelFormat function failed: {}\", std::io::Error::last_os_error());\n    }\n} // else: format already fixed for this window; reuse it","handlingStrategy":"validation","validationCode":"// Respect the one-format-per-window rule before setting\nlet existing = unsafe { GetPixelFormat(hdc) };\nif existing != 0 {\n    // format already chosen for this window: describe and reuse it\n    let mut pfd: PIXELFORMATDESCRIPTOR = unsafe { std::mem::zeroed() };\n    unsafe { DescribePixelFormat(hdc, existing, std::mem::size_of::<PIXELFORMATDESCRIPTOR>() as _, &mut pfd) };\n    return Ok(existing);\n}","typeGuard":null,"tryCatchPattern":"let res = unsafe { SetPixelFormat(hdc, format_id, &pfd) };\nif res == 0 {\n    let e = std::io::Error::last_os_error();\n    if e.raw_os_error() == Some(2000) /* ERROR_INVALID_PIXEL_FORMAT */ || unsafe { GetPixelFormat(hdc) } != 0 {\n        // already set: reuse the existing format instead of failing\n        return Ok(unsafe { GetPixelFormat(hdc) });\n    }\n    anyhow::bail!(\"SetPixelFormat function failed: {e}\");\n}","preventionTips":["Remember SetPixelFormat succeeds only once per HWND/DC: design re-init to reuse, not re-set","Create a new window when a different pixel format is truly required","Have exactly one owner of GL initialization per window to avoid racing setters"],"tags":["windows","wgl","opengl","pixelformat","setpixelformat"],"backgroundTag":"pixel-format-not-supported","analyzedSha":"3ff7522b9617ec920f7fbdb22b57e93d42d93ee9","analyzedAt":"2026-08-20T04:47:31.434Z","contentChangedAt":"2026-08-20T04:47:31.434Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}