Blender V5.3
graphics_interop.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_METAL
6
8
10
12
13MetalDeviceGraphicsInterop::MetalDeviceGraphicsInterop(MetalDeviceQueue *queue)
14 : queue_(queue), device_(static_cast<MetalDevice *>(queue->device))
15{
16}
17
18MetalDeviceGraphicsInterop::~MetalDeviceGraphicsInterop()
19{
20 device_->remove_from_residency_set(mem_.mtlBuffer);
21}
22
23void MetalDeviceGraphicsInterop::set_buffer(GraphicsInteropBuffer &interop_buffer)
24{
25 if (interop_buffer.is_empty()) {
26 device_->remove_from_residency_set(mem_.mtlBuffer);
27 mem_.mtlBuffer = nullptr;
28 size_ = 0;
29 return;
30 }
31
32 need_zero_ |= interop_buffer.take_zero();
33
34 if (!interop_buffer.has_new_handle()) {
35 return;
36 }
37
38 /* The interop buffer is externally owned, so it doesn't go through metal_mem_alloc. We still
39 * need to put it in the residency set so kernels can write display output into it. */
40 device_->remove_from_residency_set(mem_.mtlBuffer);
41 mem_.mtlBuffer = reinterpret_cast<id<MTLBuffer>>(interop_buffer.take_handle());
42 size_ = interop_buffer.get_size();
43 device_->add_to_residency_set(mem_.mtlBuffer);
44}
45
46device_ptr MetalDeviceGraphicsInterop::map()
47{
48 if (mem_.mtlBuffer && need_zero_) {
49 memset([mem_.mtlBuffer contents], 0, size_);
50 need_zero_ = false;
51 }
52
53 return device_ptr(&mem_);
54}
55
56void MetalDeviceGraphicsInterop::unmap() {}
57
59
60#endif
#define CCL_NAMESPACE_END
uint64_t device_ptr
Definition types_base.h:44