gl-x11.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 = bzalloc(sizeof(struct gl_windowinfo));
  48. wi->id = info->window.id;
  49. /* wi->display = info->window.display; */
  50. /*
  51. The above no longer works with Qt.
  52. Let's hope it continues to work.
  53. */
  54. return wi;
  55. }
  56. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  57. {
  58. bfree(wi);
  59. }
  60. extern void gl_getclientsize(struct gs_swap_chain *swap,
  61. uint32_t *width, uint32_t *height)
  62. {
  63. XWindowAttributes info = { 0 };
  64. XGetWindowAttributes(swap->wi->display, swap->wi->id, &info);
  65. *height = info.height;
  66. *width = info.width;
  67. }
  68. static void print_info_stuff(struct gs_init_data *info)
  69. {
  70. blog( LOG_INFO,
  71. "X and Y: %i %i\n"
  72. "Backbuffers: %i\n"
  73. "Color Format: %i\n"
  74. "ZStencil Format: %i\n"
  75. "Adapter: %i\n",
  76. info->cx, info->cy,
  77. info->num_backbuffers,
  78. info->format, info->zsformat,
  79. info->adapter
  80. );
  81. }
  82. struct gl_platform *gl_platform_create(device_t device,
  83. struct gs_init_data *info)
  84. {
  85. int num_configs = 0;
  86. int error_base = 0, event_base = 0;
  87. Display *display = XOpenDisplay(NULL); /* Open default screen */
  88. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  89. GLXFBConfig* configs;
  90. print_info_stuff(info);
  91. if (!display) {
  92. blog(LOG_ERROR, "Unable to find display. DISPLAY variable "
  93. "may not be set correctly.");
  94. goto fail0;
  95. }
  96. if (!glx_LoadFunctions(display, DefaultScreen(display))) {
  97. blog(LOG_ERROR, "Unable to load GLX entry functions.");
  98. goto fail0;
  99. }
  100. if (!glXQueryExtension(display, &error_base, &event_base)) {
  101. blog(LOG_ERROR, "GLX not supported.");
  102. goto fail0;
  103. }
  104. /* We require glX version 1.3 */
  105. {
  106. int major = 0, minor = 0;
  107. glXQueryVersion(display, &major, &minor);
  108. if (major < 1 || (major == 1 && minor < 3)) {
  109. blog(LOG_ERROR, "GLX version found: %i.%i\nRequired: "
  110. "1.3", 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),
  119. fb_attribs, &num_configs);
  120. if(!configs) {
  121. blog(LOG_ERROR, "Attribute list or screen is invalid.");
  122. goto fail0;
  123. }
  124. if(num_configs == 0) {
  125. blog(LOG_ERROR, "No framebuffer configurations found.");
  126. goto fail1;
  127. }
  128. /* We just use the first configuration found... as it does everything
  129. * we want at the very least. */
  130. plat->context = glXCreateContextAttribsARB(display, configs[0], NULL,
  131. True, ctx_attribs);
  132. if(!plat->context) {
  133. blog(LOG_ERROR, "Failed to create OpenGL context.");
  134. goto fail1;
  135. }
  136. if(!glXMakeCurrent(display, info->window.id, plat->context)) {
  137. blog(LOG_ERROR, "Failed to make context current.");
  138. goto fail2;
  139. }
  140. if (!ogl_LoadFunctions()) {
  141. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  142. goto fail2;
  143. }
  144. blog(LOG_INFO, "OpenGL version: %s\n", glGetString(GL_VERSION));
  145. plat->swap.device = device;
  146. plat->swap.info = *info;
  147. plat->swap.wi = gl_windowinfo_create(info);
  148. plat->swap.wi->display = display;
  149. XFree(configs);
  150. XSync(display, False);
  151. return plat;
  152. fail2:
  153. glXMakeCurrent(display, None, NULL);
  154. glXDestroyContext(display, plat->context);
  155. fail1:
  156. XFree(configs);
  157. fail0:
  158. bfree(plat);
  159. return NULL;
  160. }
  161. void gl_platform_destroy(struct gl_platform *platform)
  162. {
  163. if (!platform)
  164. return;
  165. glXMakeCurrent(platform->swap.wi->display, None, NULL);
  166. glXDestroyContext(platform->swap.wi->display, platform->context);
  167. XCloseDisplay(platform->swap.wi->display);
  168. gl_windowinfo_destroy(platform->swap.wi);
  169. bfree(platform);
  170. }
  171. void device_entercontext(device_t device)
  172. {
  173. GLXContext context = device->plat->context;
  174. XID window = device->plat->swap.wi->id;
  175. Display *display = device->plat->swap.wi->display;
  176. if (device->cur_swap)
  177. device->plat->swap.wi->id = device->cur_swap->wi->id;
  178. if (!glXMakeCurrent(display, window, context)) {
  179. blog(LOG_ERROR, "Failed to make context current.");
  180. }
  181. }
  182. void device_leavecontext(device_t device)
  183. {
  184. Display *display = device->plat->swap.wi->display;
  185. if(!glXMakeCurrent(display, None, NULL)) {
  186. blog(LOG_ERROR, "Failed to reset current context.");
  187. }
  188. }
  189. void gl_update(device_t device)
  190. {
  191. /* I don't know of anything we can do here. */
  192. }
  193. void device_load_swapchain(device_t device, swapchain_t swap)
  194. {
  195. if(!swap)
  196. swap = &device->plat->swap;
  197. device->cur_swap = swap;
  198. }
  199. void device_present(device_t device)
  200. {
  201. Display *display = device->plat->swap.wi->display;
  202. XID window = device->plat->swap.wi->id;
  203. glXSwapBuffers(display, window);
  204. }