1#include <muda/compute_graph/compute_graph_builder.h>
2#include <muda/buffer/buffer_launch.h>
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)
19template <
bool IsConst,
typename T>
20MUDA_GENERIC Buffer3DViewT<IsConst, T>::Buffer3DViewT(auto_const_t<T>* data,
22 size_t pitch_bytes_area,
25 const Offset3D& offset,
26 const Extent3D& extent) MUDA_NOEXCEPT
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),
37template <
bool IsConst,
typename T>
38MUDA_GENERIC Buffer3DViewT<IsConst, T>::Buffer3DViewT(T* data,
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)
47template <
bool IsConst,
typename T>
48MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::as_const() const MUDA_NOEXCEPT->ConstView
50 return ConstView{*
this};
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>*
57 x += m_offset.offset_in_depth();
58 y += m_offset.offset_in_height();
59 z += m_offset.offset_in_width();
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;
66template <
bool IsConst,
typename T>
67MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::data(
size_t flatten_i)
const
68 MUDA_NOEXCEPT->auto_const_t<T>*
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();
79template <
bool IsConst,
typename T>
80MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::origin_data() const MUDA_NOEXCEPT->auto_const_t<T>*
85template <
bool IsConst,
typename T>
86MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::extent() const MUDA_NOEXCEPT->Extent3D
91template <
bool IsConst,
typename T>
92MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::offset() const MUDA_NOEXCEPT->Offset3D
97template <
bool IsConst,
typename T>
98MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::pitch_bytes() const MUDA_NOEXCEPT->
size_t
100 return m_pitch_bytes;
103template <
bool IsConst,
typename T>
104MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::pitch_bytes_area() const
105 MUDA_NOEXCEPT->
size_t
107 return m_pitch_bytes_area;
110template <
bool IsConst,
typename T>
111MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::cuda_pitched_ptr() const
112 MUDA_NOEXCEPT->cudaPitchedPtr
114 return make_cudaPitchedPtr(remove_const(m_data),
115 remove_const(m_pitch_bytes),
116 m_origin_width *
sizeof(T),
120template <
bool IsConst,
typename T>
121MUDA_GENERIC
size_t Buffer3DViewT<IsConst, T>::total_size() const MUDA_NOEXCEPT
123 return m_extent.width() * m_extent.height() * m_extent.depth();
126template <
bool IsConst,
typename T>
127MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::subview(
128 Offset3D offset, Extent3D extent)
const MUDA_NOEXCEPT->ThisView
131 if(ComputeGraphBuilder::is_topo_building())
136 extent = m_extent - as_extent(offset);
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(),
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};
149template <
bool IsConst,
typename T>
150MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::viewer() const MUDA_NOEXCEPT->ThisViewer
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()),
158 (
int)m_pitch_bytes_area};
161template <
bool IsConst,
typename T>
162MUDA_GENERIC
auto Buffer3DViewT<IsConst, T>::cviewer() const MUDA_NOEXCEPT->CViewer
167template <
bool IsConst,
typename T>
168MUDA_HOST
void Buffer3DViewT<IsConst, T>::fill(
const T& v)
const MUDA_REQUIRES(!IsConst)
170 static_assert(!IsConst,
"This must be non-const buffer.");
171 BufferLaunch().template fill<T>(*
this, v).wait();
174template <
bool IsConst,
typename T>
175MUDA_HOST
void Buffer3DViewT<IsConst, T>::copy_from(
const Buffer3DViewT<true, T>& other)
const
176 MUDA_REQUIRES(!IsConst)
178 static_assert(!IsConst,
"This must be non-const buffer.");
179 BufferLaunch().template copy<T>(*
this, other).wait();
182template <
bool IsConst,
typename T>
183MUDA_HOST
void Buffer3DViewT<IsConst, T>::copy_from(
const T* host)
const
184 MUDA_REQUIRES(!IsConst)
186 static_assert(!IsConst,
"This must be non-const buffer.");
187 BufferLaunch().template copy<T>(*
this, host).wait();
190template <
bool IsConst,
typename T>
191MUDA_HOST
void Buffer3DViewT<IsConst, T>::copy_to(T* host)
const
193 BufferLaunch().template copy<T>(host, *
this).wait();