gl-x11.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include <X11/Xlib.h>
  2. #include <stdio.h>
  3. #include "gl-subsystem.h"
  4. #include <GL/glx.h>
  5. #include <GL/glxext.h>
  6. static PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
  7. static const GLenum fb_attribs[] = {
  8. /* Hardcoded for now... */
  9. GLX_STENCIL_SIZE, 8,
  10. GLX_DEPTH_SIZE, 24,
  11. GLX_BUFFER_SIZE, 32, /* Color buffer depth */
  12. GLX_DOUBLEBUFFER, True,
  13. None
  14. };
  15. static const GLenum ctx_attribs[] = {
  16. #ifdef _DEBUG
  17. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
  18. #endif
  19. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  20. None,
  21. };
  22. #define ERROR_TEXT_LEN 1024
  23. struct gl_windowinfo {
  24. uint32_t id;
  25. Display *display;
  26. };
  27. struct gl_platform {
  28. GLXContext context;
  29. struct gs_swap_chain swap;
  30. };
  31. static int GLXErrorHandler(Display *disp, XErrorEvent *error)
  32. {
  33. char error_buf[ERROR_TEXT_LEN];
  34. XGetErrorText(disp, error->error_code, error_buf, ERROR_TEXT_LEN);
  35. blog(LOG_ERROR, "GLX error: %s\n", error_buf);
  36. return 0;
  37. }
  38. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  39. {
  40. return &platform->swap;
  41. }
  42. extern struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  43. {
  44. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  45. memset(wi, 0, sizeof(struct gl_windowinfo));
  46. wi->id = info->window.id;
  47. return wi;
  48. }
  49. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  50. {
  51. bfree(wi);
  52. }
  53. extern void gl_getclientsize(struct gs_swap_chain *swap,
  54. uint32_t *width, uint32_t *height)
  55. {
  56. XWindowAttributes info = { 0 };
  57. XGetWindowAttributes(swap->wi->display, swap->wi->id, &info);
  58. *height = info.height;
  59. *width = info.width;
  60. }
  61. static void print_info_stuff(struct gs_init_data *info)
  62. {
  63. blog( LOG_INFO,
  64. "X and Y: %i %i\n"
  65. "Backbuffers: %i\n"
  66. "Color Format: %i\n"
  67. "ZStencil Format: %i\n"
  68. "Adapter: %i\n",
  69. info->cx, info->cy,
  70. info->num_backbuffers,
  71. info->format, info->zsformat,
  72. info->adapter
  73. );
  74. }
  75. struct gl_platform *gl_platform_create(device_t device,
  76. struct gs_init_data *info)
  77. {
  78. int num_configs = 0;
  79. int error_base = 0, event_base = 0;
  80. Display *display = XOpenDisplay(NULL); /* Open default screen */
  81. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  82. GLXFBConfig* configs;
  83. print_info_stuff(info);
  84. memset(plat, 0, sizeof(struct gl_platform));
  85. if (!display) {
  86. blog(LOG_ERROR, "Unable to find display. DISPLAY variable may not be set correctly.");
  87. goto fail0;
  88. }
  89. if (!glXQueryExtension(display, &error_base, &event_base)) {
  90. blog(LOG_ERROR, "GLX not supported.");
  91. goto fail0;
  92. }
  93. XSetErrorHandler(GLXErrorHandler);
  94. /* We require glX version 1.4 */
  95. {
  96. int major = 0, minor = 0;
  97. glXQueryVersion(display, &major, &minor);
  98. if (major < 1 || minor < 4) {
  99. blog(LOG_ERROR, "GLX version found: %i.%i\nRequired: 1.4", major, minor);
  100. goto fail0;
  101. }
  102. }
  103. glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress("glXCreateContextAttribsARB");
  104. if (!glXCreateContextAttribsARB) {
  105. blog(LOG_ERROR, "ARB_GLX_create_context not supported!");
  106. goto fail0;
  107. }
  108. configs = glXChooseFBConfig(display, DefaultScreen(display), fb_attribs, &num_configs);
  109. if(!configs) {
  110. blog(LOG_ERROR, "Attribute list or screen is invalid.");
  111. goto fail0;
  112. }
  113. if(num_configs == 0) {
  114. blog(LOG_ERROR, "No framebuffer configurations found.");
  115. goto fail1;
  116. }
  117. /* We just use the first configuration found... as it does everything we want at the very least. */
  118. plat->context = glXCreateContextAttribsARB(display, configs[0], NULL, True, ctx_attribs);
  119. if(!plat->context) {
  120. blog(LOG_ERROR, "Failed to create OpenGL context.");
  121. goto fail1;
  122. }
  123. if(!glXMakeCurrent(display, info->window.id, plat->context)) {
  124. blog(LOG_ERROR, "Failed to make context current.");
  125. goto fail2;
  126. }
  127. /* Initialize GLEW */
  128. {
  129. glewExperimental = true;
  130. GLenum err = glewInit();
  131. if (GLEW_OK != err) {
  132. blog(LOG_ERROR, glewGetErrorString(err));
  133. goto fail2;
  134. }
  135. }
  136. blog(LOG_INFO, "OpenGL version: %s\n", glGetString(GL_VERSION));
  137. plat->swap.device = device;
  138. plat->swap.info = *info;
  139. plat->swap.wi = gl_windowinfo_create(info);
  140. plat->swap.wi->display = display;
  141. XFree(configs);
  142. XSync(display, False);
  143. return plat;
  144. fail2:
  145. glXMakeCurrent(display, None, NULL);
  146. glXDestroyContext(display, plat->context);
  147. fail1:
  148. XFree(configs);
  149. fail0:
  150. bfree(plat);
  151. return NULL;
  152. }
  153. extern void gl_platform_destroy(struct gl_platform *platform)
  154. {
  155. if (!platform)
  156. return;
  157. glXMakeCurrent(platform->swap.wi->display, None, NULL);
  158. glXDestroyContext(platform->swap.wi->display, platform->context);
  159. XCloseDisplay(platform->swap.wi->display);
  160. gl_windowinfo_destroy(platform->swap.wi);
  161. bfree(platform);
  162. }
  163. void device_entercontext(device_t device)
  164. {
  165. GLXContext context = device->plat->context;
  166. XID window = device->plat->swap.wi->id;
  167. Display *display = device->plat->swap.wi->display;
  168. if (device->cur_swap)
  169. device->plat->swap.wi->id = device->cur_swap->wi->id;
  170. if (!glXMakeCurrent(display, window, context)) {
  171. blog(LOG_ERROR, "Failed to make context current.");
  172. }
  173. }
  174. void device_leavecontext(device_t device)
  175. {
  176. Display *display = device->plat->swap.wi->display;
  177. if(!glXMakeCurrent(display, None, NULL)) {
  178. blog(LOG_ERROR, "Failed to reset current context.");
  179. }
  180. }
  181. void gl_update(device_t device)
  182. {
  183. /* I don't know of anything we can do here. */
  184. }
  185. void device_load_swapchain(device_t device, swapchain_t swap)
  186. {
  187. if(!swap)
  188. swap = &device->plat->swap;
  189. device->cur_swap = swap;
  190. }
  191. void device_present(device_t device)
  192. {
  193. Display *display = device->plat->swap.wi->display;
  194. XID window = device->plat->swap.wi->id;
  195. glXSwapBuffers(display, window);
  196. }