1
0

gl-x11.c 15 KB

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