gl-x11.c 14 KB

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