[OPENGL32_WINETEST]
[reactos.git] / rostests / winetests / opengl32 / opengl.c
1 /*
2 * Some tests for OpenGL functions
3 *
4 * Copyright (C) 2007-2008 Roderick Colenbrander
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <windows.h>
22 #include <wingdi.h>
23 #include "wine/test.h"
24
25 void WINAPI glClearColor(float red, float green, float blue, float alpha);
26 void WINAPI glClear(unsigned int mask);
27 void WINAPI glFinish(void);
28 #define GL_COLOR_BUFFER_BIT 0x00004000
29 const unsigned char * WINAPI glGetString(unsigned int);
30 #define GL_VENDOR 0x1F00
31 #define GL_RENDERER 0x1F01
32 #define GL_VERSION 0x1F02
33
34 #define MAX_FORMATS 256
35 typedef void* HPBUFFERARB;
36
37 /* WGL_ARB_create_context */
38 HGLRC (WINAPI *pwglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext, const int *attribList);
39 /* GetLastError */
40 #define ERROR_INVALID_VERSION_ARB 0x2095
41 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
42 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
43 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
44 #define WGL_CONTEXT_FLAGS_ARB 0x2094
45 /* Flags for WGL_CONTEXT_FLAGS_ARB */
46 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
47 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
48
49 /* WGL_ARB_extensions_string */
50 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
51 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
52
53 /* WGL_ARB_make_current_read */
54 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
55 static HDC (WINAPI *pwglGetCurrentReadDCARB)(void);
56
57 /* WGL_ARB_pixel_format */
58 #define WGL_ACCELERATION_ARB 0x2003
59 #define WGL_COLOR_BITS_ARB 0x2014
60 #define WGL_RED_BITS_ARB 0x2015
61 #define WGL_GREEN_BITS_ARB 0x2017
62 #define WGL_BLUE_BITS_ARB 0x2019
63 #define WGL_ALPHA_BITS_ARB 0x201B
64 #define WGL_SUPPORT_GDI_ARB 0x200F
65 #define WGL_DOUBLE_BUFFER_ARB 0x2011
66 #define WGL_NO_ACCELERATION_ARB 0x2025
67 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
68 #define WGL_FULL_ACCELERATION_ARB 0x2027
69
70 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
71 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
72
73 /* WGL_ARB_pbuffer */
74 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
75 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
76 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
77
78 static const char* wgl_extensions = NULL;
79
80 static void init_functions(void)
81 {
82 #define GET_PROC(func) \
83 p ## func = (void*)wglGetProcAddress(#func); \
84 if(!p ## func) \
85 trace("wglGetProcAddress(%s) failed\n", #func);
86
87 /* WGL_ARB_create_context */
88 GET_PROC(wglCreateContextAttribsARB);
89
90 /* WGL_ARB_extensions_string */
91 GET_PROC(wglGetExtensionsStringARB)
92
93 /* WGL_ARB_make_current_read */
94 GET_PROC(wglMakeContextCurrentARB);
95 GET_PROC(wglGetCurrentReadDCARB);
96
97 /* WGL_ARB_pixel_format */
98 GET_PROC(wglChoosePixelFormatARB)
99 GET_PROC(wglGetPixelFormatAttribivARB)
100
101 /* WGL_ARB_pbuffer */
102 GET_PROC(wglCreatePbufferARB)
103 GET_PROC(wglGetPbufferDCARB)
104 GET_PROC(wglReleasePbufferDCARB)
105
106 #undef GET_PROC
107 }
108
109 static void test_pbuffers(HDC hdc)
110 {
111 const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
112 0 };
113 int iFormats[MAX_FORMATS];
114 unsigned int nOnscreenFormats;
115 unsigned int nFormats;
116 int i, res;
117 int iPixelFormat = 0;
118
119 nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
120
121 /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
122 * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
123 * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
124 * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
125 * and a pixelformat that's only available for offscreen rendering (this means that only
126 * wglChoosePixelFormatARB and friends know about the format.
127 *
128 * The first thing we need are pixelformats with pbuffer capabilities.
129 */
130 res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
131 if(res <= 0)
132 {
133 skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
134 return;
135 }
136 trace("nOnscreenFormats: %d\n", nOnscreenFormats);
137 trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
138
139 /* Try to select an onscreen pixelformat out of the list */
140 for(i=0; i < nFormats; i++)
141 {
142 /* Check if the format is onscreen, if it is choose it */
143 if(iFormats[i] <= nOnscreenFormats)
144 {
145 iPixelFormat = iFormats[i];
146 trace("Selected iPixelFormat=%d\n", iPixelFormat);
147 break;
148 }
149 }
150
151 /* A video driver supports a large number of onscreen and offscreen pixelformats.
152 * The traditional WGL calls only see a subset of the whole pixelformat list. First
153 * of all they only see the onscreen formats (the offscreen formats are at the end of the
154 * pixelformat list) and second extended pixelformat capabilities are hidden from the
155 * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
156 *
157 * Below we check if the pixelformat is also supported onscreen.
158 */
159 if(iPixelFormat != 0)
160 {
161 HDC pbuffer_hdc;
162 int attrib = 0;
163 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, &attrib);
164 if(!pbuffer)
165 skip("Pbuffer creation failed!\n");
166
167 /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
168 pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
169 res = GetPixelFormat(pbuffer_hdc);
170 ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
171 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
172 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
173
174 pwglReleasePbufferDCARB(pbuffer, hdc);
175 }
176 else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
177
178 /* Search for a real offscreen format */
179 for(i=0, iPixelFormat=0; i<nFormats; i++)
180 {
181 if(iFormats[i] > nOnscreenFormats)
182 {
183 iPixelFormat = iFormats[i];
184 trace("Selected iPixelFormat: %d\n", iPixelFormat);
185 break;
186 }
187 }
188
189 if(iPixelFormat != 0)
190 {
191 HDC pbuffer_hdc;
192 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
193 if(!pbuffer)
194 skip("Pbuffer creation failed!\n");
195
196 /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
197 pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
198 res = GetPixelFormat(pbuffer_hdc);
199
200 ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
201 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
202 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
203 pwglReleasePbufferDCARB(pbuffer, hdc);
204 }
205 else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
206 }
207
208 static void test_setpixelformat(HDC winhdc)
209 {
210 int res = 0;
211 int nCfgs;
212 int pf;
213 int i;
214 HWND hwnd;
215 PIXELFORMATDESCRIPTOR pfd = {
216 sizeof(PIXELFORMATDESCRIPTOR),
217 1, /* version */
218 PFD_DRAW_TO_WINDOW |
219 PFD_SUPPORT_OPENGL |
220 PFD_DOUBLEBUFFER,
221 PFD_TYPE_RGBA,
222 24, /* 24-bit color depth */
223 0, 0, 0, 0, 0, 0, /* color bits */
224 0, /* alpha buffer */
225 0, /* shift bit */
226 0, /* accumulation buffer */
227 0, 0, 0, 0, /* accum bits */
228 32, /* z-buffer */
229 0, /* stencil buffer */
230 0, /* auxiliary buffer */
231 PFD_MAIN_PLANE, /* main layer */
232 0, /* reserved */
233 0, 0, 0 /* layer masks */
234 };
235
236 HDC hdc = GetDC(0);
237 ok(hdc != 0, "GetDC(0) failed!\n");
238
239 /* This should pass even on the main device context */
240 pf = ChoosePixelFormat(hdc, &pfd);
241 ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
242
243 /* SetPixelFormat on the main device context 'X root window' should fail,
244 * but some broken drivers allow it
245 */
246 res = SetPixelFormat(hdc, pf, &pfd);
247 trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
248
249 /* Setting the same format that was set on the HDC is allowed; other
250 formats fail */
251 nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
252 pf = GetPixelFormat(winhdc);
253 for(i = 1;i <= nCfgs;i++)
254 {
255 int res = SetPixelFormat(winhdc, i, NULL);
256 if(i == pf) ok(res, "Failed to set the same pixel format\n");
257 else ok(!res, "Unexpectedly set an alternate pixel format\n");
258 }
259
260 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
261 10, 10, 200, 200, NULL, NULL, NULL, NULL);
262 ok(hwnd != NULL, "err: %d\n", GetLastError());
263 if (hwnd)
264 {
265 HDC hdc = GetDC( hwnd );
266 pf = ChoosePixelFormat( hdc, &pfd );
267 ok( pf != 0, "ChoosePixelFormat failed\n" );
268 res = SetPixelFormat( hdc, pf, &pfd );
269 ok( res != 0, "SetPixelFormat failed\n" );
270 i = GetPixelFormat( hdc );
271 ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
272 ReleaseDC( hwnd, hdc );
273 hdc = GetWindowDC( hwnd );
274 i = GetPixelFormat( hdc );
275 ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
276 ReleaseDC( hwnd, hdc );
277 DestroyWindow( hwnd );
278 }
279
280 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
281 10, 10, 200, 200, NULL, NULL, NULL, NULL);
282 ok(hwnd != NULL, "err: %d\n", GetLastError());
283 if (hwnd)
284 {
285 HDC hdc = GetWindowDC( hwnd );
286 pf = ChoosePixelFormat( hdc, &pfd );
287 ok( pf != 0, "ChoosePixelFormat failed\n" );
288 res = SetPixelFormat( hdc, pf, &pfd );
289 ok( res != 0, "SetPixelFormat failed\n" );
290 i = GetPixelFormat( hdc );
291 ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
292 ReleaseDC( hwnd, hdc );
293 DestroyWindow( hwnd );
294 }
295 }
296
297 static void test_sharelists(HDC winhdc)
298 {
299 HGLRC hglrc1, hglrc2, hglrc3;
300 int res;
301
302 hglrc1 = wglCreateContext(winhdc);
303 res = wglShareLists(0, 0);
304 ok(res == FALSE, "Sharing display lists for no contexts passed!\n");
305
306 /* Test 1: Create a context and just share lists without doing anything special */
307 hglrc2 = wglCreateContext(winhdc);
308 if(hglrc2)
309 {
310 res = wglShareLists(hglrc1, hglrc2);
311 ok(res, "Sharing of display lists failed\n");
312 wglDeleteContext(hglrc2);
313 }
314
315 /* Test 2: Share display lists with a 'destination' context which has been made current */
316 hglrc2 = wglCreateContext(winhdc);
317 if(hglrc2)
318 {
319 res = wglMakeCurrent(winhdc, hglrc2);
320 ok(res, "Make current failed\n");
321 res = wglShareLists(hglrc1, hglrc2);
322 todo_wine ok(res, "Sharing display lists with a destination context which has been made current failed\n");
323 wglMakeCurrent(0, 0);
324 wglDeleteContext(hglrc2);
325 }
326
327 /* Test 3: Share display lists with a context which already shares display lists with another context.
328 * According to MSDN the second parameter cannot share any display lists but some buggy drivers might allow it */
329 hglrc3 = wglCreateContext(winhdc);
330 if(hglrc3)
331 {
332 res = wglShareLists(hglrc3, hglrc1);
333 ok(res == FALSE, "Sharing of display lists passed for a context which already shared lists before\n");
334 wglDeleteContext(hglrc3);
335 }
336
337 /* Test 4: Share display lists with a 'source' context which has been made current */
338 hglrc2 = wglCreateContext(winhdc);
339 if(hglrc2)
340 {
341 res = wglMakeCurrent(winhdc, hglrc1);
342 ok(res, "Make current failed\n");
343 res = wglShareLists(hglrc1, hglrc2);
344 ok(res, "Sharing display lists with a source context which has been made current failed\n");
345 wglMakeCurrent(0, 0);
346 wglDeleteContext(hglrc2);
347 }
348 }
349
350 static void test_makecurrent(HDC winhdc)
351 {
352 BOOL ret;
353 HGLRC hglrc;
354 DWORD error;
355
356 hglrc = wglCreateContext(winhdc);
357 ok( hglrc != 0, "wglCreateContext failed\n" );
358
359 ret = wglMakeCurrent( winhdc, hglrc );
360 ok( ret, "wglMakeCurrent failed\n" );
361
362 ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
363
364 /* set the same context again */
365 ret = wglMakeCurrent( winhdc, hglrc );
366 ok( ret, "wglMakeCurrent failed\n" );
367
368 /* check wglMakeCurrent(x, y) after another call to wglMakeCurrent(x, y) */
369 ret = wglMakeCurrent( winhdc, NULL );
370 ok( ret, "wglMakeCurrent failed\n" );
371
372 ret = wglMakeCurrent( winhdc, NULL );
373 ok( ret, "wglMakeCurrent failed\n" );
374
375 SetLastError( 0xdeadbeef );
376 ret = wglMakeCurrent( NULL, NULL );
377 ok( !ret, "wglMakeCurrent succeeded\n" );
378 error = GetLastError();
379 ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
380
381 ret = wglMakeCurrent( winhdc, NULL );
382 ok( ret, "wglMakeCurrent failed\n" );
383
384 ret = wglMakeCurrent( winhdc, hglrc );
385 ok( ret, "wglMakeCurrent failed\n" );
386
387 ret = wglMakeCurrent( NULL, NULL );
388 ok( ret, "wglMakeCurrent failed\n" );
389
390 SetLastError( 0xdeadbeef );
391 ret = wglMakeCurrent( NULL, NULL );
392 ok( !ret, "wglMakeCurrent succeeded\n" );
393 error = GetLastError();
394 ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
395
396 ret = wglMakeCurrent( winhdc, hglrc );
397 ok( ret, "wglMakeCurrent failed\n" );
398 }
399
400 static void test_colorbits(HDC hdc)
401 {
402 const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
403 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
404 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
405 const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
406 unsigned int nFormats;
407 int res;
408 int iPixelFormat = 0;
409
410 if (!pwglChoosePixelFormatARB)
411 {
412 win_skip("wglChoosePixelFormatARB is not available\n");
413 return;
414 }
415
416 /* We need a pixel format with at least one bit of alpha */
417 res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
418 if(res == FALSE || nFormats == 0)
419 {
420 skip("No suitable pixel formats found\n");
421 return;
422 }
423
424 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
425 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
426 if(res == FALSE)
427 {
428 skip("wglGetPixelFormatAttribivARB failed\n");
429 return;
430 }
431 iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
432 ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
433 iAttribRet[0], iAttribRet[1]);
434 }
435
436 static void test_gdi_dbuf(HDC hdc)
437 {
438 const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
439 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
440 unsigned int nFormats;
441 int iPixelFormat;
442 int res;
443
444 if (!pwglGetPixelFormatAttribivARB)
445 {
446 win_skip("wglGetPixelFormatAttribivARB is not available\n");
447 return;
448 }
449
450 nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
451 for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
452 {
453 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
454 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
455 iAttribRet);
456 ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
457 if(res == FALSE)
458 continue;
459
460 ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
461 }
462 }
463
464 static void test_acceleration(HDC hdc)
465 {
466 const int iAttribList[] = { WGL_ACCELERATION_ARB };
467 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
468 unsigned int nFormats;
469 int iPixelFormat;
470 int res;
471 PIXELFORMATDESCRIPTOR pfd;
472
473 if (!pwglGetPixelFormatAttribivARB)
474 {
475 win_skip("wglGetPixelFormatAttribivARB is not available\n");
476 return;
477 }
478
479 nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
480 for(iPixelFormat = 1; iPixelFormat <= nFormats; iPixelFormat++)
481 {
482 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
483 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
484 iAttribRet);
485 ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
486 if(res == FALSE)
487 continue;
488
489 memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
490 DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
491
492 switch(iAttribRet[0])
493 {
494 case WGL_NO_ACCELERATION_ARB:
495 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == PFD_GENERIC_FORMAT , "Expected only PFD_GENERIC_FORMAT to be set for WGL_NO_ACCELERATION_ARB!: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
496 break;
497 case WGL_GENERIC_ACCELERATION_ARB:
498 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED), "Expected both PFD_GENERIC_FORMAT and PFD_GENERIC_ACCELERATION to be set for WGL_GENERIC_ACCELERATION_ARB: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
499 break;
500 case WGL_FULL_ACCELERATION_ARB:
501 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == 0, "Expected no PFD_GENERIC_FORMAT/_ACCELERATION to be set for WGL_FULL_ACCELERATION_ARB: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
502 break;
503 }
504 }
505 }
506
507 static void test_bitmap_rendering(void)
508 {
509 PIXELFORMATDESCRIPTOR pfd;
510 int i, iPixelFormat=0;
511 unsigned int nFormats;
512 HGLRC hglrc;
513 BITMAPINFO biDst;
514 HBITMAP bmpDst, oldDst;
515 HDC hdcDst, hdcScreen;
516 UINT32 *dstBuffer;
517
518 memset(&biDst, 0, sizeof(BITMAPINFO));
519 biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
520 biDst.bmiHeader.biWidth = 2;
521 biDst.bmiHeader.biHeight = -2;
522 biDst.bmiHeader.biPlanes = 1;
523 biDst.bmiHeader.biBitCount = 32;
524 biDst.bmiHeader.biCompression = BI_RGB;
525
526 hdcScreen = CreateCompatibleDC(0);
527 if(GetDeviceCaps(hdcScreen, BITSPIXEL) != 32)
528 {
529 DeleteDC(hdcScreen);
530 trace("Skipping bitmap rendering test\n");
531 return;
532 }
533
534 hdcDst = CreateCompatibleDC(hdcScreen);
535 bmpDst = CreateDIBSection(hdcDst, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
536 oldDst = SelectObject(hdcDst, bmpDst);
537
538 /* Pick a pixel format by hand because ChoosePixelFormat is unreliable */
539 nFormats = DescribePixelFormat(hdcDst, 0, 0, NULL);
540 for(i=1; i<=nFormats; i++)
541 {
542 memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
543 DescribePixelFormat(hdcDst, i, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
544
545 if((pfd.dwFlags & PFD_DRAW_TO_BITMAP) &&
546 (pfd.dwFlags & PFD_SUPPORT_OPENGL) &&
547 (pfd.cColorBits == 32) &&
548 (pfd.cAlphaBits == 8) )
549 {
550 iPixelFormat = i;
551 break;
552 }
553 }
554
555 if(!iPixelFormat)
556 {
557 skip("Unable to find a suitable pixel format\n");
558 }
559 else
560 {
561 SetPixelFormat(hdcDst, iPixelFormat, &pfd);
562 hglrc = wglCreateContext(hdcDst);
563 todo_wine ok(hglrc != NULL, "Unable to create a context\n");
564
565 if(hglrc)
566 {
567 wglMakeCurrent(hdcDst, hglrc);
568
569 /* Note this is RGBA but we read ARGB back */
570 glClearColor((float)0x22/0xff, (float)0x33/0xff, (float)0x44/0xff, (float)0x11/0xff);
571 glClear(GL_COLOR_BUFFER_BIT);
572 glFinish();
573
574 /* Note apparently the alpha channel is not supported by the software renderer (bitmap only works using software) */
575 ok(dstBuffer[0] == 0x223344, "Expected color=0x223344, received color=%x\n", dstBuffer[0]);
576
577 wglMakeCurrent(NULL, NULL);
578 wglDeleteContext(hglrc);
579 }
580 }
581
582 SelectObject(hdcDst, oldDst);
583 DeleteObject(bmpDst);
584 DeleteDC(hdcDst);
585
586 DeleteDC(hdcScreen);
587 }
588
589 struct wgl_thread_param
590 {
591 HANDLE test_finished;
592 HGLRC hglrc;
593 BOOL hglrc_deleted;
594 };
595
596 static DWORD WINAPI wgl_thread(void *param)
597 {
598 struct wgl_thread_param *p = param;
599
600 p->hglrc_deleted = wglDeleteContext(p->hglrc);
601 SetEvent(p->test_finished);
602
603 return 0;
604 }
605
606 static void test_deletecontext(HDC hdc)
607 {
608 struct wgl_thread_param thread_params;
609 HGLRC hglrc = wglCreateContext(hdc);
610 HANDLE thread_handle;
611 DWORD res, tid;
612
613 if(!hglrc)
614 {
615 skip("wglCreateContext failed!\n");
616 return;
617 }
618
619 res = wglMakeCurrent(hdc, hglrc);
620 if(!res)
621 {
622 skip("wglMakeCurrent failed!\n");
623 return;
624 }
625
626 /* WGL doesn't allow you to delete a context from a different thread than the one in which it is current.
627 * This differs from GLX which does allow it but it delays actual deletion until the context becomes not current.
628 */
629 thread_params.hglrc = hglrc;
630 thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
631 thread_handle = CreateThread(NULL, 0, wgl_thread, &thread_params, 0, &tid);
632 ok(!!thread_handle, "Failed to create thread, last error %#x.\n", GetLastError());
633 if(thread_handle)
634 {
635 WaitForSingleObject(thread_handle, INFINITE);
636 ok(thread_params.hglrc_deleted == FALSE, "Attempt to delete WGL context from another thread passed but should fail!\n");
637 }
638 CloseHandle(thread_params.test_finished);
639
640 res = wglDeleteContext(hglrc);
641 ok(res == TRUE, "wglDeleteContext failed\n");
642
643 /* WGL makes a context not current when deleting it. This differs from GLX behavior where
644 * deletion takes place when the thread becomes not current. */
645 hglrc = wglGetCurrentContext();
646 ok(hglrc == NULL, "A WGL context is active while none was expected\n");
647 }
648
649 static void test_make_current_read(HDC hdc)
650 {
651 int res;
652 HDC hread;
653 HGLRC hglrc = wglCreateContext(hdc);
654
655 if(!hglrc)
656 {
657 skip("wglCreateContext failed!\n");
658 return;
659 }
660
661 res = wglMakeCurrent(hdc, hglrc);
662 if(!res)
663 {
664 skip("wglMakeCurrent failed!\n");
665 return;
666 }
667
668 /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
669 hread = pwglGetCurrentReadDCARB();
670 trace("hread %p, hdc %p\n", hread, hdc);
671 ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
672
673 pwglMakeContextCurrentARB(hdc, hdc, hglrc);
674 hread = pwglGetCurrentReadDCARB();
675 ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
676 }
677
678 static void test_dc(HWND hwnd, HDC hdc)
679 {
680 int pf1, pf2;
681 HDC hdc2;
682
683 /* Get another DC and make sure it has the same pixel format */
684 hdc2 = GetDC(hwnd);
685 if(hdc != hdc2)
686 {
687 pf1 = GetPixelFormat(hdc);
688 pf2 = GetPixelFormat(hdc2);
689 ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
690 }
691 else
692 skip("Could not get a different DC for the window\n");
693
694 if(hdc2)
695 {
696 ReleaseDC(hwnd, hdc2);
697 hdc2 = NULL;
698 }
699 }
700
701 /* Nvidia converts win32 error codes to (0xc007 << 16) | win32_error_code */
702 #define NVIDIA_HRESULT_FROM_WIN32(x) (HRESULT_FROM_WIN32(x) | 0x40000000)
703 static void test_opengl3(HDC hdc)
704 {
705 /* Try to create a context compatible with OpenGL 1.x; 1.0-2.1 is allowed */
706 {
707 HGLRC gl3Ctx;
708 int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 1, 0};
709
710 gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
711 ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 1.x context failed!\n");
712 wglDeleteContext(gl3Ctx);
713 }
714
715 /* Try to pass an invalid HDC */
716 {
717 HGLRC gl3Ctx;
718 DWORD error;
719 gl3Ctx = pwglCreateContextAttribsARB((HDC)0xdeadbeef, 0, 0);
720 ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid HDC passed\n");
721 error = GetLastError();
722 todo_wine ok(error == ERROR_DC_NOT_FOUND ||
723 broken(error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_DATA)), /* Nvidia Vista + Win7 */
724 "Expected ERROR_DC_NOT_FOUND, got error=%x\n", error);
725 wglDeleteContext(gl3Ctx);
726 }
727
728 /* Try to pass an invalid shareList */
729 {
730 HGLRC gl3Ctx;
731 DWORD error;
732 gl3Ctx = pwglCreateContextAttribsARB(hdc, (HGLRC)0xdeadbeef, 0);
733 todo_wine ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid shareList passed\n");
734 error = GetLastError();
735 /* The Nvidia implementation seems to return hresults instead of win32 error codes */
736 todo_wine ok(error == ERROR_INVALID_OPERATION ||
737 error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_OPERATION), "Expected ERROR_INVALID_OPERATION, got error=%x\n", error);
738 wglDeleteContext(gl3Ctx);
739 }
740
741 /* Try to create an OpenGL 3.0 context */
742 {
743 int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
744 HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
745
746 if(gl3Ctx == NULL)
747 {
748 skip("Skipping the rest of the WGL_ARB_create_context test due to lack of OpenGL 3.0\n");
749 return;
750 }
751
752 wglDeleteContext(gl3Ctx);
753 }
754
755 /* Test matching an OpenGL 3.0 context with an older one, OpenGL 3.0 should allow it until the new object model is introduced in a future revision */
756 {
757 HGLRC glCtx = wglCreateContext(hdc);
758
759 int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
760 int attribs_future[] = {WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
761
762 HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs);
763 ok(gl3Ctx != NULL, "Sharing of a display list between OpenGL 3.0 and OpenGL 1.x/2.x failed!\n");
764 if(gl3Ctx)
765 wglDeleteContext(gl3Ctx);
766
767 gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs_future);
768 ok(gl3Ctx != NULL, "Sharing of a display list between a forward compatible OpenGL 3.0 context and OpenGL 1.x/2.x failed!\n");
769 if(gl3Ctx)
770 wglDeleteContext(gl3Ctx);
771
772 if(glCtx)
773 wglDeleteContext(glCtx);
774 }
775
776 /* Try to create an OpenGL 3.0 context and test windowless rendering */
777 {
778 HGLRC gl3Ctx;
779 int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
780 BOOL res;
781
782 gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
783 ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 3.0 context failed!\n");
784
785 /* OpenGL 3.0 allows offscreen rendering WITHOUT a drawable
786 * Neither AMD or Nvidia support it at this point. The WGL_ARB_create_context specs also say that
787 * it is hard because drivers use the HDC to enter the display driver and it sounds like they don't
788 * expect drivers to ever offer it.
789 */
790 res = wglMakeCurrent(0, gl3Ctx);
791 ok(res == FALSE, "Wow, OpenGL 3.0 windowless rendering passed while it was expected not to!\n");
792 if(res)
793 wglMakeCurrent(0, 0);
794
795 if(gl3Ctx)
796 wglDeleteContext(gl3Ctx);
797 }
798 }
799
800 static void test_minimized(void)
801 {
802 PIXELFORMATDESCRIPTOR pf_desc =
803 {
804 sizeof(PIXELFORMATDESCRIPTOR),
805 1, /* version */
806 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
807 PFD_TYPE_RGBA,
808 24, /* 24-bit color depth */
809 0, 0, 0, 0, 0, 0, /* color bits */
810 0, /* alpha buffer */
811 0, /* shift bit */
812 0, /* accumulation buffer */
813 0, 0, 0, 0, /* accum bits */
814 32, /* z-buffer */
815 0, /* stencil buffer */
816 0, /* auxiliary buffer */
817 PFD_MAIN_PLANE, /* main layer */
818 0, /* reserved */
819 0, 0, 0 /* layer masks */
820 };
821 int pixel_format;
822 HWND window;
823 LONG style;
824 HGLRC ctx;
825 BOOL ret;
826 HDC dc;
827
828 window = CreateWindowA("static", "opengl32_test",
829 WS_POPUP | WS_MINIMIZE, 0, 0, 640, 480, 0, 0, 0, 0);
830 ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
831
832 dc = GetDC(window);
833 ok(!!dc, "Failed to get DC.\n");
834
835 pixel_format = ChoosePixelFormat(dc, &pf_desc);
836 if (!pixel_format)
837 {
838 win_skip("Failed to find pixel format.\n");
839 ReleaseDC(window, dc);
840 DestroyWindow(window);
841 return;
842 }
843
844 ret = SetPixelFormat(dc, pixel_format, &pf_desc);
845 ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
846
847 style = GetWindowLongA(window, GWL_STYLE);
848 ok(style & WS_MINIMIZE, "Window should be minimized, got style %#x.\n", style);
849
850 ctx = wglCreateContext(dc);
851 ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
852
853 ret = wglMakeCurrent(dc, ctx);
854 ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
855
856 style = GetWindowLongA(window, GWL_STYLE);
857 ok(style & WS_MINIMIZE, "window should be minimized, got style %#x.\n", style);
858
859 ret = wglMakeCurrent(NULL, NULL);
860 ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
861
862 ret = wglDeleteContext(ctx);
863 ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
864
865 ReleaseDC(window, dc);
866 DestroyWindow(window);
867 }
868
869 START_TEST(opengl)
870 {
871 HWND hwnd;
872 PIXELFORMATDESCRIPTOR pfd = {
873 sizeof(PIXELFORMATDESCRIPTOR),
874 1, /* version */
875 PFD_DRAW_TO_WINDOW |
876 PFD_SUPPORT_OPENGL |
877 PFD_DOUBLEBUFFER,
878 PFD_TYPE_RGBA,
879 24, /* 24-bit color depth */
880 0, 0, 0, 0, 0, 0, /* color bits */
881 0, /* alpha buffer */
882 0, /* shift bit */
883 0, /* accumulation buffer */
884 0, 0, 0, 0, /* accum bits */
885 32, /* z-buffer */
886 0, /* stencil buffer */
887 0, /* auxiliary buffer */
888 PFD_MAIN_PLANE, /* main layer */
889 0, /* reserved */
890 0, 0, 0 /* layer masks */
891 };
892
893 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
894 10, 10, 200, 200, NULL, NULL, NULL, NULL);
895 ok(hwnd != NULL, "err: %d\n", GetLastError());
896 if (hwnd)
897 {
898 HDC hdc;
899 int iPixelFormat, res;
900 HGLRC hglrc;
901 DWORD error;
902 ShowWindow(hwnd, SW_SHOW);
903
904 hdc = GetDC(hwnd);
905
906 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
907 if(iPixelFormat == 0)
908 {
909 /* This should never happen as ChoosePixelFormat always returns a closest match, but currently this fails in Wine if we don't have glX */
910 win_skip("Unable to find pixel format.\n");
911 goto cleanup;
912 }
913
914 /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
915 hglrc = wglCreateContext(hdc);
916 ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
917 error = GetLastError();
918 ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
919
920 res = SetPixelFormat(hdc, iPixelFormat, &pfd);
921 ok(res, "SetPixelformat failed: %x\n", GetLastError());
922
923 test_bitmap_rendering();
924 test_minimized();
925 test_dc(hwnd, hdc);
926
927 hglrc = wglCreateContext(hdc);
928 res = wglMakeCurrent(hdc, hglrc);
929 ok(res, "wglMakeCurrent failed!\n");
930 if(res)
931 {
932 trace("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
933 trace("OpenGL driver version: %s\n", glGetString(GL_VERSION));
934 trace("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
935 }
936 else
937 {
938 skip("Skipping OpenGL tests without a current context\n");
939 return;
940 }
941
942 /* Initialisation of WGL functions depends on an implicit WGL context. For this reason we can't load them before making
943 * any WGL call :( On Wine this would work but not on real Windows because there can be different implementations (software, ICD, MCD).
944 */
945 init_functions();
946 /* The lack of wglGetExtensionsStringARB in general means broken software rendering or the lack of decent OpenGL support, skip tests in such cases */
947 if (!pwglGetExtensionsStringARB)
948 {
949 win_skip("wglGetExtensionsStringARB is not available\n");
950 return;
951 }
952
953 test_deletecontext(hdc);
954 test_makecurrent(hdc);
955 test_setpixelformat(hdc);
956 test_sharelists(hdc);
957 test_colorbits(hdc);
958 test_gdi_dbuf(hdc);
959 test_acceleration(hdc);
960
961 wgl_extensions = pwglGetExtensionsStringARB(hdc);
962 if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
963
964 if(strstr(wgl_extensions, "WGL_ARB_create_context"))
965 test_opengl3(hdc);
966
967 if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
968 test_make_current_read(hdc);
969 else
970 skip("WGL_ARB_make_current_read not supported, skipping test\n");
971
972 if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
973 test_pbuffers(hdc);
974 else
975 skip("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
976
977 cleanup:
978 ReleaseDC(hwnd, hdc);
979 DestroyWindow(hwnd);
980 }
981 }