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