1 #include "rs_core.rsh" 2 #include "rs_graphics.rsh" 3 #include "rs_structs.h" 4 5 /* Function declarations from libRS */ 6 extern float4 __attribute__((overloadable)) convert_float4(uchar4 c); 7 8 /* Implementation of Core Runtime */ 9 10 11 ///////////////////////////////////////////////////// 12 // Matrix ops 13 ///////////////////////////////////////////////////// 14 15 16 extern void __attribute__((overloadable)) 17 rsMatrixLoadIdentity(rs_matrix4x4 *m) { 18 m->m[0] = 1.f; 19 m->m[1] = 0.f; 20 m->m[2] = 0.f; 21 m->m[3] = 0.f; 22 m->m[4] = 0.f; 23 m->m[5] = 1.f; 24 m->m[6] = 0.f; 25 m->m[7] = 0.f; 26 m->m[8] = 0.f; 27 m->m[9] = 0.f; 28 m->m[10] = 1.f; 29 m->m[11] = 0.f; 30 m->m[12] = 0.f; 31 m->m[13] = 0.f; 32 m->m[14] = 0.f; 33 m->m[15] = 1.f; 34 } 35 36 extern void __attribute__((overloadable)) 37 rsMatrixLoadIdentity(rs_matrix3x3 *m) { 38 m->m[0] = 1.f; 39 m->m[1] = 0.f; 40 m->m[2] = 0.f; 41 m->m[3] = 0.f; 42 m->m[4] = 1.f; 43 m->m[5] = 0.f; 44 m->m[6] = 0.f; 45 m->m[7] = 0.f; 46 m->m[8] = 1.f; 47 } 48 extern void __attribute__((overloadable)) 49 rsMatrixLoadIdentity(rs_matrix2x2 *m) { 50 m->m[0] = 1.f; 51 m->m[1] = 0.f; 52 m->m[2] = 0.f; 53 m->m[3] = 1.f; 54 } 55 56 extern void __attribute__((overloadable)) 57 rsMatrixLoad(rs_matrix4x4 *m, const float *f) { 58 m->m[0] = f[0]; 59 m->m[1] = f[1]; 60 m->m[2] = f[2]; 61 m->m[3] = f[3]; 62 m->m[4] = f[4]; 63 m->m[5] = f[5]; 64 m->m[6] = f[6]; 65 m->m[7] = f[7]; 66 m->m[8] = f[8]; 67 m->m[9] = f[9]; 68 m->m[10] = f[10]; 69 m->m[11] = f[11]; 70 m->m[12] = f[12]; 71 m->m[13] = f[13]; 72 m->m[14] = f[14]; 73 m->m[15] = f[15]; 74 } 75 extern void __attribute__((overloadable)) 76 rsMatrixLoad(rs_matrix3x3 *m, const float *f) { 77 m->m[0] = f[0]; 78 m->m[1] = f[1]; 79 m->m[2] = f[2]; 80 m->m[3] = f[3]; 81 m->m[4] = f[4]; 82 m->m[5] = f[5]; 83 m->m[6] = f[6]; 84 m->m[7] = f[7]; 85 m->m[8] = f[8]; 86 } 87 extern void __attribute__((overloadable)) 88 rsMatrixLoad(rs_matrix2x2 *m, const float *f) { 89 m->m[0] = f[0]; 90 m->m[1] = f[1]; 91 m->m[2] = f[2]; 92 m->m[3] = f[3]; 93 } 94 95 extern void __attribute__((overloadable)) 96 rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix4x4 *s) { 97 m->m[0] = s->m[0]; 98 m->m[1] = s->m[1]; 99 m->m[2] = s->m[2]; 100 m->m[3] = s->m[3]; 101 m->m[4] = s->m[4]; 102 m->m[5] = s->m[5]; 103 m->m[6] = s->m[6]; 104 m->m[7] = s->m[7]; 105 m->m[8] = s->m[8]; 106 m->m[9] = s->m[9]; 107 m->m[10] = s->m[10]; 108 m->m[11] = s->m[11]; 109 m->m[12] = s->m[12]; 110 m->m[13] = s->m[13]; 111 m->m[14] = s->m[14]; 112 m->m[15] = s->m[15]; 113 } 114 extern void __attribute__((overloadable)) 115 rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix3x3 *v) { 116 m->m[0] = v->m[0]; 117 m->m[1] = v->m[1]; 118 m->m[2] = v->m[2]; 119 m->m[3] = 0.f; 120 m->m[4] = v->m[3]; 121 m->m[5] = v->m[4]; 122 m->m[6] = v->m[5]; 123 m->m[7] = 0.f; 124 m->m[8] = v->m[6]; 125 m->m[9] = v->m[7]; 126 m->m[10] = v->m[8]; 127 m->m[11] = 0.f; 128 m->m[12] = 0.f; 129 m->m[13] = 0.f; 130 m->m[14] = 0.f; 131 m->m[15] = 1.f; 132 } 133 extern void __attribute__((overloadable)) 134 rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix2x2 *v) { 135 m->m[0] = v->m[0]; 136 m->m[1] = v->m[1]; 137 m->m[2] = 0.f; 138 m->m[3] = 0.f; 139 m->m[4] = v->m[2]; 140 m->m[5] = v->m[3]; 141 m->m[6] = 0.f; 142 m->m[7] = 0.f; 143 m->m[8] = 0.f; 144 m->m[9] = 0.f; 145 m->m[10] = 1.f; 146 m->m[11] = 0.f; 147 m->m[12] = 0.f; 148 m->m[13] = 0.f; 149 m->m[14] = 0.f; 150 m->m[15] = 1.f; 151 } 152 extern void __attribute__((overloadable)) 153 rsMatrixLoad(rs_matrix3x3 *m, const rs_matrix3x3 *s) { 154 m->m[0] = s->m[0]; 155 m->m[1] = s->m[1]; 156 m->m[2] = s->m[2]; 157 m->m[3] = s->m[3]; 158 m->m[4] = s->m[4]; 159 m->m[5] = s->m[5]; 160 m->m[6] = s->m[6]; 161 m->m[7] = s->m[7]; 162 m->m[8] = s->m[8]; 163 } 164 extern void __attribute__((overloadable)) 165 rsMatrixLoad(rs_matrix2x2 *m, const rs_matrix2x2 *s) { 166 m->m[0] = s->m[0]; 167 m->m[1] = s->m[1]; 168 m->m[2] = s->m[2]; 169 m->m[3] = s->m[3]; 170 } 171 172 173 extern void __attribute__((overloadable)) 174 rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v) { 175 m->m[row * 4 + col] = v; 176 } 177 178 extern float __attribute__((overloadable)) 179 rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col) { 180 return m->m[row * 4 + col]; 181 } 182 183 extern void __attribute__((overloadable)) 184 rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v) { 185 m->m[row * 3 + col] = v; 186 } 187 188 extern float __attribute__((overloadable)) 189 rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col) { 190 return m->m[row * 3 + col]; 191 } 192 193 extern void __attribute__((overloadable)) 194 rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v) { 195 m->m[row * 2 + col] = v; 196 } 197 198 extern float __attribute__((overloadable)) 199 rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col) { 200 return m->m[row * 2 + col]; 201 } 202 203 extern float2 __attribute__((overloadable)) 204 rsMatrixMultiply(const rs_matrix2x2 *m, float2 in) { 205 float2 ret; 206 ret.x = (m->m[0] * in.x) + (m->m[2] * in.y); 207 ret.y = (m->m[1] * in.x) + (m->m[3] * in.y); 208 return ret; 209 } 210 extern float2 __attribute__((overloadable)) 211 rsMatrixMultiply(rs_matrix2x2 *m, float2 in) { 212 return rsMatrixMultiply((const rs_matrix2x2 *)m, in); 213 } 214 215 extern float4 __attribute__((overloadable)) 216 rsMatrixMultiply(rs_matrix4x4 *m, float4 in) { 217 return rsMatrixMultiply((const rs_matrix4x4 *)m, in); 218 } 219 220 extern float4 __attribute__((overloadable)) 221 rsMatrixMultiply(rs_matrix4x4 *m, float3 in) { 222 return rsMatrixMultiply((const rs_matrix4x4 *)m, in); 223 } 224 225 extern float4 __attribute__((overloadable)) 226 rsMatrixMultiply(rs_matrix4x4 *m, float2 in) { 227 return rsMatrixMultiply((const rs_matrix4x4 *)m, in); 228 } 229 230 extern float3 __attribute__((overloadable)) 231 rsMatrixMultiply(rs_matrix3x3 *m, float3 in) { 232 return rsMatrixMultiply((const rs_matrix3x3 *)m, in); 233 } 234 235 extern float3 __attribute__((overloadable)) 236 rsMatrixMultiply(rs_matrix3x3 *m, float2 in) { 237 return rsMatrixMultiply((const rs_matrix3x3 *)m, in); 238 } 239 240 extern void __attribute__((overloadable)) 241 rsMatrixLoadMultiply(rs_matrix4x4 *ret, const rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs) { 242 for (int i=0 ; i<4 ; i++) { 243 float ri0 = 0; 244 float ri1 = 0; 245 float ri2 = 0; 246 float ri3 = 0; 247 for (int j=0 ; j<4 ; j++) { 248 const float rhs_ij = rsMatrixGet(rhs, i, j); 249 ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij; 250 ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij; 251 ri2 += rsMatrixGet(lhs, j, 2) * rhs_ij; 252 ri3 += rsMatrixGet(lhs, j, 3) * rhs_ij; 253 } 254 rsMatrixSet(ret, i, 0, ri0); 255 rsMatrixSet(ret, i, 1, ri1); 256 rsMatrixSet(ret, i, 2, ri2); 257 rsMatrixSet(ret, i, 3, ri3); 258 } 259 } 260 261 extern void __attribute__((overloadable)) 262 rsMatrixMultiply(rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs) { 263 rs_matrix4x4 r; 264 rsMatrixLoadMultiply(&r, lhs, rhs); 265 rsMatrixLoad(lhs, &r); 266 } 267 268 extern void __attribute__((overloadable)) 269 rsMatrixLoadMultiply(rs_matrix3x3 *ret, const rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs) { 270 for (int i=0 ; i<3 ; i++) { 271 float ri0 = 0; 272 float ri1 = 0; 273 float ri2 = 0; 274 for (int j=0 ; j<3 ; j++) { 275 const float rhs_ij = rsMatrixGet(rhs, i, j); 276 ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij; 277 ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij; 278 ri2 += rsMatrixGet(lhs, j, 2) * rhs_ij; 279 } 280 rsMatrixSet(ret, i, 0, ri0); 281 rsMatrixSet(ret, i, 1, ri1); 282 rsMatrixSet(ret, i, 2, ri2); 283 } 284 } 285 286 extern void __attribute__((overloadable)) 287 rsMatrixMultiply(rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs) { 288 rs_matrix3x3 r; 289 rsMatrixLoadMultiply(&r, lhs, rhs); 290 rsMatrixLoad(lhs, &r); 291 } 292 293 extern void __attribute__((overloadable)) 294 rsMatrixLoadMultiply(rs_matrix2x2 *ret, const rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs) { 295 for (int i=0 ; i<2 ; i++) { 296 float ri0 = 0; 297 float ri1 = 0; 298 for (int j=0 ; j<2 ; j++) { 299 const float rhs_ij = rsMatrixGet(rhs, i, j); 300 ri0 += rsMatrixGet(lhs, j, 0) * rhs_ij; 301 ri1 += rsMatrixGet(lhs, j, 1) * rhs_ij; 302 } 303 rsMatrixSet(ret, i, 0, ri0); 304 rsMatrixSet(ret, i, 1, ri1); 305 } 306 } 307 308 extern void __attribute__((overloadable)) 309 rsMatrixMultiply(rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs) { 310 rs_matrix2x2 r; 311 rsMatrixLoadMultiply(&r, lhs, rhs); 312 rsMatrixLoad(lhs, &r); 313 } 314 315