gl-x11.c 5.7 KB

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