1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
#ifndef __RS_CORE_RSH__
#define __RS_CORE_RSH__
#define _RS_RUNTIME extern
// Debugging, print to the LOG a description string and a value.
extern void __attribute__((overloadable))
rsDebug(const char *, float);
extern void __attribute__((overloadable))
rsDebug(const char *, float, float);
extern void __attribute__((overloadable))
rsDebug(const char *, float, float, float);
extern void __attribute__((overloadable))
rsDebug(const char *, float, float, float, float);
extern void __attribute__((overloadable))
rsDebug(const char *, double);
extern void __attribute__((overloadable))
rsDebug(const char *, const rs_matrix4x4 *);
extern void __attribute__((overloadable))
rsDebug(const char *, const rs_matrix3x3 *);
extern void __attribute__((overloadable))
rsDebug(const char *, const rs_matrix2x2 *);
extern void __attribute__((overloadable))
rsDebug(const char *, int);
extern void __attribute__((overloadable))
rsDebug(const char *, uint);
extern void __attribute__((overloadable))
rsDebug(const char *, long);
extern void __attribute__((overloadable))
rsDebug(const char *, unsigned long);
extern void __attribute__((overloadable))
rsDebug(const char *, long long);
extern void __attribute__((overloadable))
rsDebug(const char *, unsigned long long);
extern void __attribute__((overloadable))
rsDebug(const char *, const void *);
#define RS_DEBUG(a) rsDebug(#a, a)
#define RS_DEBUG_MARKER rsDebug(__FILE__, __LINE__)
_RS_RUNTIME void __attribute__((overloadable)) rsDebug(const char *s, float2 v);
_RS_RUNTIME void __attribute__((overloadable)) rsDebug(const char *s, float3 v);
_RS_RUNTIME void __attribute__((overloadable)) rsDebug(const char *s, float4 v);
_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b);
_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a);
_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color);
_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color);
_RS_RUNTIME float4 rsUnpackColor8888(uchar4 c);
//extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float r, float g, float b);
//extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float3);
//extern float4 rsUnpackColor565(uchar4);
/////////////////////////////////////////////////////
// Matrix ops
/////////////////////////////////////////////////////
_RS_RUNTIME void __attribute__((overloadable))
rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v);
_RS_RUNTIME float __attribute__((overloadable))
rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col);
_RS_RUNTIME void __attribute__((overloadable))
rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v);
_RS_RUNTIME float __attribute__((overloadable))
rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col);
_RS_RUNTIME void __attribute__((overloadable))
rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v);
_RS_RUNTIME float __attribute__((overloadable))
rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col);
extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix4x4 *m);
extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix3x3 *m);
extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix2x2 *m);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const float *v);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix3x3 *m, const float *v);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix2x2 *m, const float *v);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix4x4 *v);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix3x3 *v);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix2x2 *v);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix3x3 *m, const rs_matrix3x3 *v);
extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix2x2 *m, const rs_matrix2x2 *v);
extern void __attribute__((overloadable))
rsMatrixLoadRotate(rs_matrix4x4 *m, float rot, float x, float y, float z);
extern void __attribute__((overloadable))
rsMatrixLoadScale(rs_matrix4x4 *m, float x, float y, float z);
extern void __attribute__((overloadable))
rsMatrixLoadTranslate(rs_matrix4x4 *m, float x, float y, float z);
extern void __attribute__((overloadable))
rsMatrixLoadMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs);
extern void __attribute__((overloadable))
rsMatrixMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *rhs);
extern void __attribute__((overloadable))
rsMatrixLoadMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs);
extern void __attribute__((overloadable))
rsMatrixMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *rhs);
extern void __attribute__((overloadable))
rsMatrixLoadMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs);
extern void __attribute__((overloadable))
rsMatrixMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *rhs);
extern void __attribute__((overloadable))
rsMatrixRotate(rs_matrix4x4 *m, float rot, float x, float y, float z);
extern void __attribute__((overloadable))
rsMatrixScale(rs_matrix4x4 *m, float x, float y, float z);
extern void __attribute__((overloadable))
rsMatrixTranslate(rs_matrix4x4 *m, float x, float y, float z);
extern void __attribute__((overloadable))
rsMatrixLoadOrtho(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far);
extern void __attribute__((overloadable))
rsMatrixLoadFrustum(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far);
extern void __attribute__((overloadable))
rsMatrixLoadPerspective(rs_matrix4x4* m, float fovy, float aspect, float near, float far);
_RS_RUNTIME float4 __attribute__((overloadable))
rsMatrixMultiply(rs_matrix4x4 *m, float4 in);
_RS_RUNTIME float4 __attribute__((overloadable))
rsMatrixMultiply(rs_matrix4x4 *m, float3 in);
_RS_RUNTIME float4 __attribute__((overloadable))
rsMatrixMultiply(rs_matrix4x4 *m, float2 in);
_RS_RUNTIME float3 __attribute__((overloadable))
rsMatrixMultiply(rs_matrix3x3 *m, float3 in);
_RS_RUNTIME float3 __attribute__((overloadable))
rsMatrixMultiply(rs_matrix3x3 *m, float2 in);
_RS_RUNTIME float2 __attribute__((overloadable))
rsMatrixMultiply(rs_matrix2x2 *m, float2 in);
// Returns true if the matrix was successfully inversed
extern bool __attribute__((overloadable)) rsMatrixInverse(rs_matrix4x4 *m);
extern bool __attribute__((overloadable)) rsMatrixInverseTranspose(rs_matrix4x4 *m);
extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix4x4 *m);
extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix3x3 *m);
extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix2x2 *m);
/////////////////////////////////////////////////////
// quaternion ops
/////////////////////////////////////////////////////
_RS_STATIC void __attribute__((overloadable))
rsQuaternionSet(rs_quaternion *q, float w, float x, float y, float z) {
q->w = w;
q->x = x;
q->y = y;
q->z = z;
}
_RS_STATIC void __attribute__((overloadable))
rsQuaternionSet(rs_quaternion *q, const rs_quaternion *rhs) {
q->w = rhs->w;
q->x = rhs->x;
q->y = rhs->y;
q->z = rhs->z;
}
_RS_STATIC void __attribute__((overloadable))
rsQuaternionMultiply(rs_quaternion *q, float s) {
q->w *= s;
q->x *= s;
q->y *= s;
q->z *= s;
}
_RS_STATIC void __attribute__((overloadable))
rsQuaternionMultiply(rs_quaternion *q, const rs_quaternion *rhs) {
q->w = -q->x*rhs->x - q->y*rhs->y - q->z*rhs->z + q->w*rhs->w;
q->x = q->x*rhs->w + q->y*rhs->z - q->z*rhs->y + q->w*rhs->x;
q->y = -q->x*rhs->z + q->y*rhs->w + q->z*rhs->x + q->w*rhs->y;
q->z = q->x*rhs->y - q->y*rhs->x + q->z*rhs->w + q->w*rhs->z;
}
_RS_STATIC void
rsQuaternionAdd(rs_quaternion *q, const rs_quaternion *rhs) {
q->w *= rhs->w;
q->x *= rhs->x;
q->y *= rhs->y;
q->z *= rhs->z;
}
_RS_STATIC void
rsQuaternionLoadRotateUnit(rs_quaternion *q, float rot, float x, float y, float z) {
rot *= (float)(M_PI / 180.0f) * 0.5f;
float c = cos(rot);
float s = sin(rot);
q->w = c;
q->x = x * s;
q->y = y * s;
q->z = z * s;
}
_RS_STATIC void
rsQuaternionLoadRotate(rs_quaternion *q, float rot, float x, float y, float z) {
const float len = x*x + y*y + z*z;
if (len != 1) {
const float recipLen = 1.f / sqrt(len);
x *= recipLen;
y *= recipLen;
z *= recipLen;
}
rsQuaternionLoadRotateUnit(q, rot, x, y, z);
}
_RS_STATIC void
rsQuaternionConjugate(rs_quaternion *q) {
q->x = -q->x;
q->y = -q->y;
q->z = -q->z;
}
_RS_STATIC float
rsQuaternionDot(const rs_quaternion *q0, const rs_quaternion *q1) {
return q0->w*q1->w + q0->x*q1->x + q0->y*q1->y + q0->z*q1->z;
}
_RS_STATIC void
rsQuaternionNormalize(rs_quaternion *q) {
const float len = rsQuaternionDot(q, q);
if (len != 1) {
const float recipLen = 1.f / sqrt(len);
rsQuaternionMultiply(q, recipLen);
}
}
_RS_STATIC void
rsQuaternionSlerp(rs_quaternion *q, const rs_quaternion *q0, const rs_quaternion *q1, float t) {
if (t <= 0.0f) {
rsQuaternionSet(q, q0);
return;
}
if (t >= 1.0f) {
rsQuaternionSet(q, q1);
return;
}
rs_quaternion tempq0, tempq1;
rsQuaternionSet(&tempq0, q0);
rsQuaternionSet(&tempq1, q1);
float angle = rsQuaternionDot(q0, q1);
if (angle < 0) {
rsQuaternionMultiply(&tempq0, -1.0f);
angle *= -1.0f;
}
float scale, invScale;
if (angle + 1.0f > 0.05f) {
if (1.0f - angle >= 0.05f) {
float theta = acos(angle);
float invSinTheta = 1.0f / sin(theta);
scale = sin(theta * (1.0f - t)) * invSinTheta;
invScale = sin(theta * t) * invSinTheta;
} else {
scale = 1.0f - t;
invScale = t;
}
} else {
rsQuaternionSet(&tempq1, tempq0.z, -tempq0.y, tempq0.x, -tempq0.w);
scale = sin(M_PI * (0.5f - t));
invScale = sin(M_PI * t);
}
rsQuaternionSet(q, tempq0.w*scale + tempq1.w*invScale, tempq0.x*scale + tempq1.x*invScale,
tempq0.y*scale + tempq1.y*invScale, tempq0.z*scale + tempq1.z*invScale);
}
_RS_STATIC void rsQuaternionGetMatrixUnit(rs_matrix4x4 *m, const rs_quaternion *q) {
float x2 = 2.0f * q->x * q->x;
float y2 = 2.0f * q->y * q->y;
float z2 = 2.0f * q->z * q->z;
float xy = 2.0f * q->x * q->y;
float wz = 2.0f * q->w * q->z;
float xz = 2.0f * q->x * q->z;
float wy = 2.0f * q->w * q->y;
float wx = 2.0f * q->w * q->x;
float yz = 2.0f * q->y * q->z;
m->m[0] = 1.0f - y2 - z2;
m->m[1] = xy - wz;
m->m[2] = xz + wy;
m->m[3] = 0.0f;
m->m[4] = xy + wz;
m->m[5] = 1.0f - x2 - z2;
m->m[6] = yz - wx;
m->m[7] = 0.0f;
m->m[8] = xz - wy;
m->m[9] = yz - wx;
m->m[10] = 1.0f - x2 - y2;
m->m[11] = 0.0f;
m->m[12] = 0.0f;
m->m[13] = 0.0f;
m->m[14] = 0.0f;
m->m[15] = 1.0f;
}
/////////////////////////////////////////////////////
// utility funcs
/////////////////////////////////////////////////////
__inline__ _RS_STATIC void __attribute__((overloadable, always_inline))
rsExtractFrustumPlanes(const rs_matrix4x4 *modelViewProj,
float4 *left, float4 *right,
float4 *top, float4 *bottom,
float4 *near, float4 *far) {
// x y z w = a b c d in the plane equation
left->x = modelViewProj->m[3] + modelViewProj->m[0];
left->y = modelViewProj->m[7] + modelViewProj->m[4];
left->z = modelViewProj->m[11] + modelViewProj->m[8];
left->w = modelViewProj->m[15] + modelViewProj->m[12];
right->x = modelViewProj->m[3] - modelViewProj->m[0];
right->y = modelViewProj->m[7] - modelViewProj->m[4];
right->z = modelViewProj->m[11] - modelViewProj->m[8];
right->w = modelViewProj->m[15] - modelViewProj->m[12];
top->x = modelViewProj->m[3] - modelViewProj->m[1];
top->y = modelViewProj->m[7] - modelViewProj->m[5];
top->z = modelViewProj->m[11] - modelViewProj->m[9];
top->w = modelViewProj->m[15] - modelViewProj->m[13];
bottom->x = modelViewProj->m[3] + modelViewProj->m[1];
bottom->y = modelViewProj->m[7] + modelViewProj->m[5];
bottom->z = modelViewProj->m[11] + modelViewProj->m[9];
bottom->w = modelViewProj->m[15] + modelViewProj->m[13];
near->x = modelViewProj->m[3] + modelViewProj->m[2];
near->y = modelViewProj->m[7] + modelViewProj->m[6];
near->z = modelViewProj->m[11] + modelViewProj->m[10];
near->w = modelViewProj->m[15] + modelViewProj->m[14];
far->x = modelViewProj->m[3] - modelViewProj->m[2];
far->y = modelViewProj->m[7] - modelViewProj->m[6];
far->z = modelViewProj->m[11] - modelViewProj->m[10];
far->w = modelViewProj->m[15] - modelViewProj->m[14];
float len = length(left->xyz);
*left /= len;
len = length(right->xyz);
*right /= len;
len = length(top->xyz);
*top /= len;
len = length(bottom->xyz);
*bottom /= len;
len = length(near->xyz);
*near /= len;
len = length(far->xyz);
*far /= len;
}
__inline__ _RS_STATIC bool __attribute__((overloadable, always_inline))
rsIsSphereInFrustum(float4 *sphere,
float4 *left, float4 *right,
float4 *top, float4 *bottom,
float4 *near, float4 *far) {
float distToCenter = dot(left->xyz, sphere->xyz) + left->w;
if (distToCenter < -sphere->w) {
return false;
}
distToCenter = dot(right->xyz, sphere->xyz) + right->w;
if (distToCenter < -sphere->w) {
return false;
}
distToCenter = dot(top->xyz, sphere->xyz) + top->w;
if (distToCenter < -sphere->w) {
return false;
}
distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w;
if (distToCenter < -sphere->w) {
return false;
}
distToCenter = dot(near->xyz, sphere->xyz) + near->w;
if (distToCenter < -sphere->w) {
return false;
}
distToCenter = dot(far->xyz, sphere->xyz) + far->w;
if (distToCenter < -sphere->w) {
return false;
}
return true;
}
/////////////////////////////////////////////////////
// int ops
/////////////////////////////////////////////////////
_RS_RUNTIME uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high);
_RS_RUNTIME int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high);
_RS_RUNTIME ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high);
_RS_RUNTIME short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
_RS_RUNTIME uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high);
_RS_RUNTIME char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high);
#undef _RS_RUNTIME
#endif
|