state.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* libs/opengles/state.cpp
  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. #include <stdlib.h>
  18. #include "context.h"
  19. #include "fp.h"
  20. #include "state.h"
  21. #include "array.h"
  22. #include "matrix.h"
  23. #include "vertex.h"
  24. #include "light.h"
  25. #include "texture.h"
  26. #include "BufferObjectManager.h"
  27. #include "TextureObjectManager.h"
  28. namespace android {
  29. // ----------------------------------------------------------------------------
  30. static char const * const gVendorString = "Android";
  31. static char const * const gRendererString = "Android PixelFlinger 1.4";
  32. static char const * const gVersionString = "OpenGL ES-CM 1.0";
  33. static char const * const gExtensionsString =
  34. "GL_OES_byte_coordinates " // OK
  35. "GL_OES_fixed_point " // OK
  36. "GL_OES_single_precision " // OK
  37. "GL_OES_read_format " // OK
  38. "GL_OES_compressed_paletted_texture " // OK
  39. "GL_OES_draw_texture " // OK
  40. "GL_OES_matrix_get " // OK
  41. "GL_OES_query_matrix " // OK
  42. // "GL_OES_point_size_array " // TODO
  43. // "GL_OES_point_sprite " // TODO
  44. "GL_OES_EGL_image " // OK
  45. "GL_OES_EGL_sync " // OK
  46. #ifdef GL_OES_compressed_ETC1_RGB8_texture
  47. "GL_OES_compressed_ETC1_RGB8_texture " // OK
  48. #endif
  49. "GL_ARB_texture_compression " // OK
  50. "GL_ARB_texture_non_power_of_two " // OK
  51. "GL_ANDROID_user_clip_plane " // OK
  52. "GL_ANDROID_vertex_buffer_object " // OK
  53. "GL_ANDROID_generate_mipmap " // OK
  54. ;
  55. // ----------------------------------------------------------------------------
  56. #if 0
  57. #pragma mark -
  58. #endif
  59. ogles_context_t *ogles_init(size_t extra)
  60. {
  61. void* const base = malloc(extra + sizeof(ogles_context_t) + 32);
  62. if (!base) return 0;
  63. ogles_context_t *c =
  64. (ogles_context_t *)((ptrdiff_t(base) + extra + 31) & ~0x1FL);
  65. memset(c, 0, sizeof(ogles_context_t));
  66. ggl_init_context(&(c->rasterizer));
  67. // XXX: this should be passed as an argument
  68. sp<EGLSurfaceManager> smgr(new EGLSurfaceManager());
  69. c->surfaceManager = smgr.get();
  70. c->surfaceManager->incStrong(c);
  71. sp<EGLBufferObjectManager> bomgr(new EGLBufferObjectManager());
  72. c->bufferObjectManager = bomgr.get();
  73. c->bufferObjectManager->incStrong(c);
  74. ogles_init_array(c);
  75. ogles_init_matrix(c);
  76. ogles_init_vertex(c);
  77. ogles_init_light(c);
  78. ogles_init_texture(c);
  79. c->rasterizer.base = base;
  80. c->point.size = TRI_ONE;
  81. c->line.width = TRI_ONE;
  82. // in OpenGL, writing to the depth buffer is enabled by default.
  83. c->rasterizer.procs.depthMask(c, 1);
  84. // OpenGL enables dithering by default
  85. c->rasterizer.procs.enable(c, GL_DITHER);
  86. return c;
  87. }
  88. void ogles_uninit(ogles_context_t* c)
  89. {
  90. ogles_uninit_array(c);
  91. ogles_uninit_matrix(c);
  92. ogles_uninit_vertex(c);
  93. ogles_uninit_light(c);
  94. ogles_uninit_texture(c);
  95. c->surfaceManager->decStrong(c);
  96. c->bufferObjectManager->decStrong(c);
  97. ggl_uninit_context(&(c->rasterizer));
  98. free(c->rasterizer.base);
  99. }
  100. void _ogles_error(ogles_context_t* c, GLenum error)
  101. {
  102. if (c->error == GL_NO_ERROR)
  103. c->error = error;
  104. }
  105. static bool stencilop_valid(GLenum op) {
  106. switch (op) {
  107. case GL_KEEP:
  108. case GL_ZERO:
  109. case GL_REPLACE:
  110. case GL_INCR:
  111. case GL_DECR:
  112. case GL_INVERT:
  113. return true;
  114. }
  115. return false;
  116. }
  117. static void enable_disable(ogles_context_t* c, GLenum cap, int enabled)
  118. {
  119. if ((cap >= GL_LIGHT0) && (cap<GL_LIGHT0+OGLES_MAX_LIGHTS)) {
  120. c->lighting.lights[cap-GL_LIGHT0].enable = enabled;
  121. c->lighting.enabledLights &= ~(1<<(cap-GL_LIGHT0));
  122. c->lighting.enabledLights |= (enabled<<(cap-GL_LIGHT0));
  123. return;
  124. }
  125. switch (cap) {
  126. case GL_POINT_SMOOTH:
  127. c->point.smooth = enabled;
  128. break;
  129. case GL_LINE_SMOOTH:
  130. c->line.smooth = enabled;
  131. break;
  132. case GL_POLYGON_OFFSET_FILL:
  133. c->polygonOffset.enable = enabled;
  134. break;
  135. case GL_CULL_FACE:
  136. c->cull.enable = enabled;
  137. break;
  138. case GL_LIGHTING:
  139. c->lighting.enable = enabled;
  140. break;
  141. case GL_COLOR_MATERIAL:
  142. c->lighting.colorMaterial.enable = enabled;
  143. break;
  144. case GL_NORMALIZE:
  145. case GL_RESCALE_NORMAL:
  146. c->transforms.rescaleNormals = enabled ? cap : 0;
  147. // XXX: invalidate mvit
  148. break;
  149. case GL_CLIP_PLANE0:
  150. case GL_CLIP_PLANE1:
  151. case GL_CLIP_PLANE2:
  152. case GL_CLIP_PLANE3:
  153. case GL_CLIP_PLANE4:
  154. case GL_CLIP_PLANE5:
  155. c->clipPlanes.enable &= ~(1<<(cap-GL_CLIP_PLANE0));
  156. c->clipPlanes.enable |= (enabled<<(cap-GL_CLIP_PLANE0));
  157. ogles_invalidate_perspective(c);
  158. break;
  159. case GL_FOG:
  160. case GL_DEPTH_TEST:
  161. ogles_invalidate_perspective(c);
  162. // fall-through...
  163. case GL_BLEND:
  164. case GL_SCISSOR_TEST:
  165. case GL_ALPHA_TEST:
  166. case GL_COLOR_LOGIC_OP:
  167. case GL_DITHER:
  168. case GL_STENCIL_TEST:
  169. case GL_TEXTURE_2D:
  170. // these need to fall through into the rasterizer
  171. c->rasterizer.procs.enableDisable(c, cap, enabled);
  172. break;
  173. case GL_TEXTURE_EXTERNAL_OES:
  174. c->rasterizer.procs.enableDisable(c, GL_TEXTURE_2D, enabled);
  175. break;
  176. case GL_MULTISAMPLE:
  177. case GL_SAMPLE_ALPHA_TO_COVERAGE:
  178. case GL_SAMPLE_ALPHA_TO_ONE:
  179. case GL_SAMPLE_COVERAGE:
  180. // not supported in this implementation
  181. break;
  182. default:
  183. ogles_error(c, GL_INVALID_ENUM);
  184. return;
  185. }
  186. }
  187. // ----------------------------------------------------------------------------
  188. }; // namespace android
  189. // ----------------------------------------------------------------------------
  190. using namespace android;
  191. #if 0
  192. #pragma mark -
  193. #endif
  194. // These ones are super-easy, we're not supporting those features!
  195. void glSampleCoverage(GLclampf /*value*/, GLboolean /*invert*/) {
  196. }
  197. void glSampleCoveragex(GLclampx /*value*/, GLboolean /*invert*/) {
  198. }
  199. void glStencilFunc(GLenum func, GLint /*ref*/, GLuint /*mask*/) {
  200. ogles_context_t* c = ogles_context_t::get();
  201. if (func < GL_NEVER || func > GL_ALWAYS) {
  202. ogles_error(c, GL_INVALID_ENUM);
  203. return;
  204. }
  205. // from OpenGL|ES 1.0 sepcification:
  206. // If there is no stencil buffer, no stencil modification can occur
  207. // and it is as if the stencil test always passes.
  208. }
  209. void glStencilOp(GLenum fail, GLenum zfail, GLenum zpass) {
  210. ogles_context_t* c = ogles_context_t::get();
  211. if ((stencilop_valid(fail) &
  212. stencilop_valid(zfail) &
  213. stencilop_valid(zpass)) == 0) {
  214. ogles_error(c, GL_INVALID_ENUM);
  215. return;
  216. }
  217. }
  218. // ----------------------------------------------------------------------------
  219. void glAlphaFunc(GLenum func, GLclampf ref)
  220. {
  221. glAlphaFuncx(func, gglFloatToFixed(ref));
  222. }
  223. void glCullFace(GLenum mode)
  224. {
  225. ogles_context_t* c = ogles_context_t::get();
  226. switch (mode) {
  227. case GL_FRONT:
  228. case GL_BACK:
  229. case GL_FRONT_AND_BACK:
  230. break;
  231. default:
  232. ogles_error(c, GL_INVALID_ENUM);
  233. }
  234. c->cull.cullFace = mode;
  235. }
  236. void glFrontFace(GLenum mode)
  237. {
  238. ogles_context_t* c = ogles_context_t::get();
  239. switch (mode) {
  240. case GL_CW:
  241. case GL_CCW:
  242. break;
  243. default:
  244. ogles_error(c, GL_INVALID_ENUM);
  245. return;
  246. }
  247. c->cull.frontFace = mode;
  248. }
  249. void glHint(GLenum target, GLenum mode)
  250. {
  251. ogles_context_t* c = ogles_context_t::get();
  252. switch (target) {
  253. case GL_FOG_HINT:
  254. case GL_GENERATE_MIPMAP_HINT:
  255. case GL_LINE_SMOOTH_HINT:
  256. break;
  257. case GL_POINT_SMOOTH_HINT:
  258. c->rasterizer.procs.enableDisable(c,
  259. GGL_POINT_SMOOTH_NICE, mode==GL_NICEST);
  260. break;
  261. case GL_PERSPECTIVE_CORRECTION_HINT:
  262. c->perspective = (mode == GL_NICEST) ? 1 : 0;
  263. break;
  264. default:
  265. ogles_error(c, GL_INVALID_ENUM);
  266. }
  267. }
  268. void glEnable(GLenum cap) {
  269. ogles_context_t* c = ogles_context_t::get();
  270. enable_disable(c, cap, 1);
  271. }
  272. void glDisable(GLenum cap) {
  273. ogles_context_t* c = ogles_context_t::get();
  274. enable_disable(c, cap, 0);
  275. }
  276. void glFinish()
  277. { // nothing to do for our software implementation
  278. }
  279. void glFlush()
  280. { // nothing to do for our software implementation
  281. }
  282. GLenum glGetError()
  283. {
  284. // From OpenGL|ES 1.0 specification:
  285. // If more than one flag has recorded an error, glGetError returns
  286. // and clears an arbitrary error flag value. Thus, glGetError should
  287. // always be called in a loop, until it returns GL_NO_ERROR,
  288. // if all error flags are to be reset.
  289. ogles_context_t* c = ogles_context_t::get();
  290. if (c->error) {
  291. const GLenum ret(c->error);
  292. c->error = 0;
  293. return ret;
  294. }
  295. if (c->rasterizer.error) {
  296. const GLenum ret(c->rasterizer.error);
  297. c->rasterizer.error = 0;
  298. return ret;
  299. }
  300. return GL_NO_ERROR;
  301. }
  302. const GLubyte* glGetString(GLenum string)
  303. {
  304. switch (string) {
  305. case GL_VENDOR: return (const GLubyte*)gVendorString;
  306. case GL_RENDERER: return (const GLubyte*)gRendererString;
  307. case GL_VERSION: return (const GLubyte*)gVersionString;
  308. case GL_EXTENSIONS: return (const GLubyte*)gExtensionsString;
  309. }
  310. ogles_context_t* c = ogles_context_t::get();
  311. ogles_error(c, GL_INVALID_ENUM);
  312. return 0;
  313. }
  314. void glGetIntegerv(GLenum pname, GLint *params)
  315. {
  316. int i;
  317. ogles_context_t* c = ogles_context_t::get();
  318. switch (pname) {
  319. case GL_ALIASED_POINT_SIZE_RANGE:
  320. params[0] = 0;
  321. params[1] = GGL_MAX_ALIASED_POINT_SIZE;
  322. break;
  323. case GL_ALIASED_LINE_WIDTH_RANGE:
  324. params[0] = 0;
  325. params[1] = GGL_MAX_ALIASED_POINT_SIZE;
  326. break;
  327. case GL_ALPHA_BITS: {
  328. int index = c->rasterizer.state.buffers.color.format;
  329. GGLFormat const * formats = gglGetPixelFormatTable();
  330. params[0] = formats[index].ah - formats[index].al;
  331. break;
  332. }
  333. case GL_RED_BITS: {
  334. int index = c->rasterizer.state.buffers.color.format;
  335. GGLFormat const * formats = gglGetPixelFormatTable();
  336. params[0] = formats[index].rh - formats[index].rl;
  337. break;
  338. }
  339. case GL_GREEN_BITS: {
  340. int index = c->rasterizer.state.buffers.color.format;
  341. GGLFormat const * formats = gglGetPixelFormatTable();
  342. params[0] = formats[index].gh - formats[index].gl;
  343. break;
  344. }
  345. case GL_BLUE_BITS: {
  346. int index = c->rasterizer.state.buffers.color.format;
  347. GGLFormat const * formats = gglGetPixelFormatTable();
  348. params[0] = formats[index].bh - formats[index].bl;
  349. break;
  350. }
  351. case GL_COMPRESSED_TEXTURE_FORMATS:
  352. params[ 0] = GL_PALETTE4_RGB8_OES;
  353. params[ 1] = GL_PALETTE4_RGBA8_OES;
  354. params[ 2] = GL_PALETTE4_R5_G6_B5_OES;
  355. params[ 3] = GL_PALETTE4_RGBA4_OES;
  356. params[ 4] = GL_PALETTE4_RGB5_A1_OES;
  357. params[ 5] = GL_PALETTE8_RGB8_OES;
  358. params[ 6] = GL_PALETTE8_RGBA8_OES;
  359. params[ 7] = GL_PALETTE8_R5_G6_B5_OES;
  360. params[ 8] = GL_PALETTE8_RGBA4_OES;
  361. params[ 9] = GL_PALETTE8_RGB5_A1_OES;
  362. i = 10;
  363. #ifdef GL_OES_compressed_ETC1_RGB8_texture
  364. params[i++] = GL_ETC1_RGB8_OES;
  365. #endif
  366. break;
  367. case GL_DEPTH_BITS:
  368. params[0] = c->rasterizer.state.buffers.depth.format ? 0 : 16;
  369. break;
  370. case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
  371. params[0] = GL_RGB;
  372. break;
  373. case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
  374. params[0] = GL_UNSIGNED_SHORT_5_6_5;
  375. break;
  376. case GL_MAX_LIGHTS:
  377. params[0] = OGLES_MAX_LIGHTS;
  378. break;
  379. case GL_MAX_CLIP_PLANES:
  380. params[0] = OGLES_MAX_CLIP_PLANES;
  381. break;
  382. case GL_MAX_MODELVIEW_STACK_DEPTH:
  383. params[0] = OGLES_MODELVIEW_STACK_DEPTH;
  384. break;
  385. case GL_MAX_PROJECTION_STACK_DEPTH:
  386. params[0] = OGLES_PROJECTION_STACK_DEPTH;
  387. break;
  388. case GL_MAX_TEXTURE_STACK_DEPTH:
  389. params[0] = OGLES_TEXTURE_STACK_DEPTH;
  390. break;
  391. case GL_MAX_TEXTURE_SIZE:
  392. params[0] = GGL_MAX_TEXTURE_SIZE;
  393. break;
  394. case GL_MAX_TEXTURE_UNITS:
  395. params[0] = GGL_TEXTURE_UNIT_COUNT;
  396. break;
  397. case GL_MAX_VIEWPORT_DIMS:
  398. params[0] = GGL_MAX_VIEWPORT_DIMS;
  399. params[1] = GGL_MAX_VIEWPORT_DIMS;
  400. break;
  401. case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
  402. params[0] = OGLES_NUM_COMPRESSED_TEXTURE_FORMATS;
  403. break;
  404. case GL_SMOOTH_LINE_WIDTH_RANGE:
  405. params[0] = 0;
  406. params[1] = GGL_MAX_SMOOTH_LINE_WIDTH;
  407. break;
  408. case GL_SMOOTH_POINT_SIZE_RANGE:
  409. params[0] = 0;
  410. params[1] = GGL_MAX_SMOOTH_POINT_SIZE;
  411. break;
  412. case GL_STENCIL_BITS:
  413. params[0] = 0;
  414. break;
  415. case GL_SUBPIXEL_BITS:
  416. params[0] = GGL_SUBPIXEL_BITS;
  417. break;
  418. case GL_MODELVIEW_MATRIX_FLOAT_AS_INT_BITS_OES:
  419. memcpy( params,
  420. c->transforms.modelview.top().elements(),
  421. 16*sizeof(GLint));
  422. break;
  423. case GL_PROJECTION_MATRIX_FLOAT_AS_INT_BITS_OES:
  424. memcpy( params,
  425. c->transforms.projection.top().elements(),
  426. 16*sizeof(GLint));
  427. break;
  428. case GL_TEXTURE_MATRIX_FLOAT_AS_INT_BITS_OES:
  429. memcpy( params,
  430. c->transforms.texture[c->textures.active].top().elements(),
  431. 16*sizeof(GLint));
  432. break;
  433. default:
  434. ogles_error(c, GL_INVALID_ENUM);
  435. break;
  436. }
  437. }
  438. // ----------------------------------------------------------------------------
  439. void glPointSize(GLfloat size)
  440. {
  441. ogles_context_t* c = ogles_context_t::get();
  442. if (size <= 0) {
  443. ogles_error(c, GL_INVALID_ENUM);
  444. return;
  445. }
  446. c->point.size = TRI_FROM_FIXED(gglFloatToFixed(size));
  447. }
  448. void glPointSizex(GLfixed size)
  449. {
  450. ogles_context_t* c = ogles_context_t::get();
  451. if (size <= 0) {
  452. ogles_error(c, GL_INVALID_ENUM);
  453. return;
  454. }
  455. c->point.size = TRI_FROM_FIXED(size);
  456. }
  457. // ----------------------------------------------------------------------------
  458. void glLineWidth(GLfloat width)
  459. {
  460. ogles_context_t* c = ogles_context_t::get();
  461. if (width <= 0) {
  462. ogles_error(c, GL_INVALID_ENUM);
  463. return;
  464. }
  465. c->line.width = TRI_FROM_FIXED(gglFloatToFixed(width));
  466. }
  467. void glLineWidthx(GLfixed width)
  468. {
  469. ogles_context_t* c = ogles_context_t::get();
  470. if (width <= 0) {
  471. ogles_error(c, GL_INVALID_ENUM);
  472. return;
  473. }
  474. c->line.width = TRI_FROM_FIXED(width);
  475. }
  476. // ----------------------------------------------------------------------------
  477. void glColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a) {
  478. ogles_context_t* c = ogles_context_t::get();
  479. c->rasterizer.procs.colorMask(c, r, g, b, a);
  480. }
  481. void glDepthMask(GLboolean flag) {
  482. ogles_context_t* c = ogles_context_t::get();
  483. c->rasterizer.procs.depthMask(c, flag);
  484. }
  485. void glStencilMask(GLuint mask) {
  486. ogles_context_t* c = ogles_context_t::get();
  487. c->rasterizer.procs.stencilMask(c, mask);
  488. }
  489. void glDepthFunc(GLenum func) {
  490. ogles_context_t* c = ogles_context_t::get();
  491. c->rasterizer.procs.depthFunc(c, func);
  492. }
  493. void glLogicOp(GLenum opcode) {
  494. ogles_context_t* c = ogles_context_t::get();
  495. c->rasterizer.procs.logicOp(c, opcode);
  496. }
  497. void glAlphaFuncx(GLenum func, GLclampx ref) {
  498. ogles_context_t* c = ogles_context_t::get();
  499. c->rasterizer.procs.alphaFuncx(c, func, ref);
  500. }
  501. void glBlendFunc(GLenum sfactor, GLenum dfactor) {
  502. ogles_context_t* c = ogles_context_t::get();
  503. c->rasterizer.procs.blendFunc(c, sfactor, dfactor);
  504. }
  505. void glClear(GLbitfield mask) {
  506. ogles_context_t* c = ogles_context_t::get();
  507. c->rasterizer.procs.clear(c, mask);
  508. }
  509. void glClearColorx(GLclampx red, GLclampx green, GLclampx blue, GLclampx alpha) {
  510. ogles_context_t* c = ogles_context_t::get();
  511. c->rasterizer.procs.clearColorx(c, red, green, blue, alpha);
  512. }
  513. void glClearColor(GLclampf r, GLclampf g, GLclampf b, GLclampf a)
  514. {
  515. ogles_context_t* c = ogles_context_t::get();
  516. c->rasterizer.procs.clearColorx(c,
  517. gglFloatToFixed(r),
  518. gglFloatToFixed(g),
  519. gglFloatToFixed(b),
  520. gglFloatToFixed(a));
  521. }
  522. void glClearDepthx(GLclampx depth) {
  523. ogles_context_t* c = ogles_context_t::get();
  524. c->rasterizer.procs.clearDepthx(c, depth);
  525. }
  526. void glClearDepthf(GLclampf depth)
  527. {
  528. ogles_context_t* c = ogles_context_t::get();
  529. c->rasterizer.procs.clearDepthx(c, gglFloatToFixed(depth));
  530. }
  531. void glClearStencil(GLint s) {
  532. ogles_context_t* c = ogles_context_t::get();
  533. c->rasterizer.procs.clearStencil(c, s);
  534. }