Skip to content

Commit 0c9a981

Browse files
committed
graphics: map GL resources to RAII buffers
1 parent f0ae028 commit 0c9a981

File tree

8 files changed

+231
-111
lines changed

8 files changed

+231
-111
lines changed

cuda_core/cuda/core/_cpp/resource_handles.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ decltype(&cuLibraryUnload) p_cuLibraryUnload = nullptr;
5757
decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel = nullptr;
5858

5959
// GL interop pointers
60+
decltype(&cuGraphicsUnmapResources) p_cuGraphicsUnmapResources = nullptr;
6061
decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource = nullptr;
6162

6263
// NVRTC function pointers
@@ -569,6 +570,23 @@ DevicePtrHandle deviceptr_create_with_owner(CUdeviceptr ptr, PyObject* owner) {
569570
return DevicePtrHandle(box, &box->resource);
570571
}
571572

573+
DevicePtrHandle deviceptr_create_mapped_graphics(
574+
CUdeviceptr ptr,
575+
const GraphicsResourceHandle& h_resource,
576+
const StreamHandle& h_stream
577+
) {
578+
auto box = std::shared_ptr<DevicePtrBox>(
579+
new DevicePtrBox{ptr, h_stream},
580+
[h_resource](DevicePtrBox* b) {
581+
GILReleaseGuard gil;
582+
CUgraphicsResource resource = as_cu(h_resource);
583+
p_cuGraphicsUnmapResources(1, &resource, as_cu(b->h_stream));
584+
delete b;
585+
}
586+
);
587+
return DevicePtrHandle(box, &box->resource);
588+
}
589+
572590
// ============================================================================
573591
// MemoryResource-owned Device Pointer Handles
574592
// ============================================================================

cuda_core/cuda/core/_cpp/resource_handles.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ extern decltype(&cuLibraryUnload) p_cuLibraryUnload;
7373
extern decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel;
7474

7575
// Graphics interop
76+
extern decltype(&cuGraphicsUnmapResources) p_cuGraphicsUnmapResources;
7677
extern decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource;
7778

7879
// ============================================================================
@@ -244,6 +245,16 @@ DevicePtrHandle deviceptr_create_ref(CUdeviceptr ptr);
244245
// If owner is nullptr, equivalent to deviceptr_create_ref.
245246
DevicePtrHandle deviceptr_create_with_owner(CUdeviceptr ptr, PyObject* owner);
246247

248+
// Create a device pointer handle for a mapped graphics resource.
249+
// The pointer structurally depends on the provided graphics resource handle.
250+
// When the last reference is released, cuGraphicsUnmapResources is called on
251+
// the stored stream, then the graphics resource may be unregistered when its
252+
// own handle is released.
253+
DevicePtrHandle deviceptr_create_mapped_graphics(
254+
CUdeviceptr ptr,
255+
const GraphicsResourceHandle& h_resource,
256+
const StreamHandle& h_stream);
257+
247258
// Callback type for MemoryResource deallocation.
248259
// Called from the shared_ptr deleter when a handle created via
249260
// deviceptr_create_with_mr is destroyed. The implementation is responsible

cuda_core/cuda/core/_graphics.pxd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from cuda.core._resource_handles cimport GraphicsResourceHandle
6-
from cuda.core._memory._buffer cimport Buffer
76

87

9-
cdef class GraphicsResource(Buffer):
8+
cdef class GraphicsResource:
109

1110
cdef:
1211
GraphicsResourceHandle _handle
13-
bint _mapped
14-
object _map_stream
12+
object _mapped_buffer
13+
object _context_manager_stream
14+
object _entered_buffer
1515

1616
cpdef close(self, stream=*)

0 commit comments

Comments
 (0)