gl-x11.c 6.1 KB

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