rs_math.rsh revision c5184e202ced435258adb2cfe2013570e7190954
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Don't edit this file!  It is auto-generated by frameworks/rs/api/gen_runtime.
18
19/*
20 * rs_math.rsh: TODO Add documentation
21 *
22 * TODO Add documentation
23 */
24#ifndef RENDERSCRIPT_RS_MATH_RSH
25#define RENDERSCRIPT_RS_MATH_RSH
26
27/*
28 * rsClamp: Restrain a value to a range
29 *
30 * Clamp a value between low and high.
31 *
32 * Deprecated.  Use clamp() instead.
33 *
34 * Parameters:
35 *   amount The value to clamp
36 *   low Lower bound
37 *   high Upper bound
38 */
39extern char __attribute__((const, always_inline, overloadable))
40    rsClamp(char amount, char low, char high);
41
42extern uchar __attribute__((const, always_inline, overloadable))
43    rsClamp(uchar amount, uchar low, uchar high);
44
45extern short __attribute__((const, always_inline, overloadable))
46    rsClamp(short amount, short low, short high);
47
48extern ushort __attribute__((const, always_inline, overloadable))
49    rsClamp(ushort amount, ushort low, ushort high);
50
51extern int __attribute__((const, always_inline, overloadable))
52    rsClamp(int amount, int low, int high);
53
54extern uint __attribute__((const, always_inline, overloadable))
55    rsClamp(uint amount, uint low, uint high);
56
57/*
58 * Computes 6 frustum planes from the view projection matrix
59 *
60 * Parameters:
61 *   viewProj matrix to extract planes from
62 *   left left plane
63 *   right right plane
64 *   top top plane
65 *   bottom bottom plane
66 *   near near plane
67 *   far far plane
68 */
69static inline void __attribute__((always_inline, overloadable))
70    rsExtractFrustumPlanes(const rs_matrix4x4* viewProj, float4* left, float4* right, float4* top,
71                           float4* bottom, float4* near, float4* far) {
72    // x y z w = a b c d in the plane equation
73    left->x = viewProj->m[3] + viewProj->m[0];
74    left->y = viewProj->m[7] + viewProj->m[4];
75    left->z = viewProj->m[11] + viewProj->m[8];
76    left->w = viewProj->m[15] + viewProj->m[12];
77
78    right->x = viewProj->m[3] - viewProj->m[0];
79    right->y = viewProj->m[7] - viewProj->m[4];
80    right->z = viewProj->m[11] - viewProj->m[8];
81    right->w = viewProj->m[15] - viewProj->m[12];
82
83    top->x = viewProj->m[3] - viewProj->m[1];
84    top->y = viewProj->m[7] - viewProj->m[5];
85    top->z = viewProj->m[11] - viewProj->m[9];
86    top->w = viewProj->m[15] - viewProj->m[13];
87
88    bottom->x = viewProj->m[3] + viewProj->m[1];
89    bottom->y = viewProj->m[7] + viewProj->m[5];
90    bottom->z = viewProj->m[11] + viewProj->m[9];
91    bottom->w = viewProj->m[15] + viewProj->m[13];
92
93    near->x = viewProj->m[3] + viewProj->m[2];
94    near->y = viewProj->m[7] + viewProj->m[6];
95    near->z = viewProj->m[11] + viewProj->m[10];
96    near->w = viewProj->m[15] + viewProj->m[14];
97
98    far->x = viewProj->m[3] - viewProj->m[2];
99    far->y = viewProj->m[7] - viewProj->m[6];
100    far->z = viewProj->m[11] - viewProj->m[10];
101    far->w = viewProj->m[15] - viewProj->m[14];
102
103    float len = length(left->xyz);
104    *left /= len;
105    len = length(right->xyz);
106    *right /= len;
107    len = length(top->xyz);
108    *top /= len;
109    len = length(bottom->xyz);
110    *bottom /= len;
111    len = length(near->xyz);
112    *near /= len;
113    len = length(far->xyz);
114    *far /= len;
115}
116
117/*
118 * Returns the fractional part of a float
119 */
120extern float __attribute__((const, overloadable))
121    rsFrac(float v);
122
123/*
124 * Checks if a sphere is withing the 6 frustum planes
125 *
126 * Parameters:
127 *   sphere float4 representing the sphere
128 *   left left plane
129 *   right right plane
130 *   top top plane
131 *   bottom bottom plane
132 *   near near plane
133 *   far far plane
134 */
135static inline bool __attribute__((always_inline, overloadable))
136    rsIsSphereInFrustum(float4* sphere, float4* left, float4* right, float4* top, float4* bottom,
137                        float4* near, float4* far) {
138    float distToCenter = dot(left->xyz, sphere->xyz) + left->w;
139    if (distToCenter < -sphere->w) {
140        return false;
141    }
142    distToCenter = dot(right->xyz, sphere->xyz) + right->w;
143    if (distToCenter < -sphere->w) {
144        return false;
145    }
146    distToCenter = dot(top->xyz, sphere->xyz) + top->w;
147    if (distToCenter < -sphere->w) {
148        return false;
149    }
150    distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w;
151    if (distToCenter < -sphere->w) {
152        return false;
153    }
154    distToCenter = dot(near->xyz, sphere->xyz) + near->w;
155    if (distToCenter < -sphere->w) {
156        return false;
157    }
158    distToCenter = dot(far->xyz, sphere->xyz) + far->w;
159    if (distToCenter < -sphere->w) {
160        return false;
161    }
162    return true;
163}
164
165/*
166 * Pack floating point (0-1) RGB values into a uchar4.
167 *
168 * For the float3 variant and the variant that only specifies r, g, b,
169 * the alpha component is set to 255 (1.0).
170 */
171extern uchar4 __attribute__((const, overloadable))
172    rsPackColorTo8888(float r, float g, float b);
173
174extern uchar4 __attribute__((const, overloadable))
175    rsPackColorTo8888(float r, float g, float b, float a);
176
177extern uchar4 __attribute__((const, overloadable))
178    rsPackColorTo8888(float3 color);
179
180extern uchar4 __attribute__((const, overloadable))
181    rsPackColorTo8888(float4 color);
182
183/*
184 * Return a random value between 0 (or min_value) and max_malue.
185 */
186extern int __attribute__((overloadable))
187    rsRand(int max_value);
188
189extern int __attribute__((overloadable))
190    rsRand(int min_value, int max_value);
191
192extern float __attribute__((overloadable))
193    rsRand(float max_value);
194
195extern float __attribute__((overloadable))
196    rsRand(float min_value, float max_value);
197
198/*
199 * Unpack a uchar4 color to float4.  The resulting float range will be (0-1).
200 */
201extern float4 __attribute__((const))
202    rsUnpackColor8888(uchar4 c);
203
204/*
205 * Convert from YUV to RGBA.
206 */
207extern float4 __attribute__((const, overloadable))
208    rsYuvToRGBA_float4(uchar y, uchar u, uchar v);
209
210extern uchar4 __attribute__((const, overloadable))
211    rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v);
212
213#endif // RENDERSCRIPT_RS_MATH_RSH
214