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