Zivid C++ API 2.18.0+1b44dbef-1
DeviceArray.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of the Zivid API
3 *
4 * Copyright 2015-2026 (C) Zivid AS
5 * All rights reserved.
6 *
7 * Zivid Software License, v1.0
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of Zivid AS nor the names of its contributors may be used
20 * to endorse or promote products derived from this software without specific
21 * prior written permission.
22 *
23 * 4. This software, with or without modification, must not be used with any
24 * other 3D camera than from Zivid AS.
25 *
26 * 5. Any software provided in binary form under this license must not be
27 * reverse engineered, decompiled, modified and/or disassembled.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Contact: Zivid Customer Success Team <customersuccess@zivid.com>
41 * Info: http://www.zivid.com
42 ******************************************************************************/
43
44#pragma once
45
46#include "Zivid/Array1D.h"
47#include "Zivid/Array2D.h"
48#include "Zivid/Color.h"
51#include "Zivid/Disparity.h"
52#include "Zivid/Image.h"
53#include "Zivid/Normal.h"
54#include "Zivid/Point.h"
55#include "Zivid/SNR.h"
56
57#include <memory>
58#include <type_traits>
59#include <vector>
60
61namespace Zivid
62{
92 template<typename Format>
94 {
95 static_assert(
96 std::is_same_v<Format, PointXYZ> || std::is_same_v<Format, PointXYZW> || std::is_same_v<Format, PointZ>
97 || std::is_same_v<Format, SNR> || std::is_same_v<Format, Disparity> || std::is_same_v<Format, NormalXYZ>
98 || std::is_same_v<Format, ColorRGBA> || std::is_same_v<Format, ColorRGBA_SRGB>
99 || std::is_same_v<Format, ColorBGRA> || std::is_same_v<Format, ColorBGRA_SRGB>
100 || std::is_same_v<Format, ColorRGBAf> || std::is_same_v<Format, ColorRGB>
101 || std::is_same_v<Format, ColorRGB_SRGB> || std::is_same_v<Format, ColorBGR>
102 || std::is_same_v<Format, ColorBGR_SRGB>,
103 "Format must be one of: PointXYZ, PointXYZW, PointZ, SNR, Disparity, NormalXYZ, "
104 "ColorRGBA, ColorRGBA_SRGB, ColorBGRA, ColorBGRA_SRGB, ColorRGBAf, ColorRGB, ColorRGB_SRGB, "
105 "ColorBGR, or ColorBGR_SRGB");
106
107 public:
109 DeviceArray() = default;
110
112 ~DeviceArray() = default;
113
115 DeviceArray(const DeviceArray &other) = default;
116
118 DeviceArray &operator=(const DeviceArray &other) = default;
119
121 DeviceArray(DeviceArray &&other) noexcept = default;
122
124 DeviceArray &operator=(DeviceArray &&other) noexcept = default;
125
136 void *devicePointer() const;
137
139 std::vector<int> shape() const;
140
142 std::vector<int> strides() const;
143
145 std::vector<int> stridesInBytes() const;
146
148 size_t size() const;
149
151 size_t sizeInBytes() const;
152
156
158 bool isValid() const;
159
161 bool isEmpty() const;
162
182
197
212 void copyToHost(Format *destination, size_t sizeInBytes, StreamOrQueue streamOrQueue) const;
213
214#ifndef NO_DOC
215 explicit DeviceArray(std::shared_ptr<class DeviceArrayInterface> impl);
216 const std::shared_ptr<class DeviceArrayInterface> &getImpl() const;
217#endif
218
219 private:
220 std::shared_ptr<class DeviceArrayInterface> m_impl;
221 };
222
223#ifndef NO_DOC
227 extern template class ZIVID_CORE_EXPORT_NON_WIN32 DeviceArray<SNR>;
239#endif
240
268 template<typename Format>
270 {
271 static_assert(
272 std::is_same_v<Format, PointXYZ> || std::is_same_v<Format, PointXYZW> || std::is_same_v<Format, PointZ>
273 || std::is_same_v<Format, SNR> || std::is_same_v<Format, Disparity> || std::is_same_v<Format, NormalXYZ>
274 || std::is_same_v<Format, ColorRGBA> || std::is_same_v<Format, ColorRGBA_SRGB>
275 || std::is_same_v<Format, ColorBGRA> || std::is_same_v<Format, ColorBGRA_SRGB>
276 || std::is_same_v<Format, ColorRGBAf> || std::is_same_v<Format, ColorRGB>
277 || std::is_same_v<Format, ColorRGB_SRGB> || std::is_same_v<Format, ColorBGR>
278 || std::is_same_v<Format, ColorBGR_SRGB>,
279 "Format must be one of: PointXYZ, PointXYZW, PointZ, SNR, Disparity, NormalXYZ, "
280 "ColorRGBA, ColorRGBA_SRGB, ColorBGRA, ColorBGRA_SRGB, ColorRGBAf, ColorRGB, ColorRGB_SRGB, "
281 "ColorBGR, or ColorBGR_SRGB");
282
283 public:
285 DeviceArrayView() = default;
286
288 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
290
292 ~DeviceArrayView() = default;
293
295 DeviceArrayView(const DeviceArrayView &other) = default;
296
298 DeviceArrayView &operator=(const DeviceArrayView &other) = default;
299
301 DeviceArrayView(DeviceArrayView &&other) noexcept = default;
302
304 DeviceArrayView &operator=(DeviceArrayView &&other) noexcept = default;
305
307 void *devicePointer() const;
308
310 std::vector<int> shape() const;
311
313 std::vector<int> strides() const;
314
316 std::vector<int> stridesInBytes() const;
317
319 size_t size() const;
320
322 size_t sizeInBytes() const;
323
326
328 bool isValid() const;
329
331 bool isEmpty() const;
332
341 void copyToHost(Format *destination, size_t sizeInBytes, StreamOrQueue streamOrQueue) const;
342
351
360
361#ifndef NO_DOC
362 explicit DeviceArrayView(std::shared_ptr<class DeviceArrayInterface> impl);
363 const std::shared_ptr<class DeviceArrayInterface> &getImpl() const;
364#endif
365
366 private:
367 std::shared_ptr<class DeviceArrayInterface> m_impl;
368 };
369
370#ifndef NO_DOC
386#endif
387
388#ifndef NO_DOC
390 namespace Detail
391 {
392 template<typename ColorFormat>
393 struct SupportedImageDeviceArrayColorFormat : std::false_type
394 {};
395
396 template<>
397 struct SupportedImageDeviceArrayColorFormat<ColorRGBA> : std::true_type
398 {};
399
400 template<>
401 struct SupportedImageDeviceArrayColorFormat<ColorBGRA> : std::true_type
402 {};
403
404 template<>
405 struct SupportedImageDeviceArrayColorFormat<ColorRGBA_SRGB> : std::true_type
406 {};
407
408 template<>
409 struct SupportedImageDeviceArrayColorFormat<ColorBGRA_SRGB> : std::true_type
410 {};
411
412 template<>
413 struct SupportedImageDeviceArrayColorFormat<ColorRGBAf> : std::true_type
414 {};
415
416 template<>
417 struct SupportedImageDeviceArrayColorFormat<ColorRGB> : std::true_type
418 {};
419
420 template<>
421 struct SupportedImageDeviceArrayColorFormat<ColorRGB_SRGB> : std::true_type
422 {};
423
424 template<>
425 struct SupportedImageDeviceArrayColorFormat<ColorBGR> : std::true_type
426 {};
427
428 template<>
429 struct SupportedImageDeviceArrayColorFormat<ColorBGR_SRGB> : std::true_type
430 {};
431
432 template<typename Format>
433 struct SupportedDeviceArrayViewFormat : std::bool_constant<SupportedImageDeviceArrayColorFormat<Format>::value>
434 {};
435 } // namespace Detail
436#endif
437
450 template<typename ColorFormat, typename = std::enable_if_t<Detail::SupportedImageColorFormat<ColorFormat>::value>>
452 {
453 return Image<ColorFormat>{ buffer.toArray2D(streamOrQueue) };
454 }
455
468 template<typename ColorFormat, typename = std::enable_if_t<Detail::SupportedImageColorFormat<ColorFormat>::value>>
470 {
471 return Image<ColorFormat>{ buffer.toArray2D(streamOrQueue) };
472 }
473} // namespace Zivid
#define ZIVID_CORE_EXPORT_NON_WIN32
Definition CoreExport.h:61
One-dimensional container of data.
Definition Array1D.h:72
Two-dimensional container of data.
Definition Array2D.h:83
Non-owning view of a device buffer.
Definition DeviceArray.h:270
void * devicePointer() const
Get the raw GPU device pointer to the data.
DeviceArrayView(const DeviceArray< Format > &source)
Borrow a view from an owning DeviceArray.
DeviceArrayView(DeviceArrayView &&other) noexcept=default
Move constructor.
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.
Array2D< Format > toArray2D(StreamOrQueue streamOrQueue) const
Enqueue a device-to-host copy and return as Array2D without synchronizing.
bool isValid() const
Check if the buffer is valid (not empty)
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 isEmpty() const
Check if the buffer is empty.
~DeviceArrayView()=default
Destructor.
DeviceArrayView()=default
Default constructor - creates an empty view.
DeviceArrayView(const DeviceArrayView &other)=default
Copy constructor - shares the view.
std::vector< int > strides() const
Get the strides of the data in element count.
DeviceArrayView & operator=(DeviceArrayView &&other) noexcept=default
Move assignment.
DeviceArrayView & operator=(const DeviceArrayView &other)=default
Copy assignment.
Array1D< Format > toArray1D(StreamOrQueue streamOrQueue) const
Enqueue a device-to-host copy and return as Array1D without synchronizing.
std::vector< int > shape() const
Get the shape of the data.
void copyToHost(Format *destination, size_t sizeInBytes, StreamOrQueue streamOrQueue) const
Enqueue a device-to-host copy without synchronizing.
Reference-counted handle to data on a GPU device.
Definition DeviceArray.h:94
ComputeBackend backend() const
Get the GPU compute backend type of this buffer.
void * devicePointer() const
Get the raw GPU device pointer to the data.
DeviceArray & operator=(const DeviceArray &other)=default
Copy assignment - shares ownership of the buffer.
DeviceArray(DeviceArray &&other) noexcept=default
Move constructor.
bool isEmpty() const
Check if the buffer is empty.
DeviceArray & operator=(DeviceArray &&other) noexcept=default
Move assignment.
size_t size() const
Get the total number of elements in the data.
std::vector< int > strides() const
Get the strides of the data in element count.
void copyToHost(Format *destination, size_t sizeInBytes, StreamOrQueue streamOrQueue) const
Copy device buffer data to host memory.
Array2D< Format > toArray2D(StreamOrQueue streamOrQueue) const
Copy device buffer data to host and return as Array2D.
size_t sizeInBytes() const
Get the total size of the data in bytes.
DeviceArray(const DeviceArray &other)=default
Copy constructor - shares ownership of the buffer.
~DeviceArray()=default
Destructor - releases reference to the underlying buffer.
bool isValid() const
Check if the buffer is valid (not empty)
Array1D< Format > toArray1D(StreamOrQueue streamOrQueue) const
Copy device buffer data to host and return as Array1D.
std::vector< int > shape() const
Get the shape of the data.
std::vector< int > stridesInBytes() const
Get the strides of the data in bytes.
DeviceArray()=default
Default constructor - creates an empty DeviceArray.
A 2-dimensional image.
Definition Image.h:141
Definition EnvironmentInfo.h:74
The main Zivid namespace. All Zivid code is found here.
Definition Application.h:85
Image< ColorFormat > toImage(const DeviceArray< ColorFormat > &buffer, StreamOrQueue streamOrQueue)
Enqueue a device-to-host copy and return as Image without synchronizing.
Definition DeviceArray.h:451
ComputeBackend
GPU compute backend type.
Definition ComputeWrappers.h:52
A user CUDA stream or OpenCL command queue, selected by which member is populated.
Definition ComputeWrappers.h:106