push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / dlls / opengl32 / tests / opengl.c
blob1a3b7706165628bbf100c11678eb53af5ad927e9
1 /*
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
21 #include <windows.h>
22 #include <wingdi.h>
23 #include "wine/test.h"
25 #define MAX_FORMATS 256
26 typedef void* HPBUFFERARB;
28 /* WGL_ARB_extensions_string */
29 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
30 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
32 /* WGL_ARB_make_current_read */
33 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
34 static HDC (WINAPI *pwglGetCurrentReadDCARB)();
36 /* WGL_ARB_pixel_format */
37 #define WGL_COLOR_BITS_ARB 0x2014
38 #define WGL_RED_BITS_ARB 0x2015
39 #define WGL_GREEN_BITS_ARB 0x2017
40 #define WGL_BLUE_BITS_ARB 0x2019
41 #define WGL_ALPHA_BITS_ARB 0x201B
42 #define WGL_SUPPORT_GDI_ARB 0x200F
43 #define WGL_DOUBLE_BUFFER_ARB 0x2011
45 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
46 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
48 /* WGL_ARB_pbuffer */
49 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
50 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
51 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
53 static const char* wgl_extensions = NULL;
55 static void init_functions(void)
57 #define GET_PROC(func) \
58 p ## func = (void*)wglGetProcAddress(#func); \
59 if(!p ## func) \
60 trace("wglGetProcAddress(%s) failed\n", #func);
62 /* WGL_ARB_extensions_string */
63 GET_PROC(wglGetExtensionsStringARB)
65 /* WGL_ARB_make_current_read */
66 GET_PROC(wglMakeContextCurrentARB);
67 GET_PROC(wglGetCurrentReadDCARB);
69 /* WGL_ARB_pixel_format */
70 GET_PROC(wglChoosePixelFormatARB)
71 GET_PROC(wglGetPixelFormatAttribivARB)
73 /* WGL_ARB_pbuffer */
74 GET_PROC(wglCreatePbufferARB)
75 GET_PROC(wglGetPbufferDCARB)
76 GET_PROC(wglReleasePbufferDCARB)
78 #undef GET_PROC
81 static void test_pbuffers(HDC hdc)
83 const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
84 0 };
85 int iFormats[MAX_FORMATS];
86 unsigned int nOnscreenFormats;
87 unsigned int nFormats;
88 int i, res;
89 int iPixelFormat = 0;
91 nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
93 /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
94 * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
95 * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
96 * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
97 * and a pixelformat that's only available for offscreen rendering (this means that only
98 * wglChoosePixelFormatARB and friends know about the format.
100 * The first thing we need are pixelformats with pbuffer capabilities.
102 res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
103 if(res <= 0)
105 skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
106 return;
108 trace("nOnscreenFormats: %d\n", nOnscreenFormats);
109 trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
111 /* Try to select an onscreen pixelformat out of the list */
112 for(i=0; i < nFormats; i++)
114 /* Check if the format is onscreen, if it is choose it */
115 if(iFormats[i] <= nOnscreenFormats)
117 iPixelFormat = iFormats[i];
118 trace("Selected iPixelFormat=%d\n", iPixelFormat);
119 break;
123 /* A video driver supports a large number of onscreen and offscreen pixelformats.
124 * The traditional WGL calls only see a subset of the whole pixelformat list. First
125 * of all they only see the onscreen formats (the offscreen formats are at the end of the
126 * pixelformat list) and second extended pixelformat capabilities are hidden from the
127 * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
129 * Below we check if the pixelformat is also supported onscreen.
131 if(iPixelFormat != 0)
133 HDC pbuffer_hdc;
134 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
135 if(!pbuffer)
136 skip("Pbuffer creation failed!\n");
138 /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
139 pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
140 res = GetPixelFormat(pbuffer_hdc);
141 ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
142 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
143 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
145 pwglReleasePbufferDCARB(pbuffer, hdc);
147 else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
149 /* Search for a real offscreen format */
150 for(i=0, iPixelFormat=0; i<nFormats; i++)
152 if(iFormats[i] > nOnscreenFormats)
154 iPixelFormat = iFormats[i];
155 trace("Selected iPixelFormat: %d\n", iPixelFormat);
156 break;
160 if(iPixelFormat != 0)
162 HDC pbuffer_hdc;
163 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
164 if(!pbuffer)
165 skip("Pbuffer creation failed!\n");
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);
171 ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
172 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
173 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
174 pwglReleasePbufferDCARB(pbuffer, hdc);
176 else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
179 static void test_choosepixelformat(HDC hdc)
181 int iPixelFormat;
182 int nFormats;
183 BOOL found=FALSE;
184 PIXELFORMATDESCRIPTOR pfd = {
185 sizeof(PIXELFORMATDESCRIPTOR),
186 1, /* version */
187 PFD_DRAW_TO_WINDOW |
188 PFD_SUPPORT_OPENGL |
189 PFD_DOUBLEBUFFER,
190 PFD_TYPE_RGBA,
191 32, /* 32-bit color depth */
192 0, 0, 0, 0, 0, 0, /* color bits */
193 0, /* alpha buffer */
194 0, /* shift bit */
195 0, /* accumulation buffer */
196 0, 0, 0, 0, /* accum bits */
197 0, /* z-buffer */
198 0, /* stencil buffer */
199 0, /* auxiliary buffer */
200 PFD_MAIN_PLANE, /* main layer */
201 0, /* reserved */
202 0, 0, 0 /* layer masks */
205 /* Below we test the behavior of ChoosePixelFormat. As documented on MSDN this
206 * function doesn't give any guarantees about its outcome. Programs should not
207 * rely on weird behavior of the function but unfortunately a few programs like
208 * e.g. Serious Sam TSE rely on it.
210 * MSDN documents of a few flags like double buffering / stereo that they can be set to DONTCARE.
211 * It appears that a 0 value on other options like alpha, red, .. means DONTCARE. The hypothesis
212 * is that ChoosePixelFormat returns the first available format which matches the criteria.
214 * This test tries to proof the DONTCARE behavior by passing an almost 'empty' pfd to
215 * ChoosePixelFormat. The pfd only has some really needed flags (RGBA, window, double buffer) set.
216 * Further a 32 bit color buffer has been requested. The idea is that when a format with e.g. depth or stencil bits
217 * is returned, while there are also 'better' candidates in the list without them (but located AFTER the returned one)
218 * that an option set to zero means DONTCARE. We try to proof this by checking the aux/depth/stencil bits.
219 * Proofing this behavior for the color bits isn't possible as all formats have red/green/blue/(alpha), so we assume
220 * that if it holds for aux/depth/stencil it also holds for the others.
222 * The test below passes at least on various ATI cards (rv250, r300) and Nvidia cards.
225 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
226 if(iPixelFormat) {
227 PIXELFORMATDESCRIPTOR pfd_tmp;
228 BOOL res;
229 int i;
231 memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
232 res = DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
234 nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
235 /* Start testing from iPixelFormat, second formats start counting from index=1, so use '<=' */
236 for(i=iPixelFormat; i<=nFormats; i++) {
237 memset(&pfd_tmp, 0, sizeof(PIXELFORMATDESCRIPTOR));
238 res = DescribePixelFormat(hdc, i, sizeof(PIXELFORMATDESCRIPTOR), &pfd_tmp);
239 if(!res)
240 continue;
242 /* Check if there is a format which better matches the requirements */
243 if((pfd_tmp.cAuxBuffers < pfd.cAuxBuffers) || (pfd_tmp.cDepthBits < pfd.cDepthBits) || (pfd_tmp.cStencilBits < pfd.cStencilBits))
244 found = TRUE;
247 /* When found=TRUE we were able to confirm our hypothesis */
248 ok(found == TRUE, "Unable to confirm DONTCARE behavior of unset pixelformatdescriptor flags\n");
253 static void test_setpixelformat(HDC winhdc)
255 int res = 0;
256 int nCfgs;
257 int pf;
258 int i;
259 PIXELFORMATDESCRIPTOR pfd = {
260 sizeof(PIXELFORMATDESCRIPTOR),
261 1, /* version */
262 PFD_DRAW_TO_WINDOW |
263 PFD_SUPPORT_OPENGL |
264 PFD_DOUBLEBUFFER,
265 PFD_TYPE_RGBA,
266 24, /* 24-bit color depth */
267 0, 0, 0, 0, 0, 0, /* color bits */
268 0, /* alpha buffer */
269 0, /* shift bit */
270 0, /* accumulation buffer */
271 0, 0, 0, 0, /* accum bits */
272 32, /* z-buffer */
273 0, /* stencil buffer */
274 0, /* auxiliary buffer */
275 PFD_MAIN_PLANE, /* main layer */
276 0, /* reserved */
277 0, 0, 0 /* layer masks */
280 HDC hdc = GetDC(0);
281 ok(hdc != 0, "GetDC(0) failed!\n");
283 /* This should pass even on the main device context */
284 pf = ChoosePixelFormat(hdc, &pfd);
285 ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
287 /* SetPixelFormat on the main device context 'X root window' should fail */
288 res = SetPixelFormat(hdc, pf, &pfd);
289 ok(res == 0, "SetPixelFormat on main device context should fail\n");
291 /* Setting the same format that was set on the HDC is allowed; other
292 formats fail */
293 nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
294 pf = GetPixelFormat(winhdc);
295 for(i = 1;i <= nCfgs;i++)
297 int res = SetPixelFormat(winhdc, i, NULL);
298 if(i == pf) ok(res, "Failed to set the same pixel format\n");
299 else ok(!res, "Unexpectedly set an alternate pixel format\n");
303 static void test_colorbits(HDC hdc)
305 const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
306 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
307 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
308 const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
309 unsigned int nFormats;
310 int res;
311 int iPixelFormat = 0;
313 if (!pwglChoosePixelFormatARB)
315 skip("wglChoosePixelFormatARB is not available\n");
316 return;
319 /* We need a pixel format with at least one bit of alpha */
320 res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
321 if(res == FALSE || nFormats == 0)
323 skip("No suitable pixel formats found\n");
324 return;
327 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
328 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
329 if(res == FALSE)
331 skip("wglGetPixelFormatAttribivARB failed\n");
332 return;
334 iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
335 ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
336 iAttribRet[0], iAttribRet[1]);
339 static void test_gdi_dbuf(HDC hdc)
341 const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
342 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
343 unsigned int nFormats;
344 int iPixelFormat;
345 int res;
347 if (!pwglGetPixelFormatAttribivARB)
349 skip("wglGetPixelFormatAttribivARB is not available\n");
350 return;
353 nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
354 for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
356 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
357 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
358 iAttribRet);
359 ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
360 if(res == FALSE)
361 continue;
363 ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
367 static void test_make_current_read(HDC hdc)
369 int res;
370 HDC hread;
371 HGLRC hglrc = wglCreateContext(hdc);
373 if(!hglrc)
375 skip("wglCreateContext failed!\n");
376 return;
379 res = wglMakeCurrent(hdc, hglrc);
380 if(!res)
382 skip("wglMakeCurrent failed!\n");
383 return;
386 /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
387 hread = pwglGetCurrentReadDCARB();
388 trace("hread %p, hdc %p\n", hread, hdc);
389 ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
391 pwglMakeContextCurrentARB(hdc, hdc, hglrc);
392 hread = pwglGetCurrentReadDCARB();
393 ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
396 static void test_dc(HWND hwnd, HDC hdc)
398 int pf1, pf2;
399 HDC hdc2;
401 /* Get another DC and make sure it has the same pixel format */
402 hdc2 = GetDC(hwnd);
403 if(hdc != hdc2)
405 pf1 = GetPixelFormat(hdc);
406 pf2 = GetPixelFormat(hdc2);
407 ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
409 else
410 skip("Could not get a different DC for the window\n");
412 if(hdc2)
414 ReleaseDC(hwnd, hdc2);
415 hdc2 = NULL;
419 START_TEST(opengl)
421 HWND hwnd;
422 PIXELFORMATDESCRIPTOR pfd = {
423 sizeof(PIXELFORMATDESCRIPTOR),
424 1, /* version */
425 PFD_DRAW_TO_WINDOW |
426 PFD_SUPPORT_OPENGL |
427 PFD_DOUBLEBUFFER,
428 PFD_TYPE_RGBA,
429 24, /* 24-bit color depth */
430 0, 0, 0, 0, 0, 0, /* color bits */
431 0, /* alpha buffer */
432 0, /* shift bit */
433 0, /* accumulation buffer */
434 0, 0, 0, 0, /* accum bits */
435 32, /* z-buffer */
436 0, /* stencil buffer */
437 0, /* auxiliary buffer */
438 PFD_MAIN_PLANE, /* main layer */
439 0, /* reserved */
440 0, 0, 0 /* layer masks */
443 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
444 10, 10, 200, 200, NULL, NULL, NULL, NULL);
445 ok(hwnd != NULL, "err: %d\n", GetLastError());
446 if (hwnd)
448 HDC hdc;
449 int iPixelFormat, res;
450 HGLRC hglrc;
451 DWORD error;
452 ShowWindow(hwnd, SW_SHOW);
454 hdc = GetDC(hwnd);
456 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
457 ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
459 /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
460 hglrc = wglCreateContext(hdc);
461 ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
462 error = GetLastError();
463 ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
465 res = SetPixelFormat(hdc, iPixelFormat, &pfd);
466 ok(res, "SetPixelformat failed: %x\n", GetLastError());
468 test_dc(hwnd, hdc);
470 hglrc = wglCreateContext(hdc);
471 res = wglMakeCurrent(hdc, hglrc);
472 ok(res, "wglMakeCurrent failed!\n");
473 init_functions();
475 test_choosepixelformat(hdc);
476 test_setpixelformat(hdc);
477 test_colorbits(hdc);
478 test_gdi_dbuf(hdc);
480 if (!pwglGetExtensionsStringARB)
482 skip("wglGetExtensionsStringARB is not available\n");
483 DestroyWindow(hwnd);
484 return;
487 wgl_extensions = pwglGetExtensionsStringARB(hdc);
488 if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
490 if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
491 test_make_current_read(hdc);
492 else
493 trace("WGL_ARB_make_current_read not supported, skipping test\n");
495 if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
496 test_pbuffers(hdc);
497 else
498 trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
500 DestroyWindow(hwnd);