Blender V4.3
cuda/graphics_interop.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_CUDA
6
8
10# include "device/cuda/util.h"
11
13
14CUDADeviceGraphicsInterop::CUDADeviceGraphicsInterop(CUDADeviceQueue *queue)
15 : queue_(queue), device_(static_cast<CUDADevice *>(queue->device))
16{
17}
18
19CUDADeviceGraphicsInterop::~CUDADeviceGraphicsInterop()
20{
21 CUDAContextScope scope(device_);
22
23 if (cu_graphics_resource_) {
24 cuda_device_assert(device_, cuGraphicsUnregisterResource(cu_graphics_resource_));
25 }
26}
27
28void CUDADeviceGraphicsInterop::set_display_interop(
29 const DisplayDriver::GraphicsInterop &display_interop)
30{
31 const int64_t new_buffer_area = int64_t(display_interop.buffer_width) *
32 display_interop.buffer_height;
33
34 need_clear_ = display_interop.need_clear;
35
36 if (!display_interop.need_recreate) {
37 if (opengl_pbo_id_ == display_interop.opengl_pbo_id && buffer_area_ == new_buffer_area) {
38 return;
39 }
40 }
41
42 CUDAContextScope scope(device_);
43
44 if (cu_graphics_resource_) {
45 cuda_device_assert(device_, cuGraphicsUnregisterResource(cu_graphics_resource_));
46 }
47
48 const CUresult result = cuGraphicsGLRegisterBuffer(
49 &cu_graphics_resource_, display_interop.opengl_pbo_id, CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
50 if (result != CUDA_SUCCESS) {
51 LOG(ERROR) << "Error registering OpenGL buffer: " << cuewErrorString(result);
52 }
53
54 opengl_pbo_id_ = display_interop.opengl_pbo_id;
55 buffer_area_ = new_buffer_area;
56}
57
58device_ptr CUDADeviceGraphicsInterop::map()
59{
60 if (!cu_graphics_resource_) {
61 return 0;
62 }
63
64 CUDAContextScope scope(device_);
65
66 CUdeviceptr cu_buffer;
67 size_t bytes;
68
69 cuda_device_assert(device_, cuGraphicsMapResources(1, &cu_graphics_resource_, queue_->stream()));
70 cuda_device_assert(
71 device_, cuGraphicsResourceGetMappedPointer(&cu_buffer, &bytes, cu_graphics_resource_));
72
73 if (need_clear_) {
74 cuda_device_assert(
75 device_, cuMemsetD8Async(static_cast<CUdeviceptr>(cu_buffer), 0, bytes, queue_->stream()));
76
77 need_clear_ = false;
78 }
79
80 return static_cast<device_ptr>(cu_buffer);
81}
82
83void CUDADeviceGraphicsInterop::unmap()
84{
85 CUDAContextScope scope(device_);
86
87 cuda_device_assert(device_,
88 cuGraphicsUnmapResources(1, &cu_graphics_resource_, queue_->stream()));
89}
90
92
93#endif
#define CCL_NAMESPACE_END
#define LOG(severity)
Definition log.h:33
__int64 int64_t
Definition stdint.h:89
uint64_t device_ptr
Definition util/types.h:45