MUDA
Loading...
Searching...
No Matches
device_buffer_3d.inl
1#include <muda/buffer/buffer_launch.h>
2
3namespace muda
4{
5template <typename T>
6DeviceBuffer3D<T>::DeviceBuffer3D(const Extent3D& n)
7{
8 BufferLaunch()
9 .resize(*this, n) //
10 .wait();
11}
12
13template <typename T>
14DeviceBuffer3D<T>::DeviceBuffer3D()
15 : m_data(nullptr)
16 , m_pitch_bytes(0)
17 , m_pitch_bytes_area(0)
18 , m_extent(Extent3D::Zero())
19 , m_capacity(Extent3D::Zero())
20{
21}
22
23template <typename T>
24DeviceBuffer3D<T>::DeviceBuffer3D(const DeviceBuffer3D<T>& other)
25{
26 BufferLaunch()
27 .resize(*this, other.extent()) //
28 .copy(view(), other.view()) //
29 .wait();
30}
31
32template <typename T>
33DeviceBuffer3D<T>::DeviceBuffer3D(DeviceBuffer3D<T>&& other) MUDA_NOEXCEPT
34 : m_data(other.m_data),
35 m_pitch_bytes(other.m_pitch_bytes),
36 m_pitch_bytes_area(other.m_pitch_bytes_area),
37 m_extent(other.m_extent),
38 m_capacity(other.m_capacity)
39{
40 other.m_data = nullptr;
41 other.m_pitch_bytes = 0;
42 other.m_pitch_bytes_area = 0;
43 other.m_extent = Extent3D::Zero();
44 other.m_capacity = Extent3D::Zero();
45}
46
47template <typename T>
48DeviceBuffer3D<T>& DeviceBuffer3D<T>::operator=(const DeviceBuffer3D<T>& other)
49{
50 if(this == &other)
51 return *this;
52
53 BufferLaunch()
54 .resize(*this, other.extent()) //
55 .copy(view(), other.view()) //
56 .wait();
57
58 return *this;
59}
60
61template <typename T>
62DeviceBuffer3D<T>& DeviceBuffer3D<T>::operator=(DeviceBuffer3D<T>&& other)
63{
64 if(this == &other)
65 return *this;
66
67 if(m_data)
68 BufferLaunch().free(*this).wait();
69
70 m_data = other.m_data;
71 m_pitch_bytes = other.m_pitch_bytes;
72 m_pitch_bytes_area = other.m_pitch_bytes_area;
73 m_extent = other.m_extent;
74 m_capacity = other.m_capacity;
75
76 other.m_data = nullptr;
77 other.m_pitch_bytes = 0;
78 other.m_pitch_bytes_area = 0;
79 other.m_extent = Extent3D::Zero();
80 other.m_capacity = Extent3D::Zero();
81
82 return *this;
83}
84
85template <typename T>
86DeviceBuffer3D<T>::DeviceBuffer3D(CBuffer3DView<T> other)
87{
88 BufferLaunch()
89 .alloc(*this, other.extent()) //
90 .copy(view(), other) //
91 .wait();
92}
93
94template <typename T>
95DeviceBuffer3D<T>& DeviceBuffer3D<T>::operator=(CBuffer3DView<T> other)
96{
97 BufferLaunch()
98 .resize(*this, other.extent()) //
99 .copy(view(), other) //
100 .wait();
101
102 return *this;
103}
104
105template <typename T>
106void DeviceBuffer3D<T>::copy_to(std::vector<T>& host) const
107{
108 host.resize(total_size());
109 view().copy_to(host.data());
110}
111
112template <typename T>
113void DeviceBuffer3D<T>::copy_from(const std::vector<T>& host)
114{
115 MUDA_ASSERT(host.size() == total_size(),
116 "Need eqaul total size, host_size=%d, total_size=%d",
117 host.size(),
118 total_size());
119
120 view().copy_from(host.data());
121}
122
123template <typename T>
124void DeviceBuffer3D<T>::resize(Extent3D new_extent)
125{
126 BufferLaunch()
127 .resize(*this, new_extent) //
128 .wait();
129}
130
131template <typename T>
132void DeviceBuffer3D<T>::resize(Extent3D new_extent, const T& value)
133{
134 BufferLaunch()
135 .resize(*this, new_extent, value) //
136 .wait();
137}
138
139template <typename T>
140void DeviceBuffer3D<T>::reserve(Extent3D new_capacity)
141{
142 BufferLaunch()
143 .reserve(*this, new_capacity) //
144 .wait();
145}
146
147template <typename T>
148void DeviceBuffer3D<T>::clear()
149{
150 BufferLaunch().clear(*this).wait();
151}
152
153template <typename T>
154void DeviceBuffer3D<T>::shrink_to_fit()
155{
156 BufferLaunch().shrink_to_fit(*this).wait();
157}
158
159template <typename T>
160void DeviceBuffer3D<T>::fill(const T& v)
161{
162 BufferLaunch().fill(view(), v).wait();
163}
164
165template <typename T>
166DeviceBuffer3D<T>::~DeviceBuffer3D()
167{
168 if(m_data)
169 BufferLaunch().free(*this).wait();
170}
171} // namespace muda