Tidy up some of the formatting in drawprim and add missing strided
[wine] / dlls / wined3d / directx.c
1 /*
2  * IWineD3D implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /* Compile time diagnostics: */
25
26 /* Uncomment this to force only a single display mode to be exposed: */
27 /*#define DEBUG_SINGLE_MODE*/
28
29
30 #include "config.h"
31 #include "wined3d_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
35 #define GLINFO_LOCATION This->gl_info
36
37 /**********************************************************
38  * Utility functions follow
39  **********************************************************/
40
41 /* x11drv GDI escapes */
42 #define X11DRV_ESCAPE 6789
43 enum x11drv_escape_codes
44 {
45     X11DRV_GET_DISPLAY,   /* get X11 display for a DC */
46     X11DRV_GET_DRAWABLE,  /* get current drawable for a DC */
47     X11DRV_GET_FONT,      /* get current X font for a DC */
48 };
49
50 /* retrieve the X display to use on a given DC */
51 inline static Display *get_display( HDC hdc )
52 {
53     Display *display;
54     enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;
55
56     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
57                     sizeof(display), (LPSTR)&display )) display = NULL;
58     return display;
59 }
60
61 /**
62  * Note: GL seems to trap if GetDeviceCaps is called before any HWND's created
63  * ie there is no GL Context - Get a default rendering context to enable the
64  * function query some info from GL
65  */
66 static WineD3D_Context* WineD3D_CreateFakeGLContext(void) {
67     static WineD3D_Context ctx = { NULL, NULL, NULL, 0, 0 };
68     WineD3D_Context* ret = NULL;
69
70     if (glXGetCurrentContext() == NULL) {
71        BOOL         gotContext  = FALSE;
72        BOOL         created     = FALSE;
73        XVisualInfo  template;
74        HDC          device_context;
75        Visual*      visual;
76        BOOL         failed = FALSE;
77        int          num;
78        XWindowAttributes win_attr;
79
80        TRACE_(d3d_caps)("Creating Fake GL Context\n");
81
82        ctx.drawable = (Drawable) GetPropA(GetDesktopWindow(), "__wine_x11_whole_window");
83
84        /* Get the display */
85        device_context = GetDC(0);
86        ctx.display = get_display(device_context);
87        ReleaseDC(0, device_context);
88
89        /* Get the X visual */
90        ENTER_GL();
91        if (XGetWindowAttributes(ctx.display, ctx.drawable, &win_attr)) {
92            visual = win_attr.visual;
93        } else {
94            visual = DefaultVisual(ctx.display, DefaultScreen(ctx.display));
95        }
96        template.visualid = XVisualIDFromVisual(visual);
97        ctx.visInfo = XGetVisualInfo(ctx.display, VisualIDMask, &template, &num);
98        if (ctx.visInfo == NULL) {
99            LEAVE_GL();
100            WARN_(d3d_caps)("Error creating visual info for capabilities initialization\n");
101            failed = TRUE;
102        }
103
104        /* Create a GL context */
105        if (!failed) {
106            ctx.glCtx = glXCreateContext(ctx.display, ctx.visInfo, NULL, GL_TRUE);
107
108            if (ctx.glCtx == NULL) {
109                LEAVE_GL();
110                WARN_(d3d_caps)("Error creating default context for capabilities initialization\n");
111                failed = TRUE;
112            }
113        }
114
115        /* Make it the current GL context */
116        if (!failed && glXMakeCurrent(ctx.display, ctx.drawable, ctx.glCtx) == False) {
117            glXDestroyContext(ctx.display, ctx.glCtx);
118            LEAVE_GL();
119            WARN_(d3d_caps)("Error setting default context as current for capabilities initialization\n");
120            failed = TRUE;
121        }
122
123        /* It worked! Wow... */
124        if (!failed) {
125            gotContext = TRUE;
126            created = TRUE;
127            ret = &ctx;
128        } else {
129            ret = NULL;
130        }
131
132    } else {
133      if (ctx.ref > 0) ret = &ctx;
134    }
135
136    if (NULL != ret) InterlockedIncrement(&ret->ref);
137    return ret;
138 }
139
140 static void WineD3D_ReleaseFakeGLContext(WineD3D_Context* ctx) {
141     /* If we created a dummy context, throw it away */
142     if (NULL != ctx) {
143         if (0 == InterlockedDecrement(&ctx->ref)) {
144             glXMakeCurrent(ctx->display, None, NULL);
145             glXDestroyContext(ctx->display, ctx->glCtx);
146             ctx->display = NULL;
147             ctx->glCtx = NULL;
148             LEAVE_GL();
149         }
150     }
151 }
152
153 /**********************************************************
154  * IUnknown parts follows
155  **********************************************************/
156
157 HRESULT WINAPI IWineD3DImpl_QueryInterface(IWineD3D *iface,REFIID riid,LPVOID *ppobj)
158 {
159     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
160     /* FIXME: This needs to extend an IWineD3DBaseObject */
161
162     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
163     if (IsEqualGUID(riid, &IID_IUnknown)
164         || IsEqualGUID(riid, &IID_IWineD3DDevice)) {
165         IUnknown_AddRef(iface);
166         *ppobj = This;
167         return D3D_OK;
168     }
169
170     return E_NOINTERFACE;
171 }
172
173 ULONG WINAPI IWineD3DImpl_AddRef(IWineD3D *iface) {
174     IWineD3DImpl *This = (IWineD3DImpl *)iface;
175     ULONG refCount = InterlockedIncrement(&This->ref);
176
177     TRACE("(%p) : AddRef increasing from %ld\n", This, refCount - 1);
178     return refCount;
179 }
180
181 ULONG WINAPI IWineD3DImpl_Release(IWineD3D *iface) {
182     IWineD3DImpl *This = (IWineD3DImpl *)iface;
183     ULONG ref;
184     TRACE("(%p) : Releasing from %ld\n", This, This->ref);
185     ref = InterlockedDecrement(&This->ref);
186     if (ref == 0) {
187         HeapFree(GetProcessHeap(), 0, This);
188     }
189
190     return ref;
191 }
192
193 /**********************************************************
194  * IWineD3D parts follows
195  **********************************************************/
196
197 static BOOL IWineD3DImpl_FillGLCaps(WineD3D_GL_Info *gl_info, Display* display) {
198     const char *GL_Extensions    = NULL;
199     const char *GLX_Extensions   = NULL;
200     const char *gl_string        = NULL;
201     const char *gl_string_cursor = NULL;
202     GLint       gl_max;
203     GLfloat     gl_float;
204     Bool        test = 0;
205     int         major, minor;
206     WineD3D_Context *fake_ctx = NULL;
207     BOOL             gotContext  = FALSE;
208
209     /* Make sure that we've got a context */
210     if (glXGetCurrentContext() == NULL) {
211         /* TODO: CreateFakeGLContext should really take a display as a parameter  */
212         fake_ctx = WineD3D_CreateFakeGLContext();
213         if (NULL != fake_ctx) gotContext = TRUE;
214     } else {
215         gotContext = TRUE;
216     }
217
218
219     TRACE_(d3d_caps)("(%p, %p)\n", gl_info, display);
220
221     /* Fill in the GL info retrievable depending on the display */
222     if (NULL != display) {
223         test = glXQueryVersion(display, &major, &minor);
224         gl_info->glx_version = ((major & 0x0000FFFF) << 16) | (minor & 0x0000FFFF);
225         gl_string = glXGetClientString(display, GLX_VENDOR);
226     } else {
227         FIXME("Display must not be NULL, use glXGetCurrentDisplay or getAdapterDisplay()\n");
228         gl_string = glGetString(GL_VENDOR);
229     }
230     TRACE_(d3d_caps)("Filling vendor string %s\n", gl_string);
231     if (gl_string != NULL) {
232         /* Fill in the GL vendor */
233         if (strstr(gl_string, "NVIDIA")) {
234             gl_info->gl_vendor = VENDOR_NVIDIA;
235         } else if (strstr(gl_string, "ATI")) {
236             gl_info->gl_vendor = VENDOR_ATI;
237         } else {
238             gl_info->gl_vendor = VENDOR_WINE;
239         }
240     } else {
241         gl_info->gl_vendor = VENDOR_WINE;
242     }
243
244
245     TRACE_(d3d_caps)("found GL_VENDOR (%s)->(0x%04x)\n", debugstr_a(gl_string), gl_info->gl_vendor);
246
247     /* Parse the GL_VERSION field into major and minor information */
248     gl_string = glGetString(GL_VERSION);
249     if (gl_string != NULL) {
250
251         switch (gl_info->gl_vendor) {
252         case VENDOR_NVIDIA:
253             gl_string_cursor = strstr(gl_string, "NVIDIA");
254             gl_string_cursor = strstr(gl_string_cursor, " ");
255             while (*gl_string_cursor && ' ' == *gl_string_cursor) ++gl_string_cursor;
256             if (*gl_string_cursor) {
257                 char tmp[16];
258                 int cursor = 0;
259
260                 while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
261                     tmp[cursor++] = *gl_string_cursor;
262                     ++gl_string_cursor;
263                 }
264                 tmp[cursor] = 0;
265                 major = atoi(tmp);
266
267                 if (*gl_string_cursor != '.') WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
268                 ++gl_string_cursor;
269
270                 while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
271                     tmp[cursor++] = *gl_string_cursor;
272                     ++gl_string_cursor;
273                 }
274                 tmp[cursor] = 0;
275                 minor = atoi(tmp);
276             }
277             break;
278
279         case VENDOR_ATI:
280             major = minor = 0;
281             gl_string_cursor = strchr(gl_string, '-');
282             if (gl_string_cursor) {
283                 int error = 0;
284                 gl_string_cursor++;
285
286                 /* Check if version number is of the form x.y.z */
287                 if (*gl_string_cursor > '9' && *gl_string_cursor < '0')
288                     error = 1;
289                 if (!error && *(gl_string_cursor+2) > '9' && *(gl_string_cursor+2) < '0')
290                     error = 1;
291                 if (!error && *(gl_string_cursor+4) > '9' && *(gl_string_cursor+4) < '0')
292                     error = 1;
293                 if (!error && *(gl_string_cursor+1) != '.' && *(gl_string_cursor+3) != '.')
294                     error = 1;
295
296                 /* Mark version number as malformed */
297                 if (error)
298                     gl_string_cursor = 0;
299             }
300
301             if (!gl_string_cursor)
302                 WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
303             else {
304                 major = *gl_string_cursor - '0';
305                 minor = (*(gl_string_cursor+2) - '0') * 256 + (*(gl_string_cursor+4) - '0');
306             }
307             break;
308
309         default:
310             major = 0;
311             minor = 9;
312         }
313         gl_info->gl_driver_version = MAKEDWORD_VERSION(major, minor);
314         TRACE_(d3d_caps)("found GL_VERSION  (%s)->(0x%08lx)\n", debugstr_a(gl_string), gl_info->gl_driver_version);
315
316         /* Fill in the renderer information */
317         gl_string = glGetString(GL_RENDERER);
318         strcpy(gl_info->gl_renderer, gl_string);
319
320         switch (gl_info->gl_vendor) {
321         case VENDOR_NVIDIA:
322             if (strstr(gl_info->gl_renderer, "GeForce4 Ti")) {
323                 gl_info->gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
324             } else if (strstr(gl_info->gl_renderer, "GeForceFX")) {
325                 gl_info->gl_card = CARD_NVIDIA_GEFORCEFX_5900ULTRA;
326             } else {
327                 gl_info->gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
328             }
329             break;
330
331         case VENDOR_ATI:
332             if (strstr(gl_info->gl_renderer, "RADEON 9800 PRO")) {
333                 gl_info->gl_card = CARD_ATI_RADEON_9800PRO;
334             } else if (strstr(gl_info->gl_renderer, "RADEON 9700 PRO")) {
335                 gl_info->gl_card = CARD_ATI_RADEON_9700PRO;
336             } else {
337                 gl_info->gl_card = CARD_ATI_RADEON_8500;
338             }
339             break;
340
341         default:
342             gl_info->gl_card = CARD_WINE;
343             break;
344         }
345     } else {
346         FIXME("get version string returned null\n");
347     }
348
349     TRACE_(d3d_caps)("found GL_RENDERER (%s)->(0x%04x)\n", debugstr_a(gl_info->gl_renderer), gl_info->gl_card);
350
351     /*
352      * Initialize openGL extension related variables
353      *  with Default values
354      */
355     memset(&gl_info->supported, 0, sizeof(gl_info->supported));
356     gl_info->max_textures   = 1;
357     gl_info->max_samplers   = 1;
358     gl_info->ps_arb_version = PS_VERSION_NOT_SUPPORTED;
359     gl_info->vs_arb_version = VS_VERSION_NOT_SUPPORTED;
360     gl_info->vs_nv_version  = VS_VERSION_NOT_SUPPORTED;
361     gl_info->vs_ati_version = VS_VERSION_NOT_SUPPORTED;
362
363     /* Now work out what GL support this card really has */
364 #define USE_GL_FUNC(type, pfn) gl_info->pfn = NULL;
365     GL_EXT_FUNCS_GEN;
366 #undef USE_GL_FUNC
367
368     /* Retrieve opengl defaults */
369     glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
370     gl_info->max_clipplanes = min(D3DMAXUSERCLIPPLANES, gl_max);
371     TRACE_(d3d_caps)("ClipPlanes support - num Planes=%d\n", gl_max);
372
373     glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
374     gl_info->max_lights = gl_max;
375     TRACE_(d3d_caps)("Lights support - max lights=%d\n", gl_max);
376
377     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_max);
378     gl_info->max_texture_size = gl_max;
379     TRACE_(d3d_caps)("Maximum texture size support - max texture size=%d\n", gl_max);
380
381     glGetFloatv(GL_POINT_SIZE_RANGE, &gl_float);
382     gl_info->max_pointsize = gl_float;
383     TRACE_(d3d_caps)("Maximum point size support - max texture size=%f\n", gl_float);
384
385     /* Parse the gl supported features, in theory enabling parts of our code appropriately */
386     GL_Extensions = glGetString(GL_EXTENSIONS);
387     TRACE_(d3d_caps)("GL_Extensions reported:\n");
388
389     if (NULL == GL_Extensions) {
390         ERR("   GL_Extensions returns NULL\n");
391     } else {
392         while (*GL_Extensions != 0x00) {
393             const char *Start = GL_Extensions;
394             char        ThisExtn[256];
395
396             memset(ThisExtn, 0x00, sizeof(ThisExtn));
397             while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
398                 GL_Extensions++;
399             }
400             memcpy(ThisExtn, Start, (GL_Extensions - Start));
401             TRACE_(d3d_caps)("- %s\n", ThisExtn);
402
403             /**
404              * ARB
405              */
406             if (strcmp(ThisExtn, "GL_ARB_fragment_program") == 0) {
407                 gl_info->ps_arb_version = PS_VERSION_11;
408                 TRACE_(d3d_caps)(" FOUND: ARB Pixel Shader support - version=%02x\n", gl_info->ps_arb_version);
409                 gl_info->supported[ARB_FRAGMENT_PROGRAM] = TRUE;
410                 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &gl_max);
411                 TRACE_(d3d_caps)(" FOUND: ARB Pixel Shader support - GL_MAX_TEXTURE_IMAGE_UNITS_ARB=%u\n", gl_max);
412                 gl_info->max_samplers = min(MAX_SAMPLERS, gl_max);
413             } else if (strcmp(ThisExtn, "GL_ARB_multisample") == 0) {
414                 TRACE_(d3d_caps)(" FOUND: ARB Multisample support\n");
415                 gl_info->supported[ARB_MULTISAMPLE] = TRUE;
416             } else if (strcmp(ThisExtn, "GL_ARB_multitexture") == 0) {
417                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
418                 TRACE_(d3d_caps)(" FOUND: ARB Multitexture support - GL_MAX_TEXTURE_UNITS_ARB=%u\n", gl_max);
419                 gl_info->supported[ARB_MULTITEXTURE] = TRUE;
420                 gl_info->max_textures = min(MAX_TEXTURES, gl_max);
421                 gl_info->max_samplers = max(gl_info->max_samplers, gl_max);
422             } else if (strcmp(ThisExtn, "GL_ARB_texture_cube_map") == 0) {
423                 TRACE_(d3d_caps)(" FOUND: ARB Texture Cube Map support\n");
424                 gl_info->supported[ARB_TEXTURE_CUBE_MAP] = TRUE;
425                 TRACE_(d3d_caps)(" IMPLIED: NVIDIA (NV) Texture Gen Reflection support\n");
426                 gl_info->supported[NV_TEXGEN_REFLECTION] = TRUE;
427             } else if (strcmp(ThisExtn, "GL_ARB_texture_compression") == 0) {
428                 TRACE_(d3d_caps)(" FOUND: ARB Texture Compression support\n");
429                 gl_info->supported[ARB_TEXTURE_COMPRESSION] = TRUE;
430             } else if (strcmp(ThisExtn, "GL_ARB_texture_env_add") == 0) {
431                 TRACE_(d3d_caps)(" FOUND: ARB Texture Env Add support\n");
432                 gl_info->supported[ARB_TEXTURE_ENV_ADD] = TRUE;
433             } else if (strcmp(ThisExtn, "GL_ARB_texture_env_combine") == 0) {
434                 TRACE_(d3d_caps)(" FOUND: ARB Texture Env combine support\n");
435                 gl_info->supported[ARB_TEXTURE_ENV_COMBINE] = TRUE;
436             } else if (strcmp(ThisExtn, "GL_ARB_texture_env_dot3") == 0) {
437                 TRACE_(d3d_caps)(" FOUND: ARB Dot3 support\n");
438                 gl_info->supported[ARB_TEXTURE_ENV_DOT3] = TRUE;
439             } else if (strcmp(ThisExtn, "GL_ARB_texture_border_clamp") == 0) {
440                 TRACE_(d3d_caps)(" FOUND: ARB Texture border clamp support\n");
441                 gl_info->supported[ARB_TEXTURE_BORDER_CLAMP] = TRUE;
442             } else if (strcmp(ThisExtn, "GL_ARB_texture_mirrored_repeat") == 0) {
443                 TRACE_(d3d_caps)(" FOUND: ARB Texture mirrored repeat support\n");
444                 gl_info->supported[ARB_TEXTURE_MIRRORED_REPEAT] = TRUE;
445             } else if (strcmp(ThisExtn, "GLX_ARB_multisample") == 0) {
446                 TRACE_(d3d_caps)(" FOUND: ARB multisample support\n");
447                 gl_info->supported[ARB_MULTISAMPLE] = TRUE;
448             } else if (strcmp(ThisExtn, "GL_ARB_point_sprite") == 0) {
449                 TRACE_(d3d_caps)(" FOUND: ARB point sprint support\n");
450                 gl_info->supported[ARB_POINT_SPRITE] = TRUE;
451             } else if (strstr(ThisExtn, "GL_ARB_vertex_program")) {
452                 gl_info->vs_arb_version = VS_VERSION_11;
453                 TRACE_(d3d_caps)(" FOUND: ARB Vertex Shader support - version=%02x\n", gl_info->vs_arb_version);
454                 gl_info->supported[ARB_VERTEX_PROGRAM] = TRUE;
455             } else if (strcmp(ThisExtn, "GL_ARB_vertex_blend") == 0) {
456                 glGetIntegerv(GL_MAX_VERTEX_UNITS_ARB, &gl_max);
457                 TRACE_(d3d_caps)(" FOUND: ARB Vertex Blend support GL_MAX_VERTEX_UNITS_ARB %d\n", gl_max);
458                 gl_info->max_blends = gl_max;
459                 gl_info->supported[ARB_VERTEX_BLEND] = TRUE;
460             } else if (strcmp(ThisExtn, "GL_ARB_vertex_buffer_object") == 0) {
461                 TRACE_(d3d_caps)(" FOUND: ARB Vertex Buffer support\n");
462                 gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] = TRUE;
463             /**
464              * EXT
465              */
466             } else if (strcmp(ThisExtn, "GL_EXT_fog_coord") == 0) {
467                 TRACE_(d3d_caps)(" FOUND: EXT Fog coord support\n");
468                 gl_info->supported[EXT_FOG_COORD] = TRUE;
469             } else if (strcmp(ThisExtn, "GL_EXT_paletted_texture") == 0) { /* handle paletted texture extensions */
470                 TRACE_(d3d_caps)(" FOUND: EXT Paletted texture support\n");
471                 gl_info->supported[EXT_PALETTED_TEXTURE] = TRUE;
472             } else if (strcmp(ThisExtn, "GL_EXT_point_parameters") == 0) {
473                 TRACE_(d3d_caps)(" FOUND: EXT Point parameters support\n");
474                 gl_info->supported[EXT_POINT_PARAMETERS] = TRUE;
475             } else if (strcmp(ThisExtn, "GL_EXT_secondary_color") == 0) {
476                 TRACE_(d3d_caps)(" FOUND: EXT Secondary coord support\n");
477                 gl_info->supported[EXT_SECONDARY_COLOR] = TRUE;
478             } else if (strcmp(ThisExtn, "GL_EXT_stencil_wrap") == 0) {
479                 TRACE_(d3d_caps)(" FOUND: EXT Stencil wrap support\n");
480                 gl_info->supported[EXT_STENCIL_WRAP] = TRUE;
481             } else if (strcmp(ThisExtn, "GL_EXT_texture_compression_s3tc") == 0) {
482                 TRACE_(d3d_caps)(" FOUND: EXT Texture S3TC compression support\n");
483                 gl_info->supported[EXT_TEXTURE_COMPRESSION_S3TC] = TRUE;
484             } else if (strcmp(ThisExtn, "GL_EXT_texture_env_add") == 0) {
485                 TRACE_(d3d_caps)(" FOUND: EXT Texture Env Add support\n");
486                 gl_info->supported[EXT_TEXTURE_ENV_ADD] = TRUE;
487             } else if (strcmp(ThisExtn, "GL_EXT_texture_env_combine") == 0) {
488                 TRACE_(d3d_caps)(" FOUND: EXT Texture Env combine support\n");
489                 gl_info->supported[EXT_TEXTURE_ENV_COMBINE] = TRUE;
490             } else if (strcmp(ThisExtn, "GL_EXT_texture_env_dot3") == 0) {
491                 TRACE_(d3d_caps)(" FOUND: EXT Dot3 support\n");
492                 gl_info->supported[EXT_TEXTURE_ENV_DOT3] = TRUE;
493             } else if (strcmp(ThisExtn, "GL_EXT_texture_filter_anisotropic") == 0) {
494                 gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC] = TRUE;
495                 glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_max);
496                 TRACE_(d3d_caps)(" FOUND: EXT Texture Anisotropic filter support. GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT %d\n", gl_max);
497                 gl_info->max_anisotropy = gl_max;
498             } else if (strcmp(ThisExtn, "GL_EXT_texture_lod") == 0) {
499                 TRACE_(d3d_caps)(" FOUND: EXT Texture LOD support\n");
500                 gl_info->supported[EXT_TEXTURE_LOD] = TRUE;
501             } else if (strcmp(ThisExtn, "GL_EXT_texture_lod_bias") == 0) {
502                 TRACE_(d3d_caps)(" FOUND: EXT Texture LOD bias support\n");
503                 gl_info->supported[EXT_TEXTURE_LOD_BIAS] = TRUE;
504             } else if (strcmp(ThisExtn, "GL_EXT_vertex_weighting") == 0) {
505                 TRACE_(d3d_caps)(" FOUND: EXT Vertex weighting support\n");
506                 gl_info->supported[EXT_VERTEX_WEIGHTING] = TRUE;
507
508             /**
509              * NVIDIA
510              */
511             } else if (strstr(ThisExtn, "GL_NV_fog_distance")) {
512                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Fog Distance support\n");
513                 gl_info->supported[NV_FOG_DISTANCE] = TRUE;
514             } else if (strstr(ThisExtn, "GL_NV_fragment_program")) {
515                 gl_info->ps_nv_version = PS_VERSION_11;
516                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Pixel Shader support - version=%02x\n", gl_info->ps_nv_version);
517             } else if (strcmp(ThisExtn, "GL_NV_register_combiners") == 0) {
518                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (1) support\n");
519                 gl_info->supported[NV_REGISTER_COMBINERS] = TRUE;
520             } else if (strcmp(ThisExtn, "GL_NV_register_combiners2") == 0) {
521                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (2) support\n");
522                 gl_info->supported[NV_REGISTER_COMBINERS2] = TRUE;
523             } else if (strcmp(ThisExtn, "GL_NV_texgen_reflection") == 0) {
524                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Gen Reflection support\n");
525                 gl_info->supported[NV_TEXGEN_REFLECTION] = TRUE;
526             } else if (strcmp(ThisExtn, "GL_NV_texture_env_combine4") == 0) {
527                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Env combine (4) support\n");
528                 gl_info->supported[NV_TEXTURE_ENV_COMBINE4] = TRUE;
529             } else if (strcmp(ThisExtn, "GL_NV_texture_shader") == 0) {
530                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (1) support\n");
531                 gl_info->supported[NV_TEXTURE_SHADER] = TRUE;
532             } else if (strcmp(ThisExtn, "GL_NV_texture_shader2") == 0) {
533                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (2) support\n");
534                 gl_info->supported[NV_TEXTURE_SHADER2] = TRUE;
535             } else if (strcmp(ThisExtn, "GL_NV_texture_shader3") == 0) {
536                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (3) support\n");
537                 gl_info->supported[NV_TEXTURE_SHADER3] = TRUE;
538             } else if (strstr(ThisExtn, "GL_NV_vertex_program")) {
539                 gl_info->vs_nv_version = max(gl_info->vs_nv_version, (0 == strcmp(ThisExtn, "GL_NV_vertex_program1_1")) ? VS_VERSION_11 : VS_VERSION_10);
540                 gl_info->vs_nv_version = max(gl_info->vs_nv_version, (0 == strcmp(ThisExtn, "GL_NV_vertex_program2"))   ? VS_VERSION_20 : VS_VERSION_10);
541                 TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Vertex Shader support - version=%02x\n", gl_info->vs_nv_version);
542                 gl_info->supported[NV_VERTEX_PROGRAM] = TRUE;
543
544             /**
545              * ATI
546              */
547             /** TODO */
548             } else if (strcmp(ThisExtn, "GL_ATI_texture_env_combine3") == 0) {
549                 TRACE_(d3d_caps)(" FOUND: ATI Texture Env combine (3) support\n");
550                 gl_info->supported[ATI_TEXTURE_ENV_COMBINE3] = TRUE;
551             } else if (strcmp(ThisExtn, "GL_ATI_texture_mirror_once") == 0) {
552                 TRACE_(d3d_caps)(" FOUND: ATI Texture Mirror Once support\n");
553                 gl_info->supported[ATI_TEXTURE_MIRROR_ONCE] = TRUE;
554             } else if (strcmp(ThisExtn, "GL_EXT_vertex_shader") == 0) {
555                 gl_info->vs_ati_version = VS_VERSION_11;
556                 TRACE_(d3d_caps)(" FOUND: ATI (EXT) Vertex Shader support - version=%02x\n", gl_info->vs_ati_version);
557                 gl_info->supported[EXT_VERTEX_SHADER] = TRUE;
558             }
559
560
561             if (*GL_Extensions == ' ') GL_Extensions++;
562         }
563     }
564
565 #define USE_GL_FUNC(type, pfn) gl_info->pfn = (type) glXGetProcAddressARB( (const GLubyte *) #pfn);
566     GL_EXT_FUNCS_GEN;
567 #undef USE_GL_FUNC
568
569     if (display != NULL) {
570         GLX_Extensions = glXQueryExtensionsString(display, DefaultScreen(display));
571         TRACE_(d3d_caps)("GLX_Extensions reported:\n");
572
573         if (NULL == GLX_Extensions) {
574             ERR("   GLX_Extensions returns NULL\n");
575         } else {
576             while (*GLX_Extensions != 0x00) {
577                 const char *Start = GLX_Extensions;
578                 char ThisExtn[256];
579
580                 memset(ThisExtn, 0x00, sizeof(ThisExtn));
581                 while (*GLX_Extensions != ' ' && *GLX_Extensions != 0x00) {
582                     GLX_Extensions++;
583                 }
584                 memcpy(ThisExtn, Start, (GLX_Extensions - Start));
585                 TRACE_(d3d_caps)("- %s\n", ThisExtn);
586                 if (*GLX_Extensions == ' ') GLX_Extensions++;
587             }
588         }
589     }
590
591 #define USE_GL_FUNC(type, pfn) gl_info->pfn = (type) glXGetProcAddressARB( (const GLubyte *) #pfn);
592     GLX_EXT_FUNCS_GEN;
593 #undef USE_GL_FUNC
594
595     /* If we created a dummy context, throw it away */
596     if (NULL != fake_ctx) WineD3D_ReleaseFakeGLContext(fake_ctx);
597
598     /* Only save the values obtained when a display is provided */
599     if (fake_ctx == NULL) {
600         return TRUE;
601     } else {
602         return FALSE;
603     }
604 }
605
606 /**********************************************************
607  * IWineD3D implementation follows
608  **********************************************************/
609
610 UINT     WINAPI  IWineD3DImpl_GetAdapterCount (IWineD3D *iface) {
611     IWineD3DImpl *This = (IWineD3DImpl *)iface;
612
613     /* FIXME: Set to one for now to imply the display */
614     TRACE_(d3d_caps)("(%p): Mostly stub, only returns primary display\n", This);
615     return 1;
616 }
617
618 HRESULT  WINAPI  IWineD3DImpl_RegisterSoftwareDevice(IWineD3D *iface, void* pInitializeFunction) {
619     IWineD3DImpl *This = (IWineD3DImpl *)iface;
620     FIXME("(%p)->(%p): stub\n", This, pInitializeFunction);
621     return D3D_OK;
622 }
623
624 HMONITOR WINAPI  IWineD3DImpl_GetAdapterMonitor(IWineD3D *iface, UINT Adapter) {
625     IWineD3DImpl *This = (IWineD3DImpl *)iface;
626     FIXME_(d3d_caps)("(%p)->(Adptr:%d)\n", This, Adapter);
627     if (Adapter >= IWineD3DImpl_GetAdapterCount(iface)) {
628         return NULL;
629     }
630     return D3D_OK;
631 }
632
633 /* FIXME: GetAdapterModeCount and EnumAdapterModes currently only returns modes
634      of the same bpp but different resolutions                                  */
635
636 /* Note: dx9 supplies a format. Calls from d3d8 supply D3DFMT_UNKNOWN */
637 UINT     WINAPI  IWineD3DImpl_GetAdapterModeCount(IWineD3D *iface, UINT Adapter, WINED3DFORMAT Format) {
638     IWineD3DImpl *This = (IWineD3DImpl *)iface;
639     TRACE_(d3d_caps)("(%p}->(Adapter: %d, Format: %s)\n", This, Adapter, debug_d3dformat(Format));
640
641     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
642         return 0;
643     }
644
645     if (Adapter == 0) { /* Display */
646         int i = 0;
647         int j = 0;
648 #if !defined( DEBUG_SINGLE_MODE )
649         DEVMODEW DevModeW;
650
651         /* Work out the current screen bpp */
652         HDC hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
653         int bpp = GetDeviceCaps(hdc, BITSPIXEL);
654         DeleteDC(hdc);
655
656         while (EnumDisplaySettingsExW(NULL, j, &DevModeW, 0)) {
657             j++;
658             switch (Format)
659             {
660             case D3DFMT_UNKNOWN:
661                    i++;
662                    break;
663             case D3DFMT_X8R8G8B8:
664             case D3DFMT_A8R8G8B8:
665                    if (min(DevModeW.dmBitsPerPel, bpp) == 32) i++;
666                    if (min(DevModeW.dmBitsPerPel, bpp) == 24) i++;
667                    break;
668             case D3DFMT_X1R5G5B5:
669             case D3DFMT_A1R5G5B5:
670             case D3DFMT_R5G6B5:
671                    if (min(DevModeW.dmBitsPerPel, bpp) == 16) i++;
672                    break;
673             default:
674                    /* Skip other modes as they do not match requested format */
675                    break;
676             }
677         }
678 #else
679         i = 1;
680         j = 1;
681 #endif
682         TRACE_(d3d_caps)("(%p}->(Adapter: %d) => %d (out of %d)\n", This, Adapter, i, j);
683         return i;
684     } else {
685         FIXME_(d3d_caps)("Adapter not primary display\n");
686     }
687     return 0;
688 }
689
690 /* Note: dx9 supplies a format. Calls from d3d8 supply D3DFMT_UNKNOWN */
691 HRESULT WINAPI IWineD3DImpl_EnumAdapterModes(IWineD3D *iface, UINT Adapter, WINED3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
692     IWineD3DImpl *This = (IWineD3DImpl *)iface;
693     TRACE_(d3d_caps)("(%p}->(Adapter:%d, mode:%d, pMode:%p, format:%s)\n", This, Adapter, Mode, pMode, debug_d3dformat(Format));
694
695     /* Validate the parameters as much as possible */
696     if (NULL == pMode ||
697         Adapter >= IWineD3DImpl_GetAdapterCount(iface) ||
698         Mode    >= IWineD3DImpl_GetAdapterModeCount(iface, Adapter, Format)) {
699         return D3DERR_INVALIDCALL;
700     }
701
702     if (Adapter == 0) { /* Display */
703 #if !defined( DEBUG_SINGLE_MODE )
704         DEVMODEW DevModeW;
705         int ModeIdx = 0;
706
707         /* Work out the current screen bpp */
708         HDC hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
709         int bpp = GetDeviceCaps(hdc, BITSPIXEL);
710         DeleteDC(hdc);
711
712         /* If we are filtering to a specific format, then need to skip all unrelated
713            modes, but if mode is irrelevant, then we can use the index directly      */
714         if (Format == D3DFMT_UNKNOWN)
715         {
716             ModeIdx = Mode;
717         } else {
718             int i = 0;
719             int j = 0;
720             DEVMODEW DevModeWtmp;
721
722
723             while (i<(Mode+1) && EnumDisplaySettingsExW(NULL, j, &DevModeWtmp, 0)) {
724                 j++;
725                 switch (Format)
726                 {
727                 case D3DFMT_UNKNOWN:
728                        i++;
729                        break;
730                 case D3DFMT_X8R8G8B8:
731                 case D3DFMT_A8R8G8B8:
732                        if (min(DevModeWtmp.dmBitsPerPel, bpp) == 32) i++;
733                        if (min(DevModeWtmp.dmBitsPerPel, bpp) == 24) i++;
734                        break;
735                 case D3DFMT_X1R5G5B5:
736                 case D3DFMT_A1R5G5B5:
737                 case D3DFMT_R5G6B5:
738                        if (min(DevModeWtmp.dmBitsPerPel, bpp) == 16) i++;
739                        break;
740                 default:
741                        /* Skip other modes as they do not match requested format */
742                        break;
743                 }
744             }
745             ModeIdx = j;
746         }
747
748         /* Now get the display mode via the calculated index */
749         if (EnumDisplaySettingsExW(NULL, ModeIdx, &DevModeW, 0))
750         {
751             pMode->Width        = DevModeW.dmPelsWidth;
752             pMode->Height       = DevModeW.dmPelsHeight;
753             bpp                 = min(DevModeW.dmBitsPerPel, bpp);
754             pMode->RefreshRate  = D3DADAPTER_DEFAULT;
755             if (DevModeW.dmFields&DM_DISPLAYFREQUENCY)
756             {
757                 pMode->RefreshRate = DevModeW.dmDisplayFrequency;
758             }
759
760             if (Format == D3DFMT_UNKNOWN)
761             {
762                 switch (bpp) {
763                 case  8: pMode->Format = D3DFMT_R3G3B2;   break;
764                 case 16: pMode->Format = D3DFMT_R5G6B5;   break;
765                 case 24: /* pMode->Format = D3DFMT_R5G6B5;   break;*/ /* Make 24bit appear as 32 bit */
766                 case 32: pMode->Format = D3DFMT_A8R8G8B8; break;
767                 default: pMode->Format = D3DFMT_UNKNOWN;
768                 }
769             } else {
770                 pMode->Format = Format;
771             }
772         }
773         else
774         {
775             TRACE_(d3d_caps)("Requested mode out of range %d\n", Mode);
776             return D3DERR_INVALIDCALL;
777         }
778
779 #else
780         /* Return one setting of the format requested */
781         if (Mode > 0) return D3DERR_INVALIDCALL;
782         pMode->Width        = 800;
783         pMode->Height       = 600;
784         pMode->RefreshRate  = D3DADAPTER_DEFAULT;
785         pMode->Format       = (Format == D3DFMT_UNKNOWN) ? D3DFMT_A8R8G8B8 : Format;
786         bpp = 32;
787 #endif
788         TRACE_(d3d_caps)("W %d H %d rr %d fmt (%x - %s) bpp %u\n", pMode->Width, pMode->Height,
789                  pMode->RefreshRate, pMode->Format, debug_d3dformat(pMode->Format), bpp);
790
791     } else {
792         FIXME_(d3d_caps)("Adapter not primary display\n");
793     }
794
795     return D3D_OK;
796 }
797
798 HRESULT WINAPI IWineD3DImpl_GetAdapterDisplayMode(IWineD3D *iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
799     IWineD3DImpl *This = (IWineD3DImpl *)iface;
800     TRACE_(d3d_caps)("(%p}->(Adapter: %d, pMode: %p)\n", This, Adapter, pMode);
801
802     if (NULL == pMode ||
803         Adapter >= IWineD3D_GetAdapterCount(iface)) {
804         return D3DERR_INVALIDCALL;
805     }
806
807     if (Adapter == 0) { /* Display */
808         int bpp = 0;
809         DEVMODEW DevModeW;
810
811         EnumDisplaySettingsExW(NULL, (DWORD)-1, &DevModeW, 0);
812         pMode->Width        = DevModeW.dmPelsWidth;
813         pMode->Height       = DevModeW.dmPelsHeight;
814         bpp                 = DevModeW.dmBitsPerPel;
815         pMode->RefreshRate  = D3DADAPTER_DEFAULT;
816         if (DevModeW.dmFields&DM_DISPLAYFREQUENCY)
817         {
818             pMode->RefreshRate = DevModeW.dmDisplayFrequency;
819         }
820
821         switch (bpp) {
822         case  8: pMode->Format       = D3DFMT_R3G3B2;   break;
823         case 16: pMode->Format       = D3DFMT_R5G6B5;   break;
824         case 24: pMode->Format       = D3DFMT_X8R8G8B8; break; /* Robots needs 24bit to be X8R8G8B8 */
825         case 32: pMode->Format       = D3DFMT_A8R8G8B8; break;
826         default: pMode->Format       = D3DFMT_UNKNOWN;
827         }
828
829     } else {
830         FIXME_(d3d_caps)("Adapter not primary display\n");
831     }
832
833     TRACE_(d3d_caps)("returning w:%d, h:%d, ref:%d, fmt:%s\n", pMode->Width,
834           pMode->Height, pMode->RefreshRate, debug_d3dformat(pMode->Format));
835     return D3D_OK;
836 }
837
838 static Display * WINAPI IWineD3DImpl_GetAdapterDisplay(IWineD3D *iface, UINT Adapter) {
839     Display *display;
840     HDC     device_context;
841     /* only works with one adapter at the moment... */
842
843     /* Get the display */
844     device_context = GetDC(0);
845     display = get_display(device_context);
846     ReleaseDC(0, device_context);
847     return display;
848 }
849
850 /* NOTE: due to structure differences between dx8 and dx9 D3DADAPTER_IDENTIFIER,
851    and fields being inserted in the middle, a new structure is used in place    */
852 HRESULT WINAPI IWineD3DImpl_GetAdapterIdentifier(IWineD3D *iface, UINT Adapter, DWORD Flags,
853                                                    WINED3DADAPTER_IDENTIFIER* pIdentifier) {
854     IWineD3DImpl *This = (IWineD3DImpl *)iface;
855
856     TRACE_(d3d_caps)("(%p}->(Adapter: %d, Flags: %lx, pId=%p)\n", This, Adapter, Flags, pIdentifier);
857
858     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
859         return D3DERR_INVALIDCALL;
860     }
861
862     if (Adapter == 0) { /* Display - only device supported for now */
863
864         BOOL isGLInfoValid = This->isGLInfoValid;
865
866         /* FillGLCaps updates gl_info, but we only want to store and
867            reuse the values once we have a context which is valid. Values from
868            a temporary context may differ from the final ones                 */
869         if (isGLInfoValid == FALSE) {
870             /* If we don't know the device settings, go query them now */
871             isGLInfoValid = IWineD3DImpl_FillGLCaps(&This->gl_info, IWineD3DImpl_GetAdapterDisplay(iface, Adapter));
872         }
873
874         /* If it worked, return the information requested */
875         if (isGLInfoValid) {
876           TRACE_(d3d_caps)("device/Vendor Name and Version detection using FillGLCaps\n");
877           strcpy(pIdentifier->Driver, "Display");
878           strcpy(pIdentifier->Description, "Direct3D HAL");
879
880           /* Note dx8 doesn't supply a DeviceName */
881           if (NULL != pIdentifier->DeviceName) strcpy(pIdentifier->DeviceName, "\\\\.\\DISPLAY"); /* FIXME: May depend on desktop? */
882           pIdentifier->DriverVersion->u.HighPart = 0xa;
883           pIdentifier->DriverVersion->u.LowPart = This->gl_info.gl_driver_version;
884           *(pIdentifier->VendorId) = This->gl_info.gl_vendor;
885           *(pIdentifier->DeviceId) = This->gl_info.gl_card;
886           *(pIdentifier->SubSysId) = 0;
887           *(pIdentifier->Revision) = 0;
888
889         } else {
890
891           /* If it failed, return dummy values from an NVidia driver */
892           WARN_(d3d_caps)("Cannot get GLCaps for device/Vendor Name and Version detection using FillGLCaps, currently using NVIDIA identifiers\n");
893           strcpy(pIdentifier->Driver, "Display");
894           strcpy(pIdentifier->Description, "Direct3D HAL");
895           if (NULL != pIdentifier->DeviceName) strcpy(pIdentifier->DeviceName, "\\\\.\\DISPLAY"); /* FIXME: May depend on desktop? */
896           pIdentifier->DriverVersion->u.HighPart = 0xa;
897           pIdentifier->DriverVersion->u.LowPart = MAKEDWORD_VERSION(53, 96); /* last Linux Nvidia drivers */
898           *(pIdentifier->VendorId) = VENDOR_NVIDIA;
899           *(pIdentifier->DeviceId) = CARD_NVIDIA_GEFORCE4_TI4600;
900           *(pIdentifier->SubSysId) = 0;
901           *(pIdentifier->Revision) = 0;
902         }
903
904         /*FIXME: memcpy(&pIdentifier->DeviceIdentifier, ??, sizeof(??GUID)); */
905         if (Flags & D3DENUM_NO_WHQL_LEVEL) {
906             *(pIdentifier->WHQLLevel) = 0;
907         } else {
908             *(pIdentifier->WHQLLevel) = 1;
909         }
910
911     } else {
912         FIXME_(d3d_caps)("Adapter not primary display\n");
913     }
914
915     return D3D_OK;
916 }
917
918 static BOOL IWineD3DImpl_IsGLXFBConfigCompatibleWithRenderFmt(WineD3D_Context* ctx, GLXFBConfig cfgs, WINED3DFORMAT Format) {
919   int gl_test;
920   int rb, gb, bb, ab, type, buf_sz;
921
922   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_RED_SIZE,   &rb);
923   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_GREEN_SIZE, &gb);
924   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_BLUE_SIZE,  &bb);
925   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_ALPHA_SIZE, &ab);
926   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_RENDER_TYPE, &type);
927   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_BUFFER_SIZE, &buf_sz);
928
929   switch (Format) {
930   case WINED3DFMT_X8R8G8B8:
931   case WINED3DFMT_R8G8B8:
932     if (8 == rb && 8 == gb && 8 == bb) return TRUE;
933     break;
934   case WINED3DFMT_A8R8G8B8:
935     if (8 == rb && 8 == gb && 8 == bb && 8 == ab) return TRUE;
936     break;
937   case WINED3DFMT_X1R5G5B5:
938     if (5 == rb && 5 == gb && 5 == bb) return TRUE;
939     break;
940   case WINED3DFMT_A1R5G5B5:
941     if (5 == rb && 5 == gb && 5 == bb && 1 == ab) return TRUE;
942     break;
943   case WINED3DFMT_R5G6B5:
944     if (5 == rb && 6 == gb && 5 == bb) return TRUE;
945     break;
946   case WINED3DFMT_R3G3B2:
947     if (3 == rb && 3 == gb && 2 == bb) return TRUE;
948     break;
949   case WINED3DFMT_A8P8:
950     if (type & GLX_COLOR_INDEX_BIT && 8 == buf_sz && 8 == ab) return TRUE;
951     break;
952   case WINED3DFMT_P8:
953     if (type & GLX_COLOR_INDEX_BIT && 8 == buf_sz) return TRUE;
954     break;
955   default:
956     ERR("unsupported format %s\n", debug_d3dformat(Format));
957     break;
958   }
959   return FALSE;
960 }
961
962 static BOOL IWineD3DImpl_IsGLXFBConfigCompatibleWithDepthFmt(WineD3D_Context* ctx, GLXFBConfig cfgs, WINED3DFORMAT Format) {
963   int gl_test;
964   int db, sb;
965
966   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_DEPTH_SIZE, &db);
967   gl_test = glXGetFBConfigAttrib(ctx->display, cfgs, GLX_STENCIL_SIZE, &sb);
968
969   switch (Format) {
970   case WINED3DFMT_D16:
971   case WINED3DFMT_D16_LOCKABLE:
972     if (16 == db) return TRUE;
973     break;
974   case WINED3DFMT_D32:
975     if (32 == db) return TRUE;
976     break;
977   case WINED3DFMT_D15S1:
978     if (15 == db) return TRUE;
979     break;
980   case WINED3DFMT_D24S8:
981     if (24 == db && 8 == sb) return TRUE;
982     break;
983   case WINED3DFMT_D24X8:
984     if (24 == db) return TRUE;
985     break;
986   case WINED3DFMT_D24X4S4:
987     if (24 == db && 4 == sb) return TRUE;
988     break;
989   case WINED3DFMT_D32F_LOCKABLE:
990     if (32 == db) return TRUE;
991     break;
992   default:
993     ERR("unsupported format %s\n", debug_d3dformat(Format));
994     break;
995   }
996   return FALSE;
997 }
998
999 HRESULT WINAPI IWineD3DImpl_CheckDepthStencilMatch(IWineD3D *iface, UINT Adapter, D3DDEVTYPE DeviceType,
1000                                                    WINED3DFORMAT AdapterFormat,
1001                                                    WINED3DFORMAT RenderTargetFormat,
1002                                                    WINED3DFORMAT DepthStencilFormat) {
1003     IWineD3DImpl *This = (IWineD3DImpl *)iface;
1004     WARN_(d3d_caps)("(%p)-> (STUB) (Adptr:%d, DevType:(%x,%s), AdptFmt:(%x,%s), RendrTgtFmt:(%x,%s), DepthStencilFmt:(%x,%s))\n",
1005            This, Adapter,
1006            DeviceType, debug_d3ddevicetype(DeviceType),
1007            AdapterFormat, debug_d3dformat(AdapterFormat),
1008            RenderTargetFormat, debug_d3dformat(RenderTargetFormat),
1009            DepthStencilFormat, debug_d3dformat(DepthStencilFormat));
1010
1011     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
1012         return D3DERR_INVALIDCALL;
1013     }
1014
1015     {
1016       GLXFBConfig* cfgs = NULL;
1017       int nCfgs = 0;
1018       int it;
1019       HRESULT hr = D3DERR_NOTAVAILABLE;
1020
1021       WineD3D_Context* ctx = WineD3D_CreateFakeGLContext();
1022       if (NULL != ctx) {
1023         cfgs = glXGetFBConfigs(ctx->display, DefaultScreen(ctx->display), &nCfgs);
1024         for (it = 0; it < nCfgs; ++it) {
1025             if (IWineD3DImpl_IsGLXFBConfigCompatibleWithRenderFmt(ctx, cfgs[it], RenderTargetFormat)) {
1026                 if (IWineD3DImpl_IsGLXFBConfigCompatibleWithDepthFmt(ctx, cfgs[it], DepthStencilFormat)) {
1027                     hr = D3D_OK;
1028                     break ;
1029                 }
1030             }
1031         }
1032         XFree(cfgs);
1033
1034         WineD3D_ReleaseFakeGLContext(ctx);
1035         return hr;
1036       }
1037     }
1038
1039     return D3DERR_NOTAVAILABLE;
1040 }
1041
1042 HRESULT WINAPI IWineD3DImpl_CheckDeviceMultiSampleType(IWineD3D *iface, UINT Adapter, D3DDEVTYPE DeviceType, 
1043                                                        WINED3DFORMAT SurfaceFormat,
1044                                                        BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD*   pQualityLevels) {
1045
1046     IWineD3DImpl *This = (IWineD3DImpl *)iface;
1047     TRACE_(d3d_caps)("(%p)-> (STUB) (Adptr:%d, DevType:(%x,%s), SurfFmt:(%x,%s), Win?%d, MultiSamp:%x, pQual:%p)\n",
1048           This,
1049           Adapter,
1050           DeviceType, debug_d3ddevicetype(DeviceType),
1051           SurfaceFormat, debug_d3dformat(SurfaceFormat),
1052           Windowed,
1053           MultiSampleType,
1054           pQualityLevels);
1055
1056     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
1057         return D3DERR_INVALIDCALL;
1058     }
1059
1060     if (pQualityLevels != NULL) {
1061         static int s_single_shot = 0;
1062         if (!s_single_shot) {
1063             FIXME("Quality levels unsupported at present\n");
1064             s_single_shot = 1;
1065         }
1066         *pQualityLevels = 1; /* Guess at a value! */
1067     }
1068
1069     if (D3DMULTISAMPLE_NONE == MultiSampleType) return D3D_OK;
1070     return D3DERR_NOTAVAILABLE;
1071 }
1072
1073 HRESULT WINAPI IWineD3DImpl_CheckDeviceType(IWineD3D *iface, UINT Adapter, D3DDEVTYPE CheckType,
1074                                             WINED3DFORMAT DisplayFormat, WINED3DFORMAT BackBufferFormat, BOOL Windowed) {
1075
1076     IWineD3DImpl *This = (IWineD3DImpl *)iface;
1077     TRACE_(d3d_caps)("(%p)-> (STUB) (Adptr:%d, CheckType:(%x,%s), DispFmt:(%x,%s), BackBuf:(%x,%s), Win?%d): stub\n",
1078           This,
1079           Adapter,
1080           CheckType, debug_d3ddevicetype(CheckType),
1081           DisplayFormat, debug_d3dformat(DisplayFormat),
1082           BackBufferFormat, debug_d3dformat(BackBufferFormat),
1083           Windowed);
1084
1085     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
1086         return D3DERR_INVALIDCALL;
1087     }
1088
1089     {
1090       GLXFBConfig* cfgs = NULL;
1091       int nCfgs = 0;
1092       int it;
1093       HRESULT hr = D3DERR_NOTAVAILABLE;
1094
1095       WineD3D_Context* ctx = WineD3D_CreateFakeGLContext();
1096       if (NULL != ctx) {
1097         cfgs = glXGetFBConfigs(ctx->display, DefaultScreen(ctx->display), &nCfgs);
1098         for (it = 0; it < nCfgs; ++it) {
1099             if (IWineD3DImpl_IsGLXFBConfigCompatibleWithRenderFmt(ctx, cfgs[it], DisplayFormat)) {
1100                 hr = D3D_OK;
1101                 break ;
1102             }
1103         }
1104         XFree(cfgs);
1105
1106         WineD3D_ReleaseFakeGLContext(ctx);
1107         return hr;
1108       }
1109     }
1110
1111     return D3DERR_NOTAVAILABLE;
1112 }
1113
1114 HRESULT WINAPI IWineD3DImpl_CheckDeviceFormat(IWineD3D *iface, UINT Adapter, D3DDEVTYPE DeviceType, 
1115                                               WINED3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType, WINED3DFORMAT CheckFormat) {
1116     IWineD3DImpl *This = (IWineD3DImpl *)iface;
1117     TRACE_(d3d_caps)("(%p)-> (STUB) (Adptr:%d, DevType:(%u,%s), AdptFmt:(%u,%s), Use:(%lu,%s), ResTyp:(%x,%s), CheckFmt:(%u,%s)) ",
1118           This,
1119           Adapter,
1120           DeviceType, debug_d3ddevicetype(DeviceType),
1121           AdapterFormat, debug_d3dformat(AdapterFormat),
1122           Usage, debug_d3dusage(Usage),
1123           RType, debug_d3dresourcetype(RType),
1124           CheckFormat, debug_d3dformat(CheckFormat));
1125
1126     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
1127         return D3DERR_INVALIDCALL;
1128     }
1129
1130     if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
1131         switch (CheckFormat) {
1132         case D3DFMT_DXT1:
1133         case D3DFMT_DXT3:
1134         case D3DFMT_DXT5:
1135           TRACE_(d3d_caps)("[OK]\n");
1136           return D3D_OK;
1137         default:
1138             break; /* Avoid compiler warnings */
1139         }
1140     }
1141
1142     switch (CheckFormat) {
1143     /*****
1144      * check supported using GL_SUPPORT
1145      */
1146     case D3DFMT_DXT1:
1147     case D3DFMT_DXT2:
1148     case D3DFMT_DXT3:
1149     case D3DFMT_DXT4:
1150     case D3DFMT_DXT5:
1151
1152     /*****
1153      *  supported
1154      */
1155       /*case D3DFMT_R5G6B5: */
1156       /*case D3DFMT_X1R5G5B5:*/
1157       /*case D3DFMT_A1R5G5B5: */
1158       /*case D3DFMT_A4R4G4B4:*/
1159
1160     /*****
1161      * unsupported
1162      */
1163
1164       /* color buffer */
1165       /*case D3DFMT_X8R8G8B8:*/
1166     case D3DFMT_A8R3G3B2:
1167
1168       /* Paletted */
1169     case D3DFMT_P8:
1170     case D3DFMT_A8P8:
1171
1172       /* Luminance */
1173     case D3DFMT_L8:
1174     case D3DFMT_A8L8:
1175     case D3DFMT_A4L4:
1176
1177       /* Bump */
1178 #if 0
1179     case D3DFMT_V8U8:
1180     case D3DFMT_V16U16:
1181 #endif
1182     case D3DFMT_L6V5U5:
1183     case D3DFMT_X8L8V8U8:
1184     case D3DFMT_Q8W8V8U8:
1185     case D3DFMT_W11V11U10:
1186
1187     /****
1188      * currently hard to support
1189      */
1190     case D3DFMT_UYVY:
1191     case D3DFMT_YUY2:
1192
1193       /* Since we do not support these formats right now, don't pretend to. */
1194       TRACE_(d3d_caps)("[FAILED]\n");
1195       return D3DERR_NOTAVAILABLE;
1196     default:
1197       break;
1198     }
1199
1200     TRACE_(d3d_caps)("[OK]\n");
1201     return D3D_OK;
1202 }
1203
1204 HRESULT  WINAPI  IWineD3DImpl_CheckDeviceFormatConversion(IWineD3D *iface, UINT Adapter, D3DDEVTYPE DeviceType,
1205                                                           WINED3DFORMAT SourceFormat, WINED3DFORMAT TargetFormat) {
1206     IWineD3DImpl *This = (IWineD3DImpl *)iface;
1207
1208     FIXME_(d3d_caps)("(%p)-> (STUB) (Adptr:%d, DevType:(%u,%s), SrcFmt:(%u,%s), TgtFmt:(%u,%s))",
1209           This,
1210           Adapter,
1211           DeviceType, debug_d3ddevicetype(DeviceType),
1212           SourceFormat, debug_d3dformat(SourceFormat),
1213           TargetFormat, debug_d3dformat(TargetFormat));
1214     return D3D_OK;
1215 }
1216
1217 /* Note: d3d8 passes in a pointer to a D3DCAPS8 structure, which is a true
1218       subset of a D3DCAPS9 structure. However, it has to come via a void *
1219       as the d3d8 interface cannot import the d3d9 header                  */
1220 HRESULT WINAPI IWineD3DImpl_GetDeviceCaps(IWineD3D *iface, UINT Adapter, D3DDEVTYPE DeviceType, WINED3DCAPS* pCaps) {
1221
1222     IWineD3DImpl    *This = (IWineD3DImpl *)iface;
1223
1224     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType: %x, pCaps: %p)\n", This, Adapter, DeviceType, pCaps);
1225
1226     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
1227         return D3DERR_INVALIDCALL;
1228     }
1229
1230     /* If we don't know the device settings, go query them now */
1231     if (This->isGLInfoValid == FALSE) {
1232         /* use the desktop window to fill gl caps */
1233         BOOL rc = IWineD3DImpl_FillGLCaps(&This->gl_info, IWineD3DImpl_GetAdapterDisplay(iface, Adapter));
1234
1235         /* We are running off a real context, save the values */
1236         if (rc) This->isGLInfoValid = TRUE;
1237
1238     }
1239
1240     /* ------------------------------------------------
1241        The following fields apply to both d3d8 and d3d9
1242        ------------------------------------------------ */
1243     *pCaps->DeviceType              = (DeviceType == D3DDEVTYPE_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;  /* Not quite true, but use h/w supported by opengl I suppose */
1244     *pCaps->AdapterOrdinal          = Adapter;
1245
1246     *pCaps->Caps                    = 0;
1247     *pCaps->Caps2                   = D3DCAPS2_CANRENDERWINDOWED;
1248     *pCaps->Caps3                   = D3DDEVCAPS_HWTRANSFORMANDLIGHT;
1249     *pCaps->PresentationIntervals   = D3DPRESENT_INTERVAL_IMMEDIATE;
1250
1251     *pCaps->CursorCaps              = 0;
1252
1253
1254     *pCaps->DevCaps                 = D3DDEVCAPS_DRAWPRIMTLVERTEX    |
1255                                       D3DDEVCAPS_HWTRANSFORMANDLIGHT |
1256                                       D3DDEVCAPS_PUREDEVICE          |
1257                                       D3DDEVCAPS_HWRASTERIZATION;
1258
1259
1260     *pCaps->PrimitiveMiscCaps       = D3DPMISCCAPS_CULLCCW               |
1261                                       D3DPMISCCAPS_CULLCW                |
1262                                       D3DPMISCCAPS_COLORWRITEENABLE      |
1263                                       D3DPMISCCAPS_CLIPTLVERTS           |
1264                                       D3DPMISCCAPS_CLIPPLANESCALEDPOINTS |
1265                                       D3DPMISCCAPS_MASKZ;
1266                                /*NOT: D3DPMISCCAPS_TSSARGTEMP*/
1267
1268     *pCaps->RasterCaps              = D3DPRASTERCAPS_DITHER    |
1269                                       D3DPRASTERCAPS_PAT       |
1270                                       D3DPRASTERCAPS_WFOG      |
1271                                       D3DPRASTERCAPS_ZFOG      |
1272                                       D3DPRASTERCAPS_FOGVERTEX |
1273                                       D3DPRASTERCAPS_FOGTABLE  |
1274                                       D3DPRASTERCAPS_FOGRANGE;
1275
1276     if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC)) {
1277       *pCaps->RasterCaps |= D3DPRASTERCAPS_ANISOTROPY    |
1278                             D3DPRASTERCAPS_ZBIAS         |
1279                             D3DPRASTERCAPS_MIPMAPLODBIAS;
1280     }
1281                         /* FIXME Add:
1282                            D3DPRASTERCAPS_COLORPERSPECTIVE
1283                            D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE
1284                            D3DPRASTERCAPS_ANTIALIASEDGES
1285                            D3DPRASTERCAPS_ZBUFFERLESSHSR
1286                            D3DPRASTERCAPS_WBUFFER */
1287
1288     *pCaps->ZCmpCaps = D3DPCMPCAPS_ALWAYS       |
1289                        D3DPCMPCAPS_EQUAL        |
1290                        D3DPCMPCAPS_GREATER      |
1291                        D3DPCMPCAPS_GREATEREQUAL |
1292                        D3DPCMPCAPS_LESS         |
1293                        D3DPCMPCAPS_LESSEQUAL    |
1294                        D3DPCMPCAPS_NEVER        |
1295                        D3DPCMPCAPS_NOTEQUAL;
1296
1297     *pCaps->SrcBlendCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
1298     *pCaps->DestBlendCaps = 0xFFFFFFFF;   /*FIXME: Tidy up later */
1299     *pCaps->AlphaCmpCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
1300
1301     *pCaps->ShadeCaps     = D3DPSHADECAPS_SPECULARGOURAUDRGB |
1302                             D3DPSHADECAPS_COLORGOURAUDRGB;
1303
1304     *pCaps->TextureCaps =  D3DPTEXTURECAPS_ALPHA              |
1305                            D3DPTEXTURECAPS_ALPHAPALETTE       |
1306                            D3DPTEXTURECAPS_VOLUMEMAP          |
1307                            D3DPTEXTURECAPS_MIPMAP             |
1308                            D3DPTEXTURECAPS_PROJECTED          |
1309                            D3DPTEXTURECAPS_PERSPECTIVE        |
1310                            D3DPTEXTURECAPS_VOLUMEMAP_POW2 ;
1311                           /* TODO: add support for NON-POW2 if avaialble
1312
1313                           */
1314     if (This->dxVersion > 8) {
1315         *pCaps->TextureCaps |= D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1316
1317     } else {  /* NONPOW2 isn't accessible by d3d8 yet */
1318         *pCaps->TextureCaps |= D3DPTEXTURECAPS_POW2;
1319     }
1320
1321     if (GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
1322         *pCaps->TextureCaps |= D3DPTEXTURECAPS_CUBEMAP     |
1323                              D3DPTEXTURECAPS_MIPCUBEMAP    |
1324                              D3DPTEXTURECAPS_CUBEMAP_POW2;
1325
1326     }
1327
1328     *pCaps->TextureFilterCaps = D3DPTFILTERCAPS_MAGFLINEAR |
1329                                 D3DPTFILTERCAPS_MAGFPOINT  |
1330                                 D3DPTFILTERCAPS_MINFLINEAR |
1331                                 D3DPTFILTERCAPS_MINFPOINT  |
1332                                 D3DPTFILTERCAPS_MIPFLINEAR |
1333                                 D3DPTFILTERCAPS_MIPFPOINT;
1334
1335     *pCaps->CubeTextureFilterCaps = 0;
1336     *pCaps->VolumeTextureFilterCaps = 0;
1337
1338     *pCaps->TextureAddressCaps =  D3DPTADDRESSCAPS_BORDER |
1339                                   D3DPTADDRESSCAPS_CLAMP  |
1340                                   D3DPTADDRESSCAPS_WRAP;
1341
1342     if (GL_SUPPORT(ARB_TEXTURE_BORDER_CLAMP)) {
1343         *pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_BORDER;
1344     }
1345     if (GL_SUPPORT(ARB_TEXTURE_MIRRORED_REPEAT)) {
1346         *pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRROR;
1347     }
1348     if (GL_SUPPORT(ATI_TEXTURE_MIRROR_ONCE)) {
1349         *pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRRORONCE;
1350     }
1351
1352     *pCaps->VolumeTextureAddressCaps = 0;
1353
1354     *pCaps->LineCaps = D3DLINECAPS_TEXTURE |
1355                        D3DLINECAPS_ZTEST;
1356                       /* FIXME: Add
1357                          D3DLINECAPS_BLEND
1358                          D3DLINECAPS_ALPHACMP
1359                          D3DLINECAPS_FOG */
1360
1361     *pCaps->MaxTextureWidth  = GL_LIMITS(texture_size);
1362     *pCaps->MaxTextureHeight = GL_LIMITS(texture_size);
1363
1364     *pCaps->MaxVolumeExtent = 0;
1365
1366     *pCaps->MaxTextureRepeat = 32768;
1367     *pCaps->MaxTextureAspectRatio = 32768;
1368     *pCaps->MaxVertexW = 1.0;
1369
1370     *pCaps->GuardBandLeft = 0;
1371     *pCaps->GuardBandTop = 0;
1372     *pCaps->GuardBandRight = 0;
1373     *pCaps->GuardBandBottom = 0;
1374
1375     *pCaps->ExtentsAdjust = 0;
1376
1377     *pCaps->StencilCaps =  D3DSTENCILCAPS_DECRSAT |
1378                            D3DSTENCILCAPS_INCRSAT |
1379                            D3DSTENCILCAPS_INVERT  |
1380                            D3DSTENCILCAPS_KEEP    |
1381                            D3DSTENCILCAPS_REPLACE |
1382                            D3DSTENCILCAPS_ZERO;
1383     if (GL_SUPPORT(EXT_STENCIL_WRAP)) {
1384       *pCaps->StencilCaps |= D3DSTENCILCAPS_DECR  |
1385                              D3DSTENCILCAPS_INCR;
1386     }
1387
1388     *pCaps->FVFCaps = D3DFVFCAPS_PSIZE | 0x0008; /* 8 texture coords */
1389
1390     *pCaps->TextureOpCaps =  D3DTEXOPCAPS_ADD         |
1391                              D3DTEXOPCAPS_ADDSIGNED   |
1392                              D3DTEXOPCAPS_ADDSIGNED2X |
1393                              D3DTEXOPCAPS_MODULATE    |
1394                              D3DTEXOPCAPS_MODULATE2X  |
1395                              D3DTEXOPCAPS_MODULATE4X  |
1396                              D3DTEXOPCAPS_SELECTARG1  |
1397                              D3DTEXOPCAPS_SELECTARG2  |
1398                              D3DTEXOPCAPS_DISABLE;
1399 #if defined(GL_VERSION_1_3)
1400     *pCaps->TextureOpCaps |= D3DTEXOPCAPS_DOTPRODUCT3 |
1401                              D3DTEXOPCAPS_SUBTRACT;
1402 #endif
1403     if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE) ||
1404         GL_SUPPORT(EXT_TEXTURE_ENV_COMBINE) ||
1405         GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1406         *pCaps->TextureOpCaps |= D3DTEXOPCAPS_BLENDDIFFUSEALPHA |
1407                                 D3DTEXOPCAPS_BLENDTEXTUREALPHA  |
1408                                 D3DTEXOPCAPS_BLENDFACTORALPHA   |
1409                                 D3DTEXOPCAPS_BLENDCURRENTALPHA  |
1410                                 D3DTEXOPCAPS_LERP;
1411     }
1412     if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
1413         *pCaps->TextureOpCaps |= D3DTEXOPCAPS_ADDSMOOTH             |
1414                                 D3DTEXOPCAPS_MULTIPLYADD            |
1415                                 D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
1416                                 D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1417                                 D3DTEXOPCAPS_BLENDTEXTUREALPHAPM;
1418     }
1419
1420 #if 0
1421     *pCaps->TextureOpCaps |= D3DTEXOPCAPS_BUMPENVMAP;
1422                             /* FIXME: Add
1423                             D3DTEXOPCAPS_BUMPENVMAPLUMINANCE 
1424                             D3DTEXOPCAPS_PREMODULATE */
1425 #endif
1426
1427     *pCaps->MaxTextureBlendStages   = GL_LIMITS(textures);
1428     *pCaps->MaxSimultaneousTextures = GL_LIMITS(textures);
1429     *pCaps->MaxUserClipPlanes       = GL_LIMITS(clipplanes);
1430     *pCaps->MaxActiveLights         = GL_LIMITS(lights);
1431
1432
1433
1434 #if 0 /* TODO: Blends support in drawprim */
1435     *pCaps->MaxVertexBlendMatrices      = GL_LIMITS(blends);
1436 #else
1437     *pCaps->MaxVertexBlendMatrices      = 0;
1438 #endif
1439     *pCaps->MaxVertexBlendMatrixIndex   = 1;
1440
1441     *pCaps->MaxAnisotropy   = GL_LIMITS(anisotropy);
1442     *pCaps->MaxPointSize    = GL_LIMITS(pointsize);
1443
1444
1445     *pCaps->VertexProcessingCaps = D3DVTXPCAPS_DIRECTIONALLIGHTS |
1446                                    D3DVTXPCAPS_MATERIALSOURCE7   |
1447                                    D3DVTXPCAPS_POSITIONALLIGHTS  |
1448                                    D3DVTXPCAPS_LOCALVIEWER |
1449                                    D3DVTXPCAPS_TEXGEN;
1450                                   /* FIXME: Add 
1451                                      D3DVTXPCAPS_TWEENING */
1452
1453     *pCaps->MaxPrimitiveCount   = 0xFFFFFFFF;
1454     *pCaps->MaxVertexIndex      = 0xFFFFFFFF;
1455     *pCaps->MaxStreams          = MAX_STREAMS;
1456     *pCaps->MaxStreamStride     = 1024;
1457
1458     if (((wined3d_settings.vs_mode == VS_HW) && GL_SUPPORT(ARB_VERTEX_PROGRAM)) || (wined3d_settings.vs_mode == VS_SW) || (DeviceType == D3DDEVTYPE_REF)) {
1459       *pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
1460
1461       if (This->gl_info.gl_vendor == VENDOR_MESA ||
1462           This->gl_info.gl_vendor == VENDOR_WINE) {
1463         *pCaps->MaxVertexShaderConst = 95;
1464       } else {
1465         *pCaps->MaxVertexShaderConst = WINED3D_VSHADER_MAX_CONSTANTS;
1466       }
1467     } else {
1468         *pCaps->VertexShaderVersion  = 0;
1469         *pCaps->MaxVertexShaderConst = 0;
1470     }
1471
1472     if ((wined3d_settings.ps_mode == PS_HW) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && (DeviceType != D3DDEVTYPE_REF)) {
1473         *pCaps->PixelShaderVersion    = D3DPS_VERSION(1,4);
1474         *pCaps->PixelShader1xMaxValue = 1.0;
1475     } else {
1476         *pCaps->PixelShaderVersion    = 0;
1477         *pCaps->PixelShader1xMaxValue = 0.0;
1478     }
1479     /* TODO: ARB_FRAGMENT_PROGRAM_100 */
1480
1481     /* ------------------------------------------------
1482        The following fields apply to d3d9 only
1483        ------------------------------------------------ */
1484     if (This->dxVersion > 8) {
1485         GLint max_buffers = 1;
1486         FIXME("Caps support for directx9 is nonexistent at the moment!\n");
1487         *pCaps->DevCaps2                          = 0;
1488         /* TODO: D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES */
1489         *pCaps->MaxNpatchTessellationLevel        = 0;
1490         *pCaps->MasterAdapterOrdinal              = 0;
1491         *pCaps->AdapterOrdinalInGroup             = 0;
1492         *pCaps->NumberOfAdaptersInGroup           = 1;
1493         *pCaps->DeclTypes                         = 0;
1494 #if 0 /*FIXME: Simultaneous render targets*/
1495         GL_MAX_DRAW_BUFFERS_ATI 0x00008824
1496         if (GL_SUPPORT(GL_MAX_DRAW_BUFFERS_ATI)) {
1497             ENTER_GL();
1498             glEnable(GL_MAX_DRAW_BUFFERS_ATI);
1499             glGetIntegerv(GL_MAX_DRAW_BUFFERS_ATI, &max_buffers);
1500             glDisable(GL_MAX_DRAW_BUFFERS_ATI);
1501             LEAVE_GL();
1502         }
1503 #endif
1504         *pCaps->NumSimultaneousRTs                = max_buffers;
1505         *pCaps->StretchRectFilterCaps             = 0;
1506         /* TODO: add
1507            D3DPTFILTERCAPS_MINFPOINT
1508            D3DPTFILTERCAPS_MAGFPOINT
1509            D3DPTFILTERCAPS_MINFLINEAR
1510            D3DPTFILTERCAPS_MAGFLINEAR
1511         */
1512         *pCaps->VS20Caps.Caps                     = 0;
1513         *pCaps->PS20Caps.Caps                     = 0;
1514         *pCaps->VertexTextureFilterCaps           = 0;
1515         *pCaps->MaxVShaderInstructionsExecuted    = 0;
1516         *pCaps->MaxPShaderInstructionsExecuted    = 0;
1517         *pCaps->MaxVertexShader30InstructionSlots = 0;
1518         *pCaps->MaxPixelShader30InstructionSlots  = 0;
1519     }
1520
1521     return D3D_OK;
1522 }
1523
1524
1525 /* Note due to structure differences between dx8 and dx9 D3DPRESENT_PARAMETERS,
1526    and fields being inserted in the middle, a new structure is used in place    */
1527 HRESULT  WINAPI  IWineD3DImpl_CreateDevice(IWineD3D *iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
1528                                            DWORD BehaviourFlags, WINED3DPRESENT_PARAMETERS* pPresentationParameters,
1529                                            IWineD3DDevice** ppReturnedDeviceInterface, IUnknown *parent,
1530                                            D3DCB_CREATEADDITIONALSWAPCHAIN D3DCB_CreateAdditionalSwapChain) {
1531
1532     IWineD3DDeviceImpl *object  = NULL;
1533     IWineD3DImpl       *This    = (IWineD3DImpl *)iface;
1534     IWineD3DSwapChainImpl *swapchain;
1535
1536     /* Validate the adapter number */
1537     if (Adapter >= IWineD3D_GetAdapterCount(iface)) {
1538         return D3DERR_INVALIDCALL;
1539     }
1540
1541     /* Create a WineD3DDevice object */
1542     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DDeviceImpl));
1543     *ppReturnedDeviceInterface = (IWineD3DDevice *)object;
1544     TRACE("Created WineD3DDevice object @ %p \n", object);
1545     if (NULL == object) {
1546       return D3DERR_OUTOFVIDEOMEMORY;
1547     }
1548
1549     /* Set up initial COM information */
1550     object->lpVtbl  = &IWineD3DDevice_Vtbl;
1551     object->ref     = 1;
1552     object->wineD3D = iface;
1553     IWineD3D_AddRef(object->wineD3D);
1554     object->parent  = parent;
1555
1556     TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
1557           hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);
1558     TRACE("(%p)->(DepthStencil:(%u,%s), BackBufferFormat:(%u,%s))\n", This,
1559           *(pPresentationParameters->AutoDepthStencilFormat), debug_d3dformat(*(pPresentationParameters->AutoDepthStencilFormat)),
1560           *(pPresentationParameters->BackBufferFormat), debug_d3dformat(*(pPresentationParameters->BackBufferFormat)));
1561
1562     /* Save the creation parameters */
1563     object->createParms.AdapterOrdinal = Adapter;
1564     object->createParms.DeviceType     = DeviceType;
1565     object->createParms.hFocusWindow   = hFocusWindow;
1566     object->createParms.BehaviorFlags  = BehaviourFlags;
1567
1568     /* Initialize other useful values */
1569     object->adapterNo                    = Adapter;
1570     object->devType                      = DeviceType;
1571
1572     /* FIXME: Use for dx8 code eventually too! */
1573     /* Deliberately no indentation here, as this if will be removed when dx8 support merged in */
1574     if (This->dxVersion > 8) {
1575         TRACE("(%p) : Creating stateblock\n", This);
1576         /* Creating the startup stateBlock - Note Special Case: 0 => Don't fill in yet! */
1577         if (D3D_OK != IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)object,
1578                                          WINED3DSBT_INIT,
1579                                         (IWineD3DStateBlock **)&object->stateBlock,
1580                                         NULL)  || NULL == object->stateBlock) {   /* Note: No parent needed for initial internal stateblock */
1581             WARN("Failed to create stateblock\n");
1582             goto create_device_error;
1583         }
1584         TRACE("(%p) : Created stateblock (%p) \n", This, object->stateBlock);
1585         object->updateStateBlock = object->stateBlock;
1586         IWineD3DStateBlock_AddRef((IWineD3DStateBlock*)object->updateStateBlock);
1587         /* Setup surfaces for the backbuffer, frontbuffer and depthstencil buffer */
1588
1589         /* Setup some defaults for creating the implicite swapchain */
1590         ENTER_GL();
1591         IWineD3DImpl_FillGLCaps(&This->gl_info, IWineD3DImpl_GetAdapterDisplay(iface, Adapter));
1592         LEAVE_GL();
1593
1594         /* Setup the implicit swapchain */
1595         TRACE("Creating implicit swapchain\n");
1596         if (D3D_OK != D3DCB_CreateAdditionalSwapChain((IUnknown *) object->parent, pPresentationParameters, (IWineD3DSwapChain **)&swapchain) || swapchain == NULL) {
1597             WARN("Failed to create implicite swapchain\n");
1598             goto create_device_error;
1599         }
1600
1601         object->renderTarget = swapchain->backBuffer;
1602         IWineD3DSurface_AddRef(object->renderTarget);
1603         /* Depth Stencil support */
1604         object->stencilBufferTarget = object->depthStencilBuffer;
1605         if (NULL != object->stencilBufferTarget) {
1606             IWineD3DSurface_AddRef(object->stencilBufferTarget);
1607         }
1608
1609         /* Set up some starting GL setup */
1610         ENTER_GL();
1611         /*
1612         * Initialize openGL extension related variables
1613         *  with Default values
1614         */
1615
1616         This->isGLInfoValid = IWineD3DImpl_FillGLCaps(&This->gl_info, swapchain->display);
1617         /* Setup all the devices defaults */
1618         IWineD3DStateBlock_InitStartupStateBlock((IWineD3DStateBlock *)object->stateBlock);
1619 #if 0
1620         IWineD3DImpl_CheckGraphicsMemory();
1621 #endif
1622         LEAVE_GL();
1623
1624         { /* Set a default viewport */
1625             D3DVIEWPORT9 vp;
1626             vp.X      = 0;
1627             vp.Y      = 0;
1628             vp.Width  = *(pPresentationParameters->BackBufferWidth);
1629             vp.Height = *(pPresentationParameters->BackBufferHeight);
1630             vp.MinZ   = 0.0f;
1631             vp.MaxZ   = 1.0f;
1632             IWineD3DDevice_SetViewport((IWineD3DDevice *)object, &vp);
1633         }
1634
1635
1636         /* Initialize the current view state */
1637         object->modelview_valid = 1;
1638         object->proj_valid = 0;
1639         object->view_ident = 1;
1640         object->last_was_rhw = 0;
1641         glGetIntegerv(GL_MAX_LIGHTS, &object->maxConcurrentLights);
1642         TRACE("(%p,%d) All defaults now set up, leaving CreateDevice with %p\n", This, Adapter, object);
1643
1644         /* Clear the screen */
1645         IWineD3DDevice_Clear((IWineD3DDevice *) object, 0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER|D3DCLEAR_TARGET, 0x00, 1.0, 0);
1646
1647     } else { /* End of FIXME: remove when dx8 merged in */
1648
1649         FIXME("(%p) Incomplete stub for d3d8\n", This);
1650
1651     }
1652
1653     return D3D_OK;
1654 create_device_error:
1655     if (object->updateStateBlock != NULL) {
1656         IWineD3DStateBlock_Release((IWineD3DStateBlock *)object->updateStateBlock);
1657         object->updateStateBlock = NULL;
1658     }
1659     if (object->stateBlock != NULL) {
1660         IWineD3DStateBlock_Release((IWineD3DStateBlock *)object->stateBlock);
1661         object->stateBlock = NULL;
1662     }
1663     if (object->renderTarget != NULL) {
1664         IWineD3DSurface_Release(object->renderTarget);
1665         object->renderTarget = NULL;
1666     }
1667     if (object->stencilBufferTarget != NULL) {
1668         IWineD3DSurface_Release(object->stencilBufferTarget);
1669         object->stencilBufferTarget = NULL;
1670     }
1671     if (object->stencilBufferTarget != NULL) {
1672         IWineD3DSurface_Release(object->stencilBufferTarget);
1673         object->stencilBufferTarget = NULL;
1674     }
1675     if (swapchain != NULL) {
1676         IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1677         swapchain = NULL;
1678     }
1679     HeapFree(GetProcessHeap(), 0, object);
1680     *ppReturnedDeviceInterface = NULL;
1681     return D3DERR_INVALIDCALL;
1682
1683 }
1684
1685 HRESULT WINAPI IWineD3DImpl_GetParent(IWineD3D *iface, IUnknown **pParent) {
1686     IWineD3DImpl *This = (IWineD3DImpl *)iface;
1687     IUnknown_AddRef(This->parent);
1688     *pParent = This->parent;
1689     return D3D_OK;
1690 }
1691
1692 /**********************************************************
1693  * IWineD3D VTbl follows
1694  **********************************************************/
1695
1696 const IWineD3DVtbl IWineD3D_Vtbl =
1697 {
1698     /* IUnknown */
1699     IWineD3DImpl_QueryInterface,
1700     IWineD3DImpl_AddRef,
1701     IWineD3DImpl_Release,
1702     /* IWineD3D */
1703     IWineD3DImpl_GetParent,
1704     IWineD3DImpl_GetAdapterCount,
1705     IWineD3DImpl_RegisterSoftwareDevice,
1706     IWineD3DImpl_GetAdapterMonitor,
1707     IWineD3DImpl_GetAdapterModeCount,
1708     IWineD3DImpl_EnumAdapterModes,
1709     IWineD3DImpl_GetAdapterDisplayMode,
1710     IWineD3DImpl_GetAdapterIdentifier,
1711     IWineD3DImpl_CheckDeviceMultiSampleType,
1712     IWineD3DImpl_CheckDepthStencilMatch,
1713     IWineD3DImpl_CheckDeviceType,
1714     IWineD3DImpl_CheckDeviceFormat,
1715     IWineD3DImpl_CheckDeviceFormatConversion,
1716     IWineD3DImpl_GetDeviceCaps,
1717     IWineD3DImpl_CreateDevice
1718 };