Zivid C++ API 2.18.0+1b44dbef-1
Zivid::DeviceArray< Format > Class Template Reference

Reference-counted handle to data on a GPU device. More...

#include <Zivid/DeviceArray.h>

Public Member Functions

 DeviceArray ()=default
 Default constructor - creates an empty DeviceArray.
 ~DeviceArray ()=default
 Destructor - releases reference to the underlying buffer.
 DeviceArray (const DeviceArray &other)=default
 Copy constructor - shares ownership of the buffer.
DeviceArrayoperator= (const DeviceArray &other)=default
 Copy assignment - shares ownership of the buffer.
 DeviceArray (DeviceArray &&other) noexcept=default
 Move constructor.
DeviceArrayoperator= (DeviceArray &&other) noexcept=default
 Move assignment.
void * devicePointer () const
 Get the raw GPU device pointer to the data.
std::vector< int > shape () const
 Get the shape of the data.
std::vector< int > strides () const
 Get the strides of the data in element count.
std::vector< int > stridesInBytes () const
 Get the strides of the data in bytes.
size_t size () const
 Get the total number of elements in the data.
size_t sizeInBytes () const
 Get the total size of the data in bytes.
ComputeBackend backend () const
 Get the GPU compute backend type of this buffer.
bool isValid () const
 Check if the buffer is valid (not empty)
bool isEmpty () const
 Check if the buffer is empty.
Array2D< Format > toArray2D (StreamOrQueue streamOrQueue) const
 Copy device buffer data to host and return as Array2D.
Array1D< Format > toArray1D (StreamOrQueue streamOrQueue) const
 Copy device buffer data to host and return as Array1D.
void copyToHost (Format *destination, size_t sizeInBytes, StreamOrQueue streamOrQueue) const
 Copy device buffer data to host memory.

Detailed Description

template<typename Format>
class Zivid::DeviceArray< Format >

Reference-counted handle to data on a GPU device.

The underlying device buffer is reference counted and will remain valid as long as any DeviceArray instance references it.

The buffer provides access to a raw device pointer that can be used with GPU compute frameworks (such as CUDA), compute libraries, or other GPU frameworks for efficient processing without CPU transfers.

Obtain a DeviceArray from a PointCloud using methods like devicePointsXYZ(), devicePointsXYZW(), deviceSNRs(), deviceNormalsXYZ(), deviceImageRGBA(), etc. The acquisition method takes a user stream or queue and synchronizes the SDK's compute stream into it before returning. The returned DeviceArray is therefore already ordered against the caller's stream/queue, and devicePointer() is a plain accessor.

Lifetime: a DeviceArray allocated by the SDK must not outlive the Zivid Application that produced it. For device memory that needs to outlive the Application, allocate the buffer yourself and pass it into the fill variant of the acquisition method (e.g. imageDeviceArray<Format>(streamOrQueue, destinationBuffer)).

Example usage:

// Acquire an XYZ buffer synchronized on the user's CUDA stream
auto xyzBuffer = pointCloud.devicePointsXYZ(Zivid::CUDAStreamPtr{ cudaStream });
// Access the raw device pointer - no additional sync needed
void *ptr = xyzBuffer.devicePointer();
Wrapper for user-provided CUDA stream for synchronization.
Definition ComputeWrappers.h:85

Constructor & Destructor Documentation

◆ DeviceArray() [1/3]

template<typename Format>
Zivid::DeviceArray< Format >::DeviceArray ( )
default

Default constructor - creates an empty DeviceArray.

◆ ~DeviceArray()

template<typename Format>
Zivid::DeviceArray< Format >::~DeviceArray ( )
default

Destructor - releases reference to the underlying buffer.

◆ DeviceArray() [2/3]

template<typename Format>
Zivid::DeviceArray< Format >::DeviceArray ( const DeviceArray< Format > & other)
default

Copy constructor - shares ownership of the buffer.

◆ DeviceArray() [3/3]

template<typename Format>
Zivid::DeviceArray< Format >::DeviceArray ( DeviceArray< Format > && other)
defaultnoexcept

Move constructor.

Member Function Documentation

◆ backend()

template<typename Format>
ComputeBackend Zivid::DeviceArray< Format >::backend ( ) const

Get the GPU compute backend type of this buffer.

Returns
The GPU compute backend (CUDA or OpenCL)

◆ copyToHost()

template<typename Format>
void Zivid::DeviceArray< Format >::copyToHost ( Format * destination,
size_t sizeInBytes,
StreamOrQueue streamOrQueue ) const

Copy device buffer data to host memory.

Parameters
destinationPointer to host memory where data will be copied
sizeInBytesSize of the destination buffer in bytes
streamOrQueueThe CUDA stream / OpenCL command queue to enqueue the D2H copy on

Enqueues the device-to-host copy on streamOrQueue and returns immediately. The caller is responsible for synchronizing streamOrQueue (e.g. via Zivid::synchronizeStream) before reading from destination. The caller must also ensure that the destination buffer is large enough to hold the data.

Pass the same stream/queue that was used at acquisition so the D2H copy is correctly ordered against the SDK's compute. For a blocking copy that does the sync internally, use the parameterless host accessors on the originating Frame2D / PointCloud.

◆ devicePointer()

template<typename Format>
void * Zivid::DeviceArray< Format >::devicePointer ( ) const

Get the raw GPU device pointer to the data.

Returns
Raw device pointer (CUstream-typed for CUDA builds, cl_mem cast to void* for OpenCL).

No synchronization is performed. The DeviceArray was already synchronized against the caller's stream/queue at acquisition time by the method that produced it (e.g. Frame2D::imageDeviceArray, PointCloud::devicePointsXYZ). Callers can use the pointer on that same stream/queue immediately.

The returned pointer is only valid as long as this DeviceArray (or a copy) exists, and only while the Zivid Application that produced the DeviceArray is alive.

◆ isEmpty()

template<typename Format>
bool Zivid::DeviceArray< Format >::isEmpty ( ) const

Check if the buffer is empty.

◆ isValid()

template<typename Format>
bool Zivid::DeviceArray< Format >::isValid ( ) const

Check if the buffer is valid (not empty)

◆ operator=() [1/2]

template<typename Format>
DeviceArray & Zivid::DeviceArray< Format >::operator= ( const DeviceArray< Format > & other)
default

Copy assignment - shares ownership of the buffer.

◆ operator=() [2/2]

template<typename Format>
DeviceArray & Zivid::DeviceArray< Format >::operator= ( DeviceArray< Format > && other)
defaultnoexcept

Move assignment.

◆ shape()

template<typename Format>
std::vector< int > Zivid::DeviceArray< Format >::shape ( ) const

Get the shape of the data.

◆ size()

template<typename Format>
size_t Zivid::DeviceArray< Format >::size ( ) const

Get the total number of elements in the data.

◆ sizeInBytes()

template<typename Format>
size_t Zivid::DeviceArray< Format >::sizeInBytes ( ) const

Get the total size of the data in bytes.

◆ strides()

template<typename Format>
std::vector< int > Zivid::DeviceArray< Format >::strides ( ) const

Get the strides of the data in element count.

◆ stridesInBytes()

template<typename Format>
std::vector< int > Zivid::DeviceArray< Format >::stridesInBytes ( ) const

Get the strides of the data in bytes.

◆ toArray1D()

template<typename Format>
Array1D< Format > Zivid::DeviceArray< Format >::toArray1D ( StreamOrQueue streamOrQueue) const

Copy device buffer data to host and return as Array1D.

Parameters
streamOrQueueThe CUDA stream / OpenCL command queue to enqueue the D2H copy on
Returns
Array1D of the Format type, holding host memory that has not yet been synchronized

Valid for all DeviceArray formats: PointXYZ, PointXYZW, PointZ, SNR, NormalXYZ, ColorRGBA, ColorRGBA_SRGB, ColorBGRA, ColorBGRA_SRGB, and ColorRGBAf. Throws an exception if the DeviceArray is not from an unorganized point cloud.

The D2H copy is enqueued on streamOrQueue and the function returns immediately; the host memory behind the returned Array1D is NOT safe to read until the caller synchronizes streamOrQueue (e.g. via Zivid::synchronizeStream). For a blocking copy, use the parameterless host accessors on the originating UnorganizedPointCloud (e.g. UnorganizedPointCloud::pointsXYZ).

◆ toArray2D()

template<typename Format>
Array2D< Format > Zivid::DeviceArray< Format >::toArray2D ( StreamOrQueue streamOrQueue) const

Copy device buffer data to host and return as Array2D.

Parameters
streamOrQueueThe CUDA stream / OpenCL command queue to enqueue the D2H copy on
Returns
Array2D of the Format type, holding host memory that has not yet been synchronized

Valid for all DeviceArray formats: PointXYZ, PointXYZW, PointZ, SNR, NormalXYZ, ColorRGBA, ColorRGBA_SRGB, ColorBGRA, ColorBGRA_SRGB, and ColorRGBAf. Throws an exception if the DeviceArray is not from an organized point cloud or from Frame2D.

The D2H copy is enqueued on streamOrQueue and the function returns immediately; the host memory behind the returned Array2D is NOT safe to read until the caller synchronizes streamOrQueue (e.g. via Zivid::synchronizeStream). Pass the same stream/queue that was used at acquisition (e.g. with PointCloud::devicePointsXYZ or Frame2D::imageDeviceArray) so the D2H copy is correctly ordered against the SDK's compute.

If you don't want to manage stream synchronization yourself, use the parameterless host accessors on the originating Frame2D / PointCloud (e.g. PointCloud::pointXYZArray, Frame2D::imageRGBA); those return host-ready Array2D / Image using the SDK's internal stream.


The documentation for this class was generated from the following file: