matrix.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* libs/opengles/matrix.h
  2. **
  3. ** Copyright 2006, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #ifndef ANDROID_OPENGLES_MATRIX_H
  18. #define ANDROID_OPENGLES_MATRIX_H
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #include <sys/types.h>
  22. #include <utils/Log.h>
  23. #include <private/pixelflinger/ggl_context.h>
  24. #include <GLES/gl.h>
  25. namespace android {
  26. const int OGLES_MODELVIEW_STACK_DEPTH = 16;
  27. const int OGLES_PROJECTION_STACK_DEPTH = 2;
  28. const int OGLES_TEXTURE_STACK_DEPTH = 2;
  29. void ogles_init_matrix(ogles_context_t*);
  30. void ogles_uninit_matrix(ogles_context_t*);
  31. void ogles_invalidate_perspective(ogles_context_t* c);
  32. void ogles_validate_transform_impl(ogles_context_t* c, uint32_t want);
  33. int ogles_surfaceport(ogles_context_t* c, GLint x, GLint y);
  34. void ogles_scissor(ogles_context_t* c,
  35. GLint x, GLint y, GLsizei w, GLsizei h);
  36. void ogles_viewport(ogles_context_t* c,
  37. GLint x, GLint y, GLsizei w, GLsizei h);
  38. inline void ogles_validate_transform(
  39. ogles_context_t* c, uint32_t want)
  40. {
  41. if (c->transforms.dirty & want)
  42. ogles_validate_transform_impl(c, want);
  43. }
  44. // ----------------------------------------------------------------------------
  45. inline
  46. GLfixed vsquare3(GLfixed a, GLfixed b, GLfixed c)
  47. {
  48. #if defined(__arm__) && !defined(__thumb__)
  49. GLfixed r;
  50. int32_t t;
  51. asm(
  52. "smull %0, %1, %2, %2 \n"
  53. "smlal %0, %1, %3, %3 \n"
  54. "smlal %0, %1, %4, %4 \n"
  55. "movs %0, %0, lsr #16 \n"
  56. "adc %0, %0, %1, lsl #16 \n"
  57. : "=&r"(r), "=&r"(t)
  58. : "%r"(a), "r"(b), "r"(c)
  59. : "cc"
  60. );
  61. return r;
  62. #elif defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
  63. GLfixed res;
  64. int32_t t1,t2,t3;
  65. asm(
  66. "mult %[a], %[a] \r\n"
  67. "li %[res],0x8000 \r\n"
  68. "madd %[b],%[b] \r\n"
  69. "move %[t3],$zero \r\n"
  70. "madd %[c],%[c] \r\n"
  71. "mflo %[t1]\r\n"
  72. "mfhi %[t2]\r\n"
  73. "addu %[t1],%[res],%[t1]\r\n" /*add 0x8000*/
  74. "sltu %[t3],%[t1],%[res]\r\n"
  75. "addu %[t2],%[t2],%[t3]\r\n"
  76. "srl %[res],%[t1],16\r\n"
  77. "sll %[t2],%[t2],16\r\n"
  78. "or %[res],%[res],%[t2]\r\n"
  79. : [res]"=&r"(res),[t1]"=&r"(t1),[t2]"=&r"(t2),[t3]"=&r"(t3)
  80. : [a] "r" (a),[b] "r" (b),[c] "r" (c)
  81. : "%hi","%lo"
  82. );
  83. return res;
  84. #else
  85. return (( int64_t(a)*a +
  86. int64_t(b)*b +
  87. int64_t(c)*c + 0x8000)>>16);
  88. #endif
  89. }
  90. static inline GLfixed mla2a( GLfixed a0, GLfixed b0,
  91. GLfixed a1, GLfixed b1,
  92. GLfixed c)
  93. {
  94. #if defined(__arm__) && !defined(__thumb__)
  95. GLfixed r;
  96. int32_t t;
  97. asm(
  98. "smull %0, %1, %2, %3 \n"
  99. "smlal %0, %1, %4, %5 \n"
  100. "add %0, %6, %0, lsr #16 \n"
  101. "add %0, %0, %1, lsl #16 \n"
  102. : "=&r"(r), "=&r"(t)
  103. : "%r"(a0), "r"(b0),
  104. "%r"(a1), "r"(b1),
  105. "r"(c)
  106. :
  107. );
  108. return r;
  109. #else
  110. return (( int64_t(a0)*b0 +
  111. int64_t(a1)*b1)>>16) + c;
  112. #endif
  113. }
  114. static inline GLfixed mla3a( GLfixed a0, GLfixed b0,
  115. GLfixed a1, GLfixed b1,
  116. GLfixed a2, GLfixed b2,
  117. GLfixed c)
  118. {
  119. #if defined(__arm__) && !defined(__thumb__)
  120. GLfixed r;
  121. int32_t t;
  122. asm(
  123. "smull %0, %1, %2, %3 \n"
  124. "smlal %0, %1, %4, %5 \n"
  125. "smlal %0, %1, %6, %7 \n"
  126. "add %0, %8, %0, lsr #16 \n"
  127. "add %0, %0, %1, lsl #16 \n"
  128. : "=&r"(r), "=&r"(t)
  129. : "%r"(a0), "r"(b0),
  130. "%r"(a1), "r"(b1),
  131. "%r"(a2), "r"(b2),
  132. "r"(c)
  133. :
  134. );
  135. return r;
  136. #elif defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
  137. GLfixed res;
  138. int32_t t1,t2;
  139. asm(
  140. "mult %[a0],%[b0] \r\n"
  141. "madd %[a1],%[b1] \r\n"
  142. "madd %[a2],%[b2] \r\n"
  143. "mflo %[t2]\r\n"
  144. "mfhi %[t1]\r\n"
  145. "srl %[t2],%[t2],16\r\n"
  146. "sll %[t1],%[t1],16\r\n"
  147. "or %[t2],%[t2],%[t1]\r\n"
  148. "addu %[res],%[t2],%[c]"
  149. : [res]"=&r"(res),[t1]"=&r"(t1),[t2]"=&r"(t2)
  150. : [a0] "r" (a0),[b0] "r" (b0),[a1] "r" (a1),[b1] "r" (b1),[a2] "r" (a2),[b2] "r" (b2),[c] "r" (c)
  151. : "%hi","%lo"
  152. );
  153. return res;
  154. #else
  155. return (( int64_t(a0)*b0 +
  156. int64_t(a1)*b1 +
  157. int64_t(a2)*b2)>>16) + c;
  158. #endif
  159. }
  160. // b0, b1, b2 are signed 16-bit quanities
  161. // that have been shifted right by 'shift' bits relative to normal
  162. // S16.16 fixed point
  163. static inline GLfixed mla3a16( GLfixed a0, int32_t b1b0,
  164. GLfixed a1,
  165. GLfixed a2, int32_t b2,
  166. GLint shift,
  167. GLfixed c)
  168. {
  169. #if defined(__arm__) && !defined(__thumb__)
  170. GLfixed r;
  171. asm(
  172. "smulwb %0, %1, %2 \n"
  173. "smlawt %0, %3, %2, %0 \n"
  174. "smlawb %0, %4, %5, %0 \n"
  175. "add %0, %7, %0, lsl %6 \n"
  176. : "=&r"(r)
  177. : "r"(a0), "r"(b1b0),
  178. "r"(a1),
  179. "r"(a2), "r"(b2),
  180. "r"(shift),
  181. "r"(c)
  182. :
  183. );
  184. return r;
  185. #else
  186. int32_t accum;
  187. int16_t b0 = b1b0 & 0xffff;
  188. int16_t b1 = (b1b0 >> 16) & 0xffff;
  189. accum = int64_t(a0)*int16_t(b0) >> 16;
  190. accum += int64_t(a1)*int16_t(b1) >> 16;
  191. accum += int64_t(a2)*int16_t(b2) >> 16;
  192. accum = (accum << shift) + c;
  193. return accum;
  194. #endif
  195. }
  196. static inline GLfixed mla3a16_btb( GLfixed a0,
  197. GLfixed a1,
  198. GLfixed a2,
  199. int32_t b1b0, int32_t xxb2,
  200. GLint shift,
  201. GLfixed c)
  202. {
  203. #if defined(__arm__) && !defined(__thumb__)
  204. GLfixed r;
  205. asm(
  206. "smulwb %0, %1, %4 \n"
  207. "smlawt %0, %2, %4, %0 \n"
  208. "smlawb %0, %3, %5, %0 \n"
  209. "add %0, %7, %0, lsl %6 \n"
  210. : "=&r"(r)
  211. : "r"(a0),
  212. "r"(a1),
  213. "r"(a2),
  214. "r"(b1b0), "r"(xxb2),
  215. "r"(shift),
  216. "r"(c)
  217. :
  218. );
  219. return r;
  220. #else
  221. int32_t accum;
  222. int16_t b0 = b1b0 & 0xffff;
  223. int16_t b1 = (b1b0 >> 16) & 0xffff;
  224. int16_t b2 = xxb2 & 0xffff;
  225. accum = int64_t(a0)*int16_t(b0) >> 16;
  226. accum += int64_t(a1)*int16_t(b1) >> 16;
  227. accum += int64_t(a2)*int16_t(b2) >> 16;
  228. accum = (accum << shift) + c;
  229. return accum;
  230. #endif
  231. }
  232. static inline GLfixed mla3a16_btt( GLfixed a0,
  233. GLfixed a1,
  234. GLfixed a2,
  235. int32_t b1b0, int32_t b2xx,
  236. GLint shift,
  237. GLfixed c)
  238. {
  239. #if defined(__arm__) && !defined(__thumb__)
  240. GLfixed r;
  241. asm(
  242. "smulwb %0, %1, %4 \n"
  243. "smlawt %0, %2, %4, %0 \n"
  244. "smlawt %0, %3, %5, %0 \n"
  245. "add %0, %7, %0, lsl %6 \n"
  246. : "=&r"(r)
  247. : "r"(a0),
  248. "r"(a1),
  249. "r"(a2),
  250. "r"(b1b0), "r"(b2xx),
  251. "r"(shift),
  252. "r"(c)
  253. :
  254. );
  255. return r;
  256. #else
  257. int32_t accum;
  258. int16_t b0 = b1b0 & 0xffff;
  259. int16_t b1 = (b1b0 >> 16) & 0xffff;
  260. int16_t b2 = (b2xx >> 16) & 0xffff;
  261. accum = int64_t(a0)*int16_t(b0) >> 16;
  262. accum += int64_t(a1)*int16_t(b1) >> 16;
  263. accum += int64_t(a2)*int16_t(b2) >> 16;
  264. accum = (accum << shift) + c;
  265. return accum;
  266. #endif
  267. }
  268. static inline GLfixed mla3( GLfixed a0, GLfixed b0,
  269. GLfixed a1, GLfixed b1,
  270. GLfixed a2, GLfixed b2)
  271. {
  272. #if defined(__arm__) && !defined(__thumb__)
  273. GLfixed r;
  274. int32_t t;
  275. asm(
  276. "smull %0, %1, %2, %3 \n"
  277. "smlal %0, %1, %4, %5 \n"
  278. "smlal %0, %1, %6, %7 \n"
  279. "movs %0, %0, lsr #16 \n"
  280. "adc %0, %0, %1, lsl #16 \n"
  281. : "=&r"(r), "=&r"(t)
  282. : "%r"(a0), "r"(b0),
  283. "%r"(a1), "r"(b1),
  284. "%r"(a2), "r"(b2)
  285. : "cc"
  286. );
  287. return r;
  288. #else
  289. return (( int64_t(a0)*b0 +
  290. int64_t(a1)*b1 +
  291. int64_t(a2)*b2 + 0x8000)>>16);
  292. #endif
  293. }
  294. static inline GLfixed mla4( GLfixed a0, GLfixed b0,
  295. GLfixed a1, GLfixed b1,
  296. GLfixed a2, GLfixed b2,
  297. GLfixed a3, GLfixed b3)
  298. {
  299. #if defined(__arm__) && !defined(__thumb__)
  300. GLfixed r;
  301. int32_t t;
  302. asm(
  303. "smull %0, %1, %2, %3 \n"
  304. "smlal %0, %1, %4, %5 \n"
  305. "smlal %0, %1, %6, %7 \n"
  306. "smlal %0, %1, %8, %9 \n"
  307. "movs %0, %0, lsr #16 \n"
  308. "adc %0, %0, %1, lsl #16 \n"
  309. : "=&r"(r), "=&r"(t)
  310. : "%r"(a0), "r"(b0),
  311. "%r"(a1), "r"(b1),
  312. "%r"(a2), "r"(b2),
  313. "%r"(a3), "r"(b3)
  314. : "cc"
  315. );
  316. return r;
  317. #else
  318. return (( int64_t(a0)*b0 +
  319. int64_t(a1)*b1 +
  320. int64_t(a2)*b2 +
  321. int64_t(a3)*b3 + 0x8000)>>16);
  322. #endif
  323. }
  324. inline
  325. GLfixed dot4(const GLfixed* a, const GLfixed* b)
  326. {
  327. return mla4(a[0], b[0], a[1], b[1], a[2], b[2], a[3], b[3]);
  328. }
  329. inline
  330. GLfixed dot3(const GLfixed* a, const GLfixed* b)
  331. {
  332. return mla3(a[0], b[0], a[1], b[1], a[2], b[2]);
  333. }
  334. }; // namespace android
  335. #endif // ANDROID_OPENGLES_MATRIX_H