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