{"record":{"id":"2855d6e11dbb0b7a","repo":"unoplatform/uno","slug":"choosepixelformat-failed","errorCode":null,"errorMessage":"ChoosePixelFormat failed","messagePattern":"ChoosePixelFormat failed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/AddIns/Uno.WinUI.Graphics3DGL/Windows/WinUINativeOpenGLWrapper.cs","lineNumber":63,"sourceCode":"\t\tpfd.iPixelType = WindowsRenderingNativeMethods.PFD_TYPE_RGBA;\n\t\tpfd.cColorBits = 32;\n\t\tpfd.cRedBits = 8;\n\t\tpfd.cGreenBits = 8;\n\t\tpfd.cBlueBits = 8;\n\t\tpfd.cAlphaBits = 8;\n\t\tpfd.cDepthBits = 16;\n\t\tpfd.cStencilBits = 1; // anything > 0 is fine, we will most likely get 8\n\t\tpfd.iLayerType = WindowsRenderingNativeMethods.PFD_MAIN_PLANE;\n\n\t\tvar pixelFormat = WindowsRenderingNativeMethods.ChoosePixelFormat(_hdc, ref pfd);\n\n\t\t// To inspect the chosen pixel format:\n\t\t// WindowsRenderingNativeMethods.PIXELFORMATDESCRIPTOR temp_pfd = default;\n\t\t// WindowsRenderingNativeMethods.DescribePixelFormat(_hdc, _pixelFormat, (uint)Marshal.SizeOf<WindowsRenderingNativeMethods.PIXELFORMATDESCRIPTOR>(), ref temp_pfd);\n\n\t\tif (pixelFormat == 0)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"ChoosePixelFormat failed\");\n\t\t}\n\n\t\tif (WindowsRenderingNativeMethods.SetPixelFormat(_hdc, pixelFormat, ref pfd) == 0)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"SetPixelFormat failed\");\n\t\t}\n\n\t\t_glContext = WindowsRenderingNativeMethods.wglCreateContext(_hdc);\n\n\t\tif (_glContext == IntPtr.Zero)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"wglCreateContext failed\");\n\t\t}\n\t}\n\n\t// https://sharovarskyi.com/blog/posts/csharp-win32-opengl-silknet/\n\tpublic bool TryGetProcAddress(string proc, out nint addr)\n\t{","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.WinUI.Graphics3DGL/Windows/WinUINativeOpenGLWrapper.cs#L45-L81","documentation":"WinUINativeOpenGLWrapper (Windows OpenGL host for GLCanvasElement) calls GDI ChoosePixelFormat on the window's HDC with a PIXELFORMATDESCRIPTOR requesting double-buffer, RGBA, 24-bit color, 8-bit stencil, 16-bit depth. ChoosePixelFormat returns 0 when no pixel format on the device matches the descriptor or the HDC is invalid. The wrapper throws InvalidOperationException, halting GL context creation.","triggerScenarios":"HDC is null/invalid (window not realized before init); the display driver exposes no pixel format matching the requested depth/stencil/double-buffer combination; running over Remote Desktop / a software renderer with reduced format support; the DC was already released.","commonSituations":"Headless or RDP session with no hardware accelerated pixel format; the OpenGL host initialized before the HWND/HDC is ready; outdated/missing GPU drivers; conflicting pixel format already set on the DC (a DC can only have its pixel format set once).","solutions":["Ensure the window is created and its HDC is valid before calling ChoosePixelFormat (realize the HWND first).","Relax the PIXELFORMATDESCRIPTOR (drop stencil, reduce depth bits) and retry; some software renderers only match minimal formats.","Update/repair GPU drivers; on RDP, enable RemoteFX or test locally.","Confirm the DC has not already had a different pixel format applied (it cannot be changed after SetPixelFormat on some implementations)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (_hdc == IntPtr.Zero) throw new InvalidOperationException(\"HDC not ready; realize the window first.\");\nvar pf = WindowsRenderingNativeMethods.ChoosePixelFormat(_hdc, ref pfd);\nif (pf == 0) { /* log GetLastError, relax descriptor, retry */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the HWND is created and its HDC valid before pixel format selection.","Relax depth/stencil requirements if no format matches.","Update GPU drivers; avoid RDP sessions without GPU/RemoteFX for GL."],"tags":["opengl","windows","pixelformat","native-interop","gdi"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}