Zivid C++ API 2.19.0+b5c53d0f-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/Image.h"
52#include "Zivid/Normal.h"
53#include "Zivid/Point.h"
54#include "Zivid/SNR.h"
55
56#include <memory>
57#include <type_traits>
58#include <vector>
59
60namespace Zivid
61{
62#ifndef NO_DOC
64 namespace Detail
65 {
66 template<typename Format>
67 struct IsDeviceArrayFormat : std::bool_constant<
68 std::is_same_v<Format, PointXYZ> || std::is_same_v<Format, PointXYZW>
69 || std::is_same_v<Format, PointZ> || std::is_same_v<Format, SNR>
70 || std::is_same_v<Format, NormalXYZ> || std::is_same_v<Format, ColorRGBA>
71 || std::is_same_v<Format, ColorRGBA_SRGB> || std::is_same_v<Format, ColorBGRA>
72 || std::is_same_v<Format, ColorBGRA_SRGB> || std::is_same_v<Format, ColorRGBAf>
73 || std::is_same_v<Format, ColorRGB> || std::is_same_v<Format, ColorRGB_SRGB>
74 || std::is_same_v<Format, ColorBGR> || std::is_same_v<Format, ColorBGR_SRGB>>
75 {};
76 } // namespace Detail
77#endif
78
108 template<typename Format>
110 {
111 static_assert(
112 Detail::IsDeviceArrayFormat<Format>::value,
113 "Format must be one of: PointXYZ, PointXYZW, PointZ, SNR, NormalXYZ, "
114 "ColorRGBA, ColorRGBA_SRGB, ColorBGRA, ColorBGRA_SRGB, ColorRGBAf, ColorRGB, ColorRGB_SRGB, "
115 "ColorBGR, or ColorBGR_SRGB");
116
117 public:
119 DeviceArray() = default;
120
122 ~DeviceArray() = default;
123
125 DeviceArray(const DeviceArray &other) = default;
126
128 DeviceArray &operator=(const DeviceArray &other) = default;
129
131 DeviceArray(DeviceArray &&other) noexcept = default;
132
134 DeviceArray &operator=(DeviceArray &&other) noexcept = default;
135
146 void *devicePointer() const;
147
149 std::vector<int> shape() const;
150
152 std::vector<int> strides() const;
153
155 std::vector<int> stridesInBytes() const;
156
158 size_t size() const;
159
161 size_t sizeInBytes() const;
162
166
168 bool isValid() const;
169
171 bool isEmpty() const;
172
192
207
222 void copyToHost(Format *destination, size_t sizeInBytes, StreamOrQueue streamOrQueue) const;
223
224#ifndef NO_DOC
225 explicit DeviceArray(std::shared_ptr<class DeviceArrayInterface> impl);
226 const std::shared_ptr<class DeviceArrayInterface> &getImpl() const;
227#endif
228
229 private:
230 std::shared_ptr<class DeviceArrayInterface> m_impl;
231 };
232
233#ifndef NO_DOC
237 extern template class ZIVID_CORE_EXPORT_NON_WIN32 DeviceArray<SNR>;
248#endif
249
277 template<typename Format>
279 {
280 static_assert(
281 Detail::IsDeviceArrayFormat<Format>::value,
282 "Format must be one of: PointXYZ, PointXYZW, PointZ, SNR, NormalXYZ, "
283 "ColorRGBA, ColorRGBA_SRGB, ColorBGRA, ColorBGRA_SRGB, ColorRGBAf, ColorRGB, ColorRGB_SRGB, "
284 "ColorBGR, or ColorBGR_SRGB");
285
286 public:
288 DeviceArrayView() = default;
289
291 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
293
295 ~DeviceArrayView() = default;
296
298 DeviceArrayView(const DeviceArrayView &other) = default;
299
301 DeviceArrayView &operator=(const DeviceArrayView &other) = default;
302
304 DeviceArrayView(DeviceArrayView &&other) noexcept = default;
305
307 DeviceArrayView &operator=(DeviceArrayView &&other) noexcept = default;
308
310 void *devicePointer() const;
311
313 std::vector<int> shape() const;
314
316 std::vector<int> strides() const;
317
319 std::vector<int> stridesInBytes() const;
320
322 size_t size() const;
323
325 size_t sizeInBytes() const;
326
329
331 bool isValid() const;
332
334 bool isEmpty() const;
335
344 void copyToHost(Format *destination, size_t sizeInBytes, StreamOrQueue streamOrQueue) const;
345
354
363
364#ifndef NO_DOC
365 explicit DeviceArrayView(std::shared_ptr<class DeviceArrayInterface> impl);
366 const std::shared_ptr<class DeviceArrayInterface> &getImpl() const;
367#endif
368
369 private:
370 std::shared_ptr<class DeviceArrayInterface> m_impl;
371 };
372
373#ifndef NO_DOC
388#endif
389
390#ifndef NO_DOC
392 namespace Detail
393 {
394 template<typename ColorFormat>
395 struct SupportedImageDeviceArrayColorFormat : std::false_type
396 {};
397
398 template<>
399 struct SupportedImageDeviceArrayColorFormat<ColorRGBA> : std::true_type
400 {};
401
402 template<>
403 struct SupportedImageDeviceArrayColorFormat<ColorBGRA> : std::true_type
404 {};
405
406 template<>
407 struct SupportedImageDeviceArrayColorFormat<ColorRGBA_SRGB> : std::true_type
408 {};
409
410 template<>
411 struct SupportedImageDeviceArrayColorFormat<ColorBGRA_SRGB> : std::true_type
412 {};
413
414 template<>
415 struct SupportedImageDeviceArrayColorFormat<ColorRGBAf> : std::true_type
416 {};
417
418 template<>
419 struct SupportedImageDeviceArrayColorFormat<ColorRGB> : std::true_type
420 {};
421
422 template<>
423 struct SupportedImageDeviceArrayColorFormat<ColorRGB_SRGB> : std::true_type
424 {};
425
426 template<>
427 struct SupportedImageDeviceArrayColorFormat<ColorBGR> : std::true_type
428 {};
429
430 template<>
431 struct SupportedImageDeviceArrayColorFormat<ColorBGR_SRGB> : std::true_type
432 {};
433
434 template<typename Format>
435 struct SupportedDeviceArrayViewFormat : std::bool_constant<SupportedImageDeviceArrayColorFormat<Format>::value>
436 {};
437 } // namespace Detail
438#endif
439
452 template<typename ColorFormat, typename = std::enable_if_t<Detail::SupportedImageColorFormat<ColorFormat>::value>>
454 {
455 return Image<ColorFormat>{ buffer.toArray2D(streamOrQueue) };
456 }
457
470 template<typename ColorFormat, typename = std::enable_if_t<Detail::SupportedImageColorFormat<ColorFormat>::value>>
472 {
473 return Image<ColorFormat>{ buffer.toArray2D(streamOrQueue) };
474 }
475} // 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:279
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:110
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:453
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