muda
buffer_3d_view.h
1 #pragma once
2 #include <cuda.h>
3 #include <cuda_runtime.h>
4 #include <cuda_runtime_api.h>
5 #include <cinttypes>
6 #include <muda/type_traits/type_modifier.h>
7 #include <muda/viewer/dense/dense_3d.h>
8 #include <muda/tools/extent.h>
9 #include <muda/buffer/buffer_info_accessor.h>
10 #include <muda/view/view_base.h>
11 namespace muda
12 {
13 template <bool IsConst, typename T>
14 class Buffer3DViewBase : public ViewBase<IsConst>
15 {
16  using Base = ViewBase<IsConst>;
17  public:
18  static_assert(!std::is_const_v<T>, "Ty must be non-const");
23 
24  using CViewer = CDense3D<T>;
25  using Viewer = Dense3D<T>;
26  using ThisViewer = std::conditional_t<IsConst, CViewer, Viewer>;
27 
28  private:
29  friend class BufferLaunch;
30  friend class details::buffer::BufferInfoAccessor<ThisView>;
31 
32  template<typename U>
33  using auto_const_t = typename Base::template auto_const_t<U>;
34 
35  protected:
36  auto_const_t<T>* m_data = nullptr;
37  size_t m_pitch_bytes = ~0;
38  size_t m_pitch_bytes_area = ~0;
39  size_t m_origin_width = ~0;
40  size_t m_origin_height = ~0;
41 
42  Offset3D m_offset;
43  Extent3D m_extent;
44 
45  public:
46  MUDA_GENERIC Buffer3DViewBase() MUDA_NOEXCEPT = default;
47 
48  MUDA_GENERIC Buffer3DViewBase(auto_const_t<T>* data,
49  size_t pitch_bytes,
50  size_t pitch_bytes_area,
51  size_t origin_width,
52  size_t origin_height,
53  const Offset3D& offset,
54  const Extent3D& extent) MUDA_NOEXCEPT
55  : m_data(data),
56  m_pitch_bytes(pitch_bytes),
57  m_pitch_bytes_area(pitch_bytes_area),
58  m_origin_width(origin_width),
59  m_origin_height(origin_height),
60  m_offset(offset),
61  m_extent(extent)
62  {
63  }
64 
65  MUDA_GENERIC Buffer3DViewBase(T* data,
66  size_t pitch_bytes,
67  size_t pitch_bytes_area,
68  const Offset3D& offset,
69  const Extent3D& extent) MUDA_NOEXCEPT
71  data, pitch_bytes, pitch_bytes_area, extent.width(), extent.height(), offset, extent)
72  {
73  }
74 
75  // implicit conversion
76 
77  ConstView as_const() const MUDA_NOEXCEPT
78  {
79  return ConstView{m_data, m_pitch_bytes, m_pitch_bytes_area, m_offset, m_extent};
80  }
81 
82  operator ConstView() const MUDA_NOEXCEPT { return as_const(); }
83 
84  // non-const accessor
85  MUDA_GENERIC auto_const_t<T>* data(size_t x, size_t y, size_t z) MUDA_NOEXCEPT;
86  MUDA_GENERIC auto_const_t<T>* data(size_t flatten_i) MUDA_NOEXCEPT;
87  MUDA_GENERIC auto_const_t<T>* origin_data() MUDA_NOEXCEPT { return m_data; }
88  MUDA_GENERIC ThisView subview(Offset3D offset, Extent3D extent = {}) MUDA_NOEXCEPT;
89  MUDA_GENERIC ThisViewer viewer() MUDA_NOEXCEPT;
90 
91  // const accessor
92 
93  MUDA_GENERIC auto extent() const MUDA_NOEXCEPT { return m_extent; }
94  MUDA_GENERIC const T* data(size_t x, size_t y, size_t z) const MUDA_NOEXCEPT;
95  MUDA_GENERIC const T* data(size_t flatten_i) const MUDA_NOEXCEPT;
96  MUDA_GENERIC const T* origin_data() const MUDA_NOEXCEPT { return m_data; }
97  MUDA_GENERIC auto offset() const MUDA_NOEXCEPT { return m_offset; }
98  MUDA_GENERIC auto pitch_bytes() const MUDA_NOEXCEPT
99  {
100  return m_pitch_bytes;
101  }
102  MUDA_GENERIC auto pitch_bytes_area() const MUDA_NOEXCEPT
103  {
104  return m_pitch_bytes_area;
105  }
106  MUDA_GENERIC size_t total_size() const MUDA_NOEXCEPT;
107  MUDA_GENERIC ConstView subview(Offset3D offset, Extent3D extent = {}) const MUDA_NOEXCEPT;
108  MUDA_GENERIC CViewer cviewer() const MUDA_NOEXCEPT;
109  MUDA_GENERIC cudaPitchedPtr cuda_pitched_ptr() const MUDA_NOEXCEPT
110  {
111  return make_cudaPitchedPtr(remove_const(m_data),
112  remove_const(m_pitch_bytes),
113  m_origin_width * sizeof(T),
114  m_origin_height);
115  }
116 };
117 
118 template <typename T>
119 class CBuffer3DView : public Buffer3DViewBase<true, T>
120 {
122 
123  public:
124  using Base::Base;
125 
126  MUDA_GENERIC CBuffer3DView(const Base& base)
127  : Base(base)
128  {
129  }
130 
131  MUDA_GENERIC CBuffer3DView<T> subview(Offset3D offset, Extent3D extent = {}) const MUDA_NOEXCEPT
132  {
133  return CBuffer3DView<T>{Base::subview(offset, extent)};
134  }
135 
136  MUDA_HOST void copy_to(T* host) const;
137 
138  MUDA_GENERIC auto as_const() const MUDA_NOEXCEPT { return *this; }
139 };
140 
141 template <typename T>
142 class Buffer3DView : public Buffer3DViewBase<false, T>
143 {
145 
146  public:
147  using Base::Base;
148 
149  MUDA_GENERIC Buffer3DView(const Base& base)
150  : Base(base)
151  {
152  }
153 
154  MUDA_GENERIC Buffer3DView(const CBuffer3DView<T>&) = delete;
155 
156  MUDA_GENERIC CBuffer3DView<T> as_const() const MUDA_NOEXCEPT
157  {
158  return CBuffer3DView<T>{Base::as_const()};
159  }
160 
161  MUDA_GENERIC operator CBuffer3DView<T>() const MUDA_NOEXCEPT
162  {
163  return as_const();
164  }
165 
166  MUDA_HOST void fill(const T& v);
167  MUDA_HOST void copy_from(const Buffer3DView<T>& other);
168  MUDA_HOST void copy_from(const T* host);
169  MUDA_HOST void copy_to(T* host) const
170  {
171  CBuffer3DView<T>{*this}.copy_to(host);
172  }
173 };
174 
175 template <typename T>
177 {
178  using type = CBuffer3DView<T>;
179 };
180 
181 template <typename T>
183 {
184  using type = Buffer3DView<T>;
185 };
186 } // namespace muda
187 
188 #include "details/buffer_3d_view.inl"
Definition: extent.h:39
Definition: dense_3d.h:19
Definition: view_base.h:7
Definition: type_modifier.h:21
Definition: type_modifier.h:27
Definition: buffer_3d_view.h:119
Definition: buffer_info_accessor.h:6
Definition: buffer_3d_view.h:14
Definition: assert.h:13
Definition: buffer_3d_view.h:142
Definition: extent.h:102
Definition: buffer_launch.h:36