Release 1.4-rc5.
[wine] / dlls / opengl32 / tests / opengl.c
1 /*
2  * Some tests for OpenGL functions
3  *
4  * Copyright (C) 2007-2008 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 void WINAPI glClearColor(float red, float green, float blue, float alpha);
26 void WINAPI glClear(unsigned int mask);
27 #define GL_COLOR 0x1800
28 typedef unsigned int GLenum;
29 typedef int GLint;
30 void WINAPI glCopyPixels(int x, int y, int width, int height, GLenum type);
31 void WINAPI glFinish(void);
32 #define GL_NO_ERROR 0x0
33 #define GL_INVALID_OPERATION 0x502
34 GLenum WINAPI glGetError(void);
35 #define GL_COLOR_BUFFER_BIT 0x00004000
36 const unsigned char * WINAPI glGetString(unsigned int);
37 #define GL_VENDOR 0x1F00
38 #define GL_RENDERER 0x1F01
39 #define GL_VERSION 0x1F02
40
41 #define GL_VIEWPORT 0x0ba2
42 void WINAPI glGetIntegerv(GLenum pname, GLint *params);
43
44 #define MAX_FORMATS 256
45 typedef void* HPBUFFERARB;
46
47 /* WGL_ARB_create_context */
48 static HGLRC (WINAPI *pwglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext, const int *attribList);
49 /* GetLastError */
50 #define ERROR_INVALID_VERSION_ARB 0x2095
51 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
52 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
53 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
54 #define WGL_CONTEXT_FLAGS_ARB 0x2094
55 /* Flags for WGL_CONTEXT_FLAGS_ARB */
56 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
57 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
58
59 /* WGL_ARB_extensions_string */
60 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
61 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
62
63 /* WGL_ARB_make_current_read */
64 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
65 static HDC (WINAPI *pwglGetCurrentReadDCARB)(void);
66
67 /* WGL_ARB_pixel_format */
68 #define WGL_ACCELERATION_ARB 0x2003
69 #define WGL_COLOR_BITS_ARB 0x2014
70 #define WGL_RED_BITS_ARB   0x2015
71 #define WGL_GREEN_BITS_ARB 0x2017
72 #define WGL_BLUE_BITS_ARB  0x2019
73 #define WGL_ALPHA_BITS_ARB 0x201B
74 #define WGL_SUPPORT_GDI_ARB   0x200F
75 #define WGL_DOUBLE_BUFFER_ARB 0x2011
76 #define WGL_NO_ACCELERATION_ARB        0x2025
77 #define WGL_GENERIC_ACCELERATION_ARB   0x2026
78 #define WGL_FULL_ACCELERATION_ARB      0x2027
79
80 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
81 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
82
83 /* WGL_ARB_pbuffer */
84 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
85 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
86 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
87
88 static const char* wgl_extensions = NULL;
89
90 static void init_functions(void)
91 {
92 #define GET_PROC(func) \
93     p ## func = (void*)wglGetProcAddress(#func); \
94     if(!p ## func) \
95       trace("wglGetProcAddress(%s) failed\n", #func);
96
97     /* WGL_ARB_create_context */
98     GET_PROC(wglCreateContextAttribsARB);
99
100     /* WGL_ARB_extensions_string */
101     GET_PROC(wglGetExtensionsStringARB)
102
103     /* WGL_ARB_make_current_read */
104     GET_PROC(wglMakeContextCurrentARB);
105     GET_PROC(wglGetCurrentReadDCARB);
106
107     /* WGL_ARB_pixel_format */
108     GET_PROC(wglChoosePixelFormatARB)
109     GET_PROC(wglGetPixelFormatAttribivARB)
110
111     /* WGL_ARB_pbuffer */
112     GET_PROC(wglCreatePbufferARB)
113     GET_PROC(wglGetPbufferDCARB)
114     GET_PROC(wglReleasePbufferDCARB)
115
116 #undef GET_PROC
117 }
118
119 static void test_pbuffers(HDC hdc)
120 {
121     const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
122                                 0 };
123     int iFormats[MAX_FORMATS];
124     unsigned int nOnscreenFormats;
125     unsigned int nFormats;
126     int i, res;
127     int iPixelFormat = 0;
128
129     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
130
131     /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
132      * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
133      * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
134      * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
135      * and a pixelformat that's only available for offscreen rendering (this means that only
136      * wglChoosePixelFormatARB and friends know about the format.
137      *
138      * The first thing we need are pixelformats with pbuffer capabilities.
139      */
140     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
141     if(res <= 0)
142     {
143         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
144         return;
145     }
146     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
147     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
148
149     /* Try to select an onscreen pixelformat out of the list */
150     for(i=0; i < nFormats; i++)
151     {
152         /* Check if the format is onscreen, if it is choose it */
153         if(iFormats[i] <= nOnscreenFormats)
154         {
155             iPixelFormat = iFormats[i];
156             trace("Selected iPixelFormat=%d\n", iPixelFormat);
157             break;
158         }
159     }
160
161     /* A video driver supports a large number of onscreen and offscreen pixelformats.
162      * The traditional WGL calls only see a subset of the whole pixelformat list. First
163      * of all they only see the onscreen formats (the offscreen formats are at the end of the
164      * pixelformat list) and second extended pixelformat capabilities are hidden from the
165      * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
166      *
167      * Below we check if the pixelformat is also supported onscreen.
168      */
169     if(iPixelFormat != 0)
170     {
171         HDC pbuffer_hdc;
172         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
173         if(!pbuffer)
174             skip("Pbuffer creation failed!\n");
175
176         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
177         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
178         res = GetPixelFormat(pbuffer_hdc);
179         ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
180         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
181         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
182
183         pwglReleasePbufferDCARB(pbuffer, hdc);
184     }
185     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
186
187     /* Search for a real offscreen format */
188     for(i=0, iPixelFormat=0; i<nFormats; i++)
189     {
190         if(iFormats[i] > nOnscreenFormats)
191         {
192             iPixelFormat = iFormats[i];
193             trace("Selected iPixelFormat: %d\n", iPixelFormat);
194             break;
195         }
196     }
197
198     if(iPixelFormat != 0)
199     {
200         HDC pbuffer_hdc;
201         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
202         if(!pbuffer)
203             skip("Pbuffer creation failed!\n");
204
205         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
206         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
207         res = GetPixelFormat(pbuffer_hdc);
208
209         ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
210         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
211         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
212         pwglReleasePbufferDCARB(pbuffer, hdc);
213     }
214     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
215 }
216
217 static void test_setpixelformat(HDC winhdc)
218 {
219     int res = 0;
220     int nCfgs;
221     int pf;
222     int i;
223     HWND hwnd;
224     PIXELFORMATDESCRIPTOR pfd = {
225         sizeof(PIXELFORMATDESCRIPTOR),
226         1,                     /* version */
227         PFD_DRAW_TO_WINDOW |
228         PFD_SUPPORT_OPENGL |
229         PFD_DOUBLEBUFFER,
230         PFD_TYPE_RGBA,
231         24,                    /* 24-bit color depth */
232         0, 0, 0, 0, 0, 0,      /* color bits */
233         0,                     /* alpha buffer */
234         0,                     /* shift bit */
235         0,                     /* accumulation buffer */
236         0, 0, 0, 0,            /* accum bits */
237         32,                    /* z-buffer */
238         0,                     /* stencil buffer */
239         0,                     /* auxiliary buffer */
240         PFD_MAIN_PLANE,        /* main layer */
241         0,                     /* reserved */
242         0, 0, 0                /* layer masks */
243     };
244
245     HDC hdc = GetDC(0);
246     ok(hdc != 0, "GetDC(0) failed!\n");
247
248     /* This should pass even on the main device context */
249     pf = ChoosePixelFormat(hdc, &pfd);
250     ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
251
252     /* SetPixelFormat on the main device context 'X root window' should fail,
253      * but some broken drivers allow it
254      */
255     res = SetPixelFormat(hdc, pf, &pfd);
256     trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
257
258     /* Setting the same format that was set on the HDC is allowed; other
259        formats fail */
260     nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
261     pf = GetPixelFormat(winhdc);
262     for(i = 1;i <= nCfgs;i++)
263     {
264         int res = SetPixelFormat(winhdc, i, NULL);
265         if(i == pf) ok(res, "Failed to set the same pixel format\n");
266         else ok(!res, "Unexpectedly set an alternate pixel format\n");
267     }
268
269     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
270                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
271     ok(hwnd != NULL, "err: %d\n", GetLastError());
272     if (hwnd)
273     {
274         HDC hdc = GetDC( hwnd );
275         pf = ChoosePixelFormat( hdc, &pfd );
276         ok( pf != 0, "ChoosePixelFormat failed\n" );
277         res = SetPixelFormat( hdc, pf, &pfd );
278         ok( res != 0, "SetPixelFormat failed\n" );
279         i = GetPixelFormat( hdc );
280         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
281         ReleaseDC( hwnd, hdc );
282         hdc = GetWindowDC( hwnd );
283         i = GetPixelFormat( hdc );
284         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
285         ReleaseDC( hwnd, hdc );
286         DestroyWindow( hwnd );
287     }
288
289     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
290                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
291     ok(hwnd != NULL, "err: %d\n", GetLastError());
292     if (hwnd)
293     {
294         HDC hdc = GetWindowDC( hwnd );
295         pf = ChoosePixelFormat( hdc, &pfd );
296         ok( pf != 0, "ChoosePixelFormat failed\n" );
297         res = SetPixelFormat( hdc, pf, &pfd );
298         ok( res != 0, "SetPixelFormat failed\n" );
299         i = GetPixelFormat( hdc );
300         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
301         ReleaseDC( hwnd, hdc );
302         DestroyWindow( hwnd );
303     }
304 }
305
306 static void test_sharelists(HDC winhdc)
307 {
308     HGLRC hglrc1, hglrc2, hglrc3;
309     int res;
310
311     hglrc1 = wglCreateContext(winhdc);
312     res = wglShareLists(0, 0);
313     ok(res == FALSE, "Sharing display lists for no contexts passed!\n");
314
315     /* Test 1: Create a context and just share lists without doing anything special */
316     hglrc2 = wglCreateContext(winhdc);
317     if(hglrc2)
318     {
319         res = wglShareLists(hglrc1, hglrc2);
320         ok(res, "Sharing of display lists failed\n");
321         wglDeleteContext(hglrc2);
322     }
323
324     /* Test 2: Share display lists with a 'destination' context which has been made current */
325     hglrc2 = wglCreateContext(winhdc);
326     if(hglrc2)
327     {
328         res = wglMakeCurrent(winhdc, hglrc2);
329         ok(res, "Make current failed\n");
330         res = wglShareLists(hglrc1, hglrc2);
331         todo_wine ok(res, "Sharing display lists with a destination context which has been made current failed\n");
332         wglMakeCurrent(0, 0);
333         wglDeleteContext(hglrc2);
334     }
335
336     /* Test 3: Share display lists with a context which already shares display lists with another context.
337      * According to MSDN the second parameter cannot share any display lists but some buggy drivers might allow it */
338     hglrc3 = wglCreateContext(winhdc);
339     if(hglrc3)
340     {
341         res = wglShareLists(hglrc3, hglrc1);
342         ok(res == FALSE, "Sharing of display lists passed for a context which already shared lists before\n");
343         wglDeleteContext(hglrc3);
344     }
345
346     /* Test 4: Share display lists with a 'source' context which has been made current */
347     hglrc2 = wglCreateContext(winhdc);
348     if(hglrc2)
349     {
350         res = wglMakeCurrent(winhdc, hglrc1);
351         ok(res, "Make current failed\n");
352         res = wglShareLists(hglrc1, hglrc2);
353         ok(res, "Sharing display lists with a source context which has been made current failed\n");
354         wglMakeCurrent(0, 0);
355         wglDeleteContext(hglrc2);
356     }
357 }
358
359 static void test_makecurrent(HDC winhdc)
360 {
361     BOOL ret;
362     HGLRC hglrc;
363     DWORD error;
364
365     hglrc = wglCreateContext(winhdc);
366     ok( hglrc != 0, "wglCreateContext failed\n" );
367
368     ret = wglMakeCurrent( winhdc, hglrc );
369     ok( ret, "wglMakeCurrent failed\n" );
370
371     ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
372
373     /* set the same context again */
374     ret = wglMakeCurrent( winhdc, hglrc );
375     ok( ret, "wglMakeCurrent failed\n" );
376
377     /* check wglMakeCurrent(x, y) after another call to wglMakeCurrent(x, y) */
378     ret = wglMakeCurrent( winhdc, NULL );
379     ok( ret, "wglMakeCurrent failed\n" );
380
381     ret = wglMakeCurrent( winhdc, NULL );
382     ok( ret, "wglMakeCurrent failed\n" );
383
384     SetLastError( 0xdeadbeef );
385     ret = wglMakeCurrent( NULL, NULL );
386     ok( !ret, "wglMakeCurrent succeeded\n" );
387     error = GetLastError();
388     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
389
390     ret = wglMakeCurrent( winhdc, NULL );
391     ok( ret, "wglMakeCurrent failed\n" );
392
393     ret = wglMakeCurrent( winhdc, hglrc );
394     ok( ret, "wglMakeCurrent failed\n" );
395
396     ret = wglMakeCurrent( NULL, NULL );
397     ok( ret, "wglMakeCurrent failed\n" );
398
399     ok( wglGetCurrentContext() == NULL, "wrong context\n" );
400
401     SetLastError( 0xdeadbeef );
402     ret = wglMakeCurrent( NULL, NULL );
403     ok( !ret, "wglMakeCurrent succeeded\n" );
404     error = GetLastError();
405     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
406
407     ret = wglMakeCurrent( winhdc, hglrc );
408     ok( ret, "wglMakeCurrent failed\n" );
409 }
410
411 static void test_colorbits(HDC hdc)
412 {
413     const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
414                                 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
415     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
416     const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
417     unsigned int nFormats;
418     int res;
419     int iPixelFormat = 0;
420
421     if (!pwglChoosePixelFormatARB)
422     {
423         win_skip("wglChoosePixelFormatARB is not available\n");
424         return;
425     }
426
427     /* We need a pixel format with at least one bit of alpha */
428     res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
429     if(res == FALSE || nFormats == 0)
430     {
431         skip("No suitable pixel formats found\n");
432         return;
433     }
434
435     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
436               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
437     if(res == FALSE)
438     {
439         skip("wglGetPixelFormatAttribivARB failed\n");
440         return;
441     }
442     iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
443     ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
444                                        iAttribRet[0], iAttribRet[1]);
445 }
446
447 static void test_gdi_dbuf(HDC hdc)
448 {
449     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
450     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
451     unsigned int nFormats;
452     int iPixelFormat;
453     int res;
454
455     if (!pwglGetPixelFormatAttribivARB)
456     {
457         win_skip("wglGetPixelFormatAttribivARB is not available\n");
458         return;
459     }
460
461     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
462     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
463     {
464         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
465                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
466                   iAttribRet);
467         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
468         if(res == FALSE)
469             continue;
470
471         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
472     }
473 }
474
475 static void test_acceleration(HDC hdc)
476 {
477     const int iAttribList[] = { WGL_ACCELERATION_ARB };
478     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
479     unsigned int nFormats;
480     int iPixelFormat;
481     int res;
482     PIXELFORMATDESCRIPTOR pfd;
483
484     if (!pwglGetPixelFormatAttribivARB)
485     {
486         win_skip("wglGetPixelFormatAttribivARB is not available\n");
487         return;
488     }
489
490     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
491     for(iPixelFormat = 1; iPixelFormat <= nFormats; iPixelFormat++)
492     {
493         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
494                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
495                   iAttribRet);
496         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
497         if(res == FALSE)
498             continue;
499
500         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
501         DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
502
503         switch(iAttribRet[0])
504         {
505             case WGL_NO_ACCELERATION_ARB:
506                 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == PFD_GENERIC_FORMAT , "Expected only PFD_GENERIC_FORMAT to be set for WGL_NO_ACCELERATION_ARB!: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
507                 break;
508             case WGL_GENERIC_ACCELERATION_ARB:
509                 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED), "Expected both PFD_GENERIC_FORMAT and PFD_GENERIC_ACCELERATION to be set for WGL_GENERIC_ACCELERATION_ARB: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
510                 break;
511             case WGL_FULL_ACCELERATION_ARB:
512                 ok( (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED)) == 0, "Expected no PFD_GENERIC_FORMAT/_ACCELERATION to be set for WGL_FULL_ACCELERATION_ARB: iPixelFormat=%d, dwFlags=%x!\n", iPixelFormat, pfd.dwFlags);
513                 break;
514         }
515     }
516 }
517
518 static void test_bitmap_rendering(void)
519 {
520     PIXELFORMATDESCRIPTOR pfd;
521     int i, iPixelFormat=0;
522     unsigned int nFormats;
523     HGLRC hglrc;
524     BITMAPINFO biDst;
525     HBITMAP bmpDst, oldDst;
526     HDC hdcDst, hdcScreen;
527     UINT32 *dstBuffer;
528
529     memset(&biDst, 0, sizeof(BITMAPINFO));
530     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
531     biDst.bmiHeader.biWidth = 2;
532     biDst.bmiHeader.biHeight = -2;
533     biDst.bmiHeader.biPlanes = 1;
534     biDst.bmiHeader.biBitCount = 32;
535     biDst.bmiHeader.biCompression = BI_RGB;
536
537     hdcScreen = CreateCompatibleDC(0);
538     if(GetDeviceCaps(hdcScreen, BITSPIXEL) != 32)
539     {
540         DeleteDC(hdcScreen);
541         trace("Skipping bitmap rendering test\n");
542         return;
543     }
544
545     hdcDst = CreateCompatibleDC(hdcScreen);
546     bmpDst = CreateDIBSection(hdcDst, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
547     oldDst = SelectObject(hdcDst, bmpDst);
548
549     /* Pick a pixel format by hand because ChoosePixelFormat is unreliable */
550     nFormats = DescribePixelFormat(hdcDst, 0, 0, NULL);
551     for(i=1; i<=nFormats; i++)
552     {
553         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
554         DescribePixelFormat(hdcDst, i, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
555
556         if((pfd.dwFlags & PFD_DRAW_TO_BITMAP) &&
557            (pfd.dwFlags & PFD_SUPPORT_OPENGL) &&
558            (pfd.cColorBits == 32) &&
559            (pfd.cAlphaBits == 8) )
560         {
561             iPixelFormat = i;
562             break;
563         }
564     }
565
566     if(!iPixelFormat)
567     {
568         skip("Unable to find a suitable pixel format\n");
569     }
570     else
571     {
572         SetPixelFormat(hdcDst, iPixelFormat, &pfd);
573         hglrc = wglCreateContext(hdcDst);
574         todo_wine ok(hglrc != NULL, "Unable to create a context\n");
575
576         if(hglrc)
577         {
578             wglMakeCurrent(hdcDst, hglrc);
579
580             /* Note this is RGBA but we read ARGB back */
581             glClearColor((float)0x22/0xff, (float)0x33/0xff, (float)0x44/0xff, (float)0x11/0xff);
582             glClear(GL_COLOR_BUFFER_BIT);
583             glFinish();
584
585             /* Note apparently the alpha channel is not supported by the software renderer (bitmap only works using software) */
586             ok(dstBuffer[0] == 0x223344, "Expected color=0x223344, received color=%x\n", dstBuffer[0]);
587
588             wglMakeCurrent(NULL, NULL);
589             wglDeleteContext(hglrc);
590         }
591     }
592
593     SelectObject(hdcDst, oldDst);
594     DeleteObject(bmpDst);
595     DeleteDC(hdcDst);
596
597     DeleteDC(hdcScreen);
598 }
599
600 struct wgl_thread_param
601 {
602     HANDLE test_finished;
603     HGLRC hglrc;
604     BOOL hglrc_deleted;
605     DWORD last_error;
606 };
607
608 static DWORD WINAPI wgl_thread(void *param)
609 {
610     struct wgl_thread_param *p = param;
611
612     SetLastError(0xdeadbeef);
613     p->hglrc_deleted = wglDeleteContext(p->hglrc);
614     p->last_error = GetLastError();
615     SetEvent(p->test_finished);
616
617     return 0;
618 }
619
620 static void test_deletecontext(HDC hdc)
621 {
622     struct wgl_thread_param thread_params;
623     HGLRC hglrc = wglCreateContext(hdc);
624     HANDLE thread_handle;
625     DWORD res, tid;
626
627     SetLastError(0xdeadbeef);
628     res = wglDeleteContext(NULL);
629     ok(res == FALSE, "wglDeleteContext succeeded\n");
630     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected last error to be ERROR_INVALID_HANDLE, got %u\n", GetLastError());
631
632     if(!hglrc)
633     {
634         skip("wglCreateContext failed!\n");
635         return;
636     }
637
638     res = wglMakeCurrent(hdc, hglrc);
639     if(!res)
640     {
641         skip("wglMakeCurrent failed!\n");
642         return;
643     }
644
645     /* WGL doesn't allow you to delete a context from a different thread than the one in which it is current.
646      * This differs from GLX which does allow it but it delays actual deletion until the context becomes not current.
647      */
648     thread_params.hglrc = hglrc;
649     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
650     thread_handle = CreateThread(NULL, 0, wgl_thread, &thread_params, 0, &tid);
651     ok(!!thread_handle, "Failed to create thread, last error %#x.\n", GetLastError());
652     if(thread_handle)
653     {
654         WaitForSingleObject(thread_handle, INFINITE);
655         ok(thread_params.hglrc_deleted == FALSE, "Attempt to delete WGL context from another thread passed but should fail!\n");
656         ok(thread_params.last_error == ERROR_BUSY, "Expected last error to be ERROR_BUSY, got %u\n", thread_params.last_error);
657     }
658     CloseHandle(thread_params.test_finished);
659
660     res = wglDeleteContext(hglrc);
661     ok(res == TRUE, "wglDeleteContext failed\n");
662
663     /* Attempting to delete the same context twice should fail. */
664     SetLastError(0xdeadbeef);
665     res = wglDeleteContext(hglrc);
666     ok(res == FALSE, "wglDeleteContext succeeded\n");
667     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected last error to be ERROR_INVALID_HANDLE, got %u\n", GetLastError());
668
669     /* WGL makes a context not current when deleting it. This differs from GLX behavior where
670      * deletion takes place when the thread becomes not current. */
671     hglrc = wglGetCurrentContext();
672     ok(hglrc == NULL, "A WGL context is active while none was expected\n");
673 }
674
675 static void test_make_current_read(HDC hdc)
676 {
677     int res;
678     HDC hread;
679     HGLRC hglrc = wglCreateContext(hdc);
680
681     if(!hglrc)
682     {
683         skip("wglCreateContext failed!\n");
684         return;
685     }
686
687     res = wglMakeCurrent(hdc, hglrc);
688     if(!res)
689     {
690         skip("wglMakeCurrent failed!\n");
691         return;
692     }
693
694     /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
695     hread = pwglGetCurrentReadDCARB();
696     trace("hread %p, hdc %p\n", hread, hdc);
697     ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
698
699     pwglMakeContextCurrentARB(hdc, hdc, hglrc);
700     hread = pwglGetCurrentReadDCARB();
701     ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
702 }
703
704 static void test_dc(HWND hwnd, HDC hdc)
705 {
706     int pf1, pf2;
707     HDC hdc2;
708
709     /* Get another DC and make sure it has the same pixel format */
710     hdc2 = GetDC(hwnd);
711     if(hdc != hdc2)
712     {
713         pf1 = GetPixelFormat(hdc);
714         pf2 = GetPixelFormat(hdc2);
715         ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
716     }
717     else
718         skip("Could not get a different DC for the window\n");
719
720     if(hdc2)
721     {
722         ReleaseDC(hwnd, hdc2);
723         hdc2 = NULL;
724     }
725 }
726
727 /* Nvidia converts win32 error codes to (0xc007 << 16) | win32_error_code */
728 #define NVIDIA_HRESULT_FROM_WIN32(x) (HRESULT_FROM_WIN32(x) | 0x40000000)
729 static void test_opengl3(HDC hdc)
730 {
731     /* Try to create a context compatible with OpenGL 1.x; 1.0-2.1 is allowed */
732     {
733         HGLRC gl3Ctx;
734         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 1, 0};
735
736         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
737         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 1.x context failed!\n");
738         wglDeleteContext(gl3Ctx);
739     }
740
741     /* Try to pass an invalid HDC */
742     {
743         HGLRC gl3Ctx;
744         DWORD error;
745         gl3Ctx = pwglCreateContextAttribsARB((HDC)0xdeadbeef, 0, 0);
746         ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid HDC passed\n");
747         error = GetLastError();
748         todo_wine ok(error == ERROR_DC_NOT_FOUND ||
749                      broken(error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_DATA)), /* Nvidia Vista + Win7 */
750                      "Expected ERROR_DC_NOT_FOUND, got error=%x\n", error);
751         wglDeleteContext(gl3Ctx);
752     }
753
754     /* Try to pass an invalid shareList */
755     {
756         HGLRC gl3Ctx;
757         DWORD error;
758         gl3Ctx = pwglCreateContextAttribsARB(hdc, (HGLRC)0xdeadbeef, 0);
759         todo_wine ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid shareList passed\n");
760         error = GetLastError();
761         /* The Nvidia implementation seems to return hresults instead of win32 error codes */
762         todo_wine ok(error == ERROR_INVALID_OPERATION ||
763                      error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_OPERATION), "Expected ERROR_INVALID_OPERATION, got error=%x\n", error);
764         wglDeleteContext(gl3Ctx);
765     }
766
767     /* Try to create an OpenGL 3.0 context */
768     {
769         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
770         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
771
772         if(gl3Ctx == NULL)
773         {
774             skip("Skipping the rest of the WGL_ARB_create_context test due to lack of OpenGL 3.0\n");
775             return;
776         }
777
778         wglDeleteContext(gl3Ctx);
779     }
780
781     /* Test matching an OpenGL 3.0 context with an older one, OpenGL 3.0 should allow it until the new object model is introduced in a future revision */
782     {
783         HGLRC glCtx = wglCreateContext(hdc);
784
785         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
786         int attribs_future[] = {WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
787
788         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs);
789         ok(gl3Ctx != NULL, "Sharing of a display list between OpenGL 3.0 and OpenGL 1.x/2.x failed!\n");
790         if(gl3Ctx)
791             wglDeleteContext(gl3Ctx);
792
793         gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs_future);
794         ok(gl3Ctx != NULL, "Sharing of a display list between a forward compatible OpenGL 3.0 context and OpenGL 1.x/2.x failed!\n");
795         if(gl3Ctx)
796             wglDeleteContext(gl3Ctx);
797
798         if(glCtx)
799             wglDeleteContext(glCtx);
800     }
801
802     /* Try to create an OpenGL 3.0 context and test windowless rendering */
803     {
804         HGLRC gl3Ctx;
805         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
806         BOOL res;
807
808         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
809         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 3.0 context failed!\n");
810
811         /* OpenGL 3.0 allows offscreen rendering WITHOUT a drawable
812          * Neither AMD or Nvidia support it at this point. The WGL_ARB_create_context specs also say that
813          * it is hard because drivers use the HDC to enter the display driver and it sounds like they don't
814          * expect drivers to ever offer it.
815          */
816         res = wglMakeCurrent(0, gl3Ctx);
817         ok(res == FALSE, "Wow, OpenGL 3.0 windowless rendering passed while it was expected not to!\n");
818         if(res)
819             wglMakeCurrent(0, 0);
820
821         if(gl3Ctx)
822             wglDeleteContext(gl3Ctx);
823     }
824 }
825
826 static void test_minimized(void)
827 {
828     PIXELFORMATDESCRIPTOR pf_desc =
829     {
830         sizeof(PIXELFORMATDESCRIPTOR),
831         1,                     /* version */
832         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
833         PFD_TYPE_RGBA,
834         24,                    /* 24-bit color depth */
835         0, 0, 0, 0, 0, 0,      /* color bits */
836         0,                     /* alpha buffer */
837         0,                     /* shift bit */
838         0,                     /* accumulation buffer */
839         0, 0, 0, 0,            /* accum bits */
840         32,                    /* z-buffer */
841         0,                     /* stencil buffer */
842         0,                     /* auxiliary buffer */
843         PFD_MAIN_PLANE,        /* main layer */
844         0,                     /* reserved */
845         0, 0, 0                /* layer masks */
846     };
847     int pixel_format;
848     HWND window;
849     LONG style;
850     HGLRC ctx;
851     BOOL ret;
852     HDC dc;
853
854     window = CreateWindowA("static", "opengl32_test",
855             WS_POPUP | WS_MINIMIZE, 0, 0, 640, 480, 0, 0, 0, 0);
856     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
857
858     dc = GetDC(window);
859     ok(!!dc, "Failed to get DC.\n");
860
861     pixel_format = ChoosePixelFormat(dc, &pf_desc);
862     if (!pixel_format)
863     {
864         win_skip("Failed to find pixel format.\n");
865         ReleaseDC(window, dc);
866         DestroyWindow(window);
867         return;
868     }
869
870     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
871     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
872
873     style = GetWindowLongA(window, GWL_STYLE);
874     ok(style & WS_MINIMIZE, "Window should be minimized, got style %#x.\n", style);
875
876     ctx = wglCreateContext(dc);
877     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
878
879     ret = wglMakeCurrent(dc, ctx);
880     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
881
882     style = GetWindowLongA(window, GWL_STYLE);
883     ok(style & WS_MINIMIZE, "window should be minimized, got style %#x.\n", style);
884
885     ret = wglMakeCurrent(NULL, NULL);
886     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
887
888     ret = wglDeleteContext(ctx);
889     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
890
891     ReleaseDC(window, dc);
892     DestroyWindow(window);
893 }
894
895 static void test_window_dc(void)
896 {
897     PIXELFORMATDESCRIPTOR pf_desc =
898     {
899         sizeof(PIXELFORMATDESCRIPTOR),
900         1,                     /* version */
901         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
902         PFD_TYPE_RGBA,
903         24,                    /* 24-bit color depth */
904         0, 0, 0, 0, 0, 0,      /* color bits */
905         0,                     /* alpha buffer */
906         0,                     /* shift bit */
907         0,                     /* accumulation buffer */
908         0, 0, 0, 0,            /* accum bits */
909         32,                    /* z-buffer */
910         0,                     /* stencil buffer */
911         0,                     /* auxiliary buffer */
912         PFD_MAIN_PLANE,        /* main layer */
913         0,                     /* reserved */
914         0, 0, 0                /* layer masks */
915     };
916     int pixel_format;
917     HWND window;
918     RECT vp, r;
919     HGLRC ctx;
920     BOOL ret;
921     HDC dc;
922
923     window = CreateWindowA("static", "opengl32_test",
924             WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
925     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
926
927     ShowWindow(window, SW_SHOW);
928
929     dc = GetWindowDC(window);
930     ok(!!dc, "Failed to get DC.\n");
931
932     pixel_format = ChoosePixelFormat(dc, &pf_desc);
933     if (!pixel_format)
934     {
935         win_skip("Failed to find pixel format.\n");
936         ReleaseDC(window, dc);
937         DestroyWindow(window);
938         return;
939     }
940
941     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
942     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
943
944     ctx = wglCreateContext(dc);
945     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
946
947     ret = wglMakeCurrent(dc, ctx);
948     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
949
950     GetClientRect(window, &r);
951     glGetIntegerv(GL_VIEWPORT, (GLint *)&vp);
952     ok(EqualRect(&r, &vp), "Viewport not equal to client rect.\n");
953
954     ret = wglMakeCurrent(NULL, NULL);
955     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
956
957     ret = wglDeleteContext(ctx);
958     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
959
960     ReleaseDC(window, dc);
961     DestroyWindow(window);
962 }
963
964 static void test_destroy(HDC oldhdc)
965 {
966     PIXELFORMATDESCRIPTOR pf_desc =
967     {
968         sizeof(PIXELFORMATDESCRIPTOR),
969         1,                     /* version */
970         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
971         PFD_TYPE_RGBA,
972         24,                    /* 24-bit color depth */
973         0, 0, 0, 0, 0, 0,      /* color bits */
974         0,                     /* alpha buffer */
975         0,                     /* shift bit */
976         0,                     /* accumulation buffer */
977         0, 0, 0, 0,            /* accum bits */
978         32,                    /* z-buffer */
979         0,                     /* stencil buffer */
980         0,                     /* auxiliary buffer */
981         PFD_MAIN_PLANE,        /* main layer */
982         0,                     /* reserved */
983         0, 0, 0                /* layer masks */
984     };
985     int pixel_format;
986     HWND window;
987     HGLRC ctx;
988     BOOL ret;
989     HDC dc;
990     GLenum glerr;
991     DWORD err;
992     HGLRC oldctx = wglGetCurrentContext();
993
994     ok(!!oldctx, "Expected to find a valid current context.\n");
995
996     window = CreateWindowA("static", "opengl32_test",
997             WS_POPUP, 0, 0, 640, 480, 0, 0, 0, 0);
998     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
999
1000     dc = GetDC(window);
1001     ok(!!dc, "Failed to get DC.\n");
1002
1003     pixel_format = ChoosePixelFormat(dc, &pf_desc);
1004     if (!pixel_format)
1005     {
1006         win_skip("Failed to find pixel format.\n");
1007         ReleaseDC(window, dc);
1008         DestroyWindow(window);
1009         return;
1010     }
1011
1012     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
1013     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
1014
1015     ctx = wglCreateContext(dc);
1016     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
1017
1018     ret = wglMakeCurrent(dc, ctx);
1019     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1020
1021     glClear(GL_COLOR_BUFFER_BIT);
1022     glFinish();
1023     glerr = glGetError();
1024     ok(glerr == GL_NO_ERROR, "Failed glClear, error %#x.\n", glerr);
1025     ret = SwapBuffers(dc);
1026     ok(ret, "Failed SwapBuffers, error %#x.\n", GetLastError());
1027
1028     ret = DestroyWindow(window);
1029     ok(ret, "Failed to destroy window, last error %#x.\n", GetLastError());
1030
1031     ok(wglGetCurrentContext() == ctx, "Wrong current context.\n");
1032
1033     SetLastError(0xdeadbeef);
1034     ret = wglMakeCurrent(dc, ctx);
1035     err = GetLastError();
1036     ok(!ret && err == ERROR_INVALID_HANDLE,
1037             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1038     SetLastError(0xdeadbeef);
1039     ret = SwapBuffers(dc);
1040     err = GetLastError();
1041     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1042
1043     ok(wglGetCurrentContext() == ctx, "Wrong current context.\n");
1044
1045     glClear(GL_COLOR_BUFFER_BIT);
1046     glFinish();
1047     glerr = glGetError();
1048     ok(glerr == GL_NO_ERROR, "Failed glClear, error %#x.\n", glerr);
1049     SetLastError(0xdeadbeef);
1050     ret = SwapBuffers(dc);
1051     err = GetLastError();
1052     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1053
1054     ret = wglMakeCurrent(NULL, NULL);
1055     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
1056
1057     glClear(GL_COLOR_BUFFER_BIT);
1058     glFinish();
1059     glerr = glGetError();
1060     todo_wine ok(glerr == GL_INVALID_OPERATION, "Failed glClear, error %#x.\n", glerr);
1061     SetLastError(0xdeadbeef);
1062     ret = SwapBuffers(dc);
1063     err = GetLastError();
1064     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1065
1066     SetLastError(0xdeadbeef);
1067     ret = wglMakeCurrent(dc, ctx);
1068     err = GetLastError();
1069     ok(!ret && err == ERROR_INVALID_HANDLE,
1070             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1071
1072     ok(wglGetCurrentContext() == NULL, "Wrong current context.\n");
1073
1074     ret = wglMakeCurrent(oldhdc, oldctx);
1075     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1076     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1077
1078     SetLastError(0xdeadbeef);
1079     ret = wglMakeCurrent(dc, ctx);
1080     err = GetLastError();
1081     ok(!ret && err == ERROR_INVALID_HANDLE,
1082             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1083
1084     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1085
1086     ret = wglDeleteContext(ctx);
1087     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
1088
1089     ReleaseDC(window, dc);
1090
1091     ret = wglMakeCurrent(oldhdc, oldctx);
1092     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1093 }
1094
1095 static void test_destroy_read(HDC oldhdc)
1096 {
1097     PIXELFORMATDESCRIPTOR pf_desc =
1098     {
1099         sizeof(PIXELFORMATDESCRIPTOR),
1100         1,                     /* version */
1101         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
1102         PFD_TYPE_RGBA,
1103         24,                    /* 24-bit color depth */
1104         0, 0, 0, 0, 0, 0,      /* color bits */
1105         0,                     /* alpha buffer */
1106         0,                     /* shift bit */
1107         0,                     /* accumulation buffer */
1108         0, 0, 0, 0,            /* accum bits */
1109         32,                    /* z-buffer */
1110         0,                     /* stencil buffer */
1111         0,                     /* auxiliary buffer */
1112         PFD_MAIN_PLANE,        /* main layer */
1113         0,                     /* reserved */
1114         0, 0, 0                /* layer masks */
1115     };
1116     int pixel_format;
1117     HWND draw_window, read_window;
1118     HGLRC ctx;
1119     BOOL ret;
1120     HDC read_dc, draw_dc;
1121     GLenum glerr;
1122     DWORD err;
1123     HGLRC oldctx = wglGetCurrentContext();
1124
1125     ok(!!oldctx, "Expected to find a valid current context\n");
1126
1127     draw_window = CreateWindowA("static", "opengl32_test",
1128             WS_POPUP, 0, 0, 640, 480, 0, 0, 0, 0);
1129     ok(!!draw_window, "Failed to create window, last error %#x.\n", GetLastError());
1130
1131     draw_dc = GetDC(draw_window);
1132     ok(!!draw_dc, "Failed to get DC.\n");
1133
1134     pixel_format = ChoosePixelFormat(draw_dc, &pf_desc);
1135     if (!pixel_format)
1136     {
1137         win_skip("Failed to find pixel format.\n");
1138         ReleaseDC(draw_window, draw_dc);
1139         DestroyWindow(draw_window);
1140         return;
1141     }
1142
1143     ret = SetPixelFormat(draw_dc, pixel_format, &pf_desc);
1144     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
1145
1146     read_window = CreateWindowA("static", "opengl32_test",
1147             WS_POPUP, 0, 0, 640, 480, 0, 0, 0, 0);
1148     ok(!!read_window, "Failed to create window, last error %#x.\n", GetLastError());
1149
1150     read_dc = GetDC(read_window);
1151     ok(!!draw_dc, "Failed to get DC.\n");
1152
1153     pixel_format = ChoosePixelFormat(read_dc, &pf_desc);
1154     if (!pixel_format)
1155     {
1156         win_skip("Failed to find pixel format.\n");
1157         ReleaseDC(read_window, read_dc);
1158         DestroyWindow(read_window);
1159         ReleaseDC(draw_window, draw_dc);
1160         DestroyWindow(draw_window);
1161         return;
1162     }
1163
1164     ret = SetPixelFormat(read_dc, pixel_format, &pf_desc);
1165     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
1166
1167     ctx = wglCreateContext(draw_dc);
1168     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
1169
1170     ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1171     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
1172
1173     glCopyPixels(0, 0, 640, 480, GL_COLOR);
1174     glFinish();
1175     glerr = glGetError();
1176     ok(glerr == GL_NO_ERROR, "Failed glCopyPixel, error %#x.\n", glerr);
1177     ret = SwapBuffers(draw_dc);
1178     ok(ret, "Failed SwapBuffers, error %#x.\n", GetLastError());
1179
1180     ret = DestroyWindow(read_window);
1181     ok(ret, "Failed to destroy window, last error %#x.\n", GetLastError());
1182
1183     ok(wglGetCurrentContext() == ctx, "Wrong current context.\n");
1184
1185     if (0) /* Crashes on AMD on Windows */
1186     {
1187         glCopyPixels(0, 0, 640, 480, GL_COLOR);
1188         glFinish();
1189         glerr = glGetError();
1190         ok(glerr == GL_NO_ERROR, "Failed glCopyPixel, error %#x.\n", glerr);
1191     }
1192
1193     glClear(GL_COLOR_BUFFER_BIT);
1194     glFinish();
1195     glerr = glGetError();
1196     ok(glerr == GL_NO_ERROR, "Failed glClear, error %#x.\n", glerr);
1197     ret = SwapBuffers(draw_dc);
1198     ok(ret, "Failed SwapBuffers, error %#x.\n", GetLastError());
1199
1200     ret = wglMakeCurrent(NULL, NULL);
1201     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
1202
1203     if (0) /* This crashes with Nvidia drivers on Windows. */
1204     {
1205         SetLastError(0xdeadbeef);
1206         ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1207         err = GetLastError();
1208         ok(!ret && err == ERROR_INVALID_HANDLE,
1209                 "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1210     }
1211
1212     ret = DestroyWindow(draw_window);
1213     ok(ret, "Failed to destroy window, last error %#x.\n", GetLastError());
1214
1215     glClear(GL_COLOR_BUFFER_BIT);
1216     glFinish();
1217     glerr = glGetError();
1218     todo_wine ok(glerr == GL_INVALID_OPERATION, "Failed glClear, error %#x.\n", glerr);
1219     SetLastError(0xdeadbeef);
1220     ret = SwapBuffers(draw_dc);
1221     err = GetLastError();
1222     ok(!ret && err == ERROR_INVALID_HANDLE, "Unexpected behavior with SwapBuffer, last error %#x.\n", err);
1223
1224     SetLastError(0xdeadbeef);
1225     ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1226     err = GetLastError();
1227     ok(!ret && (err == ERROR_INVALID_HANDLE || err == 0xc0070006),
1228             "Unexpected behavior when making context current, ret %d, last error %#x.\n", ret, err);
1229
1230     ok(wglGetCurrentContext() == NULL, "Wrong current context.\n");
1231
1232     wglMakeCurrent(NULL, NULL);
1233
1234     wglMakeCurrent(oldhdc, oldctx);
1235     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1236
1237     SetLastError(0xdeadbeef);
1238     ret = pwglMakeContextCurrentARB(draw_dc, read_dc, ctx);
1239     err = GetLastError();
1240     ok(!ret && (err == ERROR_INVALID_HANDLE || err == 0xc0070006),
1241             "Unexpected behavior when making context current, last error %#x.\n", err);
1242
1243     ok(wglGetCurrentContext() == oldctx, "Wrong current context.\n");
1244
1245     ret = wglDeleteContext(ctx);
1246     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
1247
1248     ReleaseDC(read_window, read_dc);
1249     ReleaseDC(draw_window, draw_dc);
1250
1251     wglMakeCurrent(oldhdc, oldctx);
1252 }
1253
1254 START_TEST(opengl)
1255 {
1256     HWND hwnd;
1257     PIXELFORMATDESCRIPTOR pfd = {
1258         sizeof(PIXELFORMATDESCRIPTOR),
1259         1,                     /* version */
1260         PFD_DRAW_TO_WINDOW |
1261         PFD_SUPPORT_OPENGL |
1262         PFD_DOUBLEBUFFER,
1263         PFD_TYPE_RGBA,
1264         24,                    /* 24-bit color depth */
1265         0, 0, 0, 0, 0, 0,      /* color bits */
1266         0,                     /* alpha buffer */
1267         0,                     /* shift bit */
1268         0,                     /* accumulation buffer */
1269         0, 0, 0, 0,            /* accum bits */
1270         32,                    /* z-buffer */
1271         0,                     /* stencil buffer */
1272         0,                     /* auxiliary buffer */
1273         PFD_MAIN_PLANE,        /* main layer */
1274         0,                     /* reserved */
1275         0, 0, 0                /* layer masks */
1276     };
1277
1278     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
1279                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
1280     ok(hwnd != NULL, "err: %d\n", GetLastError());
1281     if (hwnd)
1282     {
1283         HDC hdc;
1284         int iPixelFormat, res;
1285         HGLRC hglrc;
1286         DWORD error;
1287         ShowWindow(hwnd, SW_SHOW);
1288
1289         hdc = GetDC(hwnd);
1290
1291         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
1292         if(iPixelFormat == 0)
1293         {
1294             /* This should never happen as ChoosePixelFormat always returns a closest match, but currently this fails in Wine if we don't have glX */
1295             win_skip("Unable to find pixel format.\n");
1296             goto cleanup;
1297         }
1298
1299         /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
1300         hglrc = wglCreateContext(hdc);
1301         ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
1302         error = GetLastError();
1303         ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
1304
1305         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
1306         ok(res, "SetPixelformat failed: %x\n", GetLastError());
1307
1308         test_bitmap_rendering();
1309         test_minimized();
1310         test_window_dc();
1311         test_dc(hwnd, hdc);
1312
1313         hglrc = wglCreateContext(hdc);
1314         res = wglMakeCurrent(hdc, hglrc);
1315         ok(res, "wglMakeCurrent failed!\n");
1316         if(res)
1317         {
1318             trace("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
1319             trace("OpenGL driver version: %s\n", glGetString(GL_VERSION));
1320             trace("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
1321         }
1322         else
1323         {
1324             skip("Skipping OpenGL tests without a current context\n");
1325             return;
1326         }
1327
1328         /* Initialisation of WGL functions depends on an implicit WGL context. For this reason we can't load them before making
1329          * any WGL call :( On Wine this would work but not on real Windows because there can be different implementations (software, ICD, MCD).
1330          */
1331         init_functions();
1332         /* The lack of wglGetExtensionsStringARB in general means broken software rendering or the lack of decent OpenGL support, skip tests in such cases */
1333         if (!pwglGetExtensionsStringARB)
1334         {
1335             win_skip("wglGetExtensionsStringARB is not available\n");
1336             return;
1337         }
1338
1339         test_deletecontext(hdc);
1340         test_makecurrent(hdc);
1341         test_setpixelformat(hdc);
1342         test_destroy(hdc);
1343         test_sharelists(hdc);
1344         test_colorbits(hdc);
1345         test_gdi_dbuf(hdc);
1346         test_acceleration(hdc);
1347
1348         wgl_extensions = pwglGetExtensionsStringARB(hdc);
1349         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
1350
1351         if(strstr(wgl_extensions, "WGL_ARB_create_context"))
1352             test_opengl3(hdc);
1353
1354         if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
1355         {
1356             test_make_current_read(hdc);
1357             test_destroy_read(hdc);
1358         }
1359         else
1360             skip("WGL_ARB_make_current_read not supported, skipping test\n");
1361
1362         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
1363             test_pbuffers(hdc);
1364         else
1365             skip("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
1366
1367 cleanup:
1368         ReleaseDC(hwnd, hdc);
1369         DestroyWindow(hwnd);
1370     }
1371 }