[Bf-blender-cvs] [9c31750c17a] cycles-x: Refactor: Move Cycles X CUDA interop class to own file
Sergey Sharybin
noreply at git.blender.org
Mon Jun 28 10:51:27 CEST 2021
Commit: 9c31750c17af67ba4ebc97706338d21745cb56cf
Author: Sergey Sharybin
Date: Fri Jun 25 16:08:18 2021 +0200
Branches: cycles-x
https://developer.blender.org/rB9c31750c17af67ba4ebc97706338d21745cb56cf
Refactor: Move Cycles X CUDA interop class to own file
No functional changes.
Isolates logic, makes it easier to adopt for coming changes.
===================================================================
M intern/cycles/device/CMakeLists.txt
M intern/cycles/device/cuda/device_impl.cpp
A intern/cycles/device/cuda/graphics_interop.cpp
A intern/cycles/device/cuda/graphics_interop.h
===================================================================
diff --git a/intern/cycles/device/CMakeLists.txt b/intern/cycles/device/CMakeLists.txt
index 6e60b730d15..fe3836cad8d 100644
--- a/intern/cycles/device/CMakeLists.txt
+++ b/intern/cycles/device/CMakeLists.txt
@@ -60,6 +60,8 @@ set(SRC_CUDA
cuda/device.h
cuda/device_impl.cpp
cuda/device_impl.h
+ cuda/graphics_interop.cpp
+ cuda/graphics_interop.h
cuda/kernel.cpp
cuda/kernel.h
cuda/queue.cpp
diff --git a/intern/cycles/device/cuda/device_impl.cpp b/intern/cycles/device/cuda/device_impl.cpp
index c57ad2abade..7a743c483bd 100644
--- a/intern/cycles/device/cuda/device_impl.cpp
+++ b/intern/cycles/device/cuda/device_impl.cpp
@@ -23,6 +23,7 @@
# include <string.h>
# include "device/cuda/device_impl.h"
+# include "device/cuda/graphics_interop.h"
# include "render/buffers.h"
@@ -1319,95 +1320,6 @@ unique_ptr<DeviceQueue> CUDADevice::gpu_queue_create()
return make_unique<CUDADeviceQueue>(this);
}
-/* --------------------------------------------------------------------
- * Graphics resources interoperability.
- */
-
-namespace {
-
-class CUDADeviceGraphicsInterop : public DeviceGraphicsInterop {
- public:
- CUDADeviceGraphicsInterop(CUDADevice *device) : device_(device)
- {
- }
-
- CUDADeviceGraphicsInterop(const CUDADeviceGraphicsInterop &other) = delete;
- CUDADeviceGraphicsInterop(CUDADeviceGraphicsInterop &&other) noexcept = delete;
-
- ~CUDADeviceGraphicsInterop()
- {
- CUDAContextScope scope(device_);
-
- if (cu_graphics_resource_) {
- cuda_device_assert(device_, cuGraphicsUnregisterResource(cu_graphics_resource_));
- }
- }
-
- CUDADeviceGraphicsInterop &operator=(const CUDADeviceGraphicsInterop &other) = delete;
- CUDADeviceGraphicsInterop &operator=(CUDADeviceGraphicsInterop &&other) = delete;
-
- virtual void set_destination(const DeviceGraphicsInteropDestination &destination) override
- {
- const int64_t new_buffer_area = int64_t(destination.buffer_width) * destination.buffer_height;
-
- if (opengl_pbo_id_ == destination.opengl_pbo_id && buffer_area_ == new_buffer_area) {
- return;
- }
-
- CUDAContextScope scope(device_);
-
- if (cu_graphics_resource_) {
- cuda_device_assert(device_, cuGraphicsUnregisterResource(cu_graphics_resource_));
- }
-
- const CUresult result = cuGraphicsGLRegisterBuffer(
- &cu_graphics_resource_, destination.opengl_pbo_id, CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
- if (result != CUDA_SUCCESS) {
- LOG(ERROR) << "Error registering OpenGL buffer: " << cuewErrorString(result);
- }
-
- opengl_pbo_id_ = destination.opengl_pbo_id;
- buffer_area_ = new_buffer_area;
- }
-
- virtual device_ptr map() override
- {
- if (!cu_graphics_resource_) {
- return 0;
- }
-
- CUDAContextScope scope(device_);
-
- CUdeviceptr cu_buffer;
- size_t bytes;
-
- cuda_device_assert(device_, cuGraphicsMapResources(1, &cu_graphics_resource_, 0));
- cuda_device_assert(
- device_, cuGraphicsResourceGetMappedPointer(&cu_buffer, &bytes, cu_graphics_resource_));
-
- return static_cast<device_ptr>(cu_buffer);
- }
-
- virtual void unmap() override
- {
- CUDAContextScope scope(device_);
-
- cuda_device_assert(device_, cuGraphicsUnmapResources(1, &cu_graphics_resource_, 0));
- }
-
- protected:
- CUDADevice *device_ = nullptr;
-
- /* OpenGL PBO which is currently registered as the destination for the CUDA buffer. */
- uint opengl_pbo_id_ = 0;
- /* Buffer area in pixels of the corresponding PBO. */
- int64_t buffer_area_ = 0;
-
- CUgraphicsResource cu_graphics_resource_ = nullptr;
-};
-
-} /* namespace */
-
bool CUDADevice::should_use_graphics_interop()
{
/* Check whether this device is part of OpenGL context.
diff --git a/intern/cycles/device/cuda/graphics_interop.cpp b/intern/cycles/device/cuda/graphics_interop.cpp
new file mode 100644
index 00000000000..39a040eeb11
--- /dev/null
+++ b/intern/cycles/device/cuda/graphics_interop.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2011-2021 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifdef WITH_CUDA
+
+# include "device/cuda/graphics_interop.h"
+
+# include "device/cuda/device_impl.h"
+# include "device/cuda/util.h"
+
+CCL_NAMESPACE_BEGIN
+
+CUDADeviceGraphicsInterop::CUDADeviceGraphicsInterop(CUDADevice *device) : device_(device)
+{
+}
+
+CUDADeviceGraphicsInterop::~CUDADeviceGraphicsInterop()
+{
+ CUDAContextScope scope(device_);
+
+ if (cu_graphics_resource_) {
+ cuda_device_assert(device_, cuGraphicsUnregisterResource(cu_graphics_resource_));
+ }
+}
+
+void CUDADeviceGraphicsInterop::set_destination(
+ const DeviceGraphicsInteropDestination &destination)
+{
+ const int64_t new_buffer_area = int64_t(destination.buffer_width) * destination.buffer_height;
+
+ if (opengl_pbo_id_ == destination.opengl_pbo_id && buffer_area_ == new_buffer_area) {
+ return;
+ }
+
+ CUDAContextScope scope(device_);
+
+ if (cu_graphics_resource_) {
+ cuda_device_assert(device_, cuGraphicsUnregisterResource(cu_graphics_resource_));
+ }
+
+ const CUresult result = cuGraphicsGLRegisterBuffer(
+ &cu_graphics_resource_, destination.opengl_pbo_id, CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
+ if (result != CUDA_SUCCESS) {
+ LOG(ERROR) << "Error registering OpenGL buffer: " << cuewErrorString(result);
+ }
+
+ opengl_pbo_id_ = destination.opengl_pbo_id;
+ buffer_area_ = new_buffer_area;
+}
+
+device_ptr CUDADeviceGraphicsInterop::map()
+{
+ if (!cu_graphics_resource_) {
+ return 0;
+ }
+
+ CUDAContextScope scope(device_);
+
+ CUdeviceptr cu_buffer;
+ size_t bytes;
+
+ cuda_device_assert(device_, cuGraphicsMapResources(1, &cu_graphics_resource_, 0));
+ cuda_device_assert(
+ device_, cuGraphicsResourceGetMappedPointer(&cu_buffer, &bytes, cu_graphics_resource_));
+
+ return static_cast<device_ptr>(cu_buffer);
+}
+
+void CUDADeviceGraphicsInterop::unmap()
+{
+ CUDAContextScope scope(device_);
+
+ cuda_device_assert(device_, cuGraphicsUnmapResources(1, &cu_graphics_resource_, 0));
+}
+
+CCL_NAMESPACE_END
+
+#endif
diff --git a/intern/cycles/device/cuda/graphics_interop.h b/intern/cycles/device/cuda/graphics_interop.h
new file mode 100644
index 00000000000..57d88665e50
--- /dev/null
+++ b/intern/cycles/device/cuda/graphics_interop.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2011-2021 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifdef WITH_CUDA
+
+# include "device/device_graphics_interop.h"
+
+# ifdef WITH_CUDA_DYNLOAD
+# include "cuew.h"
+# else
+# include <cuda.h>
+# endif
+
+CCL_NAMESPACE_BEGIN
+
+class CUDADevice;
+
+class CUDADeviceGraphicsInterop : public DeviceGraphicsInterop {
+ public:
+ CUDADeviceGraphicsInterop(CUDADevice *device);
+
+ CUDADeviceGraphicsInterop(const CUDADeviceGraphicsInterop &other) = delete;
+ CUDADeviceGraphicsInterop(CUDADeviceGraphicsInterop &&other) noexcept = delete;
+
+ ~CUDADeviceGraphicsInterop();
+
+ CUDADeviceGraphicsInterop &operator=(const CUDADeviceGraphicsInterop &other) = delete;
+ CUDADeviceGraphicsInterop &operator=(CUDADeviceGraphicsInterop &&other) = delete;
+
+ virtual void set_destination(const DeviceGraphicsInteropDestination &destination) override;
+
+ virtual device_ptr map() override;
+ virtual void unmap() override;
+
+ protected:
+ CUDADevice *device_ = nullptr;
+
+ /* OpenGL PBO which is currently registered as the destination for the CUDA buffer. */
+ uint opengl_pbo_id_ = 0;
+ /* Buffer area in pixels of the corresponding PBO. */
+ int64_t buffer_area_ = 0;
+
+ CUgraphicsResource cu_graphics_resource_ = nullptr;
+};
+
+CCL_NAMESPACE_END
+
+#endif
More information about the Bf-blender-cvs
mailing list