Skip to content

Buffer

Buffer wrapper.

This module provides a thin Python wrapper around OIDN's buffer API.

Buffers are owned by a :class:pyoidn.device.Device and can be used with filters via oidnSetFilterImage (buffer-based images) or manually read/written from host memory.

Typical usage:

.. code-block:: python

import pyoidn
import numpy as np

data = np.zeros((64, 64, 3), dtype=np.float32)
with pyoidn.Device() as device:
    device.commit()

    buf = pyoidn.Buffer(device, data.nbytes, storage=pyoidn.OIDN_STORAGE_HOST)
    buf.write(0, data.nbytes, data)
    # ... use buf with Filter.set_image(...)
    buf.release()

Notes

  • This wrapper mirrors OIDN's lifecycle: allocate -> use -> :meth:Buffer.release.
  • read*/write* take host buffers (e.g., bytes, bytearray, memoryview, NumPy arrays).
  • Refer to https://github.com/RenderKit/oidn?tab=readme-ov-file#buffers for the OIDN buffer functions.

Buffer

Buffer(device: Device, byte_size: int, storage: Optional[int] = None)

OIDN buffer wrapper.

Mirrors the functions in OIDN_FUNCTION_BUFFER.

Notes: - This is a thin wrapper; call release() when done. - read*/write* operate on host memory pointers.

Create a new OIDN buffer.

Parameters:

Name Type Description Default
device Device

The :class:pyoidn.device.Device that owns this buffer.

required
byte_size int

Buffer size in bytes.

required
storage Optional[int]

Optional storage type, one of OIDN_STORAGE_*. If omitted, OIDN chooses a default.

None

Raises:

Type Description
ValueError

If byte_size is negative.

size property

size: int

Return the buffer size in bytes.

storage property

storage: int

Return the storage type (one of OIDN_STORAGE_*).

__enter__

__enter__() -> 'Buffer'

Enter a context manager.

Returns:

Type Description
'Buffer'

This buffer.

__exit__

__exit__(exc_type, exc, tb)

Exit a context manager and release the buffer.

get_data

get_data()

Return the raw pointer returned by oidnGetBufferData.

Returns:

Type Description

A cffi pointer.

read

read(byte_offset: int, byte_size: int, dst) -> None

Read from this buffer into a writable host buffer.

Parameters:

Name Type Description Default
byte_offset int

Offset in bytes.

required
byte_size int

Number of bytes to read.

required
dst

Destination host buffer. Must be writable and support the buffer protocol (e.g., bytearray, writable memoryview, NumPy array).

required

Raises:

Type Description
ValueError

If byte_offset or byte_size is negative.

read_async

read_async(byte_offset: int, byte_size: int, dst) -> None

Asynchronous version of :meth:read.

You must synchronize via :meth:pyoidn.device.Device.wait before reading the destination.

Parameters:

Name Type Description Default
byte_offset int

Offset in bytes.

required
byte_size int

Number of bytes to read.

required
dst

Destination host buffer (writable).

required

Raises:

Type Description
ValueError

If byte_offset or byte_size is negative.

release

release()

Release the underlying OIDN buffer handle.

This method is idempotent.

shared classmethod

shared(device: Device, dev_ptr, byte_size: int) -> 'Buffer'

Create a shared buffer from an existing pointer.

This wraps an external allocation as an OIDN buffer without copying.

Parameters:

Name Type Description Default
device Device

The :class:pyoidn.device.Device that owns this buffer.

required
dev_ptr

A pointer to existing memory. Can be an integer address or a cffi pointer compatible with void*.

required
byte_size int

Buffer size in bytes.

required

Raises:

Type Description
ValueError

If byte_size is negative.

shared_from_numpy classmethod

shared_from_numpy(device: Device, array: ndarray) -> 'Buffer'

Create a shared buffer from a NumPy array.

This wraps the NumPy array's memory as an OIDN buffer without copying.

Parameters:

Name Type Description Default
device Device

The :class:pyoidn.device.Device that owns this buffer.

required
array ndarray

A NumPy array in host memory.

required

Raises:

Type Description
ValueError

If the array is not contiguous.

shared_from_torch classmethod

shared_from_torch(device: Device, tensor: Any) -> 'Buffer'

Create a shared buffer from a PyTorch tensor.

This wraps the tensor's memory as an OIDN buffer without copying.

Parameters:

Name Type Description Default
device Device

The :class:pyoidn.device.Device that owns this buffer.

required
tensor Any

A PyTorch tensor in host or device memory.

required

Raises:

Type Description
ImportError

If PyTorch is not available.

ValueError

If the tensor is not contiguous.

write

write(byte_offset: int, byte_size: int, src) -> None

Write into this buffer from a host buffer.

Parameters:

Name Type Description Default
byte_offset int

Offset in bytes.

required
byte_size int

Number of bytes to write.

required
src

Source host buffer. Must support the buffer protocol (e.g., bytes, bytearray, memoryview, NumPy array).

required

Raises:

Type Description
ValueError

If byte_offset or byte_size is negative.

write_async

write_async(byte_offset: int, byte_size: int, src) -> None

Asynchronous version of :meth:write.

You must synchronize via :meth:pyoidn.device.Device.wait before using the written data.

Parameters:

Name Type Description Default
byte_offset int

Offset in bytes.

required
byte_size int

Number of bytes to write.

required
src

Source host buffer.

required

Raises:

Type Description
ValueError

If byte_offset or byte_size is negative.