From 252c0a6f37bbec9b8fdd9017fb6770e0970e9e9a Mon Sep 17 00:00:00 2001 From: rdb Date: Thu, 29 Jan 2026 21:09:19 +0100 Subject: [PATCH] display: Make context current before dispatching compute shader --- .../cocoaGLGraphicsStateGuardian.h | 2 ++ .../cocoaGLGraphicsStateGuardian.mm | 12 ++++++++++ panda/src/display/graphicsEngine.cxx | 1 + panda/src/display/graphicsStateGuardian.cxx | 9 +++++++ panda/src/display/graphicsStateGuardian.h | 1 + .../egldisplay/eglGraphicsStateGuardian.cxx | 24 +++++++++++++++++++ .../src/egldisplay/eglGraphicsStateGuardian.h | 2 ++ .../glxdisplay/glxGraphicsStateGuardian.cxx | 13 ++++++++++ .../src/glxdisplay/glxGraphicsStateGuardian.h | 2 ++ 9 files changed, 66 insertions(+) diff --git a/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.h b/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.h index 00cbe9ce55..769561a792 100644 --- a/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.h +++ b/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.h @@ -39,6 +39,8 @@ public: virtual ~CocoaGLGraphicsStateGuardian(); + virtual bool make_current(); + INLINE void lock_context(); INLINE void unlock_context(); diff --git a/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.mm b/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.mm index 9cacb3e270..254b867fc7 100644 --- a/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.mm +++ b/panda/src/cocoagldisplay/cocoaGLGraphicsStateGuardian.mm @@ -297,6 +297,18 @@ choose_pixel_format(const FrameBufferProperties &properties, } } +/** + * Ensures that the context is current. May return false if the context cannot + * be bound without a window. + */ +bool CocoaGLGraphicsStateGuardian:: +make_current() { + lock_context(); + [_context makeCurrentContext]; + unlock_context(); + return true; +} + /** * Queries the runtime version of OpenGL in use. */ diff --git a/panda/src/display/graphicsEngine.cxx b/panda/src/display/graphicsEngine.cxx index c8fcf9500f..9263b956be 100644 --- a/panda/src/display/graphicsEngine.cxx +++ b/panda/src/display/graphicsEngine.cxx @@ -1206,6 +1206,7 @@ dispatch_compute(const LVecBase3i &work_groups, const RenderState *state, Graphi nassertv(gsg != nullptr); run_on_draw_thread([=] () { + gsg->make_current(); gsg->push_group_marker(std::string("Compute ") + shader->get_filename(Shader::ST_compute).get_basename()); gsg->set_state_and_transform(state, TransformState::make_identity()); gsg->dispatch_compute(work_groups[0], work_groups[1], work_groups[2]); diff --git a/panda/src/display/graphicsStateGuardian.cxx b/panda/src/display/graphicsStateGuardian.cxx index 409b8ce440..528f6830ec 100644 --- a/panda/src/display/graphicsStateGuardian.cxx +++ b/panda/src/display/graphicsStateGuardian.cxx @@ -3084,6 +3084,15 @@ reset() { _is_valid = true; } +/** + * Ensures that the context is current. May return false if the context cannot + * be bound without a window. + */ +bool GraphicsStateGuardian:: +make_current() { + return false; +} + /** * Simultaneously resets the render state and the transform state. * diff --git a/panda/src/display/graphicsStateGuardian.h b/panda/src/display/graphicsStateGuardian.h index 8d62382f18..eea337ead8 100644 --- a/panda/src/display/graphicsStateGuardian.h +++ b/panda/src/display/graphicsStateGuardian.h @@ -421,6 +421,7 @@ public: INLINE bool reset_if_new(); INLINE void mark_new(); virtual void reset(); + virtual bool make_current(); INLINE CPT(TransformState) get_external_transform() const; INLINE CPT(TransformState) get_internal_transform() const; diff --git a/panda/src/egldisplay/eglGraphicsStateGuardian.cxx b/panda/src/egldisplay/eglGraphicsStateGuardian.cxx index f4348f26ca..08451c1896 100644 --- a/panda/src/egldisplay/eglGraphicsStateGuardian.cxx +++ b/panda/src/egldisplay/eglGraphicsStateGuardian.cxx @@ -280,12 +280,36 @@ void eglGraphicsStateGuardian:: reset() { BaseGraphicsStateGuardian::reset(); +#ifdef OPENGLES + _supports_surfaceless = has_extension("GL_OES_surfaceless_context") && +#else + _supports_surfaceless = +#endif + has_extension("EGL_KHR_surfaceless_context"); + if (_gl_renderer == "Software Rasterizer") { _fbprops.set_force_software(1); _fbprops.set_force_hardware(0); } } +/** + * Ensures that the context is current. May return false if the context cannot + * be bound without a window. + */ +bool eglGraphicsStateGuardian:: +make_current() { + if (eglGetCurrentContext() == _context) { + return true; + } + + if (_supports_surfaceless) { + return eglMakeCurrent(_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, _context) != EGL_FALSE; + } + + return false; +} + /** * Returns true if the runtime GLX version number is at least the indicated * value, false otherwise. diff --git a/panda/src/egldisplay/eglGraphicsStateGuardian.h b/panda/src/egldisplay/eglGraphicsStateGuardian.h index 94b1c724d6..d5beb11509 100644 --- a/panda/src/egldisplay/eglGraphicsStateGuardian.h +++ b/panda/src/egldisplay/eglGraphicsStateGuardian.h @@ -49,6 +49,7 @@ public: virtual ~eglGraphicsStateGuardian(); virtual void reset(); + virtual bool make_current(); bool egl_is_at_least_version(int major_version, int minor_version) const; @@ -60,6 +61,7 @@ public: #endif EGLConfig _fbconfig; FrameBufferProperties _fbprops; + bool _supports_surfaceless = false; protected: virtual void gl_flush() const; diff --git a/panda/src/glxdisplay/glxGraphicsStateGuardian.cxx b/panda/src/glxdisplay/glxGraphicsStateGuardian.cxx index cdaf98800b..f32c998495 100644 --- a/panda/src/glxdisplay/glxGraphicsStateGuardian.cxx +++ b/panda/src/glxdisplay/glxGraphicsStateGuardian.cxx @@ -434,6 +434,19 @@ choose_pixel_format(const FrameBufferProperties &properties, _context_has_pbuffer = false; } +/** + * Ensures that the context is current. May return false if the context cannot + * be bound without a window. + */ +bool glxGraphicsStateGuardian:: +make_current() { + if (glXGetCurrentContext() == _context) { + return true; + } + + return false; +} + /** * Returns true if the runtime GLX version number is at least the indicated * value, false otherwise. diff --git a/panda/src/glxdisplay/glxGraphicsStateGuardian.h b/panda/src/glxdisplay/glxGraphicsStateGuardian.h index 6e13a67484..1c3fd80dd6 100644 --- a/panda/src/glxdisplay/glxGraphicsStateGuardian.h +++ b/panda/src/glxdisplay/glxGraphicsStateGuardian.h @@ -88,6 +88,8 @@ public: virtual ~glxGraphicsStateGuardian(); + virtual bool make_current(); + bool glx_is_at_least_version(int major_version, int minor_version) const; GLXContext _share_context;