|
| 1 | +#include "image_common.h" |
| 2 | + |
| 3 | +#ifdef cl_khr_fp16 |
| 4 | +#pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 5 | +#endif |
| 6 | + |
| 7 | +// From |
| 8 | +// https://github.com/ROCm/clr/tree/amd-staging/hipamd/include/hip/amd_detail/texture_fetch_functions.h |
| 9 | +_CLC_CONST_AS const unsigned int SAMPLER_OBJECT_OFFSET_DWORD = 12; |
| 10 | + |
| 11 | +// Using the builtin as_type() and as_typen() functions to reinterpret types. |
| 12 | +// The restriction being is that element "type"s need to be of the same size. |
| 13 | +#define _CLC_DEFINE_BUILTIN_CAST_VEC4_TO_VEC3(vec4_elem_t, to_t) \ |
| 14 | + _CLC_DEF to_t##3 __clc_cast_from_##vec4_elem_t##4_to_##to_t##3( \ |
| 15 | + vec4_elem_t##4 from) { \ |
| 16 | + vec4_elem_t##3 casted = as_##vec4_elem_t##3(from); \ |
| 17 | + return as_##to_t##3(casted); \ |
| 18 | + } |
| 19 | +#define _CLC_DEFINE_BUILTIN_CAST_VEC4_TO_VEC2(vec4_elem_t, to_t) \ |
| 20 | + _CLC_DEF to_t##2 __clc_cast_from_##vec4_elem_t##4_to_##to_t##2( \ |
| 21 | + vec4_elem_t##4 from) { \ |
| 22 | + vec4_elem_t##4 casted = as_##vec4_elem_t##4(from); \ |
| 23 | + return as_##to_t##2((vec4_elem_t##2)(casted.x, casted.y)); \ |
| 24 | + } |
| 25 | +#define _CLC_DEFINE_BUILTIN_CAST_VEC4_TO_SCALAR(vec4_elem_t, to_t) \ |
| 26 | + _CLC_DEF to_t __clc_cast_from_##vec4_elem_t##4_to_##to_t( \ |
| 27 | + vec4_elem_t##4 from) { \ |
| 28 | + vec4_elem_t##4 casted = as_##vec4_elem_t##4(from); \ |
| 29 | + return as_##to_t(casted.x); \ |
| 30 | + } |
| 31 | +#define _CLC_DEFINE_BUILTIN_CAST_VEC3_TO_VEC4(from_t, vec4_elem_t) \ |
| 32 | + _CLC_DEF vec4_elem_t##4 __clc_cast_from_##from_t##3_to_##vec4_elem_t##4( \ |
| 33 | + from_t##3 from) { \ |
| 34 | + vec4_elem_t##3 casted = as_##vec4_elem_t##3(from); \ |
| 35 | + return as_##vec4_elem_t##4(casted); \ |
| 36 | + } |
| 37 | +#define _CLC_DEFINE_BUILTIN_CAST_VEC2_TO_VEC4(from_t, vec4_elem_t) \ |
| 38 | + _CLC_DEF vec4_elem_t##4 __clc_cast_from_##from_t##2_to_##vec4_elem_t##4( \ |
| 39 | + from_t##2 from) { \ |
| 40 | + vec4_elem_t##2 casted = as_##vec4_elem_t##2(from); \ |
| 41 | + return (vec4_elem_t##4)(casted.x, casted.y, 0, 0); \ |
| 42 | + } |
| 43 | +#define _CLC_DEFINE_BUILTIN_CAST_SCALAR_TO_VEC4(from_t, vec4_elem_t) \ |
| 44 | + _CLC_DEF vec4_elem_t##4 __clc_cast_from_##from_t##_to_##vec4_elem_t##4( \ |
| 45 | + from_t from) { \ |
| 46 | + vec4_elem_t casted = as_##vec4_elem_t(from); \ |
| 47 | + return (vec4_elem_t##4)(casted, 0, 0, 0); \ |
| 48 | + } |
| 49 | + |
| 50 | +// Generic casts between builtin types. |
| 51 | +#define _CLC_DEFINE_CAST_VEC4(vec4_elem_t, to_t) \ |
| 52 | + _CLC_DEF to_t##4 __clc_cast_from_##vec4_elem_t##4_to_##to_t##4( \ |
| 53 | + vec4_elem_t##4 from) { \ |
| 54 | + return (to_t##4)(from.x, from.y, from.z, from.w); \ |
| 55 | + } |
| 56 | +#define _CLC_DEFINE_CAST_VEC4_TO_VEC3(vec4_elem_t, to_t) \ |
| 57 | + _CLC_DEF to_t##3 __clc_cast_from_##vec4_elem_t##4_to_##to_t##3( \ |
| 58 | + vec4_elem_t##4 from) { \ |
| 59 | + return (to_t##3)(from.x, from.y, from.z); \ |
| 60 | + } |
| 61 | +#define _CLC_DEFINE_CAST_VEC4_TO_VEC2(vec4_elem_t, to_t) \ |
| 62 | + _CLC_DEF to_t##2 __clc_cast_from_##vec4_elem_t##4_to_##to_t##2( \ |
| 63 | + vec4_elem_t##4 from) { \ |
| 64 | + return (to_t##2)(from.x, from.y); \ |
| 65 | + } |
| 66 | +#define _CLC_DEFINE_CAST_VEC4_TO_SCALAR(vec4_elem_t, to_t) \ |
| 67 | + _CLC_DEF to_t __clc_cast_from_##vec4_elem_t##4_to_##to_t( \ |
| 68 | + vec4_elem_t##4 from) { \ |
| 69 | + return (to_t)from.x; \ |
| 70 | + } |
| 71 | +#define _CLC_DEFINE_CAST_VEC3_TO_VEC4(from_t, vec4_elem_t) \ |
| 72 | + _CLC_DEF vec4_elem_t##4 __clc_cast_from_##from_t##3_to_##vec4_elem_t##4( \ |
| 73 | + from_t##3 from) { \ |
| 74 | + return (vec4_elem_t##4)(from.x, from.y, from.z, 0); \ |
| 75 | + } |
| 76 | +#define _CLC_DEFINE_CAST_VEC2_TO_VEC4(from_t, vec4_elem_t) \ |
| 77 | + _CLC_DEF vec4_elem_t##4 __clc_cast_from_##from_t##2_to_##vec4_elem_t##4( \ |
| 78 | + from_t##2 from) { \ |
| 79 | + return (vec4_elem_t##4)(from.x, from.y, 0, 0); \ |
| 80 | + } |
| 81 | +#define _CLC_DEFINE_CAST_SCALAR_TO_VEC4(from_t, vec4_elem_t) \ |
| 82 | + _CLC_DEF vec4_elem_t##4 __clc_cast_from_##from_t##_to_##vec4_elem_t##4( \ |
| 83 | + from_t from) { \ |
| 84 | + return (vec4_elem_t##4)(from, 0, 0, 0); \ |
| 85 | + } |
| 86 | + |
| 87 | +// Helpers to extract N channel(s) from a four-channel (RGBA/XYZW) color type. |
| 88 | + |
| 89 | +#define _CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(from_t, to_t) \ |
| 90 | + _CLC_DEFINE_CAST_VEC4(from_t, to_t) \ |
| 91 | + _CLC_DEFINE_BUILTIN_CAST_VEC4_TO_VEC3(from_t, to_t) \ |
| 92 | + _CLC_DEFINE_BUILTIN_CAST_VEC4_TO_VEC2(from_t, to_t) \ |
| 93 | + _CLC_DEFINE_BUILTIN_CAST_VEC4_TO_SCALAR(from_t, to_t) \ |
| 94 | + _CLC_DEFINE_BUILTIN_CAST_VEC2_TO_VEC4(from_t, to_t) \ |
| 95 | + _CLC_DEFINE_BUILTIN_CAST_VEC3_TO_VEC4(from_t, to_t) \ |
| 96 | + _CLC_DEFINE_BUILTIN_CAST_SCALAR_TO_VEC4(from_t, to_t) |
| 97 | + |
| 98 | +#define _CLC_DEFINE_EXTRACT_COLOR_HELPERS(from_t, to_t) \ |
| 99 | + _CLC_DEFINE_CAST_VEC4(from_t, to_t) \ |
| 100 | + _CLC_DEFINE_CAST_VEC4_TO_VEC3(from_t, to_t) \ |
| 101 | + _CLC_DEFINE_CAST_VEC4_TO_VEC2(from_t, to_t) \ |
| 102 | + _CLC_DEFINE_CAST_VEC4_TO_SCALAR(from_t, to_t) \ |
| 103 | + _CLC_DEFINE_CAST_VEC2_TO_VEC4(from_t, to_t) \ |
| 104 | + _CLC_DEFINE_CAST_VEC3_TO_VEC4(from_t, to_t) \ |
| 105 | + _CLC_DEFINE_CAST_SCALAR_TO_VEC4(from_t, to_t) |
| 106 | + |
| 107 | +// Define casts between supported builtin types for image color |
| 108 | + |
| 109 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(float, float) |
| 110 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(float, int) |
| 111 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(int, float) |
| 112 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(float, uint) |
| 113 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(uint, float) |
| 114 | +#ifdef cl_khr_fp16 |
| 115 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(half, half) |
| 116 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(half, short) |
| 117 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(short, half) |
| 118 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(half, ushort) |
| 119 | +_CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS(ushort, half) |
| 120 | +#endif |
| 121 | + |
| 122 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(float, short) |
| 123 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(short, float) |
| 124 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(float, ushort) |
| 125 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(ushort, float) |
| 126 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(float, char) |
| 127 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(char, float) |
| 128 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(float, uchar) |
| 129 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(uchar, float) |
| 130 | +#ifdef cl_khr_fp16 |
| 131 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(float, half) |
| 132 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(half, float) |
| 133 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(half, int) |
| 134 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(int, half) |
| 135 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(half, uint) |
| 136 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(uint, half) |
| 137 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(half, char) |
| 138 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(char, half) |
| 139 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(half, uchar) |
| 140 | +_CLC_DEFINE_EXTRACT_COLOR_HELPERS(uchar, half) |
| 141 | +#endif |
| 142 | + |
| 143 | +#undef _CLC_DEFINE_EXTRACT_COLOR_HELPERS |
| 144 | +#undef _CLC_DEFINE_EXTRACT_SAME_SIZE_COLOR_HELPERS |
| 145 | + |
| 146 | +#undef _CLC_DEFINE_CAST_SCALAR_TO_VEC4 |
| 147 | +#undef _CLC_DEFINE_CAST_VEC2_TO_VEC4 |
| 148 | +#undef _CLC_DEFINE_CAST_VEC3_TO_VEC4 |
| 149 | +#undef _CLC_DEFINE_CAST_VEC4_TO_SCALAR |
| 150 | +#undef _CLC_DEFINE_CAST_VEC4_TO_VEC3 |
| 151 | +#undef _CLC_DEFINE_CAST_VEC4_TO_VEC2 |
| 152 | +#undef _CLC_DEFINE_CAST_VEC4 |
| 153 | +#undef _CLC_DEFINE_BUILTIN_CAST_SCALAR_TO_VEC4 |
| 154 | +#undef _CLC_DEFINE_BUILTIN_CAST_VEC2_TO_VEC4 |
| 155 | +#undef _CLC_DEFINE_BUILTIN_VEC3_TO_VEC4 |
| 156 | +#undef _CLC_DEFINE_BUILTIN_VEC4_TO_SCALAR |
| 157 | +#undef _CLC_DEFINE_BUILTIN_VEC4_TO_VEC3 |
| 158 | +#undef _CLC_DEFINE_BUILTIN_VEC4_TO_VEC2 |
| 159 | +#undef _CLC_DEFINE_BUILTIN_VEC4 |
0 commit comments