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 
18 class texture_view;
19 
20 namespace texture {
21 
22 using raw_handle_t = CUtexObject;
23 
32 struct descriptor_t : public CUDA_TEXTURE_DESC {
33  inline descriptor_t()
34  {
35  using parent = CUDA_TEXTURE_DESC;
36  memset(static_cast<parent*>(this), 0, sizeof(parent));
37  // Note: This should set the fields directly listed in the CUDA Runtime API
38  // version of this structure to 0.
39  this->addressMode[0] = CU_TR_ADDRESS_MODE_BORDER;
40  this->addressMode[1] = CU_TR_ADDRESS_MODE_BORDER;
41  this->addressMode[2] = CU_TR_ADDRESS_MODE_BORDER;
42  this->filterMode = CU_TR_FILTER_MODE_POINT;
43  }
44 };
45 
61 inline texture_view wrap(
62  context::handle_t context_handle_,
63  texture::raw_handle_t handle,
64  bool take_ownership) noexcept;
65 
66 } // namespace texture
67 
86 class texture_view {
87  using raw_handle_type = texture::raw_handle_t;
88  using scoped_context_setter = cuda::context::current::detail_::scoped_override_t;
89 
90 public:
91  bool is_owning() const noexcept { return owning; }
92  raw_handle_type raw_handle() const noexcept { return raw_view_handle; }
93 
94 public: // constructors and destructors
95 
96  texture_view(const texture_view& other) = delete;
97 
98  texture_view(texture_view&& other) noexcept :
99  raw_view_handle(other.raw_view_handle), owning(other.raw_view_handle)
100  {
101  other.owning = false;
102  };
103 
104 
105  template <typename T, dimensionality_t NumDimensions>
106  texture_view(
109  context_handle_(arr.context_handle()), owning(true)
110  {
111  scoped_context_setter set_context(context_handle_);
112  CUDA_RESOURCE_DESC resource_descriptor;
113  memset(&resource_descriptor, 0, sizeof(resource_descriptor));
114  resource_descriptor.resType = CU_RESOURCE_TYPE_ARRAY;
115  resource_descriptor.res.array.hArray = arr.get();
116 
117  auto status = cuTexObjectCreate(&raw_view_handle, &resource_descriptor, &descriptor, nullptr);
118  throw_if_error_lazy(status, "failed creating a CUDA texture object");
119  }
120 
121 public: // operators
122 
123  ~texture_view() noexcept(false)
124  {
125  if (owning) {
126  scoped_context_setter set_context(context_handle_);
127  auto status = cuTexObjectDestroy(raw_view_handle);
128  throw_if_error_lazy(status, "failed destroying texture object");
129  }
130  }
131 
132  texture_view& operator=(const texture_view& other) = delete;
133  texture_view& operator=(texture_view& other) = delete;
134 
135 protected: // constructor
136 
137  // Usable by the wrap function
138  texture_view(context::handle_t context_handle, raw_handle_type handle , bool take_ownership) noexcept
139  : context_handle_(context_handle), raw_view_handle(handle), owning(take_ownership) { }
140 
141 public: // non-mutating getters
142 
143  context_t context() const;
144  device_t device() const;
145 
146 public: // friendship
147 
148  friend texture_view texture::wrap(context::handle_t, raw_handle_type, bool) noexcept;
149 
150 protected:
151  context::handle_t context_handle_ { } ;
152  raw_handle_type raw_view_handle { } ;
153  bool owning;
154 };
155 
156 
157 inline bool operator==(const texture_view& lhs, const texture_view& rhs) noexcept
158 {
159  return lhs.raw_handle() == rhs.raw_handle();
160 }
161 
162 inline bool operator!=(const texture_view& lhs, const texture_view& rhs) noexcept
163 {
164  return lhs.raw_handle() != rhs.raw_handle();
165 }
166 
167 namespace texture {
168 
169 inline texture_view wrap(
170  context::handle_t context_handle_,
171  texture::raw_handle_t handle,
172  bool take_ownership) noexcept
173 {
174  return { context_handle_, handle, take_ownership };
175 }
176 
177 } // namespace texture
178 
179 } // namespace cuda
180 
181 #endif // CUDA_API_WRAPPERS_TEXTURE_VIEW_HPP
A simplifying rudimentary wrapper wrapper for the CUDA runtime API&#39;s internal "texture descriptor" ob...
Definition: texture_view.hpp:32
Wrapper class for a CUDA context.
Definition: context.hpp:220
All definitions and functionality wrapping the CUDA Runtime API.
Definition: array.hpp:22
Owning wrapper for CUDA 2D and 3D arrays.
Definition: array.hpp:27
Use texture memory for optimized read only cache access.
Definition: texture_view.hpp:86
Contains a proxy class for CUDA arrays - GPU memory with 2-D or 3-D locality and hardware support for...
Facilities for exception-based handling of Runtime and Driver API errors, including a basic exception...
freestanding wrapper functions for working with CUDA&#39;s various kinds of memory spaces, arranged into a relevant namespace hierarchy.