Skip to main content

Fast graphics texture to cuda transfer

Project description

Gfx2Cuda - Graphics to CUDA interoperability

Gfx2Cuda is a python implementation of CUDA's graphics interopability methods for DirectX, OpenGL, etc. The main usage is for quick transfer of images rendered with for example Godot or Unity to CUDA memory buffers such as pytoch tensors, without needing to transfer the image to cpu and back to gpu.

For now only DirectX 11 is supported. This can be useful for implementing CUDA ipc (interprocess-communication) for Windows, since that functionality is not available in vanilla CUDA for Windows. You would use a DirectX texture as buffer that can be seen by multiple processes without having to download any gpu data to cpu and back.

Example

Render to texture and copy to pytorch tensor

import gfx2cuda
import torch

# Shape: [height, width, channels]
shape = [480, 640, 4]
tensor1 = torch.ones(shape).contiguous().cuda()
tensor2 = torch.zeros(shape).contiguous().cuda()

# Create copy of a tensor but as a texture
tex = gfx2cuda.texture(tensor1)

with tex as ptr:
    tex.copy_to(tensor2)

print(tensor2.data)
# pytorch tensor should now contain a copy of the texture data

Share texture between process, write on one process and see results in the other

from multiprocessing import Process

import gfx2cuda
import torch

shape = [4, 4, 4]

def f(handle):
    tex = gfx2cuda.open_ipc_texture(handle)
    # Received and opened the texture
    print(tex)
    # >> Texture with format TextureFormat.RGBA32FLOAT (4 x 4)
    tensor1 = torch.ones(shape).contiguous().cuda()
    with tex:
        tex.copy_from(tensor1)

if __name__ == "__main__":
    tensor = torch.zeros(shape).contiguous().cuda()
    # Initialize as all zeros
    tex = gfx2cuda.texture(tensor)

    p = Process(target=f, args=(tex.ipc_handle,))
    p.start()
    p.join()

    with tex:
        tex.copy_to(tensor)

    print(tensor.data)
    # See all ones

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gfx2cuda-0.0.2.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gfx2cuda-0.0.2-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file gfx2cuda-0.0.2.tar.gz.

File metadata

  • Download URL: gfx2cuda-0.0.2.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for gfx2cuda-0.0.2.tar.gz
Algorithm Hash digest
SHA256 071d1dac01273a3197bde9b87865e5a88f5a9fdfa41d8f03432a2a5421bd2559
MD5 06c9c01efb5392b88cef81d5c9a9712b
BLAKE2b-256 2002ef09ab8b75cb4ff44194131779923a27c693faae6a99cdad2e7fc5776e4f

See more details on using hashes here.

File details

Details for the file gfx2cuda-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: gfx2cuda-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for gfx2cuda-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49429b0ec37c2881b6f904b52c30cd82ddfafc604f9257bb96254a407598ae0e
MD5 ba6ba3ad16a8def445393f5a79b8a9b0
BLAKE2b-256 f4a4970711e74ea44acb48aab893cb9aff1c7efa76260c1212822c038877e8d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page