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