Skip to content

Commit b1bc1e8

Browse files
authored
Make GraphicsResource inherit from Buffer (#1701)
* removing the _buffer usage example from docstring * fixes * fixes
1 parent 15cfd84 commit b1bc1e8

File tree

8 files changed

+283
-113
lines changed

8 files changed

+283
-113
lines changed

cuda_core/cuda/core/_cpp/resource_handles.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel = nullptr;
6060
decltype(&cuLinkDestroy) p_cuLinkDestroy = nullptr;
6161

6262
// GL interop pointers
63+
decltype(&cuGraphicsUnmapResources) p_cuGraphicsUnmapResources = nullptr;
6364
decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource = nullptr;
6465

6566
// NVRTC function pointers
@@ -656,6 +657,23 @@ DevicePtrHandle deviceptr_create_with_owner(CUdeviceptr ptr, PyObject* owner) {
656657
return DevicePtrHandle(box, &box->resource);
657658
}
658659

660+
DevicePtrHandle deviceptr_create_mapped_graphics(
661+
CUdeviceptr ptr,
662+
const GraphicsResourceHandle& h_resource,
663+
const StreamHandle& h_stream
664+
) {
665+
auto box = std::shared_ptr<DevicePtrBox>(
666+
new DevicePtrBox{ptr, h_stream},
667+
[h_resource](DevicePtrBox* b) {
668+
GILReleaseGuard gil;
669+
CUgraphicsResource resource = as_cu(h_resource);
670+
p_cuGraphicsUnmapResources(1, &resource, as_cu(b->h_stream));
671+
delete b;
672+
}
673+
);
674+
return DevicePtrHandle(box, &box->resource);
675+
}
676+
659677
// ============================================================================
660678
// MemoryResource-owned Device Pointer Handles
661679
// ============================================================================

cuda_core/cuda/core/_cpp/resource_handles.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extern decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel;
9696
extern decltype(&cuLinkDestroy) p_cuLinkDestroy;
9797

9898
// Graphics interop
99+
extern decltype(&cuGraphicsUnmapResources) p_cuGraphicsUnmapResources;
99100
extern decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource;
100101

101102
// ============================================================================
@@ -299,6 +300,16 @@ DevicePtrHandle deviceptr_create_ref(CUdeviceptr ptr);
299300
// If owner is nullptr, equivalent to deviceptr_create_ref.
300301
DevicePtrHandle deviceptr_create_with_owner(CUdeviceptr ptr, PyObject* owner);
301302

303+
// Create a device pointer handle for a mapped graphics resource.
304+
// The pointer structurally depends on the provided graphics resource handle.
305+
// When the last reference is released, cuGraphicsUnmapResources is called on
306+
// the stored stream, then the graphics resource may be unregistered when its
307+
// own handle is released.
308+
DevicePtrHandle deviceptr_create_mapped_graphics(
309+
CUdeviceptr ptr,
310+
const GraphicsResourceHandle& h_resource,
311+
const StreamHandle& h_stream);
312+
302313
// Callback type for MemoryResource deallocation.
303314
// Called from the shared_ptr deleter when a handle created via
304315
// deviceptr_create_with_mr is destroyed. The implementation is responsible

cuda_core/cuda/core/_graphics.pxd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cdef class GraphicsResource:
99

1010
cdef:
1111
GraphicsResourceHandle _handle
12-
bint _mapped
12+
object _mapped_buffer
13+
object _context_manager_stream
14+
object _entered_buffer
1315

14-
cpdef close(self)
16+
cpdef close(self, stream=*)

0 commit comments

Comments
 (0)