gl-x11.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /******************************************************************************
  2. Copyright (C) 2014 by Zachary Lund <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <X11/Xlib.h>
  15. #include <stdio.h>
  16. #include "gl-subsystem.h"
  17. #include "GL/glx_obs.h"
  18. static const int fb_attribs[] = {
  19. /* Hardcoded for now... */
  20. GLX_STENCIL_SIZE, 8,
  21. GLX_DEPTH_SIZE, 24,
  22. GLX_BUFFER_SIZE, 32, /* Color buffer depth */
  23. GLX_DOUBLEBUFFER, True,
  24. None
  25. };
  26. static const int ctx_attribs[] = {
  27. #ifdef _DEBUG
  28. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
  29. #endif
  30. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  31. None,
  32. };
  33. struct gl_windowinfo {
  34. uint32_t id;
  35. Display *display;
  36. };
  37. struct gl_platform {
  38. GLXContext context;
  39. struct gs_swap_chain swap;
  40. };
  41. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  42. {
  43. return &platform->swap;
  44. }
  45. extern struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  46. {
  47. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  48. memset(wi, 0, sizeof(struct gl_windowinfo));
  49. wi->id = info->window.id;
  50. /* wi->display = info->window.display; */
  51. /*
  52. The above no longer works with Qt.
  53. Let's hope it continues to work.
  54. */
  55. return wi;
  56. }
  57. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  58. {
  59. bfree(wi);
  60. }
  61. extern void gl_getclientsize(struct gs_swap_chain *swap,
  62. uint32_t *width, uint32_t *height)
  63. {
  64. XWindowAttributes info = { 0 };
  65. XGetWindowAttributes(swap->wi->display, swap->wi->id, &info);
  66. *height = info.height;
  67. *width = info.width;
  68. }
  69. static void print_info_stuff(struct gs_init_data *info)
  70. {
  71. blog( LOG_INFO,
  72. "X and Y: %i %i\n"
  73. "Backbuffers: %i\n"
  74. "Color Format: %i\n"
  75. "ZStencil Format: %i\n"
  76. "Adapter: %i\n",
  77. info->cx, info->cy,
  78. info->num_backbuffers,
  79. info->format, info->zsformat,
  80. info->adapter
  81. );
  82. }
  83. struct gl_platform *gl_platform_create(device_t device,
  84. struct gs_init_data *info)
  85. {
  86. int num_configs = 0;
  87. int error_base = 0, event_base = 0;
  88. Display *display = XOpenDisplay(NULL); /* Open default screen */
  89. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  90. GLXFBConfig* configs;
  91. print_info_stuff(info);
  92. memset(plat, 0, sizeof(struct gl_platform));
  93. if (!display) {
  94. blog(LOG_ERROR, "Unable to find display. DISPLAY variable may not be set correctly.");
  95. goto fail0;
  96. }
  97. if (!glx_LoadFunctions(display, DefaultScreen(display))) {
  98. blog(LOG_ERROR, "Unable to load GLX entry functions.");
  99. goto fail0;
  100. }
  101. if (!glXQueryExtension(display, &error_base, &event_base)) {
  102. blog(LOG_ERROR, "GLX not supported.");
  103. goto fail0;
  104. }
  105. /* We require glX version 1.4 */
  106. {
  107. int major = 0, minor = 0;
  108. glXQueryVersion(display, &major, &minor);
  109. if (major < 1 || minor < 4) {
  110. blog(LOG_ERROR, "GLX version found: %i.%i\nRequired: 1.4", major, minor);
  111. goto fail0;
  112. }
  113. }
  114. if (!glx_ext_ARB_create_context) {
  115. blog(LOG_ERROR, "ARB_GLX_create_context not supported!");
  116. goto fail0;
  117. }
  118. configs = glXChooseFBConfig(display, DefaultScreen(display), fb_attribs, &num_configs);
  119. if(!configs) {
  120. blog(LOG_ERROR, "Attribute list or screen is invalid.");
  121. goto fail0;
  122. }
  123. if(num_configs == 0) {
  124. blog(LOG_ERROR, "No framebuffer configurations found.");
  125. goto fail1;
  126. }
  127. /* We just use the first configuration found... as it does everything we want at the very least. */
  128. plat->context = glXCreateContextAttribsARB(display, configs[0], NULL, True, ctx_attribs);
  129. if(!plat->context) {
  130. blog(LOG_ERROR, "Failed to create OpenGL context.");
  131. goto fail1;
  132. }
  133. if(!glXMakeCurrent(display, info->window.id, plat->context)) {
  134. blog(LOG_ERROR, "Failed to make context current.");
  135. goto fail2;
  136. }
  137. if (!ogl_LoadFunctions()) {
  138. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  139. goto fail2;
  140. }
  141. blog(LOG_INFO, "OpenGL version: %s\n", glGetString(GL_VERSION));
  142. plat->swap.device = device;
  143. plat->swap.info = *info;
  144. plat->swap.wi = gl_windowinfo_create(info);
  145. plat->swap.wi->display = display;
  146. XFree(configs);
  147. XSync(display, False);
  148. return plat;
  149. fail2:
  150. glXMakeCurrent(display, None, NULL);
  151. glXDestroyContext(display, plat->context);
  152. fail1:
  153. XFree(configs);
  154. fail0:
  155. bfree(plat);
  156. return NULL;
  157. }
  158. void gl_platform_destroy(struct gl_platform *platform)
  159. {
  160. if (!platform)
  161. return;
  162. glXMakeCurrent(platform->swap.wi->display, None, NULL);
  163. glXDestroyContext(platform->swap.wi->display, platform->context);
  164. XCloseDisplay(platform->swap.wi->display);
  165. gl_windowinfo_destroy(platform->swap.wi);
  166. bfree(platform);
  167. }
  168. void device_entercontext(device_t device)
  169. {
  170. GLXContext context = device->plat->context;
  171. XID window = device->plat->swap.wi->id;
  172. Display *display = device->plat->swap.wi->display;
  173. if (device->cur_swap)
  174. device->plat->swap.wi->id = device->cur_swap->wi->id;
  175. if (!glXMakeCurrent(display, window, context)) {
  176. blog(LOG_ERROR, "Failed to make context current.");
  177. }
  178. }
  179. void device_leavecontext(device_t device)
  180. {
  181. Display *display = device->plat->swap.wi->display;
  182. if(!glXMakeCurrent(display, None, NULL)) {
  183. blog(LOG_ERROR, "Failed to reset current context.");
  184. }
  185. }
  186. void gl_update(device_t device)
  187. {
  188. /* I don't know of anything we can do here. */
  189. }
  190. void device_load_swapchain(device_t device, swapchain_t swap)
  191. {
  192. if(!swap)
  193. swap = &device->plat->swap;
  194. device->cur_swap = swap;
  195. }
  196. void device_present(device_t device)
  197. {
  198. Display *display = device->plat->swap.wi->display;
  199. XID window = device->plat->swap.wi->id;
  200. glXSwapBuffers(display, window);
  201. }