gl-windows.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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,
  104. 0, 0, 2, 2,
  105. NULL, NULL, GetModuleHandle(NULL), NULL);
  106. if (!hwnd)
  107. blog(LOG_ERROR, "Could not create dummy context window");
  108. return hwnd;
  109. }
  110. static inline bool wgl_make_current(HDC hdc, HGLRC hglrc)
  111. {
  112. bool success = wglMakeCurrent(hdc, hglrc);
  113. if (!success)
  114. blog(LOG_ERROR, "wglMakeCurrent failed, GetLastError "
  115. "returned %lu", 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 const int attribs[] =
  132. {
  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,
  137. 0, 0
  138. };
  139. static inline HGLRC gl_init_context(HDC hdc)
  140. {
  141. #ifdef _DEBUG
  142. if (GLAD_WGL_ARB_create_context) {
  143. HGLRC hglrc = wglCreateContextAttribsARB(hdc, 0, attribs);
  144. if (!hglrc) {
  145. blog(LOG_ERROR, "wglCreateContextAttribsARB failed, "
  146. "%lu", 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, 0, 1, 1,
  315. NULL, NULL, GetModuleHandleW(NULL), NULL);
  316. if (!plat->window.hwnd) {
  317. blog(LOG_ERROR, "Failed to create dummy GL window, %lu",
  318. GetLastError());
  319. return false;
  320. }
  321. plat->window.hdc = GetDC(plat->window.hwnd);
  322. if (!plat->window.hdc) {
  323. blog(LOG_ERROR, "Failed to get dummy GL window DC (%lu)",
  324. GetLastError());
  325. return false;
  326. }
  327. return true;
  328. }
  329. static bool init_default_swap(struct gl_platform *plat, gs_device_t *device,
  330. int pixel_format, PIXELFORMATDESCRIPTOR *pfd)
  331. {
  332. if (!gl_setpixelformat(plat->window.hdc, pixel_format, pfd))
  333. return false;
  334. return true;
  335. }
  336. void gl_update(gs_device_t *device)
  337. {
  338. /* does nothing on windows */
  339. UNUSED_PARAMETER(device);
  340. }
  341. static void init_dummy_swap_info(struct gs_init_data *info)
  342. {
  343. info->format = GS_RGBA;
  344. info->zsformat = GS_ZS_NONE;
  345. }
  346. struct gl_platform *gl_platform_create(gs_device_t *device, uint32_t adapter)
  347. {
  348. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  349. struct dummy_context dummy;
  350. struct gs_init_data info = {0};
  351. int pixel_format;
  352. PIXELFORMATDESCRIPTOR pfd;
  353. memset(&dummy, 0, sizeof(struct dummy_context));
  354. init_dummy_swap_info(&info);
  355. if (!gl_dummy_context_init(&dummy))
  356. goto fail;
  357. if (!gl_init_extensions(dummy.hdc))
  358. goto fail;
  359. if (!register_dummy_class())
  360. return false;
  361. if (!create_dummy_window(plat))
  362. return false;
  363. /* you have to have a dummy context open before you can actually
  364. * use wglChoosePixelFormatARB */
  365. if (!gl_getpixelformat(dummy.hdc, &info, &pixel_format, &pfd))
  366. goto fail;
  367. gl_dummy_context_free(&dummy);
  368. if (!init_default_swap(plat, device, pixel_format, &pfd))
  369. goto fail;
  370. plat->hrc = gl_init_context(plat->window.hdc);
  371. if (!plat->hrc)
  372. goto fail;
  373. if (!gladLoadGL()) {
  374. blog(LOG_ERROR, "Failed to initialize OpenGL entry functions.");
  375. goto fail;
  376. }
  377. UNUSED_PARAMETER(adapter);
  378. return plat;
  379. fail:
  380. blog(LOG_ERROR, "gl_platform_create failed");
  381. gl_platform_destroy(plat);
  382. gl_dummy_context_free(&dummy);
  383. return NULL;
  384. }
  385. void gl_platform_destroy(struct gl_platform *plat)
  386. {
  387. if (plat) {
  388. if (plat->hrc) {
  389. wglMakeCurrent(NULL, NULL);
  390. wglDeleteContext(plat->hrc);
  391. }
  392. if (plat->window.hdc)
  393. ReleaseDC(plat->window.hwnd, plat->window.hdc);
  394. if (plat->window.hwnd)
  395. DestroyWindow(plat->window.hwnd);
  396. bfree(plat);
  397. }
  398. }
  399. bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  400. {
  401. UNUSED_PARAMETER(swap);
  402. return true;
  403. }
  404. void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  405. {
  406. UNUSED_PARAMETER(swap);
  407. }
  408. struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
  409. {
  410. struct gl_windowinfo *wi = gl_windowinfo_bare(info);
  411. PIXELFORMATDESCRIPTOR pfd;
  412. int pixel_format;
  413. if (!wi)
  414. return NULL;
  415. if (!gl_getpixelformat(wi->hdc, info, &pixel_format, &pfd))
  416. goto fail;
  417. if (!gl_setpixelformat(wi->hdc, pixel_format, &pfd))
  418. goto fail;
  419. return wi;
  420. fail:
  421. blog(LOG_ERROR, "gl_windowinfo_create failed");
  422. gl_windowinfo_destroy(wi);
  423. return NULL;
  424. }
  425. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  426. {
  427. if (wi) {
  428. if (wi->hdc)
  429. ReleaseDC(wi->hwnd, wi->hdc);
  430. bfree(wi);
  431. }
  432. }
  433. void device_enter_context(gs_device_t *device)
  434. {
  435. HDC hdc = device->plat->window.hdc;
  436. if (device->cur_swap)
  437. hdc = device->cur_swap->wi->hdc;
  438. if (!wgl_make_current(hdc, device->plat->hrc))
  439. blog(LOG_ERROR, "device_load_swapchain (GL) failed");
  440. }
  441. void device_leave_context(gs_device_t *device)
  442. {
  443. wglMakeCurrent(NULL, NULL);
  444. UNUSED_PARAMETER(device);
  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. void device_present(gs_device_t *device)
  460. {
  461. if (!SwapBuffers(device->cur_swap->wi->hdc)) {
  462. blog(LOG_ERROR, "SwapBuffers failed, GetLastError "
  463. "returned %lu", GetLastError());
  464. blog(LOG_ERROR, "device_present (GL) failed");
  465. }
  466. }
  467. extern void gl_getclientsize(const struct gs_swap_chain *swap,
  468. uint32_t *width, uint32_t *height)
  469. {
  470. RECT rc;
  471. if (swap) {
  472. GetClientRect(swap->wi->hwnd, &rc);
  473. *width = rc.right;
  474. *height = rc.bottom;
  475. } else {
  476. *width = 0;
  477. *height = 0;
  478. }
  479. }
  480. EXPORT bool device_gdi_texture_available(void)
  481. {
  482. return false;
  483. }
  484. EXPORT bool device_shared_texture_available(void)
  485. {
  486. return false;
  487. }