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