MUDA
Loading...
Searching...
No Matches
dense_3d.h
1#pragma once
2#include <muda/viewer/viewer_base.h>
3
4namespace muda
5{
6/*****************************************************************************************
7 *
8 * Dense2D (3D array)
9 * indexing (x,y,z)
10 * 1) non-pitched: x * dim_y * dimz + y * dim_z + z
11 * 2) pitched: x * dim_y * pitch + y * pitch + z
12 *
13 * Note:
14 * 1) z moves faster than y, y moves faster than x, which is the same as C/C++ 2d array
15 * 2) as for CUDA Memory3D, x index into depth, y index into height, z index into width
16 ****************************************************************************************/
17
18template <bool IsConst, typename T>
19class Dense3DBase : public ViewerBase<IsConst>
20{
22 MUDA_VIEWER_COMMON_NAME(Dense3DBase);
23
24 protected:
25 template <typename U>
26 using auto_const_t = typename Base::template auto_const_t<U>;
27
28 auto_const_t<T>* m_data;
29 int3 m_offset;
30 int3 m_dim;
31 int m_pitch_bytes;
32 int m_pitch_bytes_area;
33
34 public:
35 using value_type = T;
39
40 MUDA_GENERIC Dense3DBase() MUDA_NOEXCEPT : m_data(nullptr){};
41
42 MUDA_GENERIC Dense3DBase(auto_const_t<T>* p,
43 const int3& offset,
44 const int3& dim,
45 int pitch_bytes,
46 int pitch_bytes_area) MUDA_NOEXCEPT
47 : m_data(p),
48 m_offset(offset),
49 m_dim(dim),
50 m_pitch_bytes(pitch_bytes),
51 m_pitch_bytes_area(pitch_bytes_area)
52 {
53 }
54
55 MUDA_GENERIC auto as_const() const MUDA_NOEXCEPT
56 {
57 return ConstViewer{m_data, m_offset, m_dim, m_pitch_bytes, m_pitch_bytes_area};
58 }
59
60 MUDA_GENERIC operator ConstViewer() const MUDA_NOEXCEPT
61 {
62 return as_const();
63 }
64
65 MUDA_GENERIC auto_const_t<T>& operator()(int x, int y, int z) MUDA_NOEXCEPT
66 {
67 check();
68 check_range(x, y, z);
69 auto depth_begin = reinterpret_cast<std::byte*>(m_data) + x * m_pitch_bytes_area;
70 auto height_begin = depth_begin + y * m_pitch_bytes;
71 return *(reinterpret_cast<T*>(height_begin) + z);
72 }
73
74 MUDA_GENERIC auto_const_t<T>& operator()(const int3& xyz) MUDA_NOEXCEPT
75 {
76 return operator()(xyz.x, xyz.y, xyz.z);
77 }
78
79 MUDA_GENERIC auto_const_t<T>* data() MUDA_NOEXCEPT { return m_data; }
80
81
82 MUDA_GENERIC const T& operator()(int x, int y, int z) const MUDA_NOEXCEPT
83 {
84 return remove_const(*this)(x, y, z);
85 }
86
87
88 MUDA_GENERIC const T& operator()(const int3& xyz) const MUDA_NOEXCEPT
89 {
90 return remove_const(*this)(xyz.x, xyz.y, xyz.z);
91 }
92
93 MUDA_GENERIC auto_const_t<T>& flatten(int i) MUDA_NOEXCEPT
94 {
95 if constexpr(DEBUG_VIEWER)
96 {
97 MUDA_KERNEL_ASSERT(i >= 0 && i < total_size(),
98 "Dense3D[%s:%s]: out of range, index=%d, total_size=%d. %s(%d)",
99 this->name(),
100 this->kernel_name(),
101 i,
102 total_size(),
103 this->kernel_file(),
104 this->kernel_line());
105 }
106 auto area = m_dim.y * m_dim.z;
107 auto x = i / area;
108 auto i_in_area = i % area;
109 auto y = i_in_area / m_dim.z;
110 auto i_in_width = i_in_area % m_dim.z;
111 auto z = i_in_width;
112 return operator()(x, y, z);
113 }
114
115
116 MUDA_GENERIC const T& flatten(int i) const MUDA_NOEXCEPT
117 {
118 return remove_const(*this).flatten(i);
119 }
120
121 MUDA_GENERIC const T* data() const MUDA_NOEXCEPT { return m_data; }
122
123
124 MUDA_GENERIC auto dim() const MUDA_NOEXCEPT { return m_dim; }
125 MUDA_GENERIC int area() const MUDA_NOEXCEPT { return m_dim.y * m_dim.z; }
126 MUDA_GENERIC int volume() const MUDA_NOEXCEPT { return total_size(); }
127 MUDA_GENERIC int total_size() const MUDA_NOEXCEPT
128 {
129 return m_dim.x * area();
130 }
131 MUDA_GENERIC int pitch_bytes() const MUDA_NOEXCEPT { return m_pitch_bytes; }
132 MUDA_GENERIC int pitch_bytes_area() const MUDA_NOEXCEPT
133 {
134 return m_pitch_bytes_area;
135 }
136 MUDA_GENERIC int total_bytes() const MUDA_NOEXCEPT
137 {
138 return m_pitch_bytes_area * m_dim.x;
139 }
140
141 protected:
142 MUDA_INLINE MUDA_GENERIC void check_range(int x, int y, int z) const MUDA_NOEXCEPT
143 {
144 if constexpr(DEBUG_VIEWER)
145 {
146 if(!(x >= 0 && x < m_dim.x && y >= 0 && y < m_dim.y && z >= 0
147 && z < m_dim.z))
148 MUDA_KERNEL_ERROR("Dense3D[%s:%s]: out of range, index=(%d,%d,%d) dim=(%d,%d,%d). %s(%d)",
149 this->name(),
150 this->kernel_name(),
151 x,
152 y,
153 z,
154 m_dim.x,
155 m_dim.y,
156 m_dim.z,
157 this->kernel_file(),
158 this->kernel_line());
159 }
160 }
161
162 MUDA_INLINE MUDA_GENERIC void check() const MUDA_NOEXCEPT
163 {
164 if constexpr(DEBUG_VIEWER)
165 if(m_data == nullptr)
166 MUDA_KERNEL_ERROR("Dense3D[%s:%s]: data is null. %s(%d)",
167 this->name(),
168 this->kernel_name(),
169 this->kernel_file(),
170 this->kernel_line());
171 }
172};
173
174template <typename T>
176
177template <typename T>
179
180// viewer traits
181template <typename T>
183{
184 using type = CDense3D<T>;
185};
186
187template <typename T>
189{
190 using type = Dense3D<T>;
191};
192
193// make functions
194template <typename T>
195MUDA_INLINE MUDA_GENERIC auto make_cdense_3d(const T* data, const int3& dim) MUDA_NOEXCEPT
196{
197 auto pitch_bytes = dim.z * sizeof(T);
198 return CDense3D<T>{data,
199 make_int3(0, 0, 0),
200 dim,
201 static_cast<int>(pitch_bytes),
202 static_cast<int>(dim.y * pitch_bytes)};
203}
204
205template <typename T>
206MUDA_INLINE MUDA_GENERIC auto make_dense_3d(T* data, const int3& dim) MUDA_NOEXCEPT
207{
208 auto pitch_bytes = dim.z * sizeof(T);
209 return Dense3D<T>{data,
210 make_int3(0, 0, 0),
211 dim,
212 static_cast<int>(pitch_bytes),
213 static_cast<int>(dim.y * pitch_bytes)};
214}
215
216template <typename T>
217MUDA_INLINE MUDA_GENERIC auto make_cdense_3d(const T* data, int dimx, int dimy, int dimz) MUDA_NOEXCEPT
218{
219 return make_cdense_3d(data, make_int3(dimx, dimy, dimz));
220}
221
222template <typename T>
223MUDA_INLINE MUDA_GENERIC auto make_dense_3d(T* data, int dimx, int dimy, int dimz) MUDA_NOEXCEPT
224{
225 return make_dense_3d(data, make_int3(dimx, dimy, dimz));
226}
227} // namespace muda
Definition dense_3d.h:20
Definition viewer_base.h:18
Definition type_modifier.h:22
Definition type_modifier.h:28