cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
texture_view.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 #ifndef CUDA_API_WRAPPERS_TEXTURE_VIEW_HPP
10 #define CUDA_API_WRAPPERS_TEXTURE_VIEW_HPP
11 
12 #include "array.hpp"
13 #include "error.hpp"
14 #include "memory.hpp"
15 
16 namespace cuda {
17 
19 class texture_view;
21 
22 namespace texture {
23 
25 using raw_handle_t = CUtexObject;
26 
35 struct descriptor_t : public CUDA_TEXTURE_DESC {
36  inline descriptor_t()
37  {
38  using parent = CUDA_TEXTURE_DESC;
39  memset(static_cast<parent*>(this), 0, sizeof(parent));
40  // Note: This should set the fields directly listed in the CUDA Runtime API
41  // version of this structure to 0.
42  this->addressMode[0] = CU_TR_ADDRESS_MODE_BORDER;
43  this->addressMode[1] = CU_TR_ADDRESS_MODE_BORDER;
44  this->addressMode[2] = CU_TR_ADDRESS_MODE_BORDER;
45  this->filterMode = CU_TR_FILTER_MODE_POINT;
46  }
47 };
48 
63 inline texture_view wrap(
64  device::id_t device_id,
65  context::handle_t context_handle,
66  texture::raw_handle_t handle,
67  bool take_ownership) noexcept;
68 
69 } // namespace texture
70 
89 class texture_view {
90  using raw_handle_type = texture::raw_handle_t;
91  using scoped_context_setter = cuda::context::current::detail_::scoped_override_t;
92 
93 public:
96  device::id_t device_id() const noexcept { return device_id_; }
97  context::handle_t context_handle() const noexcept { return context_handle_; }
98  raw_handle_type raw_handle() const noexcept { return raw_view_handle; }
99  bool is_owning() const noexcept { return owning_; }
101 
102 public: // constructors and destructors
103 
104  texture_view(const texture_view& other) = delete;
105 
106  texture_view(texture_view&& other) noexcept :
107  device_id_(other.device_id_),
108  context_handle_(other.context_handle_),
109  raw_view_handle(other.raw_view_handle),
110  owning_(other.raw_view_handle)
111  {
112  other.owning_ = false;
113  };
114 
115  template <typename T, dimensionality_t NumDimensions>
116  texture_view(
119  device_id_(arr.device_id()),
120  context_handle_(arr.context_handle()),
121  owning_(true)
122  {
123  scoped_context_setter set_context(context_handle_);
124  CUDA_RESOURCE_DESC resource_descriptor;
125  memset(&resource_descriptor, 0, sizeof(resource_descriptor));
126  resource_descriptor.resType = CU_RESOURCE_TYPE_ARRAY;
127  resource_descriptor.res.array.hArray = arr.get();
128 
129  auto status = cuTexObjectCreate(&raw_view_handle, &resource_descriptor, &descriptor, nullptr);
130  throw_if_error_lazy(status, "failed creating a CUDA texture object");
131  }
132 
133 public: // operators
134 
135  ~texture_view() noexcept(false)
136  {
137  if (owning_) {
138  scoped_context_setter set_context(context_handle_);
139  auto status = cuTexObjectDestroy(raw_view_handle);
140  throw_if_error_lazy(status, "failed destroying texture object");
141  }
142  }
143 
144  texture_view& operator=(const texture_view& other) = delete;
145  texture_view& operator=(texture_view& other) = delete;
146 
147 protected: // constructor
148 
149  // Usable by the wrap function
150  texture_view(
151  device::id_t device_id,
152  context::handle_t context_handle,
153  raw_handle_type handle ,
154  bool take_ownership) noexcept
155  :
156  device_id_(device_id),
157  context_handle_(context_handle),
158  raw_view_handle(handle),
159  owning_(take_ownership) { }
160 
161 public: // non-mutating getters
162 
164  context_t context() const;
165 
167  device_t device() const;
168 
169 public: // friendship
170 
171  friend texture_view texture::wrap(device::id_t, context::handle_t, raw_handle_type, bool) noexcept;
172 
173 protected:
174  device::id_t device_id_;
175  context::handle_t context_handle_;
176  raw_handle_type raw_view_handle;
177  bool owning_;
178 };
179 
181 inline bool operator==(const texture_view& lhs, const texture_view& rhs) noexcept
182 {
183  return lhs.raw_handle() == rhs.raw_handle();
184 }
185 
186 inline bool operator!=(const texture_view& lhs, const texture_view& rhs) noexcept
187 {
188  return lhs.raw_handle() != rhs.raw_handle();
189 }
191 namespace texture {
192 
193 inline texture_view wrap(
194  device::id_t device_id,
195  context::handle_t context_handle,
196  texture::raw_handle_t handle,
197  bool take_ownership) noexcept
198 {
199  return { device_id, context_handle, handle, take_ownership };
200 }
201 
202 } // namespace texture
203 
204 } // namespace cuda
205 
206 #endif // CUDA_API_WRAPPERS_TEXTURE_VIEW_HPP
device::id_t device_id() const noexcept
Getters for this object&#39;s raw fields.
Definition: texture_view.hpp:96
A simplifying rudimentary wrapper wrapper for the CUDA runtime API&#39;s internal "texture descriptor" ob...
Definition: texture_view.hpp:35
Wrapper class for a CUDA context.
Definition: context.hpp:244
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
CUcontext handle_t
Raw CUDA driver handle for a context; see {context_t}.
Definition: types.hpp:878
Owning wrapper for CUDA 2D and 3D arrays.
Definition: array.hpp:29
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:850
Use texture memory for optimized read only cache access.
Definition: texture_view.hpp:89
Contains a proxy class for CUDA arrays - GPU memory with 2-D or 3-D locality and hardware support for...
#define throw_if_error_lazy(status__,...)
A macro for only throwing an error if we&#39;ve failed - which also ensures no string is constructed unle...
Definition: error.hpp:316
bool operator==(const context_t &lhs, const context_t &rhs) noexcept
Definition: context.hpp:762
Facilities for exception-based handling of Runtime and Driver API errors, including a basic exception...
CUtexObject raw_handle_t
The CUDA driver&#39;s raw, opaque handle for texture objects.
Definition: texture_view.hpp:25
Wrapper class for a CUDA device.
Definition: device.hpp:135
freestanding wrapper functions for working with CUDA&#39;s various kinds of memory spaces, arranged into a relevant namespace hierarchy.