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