Ying Wang | 77643ba | 2011-01-18 22:24:26 -0800 | [diff] [blame] | 1 | #include "rs_types.rsh" |
Shih-wei Liao | 39ebe2c | 2011-01-28 11:22:32 -0800 | [diff] [blame] | 2 | #include "rs_cl.rsh" |
Shih-wei Liao | f9728b9 | 2011-01-17 18:04:33 -0800 | [diff] [blame] | 3 | |
Stephen Hines | d4740c5 | 2011-08-08 15:04:52 -0700 | [diff] [blame^] | 4 | /***************************************************************************** |
| 5 | * CAUTION |
| 6 | * |
| 7 | * The following structure layout provides a more efficient way to access |
| 8 | * internal members of the C++ class Allocation owned by librs. Unfortunately, |
| 9 | * since this class has virtual members, we can't simply use offsetof() or any |
| 10 | * other compiler trickery to dynamically get the appropriate values at |
| 11 | * build-time. This layout may need to be updated whenever |
| 12 | * frameworks/base/libs/rs/rsAllocation.h is modified. |
| 13 | * |
| 14 | * Having the layout information available in this file allows us to |
| 15 | * accelerate functionality like rsAllocationGetDimX(). Without this |
| 16 | * information, we would not be able to inline the bitcode, thus resulting in |
| 17 | * potential runtime performance penalties for tight loops operating on |
| 18 | * allocations. |
| 19 | * |
| 20 | *****************************************************************************/ |
| 21 | typedef struct Allocation { |
| 22 | char __pad[44]; |
| 23 | struct Hal { |
| 24 | struct State { |
| 25 | uint32_t dimensionX; |
| 26 | uint32_t dimensionY; |
| 27 | uint32_t dimensionZ; |
| 28 | uint32_t elementSizeBytes; |
| 29 | bool hasMipmaps; |
| 30 | bool hasFaces; |
| 31 | bool hasReferences; |
| 32 | } state; |
| 33 | |
| 34 | struct DrvState { |
| 35 | void * mallocPtr; |
| 36 | } drvState; |
| 37 | } mHal; |
| 38 | } Allocation_t; |
| 39 | |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 40 | /* Declaration of 4 basic functions in libRS */ |
| 41 | extern void __attribute__((overloadable)) |
| 42 | rsDebug(const char *, float, float); |
| 43 | extern void __attribute__((overloadable)) |
| 44 | rsDebug(const char *, float, float, float); |
| 45 | extern void __attribute__((overloadable)) |
| 46 | rsDebug(const char *, float, float, float, float); |
Shih-wei Liao | f9728b9 | 2011-01-17 18:04:33 -0800 | [diff] [blame] | 47 | extern float4 __attribute__((overloadable)) convert_float4(uchar4 c); |
| 48 | |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 49 | /* Implementation of Core Runtime */ |
| 50 | |
| 51 | extern void __attribute__((overloadable)) rsDebug(const char *s, float2 v) { |
| 52 | rsDebug(s, v.x, v.y); |
| 53 | } |
| 54 | |
| 55 | extern void __attribute__((overloadable)) rsDebug(const char *s, float3 v) { |
| 56 | rsDebug(s, v.x, v.y, v.z); |
| 57 | } |
| 58 | |
| 59 | extern void __attribute__((overloadable)) rsDebug(const char *s, float4 v) { |
| 60 | rsDebug(s, v.x, v.y, v.z, v.w); |
| 61 | } |
| 62 | |
| 63 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b) |
| 64 | { |
| 65 | uchar4 c; |
| 66 | c.x = (uchar)(r * 255.f); |
| 67 | c.y = (uchar)(g * 255.f); |
| 68 | c.z = (uchar)(b * 255.f); |
| 69 | c.w = 255; |
| 70 | return c; |
| 71 | } |
| 72 | |
| 73 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a) |
| 74 | { |
| 75 | uchar4 c; |
| 76 | c.x = (uchar)(r * 255.f); |
| 77 | c.y = (uchar)(g * 255.f); |
| 78 | c.z = (uchar)(b * 255.f); |
| 79 | c.w = (uchar)(a * 255.f); |
| 80 | return c; |
| 81 | } |
| 82 | |
| 83 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color) |
| 84 | { |
| 85 | color *= 255.f; |
| 86 | uchar4 c = {color.x, color.y, color.z, 255}; |
| 87 | return c; |
| 88 | } |
| 89 | |
| 90 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color) |
| 91 | { |
| 92 | color *= 255.f; |
| 93 | uchar4 c = {color.x, color.y, color.z, color.w}; |
| 94 | return c; |
| 95 | } |
| 96 | |
| 97 | extern float4 rsUnpackColor8888(uchar4 c) |
| 98 | { |
| 99 | float4 ret = (float4)0.0039156862745f; |
| 100 | ret *= convert_float4(c); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | ///////////////////////////////////////////////////// |
| 105 | // Matrix ops |
| 106 | ///////////////////////////////////////////////////// |
| 107 | |
| 108 | extern void __attribute__((overloadable)) |
| 109 | rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v) { |
| 110 | m->m[row * 4 + col] = v; |
| 111 | } |
| 112 | |
| 113 | extern float __attribute__((overloadable)) |
| 114 | rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col) { |
| 115 | return m->m[row * 4 + col]; |
| 116 | } |
| 117 | |
| 118 | extern void __attribute__((overloadable)) |
| 119 | rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v) { |
| 120 | m->m[row * 3 + col] = v; |
| 121 | } |
| 122 | |
| 123 | extern float __attribute__((overloadable)) |
| 124 | rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col) { |
| 125 | return m->m[row * 3 + col]; |
| 126 | } |
| 127 | |
| 128 | extern void __attribute__((overloadable)) |
| 129 | rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v) { |
| 130 | m->m[row * 2 + col] = v; |
| 131 | } |
| 132 | |
| 133 | extern float __attribute__((overloadable)) |
| 134 | rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col) { |
| 135 | return m->m[row * 2 + col]; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | extern float4 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 140 | rsMatrixMultiply(const rs_matrix4x4 *m, float4 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 141 | float4 ret; |
| 142 | ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + (m->m[12] * in.w); |
| 143 | ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + (m->m[13] * in.w); |
| 144 | ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + (m->m[14] * in.w); |
| 145 | ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + (m->m[15] * in.w); |
| 146 | return ret; |
| 147 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 148 | extern float4 __attribute__((overloadable)) |
| 149 | rsMatrixMultiply(rs_matrix4x4 *m, float4 in) { |
| 150 | return rsMatrixMultiply((const rs_matrix4x4 *)m, in); |
| 151 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 152 | |
| 153 | extern float4 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 154 | rsMatrixMultiply(const rs_matrix4x4 *m, float3 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 155 | float4 ret; |
| 156 | ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + m->m[12]; |
| 157 | ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + m->m[13]; |
| 158 | ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + m->m[14]; |
| 159 | ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + m->m[15]; |
| 160 | return ret; |
| 161 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 162 | extern float4 __attribute__((overloadable)) |
| 163 | rsMatrixMultiply(rs_matrix4x4 *m, float3 in) { |
| 164 | return rsMatrixMultiply((const rs_matrix4x4 *)m, in); |
| 165 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 166 | |
| 167 | extern float4 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 168 | rsMatrixMultiply(const rs_matrix4x4 *m, float2 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 169 | float4 ret; |
| 170 | ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + m->m[12]; |
| 171 | ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + m->m[13]; |
| 172 | ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + m->m[14]; |
| 173 | ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + m->m[15]; |
| 174 | return ret; |
| 175 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 176 | extern float4 __attribute__((overloadable)) |
| 177 | rsMatrixMultiply(rs_matrix4x4 *m, float2 in) { |
| 178 | return rsMatrixMultiply((const rs_matrix4x4 *)m, in); |
| 179 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 180 | |
| 181 | extern float3 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 182 | rsMatrixMultiply(const rs_matrix3x3 *m, float3 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 183 | float3 ret; |
| 184 | ret.x = (m->m[0] * in.x) + (m->m[3] * in.y) + (m->m[6] * in.z); |
| 185 | ret.y = (m->m[1] * in.x) + (m->m[4] * in.y) + (m->m[7] * in.z); |
| 186 | ret.z = (m->m[2] * in.x) + (m->m[5] * in.y) + (m->m[8] * in.z); |
| 187 | return ret; |
| 188 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 189 | extern float3 __attribute__((overloadable)) |
| 190 | rsMatrixMultiply(rs_matrix3x3 *m, float3 in) { |
| 191 | return rsMatrixMultiply((const rs_matrix3x3 *)m, in); |
| 192 | } |
| 193 | |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 194 | |
| 195 | extern float3 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 196 | rsMatrixMultiply(const rs_matrix3x3 *m, float2 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 197 | float3 ret; |
| 198 | ret.x = (m->m[0] * in.x) + (m->m[3] * in.y); |
| 199 | ret.y = (m->m[1] * in.x) + (m->m[4] * in.y); |
| 200 | ret.z = (m->m[2] * in.x) + (m->m[5] * in.y); |
| 201 | return ret; |
| 202 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 203 | extern float3 __attribute__((overloadable)) |
| 204 | rsMatrixMultiply(rs_matrix3x3 *m, float2 in) { |
| 205 | return rsMatrixMultiply((const rs_matrix3x3 *)m, in); |
| 206 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 207 | |
| 208 | extern float2 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 209 | rsMatrixMultiply(const rs_matrix2x2 *m, float2 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 210 | float2 ret; |
| 211 | ret.x = (m->m[0] * in.x) + (m->m[2] * in.y); |
| 212 | ret.y = (m->m[1] * in.x) + (m->m[3] * in.y); |
| 213 | return ret; |
| 214 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 215 | extern float2 __attribute__((overloadable)) |
| 216 | rsMatrixMultiply(rs_matrix2x2 *m, float2 in) { |
| 217 | return rsMatrixMultiply((const rs_matrix2x2 *)m, in); |
| 218 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 219 | |
| 220 | ///////////////////////////////////////////////////// |
| 221 | // int ops |
| 222 | ///////////////////////////////////////////////////// |
| 223 | |
| 224 | extern uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high) { |
| 225 | return amount < low ? low : (amount > high ? high : amount); |
| 226 | } |
| 227 | extern int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high) { |
| 228 | return amount < low ? low : (amount > high ? high : amount); |
| 229 | } |
| 230 | extern ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high) { |
| 231 | return amount < low ? low : (amount > high ? high : amount); |
| 232 | } |
| 233 | extern short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high) { |
| 234 | return amount < low ? low : (amount > high ? high : amount); |
| 235 | } |
| 236 | extern uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high) { |
| 237 | return amount < low ? low : (amount > high ? high : amount); |
| 238 | } |
| 239 | extern char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high) { |
| 240 | return amount < low ? low : (amount > high ? high : amount); |
| 241 | } |
Stephen Hines | d4740c5 | 2011-08-08 15:04:52 -0700 | [diff] [blame^] | 242 | |
| 243 | // Opaque Allocation type operations |
| 244 | extern uint32_t __attribute__((overloadable)) |
| 245 | rsAllocationGetDimX(rs_allocation a) { |
| 246 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 247 | return alloc->mHal.state.dimensionX; |
| 248 | } |
| 249 | |
| 250 | extern uint32_t __attribute__((overloadable)) |
| 251 | rsAllocationGetDimY(rs_allocation a) { |
| 252 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 253 | return alloc->mHal.state.dimensionY; |
| 254 | } |
| 255 | |
| 256 | extern uint32_t __attribute__((overloadable)) |
| 257 | rsAllocationGetDimZ(rs_allocation a) { |
| 258 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 259 | return alloc->mHal.state.dimensionZ; |
| 260 | } |
| 261 | |
| 262 | extern uint32_t __attribute__((overloadable)) |
| 263 | rsAllocationGetDimLOD(rs_allocation a) { |
| 264 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 265 | return alloc->mHal.state.hasMipmaps; |
| 266 | } |
| 267 | |
| 268 | extern uint32_t __attribute__((overloadable)) |
| 269 | rsAllocationGetDimFaces(rs_allocation a) { |
| 270 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 271 | return alloc->mHal.state.hasFaces; |
| 272 | } |
| 273 | |
| 274 | extern const void * __attribute__((overloadable)) |
| 275 | rsGetElementAt(rs_allocation a, uint32_t x) { |
| 276 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 277 | const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.mallocPtr; |
| 278 | const uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 279 | return &p[eSize * x]; |
| 280 | } |
| 281 | |
| 282 | extern const void * __attribute__((overloadable)) |
| 283 | rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y) { |
| 284 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 285 | const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.mallocPtr; |
| 286 | const uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 287 | const uint32_t dimX = alloc->mHal.state.dimensionX; |
| 288 | return &p[eSize * (x + y * dimX)]; |
| 289 | } |
| 290 | |
| 291 | extern const void * __attribute__((overloadable)) |
| 292 | rsGetElementAt(rs_allocation a, uint32_t x, uint32_t y, uint32_t z) { |
| 293 | Allocation_t *alloc = (Allocation_t *)a.p; |
| 294 | const uint8_t *p = (const uint8_t *)alloc->mHal.drvState.mallocPtr; |
| 295 | const uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 296 | const uint32_t dimX = alloc->mHal.state.dimensionX; |
| 297 | const uint32_t dimY = alloc->mHal.state.dimensionY; |
| 298 | return &p[eSize * (x + y * dimX + z * dimX * dimY)]; |
| 299 | } |
| 300 | |