gl-x11.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. /* Version 2 of the GLX backend...
  15. * Difference from version 1 is that we use XCB to help alleviate
  16. * pains in a threaded environment that is prone to error.
  17. * These errors must be readable and handled for the sake of,
  18. * not only the users' sanity, but my own.
  19. *
  20. * With that said, we have more error checking capabilities...
  21. * and not all of them are used to help simplify current code.
  22. *
  23. * TODO: Implement more complete error checking.
  24. * NOTE: GLX loading functions are placed illogically
  25. * for the sake of convenience.
  26. */
  27. #include <X11/Xlib.h>
  28. #include <X11/Xlib-xcb.h>
  29. #include <xcb/xcb.h>
  30. #include <stdio.h>
  31. #include "gl-subsystem.h"
  32. #include <glad/glad_glx.h>
  33. static const int ctx_attribs[] = {
  34. #ifdef _DEBUG
  35. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
  36. #endif
  37. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  38. GLX_CONTEXT_MAJOR_VERSION_ARB, 3, GLX_CONTEXT_MINOR_VERSION_ARB, 2,
  39. None,
  40. };
  41. struct gl_windowinfo {
  42. /* We store this value since we can fetch a lot
  43. * of information not only concerning the config
  44. * but the visual, and various other settings
  45. * for the context.
  46. */
  47. GLXFBConfig config;
  48. /* Windows in X11 are defined with integers (XID).
  49. * xcb_window_t is a define for this... they are
  50. * compatible with Xlib as well.
  51. */
  52. xcb_window_t window;
  53. /* We can't fetch screen without a request so we cache it. */
  54. int screen;
  55. };
  56. struct gl_platform {
  57. Display *display;
  58. GLXContext context;
  59. struct gs_swap_chain swap;
  60. };
  61. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  62. {
  63. return &platform->swap;
  64. }
  65. static void print_info_stuff(const struct gs_init_data *info)
  66. {
  67. blog( LOG_INFO,
  68. "X and Y: %i %i\n"
  69. "Backbuffers: %i\n"
  70. "Color Format: %i\n"
  71. "ZStencil Format: %i\n"
  72. "Adapter: %i\n",
  73. info->cx, info->cy,
  74. info->num_backbuffers,
  75. info->format, info->zsformat,
  76. info->adapter
  77. );
  78. }
  79. /* The following utility functions are copied verbatim from WGL code.
  80. * GLX and WGL are more similar than most people realize. */
  81. /* For now, only support basic 32bit formats for graphics output. */
  82. static inline int get_color_format_bits(enum gs_color_format format)
  83. {
  84. switch ((uint32_t)format) {
  85. case GS_RGBA:
  86. return 32;
  87. default:
  88. return 0;
  89. }
  90. }
  91. static inline int get_depth_format_bits(enum gs_zstencil_format zsformat)
  92. {
  93. switch ((uint32_t)zsformat) {
  94. case GS_Z16:
  95. return 16;
  96. case GS_Z24_S8:
  97. return 24;
  98. default:
  99. return 0;
  100. }
  101. }
  102. static inline int get_stencil_format_bits(enum gs_zstencil_format zsformat)
  103. {
  104. switch ((uint32_t)zsformat) {
  105. case GS_Z24_S8:
  106. return 8;
  107. default:
  108. return 0;
  109. }
  110. }
  111. /*
  112. * Since we cannot take advantage of the asynchronous nature of xcb,
  113. * all of the helper functions are synchronous but thread-safe.
  114. *
  115. * They check for errors and will return 0 on problems
  116. * with the exception of when 0 is a valid return value... in which case
  117. * read the specific function comments.
  118. */
  119. /* Returns -1 on invalid screen. */
  120. static int get_screen_num_from_xcb_screen(xcb_connection_t *xcb_conn,
  121. xcb_screen_t *screen)
  122. {
  123. xcb_screen_iterator_t iter =
  124. xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  125. int screen_num = 0;
  126. for (; iter.rem; xcb_screen_next(&iter), ++screen_num)
  127. if (iter.data == screen)
  128. return screen_num;
  129. return -1;
  130. }
  131. static xcb_screen_t *get_screen_from_root(xcb_connection_t *xcb_conn,
  132. xcb_window_t root)
  133. {
  134. xcb_screen_iterator_t iter =
  135. xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  136. while (iter.rem) {
  137. if (iter.data->root == root)
  138. return iter.data;
  139. xcb_screen_next(&iter);
  140. }
  141. return 0;
  142. }
  143. static inline int get_screen_num_from_root(xcb_connection_t *xcb_conn,
  144. xcb_window_t root)
  145. {
  146. xcb_screen_t *screen = get_screen_from_root(xcb_conn, root);
  147. if (!screen) return -1;
  148. return get_screen_num_from_xcb_screen(xcb_conn, screen);
  149. }
  150. static xcb_get_geometry_reply_t* get_window_geometry(
  151. xcb_connection_t *xcb_conn, xcb_drawable_t drawable)
  152. {
  153. xcb_get_geometry_cookie_t cookie;
  154. xcb_generic_error_t *error;
  155. xcb_get_geometry_reply_t *reply;
  156. cookie = xcb_get_geometry(xcb_conn, drawable);
  157. reply = xcb_get_geometry_reply(xcb_conn, cookie, &error);
  158. if (error) {
  159. blog(LOG_ERROR, "Failed to fetch parent window geometry!");
  160. free(error);
  161. free(reply);
  162. return 0;
  163. }
  164. free(error);
  165. return reply;
  166. }
  167. static bool gl_context_create(struct gl_platform *plat)
  168. {
  169. Display *display = plat->display;
  170. GLXFBConfig config = plat->swap.wi->config;
  171. int major, minor;
  172. GLXContext context;
  173. {
  174. int error_base;
  175. int event_base;
  176. if (!glXQueryExtension(display, &error_base, &event_base)) {
  177. blog(LOG_ERROR, "GLX not supported.");
  178. return 0;
  179. }
  180. }
  181. /* We require glX version 1.3 */
  182. glXQueryVersion(display, &major, &minor);
  183. if (major < 1 || (major == 1 && minor < 3)) {
  184. blog(LOG_ERROR, "GLX version found: %i.%i\nRequired: "
  185. "1.3", major, minor);
  186. return false;
  187. }
  188. if (!GLAD_GLX_ARB_create_context) {
  189. blog(LOG_ERROR, "ARB_GLX_create_context not supported!");
  190. return false;
  191. }
  192. context = glXCreateContextAttribsARB(display, config, NULL,
  193. true, ctx_attribs);
  194. if (!context) {
  195. blog(LOG_ERROR, "Failed to create OpenGL context.");
  196. return false;
  197. }
  198. plat->context = context;
  199. plat->display = display;
  200. return true;
  201. }
  202. static void gl_context_destroy(struct gl_platform *plat)
  203. {
  204. Display *display = plat->display;
  205. glXMakeCurrent(display, None, 0);
  206. glXDestroyContext(display, plat->context);
  207. bfree(plat);
  208. }
  209. extern struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
  210. {
  211. UNUSED_PARAMETER(info);
  212. return bmalloc(sizeof(struct gl_windowinfo));
  213. }
  214. extern void gl_windowinfo_destroy(struct gl_windowinfo *info)
  215. {
  216. UNUSED_PARAMETER(info);
  217. bfree(info);
  218. }
  219. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  220. const struct gs_init_data *info)
  221. {
  222. /* There's some trickery here... we're mixing libX11, xcb, and GLX
  223. For an explanation see here: http://xcb.freedesktop.org/MixingCalls/
  224. Essentially, GLX requires Xlib. Everything else we use xcb. */
  225. struct gl_windowinfo *wi = gl_windowinfo_create(info);
  226. struct gl_platform * plat = bmalloc(sizeof(struct gl_platform));
  227. Display * display;
  228. print_info_stuff(info);
  229. if (!wi) {
  230. blog(LOG_ERROR, "Failed to create window info!");
  231. goto fail_wi_create;
  232. }
  233. display = XOpenDisplay(XDisplayString(info->window.display));
  234. if (!display) {
  235. blog(LOG_ERROR, "Unable to open new X connection!");
  236. goto fail_display_open;
  237. }
  238. XSetEventQueueOwner(display, XCBOwnsEventQueue);
  239. /* We assume later that cur_swap is already set. */
  240. device->cur_swap = &plat->swap;
  241. device->plat = plat;
  242. plat->display = display;
  243. plat->swap.device = device;
  244. plat->swap.info = *info;
  245. plat->swap.wi = wi;
  246. if (!gl_platform_init_swapchain(&plat->swap)) {
  247. blog(LOG_ERROR, "Failed to initialize swap chain!");
  248. goto fail_init_swapchain;
  249. }
  250. if (!gl_context_create(plat)) {
  251. blog(LOG_ERROR, "Failed to create context!");
  252. goto fail_context_create;
  253. }
  254. if (!glXMakeCurrent(plat->display, wi->window, plat->context)) {
  255. blog(LOG_ERROR, "Failed to make context current.");
  256. goto fail_make_current;
  257. }
  258. if (!gladLoadGL()) {
  259. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  260. goto fail_load_gl;
  261. }
  262. blog(LOG_INFO, "OpenGL version: %s\n", glGetString(GL_VERSION));
  263. goto success;
  264. fail_make_current:
  265. gl_context_destroy(plat);
  266. fail_context_create:
  267. fail_load_gl:
  268. fail_init_swapchain:
  269. XCloseDisplay(display);
  270. fail_display_open:
  271. fail_wi_create:
  272. gl_windowinfo_destroy(wi);
  273. free(plat);
  274. plat = NULL;
  275. success:
  276. return plat;
  277. }
  278. extern void gl_platform_destroy(struct gl_platform *plat)
  279. {
  280. if (!plat) /* In what case would platform be invalid here? */
  281. return;
  282. struct gl_windowinfo *wi = plat->swap.wi;
  283. gl_context_destroy(plat);
  284. gl_windowinfo_destroy(wi);
  285. }
  286. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  287. {
  288. Display *display = swap->device->plat->display;
  289. struct gs_init_data *info = &swap->info;
  290. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  291. xcb_window_t wid = xcb_generate_id(xcb_conn);
  292. xcb_window_t parent = swap->info.window.id;
  293. xcb_get_geometry_reply_t *geometry =
  294. get_window_geometry(xcb_conn, parent);
  295. bool status = false;
  296. int screen_num;
  297. int visual;
  298. GLXFBConfig *fb_config;
  299. if (!geometry) goto fail_geometry_request;
  300. screen_num = get_screen_num_from_root(xcb_conn, geometry->root);
  301. if (screen_num == -1) {
  302. goto fail_screen;
  303. }
  304. /* NOTE:
  305. * So GLX is odd. You can have different extensions per screen,
  306. * not just per video card or visual.
  307. *
  308. * Because of this, it makes sense to call LoadGLX everytime
  309. * we open a frackin' window. In Windows, entry points can change
  310. * so it makes more sense there. Here, despite it virtually never
  311. * having the possibility of changing unless the user is intentionally
  312. * being an asshole to cause this behavior, we still have to give it
  313. * the correct screen num just out of good practice. *sigh*
  314. */
  315. if (!gladLoadGLX(display, screen_num)) {
  316. blog(LOG_ERROR, "Unable to load GLX entry functions.");
  317. goto fail_load_glx;
  318. }
  319. /* Define our FBConfig hints for GLX... */
  320. const int fb_attribs[] = {
  321. GLX_STENCIL_SIZE, get_stencil_format_bits(info->zsformat),
  322. GLX_DEPTH_SIZE, get_depth_format_bits(info->zsformat),
  323. GLX_BUFFER_SIZE, get_color_format_bits(info->format),
  324. GLX_DOUBLEBUFFER, true,
  325. GLX_X_RENDERABLE, true,
  326. None
  327. };
  328. /* ...fetch the best match... */
  329. {
  330. int num_configs;
  331. fb_config = glXChooseFBConfig(display, screen_num,
  332. fb_attribs, &num_configs);
  333. if (!fb_config || !num_configs) {
  334. blog(LOG_ERROR, "Failed to find FBConfig!");
  335. goto fail_fb_config;
  336. }
  337. }
  338. /* ...then fetch matching visual info for xcb. */
  339. {
  340. int error = glXGetFBConfigAttrib(display, fb_config[0], GLX_VISUAL_ID, &visual);
  341. if (error) {
  342. blog(LOG_ERROR, "Bad call to GetFBConfigAttrib!");
  343. goto fail_visual_id;
  344. }
  345. }
  346. xcb_colormap_t colormap = xcb_generate_id(xcb_conn);
  347. uint32_t mask = XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP;
  348. uint32_t mask_values[] = { 0, colormap, 0 };
  349. xcb_create_colormap(xcb_conn,
  350. XCB_COLORMAP_ALLOC_NONE,
  351. colormap,
  352. parent,
  353. visual
  354. );
  355. xcb_create_window(
  356. xcb_conn, 24 /* Hardcoded? */,
  357. wid, parent,
  358. 0, 0,
  359. geometry->width,
  360. geometry->height,
  361. 0, 0,
  362. visual, mask, mask_values
  363. );
  364. swap->wi->config = fb_config[0];
  365. swap->wi->window = wid;
  366. xcb_map_window(xcb_conn, wid);
  367. XFree(fb_config);
  368. status = true;
  369. goto success;
  370. fail_visual_id:
  371. XFree(fb_config);
  372. fail_fb_config:
  373. fail_load_glx:
  374. fail_screen:
  375. fail_geometry_request:
  376. success:
  377. free(geometry);
  378. return status;
  379. }
  380. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  381. {
  382. UNUSED_PARAMETER(swap);
  383. /* Really nothing to clean up? */
  384. }
  385. extern void device_enter_context(gs_device_t *device)
  386. {
  387. GLXContext context = device->plat->context;
  388. XID window = device->cur_swap->wi->window;
  389. Display *display = device->plat->display;
  390. if (!glXMakeCurrent(display, window, context)) {
  391. blog(LOG_ERROR, "Failed to make context current.");
  392. }
  393. }
  394. extern void device_leave_context(gs_device_t *device)
  395. {
  396. Display *display = device->plat->display;
  397. if (!glXMakeCurrent(display, None, NULL)) {
  398. blog(LOG_ERROR, "Failed to reset current context.");
  399. }
  400. }
  401. extern void gl_getclientsize(const struct gs_swap_chain *swap,
  402. uint32_t *width, uint32_t *height)
  403. {
  404. xcb_connection_t *xcb_conn = XGetXCBConnection(swap->device->plat->display);
  405. xcb_window_t window = swap->wi->window;
  406. xcb_get_geometry_reply_t *geometry = get_window_geometry(xcb_conn, window);
  407. *width = geometry->width;
  408. *height = geometry->height;
  409. free(geometry);
  410. }
  411. extern void gl_update(gs_device_t *device)
  412. {
  413. Display *display = device->plat->display;
  414. xcb_window_t window = device->cur_swap->wi->window;
  415. uint32_t values[] = {
  416. device->cur_swap->info.cx,
  417. device->cur_swap->info.cy
  418. };
  419. xcb_configure_window(
  420. XGetXCBConnection(display), window,
  421. XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  422. values
  423. );
  424. }
  425. extern void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  426. {
  427. if (!swap)
  428. swap = &device->plat->swap;
  429. if (device->cur_swap == swap)
  430. return;
  431. Display *dpy = device->plat->display;
  432. XID window = swap->wi->window;
  433. GLXContext ctx = device->plat->context;
  434. device->cur_swap = swap;
  435. if (!glXMakeCurrent(dpy, window, ctx)) {
  436. blog(LOG_ERROR, "Failed to make context current.");
  437. }
  438. }
  439. extern void device_present(gs_device_t *device)
  440. {
  441. Display *display = device->plat->display;
  442. XID window = device->cur_swap->wi->window;
  443. /* TODO: Handle XCB events. */
  444. glXSwapBuffers(display, window);
  445. }