Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getteximage.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * Test glGetTexImage()
  3. * Brian Paul
  4. * 9 June 2009
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #include <GL/glew.h>
  10. #include <GL/glut.h>
  11. static int Win;
  12. static void
  13. TestGetTexImage(GLboolean npot)
  14. {
  15. GLuint iter;
  16. GLubyte *data = (GLubyte *) malloc(1024 * 1024 * 4);
  17. GLubyte *data2 = (GLubyte *) malloc(1024 * 1024 * 4);
  18. glEnable(GL_TEXTURE_2D);
  19. printf("glTexImage2D + glGetTexImage:\n");
  20. for (iter = 0; iter < 8; iter++) {
  21. GLint p = (iter % 8) + 3;
  22. GLint w = npot ? (p * 20) : (1 << p);
  23. GLint h = npot ? (p * 10) : (1 << p);
  24. GLuint i;
  25. GLint level = 0;
  26. printf(" Testing %d x %d tex image\n", w, h);
  27. /* fill data */
  28. for (i = 0; i < w * h * 4; i++) {
  29. data[i] = i & 0xff;
  30. data2[i] = 0;
  31. }
  32. glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0,
  33. GL_RGBA, GL_UNSIGNED_BYTE, data);
  34. glBegin(GL_POINTS);
  35. glVertex2f(0, 0);
  36. glEnd();
  37. /* get */
  38. glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
  39. /* compare */
  40. for (i = 0; i < w * h * 4; i++) {
  41. if (data2[i] != data[i]) {
  42. printf("glTexImage + glGetTexImage failure!\n");
  43. printf("Expected value %d, found %d\n", data[i], data2[i]);
  44. abort();
  45. }
  46. }
  47. /* get as BGRA */
  48. glGetTexImage(GL_TEXTURE_2D, level, GL_BGRA, GL_UNSIGNED_BYTE, data2);
  49. /* compare */
  50. {
  51. const GLubyte *rgba = (GLubyte *) data;
  52. const GLubyte *bgra = (GLubyte *) data2;
  53. for (i = 0; i < w * h; i += 4) {
  54. if (rgba[i+0] != bgra[i+2] ||
  55. rgba[i+1] != bgra[i+1] ||
  56. rgba[i+2] != bgra[i+0] ||
  57. rgba[i+3] != bgra[i+3]) {
  58. printf("glTexImage + glGetTexImage(GL_BGRA) failure!\n");
  59. printf("Expected value %d, found %d\n", data[i], data2[i]);
  60. abort();
  61. }
  62. }
  63. }
  64. }
  65. printf("Passed\n");
  66. glDisable(GL_TEXTURE_2D);
  67. free(data);
  68. free(data2);
  69. }
  70. static GLboolean
  71. ColorsEqual(const GLubyte ref[4], const GLubyte act[4])
  72. {
  73. if (abs((int) ref[0] - (int) act[0]) > 1 ||
  74. abs((int) ref[1] - (int) act[1]) > 1 ||
  75. abs((int) ref[2] - (int) act[2]) > 1 ||
  76. abs((int) ref[3] - (int) act[3]) > 1) {
  77. printf("expected %d %d %d %d\n", ref[0], ref[1], ref[2], ref[3]);
  78. printf("found %d %d %d %d\n", act[0], act[1], act[2], act[3]);
  79. return GL_FALSE;
  80. }
  81. return GL_TRUE;
  82. }
  83. static void
  84. TestGetTexImageRTT(GLboolean npot)
  85. {
  86. GLuint iter;
  87. printf("Render to texture + glGetTexImage:\n");
  88. for (iter = 0; iter < 8; iter++) {
  89. GLuint fb, tex;
  90. GLint w, h;
  91. GLint level = 0;
  92. if (npot) {
  93. w = 200 + iter * 40;
  94. h = 200 + iter * 12;
  95. }
  96. else {
  97. w = 4 << iter;
  98. h = 4 << iter;
  99. }
  100. glGenTextures(1, &tex);
  101. glGenFramebuffersEXT(1, &fb);
  102. glBindTexture(GL_TEXTURE_2D, tex);
  103. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  104. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  105. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
  106. GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  107. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
  108. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  109. GL_TEXTURE_2D, tex, level);
  110. glViewport(0, 0, w, h);
  111. printf(" Testing %d x %d tex image\n", w, h);
  112. {
  113. static const GLubyte blue[4] = {0, 0, 255, 255};
  114. GLubyte color[4];
  115. GLubyte *data2 = (GLubyte *) malloc(w * h * 4);
  116. GLuint i;
  117. /* random clear color */
  118. for (i = 0; i < 4; i++) {
  119. color[i] = rand() % 256;
  120. }
  121. glClearColor(color[0] / 255.0,
  122. color[1] / 255.0,
  123. color[2] / 255.0,
  124. color[3] / 255.0);
  125. glClear(GL_COLOR_BUFFER_BIT);
  126. /* draw polygon over top half, in blue */
  127. glColor4ubv(blue);
  128. glRectf(0, 0.5, 1.0, 1.0);
  129. /* get */
  130. glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
  131. /* compare */
  132. for (i = 0; i < w * h; i += 4) {
  133. if (i < w * h / 2) {
  134. /* lower half */
  135. if (!ColorsEqual(color, data2 + i * 4)) {
  136. printf("Render to texture failure (expected clear color)!\n");
  137. abort();
  138. }
  139. }
  140. else {
  141. /* upper half */
  142. if (!ColorsEqual(blue, data2 + i * 4)) {
  143. printf("Render to texture failure (expected blue)!\n");
  144. abort();
  145. }
  146. }
  147. }
  148. free(data2);
  149. }
  150. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  151. glDeleteFramebuffersEXT(1, &fb);
  152. glDeleteTextures(1, &tex);
  153. }
  154. printf("Passed\n");
  155. }
  156. static void
  157. Draw(void)
  158. {
  159. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  160. TestGetTexImage(GL_FALSE);
  161. if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
  162. TestGetTexImage(GL_TRUE);
  163. if (glutExtensionSupported("GL_EXT_framebuffer_object") ||
  164. glutExtensionSupported("GL_ARB_framebuffer_object")) {
  165. TestGetTexImageRTT(GL_FALSE);
  166. if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
  167. TestGetTexImageRTT(GL_TRUE);
  168. }
  169. glutDestroyWindow(Win);
  170. exit(0);
  171. glutSwapBuffers();
  172. }
  173. static void
  174. Reshape(int width, int height)
  175. {
  176. glViewport(0, 0, width, height);
  177. glMatrixMode(GL_PROJECTION);
  178. glLoadIdentity();
  179. glOrtho(0, 1, 0, 1, -1, 1);
  180. glMatrixMode(GL_MODELVIEW);
  181. glLoadIdentity();
  182. glTranslatef(0.0, 0.0, 0.0);
  183. }
  184. static void
  185. Key(unsigned char key, int x, int y)
  186. {
  187. (void) x;
  188. (void) y;
  189. switch (key) {
  190. case 27:
  191. glutDestroyWindow(Win);
  192. exit(0);
  193. break;
  194. }
  195. glutPostRedisplay();
  196. }
  197. static void
  198. Init(void)
  199. {
  200. }
  201. int
  202. main(int argc, char *argv[])
  203. {
  204. glutInit(&argc, argv);
  205. glutInitWindowPosition(0, 0);
  206. glutInitWindowSize(400, 400);
  207. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  208. Win = glutCreateWindow(argv[0]);
  209. glewInit();
  210. glutReshapeFunc(Reshape);
  211. glutKeyboardFunc(Key);
  212. glutDisplayFunc(Draw);
  213. Init();
  214. glutMainLoop();
  215. return 0;
  216. }