wgl: WGL_COLOR_BITS_ARB does include alpha.
[wine.git] / dlls / opengl32 / tests / opengl.c
blob6ef1d25c867efd9afa97738b8d70d48f5b229e11
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_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 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
39 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
41 /* WGL_ARB_pbuffer */
42 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
43 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
44 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
46 static const char* wgl_extensions = NULL;
48 static void init_functions(void)
50 /* WGL_ARB_extensions_string */
51 pwglGetExtensionsStringARB = (void*)wglGetProcAddress("wglGetExtensionsStringARB");
53 /* WGL_ARB_pixel_format */
54 pwglChoosePixelFormatARB = (void*)wglGetProcAddress("wglChoosePixelFormatARB");
55 pwglGetPixelFormatAttribivARB = (void*)wglGetProcAddress("wglGetPixelFormatAttribivARB");
57 /* WGL_ARB_pbuffer */
58 pwglCreatePbufferARB = (void*)wglGetProcAddress("wglCreatePbufferARB");
59 pwglGetPbufferDCARB = (void*)wglGetProcAddress("wglGetPbufferDCARB");
60 pwglReleasePbufferDCARB = (void*)wglGetProcAddress("wglReleasePbufferDCARB");
63 static void test_pbuffers(HDC hdc)
65 const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
66 0 };
67 int iFormats[MAX_FORMATS];
68 unsigned int nOnscreenFormats;
69 unsigned int nFormats;
70 int i, res;
71 int iPixelFormat = 0;
73 nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
75 /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
76 * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
77 * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
78 * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
79 * and a pixelformat that's only available for offscreen rendering (this means that only
80 * wglChoosePixelFormatARB and friends know about the format.
82 * The first thing we need are pixelformats with pbuffer capabilites.
84 res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
85 if(res <= 0)
87 skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
88 return;
90 trace("nOnscreenFormats: %d\n", nOnscreenFormats);
91 trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
93 /* Try to select an onscreen pixelformat out of the list */
94 for(i=0; i < nFormats; i++)
96 /* Check if the format is onscreen, if it is choose it */
97 if(iFormats[i] <= nOnscreenFormats)
99 iPixelFormat = iFormats[i];
100 trace("Selected iPixelFormat=%d\n", iPixelFormat);
101 break;
105 /* A video driver supports a large number of onscreen and offscreen pixelformats.
106 * The traditional WGL calls only see a subset of the whole pixelformat list. First
107 * of all they only see the onscreen formats (the offscreen formats are at the end of the
108 * pixelformat list) and second extended pixelformat capabilities are hidden from the
109 * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
111 * Below we check if the pixelformat is also supported onscreen.
113 if(iPixelFormat != 0)
115 HDC pbuffer_hdc;
116 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
117 if(!pbuffer)
118 skip("Pbuffer creation failed!\n");
120 /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
121 pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
122 res = GetPixelFormat(pbuffer_hdc);
123 ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
124 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
125 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
127 pwglReleasePbufferDCARB(pbuffer, hdc);
129 else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
131 /* Search for a real offscreen format */
132 for(i=0, iPixelFormat=0; i<nFormats; i++)
134 if(iFormats[i] > nOnscreenFormats)
136 iPixelFormat = iFormats[i];
137 trace("Selected iPixelFormat: %d\n", iPixelFormat);
138 break;
142 if(iPixelFormat != 0)
144 HDC pbuffer_hdc;
145 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
146 if(!pbuffer)
147 skip("Pbuffer creation failed!\n");
149 /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
150 pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
151 res = GetPixelFormat(pbuffer_hdc);
153 ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
154 trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
155 trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
156 pwglReleasePbufferDCARB(pbuffer, hdc);
158 else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
161 static void test_setpixelformat(void)
163 int res = 0;
164 int pf;
165 PIXELFORMATDESCRIPTOR pfd = {
166 sizeof(PIXELFORMATDESCRIPTOR),
167 1, /* version */
168 PFD_DRAW_TO_WINDOW |
169 PFD_SUPPORT_OPENGL |
170 PFD_DOUBLEBUFFER,
171 PFD_TYPE_RGBA,
172 24, /* 24-bit color depth */
173 0, 0, 0, 0, 0, 0, /* color bits */
174 0, /* alpha buffer */
175 0, /* shift bit */
176 0, /* accumulation buffer */
177 0, 0, 0, 0, /* accum bits */
178 32, /* z-buffer */
179 0, /* stencil buffer */
180 0, /* auxiliary buffer */
181 PFD_MAIN_PLANE, /* main layer */
182 0, /* reserved */
183 0, 0, 0 /* layer masks */
186 HDC hdc = GetDC(0);
187 ok(hdc != 0, "GetDC(0) failed!\n");
189 /* This should pass even on the main device context */
190 pf = ChoosePixelFormat(hdc, &pfd);
191 ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
193 /* SetPixelFormat on the main device context 'X root window' should fail */
194 res = SetPixelFormat(hdc, pf, &pfd);
195 ok(res == 0, "SetPixelFormat on main device context should fail\n");
198 static void test_colorbits(HDC hdc)
200 const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
201 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
202 int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
203 const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
204 unsigned int nFormats;
205 int res;
206 int iPixelFormat = 0;
208 /* We need a pixel format with at least one bit of alpha */
209 res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
210 if(res == FALSE || nFormats == 0)
212 skip("No suitable pixel formats found\n");
213 return;
216 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
217 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
218 if(res == FALSE)
220 skip("wglGetPixelFormatAttribivARB failed\n");
221 return;
223 iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
224 ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
225 iAttribRet[0], iAttribRet[1]);
228 START_TEST(opengl)
230 HWND hwnd;
231 PIXELFORMATDESCRIPTOR pfd = {
232 sizeof(PIXELFORMATDESCRIPTOR),
233 1, /* version */
234 PFD_DRAW_TO_WINDOW |
235 PFD_SUPPORT_OPENGL |
236 PFD_DOUBLEBUFFER,
237 PFD_TYPE_RGBA,
238 24, /* 24-bit color depth */
239 0, 0, 0, 0, 0, 0, /* color bits */
240 0, /* alpha buffer */
241 0, /* shift bit */
242 0, /* accumulation buffer */
243 0, 0, 0, 0, /* accum bits */
244 32, /* z-buffer */
245 0, /* stencil buffer */
246 0, /* auxiliary buffer */
247 PFD_MAIN_PLANE, /* main layer */
248 0, /* reserved */
249 0, 0, 0 /* layer masks */
252 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
253 10, 10, 200, 200, NULL, NULL, NULL, NULL);
254 ok(hwnd != NULL, "err: %d\n", GetLastError());
255 if (hwnd)
257 HDC hdc;
258 int iPixelFormat, res;
259 HGLRC hglrc;
260 ShowWindow(hwnd, SW_SHOW);
262 hdc = GetDC(hwnd);
264 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
265 ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
267 res = SetPixelFormat(hdc, iPixelFormat, &pfd);
268 ok(res, "SetPixelformat failed: %x\n", GetLastError());
270 hglrc = wglCreateContext(hdc);
271 res = wglMakeCurrent(hdc, hglrc);
272 ok(res, "wglMakeCurrent failed!\n");
273 init_functions();
275 test_setpixelformat();
276 test_colorbits(hdc);
278 wgl_extensions = pwglGetExtensionsStringARB(hdc);
279 if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
281 if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
282 test_pbuffers(hdc);
283 else
284 trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
286 DestroyWindow(hwnd);