1#include <muda/compute_graph/compute_graph_builder.h>
2#include <muda/buffer/buffer_launch.h>
6template <
bool IsConst,
typename T>
7MUDA_GENERIC Buffer2DViewT<IsConst, T>::Buffer2DViewT(auto_const_t<T>* data,
11 const Offset2D& offset,
12 const Extent2D& extent) MUDA_NOEXCEPT
14 m_pitch_bytes(pitch_bytes),
15 m_origin_width(origin_width),
16 m_origin_height(origin_height),
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)
35template <
bool IsConst,
typename T>
36MUDA_GENERIC Buffer2DViewT<IsConst, T>::Buffer2DViewT(auto_const_t<T>* data,
38 const Offset2D& offset,
39 const Extent2D& extent) MUDA_NOEXCEPT
40 : Buffer2DViewT(data, pitch_bytes, extent.width(), extent.height(), offset, extent)
44template <
bool IsConst,
typename T>
45MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::as_const() const MUDA_NOEXCEPT->ConstView
47 return ConstView{*
this};
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>*
54 x += m_offset.offset_in_height();
55 y += m_offset.offset_in_width();
58 reinterpret_cast<std::byte*
>(remove_const(m_data)) + m_pitch_bytes * x;
59 return reinterpret_cast<auto_const_t<T>*
>(height_begin) + y;
62template <
bool IsConst,
typename T>
63MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::data(
size_t flatten_i)
const
64 MUDA_NOEXCEPT->auto_const_t<T>*
66 auto x = flatten_i / m_extent.width();
67 auto y = flatten_i % m_extent.width();
71template <
bool IsConst,
typename T>
72MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::origin_data() const MUDA_NOEXCEPT->auto_const_t<T>*
78template <
bool IsConst,
typename T>
79MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::subview(
80 Offset2D offset, Extent2D extent)
const MUDA_NOEXCEPT->ThisView
83 if(ComputeGraphBuilder::is_topo_building())
88 extent = m_extent - as_extent(offset);
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(),
96 return ThisView{m_data, m_pitch_bytes, m_origin_width, m_origin_height, offset, extent};
99template <
bool IsConst,
typename T>
100MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::viewer() const MUDA_NOEXCEPT->ThisViewer
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()),
109template <
bool IsConst,
typename T>
110MUDA_GENERIC cudaPitchedPtr Buffer2DViewT<IsConst, T>::cuda_pitched_ptr() const MUDA_NOEXCEPT
112 return make_cudaPitchedPtr(remove_const(m_data),
113 remove_const(m_pitch_bytes),
114 m_origin_width *
sizeof(T),
118template <
bool IsConst,
typename T>
119MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::extent() const MUDA_NOEXCEPT->Extent2D
124template <
bool IsConst,
typename T>
125MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::pitch_bytes() const MUDA_NOEXCEPT->
size_t
127 return m_pitch_bytes;
130template <
bool IsConst,
typename T>
131MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::offset() const MUDA_NOEXCEPT->Offset2D
135template <
bool IsConst,
typename T>
136MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::total_size() const MUDA_NOEXCEPT->
size_t
138 return m_extent.width() * m_extent.height();
141template <
bool IsConst,
typename T>
142MUDA_GENERIC
auto Buffer2DViewT<IsConst, T>::cviewer() const MUDA_NOEXCEPT->CViewer
147template <
bool IsConst,
typename T>
148MUDA_HOST
void Buffer2DViewT<IsConst, T>::copy_to(T* host)
const
150 BufferLaunch().template copy<T>(host, *
this).wait();
153template <
bool IsConst,
typename T>
154MUDA_HOST
void Buffer2DViewT<IsConst, T>::fill(
const T& val) MUDA_REQUIRES(!IsConst)
156 BufferLaunch().template fill(*
this, val).wait();
159template <
bool IsConst,
typename T>
160MUDA_HOST
void Buffer2DViewT<IsConst, T>::copy_from(
const Buffer2DViewT<true, T>& other)
161 MUDA_REQUIRES(!IsConst)
163 BufferLaunch().template copy<T>(*
this, other).wait();
166template <
bool IsConst,
typename T>
167MUDA_HOST
void Buffer2DViewT<IsConst, T>::copy_from(
const T* host) MUDA_REQUIRES(!IsConst)
169 BufferLaunch().template copy<T>(*
this, host).wait();