mlang/tests: Reduce the size of the test output a little.
[wine] / dlls / opengl32 / tests / opengl.c
1 /*
2  * Some tests for OpenGL functions
3  *
4  * Copyright (C) 2007 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 #define MAX_FORMATS 256
26 typedef void* HPBUFFERARB;
27
28 /* WGL_ARB_extensions_string */
29 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
30 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
31
32 /* WGL_ARB_make_current_read */
33 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
34 static HDC (WINAPI *pwglGetCurrentReadDCARB)();
35
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
44
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 *);
47
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);
52
53 static const char* wgl_extensions = NULL;
54
55 static void init_functions(void)
56 {
57 #define GET_PROC(func) \
58     p ## func = (void*)wglGetProcAddress(#func); \
59     if(!p ## func) \
60       trace("wglGetProcAddress(%s) failed\n", #func);
61
62     /* WGL_ARB_extensions_string */
63     GET_PROC(wglGetExtensionsStringARB)
64
65     /* WGL_ARB_make_current_read */
66     GET_PROC(wglMakeContextCurrentARB);
67     GET_PROC(wglGetCurrentReadDCARB);
68
69     /* WGL_ARB_pixel_format */
70     GET_PROC(wglChoosePixelFormatARB)
71     GET_PROC(wglGetPixelFormatAttribivARB)
72
73     /* WGL_ARB_pbuffer */
74     GET_PROC(wglCreatePbufferARB)
75     GET_PROC(wglGetPbufferDCARB)
76     GET_PROC(wglReleasePbufferDCARB)
77
78 #undef GET_PROC
79 }
80
81 static void test_pbuffers(HDC hdc)
82 {
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;
90
91     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
92
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.
99      *
100      * The first thing we need are pixelformats with pbuffer capabilities.
101      */
102     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
103     if(res <= 0)
104     {
105         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
106         return;
107     }
108     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
109     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
110
111     /* Try to select an onscreen pixelformat out of the list */
112     for(i=0; i < nFormats; i++)
113     {
114         /* Check if the format is onscreen, if it is choose it */
115         if(iFormats[i] <= nOnscreenFormats)
116         {
117             iPixelFormat = iFormats[i];
118             trace("Selected iPixelFormat=%d\n", iPixelFormat);
119             break;
120         }
121     }
122
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.
128      *
129      * Below we check if the pixelformat is also supported onscreen.
130      */
131     if(iPixelFormat != 0)
132     {
133         HDC pbuffer_hdc;
134         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
135         if(!pbuffer)
136             skip("Pbuffer creation failed!\n");
137
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);
144
145         pwglReleasePbufferDCARB(pbuffer, hdc);
146     }
147     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
148
149     /* Search for a real offscreen format */
150     for(i=0, iPixelFormat=0; i<nFormats; i++)
151     {
152         if(iFormats[i] > nOnscreenFormats)
153         {
154             iPixelFormat = iFormats[i];
155             trace("Selected iPixelFormat: %d\n", iPixelFormat);
156             break;
157         }
158     }
159
160     if(iPixelFormat != 0)
161     {
162         HDC pbuffer_hdc;
163         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
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
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);
175     }
176     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
177 }
178
179 static void test_setpixelformat(HDC winhdc)
180 {
181     int res = 0;
182     int nCfgs;
183     int pf;
184     int i;
185     HWND hwnd;
186     PIXELFORMATDESCRIPTOR pfd = {
187         sizeof(PIXELFORMATDESCRIPTOR),
188         1,                     /* version */
189         PFD_DRAW_TO_WINDOW |
190         PFD_SUPPORT_OPENGL |
191         PFD_DOUBLEBUFFER,
192         PFD_TYPE_RGBA,
193         24,                    /* 24-bit color depth */
194         0, 0, 0, 0, 0, 0,      /* color bits */
195         0,                     /* alpha buffer */
196         0,                     /* shift bit */
197         0,                     /* accumulation buffer */
198         0, 0, 0, 0,            /* accum bits */
199         32,                    /* z-buffer */
200         0,                     /* stencil buffer */
201         0,                     /* auxiliary buffer */
202         PFD_MAIN_PLANE,        /* main layer */
203         0,                     /* reserved */
204         0, 0, 0                /* layer masks */
205     };
206
207     HDC hdc = GetDC(0);
208     ok(hdc != 0, "GetDC(0) failed!\n");
209
210     /* This should pass even on the main device context */
211     pf = ChoosePixelFormat(hdc, &pfd);
212     ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
213
214     /* SetPixelFormat on the main device context 'X root window' should fail,
215      * but some broken drivers allow it
216      */
217     res = SetPixelFormat(hdc, pf, &pfd);
218     trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
219
220     /* Setting the same format that was set on the HDC is allowed; other
221        formats fail */
222     nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
223     pf = GetPixelFormat(winhdc);
224     for(i = 1;i <= nCfgs;i++)
225     {
226         int res = SetPixelFormat(winhdc, i, NULL);
227         if(i == pf) ok(res, "Failed to set the same pixel format\n");
228         else ok(!res, "Unexpectedly set an alternate pixel format\n");
229     }
230
231     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
232                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
233     ok(hwnd != NULL, "err: %d\n", GetLastError());
234     if (hwnd)
235     {
236         HDC hdc = GetDC( hwnd );
237         pf = ChoosePixelFormat( hdc, &pfd );
238         ok( pf != 0, "ChoosePixelFormat failed\n" );
239         res = SetPixelFormat( hdc, pf, &pfd );
240         ok( res != 0, "SetPixelFormat failed\n" );
241         i = GetPixelFormat( hdc );
242         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
243         ReleaseDC( hwnd, hdc );
244         hdc = GetWindowDC( hwnd );
245         i = GetPixelFormat( hdc );
246         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
247         ReleaseDC( hwnd, hdc );
248         DestroyWindow( hwnd );
249     }
250
251     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
252                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
253     ok(hwnd != NULL, "err: %d\n", GetLastError());
254     if (hwnd)
255     {
256         HDC hdc = GetWindowDC( hwnd );
257         pf = ChoosePixelFormat( hdc, &pfd );
258         ok( pf != 0, "ChoosePixelFormat failed\n" );
259         res = SetPixelFormat( hdc, pf, &pfd );
260         ok( res != 0, "SetPixelFormat failed\n" );
261         i = GetPixelFormat( hdc );
262         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
263         ReleaseDC( hwnd, hdc );
264         DestroyWindow( hwnd );
265     }
266 }
267
268 static void test_colorbits(HDC hdc)
269 {
270     const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
271                                 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
272     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
273     const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
274     unsigned int nFormats;
275     int res;
276     int iPixelFormat = 0;
277
278     if (!pwglChoosePixelFormatARB)
279     {
280         skip("wglChoosePixelFormatARB is not available\n");
281         return;
282     }
283
284     /* We need a pixel format with at least one bit of alpha */
285     res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
286     if(res == FALSE || nFormats == 0)
287     {
288         skip("No suitable pixel formats found\n");
289         return;
290     }
291
292     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
293               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
294     if(res == FALSE)
295     {
296         skip("wglGetPixelFormatAttribivARB failed\n");
297         return;
298     }
299     iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
300     ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
301                                        iAttribRet[0], iAttribRet[1]);
302 }
303
304 static void test_gdi_dbuf(HDC hdc)
305 {
306     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
307     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
308     unsigned int nFormats;
309     int iPixelFormat;
310     int res;
311
312     if (!pwglGetPixelFormatAttribivARB)
313     {
314         skip("wglGetPixelFormatAttribivARB is not available\n");
315         return;
316     }
317
318     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
319     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
320     {
321         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
322                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
323                   iAttribRet);
324         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
325         if(res == FALSE)
326             continue;
327
328         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
329     }
330 }
331
332 static void test_make_current_read(HDC hdc)
333 {
334     int res;
335     HDC hread;
336     HGLRC hglrc = wglCreateContext(hdc);
337
338     if(!hglrc)
339     {
340         skip("wglCreateContext failed!\n");
341         return;
342     }
343
344     res = wglMakeCurrent(hdc, hglrc);
345     if(!res)
346     {
347         skip("wglMakeCurrent failed!\n");
348         return;
349     }
350
351     /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
352     hread = pwglGetCurrentReadDCARB();
353     trace("hread %p, hdc %p\n", hread, hdc);
354     ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
355
356     pwglMakeContextCurrentARB(hdc, hdc, hglrc);
357     hread = pwglGetCurrentReadDCARB();
358     ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
359 }
360
361 static void test_dc(HWND hwnd, HDC hdc)
362 {
363     int pf1, pf2;
364     HDC hdc2;
365
366     /* Get another DC and make sure it has the same pixel format */
367     hdc2 = GetDC(hwnd);
368     if(hdc != hdc2)
369     {
370         pf1 = GetPixelFormat(hdc);
371         pf2 = GetPixelFormat(hdc2);
372         ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
373     }
374     else
375         skip("Could not get a different DC for the window\n");
376
377     if(hdc2)
378     {
379         ReleaseDC(hwnd, hdc2);
380         hdc2 = NULL;
381     }
382 }
383
384 START_TEST(opengl)
385 {
386     HWND hwnd;
387     PIXELFORMATDESCRIPTOR pfd = {
388         sizeof(PIXELFORMATDESCRIPTOR),
389         1,                     /* version */
390         PFD_DRAW_TO_WINDOW |
391         PFD_SUPPORT_OPENGL |
392         PFD_DOUBLEBUFFER,
393         PFD_TYPE_RGBA,
394         24,                    /* 24-bit color depth */
395         0, 0, 0, 0, 0, 0,      /* color bits */
396         0,                     /* alpha buffer */
397         0,                     /* shift bit */
398         0,                     /* accumulation buffer */
399         0, 0, 0, 0,            /* accum bits */
400         32,                    /* z-buffer */
401         0,                     /* stencil buffer */
402         0,                     /* auxiliary buffer */
403         PFD_MAIN_PLANE,        /* main layer */
404         0,                     /* reserved */
405         0, 0, 0                /* layer masks */
406     };
407
408     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
409                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
410     ok(hwnd != NULL, "err: %d\n", GetLastError());
411     if (hwnd)
412     {
413         HDC hdc;
414         int iPixelFormat, res;
415         HGLRC hglrc;
416         DWORD error;
417         ShowWindow(hwnd, SW_SHOW);
418
419         hdc = GetDC(hwnd);
420
421         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
422         ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
423
424         /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
425         hglrc = wglCreateContext(hdc);
426         ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
427         error = GetLastError();
428         ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
429
430         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
431         ok(res, "SetPixelformat failed: %x\n", GetLastError());
432
433         test_dc(hwnd, hdc);
434
435         hglrc = wglCreateContext(hdc);
436         res = wglMakeCurrent(hdc, hglrc);
437         ok(res, "wglMakeCurrent failed!\n");
438         init_functions();
439
440         test_setpixelformat(hdc);
441         test_colorbits(hdc);
442         test_gdi_dbuf(hdc);
443
444         if (!pwglGetExtensionsStringARB)
445         {
446             skip("wglGetExtensionsStringARB is not available\n");
447             DestroyWindow(hwnd);
448             return;
449         }
450
451         wgl_extensions = pwglGetExtensionsStringARB(hdc);
452         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
453
454         if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
455             test_make_current_read(hdc);
456         else
457             trace("WGL_ARB_make_current_read not supported, skipping test\n");
458
459         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
460             test_pbuffers(hdc);
461         else
462             trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
463
464         DestroyWindow(hwnd);
465     }
466 }