[Bf-blender-cvs] [700980612fe] cycles-x: Fix Cycles X interop on non-OpenGL device

Sergey Sharybin noreply at git.blender.org
Mon Jun 28 10:51:28 CEST 2021


Commit: 700980612fe47621f1acf2f1e7382875ca0bf9a8
Author: Sergey Sharybin
Date:   Fri Jun 25 16:20:43 2021 +0200
Branches: cycles-x
https://developer.blender.org/rB700980612fe47621f1acf2f1e7382875ca0bf9a8

Fix Cycles X interop on non-OpenGL device

Graphics interop for non-display GPU got broken with the CUDA streams
addition.

Now we create interop object via the queue, so its possible to use
proper CUDA stream for mapping/unmapping.

Not sure why adding extra context synchronization did not resolve the
issue, but this change seems to make sense anyway.

Here its only reproducible with non-display GPU (for which it was
needed to tweak `should_use_graphics_interop()`).  But some time ago
there reports about missing image in the viewport, so could be related.

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

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

M	intern/cycles/device/cuda/device_impl.cpp
M	intern/cycles/device/cuda/device_impl.h
M	intern/cycles/device/cuda/graphics_interop.cpp
M	intern/cycles/device/cuda/graphics_interop.h
M	intern/cycles/device/cuda/queue.cpp
M	intern/cycles/device/cuda/queue.h
M	intern/cycles/device/device.h
M	intern/cycles/device/device_queue.h
M	intern/cycles/integrator/path_trace_work_gpu.cpp

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

diff --git a/intern/cycles/device/cuda/device_impl.cpp b/intern/cycles/device/cuda/device_impl.cpp
index 7a743c483bd..c4f997cf6ec 100644
--- a/intern/cycles/device/cuda/device_impl.cpp
+++ b/intern/cycles/device/cuda/device_impl.cpp
@@ -23,7 +23,6 @@
 #  include <string.h>
 
 #  include "device/cuda/device_impl.h"
-#  include "device/cuda/graphics_interop.h"
 
 #  include "render/buffers.h"
 
@@ -1350,11 +1349,6 @@ bool CUDADevice::should_use_graphics_interop()
   return false;
 }
 
-unique_ptr<DeviceGraphicsInterop> CUDADevice::graphics_interop_create()
-{
-  return make_unique<CUDADeviceGraphicsInterop>(this);
-}
-
 int CUDADevice::get_num_multiprocessors()
 {
   return get_device_default_attribute(CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT, 0);
diff --git a/intern/cycles/device/cuda/device_impl.h b/intern/cycles/device/cuda/device_impl.h
index 13a238c486a..9d80b4cc288 100644
--- a/intern/cycles/device/cuda/device_impl.h
+++ b/intern/cycles/device/cuda/device_impl.h
@@ -147,9 +147,7 @@ class CUDADevice : public Device {
 
   void tex_free(device_texture &mem);
 
-  /* Graphics resources interoperability. */
   virtual bool should_use_graphics_interop() override;
-  virtual unique_ptr<DeviceGraphicsInterop> graphics_interop_create() override;
 
   virtual unique_ptr<DeviceQueue> gpu_queue_create() override;
 
diff --git a/intern/cycles/device/cuda/graphics_interop.cpp b/intern/cycles/device/cuda/graphics_interop.cpp
index 39a040eeb11..0bd5ae31631 100644
--- a/intern/cycles/device/cuda/graphics_interop.cpp
+++ b/intern/cycles/device/cuda/graphics_interop.cpp
@@ -23,7 +23,8 @@
 
 CCL_NAMESPACE_BEGIN
 
-CUDADeviceGraphicsInterop::CUDADeviceGraphicsInterop(CUDADevice *device) : device_(device)
+CUDADeviceGraphicsInterop::CUDADeviceGraphicsInterop(CUDADeviceQueue *queue)
+    : queue_(queue), device_(static_cast<CUDADevice *>(queue->device))
 {
 }
 
@@ -72,7 +73,7 @@ device_ptr CUDADeviceGraphicsInterop::map()
   CUdeviceptr cu_buffer;
   size_t bytes;
 
-  cuda_device_assert(device_, cuGraphicsMapResources(1, &cu_graphics_resource_, 0));
+  cuda_device_assert(device_, cuGraphicsMapResources(1, &cu_graphics_resource_, queue_->stream()));
   cuda_device_assert(
       device_, cuGraphicsResourceGetMappedPointer(&cu_buffer, &bytes, cu_graphics_resource_));
 
@@ -83,7 +84,8 @@ void CUDADeviceGraphicsInterop::unmap()
 {
   CUDAContextScope scope(device_);
 
-  cuda_device_assert(device_, cuGraphicsUnmapResources(1, &cu_graphics_resource_, 0));
+  cuda_device_assert(device_,
+                     cuGraphicsUnmapResources(1, &cu_graphics_resource_, queue_->stream()));
 }
 
 CCL_NAMESPACE_END
diff --git a/intern/cycles/device/cuda/graphics_interop.h b/intern/cycles/device/cuda/graphics_interop.h
index 57d88665e50..447a364a86f 100644
--- a/intern/cycles/device/cuda/graphics_interop.h
+++ b/intern/cycles/device/cuda/graphics_interop.h
@@ -27,10 +27,11 @@
 CCL_NAMESPACE_BEGIN
 
 class CUDADevice;
+class CUDADeviceQueue;
 
 class CUDADeviceGraphicsInterop : public DeviceGraphicsInterop {
  public:
-  CUDADeviceGraphicsInterop(CUDADevice *device);
+  explicit CUDADeviceGraphicsInterop(CUDADeviceQueue *queue);
 
   CUDADeviceGraphicsInterop(const CUDADeviceGraphicsInterop &other) = delete;
   CUDADeviceGraphicsInterop(CUDADeviceGraphicsInterop &&other) noexcept = delete;
@@ -46,6 +47,7 @@ class CUDADeviceGraphicsInterop : public DeviceGraphicsInterop {
   virtual void unmap() override;
 
  protected:
+  CUDADeviceQueue *queue_ = nullptr;
   CUDADevice *device_ = nullptr;
 
   /* OpenGL PBO which is currently registered as the destination for the CUDA buffer. */
diff --git a/intern/cycles/device/cuda/queue.cpp b/intern/cycles/device/cuda/queue.cpp
index 6b958b6f29e..0ab387d38cd 100644
--- a/intern/cycles/device/cuda/queue.cpp
+++ b/intern/cycles/device/cuda/queue.cpp
@@ -17,7 +17,9 @@
 #ifdef WITH_CUDA
 
 #  include "device/cuda/queue.h"
+
 #  include "device/cuda/device_impl.h"
+#  include "device/cuda/graphics_interop.h"
 #  include "device/cuda/kernel.h"
 
 CCL_NAMESPACE_BEGIN
@@ -198,6 +200,11 @@ void CUDADeviceQueue::copy_from_device(device_memory &mem)
           mem.host_pointer, (CUdeviceptr)mem.device_pointer, mem.memory_size(), cuda_stream_));
 }
 
+unique_ptr<DeviceGraphicsInterop> CUDADeviceQueue::graphics_interop_create()
+{
+  return make_unique<CUDADeviceGraphicsInterop>(this);
+}
+
 CCL_NAMESPACE_END
 
 #endif /* WITH_CUDA */
diff --git a/intern/cycles/device/cuda/queue.h b/intern/cycles/device/cuda/queue.h
index 48c97d69301..9302616734f 100644
--- a/intern/cycles/device/cuda/queue.h
+++ b/intern/cycles/device/cuda/queue.h
@@ -55,6 +55,8 @@ class CUDADeviceQueue : public DeviceQueue {
     return cuda_stream_;
   }
 
+  virtual unique_ptr<DeviceGraphicsInterop> graphics_interop_create() override;
+
  protected:
   CUDADevice *cuda_device_;
   CUstream cuda_stream_;
diff --git a/intern/cycles/device/device.h b/intern/cycles/device/device.h
index 180e33aebf3..a39f5b328c2 100644
--- a/intern/cycles/device/device.h
+++ b/intern/cycles/device/device.h
@@ -22,7 +22,6 @@
 #include "bvh/bvh_params.h"
 
 #include "device/device_denoise.h"
-#include "device/device_graphics_interop.h"
 #include "device/device_memory.h"
 
 #include "util/util_function.h"
@@ -374,14 +373,6 @@ class Device {
     return false;
   }
 
-  /* Create graphics interoperability context which will be taking care of mapping graphics
-   * resource as a buffer writable by kernels of this device. */
-  virtual unique_ptr<DeviceGraphicsInterop> graphics_interop_create()
-  {
-    LOG(FATAL) << "Request of GPU interop of a device which does not support it.";
-    return nullptr;
-  }
-
   /* Buffer denoising. */
 
   /* TODO(sergey): Need to pass real parameters needed for denoising. */
diff --git a/intern/cycles/device/device_queue.h b/intern/cycles/device/device_queue.h
index db9345bdef4..c7b0591122c 100644
--- a/intern/cycles/device/device_queue.h
+++ b/intern/cycles/device/device_queue.h
@@ -18,7 +18,10 @@
 
 #include "device/device_kernel.h"
 
+#include "device/device_graphics_interop.h"
+#include "util/util_logging.h"
 #include "util/util_map.h"
+#include "util/util_unique_ptr.h"
 
 CCL_NAMESPACE_BEGIN
 
@@ -74,6 +77,19 @@ class DeviceQueue {
   virtual void copy_to_device(device_memory &mem) = 0;
   virtual void copy_from_device(device_memory &mem) = 0;
 
+  /* Graphics resources interoperability.
+   *
+   * The interoperability comes here by the meaning that the device is capable of computing result
+   * directly into an OpenGL (or other graphics library) buffer. */
+
+  /* Create graphics interoperability context which will be taking care of mapping graphics
+   * resource as a buffer writable by kernels of this device. */
+  virtual unique_ptr<DeviceGraphicsInterop> graphics_interop_create()
+  {
+    LOG(FATAL) << "Request of GPU interop of a device which does not support it.";
+    return nullptr;
+  }
+
   /* Device this queue has been created for. */
   Device *device;
 
diff --git a/intern/cycles/integrator/path_trace_work_gpu.cpp b/intern/cycles/integrator/path_trace_work_gpu.cpp
index 7a546c9bb4d..3b8372d4da2 100644
--- a/intern/cycles/integrator/path_trace_work_gpu.cpp
+++ b/intern/cycles/integrator/path_trace_work_gpu.cpp
@@ -695,10 +695,8 @@ bool PathTraceWorkGPU::copy_to_gpu_display_interop(GPUDisplay *gpu_display,
                                                    PassMode pass_mode,
                                                    int num_samples)
 {
-  Device *device = queue_->device;
-
   if (!device_graphics_interop_) {
-    device_graphics_interop_ = device->graphics_interop_create();
+    device_graphics_interop_ = queue_->graphics_interop_create();
   }
 
   const DeviceGraphicsInteropDestination graphics_interop_dst =



More information about the Bf-blender-cvs mailing list