# OpenGL interoperability

**URL:** <https://dev-discuss.pytorch.org/t/opengl-interoperability/2696>\
**Category:** NVIDIA CUDA\
**Created:** [December 18, 2024, 5:08pm UTC](https://dev-discuss.pytorch.org/t/opengl-interoperability/2696 "2024-12-18T17:08:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cajoek](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/cajoek/32/2462_2.png) [@cajoek](https://dev-discuss.pytorch.org/u/cajoek)\
**Post date:** [December 18, 2024, 5:08pm UTC](https://dev-discuss.pytorch.org/t/opengl-interoperability/2696/1 "2024-12-18T17:08:51Z")

</div>

### 🚀 The feature, motivation and pitch

Zero-copy transfer of data between PyTorch and OpenGL on GPU by including “OpenGL interoperability” from CUDA in pytorch.

I am working on a real-time machine learning graphics project which uses OpenGL both as an intermediate processing step in the model and to visualize the output. Right now transfer of data between PyTorch and OpenGL is a problem for both training and inference.  
Without any additional packages i can copy data from PyTorch CUDA to CPU and then back to OpenGL on GPU, this is very simple but slow.  
I can instead use some cuda bindings for python and a separate CUDA Toolkit installation to avoid the data transfer but this is quite complex and there are many competing ways and tools for doing this which makes it hard to navigate.

Old similar issue: [#20047](https://github.com/pytorch/pytorch/issues/20047)  
Crosspost: [pytorch/vision#8803](https://github.com/pytorch/vision/issues/8803)

### Alternatives

_No response_

### Additional context

The 2 main ways I have been using OpenGL from python are with the packages `moderngl` and `PyOpenGL`.

> <https://github.com/pytorch/pytorch/issues/143419#issue-2745909540>
>
> \### 🚀 The feature, motivation and pitch
> 
> Zero-copy transfer of data between PyTo…rch and OpenGL on GPU by including "OpenGL interoperability" from CUDA in pytorch.
> 
> I am working on a real-time machine learning graphics project which uses OpenGL both as an intermediate processing step in the model and to visualize the output. Right now transfer of data between PyTorch and OpenGL is a problem for both training and inference.
> Without any additional packages i can copy data from PyTorch CUDA to CPU and then back to OpenGL on GPU, this is very simple but slow.
> I can instead use some cuda bindings for python and a separate CUDA Toolkit installation to avoid the data transfer but this is quite complex and there are many competing ways and tools for doing this which makes it hard to navigate.
> 
> Old similar issue: https://github.com/pytorch/pytorch/issues/20047
> Crosspost: https://github.com/pytorch/vision/issues/8803
> 
> \### Alternatives
> 
> \_No response\_
> 
> \### Additional context
> 
> The 2 main ways I have been using OpenGL from python are with the packages \`moderngl\` and \`PyOpenGL\`.
> 
> cc @ptrblck @msaroufim @eqy

---

<div class="post-metadata">

**Author:** ![albanD](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/alband/32/9_2.png) [@albanD](https://dev-discuss.pytorch.org/u/albanD)\
**Post date:** [December 19, 2024, 9:31am UTC](https://dev-discuss.pytorch.org/t/opengl-interoperability/2696/2 "2024-12-19T09:31:16Z")

</div>

Hey!

The main challenge for these kind of bindings is making a simple ownership system.

But if you have a simple case where you just want to share the data, you should be able to rewrap one way or the other by grabbing the data\_ptr() and size/stride/dtype metadata from one and creating an object on the other side with it.  
I would recommend reading this file that contains the two functions converting to and from numpy which will look exactly the same as an OpenGL equivalent: [pytorch/torch/csrc/utils/tensor\_numpy.cpp at main · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/blob/main/torch/csrc/utils/tensor_numpy.cpp#L262)

---

<div class="post-metadata">

**Author:** ![yquemener](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/yquemener/32/2976_2.png) [@yquemener](https://dev-discuss.pytorch.org/u/yquemener)\
**Post date:** [September 30, 2025, 6:04pm UTC](https://dev-discuss.pytorch.org/t/opengl-interoperability/2696/3 "2025-09-30T18:04:49Z")

</div>

Hi, I stumbled upon that post while looking for a way to manage visualization of ML weights without copying huge tensors. I worked from this cupy ↔ GL example to get…

> <https://gist.github.com/keckj/e37d312128eac8c5fca790ce1e7fc437>

… to that file that does display as an OpenGL texture the content of a PyTorch Tensor without making any copy:

> <https://github.com/yquemener/WoL/blob/26dc71da259afb29d7273edfff7f3a02f63e01e5/experiments/memory_sharing/minimal_interop.py>

The main issue is that you have to initialize an OpenGL buffer as you are allocating the tensor, you can’t directly access an already allocated tensor, but that’s a good start.
