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