wgl: GetPixelFormat fix for offscreen formats.
[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 START_TEST(opengl)
155 {
156     HWND hwnd;
157     PIXELFORMATDESCRIPTOR pfd = {
158         sizeof(PIXELFORMATDESCRIPTOR),
159         1,                     /* version */
160         PFD_DRAW_TO_WINDOW |
161         PFD_SUPPORT_OPENGL |
162         PFD_DOUBLEBUFFER,
163         PFD_TYPE_RGBA,
164         24,                    /* 24-bit color depth */
165         0, 0, 0, 0, 0, 0,      /* color bits */
166         0,                     /* alpha buffer */
167         0,                     /* shift bit */
168         0,                     /* accumulation buffer */
169         0, 0, 0, 0,            /* accum bits */
170         32,                    /* z-buffer */
171         0,                     /* stencil buffer */
172         0,                     /* auxiliary buffer */
173         PFD_MAIN_PLANE,        /* main layer */
174         0,                     /* reserved */
175         0, 0, 0                /* layer masks */
176     };
177
178     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
179                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
180     ok(hwnd != NULL, "err: %d\n", GetLastError());
181     if (hwnd)
182     {
183         HDC hdc;
184         int iPixelFormat, res;
185         HGLRC hglrc;
186         ShowWindow(hwnd, SW_SHOW);
187
188         hdc = GetDC(hwnd);
189
190         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
191         ok(iPixelFormat > 0, "No pixelformat found!\n"); /* This should never happen as ChoosePixelFormat always returns a closest match */
192
193         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
194         ok(res, "SetPixelformat failed: %x\n", GetLastError());
195
196         hglrc = wglCreateContext(hdc);
197         res = wglMakeCurrent(hdc, hglrc);
198         ok(res, "wglMakeCurrent failed!\n");
199         init_functions();
200
201         wgl_extensions = pwglGetExtensionsStringARB(hdc);
202         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
203
204         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
205             test_pbuffers(hdc);
206         else
207             trace("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
208
209         DestroyWindow(hwnd);
210     }
211 }