blob: e615afb66a0b07b06515d40a21af36eef0f81d18 [file] [log] [blame]
Ying Wang77643ba2011-01-18 22:24:26 -08001#include "rs_types.rsh"
Shih-wei Liao39ebe2c2011-01-28 11:22:32 -08002#include "rs_cl.rsh"
Shih-wei Liaof9728b92011-01-17 18:04:33 -08003
Stephen Hinesd4740c52011-08-08 15:04:52 -07004/*****************************************************************************
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 *****************************************************************************/
21typedef 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 Liao50cdf8f2011-01-26 14:10:25 -080040/* Declaration of 4 basic functions in libRS */
41extern void __attribute__((overloadable))
42 rsDebug(const char *, float, float);
43extern void __attribute__((overloadable))
44 rsDebug(const char *, float, float, float);
45extern void __attribute__((overloadable))
46 rsDebug(const char *, float, float, float, float);
Shih-wei Liaof9728b92011-01-17 18:04:33 -080047extern float4 __attribute__((overloadable)) convert_float4(uchar4 c);
48
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -080049/* Implementation of Core Runtime */
50
51extern void __attribute__((overloadable)) rsDebug(const char *s, float2 v) {
52 rsDebug(s, v.x, v.y);
53}
54
55extern void __attribute__((overloadable)) rsDebug(const char *s, float3 v) {
56 rsDebug(s, v.x, v.y, v.z);
57}
58
59extern void __attribute__((overloadable)) rsDebug(const char *s, float4 v) {
60 rsDebug(s, v.x, v.y, v.z, v.w);
61}
62
63extern 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
73extern 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
83extern 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
90extern 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
97extern 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
108extern void __attribute__((overloadable))
109rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v) {
110 m->m[row * 4 + col] = v;
111}
112
113extern float __attribute__((overloadable))
114rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col) {
115 return m->m[row * 4 + col];
116}
117
118extern void __attribute__((overloadable))
119rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v) {
120 m->m[row * 3 + col] = v;
121}
122
123extern float __attribute__((overloadable))
124rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col) {
125 return m->m[row * 3 + col];
126}
127
128extern void __attribute__((overloadable))
129rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v) {
130 m->m[row * 2 + col] = v;
131}
132
133extern float __attribute__((overloadable))
134rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col) {
135 return m->m[row * 2 + col];
136}
137
138
139extern float4 __attribute__((overloadable))
Jason Samsf9931b82011-07-26 17:28:38 -0700140rsMatrixMultiply(const rs_matrix4x4 *m, float4 in) {
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800141 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 Samsf9931b82011-07-26 17:28:38 -0700148extern float4 __attribute__((overloadable))
149rsMatrixMultiply(rs_matrix4x4 *m, float4 in) {
150 return rsMatrixMultiply((const rs_matrix4x4 *)m, in);
151}
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800152
153extern float4 __attribute__((overloadable))
Jason Samsf9931b82011-07-26 17:28:38 -0700154rsMatrixMultiply(const rs_matrix4x4 *m, float3 in) {
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800155 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 Samsf9931b82011-07-26 17:28:38 -0700162extern float4 __attribute__((overloadable))
163rsMatrixMultiply(rs_matrix4x4 *m, float3 in) {
164 return rsMatrixMultiply((const rs_matrix4x4 *)m, in);
165}
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800166
167extern float4 __attribute__((overloadable))
Jason Samsf9931b82011-07-26 17:28:38 -0700168rsMatrixMultiply(const rs_matrix4x4 *m, float2 in) {
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800169 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 Samsf9931b82011-07-26 17:28:38 -0700176extern float4 __attribute__((overloadable))
177rsMatrixMultiply(rs_matrix4x4 *m, float2 in) {
178 return rsMatrixMultiply((const rs_matrix4x4 *)m, in);
179}
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800180
181extern float3 __attribute__((overloadable))
Jason Samsf9931b82011-07-26 17:28:38 -0700182rsMatrixMultiply(const rs_matrix3x3 *m, float3 in) {
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800183 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 Samsf9931b82011-07-26 17:28:38 -0700189extern float3 __attribute__((overloadable))
190rsMatrixMultiply(rs_matrix3x3 *m, float3 in) {
191 return rsMatrixMultiply((const rs_matrix3x3 *)m, in);
192}
193
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800194
195extern float3 __attribute__((overloadable))
Jason Samsf9931b82011-07-26 17:28:38 -0700196rsMatrixMultiply(const rs_matrix3x3 *m, float2 in) {
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800197 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 Samsf9931b82011-07-26 17:28:38 -0700203extern float3 __attribute__((overloadable))
204rsMatrixMultiply(rs_matrix3x3 *m, float2 in) {
205 return rsMatrixMultiply((const rs_matrix3x3 *)m, in);
206}
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800207
208extern float2 __attribute__((overloadable))
Jason Samsf9931b82011-07-26 17:28:38 -0700209rsMatrixMultiply(const rs_matrix2x2 *m, float2 in) {
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800210 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 Samsf9931b82011-07-26 17:28:38 -0700215extern float2 __attribute__((overloadable))
216rsMatrixMultiply(rs_matrix2x2 *m, float2 in) {
217 return rsMatrixMultiply((const rs_matrix2x2 *)m, in);
218}
Shih-wei Liao50cdf8f2011-01-26 14:10:25 -0800219
220/////////////////////////////////////////////////////
221// int ops
222/////////////////////////////////////////////////////
223
224extern uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high) {
225 return amount < low ? low : (amount > high ? high : amount);
226}
227extern int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high) {
228 return amount < low ? low : (amount > high ? high : amount);
229}
230extern ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high) {
231 return amount < low ? low : (amount > high ? high : amount);
232}
233extern short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high) {
234 return amount < low ? low : (amount > high ? high : amount);
235}
236extern uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high) {
237 return amount < low ? low : (amount > high ? high : amount);
238}
239extern char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high) {
240 return amount < low ? low : (amount > high ? high : amount);
241}
Stephen Hinesd4740c52011-08-08 15:04:52 -0700242
243// Opaque Allocation type operations
244extern 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
250extern 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
256extern 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
262extern 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
268extern 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
274extern 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
282extern 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
291extern 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