HIP: Heterogenous-computing Interface for Portability
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hip_texture.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 //#pragma once
24 
25 #ifndef HIP_HCC_DETAIL_TEXTURE_H
26 #define HIP_HCC_DETAIL_TEXTURE_H
27 
33 #include <limits.h>
34 
35 //#include <hip/hcc_detail/hip_runtime.h>
36 
37 //----
38 //Texture - TODO - likely need to move this to a separate file only included with kernel compilation.
39 #define hipTextureType1D 1
40 
41 typedef enum {
42  hipChannelFormatKindSigned = 0,
43  hipChannelFormatKindUnsigned,
44  hipChannelFormatKindFloat,
45  hipChannelFormatKindNone
46 
47 } hipChannelFormatKind;
48 
49 typedef struct hipChannelFormatDesc {
50  int x;
51  int y;
52  int z;
53  int w;
54  hipChannelFormatKind f;
56 
57 typedef enum hipTextureReadMode
58 {
62 
64 {
68 
70  hipTextureFilterMode filterMode;
71  bool normalized;
72  hipChannelFormatDesc channelDesc;
73 };
74 #if __cplusplus
75 template <class T, int texType=hipTextureType1D, enum hipTextureReadMode=hipReadModeElementType>
76 struct texture : public textureReference {
77 
78  const T * _dataPtr; // pointer to underlying data.
79 
80  //texture() : filterMode(hipFilterModePoint), normalized(false), _dataPtr(NULL) {};
81  unsigned int width;
82  unsigned int height;
83 
84 };
85 #endif
86 
87 typedef struct {
88  unsigned int width;
89  unsigned int height;
90  hipChannelFormatKind f;
91  void* data; //FIXME: generalize this
92 } hipArray;
93 
94 
95 #define tex1Dfetch(_tex, _addr) (_tex._dataPtr[_addr])
96 
97 #define tex2D(_tex, _dx, _dy) \
98  _tex._dataPtr[(unsigned int)_dx + (unsigned int)_dy*(_tex.width)]
99 
113  size_t width, size_t height = 0, unsigned int flags = 0);
114 
124 
139 hipError_t hipMemcpy2D(void* dst, size_t dpitch, const void* src, size_t spitch, size_t width, size_t height, hipMemcpyKind kind);
140 
155 hipError_t hipMemcpy2DToArray(hipArray* dst, size_t wOffset, size_t hOffset, const void* src,
156  size_t spitch, size_t width, size_t height, hipMemcpyKind kind);
157 
172 hipError_t hipMemcpyToArray(hipArray* dst, size_t wOffset, size_t hOffset,
173  const void* src, size_t count, hipMemcpyKind kind);
174 
175 
183 // These are C++ APIs - maybe belong in separate file.
207 // C API:
208 #if 0
209 hipChannelFormatDesc hipBindTexture(size_t *offset, struct textureReference *tex, const void *devPtr, const struct hipChannelFormatDesc *desc, size_t size=UINT_MAX)
210 {
211  tex->_dataPtr = devPtr;
212 }
213 #endif
214 
226 hipChannelFormatDesc hipCreateChannelDesc(int x, int y, int z, int w, hipChannelFormatKind f);
227 
228 // descriptors
229 template <typename T> inline hipChannelFormatDesc hipCreateChannelDesc() {
230  return hipCreateChannelDesc(0, 0, 0, 0, hipChannelFormatKindNone);
231 }
232 template <> inline hipChannelFormatDesc hipCreateChannelDesc<int>() {
233  int e = (int)sizeof(int) * 8;
234  return hipCreateChannelDesc(e, 0, 0, 0, hipChannelFormatKindSigned);
235 }
236 template <> inline hipChannelFormatDesc hipCreateChannelDesc<unsigned int>() {
237  int e = (int)sizeof(unsigned int) * 8;
238  return hipCreateChannelDesc(e, 0, 0, 0, hipChannelFormatKindUnsigned);
239 }
240 template <> inline hipChannelFormatDesc hipCreateChannelDesc<long>() {
241  int e = (int)sizeof(long) * 8;
242  return hipCreateChannelDesc(e, 0, 0, 0, hipChannelFormatKindSigned);
243 }
244 template <> inline hipChannelFormatDesc hipCreateChannelDesc<unsigned long>() {
245  int e = (int)sizeof(unsigned long) * 8;
246  return hipCreateChannelDesc(e, 0, 0, 0, hipChannelFormatKindUnsigned);
247 }
248 template <> inline hipChannelFormatDesc hipCreateChannelDesc<float>() {
249  int e = (int)sizeof(float) * 8;
250  return hipCreateChannelDesc(e, 0, 0, 0, hipChannelFormatKindFloat);
251 }
252 
253 /*
254  * @brief hipBindTexture Binds size bytes of the memory area pointed to by @p devPtr to the texture reference tex.
255  *
256  * @p desc describes how the memory is interpreted when fetching values from the texture. The @p offset parameter is an optional byte offset as with the low-level
257  * hipBindTexture() function. Any memory previously bound to tex is unbound.
258  *
259  * @param[in] offset - Offset in bytes
260  * @param[out] tex - texture to bind
261  * @param[in] devPtr - Memory area on device
262  * @param[in] desc - Channel format
263  * @param[in] size - Size of the memory area pointed to by devPtr
264  * @return #hipSuccess, #hipErrorInvalidValue, #hipErrorMemoryFree, #hipErrorUnknown
265  **/
266 template <class T, int dim, enum hipTextureReadMode readMode>
267 hipError_t hipBindTexture(size_t *offset,
268  struct texture<T, dim, readMode> &tex,
269  const void *devPtr,
270  const struct hipChannelFormatDesc *desc,
271  size_t size=UINT_MAX)
272 {
273  tex._dataPtr = static_cast<const T*>(devPtr);
274 
275  return hipSuccess;
276 }
277 
278 /*
279  * @brief hipBindTexture Binds size bytes of the memory area pointed to by @p devPtr to the texture reference tex.
280  *
281  * @p desc describes how the memory is interpreted when fetching values from the texture. The @p offset parameter is an optional byte offset as with the low-level
282  * hipBindTexture() function. Any memory previously bound to tex is unbound.
283  *
284  * @param[in] offset - Offset in bytes
285  * @param[in] tex - texture to bind
286  * @param[in] devPtr - Memory area on device
287  * @param[in] size - Size of the memory area pointed to by devPtr
288  * @return #hipSuccess, #hipErrorInvalidValue, #hipErrorMemoryFree, #hipErrorUnknown
289  **/
290 template <class T, int dim, enum hipTextureReadMode readMode>
291 hipError_t hipBindTexture(size_t *offset,
292  struct texture<T, dim, readMode> &tex,
293  const void *devPtr,
294  size_t size=UINT_MAX)
295 {
296  return hipBindTexture(offset, tex, devPtr, &tex.channelDesc, size);
297 }
298 
299 template <class T, int dim, enum hipTextureReadMode readMode>
300 hipError_t hipBindTextureToArray(struct texture<T, dim, readMode> &tex, hipArray* array) {
301  tex.width = array->width;
302  tex.height = array->height;
303  tex._dataPtr = static_cast<const T*>(array->data);
304  return hipSuccess;
305 }
306 
307 /*
308  * @brief Unbinds the textuer bound to @p tex
309  *
310  * @param[in] tex - texture to unbind
311  *
312  * @return #hipSuccess
313  **/
314 template <class T, int dim, enum hipTextureReadMode readMode>
315 hipError_t hipUnbindTexture(struct texture<T, dim, readMode> &tex)
316 {
317  tex._dataPtr = NULL;
318 
319  return hipSuccess;
320 }
321 
322 
323 
324 // doxygen end Texture
330 // End doxygen API:
335 #endif
336 
hipError_t hipFreeArray(hipArray *array)
Frees an array on the device.
Definition: hip_memory.cpp:999
hipChannelFormatDesc hipCreateChannelDesc(int x, int y, int z, int w, hipChannelFormatKind f)
Returns a channel descriptor using the specified format.
Definition: hip_memory.cpp:276
Successful completion.
Definition: hip_runtime_api.h:153
hipError_t hipMemcpy2DToArray(hipArray *dst, size_t wOffset, size_t hOffset, const void *src, size_t spitch, size_t width, size_t height, hipMemcpyKind kind)
Copies data between host and device.
Definition: hip_memory.cpp:692
hipTextureFilterMode
Definition: hip_texture.h:63
Definition: hip_texture.h:59
Definition: hip_texture.h:69
Definition: hip_texture.h:87
hipError_t
Definition: hip_runtime_api.h:152
hipError_t hipMemcpyToArray(hipArray *dst, size_t wOffset, size_t hOffset, const void *src, size_t count, hipMemcpyKind kind)
Copies data between host and device.
Definition: hip_memory.cpp:745
hipMemcpyKind
Definition: hip_runtime_api.h:165
Definition: hip_texture.h:65
Definition: hip_texture.h:49
hipTextureReadMode
Definition: hip_texture.h:57
hipError_t hipMemcpy2D(void *dst, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height, hipMemcpyKind kind)
Copies data between host and device.
Definition: hip_memory.cpp:666
hipError_t hipMallocArray(hipArray **array, const hipChannelFormatDesc *desc, size_t width, size_t height=0, unsigned int flags=0)
Allocate an array on the device.
Definition: hip_memory.cpp:284