Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

'Avalonia.OpenGL.OpenGlException': Unable to make the context current

Open afcsoft opened this issue 4 years ago • 9 comments

Describe the bug

OpenGL rendering controls works just fine but about in 2 minutes, it crashes with this exception:

'Avalonia.OpenGL.OpenGlException': Unable to make the context current

Also, nobody returns my e-mail about paid support request. Hope you guys are okay.

To Reproduce

  1. Open XamlControlGallery project
  2. Run it
  3. Just wait for crash.

Screenshots image image

  • OS: Windows 10 Pro Version: 20H2 Build: 19042.746
  • PC:
    • CPU: Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz 1.80 GHz
    • GPU: Intel UHD Graphics 620 , Radeon(TM) 520
  • Avalonia Version 0.10.0

Additional context Logs:

[Visual] Exception in render loop: 'Avalonia.OpenGL.OpenGlException: Unable to make the context current
   at Avalonia.Win32.OpenGl.WglRestoreContext..ctor(IntPtr gc, IntPtr context, Object monitor, Boolean takeMonitor) in /_/src/Windows/Avalonia.Win32/OpenGl/WglRestoreContext.cs:line 27
   at Avalonia.Win32.OpenGl.WglGlPlatformSurface.RenderTarget.BeginDraw() in /_/src/Windows/Avalonia.Win32/OpenGl/WglGlPlatformSurface.cs:line 52
   at Avalonia.Skia.GlRenderTarget.BeginRenderingSession() in /_/src/Skia/Avalonia.Skia/Gpu/OpenGl/GlRenderTarget.cs:line 101
   at Avalonia.Skia.SkiaGpuRenderTarget.CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) in /_/src/Skia/Avalonia.Skia/Gpu/SkiaGpuRenderTarget.cs:line 40
   at Avalonia.Rendering.DeferredRenderer.EnsureDrawingContext(IDrawingContextImpl& context) in /_/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs:line 599
   at Avalonia.Rendering.DeferredRenderer.UpdateRenderLayersAndConsumeSceneIfNeeded(IDrawingContextImpl& context, Boolean recursiveCall) in /_/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs:line 397
   at Avalonia.Rendering.DeferredRenderer.Render(Boolean forceComposite) in /_/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs:line 340
   at Avalonia.Rendering.RenderLoop.TimerTick(TimeSpan time) in /_/src/Avalonia.Visuals/Rendering/RenderLoop.cs:line 145' (RenderLoop #53857724)

afcsoft avatar Feb 25 '21 21:02 afcsoft

@kekekeks ☝️

grokys avatar Feb 25 '21 22:02 grokys

Disabling AllowEglInitialization helped a bit. Not crashing about 5 mins. Still same exception.

afcsoft avatar Feb 25 '21 23:02 afcsoft

I am getting the very same issue if WGL is enabled even for simpler apps on various Windows 10 and Windows 11 machines.

2022-01-14 00:02:20.037 | Avalonia             | TID[     7] | W:8/32765/32767 C:8/1000/1000 | Error     |  [Visual] Exception in render loop: 'Avalonia.OpenGL.OpenGlException: Unable to make the context current
   at Avalonia.Win32.OpenGl.WglGlPlatformSurface.RenderTarget.BeginDraw() in /_/src/Windows/Avalonia.Win32/OpenGl/WglGlPlatformSurface.cs:line 47
   at Avalonia.Skia.GlRenderTarget.BeginRenderingSession() in /_/src/Skia/Avalonia.Skia/Gpu/OpenGl/GlRenderTarget.cs:line 101
   at Avalonia.Skia.SkiaGpuRenderTarget.CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) in /_/src/Skia/Avalonia.Skia/Gpu/SkiaGpuRenderTarget.cs:line 40
   at Avalonia.Rendering.DeferredRenderer.EnsureDrawingContext(IDrawingContextImpl& context) in /_/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs:line 618
   at Avalonia.Rendering.DeferredRenderer.UpdateRenderLayersAndConsumeSceneIfNeeded(IDrawingContextImpl& context, Boolean recursiveCall) in /_/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs:line 416
   at Avalonia.Rendering.DeferredRenderer.Render(Boolean forceComposite) in /_/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs:line 359
   at Avalonia.Rendering.DeferredRenderer.Avalonia.Rendering.IRenderLoopTask.Render() in /_/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs:line 277
   at Avalonia.Rendering.RenderLoop.TimerTick(TimeSpan time) in /_/src/Avalonia.Visuals/Rendering/RenderLoop.cs:line 147' (RenderLoop #35236192)

You can see that in TID the thread id is 7, hence, not the UI thread which would be 1.

Maybe this helps...

llfab avatar Jan 14 '22 00:01 llfab

Still happening. But randomly. I'm not sure what's causing it image

Hanprogramer avatar Mar 09 '22 14:03 Hanprogramer

Is there any update on this issue or plan on working on it? It is still happening. This should be a very high priority bug as it is essentially a non-starter for any project that wants to use OpenGL within Avalonia on Windows.

jmb10101 avatar May 22 '22 22:05 jmb10101

There are plans to finish and merge https://github.com/AvaloniaUI/Avalonia/pull/6391 before 11.0. That should be way more stable.

kekekeks avatar May 23 '22 09:05 kekekeks

That is strange, I got the same exception on X11

[OpenGL] Unable to initialize GLX-based rendering: 'Avalonia.OpenGL.OpenGlException: Unable to find a suitable GL function for CreateContextAttribsARB
   at Avalonia.OpenGL.GlInterfaceBase`1..ctor(Func`2 getProcAddress, TContext context) in /_/src/Avalonia.OpenGL/GlInterfaceBase.cs:line 58
   at Avalonia.OpenGL.GlInterfaceBase..ctor(Func`2 getProcAddress) in /_/src/Avalonia.OpenGL/GlInterfaceBase.cs:line 11
   at Avalonia.X11.Glx.GlxInterface..ctor() in /_/src/Avalonia.X11/Glx/Glx.cs:line 110
   at Avalonia.X11.Glx.GlxDisplay..ctor(X11Info x11, IList`1 probeProfiles) in /_/src/Avalonia.X11/Glx/GlxDisplay.cs:line 20
   at Avalonia.X11.Glx.GlxPlatformOpenGlInterface.TryCreate(X11Info x11, IList`1 glProfiles) in /_/src/Avalonia.X11/Glx/GlxPlatformFeature.cs:line 34'

StipecMV avatar Oct 24 '22 10:10 StipecMV

@StipecMV that's a completely different issue. Your glXGetProcAddress has failed for glXCreateContextAttribsARB. Which Linux distro are you using?

kekekeks avatar Oct 25 '22 06:10 kekekeks

Any updates?

afcsoft avatar Oct 25 '22 13:10 afcsoft

Still happening, please fix this issue, as this is very disturbing. The issue happens only on NVidia graphics cards. I know this because I was writing my on UI Framework and having the same issue. The best way to handle this is just to add Try/Catch when drawing and handle specifically OpenGlException, then just print the exception instead

image

Hanprogramer avatar Jan 06 '23 07:01 Hanprogramer

Still happening, please fix this issue, as this is very disturbing. The issue happens only on NVidia graphics cards. I know this because I was writing my on UI Framework and having the same issue. The best way to handle this is just to add Try/Catch when drawing and handle specifically OpenGlException

image

"The issue happens only on Nvidia graphics cards" is incorrect. Intel/AMD/Nvidia does not matter. It throws this exception randomly. It is just an OpenGL implementation error.

afcsoft avatar Jan 06 '23 07:01 afcsoft

Still happening, please fix this issue, as this is very disturbing. The issue happens only on NVidia graphics cards. I know this because I was writing my on UI Framework and having the same issue. The best way to handle this is just to add Try/Catch when drawing and handle specifically OpenGlException image

"The issue happens only on Nvidia graphics cards" is incorrect. Intel/AMD/Nvidia does not matter. It throws this exception randomly. It is just an OpenGL implementation error.

What caused the error then? I'm curious. Is it about threading? Edit: if i changed my application settings from the NVidia control panel to "High performance NVidia graphics" which uses the dedicated GPU instead of integrated (by default). The error goes away

Edit2: yeah i forgot i did send a screenshot before from my AMD machine. But still edit1 is something we should think about

Hanprogramer avatar Jan 06 '23 07:01 Hanprogramer

It is just an OpenGL implementation error. And your last statement contradicts with your first statement. Which one is true?

afcsoft avatar Jan 06 '23 08:01 afcsoft

I thought it might be from threading. But i checked again without threading turned on on my own UI framework, still the same. Turning on "high performance" mode on nvidia still solves the issue, but we can't expect end users to do this.

Hanprogramer avatar Jan 06 '23 08:01 Hanprogramer

I don't think windows or any driver will change to active gpu during the runtime. So that may be true that integrated gpus do have a problem with avalonia's opengl control. But my tests shows the almost opposite. This exception occurs very randomly. That does not mean it won't happen. It is likely to happen anyone that uses avalonia's opengl control in an app. This error will never get a solution from avalonia team, (This is just my guess) It is possible that they can't replicate the issue at all.

afcsoft avatar Jan 06 '23 08:01 afcsoft

I don't think windows or any driver will change to active gpu during the runtime. So that may be true that integrated gpus do have a problem with avalonia's opengl control. But my tests shows the almost opposite. This exception occurs very randomly. That does not mean it won't happen. It is likely to happen anyone that uses avalonia's opengl control in an app. This error will never get a solution from avalonia team, (This is just my guess) It is possible that they can't replicate the issue at all.

Not just Avalonia though, my own UI library also have the same problem. It uses Silk.NET OpenGL for backend and SkiaSharp. Could either be problem with OpenGl and .NET itself. But this is just my guess

EDIT: Using the dedicated graphics device no longer fix my problem. Idk why

EDIT2: Here's how i fixed it on my own ui library:


                try
                {
                    SkiaCtx.Flush();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

Hanprogramer avatar Jan 06 '23 09:01 Hanprogramer

Should be fixed with https://github.com/AvaloniaUI/Avalonia/pull/9639 Also, UseWGL is not required and not recommended after https://github.com/AvaloniaUI/Avalonia/issues/9925

maxkatz6 avatar Feb 01 '23 18:02 maxkatz6