makefiles: Rename the SRCDIR, TOPSRCDIR and TOPOBJDIR variables to follow autoconf...
[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 void WINAPI glFinish(void);
28 #define GL_COLOR_BUFFER_BIT 0x00004000
29 const unsigned char * WINAPI glGetString(unsigned int);
30 #define GL_VENDOR 0x1F00
31 #define GL_RENDERER 0x1F01
32 #define GL_VERSION 0x1F02
33
34 #define MAX_FORMATS 256
35 typedef void* HPBUFFERARB;
36
37 /* WGL_ARB_create_context */
38 HGLRC (WINAPI *pwglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext, const int *attribList);
39 /* GetLastError */
40 #define ERROR_INVALID_VERSION_ARB 0x2095
41 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
42 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
43 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
44 #define WGL_CONTEXT_FLAGS_ARB 0x2094
45 /* Flags for WGL_CONTEXT_FLAGS_ARB */
46 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
47 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
48
49 /* WGL_ARB_extensions_string */
50 static const char* (WINAPI *pwglGetExtensionsStringARB)(HDC);
51 static int (WINAPI *pwglReleasePbufferDCARB)(HPBUFFERARB, HDC);
52
53 /* WGL_ARB_make_current_read */
54 static BOOL (WINAPI *pwglMakeContextCurrentARB)(HDC hdraw, HDC hread, HGLRC hglrc);
55 static HDC (WINAPI *pwglGetCurrentReadDCARB)(void);
56
57 /* WGL_ARB_pixel_format */
58 #define WGL_ACCELERATION_ARB 0x2003
59 #define WGL_COLOR_BITS_ARB 0x2014
60 #define WGL_RED_BITS_ARB   0x2015
61 #define WGL_GREEN_BITS_ARB 0x2017
62 #define WGL_BLUE_BITS_ARB  0x2019
63 #define WGL_ALPHA_BITS_ARB 0x201B
64 #define WGL_SUPPORT_GDI_ARB   0x200F
65 #define WGL_DOUBLE_BUFFER_ARB 0x2011
66 #define WGL_NO_ACCELERATION_ARB        0x2025
67 #define WGL_GENERIC_ACCELERATION_ARB   0x2026
68 #define WGL_FULL_ACCELERATION_ARB      0x2027
69
70 static BOOL (WINAPI *pwglChoosePixelFormatARB)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
71 static BOOL (WINAPI *pwglGetPixelFormatAttribivARB)(HDC, int, int, UINT, const int *, int *);
72
73 /* WGL_ARB_pbuffer */
74 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
75 static HPBUFFERARB* (WINAPI *pwglCreatePbufferARB)(HDC, int, int, int, const int *);
76 static HDC (WINAPI *pwglGetPbufferDCARB)(HPBUFFERARB);
77
78 static const char* wgl_extensions = NULL;
79
80 static void init_functions(void)
81 {
82 #define GET_PROC(func) \
83     p ## func = (void*)wglGetProcAddress(#func); \
84     if(!p ## func) \
85       trace("wglGetProcAddress(%s) failed\n", #func);
86
87     /* WGL_ARB_create_context */
88     GET_PROC(wglCreateContextAttribsARB);
89
90     /* WGL_ARB_extensions_string */
91     GET_PROC(wglGetExtensionsStringARB)
92
93     /* WGL_ARB_make_current_read */
94     GET_PROC(wglMakeContextCurrentARB);
95     GET_PROC(wglGetCurrentReadDCARB);
96
97     /* WGL_ARB_pixel_format */
98     GET_PROC(wglChoosePixelFormatARB)
99     GET_PROC(wglGetPixelFormatAttribivARB)
100
101     /* WGL_ARB_pbuffer */
102     GET_PROC(wglCreatePbufferARB)
103     GET_PROC(wglGetPbufferDCARB)
104     GET_PROC(wglReleasePbufferDCARB)
105
106 #undef GET_PROC
107 }
108
109 static void test_pbuffers(HDC hdc)
110 {
111     const int iAttribList[] = { WGL_DRAW_TO_PBUFFER_ARB, 1, /* Request pbuffer support */
112                                 0 };
113     int iFormats[MAX_FORMATS];
114     unsigned int nOnscreenFormats;
115     unsigned int nFormats;
116     int i, res;
117     int iPixelFormat = 0;
118
119     nOnscreenFormats = DescribePixelFormat(hdc, 0, 0, NULL);
120
121     /* When you want to render to a pbuffer you need to call wglGetPbufferDCARB which
122      * returns a 'magic' HDC which you can then pass to wglMakeCurrent to switch rendering
123      * to the pbuffer. Below some tests are performed on what happens if you use standard WGL calls
124      * on this 'magic' HDC for both a pixelformat that support onscreen and offscreen rendering
125      * and a pixelformat that's only available for offscreen rendering (this means that only
126      * wglChoosePixelFormatARB and friends know about the format.
127      *
128      * The first thing we need are pixelformats with pbuffer capabilities.
129      */
130     res = pwglChoosePixelFormatARB(hdc, iAttribList, NULL, MAX_FORMATS, iFormats, &nFormats);
131     if(res <= 0)
132     {
133         skip("No pbuffer compatible formats found while WGL_ARB_pbuffer is supported\n");
134         return;
135     }
136     trace("nOnscreenFormats: %d\n", nOnscreenFormats);
137     trace("Total number of pbuffer capable pixelformats: %d\n", nFormats);
138
139     /* Try to select an onscreen pixelformat out of the list */
140     for(i=0; i < nFormats; i++)
141     {
142         /* Check if the format is onscreen, if it is choose it */
143         if(iFormats[i] <= nOnscreenFormats)
144         {
145             iPixelFormat = iFormats[i];
146             trace("Selected iPixelFormat=%d\n", iPixelFormat);
147             break;
148         }
149     }
150
151     /* A video driver supports a large number of onscreen and offscreen pixelformats.
152      * The traditional WGL calls only see a subset of the whole pixelformat list. First
153      * of all they only see the onscreen formats (the offscreen formats are at the end of the
154      * pixelformat list) and second extended pixelformat capabilities are hidden from the
155      * standard WGL calls. Only functions that depend on WGL_ARB_pixel_format can see them.
156      *
157      * Below we check if the pixelformat is also supported onscreen.
158      */
159     if(iPixelFormat != 0)
160     {
161         HDC pbuffer_hdc;
162         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
163         if(!pbuffer)
164             skip("Pbuffer creation failed!\n");
165
166         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
167         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
168         res = GetPixelFormat(pbuffer_hdc);
169         ok(res == iPixelFormat, "Unexpected iPixelFormat=%d returned by GetPixelFormat for format %d\n", res, iPixelFormat);
170         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
171         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
172
173         pwglReleasePbufferDCARB(pbuffer, hdc);
174     }
175     else skip("Pbuffer test for onscreen pixelformat skipped as no onscreen format with pbuffer capabilities have been found\n");
176
177     /* Search for a real offscreen format */
178     for(i=0, iPixelFormat=0; i<nFormats; i++)
179     {
180         if(iFormats[i] > nOnscreenFormats)
181         {
182             iPixelFormat = iFormats[i];
183             trace("Selected iPixelFormat: %d\n", iPixelFormat);
184             break;
185         }
186     }
187
188     if(iPixelFormat != 0)
189     {
190         HDC pbuffer_hdc;
191         HPBUFFERARB pbuffer = pwglCreatePbufferARB(hdc, iPixelFormat, 640 /* width */, 480 /* height */, NULL);
192         if(!pbuffer)
193             skip("Pbuffer creation failed!\n");
194
195         /* Test the pixelformat returned by GetPixelFormat on a pbuffer as the behavior is not clear */
196         pbuffer_hdc = pwglGetPbufferDCARB(pbuffer);
197         res = GetPixelFormat(pbuffer_hdc);
198
199         ok(res == 1, "Unexpected iPixelFormat=%d (1 expected) returned by GetPixelFormat for offscreen format %d\n", res, iPixelFormat);
200         trace("iPixelFormat returned by GetPixelFormat: %d\n", res);
201         trace("PixelFormat from wglChoosePixelFormatARB: %d\n", iPixelFormat);
202         pwglReleasePbufferDCARB(pbuffer, hdc);
203     }
204     else skip("Pbuffer test for offscreen pixelformat skipped as no offscreen-only format with pbuffer capabilities has been found\n");
205 }
206
207 static void test_setpixelformat(HDC winhdc)
208 {
209     int res = 0;
210     int nCfgs;
211     int pf;
212     int i;
213     HWND hwnd;
214     PIXELFORMATDESCRIPTOR pfd = {
215         sizeof(PIXELFORMATDESCRIPTOR),
216         1,                     /* version */
217         PFD_DRAW_TO_WINDOW |
218         PFD_SUPPORT_OPENGL |
219         PFD_DOUBLEBUFFER,
220         PFD_TYPE_RGBA,
221         24,                    /* 24-bit color depth */
222         0, 0, 0, 0, 0, 0,      /* color bits */
223         0,                     /* alpha buffer */
224         0,                     /* shift bit */
225         0,                     /* accumulation buffer */
226         0, 0, 0, 0,            /* accum bits */
227         32,                    /* z-buffer */
228         0,                     /* stencil buffer */
229         0,                     /* auxiliary buffer */
230         PFD_MAIN_PLANE,        /* main layer */
231         0,                     /* reserved */
232         0, 0, 0                /* layer masks */
233     };
234
235     HDC hdc = GetDC(0);
236     ok(hdc != 0, "GetDC(0) failed!\n");
237
238     /* This should pass even on the main device context */
239     pf = ChoosePixelFormat(hdc, &pfd);
240     ok(pf != 0, "ChoosePixelFormat failed on main device context\n");
241
242     /* SetPixelFormat on the main device context 'X root window' should fail,
243      * but some broken drivers allow it
244      */
245     res = SetPixelFormat(hdc, pf, &pfd);
246     trace("SetPixelFormat on main device context %s\n", res ? "succeeded" : "failed");
247
248     /* Setting the same format that was set on the HDC is allowed; other
249        formats fail */
250     nCfgs = DescribePixelFormat(winhdc, 0, 0, NULL);
251     pf = GetPixelFormat(winhdc);
252     for(i = 1;i <= nCfgs;i++)
253     {
254         int res = SetPixelFormat(winhdc, i, NULL);
255         if(i == pf) ok(res, "Failed to set the same pixel format\n");
256         else ok(!res, "Unexpectedly set an alternate pixel format\n");
257     }
258
259     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
260                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
261     ok(hwnd != NULL, "err: %d\n", GetLastError());
262     if (hwnd)
263     {
264         HDC hdc = GetDC( hwnd );
265         pf = ChoosePixelFormat( hdc, &pfd );
266         ok( pf != 0, "ChoosePixelFormat failed\n" );
267         res = SetPixelFormat( hdc, pf, &pfd );
268         ok( res != 0, "SetPixelFormat failed\n" );
269         i = GetPixelFormat( hdc );
270         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
271         ReleaseDC( hwnd, hdc );
272         hdc = GetWindowDC( hwnd );
273         i = GetPixelFormat( hdc );
274         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
275         ReleaseDC( hwnd, hdc );
276         DestroyWindow( hwnd );
277     }
278
279     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
280                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
281     ok(hwnd != NULL, "err: %d\n", GetLastError());
282     if (hwnd)
283     {
284         HDC hdc = GetWindowDC( hwnd );
285         pf = ChoosePixelFormat( hdc, &pfd );
286         ok( pf != 0, "ChoosePixelFormat failed\n" );
287         res = SetPixelFormat( hdc, pf, &pfd );
288         ok( res != 0, "SetPixelFormat failed\n" );
289         i = GetPixelFormat( hdc );
290         ok( i == pf, "GetPixelFormat returned wrong format %d/%d\n", i, pf );
291         ReleaseDC( hwnd, hdc );
292         DestroyWindow( hwnd );
293     }
294 }
295
296 static void test_sharelists(HDC winhdc)
297 {
298     HGLRC hglrc1, hglrc2, hglrc3;
299     int res;
300
301     hglrc1 = wglCreateContext(winhdc);
302     res = wglShareLists(0, 0);
303     ok(res == FALSE, "Sharing display lists for no contexts passed!\n");
304
305     /* Test 1: Create a context and just share lists without doing anything special */
306     hglrc2 = wglCreateContext(winhdc);
307     if(hglrc2)
308     {
309         res = wglShareLists(hglrc1, hglrc2);
310         ok(res, "Sharing of display lists failed\n");
311         wglDeleteContext(hglrc2);
312     }
313
314     /* Test 2: Share display lists with a 'destination' context which has been made current */
315     hglrc2 = wglCreateContext(winhdc);
316     if(hglrc2)
317     {
318         res = wglMakeCurrent(winhdc, hglrc2);
319         ok(res, "Make current failed\n");
320         res = wglShareLists(hglrc1, hglrc2);
321         todo_wine ok(res, "Sharing display lists with a destination context which has been made current failed\n");
322         wglMakeCurrent(0, 0);
323         wglDeleteContext(hglrc2);
324     }
325
326     /* Test 3: Share display lists with a context which already shares display lists with another context.
327      * According to MSDN the second parameter cannot share any display lists but some buggy drivers might allow it */
328     hglrc3 = wglCreateContext(winhdc);
329     if(hglrc3)
330     {
331         res = wglShareLists(hglrc3, hglrc1);
332         ok(res == FALSE, "Sharing of display lists passed for a context which already shared lists before\n");
333         wglDeleteContext(hglrc3);
334     }
335
336     /* Test 4: Share display lists with a 'source' context which has been made current */
337     hglrc2 = wglCreateContext(winhdc);
338     if(hglrc2)
339     {
340         res = wglMakeCurrent(winhdc, hglrc1);
341         ok(res, "Make current failed\n");
342         res = wglShareLists(hglrc1, hglrc2);
343         ok(res, "Sharing display lists with a source context which has been made current failed\n");
344         wglMakeCurrent(0, 0);
345         wglDeleteContext(hglrc2);
346     }
347 }
348
349 static void test_makecurrent(HDC winhdc)
350 {
351     BOOL ret;
352     HGLRC hglrc;
353     DWORD error;
354
355     hglrc = wglCreateContext(winhdc);
356     ok( hglrc != 0, "wglCreateContext failed\n" );
357
358     ret = wglMakeCurrent( winhdc, hglrc );
359     ok( ret, "wglMakeCurrent failed\n" );
360
361     ok( wglGetCurrentContext() == hglrc, "wrong context\n" );
362
363     /* set the same context again */
364     ret = wglMakeCurrent( winhdc, hglrc );
365     ok( ret, "wglMakeCurrent failed\n" );
366
367     /* check wglMakeCurrent(x, y) after another call to wglMakeCurrent(x, y) */
368     ret = wglMakeCurrent( winhdc, NULL );
369     ok( ret, "wglMakeCurrent failed\n" );
370
371     ret = wglMakeCurrent( winhdc, NULL );
372     ok( ret, "wglMakeCurrent failed\n" );
373
374     SetLastError( 0xdeadbeef );
375     ret = wglMakeCurrent( NULL, NULL );
376     ok( !ret, "wglMakeCurrent succeeded\n" );
377     error = GetLastError();
378     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
379
380     ret = wglMakeCurrent( winhdc, NULL );
381     ok( ret, "wglMakeCurrent failed\n" );
382
383     ret = wglMakeCurrent( winhdc, hglrc );
384     ok( ret, "wglMakeCurrent failed\n" );
385
386     ret = wglMakeCurrent( NULL, NULL );
387     ok( ret, "wglMakeCurrent failed\n" );
388
389     SetLastError( 0xdeadbeef );
390     ret = wglMakeCurrent( NULL, NULL );
391     ok( !ret, "wglMakeCurrent succeeded\n" );
392     error = GetLastError();
393     ok( error == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got error=%x\n", error);
394
395     ret = wglMakeCurrent( winhdc, hglrc );
396     ok( ret, "wglMakeCurrent failed\n" );
397 }
398
399 static void test_colorbits(HDC hdc)
400 {
401     const int iAttribList[] = { WGL_COLOR_BITS_ARB, WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB,
402                                 WGL_BLUE_BITS_ARB, WGL_ALPHA_BITS_ARB };
403     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
404     const int iAttribs[] = { WGL_ALPHA_BITS_ARB, 1, 0 };
405     unsigned int nFormats;
406     int res;
407     int iPixelFormat = 0;
408
409     if (!pwglChoosePixelFormatARB)
410     {
411         win_skip("wglChoosePixelFormatARB is not available\n");
412         return;
413     }
414
415     /* We need a pixel format with at least one bit of alpha */
416     res = pwglChoosePixelFormatARB(hdc, iAttribs, NULL, 1, &iPixelFormat, &nFormats);
417     if(res == FALSE || nFormats == 0)
418     {
419         skip("No suitable pixel formats found\n");
420         return;
421     }
422
423     res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
424               sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList, iAttribRet);
425     if(res == FALSE)
426     {
427         skip("wglGetPixelFormatAttribivARB failed\n");
428         return;
429     }
430     iAttribRet[1] += iAttribRet[2]+iAttribRet[3]+iAttribRet[4];
431     ok(iAttribRet[0] == iAttribRet[1], "WGL_COLOR_BITS_ARB (%d) does not equal R+G+B+A (%d)!\n",
432                                        iAttribRet[0], iAttribRet[1]);
433 }
434
435 static void test_gdi_dbuf(HDC hdc)
436 {
437     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
438     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
439     unsigned int nFormats;
440     int iPixelFormat;
441     int res;
442
443     if (!pwglGetPixelFormatAttribivARB)
444     {
445         win_skip("wglGetPixelFormatAttribivARB is not available\n");
446         return;
447     }
448
449     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
450     for(iPixelFormat = 1;iPixelFormat <= nFormats;iPixelFormat++)
451     {
452         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
453                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
454                   iAttribRet);
455         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
456         if(res == FALSE)
457             continue;
458
459         ok(!(iAttribRet[0] && iAttribRet[1]), "GDI support and double buffering on pixel format %d\n", iPixelFormat);
460     }
461 }
462
463 static void test_acceleration(HDC hdc)
464 {
465     const int iAttribList[] = { WGL_ACCELERATION_ARB };
466     int iAttribRet[sizeof(iAttribList)/sizeof(iAttribList[0])];
467     unsigned int nFormats;
468     int iPixelFormat;
469     int res;
470     PIXELFORMATDESCRIPTOR pfd;
471
472     if (!pwglGetPixelFormatAttribivARB)
473     {
474         win_skip("wglGetPixelFormatAttribivARB is not available\n");
475         return;
476     }
477
478     nFormats = DescribePixelFormat(hdc, 0, 0, NULL);
479     for(iPixelFormat = 1; iPixelFormat <= nFormats; iPixelFormat++)
480     {
481         res = pwglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0,
482                   sizeof(iAttribList)/sizeof(iAttribList[0]), iAttribList,
483                   iAttribRet);
484         ok(res!=FALSE, "wglGetPixelFormatAttribivARB failed for pixel format %d\n", iPixelFormat);
485         if(res == FALSE)
486             continue;
487
488         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
489         DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
490
491         switch(iAttribRet[0])
492         {
493             case WGL_NO_ACCELERATION_ARB:
494                 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);
495                 break;
496             case WGL_GENERIC_ACCELERATION_ARB:
497                 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);
498                 break;
499             case WGL_FULL_ACCELERATION_ARB:
500                 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);
501                 break;
502         }
503     }
504 }
505
506 static void test_bitmap_rendering(void)
507 {
508     PIXELFORMATDESCRIPTOR pfd;
509     int i, iPixelFormat=0;
510     unsigned int nFormats;
511     HGLRC hglrc;
512     BITMAPINFO biDst;
513     HBITMAP bmpDst, oldDst;
514     HDC hdcDst, hdcScreen;
515     UINT32 *dstBuffer;
516
517     memset(&biDst, 0, sizeof(BITMAPINFO));
518     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
519     biDst.bmiHeader.biWidth = 2;
520     biDst.bmiHeader.biHeight = -2;
521     biDst.bmiHeader.biPlanes = 1;
522     biDst.bmiHeader.biBitCount = 32;
523     biDst.bmiHeader.biCompression = BI_RGB;
524
525     hdcScreen = CreateCompatibleDC(0);
526     if(GetDeviceCaps(hdcScreen, BITSPIXEL) != 32)
527     {
528         DeleteDC(hdcScreen);
529         trace("Skipping bitmap rendering test\n");
530         return;
531     }
532
533     hdcDst = CreateCompatibleDC(hdcScreen);
534     bmpDst = CreateDIBSection(hdcDst, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
535     oldDst = SelectObject(hdcDst, bmpDst);
536
537     /* Pick a pixel format by hand because ChoosePixelFormat is unreliable */
538     nFormats = DescribePixelFormat(hdcDst, 0, 0, NULL);
539     for(i=1; i<=nFormats; i++)
540     {
541         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
542         DescribePixelFormat(hdcDst, i, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
543
544         if((pfd.dwFlags & PFD_DRAW_TO_BITMAP) &&
545            (pfd.dwFlags & PFD_SUPPORT_OPENGL) &&
546            (pfd.cColorBits == 32) &&
547            (pfd.cAlphaBits == 8) )
548         {
549             iPixelFormat = i;
550             break;
551         }
552     }
553
554     if(!iPixelFormat)
555     {
556         skip("Unable to find a suitable pixel format\n");
557     }
558     else
559     {
560         SetPixelFormat(hdcDst, iPixelFormat, &pfd);
561         hglrc = wglCreateContext(hdcDst);
562         todo_wine ok(hglrc != NULL, "Unable to create a context\n");
563
564         if(hglrc)
565         {
566             wglMakeCurrent(hdcDst, hglrc);
567
568             /* Note this is RGBA but we read ARGB back */
569             glClearColor((float)0x22/0xff, (float)0x33/0xff, (float)0x44/0xff, (float)0x11/0xff);
570             glClear(GL_COLOR_BUFFER_BIT);
571             glFinish();
572
573             /* Note apparently the alpha channel is not supported by the software renderer (bitmap only works using software) */
574             ok(dstBuffer[0] == 0x223344, "Expected color=0x223344, received color=%x\n", dstBuffer[0]);
575
576             wglMakeCurrent(NULL, NULL);
577             wglDeleteContext(hglrc);
578         }
579     }
580
581     SelectObject(hdcDst, oldDst);
582     DeleteObject(bmpDst);
583     DeleteDC(hdcDst);
584
585     DeleteDC(hdcScreen);
586 }
587
588 struct wgl_thread_param
589 {
590     HANDLE test_finished;
591     HGLRC hglrc;
592     BOOL hglrc_deleted;
593 };
594
595 static DWORD WINAPI wgl_thread(void *param)
596 {
597     struct wgl_thread_param *p = param;
598
599     p->hglrc_deleted = wglDeleteContext(p->hglrc);
600     SetEvent(p->test_finished);
601
602     return 0;
603 }
604
605 static void test_deletecontext(HDC hdc)
606 {
607     struct wgl_thread_param thread_params;
608     HGLRC hglrc = wglCreateContext(hdc);
609     HANDLE thread_handle;
610     DWORD res, tid;
611
612     if(!hglrc)
613     {
614         skip("wglCreateContext failed!\n");
615         return;
616     }
617
618     res = wglMakeCurrent(hdc, hglrc);
619     if(!res)
620     {
621         skip("wglMakeCurrent failed!\n");
622         return;
623     }
624
625     /* WGL doesn't allow you to delete a context from a different thread than the one in which it is current.
626      * This differs from GLX which does allow it but it delays actual deletion until the context becomes not current.
627      */
628     thread_params.hglrc = hglrc;
629     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
630     thread_handle = CreateThread(NULL, 0, wgl_thread, &thread_params, 0, &tid);
631     ok(!!thread_handle, "Failed to create thread, last error %#x.\n", GetLastError());
632     if(thread_handle)
633     {
634         WaitForSingleObject(thread_handle, INFINITE);
635         ok(thread_params.hglrc_deleted == FALSE, "Attempt to delete WGL context from another thread passed but should fail!\n");
636     }
637     CloseHandle(thread_params.test_finished);
638
639     res = wglDeleteContext(hglrc);
640     ok(res == TRUE, "wglDeleteContext failed\n");
641
642     /* WGL makes a context not current when deleting it. This differs from GLX behavior where
643      * deletion takes place when the thread becomes not current. */
644     hglrc = wglGetCurrentContext();
645     ok(hglrc == NULL, "A WGL context is active while none was expected\n");
646 }
647
648 static void test_make_current_read(HDC hdc)
649 {
650     int res;
651     HDC hread;
652     HGLRC hglrc = wglCreateContext(hdc);
653
654     if(!hglrc)
655     {
656         skip("wglCreateContext failed!\n");
657         return;
658     }
659
660     res = wglMakeCurrent(hdc, hglrc);
661     if(!res)
662     {
663         skip("wglMakeCurrent failed!\n");
664         return;
665     }
666
667     /* Test what wglGetCurrentReadDCARB does for wglMakeCurrent as the spec doesn't mention it */
668     hread = pwglGetCurrentReadDCARB();
669     trace("hread %p, hdc %p\n", hread, hdc);
670     ok(hread == hdc, "wglGetCurrentReadDCARB failed for standard wglMakeCurrent\n");
671
672     pwglMakeContextCurrentARB(hdc, hdc, hglrc);
673     hread = pwglGetCurrentReadDCARB();
674     ok(hread == hdc, "wglGetCurrentReadDCARB failed for wglMakeContextCurrent\n");
675 }
676
677 static void test_dc(HWND hwnd, HDC hdc)
678 {
679     int pf1, pf2;
680     HDC hdc2;
681
682     /* Get another DC and make sure it has the same pixel format */
683     hdc2 = GetDC(hwnd);
684     if(hdc != hdc2)
685     {
686         pf1 = GetPixelFormat(hdc);
687         pf2 = GetPixelFormat(hdc2);
688         ok(pf1 == pf2, "Second DC does not have the same format (%d != %d)\n", pf1, pf2);
689     }
690     else
691         skip("Could not get a different DC for the window\n");
692
693     if(hdc2)
694     {
695         ReleaseDC(hwnd, hdc2);
696         hdc2 = NULL;
697     }
698 }
699
700 /* Nvidia converts win32 error codes to (0xc007 << 16) | win32_error_code */
701 #define NVIDIA_HRESULT_FROM_WIN32(x) (HRESULT_FROM_WIN32(x) | 0x40000000)
702 static void test_opengl3(HDC hdc)
703 {
704     /* Try to create a context compatible with OpenGL 1.x; 1.0-2.1 is allowed */
705     {
706         HGLRC gl3Ctx;
707         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 1, 0};
708
709         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
710         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 1.x context failed!\n");
711         wglDeleteContext(gl3Ctx);
712     }
713
714     /* Try to pass an invalid HDC */
715     {
716         HGLRC gl3Ctx;
717         DWORD error;
718         gl3Ctx = pwglCreateContextAttribsARB((HDC)0xdeadbeef, 0, 0);
719         ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid HDC passed\n");
720         error = GetLastError();
721         todo_wine ok(error == ERROR_DC_NOT_FOUND ||
722                      broken(error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_DATA)), /* Nvidia Vista + Win7 */
723                      "Expected ERROR_DC_NOT_FOUND, got error=%x\n", error);
724         wglDeleteContext(gl3Ctx);
725     }
726
727     /* Try to pass an invalid shareList */
728     {
729         HGLRC gl3Ctx;
730         DWORD error;
731         gl3Ctx = pwglCreateContextAttribsARB(hdc, (HGLRC)0xdeadbeef, 0);
732         todo_wine ok(gl3Ctx == 0, "pwglCreateContextAttribsARB using an invalid shareList passed\n");
733         error = GetLastError();
734         /* The Nvidia implementation seems to return hresults instead of win32 error codes */
735         todo_wine ok(error == ERROR_INVALID_OPERATION ||
736                      error == NVIDIA_HRESULT_FROM_WIN32(ERROR_INVALID_OPERATION), "Expected ERROR_INVALID_OPERATION, got error=%x\n", error);
737         wglDeleteContext(gl3Ctx);
738     }
739
740     /* Try to create an OpenGL 3.0 context */
741     {
742         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
743         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
744
745         if(gl3Ctx == NULL)
746         {
747             skip("Skipping the rest of the WGL_ARB_create_context test due to lack of OpenGL 3.0\n");
748             return;
749         }
750
751         wglDeleteContext(gl3Ctx);
752     }
753
754     /* 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 */
755     {
756         HGLRC glCtx = wglCreateContext(hdc);
757
758         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
759         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};
760
761         HGLRC gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs);
762         ok(gl3Ctx != NULL, "Sharing of a display list between OpenGL 3.0 and OpenGL 1.x/2.x failed!\n");
763         if(gl3Ctx)
764             wglDeleteContext(gl3Ctx);
765
766         gl3Ctx = pwglCreateContextAttribsARB(hdc, glCtx, attribs_future);
767         ok(gl3Ctx != NULL, "Sharing of a display list between a forward compatible OpenGL 3.0 context and OpenGL 1.x/2.x failed!\n");
768         if(gl3Ctx)
769             wglDeleteContext(gl3Ctx);
770
771         if(glCtx)
772             wglDeleteContext(glCtx);
773     }
774
775     /* Try to create an OpenGL 3.0 context and test windowless rendering */
776     {
777         HGLRC gl3Ctx;
778         int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0};
779         BOOL res;
780
781         gl3Ctx = pwglCreateContextAttribsARB(hdc, 0, attribs);
782         ok(gl3Ctx != 0, "pwglCreateContextAttribsARB for a 3.0 context failed!\n");
783
784         /* OpenGL 3.0 allows offscreen rendering WITHOUT a drawable
785          * Neither AMD or Nvidia support it at this point. The WGL_ARB_create_context specs also say that
786          * it is hard because drivers use the HDC to enter the display driver and it sounds like they don't
787          * expect drivers to ever offer it.
788          */
789         res = wglMakeCurrent(0, gl3Ctx);
790         ok(res == FALSE, "Wow, OpenGL 3.0 windowless rendering passed while it was expected not to!\n");
791         if(res)
792             wglMakeCurrent(0, 0);
793
794         if(gl3Ctx)
795             wglDeleteContext(gl3Ctx);
796     }
797 }
798
799 static void test_minimized(void)
800 {
801     PIXELFORMATDESCRIPTOR pf_desc =
802     {
803         sizeof(PIXELFORMATDESCRIPTOR),
804         1,                     /* version */
805         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
806         PFD_TYPE_RGBA,
807         24,                    /* 24-bit color depth */
808         0, 0, 0, 0, 0, 0,      /* color bits */
809         0,                     /* alpha buffer */
810         0,                     /* shift bit */
811         0,                     /* accumulation buffer */
812         0, 0, 0, 0,            /* accum bits */
813         32,                    /* z-buffer */
814         0,                     /* stencil buffer */
815         0,                     /* auxiliary buffer */
816         PFD_MAIN_PLANE,        /* main layer */
817         0,                     /* reserved */
818         0, 0, 0                /* layer masks */
819     };
820     int pixel_format;
821     HWND window;
822     LONG style;
823     HGLRC ctx;
824     BOOL ret;
825     HDC dc;
826
827     window = CreateWindowA("static", "opengl32_test",
828             WS_POPUP | WS_MINIMIZE, 0, 0, 640, 480, 0, 0, 0, 0);
829     ok(!!window, "Failed to create window, last error %#x.\n", GetLastError());
830
831     dc = GetDC(window);
832     ok(!!dc, "Failed to get DC.\n");
833
834     pixel_format = ChoosePixelFormat(dc, &pf_desc);
835     if (!pixel_format)
836     {
837         win_skip("Failed to find pixel format.\n");
838         ReleaseDC(window, dc);
839         DestroyWindow(window);
840         return;
841     }
842
843     ret = SetPixelFormat(dc, pixel_format, &pf_desc);
844     ok(ret, "Failed to set pixel format, last error %#x.\n", GetLastError());
845
846     style = GetWindowLongA(window, GWL_STYLE);
847     ok(style & WS_MINIMIZE, "Window should be minimized, got style %#x.\n", style);
848
849     ctx = wglCreateContext(dc);
850     ok(!!ctx, "Failed to create GL context, last error %#x.\n", GetLastError());
851
852     ret = wglMakeCurrent(dc, ctx);
853     ok(ret, "Failed to make context current, last error %#x.\n", GetLastError());
854
855     style = GetWindowLongA(window, GWL_STYLE);
856     ok(style & WS_MINIMIZE, "window should be minimized, got style %#x.\n", style);
857
858     ret = wglMakeCurrent(NULL, NULL);
859     ok(ret, "Failed to clear current context, last error %#x.\n", GetLastError());
860
861     ret = wglDeleteContext(ctx);
862     ok(ret, "Failed to delete GL context, last error %#x.\n", GetLastError());
863
864     ReleaseDC(window, dc);
865     DestroyWindow(window);
866 }
867
868 START_TEST(opengl)
869 {
870     HWND hwnd;
871     PIXELFORMATDESCRIPTOR pfd = {
872         sizeof(PIXELFORMATDESCRIPTOR),
873         1,                     /* version */
874         PFD_DRAW_TO_WINDOW |
875         PFD_SUPPORT_OPENGL |
876         PFD_DOUBLEBUFFER,
877         PFD_TYPE_RGBA,
878         24,                    /* 24-bit color depth */
879         0, 0, 0, 0, 0, 0,      /* color bits */
880         0,                     /* alpha buffer */
881         0,                     /* shift bit */
882         0,                     /* accumulation buffer */
883         0, 0, 0, 0,            /* accum bits */
884         32,                    /* z-buffer */
885         0,                     /* stencil buffer */
886         0,                     /* auxiliary buffer */
887         PFD_MAIN_PLANE,        /* main layer */
888         0,                     /* reserved */
889         0, 0, 0                /* layer masks */
890     };
891
892     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
893                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
894     ok(hwnd != NULL, "err: %d\n", GetLastError());
895     if (hwnd)
896     {
897         HDC hdc;
898         int iPixelFormat, res;
899         HGLRC hglrc;
900         DWORD error;
901         ShowWindow(hwnd, SW_SHOW);
902
903         hdc = GetDC(hwnd);
904
905         iPixelFormat = ChoosePixelFormat(hdc, &pfd);
906         if(iPixelFormat == 0)
907         {
908             /* This should never happen as ChoosePixelFormat always returns a closest match, but currently this fails in Wine if we don't have glX */
909             win_skip("Unable to find pixel format.\n");
910             goto cleanup;
911         }
912
913         /* We shouldn't be able to create a context from a hdc which doesn't have a pixel format set */
914         hglrc = wglCreateContext(hdc);
915         ok(hglrc == NULL, "wglCreateContext should fail when no pixel format has been set, but it passed\n");
916         error = GetLastError();
917         ok(error == ERROR_INVALID_PIXEL_FORMAT, "expected ERROR_INVALID_PIXEL_FORMAT for wglCreateContext without a pixelformat set, but received %#x\n", error);
918
919         res = SetPixelFormat(hdc, iPixelFormat, &pfd);
920         ok(res, "SetPixelformat failed: %x\n", GetLastError());
921
922         test_bitmap_rendering();
923         test_minimized();
924         test_dc(hwnd, hdc);
925
926         hglrc = wglCreateContext(hdc);
927         res = wglMakeCurrent(hdc, hglrc);
928         ok(res, "wglMakeCurrent failed!\n");
929         if(res)
930         {
931             trace("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
932             trace("OpenGL driver version: %s\n", glGetString(GL_VERSION));
933             trace("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
934         }
935         else
936         {
937             skip("Skipping OpenGL tests without a current context\n");
938             return;
939         }
940
941         /* Initialisation of WGL functions depends on an implicit WGL context. For this reason we can't load them before making
942          * any WGL call :( On Wine this would work but not on real Windows because there can be different implementations (software, ICD, MCD).
943          */
944         init_functions();
945         /* The lack of wglGetExtensionsStringARB in general means broken software rendering or the lack of decent OpenGL support, skip tests in such cases */
946         if (!pwglGetExtensionsStringARB)
947         {
948             win_skip("wglGetExtensionsStringARB is not available\n");
949             return;
950         }
951
952         test_deletecontext(hdc);
953         test_makecurrent(hdc);
954         test_setpixelformat(hdc);
955         test_sharelists(hdc);
956         test_colorbits(hdc);
957         test_gdi_dbuf(hdc);
958         test_acceleration(hdc);
959
960         wgl_extensions = pwglGetExtensionsStringARB(hdc);
961         if(wgl_extensions == NULL) skip("Skipping opengl32 tests because this OpenGL implementation doesn't support WGL extensions!\n");
962
963         if(strstr(wgl_extensions, "WGL_ARB_create_context"))
964             test_opengl3(hdc);
965
966         if(strstr(wgl_extensions, "WGL_ARB_make_current_read"))
967             test_make_current_read(hdc);
968         else
969             skip("WGL_ARB_make_current_read not supported, skipping test\n");
970
971         if(strstr(wgl_extensions, "WGL_ARB_pbuffer"))
972             test_pbuffers(hdc);
973         else
974             skip("WGL_ARB_pbuffer not supported, skipping pbuffer test\n");
975
976 cleanup:
977         ReleaseDC(hwnd, hdc);
978         DestroyWindow(hwnd);
979     }
980 }