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