From 6b9655eba976ab907d37964883008c3291733f01 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Thu, 28 Mar 2024 18:00:30 +0100 Subject: [PATCH] Fix #120007: Crash rendering when using OptiX and Grease Pencil on recent drivers The issue seems to be caused with the recent NVidia drivers, which will crash if OptiX context is created prior to the OpenGL context on Linux. This happens with drivers 545.29.06, 550.54.14, 550.67 and kernel 6.8 on Ubuntu 24.04, and also happens with NVidia driver 550.67 on Gentoo. While it does seem the issue is likely on the driver side, the timeline for it being resolved there is unknown. Until then it is possible to apply workaround on Blender side, which will initialize GPU context prior to rendering with an external render engine when it is known the GPU context will be needed later on. Note this is not a complex fix, because in general it's still possible to render a frame without and then with grease pencil, in which case OptiX will still be initialized first. But we don't want to always initialize OpenGL, and we can't predict future operations. Pull Request: https://projects.blender.org/blender/blender/pulls/120026 --- source/blender/render/intern/engine.cc | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/source/blender/render/intern/engine.cc b/source/blender/render/intern/engine.cc index aa0ee392248..cb73984798a 100644 --- a/source/blender/render/intern/engine.cc +++ b/source/blender/render/intern/engine.cc @@ -854,6 +854,28 @@ static void engine_render_view_layer(Render *re, if (use_gpu_context) { DRW_render_context_enable(engine->re); } + else if (engine->has_grease_pencil && use_grease_pencil && G.background) { + /* Workaround for specific NVidia drivers which crash on Linux when OptiX context is + * initialized prior to OpenGL context. This affects driver versions 545.29.06, 550.54.14, + * and 550.67 running on kernel 6.8. + * + * The idea here is to initialize GPU context before giving control to the render engine in + * cases when we know that the GPU context will definitely be needed later on. + * + * Only do it for background renders to avoid possible extra global locking during the + * context initialization. For the non-background renders the GPU context is already + * initialized for the Blender interface and no workaround is needed. + * + * Technically it is enough to only call WM_init_gpu() here, but it expects to only be called + * once, and from here it is not possible to know whether GPU sub-system is initialized or + * not. So instead temporarily enable the render context, which will take care of the GPU + * context initialization. + * + * For demo file and tracking progress of possible fixes on driver side refer to #120007. */ + DRW_render_context_enable(engine->re); + DRW_render_context_disable(engine->re); + } + if (engine->type->update) { engine->type->update(engine, re->main, engine->depsgraph); }