2 * Some tests for OpenGL functions
4 * Copyright (C) 2007 Roderick Colenbrander
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.
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.
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
23 #include "wine/test.h"
25 const unsigned char * WINAPI glGetString(unsigned int);
26 #define GL_VENDOR 0x1F00
27 #define GL_RENDERER 0x1F01
28 #define GL_VERSION 0x1F02
30 #define MAX_FORMATS 256
31 typedef void* HPBUFFERARB;
33 /* WGL_ARB_extensions_string */
34 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
35 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
37 /* WGL_ARB_make_current_read */
38 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
39 static HDC (WINAPI *pwglGetCurrentReadDCARB)();
41 /* WGL_ARB_pixel_format */
42 #define WGL_COLOR_BITS_ARB 0x2014
43 #define WGL_RED_BITS_ARB 0x2015
44 #define WGL_GREEN_BITS_ARB 0x2017
45 #define WGL_BLUE_BITS_ARB 0x2019
46 #define WGL_ALPHA_BITS_ARB 0x201B
47 #define WGL_SUPPORT_GDI_ARB 0x200F
48 #define WGL_DOUBLE_BUFFER_ARB 0x2011
50 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
51 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
54 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
55 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
56 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
58 static const char* wgl_extensions = NULL;
60 static void init_functions(void)
62 #define GET_PROC(func) \
63 p ## func = (void*)wglGetProcAddress(#func); \
65 trace("wglGetProcAddress(%s) failed\n", #func);
67 /* WGL_ARB_extensions_string */
68 GET_PROC(wglGetExtensionsStringARB)
70 /* WGL_ARB_make_current_read */
71 GET_PROC(wglMakeContextCurrentARB);
72 GET_PROC(wglGetCurrentReadDCARB);
74 /* WGL_ARB_pixel_format */
75 GET_PROC(wglChoosePixelFormatARB)
76 GET_PROC(wglGetPixelFormatAttribivARB)
79 GET_PROC(wglCreatePbufferARB)
80 GET_PROC(wglGetPbufferDCARB)
81 GET_PROC(wglReleasePbufferDCARB)
86 static void test_pbuffers(HDC hdc)
88 const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
90 int iFormats[MAX_FORMATS];
91 unsigned int nOnscreenFormats;
92 unsigned int nFormats;
96 nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
98 /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
99 * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
100 * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
101 * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
102 * and a pixelformat that's only available for offscreen rendering (this means that only
103 * wglChoosePixelFormatARB and friends know about the format.
105 * The first thing we need are pixelformats with pbuffer capabilities.
107 res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
110 skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
113 trace("nOnscreenFormats: %d\n", nOnscreenFormats);
114 trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
116 /* Try to select an onscreen pixelformat out of the list */
117 for(i=0; i < nFormats; i++)
119 /* Check if the format is onscreen, if it is choose it */
120 if(iFormats[i] <= nOnscreenFormats)
122 iPixelFormat = iFormats[i];
123 trace("Selected iPixelFormat=%d\n", iPixelFormat);
128 /* A video driver supports a large number of onscreen and offscreen pixelformats.
129 * The traditional WGL calls only see a subset of the whole pixelformat list. First
130 * of all they only see the onscreen formats (the offscreen formats are at the end of the
131 * pixelformat list) and second extended pixelformat capabilities are hidden from the
132 * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
134 * Below we check if the pixelformat is also supported onscreen.
136 if(iPixelFormat != 0)
139 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
141 skip("Pbuffer creation failed!\n");
143 /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
144 pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
145 res = GetPixelFormat(pbuffer_hdc);
146 ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
147 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
148 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
150 pwglReleasePbufferDCARB(pbuffer, hdc);
152 else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
154 /* Search for a real offscreen format */
155 for(i=0, iPixelFormat=0; i<nFormats; i++)
157 if(iFormats[i] > nOnscreenFormats)
159 iPixelFormat = iFormats[i];
160 trace("Selected iPixelFormat: %d\n", iPixelFormat);
165 if(iPixelFormat != 0)
168 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
170 skip("Pbuffer creation failed!\n");
172 /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
173 pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
174 res = GetPixelFormat(pbuffer_hdc);
176 ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
177 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
178 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
179 pwglReleasePbufferDCARB(pbuffer, hdc);
181 else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
184 static void test_setpixelformat(HDC winhdc)
191 PIXELFORMATDESCRIPTOR pfd = {
192 sizeof(PIXELFORMATDESCRIPTOR),
198 24, /* 24-bit color depth */
199 0, 0, 0, 0, 0, 0, /* color bits */
200 0, /* alpha buffer */
202 0, /* accumulation buffer */
203 0, 0, 0, 0, /* accum bits */
205 0, /* stencil buffer */
206 0, /* auxiliary buffer */
207 PFD_MAIN_PLANE, /* main layer */
209 0, 0, 0 /* layer masks */
213 ok(hdc != 0, "GetDC(0) failed!\n");
215 /* This should pass even on the main device context */
216 pf = ChoosePixelFormat(hdc, &pfd);
217 ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
219 /* SetPixelFormat on the main device context 'X root window' should fail,
220 * but some broken drivers allow it
222 res = SetPixelFormat(hdc, pf, &pfd);
223 trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
225 /* Setting the same format that was set on the HDC is allowed; other
227 nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
228 pf = GetPixelFormat(winhdc);
229 for(i = 1;i <= nCfgs;i++)
231 int res = SetPixelFormat(winhdc, i, NULL);
232 if(i == pf) ok(res, "Failed to set the same pixel format\n");
233 else ok(!res, "Unexpectedly set an alternate pixel format\n");
236 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
237 10, 10, 200, 200, NULL, NULL, NULL, NULL);
238 ok(hwnd != NULL, "err: %d\n", GetLastError());
241 HDC hdc = GetDC( hwnd );
242 pf = ChoosePixelFormat( hdc, &pfd );
243 ok( pf != 0, "ChoosePixelFormat failed\n" );
244 res = SetPixelFormat( hdc, pf, &pfd );
245 ok( res != 0, "SetPixelFormat failed\n" );
246 i = GetPixelFormat( hdc );
247 ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
248 ReleaseDC( hwnd, hdc );
249 hdc = GetWindowDC( hwnd );
250 i = GetPixelFormat( hdc );
251 ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
252 ReleaseDC( hwnd, hdc );
253 DestroyWindow( hwnd );
256 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
257 10, 10, 200, 200, NULL, NULL, NULL, NULL);
258 ok(hwnd != NULL, "err: %d\n", GetLastError());
261 HDC hdc = GetWindowDC( hwnd );
262 pf = ChoosePixelFormat( hdc, &pfd );
263 ok( pf != 0, "ChoosePixelFormat failed\n" );
264 res = SetPixelFormat( hdc, pf, &pfd );
265 ok( res != 0, "SetPixelFormat failed\n" );
266 i = GetPixelFormat( hdc );
267 ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
268 ReleaseDC( hwnd, hdc );
269 DestroyWindow( hwnd );
273 static void test_makecurrent(HDC winhdc)
280 ok( hdc != 0, "GetDC(0) failed\n" );
282 hglrc = wglCreateContext(winhdc);
283 ok( hglrc != 0, "wglCreateContext failed\n" );
285 ret = wglMakeCurrent( winhdc, hglrc );
286 ok( ret, "wglMakeCurrent failed\n" );
288 ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
290 SetLastError( 0xdeadbeef );
291 ret = wglMakeCurrent( hdc, hglrc );
292 ok( !ret, "wglMakeCurrent succeeded\n" );
293 ok( GetLastError() == ERROR_INVALID_PIXEL_FORMAT, "last error %u\n", GetLastError() );
295 ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
300 static void test_colorbits(HDC hdc)
302 const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
303 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
304 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
305 const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
306 unsigned int nFormats;
308 int iPixelFormat = 0;
310 if (!pwglChoosePixelFormatARB)
312 skip("wglChoosePixelFormatARB is not available\n");
316 /* We need a pixel format with at least one bit of alpha */
317 res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
318 if(res == FALSE || nFormats == 0)
320 skip("No suitable pixel formats found\n");
324 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
325 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
328 skip("wglGetPixelFormatAttribivARB failed\n");
331 iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
332 ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
333 iAttribRet[0], iAttribRet[1]);
336 static void test_gdi_dbuf(HDC hdc)
338 const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
339 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
340 unsigned int nFormats;
344 if (!pwglGetPixelFormatAttribivARB)
346 skip("wglGetPixelFormatAttribivARB is not available\n");
350 nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
351 for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
353 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
354 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
356 ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
360 ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
364 static void test_make_current_read(HDC hdc)
368 HGLRC hglrc = wglCreateContext(hdc);
372 skip("wglCreateContext failed!\n");
376 res = wglMakeCurrent(hdc, hglrc);
379 skip("wglMakeCurrent failed!\n");
383 /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
384 hread = pwglGetCurrentReadDCARB();
385 trace("hread %p, hdc %p\n", hread, hdc);
386 ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
388 pwglMakeContextCurrentARB(hdc, hdc, hglrc);
389 hread = pwglGetCurrentReadDCARB();
390 ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
393 static void test_dc(HWND hwnd, HDC hdc)
398 /* Get another DC and make sure it has the same pixel format */
402 pf1 = GetPixelFormat(hdc);
403 pf2 = GetPixelFormat(hdc2);
404 ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
407 skip("Could not get a different DC for the window\n");
411 ReleaseDC(hwnd, hdc2);
419 PIXELFORMATDESCRIPTOR pfd = {
420 sizeof(PIXELFORMATDESCRIPTOR),
426 24, /* 24-bit color depth */
427 0, 0, 0, 0, 0, 0, /* color bits */
428 0, /* alpha buffer */
430 0, /* accumulation buffer */
431 0, 0, 0, 0, /* accum bits */
433 0, /* stencil buffer */
434 0, /* auxiliary buffer */
435 PFD_MAIN_PLANE, /* main layer */
437 0, 0, 0 /* layer masks */
441 /* The lack of wglGetExtensionsStringARB in general means broken software rendering or the lack of decent OpenGL support, skip tests in such cases */
442 if (!pwglGetExtensionsStringARB)
444 skip("wglGetExtensionsStringARB is not available\n");
448 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
449 10, 10, 200, 200, NULL, NULL, NULL, NULL);
450 ok(hwnd != NULL, "err: %d\n", GetLastError());
454 int iPixelFormat, res;
457 ShowWindow(hwnd, SW_SHOW);
461 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
462 ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
464 /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
465 hglrc = wglCreateContext(hdc);
466 ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
467 error = GetLastError();
468 ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
470 res = SetPixelFormat(hdc, iPixelFormat, &pfd);
471 ok(res, "SetPixelformat failed: %x\n", GetLastError());
475 hglrc = wglCreateContext(hdc);
476 res = wglMakeCurrent(hdc, hglrc);
477 ok(res, "wglMakeCurrent failed!\n");
480 trace("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
481 trace("OpenGL driver version: %s\n", glGetString(GL_VERSION));
482 trace("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
485 test_makecurrent(hdc);
486 test_setpixelformat(hdc);
490 wgl_extensions = pwglGetExtensionsStringARB(hdc);
491 if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
493 if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
494 test_make_current_read(hdc);
496 trace("WGL_ARB_make_current_read not supported, skipping test\n");
498 if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
501 trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");