MUDA
Loading...
Searching...
No Matches
buffer_3d_view.inl
1#include <muda/compute_graph/compute_graph_builder.h>
2#include <muda/buffer/buffer_launch.h>
3
4namespace muda
5{
6template <bool IsConst, typename T>
7template <bool OtherIsConst>
8MUDA_GENERIC Buffer3DViewT<IsConst, T>::Buffer3DViewT(const Buffer3DViewT<OtherIsConst, T>& other) MUDA_NOEXCEPT
9 : m_data(other.m_data),
10 m_pitch_bytes(other.m_pitch_bytes),
11 m_pitch_bytes_area(other.m_pitch_bytes_area),
12 m_origin_width(other.m_origin_width),
13 m_origin_height(other.m_origin_height),
14 m_offset(other.m_offset),
15 m_extent(other.m_extent)
16{
17}
18
19template <bool IsConst, typename T>
20MUDA_GENERIC Buffer3DViewT<IsConst, T>::Buffer3DViewT(auto_const_t<T>* data,
21 size_t pitch_bytes,
22 size_t pitch_bytes_area,
23 size_t origin_width,
24 size_t origin_height,
25 const Offset3D& offset,
26 const Extent3D& extent) MUDA_NOEXCEPT
27 : m_data(data),
28 m_pitch_bytes(pitch_bytes),
29 m_pitch_bytes_area(pitch_bytes_area),
30 m_origin_width(origin_width),
31 m_origin_height(origin_height),
32 m_offset(offset),
33 m_extent(extent)
34{
35}
36
37template <bool IsConst, typename T>
38MUDA_GENERIC Buffer3DViewT<IsConst, T>::Buffer3DViewT(T* data,
39 size_t pitch_bytes,
40 size_t pitch_bytes_area,
41 const Offset3D& offset,
42 const Extent3D& extent) MUDA_NOEXCEPT
43 : Buffer3DViewT(data, pitch_bytes, pitch_bytes_area, extent.width(), extent.height(), offset, extent)
44{
45}
46
47template <bool IsConst, typename T>
48MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::as_const() const MUDA_NOEXCEPT->ConstView
49{
50 return ConstView{*this};
51}
52
53template <bool IsConst, typename T>
54MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::data(size_t x, size_t y, size_t z) const
55 MUDA_NOEXCEPT->auto_const_t<T>*
56{
57 x += m_offset.offset_in_depth();
58 y += m_offset.offset_in_height();
59 z += m_offset.offset_in_width();
60 auto depth_begin =
61 reinterpret_cast<std::byte*>(remove_const(m_data)) + m_pitch_bytes_area * x;
62 auto height_begin = depth_begin + m_pitch_bytes * y;
63 return reinterpret_cast<T*>(height_begin) + z;
64}
65
66template <bool IsConst, typename T>
67MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::data(size_t flatten_i) const
68 MUDA_NOEXCEPT->auto_const_t<T>*
69{
70 auto area = m_extent.width() * m_extent.height();
71 auto x = flatten_i / area;
72 auto i_in_area = flatten_i % area;
73 auto y = i_in_area / m_extent.width();
74 auto i_in_width = i_in_area % m_extent.width();
75 auto z = i_in_width;
76 return data(x, y, z);
77}
78
79template <bool IsConst, typename T>
80MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::origin_data() const MUDA_NOEXCEPT->auto_const_t<T>*
81{
82 return m_data;
83}
84
85template <bool IsConst, typename T>
86MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::extent() const MUDA_NOEXCEPT->Extent3D
87{
88 return m_extent;
89}
90
91template <bool IsConst, typename T>
92MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::offset() const MUDA_NOEXCEPT->Offset3D
93{
94 return m_offset;
95}
96
97template <bool IsConst, typename T>
98MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::pitch_bytes() const MUDA_NOEXCEPT->size_t
99{
100 return m_pitch_bytes;
101}
102
103template <bool IsConst, typename T>
104MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::pitch_bytes_area() const
105 MUDA_NOEXCEPT->size_t
106{
107 return m_pitch_bytes_area;
108}
109
110template <bool IsConst, typename T>
111MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::cuda_pitched_ptr() const
112 MUDA_NOEXCEPT->cudaPitchedPtr
113{
114 return make_cudaPitchedPtr(remove_const(m_data),
115 remove_const(m_pitch_bytes),
116 m_origin_width * sizeof(T),
117 m_origin_height);
118}
119
120template <bool IsConst, typename T>
121MUDA_GENERIC size_t Buffer3DViewT<IsConst, T>::total_size() const MUDA_NOEXCEPT
122{
123 return m_extent.width() * m_extent.height() * m_extent.depth();
124}
125
126template <bool IsConst, typename T>
127MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::subview(
128 Offset3D offset, Extent3D extent) const MUDA_NOEXCEPT->ThisView
129{
130#ifndef __CUDA_ARCH__
131 if(ComputeGraphBuilder::is_topo_building())
132 return ThisView{}; // dummy
133#endif
134
135 if(!extent.valid())
136 extent = m_extent - as_extent(offset);
137
138 MUDA_KERNEL_ASSERT(extent + as_extent(offset) <= m_extent,
139 "Buffer3DView out of range, extent = (%d,%d,%d), yours = (%d,%d,%d)",
140 (int)m_extent.depth(),
141 (int)m_extent.height(),
142 (int)m_extent.width(),
143 (int)extent.depth(),
144 (int)extent.height(),
145 (int)extent.width());
146 return ThisView{m_data, m_pitch_bytes, m_pitch_bytes_area, m_origin_width, m_origin_height, offset, extent};
147}
148
149template <bool IsConst, typename T>
150MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::viewer() const MUDA_NOEXCEPT->ThisViewer
151{
152 return ThisViewer{m_data,
153 make_int3(m_offset.offset_in_depth(),
154 m_offset.offset_in_height(),
155 m_offset.offset_in_width()),
156 make_int3(m_extent.depth(), m_extent.height(), m_extent.width()),
157 (int)m_pitch_bytes,
158 (int)m_pitch_bytes_area};
159}
160
161template <bool IsConst, typename T>
162MUDA_GENERIC auto Buffer3DViewT<IsConst, T>::cviewer() const MUDA_NOEXCEPT->CViewer
163{
164 return viewer();
165}
166
167template <bool IsConst, typename T>
168MUDA_HOST void Buffer3DViewT<IsConst, T>::fill(const T& v) const MUDA_REQUIRES(!IsConst)
169{
170 static_assert(!IsConst, "This must be non-const buffer.");
171 BufferLaunch().template fill<T>(*this, v).wait();
172}
173
174template <bool IsConst, typename T>
175MUDA_HOST void Buffer3DViewT<IsConst, T>::copy_from(const Buffer3DViewT<true, T>& other) const
176 MUDA_REQUIRES(!IsConst)
177{
178 static_assert(!IsConst, "This must be non-const buffer.");
179 BufferLaunch().template copy<T>(*this, other).wait();
180}
181
182template <bool IsConst, typename T>
183MUDA_HOST void Buffer3DViewT<IsConst, T>::copy_from(const T* host) const
184 MUDA_REQUIRES(!IsConst)
185{
186 static_assert(!IsConst, "This must be non-const buffer.");
187 BufferLaunch().template copy<T>(*this, host).wait();
188}
189
190template <bool IsConst, typename T>
191MUDA_HOST void Buffer3DViewT<IsConst, T>::copy_to(T* host) const
192{
193 BufferLaunch().template copy<T>(host, *this).wait();
194}
195} // namespace muda