gl-windows.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[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. #define WIN32_LEAN_AND_MEAN
  15. #include <windows.h>
  16. #include <util/darray.h>
  17. #include "gl-subsystem.h"
  18. #include <glad/glad_wgl.h>
  19. /* Basically swapchain-specific information. Fortunately for windows this is
  20. * super basic stuff */
  21. struct gl_windowinfo {
  22. HWND hwnd;
  23. HDC hdc;
  24. };
  25. /* Like the other subsystems, the GL subsystem has one swap chain created by
  26. * default. */
  27. struct gl_platform {
  28. HGLRC hrc;
  29. struct gl_windowinfo window;
  30. };
  31. /* For now, only support basic 32bit formats for graphics output. */
  32. static inline int get_color_format_bits(enum gs_color_format format)
  33. {
  34. switch (format) {
  35. case GS_RGBA:
  36. case GS_BGRA:
  37. return 32;
  38. default:
  39. return 0;
  40. }
  41. }
  42. static inline int get_depth_format_bits(enum gs_zstencil_format zsformat)
  43. {
  44. switch (zsformat) {
  45. case GS_Z16:
  46. return 16;
  47. case GS_Z24_S8:
  48. return 24;
  49. default:
  50. return 0;
  51. }
  52. }
  53. static inline int get_stencil_format_bits(enum gs_zstencil_format zsformat)
  54. {
  55. switch (zsformat) {
  56. case GS_Z24_S8:
  57. return 8;
  58. default:
  59. return 0;
  60. }
  61. }
  62. /* would use designated initializers but Microsoft sort of sucks */
  63. static inline void init_dummy_pixel_format(PIXELFORMATDESCRIPTOR *pfd)
  64. {
  65. memset(pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  66. pfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
  67. pfd->nVersion = 1;
  68. pfd->iPixelType = PFD_TYPE_RGBA;
  69. pfd->cColorBits = 32;
  70. pfd->cDepthBits = 24;
  71. pfd->cStencilBits = 8;
  72. pfd->iLayerType = PFD_MAIN_PLANE;
  73. pfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  74. }
  75. static const char *dummy_window_class = "GLDummyWindow";
  76. static bool registered_dummy_window_class = false;
  77. struct dummy_context {
  78. HWND hwnd;
  79. HGLRC hrc;
  80. HDC hdc;
  81. };
  82. /* Need a dummy window for the dummy context */
  83. static bool gl_register_dummy_window_class(void)
  84. {
  85. WNDCLASSA wc;
  86. if (registered_dummy_window_class)
  87. return true;
  88. memset(&wc, 0, sizeof(wc));
  89. wc.style = CS_OWNDC;
  90. wc.hInstance = GetModuleHandle(NULL);
  91. wc.lpfnWndProc = DefWindowProc;
  92. wc.lpszClassName = dummy_window_class;
  93. if (!RegisterClassA(&wc)) {
  94. blog(LOG_ERROR, "Could not create dummy window class");
  95. return false;
  96. }
  97. registered_dummy_window_class = true;
  98. return true;
  99. }
  100. static inline HWND gl_create_dummy_window(void)
  101. {
  102. HWND hwnd = CreateWindowExA(0, dummy_window_class, "Dummy GL Window", WS_POPUP, 0, 0, 2, 2, NULL, NULL,
  103. GetModuleHandle(NULL), NULL);
  104. if (!hwnd)
  105. blog(LOG_ERROR, "Could not create dummy context window");
  106. return hwnd;
  107. }
  108. static inline bool wgl_make_current(HDC hdc, HGLRC hglrc)
  109. {
  110. bool success = wglMakeCurrent(hdc, hglrc);
  111. if (!success)
  112. blog(LOG_ERROR,
  113. "wglMakeCurrent failed, GetLastError "
  114. "returned %lu",
  115. GetLastError());
  116. return success;
  117. }
  118. static inline HGLRC gl_init_basic_context(HDC hdc)
  119. {
  120. HGLRC hglrc = wglCreateContext(hdc);
  121. if (!hglrc) {
  122. blog(LOG_ERROR, "wglCreateContext failed, %lu", GetLastError());
  123. return NULL;
  124. }
  125. if (!wgl_make_current(hdc, hglrc)) {
  126. wglDeleteContext(hglrc);
  127. return NULL;
  128. }
  129. return hglrc;
  130. }
  131. static inline HGLRC gl_init_context(HDC hdc)
  132. {
  133. static const int attribs[] = {
  134. #ifdef _DEBUG
  135. WGL_CONTEXT_FLAGS_ARB,
  136. WGL_CONTEXT_DEBUG_BIT_ARB,
  137. #endif
  138. WGL_CONTEXT_PROFILE_MASK_ARB,
  139. WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  140. WGL_CONTEXT_MAJOR_VERSION_ARB,
  141. 3,
  142. WGL_CONTEXT_MINOR_VERSION_ARB,
  143. 3,
  144. 0,
  145. 0};
  146. HGLRC hglrc = wglCreateContextAttribsARB(hdc, 0, attribs);
  147. if (!hglrc) {
  148. blog(LOG_ERROR,
  149. "wglCreateContextAttribsARB failed, "
  150. "%lu",
  151. GetLastError());
  152. return NULL;
  153. }
  154. if (!wgl_make_current(hdc, hglrc)) {
  155. wglDeleteContext(hglrc);
  156. return NULL;
  157. }
  158. return hglrc;
  159. }
  160. static bool gl_dummy_context_init(struct dummy_context *dummy)
  161. {
  162. PIXELFORMATDESCRIPTOR pfd;
  163. int format_index;
  164. if (!gl_register_dummy_window_class())
  165. return false;
  166. dummy->hwnd = gl_create_dummy_window();
  167. if (!dummy->hwnd)
  168. return false;
  169. dummy->hdc = GetDC(dummy->hwnd);
  170. init_dummy_pixel_format(&pfd);
  171. format_index = ChoosePixelFormat(dummy->hdc, &pfd);
  172. if (!format_index) {
  173. blog(LOG_ERROR, "Dummy ChoosePixelFormat failed, %lu", GetLastError());
  174. return false;
  175. }
  176. if (!SetPixelFormat(dummy->hdc, format_index, &pfd)) {
  177. blog(LOG_ERROR, "Dummy SetPixelFormat failed, %lu", GetLastError());
  178. return false;
  179. }
  180. dummy->hrc = gl_init_basic_context(dummy->hdc);
  181. if (!dummy->hrc) {
  182. blog(LOG_ERROR, "Failed to initialize dummy context");
  183. return false;
  184. }
  185. return true;
  186. }
  187. static inline void gl_dummy_context_free(struct dummy_context *dummy)
  188. {
  189. wglMakeCurrent(NULL, NULL);
  190. wglDeleteContext(dummy->hrc);
  191. DestroyWindow(dummy->hwnd);
  192. memset(dummy, 0, sizeof(struct dummy_context));
  193. }
  194. static inline void required_extension_error(const char *extension)
  195. {
  196. blog(LOG_ERROR, "OpenGL extension %s is required", extension);
  197. }
  198. static bool gl_init_extensions(HDC hdc)
  199. {
  200. if (!gladLoadWGL(hdc)) {
  201. blog(LOG_ERROR, "Failed to load WGL entry functions.");
  202. return false;
  203. }
  204. if (!GLAD_WGL_ARB_pixel_format) {
  205. required_extension_error("ARB_pixel_format");
  206. return false;
  207. }
  208. if (!GLAD_WGL_ARB_create_context) {
  209. required_extension_error("ARB_create_context");
  210. return false;
  211. }
  212. if (!GLAD_WGL_ARB_create_context_profile) {
  213. required_extension_error("ARB_create_context_profile");
  214. return false;
  215. }
  216. return true;
  217. }
  218. static inline void add_attrib(struct darray *list, int attrib, int val)
  219. {
  220. darray_push_back(sizeof(int), list, &attrib);
  221. darray_push_back(sizeof(int), list, &val);
  222. }
  223. /* Creates the real pixel format for the target window */
  224. static int gl_choose_pixel_format(HDC hdc, const struct gs_init_data *info)
  225. {
  226. struct darray attribs;
  227. int color_bits = get_color_format_bits(info->format);
  228. int depth_bits = get_depth_format_bits(info->zsformat);
  229. int stencil_bits = get_stencil_format_bits(info->zsformat);
  230. UINT num_formats;
  231. BOOL success;
  232. int format;
  233. if (!color_bits) {
  234. blog(LOG_ERROR, "gl_init_pixel_format: color format not "
  235. "supported");
  236. return false;
  237. }
  238. darray_init(&attribs);
  239. add_attrib(&attribs, WGL_DRAW_TO_WINDOW_ARB, GL_TRUE);
  240. add_attrib(&attribs, WGL_SUPPORT_OPENGL_ARB, GL_TRUE);
  241. add_attrib(&attribs, WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB);
  242. add_attrib(&attribs, WGL_DOUBLE_BUFFER_ARB, GL_TRUE);
  243. add_attrib(&attribs, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB);
  244. add_attrib(&attribs, WGL_COLOR_BITS_ARB, color_bits);
  245. add_attrib(&attribs, WGL_DEPTH_BITS_ARB, depth_bits);
  246. add_attrib(&attribs, WGL_STENCIL_BITS_ARB, stencil_bits);
  247. add_attrib(&attribs, 0, 0);
  248. success = wglChoosePixelFormatARB(hdc, attribs.array, NULL, 1, &format, &num_formats);
  249. if (!success || !num_formats) {
  250. blog(LOG_ERROR, "wglChoosePixelFormatARB failed, %lu", GetLastError());
  251. format = 0;
  252. }
  253. darray_free(&attribs);
  254. return format;
  255. }
  256. static inline bool gl_getpixelformat(HDC hdc, const struct gs_init_data *info, int *format, PIXELFORMATDESCRIPTOR *pfd)
  257. {
  258. if (!format)
  259. return false;
  260. *format = gl_choose_pixel_format(hdc, info);
  261. if (!DescribePixelFormat(hdc, *format, sizeof(*pfd), pfd)) {
  262. blog(LOG_ERROR, "DescribePixelFormat failed, %lu", GetLastError());
  263. return false;
  264. }
  265. return true;
  266. }
  267. static inline bool gl_setpixelformat(HDC hdc, int format, PIXELFORMATDESCRIPTOR *pfd)
  268. {
  269. if (!SetPixelFormat(hdc, format, pfd)) {
  270. blog(LOG_ERROR, "SetPixelFormat failed, %lu", GetLastError());
  271. return false;
  272. }
  273. return true;
  274. }
  275. static struct gl_windowinfo *gl_windowinfo_bare(const struct gs_init_data *info)
  276. {
  277. struct gl_windowinfo *wi = bzalloc(sizeof(struct gl_windowinfo));
  278. wi->hwnd = info->window.hwnd;
  279. wi->hdc = GetDC(wi->hwnd);
  280. if (!wi->hdc) {
  281. blog(LOG_ERROR, "Unable to get device context from window");
  282. bfree(wi);
  283. return NULL;
  284. }
  285. return wi;
  286. }
  287. #define DUMMY_WNDCLASS "Dummy GL Window Class"
  288. static bool register_dummy_class(void)
  289. {
  290. static bool created = false;
  291. WNDCLASSA wc = {0};
  292. wc.style = CS_OWNDC;
  293. wc.hInstance = GetModuleHandleW(NULL);
  294. wc.lpfnWndProc = (WNDPROC)DefWindowProcA;
  295. wc.lpszClassName = DUMMY_WNDCLASS;
  296. if (created)
  297. return true;
  298. if (!RegisterClassA(&wc)) {
  299. blog(LOG_ERROR, "Failed to register dummy GL window class, %lu", GetLastError());
  300. return false;
  301. }
  302. created = true;
  303. return true;
  304. }
  305. static bool create_dummy_window(struct gl_platform *plat)
  306. {
  307. plat->window.hwnd = CreateWindowExA(0, DUMMY_WNDCLASS, "OpenGL Dummy Window", WS_POPUP, 0, 0, 1, 1, NULL, NULL,
  308. GetModuleHandleW(NULL), NULL);
  309. if (!plat->window.hwnd) {
  310. blog(LOG_ERROR, "Failed to create dummy GL window, %lu", GetLastError());
  311. return false;
  312. }
  313. plat->window.hdc = GetDC(plat->window.hwnd);
  314. if (!plat->window.hdc) {
  315. blog(LOG_ERROR, "Failed to get dummy GL window DC (%lu)", GetLastError());
  316. return false;
  317. }
  318. return true;
  319. }
  320. static bool init_default_swap(struct gl_platform *plat, gs_device_t *device, int pixel_format,
  321. PIXELFORMATDESCRIPTOR *pfd)
  322. {
  323. if (!gl_setpixelformat(plat->window.hdc, pixel_format, pfd))
  324. return false;
  325. return true;
  326. }
  327. void gl_update(gs_device_t *device)
  328. {
  329. /* does nothing on windows */
  330. UNUSED_PARAMETER(device);
  331. }
  332. void gl_clear_context(gs_device_t *device)
  333. {
  334. UNUSED_PARAMETER(device);
  335. wglMakeCurrent(NULL, NULL);
  336. }
  337. static void init_dummy_swap_info(struct gs_init_data *info)
  338. {
  339. info->format = GS_RGBA;
  340. info->zsformat = GS_ZS_NONE;
  341. }
  342. struct gl_platform *gl_platform_create(gs_device_t *device, uint32_t adapter)
  343. {
  344. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  345. struct dummy_context dummy;
  346. struct gs_init_data info = {0};
  347. int pixel_format;
  348. PIXELFORMATDESCRIPTOR pfd;
  349. memset(&dummy, 0, sizeof(struct dummy_context));
  350. init_dummy_swap_info(&info);
  351. if (!gl_dummy_context_init(&dummy))
  352. goto fail;
  353. if (!gl_init_extensions(dummy.hdc))
  354. goto fail;
  355. if (!register_dummy_class())
  356. return false;
  357. if (!create_dummy_window(plat))
  358. return false;
  359. /* you have to have a dummy context open before you can actually
  360. * use wglChoosePixelFormatARB */
  361. if (!gl_getpixelformat(dummy.hdc, &info, &pixel_format, &pfd))
  362. goto fail;
  363. gl_dummy_context_free(&dummy);
  364. if (!init_default_swap(plat, device, pixel_format, &pfd))
  365. goto fail;
  366. plat->hrc = gl_init_context(plat->window.hdc);
  367. if (!plat->hrc)
  368. goto fail;
  369. if (!gladLoadGL()) {
  370. blog(LOG_ERROR, "Failed to initialize OpenGL entry functions.");
  371. goto fail;
  372. }
  373. UNUSED_PARAMETER(adapter);
  374. return plat;
  375. fail:
  376. blog(LOG_ERROR, "gl_platform_create failed");
  377. gl_platform_destroy(plat);
  378. gl_dummy_context_free(&dummy);
  379. return NULL;
  380. }
  381. void gl_platform_destroy(struct gl_platform *plat)
  382. {
  383. if (plat) {
  384. if (plat->hrc) {
  385. wglMakeCurrent(NULL, NULL);
  386. wglDeleteContext(plat->hrc);
  387. }
  388. if (plat->window.hdc)
  389. ReleaseDC(plat->window.hwnd, plat->window.hdc);
  390. if (plat->window.hwnd)
  391. DestroyWindow(plat->window.hwnd);
  392. bfree(plat);
  393. }
  394. }
  395. bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  396. {
  397. UNUSED_PARAMETER(swap);
  398. return true;
  399. }
  400. void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  401. {
  402. UNUSED_PARAMETER(swap);
  403. }
  404. struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
  405. {
  406. struct gl_windowinfo *wi = gl_windowinfo_bare(info);
  407. PIXELFORMATDESCRIPTOR pfd;
  408. int pixel_format;
  409. if (!wi)
  410. return NULL;
  411. if (!gl_getpixelformat(wi->hdc, info, &pixel_format, &pfd))
  412. goto fail;
  413. if (!gl_setpixelformat(wi->hdc, pixel_format, &pfd))
  414. goto fail;
  415. return wi;
  416. fail:
  417. blog(LOG_ERROR, "gl_windowinfo_create failed");
  418. gl_windowinfo_destroy(wi);
  419. return NULL;
  420. }
  421. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  422. {
  423. if (wi) {
  424. if (wi->hdc)
  425. ReleaseDC(wi->hwnd, wi->hdc);
  426. bfree(wi);
  427. }
  428. }
  429. void device_enter_context(gs_device_t *device)
  430. {
  431. HDC hdc = device->plat->window.hdc;
  432. if (device->cur_swap)
  433. hdc = device->cur_swap->wi->hdc;
  434. if (!wgl_make_current(hdc, device->plat->hrc))
  435. blog(LOG_ERROR, "device_enter_context (GL) failed");
  436. }
  437. void device_leave_context(gs_device_t *device)
  438. {
  439. UNUSED_PARAMETER(device);
  440. wglMakeCurrent(NULL, NULL);
  441. }
  442. void *device_get_device_obj(gs_device_t *device)
  443. {
  444. return device->plat->hrc;
  445. }
  446. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  447. {
  448. HDC hdc = device->plat->window.hdc;
  449. if (device->cur_swap == swap)
  450. return;
  451. device->cur_swap = swap;
  452. if (swap)
  453. hdc = swap->wi->hdc;
  454. if (hdc) {
  455. if (!wgl_make_current(hdc, device->plat->hrc))
  456. blog(LOG_ERROR, "device_load_swapchain (GL) failed");
  457. }
  458. }
  459. bool device_is_present_ready(gs_device_t *device)
  460. {
  461. UNUSED_PARAMETER(device);
  462. return true;
  463. }
  464. void device_present(gs_device_t *device)
  465. {
  466. if (!SwapBuffers(device->cur_swap->wi->hdc)) {
  467. blog(LOG_ERROR,
  468. "SwapBuffers failed, GetLastError "
  469. "returned %lu",
  470. GetLastError());
  471. blog(LOG_ERROR, "device_present (GL) failed");
  472. }
  473. }
  474. extern void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width, uint32_t *height)
  475. {
  476. RECT rc;
  477. if (swap) {
  478. GetClientRect(swap->wi->hwnd, &rc);
  479. *width = rc.right;
  480. *height = rc.bottom;
  481. } else {
  482. *width = 0;
  483. *height = 0;
  484. }
  485. }
  486. EXPORT bool device_is_monitor_hdr(gs_device_t *device, void *monitor)
  487. {
  488. return false;
  489. }
  490. EXPORT bool device_gdi_texture_available(void)
  491. {
  492. return false;
  493. }
  494. EXPORT bool device_shared_texture_available(void)
  495. {
  496. return false;
  497. }