gl-windows.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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 ((uint32_t)format) {
  35. case GS_RGBA:
  36. return 32;
  37. default:
  38. return 0;
  39. }
  40. }
  41. static inline int get_depth_format_bits(enum gs_zstencil_format zsformat)
  42. {
  43. switch ((uint32_t)zsformat) {
  44. case GS_Z16:
  45. return 16;
  46. case GS_Z24_S8:
  47. return 24;
  48. default:
  49. return 0;
  50. }
  51. }
  52. static inline int get_stencil_format_bits(enum gs_zstencil_format zsformat)
  53. {
  54. switch ((uint32_t)zsformat) {
  55. case GS_Z24_S8:
  56. return 8;
  57. default:
  58. return 0;
  59. }
  60. }
  61. /* would use designated initializers but Microsoft sort of sucks */
  62. static inline void init_dummy_pixel_format(PIXELFORMATDESCRIPTOR *pfd)
  63. {
  64. memset(pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  65. pfd->nSize = sizeof(pfd);
  66. pfd->nVersion = 1;
  67. pfd->iPixelType = PFD_TYPE_RGBA;
  68. pfd->cColorBits = 32;
  69. pfd->cDepthBits = 24;
  70. pfd->cStencilBits = 8;
  71. pfd->iLayerType = PFD_MAIN_PLANE;
  72. pfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
  73. 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",
  103. WS_POPUP, 0, 0, 2, 2, NULL, NULL,
  104. GetModuleHandle(NULL), NULL);
  105. if (!hwnd)
  106. blog(LOG_ERROR, "Could not create dummy context window");
  107. return hwnd;
  108. }
  109. static inline bool wgl_make_current(HDC hdc, HGLRC hglrc)
  110. {
  111. bool success = wglMakeCurrent(hdc, hglrc);
  112. if (!success)
  113. blog(LOG_ERROR,
  114. "wglMakeCurrent failed, GetLastError "
  115. "returned %lu",
  116. GetLastError());
  117. return success;
  118. }
  119. static inline HGLRC gl_init_basic_context(HDC hdc)
  120. {
  121. HGLRC hglrc = wglCreateContext(hdc);
  122. if (!hglrc) {
  123. blog(LOG_ERROR, "wglCreateContext failed, %lu", GetLastError());
  124. return NULL;
  125. }
  126. if (!wgl_make_current(hdc, hglrc)) {
  127. wglDeleteContext(hglrc);
  128. return NULL;
  129. }
  130. return hglrc;
  131. }
  132. static const int attribs[] = {
  133. #ifdef _DEBUG
  134. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
  135. #endif
  136. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0, 0};
  137. static inline HGLRC gl_init_context(HDC hdc)
  138. {
  139. #ifdef _DEBUG
  140. if (GLAD_WGL_ARB_create_context) {
  141. HGLRC hglrc = wglCreateContextAttribsARB(hdc, 0, attribs);
  142. if (!hglrc) {
  143. blog(LOG_ERROR,
  144. "wglCreateContextAttribsARB failed, "
  145. "%lu",
  146. GetLastError());
  147. return NULL;
  148. }
  149. if (!wgl_make_current(hdc, hglrc)) {
  150. wglDeleteContext(hglrc);
  151. return NULL;
  152. }
  153. return hglrc;
  154. }
  155. #endif
  156. return gl_init_basic_context(hdc);
  157. }
  158. static bool gl_dummy_context_init(struct dummy_context *dummy)
  159. {
  160. PIXELFORMATDESCRIPTOR pfd;
  161. int format_index;
  162. if (!gl_register_dummy_window_class())
  163. return false;
  164. dummy->hwnd = gl_create_dummy_window();
  165. if (!dummy->hwnd)
  166. return false;
  167. dummy->hdc = GetDC(dummy->hwnd);
  168. init_dummy_pixel_format(&pfd);
  169. format_index = ChoosePixelFormat(dummy->hdc, &pfd);
  170. if (!format_index) {
  171. blog(LOG_ERROR, "Dummy ChoosePixelFormat failed, %lu",
  172. GetLastError());
  173. return false;
  174. }
  175. if (!SetPixelFormat(dummy->hdc, format_index, &pfd)) {
  176. blog(LOG_ERROR, "Dummy SetPixelFormat failed, %lu",
  177. 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,
  249. &num_formats);
  250. if (!success || !num_formats) {
  251. blog(LOG_ERROR, "wglChoosePixelFormatARB failed, %lu",
  252. GetLastError());
  253. format = 0;
  254. }
  255. darray_free(&attribs);
  256. return format;
  257. }
  258. static inline bool gl_getpixelformat(HDC hdc, const struct gs_init_data *info,
  259. int *format, PIXELFORMATDESCRIPTOR *pfd)
  260. {
  261. if (!format)
  262. return false;
  263. *format = gl_choose_pixel_format(hdc, info);
  264. if (!DescribePixelFormat(hdc, *format, sizeof(*pfd), pfd)) {
  265. blog(LOG_ERROR, "DescribePixelFormat failed, %lu",
  266. GetLastError());
  267. return false;
  268. }
  269. return true;
  270. }
  271. static inline bool gl_setpixelformat(HDC hdc, int format,
  272. PIXELFORMATDESCRIPTOR *pfd)
  273. {
  274. if (!SetPixelFormat(hdc, format, pfd)) {
  275. blog(LOG_ERROR, "SetPixelFormat failed, %lu", GetLastError());
  276. return false;
  277. }
  278. return true;
  279. }
  280. static struct gl_windowinfo *gl_windowinfo_bare(const struct gs_init_data *info)
  281. {
  282. struct gl_windowinfo *wi = bzalloc(sizeof(struct gl_windowinfo));
  283. wi->hwnd = info->window.hwnd;
  284. wi->hdc = GetDC(wi->hwnd);
  285. if (!wi->hdc) {
  286. blog(LOG_ERROR, "Unable to get device context from window");
  287. bfree(wi);
  288. return NULL;
  289. }
  290. return wi;
  291. }
  292. #define DUMMY_WNDCLASS "Dummy GL Window Class"
  293. static bool register_dummy_class(void)
  294. {
  295. static bool created = false;
  296. WNDCLASSA wc = {0};
  297. wc.style = CS_OWNDC;
  298. wc.hInstance = GetModuleHandleW(NULL);
  299. wc.lpfnWndProc = (WNDPROC)DefWindowProcA;
  300. wc.lpszClassName = DUMMY_WNDCLASS;
  301. if (created)
  302. return true;
  303. if (!RegisterClassA(&wc)) {
  304. blog(LOG_ERROR, "Failed to register dummy GL window class, %lu",
  305. GetLastError());
  306. return false;
  307. }
  308. created = true;
  309. return true;
  310. }
  311. static bool create_dummy_window(struct gl_platform *plat)
  312. {
  313. plat->window.hwnd = CreateWindowExA(0, DUMMY_WNDCLASS,
  314. "OpenGL Dummy Window", WS_POPUP, 0,
  315. 0, 1, 1, NULL, NULL,
  316. GetModuleHandleW(NULL), NULL);
  317. if (!plat->window.hwnd) {
  318. blog(LOG_ERROR, "Failed to create dummy GL window, %lu",
  319. GetLastError());
  320. return false;
  321. }
  322. plat->window.hdc = GetDC(plat->window.hwnd);
  323. if (!plat->window.hdc) {
  324. blog(LOG_ERROR, "Failed to get dummy GL window DC (%lu)",
  325. GetLastError());
  326. return false;
  327. }
  328. return true;
  329. }
  330. static bool init_default_swap(struct gl_platform *plat, gs_device_t *device,
  331. int pixel_format, PIXELFORMATDESCRIPTOR *pfd)
  332. {
  333. if (!gl_setpixelformat(plat->window.hdc, pixel_format, pfd))
  334. return false;
  335. return true;
  336. }
  337. void gl_update(gs_device_t *device)
  338. {
  339. /* does nothing on windows */
  340. UNUSED_PARAMETER(device);
  341. }
  342. static void init_dummy_swap_info(struct gs_init_data *info)
  343. {
  344. info->format = GS_RGBA;
  345. info->zsformat = GS_ZS_NONE;
  346. }
  347. struct gl_platform *gl_platform_create(gs_device_t *device, uint32_t adapter)
  348. {
  349. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  350. struct dummy_context dummy;
  351. struct gs_init_data info = {0};
  352. int pixel_format;
  353. PIXELFORMATDESCRIPTOR pfd;
  354. memset(&dummy, 0, sizeof(struct dummy_context));
  355. init_dummy_swap_info(&info);
  356. if (!gl_dummy_context_init(&dummy))
  357. goto fail;
  358. if (!gl_init_extensions(dummy.hdc))
  359. goto fail;
  360. if (!register_dummy_class())
  361. return false;
  362. if (!create_dummy_window(plat))
  363. return false;
  364. /* you have to have a dummy context open before you can actually
  365. * use wglChoosePixelFormatARB */
  366. if (!gl_getpixelformat(dummy.hdc, &info, &pixel_format, &pfd))
  367. goto fail;
  368. gl_dummy_context_free(&dummy);
  369. if (!init_default_swap(plat, device, pixel_format, &pfd))
  370. goto fail;
  371. plat->hrc = gl_init_context(plat->window.hdc);
  372. if (!plat->hrc)
  373. goto fail;
  374. if (!gladLoadGL()) {
  375. blog(LOG_ERROR, "Failed to initialize OpenGL entry functions.");
  376. goto fail;
  377. }
  378. UNUSED_PARAMETER(adapter);
  379. return plat;
  380. fail:
  381. blog(LOG_ERROR, "gl_platform_create failed");
  382. gl_platform_destroy(plat);
  383. gl_dummy_context_free(&dummy);
  384. return NULL;
  385. }
  386. void gl_platform_destroy(struct gl_platform *plat)
  387. {
  388. if (plat) {
  389. if (plat->hrc) {
  390. wglMakeCurrent(NULL, NULL);
  391. wglDeleteContext(plat->hrc);
  392. }
  393. if (plat->window.hdc)
  394. ReleaseDC(plat->window.hwnd, plat->window.hdc);
  395. if (plat->window.hwnd)
  396. DestroyWindow(plat->window.hwnd);
  397. bfree(plat);
  398. }
  399. }
  400. bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  401. {
  402. UNUSED_PARAMETER(swap);
  403. return true;
  404. }
  405. void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  406. {
  407. UNUSED_PARAMETER(swap);
  408. }
  409. struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
  410. {
  411. struct gl_windowinfo *wi = gl_windowinfo_bare(info);
  412. PIXELFORMATDESCRIPTOR pfd;
  413. int pixel_format;
  414. if (!wi)
  415. return NULL;
  416. if (!gl_getpixelformat(wi->hdc, info, &pixel_format, &pfd))
  417. goto fail;
  418. if (!gl_setpixelformat(wi->hdc, pixel_format, &pfd))
  419. goto fail;
  420. return wi;
  421. fail:
  422. blog(LOG_ERROR, "gl_windowinfo_create failed");
  423. gl_windowinfo_destroy(wi);
  424. return NULL;
  425. }
  426. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  427. {
  428. if (wi) {
  429. if (wi->hdc)
  430. ReleaseDC(wi->hwnd, wi->hdc);
  431. bfree(wi);
  432. }
  433. }
  434. void device_enter_context(gs_device_t *device)
  435. {
  436. HDC hdc = device->plat->window.hdc;
  437. if (device->cur_swap)
  438. hdc = device->cur_swap->wi->hdc;
  439. if (!wgl_make_current(hdc, device->plat->hrc))
  440. blog(LOG_ERROR, "device_enter_context (GL) failed");
  441. }
  442. void device_leave_context(gs_device_t *device)
  443. {
  444. wglMakeCurrent(NULL, NULL);
  445. UNUSED_PARAMETER(device);
  446. }
  447. void *device_get_device_obj(gs_device_t *device)
  448. {
  449. return device->plat->hrc;
  450. }
  451. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  452. {
  453. HDC hdc = device->plat->window.hdc;
  454. if (device->cur_swap == swap)
  455. return;
  456. device->cur_swap = swap;
  457. if (swap)
  458. hdc = swap->wi->hdc;
  459. if (hdc) {
  460. if (!wgl_make_current(hdc, device->plat->hrc))
  461. blog(LOG_ERROR, "device_load_swapchain (GL) failed");
  462. }
  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,
  475. uint32_t *height)
  476. {
  477. RECT rc;
  478. if (swap) {
  479. GetClientRect(swap->wi->hwnd, &rc);
  480. *width = rc.right;
  481. *height = rc.bottom;
  482. } else {
  483. *width = 0;
  484. *height = 0;
  485. }
  486. }
  487. EXPORT bool device_gdi_texture_available(void)
  488. {
  489. return false;
  490. }
  491. EXPORT bool device_shared_texture_available(void)
  492. {
  493. return false;
  494. }