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