gl-x11.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. static int ctx_pbuffer_attribs[] = {
  42. GLX_PBUFFER_WIDTH, 2,
  43. GLX_PBUFFER_HEIGHT, 2,
  44. None
  45. };
  46. static int ctx_visual_attribs[] = {
  47. GLX_STENCIL_SIZE, 0,
  48. GLX_DEPTH_SIZE, 0,
  49. GLX_BUFFER_SIZE, 32,
  50. GLX_DOUBLEBUFFER, true,
  51. GLX_X_RENDERABLE, true,
  52. None
  53. };
  54. struct gl_windowinfo {
  55. /* We store this value since we can fetch a lot
  56. * of information not only concerning the config
  57. * but the visual, and various other settings
  58. * for the context.
  59. */
  60. GLXFBConfig config;
  61. /* Windows in X11 are defined with integers (XID).
  62. * xcb_window_t is a define for this... they are
  63. * compatible with Xlib as well.
  64. */
  65. xcb_window_t window;
  66. /* We can't fetch screen without a request so we cache it. */
  67. int screen;
  68. };
  69. struct gl_platform {
  70. Display *display;
  71. GLXContext context;
  72. GLXPbuffer pbuffer;
  73. };
  74. static void print_info_stuff(const struct gs_init_data *info)
  75. {
  76. blog( LOG_INFO,
  77. "X and Y: %i %i\n"
  78. "Backbuffers: %i\n"
  79. "Color Format: %i\n"
  80. "ZStencil Format: %i\n"
  81. "Adapter: %i\n",
  82. info->cx, info->cy,
  83. info->num_backbuffers,
  84. info->format, info->zsformat,
  85. info->adapter
  86. );
  87. }
  88. /* The following utility functions are copied verbatim from WGL code.
  89. * GLX and WGL are more similar than most people realize. */
  90. /* For now, only support basic 32bit formats for graphics output. */
  91. static inline int get_color_format_bits(enum gs_color_format format)
  92. {
  93. switch ((uint32_t)format) {
  94. case GS_RGBA:
  95. return 32;
  96. default:
  97. return 0;
  98. }
  99. }
  100. static inline int get_depth_format_bits(enum gs_zstencil_format zsformat)
  101. {
  102. switch ((uint32_t)zsformat) {
  103. case GS_Z16:
  104. return 16;
  105. case GS_Z24_S8:
  106. return 24;
  107. default:
  108. return 0;
  109. }
  110. }
  111. static inline int get_stencil_format_bits(enum gs_zstencil_format zsformat)
  112. {
  113. switch ((uint32_t)zsformat) {
  114. case GS_Z24_S8:
  115. return 8;
  116. default:
  117. return 0;
  118. }
  119. }
  120. /*
  121. * Since we cannot take advantage of the asynchronous nature of xcb,
  122. * all of the helper functions are synchronous but thread-safe.
  123. *
  124. * They check for errors and will return 0 on problems
  125. * with the exception of when 0 is a valid return value... in which case
  126. * read the specific function comments.
  127. */
  128. /* Returns -1 on invalid screen. */
  129. static int get_screen_num_from_xcb_screen(xcb_connection_t *xcb_conn,
  130. xcb_screen_t *screen)
  131. {
  132. xcb_screen_iterator_t iter =
  133. xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  134. int screen_num = 0;
  135. for (; iter.rem; xcb_screen_next(&iter), ++screen_num)
  136. if (iter.data == screen)
  137. return screen_num;
  138. return -1;
  139. }
  140. static xcb_screen_t *get_screen_from_root(xcb_connection_t *xcb_conn,
  141. xcb_window_t root)
  142. {
  143. xcb_screen_iterator_t iter =
  144. xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  145. while (iter.rem) {
  146. if (iter.data->root == root)
  147. return iter.data;
  148. xcb_screen_next(&iter);
  149. }
  150. return 0;
  151. }
  152. static inline int get_screen_num_from_root(xcb_connection_t *xcb_conn,
  153. xcb_window_t root)
  154. {
  155. xcb_screen_t *screen = get_screen_from_root(xcb_conn, root);
  156. if (!screen) return -1;
  157. return get_screen_num_from_xcb_screen(xcb_conn, screen);
  158. }
  159. static xcb_get_geometry_reply_t* get_window_geometry(
  160. xcb_connection_t *xcb_conn, xcb_drawable_t drawable)
  161. {
  162. xcb_get_geometry_cookie_t cookie;
  163. xcb_generic_error_t *error;
  164. xcb_get_geometry_reply_t *reply;
  165. cookie = xcb_get_geometry(xcb_conn, drawable);
  166. reply = xcb_get_geometry_reply(xcb_conn, cookie, &error);
  167. if (error) {
  168. blog(LOG_ERROR, "Failed to fetch parent window geometry!");
  169. free(error);
  170. free(reply);
  171. return 0;
  172. }
  173. free(error);
  174. return reply;
  175. }
  176. static bool gl_context_create(struct gl_platform *plat)
  177. {
  178. Display *display = plat->display;
  179. int frame_buf_config_count = 0;
  180. GLXFBConfig *config = NULL;
  181. GLXContext context;
  182. bool success = false;
  183. if (!GLAD_GLX_ARB_create_context) {
  184. blog(LOG_ERROR, "ARB_GLX_create_context not supported!");
  185. return false;
  186. }
  187. config = glXChooseFBConfig(display, DefaultScreen(display),
  188. ctx_visual_attribs, &frame_buf_config_count);
  189. if (!config) {
  190. blog(LOG_ERROR, "Failed to create OpenGL frame buffer config");
  191. return false;
  192. }
  193. context = glXCreateContextAttribsARB(display, config[0], NULL,
  194. true, ctx_attribs);
  195. if (!context) {
  196. blog(LOG_ERROR, "Failed to create OpenGL context.");
  197. goto error;
  198. }
  199. plat->context = context;
  200. plat->display = display;
  201. plat->pbuffer = glXCreatePbuffer(display, config[0],
  202. ctx_pbuffer_attribs);
  203. if (!plat->pbuffer) {
  204. blog(LOG_ERROR, "Failed to create OpenGL pbuffer");
  205. goto error;
  206. }
  207. success = true;
  208. error:
  209. XFree(config);
  210. XSync(display, false);
  211. return success;
  212. }
  213. static void gl_context_destroy(struct gl_platform *plat)
  214. {
  215. Display *display = plat->display;
  216. glXMakeContextCurrent(display, None, None, NULL);
  217. glXDestroyContext(display, plat->context);
  218. bfree(plat);
  219. }
  220. extern struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
  221. {
  222. UNUSED_PARAMETER(info);
  223. return bmalloc(sizeof(struct gl_windowinfo));
  224. }
  225. extern void gl_windowinfo_destroy(struct gl_windowinfo *info)
  226. {
  227. UNUSED_PARAMETER(info);
  228. bfree(info);
  229. }
  230. static Display *open_windowless_display(void)
  231. {
  232. Display *display = XOpenDisplay(NULL);
  233. xcb_connection_t *xcb_conn;
  234. xcb_screen_iterator_t screen_iterator;
  235. xcb_screen_t *screen;
  236. int screen_num;
  237. if (!display) {
  238. blog(LOG_ERROR, "Unable to open new X connection!");
  239. return NULL;
  240. }
  241. xcb_conn = XGetXCBConnection(display);
  242. if (!xcb_conn) {
  243. blog(LOG_ERROR, "Unable to get XCB connection to main display");
  244. goto error;
  245. }
  246. screen_iterator = xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  247. screen = screen_iterator.data;
  248. if (!screen) {
  249. blog(LOG_ERROR, "Unable to get screen root");
  250. goto error;
  251. }
  252. screen_num = get_screen_num_from_root(xcb_conn, screen->root);
  253. if (screen_num == -1) {
  254. blog(LOG_ERROR, "Unable to get screen number from root");
  255. goto error;
  256. }
  257. if (!gladLoadGLX(display, screen_num)) {
  258. blog(LOG_ERROR, "Unable to load GLX entry functions.");
  259. goto error;
  260. }
  261. return display;
  262. error:
  263. if (display)
  264. XCloseDisplay(display);
  265. return NULL;
  266. }
  267. static int x_error_handler(Display *display, XErrorEvent *error)
  268. {
  269. char str[512];
  270. XGetErrorText(display, error->error_code, str, sizeof(str));
  271. blog(LOG_ERROR, "X Error: %s", str);
  272. return 0;
  273. }
  274. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  275. uint32_t adapter)
  276. {
  277. /* There's some trickery here... we're mixing libX11, xcb, and GLX
  278. For an explanation see here: http://xcb.freedesktop.org/MixingCalls/
  279. Essentially, GLX requires Xlib. Everything else we use xcb. */
  280. struct gl_platform * plat = bmalloc(sizeof(struct gl_platform));
  281. Display * display = open_windowless_display();
  282. if (!display) {
  283. goto fail_display_open;
  284. }
  285. XSetEventQueueOwner(display, XCBOwnsEventQueue);
  286. XSetErrorHandler(x_error_handler);
  287. /* We assume later that cur_swap is already set. */
  288. device->plat = plat;
  289. plat->display = display;
  290. if (!gl_context_create(plat)) {
  291. blog(LOG_ERROR, "Failed to create context!");
  292. goto fail_context_create;
  293. }
  294. if (!glXMakeContextCurrent(plat->display, plat->pbuffer, plat->pbuffer,
  295. plat->context)) {
  296. blog(LOG_ERROR, "Failed to make context current.");
  297. goto fail_make_current;
  298. }
  299. if (!gladLoadGL()) {
  300. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  301. goto fail_load_gl;
  302. }
  303. goto success;
  304. fail_make_current:
  305. gl_context_destroy(plat);
  306. fail_context_create:
  307. fail_load_gl:
  308. XCloseDisplay(display);
  309. fail_display_open:
  310. bfree(plat);
  311. plat = NULL;
  312. success:
  313. UNUSED_PARAMETER(adapter);
  314. return plat;
  315. }
  316. extern void gl_platform_destroy(struct gl_platform *plat)
  317. {
  318. if (!plat) /* In what case would platform be invalid here? */
  319. return;
  320. gl_context_destroy(plat);
  321. }
  322. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  323. {
  324. Display *display = swap->device->plat->display;
  325. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  326. xcb_window_t wid = xcb_generate_id(xcb_conn);
  327. xcb_window_t parent = swap->info.window.id;
  328. xcb_get_geometry_reply_t *geometry =
  329. get_window_geometry(xcb_conn, parent);
  330. bool status = false;
  331. int screen_num;
  332. int visual;
  333. GLXFBConfig *fb_config;
  334. if (!geometry) goto fail_geometry_request;
  335. screen_num = get_screen_num_from_root(xcb_conn, geometry->root);
  336. if (screen_num == -1) {
  337. goto fail_screen;
  338. }
  339. /* ...fetch the best match... */
  340. {
  341. int num_configs;
  342. fb_config = glXChooseFBConfig(display, screen_num,
  343. ctx_visual_attribs, &num_configs);
  344. if (!fb_config || !num_configs) {
  345. blog(LOG_ERROR, "Failed to find FBConfig!");
  346. goto fail_fb_config;
  347. }
  348. }
  349. /* ...then fetch matching visual info for xcb. */
  350. {
  351. int error = glXGetFBConfigAttrib(display, fb_config[0], GLX_VISUAL_ID, &visual);
  352. if (error) {
  353. blog(LOG_ERROR, "Bad call to GetFBConfigAttrib!");
  354. goto fail_visual_id;
  355. }
  356. }
  357. xcb_colormap_t colormap = xcb_generate_id(xcb_conn);
  358. uint32_t mask = XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP;
  359. uint32_t mask_values[] = { 0, colormap, 0 };
  360. xcb_create_colormap(xcb_conn,
  361. XCB_COLORMAP_ALLOC_NONE,
  362. colormap,
  363. parent,
  364. visual
  365. );
  366. xcb_create_window(
  367. xcb_conn, 24 /* Hardcoded? */,
  368. wid, parent,
  369. 0, 0,
  370. geometry->width,
  371. geometry->height,
  372. 0, 0,
  373. visual, mask, mask_values
  374. );
  375. swap->wi->config = fb_config[0];
  376. swap->wi->window = wid;
  377. xcb_map_window(xcb_conn, wid);
  378. XFree(fb_config);
  379. status = true;
  380. goto success;
  381. fail_visual_id:
  382. XFree(fb_config);
  383. fail_fb_config:
  384. fail_screen:
  385. fail_geometry_request:
  386. success:
  387. free(geometry);
  388. return status;
  389. }
  390. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  391. {
  392. UNUSED_PARAMETER(swap);
  393. /* Really nothing to clean up? */
  394. }
  395. extern void device_enter_context(gs_device_t *device)
  396. {
  397. GLXContext context = device->plat->context;
  398. Display *display = device->plat->display;
  399. if (device->cur_swap) {
  400. XID window = device->cur_swap->wi->window;
  401. if (!glXMakeContextCurrent(display, window, window, context)) {
  402. blog(LOG_ERROR, "Failed to make context current.");
  403. }
  404. } else {
  405. GLXPbuffer pbuf = device->plat->pbuffer;
  406. if (!glXMakeContextCurrent(display, pbuf, pbuf, context)) {
  407. blog(LOG_ERROR, "Failed to make context current.");
  408. }
  409. }
  410. }
  411. extern void device_leave_context(gs_device_t *device)
  412. {
  413. Display *display = device->plat->display;
  414. if (!glXMakeContextCurrent(display, None, None, NULL)) {
  415. blog(LOG_ERROR, "Failed to reset current context.");
  416. }
  417. }
  418. extern void gl_getclientsize(const struct gs_swap_chain *swap,
  419. uint32_t *width, uint32_t *height)
  420. {
  421. xcb_connection_t *xcb_conn = XGetXCBConnection(swap->device->plat->display);
  422. xcb_window_t window = swap->wi->window;
  423. xcb_get_geometry_reply_t *geometry = get_window_geometry(xcb_conn, window);
  424. *width = geometry->width;
  425. *height = geometry->height;
  426. free(geometry);
  427. }
  428. extern void gl_update(gs_device_t *device)
  429. {
  430. Display *display = device->plat->display;
  431. xcb_window_t window = device->cur_swap->wi->window;
  432. uint32_t values[] = {
  433. device->cur_swap->info.cx,
  434. device->cur_swap->info.cy
  435. };
  436. xcb_configure_window(
  437. XGetXCBConnection(display), window,
  438. XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  439. values
  440. );
  441. }
  442. extern void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  443. {
  444. if (device->cur_swap == swap)
  445. return;
  446. Display *dpy = device->plat->display;
  447. GLXContext ctx = device->plat->context;
  448. device->cur_swap = swap;
  449. if (swap) {
  450. XID window = swap->wi->window;
  451. if (!glXMakeContextCurrent(dpy, window, window, ctx)) {
  452. blog(LOG_ERROR, "Failed to make context current.");
  453. }
  454. } else {
  455. GLXPbuffer pbuf = device->plat->pbuffer;
  456. if (!glXMakeContextCurrent(dpy, pbuf, pbuf, ctx)) {
  457. blog(LOG_ERROR, "Failed to make context current.");
  458. }
  459. }
  460. }
  461. enum swap_type {
  462. SWAP_TYPE_NORMAL,
  463. SWAP_TYPE_EXT,
  464. SWAP_TYPE_MESA,
  465. SWAP_TYPE_SGI,
  466. };
  467. extern void device_present(gs_device_t *device)
  468. {
  469. static bool initialized = false;
  470. static enum swap_type swap_type = SWAP_TYPE_NORMAL;
  471. Display *display = device->plat->display;
  472. XID window = device->cur_swap->wi->window;
  473. if (!initialized) {
  474. if (GLAD_GLX_EXT_swap_control)
  475. swap_type = SWAP_TYPE_EXT;
  476. else if (GLAD_GLX_MESA_swap_control)
  477. swap_type = SWAP_TYPE_MESA;
  478. else if (GLAD_GLX_SGI_swap_control)
  479. swap_type = SWAP_TYPE_SGI;
  480. initialized = true;
  481. }
  482. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  483. xcb_generic_event_t *xcb_event;
  484. while((xcb_event = xcb_poll_for_event(xcb_conn))) {
  485. /* TODO: Handle XCB events. */
  486. free(xcb_event);
  487. }
  488. switch (swap_type) {
  489. case SWAP_TYPE_EXT: glXSwapIntervalEXT(display, window, 0); break;
  490. case SWAP_TYPE_MESA: glXSwapIntervalMESA(0); break;
  491. case SWAP_TYPE_SGI: glXSwapIntervalSGI(0); break;
  492. case SWAP_TYPE_NORMAL:;
  493. }
  494. glXSwapBuffers(display, window);
  495. }