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)
278 hglrc = wglCreateContext(winhdc);
279 ok( hglrc != 0, "wglCreateContext failed\n" );
281 ret = wglMakeCurrent( winhdc, hglrc );
282 ok( ret, "wglMakeCurrent failed\n" );
284 ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
287 static void test_colorbits(HDC hdc)
289 const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
290 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
291 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
292 const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
293 unsigned int nFormats;
295 int iPixelFormat = 0;
297 if (!pwglChoosePixelFormatARB)
299 skip("wglChoosePixelFormatARB is not available\n");
303 /* We need a pixel format with at least one bit of alpha */
304 res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
305 if(res == FALSE || nFormats == 0)
307 skip("No suitable pixel formats found\n");
311 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
312 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
315 skip("wglGetPixelFormatAttribivARB failed\n");
318 iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
319 ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
320 iAttribRet[0], iAttribRet[1]);
323 static void test_gdi_dbuf(HDC hdc)
325 const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
326 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
327 unsigned int nFormats;
331 if (!pwglGetPixelFormatAttribivARB)
333 skip("wglGetPixelFormatAttribivARB is not available\n");
337 nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
338 for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
340 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
341 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
343 ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
347 ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
351 static void test_make_current_read(HDC hdc)
355 HGLRC hglrc = wglCreateContext(hdc);
359 skip("wglCreateContext failed!\n");
363 res = wglMakeCurrent(hdc, hglrc);
366 skip("wglMakeCurrent failed!\n");
370 /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
371 hread = pwglGetCurrentReadDCARB();
372 trace("hread %p, hdc %p\n", hread, hdc);
373 ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
375 pwglMakeContextCurrentARB(hdc, hdc, hglrc);
376 hread = pwglGetCurrentReadDCARB();
377 ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
380 static void test_dc(HWND hwnd, HDC hdc)
385 /* Get another DC and make sure it has the same pixel format */
389 pf1 = GetPixelFormat(hdc);
390 pf2 = GetPixelFormat(hdc2);
391 ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
394 skip("Could not get a different DC for the window\n");
398 ReleaseDC(hwnd, hdc2);
406 PIXELFORMATDESCRIPTOR pfd = {
407 sizeof(PIXELFORMATDESCRIPTOR),
413 24, /* 24-bit color depth */
414 0, 0, 0, 0, 0, 0, /* color bits */
415 0, /* alpha buffer */
417 0, /* accumulation buffer */
418 0, 0, 0, 0, /* accum bits */
420 0, /* stencil buffer */
421 0, /* auxiliary buffer */
422 PFD_MAIN_PLANE, /* main layer */
424 0, 0, 0 /* layer masks */
428 /* The lack of wglGetExtensionsStringARB in general means broken software rendering or the lack of decent OpenGL support, skip tests in such cases */
429 if (!pwglGetExtensionsStringARB)
431 skip("wglGetExtensionsStringARB is not available\n");
435 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
436 10, 10, 200, 200, NULL, NULL, NULL, NULL);
437 ok(hwnd != NULL, "err: %d\n", GetLastError());
441 int iPixelFormat, res;
444 ShowWindow(hwnd, SW_SHOW);
448 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
449 ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
451 /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
452 hglrc = wglCreateContext(hdc);
453 ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
454 error = GetLastError();
455 ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
457 res = SetPixelFormat(hdc, iPixelFormat, &pfd);
458 ok(res, "SetPixelformat failed: %x\n", GetLastError());
462 hglrc = wglCreateContext(hdc);
463 res = wglMakeCurrent(hdc, hglrc);
464 ok(res, "wglMakeCurrent failed!\n");
467 trace("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
468 trace("OpenGL driver version: %s\n", glGetString(GL_VERSION));
469 trace("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
472 test_makecurrent(hdc);
473 test_setpixelformat(hdc);
477 wgl_extensions = pwglGetExtensionsStringARB(hdc);
478 if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
480 if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
481 test_make_current_read(hdc);
483 trace("WGL_ARB_make_current_read not supported, skipping test\n");
485 if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
488 trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");