jscript: Optimize GetDispID usage.
[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 const unsigned char * WINAPI glGetString(unsigned int);
26 #define GL_VENDOR 0x1F00
27 #define GL_RENDERER 0x1F01
28 #define GL_VERSION 0x1F02
29
30 #define MAX_FORMATS 256
31 typedef void* HPBUFFERARB;
32
33 /* WGL_ARB_extensions_string */
34 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
35 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
36
37 /* WGL_ARB_make_current_read */
38 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
39 static HDC (WINAPI *pwglGetCurrentReadDCARB)();
40
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
49
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 *);
52
53 /* WGL_ARB_pbuffer */
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);
57
58 static const char* wgl_extensions = NULL;
59
60 static void init_functions(void)
61 {
62 #define GET_PROC(func) \
63     p ## func = (void*)wglGetProcAddress(#func); \
64     if(!p ## func) \
65       trace("wglGetProcAddress(%s) failed\n", #func);
66
67     /* WGL_ARB_extensions_string */
68     GET_PROC(wglGetExtensionsStringARB)
69
70     /* WGL_ARB_make_current_read */
71     GET_PROC(wglMakeContextCurrentARB);
72     GET_PROC(wglGetCurrentReadDCARB);
73
74     /* WGL_ARB_pixel_format */
75     GET_PROC(wglChoosePixelFormatARB)
76     GET_PROC(wglGetPixelFormatAttribivARB)
77
78     /* WGL_ARB_pbuffer */
79     GET_PROC(wglCreatePbufferARB)
80     GET_PROC(wglGetPbufferDCARB)
81     GET_PROC(wglReleasePbufferDCARB)
82
83 #undef GET_PROC
84 }
85
86 static void test_pbuffers(HDC hdc)
87 {
88     const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
89                                 0 };
90     int iFormats[MAX_FORMATS];
91     unsigned int nOnscreenFormats;
92     unsigned int nFormats;
93     int i, res;
94     int iPixelFormat = 0;
95
96     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
97
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.
104      *
105      * The first thing we need are pixelformats with pbuffer capabilities.
106      */
107     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
108     if(res <= 0)
109     {
110         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
111         return;
112     }
113     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
114     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
115
116     /* Try to select an onscreen pixelformat out of the list */
117     for(i=0; i < nFormats; i++)
118     {
119         /* Check if the format is onscreen, if it is choose it */
120         if(iFormats[i] <= nOnscreenFormats)
121         {
122             iPixelFormat = iFormats[i];
123             trace("Selected iPixelFormat=%d\n", iPixelFormat);
124             break;
125         }
126     }
127
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.
133      *
134      * Below we check if the pixelformat is also supported onscreen.
135      */
136     if(iPixelFormat != 0)
137     {
138         HDC pbuffer_hdc;
139         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
140         if(!pbuffer)
141             skip("Pbuffer creation failed!\n");
142
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);
149
150         pwglReleasePbufferDCARB(pbuffer, hdc);
151     }
152     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
153
154     /* Search for a real offscreen format */
155     for(i=0, iPixelFormat=0; i<nFormats; i++)
156     {
157         if(iFormats[i] > nOnscreenFormats)
158         {
159             iPixelFormat = iFormats[i];
160             trace("Selected iPixelFormat: %d\n", iPixelFormat);
161             break;
162         }
163     }
164
165     if(iPixelFormat != 0)
166     {
167         HDC pbuffer_hdc;
168         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
169         if(!pbuffer)
170             skip("Pbuffer creation failed!\n");
171
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);
175
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);
180     }
181     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
182 }
183
184 static void test_setpixelformat(HDC winhdc)
185 {
186     int res = 0;
187     int nCfgs;
188     int pf;
189     int i;
190     HWND hwnd;
191     PIXELFORMATDESCRIPTOR pfd = {
192         sizeof(PIXELFORMATDESCRIPTOR),
193         1,                     /* version */
194         PFD_DRAW_TO_WINDOW |
195         PFD_SUPPORT_OPENGL |
196         PFD_DOUBLEBUFFER,
197         PFD_TYPE_RGBA,
198         24,                    /* 24-bit color depth */
199         0, 0, 0, 0, 0, 0,      /* color bits */
200         0,                     /* alpha buffer */
201         0,                     /* shift bit */
202         0,                     /* accumulation buffer */
203         0, 0, 0, 0,            /* accum bits */
204         32,                    /* z-buffer */
205         0,                     /* stencil buffer */
206         0,                     /* auxiliary buffer */
207         PFD_MAIN_PLANE,        /* main layer */
208         0,                     /* reserved */
209         0, 0, 0                /* layer masks */
210     };
211
212     HDC hdc = GetDC(0);
213     ok(hdc != 0, "GetDC(0) failed!\n");
214
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");
218
219     /* SetPixelFormat on the main device context 'X root window' should fail,
220      * but some broken drivers allow it
221      */
222     res = SetPixelFormat(hdc, pf, &pfd);
223     trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
224
225     /* Setting the same format that was set on the HDC is allowed; other
226        formats fail */
227     nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
228     pf = GetPixelFormat(winhdc);
229     for(i = 1;i <= nCfgs;i++)
230     {
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");
234     }
235
236     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
237                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
238     ok(hwnd != NULL, "err: %d\n", GetLastError());
239     if (hwnd)
240     {
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 );
254     }
255
256     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
257                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
258     ok(hwnd != NULL, "err: %d\n", GetLastError());
259     if (hwnd)
260     {
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 );
270     }
271 }
272
273 static void test_makecurrent(HDC winhdc)
274 {
275     BOOL ret;
276     HGLRC hglrc;
277
278     hglrc = wglCreateContext(winhdc);
279     ok( hglrc != 0, "wglCreateContext failed\n" );
280
281     ret = wglMakeCurrent( winhdc, hglrc );
282     ok( ret, "wglMakeCurrent failed\n" );
283
284     ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
285 }
286
287 static void test_colorbits(HDC hdc)
288 {
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;
294     int res;
295     int iPixelFormat = 0;
296
297     if (!pwglChoosePixelFormatARB)
298     {
299         skip("wglChoosePixelFormatARB is not available\n");
300         return;
301     }
302
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)
306     {
307         skip("No suitable pixel formats found\n");
308         return;
309     }
310
311     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
312               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
313     if(res == FALSE)
314     {
315         skip("wglGetPixelFormatAttribivARB failed\n");
316         return;
317     }
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]);
321 }
322
323 static void test_gdi_dbuf(HDC hdc)
324 {
325     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
326     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
327     unsigned int nFormats;
328     int iPixelFormat;
329     int res;
330
331     if (!pwglGetPixelFormatAttribivARB)
332     {
333         skip("wglGetPixelFormatAttribivARB is not available\n");
334         return;
335     }
336
337     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
338     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
339     {
340         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
341                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
342                   iAttribRet);
343         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
344         if(res == FALSE)
345             continue;
346
347         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
348     }
349 }
350
351 static void test_make_current_read(HDC hdc)
352 {
353     int res;
354     HDC hread;
355     HGLRC hglrc = wglCreateContext(hdc);
356
357     if(!hglrc)
358     {
359         skip("wglCreateContext failed!\n");
360         return;
361     }
362
363     res = wglMakeCurrent(hdc, hglrc);
364     if(!res)
365     {
366         skip("wglMakeCurrent failed!\n");
367         return;
368     }
369
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");
374
375     pwglMakeContextCurrentARB(hdc, hdc, hglrc);
376     hread = pwglGetCurrentReadDCARB();
377     ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
378 }
379
380 static void test_dc(HWND hwnd, HDC hdc)
381 {
382     int pf1, pf2;
383     HDC hdc2;
384
385     /* Get another DC and make sure it has the same pixel format */
386     hdc2 = GetDC(hwnd);
387     if(hdc != hdc2)
388     {
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);
392     }
393     else
394         skip("Could not get a different DC for the window\n");
395
396     if(hdc2)
397     {
398         ReleaseDC(hwnd, hdc2);
399         hdc2 = NULL;
400     }
401 }
402
403 START_TEST(opengl)
404 {
405     HWND hwnd;
406     PIXELFORMATDESCRIPTOR pfd = {
407         sizeof(PIXELFORMATDESCRIPTOR),
408         1,                     /* version */
409         PFD_DRAW_TO_WINDOW |
410         PFD_SUPPORT_OPENGL |
411         PFD_DOUBLEBUFFER,
412         PFD_TYPE_RGBA,
413         24,                    /* 24-bit color depth */
414         0, 0, 0, 0, 0, 0,      /* color bits */
415         0,                     /* alpha buffer */
416         0,                     /* shift bit */
417         0,                     /* accumulation buffer */
418         0, 0, 0, 0,            /* accum bits */
419         32,                    /* z-buffer */
420         0,                     /* stencil buffer */
421         0,                     /* auxiliary buffer */
422         PFD_MAIN_PLANE,        /* main layer */
423         0,                     /* reserved */
424         0, 0, 0                /* layer masks */
425     };
426
427     init_functions();
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)
430     {
431         skip("wglGetExtensionsStringARB is not available\n");
432         return;
433     }
434
435     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
436                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
437     ok(hwnd != NULL, "err: %d\n", GetLastError());
438     if (hwnd)
439     {
440         HDC hdc;
441         int iPixelFormat, res;
442         HGLRC hglrc;
443         DWORD error;
444         ShowWindow(hwnd, SW_SHOW);
445
446         hdc = GetDC(hwnd);
447
448         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
449         ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
450
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);
456
457         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
458         ok(res, "SetPixelformat failed: %x\n", GetLastError());
459
460         test_dc(hwnd, hdc);
461
462         hglrc = wglCreateContext(hdc);
463         res = wglMakeCurrent(hdc, hglrc);
464         ok(res, "wglMakeCurrent failed!\n");
465         if(res)
466         {
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));
470         }
471
472         test_makecurrent(hdc);
473         test_setpixelformat(hdc);
474         test_colorbits(hdc);
475         test_gdi_dbuf(hdc);
476
477         wgl_extensions = pwglGetExtensionsStringARB(hdc);
478         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
479
480         if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
481             test_make_current_read(hdc);
482         else
483             trace("WGL_ARB_make_current_read not supported, skipping test\n");
484
485         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
486             test_pbuffers(hdc);
487         else
488             trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
489
490         DestroyWindow(hwnd);
491     }
492 }