[Bf-blender-cvs] [69806f1f88d] cycles-x: Fix graphics interop destruction for offline rendering in Cycles X

Sergey Sharybin noreply at git.blender.org
Fri Sep 10 09:56:53 CEST 2021


Commit: 69806f1f88d4c78b752d902381e94a3e9e668321
Author: Sergey Sharybin
Date:   Thu Sep 9 18:30:18 2021 +0200
Branches: cycles-x
https://developer.blender.org/rB69806f1f88d4c78b752d902381e94a3e9e668321

Fix graphics interop destruction for offline rendering in Cycles X

Graphics interop API is to be sued from within an active graphics
context, which might not be a case for offline rendering where
there is no default graphics context bound.

Differential Revision: https://developer.blender.org/D12446

===================================================================

M	intern/cycles/blender/blender_gpu_display.cpp
M	intern/cycles/blender/blender_gpu_display.h
M	intern/cycles/integrator/path_trace.cpp
M	intern/cycles/integrator/path_trace.h
M	intern/cycles/integrator/path_trace_work.h
M	intern/cycles/integrator/path_trace_work_cpu.cpp
M	intern/cycles/integrator/path_trace_work_cpu.h
M	intern/cycles/integrator/path_trace_work_gpu.cpp
M	intern/cycles/integrator/path_trace_work_gpu.h
M	intern/cycles/render/gpu_display.cpp
M	intern/cycles/render/gpu_display.h

===================================================================

diff --git a/intern/cycles/blender/blender_gpu_display.cpp b/intern/cycles/blender/blender_gpu_display.cpp
index 48c7f67087f..29d483a008f 100644
--- a/intern/cycles/blender/blender_gpu_display.cpp
+++ b/intern/cycles/blender/blender_gpu_display.cpp
@@ -423,6 +423,24 @@ DeviceGraphicsInteropDestination BlenderGPUDisplay::do_graphics_interop_get()
   return interop_dst;
 }
 
+void BlenderGPUDisplay::graphics_interop_activate()
+{
+  if (!gl_context_) {
+    return;
+  }
+
+  WM_opengl_context_activate(gl_context_);
+}
+
+void BlenderGPUDisplay::graphics_interop_deactivate()
+{
+  if (!gl_context_) {
+    return;
+  }
+
+  WM_opengl_context_release(gl_context_);
+}
+
 /* --------------------------------------------------------------------
  * Drawing.
  */
diff --git a/intern/cycles/blender/blender_gpu_display.h b/intern/cycles/blender/blender_gpu_display.h
index ccfbc358171..8748da08d10 100644
--- a/intern/cycles/blender/blender_gpu_display.h
+++ b/intern/cycles/blender/blender_gpu_display.h
@@ -100,6 +100,9 @@ class BlenderGPUDisplay : public GPUDisplay {
   BlenderGPUDisplay(BL::RenderEngine &b_engine, BL::Scene &b_scene);
   ~BlenderGPUDisplay();
 
+  virtual void graphics_interop_activate() override;
+  virtual void graphics_interop_deactivate() override;
+
  protected:
   virtual bool do_update_begin(int texture_width, int texture_height) override;
   virtual void do_update_end() override;
diff --git a/intern/cycles/integrator/path_trace.cpp b/intern/cycles/integrator/path_trace.cpp
index 5805fc045b7..edec956e45d 100644
--- a/intern/cycles/integrator/path_trace.cpp
+++ b/intern/cycles/integrator/path_trace.cpp
@@ -64,6 +64,18 @@ PathTrace::PathTrace(Device *device,
   render_scheduler.set_need_schedule_rebalance(path_trace_works_.size() > 1);
 }
 
+PathTrace::~PathTrace()
+{
+  /* Destroy any GPU resource which was used for graphics interop.
+   * Need to have access to the GPUDisplay as it is the only source of drawing context which is
+   * used for interop. */
+  if (gpu_display_) {
+    for (auto &&path_trace_work : path_trace_works_) {
+      path_trace_work->destroy_gpu_resources(gpu_display_.get());
+    }
+  }
+}
+
 void PathTrace::load_kernels()
 {
   if (denoiser_) {
diff --git a/intern/cycles/integrator/path_trace.h b/intern/cycles/integrator/path_trace.h
index b78240308c7..7d9c965f067 100644
--- a/intern/cycles/integrator/path_trace.h
+++ b/intern/cycles/integrator/path_trace.h
@@ -55,6 +55,7 @@ class PathTrace {
             DeviceScene *device_scene,
             RenderScheduler &render_scheduler,
             TileManager &tile_manager);
+  ~PathTrace();
 
   /* Create devices and load kernels which are created on-demand (for example, denoising devices).
    * The progress is reported to the currently configure progress object (via `set_progress`). */
diff --git a/intern/cycles/integrator/path_trace_work.h b/intern/cycles/integrator/path_trace_work.h
index 0cde2d8bef5..ca64c1c2ffd 100644
--- a/intern/cycles/integrator/path_trace_work.h
+++ b/intern/cycles/integrator/path_trace_work.h
@@ -83,6 +83,8 @@ class PathTraceWork {
                                    PassMode pass_mode,
                                    int num_samples) = 0;
 
+  virtual void destroy_gpu_resources(GPUDisplay *gpu_display) = 0;
+
   /* Copy data from/to given render buffers.
    * Will copy pixels from a corresponding place (from multi-device point of view) of the render
    * buffers, and copy work's render buffers to the corresponding place of the destination. */
diff --git a/intern/cycles/integrator/path_trace_work_cpu.cpp b/intern/cycles/integrator/path_trace_work_cpu.cpp
index cf7e379018d..eaed0d0d636 100644
--- a/intern/cycles/integrator/path_trace_work_cpu.cpp
+++ b/intern/cycles/integrator/path_trace_work_cpu.cpp
@@ -179,6 +179,10 @@ void PathTraceWorkCPU::copy_to_gpu_display(GPUDisplay *gpu_display,
   gpu_display->unmap_texture_buffer();
 }
 
+void PathTraceWorkCPU::destroy_gpu_resources(GPUDisplay * /*gpu_display*/)
+{
+}
+
 bool PathTraceWorkCPU::copy_render_buffers_from_device()
 {
   return buffers_->copy_from_device();
diff --git a/intern/cycles/integrator/path_trace_work_cpu.h b/intern/cycles/integrator/path_trace_work_cpu.h
index c94f8c0fb89..0ea901e452d 100644
--- a/intern/cycles/integrator/path_trace_work_cpu.h
+++ b/intern/cycles/integrator/path_trace_work_cpu.h
@@ -51,6 +51,7 @@ class PathTraceWorkCPU : public PathTraceWork {
   virtual void copy_to_gpu_display(GPUDisplay *gpu_display,
                                    PassMode pass_mode,
                                    int num_samples) override;
+  virtual void destroy_gpu_resources(GPUDisplay *gpu_display) override;
 
   virtual bool copy_render_buffers_from_device() override;
   virtual bool copy_render_buffers_to_device() override;
diff --git a/intern/cycles/integrator/path_trace_work_gpu.cpp b/intern/cycles/integrator/path_trace_work_gpu.cpp
index 218d07bdf0e..af4d416117a 100644
--- a/intern/cycles/integrator/path_trace_work_gpu.cpp
+++ b/intern/cycles/integrator/path_trace_work_gpu.cpp
@@ -756,6 +756,16 @@ bool PathTraceWorkGPU::copy_to_gpu_display_interop(GPUDisplay *gpu_display,
   return true;
 }
 
+void PathTraceWorkGPU::destroy_gpu_resources(GPUDisplay *gpu_display)
+{
+  if (!device_graphics_interop_) {
+    return;
+  }
+  gpu_display->graphics_interop_activate();
+  device_graphics_interop_ = nullptr;
+  gpu_display->graphics_interop_deactivate();
+}
+
 void PathTraceWorkGPU::get_render_tile_film_pixels(const PassAccessor::Destination &destination,
                                                    PassMode pass_mode,
                                                    int num_samples)
diff --git a/intern/cycles/integrator/path_trace_work_gpu.h b/intern/cycles/integrator/path_trace_work_gpu.h
index 574599e5708..aee54d4a372 100644
--- a/intern/cycles/integrator/path_trace_work_gpu.h
+++ b/intern/cycles/integrator/path_trace_work_gpu.h
@@ -49,6 +49,7 @@ class PathTraceWorkGPU : public PathTraceWork {
   virtual void copy_to_gpu_display(GPUDisplay *gpu_display,
                                    PassMode pass_mode,
                                    int num_samples) override;
+  virtual void destroy_gpu_resources(GPUDisplay *gpu_display) override;
 
   virtual bool copy_render_buffers_from_device() override;
   virtual bool copy_render_buffers_to_device() override;
diff --git a/intern/cycles/render/gpu_display.cpp b/intern/cycles/render/gpu_display.cpp
index 2dfb678e61b..61ef69a82d2 100644
--- a/intern/cycles/render/gpu_display.cpp
+++ b/intern/cycles/render/gpu_display.cpp
@@ -185,6 +185,14 @@ DeviceGraphicsInteropDestination GPUDisplay::graphics_interop_get()
   return do_graphics_interop_get();
 }
 
+void GPUDisplay::graphics_interop_activate()
+{
+}
+
+void GPUDisplay::graphics_interop_deactivate()
+{
+}
+
 /* --------------------------------------------------------------------
  * Drawing.
  */
diff --git a/intern/cycles/render/gpu_display.h b/intern/cycles/render/gpu_display.h
index e9183666ecf..c9ba89a0665 100644
--- a/intern/cycles/render/gpu_display.h
+++ b/intern/cycles/render/gpu_display.h
@@ -151,6 +151,11 @@ class GPUDisplay {
    * device API. */
   DeviceGraphicsInteropDestination graphics_interop_get();
 
+  /* (De)activate GPU display for graphics interoperability outside of regular display udpate
+   * routines. */
+  virtual void graphics_interop_activate();
+  virtual void graphics_interop_deactivate();
+
   /* --------------------------------------------------------------------
    * Drawing.
    */



More information about the Bf-blender-cvs mailing list