Skip to content

Commit d2909f4

Browse files
rparolinclaude
andauthored
Add CUDA-Graphics (OpenGL) interop support to cuda.core (#1608)
* initial commit, plasma effect is rendering * removing the stream.sync * broken merge * Adding the nvrtc dependency to tests * Disabled vsync and reporting FPS and frame time * Including the resolution in the title update * Fix pre-commit lint issues in graphics interop files - Remove unused GraphicsResourceHandle cimport (cython-lint) - Split long f-string to stay within 120-char line limit (ruff E501) - Rename unused variable buf to _buf (ruff F841) - Add noqa: S110 to intentional try-except-pass in test cleanup - Apply ruff auto-fixes (import sorting, formatting) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 494f53c commit d2909f4

File tree

11 files changed

+5733
-253
lines changed

11 files changed

+5733
-253
lines changed

cuda_core/cuda/core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
GraphCompleteOptions,
3838
GraphDebugPrintOptions,
3939
)
40+
from cuda.core._graphics import GraphicsResource # noqa: E402
4041
from cuda.core._launch_config import LaunchConfig # noqa: E402
4142
from cuda.core._launcher import launch # noqa: E402
4243
from cuda.core._layout import _StridedLayout # noqa: E402

cuda_core/cuda/core/_cpp/resource_handles.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ decltype(&cuLibraryLoadData) p_cuLibraryLoadData = nullptr;
5656
decltype(&cuLibraryUnload) p_cuLibraryUnload = nullptr;
5757
decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel = nullptr;
5858

59+
// GL interop pointers
60+
decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource = nullptr;
61+
5962
// NVRTC function pointers
6063
decltype(&nvrtcDestroyProgram) p_nvrtcDestroyProgram = nullptr;
6164

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

68+
6569
// ============================================================================
6670
// GIL management helpers
6771
// ============================================================================
@@ -808,6 +812,28 @@ KernelHandle create_kernel_handle_ref(CUkernel kernel, const LibraryHandle& h_li
808812
return KernelHandle(box, &box->resource);
809813
}
810814

815+
// ============================================================================
816+
// Graphics Resource Handles
817+
// ============================================================================
818+
819+
namespace {
820+
struct GraphicsResourceBox {
821+
CUgraphicsResource resource;
822+
};
823+
} // namespace
824+
825+
GraphicsResourceHandle create_graphics_resource_handle(CUgraphicsResource resource) {
826+
auto box = std::shared_ptr<const GraphicsResourceBox>(
827+
new GraphicsResourceBox{resource},
828+
[](const GraphicsResourceBox* b) {
829+
GILReleaseGuard gil;
830+
p_cuGraphicsUnregisterResource(b->resource);
831+
delete b;
832+
}
833+
);
834+
return GraphicsResourceHandle(box, &box->resource);
835+
}
836+
811837
// ============================================================================
812838
// NVRTC Program Handles
813839
// ============================================================================

cuda_core/cuda/core/_cpp/resource_handles.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ extern decltype(&cuLibraryLoadData) p_cuLibraryLoadData;
7272
extern decltype(&cuLibraryUnload) p_cuLibraryUnload;
7373
extern decltype(&cuLibraryGetKernel) p_cuLibraryGetKernel;
7474

75+
// Graphics interop
76+
extern decltype(&cuGraphicsUnregisterResource) p_cuGraphicsUnregisterResource;
77+
7578
// ============================================================================
7679
// NVRTC function pointers
7780
//
@@ -104,9 +107,11 @@ using EventHandle = std::shared_ptr<const CUevent>;
104107
using MemoryPoolHandle = std::shared_ptr<const CUmemoryPool>;
105108
using LibraryHandle = std::shared_ptr<const CUlibrary>;
106109
using KernelHandle = std::shared_ptr<const CUkernel>;
110+
using GraphicsResourceHandle = std::shared_ptr<const CUgraphicsResource>;
107111
using NvrtcProgramHandle = std::shared_ptr<const nvrtcProgram>;
108112
using NvvmProgramHandle = std::shared_ptr<const nvvmProgram>;
109113

114+
110115
// ============================================================================
111116
// Context handle functions
112117
// ============================================================================
@@ -306,6 +311,15 @@ KernelHandle create_kernel_handle(const LibraryHandle& h_library, const char* na
306311
// Use for borrowed kernels. The library handle keeps the library alive.
307312
KernelHandle create_kernel_handle_ref(CUkernel kernel, const LibraryHandle& h_library);
308313

314+
// ============================================================================
315+
// Graphics resource handle functions
316+
// ============================================================================
317+
318+
// Create an owning graphics resource handle.
319+
// When the last reference is released, cuGraphicsUnregisterResource is called automatically.
320+
// Use for CUgraphicsResource handles obtained from cuGraphicsGLRegisterBuffer etc.
321+
GraphicsResourceHandle create_graphics_resource_handle(CUgraphicsResource resource);
322+
309323
// ============================================================================
310324
// NVRTC Program handle functions
311325
// ============================================================================
@@ -366,6 +380,10 @@ inline CUkernel as_cu(const KernelHandle& h) noexcept {
366380
return h ? *h : nullptr;
367381
}
368382

383+
inline CUgraphicsResource as_cu(const GraphicsResourceHandle& h) noexcept {
384+
return h ? *h : nullptr;
385+
}
386+
369387
inline nvrtcProgram as_cu(const NvrtcProgramHandle& h) noexcept {
370388
return h ? *h : nullptr;
371389
}
@@ -404,6 +422,10 @@ inline std::intptr_t as_intptr(const KernelHandle& h) noexcept {
404422
return reinterpret_cast<std::intptr_t>(as_cu(h));
405423
}
406424

425+
inline std::intptr_t as_intptr(const GraphicsResourceHandle& h) noexcept {
426+
return reinterpret_cast<std::intptr_t>(as_cu(h));
427+
}
428+
407429
inline std::intptr_t as_intptr(const NvrtcProgramHandle& h) noexcept {
408430
return reinterpret_cast<std::intptr_t>(as_cu(h));
409431
}
@@ -464,4 +486,8 @@ inline PyObject* as_py(const NvvmProgramHandle& h) noexcept {
464486
return PyLong_FromSsize_t(as_intptr(h));
465487
}
466488

489+
inline PyObject* as_py(const GraphicsResourceHandle& h) noexcept {
490+
return detail::make_py("cuda.bindings.driver", "CUgraphicsResource", as_intptr(h));
491+
}
492+
467493
} // namespace cuda_core

cuda_core/cuda/core/_graphics.pxd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from cuda.core._resource_handles cimport GraphicsResourceHandle
6+
7+
8+
cdef class GraphicsResource:
9+
10+
cdef:
11+
GraphicsResourceHandle _handle
12+
bint _mapped
13+
14+
cpdef close(self)

0 commit comments

Comments
 (0)