wgl: GDI support is not advertised with double buffering.
[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_pixel_format */
33 #define WGL_COLOR_BITS_ARB 0x2014
34 #define WGL_RED_BITS_ARB   0x2015
35 #define WGL_GREEN_BITS_ARB 0x2017
36 #define WGL_BLUE_BITS_ARB  0x2019
37 #define WGL_ALPHA_BITS_ARB 0x201B
38 #define WGL_SUPPORT_GDI_ARB   0x200F
39 #define WGL_DOUBLE_BUFFER_ARB 0x2011
40
41 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
42 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
43
44 /* WGL_ARB_pbuffer */
45 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
46 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
47 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
48
49 static const char* wgl_extensions = NULL;
50
51 static void init_functions(void)
52 {
53     /* WGL_ARB_extensions_string */
54     pwglGetExtensionsStringARB = (void*)wglGetProcAddress("wglGetExtensionsStringARB");
55
56     /* WGL_ARB_pixel_format */
57     pwglChoosePixelFormatARB = (void*)wglGetProcAddress("wglChoosePixelFormatARB");
58     pwglGetPixelFormatAttribivARB = (void*)wglGetProcAddress("wglGetPixelFormatAttribivARB");
59
60     /* WGL_ARB_pbuffer */
61     pwglCreatePbufferARB = (void*)wglGetProcAddress("wglCreatePbufferARB");
62     pwglGetPbufferDCARB = (void*)wglGetProcAddress("wglGetPbufferDCARB");
63     pwglReleasePbufferDCARB = (void*)wglGetProcAddress("wglReleasePbufferDCARB");
64 }
65
66 static void test_pbuffers(HDC hdc)
67 {
68     const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
69                                 0 };
70     int iFormats[MAX_FORMATS];
71     unsigned int nOnscreenFormats;
72     unsigned int nFormats;
73     int i, res;
74     int iPixelFormat = 0;
75
76     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
77
78     /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
79      * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
80      * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
81      * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
82      * and a pixelformat that's only available for offscreen rendering (this means that only
83      * wglChoosePixelFormatARB and friends know about the format.
84      *
85      * The first thing we need are pixelformats with pbuffer capabilites.
86      */
87     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
88     if(res <= 0)
89     {
90         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
91         return;
92     }
93     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
94     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
95
96     /* Try to select an onscreen pixelformat out of the list */
97     for(i=0; i < nFormats; i++)
98     {
99         /* Check if the format is onscreen, if it is choose it */
100         if(iFormats[i] <= nOnscreenFormats)
101         {
102             iPixelFormat = iFormats[i];
103             trace("Selected iPixelFormat=%d\n", iPixelFormat);
104             break;
105         }
106     }
107
108     /* A video driver supports a large number of onscreen and offscreen pixelformats.
109      * The traditional WGL calls only see a subset of the whole pixelformat list. First
110      * of all they only see the onscreen formats (the offscreen formats are at the end of the
111      * pixelformat list) and second extended pixelformat capabilities are hidden from the
112      * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
113      *
114      * Below we check if the pixelformat is also supported onscreen.
115      */
116     if(iPixelFormat != 0)
117     {
118         HDC pbuffer_hdc;
119         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
120         if(!pbuffer)
121             skip("Pbuffer creation failed!\n");
122
123         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
124         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
125         res = GetPixelFormat(pbuffer_hdc);
126         ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
127         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
128         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
129
130         pwglReleasePbufferDCARB(pbuffer, hdc);
131     }
132     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
133
134     /* Search for a real offscreen format */
135     for(i=0, iPixelFormat=0; i<nFormats; i++)
136     {
137         if(iFormats[i] > nOnscreenFormats)
138         {
139             iPixelFormat = iFormats[i];
140             trace("Selected iPixelFormat: %d\n", iPixelFormat);
141             break;
142         }
143     }
144
145     if(iPixelFormat != 0)
146     {
147         HDC pbuffer_hdc;
148         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
149         if(!pbuffer)
150             skip("Pbuffer creation failed!\n");
151
152         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
153         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
154         res = GetPixelFormat(pbuffer_hdc);
155
156         ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
157         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
158         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
159         pwglReleasePbufferDCARB(pbuffer, hdc);
160     }
161     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
162 }
163
164 static void test_setpixelformat(void)
165 {
166     int res = 0;
167     int pf;
168     PIXELFORMATDESCRIPTOR pfd = {
169         sizeof(PIXELFORMATDESCRIPTOR),
170         1,                     /* version */
171         PFD_DRAW_TO_WINDOW |
172         PFD_SUPPORT_OPENGL |
173         PFD_DOUBLEBUFFER,
174         PFD_TYPE_RGBA,
175         24,                    /* 24-bit color depth */
176         0, 0, 0, 0, 0, 0,      /* color bits */
177         0,                     /* alpha buffer */
178         0,                     /* shift bit */
179         0,                     /* accumulation buffer */
180         0, 0, 0, 0,            /* accum bits */
181         32,                    /* z-buffer */
182         0,                     /* stencil buffer */
183         0,                     /* auxiliary buffer */
184         PFD_MAIN_PLANE,        /* main layer */
185         0,                     /* reserved */
186         0, 0, 0                /* layer masks */
187     };
188
189     HDC hdc = GetDC(0);
190     ok(hdc != 0, "GetDC(0) failed!\n");
191
192     /* This should pass even on the main device context */
193     pf = ChoosePixelFormat(hdc, &pfd);
194     ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
195
196     /* SetPixelFormat on the main device context 'X root window' should fail */
197     res = SetPixelFormat(hdc, pf, &pfd);
198     ok(res == 0, "SetPixelFormat on main device context should fail\n");
199 }
200
201 static void test_colorbits(HDC hdc)
202 {
203     const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
204                                 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
205     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
206     const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
207     unsigned int nFormats;
208     int res;
209     int iPixelFormat = 0;
210
211     /* We need a pixel format with at least one bit of alpha */
212     res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
213     if(res == FALSE || nFormats == 0)
214     {
215         skip("No suitable pixel formats found\n");
216         return;
217     }
218
219     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
220               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
221     if(res == FALSE)
222     {
223         skip("wglGetPixelFormatAttribivARB failed\n");
224         return;
225     }
226     iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
227     ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
228                                        iAttribRet[0], iAttribRet[1]);
229 }
230
231 static void test_gdi_dbuf(HDC hdc)
232 {
233     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
234     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
235     unsigned int nFormats;
236     int iPixelFormat;
237     int res;
238
239     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
240     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
241     {
242         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
243                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
244                   iAttribRet);
245         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
246         if(res == FALSE)
247             continue;
248
249         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
250     }
251 }
252
253 START_TEST(opengl)
254 {
255     HWND hwnd;
256     PIXELFORMATDESCRIPTOR pfd = {
257         sizeof(PIXELFORMATDESCRIPTOR),
258         1,                     /* version */
259         PFD_DRAW_TO_WINDOW |
260         PFD_SUPPORT_OPENGL |
261         PFD_DOUBLEBUFFER,
262         PFD_TYPE_RGBA,
263         24,                    /* 24-bit color depth */
264         0, 0, 0, 0, 0, 0,      /* color bits */
265         0,                     /* alpha buffer */
266         0,                     /* shift bit */
267         0,                     /* accumulation buffer */
268         0, 0, 0, 0,            /* accum bits */
269         32,                    /* z-buffer */
270         0,                     /* stencil buffer */
271         0,                     /* auxiliary buffer */
272         PFD_MAIN_PLANE,        /* main layer */
273         0,                     /* reserved */
274         0, 0, 0                /* layer masks */
275     };
276
277     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
278                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
279     ok(hwnd != NULL, "err: %d\n", GetLastError());
280     if (hwnd)
281     {
282         HDC hdc;
283         int iPixelFormat, res;
284         HGLRC hglrc;
285         ShowWindow(hwnd, SW_SHOW);
286
287         hdc = GetDC(hwnd);
288
289         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
290         ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
291
292         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
293         ok(res, "SetPixelformat failed: %x\n", GetLastError());
294
295         hglrc = wglCreateContext(hdc);
296         res = wglMakeCurrent(hdc, hglrc);
297         ok(res, "wglMakeCurrent failed!\n");
298         init_functions();
299
300         test_setpixelformat();
301         test_colorbits(hdc);
302         test_gdi_dbuf(hdc);
303
304         wgl_extensions = pwglGetExtensionsStringARB(hdc);
305         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
306
307         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
308             test_pbuffers(hdc);
309         else
310             trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
311
312         DestroyWindow(hwnd);
313     }
314 }