NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tex.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
32 #pragma once
33 
34 #include <nvbio/basic/types.h>
35 #include <nvbio/basic/cuda/arch.h>
36 
37 namespace nvbio {
38 namespace cuda {
39 
40 #ifdef __CUDACC__
41 
42 #define DECL_TEXTURE_WRAPPER_CLASS(NAME, TYPE) \
43  texture<TYPE> NAME ## _tex; \
44 struct NAME ## _texture \
45 { \
46  typedef TYPE value_type; \
47  typedef value_type reference; \
48  typedef const value_type* pointer; \
49  typedef uint32 difference_type; \
50  typedef std::random_access_iterator_tag iterator_category; \
51 \
52  NVBIO_FORCEINLINE NVBIO_DEVICE TYPE operator[] (const uint32 i) const; \
53 \
54  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* NAME, uint32 size); \
55  \
56  static NVBIO_FORCEINLINE NVBIO_HOST void unbind(); \
57 }; \
58 
59 #define INST_TEXTURE_WRAPPER_CLASS(NAME, TYPE) \
60 NVBIO_FORCEINLINE NVBIO_DEVICE TYPE NAME ## _texture::operator[] (const uint32 i) const \
61 { \
62  return tex1Dfetch( NAME ## _tex, i ); \
63 } \
64 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture::bind(const uint32* ptr, uint32 size) \
65 { \
66  if (ptr == NULL) \
67  return; \
68 \
69  cudaChannelFormatDesc channel_desc = cudaCreateChannelDesc<TYPE>(); \
70  NAME ## _tex.normalized = false; \
71  NAME ## _tex.filterMode = cudaFilterModePoint; \
72  cudaBindTexture( 0, &NAME ## _tex, ptr, &channel_desc, size*sizeof(uint32) ); \
73  nvbio::cuda::check_error("NAME ## texture::bind"); \
74 } \
75 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture::unbind() \
76 { \
77  cudaUnbindTexture( &NAME ## _tex ); \
78 } \
79 
80 
81 #define DECL_TEXTURE_SELECTOR_WRAPPER_CLASS(NAME, TYPE) \
82  texture<TYPE> NAME ## _tex1; \
83  texture<TYPE> NAME ## _tex2; \
84 struct NAME ## _texture_selector \
85 { \
86  typedef TYPE value_type; \
87  typedef value_type reference; \
88  typedef const value_type* pointer; \
89  typedef uint32 difference_type; \
90  typedef std::random_access_iterator_tag iterator_category; \
91 \
92  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE NAME ## _texture_selector(const uint32 sel) : m_sel(sel) {} \
93 \
94  NVBIO_FORCEINLINE NVBIO_DEVICE TYPE operator[] (const uint32 i) const; \
95 \
96  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* ptr1, const uint32* ptr2, uint32 size); \
97 \
98  static NVBIO_FORCEINLINE NVBIO_HOST void unbind(); \
99 \
100  uint32 m_sel; \
101 }; \
102 
103 #define INST_TEXTURE_SELECTOR_WRAPPER_CLASS(NAME, TYPE) \
104 NVBIO_FORCEINLINE NVBIO_DEVICE TYPE NAME ## _texture_selector::operator[] (const uint32 i) const \
105 { \
106  if (m_sel) \
107  return tex1Dfetch( NAME ## _tex2, i ); \
108  else \
109  return tex1Dfetch( NAME ## _tex1, i ); \
110 } \
111 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture_selector::bind(const uint32* ptr1, const uint32* ptr2, uint32 size) \
112 { \
113  cudaChannelFormatDesc channel_desc = cudaCreateChannelDesc<TYPE>(); \
114  NAME ## _tex1.normalized = false; \
115  NAME ## _tex1.filterMode = cudaFilterModePoint; \
116  NAME ## _tex2.normalized = false; \
117  NAME ## _tex2.filterMode = cudaFilterModePoint; \
118  if (ptr1) cudaBindTexture( 0, &NAME ## _tex1, ptr1, &channel_desc, size*sizeof(uint32) ); \
119  if (ptr2) cudaBindTexture( 0, &NAME ## _tex2, ptr2, &channel_desc, size*sizeof(uint32) ); \
120  nvbio::cuda::check_error("NAME ## texture_selector::bind"); \
121 } \
122 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture_selector::unbind() \
123 { \
124  cudaUnbindTexture( &NAME ## _tex1 ); \
125  cudaUnbindTexture( &NAME ## _tex2 ); \
126 } \
127 
128 
129 #define TEXTURE_WRAPPER_LOG_WIDTH 14
130 #define TEXTURE_WRAPPER_WIDTH 16384
131 
132 #define DECL_TEXTURE_WRAPPER_CLASS_2D(NAME, TYPE) \
133  texture<TYPE,2> NAME ## _tex_2d; \
134 struct NAME ## _texture_2d \
135 { \
136  typedef TYPE value_type; \
137  typedef value_type reference; \
138  typedef const value_type* pointer; \
139  typedef uint32 difference_type; \
140  typedef std::random_access_iterator_tag iterator_category; \
141 \
142  NVBIO_FORCEINLINE NVBIO_DEVICE TYPE operator[] (const uint32 i) const; \
143 \
144  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* NAME, uint32 size); \
145  \
146  static NVBIO_FORCEINLINE NVBIO_HOST void unbind(); \
147 }; \
148 
149 #define INST_TEXTURE_WRAPPER_CLASS_2D(NAME, TYPE) \
150 NVBIO_FORCEINLINE NVBIO_DEVICE TYPE NAME ## _texture_2d::operator[] (const uint32 i) const \
151 { \
152  return tex2D( NAME ## _tex_2d, i&(TEXTURE_WRAPPER_WIDTH-1u), i >> TEXTURE_WRAPPER_LOG_WIDTH ); \
153 } \
154 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture_2d::bind(const uint32* ptr, uint32 size) \
155 { \
156  if (ptr == NULL) \
157  return; \
158 \
159  cudaChannelFormatDesc channel_desc = cudaCreateChannelDesc<TYPE>(); \
160  NAME ## _tex_2d.normalized = false; \
161  NAME ## _tex_2d.filterMode = cudaFilterModePoint; \
162 \
163  const uint32 comps = sizeof(TYPE)/sizeof(uint32); \
164  const uint32 w = TEXTURE_WRAPPER_WIDTH; \
165  const uint32 h = (size/comps + w-1) / w; \
166 \
167  cudaBindTexture2D( 0, &NAME ## _tex_2d, ptr, &channel_desc, w, h, w*sizeof(TYPE) ); \
168  nvbio::cuda::check_error("NAME ## texture_2d::bind"); \
169 } \
170 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture_2d::unbind() \
171 { \
172  cudaUnbindTexture( &NAME ## _tex_2d ); \
173 } \
174 
175 #define DECL_TEXTURE_SELECTOR_WRAPPER_CLASS_2D(NAME, TYPE) \
176  texture<TYPE,2> NAME ## _tex1_2d; \
177  texture<TYPE,2> NAME ## _tex2_2d; \
178 struct NAME ## _texture_selector_2d \
179 { \
180  typedef TYPE value_type; \
181  typedef value_type reference; \
182  typedef const value_type* pointer; \
183  typedef uint32 difference_type; \
184  typedef std::random_access_iterator_tag iterator_category; \
185 \
186  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE NAME ## _texture_selector_2d(const uint32 sel) : m_sel(sel) {} \
187 \
188  NVBIO_FORCEINLINE NVBIO_DEVICE TYPE operator[] (const uint32 i) const; \
189 \
190  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* ptr1, const uint32* ptr2, uint32 size); \
191 \
192  static NVBIO_FORCEINLINE NVBIO_HOST void unbind(); \
193 \
194  uint32 m_sel; \
195 }; \
196 
197 #define INST_TEXTURE_SELECTOR_WRAPPER_CLASS_2D(NAME, TYPE) \
198 NVBIO_FORCEINLINE NVBIO_DEVICE TYPE NAME ## _texture_selector_2d::operator[] (const uint32 i) const \
199 { \
200  if (m_sel) \
201  return tex2D( NAME ## _tex2_2d, i&(TEXTURE_WRAPPER_WIDTH-1u), i >> TEXTURE_WRAPPER_LOG_WIDTH ); \
202  else \
203  return tex2D( NAME ## _tex1_2d, i&(TEXTURE_WRAPPER_WIDTH-1u), i >> TEXTURE_WRAPPER_LOG_WIDTH ); \
204 } \
205 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture_selector_2d::bind(const uint32* ptr1, const uint32* ptr2, uint32 size) \
206 { \
207  cudaChannelFormatDesc channel_desc = cudaCreateChannelDesc<TYPE>(); \
208  NAME ## _tex1_2d.normalized = false; \
209  NAME ## _tex1_2d.filterMode = cudaFilterModePoint; \
210  NAME ## _tex2_2d.normalized = false; \
211  NAME ## _tex2_2d.filterMode = cudaFilterModePoint; \
212 \
213  const uint32 comps = sizeof(TYPE)/sizeof(uint32); \
214  const uint32 w = TEXTURE_WRAPPER_WIDTH; \
215  const uint32 h = (size/comps + w-1) / w; \
216 \
217  if (ptr1) cudaBindTexture2D( 0, &NAME ## _tex1_2d, ptr1, &channel_desc, w, h, w*sizeof(TYPE) ); \
218  if (ptr2) cudaBindTexture2D( 0, &NAME ## _tex2_2d, ptr2, &channel_desc, w, h, w*sizeof(TYPE) ); \
219  nvbio::cuda::check_error("NAME ## texture_selector_2d::bind"); \
220 } \
221 NVBIO_FORCEINLINE NVBIO_HOST void NAME ## _texture_selector_2d::unbind() \
222 { \
223  cudaUnbindTexture( &NAME ## _tex1_2d ); \
224  cudaUnbindTexture( &NAME ## _tex2_2d ); \
225 } \
226 
227 #else
228 
229 #define DECL_TEXTURE_WRAPPER_CLASS(NAME, TYPE) \
230 struct NAME ## _texture \
231 { \
232  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* NAME, uint32 size) {} \
233 \
234  static NVBIO_FORCEINLINE NVBIO_HOST void unbind() {} \
235 };
236 
237 #define INST_TEXTURE_WRAPPER_CLASS(NAME, TYPE)
238 
239 #define DECL_TEXTURE_SELECTOR_WRAPPER_CLASS(NAME, TYPE) \
240 struct NAME ## _texture_selector \
241 { \
242  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* NAME, uint32 size) {} \
243 \
244  static NVBIO_FORCEINLINE NVBIO_HOST void unbind() {} \
245 };
246 
247 #define INST_TEXTURE_SELECTOR_WRAPPER_CLASS(NAME, TYPE)
248 
249 #define DECL_TEXTURE_WRAPPER_CLASS_2D(NAME, TYPE) \
250 struct NAME ## _texture_2d \
251 { \
252  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* NAME, uint32 size) {} \
253 \
254  static NVBIO_FORCEINLINE NVBIO_HOST void unbind() {} \
255 };
256 
257 #define INST_TEXTURE_WRAPPER_CLASS_2D(NAME, TYPE)
258 
259 #define DECL_TEXTURE_SELECTOR_WRAPPER_CLASS_2D(NAME, TYPE) \
260 struct NAME ## _texture_selector_2d \
261 { \
262  static NVBIO_FORCEINLINE NVBIO_HOST void bind(const uint32* NAME, uint32 size) {} \
263 \
264  static NVBIO_FORCEINLINE NVBIO_HOST void unbind() {} \
265 };
266 
267 #define INST_TEXTURE_SELECTOR_WRAPPER_CLASS_2D(NAME, TYPE)
268 
269 #endif
270 
271 } // namespace cuda
272 } // namespace nvbio