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