gl-x11.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 str1[512];
  271. char str2[512];
  272. char str3[512];
  273. XGetErrorText(display, error->error_code, str1, sizeof(str1));
  274. XGetErrorText(display, error->request_code, str2, sizeof(str2));
  275. XGetErrorText(display, error->minor_code, str3, sizeof(str3));
  276. blog(LOG_ERROR, "X Error: %s, Major opcode: %s, "
  277. "Minor opcode: %s, Serial: %lu",
  278. str1, str2, str3, error->serial);
  279. return 0;
  280. }
  281. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  282. uint32_t adapter)
  283. {
  284. /* There's some trickery here... we're mixing libX11, xcb, and GLX
  285. For an explanation see here: http://xcb.freedesktop.org/MixingCalls/
  286. Essentially, GLX requires Xlib. Everything else we use xcb. */
  287. struct gl_platform * plat = bmalloc(sizeof(struct gl_platform));
  288. Display * display = open_windowless_display();
  289. if (!display) {
  290. goto fail_display_open;
  291. }
  292. XSetEventQueueOwner(display, XCBOwnsEventQueue);
  293. XSetErrorHandler(x_error_handler);
  294. /* We assume later that cur_swap is already set. */
  295. device->plat = plat;
  296. plat->display = display;
  297. if (!gl_context_create(plat)) {
  298. blog(LOG_ERROR, "Failed to create context!");
  299. goto fail_context_create;
  300. }
  301. if (!glXMakeContextCurrent(plat->display, plat->pbuffer, plat->pbuffer,
  302. plat->context)) {
  303. blog(LOG_ERROR, "Failed to make context current.");
  304. goto fail_make_current;
  305. }
  306. if (!gladLoadGL()) {
  307. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  308. goto fail_load_gl;
  309. }
  310. goto success;
  311. fail_make_current:
  312. gl_context_destroy(plat);
  313. fail_context_create:
  314. fail_load_gl:
  315. XCloseDisplay(display);
  316. fail_display_open:
  317. bfree(plat);
  318. plat = NULL;
  319. success:
  320. UNUSED_PARAMETER(adapter);
  321. return plat;
  322. }
  323. extern void gl_platform_destroy(struct gl_platform *plat)
  324. {
  325. if (!plat) /* In what case would platform be invalid here? */
  326. return;
  327. gl_context_destroy(plat);
  328. }
  329. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  330. {
  331. Display *display = swap->device->plat->display;
  332. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  333. xcb_window_t wid = xcb_generate_id(xcb_conn);
  334. xcb_window_t parent = swap->info.window.id;
  335. xcb_get_geometry_reply_t *geometry =
  336. get_window_geometry(xcb_conn, parent);
  337. bool status = false;
  338. int screen_num;
  339. int visual;
  340. GLXFBConfig *fb_config;
  341. if (!geometry) goto fail_geometry_request;
  342. screen_num = get_screen_num_from_root(xcb_conn, geometry->root);
  343. if (screen_num == -1) {
  344. goto fail_screen;
  345. }
  346. /* ...fetch the best match... */
  347. {
  348. int num_configs;
  349. fb_config = glXChooseFBConfig(display, screen_num,
  350. ctx_visual_attribs, &num_configs);
  351. if (!fb_config || !num_configs) {
  352. blog(LOG_ERROR, "Failed to find FBConfig!");
  353. goto fail_fb_config;
  354. }
  355. }
  356. /* ...then fetch matching visual info for xcb. */
  357. {
  358. int error = glXGetFBConfigAttrib(display, fb_config[0], GLX_VISUAL_ID, &visual);
  359. if (error) {
  360. blog(LOG_ERROR, "Bad call to GetFBConfigAttrib!");
  361. goto fail_visual_id;
  362. }
  363. }
  364. xcb_colormap_t colormap = xcb_generate_id(xcb_conn);
  365. uint32_t mask = XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP;
  366. uint32_t mask_values[] = { 0, colormap, 0 };
  367. xcb_create_colormap(xcb_conn,
  368. XCB_COLORMAP_ALLOC_NONE,
  369. colormap,
  370. parent,
  371. visual
  372. );
  373. xcb_create_window(
  374. xcb_conn, 24 /* Hardcoded? */,
  375. wid, parent,
  376. 0, 0,
  377. geometry->width,
  378. geometry->height,
  379. 0, 0,
  380. visual, mask, mask_values
  381. );
  382. swap->wi->config = fb_config[0];
  383. swap->wi->window = wid;
  384. xcb_map_window(xcb_conn, wid);
  385. XFree(fb_config);
  386. status = true;
  387. goto success;
  388. fail_visual_id:
  389. XFree(fb_config);
  390. fail_fb_config:
  391. fail_screen:
  392. fail_geometry_request:
  393. success:
  394. free(geometry);
  395. return status;
  396. }
  397. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  398. {
  399. UNUSED_PARAMETER(swap);
  400. /* Really nothing to clean up? */
  401. }
  402. extern void device_enter_context(gs_device_t *device)
  403. {
  404. GLXContext context = device->plat->context;
  405. Display *display = device->plat->display;
  406. if (device->cur_swap) {
  407. XID window = device->cur_swap->wi->window;
  408. if (!glXMakeContextCurrent(display, window, window, context)) {
  409. blog(LOG_ERROR, "Failed to make context current.");
  410. }
  411. } else {
  412. GLXPbuffer pbuf = device->plat->pbuffer;
  413. if (!glXMakeContextCurrent(display, pbuf, pbuf, context)) {
  414. blog(LOG_ERROR, "Failed to make context current.");
  415. }
  416. }
  417. }
  418. extern void device_leave_context(gs_device_t *device)
  419. {
  420. Display *display = device->plat->display;
  421. if (!glXMakeContextCurrent(display, None, None, NULL)) {
  422. blog(LOG_ERROR, "Failed to reset current context.");
  423. }
  424. }
  425. extern void gl_getclientsize(const struct gs_swap_chain *swap,
  426. uint32_t *width, uint32_t *height)
  427. {
  428. xcb_connection_t *xcb_conn = XGetXCBConnection(swap->device->plat->display);
  429. xcb_window_t window = swap->wi->window;
  430. xcb_get_geometry_reply_t *geometry = get_window_geometry(xcb_conn, window);
  431. if (geometry) {
  432. *width = geometry->width;
  433. *height = geometry->height;
  434. }
  435. free(geometry);
  436. }
  437. extern void gl_update(gs_device_t *device)
  438. {
  439. Display *display = device->plat->display;
  440. xcb_window_t window = device->cur_swap->wi->window;
  441. uint32_t values[] = {
  442. device->cur_swap->info.cx,
  443. device->cur_swap->info.cy
  444. };
  445. xcb_configure_window(
  446. XGetXCBConnection(display), window,
  447. XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  448. values
  449. );
  450. }
  451. extern void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  452. {
  453. if (device->cur_swap == swap)
  454. return;
  455. Display *dpy = device->plat->display;
  456. GLXContext ctx = device->plat->context;
  457. device->cur_swap = swap;
  458. if (swap) {
  459. XID window = swap->wi->window;
  460. if (!glXMakeContextCurrent(dpy, window, window, ctx)) {
  461. blog(LOG_ERROR, "Failed to make context current.");
  462. }
  463. } else {
  464. GLXPbuffer pbuf = device->plat->pbuffer;
  465. if (!glXMakeContextCurrent(dpy, pbuf, pbuf, ctx)) {
  466. blog(LOG_ERROR, "Failed to make context current.");
  467. }
  468. }
  469. }
  470. enum swap_type {
  471. SWAP_TYPE_NORMAL,
  472. SWAP_TYPE_EXT,
  473. SWAP_TYPE_MESA,
  474. SWAP_TYPE_SGI,
  475. };
  476. extern void device_present(gs_device_t *device)
  477. {
  478. static bool initialized = false;
  479. static enum swap_type swap_type = SWAP_TYPE_NORMAL;
  480. Display *display = device->plat->display;
  481. XID window = device->cur_swap->wi->window;
  482. if (!initialized) {
  483. if (GLAD_GLX_EXT_swap_control)
  484. swap_type = SWAP_TYPE_EXT;
  485. else if (GLAD_GLX_MESA_swap_control)
  486. swap_type = SWAP_TYPE_MESA;
  487. else if (GLAD_GLX_SGI_swap_control)
  488. swap_type = SWAP_TYPE_SGI;
  489. initialized = true;
  490. }
  491. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  492. xcb_generic_event_t *xcb_event;
  493. while((xcb_event = xcb_poll_for_event(xcb_conn))) {
  494. /* TODO: Handle XCB events. */
  495. free(xcb_event);
  496. }
  497. switch (swap_type) {
  498. case SWAP_TYPE_EXT: glXSwapIntervalEXT(display, window, 0); break;
  499. case SWAP_TYPE_MESA: glXSwapIntervalMESA(0); break;
  500. case SWAP_TYPE_SGI: glXSwapIntervalSGI(0); break;
  501. case SWAP_TYPE_NORMAL:;
  502. }
  503. glXSwapBuffers(display, window);
  504. }