Blender  V3.3
hip/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_HIP
5 
7 
8 # include "device/hip/device_impl.h"
9 # include "device/hip/util.h"
10 
12 
13 HIPDeviceGraphicsInterop::HIPDeviceGraphicsInterop(HIPDeviceQueue *queue)
14  : queue_(queue), device_(static_cast<HIPDevice *>(queue->device))
15 {
16 }
17 
18 HIPDeviceGraphicsInterop::~HIPDeviceGraphicsInterop()
19 {
20  HIPContextScope scope(device_);
21 
22  if (hip_graphics_resource_) {
23  hip_device_assert(device_, hipGraphicsUnregisterResource(hip_graphics_resource_));
24  }
25 }
26 
27 void HIPDeviceGraphicsInterop::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 (opengl_pbo_id_ == display_interop.opengl_pbo_id && buffer_area_ == new_buffer_area) {
36  return;
37  }
38 
39  HIPContextScope scope(device_);
40 
41  if (hip_graphics_resource_) {
42  hip_device_assert(device_, hipGraphicsUnregisterResource(hip_graphics_resource_));
43  }
44 
45  const hipError_t result = hipGraphicsGLRegisterBuffer(
46  &hip_graphics_resource_, display_interop.opengl_pbo_id, hipGraphicsRegisterFlagsNone);
47  if (result != hipSuccess) {
48  LOG(ERROR) << "Error registering OpenGL buffer: " << hipewErrorString(result);
49  }
50 
51  opengl_pbo_id_ = display_interop.opengl_pbo_id;
52  buffer_area_ = new_buffer_area;
53 }
54 
56 {
57  if (!hip_graphics_resource_) {
58  return 0;
59  }
60 
61  HIPContextScope scope(device_);
62 
63  hipDeviceptr_t hip_buffer;
64  size_t bytes;
65 
66  hip_device_assert(device_,
67  hipGraphicsMapResources(1, &hip_graphics_resource_, queue_->stream()));
68  hip_device_assert(
69  device_, hipGraphicsResourceGetMappedPointer(&hip_buffer, &bytes, hip_graphics_resource_));
70 
71  if (need_clear_) {
72  hip_device_assert(
73  device_,
74  hipMemsetD8Async(static_cast<hipDeviceptr_t>(hip_buffer), 0, bytes, queue_->stream()));
75 
76  need_clear_ = false;
77  }
78 
79  return static_cast<device_ptr>(hip_buffer);
80 }
81 
82 void HIPDeviceGraphicsInterop::unmap()
83 {
84  HIPContextScope scope(device_);
85 
86  hip_device_assert(device_,
87  hipGraphicsUnmapResources(1, &hip_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