{"record":{"id":"24c5de7629907ba5","repo":"unoplatform/uno","slug":"setpixelformat-failed","errorCode":null,"errorMessage":"SetPixelFormat failed","messagePattern":"SetPixelFormat failed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/AddIns/Uno.WinUI.Graphics3DGL/Windows/WinUINativeOpenGLWrapper.cs","lineNumber":68,"sourceCode":"\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{\n\t\tif (_opengl32.Value != IntPtr.Zero && NativeLibrary.TryGetExport(_opengl32.Value, proc, out addr))\n\t\t{\n\t\t\treturn true;\n\t\t}\n","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.WinUI.Graphics3DGL/Windows/WinUINativeOpenGLWrapper.cs#L50-L86","documentation":"Immediately after ChoosePixelFormat succeeds, the wrapper calls SetPixelFormat(hdc, pixelFormat, &pfd); it returns 0 on failure and the wrapper throws InvalidOperationException. On Windows, SetPixelFormat can only succeed once per HDC — a second attempt on a DC whose pixel format is already set will fail. It can also fail if the pixel format index is invalid for the DC or the DC is invalid.","triggerScenarios":"SetPixelFormat called twice on the same HDC (e.g. GLCanvasElement re-initializing its context after a recreate); the HDC differs from the one passed to ChoosePixelFormat; the chosen index is out of range for the DC.","commonSituations":"Window/context re-creation reusing a DC whose pixel format was already set; multiple GL hosts sharing a window's DC; reinitializing the GL wrapper after a device-loss event.","solutions":["Reuse the existing pixel format if the DC already has one (call DescribePixelFormat to check) instead of calling SetPixelFormat again.","Create a fresh HDC/HWND for a new GL context rather than reusing one whose pixel format is locked.","Ensure the same HDC is passed to ChoosePixelFormat and SetPixelFormat."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// A DC's pixel format can be set only once\nif (WindowsRenderingNativeMethods.DescribePixelFormat(_hdc, 1, 0, ref default) == 0) { /* ok to set */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call SetPixelFormat twice on the same HDC.","Allocate a fresh HDC/HWND for each new GL context.","Pass the exact HDC used for ChoosePixelFormat."],"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"}