Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cuda_core/cuda/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
GraphCompleteOptions,
GraphDebugPrintOptions,
)
from cuda.core._graphics import GraphicsRegisterFlags, GraphicsResource # noqa: E402
from cuda.core._launch_config import LaunchConfig # noqa: E402
from cuda.core._launcher import launch # noqa: E402
from cuda.core._layout import _StridedLayout # noqa: E402
Expand Down
26 changes: 26 additions & 0 deletions cuda_core/cuda/core/_cpp/resource_handles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ decltype(&cuLibraryLoadData) p_cuLibraryLoadData = nullptr;
decltype(&cuLibraryUnload) p_cuLibraryUnload = nullptr;
decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel = nullptr;

// GL interop pointers
decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource = nullptr;

// NVRTC function pointers
decltype(&nvrtcDestroyProgram) p_nvrtcDestroyProgram = nullptr;

// NVVM function pointers (may be null if NVVM is not available)
NvvmDestroyProgramFn p_nvvmDestroyProgram = nullptr;


// ============================================================================
// GIL management helpers
// ============================================================================
Expand Down Expand Up @@ -770,6 +774,28 @@ KernelHandle create_kernel_handle_ref(CUkernel kernel, const LibraryHandle& h_li
return KernelHandle(box, &box->resource);
}

// ============================================================================
// Graphics Resource Handles
// ============================================================================

namespace {
struct GraphicsResourceBox {
CUgraphicsResource resource;
};
} // namespace

GraphicsResourceHandle create_graphics_resource_handle(CUgraphicsResource resource) {
auto box = std::shared_ptr<const GraphicsResourceBox>(
new GraphicsResourceBox{resource},
[](const GraphicsResourceBox* b) {
GILReleaseGuard gil;
p_cuGraphicsUnregisterResource(b->resource);
delete b;
}
);
return GraphicsResourceHandle(box, &box->resource);
}

// ============================================================================
// NVRTC Program Handles
// ============================================================================
Expand Down
26 changes: 26 additions & 0 deletions cuda_core/cuda/core/_cpp/resource_handles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ extern decltype(&cuLibraryLoadData) p_cuLibraryLoadData;
extern decltype(&cuLibraryUnload) p_cuLibraryUnload;
extern decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel;

// Graphics interop
extern decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource;

// ============================================================================
// NVRTC function pointers
//
Expand Down Expand Up @@ -104,9 +107,11 @@ using EventHandle = std::shared_ptr<const CUevent>;
using MemoryPoolHandle = std::shared_ptr<const CUmemoryPool>;
using LibraryHandle = std::shared_ptr<const CUlibrary>;
using KernelHandle = std::shared_ptr<const CUkernel>;
using GraphicsResourceHandle = std::shared_ptr<const CUgraphicsResource>;
using NvrtcProgramHandle = std::shared_ptr<const nvrtcProgram>;
using NvvmProgramHandle = std::shared_ptr<const nvvmProgram>;


// ============================================================================
// Context handle functions
// ============================================================================
Expand Down Expand Up @@ -289,6 +294,15 @@ KernelHandle create_kernel_handle(const LibraryHandle& h_library, const char* na
// Use for borrowed kernels. The library handle keeps the library alive.
KernelHandle create_kernel_handle_ref(CUkernel kernel, const LibraryHandle& h_library);

// ============================================================================
// Graphics resource handle functions
// ============================================================================

// Create an owning graphics resource handle.
// When the last reference is released, cuGraphicsUnregisterResource is called automatically.
// Use for CUgraphicsResource handles obtained from cuGraphicsGLRegisterBuffer etc.
GraphicsResourceHandle create_graphics_resource_handle(CUgraphicsResource resource);

// ============================================================================
// NVRTC Program handle functions
// ============================================================================
Expand Down Expand Up @@ -349,6 +363,10 @@ inline CUkernel as_cu(const KernelHandle& h) noexcept {
return h ? *h : nullptr;
}

inline CUgraphicsResource as_cu(const GraphicsResourceHandle& h) noexcept {
return h ? *h : nullptr;
}

inline nvrtcProgram as_cu(const NvrtcProgramHandle& h) noexcept {
return h ? *h : nullptr;
}
Expand Down Expand Up @@ -387,6 +405,10 @@ inline std::intptr_t as_intptr(const KernelHandle& h) noexcept {
return reinterpret_cast<std::intptr_t>(as_cu(h));
}

inline std::intptr_t as_intptr(const GraphicsResourceHandle& h) noexcept {
return reinterpret_cast<std::intptr_t>(as_cu(h));
}

inline std::intptr_t as_intptr(const NvrtcProgramHandle& h) noexcept {
return reinterpret_cast<std::intptr_t>(as_cu(h));
}
Expand Down Expand Up @@ -447,4 +469,8 @@ inline PyObject* as_py(const NvvmProgramHandle& h) noexcept {
return PyLong_FromSsize_t(as_intptr(h));
}

inline PyObject* as_py(const GraphicsResourceHandle& h) noexcept {
return detail::make_py("cuda.bindings.driver", "CUgraphicsResource", as_intptr(h));
}

} // namespace cuda_core
14 changes: 14 additions & 0 deletions cuda_core/cuda/core/_graphics.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0

from cuda.core._resource_handles cimport GraphicsResourceHandle


cdef class GraphicsResource:

cdef:
GraphicsResourceHandle _handle
bint _mapped

cpdef close(self)
Loading