Jetson Inference
DNN Vision Library
cudaVector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is 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
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __CUDA_VECTOR_TEMPLATES_H__
24 #define __CUDA_VECTOR_TEMPLATES_H__
25 
26 
27 // vector overloads
28 #include "cudaMath.h"
29 
30 // static assertion
31 #include <type_traits>
32 
33 
39 
41 
42 // get base type (uint8 or float) from vector
43 template<class T> struct cudaVectorTypeInfo;
44 
45 template<> struct cudaVectorTypeInfo<uchar> { typedef uint8_t Base; };
46 template<> struct cudaVectorTypeInfo<uchar3> { typedef uint8_t Base; };
47 template<> struct cudaVectorTypeInfo<uchar4> { typedef uint8_t Base; };
48 
49 template<> struct cudaVectorTypeInfo<float> { typedef float Base; };
50 template<> struct cudaVectorTypeInfo<float3> { typedef float Base; };
51 template<> struct cudaVectorTypeInfo<float4> { typedef float Base; };
52 
53 
54 // static compile-time assertion
55 template<typename T> struct cuda_assert_false : std::false_type { };
56 
57 
58 // make_vec<T> templates
59 template<typename T> inline __host__ __device__ T make_vec( typename cudaVectorTypeInfo<T>::Base x, typename cudaVectorTypeInfo<T>::Base y, typename cudaVectorTypeInfo<T>::Base z, typename cudaVectorTypeInfo<T>::Base w ) { static_assert(cuda_assert_false<T>::value, "invalid vector type - supported types are uchar3, uchar4, float3, float4"); }
60 
61 template<> inline __host__ __device__ uchar make_vec( uint8_t x, uint8_t y, uint8_t z, uint8_t w ) { return x; }
62 template<> inline __host__ __device__ uchar3 make_vec( uint8_t x, uint8_t y, uint8_t z, uint8_t w ) { return make_uchar3(x,y,z); }
63 template<> inline __host__ __device__ uchar4 make_vec( uint8_t x, uint8_t y, uint8_t z, uint8_t w ) { return make_uchar4(x,y,z,w); }
64 
65 template<> inline __host__ __device__ float make_vec( float x, float y, float z, float w ) { return x; }
66 template<> inline __host__ __device__ float3 make_vec( float x, float y, float z, float w ) { return make_float3(x,y,z); }
67 template<> inline __host__ __device__ float4 make_vec( float x, float y, float z, float w ) { return make_float4(x,y,z,w); }
68 
69 
70 // cast_vec<T> templates
71 template<typename T> inline __host__ __device__ T cast_vec( const uchar3& a ) { static_assert(cuda_assert_false<T>::value, "invalid vector type - supported types are uchar3, uchar4, float3, float4"); }
72 template<typename T> inline __host__ __device__ T cast_vec( const uchar4& a ) { static_assert(cuda_assert_false<T>::value, "invalid vector type - supported types are uchar3, uchar4, float3, float4"); }
73 template<typename T> inline __host__ __device__ T cast_vec( const float3& a ) { static_assert(cuda_assert_false<T>::value, "invalid vector type - supported types are uchar3, uchar4, float3, float4"); }
74 template<typename T> inline __host__ __device__ T cast_vec( const float4& a ) { static_assert(cuda_assert_false<T>::value, "invalid vector type - supported types are uchar3, uchar4, float3, float4"); }
75 
76 template<> inline __host__ __device__ uchar3 cast_vec( const uchar3& a ) { return make_uchar3(a); }
77 template<> inline __host__ __device__ uchar4 cast_vec( const uchar3& a ) { return make_uchar4(a); }
78 template<> inline __host__ __device__ float3 cast_vec( const uchar3& a ) { return make_float3(a); }
79 template<> inline __host__ __device__ float4 cast_vec( const uchar3& a ) { return make_float4(a); }
80 
81 template<> inline __host__ __device__ uchar3 cast_vec( const uchar4& a ) { return make_uchar3(a); }
82 template<> inline __host__ __device__ uchar4 cast_vec( const uchar4& a ) { return make_uchar4(a); }
83 template<> inline __host__ __device__ float3 cast_vec( const uchar4& a ) { return make_float3(a); }
84 template<> inline __host__ __device__ float4 cast_vec( const uchar4& a ) { return make_float4(a); }
85 
86 template<> inline __host__ __device__ uchar3 cast_vec( const float3& a ) { return make_uchar3(a); }
87 template<> inline __host__ __device__ uchar4 cast_vec( const float3& a ) { return make_uchar4(a); }
88 template<> inline __host__ __device__ float3 cast_vec( const float3& a ) { return make_float3(a); }
89 template<> inline __host__ __device__ float4 cast_vec( const float3& a ) { return make_float4(a); }
90 
91 template<> inline __host__ __device__ uchar3 cast_vec( const float4& a ) { return make_uchar3(a); }
92 template<> inline __host__ __device__ uchar4 cast_vec( const float4& a ) { return make_uchar4(a); }
93 template<> inline __host__ __device__ float3 cast_vec( const float4& a ) { return make_float3(a); }
94 template<> inline __host__ __device__ float4 cast_vec( const float4& a ) { return make_float4(a); }
95 
96 
97 // extract alpha color component
98 template<typename T> inline __device__ typename cudaVectorTypeInfo<T>::Base alpha( T vec, typename cudaVectorTypeInfo<T>::Base default_alpha=255 ) { static_assert(cuda_assert_false<T>::value, "invalid vector type - supported types are uchar3, uchar4, float3, float4"); }
99 
100 template<> inline __host__ __device__ uint8_t alpha( uchar3 vec, uint8_t default_alpha ) { return default_alpha; }
101 template<> inline __host__ __device__ uint8_t alpha( uchar4 vec, uint8_t default_alpha ) { return vec.w; }
102 
103 template<> inline __host__ __device__ float alpha( float3 vec, float default_alpha ) { return default_alpha; }
104 template<> inline __host__ __device__ float alpha( float4 vec, float default_alpha ) { return vec.w; }
105 
107 
108 #endif
109 
cuda_assert_false
Definition: cudaVector.h:55
cudaVectorTypeInfo< float >::Base
float Base
Definition: cudaVector.h:49
make_uchar3
__host__ __device__ uchar3 make_uchar3(uchar s)
Definition: cudaMath.h:207
make_float3
__host__ __device__ float3 make_float3(float s)
Definition: cudaMath.h:128
cudaVectorTypeInfo< float4 >::Base
float Base
Definition: cudaVector.h:51
cudaVectorTypeInfo< float3 >::Base
float Base
Definition: cudaVector.h:50
cudaMath.h
make_vec
__host__ __device__ T make_vec(typename cudaVectorTypeInfo< T >::Base x, typename cudaVectorTypeInfo< T >::Base y, typename cudaVectorTypeInfo< T >::Base z, typename cudaVectorTypeInfo< T >::Base w)
Definition: cudaVector.h:59
cudaVectorTypeInfo< uchar >::Base
uint8_t Base
Definition: cudaVector.h:45
cudaVectorTypeInfo< uchar4 >::Base
uint8_t Base
Definition: cudaVector.h:47
make_float4
__host__ __device__ float4 make_float4(float s)
Definition: cudaMath.h:248
uchar
unsigned char uchar
Definition: cudaMath.h:37
make_uchar4
__host__ __device__ uchar4 make_uchar4(uchar s)
Definition: cudaMath.h:320
cudaVectorTypeInfo
Definition: cudaVector.h:43
cast_vec
__host__ __device__ T cast_vec(const uchar3 &a)
Definition: cudaVector.h:71
cudaVectorTypeInfo< uchar3 >::Base
uint8_t Base
Definition: cudaVector.h:46
alpha
__device__ cudaVectorTypeInfo< T >::Base alpha(T vec, typename cudaVectorTypeInfo< T >::Base default_alpha=255)
Definition: cudaVector.h:98