Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #include <GLES2/gl2.h>
  2. #include <EGL/egl.h>
  3. #include <EGL/eglext.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <stdlib.h>
  8.  
  9. #define N_TEXTURES 40
  10.  
  11. static const EGLint attribs[] = {
  12. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  13. EGL_RED_SIZE, 1,
  14. EGL_GREEN_SIZE, 1,
  15. EGL_BLUE_SIZE, 1,
  16. EGL_DEPTH_SIZE, 1,
  17. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  18. EGL_NONE
  19. };
  20.  
  21. static const EGLint context_attribs[] ={
  22. EGL_CONTEXT_CLIENT_VERSION, 2,
  23. EGL_NONE
  24. };
  25.  
  26. struct egl_state {
  27. Display *dpy;
  28. Window win;
  29. EGLDisplay egl_dpy;
  30. EGLConfig cfg;
  31. EGLContext ctx;
  32. EGLSurface surf;
  33. EGLint major, minor;
  34. int depth;
  35. int width;
  36. int height;
  37. };
  38.  
  39. static void
  40. create_window (struct egl_state *state)
  41. {
  42. XSetWindowAttributes window_attr;
  43. XVisualInfo template, *vinfo;
  44. EGLint id;
  45. unsigned long mask;
  46. int screen = DefaultScreen (state->dpy);
  47. Window root_win = RootWindow (state->dpy, screen);
  48. int count;
  49.  
  50. if (!eglGetConfigAttrib (state->egl_dpy,
  51. state->cfg, EGL_NATIVE_VISUAL_ID, &id))
  52. {
  53. fprintf (stderr, "eglGetConfigAttrib() failed\n");
  54. exit (1);
  55. }
  56.  
  57. template.visualid = id;
  58. vinfo = XGetVisualInfo (state->dpy, VisualIDMask, &template, &count);
  59. if (count != 1)
  60. {
  61. fprintf (stderr, "XGetVisualInfo() failed\n");
  62. exit (1);
  63. }
  64.  
  65. state->depth = vinfo->depth;
  66. window_attr.background_pixel = 0;
  67. window_attr.border_pixel = 0;
  68. window_attr.colormap = XCreateColormap (state->dpy, root_win,
  69. vinfo->visual, AllocNone);
  70. window_attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  71. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  72. state->win = XCreateWindow (state->dpy, root_win, 0, 0,
  73. state->width, state->height,
  74. 0, vinfo->depth, InputOutput,
  75. vinfo->visual, mask, &window_attr);
  76.  
  77. XMapWindow (state->dpy, state->win);
  78.  
  79. XFree (vinfo);
  80. }
  81.  
  82. static void
  83. run_test (void)
  84. {
  85. GLuint textures[N_TEXTURES];
  86. int i;
  87.  
  88. /* Create all of the textures straight up */
  89. for (i = 0; i < N_TEXTURES; i++)
  90. {
  91. glGenTextures (1, textures + i);
  92. glBindTexture (GL_TEXTURE_2D, textures[i]);
  93. glTexParameteri (GL_TEXTURE_2D,
  94. GL_TEXTURE_MIN_FILTER,
  95. GL_NEAREST);
  96. glTexImage2D (GL_TEXTURE_2D, 0,
  97. GL_RGBA,
  98. 128, 128,
  99. 0,
  100. GL_RGBA,
  101. GL_UNSIGNED_BYTE,
  102. NULL);
  103. }
  104.  
  105. /* Bind and destroy an FBO to each one in turn */
  106. for (i = 0; i < N_TEXTURES; i++)
  107. {
  108. GLuint fbo;
  109. GLenum status;
  110.  
  111. glGenFramebuffers (1, &fbo);
  112. glBindFramebuffer (GL_FRAMEBUFFER, fbo);
  113. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  114. GL_TEXTURE_2D, textures[i], 0);
  115.  
  116. status = glCheckFramebufferStatus (GL_FRAMEBUFFER);
  117.  
  118. printf ("status of FBO %i = 0x%x (%s)\n",
  119. i,
  120. status,
  121. status == GL_FRAMEBUFFER_COMPLETE ? "complete" :
  122. status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
  123. ? "incomplete attachment" :
  124. status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
  125. ? "missing attachment" :
  126. "?");
  127.  
  128. glDeleteFramebuffers (1, &fbo);
  129. }
  130.  
  131. glDeleteTextures (N_TEXTURES, textures);
  132. }
  133.  
  134. int
  135. main (int argc, char **argv)
  136. {
  137. struct egl_state state;
  138. int count;
  139.  
  140. state.dpy = XOpenDisplay (NULL);
  141. if (state.dpy == NULL)
  142. {
  143. fprintf (stderr, "couldn't open display\n");
  144. return EXIT_FAILURE;
  145. }
  146.  
  147. state.egl_dpy = eglGetDisplay (state.dpy);
  148. if (state.egl_dpy == EGL_NO_DISPLAY)
  149. {
  150. fprintf (stderr, "eglGetDisplay() failed\n");
  151. return EXIT_FAILURE;
  152. }
  153.  
  154. if (!eglInitialize (state.egl_dpy, &state.major, &state.minor))
  155. {
  156. fprintf (stderr, "eglInitialize() failed\n");
  157. return EXIT_FAILURE;
  158. }
  159.  
  160. if (!eglChooseConfig (state.egl_dpy, attribs, &state.cfg, 1, &count) ||
  161. count == 0)
  162. {
  163. fprintf (stderr, "eglChooseConfig() failed\n");
  164. return EXIT_FAILURE;
  165. }
  166.  
  167. state.ctx = eglCreateContext (state.egl_dpy, state.cfg,
  168. EGL_NO_CONTEXT, context_attribs);
  169. if (state.ctx == EGL_NO_CONTEXT)
  170. {
  171. fprintf (stderr, "eglCreateContext() failed\n");
  172. return EXIT_FAILURE;
  173. }
  174.  
  175. state.width = 300;
  176. state.height = 300;
  177. create_window (&state);
  178.  
  179. state.surf = eglCreateWindowSurface (state.egl_dpy,
  180. state.cfg, state.win, NULL);
  181. if (state.surf == EGL_NO_SURFACE)
  182. {
  183. fprintf (stderr, "eglCreateWindowSurface() failed\n");
  184. return EXIT_FAILURE;
  185. }
  186.  
  187. if (!eglMakeCurrent (state.egl_dpy, state.surf, state.surf, state.ctx))
  188. {
  189. fprintf (stderr, "eglMakeCurrent() failed\n");
  190. return EXIT_FAILURE;
  191. }
  192.  
  193. run_test ();
  194.  
  195. eglTerminate (state.egl_dpy);
  196.  
  197. return EXIT_SUCCESS;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement