push 4d5485f9b89f417d46b39b93e8d940437007f325
[wine/hacks.git] / dlls / opengl32 / tests / opengl.c
blob2aca1d63dadf713a76c02cdad994a7fb20b4378f
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 #define WGL_SUPPORT_GDI_ARB 0x200F
39 #define WGL_DOUBLE_BUFFER_ARB 0x2011
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 *);
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);
49 static const char* wgl_extensions = NULL;
51 static void init_functions(void)
53 /* WGL_ARB_extensions_string */
54 pwglGetExtensionsStringARB = (void*)wglGetProcAddress("wglGetExtensionsStringARB");
56 /* WGL_ARB_pixel_format */
57 pwglChoosePixelFormatARB = (void*)wglGetProcAddress("wglChoosePixelFormatARB");
58 pwglGetPixelFormatAttribivARB = (void*)wglGetProcAddress("wglGetPixelFormatAttribivARB");
60 /* WGL_ARB_pbuffer */
61 pwglCreatePbufferARB = (void*)wglGetProcAddress("wglCreatePbufferARB");
62 pwglGetPbufferDCARB = (void*)wglGetProcAddress("wglGetPbufferDCARB");
63 pwglReleasePbufferDCARB = (void*)wglGetProcAddress("wglReleasePbufferDCARB");
66 static void test_pbuffers(HDC hdc)
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;
76 nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
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.
85 * The first thing we need are pixelformats with pbuffer capabilites.
87 res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
88 if(res <= 0)
90 skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
91 return;
93 trace("nOnscreenFormats: %d\n", nOnscreenFormats);
94 trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
96 /* Try to select an onscreen pixelformat out of the list */
97 for(i=0; i < nFormats; i++)
99 /* Check if the format is onscreen, if it is choose it */
100 if(iFormats[i] <= nOnscreenFormats)
102 iPixelFormat = iFormats[i];
103 trace("Selected iPixelFormat=%d\n", iPixelFormat);
104 break;
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.
114 * Below we check if the pixelformat is also supported onscreen.
116 if(iPixelFormat != 0)
118 HDC pbuffer_hdc;
119 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
120 if(!pbuffer)
121 skip("Pbuffer creation failed!\n");
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);
130 pwglReleasePbufferDCARB(pbuffer, hdc);
132 else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
134 /* Search for a real offscreen format */
135 for(i=0, iPixelFormat=0; i<nFormats; i++)
137 if(iFormats[i] > nOnscreenFormats)
139 iPixelFormat = iFormats[i];
140 trace("Selected iPixelFormat: %d\n", iPixelFormat);
141 break;
145 if(iPixelFormat != 0)
147 HDC pbuffer_hdc;
148 HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
149 if(!pbuffer)
150 skip("Pbuffer creation failed!\n");
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);
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);
161 else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
164 static void test_setpixelformat(void)
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 */
189 HDC hdc = GetDC(0);
190 ok(hdc != 0, "GetDC(0) failed!\n");
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");
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");
201 static void test_colorbits(HDC hdc)
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;
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)
215 skip("No suitable pixel formats found\n");
216 return;
219 res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
220 sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
221 if(res == FALSE)
223 skip("wglGetPixelFormatAttribivARB failed\n");
224 return;
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]);
231 static void test_gdi_dbuf(HDC hdc)
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;
239 nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
240 for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
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;
249 ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
253 START_TEST(opengl)
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 */
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)
282 HDC hdc;
283 int iPixelFormat, res;
284 HGLRC hglrc;
285 ShowWindow(hwnd, SW_SHOW);
287 hdc = GetDC(hwnd);
289 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
290 ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
292 res = SetPixelFormat(hdc, iPixelFormat, &pfd);
293 ok(res, "SetPixelformat failed: %x\n", GetLastError());
295 hglrc = wglCreateContext(hdc);
296 res = wglMakeCurrent(hdc, hglrc);
297 ok(res, "wglMakeCurrent failed!\n");
298 init_functions();
300 test_setpixelformat();
301 test_colorbits(hdc);
302 test_gdi_dbuf(hdc);
304 wgl_extensions = pwglGetExtensionsStringARB(hdc);
305 if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
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");
312 DestroyWindow(hwnd);