Stephen Hines | 05f25d2 | 2011-08-09 14:21:04 -0700 | [diff] [blame] | 1 | #include "rs_core.rsh" |
Alex Sakhartchouk | 5681ee6 | 2011-10-15 09:55:24 -0700 | [diff] [blame] | 2 | #include "rs_graphics.rsh" |
Alex Sakhartchouk | ff52255 | 2012-03-21 10:52:59 -0700 | [diff] [blame] | 3 | #include "rs_structs.h" |
Alex Sakhartchouk | aa08fd6 | 2011-11-17 16:01:24 -0800 | [diff] [blame] | 4 | |
Stephen Hines | cfee7e9 | 2012-05-14 18:02:57 -0700 | [diff] [blame] | 5 | /* Function declarations from libRS */ |
Shih-wei Liao | f9728b9 | 2011-01-17 18:04:33 -0800 | [diff] [blame] | 6 | extern float4 __attribute__((overloadable)) convert_float4(uchar4 c); |
| 7 | |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 8 | /* Implementation of Core Runtime */ |
| 9 | |
Jason Sams | f7ab377 | 2012-02-29 15:59:43 -0800 | [diff] [blame] | 10 | /* |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 11 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b) |
| 12 | { |
| 13 | uchar4 c; |
Jason Sams | 9d3f8a7 | 2011-10-13 17:20:04 -0700 | [diff] [blame] | 14 | c.x = (uchar)(r * 255.f + 0.5f); |
| 15 | c.y = (uchar)(g * 255.f + 0.5f); |
| 16 | c.z = (uchar)(b * 255.f + 0.5f); |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 17 | c.w = 255; |
| 18 | return c; |
| 19 | } |
| 20 | |
| 21 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a) |
| 22 | { |
| 23 | uchar4 c; |
Jason Sams | 9d3f8a7 | 2011-10-13 17:20:04 -0700 | [diff] [blame] | 24 | c.x = (uchar)(r * 255.f + 0.5f); |
| 25 | c.y = (uchar)(g * 255.f + 0.5f); |
| 26 | c.z = (uchar)(b * 255.f + 0.5f); |
| 27 | c.w = (uchar)(a * 255.f + 0.5f); |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 28 | return c; |
| 29 | } |
| 30 | |
| 31 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color) |
| 32 | { |
| 33 | color *= 255.f; |
Jason Sams | 9d3f8a7 | 2011-10-13 17:20:04 -0700 | [diff] [blame] | 34 | color += 0.5f; |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 35 | uchar4 c = {color.x, color.y, color.z, 255}; |
| 36 | return c; |
| 37 | } |
| 38 | |
| 39 | extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color) |
| 40 | { |
| 41 | color *= 255.f; |
Jason Sams | 9d3f8a7 | 2011-10-13 17:20:04 -0700 | [diff] [blame] | 42 | color += 0.5f; |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 43 | uchar4 c = {color.x, color.y, color.z, color.w}; |
| 44 | return c; |
| 45 | } |
Jason Sams | f7ab377 | 2012-02-29 15:59:43 -0800 | [diff] [blame] | 46 | */ |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 47 | |
| 48 | extern float4 rsUnpackColor8888(uchar4 c) |
| 49 | { |
Jason Sams | 9d3f8a7 | 2011-10-13 17:20:04 -0700 | [diff] [blame] | 50 | float4 ret = (float4)0.003921569f; |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 51 | ret *= convert_float4(c); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | ///////////////////////////////////////////////////// |
| 56 | // Matrix ops |
| 57 | ///////////////////////////////////////////////////// |
| 58 | |
| 59 | extern void __attribute__((overloadable)) |
| 60 | rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v) { |
| 61 | m->m[row * 4 + col] = v; |
| 62 | } |
| 63 | |
| 64 | extern float __attribute__((overloadable)) |
| 65 | rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col) { |
| 66 | return m->m[row * 4 + col]; |
| 67 | } |
| 68 | |
| 69 | extern void __attribute__((overloadable)) |
| 70 | rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v) { |
| 71 | m->m[row * 3 + col] = v; |
| 72 | } |
| 73 | |
| 74 | extern float __attribute__((overloadable)) |
| 75 | rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col) { |
| 76 | return m->m[row * 3 + col]; |
| 77 | } |
| 78 | |
| 79 | extern void __attribute__((overloadable)) |
| 80 | rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v) { |
| 81 | m->m[row * 2 + col] = v; |
| 82 | } |
| 83 | |
| 84 | extern float __attribute__((overloadable)) |
| 85 | rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col) { |
| 86 | return m->m[row * 2 + col]; |
| 87 | } |
| 88 | |
Jason Sams | 5bc951c | 2012-02-27 19:32:15 -0800 | [diff] [blame] | 89 | /* |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 90 | extern float4 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 91 | rsMatrixMultiply(const rs_matrix4x4 *m, float4 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 92 | float4 ret; |
| 93 | ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + (m->m[12] * in.w); |
| 94 | ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + (m->m[13] * in.w); |
| 95 | ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + (m->m[14] * in.w); |
| 96 | ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + (m->m[15] * in.w); |
| 97 | return ret; |
| 98 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 99 | extern float4 __attribute__((overloadable)) |
| 100 | rsMatrixMultiply(rs_matrix4x4 *m, float4 in) { |
| 101 | return rsMatrixMultiply((const rs_matrix4x4 *)m, in); |
| 102 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 103 | |
| 104 | extern float4 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 105 | rsMatrixMultiply(const rs_matrix4x4 *m, float3 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 106 | float4 ret; |
| 107 | ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + (m->m[8] * in.z) + m->m[12]; |
| 108 | ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + (m->m[9] * in.z) + m->m[13]; |
| 109 | ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + (m->m[10] * in.z) + m->m[14]; |
| 110 | ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + (m->m[11] * in.z) + m->m[15]; |
| 111 | return ret; |
| 112 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 113 | extern float4 __attribute__((overloadable)) |
| 114 | rsMatrixMultiply(rs_matrix4x4 *m, float3 in) { |
| 115 | return rsMatrixMultiply((const rs_matrix4x4 *)m, in); |
| 116 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 117 | |
| 118 | extern float4 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 119 | rsMatrixMultiply(const rs_matrix4x4 *m, float2 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 120 | float4 ret; |
| 121 | ret.x = (m->m[0] * in.x) + (m->m[4] * in.y) + m->m[12]; |
| 122 | ret.y = (m->m[1] * in.x) + (m->m[5] * in.y) + m->m[13]; |
| 123 | ret.z = (m->m[2] * in.x) + (m->m[6] * in.y) + m->m[14]; |
| 124 | ret.w = (m->m[3] * in.x) + (m->m[7] * in.y) + m->m[15]; |
| 125 | return ret; |
| 126 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 127 | extern float4 __attribute__((overloadable)) |
| 128 | rsMatrixMultiply(rs_matrix4x4 *m, float2 in) { |
| 129 | return rsMatrixMultiply((const rs_matrix4x4 *)m, in); |
| 130 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 131 | |
| 132 | extern float3 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 133 | rsMatrixMultiply(const rs_matrix3x3 *m, float3 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 134 | float3 ret; |
| 135 | ret.x = (m->m[0] * in.x) + (m->m[3] * in.y) + (m->m[6] * in.z); |
| 136 | ret.y = (m->m[1] * in.x) + (m->m[4] * in.y) + (m->m[7] * in.z); |
| 137 | ret.z = (m->m[2] * in.x) + (m->m[5] * in.y) + (m->m[8] * in.z); |
| 138 | return ret; |
| 139 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 140 | extern float3 __attribute__((overloadable)) |
| 141 | rsMatrixMultiply(rs_matrix3x3 *m, float3 in) { |
| 142 | return rsMatrixMultiply((const rs_matrix3x3 *)m, in); |
| 143 | } |
| 144 | |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 145 | extern float3 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 146 | rsMatrixMultiply(const rs_matrix3x3 *m, float2 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 147 | float3 ret; |
| 148 | ret.x = (m->m[0] * in.x) + (m->m[3] * in.y); |
| 149 | ret.y = (m->m[1] * in.x) + (m->m[4] * in.y); |
| 150 | ret.z = (m->m[2] * in.x) + (m->m[5] * in.y); |
| 151 | return ret; |
| 152 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 153 | extern float3 __attribute__((overloadable)) |
| 154 | rsMatrixMultiply(rs_matrix3x3 *m, float2 in) { |
| 155 | return rsMatrixMultiply((const rs_matrix3x3 *)m, in); |
| 156 | } |
Jason Sams | 5bc951c | 2012-02-27 19:32:15 -0800 | [diff] [blame] | 157 | */ |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 158 | |
| 159 | extern float2 __attribute__((overloadable)) |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 160 | rsMatrixMultiply(const rs_matrix2x2 *m, float2 in) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 161 | float2 ret; |
| 162 | ret.x = (m->m[0] * in.x) + (m->m[2] * in.y); |
| 163 | ret.y = (m->m[1] * in.x) + (m->m[3] * in.y); |
| 164 | return ret; |
| 165 | } |
Jason Sams | f9931b8 | 2011-07-26 17:28:38 -0700 | [diff] [blame] | 166 | extern float2 __attribute__((overloadable)) |
| 167 | rsMatrixMultiply(rs_matrix2x2 *m, float2 in) { |
| 168 | return rsMatrixMultiply((const rs_matrix2x2 *)m, in); |
| 169 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 170 | |
| 171 | ///////////////////////////////////////////////////// |
| 172 | // int ops |
| 173 | ///////////////////////////////////////////////////// |
| 174 | |
Jason Sams | 17930e1 | 2012-04-10 17:21:34 -0700 | [diff] [blame] | 175 | extern uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high) { |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 176 | return amount < low ? low : (amount > high ? high : amount); |
Jason Sams | 17930e1 | 2012-04-10 17:21:34 -0700 | [diff] [blame] | 177 | } |
Shih-wei Liao | 50cdf8f | 2011-01-26 14:10:25 -0800 | [diff] [blame] | 178 | extern int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high) { |
| 179 | return amount < low ? low : (amount > high ? high : amount); |
| 180 | } |
| 181 | extern ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high) { |
| 182 | return amount < low ? low : (amount > high ? high : amount); |
| 183 | } |
| 184 | extern short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high) { |
| 185 | return amount < low ? low : (amount > high ? high : amount); |
| 186 | } |
| 187 | extern uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high) { |
| 188 | return amount < low ? low : (amount > high ? high : amount); |
| 189 | } |
| 190 | extern char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high) { |
| 191 | return amount < low ? low : (amount > high ? high : amount); |
| 192 | } |