crypt32: Fix Mingw compile.
[wine] / dlls / opengl32 / wgl_ext.c
1 /* Support for window-specific OpenGL extensions.
2  *
3  * Copyright (c) 2004 Lionel Ulmer
4  * Copyright (c) 2005 Alex Woods
5  * Copyright (c) 2005 Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winerror.h"
33
34 #include "gdi.h"
35 #include "wgl_ext.h"
36 #include "opengl_ext.h"
37 #include "wine/library.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
41
42
43 /* x11drv GDI escapes */
44 #define X11DRV_ESCAPE 6789
45 enum x11drv_escape_codes
46 {
47     X11DRV_GET_DISPLAY,   /* get X11 display for a DC */
48     X11DRV_GET_DRAWABLE,  /* get current drawable for a DC */
49     X11DRV_GET_FONT,      /* get current X font for a DC */
50     X11DRV_SET_DRAWABLE,     /* set current drawable for a DC */
51 };
52 struct x11drv_escape_set_drawable
53 {
54     enum x11drv_escape_codes code;         /* escape code (X11DRV_SET_DRAWABLE) */
55     Drawable                 drawable;     /* X drawable */
56     int                      mode;         /* ClipByChildren or IncludeInferiors */
57     POINT                    org;          /* origin of DC relative to drawable */
58     POINT                    drawable_org; /* origin of drawable relative to screen */
59 };
60
61 /* retrieve the X display to use on a given DC */
62 inline static Display *get_display( HDC hdc )
63 {
64     Display *display;
65     enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;
66
67     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
68                     sizeof(display), (LPSTR)&display )) display = NULL;
69     return display;
70 }
71 inline static void set_drawable( HDC hdc, Drawable drawable )
72 {
73     struct x11drv_escape_set_drawable escape;
74
75     escape.code = X11DRV_SET_DRAWABLE;
76     escape.drawable = drawable;
77     escape.mode = IncludeInferiors;
78     escape.org.x = escape.org.y = 0;
79     escape.drawable_org.x = escape.drawable_org.y = 0;
80
81     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape, 0, NULL );
82 }
83
84 /* Some WGL extensions... */
85 static const char *WGL_extensions_base = "WGL_ARB_extensions_string WGL_EXT_extensions_string";
86 static char *WGL_extensions = NULL;
87
88 /**
89  * Extensions-query functions 
90  *
91  * @TODO: use a struct to handle parameters 
92  */
93 BOOL query_function_make_current_read(glXGetProcAddressARB_t proc, const char *gl_version, const char *gl_extensions, 
94                                       const char* glx_version, const char *glx_extensions,
95                                       const char *server_glx_extensions, const char *client_glx_extensions)
96 {
97   return 0 <= strcmp("1.3", glx_version);
98 }
99
100 BOOL query_function_multisample(glXGetProcAddressARB_t proc, const char *gl_version, const char *gl_extensions, 
101                                 const char* glx_version, const char *glx_extensions,
102                                 const char *server_glx_extensions, const char *client_glx_extensions)
103 {
104   return NULL != strstr(glx_extensions, "GLX_ARB_multisample");
105 }
106
107 BOOL query_function_pbuffer(glXGetProcAddressARB_t proc, const char *gl_version, const char *gl_extensions, 
108                             const char* glx_version, const char *glx_extensions,
109                             const char *server_glx_extensions, const char *client_glx_extensions)
110 {
111   TRACE("gl_version is: \"%s\"\n", gl_version);
112   TRACE("glx_exts is: \"%s\"\n", glx_extensions);
113
114   return 0 <= strcmp("1.3", glx_version) || NULL != strstr(glx_extensions, "GLX_SGIX_pbuffer");
115 }
116
117 BOOL query_function_pixel_format(glXGetProcAddressARB_t proc, const char *gl_version, const char *gl_extensions, 
118                                  const char* glx_version, const char *glx_extensions,
119                                  const char *server_glx_extensions, const char *client_glx_extensions)
120 {
121   return TRUE;
122 }
123
124 /** GLX_ARB_render_texture */
125 /**
126  * http://oss.sgi.com/projects/ogl-sample/registry/ARB/wgl_render_texture.txt
127  * ~/tmp/ogl/ogl_offscreen_rendering_3
128  */
129 Bool (*p_glXBindTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
130 Bool (*p_glXReleaseTexImageARB)(Display *dpy, GLXPbuffer pbuffer, int buffer);
131 Bool (*p_glXDrawableAttribARB)(Display *dpy, GLXDrawable draw, const int *attribList);
132 int  use_render_texture_emulation = 0;
133 int  use_render_texture_ati = 0;
134 BOOL query_function_render_texture(glXGetProcAddressARB_t proc, const char *gl_version, const char *gl_extensions, 
135                                    const char* glx_version, const char *glx_extensions,
136                                    const char *server_glx_extensions, const char *client_glx_extensions)
137 {
138   BOOL bTest = FALSE;
139   if (NULL != strstr(glx_extensions, "GLX_ATI_render_texture")) {
140     p_glXBindTexImageARB = proc( (const GLubyte *) "glXBindTexImageARB");
141     p_glXReleaseTexImageARB = proc( (const GLubyte *) "glXReleaseTexImageARB");
142     p_glXDrawableAttribARB = proc( (const GLubyte *) "glXDrawableAttribARB");
143     bTest = (NULL != p_glXBindTexImageARB && NULL != p_glXReleaseTexImageARB && NULL != p_glXDrawableAttribARB);
144   }
145   if (bTest) {
146     TRACE("Active WGL_render_texture: found GLX_ATI_render_texture extensions\n");
147     use_render_texture_ati = 1;
148     return bTest;
149   }
150   bTest = (0 <= strcmp("1.3", glx_version) || NULL != strstr(glx_extensions, "GLX_SGIX_pbuffer"));
151   if (bTest) {
152     if (NULL != strstr(glx_extensions, "GLX_ARB_render_texture")) {
153       p_glXBindTexImageARB = proc( (const GLubyte *) "glXBindTexImageARB");
154       p_glXReleaseTexImageARB = proc( (const GLubyte *) "glXReleaseTexImageARB");
155       p_glXDrawableAttribARB = proc( (const GLubyte *) "glXDrawableAttribARB");
156       TRACE("glXBindTexImageARB found as %p\n", p_glXBindTexImageARB);
157       TRACE("glXReleaseTexImageARB found as %p\n", p_glXReleaseTexImageARB);
158       TRACE("glXDrawableAttribARB found as %p\n", p_glXDrawableAttribARB);
159       bTest = (NULL != p_glXBindTexImageARB && NULL != p_glXReleaseTexImageARB && NULL != p_glXDrawableAttribARB);
160     } else {      
161       TRACE("Active WGL_render_texture: emulation using pbuffers\n");
162       use_render_texture_emulation = 1;
163     }
164   }
165   return bTest;
166 }
167
168 int (*p_glXSwapIntervalSGI)(int);
169 BOOL query_function_swap_control(glXGetProcAddressARB_t proc, const char *gl_version, const char *gl_extensions, 
170                                  const char* glx_version, const char *glx_extensions,
171                                  const char *server_glx_extensions, const char *client_glx_extensions)
172 {
173   BOOL bTest = (0 <= strcmp("1.3", glx_version) || NULL != strstr(glx_extensions, "GLX_SGI_swap_control"));
174   if (bTest) {
175     p_glXSwapIntervalSGI = proc( (const GLubyte *) "glXSwapIntervalSGI");
176     bTest = (NULL != p_glXSwapIntervalSGI);
177   }
178   return bTest;
179 }
180
181 /***********************************************************************
182  *              wglGetExtensionsStringEXT(OPENGL32.@)
183  */
184 const char * WINAPI wglGetExtensionsStringEXT(void) {
185     TRACE("() returning \"%s\"\n", WGL_extensions);
186
187     return WGL_extensions;
188 }
189
190 /***********************************************************************
191  *              wglGetExtensionsStringARB(OPENGL32.@)
192  */
193 const char * WINAPI wglGetExtensionsStringARB(HDC hdc) {
194     TRACE("() returning \"%s\"\n", WGL_extensions);
195
196     return WGL_extensions;    
197 }
198
199 static int swap_interval = 1;
200
201 /***********************************************************************
202  *              wglSwapIntervalEXT(OPENGL32.@)
203  */
204 BOOL WINAPI wglSwapIntervalEXT(int interval) {
205   TRACE("(%d)\n", interval);
206   swap_interval = interval;
207   if (NULL != p_glXSwapIntervalSGI) {
208     return 0 == p_glXSwapIntervalSGI(interval);
209   }
210   WARN("(): GLX_SGI_swap_control extension seems not supported\n");
211   return TRUE;
212 }
213
214 /***********************************************************************
215  *              wglGetSwapIntervalEXT(OPENGL32.@)
216  */
217 int WINAPI wglGetSwapIntervalEXT(VOID) {
218     FIXME("(),stub!\n");
219     return swap_interval;
220 }
221
222 typedef struct wine_glpbuffer {
223   Drawable   drawable;
224   Display*   display;
225   int        pixelFormat;
226   int        width;
227   int        height;
228   int*       attribList;
229   HDC        hdc;
230
231   int        use_render_texture;
232   GLuint     texture_target;
233   GLuint     texture_bind_target;
234   GLuint     texture;
235   int        texture_level;
236   HDC        prev_hdc;
237   HGLRC      prev_ctx;
238   HDC        render_hdc;
239   HGLRC      render_ctx;
240 } Wine_GLPBuffer;
241
242 #define PUSH1(attribs,att)        attribs[nAttribs++] = (att); 
243 #define PUSH2(attribs,att,value)  attribs[nAttribs++] = (att); attribs[nAttribs++] = (value);
244
245 #define WGL_NUMBER_PIXEL_FORMATS_ARB            0x2000
246 #define WGL_DRAW_TO_WINDOW_ARB                  0x2001
247 #define WGL_DRAW_TO_BITMAP_ARB                  0x2002
248 #define WGL_ACCELERATION_ARB                    0x2003
249 #define WGL_NEED_PALETTE_ARB                    0x2004
250 #define WGL_NEED_SYSTEM_PALETTE_ARB             0x2005
251 #define WGL_SWAP_LAYER_BUFFERS_ARB              0x2006
252 #define WGL_SWAP_METHOD_ARB                     0x2007
253 #define WGL_NUMBER_OVERLAYS_ARB                 0x2008
254 #define WGL_NUMBER_UNDERLAYS_ARB                0x2009
255 #define WGL_TRANSPARENT_ARB                     0x200A
256 #define WGL_TRANSPARENT_RED_VALUE_ARB           0x2037
257 #define WGL_TRANSPARENT_GREEN_VALUE_ARB         0x2038
258 #define WGL_TRANSPARENT_BLUE_VALUE_ARB          0x2039
259 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB         0x203A
260 #define WGL_TRANSPARENT_INDEX_VALUE_ARB         0x203B
261 #define WGL_SHARE_DEPTH_ARB                     0x200C
262 #define WGL_SHARE_STENCIL_ARB                   0x200D
263 #define WGL_SHARE_ACCUM_ARB                     0x200E
264 #define WGL_SUPPORT_GDI_ARB                     0x200F
265 #define WGL_SUPPORT_OPENGL_ARB                  0x2010
266 #define WGL_DOUBLE_BUFFER_ARB                   0x2011
267 #define WGL_STEREO_ARB                          0x2012
268 #define WGL_PIXEL_TYPE_ARB                      0x2013
269 #define WGL_COLOR_BITS_ARB                      0x2014
270 #define WGL_RED_BITS_ARB                        0x2015
271 #define WGL_RED_SHIFT_ARB                       0x2016
272 #define WGL_GREEN_BITS_ARB                      0x2017
273 #define WGL_GREEN_SHIFT_ARB                     0x2018
274 #define WGL_BLUE_BITS_ARB                       0x2019
275 #define WGL_BLUE_SHIFT_ARB                      0x201A
276 #define WGL_ALPHA_BITS_ARB                      0x201B
277 #define WGL_ALPHA_SHIFT_ARB                     0x201C
278 #define WGL_ACCUM_BITS_ARB                      0x201D
279 #define WGL_ACCUM_RED_BITS_ARB                  0x201E
280 #define WGL_ACCUM_GREEN_BITS_ARB                0x201F
281 #define WGL_ACCUM_BLUE_BITS_ARB                 0x2020
282 #define WGL_ACCUM_ALPHA_BITS_ARB                0x2021
283 #define WGL_DEPTH_BITS_ARB                      0x2022
284 #define WGL_STENCIL_BITS_ARB                    0x2023
285 #define WGL_AUX_BUFFERS_ARB                     0x2024
286
287 #define WGL_NO_ACCELERATION_ARB                 0x2025
288 #define WGL_GENERIC_ACCELERATION_ARB            0x2026
289 #define WGL_FULL_ACCELERATION_ARB               0x2027
290
291 #define WGL_PBUFFER_WIDTH_ARB                   0x2034
292 #define WGL_PBUFFER_HEIGHT_ARB                  0x2035
293 #define WGL_PBUFFER_LOST_ARB                    0x2036
294 #define WGL_DRAW_TO_PBUFFER_ARB                 0x202D
295 #define WGL_MAX_PBUFFER_PIXELS_ARB              0x202E
296 #define WGL_MAX_PBUFFER_WIDTH_ARB               0x202F
297 #define WGL_MAX_PBUFFER_HEIGHT_ARB              0x2030
298 #define WGL_PBUFFER_LARGEST_ARB                 0x2033
299
300 #define WGL_TYPE_RGBA_ARB                       0x202B
301 #define WGL_TYPE_COLORINDEX_ARB                 0x202C
302
303 #define WGL_SAMPLE_BUFFERS_ARB                  0x2041
304 #define WGL_SAMPLES_ARB                         0x2042
305
306 /**
307  * WGL_render_texture 
308  */
309 /** GetPixelFormat/ChoosePixelFormat */
310 #define WGL_BIND_TO_TEXTURE_RGB_ARB             0x2070
311 #define WGL_BIND_TO_TEXTURE_RGBA_ARB            0x2071
312 /** CreatePbuffer / QueryPbuffer */
313 #define WGL_TEXTURE_FORMAT_ARB                  0x2072
314 #define WGL_TEXTURE_TARGET_ARB                  0x2073
315 #define WGL_MIPMAP_TEXTURE_ARB                  0x2074
316 /** CreatePbuffer / QueryPbuffer */
317 #define WGL_TEXTURE_RGB_ARB                     0x2075
318 #define WGL_TEXTURE_RGBA_ARB                    0x2076
319 #define WGL_NO_TEXTURE_ARB                      0x2077
320 /** CreatePbuffer / QueryPbuffer */
321 #define WGL_TEXTURE_CUBE_MAP_ARB                0x2078
322 #define WGL_TEXTURE_1D_ARB                      0x2079
323 #define WGL_TEXTURE_2D_ARB                      0x207A
324 #define WGL_NO_TEXTURE_ARB                      0x2077
325 /** SetPbufferAttribARB/QueryPbufferARB parameters */
326 #define WGL_MIPMAP_LEVEL_ARB                    0x207B
327 #define WGL_CUBE_MAP_FACE_ARB                   0x207C
328 /** SetPbufferAttribARB/QueryPbufferARB attribs */
329 #define WGL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB     0x207D
330 #define WGL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB     0x207E
331 #define WGL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB     0x207F
332 #define WGL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB     0x2080
333 #define WGL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB     0x2081 
334 #define WGL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB     0x2082
335 /** BindTexImageARB/ReleaseTexImageARB */
336 #define WGL_FRONT_LEFT_ARB                  0x2083
337 #define WGL_FRONT_RIGHT_ARB                 0x2084
338 #define WGL_BACK_LEFT_ARB                   0x2085
339 #define WGL_BACK_RIGHT_ARB                  0x2086
340 #define WGL_AUX0_ARB                        0x2087 
341 #define WGL_AUX1_ARB                        0x2088 
342 #define WGL_AUX2_ARB                        0x2089 
343 #define WGL_AUX3_ARB                        0x208A 
344 #define WGL_AUX4_ARB                        0x208B 
345 #define WGL_AUX5_ARB                        0x208C 
346 #define WGL_AUX6_ARB                        0x208D
347 #define WGL_AUX7_ARB                        0x208E 
348 #define WGL_AUX8_ARB                        0x208F 
349 #define WGL_AUX9_ARB                        0x2090
350
351 /** 
352  * WGL_ATI_pixel_format_float / WGL_ARB_color_buffer_float
353  */
354 #define WGL_TYPE_RGBA_FLOAT_ATI             0x21A0
355 #define GL_RGBA_FLOAT_MODE_ATI              0x8820
356 #define GL_COLOR_CLEAR_UNCLAMPED_VALUE_ATI  0x8835
357 #define GL_CLAMP_VERTEX_COLOR_ARB           0x891A
358 #define GL_CLAMP_FRAGMENT_COLOR_ARB         0x891B
359 #define GL_CLAMP_READ_COLOR_ARB             0x891C
360 /** GLX_ATI_pixel_format_float */
361 #define GLX_RGBA_FLOAT_ATI_BIT              0x00000100
362 /** GLX_ARB_pixel_format_float */
363 #define GLX_RGBA_FLOAT_BIT                  0x00000004
364 #define GLX_RGBA_FLOAT_TYPE                 0x20B9 
365 /** GLX_ATI_render_texture */
366 #define GLX_BIND_TO_TEXTURE_RGB_ATI         0x9800
367 #define GLX_BIND_TO_TEXTURE_RGBA_ATI        0x9801
368 #define GLX_TEXTURE_FORMAT_ATI              0x9802
369 #define GLX_TEXTURE_TARGET_ATI              0x9803
370 #define GLX_MIPMAP_TEXTURE_ATI              0x9804
371 #define GLX_TEXTURE_RGB_ATI                 0x9805
372 #define GLX_TEXTURE_RGBA_ATI                0x9806
373 #define GLX_NO_TEXTURE_ATI                  0x9807
374 #define GLX_TEXTURE_CUBE_MAP_ATI            0x9808
375 #define GLX_TEXTURE_1D_ATI                  0x9809
376 #define GLX_TEXTURE_2D_ATI                  0x980A
377 #define GLX_MIPMAP_LEVEL_ATI                0x980B
378 #define GLX_CUBE_MAP_FACE_ATI               0x980C
379 #define GLX_TEXTURE_CUBE_MAP_POSITIVE_X_ATI 0x980D
380 #define GLX_TEXTURE_CUBE_MAP_NEGATIVE_X_ATI 0x980E
381 #define GLX_TEXTURE_CUBE_MAP_POSITIVE_Y_ATI 0x980F
382 #define GLX_TEXTURE_CUBE_MAP_NEGATIVE_Y_ATI 0x9810
383 #define GLX_TEXTURE_CUBE_MAP_POSITIVE_Z_ATI 0x9811
384 #define GLX_TEXTURE_CUBE_MAP_NEGATIVE_Z_ATI 0x9812
385 #define GLX_FRONT_LEFT_ATI                  0x9813
386 #define GLX_FRONT_RIGHT_ATI                 0x9814
387 #define GLX_BACK_LEFT_ATI                   0x9815
388 #define GLX_BACK_RIGHT_ATI                  0x9816
389 #define GLX_AUX0_ATI                        0x9817
390 #define GLX_AUX1_ATI                        0x9818
391 #define GLX_AUX2_ATI                        0x9819
392 #define GLX_AUX3_ATI                        0x981A
393 #define GLX_AUX4_ATI                        0x981B
394 #define GLX_AUX5_ATI                        0x981C
395 #define GLX_AUX6_ATI                        0x981D
396 #define GLX_AUX7_ATI                        0x981E
397 #define GLX_AUX8_ATI                        0x981F
398 #define GLX_AUX9_ATI                        0x9820
399 #define GLX_BIND_TO_TEXTURE_LUMINANCE_ATI   0x9821
400 #define GLX_BIND_TO_TEXTURE_INTENSITY_ATI   0x9822
401
402
403
404 #if 0 /* not used yet */
405 static int ConvertAttribGLXtoWGL(const int* iWGLAttr, int* oGLXAttr) {
406   int nAttribs = 0;
407   FIXME("not yet implemented!\n");
408   return nAttribs;
409 }
410 #endif
411
412 static int ConvertAttribWGLtoGLX(const int* iWGLAttr, int* oGLXAttr, Wine_GLPBuffer* pbuf) {
413   int nAttribs = 0;
414   unsigned cur = 0; 
415   int pop;
416   int isColor = 0;
417   int wantColorBits = 0;
418   int sz_alpha = 0;
419
420   while (0 != iWGLAttr[cur]) {
421     TRACE("pAttr[%d] = %x\n", cur, iWGLAttr[cur]);
422
423     switch (iWGLAttr[cur]) {
424     case WGL_COLOR_BITS_ARB:
425       pop = iWGLAttr[++cur];
426       wantColorBits = pop; /** see end */
427       break;
428     case WGL_BLUE_BITS_ARB:
429       pop = iWGLAttr[++cur];
430       PUSH2(oGLXAttr, GLX_BLUE_SIZE, pop);
431       TRACE("pAttr[%d] = GLX_BLUE_SIZE: %d\n", cur, pop);
432       break;
433     case WGL_RED_BITS_ARB:
434       pop = iWGLAttr[++cur];
435       PUSH2(oGLXAttr, GLX_RED_SIZE, pop);
436       TRACE("pAttr[%d] = GLX_RED_SIZE: %d\n", cur, pop);
437       break;
438     case WGL_GREEN_BITS_ARB:
439       pop = iWGLAttr[++cur];
440       PUSH2(oGLXAttr, GLX_GREEN_SIZE, pop);
441       TRACE("pAttr[%d] = GLX_GREEN_SIZE: %d\n", cur, pop);
442       break;
443     case WGL_ALPHA_BITS_ARB:
444       pop = iWGLAttr[++cur];
445       sz_alpha = pop;
446       PUSH2(oGLXAttr, GLX_ALPHA_SIZE, pop);
447       TRACE("pAttr[%d] = GLX_ALPHA_SIZE: %d\n", cur, pop);
448       break;
449     case WGL_DEPTH_BITS_ARB:
450       pop = iWGLAttr[++cur];
451       PUSH2(oGLXAttr, GLX_DEPTH_SIZE, pop);
452       TRACE("pAttr[%d] = GLX_DEPTH_SIZE: %d\n", cur, pop);
453       break;
454     case WGL_STENCIL_BITS_ARB:
455       pop = iWGLAttr[++cur];
456       PUSH2(oGLXAttr, GLX_STENCIL_SIZE, pop);
457       TRACE("pAttr[%d] = GLX_STENCIL_SIZE: %d\n", cur, pop);
458       break;
459     case WGL_DOUBLE_BUFFER_ARB:
460       pop = iWGLAttr[++cur];
461       PUSH2(oGLXAttr, GLX_DOUBLEBUFFER, pop);
462       TRACE("pAttr[%d] = GLX_DOUBLEBUFFER: %d\n", cur, pop);
463       break;
464
465     case WGL_PIXEL_TYPE_ARB:
466       pop = iWGLAttr[++cur];
467       switch (pop) {
468       case WGL_TYPE_COLORINDEX_ARB: pop = GLX_COLOR_INDEX_BIT; isColor = 1; break ;
469       case WGL_TYPE_RGBA_ARB: pop = GLX_RGBA_BIT; break ;
470       case WGL_TYPE_RGBA_FLOAT_ATI: pop = GLX_RGBA_FLOAT_ATI_BIT; break ;
471       default:
472         ERR("unexpected PixelType(%x)\n", pop); 
473         pop = 0;
474       }
475       PUSH2(oGLXAttr, GLX_RENDER_TYPE, pop);
476       TRACE("pAttr[%d] = GLX_RENDER_TYPE: %d\n", cur, pop);
477       break;
478
479     case WGL_SUPPORT_GDI_ARB:
480       pop = iWGLAttr[++cur];
481       PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
482       TRACE("pAttr[%d] = GLX_RENDERABLE: %d\n", cur, pop);
483       break;
484
485     case WGL_DRAW_TO_BITMAP_ARB:
486       pop = iWGLAttr[++cur];
487       PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
488       TRACE("pAttr[%d] = GLX_RENDERABLE: %d\n", cur, pop);
489       if (pop) {
490         PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT);
491         TRACE("pAttr[%d] = GLX_DRAWABLE_TYPE: GLX_PIXMAP_BIT\n", cur);
492       }
493       break;
494
495     case WGL_DRAW_TO_WINDOW_ARB:
496       pop = iWGLAttr[++cur];
497       if (pop) {
498         PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
499         TRACE("pAttr[%d] = GLX_DRAWABLE_TYPE: GLX_WINDOW_BIT\n", cur);
500       }
501       break;
502
503     case WGL_DRAW_TO_PBUFFER_ARB:
504       pop = iWGLAttr[++cur];
505       PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
506       TRACE("pAttr[%d] = GLX_RENDERABLE: %d\n", cur, pop);
507       if (pop) {
508         PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT);
509         TRACE("pAttr[%d] = GLX_DRAWABLE_TYPE: GLX_PBUFFER_BIT\n", cur);
510       }
511       break;
512
513     case WGL_ACCELERATION_ARB:
514     case WGL_SUPPORT_OPENGL_ARB:
515       pop = iWGLAttr[++cur];
516       /** nothing to do, if we are here, supposing support Accelerated OpenGL */
517       TRACE("pAttr[%d] = WGL_SUPPORT_OPENGL_ARB: %d\n", cur, pop);
518       break;
519
520     case WGL_PBUFFER_LARGEST_ARB:
521       pop = iWGLAttr[++cur];
522       PUSH2(oGLXAttr, GLX_LARGEST_PBUFFER, pop);
523       TRACE("pAttr[%d] = GLX_LARGEST_PBUFFER: %x\n", cur, pop);
524       break;
525
526     case WGL_SAMPLE_BUFFERS_ARB:
527       pop = iWGLAttr[++cur];
528       PUSH2(oGLXAttr, GLX_SAMPLE_BUFFERS_ARB, pop);
529       TRACE("pAttr[%d] = GLX_SAMPLE_BUFFERS_ARB: %x\n", cur, pop);
530       break;
531       
532     case WGL_SAMPLES_ARB:
533       pop = iWGLAttr[++cur];
534       PUSH2(oGLXAttr, GLX_SAMPLES_ARB, pop);
535       TRACE("pAttr[%d] = GLX_SAMPLES_ARB: %x\n", cur, pop);
536       break;
537
538     case WGL_TEXTURE_FORMAT_ARB:
539     case WGL_TEXTURE_TARGET_ARB:
540     case WGL_MIPMAP_TEXTURE_ARB:
541       TRACE("WGL_render_texture Attributes: %x as %x\n", iWGLAttr[cur], iWGLAttr[cur + 1]);
542       pop = iWGLAttr[++cur];
543       if (NULL == pbuf) {
544         ERR("trying to use GLX_Pbuffer Attributes without Pbuffer (was %x)\n", iWGLAttr[cur]);
545       }
546       if (use_render_texture_ati) {
547         /** nothing to do here */
548       }
549       else if (!use_render_texture_emulation) {
550         if (WGL_NO_TEXTURE_ARB != pop) {
551           ERR("trying to use WGL_render_texture Attributes without support (was %x)\n", iWGLAttr[cur]);
552           return -1; /** error: don't support it */
553         } else {
554           PUSH2(oGLXAttr, GLX_X_RENDERABLE, pop);
555           PUSH2(oGLXAttr, GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT);
556         }
557       }
558       break ;
559
560     case WGL_BIND_TO_TEXTURE_RGB_ARB:
561     case WGL_BIND_TO_TEXTURE_RGBA_ARB:
562       pop = iWGLAttr[++cur];
563       /** cannot be converted, see direct handling on 
564        *   - wglGetPixelFormatAttribivARB
565        *  TODO: wglChoosePixelFormat
566        */
567       break ;
568
569     default:
570       FIXME("unsupported %x WGL Attribute\n", iWGLAttr[cur]);
571       break;
572     }
573     ++cur;
574   }
575
576   /**
577    * Trick as WGL_COLOR_BITS_ARB != GLX_BUFFER_SIZE
578    *    WGL_COLOR_BITS_ARB + WGL_ALPHA_BITS_ARB == GLX_BUFFER_SIZE
579    *
580    *  WGL_COLOR_BITS_ARB
581    *     The number of color bitplanes in each color buffer. For RGBA
582    *     pixel types, it is the size of the color buffer, excluding the
583    *     alpha bitplanes. For color-index pixels, it is the size of the
584    *     color index buffer.
585    *
586    *  GLX_BUFFER_SIZE   
587    *     This attribute defines the number of bits per color buffer. 
588    *     For GLX FBConfigs that correspond to a PseudoColor or StaticColor visual, 
589    *     this is equal to the depth value reported in the X11 visual. 
590    *     For GLX FBConfigs that correspond to TrueColor or DirectColor visual, 
591    *     this is the sum of GLX_RED_SIZE, GLX_GREEN_SIZE, GLX_BLUE_SIZE, and GLX_ALPHA_SIZE.
592    * 
593    */
594   if (0 < wantColorBits) {
595     if (!isColor) { 
596       wantColorBits += sz_alpha; 
597     }
598     if (32 < wantColorBits) {
599       ERR("buggy %d GLX_BUFFER_SIZE default to 32\n", wantColorBits);
600       wantColorBits = 32;
601     }
602     PUSH2(oGLXAttr, GLX_BUFFER_SIZE, wantColorBits);
603     TRACE("pAttr[%d] = WGL_COLOR_BITS_ARB: %d\n", cur, wantColorBits);
604   }
605
606   return nAttribs;
607 }
608
609 GLboolean WINAPI wglGetPixelFormatAttribivARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues)
610 {
611   Display* display = get_display( hdc );
612   UINT i;
613   GLXFBConfig* cfgs = NULL;
614   GLXFBConfig  curCfg = NULL;
615   int nCfgs = 0;
616   int hTest;
617   int tmp;
618   int curGLXAttr = 0;
619
620   TRACE("(%p, %d, %d, %d, %p, %p)\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, piValues);
621   
622   if (0 < iLayerPlane) {
623     FIXME("unsupported iLayerPlane(%d) > 0, returns FALSE\n", iLayerPlane);
624     return GL_FALSE;
625   }
626
627   cfgs = glXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
628   if (NULL == cfgs) {
629     ERR("no FB Configs found for display(%p)\n", display);
630     return GL_FALSE;
631   }
632
633   for (i = 0; i < nAttributes; ++i) {
634     const int curWGLAttr = piAttributes[i];
635     TRACE("pAttr[%d] = %x\n", i, curWGLAttr);
636     
637     switch (curWGLAttr) {
638     case WGL_NUMBER_PIXEL_FORMATS_ARB:
639       piValues[i] = nCfgs; 
640       continue ;
641
642     case WGL_SUPPORT_OPENGL_ARB:
643       piValues[i] = GL_TRUE; 
644       continue ;
645
646     case WGL_ACCELERATION_ARB:
647       curGLXAttr = GLX_CONFIG_CAVEAT;
648       if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
649       curCfg = cfgs[iPixelFormat - 1];
650       hTest = glXGetFBConfigAttrib(display, curCfg, curGLXAttr, &tmp);
651       if (hTest) goto get_error;
652       switch (tmp) {
653       case GLX_NONE: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
654       case GLX_SLOW_CONFIG: piValues[i] = WGL_NO_ACCELERATION_ARB; break;
655       case GLX_NON_CONFORMANT_CONFIG: piValues[i] = WGL_FULL_ACCELERATION_ARB; break;
656       default:
657         ERR("unexpected Config Caveat(%x)\n", tmp);
658         piValues[i] = WGL_NO_ACCELERATION_ARB;
659       }
660       continue ;
661
662     case WGL_TRANSPARENT_ARB:
663       curGLXAttr = GLX_TRANSPARENT_TYPE;
664       if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
665       curCfg = cfgs[iPixelFormat - 1];
666       hTest = glXGetFBConfigAttrib(display, curCfg, curGLXAttr, &tmp);
667       if (hTest) goto get_error;
668       piValues[i] = GL_FALSE;
669       if (GLX_NONE != tmp) piValues[i] = GL_TRUE;
670       continue ;
671
672     case WGL_PIXEL_TYPE_ARB:
673       curGLXAttr = GLX_RENDER_TYPE;
674       if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
675       curCfg = cfgs[iPixelFormat - 1];
676       hTest = glXGetFBConfigAttrib(display, curCfg, curGLXAttr, &tmp);
677       if (hTest) goto get_error;
678       TRACE("WGL_PIXEL_TYPE_ARB: GLX_RENDER_TYPE = 0x%x\n", tmp);
679       if      (tmp & GLX_RGBA_BIT)           { piValues[i] = WGL_TYPE_RGBA_ARB; }
680       else if (tmp & GLX_COLOR_INDEX_BIT)    { piValues[i] = WGL_TYPE_COLORINDEX_ARB; }
681       else if (tmp & GLX_RGBA_FLOAT_BIT)     { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
682       else if (tmp & GLX_RGBA_FLOAT_ATI_BIT) { piValues[i] = WGL_TYPE_RGBA_FLOAT_ATI; }
683       else {
684         ERR("unexpected RenderType(%x)\n", tmp);
685         piValues[i] = WGL_TYPE_RGBA_ARB;
686       }
687       continue ;
688
689     case WGL_COLOR_BITS_ARB:
690       /** see ConvertAttribWGLtoGLX for explain */
691       if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
692       curCfg = cfgs[iPixelFormat - 1];
693       hTest = glXGetFBConfigAttrib(display, curCfg, GLX_BUFFER_SIZE, piValues + i);
694       if (hTest) goto get_error;
695       TRACE("WGL_COLOR_BITS_ARB: GLX_BUFFER_SIZE = %d\n", piValues[i]);
696       hTest = glXGetFBConfigAttrib(display, curCfg, GLX_ALPHA_SIZE, &tmp);
697       if (hTest) goto get_error;
698       TRACE("WGL_COLOR_BITS_ARB: GLX_ALPHA_SIZE = %d\n", tmp);
699       piValues[i] = piValues[i] - tmp;
700       continue ;
701
702     case WGL_BIND_TO_TEXTURE_RGB_ARB: 
703       if (use_render_texture_ati) {
704         curGLXAttr = GLX_BIND_TO_TEXTURE_RGB_ATI;
705         break;
706       }
707     case WGL_BIND_TO_TEXTURE_RGBA_ARB:
708       if (use_render_texture_ati) {
709         curGLXAttr = GLX_BIND_TO_TEXTURE_RGBA_ATI;
710         break;
711       }
712       if (!use_render_texture_emulation) {
713         piValues[i] = GL_FALSE;
714         continue ;      
715       }
716       curGLXAttr = GLX_RENDER_TYPE;
717       if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
718       curCfg = cfgs[iPixelFormat - 1];
719       hTest = glXGetFBConfigAttrib(display, curCfg, curGLXAttr, &tmp);
720       if (hTest) goto get_error;
721       if (GLX_COLOR_INDEX_BIT == tmp) {
722         piValues[i] = GL_FALSE;  
723         continue ;
724       }
725       hTest = glXGetFBConfigAttrib(display, curCfg, GLX_DRAWABLE_TYPE, &tmp);
726       if (hTest) goto get_error;
727       piValues[i] = (tmp & GLX_PBUFFER_BIT) ? GL_TRUE : GL_FALSE;
728       continue ;
729
730     case WGL_BLUE_BITS_ARB:
731       curGLXAttr = GLX_BLUE_SIZE;
732       break;
733     case WGL_RED_BITS_ARB:
734       curGLXAttr = GLX_RED_SIZE;
735       break;
736     case WGL_GREEN_BITS_ARB:
737       curGLXAttr = GLX_GREEN_SIZE;
738       break;
739     case WGL_ALPHA_BITS_ARB:
740       curGLXAttr = GLX_ALPHA_SIZE;
741       break;
742     case WGL_DEPTH_BITS_ARB:
743       curGLXAttr = GLX_DEPTH_SIZE;
744       break;
745     case WGL_STENCIL_BITS_ARB:
746       curGLXAttr = GLX_STENCIL_SIZE;
747       break;
748     case WGL_DOUBLE_BUFFER_ARB:
749       curGLXAttr = GLX_DOUBLEBUFFER;
750       break;
751     case WGL_STEREO_ARB:
752       curGLXAttr = GLX_STEREO;
753       break;
754     case WGL_AUX_BUFFERS_ARB:
755       curGLXAttr = GLX_AUX_BUFFERS;
756       break;
757
758     case WGL_SUPPORT_GDI_ARB:
759     case WGL_DRAW_TO_WINDOW_ARB:
760     case WGL_DRAW_TO_BITMAP_ARB:
761     case WGL_DRAW_TO_PBUFFER_ARB:
762       curGLXAttr = GLX_X_RENDERABLE;
763       break;
764
765     case WGL_PBUFFER_LARGEST_ARB:
766       curGLXAttr = GLX_LARGEST_PBUFFER;
767       break;
768
769     case WGL_SAMPLE_BUFFERS_ARB:
770       curGLXAttr = GLX_SAMPLE_BUFFERS_ARB;
771       break;
772       
773     case WGL_SAMPLES_ARB:
774       curGLXAttr = GLX_SAMPLES_ARB;
775       break;
776
777     default:
778       FIXME("unsupported %x WGL Attribute\n", curWGLAttr);
779     }
780     
781     if (0 != curGLXAttr) {
782       if (nCfgs < iPixelFormat || 0 >= iPixelFormat) goto pix_error;
783       curCfg = cfgs[iPixelFormat - 1];
784       hTest = glXGetFBConfigAttrib(display, curCfg, curGLXAttr, piValues + i);
785       if (hTest) goto get_error;
786     } else { 
787       piValues[i] = GL_FALSE; 
788     }
789   }
790   
791   return GL_TRUE;
792
793 get_error:
794   ERR("(%p): unexpected failure on GetFBConfigAttrib(%x) returns FALSE\n", hdc, curGLXAttr);
795   XFree(cfgs);
796   return GL_FALSE;
797
798 pix_error:
799   ERR("(%p): unexpected iPixelFormat(%d) vs nFormats(%d), returns FALSE\n", hdc, iPixelFormat, nCfgs);
800   XFree(cfgs);
801   return GL_FALSE;
802 }
803
804 GLboolean WINAPI wglGetPixelFormatAttribfvARB(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues)
805 {
806     FIXME("(%p, %d, %d, %d, %p, %p): stub\n", hdc, iPixelFormat, iLayerPlane, nAttributes, piAttributes, pfValues);
807     return GL_FALSE;
808 }
809 /**
810  * http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.aix.doc/libs/openglrf/glXChooseFBConfig.htm
811  */
812 GLboolean WINAPI wglChoosePixelFormatARB(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats)
813 {
814   Display* display = get_display( hdc );
815   int gl_test = 0;
816   int attribs[256];
817   int nAttribs = 0;
818
819   GLXFBConfig* cfgs = NULL;
820   int nCfgs = 0;
821   UINT it;
822   int fmt_id;
823
824   GLXFBConfig* cfgs_fmt = NULL;
825   int nCfgs_fmt = 0;
826   UINT it_fmt;
827   int tmp_fmt_id;
828
829   int pfmt_it = 0;
830
831   TRACE("(%p, %p, %p, %d, %p, %p): hackish\n", hdc, piAttribIList, pfAttribFList, nMaxFormats, piFormats, nNumFormats);
832   if (NULL != pfAttribFList) {
833     FIXME("unused pfAttribFList\n");
834   }
835
836   nAttribs = ConvertAttribWGLtoGLX(piAttribIList, attribs, NULL);
837   if (-1 == nAttribs) {
838     WARN("Cannot convert WGL to GLX attributes\n");
839     return GL_FALSE;
840   }
841   PUSH1(attribs, None);
842
843   cfgs = glXChooseFBConfig(display, DefaultScreen(display), attribs, &nCfgs);
844   if (NULL == cfgs) {
845     WARN("Compatible Pixel Format not found\n");
846     return GL_FALSE;
847   }
848
849   cfgs_fmt = glXGetFBConfigs(display, DefaultScreen(display), &nCfgs_fmt);
850   if (NULL == cfgs_fmt) {
851     ERR("Failed to get All FB Configs\n");
852     XFree(cfgs);
853     return GL_FALSE;
854   }
855
856   for (it = 0; it < nMaxFormats && it < nCfgs; ++it) {
857     gl_test = glXGetFBConfigAttrib(display, cfgs[it], GLX_FBCONFIG_ID, &fmt_id);
858     if (gl_test) {
859       ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
860       continue ;
861     }
862     for (it_fmt = 0; it_fmt < nCfgs_fmt; ++it_fmt) {
863       gl_test = glXGetFBConfigAttrib(display, cfgs_fmt[it_fmt], GLX_FBCONFIG_ID, &tmp_fmt_id);
864       if (gl_test) {
865         ERR("Failed to retrieve FBCONFIG_ID from GLXFBConfig, expect problems.\n");
866         continue ;
867       }
868       if (fmt_id == tmp_fmt_id) {
869         int tmp;
870         piFormats[pfmt_it] = it_fmt + 1;
871         ++pfmt_it;
872         glXGetFBConfigAttrib(display, cfgs_fmt[it_fmt], GLX_ALPHA_SIZE, &tmp);
873         TRACE("ALPHA_SIZE of FBCONFIG_ID(%d/%d) found as '%d'\n", it_fmt + 1, nCfgs_fmt, tmp);
874         break ;
875       }
876     }
877     if (it_fmt == nCfgs_fmt) {
878       ERR("Failed to get valid fmt for %d. Try next.\n", it);
879       continue ;
880     }
881     TRACE("at %d/%d found FBCONFIG_ID(%d/%d)\n", it + 1, nCfgs, piFormats[it], nCfgs_fmt);
882   }
883   
884   *nNumFormats = pfmt_it;
885   /** free list */
886   XFree(cfgs);
887   XFree(cfgs_fmt);
888   return GL_TRUE;
889 }
890
891 #define HPBUFFERARB void *
892 HPBUFFERARB WINAPI wglCreatePbufferARB(HDC hdc, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList)
893 {
894   Wine_GLPBuffer* object = NULL;
895   Display* display = get_display( hdc );
896   GLXFBConfig* cfgs = NULL;
897   int nCfgs = 0;
898   int attribs[256];
899   unsigned nAttribs = 0;
900
901   TRACE("(%p, %d, %d, %d, %p)\n", hdc, iPixelFormat, iWidth, iHeight, piAttribList);
902
903   if (0 >= iPixelFormat) {
904     ERR("(%p): unexpected iPixelFormat(%d) <= 0, returns NULL\n", hdc, iPixelFormat);
905     SetLastError(ERROR_INVALID_PIXEL_FORMAT);
906     return NULL; /* unespected error */
907   }
908
909   cfgs = glXGetFBConfigs(display, DefaultScreen(display), &nCfgs);
910
911   if (NULL == cfgs || 0 == nCfgs) {
912     ERR("(%p): Cannot get FB Configs for iPixelFormat(%d), returns NULL\n", hdc, iPixelFormat);
913     SetLastError(ERROR_INVALID_PIXEL_FORMAT);
914     return NULL; /* unespected error */
915   }
916   if (nCfgs < iPixelFormat) {
917     ERR("(%p): unexpected iPixelFormat(%d) > nFormats(%d), returns NULL\n", hdc, iPixelFormat, nCfgs);
918     SetLastError(ERROR_INVALID_PIXEL_FORMAT);
919     goto create_failed; /* unespected error */
920   }
921
922   object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Wine_GLPBuffer));
923   if (NULL == object) {
924     SetLastError(ERROR_NO_SYSTEM_RESOURCES);
925     goto create_failed; /* unespected error */
926   }
927   object->hdc = hdc;
928   object->display = display;
929   object->width = iWidth;
930   object->height = iHeight;
931   object->pixelFormat = iPixelFormat;
932
933   nAttribs = ConvertAttribWGLtoGLX(piAttribList, attribs, object);
934   if (-1 == nAttribs) {
935     WARN("Cannot convert WGL to GLX attributes\n");
936     goto create_failed;
937   }
938   PUSH2(attribs, GLX_PBUFFER_WIDTH,  iWidth);
939   PUSH2(attribs, GLX_PBUFFER_HEIGHT, iHeight); 
940
941   while (0 != *piAttribList) {
942     int attr_v;
943     switch (*piAttribList) {
944     case WGL_TEXTURE_FORMAT_ARB: {
945       ++piAttribList;
946       attr_v = *piAttribList;
947       TRACE("WGL_render_texture Attribute: WGL_TEXTURE_FORMAT_ARB as %x\n", attr_v);
948       if (use_render_texture_ati) {
949         int type = 0;
950         switch (attr_v) {
951         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
952         case WGL_TEXTURE_RGB_ARB: type = GLX_TEXTURE_RGB_ATI; break ;
953         case WGL_TEXTURE_RGBA_ARB: type = GLX_TEXTURE_RGBA_ATI; break ;
954         default:
955           SetLastError(ERROR_INVALID_DATA);       
956           goto create_failed;
957         }
958         object->use_render_texture = 1;
959         PUSH2(attribs, GLX_TEXTURE_FORMAT_ATI, type);
960       } else {
961         if (WGL_NO_TEXTURE_ARB == attr_v) {
962           object->use_render_texture = 0;
963         } else {
964           if (!use_render_texture_emulation) {
965             SetLastError(ERROR_INVALID_DATA);     
966             goto create_failed;
967           }
968           switch (attr_v) {
969           case WGL_TEXTURE_RGB_ARB:
970             object->use_render_texture = GL_RGB;
971             break;
972           case WGL_TEXTURE_RGBA_ARB:
973             object->use_render_texture = GL_RGBA;
974             break;
975           default:
976             SetLastError(ERROR_INVALID_DATA);     
977             goto create_failed;
978           }
979         }
980       }
981       break;
982     }
983     
984     case WGL_TEXTURE_TARGET_ARB: {
985       ++piAttribList;
986       attr_v = *piAttribList;
987       TRACE("WGL_render_texture Attribute: WGL_TEXTURE_TARGET_ARB as %x\n", attr_v);
988       if (use_render_texture_ati) {
989         int type = 0;
990         switch (attr_v) {
991         case WGL_NO_TEXTURE_ARB: type = GLX_NO_TEXTURE_ATI; break ;
992         case WGL_TEXTURE_CUBE_MAP_ARB: type = GLX_TEXTURE_CUBE_MAP_ATI; break ;
993         case WGL_TEXTURE_1D_ARB: type = GLX_TEXTURE_1D_ATI; break ;
994         case WGL_TEXTURE_2D_ARB: type = GLX_TEXTURE_2D_ATI; break ;
995         default:
996           SetLastError(ERROR_INVALID_DATA);       
997           goto create_failed;
998         }
999         PUSH2(attribs, GLX_TEXTURE_TARGET_ATI, type);
1000       } else {
1001         if (WGL_NO_TEXTURE_ARB == attr_v) {
1002           object->texture_target = 0;
1003         } else {
1004           if (!use_render_texture_emulation) {
1005             SetLastError(ERROR_INVALID_DATA);     
1006             goto create_failed;
1007           }
1008           switch (attr_v) {
1009           case WGL_TEXTURE_CUBE_MAP_ARB: {
1010             if (iWidth != iHeight) {
1011               SetLastError(ERROR_INVALID_DATA);   
1012               goto create_failed;
1013             }
1014             object->texture_target = GL_TEXTURE_CUBE_MAP;
1015             object->texture_bind_target = GL_TEXTURE_CUBE_MAP;
1016             break;
1017           }
1018           case WGL_TEXTURE_1D_ARB: {
1019             if (1 != iHeight) {
1020               SetLastError(ERROR_INVALID_DATA);   
1021               goto create_failed;
1022             }
1023             object->texture_target = GL_TEXTURE_1D;
1024             object->texture_bind_target = GL_TEXTURE_1D;
1025             break;
1026           }
1027           case WGL_TEXTURE_2D_ARB: {
1028             object->texture_target = GL_TEXTURE_2D;
1029             object->texture_bind_target = GL_TEXTURE_2D;
1030             break;
1031           }
1032           default:
1033             SetLastError(ERROR_INVALID_DATA);     
1034             goto create_failed;
1035           }
1036         }
1037       }
1038       break;
1039     }
1040
1041     case WGL_MIPMAP_TEXTURE_ARB: {
1042       ++piAttribList;
1043       attr_v = *piAttribList;
1044       TRACE("WGL_render_texture Attribute: WGL_MIPMAP_TEXTURE_ARB as %x\n", attr_v);
1045       if (use_render_texture_ati) {
1046         PUSH2(attribs, GLX_MIPMAP_TEXTURE_ATI, attr_v);
1047       } else {
1048         if (!use_render_texture_emulation) {
1049           SetLastError(ERROR_INVALID_DATA);       
1050           goto create_failed;
1051         }
1052         /*
1053         if (0 != attr_v) {
1054           SetLastError(ERROR_INVALID_DATA);       
1055           goto create_failed;   
1056         }
1057         */
1058       }
1059       break ;
1060     } 
1061     }
1062     ++piAttribList;
1063   }
1064
1065   PUSH1(attribs, None);
1066
1067   object->drawable = glXCreatePbuffer(display, cfgs[iPixelFormat - 1], attribs);
1068   TRACE("new Pbuffer drawable as %p\n", (void*) object->drawable);
1069   if (!object->drawable) {
1070     SetLastError(ERROR_NO_SYSTEM_RESOURCES);
1071     goto create_failed; /* unespected error */
1072   }
1073   TRACE("->(%p)\n", object);
1074
1075   /** free list */
1076   XFree(cfgs);
1077   return (HPBUFFERARB) object;
1078
1079 create_failed:
1080   if (NULL != cfgs) XFree(cfgs);
1081   if (NULL != object) HeapFree(GetProcessHeap(), 0, object);
1082   TRACE("->(FAILED)\n");
1083   return (HPBUFFERARB) NULL;
1084 }
1085
1086 HDC WINAPI wglGetPbufferDCARB(HPBUFFERARB hPbuffer)
1087 {
1088   Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1089   HDC hDC;
1090   if (NULL == object) {
1091     SetLastError(ERROR_INVALID_HANDLE);
1092     return NULL;
1093   }
1094   hDC = CreateCompatibleDC(object->hdc);
1095   SetPixelFormat(hDC, object->pixelFormat, NULL);
1096   set_drawable(hDC, object->drawable); /* works ?? */
1097   TRACE("(%p)->(%p)\n", hPbuffer, hDC);
1098   return hDC;
1099 }
1100
1101 int WINAPI wglReleasePbufferDCARB(HPBUFFERARB hPbuffer, HDC hdc)
1102 {
1103   TRACE("(%p, %p)\n", hPbuffer, hdc);
1104   DeleteDC(hdc);
1105   return 0;
1106 }
1107
1108 GLboolean WINAPI wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
1109 {
1110   Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1111   TRACE("(%p)\n", hPbuffer);
1112   if (NULL == object) {
1113     SetLastError(ERROR_INVALID_HANDLE);
1114     return GL_FALSE;
1115   }
1116   glXDestroyPbuffer(object->display, object->drawable);
1117   HeapFree(GetProcessHeap(), 0, object);
1118   return GL_TRUE;
1119 }
1120
1121 GLboolean WINAPI wglQueryPbufferARB(HPBUFFERARB hPbuffer, int iAttribute, int *piValue)
1122 {
1123   Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1124   TRACE("(%p, 0x%x, %p)\n", hPbuffer, iAttribute, piValue);
1125   if (NULL == object) {
1126     SetLastError(ERROR_INVALID_HANDLE);
1127     return GL_FALSE;
1128   }
1129   switch (iAttribute) {
1130   case WGL_PBUFFER_WIDTH_ARB:
1131     glXQueryDrawable(object->display, object->drawable, GLX_WIDTH, (unsigned int*) piValue);
1132     break;
1133   case WGL_PBUFFER_HEIGHT_ARB:
1134     glXQueryDrawable(object->display, object->drawable, GLX_HEIGHT, (unsigned int*) piValue);
1135     break;
1136
1137   case WGL_PBUFFER_LOST_ARB:
1138     FIXME("unsupported WGL_PBUFFER_LOST_ARB (need glXSelectEvent/GLX_DAMAGED work)\n");
1139     break;
1140
1141   case WGL_TEXTURE_FORMAT_ARB:
1142     if (use_render_texture_ati) {
1143       unsigned int tmp;
1144       int type = WGL_NO_TEXTURE_ARB;
1145       glXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_FORMAT_ATI, &tmp);
1146       switch (tmp) {
1147       case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
1148       case GLX_TEXTURE_RGB_ATI: type = WGL_TEXTURE_RGB_ARB; break ;
1149       case GLX_TEXTURE_RGBA_ATI: type = WGL_TEXTURE_RGBA_ARB; break ;
1150       }
1151       *piValue = type;
1152     } else {
1153       if (!object->use_render_texture) {
1154         *piValue = WGL_NO_TEXTURE_ARB;
1155       } else {
1156         if (!use_render_texture_emulation) {
1157           SetLastError(ERROR_INVALID_HANDLE);
1158           return GL_FALSE;
1159         }
1160         if (GL_RGBA == object->use_render_texture) {
1161           *piValue = WGL_TEXTURE_RGBA_ARB;
1162         } else {
1163           *piValue = WGL_TEXTURE_RGB_ARB;
1164         }
1165       }
1166     }
1167     break; 
1168
1169   case WGL_TEXTURE_TARGET_ARB:
1170     if (use_render_texture_ati) {
1171       unsigned int tmp;
1172       int type = WGL_NO_TEXTURE_ARB;
1173       glXQueryDrawable(object->display, object->drawable, GLX_TEXTURE_TARGET_ATI, &tmp);
1174       switch (tmp) {
1175       case GLX_NO_TEXTURE_ATI: type = WGL_NO_TEXTURE_ARB; break ;
1176       case GLX_TEXTURE_CUBE_MAP_ATI: type = WGL_TEXTURE_CUBE_MAP_ARB; break ;
1177       case GLX_TEXTURE_1D_ATI: type = WGL_TEXTURE_1D_ARB; break ;
1178       case GLX_TEXTURE_2D_ATI: type = WGL_TEXTURE_2D_ARB; break ;
1179       }
1180       *piValue = type;
1181     } else {
1182       if (!object->texture_target) {
1183         *piValue = WGL_NO_TEXTURE_ARB;
1184       } else {
1185         if (!use_render_texture_emulation) {
1186           SetLastError(ERROR_INVALID_DATA);      
1187           return GL_FALSE;
1188         }
1189         switch (object->texture_target) {
1190         case GL_TEXTURE_1D:       *piValue = WGL_TEXTURE_CUBE_MAP_ARB; break;
1191         case GL_TEXTURE_2D:       *piValue = WGL_TEXTURE_1D_ARB; break;
1192         case GL_TEXTURE_CUBE_MAP: *piValue = WGL_TEXTURE_2D_ARB; break;
1193         }
1194       }
1195     }
1196     break;
1197
1198   case WGL_MIPMAP_TEXTURE_ARB:
1199     if (use_render_texture_ati) {
1200       glXQueryDrawable(object->display, object->drawable, GLX_MIPMAP_TEXTURE_ATI, (unsigned int*) piValue);
1201     } else {
1202       *piValue = GL_FALSE; /** don't support that */
1203       FIXME("unsupported WGL_ARB_render_texture attribute query for 0x%x\n", iAttribute);
1204     }
1205     break;
1206
1207   default:
1208     FIXME("unexpected attribute %x\n", iAttribute);
1209     break;
1210   }
1211
1212   return GL_TRUE;
1213 }
1214
1215 GLboolean WINAPI wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
1216 {
1217   Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1218   TRACE("(%p, %d)\n", hPbuffer, iBuffer);
1219   if (NULL == object) {
1220     SetLastError(ERROR_INVALID_HANDLE);
1221     return GL_FALSE;
1222   }
1223   if (!object->use_render_texture) {
1224     SetLastError(ERROR_INVALID_HANDLE);
1225     return GL_FALSE;
1226   }
1227   if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
1228     int do_init = 0;
1229     GLint prev_binded_tex;
1230     glGetIntegerv(object->texture_target, &prev_binded_tex);
1231     if (NULL == object->render_ctx) {
1232       object->render_hdc = wglGetPbufferDCARB(hPbuffer);
1233       object->render_ctx = wglCreateContext(object->render_hdc);
1234       do_init = 1;
1235     }
1236     object->prev_hdc = wglGetCurrentDC();
1237     object->prev_ctx = wglGetCurrentContext();
1238     wglMakeCurrent(object->render_hdc, object->render_ctx);
1239     /*
1240     if (do_init) {
1241       glBindTexture(object->texture_target, object->texture);   
1242       if (GL_RGBA == object->use_render_texture) {
1243         glTexImage2D(object->texture_target, 0, GL_RGBA8, object->width, object->height, 0, GL_RGBA, GL_FLOAT, NULL);
1244       } else {
1245         glTexImage2D(object->texture_target, 0, GL_RGB8, object->width, object->height, 0, GL_RGB, GL_FLOAT, NULL);
1246       }
1247     }
1248     */
1249     object->texture = prev_binded_tex;
1250     return GL_TRUE;
1251   }
1252   if (NULL != p_glXBindTexImageARB) {
1253     return p_glXBindTexImageARB(object->display, object->drawable, iBuffer);
1254   }
1255   return GL_FALSE;
1256 }
1257
1258 GLboolean WINAPI wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
1259 {
1260   Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1261   TRACE("(%p, %d)\n", hPbuffer, iBuffer);
1262   if (NULL == object) {
1263     SetLastError(ERROR_INVALID_HANDLE);
1264     return GL_FALSE;
1265   }
1266   if (!object->use_render_texture) {
1267     SetLastError(ERROR_INVALID_HANDLE);
1268     return GL_FALSE;
1269   }
1270   if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
1271     /*
1272     GLint prev_binded_tex;
1273     glGetIntegerv(object->texture_target, &prev_binded_tex);    
1274     if (GL_TEXTURE_1D == object->texture_target) {
1275       glCopyTexSubImage1D(object->texture_target, object->texture_level, 0, 0, 0, object->width);
1276     } else {
1277       glCopyTexSubImage2D(object->texture_target, object->texture_level, 0, 0, 0, 0, object->width, object->height);
1278     }
1279     glBindTexture(object->texture_target, prev_binded_tex);
1280     SwapBuffers(object->render_hdc);
1281     */
1282     glBindTexture(object->texture_target, object->texture);
1283     if (GL_TEXTURE_1D == object->texture_target) {
1284       glCopyTexSubImage1D(object->texture_target, object->texture_level, 0, 0, 0, object->width);
1285     } else {
1286       glCopyTexSubImage2D(object->texture_target, object->texture_level, 0, 0, 0, 0, object->width, object->height);
1287     }
1288
1289     wglMakeCurrent(object->prev_hdc, object->prev_ctx);
1290     return GL_TRUE;
1291   }
1292   if (NULL != p_glXReleaseTexImageARB) {
1293     return p_glXReleaseTexImageARB(object->display, object->drawable, iBuffer);
1294   }
1295   return GL_FALSE;
1296 }
1297
1298 GLboolean WINAPI wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
1299 {
1300   Wine_GLPBuffer* object = (Wine_GLPBuffer*) hPbuffer;
1301   WARN("(%p, %p): alpha-testing, report any problem\n", hPbuffer, piAttribList);
1302   if (NULL == object) {
1303     SetLastError(ERROR_INVALID_HANDLE);
1304     return GL_FALSE;
1305   }
1306   if (!object->use_render_texture) {
1307     SetLastError(ERROR_INVALID_HANDLE);
1308     return GL_FALSE;
1309   }
1310   if (!use_render_texture_ati && 1 == use_render_texture_emulation) {
1311     return GL_TRUE;
1312   }
1313   if (NULL != p_glXDrawableAttribARB) {
1314     if (use_render_texture_ati) {
1315       FIXME("Need conversion for GLX_ATI_render_texture\n");
1316     }
1317     return p_glXDrawableAttribARB(object->display, object->drawable, piAttribList); 
1318   }
1319   return GL_FALSE;
1320 }
1321
1322 static const struct {
1323     const char *name;
1324     BOOL (*query_function)(glXGetProcAddressARB_t proc, const char *gl_version, const char *gl_extensions,
1325                            const char *glx_version, const char *glx_extensions,
1326                            const char *server_glx_extensions, const char *client_glx_extensions);
1327 } extension_list[] = {
1328   { "WGL_ARB_make_current_read", query_function_make_current_read },
1329   { "WGL_ARB_multisample",       query_function_multisample },
1330   { "WGL_ARB_pbuffer",           query_function_pbuffer },
1331   { "WGL_ARB_pixel_format" ,     query_function_pixel_format },
1332   { "WGL_ARB_render_texture",    query_function_render_texture },
1333   { "WGL_EXT_swap_control",      query_function_swap_control }
1334 };
1335
1336 /* Used to initialize the WGL extension string at DLL loading */
1337 void wgl_ext_initialize_extensions(Display *display, int screen, glXGetProcAddressARB_t proc, const char* disabled_extensions)
1338 {
1339     int size = strlen(WGL_extensions_base);
1340     const char *glx_extensions = glXQueryExtensionsString(display, screen);
1341     const char *server_glx_extensions = glXQueryServerString(display, screen, GLX_EXTENSIONS);
1342     const char *client_glx_extensions = glXGetClientString(display, GLX_EXTENSIONS);
1343     const char *gl_extensions = (const char *) glGetString(GL_EXTENSIONS);
1344     const char *gl_version = (const char *) glGetString(GL_VERSION);
1345     const char *server_glx_version = glXQueryServerString(display, screen, GLX_VERSION);
1346     const char *glx_version = glXGetClientString(display, GLX_VERSION);
1347     int i;
1348
1349     TRACE("GL version         : %s.\n", debugstr_a(gl_version));
1350     TRACE("GL exts            : %s.\n", debugstr_a(gl_extensions));
1351     TRACE("GLX exts           : %s.\n", debugstr_a(glx_extensions));
1352     TRACE("Server GLX version : %s.\n", debugstr_a(server_glx_version));
1353     TRACE("Client GLX version : %s.\n", debugstr_a(glx_version));
1354     TRACE("Server GLX exts    : %s.\n", debugstr_a(server_glx_extensions));
1355     TRACE("Client GLX exts    : %s.\n", debugstr_a(client_glx_extensions));
1356
1357     for (i = 0; i < (sizeof(extension_list) / sizeof(extension_list[0])); i++) {
1358         if (strstr(disabled_extensions, extension_list[i].name)) continue ; /* disabled by config, next */
1359         if (extension_list[i].query_function(proc, 
1360                                              gl_version, gl_extensions, 
1361                                              glx_version, glx_extensions,
1362                                              server_glx_extensions, client_glx_extensions)) {
1363             size += strlen(extension_list[i].name) + 1;
1364         }
1365     }
1366
1367     /* For the moment, only 'base' extensions are supported. */
1368     WGL_extensions = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + 1);
1369     if (WGL_extensions == NULL) {
1370         WGL_extensions = (char *) WGL_extensions_base;
1371     } else {
1372         strcpy(WGL_extensions, WGL_extensions_base);
1373         for (i = 0; i < (sizeof(extension_list) / sizeof(extension_list[0])); i++) {
1374             if (strstr(disabled_extensions, extension_list[i].name)) continue ; /* disabled by config, next */
1375             if (extension_list[i].query_function(proc, 
1376                                                  gl_version, gl_extensions, 
1377                                                  glx_version, glx_extensions,
1378                                                  server_glx_extensions, client_glx_extensions)) {
1379                 strcat(WGL_extensions, " ");
1380                 strcat(WGL_extensions, extension_list[i].name);
1381             }
1382         }
1383     }
1384
1385     TRACE("Supporting following WGL extensions : %s.\n", debugstr_a(WGL_extensions));
1386 }
1387
1388 void wgl_ext_finalize_extensions(void)
1389 {
1390     if (WGL_extensions != WGL_extensions_base) {
1391         HeapFree(GetProcessHeap(), 0, WGL_extensions);
1392     }
1393 }
1394
1395
1396 /* these are located in wgl.c for convenience of implementation */
1397 BOOL    WINAPI wglMakeContextCurrentARB(HDC,HDC,HGLRC);
1398 HDC     WINAPI wglGetCurrentReadDCARB(void);
1399
1400
1401 /*
1402  * Putting this at the end to prevent having to write the prototypes :-) 
1403  *
1404  * @WARNING: this list must be ordered by name
1405  *
1406  * @TODO: real handle caps on providing some func_init functions (third param, ex: to check extensions)
1407  */
1408 WGL_extension wgl_extension_registry[] = {
1409     { "wglBindTexImageARB", (void *) wglBindTexImageARB, NULL, NULL},
1410     { "wglChoosePixelFormatARB", (void *) wglChoosePixelFormatARB, NULL, NULL},
1411     { "wglCreatePbufferARB", (void *) wglCreatePbufferARB, NULL, NULL},
1412     { "wglDestroyPbufferARB", (void *) wglDestroyPbufferARB, NULL, NULL},
1413     { "wglGetCurrentReadDCARB", (void *) wglGetCurrentReadDCARB, NULL, NULL},
1414     { "wglGetExtensionsStringARB", (void *) wglGetExtensionsStringARB, NULL, NULL},
1415     { "wglGetExtensionsStringEXT", (void *) wglGetExtensionsStringEXT, NULL, NULL},
1416     { "wglGetPbufferDCARB", (void *) wglGetPbufferDCARB, NULL, NULL},
1417     { "wglGetPixelFormatAttribfvARB", (void *) wglGetPixelFormatAttribfvARB, NULL, NULL},
1418     { "wglGetPixelFormatAttribivARB", (void *) wglGetPixelFormatAttribivARB, NULL, NULL},
1419     { "wglGetSwapIntervalEXT", (void *) wglGetSwapIntervalEXT, NULL, NULL},
1420     { "wglMakeContextCurrentARB", (void *) wglMakeContextCurrentARB, NULL, NULL },
1421     { "wglQueryPbufferARB", (void *) wglQueryPbufferARB, NULL, NULL},
1422     { "wglReleasePbufferDCARB", (void *) wglReleasePbufferDCARB, NULL, NULL},
1423     { "wglReleaseTexImageARB", (void *) wglReleaseTexImageARB, NULL, NULL},
1424     { "wglSetPbufferAttribARB", (void *) wglSetPbufferAttribARB, NULL, NULL},
1425     { "wglSwapIntervalEXT", (void *) wglSwapIntervalEXT, NULL, NULL}
1426 };
1427 int wgl_extension_registry_size = sizeof(wgl_extension_registry) / sizeof(wgl_extension_registry[0]);