gl-windows.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 3 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 "glew/include/GL/wglew.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 gs_swap_chain swap;
  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 %u", 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, %u", 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. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  134. WGL_CONTEXT_MINOR_VERSION_ARB, 2,
  135. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB |
  136. WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  137. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  138. 0, 0
  139. };
  140. static inline HGLRC gl_init_context(HDC hdc)
  141. {
  142. #ifdef _DEBUG
  143. if (WGLEW_ARB_create_context) {
  144. HGLRC hglrc = wglCreateContextAttribsARB(hdc, 0, attribs);
  145. if (!hglrc) {
  146. blog(LOG_ERROR, "wglCreateContextAttribsARB failed, %u",
  147. GetLastError());
  148. return NULL;
  149. }
  150. if (!wgl_make_current(hdc, hglrc)) {
  151. wglDeleteContext(hglrc);
  152. return NULL;
  153. }
  154. return hglrc;
  155. }
  156. #endif
  157. return gl_init_basic_context(hdc);
  158. }
  159. static bool gl_dummy_context_init(struct dummy_context *dummy)
  160. {
  161. PIXELFORMATDESCRIPTOR pfd;
  162. int format_index;
  163. if (!gl_register_dummy_window_class())
  164. return false;
  165. dummy->hwnd = gl_create_dummy_window();
  166. if (!dummy->hwnd)
  167. return false;
  168. dummy->hdc = GetDC(dummy->hwnd);
  169. init_dummy_pixel_format(&pfd);
  170. format_index = ChoosePixelFormat(dummy->hdc, &pfd);
  171. if (!format_index) {
  172. blog(LOG_ERROR, "Dummy ChoosePixelFormat failed, %u",
  173. GetLastError());
  174. return false;
  175. }
  176. if (!SetPixelFormat(dummy->hdc, format_index, &pfd)) {
  177. blog(LOG_ERROR, "Dummy SetPixelFormat failed, %u",
  178. GetLastError());
  179. return false;
  180. }
  181. dummy->hrc = gl_init_basic_context(dummy->hdc);
  182. if (!dummy->hrc) {
  183. blog(LOG_ERROR, "Failed to initialize dummy context");
  184. return false;
  185. }
  186. return true;
  187. }
  188. static inline void gl_dummy_context_free(struct dummy_context *dummy)
  189. {
  190. wglMakeCurrent(NULL, NULL);
  191. wglDeleteContext(dummy->hrc);
  192. DestroyWindow(dummy->hwnd);
  193. memset(dummy, 0, sizeof(struct dummy_context));
  194. }
  195. static inline void required_extension_error(const char *extension)
  196. {
  197. blog(LOG_ERROR, "OpenGL extension %s is required", extension);
  198. }
  199. static bool gl_init_extensions(device_t device)
  200. {
  201. GLenum errorcode = glewInit();
  202. if (errorcode != GLEW_OK) {
  203. blog(LOG_ERROR, "glewInit failed, %u", errorcode);
  204. return false;
  205. }
  206. if (!GLEW_VERSION_2_1) {
  207. blog(LOG_ERROR, "OpenGL 2.1 minimum required by the graphics "
  208. "adapter");
  209. return false;
  210. }
  211. if (!GLEW_ARB_framebuffer_object) {
  212. required_extension_error("GL_ARB_framebuffer_object");
  213. return false;
  214. }
  215. if (!WGLEW_ARB_pixel_format) {
  216. required_extension_error("WGL_ARB_pixel_format");
  217. return false;
  218. }
  219. if (GLEW_ARB_copy_image)
  220. device->copy_type = COPY_TYPE_ARB;
  221. else if (GLEW_NV_copy_image)
  222. device->copy_type = COPY_TYPE_NV;
  223. else
  224. device->copy_type = COPY_TYPE_FBO_BLIT;
  225. return true;
  226. }
  227. static inline void add_attrib(struct darray *list, int attrib, int val)
  228. {
  229. darray_push_back(sizeof(int), list, &attrib);
  230. darray_push_back(sizeof(int), list, &val);
  231. }
  232. /* Creates the real pixel format for the target window */
  233. static int gl_choose_pixel_format(HDC hdc, struct gs_init_data *info)
  234. {
  235. struct darray attribs;
  236. int color_bits = get_color_format_bits(info->format);
  237. int depth_bits = get_depth_format_bits(info->zsformat);
  238. int stencil_bits = get_stencil_format_bits(info->zsformat);
  239. UINT num_formats;
  240. BOOL success;
  241. int format;
  242. if (!color_bits) {
  243. blog(LOG_ERROR, "gl_init_pixel_format: color format not "
  244. "supported");
  245. return false;
  246. }
  247. darray_init(&attribs);
  248. add_attrib(&attribs, WGL_DRAW_TO_WINDOW_ARB, GL_TRUE);
  249. add_attrib(&attribs, WGL_SUPPORT_OPENGL_ARB, GL_TRUE);
  250. add_attrib(&attribs, WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB);
  251. add_attrib(&attribs, WGL_DOUBLE_BUFFER_ARB, GL_TRUE);
  252. add_attrib(&attribs, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB);
  253. add_attrib(&attribs, WGL_COLOR_BITS_ARB, color_bits);
  254. add_attrib(&attribs, WGL_DEPTH_BITS_ARB, depth_bits);
  255. add_attrib(&attribs, WGL_STENCIL_BITS_ARB, stencil_bits);
  256. add_attrib(&attribs, 0, 0);
  257. success = wglChoosePixelFormatARB(hdc, attribs.array, NULL, 1, &format,
  258. &num_formats);
  259. if (!success || !num_formats) {
  260. blog(LOG_ERROR, "wglChoosePixelFormatARB failed, %u",
  261. GetLastError());
  262. format = 0;
  263. }
  264. darray_free(&attribs);
  265. return format;
  266. }
  267. static inline bool gl_getpixelformat(HDC hdc, struct gs_init_data *info,
  268. int *format, PIXELFORMATDESCRIPTOR *pfd)
  269. {
  270. *format = gl_choose_pixel_format(hdc, info);
  271. if (!format)
  272. return false;
  273. if (!DescribePixelFormat(hdc, *format, sizeof(*pfd), pfd)) {
  274. blog(LOG_ERROR, "DescribePixelFormat failed, %u",
  275. GetLastError());
  276. return false;
  277. }
  278. return true;
  279. }
  280. static inline bool gl_setpixelformat(HDC hdc, int format,
  281. PIXELFORMATDESCRIPTOR *pfd)
  282. {
  283. if (!SetPixelFormat(hdc, format, pfd)) {
  284. blog(LOG_ERROR, "SetPixelFormat failed, %u", GetLastError());
  285. return false;
  286. }
  287. return true;
  288. }
  289. static struct gl_windowinfo *gl_windowinfo_bare(struct gs_init_data *info)
  290. {
  291. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  292. memset(wi, 0, sizeof(struct gl_windowinfo));
  293. wi->hwnd = info->hwnd;
  294. wi->hdc = GetDC(wi->hwnd);
  295. if (!wi->hdc) {
  296. blog(LOG_ERROR, "Unable to get device context from window");
  297. bfree(wi);
  298. return NULL;
  299. }
  300. return wi;
  301. }
  302. static bool init_default_swap(struct gl_platform *plat, device_t device,
  303. int pixel_format, PIXELFORMATDESCRIPTOR *pfd,
  304. struct gs_init_data *info)
  305. {
  306. plat->swap.device = device;
  307. plat->swap.info = *info;
  308. plat->swap.wi = gl_windowinfo_bare(info);
  309. if (!plat->swap.wi)
  310. return false;
  311. if (!gl_setpixelformat(plat->swap.wi->hdc, pixel_format, pfd))
  312. return false;
  313. return true;
  314. }
  315. #ifdef _DEBUG
  316. static void APIENTRY gl_debug_message_amd(GLuint id,
  317. GLenum category,
  318. GLenum severity,
  319. GLsizei length,
  320. const GLchar *msg,
  321. void *param)
  322. {
  323. OutputDebugStringA(msg);
  324. OutputDebugStringA("\n");
  325. }
  326. #endif
  327. struct gl_platform *gl_platform_create(device_t device,
  328. struct gs_init_data *info)
  329. {
  330. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  331. struct dummy_context dummy;
  332. int pixel_format;
  333. PIXELFORMATDESCRIPTOR pfd;
  334. memset(plat, 0, sizeof(struct gl_platform));
  335. memset(&dummy, 0, sizeof(struct dummy_context));
  336. if (!gl_dummy_context_init(&dummy))
  337. goto fail;
  338. if (!gl_init_extensions(device))
  339. goto fail;
  340. /* you have to have a dummy context open before you can actually
  341. * use wglChoosePixelFormatARB */
  342. if (!gl_getpixelformat(dummy.hdc, info, &pixel_format, &pfd))
  343. goto fail;
  344. gl_dummy_context_free(&dummy);
  345. if (!init_default_swap(plat, device, pixel_format, &pfd, info))
  346. goto fail;
  347. plat->hrc = gl_init_context(plat->swap.wi->hdc);
  348. if (!plat->hrc)
  349. goto fail;
  350. if (GLEW_ARB_seamless_cube_map) {
  351. glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  352. gl_success("GL_TEXTURE_CUBE_MAP_SEAMLESS");
  353. }
  354. #ifdef _DEBUG
  355. if (GLEW_AMD_debug_output) {
  356. glDebugMessageEnableAMD(0, 0, 0, NULL, true);
  357. glDebugMessageCallbackAMD(gl_debug_message_amd, device);
  358. gl_success("glDebugMessageCallback");
  359. }
  360. #endif
  361. return plat;
  362. fail:
  363. blog(LOG_ERROR, "gl_platform_create failed");
  364. gl_platform_destroy(plat);
  365. gl_dummy_context_free(&dummy);
  366. return NULL;
  367. }
  368. struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  369. {
  370. return &platform->swap;
  371. }
  372. void gl_platform_destroy(struct gl_platform *plat)
  373. {
  374. if (plat) {
  375. if (plat->hrc) {
  376. wglMakeCurrent(NULL, NULL);
  377. wglDeleteContext(plat->hrc);
  378. }
  379. gl_windowinfo_destroy(plat->swap.wi);
  380. bfree(plat);
  381. }
  382. }
  383. struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  384. {
  385. struct gl_windowinfo *wi = gl_windowinfo_bare(info);
  386. PIXELFORMATDESCRIPTOR pfd;
  387. int pixel_format;
  388. if (!wi)
  389. return NULL;
  390. if (!gl_getpixelformat(wi->hdc, info, &pixel_format, &pfd))
  391. goto fail;
  392. if (!gl_setpixelformat(wi->hdc, pixel_format, &pfd))
  393. goto fail;
  394. return wi;
  395. fail:
  396. blog(LOG_ERROR, "gl_windowinfo_create failed");
  397. gl_windowinfo_destroy(wi);
  398. return NULL;
  399. }
  400. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  401. {
  402. if (wi) {
  403. if (wi->hdc)
  404. ReleaseDC(wi->hwnd, wi->hdc);
  405. bfree(wi);
  406. }
  407. }
  408. void device_entercontext(device_t device)
  409. {
  410. HDC hdc = device->plat->swap.wi->hdc;
  411. if (device->cur_swap)
  412. hdc = device->cur_swap->wi->hdc;
  413. if (!wgl_make_current(hdc, device->plat->hrc))
  414. blog(LOG_ERROR, "device_load_swapchain (GL) failed");
  415. }
  416. void device_leavecontext(device_t device)
  417. {
  418. wglMakeCurrent(NULL, NULL);
  419. }
  420. void device_load_swapchain(device_t device, swapchain_t swap)
  421. {
  422. HDC hdc;
  423. if (!swap)
  424. swap = &device->plat->swap;
  425. if (device->cur_swap == swap)
  426. return;
  427. device->cur_swap = swap;
  428. if (swap)
  429. hdc = swap->wi->hdc;
  430. if (!wgl_make_current(hdc, device->plat->hrc))
  431. blog(LOG_ERROR, "device_load_swapchain (GL) failed");
  432. }
  433. void device_present(device_t device)
  434. {
  435. if (!SwapBuffers(device->cur_swap->wi->hdc)) {
  436. blog(LOG_ERROR, "SwapBuffers failed, GetLastError "
  437. "returned %u", GetLastError());
  438. blog(LOG_ERROR, "device_present (GL) failed");
  439. }
  440. }
  441. extern void gl_getclientsize(struct gs_swap_chain *swap,
  442. uint32_t *width, uint32_t *height)
  443. {
  444. RECT rc;
  445. GetClientRect(swap->wi->hwnd, &rc);
  446. *width = rc.right;
  447. *height = rc.bottom;
  448. }