Blender V5.0
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 free();
23}
24
25void HIPDeviceGraphicsInterop::set_buffer(GraphicsInteropBuffer &interop_buffer)
26{
27 HIPContextScope scope(device_);
28
29 if (interop_buffer.is_empty()) {
30 free();
31 return;
32 }
33
34 need_zero_ |= interop_buffer.take_zero();
35
36 if (!interop_buffer.has_new_handle()) {
37 return;
38 }
39
40 free();
41
42 switch (interop_buffer.get_type()) {
44 const hipError_t result = hipGraphicsGLRegisterBuffer(
45 &hip_graphics_resource_, interop_buffer.take_handle(), hipGraphicsRegisterFlagsNone);
46
47 if (result != hipSuccess) {
48 LOG_ERROR << "Error registering OpenGL buffer: " << hipewErrorString(result);
49 break;
50 }
51
52 buffer_size_ = interop_buffer.get_size();
53
54 break;
55 }
59 /* TODO: implement vulkan support. */
60 break;
61 }
62}
63
64device_ptr HIPDeviceGraphicsInterop::map()
65{
66 hipDeviceptr_t hip_buffer = 0;
67
68 if (hip_graphics_resource_) {
69 HIPContextScope scope(device_);
70 size_t bytes;
71
72 hip_device_assert(device_,
73 hipGraphicsMapResources(1, &hip_graphics_resource_, queue_->stream()));
74 hip_device_assert(
75 device_, hipGraphicsResourceGetMappedPointer(&hip_buffer, &bytes, hip_graphics_resource_));
76 }
77 else {
78 /* Vulkan buffer is always mapped. */
79 hip_buffer = hip_external_memory_ptr_;
80 }
81
82 if (hip_buffer && need_zero_) {
83 hip_device_assert(device_, hipMemsetD8Async(hip_buffer, 0, buffer_size_, queue_->stream()));
84
85 need_zero_ = false;
86 }
87
88 return static_cast<device_ptr>(hip_buffer);
89}
90
91void HIPDeviceGraphicsInterop::unmap()
92{
93 if (hip_graphics_resource_) {
94 HIPContextScope scope(device_);
95
96 hip_device_assert(device_,
97 hipGraphicsUnmapResources(1, &hip_graphics_resource_, queue_->stream()));
98 }
99}
100
101void HIPDeviceGraphicsInterop::free()
102{
103 if (hip_graphics_resource_) {
104 hip_device_assert(device_, hipGraphicsUnregisterResource(hip_graphics_resource_));
105 hip_graphics_resource_ = nullptr;
106 }
107
108 if (hip_external_memory_ptr_) {
109 hip_device_assert(device_, hipFree(hip_external_memory_ptr_));
110 hip_external_memory_ptr_ = 0;
111 }
112
113 buffer_size_ = 0;
114
115 need_zero_ = false;
116}
117
119
120#endif
void BLI_kdtree_nd_ free(KDTree *tree)
GraphicsInteropDevice::Type get_type() const
#define CCL_NAMESPACE_END
#define LOG_ERROR
Definition log.h:101
uint64_t device_ptr
Definition types_base.h:44