ddraw: Move clipper handling to ddraw.
[wine] / dlls / wined3d / directx.c
1 /*
2  * Copyright 2002-2004 Jason Edmeades
3  * Copyright 2003-2004 Raphael Junqueira
4  * Copyright 2004 Christian Costa
5  * Copyright 2005 Oliver Stieber
6  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
7  * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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 #include "config.h"
25 #include <stdio.h>
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
30
31 #define WINE_DEFAULT_VIDMEM (64 * 1024 * 1024)
32
33 /* The driver names reflect the lowest GPU supported
34  * by a certain driver, so DRIVER_AMD_R300 supports
35  * R3xx, R4xx and R5xx GPUs. */
36 enum wined3d_display_driver
37 {
38     DRIVER_AMD_RAGE_128PRO,
39     DRIVER_AMD_R100,
40     DRIVER_AMD_R300,
41     DRIVER_AMD_R600,
42     DRIVER_INTEL_GMA800,
43     DRIVER_INTEL_GMA900,
44     DRIVER_INTEL_GMA950,
45     DRIVER_INTEL_GMA3000,
46     DRIVER_NVIDIA_TNT,
47     DRIVER_NVIDIA_GEFORCE2MX,
48     DRIVER_NVIDIA_GEFORCEFX,
49     DRIVER_NVIDIA_GEFORCE6,
50     DRIVER_UNKNOWN
51 };
52
53 enum wined3d_driver_model
54 {
55     DRIVER_MODEL_WIN9X,
56     DRIVER_MODEL_NT40,
57     DRIVER_MODEL_NT5X,
58     DRIVER_MODEL_NT6X
59 };
60
61 enum wined3d_gl_vendor
62 {
63     GL_VENDOR_UNKNOWN,
64     GL_VENDOR_APPLE,
65     GL_VENDOR_FGLRX,
66     GL_VENDOR_INTEL,
67     GL_VENDOR_MESA,
68     GL_VENDOR_NVIDIA,
69 };
70
71 /* The d3d device ID */
72 static const GUID IID_D3DDEVICE_D3DUID = { 0xaeb2cdd4, 0x6e41, 0x43ea, { 0x94,0x1c,0x83,0x61,0xcc,0x76,0x07,0x81 } };
73
74 /* Extension detection */
75 static const struct
76 {
77     const char *extension_string;
78     enum wined3d_gl_extension extension;
79     DWORD version;
80 }
81 EXTENSION_MAP[] =
82 {
83     /* APPLE */
84     {"GL_APPLE_client_storage",             APPLE_CLIENT_STORAGE,           0                           },
85     {"GL_APPLE_fence",                      APPLE_FENCE,                    0                           },
86     {"GL_APPLE_float_pixels",               APPLE_FLOAT_PIXELS,             0                           },
87     {"GL_APPLE_flush_buffer_range",         APPLE_FLUSH_BUFFER_RANGE,       0                           },
88     {"GL_APPLE_flush_render",               APPLE_FLUSH_RENDER,             0                           },
89     {"GL_APPLE_ycbcr_422",                  APPLE_YCBCR_422,                0                           },
90
91     /* ARB */
92     {"GL_ARB_color_buffer_float",           ARB_COLOR_BUFFER_FLOAT,         0                           },
93     {"GL_ARB_depth_buffer_float",           ARB_DEPTH_BUFFER_FLOAT,         0                           },
94     {"GL_ARB_depth_clamp",                  ARB_DEPTH_CLAMP,                0                           },
95     {"GL_ARB_depth_texture",                ARB_DEPTH_TEXTURE,              0                           },
96     {"GL_ARB_draw_buffers",                 ARB_DRAW_BUFFERS,               0                           },
97     {"GL_ARB_draw_elements_base_vertex",    ARB_DRAW_ELEMENTS_BASE_VERTEX,  0                           },
98     {"GL_ARB_fragment_program",             ARB_FRAGMENT_PROGRAM,           0                           },
99     {"GL_ARB_fragment_shader",              ARB_FRAGMENT_SHADER,            0                           },
100     {"GL_ARB_framebuffer_object",           ARB_FRAMEBUFFER_OBJECT,         0                           },
101     {"GL_ARB_geometry_shader4",             ARB_GEOMETRY_SHADER4,           0                           },
102     {"GL_ARB_half_float_pixel",             ARB_HALF_FLOAT_PIXEL,           0                           },
103     {"GL_ARB_half_float_vertex",            ARB_HALF_FLOAT_VERTEX,          0                           },
104     {"GL_ARB_map_buffer_alignment",         ARB_MAP_BUFFER_ALIGNMENT,       0                           },
105     {"GL_ARB_map_buffer_range",             ARB_MAP_BUFFER_RANGE,           0                           },
106     {"GL_ARB_multisample",                  ARB_MULTISAMPLE,                0                           }, /* needs GLX_ARB_MULTISAMPLE as well */
107     {"GL_ARB_multitexture",                 ARB_MULTITEXTURE,               0                           },
108     {"GL_ARB_occlusion_query",              ARB_OCCLUSION_QUERY,            0                           },
109     {"GL_ARB_pixel_buffer_object",          ARB_PIXEL_BUFFER_OBJECT,        0                           },
110     {"GL_ARB_point_parameters",             ARB_POINT_PARAMETERS,           0                           },
111     {"GL_ARB_point_sprite",                 ARB_POINT_SPRITE,               0                           },
112     {"GL_ARB_provoking_vertex",             ARB_PROVOKING_VERTEX,           0                           },
113     {"GL_ARB_shader_objects",               ARB_SHADER_OBJECTS,             0                           },
114     {"GL_ARB_shader_texture_lod",           ARB_SHADER_TEXTURE_LOD,         0                           },
115     {"GL_ARB_shading_language_100",         ARB_SHADING_LANGUAGE_100,       0                           },
116     {"GL_ARB_shadow",                       ARB_SHADOW,                     0                           },
117     {"GL_ARB_sync",                         ARB_SYNC,                       0                           },
118     {"GL_ARB_texture_border_clamp",         ARB_TEXTURE_BORDER_CLAMP,       0                           },
119     {"GL_ARB_texture_compression",          ARB_TEXTURE_COMPRESSION,        0                           },
120     {"GL_ARB_texture_compression_rgtc",     ARB_TEXTURE_COMPRESSION_RGTC,   0                           },
121     {"GL_ARB_texture_cube_map",             ARB_TEXTURE_CUBE_MAP,           0                           },
122     {"GL_ARB_texture_env_add",              ARB_TEXTURE_ENV_ADD,            0                           },
123     {"GL_ARB_texture_env_combine",          ARB_TEXTURE_ENV_COMBINE,        0                           },
124     {"GL_ARB_texture_env_dot3",             ARB_TEXTURE_ENV_DOT3,           0                           },
125     {"GL_ARB_texture_float",                ARB_TEXTURE_FLOAT,              0                           },
126     {"GL_ARB_texture_mirrored_repeat",      ARB_TEXTURE_MIRRORED_REPEAT,    0                           },
127     {"GL_ARB_texture_non_power_of_two",     ARB_TEXTURE_NON_POWER_OF_TWO,   MAKEDWORD_VERSION(2, 0)     },
128     {"GL_ARB_texture_rectangle",            ARB_TEXTURE_RECTANGLE,          0                           },
129     {"GL_ARB_texture_rg",                   ARB_TEXTURE_RG,                 0                           },
130     {"GL_ARB_vertex_array_bgra",            ARB_VERTEX_ARRAY_BGRA,          0                           },
131     {"GL_ARB_vertex_blend",                 ARB_VERTEX_BLEND,               0                           },
132     {"GL_ARB_vertex_buffer_object",         ARB_VERTEX_BUFFER_OBJECT,       0                           },
133     {"GL_ARB_vertex_program",               ARB_VERTEX_PROGRAM,             0                           },
134     {"GL_ARB_vertex_shader",                ARB_VERTEX_SHADER,              0                           },
135
136     /* ATI */
137     {"GL_ATI_fragment_shader",              ATI_FRAGMENT_SHADER,            0                           },
138     {"GL_ATI_separate_stencil",             ATI_SEPARATE_STENCIL,           0                           },
139     {"GL_ATI_texture_compression_3dc",      ATI_TEXTURE_COMPRESSION_3DC,    0                           },
140     {"GL_ATI_texture_env_combine3",         ATI_TEXTURE_ENV_COMBINE3,       0                           },
141     {"GL_ATI_texture_mirror_once",          ATI_TEXTURE_MIRROR_ONCE,        0                           },
142
143     /* EXT */
144     {"GL_EXT_blend_color",                  EXT_BLEND_COLOR,                0                           },
145     {"GL_EXT_blend_equation_separate",      EXT_BLEND_EQUATION_SEPARATE,    0                           },
146     {"GL_EXT_blend_func_separate",          EXT_BLEND_FUNC_SEPARATE,        0                           },
147     {"GL_EXT_blend_minmax",                 EXT_BLEND_MINMAX,               0                           },
148     {"GL_EXT_blend_subtract",               EXT_BLEND_SUBTRACT,             0                           },
149     {"GL_EXT_depth_bounds_test",            EXT_DEPTH_BOUNDS_TEST,          0                           },
150     {"GL_EXT_draw_buffers2",                EXT_DRAW_BUFFERS2,              0                           },
151     {"GL_EXT_fog_coord",                    EXT_FOG_COORD,                  0                           },
152     {"GL_EXT_framebuffer_blit",             EXT_FRAMEBUFFER_BLIT,           0                           },
153     {"GL_EXT_framebuffer_multisample",      EXT_FRAMEBUFFER_MULTISAMPLE,    0                           },
154     {"GL_EXT_framebuffer_object",           EXT_FRAMEBUFFER_OBJECT,         0                           },
155     {"GL_EXT_gpu_program_parameters",       EXT_GPU_PROGRAM_PARAMETERS,     0                           },
156     {"GL_EXT_gpu_shader4",                  EXT_GPU_SHADER4,                0                           },
157     {"GL_EXT_packed_depth_stencil",         EXT_PACKED_DEPTH_STENCIL,       0                           },
158     {"GL_EXT_paletted_texture",             EXT_PALETTED_TEXTURE,           0                           },
159     {"GL_EXT_point_parameters",             EXT_POINT_PARAMETERS,           0                           },
160     {"GL_EXT_provoking_vertex",             EXT_PROVOKING_VERTEX,           0                           },
161     {"GL_EXT_secondary_color",              EXT_SECONDARY_COLOR,            0                           },
162     {"GL_EXT_stencil_two_side",             EXT_STENCIL_TWO_SIDE,           0                           },
163     {"GL_EXT_stencil_wrap",                 EXT_STENCIL_WRAP,               0                           },
164     {"GL_EXT_texture3D",                    EXT_TEXTURE3D,                  MAKEDWORD_VERSION(1, 2)     },
165     {"GL_EXT_texture_compression_rgtc",     EXT_TEXTURE_COMPRESSION_RGTC,   0                           },
166     {"GL_EXT_texture_compression_s3tc",     EXT_TEXTURE_COMPRESSION_S3TC,   0                           },
167     {"GL_EXT_texture_env_add",              EXT_TEXTURE_ENV_ADD,            0                           },
168     {"GL_EXT_texture_env_combine",          EXT_TEXTURE_ENV_COMBINE,        0                           },
169     {"GL_EXT_texture_env_dot3",             EXT_TEXTURE_ENV_DOT3,           0                           },
170     {"GL_EXT_texture_filter_anisotropic",   EXT_TEXTURE_FILTER_ANISOTROPIC, 0                           },
171     {"GL_EXT_texture_lod_bias",             EXT_TEXTURE_LOD_BIAS,           0                           },
172     {"GL_EXT_texture_sRGB",                 EXT_TEXTURE_SRGB,               0                           },
173     {"GL_EXT_texture_sRGB_decode",          EXT_TEXTURE_SRGB_DECODE,        0                           },
174     {"GL_EXT_vertex_array_bgra",            EXT_VERTEX_ARRAY_BGRA,          0                           },
175
176     /* NV */
177     {"GL_NV_depth_clamp",                   NV_DEPTH_CLAMP,                 0                           },
178     {"GL_NV_fence",                         NV_FENCE,                       0                           },
179     {"GL_NV_fog_distance",                  NV_FOG_DISTANCE,                0                           },
180     {"GL_NV_fragment_program",              NV_FRAGMENT_PROGRAM,            0                           },
181     {"GL_NV_fragment_program2",             NV_FRAGMENT_PROGRAM2,           0                           },
182     {"GL_NV_fragment_program_option",       NV_FRAGMENT_PROGRAM_OPTION,     0                           },
183     {"GL_NV_half_float",                    NV_HALF_FLOAT,                  0                           },
184     {"GL_NV_light_max_exponent",            NV_LIGHT_MAX_EXPONENT,          0                           },
185     {"GL_NV_point_sprite",                  NV_POINT_SPRITE,                0                           },
186     {"GL_NV_register_combiners",            NV_REGISTER_COMBINERS,          0                           },
187     {"GL_NV_register_combiners2",           NV_REGISTER_COMBINERS2,         0                           },
188     {"GL_NV_texgen_reflection",             NV_TEXGEN_REFLECTION,           0                           },
189     {"GL_NV_texture_env_combine4",          NV_TEXTURE_ENV_COMBINE4,        0                           },
190     {"GL_NV_texture_shader",                NV_TEXTURE_SHADER,              0                           },
191     {"GL_NV_texture_shader2",               NV_TEXTURE_SHADER2,             0                           },
192     {"GL_NV_vertex_program",                NV_VERTEX_PROGRAM,              0                           },
193     {"GL_NV_vertex_program1_1",             NV_VERTEX_PROGRAM1_1,           0                           },
194     {"GL_NV_vertex_program2",               NV_VERTEX_PROGRAM2,             0                           },
195     {"GL_NV_vertex_program2_option",        NV_VERTEX_PROGRAM2_OPTION,      0                           },
196     {"GL_NV_vertex_program3",               NV_VERTEX_PROGRAM3,             0                           },
197
198     /* SGI */
199     {"GL_SGIS_generate_mipmap",             SGIS_GENERATE_MIPMAP,           0                           },
200 };
201
202 /**********************************************************
203  * Utility functions follow
204  **********************************************************/
205
206 const struct min_lookup minMipLookup[] =
207 {
208     /* NONE         POINT                       LINEAR */
209     {{GL_NEAREST,   GL_NEAREST,                 GL_NEAREST}},               /* NONE */
210     {{GL_NEAREST,   GL_NEAREST_MIPMAP_NEAREST,  GL_NEAREST_MIPMAP_LINEAR}}, /* POINT*/
211     {{GL_LINEAR,    GL_LINEAR_MIPMAP_NEAREST,   GL_LINEAR_MIPMAP_LINEAR}},  /* LINEAR */
212 };
213
214 const struct min_lookup minMipLookup_noFilter[] =
215 {
216     /* NONE         POINT                       LINEAR */
217     {{GL_NEAREST,   GL_NEAREST,                 GL_NEAREST}},               /* NONE */
218     {{GL_NEAREST,   GL_NEAREST,                 GL_NEAREST}},               /* POINT */
219     {{GL_NEAREST,   GL_NEAREST,                 GL_NEAREST}},               /* LINEAR */
220 };
221
222 const struct min_lookup minMipLookup_noMip[] =
223 {
224     /* NONE         POINT                       LINEAR */
225     {{GL_NEAREST,   GL_NEAREST,                 GL_NEAREST}},               /* NONE */
226     {{GL_NEAREST,   GL_NEAREST,                 GL_NEAREST}},               /* POINT */
227     {{GL_LINEAR,    GL_LINEAR,                  GL_LINEAR }},               /* LINEAR */
228 };
229
230 const GLenum magLookup[] =
231 {
232     /* NONE     POINT       LINEAR */
233     GL_NEAREST, GL_NEAREST, GL_LINEAR,
234 };
235
236 const GLenum magLookup_noFilter[] =
237 {
238     /* NONE     POINT       LINEAR */
239     GL_NEAREST, GL_NEAREST, GL_NEAREST,
240 };
241
242 /* drawStridedSlow attributes */
243 glAttribFunc position_funcs[WINED3D_FFP_EMIT_COUNT];
244 glAttribFunc diffuse_funcs[WINED3D_FFP_EMIT_COUNT];
245 glAttribFunc specular_func_3ubv;
246 glAttribFunc specular_funcs[WINED3D_FFP_EMIT_COUNT];
247 glAttribFunc normal_funcs[WINED3D_FFP_EMIT_COUNT];
248 glMultiTexCoordFunc multi_texcoord_funcs[WINED3D_FFP_EMIT_COUNT];
249
250 /**
251  * Note: GL seems to trap if GetDeviceCaps is called before any HWND's created,
252  * i.e., there is no GL Context - Get a default rendering context to enable the
253  * function query some info from GL.
254  */
255
256 struct wined3d_fake_gl_ctx
257 {
258     HDC dc;
259     HWND wnd;
260     HGLRC gl_ctx;
261     HDC restore_dc;
262     HGLRC restore_gl_ctx;
263 };
264
265 static void WineD3D_ReleaseFakeGLContext(const struct wined3d_fake_gl_ctx *ctx)
266 {
267     TRACE_(d3d_caps)("Destroying fake GL context.\n");
268
269     if (!pwglMakeCurrent(NULL, NULL))
270     {
271         ERR_(d3d_caps)("Failed to disable fake GL context.\n");
272     }
273
274     if (!pwglDeleteContext(ctx->gl_ctx))
275     {
276         DWORD err = GetLastError();
277         ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx->gl_ctx, err);
278     }
279
280     ReleaseDC(ctx->wnd, ctx->dc);
281     DestroyWindow(ctx->wnd);
282
283     if (ctx->restore_gl_ctx && !pwglMakeCurrent(ctx->restore_dc, ctx->restore_gl_ctx))
284     {
285         ERR_(d3d_caps)("Failed to restore previous GL context.\n");
286     }
287 }
288
289 /* Do not call while under the GL lock. */
290 static BOOL WineD3D_CreateFakeGLContext(struct wined3d_fake_gl_ctx *ctx)
291 {
292     PIXELFORMATDESCRIPTOR pfd;
293     int iPixelFormat;
294
295     TRACE("getting context...\n");
296
297     ctx->restore_dc = pwglGetCurrentDC();
298     ctx->restore_gl_ctx = pwglGetCurrentContext();
299
300     /* We need a fake window as a hdc retrieved using GetDC(0) can't be used for much GL purposes. */
301     ctx->wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
302             WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL);
303     if (!ctx->wnd)
304     {
305         ERR_(d3d_caps)("Failed to create a window.\n");
306         goto fail;
307     }
308
309     ctx->dc = GetDC(ctx->wnd);
310     if (!ctx->dc)
311     {
312         ERR_(d3d_caps)("Failed to get a DC.\n");
313         goto fail;
314     }
315
316     /* PixelFormat selection */
317     ZeroMemory(&pfd, sizeof(pfd));
318     pfd.nSize = sizeof(pfd);
319     pfd.nVersion = 1;
320     pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW; /* PFD_GENERIC_ACCELERATED */
321     pfd.iPixelType = PFD_TYPE_RGBA;
322     pfd.cColorBits = 32;
323     pfd.iLayerType = PFD_MAIN_PLANE;
324
325     iPixelFormat = ChoosePixelFormat(ctx->dc, &pfd);
326     if (!iPixelFormat)
327     {
328         /* If this happens something is very wrong as ChoosePixelFormat barely fails. */
329         ERR_(d3d_caps)("Can't find a suitable iPixelFormat.\n");
330         goto fail;
331     }
332     DescribePixelFormat(ctx->dc, iPixelFormat, sizeof(pfd), &pfd);
333     SetPixelFormat(ctx->dc, iPixelFormat, &pfd);
334
335     /* Create a GL context. */
336     ctx->gl_ctx = pwglCreateContext(ctx->dc);
337     if (!ctx->gl_ctx)
338     {
339         WARN_(d3d_caps)("Error creating default context for capabilities initialization.\n");
340         goto fail;
341     }
342
343     /* Make it the current GL context. */
344     if (!pwglMakeCurrent(ctx->dc, ctx->gl_ctx))
345     {
346         ERR_(d3d_caps)("Failed to make fake GL context current.\n");
347         goto fail;
348     }
349
350     return TRUE;
351
352 fail:
353     if (ctx->gl_ctx) pwglDeleteContext(ctx->gl_ctx);
354     ctx->gl_ctx = NULL;
355     if (ctx->dc) ReleaseDC(ctx->wnd, ctx->dc);
356     ctx->dc = NULL;
357     if (ctx->wnd) DestroyWindow(ctx->wnd);
358     ctx->wnd = NULL;
359     if (ctx->restore_gl_ctx && !pwglMakeCurrent(ctx->restore_dc, ctx->restore_gl_ctx))
360     {
361         ERR_(d3d_caps)("Failed to restore previous GL context.\n");
362     }
363
364     return FALSE;
365 }
366
367 /* Adjust the amount of used texture memory */
368 unsigned int adapter_adjust_memory(struct wined3d_adapter *adapter, int amount)
369 {
370     adapter->UsedTextureRam += amount;
371     TRACE("Adjusted adapter memory by %d to %d.\n", amount, adapter->UsedTextureRam);
372     return adapter->UsedTextureRam;
373 }
374
375 static void wined3d_adapter_cleanup(struct wined3d_adapter *adapter)
376 {
377     HeapFree(GetProcessHeap(), 0, adapter->gl_info.formats);
378     HeapFree(GetProcessHeap(), 0, adapter->cfgs);
379 }
380
381 ULONG CDECL wined3d_incref(struct wined3d *wined3d)
382 {
383     ULONG refcount = InterlockedIncrement(&wined3d->ref);
384
385     TRACE("%p increasing refcount to %u.\n", wined3d, refcount);
386
387     return refcount;
388 }
389
390 ULONG CDECL wined3d_decref(struct wined3d *wined3d)
391 {
392     ULONG refcount = InterlockedDecrement(&wined3d->ref);
393
394     TRACE("%p decreasing refcount to %u.\n", wined3d, refcount);
395
396     if (!refcount)
397     {
398         unsigned int i;
399
400         for (i = 0; i < wined3d->adapter_count; ++i)
401         {
402             wined3d_adapter_cleanup(&wined3d->adapters[i]);
403         }
404         HeapFree(GetProcessHeap(), 0, wined3d);
405     }
406
407     return refcount;
408 }
409
410 /* GL locking is done by the caller */
411 static inline BOOL test_arb_vs_offset_limit(const struct wined3d_gl_info *gl_info)
412 {
413     GLuint prog;
414     BOOL ret = FALSE;
415     const char *testcode =
416         "!!ARBvp1.0\n"
417         "PARAM C[66] = { program.env[0..65] };\n"
418         "ADDRESS A0;"
419         "PARAM zero = {0.0, 0.0, 0.0, 0.0};\n"
420         "ARL A0.x, zero.x;\n"
421         "MOV result.position, C[A0.x + 65];\n"
422         "END\n";
423
424     while(glGetError());
425     GL_EXTCALL(glGenProgramsARB(1, &prog));
426     if(!prog) {
427         ERR("Failed to create an ARB offset limit test program\n");
428     }
429     GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prog));
430     GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
431                                   strlen(testcode), testcode));
432     if (glGetError())
433     {
434         TRACE("OpenGL implementation does not allow indirect addressing offsets > 63\n");
435         TRACE("error: %s\n", debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
436         ret = TRUE;
437     } else TRACE("OpenGL implementation allows offsets > 63\n");
438
439     GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, 0));
440     GL_EXTCALL(glDeleteProgramsARB(1, &prog));
441     checkGLcall("ARB vp offset limit test cleanup");
442
443     return ret;
444 }
445
446 static DWORD ver_for_ext(enum wined3d_gl_extension ext)
447 {
448     unsigned int i;
449     for (i = 0; i < (sizeof(EXTENSION_MAP) / sizeof(*EXTENSION_MAP)); ++i) {
450         if(EXTENSION_MAP[i].extension == ext) {
451             return EXTENSION_MAP[i].version;
452         }
453     }
454     return 0;
455 }
456
457 static BOOL match_amd_r300_to_500(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
458         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
459 {
460     if (card_vendor != HW_VENDOR_AMD) return FALSE;
461     if (device == CARD_AMD_RADEON_9500) return TRUE;
462     if (device == CARD_AMD_RADEON_X700) return TRUE;
463     if (device == CARD_AMD_RADEON_X1600) return TRUE;
464     return FALSE;
465 }
466
467 static BOOL match_geforce5(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
468         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
469 {
470     if (card_vendor == HW_VENDOR_NVIDIA)
471     {
472         if (device == CARD_NVIDIA_GEFORCEFX_5200 ||
473             device == CARD_NVIDIA_GEFORCEFX_5600 ||
474             device == CARD_NVIDIA_GEFORCEFX_5800)
475         {
476             return TRUE;
477         }
478     }
479     return FALSE;
480 }
481
482 static BOOL match_apple(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
483         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
484 {
485     /* MacOS has various specialities in the extensions it advertises. Some have to be loaded from
486      * the opengl 1.2+ core, while other extensions are advertised, but software emulated. So try to
487      * detect the Apple OpenGL implementation to apply some extension fixups afterwards.
488      *
489      * Detecting this isn't really easy. The vendor string doesn't mention Apple. Compile-time checks
490      * aren't sufficient either because a Linux binary may display on a macos X server via remote X11.
491      * So try to detect the GL implementation by looking at certain Apple extensions. Some extensions
492      * like client storage might be supported on other implementations too, but GL_APPLE_flush_render
493      * is specific to the Mac OS X window management, and GL_APPLE_ycbcr_422 is QuickTime specific. So
494      * the chance that other implementations support them is rather small since Win32 QuickTime uses
495      * DirectDraw, not OpenGL.
496      *
497      * This test has been moved into wined3d_guess_gl_vendor()
498      */
499     if (gl_vendor == GL_VENDOR_APPLE)
500     {
501         return TRUE;
502     }
503     return FALSE;
504 }
505
506 /* Context activation is done by the caller. */
507 static void test_pbo_functionality(struct wined3d_gl_info *gl_info)
508 {
509     /* Some OpenGL implementations, namely Apple's Geforce 8 driver, advertises PBOs,
510      * but glTexSubImage from a PBO fails miserably, with the first line repeated over
511      * all the texture. This function detects this bug by its symptom and disables PBOs
512      * if the test fails.
513      *
514      * The test uploads a 4x4 texture via the PBO in the "native" format GL_BGRA,
515      * GL_UNSIGNED_INT_8_8_8_8_REV. This format triggers the bug, and it is what we use
516      * for D3DFMT_A8R8G8B8. Then the texture is read back without any PBO and the data
517      * read back is compared to the original. If they are equal PBOs are assumed to work,
518      * otherwise the PBO extension is disabled. */
519     GLuint texture, pbo;
520     static const unsigned int pattern[] =
521     {
522         0x00000000, 0x000000ff, 0x0000ff00, 0x40ff0000,
523         0x80ffffff, 0x40ffff00, 0x00ff00ff, 0x0000ffff,
524         0x00ffff00, 0x00ff00ff, 0x0000ffff, 0x000000ff,
525         0x80ff00ff, 0x0000ffff, 0x00ff00ff, 0x40ff00ff
526     };
527     unsigned int check[sizeof(pattern) / sizeof(pattern[0])];
528
529     /* No PBO -> No point in testing them. */
530     if (!gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]) return;
531
532     ENTER_GL();
533
534     while (glGetError());
535     glGenTextures(1, &texture);
536     glBindTexture(GL_TEXTURE_2D, texture);
537
538     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
539     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 4, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
540     checkGLcall("Specifying the PBO test texture");
541
542     GL_EXTCALL(glGenBuffersARB(1, &pbo));
543     GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo));
544     GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, sizeof(pattern), pattern, GL_STREAM_DRAW_ARB));
545     checkGLcall("Specifying the PBO test pbo");
546
547     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
548     checkGLcall("Loading the PBO test texture");
549
550     GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
551     LEAVE_GL();
552
553     wglFinish(); /* just to be sure */
554
555     memset(check, 0, sizeof(check));
556     ENTER_GL();
557     glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, check);
558     checkGLcall("Reading back the PBO test texture");
559
560     glDeleteTextures(1, &texture);
561     GL_EXTCALL(glDeleteBuffersARB(1, &pbo));
562     checkGLcall("PBO test cleanup");
563
564     LEAVE_GL();
565
566     if (memcmp(check, pattern, sizeof(check)))
567     {
568         WARN_(d3d_caps)("PBO test failed, read back data doesn't match original.\n");
569         WARN_(d3d_caps)("Disabling PBOs. This may result in slower performance.\n");
570         gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] = FALSE;
571     }
572     else
573     {
574         TRACE_(d3d_caps)("PBO test successful.\n");
575     }
576 }
577
578 static BOOL match_apple_intel(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
579         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
580 {
581     return (card_vendor == HW_VENDOR_INTEL) && (gl_vendor == GL_VENDOR_APPLE);
582 }
583
584 static BOOL match_apple_nonr500ati(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
585         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
586 {
587     if (gl_vendor != GL_VENDOR_APPLE) return FALSE;
588     if (card_vendor != HW_VENDOR_AMD) return FALSE;
589     if (device == CARD_AMD_RADEON_X1600) return FALSE;
590     return TRUE;
591 }
592
593 static BOOL match_dx10_capable(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
594         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
595 {
596     /* DX9 cards support 40 single float varyings in hardware, most drivers report 32. ATI misreports
597      * 44 varyings. So assume that if we have more than 44 varyings we have a dx10 card.
598      * This detection is for the gl_ClipPos varying quirk. If a d3d9 card really supports more than 44
599      * varyings and we subtract one in dx9 shaders its not going to hurt us because the dx9 limit is
600      * hardcoded
601      *
602      * dx10 cards usually have 64 varyings */
603     return gl_info->limits.glsl_varyings > 44;
604 }
605
606 static BOOL match_not_dx10_capable(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
607         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
608 {
609     return !match_dx10_capable(gl_info, gl_renderer, gl_vendor, card_vendor, device);
610 }
611
612 /* A GL context is provided by the caller */
613 static BOOL match_allows_spec_alpha(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
614         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
615 {
616     GLenum error;
617     DWORD data[16];
618
619     if (!gl_info->supported[EXT_SECONDARY_COLOR]) return FALSE;
620
621     ENTER_GL();
622     while(glGetError());
623     GL_EXTCALL(glSecondaryColorPointerEXT)(4, GL_UNSIGNED_BYTE, 4, data);
624     error = glGetError();
625     LEAVE_GL();
626
627     if(error == GL_NO_ERROR)
628     {
629         TRACE("GL Implementation accepts 4 component specular color pointers\n");
630         return TRUE;
631     }
632     else
633     {
634         TRACE("GL implementation does not accept 4 component specular colors, error %s\n",
635               debug_glerror(error));
636         return FALSE;
637     }
638 }
639
640 /* A GL context is provided by the caller */
641 static BOOL match_broken_nv_clip(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
642         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
643 {
644     GLuint prog;
645     BOOL ret = FALSE;
646     GLint pos;
647     const char *testcode =
648         "!!ARBvp1.0\n"
649         "OPTION NV_vertex_program2;\n"
650         "MOV result.clip[0], 0.0;\n"
651         "MOV result.position, 0.0;\n"
652         "END\n";
653
654     if (!gl_info->supported[NV_VERTEX_PROGRAM2_OPTION]) return FALSE;
655
656     ENTER_GL();
657     while(glGetError());
658
659     GL_EXTCALL(glGenProgramsARB(1, &prog));
660     if(!prog)
661     {
662         ERR("Failed to create the NVvp clip test program\n");
663         LEAVE_GL();
664         return FALSE;
665     }
666     GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prog));
667     GL_EXTCALL(glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
668                                   strlen(testcode), testcode));
669     glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
670     if(pos != -1)
671     {
672         WARN("GL_NV_vertex_program2_option result.clip[] test failed\n");
673         TRACE("error: %s\n", debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
674         ret = TRUE;
675         while(glGetError());
676     }
677     else TRACE("GL_NV_vertex_program2_option result.clip[] test passed\n");
678
679     GL_EXTCALL(glBindProgramARB(GL_VERTEX_PROGRAM_ARB, 0));
680     GL_EXTCALL(glDeleteProgramsARB(1, &prog));
681     checkGLcall("GL_NV_vertex_program2_option result.clip[] test cleanup");
682
683     LEAVE_GL();
684     return ret;
685 }
686
687 /* Context activation is done by the caller. */
688 static BOOL match_fbo_tex_update(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
689         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
690 {
691     char data[4 * 4 * 4];
692     GLuint tex, fbo;
693     GLenum status;
694
695     if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return FALSE;
696
697     memset(data, 0xcc, sizeof(data));
698
699     ENTER_GL();
700
701     glGenTextures(1, &tex);
702     glBindTexture(GL_TEXTURE_2D, tex);
703     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
704     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
705     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 4, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
706     checkGLcall("glTexImage2D");
707
708     gl_info->fbo_ops.glGenFramebuffers(1, &fbo);
709     gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, fbo);
710     gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
711     checkGLcall("glFramebufferTexture2D");
712
713     status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
714     if (status != GL_FRAMEBUFFER_COMPLETE) ERR("FBO status %#x\n", status);
715     checkGLcall("glCheckFramebufferStatus");
716
717     memset(data, 0x11, sizeof(data));
718     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
719     checkGLcall("glTexSubImage2D");
720
721     glClearColor(0.996f, 0.729f, 0.745f, 0.792f);
722     glClear(GL_COLOR_BUFFER_BIT);
723     checkGLcall("glClear");
724
725     glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
726     checkGLcall("glGetTexImage");
727
728     gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
729     gl_info->fbo_ops.glBindFramebuffer(GL_FRAMEBUFFER, 0);
730     glBindTexture(GL_TEXTURE_2D, 0);
731     checkGLcall("glBindTexture");
732
733     gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
734     glDeleteTextures(1, &tex);
735     checkGLcall("glDeleteTextures");
736
737     LEAVE_GL();
738
739     return *(DWORD *)data == 0x11111111;
740 }
741
742 /* Context activation is done by the caller. */
743 static BOOL match_broken_rgba16(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
744         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
745 {
746     /* GL_RGBA16 uses GL_RGBA8 internally on Geforce 7 and older cards.
747      * This leads to graphical bugs in Half Life 2 and Unreal engine games. */
748     GLuint tex;
749     GLint size;
750
751     ENTER_GL();
752
753     glGenTextures(1, &tex);
754     glBindTexture(GL_TEXTURE_2D, tex);
755     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, 4, 4, 0, GL_RGBA, GL_UNSIGNED_SHORT, NULL);
756     checkGLcall("glTexImage2D");
757
758     glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &size);
759     checkGLcall("glGetTexLevelParameteriv");
760     TRACE("Real color depth is %d\n", size);
761
762     glBindTexture(GL_TEXTURE_2D, 0);
763     checkGLcall("glBindTexture");
764     glDeleteTextures(1, &tex);
765     checkGLcall("glDeleteTextures");
766
767     LEAVE_GL();
768
769     return size < 16;
770 }
771
772 static BOOL match_fglrx(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
773         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
774 {
775     return gl_vendor == GL_VENDOR_FGLRX;
776 }
777
778 static void quirk_arb_constants(struct wined3d_gl_info *gl_info)
779 {
780     TRACE_(d3d_caps)("Using ARB vs constant limit(=%u) for GLSL.\n", gl_info->limits.arb_vs_native_constants);
781     gl_info->limits.glsl_vs_float_constants = gl_info->limits.arb_vs_native_constants;
782     TRACE_(d3d_caps)("Using ARB ps constant limit(=%u) for GLSL.\n", gl_info->limits.arb_ps_native_constants);
783     gl_info->limits.glsl_ps_float_constants = gl_info->limits.arb_ps_native_constants;
784 }
785
786 static void quirk_apple_glsl_constants(struct wined3d_gl_info *gl_info)
787 {
788     quirk_arb_constants(gl_info);
789     /* MacOS needs uniforms for relative addressing offsets. This can accumulate to quite a few uniforms.
790      * Beyond that the general uniform isn't optimal, so reserve a number of uniforms. 12 vec4's should
791      * allow 48 different offsets or other helper immediate values. */
792     TRACE_(d3d_caps)("Reserving 12 GLSL constants for compiler private use.\n");
793     gl_info->reserved_glsl_constants = max(gl_info->reserved_glsl_constants, 12);
794 }
795
796 static void quirk_amd_dx9(struct wined3d_gl_info *gl_info)
797 {
798     /* MacOS advertises GL_ARB_texture_non_power_of_two on ATI r500 and earlier cards, although
799      * these cards only support GL_ARB_texture_rectangle(D3DPTEXTURECAPS_NONPOW2CONDITIONAL).
800      * If real NP2 textures are used, the driver falls back to software. We could just remove the
801      * extension and use GL_ARB_texture_rectangle instead, but texture_rectangle is inconvenient
802      * due to the non-normalized texture coordinates. Thus set an internal extension flag,
803      * GL_WINE_normalized_texrect, which signals the code that it can use non power of two textures
804      * as per GL_ARB_texture_non_power_of_two, but has to stick to the texture_rectangle limits.
805      *
806      * fglrx doesn't advertise GL_ARB_texture_non_power_of_two, but it advertises opengl 2.0 which
807      * has this extension promoted to core. The extension loading code sets this extension supported
808      * due to that, so this code works on fglrx as well. */
809     if(gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
810     {
811         TRACE("GL_ARB_texture_non_power_of_two advertised on R500 or earlier card, removing.\n");
812         gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] = FALSE;
813         gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] = TRUE;
814     }
815 }
816
817 static void quirk_no_np2(struct wined3d_gl_info *gl_info)
818 {
819     /*  The nVidia GeForceFX series reports OpenGL 2.0 capabilities with the latest drivers versions, but
820      *  doesn't explicitly advertise the ARB_tex_npot extension in the GL extension string.
821      *  This usually means that ARB_tex_npot is supported in hardware as long as the application is staying
822      *  within the limits enforced by the ARB_texture_rectangle extension. This however is not true for the
823      *  FX series, which instantly falls back to a slower software path as soon as ARB_tex_npot is used.
824      *  We therefore completely remove ARB_tex_npot from the list of supported extensions.
825      *
826      *  Note that wine_normalized_texrect can't be used in this case because internally it uses ARB_tex_npot,
827      *  triggering the software fallback. There is not much we can do here apart from disabling the
828      *  software-emulated extension and reenable ARB_tex_rect (which was previously disabled
829      *  in wined3d_adapter_init_gl_caps).
830      *  This fixup removes performance problems on both the FX 5900 and FX 5700 (e.g. for framebuffer
831      *  post-processing effects in the game "Max Payne 2").
832      *  The behaviour can be verified through a simple test app attached in bugreport #14724. */
833     TRACE("GL_ARB_texture_non_power_of_two advertised through OpenGL 2.0 on NV FX card, removing.\n");
834     gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] = FALSE;
835     gl_info->supported[ARB_TEXTURE_RECTANGLE] = TRUE;
836 }
837
838 static void quirk_texcoord_w(struct wined3d_gl_info *gl_info)
839 {
840     /* The Intel GPUs on MacOS set the .w register of texcoords to 0.0 by default, which causes problems
841      * with fixed function fragment processing. Ideally this flag should be detected with a test shader
842      * and OpenGL feedback mode, but some GL implementations (MacOS ATI at least, probably all MacOS ones)
843      * do not like vertex shaders in feedback mode and return an error, even though it should be valid
844      * according to the spec.
845      *
846      * We don't want to enable this on all cards, as it adds an extra instruction per texcoord used. This
847      * makes the shader slower and eats instruction slots which should be available to the d3d app.
848      *
849      * ATI Radeon HD 2xxx cards on MacOS have the issue. Instead of checking for the buggy cards, blacklist
850      * all radeon cards on Macs and whitelist the good ones. That way we're prepared for the future. If
851      * this workaround is activated on cards that do not need it, it won't break things, just affect
852      * performance negatively. */
853     TRACE("Enabling vertex texture coord fixes in vertex shaders.\n");
854     gl_info->quirks |= WINED3D_QUIRK_SET_TEXCOORD_W;
855 }
856
857 static void quirk_clip_varying(struct wined3d_gl_info *gl_info)
858 {
859     gl_info->quirks |= WINED3D_QUIRK_GLSL_CLIP_VARYING;
860 }
861
862 static void quirk_allows_specular_alpha(struct wined3d_gl_info *gl_info)
863 {
864     gl_info->quirks |= WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA;
865 }
866
867 static void quirk_disable_nvvp_clip(struct wined3d_gl_info *gl_info)
868 {
869     gl_info->quirks |= WINED3D_QUIRK_NV_CLIP_BROKEN;
870 }
871
872 static void quirk_fbo_tex_update(struct wined3d_gl_info *gl_info)
873 {
874     gl_info->quirks |= WINED3D_QUIRK_FBO_TEX_UPDATE;
875 }
876
877 static void quirk_broken_rgba16(struct wined3d_gl_info *gl_info)
878 {
879     gl_info->quirks |= WINED3D_QUIRK_BROKEN_RGBA16;
880 }
881
882 static void quirk_infolog_spam(struct wined3d_gl_info *gl_info)
883 {
884     gl_info->quirks |= WINED3D_QUIRK_INFO_LOG_SPAM;
885 }
886
887 static void quirk_limited_tex_filtering(struct wined3d_gl_info *gl_info)
888 {
889     /* Nvidia GeForce 6xxx and 7xxx support accelerated VTF only on a few
890        selected texture formats. They are apparently the only DX9 class GPUs
891        supporting VTF.
892        Also, DX9-era GPUs are somewhat limited with float textures
893        filtering and blending. */
894     gl_info->quirks |= WINED3D_QUIRK_LIMITED_TEX_FILTERING;
895 }
896
897 struct driver_quirk
898 {
899     BOOL (*match)(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
900             enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device);
901     void (*apply)(struct wined3d_gl_info *gl_info);
902     const char *description;
903 };
904
905 static const struct driver_quirk quirk_table[] =
906 {
907     {
908         match_amd_r300_to_500,
909         quirk_amd_dx9,
910         "AMD normalized texrect quirk"
911     },
912     /* MacOS advertises more GLSL vertex shader uniforms than supported by the hardware, and if more are
913      * used it falls back to software. While the compiler can detect if the shader uses all declared
914      * uniforms, the optimization fails if the shader uses relative addressing. So any GLSL shader
915      * using relative addressing falls back to software.
916      *
917      * ARB vp gives the correct amount of uniforms, so use it instead of GLSL. */
918     {
919         match_apple,
920         quirk_apple_glsl_constants,
921         "Apple GLSL uniform override"
922     },
923     {
924         match_geforce5,
925         quirk_no_np2,
926         "Geforce 5 NP2 disable"
927     },
928     {
929         match_apple_intel,
930         quirk_texcoord_w,
931         "Init texcoord .w for Apple Intel GPU driver"
932     },
933     {
934         match_apple_nonr500ati,
935         quirk_texcoord_w,
936         "Init texcoord .w for Apple ATI >= r600 GPU driver"
937     },
938     {
939         match_dx10_capable,
940         quirk_clip_varying,
941         "Reserved varying for gl_ClipPos"
942     },
943     {
944         /* GL_EXT_secondary_color does not allow 4 component secondary colors, but most
945          * GL implementations accept it. The Mac GL is the only implementation known to
946          * reject it.
947          *
948          * If we can pass 4 component specular colors, do it, because (a) we don't have
949          * to screw around with the data, and (b) the D3D fixed function vertex pipeline
950          * passes specular alpha to the pixel shader if any is used. Otherwise the
951          * specular alpha is used to pass the fog coordinate, which we pass to opengl
952          * via GL_EXT_fog_coord.
953          */
954         match_allows_spec_alpha,
955         quirk_allows_specular_alpha,
956         "Allow specular alpha quirk"
957     },
958     {
959         match_broken_nv_clip,
960         quirk_disable_nvvp_clip,
961         "Apple NV_vertex_program clip bug quirk"
962     },
963     {
964         match_fbo_tex_update,
965         quirk_fbo_tex_update,
966         "FBO rebind for attachment updates"
967     },
968     {
969         match_broken_rgba16,
970         quirk_broken_rgba16,
971         "True RGBA16 is not available"
972     },
973     {
974         match_fglrx,
975         quirk_infolog_spam,
976         "Not printing GLSL infolog"
977     },
978     {
979         match_not_dx10_capable,
980         quirk_limited_tex_filtering,
981         "Texture filtering, blending and VTF support is limited"
982     },
983 };
984
985 /* Certain applications (Steam) complain if we report an outdated driver version. In general,
986  * reporting a driver version is moot because we are not the Windows driver, and we have different
987  * bugs, features, etc.
988  *
989  * The driver version has the form "x.y.z.w".
990  *
991  * "x" is the Windows version the driver is meant for:
992  * 4 -> 95/98/NT4
993  * 5 -> 2000
994  * 6 -> 2000/XP
995  * 7 -> Vista
996  * 8 -> Win 7
997  *
998  * "y" is the maximum Direct3D version the driver supports.
999  * y  -> d3d version mapping:
1000  * 11 -> d3d6
1001  * 12 -> d3d7
1002  * 13 -> d3d8
1003  * 14 -> d3d9
1004  * 15 -> d3d10
1005  * 16 -> d3d10.1
1006  * 17 -> d3d11
1007  *
1008  * "z" is the subversion number.
1009  *
1010  * "w" is the vendor specific driver build number.
1011  */
1012
1013 struct driver_version_information
1014 {
1015     enum wined3d_display_driver driver;
1016     enum wined3d_driver_model driver_model;
1017     const char *driver_name;            /* name of Windows driver */
1018     WORD version;                       /* version word ('y'), contained in low word of DriverVersion.HighPart */
1019     WORD subversion;                    /* subversion word ('z'), contained in high word of DriverVersion.LowPart */
1020     WORD build;                         /* build number ('w'), contained in low word of DriverVersion.LowPart */
1021 };
1022
1023 /* The driver version table contains driver information for different devices on several OS versions. */
1024 static const struct driver_version_information driver_version_table[] =
1025 {
1026     /* AMD
1027      * - Radeon HD2x00 (R600) and up supported by current drivers.
1028      * - Radeon 9500 (R300) - X1*00 (R5xx) supported up to Catalyst 9.3 (Linux) and 10.2 (XP/Vista/Win7)
1029      * - Radeon 7xxx (R100) - 9250 (RV250) supported up to Catalyst 6.11 (XP)
1030      * - Rage 128 supported up to XP, latest official build 6.13.3279 dated October 2001 */
1031     {DRIVER_AMD_RAGE_128PRO,    DRIVER_MODEL_NT5X,  "ati2dvaa.dll", 13, 3279,  0},
1032     {DRIVER_AMD_R100,           DRIVER_MODEL_NT5X,  "ati2dvag.dll", 14, 10, 6614},
1033     {DRIVER_AMD_R300,           DRIVER_MODEL_NT5X,  "ati2dvag.dll", 14, 10, 6764},
1034     {DRIVER_AMD_R600,           DRIVER_MODEL_NT5X,  "ati2dvag.dll", 14, 10, 8681},
1035     {DRIVER_AMD_R300,           DRIVER_MODEL_NT6X,  "atiumdag.dll", 14, 10, 741 },
1036     {DRIVER_AMD_R600,           DRIVER_MODEL_NT6X,  "atiumdag.dll", 14, 10, 741 },
1037
1038     /* Intel
1039      * The drivers are unified but not all versions support all GPUs. At some point the 2k/xp
1040      * drivers used ialmrnt5.dll for GMA800/GMA900 but at some point the file was renamed to
1041      * igxprd32.dll but the GMA800 driver was never updated. */
1042     {DRIVER_INTEL_GMA800,       DRIVER_MODEL_NT5X,  "ialmrnt5.dll", 14, 10, 3889},
1043     {DRIVER_INTEL_GMA900,       DRIVER_MODEL_NT5X,  "igxprd32.dll", 14, 10, 4764},
1044     {DRIVER_INTEL_GMA950,       DRIVER_MODEL_NT5X,  "igxprd32.dll", 14, 10, 4926},
1045     {DRIVER_INTEL_GMA3000,      DRIVER_MODEL_NT5X,  "igxprd32.dll", 14, 10, 5218},
1046     {DRIVER_INTEL_GMA950,       DRIVER_MODEL_NT6X,  "igdumd32.dll", 14, 10, 1504},
1047     {DRIVER_INTEL_GMA3000,      DRIVER_MODEL_NT6X,  "igdumd32.dll", 15, 10, 1666},
1048
1049     /* Nvidia
1050      * - Geforce6 and newer cards are supported by the current driver (197.x) on XP-Win7
1051      * - GeforceFX support is up to 173.x on <= XP
1052      * - Geforce2MX/3/4 up to 96.x on <= XP
1053      * - TNT/Geforce1/2 up to 71.x on <= XP
1054      * All version numbers used below are from the Linux nvidia drivers. */
1055     {DRIVER_NVIDIA_TNT,         DRIVER_MODEL_NT5X,  "nv4_disp.dll", 14, 10, 7186},
1056     {DRIVER_NVIDIA_GEFORCE2MX,  DRIVER_MODEL_NT5X,  "nv4_disp.dll", 14, 10, 9371},
1057     {DRIVER_NVIDIA_GEFORCEFX,   DRIVER_MODEL_NT5X,  "nv4_disp.dll", 14, 11, 7516},
1058     {DRIVER_NVIDIA_GEFORCE6,    DRIVER_MODEL_NT5X,  "nv4_disp.dll", 15, 12, 6658},
1059     {DRIVER_NVIDIA_GEFORCE6,    DRIVER_MODEL_NT6X,  "nvd3dum.dll",  15, 12, 6658},
1060 };
1061
1062 struct gpu_description
1063 {
1064     WORD vendor;                    /* reported PCI card vendor ID  */
1065     WORD card;                      /* reported PCI card device ID  */
1066     const char *description;        /* Description of the card e.g. NVIDIA RIVA TNT */
1067     enum wined3d_display_driver driver;
1068     unsigned int vidmem;
1069 };
1070
1071 /* The amount of video memory stored in the gpu description table is the minimum amount of video memory
1072  * found on a board containing a specific GPU. */
1073 static const struct gpu_description gpu_description_table[] =
1074 {
1075     /* Nvidia cards */
1076     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_RIVA_TNT,           "NVIDIA RIVA TNT",                  DRIVER_NVIDIA_TNT,       16  },
1077     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_RIVA_TNT2,          "NVIDIA RIVA TNT2/TNT2 Pro",        DRIVER_NVIDIA_TNT,       32  },
1078     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE,            "NVIDIA GeForce 256",               DRIVER_NVIDIA_TNT,       32  },
1079     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE2,           "NVIDIA GeForce2 GTS/GeForce2 Pro", DRIVER_NVIDIA_TNT,       32  },
1080     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE2_MX,        "NVIDIA GeForce2 MX/MX 400",        DRIVER_NVIDIA_GEFORCE2MX,32  },
1081     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE3,           "NVIDIA GeForce3",                  DRIVER_NVIDIA_GEFORCE2MX,64  },
1082     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE4_MX,        "NVIDIA GeForce4 MX 460",           DRIVER_NVIDIA_GEFORCE2MX,64  },
1083     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE4_TI4200,    "NVIDIA GeForce4 Ti 4200",          DRIVER_NVIDIA_GEFORCE2MX,64, },
1084     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCEFX_5200,     "NVIDIA GeForce FX 5200",           DRIVER_NVIDIA_GEFORCEFX, 64  },
1085     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCEFX_5600,     "NVIDIA GeForce FX 5600",           DRIVER_NVIDIA_GEFORCEFX, 128 },
1086     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCEFX_5800,     "NVIDIA GeForce FX 5800",           DRIVER_NVIDIA_GEFORCEFX, 256 },
1087     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_6200,       "NVIDIA GeForce 6200",              DRIVER_NVIDIA_GEFORCE6,  64  },
1088     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_6600GT,     "NVIDIA GeForce 6600 GT",           DRIVER_NVIDIA_GEFORCE6,  128 },
1089     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_6800,       "NVIDIA GeForce 6800",              DRIVER_NVIDIA_GEFORCE6,  128 },
1090     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_7300,       "NVIDIA GeForce Go 7300",           DRIVER_NVIDIA_GEFORCE6,  256 },
1091     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_7400,       "NVIDIA GeForce Go 7400",           DRIVER_NVIDIA_GEFORCE6,  256 },
1092     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_7600,       "NVIDIA GeForce 7600 GT",           DRIVER_NVIDIA_GEFORCE6,  256 },
1093     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_7800GT,     "NVIDIA GeForce 7800 GT",           DRIVER_NVIDIA_GEFORCE6,  256 },
1094     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_8300GS,     "NVIDIA GeForce 8300 GS",           DRIVER_NVIDIA_GEFORCE6,  128 },
1095     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_8400GS,     "NVIDIA GeForce 8400 GS",           DRIVER_NVIDIA_GEFORCE6,  128 },
1096     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_8600GT,     "NVIDIA GeForce 8600 GT",           DRIVER_NVIDIA_GEFORCE6,  256 },
1097     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_8600MGT,    "NVIDIA GeForce 8600M GT",          DRIVER_NVIDIA_GEFORCE6,  512 },
1098     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_8800GTS,    "NVIDIA GeForce 8800 GTS",          DRIVER_NVIDIA_GEFORCE6,  320 },
1099     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_8800GTX,    "NVIDIA GeForce 8800 GTX",          DRIVER_NVIDIA_GEFORCE6,  768 },
1100     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_9200,       "NVIDIA GeForce 9200",              DRIVER_NVIDIA_GEFORCE6,  256 },
1101     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_9400GT,     "NVIDIA GeForce 9400 GT",           DRIVER_NVIDIA_GEFORCE6,  256 },
1102     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_9500GT,     "NVIDIA GeForce 9500 GT",           DRIVER_NVIDIA_GEFORCE6,  256 },
1103     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_9600GT,     "NVIDIA GeForce 9600 GT",           DRIVER_NVIDIA_GEFORCE6,  384 },
1104     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_9800GT,     "NVIDIA GeForce 9800 GT",           DRIVER_NVIDIA_GEFORCE6,  512 },
1105     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_210,        "NVIDIA GeForce 210",               DRIVER_NVIDIA_GEFORCE6,  512 },
1106     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT220,      "NVIDIA GeForce GT 220",            DRIVER_NVIDIA_GEFORCE6,  512 },
1107     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT240,      "NVIDIA GeForce GT 240",            DRIVER_NVIDIA_GEFORCE6,  512 },
1108     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX260,     "NVIDIA GeForce GTX 260",           DRIVER_NVIDIA_GEFORCE6,  1024},
1109     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX275,     "NVIDIA GeForce GTX 275",           DRIVER_NVIDIA_GEFORCE6,  896 },
1110     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX280,     "NVIDIA GeForce GTX 280",           DRIVER_NVIDIA_GEFORCE6,  1024},
1111     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT320M,     "NVIDIA GeForce GT 320M",           DRIVER_NVIDIA_GEFORCE6,  1024},
1112     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT325M,     "NVIDIA GeForce GT 325M",           DRIVER_NVIDIA_GEFORCE6,  1024},
1113     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT330,      "NVIDIA GeForce GT 330",            DRIVER_NVIDIA_GEFORCE6,  1024},
1114     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTS350M,    "NVIDIA GeForce GTS 350M",          DRIVER_NVIDIA_GEFORCE6,  1024},
1115     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT420,      "NVIDIA GeForce GT 420",            DRIVER_NVIDIA_GEFORCE6,  2048},
1116     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT430,      "NVIDIA GeForce GT 430",            DRIVER_NVIDIA_GEFORCE6,  1024},
1117     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT440,      "NVIDIA GeForce GT 440",            DRIVER_NVIDIA_GEFORCE6,  1024},
1118     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTS450,     "NVIDIA GeForce GTS 450",           DRIVER_NVIDIA_GEFORCE6,  1024},
1119     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX460,     "NVIDIA GeForce GTX 460",           DRIVER_NVIDIA_GEFORCE6,  768 },
1120     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX460M,    "NVIDIA GeForce GTX 460M",          DRIVER_NVIDIA_GEFORCE6,  1536},
1121     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX465,     "NVIDIA GeForce GTX 465",           DRIVER_NVIDIA_GEFORCE6,  1024},
1122     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX470,     "NVIDIA GeForce GTX 470",           DRIVER_NVIDIA_GEFORCE6,  1280},
1123     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX480,     "NVIDIA GeForce GTX 480",           DRIVER_NVIDIA_GEFORCE6,  1536},
1124     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX550,     "NVIDIA GeForce GTX 550 Ti",        DRIVER_NVIDIA_GEFORCE6,  1024},
1125     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GT555M,     "NVIDIA GeForce GT 555M",           DRIVER_NVIDIA_GEFORCE6,  1024},
1126     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX560TI,   "NVIDIA GeForce GTX 560 Ti",        DRIVER_NVIDIA_GEFORCE6,  1024},
1127     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX560,     "NVIDIA GeForce GTX 560",           DRIVER_NVIDIA_GEFORCE6,  1024},
1128     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX570,     "NVIDIA GeForce GTX 570",           DRIVER_NVIDIA_GEFORCE6,  1280},
1129     {HW_VENDOR_NVIDIA,     CARD_NVIDIA_GEFORCE_GTX580,     "NVIDIA GeForce GTX 580",           DRIVER_NVIDIA_GEFORCE6,  1536},
1130
1131     /* AMD cards */
1132     {HW_VENDOR_AMD,        CARD_AMD_RAGE_128PRO,           "ATI Rage Fury",                    DRIVER_AMD_RAGE_128PRO,  16  },
1133     {HW_VENDOR_AMD,        CARD_AMD_RADEON_7200,           "ATI RADEON 7200 SERIES",           DRIVER_AMD_R100,         32  },
1134     {HW_VENDOR_AMD,        CARD_AMD_RADEON_8500,           "ATI RADEON 8500 SERIES",           DRIVER_AMD_R100,         64  },
1135     {HW_VENDOR_AMD,        CARD_AMD_RADEON_9500,           "ATI Radeon 9500",                  DRIVER_AMD_R300,         64  },
1136     {HW_VENDOR_AMD,        CARD_AMD_RADEON_XPRESS_200M,    "ATI RADEON XPRESS 200M Series",    DRIVER_AMD_R300,         64  },
1137     {HW_VENDOR_AMD,        CARD_AMD_RADEON_X700,           "ATI Radeon X700 SE",               DRIVER_AMD_R300,         128 },
1138     {HW_VENDOR_AMD,        CARD_AMD_RADEON_X1600,          "ATI Radeon X1600 Series",          DRIVER_AMD_R300,         128 },
1139     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD2350,         "ATI Mobility Radeon HD 2350",      DRIVER_AMD_R600,         256 },
1140     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD2600,         "ATI Mobility Radeon HD 2600",      DRIVER_AMD_R600,         256 },
1141     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD2900,         "ATI Radeon HD 2900 XT",            DRIVER_AMD_R600,         512 },
1142     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD3200,         "ATI Radeon HD 3200 Graphics",      DRIVER_AMD_R600,         128 },
1143     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD4350,         "ATI Radeon HD 4350",               DRIVER_AMD_R600,         256 },
1144     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD4600,         "ATI Radeon HD 4600 Series",        DRIVER_AMD_R600,         512 },
1145     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD4700,         "ATI Radeon HD 4700 Series",        DRIVER_AMD_R600,         512 },
1146     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD4800,         "ATI Radeon HD 4800 Series",        DRIVER_AMD_R600,         512 },
1147     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD5400,         "ATI Radeon HD 5400 Series",        DRIVER_AMD_R600,         512 },
1148     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD5600,         "ATI Radeon HD 5600 Series",        DRIVER_AMD_R600,         512 },
1149     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD5700,         "ATI Radeon HD 5700 Series",        DRIVER_AMD_R600,         512 },
1150     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD5800,         "ATI Radeon HD 5800 Series",        DRIVER_AMD_R600,         1024},
1151     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD5900,         "ATI Radeon HD 5900 Series",        DRIVER_AMD_R600,         1024},
1152     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6300,         "AMD Radeon HD 6300 series Graphics", DRIVER_AMD_R600,       1024},
1153     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6400,         "AMD Radeon HD 6400 Series",        DRIVER_AMD_R600,         1024},
1154     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6410D,        "AMD Radeon HD 6410D",              DRIVER_AMD_R600,         1024},
1155     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6550D,        "AMD Radeon HD 6550D",              DRIVER_AMD_R600,         1024},
1156     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6600,         "AMD Radeon HD 6600 Series",        DRIVER_AMD_R600,         1024},
1157     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6600M,        "AMD Radeon HD 6600M Series",       DRIVER_AMD_R600,         512 },
1158     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6800,         "AMD Radeon HD 6800 Series",        DRIVER_AMD_R600,         1024},
1159     {HW_VENDOR_AMD,        CARD_AMD_RADEON_HD6900,         "AMD Radeon HD 6900 Series",        DRIVER_AMD_R600,         2048},
1160     /* Intel cards */
1161     {HW_VENDOR_INTEL,      CARD_INTEL_I830G,               "Intel(R) 82830M Graphics Controller",                       DRIVER_INTEL_GMA800,  32 },
1162     {HW_VENDOR_INTEL,      CARD_INTEL_I855G,               "Intel(R) 82852/82855 GM/GME Graphics Controller",           DRIVER_INTEL_GMA800,  32 },
1163     {HW_VENDOR_INTEL,      CARD_INTEL_I865G,               "Intel(R) 82865G Graphics Controller",                       DRIVER_INTEL_GMA800,  32 },
1164     {HW_VENDOR_INTEL,      CARD_INTEL_I915G,               "Intel(R) 82915G/GV/910GL Express Chipset Family",           DRIVER_INTEL_GMA900,  64 },
1165     {HW_VENDOR_INTEL,      CARD_INTEL_I915GM,              "Mobile Intel(R) 915GM/GMS,910GML Express Chipset Family",   DRIVER_INTEL_GMA900,  64 },
1166     {HW_VENDOR_INTEL,      CARD_INTEL_I945GM,              "Mobile Intel(R) 945GM Express Chipset Family",              DRIVER_INTEL_GMA950,  64 },
1167     {HW_VENDOR_INTEL,      CARD_INTEL_X3100,               "Mobile Intel(R) 965 Express Chipset Family",                DRIVER_INTEL_GMA3000, 128},
1168     {HW_VENDOR_INTEL,      CARD_INTEL_GM45,                "Mobile Intel(R) GM45 Express Chipset Family",               DRIVER_INTEL_GMA3000, 512}
1169 };
1170
1171 static const struct driver_version_information *get_driver_version_info(enum wined3d_display_driver driver,
1172         enum wined3d_driver_model driver_model)
1173 {
1174     unsigned int i;
1175
1176     TRACE("Looking up version info for driver=%d driver_model=%d\n", driver, driver_model);
1177     for (i = 0; i < (sizeof(driver_version_table) / sizeof(driver_version_table[0])); i++)
1178     {
1179         const struct driver_version_information *entry = &driver_version_table[i];
1180
1181         if (entry->driver == driver && entry->driver_model == driver_model)
1182         {
1183             TRACE_(d3d_caps)("Found driver '%s' version=%d subversion=%d build=%d\n",
1184                 entry->driver_name, entry->version, entry->subversion, entry->build);
1185
1186             return entry;
1187         }
1188     }
1189     return NULL;
1190 }
1191
1192 static void init_driver_info(struct wined3d_driver_info *driver_info,
1193         enum wined3d_pci_vendor vendor, enum wined3d_pci_device device)
1194 {
1195     OSVERSIONINFOW os_version;
1196     WORD driver_os_version;
1197     unsigned int i;
1198     enum wined3d_display_driver driver = DRIVER_UNKNOWN;
1199     enum wined3d_driver_model driver_model;
1200     const struct driver_version_information *version_info;
1201
1202     if (wined3d_settings.pci_vendor_id != PCI_VENDOR_NONE)
1203     {
1204         TRACE_(d3d_caps)("Overriding PCI vendor ID with: %04x\n", wined3d_settings.pci_vendor_id);
1205         vendor = wined3d_settings.pci_vendor_id;
1206     }
1207     driver_info->vendor = vendor;
1208
1209     if (wined3d_settings.pci_device_id != PCI_DEVICE_NONE)
1210     {
1211         TRACE_(d3d_caps)("Overriding PCI device ID with: %04x\n", wined3d_settings.pci_device_id);
1212         device = wined3d_settings.pci_device_id;
1213     }
1214     driver_info->device = device;
1215
1216     /* Set a default amount of video memory (64MB). In general this code isn't used unless the user
1217      * overrides the pci ids to a card which is not in our database. */
1218     driver_info->vidmem = WINE_DEFAULT_VIDMEM;
1219
1220     memset(&os_version, 0, sizeof(os_version));
1221     os_version.dwOSVersionInfoSize = sizeof(os_version);
1222     if (!GetVersionExW(&os_version))
1223     {
1224         ERR("Failed to get OS version, reporting 2000/XP.\n");
1225         driver_os_version = 6;
1226         driver_model = DRIVER_MODEL_NT5X;
1227     }
1228     else
1229     {
1230         TRACE("OS version %u.%u.\n", os_version.dwMajorVersion, os_version.dwMinorVersion);
1231         switch (os_version.dwMajorVersion)
1232         {
1233             case 4:
1234                 /* If needed we could distinguish between 9x and NT4, but this code won't make
1235                  * sense for NT4 since it had no way to obtain this info through DirectDraw 3.0.
1236                  */
1237                 driver_os_version = 4;
1238                 driver_model = DRIVER_MODEL_WIN9X;
1239                 break;
1240
1241             case 5:
1242                 driver_os_version = 6;
1243                 driver_model = DRIVER_MODEL_NT5X;
1244                 break;
1245
1246             case 6:
1247                 if (os_version.dwMinorVersion == 0)
1248                 {
1249                     driver_os_version = 7;
1250                     driver_model = DRIVER_MODEL_NT6X;
1251                 }
1252                 else
1253                 {
1254                     if (os_version.dwMinorVersion > 1)
1255                     {
1256                         FIXME("Unhandled OS version %u.%u, reporting Win 7.\n",
1257                                 os_version.dwMajorVersion, os_version.dwMinorVersion);
1258                     }
1259                     driver_os_version = 8;
1260                     driver_model = DRIVER_MODEL_NT6X;
1261                 }
1262                 break;
1263
1264             default:
1265                 FIXME("Unhandled OS version %u.%u, reporting 2000/XP.\n",
1266                         os_version.dwMajorVersion, os_version.dwMinorVersion);
1267                 driver_os_version = 6;
1268                 driver_model = DRIVER_MODEL_NT5X;
1269                 break;
1270         }
1271     }
1272
1273     /* When we reach this stage we always have a vendor or device id (it can be a default one).
1274      * This means that unless the ids are overriden, we will always find a GPU description. */
1275     for (i = 0; i < (sizeof(gpu_description_table) / sizeof(gpu_description_table[0])); i++)
1276     {
1277         if (vendor == gpu_description_table[i].vendor && device == gpu_description_table[i].card)
1278         {
1279             TRACE_(d3d_caps)("Found card %04x:%04x in driver DB.\n", vendor, device);
1280
1281             driver_info->description = gpu_description_table[i].description;
1282             driver_info->vidmem = gpu_description_table[i].vidmem * 1024*1024;
1283             driver = gpu_description_table[i].driver;
1284             break;
1285         }
1286     }
1287
1288     if (wined3d_settings.emulated_textureram)
1289     {
1290         TRACE_(d3d_caps)("Overriding amount of video memory with: %d byte\n", wined3d_settings.emulated_textureram);
1291         driver_info->vidmem = wined3d_settings.emulated_textureram;
1292     }
1293
1294     /* Try to obtain driver version information for the current Windows version. This fails in
1295      * some cases:
1296      * - the gpu is not available on the currently selected OS version:
1297      *   - Geforce GTX480 on Win98. When running applications in compatibility mode on Windows,
1298      *     version information for the current Windows version is returned instead of faked info.
1299      *     We do the same and assume the default Windows version to emulate is WinXP.
1300      *
1301      *   - Videocard is a Riva TNT but winver is set to win7 (there are no drivers for this beast)
1302      *     For now return the XP driver info. Perhaps later on we should return VESA.
1303      *
1304      * - the gpu is not in our database (can happen when the user overrides the vendor_id / device_id)
1305      *   This could be an indication that our database is not up to date, so this should be fixed.
1306      */
1307     version_info = get_driver_version_info(driver, driver_model);
1308     if (version_info)
1309     {
1310         driver_info->name = version_info->driver_name;
1311         driver_info->version_high = MAKEDWORD_VERSION(driver_os_version, version_info->version);
1312         driver_info->version_low = MAKEDWORD_VERSION(version_info->subversion, version_info->build);
1313     }
1314     else
1315     {
1316         version_info = get_driver_version_info(driver, DRIVER_MODEL_NT5X);
1317         if (version_info)
1318         {
1319             driver_info->name = version_info->driver_name;
1320             driver_info->version_high = MAKEDWORD_VERSION(driver_os_version, version_info->version);
1321             driver_info->version_low = MAKEDWORD_VERSION(version_info->subversion, version_info->build);
1322         }
1323         else
1324         {
1325             driver_info->description = "Direct3D HAL";
1326             driver_info->name = "Display";
1327             driver_info->version_high = MAKEDWORD_VERSION(driver_os_version, 15);
1328             driver_info->version_low = MAKEDWORD_VERSION(8, 6); /* Nvidia RIVA TNT, arbitrary */
1329
1330             FIXME("Unable to find a driver/device info for vendor_id=%#x device_id=%#x for driver_model=%d\n",
1331                     vendor, device, driver_model);
1332         }
1333     }
1334
1335     TRACE_(d3d_caps)("Reporting (fake) driver version 0x%08x-0x%08x.\n",
1336             driver_info->version_high, driver_info->version_low);
1337 }
1338
1339 /* Context activation is done by the caller. */
1340 static void fixup_extensions(struct wined3d_gl_info *gl_info, const char *gl_renderer,
1341         enum wined3d_gl_vendor gl_vendor, enum wined3d_pci_vendor card_vendor, enum wined3d_pci_device device)
1342 {
1343     unsigned int i;
1344
1345     for (i = 0; i < (sizeof(quirk_table) / sizeof(*quirk_table)); ++i)
1346     {
1347         if (!quirk_table[i].match(gl_info, gl_renderer, gl_vendor, card_vendor, device)) continue;
1348         TRACE_(d3d_caps)("Applying driver quirk \"%s\".\n", quirk_table[i].description);
1349         quirk_table[i].apply(gl_info);
1350     }
1351
1352     /* Find out if PBOs work as they are supposed to. */
1353     test_pbo_functionality(gl_info);
1354 }
1355
1356 static DWORD wined3d_parse_gl_version(const char *gl_version)
1357 {
1358     const char *ptr = gl_version;
1359     int major, minor;
1360
1361     major = atoi(ptr);
1362     if (major <= 0) ERR_(d3d_caps)("Invalid opengl major version: %d.\n", major);
1363
1364     while (isdigit(*ptr)) ++ptr;
1365     if (*ptr++ != '.') ERR_(d3d_caps)("Invalid opengl version string: %s.\n", debugstr_a(gl_version));
1366
1367     minor = atoi(ptr);
1368
1369     TRACE_(d3d_caps)("Found OpenGL version: %d.%d.\n", major, minor);
1370
1371     return MAKEDWORD_VERSION(major, minor);
1372 }
1373
1374 static enum wined3d_gl_vendor wined3d_guess_gl_vendor(const struct wined3d_gl_info *gl_info,
1375         const char *gl_vendor_string, const char *gl_renderer)
1376 {
1377
1378     /* MacOS has various specialities in the extensions it advertises. Some have to be loaded from
1379      * the opengl 1.2+ core, while other extensions are advertised, but software emulated. So try to
1380      * detect the Apple OpenGL implementation to apply some extension fixups afterwards.
1381      *
1382      * Detecting this isn't really easy. The vendor string doesn't mention Apple. Compile-time checks
1383      * aren't sufficient either because a Linux binary may display on a macos X server via remote X11.
1384      * So try to detect the GL implementation by looking at certain Apple extensions. Some extensions
1385      * like client storage might be supported on other implementations too, but GL_APPLE_flush_render
1386      * is specific to the Mac OS X window management, and GL_APPLE_ycbcr_422 is QuickTime specific. So
1387      * the chance that other implementations support them is rather small since Win32 QuickTime uses
1388      * DirectDraw, not OpenGL. */
1389     if (gl_info->supported[APPLE_FENCE]
1390             && gl_info->supported[APPLE_CLIENT_STORAGE]
1391             && gl_info->supported[APPLE_FLUSH_RENDER]
1392             && gl_info->supported[APPLE_YCBCR_422])
1393         return GL_VENDOR_APPLE;
1394
1395     if (strstr(gl_vendor_string, "NVIDIA"))
1396         return GL_VENDOR_NVIDIA;
1397
1398     if (strstr(gl_vendor_string, "ATI"))
1399         return GL_VENDOR_FGLRX;
1400
1401     if (strstr(gl_vendor_string, "Intel(R)")
1402             /* Intel switched from Intel(R) to Intel® recently, so just match Intel. */
1403             || strstr(gl_renderer, "Intel")
1404             || strstr(gl_vendor_string, "Intel Inc."))
1405         return GL_VENDOR_INTEL;
1406
1407     if (strstr(gl_vendor_string, "Mesa")
1408             || strstr(gl_vendor_string, "Advanced Micro Devices, Inc.")
1409             || strstr(gl_vendor_string, "DRI R300 Project")
1410             || strstr(gl_vendor_string, "X.Org R300 Project")
1411             || strstr(gl_vendor_string, "Tungsten Graphics, Inc")
1412             || strstr(gl_vendor_string, "VMware, Inc.")
1413             || strstr(gl_renderer, "Mesa")
1414             || strstr(gl_renderer, "Gallium"))
1415         return GL_VENDOR_MESA;
1416
1417     FIXME_(d3d_caps)("Received unrecognized GL_VENDOR %s. Returning GL_VENDOR_UNKNOWN.\n",
1418             debugstr_a(gl_vendor_string));
1419
1420     return GL_VENDOR_UNKNOWN;
1421 }
1422
1423 static enum wined3d_pci_vendor wined3d_guess_card_vendor(const char *gl_vendor_string, const char *gl_renderer)
1424 {
1425     if (strstr(gl_vendor_string, "NVIDIA")
1426             || strstr(gl_vendor_string, "nouveau"))
1427         return HW_VENDOR_NVIDIA;
1428
1429     if (strstr(gl_vendor_string, "ATI")
1430             || strstr(gl_vendor_string, "Advanced Micro Devices, Inc.")
1431             || strstr(gl_vendor_string, "X.Org R300 Project")
1432             || strstr(gl_renderer, "AMD")
1433             || strstr(gl_renderer, "R100")
1434             || strstr(gl_renderer, "R200")
1435             || strstr(gl_renderer, "R300")
1436             || strstr(gl_renderer, "R600")
1437             || strstr(gl_renderer, "R700"))
1438         return HW_VENDOR_AMD;
1439
1440     if (strstr(gl_vendor_string, "Intel(R)")
1441             /* Intel switched from Intel(R) to Intel® recently, so just match Intel. */
1442             || strstr(gl_renderer, "Intel")
1443             || strstr(gl_vendor_string, "Intel Inc."))
1444         return HW_VENDOR_INTEL;
1445
1446     if (strstr(gl_vendor_string, "Mesa")
1447             || strstr(gl_vendor_string, "Brian Paul")
1448             || strstr(gl_vendor_string, "Tungsten Graphics, Inc")
1449             || strstr(gl_vendor_string, "VMware, Inc."))
1450         return HW_VENDOR_SOFTWARE;
1451
1452     FIXME_(d3d_caps)("Received unrecognized GL_VENDOR %s. Returning HW_VENDOR_NVIDIA.\n", debugstr_a(gl_vendor_string));
1453
1454     return HW_VENDOR_NVIDIA;
1455 }
1456
1457 static UINT d3d_level_from_gl_info(const struct wined3d_gl_info *gl_info)
1458 {
1459     UINT level = 0;
1460
1461     if (gl_info->supported[ARB_MULTITEXTURE])
1462         level = 6;
1463     if (gl_info->supported[ARB_TEXTURE_COMPRESSION]
1464             && gl_info->supported[ARB_TEXTURE_CUBE_MAP]
1465             && gl_info->supported[ARB_TEXTURE_ENV_DOT3])
1466         level = 7;
1467     if (level == 7 && gl_info->supported[ARB_MULTISAMPLE]
1468             && gl_info->supported[ARB_TEXTURE_BORDER_CLAMP])
1469         level = 8;
1470     if (level == 8 && gl_info->supported[ARB_FRAGMENT_PROGRAM]
1471             && gl_info->supported[ARB_VERTEX_SHADER])
1472         level = 9;
1473     if (level == 9 && gl_info->supported[EXT_GPU_SHADER4])
1474         level = 10;
1475
1476     return level;
1477 }
1478
1479 static enum wined3d_pci_device select_card_nvidia_binary(const struct wined3d_gl_info *gl_info,
1480         const char *gl_renderer)
1481 {
1482     UINT d3d_level = d3d_level_from_gl_info(gl_info);
1483     unsigned int i;
1484
1485     if (d3d_level >= 10)
1486     {
1487         static const struct
1488         {
1489             const char *renderer;
1490             enum wined3d_pci_device id;
1491         }
1492         cards[] =
1493         {
1494             {"GTX 580",     CARD_NVIDIA_GEFORCE_GTX580},    /* Geforce 500 - highend */
1495             {"GTX 570",     CARD_NVIDIA_GEFORCE_GTX570},    /* Geforce 500 - midend high */
1496             {"GTX 560 Ti",  CARD_NVIDIA_GEFORCE_GTX560TI},  /* Geforce 500 - midend */
1497             {"GTX 560",     CARD_NVIDIA_GEFORCE_GTX560},    /* Geforce 500 - midend */
1498             {"GT 555M",     CARD_NVIDIA_GEFORCE_GT555M},    /* Geforce 500 - midend mobile */
1499             {"GTX 550 Ti",  CARD_NVIDIA_GEFORCE_GTX550},    /* Geforce 500 - midend */
1500             {"GTX 480",     CARD_NVIDIA_GEFORCE_GTX480},    /* Geforce 400 - highend */
1501             {"GTX 470",     CARD_NVIDIA_GEFORCE_GTX470},    /* Geforce 400 - midend high */
1502             {"GTX 465",     CARD_NVIDIA_GEFORCE_GTX465},    /* Geforce 400 - midend */
1503             {"GTX 460M",    CARD_NVIDIA_GEFORCE_GTX460M},   /* Geforce 400 - highend mobile */
1504             {"GTX 460",     CARD_NVIDIA_GEFORCE_GTX460},    /* Geforce 400 - midend */
1505             {"GTS 450",     CARD_NVIDIA_GEFORCE_GTS450},    /* Geforce 400 - midend low */
1506             {"GT 440",      CARD_NVIDIA_GEFORCE_GT440},     /* Geforce 400 - lowend */
1507             {"GT 430",      CARD_NVIDIA_GEFORCE_GT430},     /* Geforce 400 - lowend */
1508             {"GT 420",      CARD_NVIDIA_GEFORCE_GT420},     /* Geforce 400 - lowend */
1509             {"GT 330",      CARD_NVIDIA_GEFORCE_GT330},     /* Geforce 300 - highend */
1510             {"GTS 360M",    CARD_NVIDIA_GEFORCE_GTS350M},   /* Geforce 300 - highend mobile */
1511             {"GTS 350M",    CARD_NVIDIA_GEFORCE_GTS350M},   /* Geforce 300 - highend mobile */
1512             {"GT 330M",     CARD_NVIDIA_GEFORCE_GT325M},    /* Geforce 300 - midend mobile */
1513             {"GT 325M",     CARD_NVIDIA_GEFORCE_GT325M},    /* Geforce 300 - midend mobile */
1514             {"GT 320M",     CARD_NVIDIA_GEFORCE_GT320M},    /* Geforce 300 - midend mobile */
1515             {"GTX 295",     CARD_NVIDIA_GEFORCE_GTX280},    /* Geforce 200 - highend */
1516             {"GTX 285",     CARD_NVIDIA_GEFORCE_GTX280},    /* Geforce 200 - highend */
1517             {"GTX 280",     CARD_NVIDIA_GEFORCE_GTX280},    /* Geforce 200 - highend */
1518             {"GTX 275",     CARD_NVIDIA_GEFORCE_GTX275},    /* Geforce 200 - midend high */
1519             {"GTX 260",     CARD_NVIDIA_GEFORCE_GTX260},    /* Geforce 200 - midend */
1520             {"GT 240",      CARD_NVIDIA_GEFORCE_GT240},     /* Geforce 200 - midend */
1521             {"GT 220",      CARD_NVIDIA_GEFORCE_GT220},     /* Geforce 200 - lowend */
1522             {"Geforce 310", CARD_NVIDIA_GEFORCE_210},       /* Geforce 200 - lowend */
1523             {"Geforce 305", CARD_NVIDIA_GEFORCE_210},       /* Geforce 200 - lowend */
1524             {"Geforce 210", CARD_NVIDIA_GEFORCE_210},       /* Geforce 200 - lowend */
1525             {"G 210",       CARD_NVIDIA_GEFORCE_210},       /* Geforce 200 - lowend */
1526             {"GTS 250",     CARD_NVIDIA_GEFORCE_9800GT},    /* Geforce 9 - highend / Geforce 200 - midend */
1527             {"GTS 150",     CARD_NVIDIA_GEFORCE_9800GT},    /* Geforce 9 - highend / Geforce 200 - midend */
1528             {"9800",        CARD_NVIDIA_GEFORCE_9800GT},    /* Geforce 9 - highend / Geforce 200 - midend */
1529             {"GT 140",      CARD_NVIDIA_GEFORCE_9600GT},    /* Geforce 9 - midend */
1530             {"9600",        CARD_NVIDIA_GEFORCE_9600GT},    /* Geforce 9 - midend */
1531             {"GT 130",      CARD_NVIDIA_GEFORCE_9500GT},    /* Geforce 9 - midend low / Geforce 200 - low */
1532             {"GT 120",      CARD_NVIDIA_GEFORCE_9500GT},    /* Geforce 9 - midend low / Geforce 200 - low */
1533             {"9500",        CARD_NVIDIA_GEFORCE_9500GT},    /* Geforce 9 - midend low / Geforce 200 - low */
1534             {"9400",        CARD_NVIDIA_GEFORCE_9400GT},    /* Geforce 9 - lowend */
1535             {"9300",        CARD_NVIDIA_GEFORCE_9200},      /* Geforce 9 - lowend low */
1536             {"9200",        CARD_NVIDIA_GEFORCE_9200},      /* Geforce 9 - lowend low */
1537             {"9100",        CARD_NVIDIA_GEFORCE_9200},      /* Geforce 9 - lowend low */
1538             {"G 100",       CARD_NVIDIA_GEFORCE_9200},      /* Geforce 9 - lowend low */
1539             {"8800 GTX",    CARD_NVIDIA_GEFORCE_8800GTX},   /* Geforce 8 - highend high */
1540             {"8800",        CARD_NVIDIA_GEFORCE_8800GTS},   /* Geforce 8 - highend */
1541             {"8600 M",      CARD_NVIDIA_GEFORCE_8600MGT},   /* Geforce 8 - midend mobile */
1542             {"8700",        CARD_NVIDIA_GEFORCE_8600GT},    /* Geforce 8 - midend */
1543             {"8600",        CARD_NVIDIA_GEFORCE_8600GT},    /* Geforce 8 - midend */
1544             {"8500",        CARD_NVIDIA_GEFORCE_8400GS},    /* Geforce 8 - mid-lowend */
1545             {"8400",        CARD_NVIDIA_GEFORCE_8400GS},    /* Geforce 8 - mid-lowend */
1546             {"8300",        CARD_NVIDIA_GEFORCE_8300GS},    /* Geforce 8 - lowend */
1547             {"8200",        CARD_NVIDIA_GEFORCE_8300GS},    /* Geforce 8 - lowend */
1548             {"8100",        CARD_NVIDIA_GEFORCE_8300GS},    /* Geforce 8 - lowend */
1549         };
1550
1551         for (i = 0; i < sizeof(cards) / sizeof(*cards); ++i)
1552         {
1553             if (strstr(gl_renderer, cards[i].renderer))
1554                 return cards[i].id;
1555         }
1556
1557         /* Geforce8-compatible fall back if the GPU is not in the list yet */
1558         return CARD_NVIDIA_GEFORCE_8300GS;
1559     }
1560
1561     /* Both the GeforceFX, 6xxx and 7xxx series support D3D9. The last two types have more
1562      * shader capabilities, so we use the shader capabilities to distinguish between FX and 6xxx/7xxx.
1563      */
1564     if (d3d_level >= 9 && gl_info->supported[NV_VERTEX_PROGRAM3])
1565     {
1566         static const struct
1567         {
1568             const char *renderer;
1569             enum wined3d_pci_device id;
1570         }
1571         cards[] =
1572         {
1573             {"Quadro FX 5", CARD_NVIDIA_GEFORCE_7800GT},    /* Geforce 7 - highend */
1574             {"Quadro FX 4", CARD_NVIDIA_GEFORCE_7800GT},    /* Geforce 7 - highend */
1575             {"7950",        CARD_NVIDIA_GEFORCE_7800GT},    /* Geforce 7 - highend */
1576             {"7900",        CARD_NVIDIA_GEFORCE_7800GT},    /* Geforce 7 - highend */
1577             {"7800",        CARD_NVIDIA_GEFORCE_7800GT},    /* Geforce 7 - highend */
1578             {"7700",        CARD_NVIDIA_GEFORCE_7600},      /* Geforce 7 - midend */
1579             {"7600",        CARD_NVIDIA_GEFORCE_7600},      /* Geforce 7 - midend */
1580             {"7400",        CARD_NVIDIA_GEFORCE_7400},      /* Geforce 7 - lower medium */
1581             {"7300",        CARD_NVIDIA_GEFORCE_7300},      /* Geforce 7 - lowend */
1582             {"6800",        CARD_NVIDIA_GEFORCE_6800},      /* Geforce 6 - highend */
1583             {"6700",        CARD_NVIDIA_GEFORCE_6600GT},    /* Geforce 6 - midend */
1584             {"6610",        CARD_NVIDIA_GEFORCE_6600GT},    /* Geforce 6 - midend */
1585             {"6600",        CARD_NVIDIA_GEFORCE_6600GT},    /* Geforce 6 - midend */
1586         };
1587
1588         for (i = 0; i < sizeof(cards) / sizeof(*cards); ++i)
1589         {
1590             if (strstr(gl_renderer, cards[i].renderer))
1591                 return cards[i].id;
1592         }
1593
1594         /* Geforce 6/7 - lowend */
1595         return CARD_NVIDIA_GEFORCE_6200; /* Geforce 6100/6150/6200/7300/7400/7500 */
1596     }
1597
1598     if (d3d_level >= 9)
1599     {
1600         /* GeforceFX - highend */
1601         if (strstr(gl_renderer, "5800")
1602                 || strstr(gl_renderer, "5900")
1603                 || strstr(gl_renderer, "5950")
1604                 || strstr(gl_renderer, "Quadro FX"))
1605         {
1606             return CARD_NVIDIA_GEFORCEFX_5800;
1607         }
1608
1609         /* GeforceFX - midend */
1610         if (strstr(gl_renderer, "5600")
1611                 || strstr(gl_renderer, "5650")
1612                 || strstr(gl_renderer, "5700")
1613                 || strstr(gl_renderer, "5750"))
1614         {
1615             return CARD_NVIDIA_GEFORCEFX_5600;
1616         }
1617
1618         /* GeforceFX - lowend */
1619         return CARD_NVIDIA_GEFORCEFX_5200; /* GeforceFX 5100/5200/5250/5300/5500 */
1620     }
1621
1622     if (d3d_level >= 8)
1623     {
1624         if (strstr(gl_renderer, "GeForce4 Ti") || strstr(gl_renderer, "Quadro4"))
1625         {
1626             return CARD_NVIDIA_GEFORCE4_TI4200; /* Geforce4 Ti4200/Ti4400/Ti4600/Ti4800, Quadro4 */
1627         }
1628
1629         return CARD_NVIDIA_GEFORCE3; /* Geforce3 standard/Ti200/Ti500, Quadro DCC */
1630     }
1631
1632     if (d3d_level >= 7)
1633     {
1634         if (strstr(gl_renderer, "GeForce4 MX"))
1635         {
1636             return CARD_NVIDIA_GEFORCE4_MX; /* MX420/MX440/MX460/MX4000 */
1637         }
1638
1639         if (strstr(gl_renderer, "GeForce2 MX") || strstr(gl_renderer, "Quadro2 MXR"))
1640         {
1641             return CARD_NVIDIA_GEFORCE2_MX; /* Geforce2 standard/MX100/MX200/MX400, Quadro2 MXR */
1642         }
1643
1644         if (strstr(gl_renderer, "GeForce2") || strstr(gl_renderer, "Quadro2"))
1645         {
1646             return CARD_NVIDIA_GEFORCE2; /* Geforce2 GTS/Pro/Ti/Ultra, Quadro2 */
1647         }
1648
1649         return CARD_NVIDIA_GEFORCE; /* Geforce 256/DDR, Quadro */
1650     }
1651
1652     if (strstr(gl_renderer, "TNT2"))
1653     {
1654         return CARD_NVIDIA_RIVA_TNT2; /* Riva TNT2 standard/M64/Pro/Ultra */
1655     }
1656
1657     return CARD_NVIDIA_RIVA_TNT; /* Riva TNT, Vanta */
1658 }
1659
1660 static enum wined3d_pci_device select_card_amd_binary(const struct wined3d_gl_info *gl_info,
1661         const char *gl_renderer)
1662 {
1663     UINT d3d_level = d3d_level_from_gl_info(gl_info);
1664
1665     /* See http://developer.amd.com/drivers/pc_vendor_id/Pages/default.aspx
1666      *
1667      * Beware: renderer string do not match exact card model,
1668      * eg HD 4800 is returned for multiple cards, even for RV790 based ones. */
1669     if (d3d_level >= 10)
1670     {
1671         unsigned int i;
1672
1673         static const struct
1674         {
1675             const char *renderer;
1676             enum wined3d_pci_device id;
1677         }
1678         cards[] =
1679         {
1680             /* Northern Islands */
1681             {"HD 6900", CARD_AMD_RADEON_HD6900},
1682             {"HD 6800", CARD_AMD_RADEON_HD6800},
1683             {"HD 6630M",CARD_AMD_RADEON_HD6600M},
1684             {"HD 6600M",CARD_AMD_RADEON_HD6600M},
1685             {"HD 6600", CARD_AMD_RADEON_HD6600},
1686             {"HD 6500M",CARD_AMD_RADEON_HD6600M},
1687             {"HD 6500", CARD_AMD_RADEON_HD6600},
1688             {"HD 6400", CARD_AMD_RADEON_HD6400},
1689             {"HD 6300", CARD_AMD_RADEON_HD6300},
1690             {"HD 6200", CARD_AMD_RADEON_HD6300},
1691             /* Evergreen */
1692             {"HD 5870", CARD_AMD_RADEON_HD5800},    /* Radeon EG CYPRESS PRO */
1693             {"HD 5850", CARD_AMD_RADEON_HD5800},    /* Radeon EG CYPRESS XT */
1694             {"HD 5800", CARD_AMD_RADEON_HD5800},    /* Radeon EG CYPRESS HD58xx generic renderer string */
1695             {"HD 5770", CARD_AMD_RADEON_HD5700},    /* Radeon EG JUNIPER XT */
1696             {"HD 5750", CARD_AMD_RADEON_HD5700},    /* Radeon EG JUNIPER LE */
1697             {"HD 5700", CARD_AMD_RADEON_HD5700},    /* Radeon EG JUNIPER HD57xx generic renderer string */
1698             {"HD 5670", CARD_AMD_RADEON_HD5600},    /* Radeon EG REDWOOD XT */
1699             {"HD 5570", CARD_AMD_RADEON_HD5600},    /* Radeon EG REDWOOD PRO mapped to HD5600 series */
1700             {"HD 5550", CARD_AMD_RADEON_HD5600},    /* Radeon EG REDWOOD LE mapped to HD5600 series */
1701             {"HD 5450", CARD_AMD_RADEON_HD5400},    /* Radeon EG CEDAR PRO */
1702             /* R700 */
1703             {"HD 4890", CARD_AMD_RADEON_HD4800},    /* Radeon RV790 */
1704             {"HD 4870", CARD_AMD_RADEON_HD4800},    /* Radeon RV770 */
1705             {"HD 4850", CARD_AMD_RADEON_HD4800},    /* Radeon RV770 */
1706             {"HD 4830", CARD_AMD_RADEON_HD4800},    /* Radeon RV770 */
1707             {"HD 4800", CARD_AMD_RADEON_HD4800},    /* Radeon RV7xx HD48xx generic renderer string */
1708             {"HD 4770", CARD_AMD_RADEON_HD4700},    /* Radeon RV740 */
1709             {"HD 4700", CARD_AMD_RADEON_HD4700},    /* Radeon RV7xx HD47xx generic renderer string */
1710             {"HD 4670", CARD_AMD_RADEON_HD4600},    /* Radeon RV730 */
1711             {"HD 4650", CARD_AMD_RADEON_HD4600},    /* Radeon RV730 */
1712             {"HD 4600", CARD_AMD_RADEON_HD4600},    /* Radeon RV730 */
1713             {"HD 4550", CARD_AMD_RADEON_HD4350},    /* Radeon RV710 */
1714             {"HD 4350", CARD_AMD_RADEON_HD4350},    /* Radeon RV710 */
1715             /* R600/R700 integrated */
1716             {"HD 3300", CARD_AMD_RADEON_HD3200},
1717             {"HD 3200", CARD_AMD_RADEON_HD3200},
1718             {"HD 3100", CARD_AMD_RADEON_HD3200},
1719             /* R600 */
1720             {"HD 3870", CARD_AMD_RADEON_HD2900},    /* HD2900/HD3800 - highend */
1721             {"HD 3850", CARD_AMD_RADEON_HD2900},    /* HD2900/HD3800 - highend */
1722             {"HD 2900", CARD_AMD_RADEON_HD2900},    /* HD2900/HD3800 - highend */
1723             {"HD 3830", CARD_AMD_RADEON_HD2600},    /* China-only midend */
1724             {"HD 3690", CARD_AMD_RADEON_HD2600},    /* HD2600/HD3600 - midend */
1725             {"HD 3650", CARD_AMD_RADEON_HD2600},    /* HD2600/HD3600 - midend */
1726             {"HD 2600", CARD_AMD_RADEON_HD2600},    /* HD2600/HD3600 - midend */
1727             {"HD 3470", CARD_AMD_RADEON_HD2350},    /* HD2350/HD2400/HD3400 - lowend */
1728             {"HD 3450", CARD_AMD_RADEON_HD2350},    /* HD2350/HD2400/HD3400 - lowend */
1729             {"HD 3430", CARD_AMD_RADEON_HD2350},    /* HD2350/HD2400/HD3400 - lowend */
1730             {"HD 3400", CARD_AMD_RADEON_HD2350},    /* HD2350/HD2400/HD3400 - lowend */
1731             {"HD 2400", CARD_AMD_RADEON_HD2350},    /* HD2350/HD2400/HD3400 - lowend */
1732             {"HD 2350", CARD_AMD_RADEON_HD2350},    /* HD2350/HD2400/HD3400 - lowend */
1733         };
1734
1735         for (i = 0; i < sizeof(cards) / sizeof(*cards); ++i)
1736         {
1737             if (strstr(gl_renderer, cards[i].renderer))
1738                 return cards[i].id;
1739         }
1740
1741         /* Default for when no GPU has been found */
1742         return CARD_AMD_RADEON_HD3200;
1743     }
1744
1745     if (d3d_level >= 9)
1746     {
1747         /* Radeon R5xx */
1748         if (strstr(gl_renderer, "X1600")
1749                 || strstr(gl_renderer, "X1650")
1750                 || strstr(gl_renderer, "X1800")
1751                 || strstr(gl_renderer, "X1900")
1752                 || strstr(gl_renderer, "X1950"))
1753         {
1754             return CARD_AMD_RADEON_X1600;
1755         }
1756
1757         /* Radeon R4xx + X1300/X1400/X1450/X1550/X2300/X2500/HD2300 (lowend R5xx)
1758          * Note X2300/X2500/HD2300 are R5xx GPUs with a 2xxx naming but they are still DX9-only */
1759         if (strstr(gl_renderer, "X700")
1760                 || strstr(gl_renderer, "X800")
1761                 || strstr(gl_renderer, "X850")
1762                 || strstr(gl_renderer, "X1300")
1763                 || strstr(gl_renderer, "X1400")
1764                 || strstr(gl_renderer, "X1450")
1765                 || strstr(gl_renderer, "X1550")
1766                 || strstr(gl_renderer, "X2300")
1767                 || strstr(gl_renderer, "X2500")
1768                 || strstr(gl_renderer, "HD 2300")
1769                 )
1770         {
1771             return CARD_AMD_RADEON_X700;
1772         }
1773
1774         /* Radeon Xpress Series - onboard, DX9b, Shader 2.0, 300-400MHz */
1775         if (strstr(gl_renderer, "Radeon Xpress"))
1776         {
1777             return CARD_AMD_RADEON_XPRESS_200M;
1778         }
1779
1780         /* Radeon R3xx */
1781         return CARD_AMD_RADEON_9500; /* Radeon 9500/9550/9600/9700/9800/X300/X550/X600 */
1782     }
1783
1784     if (d3d_level >= 8)
1785         return CARD_AMD_RADEON_8500; /* Radeon 8500/9000/9100/9200/9300 */
1786
1787     if (d3d_level >= 7)
1788         return CARD_AMD_RADEON_7200; /* Radeon 7000/7100/7200/7500 */
1789
1790     return CARD_AMD_RAGE_128PRO;
1791 }
1792
1793 static enum wined3d_pci_device select_card_intel(const struct wined3d_gl_info *gl_info,
1794         const char *gl_renderer)
1795 {
1796     if (strstr(gl_renderer, "GM45")) return CARD_INTEL_GM45;
1797     if (strstr(gl_renderer, "X3100") || strstr(gl_renderer, "965GM"))
1798     {
1799         /* MacOS calls the card GMA X3100, otherwise known as GM965/GL960 */
1800         return CARD_INTEL_X3100;
1801     }
1802
1803     if (strstr(gl_renderer, "GMA 950") || strstr(gl_renderer, "945GM"))
1804     {
1805         /* MacOS calls the card GMA 950, but everywhere else the PCI ID is named 945GM */
1806         return CARD_INTEL_I945GM;
1807     }
1808
1809     if (strstr(gl_renderer, "915GM")) return CARD_INTEL_I915GM;
1810     if (strstr(gl_renderer, "915G")) return CARD_INTEL_I915G;
1811     if (strstr(gl_renderer, "865G")) return CARD_INTEL_I865G;
1812     if (strstr(gl_renderer, "855G")) return CARD_INTEL_I855G;
1813     if (strstr(gl_renderer, "830G")) return CARD_INTEL_I830G;
1814     return CARD_INTEL_I915G;
1815
1816 }
1817
1818 static enum wined3d_pci_device select_card_amd_mesa(const struct wined3d_gl_info *gl_info,
1819         const char *gl_renderer)
1820 {
1821     UINT d3d_level;
1822     unsigned int i;
1823
1824     /* See http://developer.amd.com/drivers/pc_vendor_id/Pages/default.aspx
1825      *
1826      * Beware: renderer string do not match exact card model,
1827      * eg HD 4800 is returned for multiple cards, even for RV790 based ones. */
1828     if (strstr(gl_renderer, "Gallium"))
1829     {
1830         /* 20101109 - These are never returned by current Gallium radeon
1831          * drivers: R700, RV790, R680, RV535, RV516, R410, RS485, RV360, RV351.
1832          *
1833          * These are returned but not handled: RC410, RV380. */
1834         static const struct
1835         {
1836             const char *renderer;
1837             enum wined3d_pci_device id;
1838         }
1839         cards[] =
1840         {
1841             /* Northern Islands */
1842             {"CAYMAN",  CARD_AMD_RADEON_HD6900},
1843             {"BARTS",   CARD_AMD_RADEON_HD6800},
1844             {"TURKS",   CARD_AMD_RADEON_HD6600},
1845             {"SUMO2",   CARD_AMD_RADEON_HD6410D},   /* SUMO2 first, because we do a strstr(). */
1846             {"SUMO",    CARD_AMD_RADEON_HD6550D},
1847             {"CAICOS",  CARD_AMD_RADEON_HD6400},
1848             {"PALM",    CARD_AMD_RADEON_HD6300},
1849             /* Evergreen */
1850             {"HEMLOCK", CARD_AMD_RADEON_HD5900},
1851             {"CYPRESS", CARD_AMD_RADEON_HD5800},
1852             {"JUNIPER", CARD_AMD_RADEON_HD5700},
1853             {"REDWOOD", CARD_AMD_RADEON_HD5600},
1854             {"CEDAR",   CARD_AMD_RADEON_HD5400},
1855             /* R700 */
1856             {"R700",    CARD_AMD_RADEON_HD4800},    /* HD4800 - highend */
1857             {"RV790",   CARD_AMD_RADEON_HD4800},
1858             {"RV770",   CARD_AMD_RADEON_HD4800},
1859             {"RV740",   CARD_AMD_RADEON_HD4700},    /* HD4700 - midend */
1860             {"RV730",   CARD_AMD_RADEON_HD4600},    /* HD4600 - midend */
1861             {"RV710",   CARD_AMD_RADEON_HD4350},    /* HD4500/HD4350 - lowend */
1862             /* R600/R700 integrated */
1863             {"RS880",   CARD_AMD_RADEON_HD3200},
1864             {"RS780",   CARD_AMD_RADEON_HD3200},
1865             /* R600 */
1866             {"R680",    CARD_AMD_RADEON_HD2900},    /* HD2900/HD3800 - highend */
1867             {"R600",    CARD_AMD_RADEON_HD2900},
1868             {"RV670",   CARD_AMD_RADEON_HD2900},
1869             {"RV635",   CARD_AMD_RADEON_HD2600},    /* HD2600/HD3600 - midend; HD3830 is China-only midend */
1870             {"RV630",   CARD_AMD_RADEON_HD2600},
1871             {"RV620",   CARD_AMD_RADEON_HD2350},    /* HD2350/HD2400/HD3400 - lowend */
1872             {"RV610",   CARD_AMD_RADEON_HD2350},
1873             /* R500 */
1874             {"R580",    CARD_AMD_RADEON_X1600},
1875             {"R520",    CARD_AMD_RADEON_X1600},
1876             {"RV570",   CARD_AMD_RADEON_X1600},
1877             {"RV560",   CARD_AMD_RADEON_X1600},
1878             {"RV535",   CARD_AMD_RADEON_X1600},
1879             {"RV530",   CARD_AMD_RADEON_X1600},
1880             {"RV516",   CARD_AMD_RADEON_X700},      /* X700 is actually R400. */
1881             {"RV515",   CARD_AMD_RADEON_X700},
1882             /* R400 */
1883             {"R481",    CARD_AMD_RADEON_X700},
1884             {"R480",    CARD_AMD_RADEON_X700},
1885             {"R430",    CARD_AMD_RADEON_X700},
1886             {"R423",    CARD_AMD_RADEON_X700},
1887             {"R420",    CARD_AMD_RADEON_X700},
1888             {"R410",    CARD_AMD_RADEON_X700},
1889             {"RV410",   CARD_AMD_RADEON_X700},
1890             /* Radeon Xpress - onboard, DX9b, Shader 2.0, 300-400MHz */
1891             {"RS740",   CARD_AMD_RADEON_XPRESS_200M},
1892             {"RS690",   CARD_AMD_RADEON_XPRESS_200M},
1893             {"RS600",   CARD_AMD_RADEON_XPRESS_200M},
1894             {"RS485",   CARD_AMD_RADEON_XPRESS_200M},
1895             {"RS482",   CARD_AMD_RADEON_XPRESS_200M},
1896             {"RS480",   CARD_AMD_RADEON_XPRESS_200M},
1897             {"RS400",   CARD_AMD_RADEON_XPRESS_200M},
1898             /* R300 */
1899             {"R360",    CARD_AMD_RADEON_9500},
1900             {"R350",    CARD_AMD_RADEON_9500},
1901             {"R300",    CARD_AMD_RADEON_9500},
1902             {"RV370",   CARD_AMD_RADEON_9500},
1903             {"RV360",   CARD_AMD_RADEON_9500},
1904             {"RV351",   CARD_AMD_RADEON_9500},
1905             {"RV350",   CARD_AMD_RADEON_9500},
1906         };
1907
1908         for (i = 0; i < sizeof(cards) / sizeof(*cards); ++i)
1909         {
1910             if (strstr(gl_renderer, cards[i].renderer))
1911                 return cards[i].id;
1912         }
1913     }
1914
1915     d3d_level = d3d_level_from_gl_info(gl_info);
1916     if (d3d_level >= 10)
1917         return CARD_AMD_RADEON_HD2600;
1918
1919     if (d3d_level >= 9)
1920     {
1921         static const struct
1922         {
1923             const char *renderer;
1924             enum wined3d_pci_device id;
1925         }
1926         cards[] =
1927         {
1928             /* R700 */
1929             {"(R700",   CARD_AMD_RADEON_HD4800},    /* HD4800 - highend */
1930             {"(RV790",  CARD_AMD_RADEON_HD4800},
1931             {"(RV770",  CARD_AMD_RADEON_HD4800},
1932             {"(RV740",  CARD_AMD_RADEON_HD4700},    /* HD4700 - midend */
1933             {"(RV730",  CARD_AMD_RADEON_HD4600},    /* HD4600 - midend */
1934             {"(RV710",  CARD_AMD_RADEON_HD4350},    /* HD4500/HD4350 - lowend */
1935             /* R600/R700 integrated */
1936             {"RS880",   CARD_AMD_RADEON_HD3200},
1937             {"RS780",   CARD_AMD_RADEON_HD3200},
1938             /* R600 */
1939             {"(R680",   CARD_AMD_RADEON_HD2900},    /* HD2900/HD3800 - highend */
1940             {"(R600",   CARD_AMD_RADEON_HD2900},
1941             {"(RV670",  CARD_AMD_RADEON_HD2900},
1942             {"(RV635",  CARD_AMD_RADEON_HD2600},    /* HD2600/HD3600 - midend; HD3830 is China-only midend */
1943             {"(RV630",  CARD_AMD_RADEON_HD2600},
1944             {"(RV620",  CARD_AMD_RADEON_HD2350},    /* HD2300/HD2400/HD3400 - lowend */
1945             {"(RV610",  CARD_AMD_RADEON_HD2350},
1946         };
1947
1948         for (i = 0; i < sizeof(cards) / sizeof(*cards); ++i)
1949         {
1950             if (strstr(gl_renderer, cards[i].renderer))
1951                 return cards[i].id;
1952         }
1953
1954         return CARD_AMD_RADEON_9500;
1955     }
1956
1957     if (d3d_level >= 8)
1958         return CARD_AMD_RADEON_8500; /* Radeon 8500/9000/9100/9200/9300 */
1959
1960     if (d3d_level >= 7)
1961         return CARD_AMD_RADEON_7200; /* Radeon 7000/7100/7200/7500 */
1962
1963     return CARD_AMD_RAGE_128PRO;
1964 }
1965
1966 static enum wined3d_pci_device select_card_nvidia_mesa(const struct wined3d_gl_info *gl_info,
1967         const char *gl_renderer)
1968 {
1969     UINT d3d_level;
1970
1971     if (strstr(gl_renderer, "Gallium"))
1972     {
1973         unsigned int i;
1974
1975         static const struct
1976         {
1977             const char *renderer;
1978             enum wined3d_pci_device id;
1979         }
1980         cards[] =
1981         {
1982             {"NVC8",    CARD_NVIDIA_GEFORCE_GTX570},
1983             {"NVC4",    CARD_NVIDIA_GEFORCE_GTX460},
1984             {"NVC3",    CARD_NVIDIA_GEFORCE_GT440},
1985             {"NVC0",    CARD_NVIDIA_GEFORCE_GTX480},
1986             {"NVAF",    CARD_NVIDIA_GEFORCE_GT320M},
1987             {"NVAC",    CARD_NVIDIA_GEFORCE_8200},
1988             {"NVAA",    CARD_NVIDIA_GEFORCE_8200},
1989             {"NVA8",    CARD_NVIDIA_GEFORCE_210},
1990             {"NVA5",    CARD_NVIDIA_GEFORCE_GT220},
1991             {"NVA3",    CARD_NVIDIA_GEFORCE_GT240},
1992             {"NVA0",    CARD_NVIDIA_GEFORCE_GTX280},
1993             {"NV98",    CARD_NVIDIA_GEFORCE_9200},
1994             {"NV96",    CARD_NVIDIA_GEFORCE_9400GT},
1995             {"NV94",    CARD_NVIDIA_GEFORCE_9600GT},
1996             {"NV92",    CARD_NVIDIA_GEFORCE_9800GT},
1997             {"NV86",    CARD_NVIDIA_GEFORCE_8500GT},
1998             {"NV84",    CARD_NVIDIA_GEFORCE_8600GT},
1999             {"NV68",    CARD_NVIDIA_GEFORCE_6200},      /* 7050 */
2000             {"NV67",    CARD_NVIDIA_GEFORCE_6200},      /* 7000M */
2001             {"NV63",    CARD_NVIDIA_GEFORCE_6200},      /* 7100 */
2002             {"NV50",    CARD_NVIDIA_GEFORCE_8800GTX},
2003             {"NV4E",    CARD_NVIDIA_GEFORCE_6200},      /* 6100 Go / 6150 Go */
2004             {"NV4C",    CARD_NVIDIA_GEFORCE_6200},      /* 6150SE */
2005             {"NV4B",    CARD_NVIDIA_GEFORCE_7600},
2006             {"NV4A",    CARD_NVIDIA_GEFORCE_6200},
2007             {"NV49",    CARD_NVIDIA_GEFORCE_7800GT},    /* 7900 */
2008             {"NV47",    CARD_NVIDIA_GEFORCE_7800GT},
2009             {"NV46",    CARD_NVIDIA_GEFORCE_7400},
2010             {"NV45",    CARD_NVIDIA_GEFORCE_6800},
2011             {"NV44",    CARD_NVIDIA_GEFORCE_6200},
2012             {"NV43",    CARD_NVIDIA_GEFORCE_6600GT},
2013             {"NV42",    CARD_NVIDIA_GEFORCE_6800},
2014             {"NV41",    CARD_NVIDIA_GEFORCE_6800},
2015             {"NV40",    CARD_NVIDIA_GEFORCE_6800},
2016             {"NV38",    CARD_NVIDIA_GEFORCEFX_5800},    /* FX 5950 Ultra */
2017             {"NV36",    CARD_NVIDIA_GEFORCEFX_5800},    /* FX 5700/5750 */
2018             {"NV35",    CARD_NVIDIA_GEFORCEFX_5800},    /* FX 5900 */
2019             {"NV34",    CARD_NVIDIA_GEFORCEFX_5200},
2020             {"NV31",    CARD_NVIDIA_GEFORCEFX_5600},
2021             {"NV30",    CARD_NVIDIA_GEFORCEFX_5800},
2022             {"NV28",    CARD_NVIDIA_GEFORCE4_TI4200},
2023             {"NV25",    CARD_NVIDIA_GEFORCE4_TI4200},
2024             {"NV20",    CARD_NVIDIA_GEFORCE3},
2025             {"NV1F",    CARD_NVIDIA_GEFORCE4_MX},       /* GF4 MX IGP */
2026             {"NV1A",    CARD_NVIDIA_GEFORCE2},          /* GF2 IGP */
2027             {"NV18",    CARD_NVIDIA_GEFORCE4_MX},
2028             {"NV17",    CARD_NVIDIA_GEFORCE4_MX},
2029             {"NV16",    CARD_NVIDIA_GEFORCE2},
2030             {"NV15",    CARD_NVIDIA_GEFORCE2},
2031             {"NV11",    CARD_NVIDIA_GEFORCE2_MX},
2032             {"NV10",    CARD_NVIDIA_GEFORCE},
2033             {"NV05",    CARD_NVIDIA_RIVA_TNT2},
2034             {"NV04",    CARD_NVIDIA_RIVA_TNT},
2035             {"NV03",    CARD_NVIDIA_RIVA_128},
2036         };
2037
2038         for (i = 0; i < sizeof(cards) / sizeof(*cards); ++i)
2039         {
2040             if (strstr(gl_renderer, cards[i].renderer))
2041                 return cards[i].id;
2042         }
2043     }
2044
2045     FIXME_(d3d_caps)("Unknown renderer %s.\n", debugstr_a(gl_renderer));
2046
2047     d3d_level = d3d_level_from_gl_info(gl_info);
2048     if (d3d_level >= 9)
2049         return CARD_NVIDIA_GEFORCEFX_5600;
2050     if (d3d_level >= 8)
2051         return CARD_NVIDIA_GEFORCE3;
2052     if (d3d_level >= 7)
2053         return CARD_NVIDIA_GEFORCE;
2054     if (d3d_level >= 6)
2055         return CARD_NVIDIA_RIVA_TNT;
2056     return CARD_NVIDIA_RIVA_128;
2057 }
2058
2059
2060 struct vendor_card_selection
2061 {
2062     enum wined3d_gl_vendor gl_vendor;
2063     enum wined3d_pci_vendor card_vendor;
2064     const char *description;        /* Description of the card selector i.e. Apple OS/X Intel */
2065     enum wined3d_pci_device (*select_card)(const struct wined3d_gl_info *gl_info, const char *gl_renderer);
2066 };
2067
2068 static const struct vendor_card_selection vendor_card_select_table[] =
2069 {
2070     {GL_VENDOR_NVIDIA, HW_VENDOR_NVIDIA,  "Nvidia binary driver",     select_card_nvidia_binary},
2071     {GL_VENDOR_APPLE,  HW_VENDOR_NVIDIA,  "Apple OSX NVidia binary driver",   select_card_nvidia_binary},
2072     {GL_VENDOR_APPLE,  HW_VENDOR_AMD,     "Apple OSX AMD/ATI binary driver",  select_card_amd_binary},
2073     {GL_VENDOR_APPLE,  HW_VENDOR_INTEL,   "Apple OSX Intel binary driver",    select_card_intel},
2074     {GL_VENDOR_FGLRX,  HW_VENDOR_AMD,     "AMD/ATI binary driver",    select_card_amd_binary},
2075     {GL_VENDOR_MESA,   HW_VENDOR_AMD,     "Mesa AMD/ATI driver",      select_card_amd_mesa},
2076     {GL_VENDOR_MESA,   HW_VENDOR_NVIDIA,  "Mesa Nouveau driver",      select_card_nvidia_mesa},
2077     {GL_VENDOR_MESA,   HW_VENDOR_INTEL,   "Mesa Intel driver",        select_card_intel},
2078     {GL_VENDOR_INTEL,  HW_VENDOR_INTEL,   "Mesa Intel driver",        select_card_intel}
2079 };
2080
2081
2082 static enum wined3d_pci_device wined3d_guess_card(const struct wined3d_gl_info *gl_info, const char *gl_renderer,
2083         enum wined3d_gl_vendor *gl_vendor, enum wined3d_pci_vendor *card_vendor)
2084 {
2085     UINT d3d_level;
2086
2087     /* Above is a list of Nvidia and ATI GPUs. Both vendors have dozens of
2088      * different GPUs with roughly the same features. In most cases GPUs from a
2089      * certain family differ in clockspeeds, the amount of video memory and the
2090      * number of shader pipelines.
2091      *
2092      * A Direct3D device object contains the PCI id (vendor + device) of the
2093      * videocard which is used for rendering. Various applications use this
2094      * information to get a rough estimation of the features of the card and
2095      * some might use it for enabling 3d effects only on certain types of
2096      * videocards. In some cases games might even use it to work around bugs
2097      * which happen on certain videocards/driver combinations. The problem is
2098      * that OpenGL only exposes a rendering string containing the name of the
2099      * videocard and not the PCI id.
2100      *
2101      * Various games depend on the PCI id, so somehow we need to provide one.
2102      * A simple option is to parse the renderer string and translate this to
2103      * the right PCI id. This is a lot of work because there are more than 200
2104      * GPUs just for Nvidia. Various cards share the same renderer string, so
2105      * the amount of code might be 'small' but there are quite a number of
2106      * exceptions which would make this a pain to maintain. Another way would
2107      * be to query the PCI id from the operating system (assuming this is the
2108      * videocard which is used for rendering which is not always the case).
2109      * This would work but it is not very portable. Second it would not work
2110      * well in, let's say, a remote X situation in which the amount of 3d
2111      * features which can be used is limited.
2112      *
2113      * As said most games only use the PCI id to get an indication of the
2114      * capabilities of the card. It doesn't really matter if the given id is
2115      * the correct one if we return the id of a card with similar 3d features.
2116      *
2117      * The code below checks the OpenGL capabilities of a videocard and matches
2118      * that to a certain level of Direct3D functionality. Once a card passes
2119      * the Direct3D9 check, we know that the card (in case of Nvidia) is at
2120      * least a GeforceFX. To give a better estimate we do a basic check on the
2121      * renderer string but if that won't pass we return a default card. This
2122      * way is better than maintaining a full card database as even without a
2123      * full database we can return a card with similar features. Second the
2124      * size of the database can be made quite small because when you know what
2125      * type of 3d functionality a card has, you know to which GPU family the
2126      * GPU must belong. Because of this you only have to check a small part of
2127      * the renderer string to distinguishes between different models from that
2128      * family.
2129      *
2130      * The code also selects a default amount of video memory which we will
2131      * use for an estimation of the amount of free texture memory. In case of
2132      * real D3D the amount of texture memory includes video memory and system
2133      * memory (to be specific AGP memory or in case of PCIE TurboCache /
2134      * HyperMemory). We don't know how much system memory can be addressed by
2135      * the system but we can make a reasonable estimation about the amount of
2136      * video memory. If the value is slightly wrong it doesn't matter as we
2137      * didn't include AGP-like memory which makes the amount of addressable
2138      * memory higher and second OpenGL isn't that critical it moves to system
2139      * memory behind our backs if really needed. Note that the amount of video
2140      * memory can be overruled using a registry setting. */
2141
2142     int i;
2143
2144     for (i = 0; i < (sizeof(vendor_card_select_table) / sizeof(*vendor_card_select_table)); ++i)
2145     {
2146         if ((vendor_card_select_table[i].gl_vendor != *gl_vendor)
2147             || (vendor_card_select_table[i].card_vendor != *card_vendor))
2148                 continue;
2149         TRACE_(d3d_caps)("Applying card_selector \"%s\".\n", vendor_card_select_table[i].description);
2150         return vendor_card_select_table[i].select_card(gl_info, gl_renderer);
2151     }
2152
2153     FIXME_(d3d_caps)("No card selector available for GL vendor %#x and card vendor %04x (using GL_RENDERER %s).\n",
2154             *gl_vendor, *card_vendor, debugstr_a(gl_renderer));
2155
2156     /* Default to generic Nvidia hardware based on the supported OpenGL extensions. The choice
2157      * for Nvidia was because the hardware and drivers they make are of good quality. This makes
2158      * them a good generic choice. */
2159     *card_vendor = HW_VENDOR_NVIDIA;
2160     d3d_level = d3d_level_from_gl_info(gl_info);
2161     if (d3d_level >= 9)
2162         return CARD_NVIDIA_GEFORCEFX_5600;
2163     if (d3d_level >= 8)
2164         return CARD_NVIDIA_GEFORCE3;
2165     if (d3d_level >= 7)
2166         return CARD_NVIDIA_GEFORCE;
2167     if (d3d_level >= 6)
2168         return CARD_NVIDIA_RIVA_TNT;
2169     return CARD_NVIDIA_RIVA_128;
2170 }
2171
2172 static const struct fragment_pipeline *select_fragment_implementation(const struct wined3d_gl_info *gl_info)
2173 {
2174     int vs_selected_mode, ps_selected_mode;
2175
2176     select_shader_mode(gl_info, &ps_selected_mode, &vs_selected_mode);
2177     if ((ps_selected_mode == SHADER_ARB || ps_selected_mode == SHADER_GLSL)
2178             && gl_info->supported[ARB_FRAGMENT_PROGRAM]) return &arbfp_fragment_pipeline;
2179     else if (ps_selected_mode == SHADER_ATI) return &atifs_fragment_pipeline;
2180     else if (gl_info->supported[NV_REGISTER_COMBINERS]
2181             && gl_info->supported[NV_TEXTURE_SHADER2]) return &nvts_fragment_pipeline;
2182     else if (gl_info->supported[NV_REGISTER_COMBINERS]) return &nvrc_fragment_pipeline;
2183     else return &ffp_fragment_pipeline;
2184 }
2185
2186 static const struct wined3d_shader_backend_ops *select_shader_backend(const struct wined3d_gl_info *gl_info)
2187 {
2188     int vs_selected_mode, ps_selected_mode;
2189
2190     select_shader_mode(gl_info, &ps_selected_mode, &vs_selected_mode);
2191     if (vs_selected_mode == SHADER_GLSL || ps_selected_mode == SHADER_GLSL) return &glsl_shader_backend;
2192     if (vs_selected_mode == SHADER_ARB || ps_selected_mode == SHADER_ARB) return &arb_program_shader_backend;
2193     return &none_shader_backend;
2194 }
2195
2196 static const struct blit_shader *select_blit_implementation(const struct wined3d_gl_info *gl_info)
2197 {
2198     int vs_selected_mode, ps_selected_mode;
2199
2200     select_shader_mode(gl_info, &ps_selected_mode, &vs_selected_mode);
2201     if ((ps_selected_mode == SHADER_ARB || ps_selected_mode == SHADER_GLSL)
2202             && gl_info->supported[ARB_FRAGMENT_PROGRAM]) return &arbfp_blit;
2203     else return &ffp_blit;
2204 }
2205
2206 static void load_gl_funcs(struct wined3d_gl_info *gl_info, DWORD gl_version)
2207 {
2208     DWORD ver;
2209
2210 #define USE_GL_FUNC(type, pfn, ext, replace) \
2211     if (gl_info->supported[ext]) gl_info->pfn = (type)pwglGetProcAddress(#pfn); \
2212     else if ((ver = ver_for_ext(ext)) && ver <= gl_version) gl_info->pfn = (type)pwglGetProcAddress(#replace); \
2213     else gl_info->pfn = NULL;
2214
2215     GL_EXT_FUNCS_GEN;
2216 #undef USE_GL_FUNC
2217
2218 #define USE_GL_FUNC(type, pfn, ext, replace) gl_info->pfn = (type)pwglGetProcAddress(#pfn);
2219     WGL_EXT_FUNCS_GEN;
2220 #undef USE_GL_FUNC
2221 }
2222
2223 /* Context activation is done by the caller. */
2224 static BOOL wined3d_adapter_init_gl_caps(struct wined3d_adapter *adapter)
2225 {
2226     struct wined3d_driver_info *driver_info = &adapter->driver_info;
2227     struct wined3d_gl_info *gl_info = &adapter->gl_info;
2228     const char *GL_Extensions    = NULL;
2229     const char *WGL_Extensions   = NULL;
2230     const char *gl_vendor_str, *gl_renderer_str, *gl_version_str;
2231     struct fragment_caps fragment_caps;
2232     enum wined3d_gl_vendor gl_vendor;
2233     enum wined3d_pci_vendor card_vendor;
2234     enum wined3d_pci_device device;
2235     GLint       gl_max;
2236     GLfloat     gl_floatv[2];
2237     unsigned    i;
2238     HDC         hdc;
2239     DWORD gl_version;
2240     size_t len;
2241
2242     TRACE_(d3d_caps)("(%p)\n", gl_info);
2243
2244     ENTER_GL();
2245
2246     gl_renderer_str = (const char *)glGetString(GL_RENDERER);
2247     TRACE_(d3d_caps)("GL_RENDERER: %s.\n", debugstr_a(gl_renderer_str));
2248     if (!gl_renderer_str)
2249     {
2250         LEAVE_GL();
2251         ERR_(d3d_caps)("Received a NULL GL_RENDERER.\n");
2252         return FALSE;
2253     }
2254
2255     gl_vendor_str = (const char *)glGetString(GL_VENDOR);
2256     TRACE_(d3d_caps)("GL_VENDOR: %s.\n", debugstr_a(gl_vendor_str));
2257     if (!gl_vendor_str)
2258     {
2259         LEAVE_GL();
2260         ERR_(d3d_caps)("Received a NULL GL_VENDOR.\n");
2261         return FALSE;
2262     }
2263
2264     /* Parse the GL_VERSION field into major and minor information */
2265     gl_version_str = (const char *)glGetString(GL_VERSION);
2266     TRACE_(d3d_caps)("GL_VERSION: %s.\n", debugstr_a(gl_version_str));
2267     if (!gl_version_str)
2268     {
2269         LEAVE_GL();
2270         ERR_(d3d_caps)("Received a NULL GL_VERSION.\n");
2271         return FALSE;
2272     }
2273     gl_version = wined3d_parse_gl_version(gl_version_str);
2274
2275     /*
2276      * Initialize openGL extension related variables
2277      *  with Default values
2278      */
2279     memset(gl_info->supported, 0, sizeof(gl_info->supported));
2280     gl_info->limits.blends = 1;
2281     gl_info->limits.buffers = 1;
2282     gl_info->limits.textures = 1;
2283     gl_info->limits.texture_coords = 1;
2284     gl_info->limits.fragment_samplers = 1;
2285     gl_info->limits.vertex_samplers = 0;
2286     gl_info->limits.combined_samplers = gl_info->limits.fragment_samplers + gl_info->limits.vertex_samplers;
2287     gl_info->limits.sampler_stages = 1;
2288     gl_info->limits.vertex_attribs = 16;
2289     gl_info->limits.glsl_vs_float_constants = 0;
2290     gl_info->limits.glsl_ps_float_constants = 0;
2291     gl_info->limits.arb_vs_float_constants = 0;
2292     gl_info->limits.arb_vs_native_constants = 0;
2293     gl_info->limits.arb_vs_instructions = 0;
2294     gl_info->limits.arb_vs_temps = 0;
2295     gl_info->limits.arb_ps_float_constants = 0;
2296     gl_info->limits.arb_ps_local_constants = 0;
2297     gl_info->limits.arb_ps_instructions = 0;
2298     gl_info->limits.arb_ps_temps = 0;
2299
2300     /* Retrieve opengl defaults */
2301     glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
2302     gl_info->limits.clipplanes = min(WINED3DMAXUSERCLIPPLANES, gl_max);
2303     TRACE_(d3d_caps)("ClipPlanes support - num Planes=%d\n", gl_max);
2304
2305     glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
2306     gl_info->limits.lights = gl_max;
2307     TRACE_(d3d_caps)("Lights support - max lights=%d\n", gl_max);
2308
2309     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_max);
2310     gl_info->limits.texture_size = gl_max;
2311     TRACE_(d3d_caps)("Maximum texture size support - max texture size=%d\n", gl_max);
2312
2313     glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, gl_floatv);
2314     gl_info->limits.pointsize_min = gl_floatv[0];
2315     gl_info->limits.pointsize_max = gl_floatv[1];
2316     TRACE_(d3d_caps)("Maximum point size support - max point size=%f\n", gl_floatv[1]);
2317
2318     /* Parse the gl supported features, in theory enabling parts of our code appropriately. */
2319     GL_Extensions = (const char *)glGetString(GL_EXTENSIONS);
2320     if (!GL_Extensions)
2321     {
2322         LEAVE_GL();
2323         ERR_(d3d_caps)("Received a NULL GL_EXTENSIONS.\n");
2324         return FALSE;
2325     }
2326
2327     LEAVE_GL();
2328
2329     TRACE_(d3d_caps)("GL_Extensions reported:\n");
2330
2331     gl_info->supported[WINED3D_GL_EXT_NONE] = TRUE;
2332
2333     while (*GL_Extensions)
2334     {
2335         const char *start;
2336
2337         while (isspace(*GL_Extensions)) ++GL_Extensions;
2338         start = GL_Extensions;
2339         while (!isspace(*GL_Extensions) && *GL_Extensions) ++GL_Extensions;
2340
2341         len = GL_Extensions - start;
2342         if (!len) continue;
2343
2344         TRACE_(d3d_caps)("- %s\n", debugstr_an(start, len));
2345
2346         for (i = 0; i < (sizeof(EXTENSION_MAP) / sizeof(*EXTENSION_MAP)); ++i)
2347         {
2348             if (len == strlen(EXTENSION_MAP[i].extension_string)
2349                     && !memcmp(start, EXTENSION_MAP[i].extension_string, len))
2350             {
2351                 TRACE_(d3d_caps)(" FOUND: %s support.\n", EXTENSION_MAP[i].extension_string);
2352                 gl_info->supported[EXTENSION_MAP[i].extension] = TRUE;
2353                 break;
2354             }
2355         }
2356     }
2357
2358     /* Now work out what GL support this card really has */
2359     load_gl_funcs( gl_info, gl_version );
2360
2361     ENTER_GL();
2362
2363     /* Now mark all the extensions supported which are included in the opengl core version. Do this *after*
2364      * loading the functions, otherwise the code above will load the extension entry points instead of the
2365      * core functions, which may not work. */
2366     for (i = 0; i < (sizeof(EXTENSION_MAP) / sizeof(*EXTENSION_MAP)); ++i)
2367     {
2368         if (!gl_info->supported[EXTENSION_MAP[i].extension]
2369                 && EXTENSION_MAP[i].version <= gl_version && EXTENSION_MAP[i].version)
2370         {
2371             TRACE_(d3d_caps)(" GL CORE: %s support.\n", EXTENSION_MAP[i].extension_string);
2372             gl_info->supported[EXTENSION_MAP[i].extension] = TRUE;
2373         }
2374     }
2375
2376     if (gl_version >= MAKEDWORD_VERSION(2, 0)) gl_info->supported[WINED3D_GL_VERSION_2_0] = TRUE;
2377
2378     if (gl_info->supported[APPLE_FENCE])
2379     {
2380         /* GL_NV_fence and GL_APPLE_fence provide the same functionality basically.
2381          * The apple extension interacts with some other apple exts. Disable the NV
2382          * extension if the apple one is support to prevent confusion in other parts
2383          * of the code. */
2384         gl_info->supported[NV_FENCE] = FALSE;
2385     }
2386     if (gl_info->supported[APPLE_FLOAT_PIXELS])
2387     {
2388         /* GL_APPLE_float_pixels == GL_ARB_texture_float + GL_ARB_half_float_pixel
2389          *
2390          * The enums are the same:
2391          * GL_RGBA16F_ARB     = GL_RGBA_FLOAT16_APPLE = 0x881A
2392          * GL_RGB16F_ARB      = GL_RGB_FLOAT16_APPLE  = 0x881B
2393          * GL_RGBA32F_ARB     = GL_RGBA_FLOAT32_APPLE = 0x8814
2394          * GL_RGB32F_ARB      = GL_RGB_FLOAT32_APPLE  = 0x8815
2395          * GL_HALF_FLOAT_ARB  = GL_HALF_APPLE         = 0x140B
2396          */
2397         if (!gl_info->supported[ARB_TEXTURE_FLOAT])
2398         {
2399             TRACE_(d3d_caps)(" IMPLIED: GL_ARB_texture_float support(from GL_APPLE_float_pixels.\n");
2400             gl_info->supported[ARB_TEXTURE_FLOAT] = TRUE;
2401         }
2402         if (!gl_info->supported[ARB_HALF_FLOAT_PIXEL])
2403         {
2404             TRACE_(d3d_caps)(" IMPLIED: GL_ARB_half_float_pixel support(from GL_APPLE_float_pixels.\n");
2405             gl_info->supported[ARB_HALF_FLOAT_PIXEL] = TRUE;
2406         }
2407     }
2408     if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2409     {
2410         /* GL_ARB_map_buffer_range and GL_APPLE_flush_buffer_range provide the same
2411          * functionality. Prefer the ARB extension */
2412         gl_info->supported[APPLE_FLUSH_BUFFER_RANGE] = FALSE;
2413     }
2414     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2415     {
2416         TRACE_(d3d_caps)(" IMPLIED: NVIDIA (NV) Texture Gen Reflection support.\n");
2417         gl_info->supported[NV_TEXGEN_REFLECTION] = TRUE;
2418     }
2419     if (!gl_info->supported[ARB_DEPTH_CLAMP] && gl_info->supported[NV_DEPTH_CLAMP])
2420     {
2421         TRACE_(d3d_caps)(" IMPLIED: ARB_depth_clamp support (by NV_depth_clamp).\n");
2422         gl_info->supported[ARB_DEPTH_CLAMP] = TRUE;
2423     }
2424     if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA] && gl_info->supported[EXT_VERTEX_ARRAY_BGRA])
2425     {
2426         TRACE_(d3d_caps)(" IMPLIED: ARB_vertex_array_bgra support (by EXT_vertex_array_bgra).\n");
2427         gl_info->supported[ARB_VERTEX_ARRAY_BGRA] = TRUE;
2428     }
2429     if (!gl_info->supported[ARB_TEXTURE_COMPRESSION_RGTC] && gl_info->supported[EXT_TEXTURE_COMPRESSION_RGTC])
2430     {
2431         TRACE_(d3d_caps)(" IMPLIED: ARB_texture_compression_rgtc support (by EXT_texture_compression_rgtc).\n");
2432         gl_info->supported[ARB_TEXTURE_COMPRESSION_RGTC] = TRUE;
2433     }
2434     if (gl_info->supported[NV_TEXTURE_SHADER2])
2435     {
2436         if (gl_info->supported[NV_REGISTER_COMBINERS])
2437         {
2438             /* Also disable ATI_FRAGMENT_SHADER if register combiners and texture_shader2
2439              * are supported. The nv extensions provide the same functionality as the
2440              * ATI one, and a bit more(signed pixelformats). */
2441             gl_info->supported[ATI_FRAGMENT_SHADER] = FALSE;
2442         }
2443     }
2444
2445     if (gl_info->supported[ARB_MAP_BUFFER_ALIGNMENT])
2446     {
2447         glGetIntegerv(GL_MIN_MAP_BUFFER_ALIGNMENT, &gl_max);
2448         TRACE_(d3d_caps)("Minimum buffer map alignment: %d.\n", gl_max);
2449     }
2450     else
2451     {
2452         WARN_(d3d_caps)("Driver doesn't guarantee a minimum buffer map alignment.\n");
2453     }
2454     if (gl_info->supported[NV_REGISTER_COMBINERS])
2455     {
2456         glGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &gl_max);
2457         gl_info->limits.general_combiners = gl_max;
2458         TRACE_(d3d_caps)("Max general combiners: %d.\n", gl_max);
2459     }
2460     if (gl_info->supported[ARB_DRAW_BUFFERS])
2461     {
2462         glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &gl_max);
2463         gl_info->limits.buffers = gl_max;
2464         TRACE_(d3d_caps)("Max draw buffers: %u.\n", gl_max);
2465     }
2466     if (gl_info->supported[ARB_MULTITEXTURE])
2467     {
2468         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
2469         gl_info->limits.textures = min(MAX_TEXTURES, gl_max);
2470         TRACE_(d3d_caps)("Max textures: %d.\n", gl_info->limits.textures);
2471         glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &gl_max);
2472         gl_info->limits.texture_coords = min(MAX_TEXTURES, gl_max);
2473         TRACE_(d3d_caps)("Max texture coords: %d.\n", gl_info->limits.texture_coords);
2474
2475         if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2476         {
2477             GLint tmp;
2478             glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &tmp);
2479             gl_info->limits.fragment_samplers = min(MAX_FRAGMENT_SAMPLERS, tmp);
2480         }
2481         else
2482         {
2483             gl_info->limits.fragment_samplers = max(gl_info->limits.fragment_samplers, gl_max);
2484         }
2485         TRACE_(d3d_caps)("Max fragment samplers: %d.\n", gl_info->limits.fragment_samplers);
2486
2487         if (gl_info->supported[ARB_VERTEX_SHADER])
2488         {
2489             GLint tmp;
2490             glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &tmp);
2491             gl_info->limits.vertex_samplers = tmp;
2492             glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, &tmp);
2493             gl_info->limits.combined_samplers = tmp;
2494             glGetIntegerv(GL_MAX_VERTEX_ATTRIBS_ARB, &tmp);
2495             gl_info->limits.vertex_attribs = tmp;
2496
2497             /* Loading GLSL sampler uniforms is much simpler if we can assume that the sampler setup
2498              * is known at shader link time. In a vertex shader + pixel shader combination this isn't
2499              * an issue because then the sampler setup only depends on the two shaders. If a pixel
2500              * shader is used with fixed function vertex processing we're fine too because fixed function
2501              * vertex processing doesn't use any samplers. If fixed function fragment processing is
2502              * used we have to make sure that all vertex sampler setups are valid together with all
2503              * possible fixed function fragment processing setups. This is true if vsamplers + MAX_TEXTURES
2504              * <= max_samplers. This is true on all d3d9 cards that support vtf(gf 6 and gf7 cards).
2505              * dx9 radeon cards do not support vertex texture fetch. DX10 cards have 128 samplers, and
2506              * dx9 is limited to 8 fixed function texture stages and 4 vertex samplers. DX10 does not have
2507              * a fixed function pipeline anymore.
2508              *
2509              * So this is just a check to check that our assumption holds true. If not, write a warning
2510              * and reduce the number of vertex samplers or probably disable vertex texture fetch. */
2511             if (gl_info->limits.vertex_samplers && gl_info->limits.combined_samplers < 12
2512                     && MAX_TEXTURES + gl_info->limits.vertex_samplers > gl_info->limits.combined_samplers)
2513             {
2514                 FIXME("OpenGL implementation supports %u vertex samplers and %u total samplers.\n",
2515                         gl_info->limits.vertex_samplers, gl_info->limits.combined_samplers);
2516                 FIXME("Expected vertex samplers + MAX_TEXTURES(=8) > combined_samplers.\n");
2517                 if (gl_info->limits.combined_samplers > MAX_TEXTURES)
2518                     gl_info->limits.vertex_samplers = gl_info->limits.combined_samplers - MAX_TEXTURES;
2519                 else
2520                     gl_info->limits.vertex_samplers = 0;
2521             }
2522         }
2523         else
2524         {
2525             gl_info->limits.combined_samplers = gl_info->limits.fragment_samplers;
2526         }
2527         TRACE_(d3d_caps)("Max vertex samplers: %u.\n", gl_info->limits.vertex_samplers);
2528         TRACE_(d3d_caps)("Max combined samplers: %u.\n", gl_info->limits.combined_samplers);
2529     }
2530     if (gl_info->supported[ARB_VERTEX_BLEND])
2531     {
2532         glGetIntegerv(GL_MAX_VERTEX_UNITS_ARB, &gl_max);
2533         gl_info->limits.blends = gl_max;
2534         TRACE_(d3d_caps)("Max blends: %u.\n", gl_info->limits.blends);
2535     }
2536     if (gl_info->supported[EXT_TEXTURE3D])
2537     {
2538         glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &gl_max);
2539         gl_info->limits.texture3d_size = gl_max;
2540         TRACE_(d3d_caps)("Max texture3D size: %d.\n", gl_info->limits.texture3d_size);
2541     }
2542     if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
2543     {
2544         glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_max);
2545         gl_info->limits.anisotropy = gl_max;
2546         TRACE_(d3d_caps)("Max anisotropy: %d.\n", gl_info->limits.anisotropy);
2547     }
2548     if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2549     {
2550         GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &gl_max));
2551         gl_info->limits.arb_ps_float_constants = gl_max;
2552         TRACE_(d3d_caps)("Max ARB_FRAGMENT_PROGRAM float constants: %d.\n", gl_info->limits.arb_ps_float_constants);
2553         GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB, &gl_max));
2554         gl_info->limits.arb_ps_native_constants = gl_max;
2555         TRACE_(d3d_caps)("Max ARB_FRAGMENT_PROGRAM native float constants: %d.\n",
2556                 gl_info->limits.arb_ps_native_constants);
2557         GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB, &gl_max));
2558         gl_info->limits.arb_ps_temps = gl_max;
2559         TRACE_(d3d_caps)("Max ARB_FRAGMENT_PROGRAM native temporaries: %d.\n", gl_info->limits.arb_ps_temps);
2560         GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, &gl_max));
2561         gl_info->limits.arb_ps_instructions = gl_max;
2562         TRACE_(d3d_caps)("Max ARB_FRAGMENT_PROGRAM native instructions: %d.\n", gl_info->limits.arb_ps_instructions);
2563         GL_EXTCALL(glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &gl_max));
2564         gl_info->limits.arb_ps_local_constants = gl_max;
2565         TRACE_(d3d_caps)("Max ARB_FRAGMENT_PROGRAM local parameters: %d.\n", gl_info->limits.arb_ps_instructions);
2566     }
2567     if (gl_info->supported[ARB_VERTEX_PROGRAM])
2568     {
2569         GL_EXTCALL(glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &gl_max));
2570         gl_info->limits.arb_vs_float_constants = gl_max;
2571         TRACE_(d3d_caps)("Max ARB_VERTEX_PROGRAM float constants: %d.\n", gl_info->limits.arb_vs_float_constants);
2572         GL_EXTCALL(glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB, &gl_max));
2573         gl_info->limits.arb_vs_native_constants = gl_max;
2574         TRACE_(d3d_caps)("Max ARB_VERTEX_PROGRAM native float constants: %d.\n",
2575                 gl_info->limits.arb_vs_native_constants);
2576         GL_EXTCALL(glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB, &gl_max));
2577         gl_info->limits.arb_vs_temps = gl_max;
2578         TRACE_(d3d_caps)("Max ARB_VERTEX_PROGRAM native temporaries: %d.\n", gl_info->limits.arb_vs_temps);
2579         GL_EXTCALL(glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, &gl_max));
2580         gl_info->limits.arb_vs_instructions = gl_max;
2581         TRACE_(d3d_caps)("Max ARB_VERTEX_PROGRAM native instructions: %d.\n", gl_info->limits.arb_vs_instructions);
2582
2583         if (test_arb_vs_offset_limit(gl_info)) gl_info->quirks |= WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT;
2584     }
2585     if (gl_info->supported[ARB_VERTEX_SHADER])
2586     {
2587         glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, &gl_max);
2588         gl_info->limits.glsl_vs_float_constants = gl_max / 4;
2589         TRACE_(d3d_caps)("Max ARB_VERTEX_SHADER float constants: %u.\n", gl_info->limits.glsl_vs_float_constants);
2590     }
2591     if (gl_info->supported[ARB_FRAGMENT_SHADER])
2592     {
2593         glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB, &gl_max);
2594         gl_info->limits.glsl_ps_float_constants = gl_max / 4;
2595         TRACE_(d3d_caps)("Max ARB_FRAGMENT_SHADER float constants: %u.\n", gl_info->limits.glsl_ps_float_constants);
2596         glGetIntegerv(GL_MAX_VARYING_FLOATS_ARB, &gl_max);
2597         gl_info->limits.glsl_varyings = gl_max;
2598         TRACE_(d3d_caps)("Max GLSL varyings: %u (%u 4 component varyings).\n", gl_max, gl_max / 4);
2599     }
2600     if (gl_info->supported[ARB_SHADING_LANGUAGE_100])
2601     {
2602         const char *str = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION_ARB);
2603         unsigned int major, minor;
2604
2605         TRACE_(d3d_caps)("GLSL version string: %s.\n", debugstr_a(str));
2606
2607         /* The format of the GLSL version string is "major.minor[.release] [vendor info]". */
2608         sscanf(str, "%u.%u", &major, &minor);
2609         gl_info->glsl_version = MAKEDWORD_VERSION(major, minor);
2610     }
2611     if (gl_info->supported[NV_LIGHT_MAX_EXPONENT])
2612     {
2613         glGetFloatv(GL_MAX_SHININESS_NV, &gl_info->limits.shininess);
2614     }
2615     else
2616     {
2617         gl_info->limits.shininess = 128.0f;
2618     }
2619     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2620     {
2621         /* If we have full NP2 texture support, disable
2622          * GL_ARB_texture_rectangle because we will never use it.
2623          * This saves a few redundant glDisable calls. */
2624         gl_info->supported[ARB_TEXTURE_RECTANGLE] = FALSE;
2625     }
2626     if (gl_info->supported[ATI_FRAGMENT_SHADER])
2627     {
2628         /* Disable NV_register_combiners and fragment shader if this is supported.
2629          * generally the NV extensions are preferred over the ATI ones, and this
2630          * extension is disabled if register_combiners and texture_shader2 are both
2631          * supported. So we reach this place only if we have incomplete NV dxlevel 8
2632          * fragment processing support. */
2633         gl_info->supported[NV_REGISTER_COMBINERS] = FALSE;
2634         gl_info->supported[NV_REGISTER_COMBINERS2] = FALSE;
2635         gl_info->supported[NV_TEXTURE_SHADER] = FALSE;
2636         gl_info->supported[NV_TEXTURE_SHADER2] = FALSE;
2637     }
2638     if (gl_info->supported[NV_HALF_FLOAT])
2639     {
2640         /* GL_ARB_half_float_vertex is a subset of GL_NV_half_float. */
2641         gl_info->supported[ARB_HALF_FLOAT_VERTEX] = TRUE;
2642     }
2643     checkGLcall("extension detection");
2644
2645     LEAVE_GL();
2646
2647     adapter->fragment_pipe = select_fragment_implementation(gl_info);
2648     adapter->shader_backend = select_shader_backend(gl_info);
2649     adapter->blitter = select_blit_implementation(gl_info);
2650
2651     adapter->fragment_pipe->get_caps(gl_info, &fragment_caps);
2652     gl_info->limits.texture_stages = fragment_caps.MaxTextureBlendStages;
2653     TRACE_(d3d_caps)("Max texture stages: %u.\n", gl_info->limits.texture_stages);
2654
2655     /* In some cases the number of texture stages can be larger than the number
2656      * of samplers. The GF4 for example can use only 2 samplers (no fragment
2657      * shaders), but 8 texture stages (register combiners). */
2658     gl_info->limits.sampler_stages = max(gl_info->limits.fragment_samplers, gl_info->limits.texture_stages);
2659
2660     if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
2661     {
2662         gl_info->fbo_ops.glIsRenderbuffer = gl_info->glIsRenderbuffer;
2663         gl_info->fbo_ops.glBindRenderbuffer = gl_info->glBindRenderbuffer;
2664         gl_info->fbo_ops.glDeleteRenderbuffers = gl_info->glDeleteRenderbuffers;
2665         gl_info->fbo_ops.glGenRenderbuffers = gl_info->glGenRenderbuffers;
2666         gl_info->fbo_ops.glRenderbufferStorage = gl_info->glRenderbufferStorage;
2667         gl_info->fbo_ops.glRenderbufferStorageMultisample = gl_info->glRenderbufferStorageMultisample;
2668         gl_info->fbo_ops.glGetRenderbufferParameteriv = gl_info->glGetRenderbufferParameteriv;
2669         gl_info->fbo_ops.glIsFramebuffer = gl_info->glIsFramebuffer;
2670         gl_info->fbo_ops.glBindFramebuffer = gl_info->glBindFramebuffer;
2671         gl_info->fbo_ops.glDeleteFramebuffers = gl_info->glDeleteFramebuffers;
2672         gl_info->fbo_ops.glGenFramebuffers = gl_info->glGenFramebuffers;
2673         gl_info->fbo_ops.glCheckFramebufferStatus = gl_info->glCheckFramebufferStatus;
2674         gl_info->fbo_ops.glFramebufferTexture1D = gl_info->glFramebufferTexture1D;
2675         gl_info->fbo_ops.glFramebufferTexture2D = gl_info->glFramebufferTexture2D;
2676         gl_info->fbo_ops.glFramebufferTexture3D = gl_info->glFramebufferTexture3D;
2677         gl_info->fbo_ops.glFramebufferRenderbuffer = gl_info->glFramebufferRenderbuffer;
2678         gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv = gl_info->glGetFramebufferAttachmentParameteriv;
2679         gl_info->fbo_ops.glBlitFramebuffer = gl_info->glBlitFramebuffer;
2680         gl_info->fbo_ops.glGenerateMipmap = gl_info->glGenerateMipmap;
2681         if (wined3d_settings.allow_multisampling)
2682         {
2683             glGetIntegerv(GL_MAX_SAMPLES, &gl_max);
2684             gl_info->limits.samples = gl_max;
2685         }
2686     }
2687     else
2688     {
2689         if (gl_info->supported[EXT_FRAMEBUFFER_OBJECT])
2690         {
2691             gl_info->fbo_ops.glIsRenderbuffer = gl_info->glIsRenderbufferEXT;
2692             gl_info->fbo_ops.glBindRenderbuffer = gl_info->glBindRenderbufferEXT;
2693             gl_info->fbo_ops.glDeleteRenderbuffers = gl_info->glDeleteRenderbuffersEXT;
2694             gl_info->fbo_ops.glGenRenderbuffers = gl_info->glGenRenderbuffersEXT;
2695             gl_info->fbo_ops.glRenderbufferStorage = gl_info->glRenderbufferStorageEXT;
2696             gl_info->fbo_ops.glGetRenderbufferParameteriv = gl_info->glGetRenderbufferParameterivEXT;
2697             gl_info->fbo_ops.glIsFramebuffer = gl_info->glIsFramebufferEXT;
2698             gl_info->fbo_ops.glBindFramebuffer = gl_info->glBindFramebufferEXT;
2699             gl_info->fbo_ops.glDeleteFramebuffers = gl_info->glDeleteFramebuffersEXT;
2700             gl_info->fbo_ops.glGenFramebuffers = gl_info->glGenFramebuffersEXT;
2701             gl_info->fbo_ops.glCheckFramebufferStatus = gl_info->glCheckFramebufferStatusEXT;
2702             gl_info->fbo_ops.glFramebufferTexture1D = gl_info->glFramebufferTexture1DEXT;
2703             gl_info->fbo_ops.glFramebufferTexture2D = gl_info->glFramebufferTexture2DEXT;
2704             gl_info->fbo_ops.glFramebufferTexture3D = gl_info->glFramebufferTexture3DEXT;
2705             gl_info->fbo_ops.glFramebufferRenderbuffer = gl_info->glFramebufferRenderbufferEXT;
2706             gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv = gl_info->glGetFramebufferAttachmentParameterivEXT;
2707             gl_info->fbo_ops.glGenerateMipmap = gl_info->glGenerateMipmapEXT;
2708         }
2709         else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2710         {
2711             WARN_(d3d_caps)("Framebuffer objects not supported, falling back to backbuffer offscreen rendering mode.\n");
2712             wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
2713         }
2714         if (gl_info->supported[EXT_FRAMEBUFFER_BLIT])
2715         {
2716             gl_info->fbo_ops.glBlitFramebuffer = gl_info->glBlitFramebufferEXT;
2717         }
2718         if (gl_info->supported[EXT_FRAMEBUFFER_MULTISAMPLE])
2719         {
2720             gl_info->fbo_ops.glRenderbufferStorageMultisample = gl_info->glRenderbufferStorageMultisampleEXT;
2721             if (wined3d_settings.allow_multisampling)
2722             {
2723                 glGetIntegerv(GL_MAX_SAMPLES, &gl_max);
2724                 gl_info->limits.samples = gl_max;
2725             }
2726         }
2727     }
2728
2729     /* MRTs are currently only supported when FBOs are used. */
2730     if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
2731     {
2732         gl_info->limits.buffers = 1;
2733     }
2734
2735     gl_vendor = wined3d_guess_gl_vendor(gl_info, gl_vendor_str, gl_renderer_str);
2736     card_vendor = wined3d_guess_card_vendor(gl_vendor_str, gl_renderer_str);
2737     TRACE_(d3d_caps)("found GL_VENDOR (%s)->(0x%04x/0x%04x)\n", debugstr_a(gl_vendor_str), gl_vendor, card_vendor);
2738
2739     device = wined3d_guess_card(gl_info, gl_renderer_str, &gl_vendor, &card_vendor);
2740     TRACE_(d3d_caps)("FOUND (fake) card: 0x%x (vendor id), 0x%x (device id)\n", card_vendor, device);
2741
2742     gl_info->wrap_lookup[WINED3DTADDRESS_WRAP - WINED3DTADDRESS_WRAP] = GL_REPEAT;
2743     gl_info->wrap_lookup[WINED3DTADDRESS_MIRROR - WINED3DTADDRESS_WRAP] =
2744             gl_info->supported[ARB_TEXTURE_MIRRORED_REPEAT] ? GL_MIRRORED_REPEAT_ARB : GL_REPEAT;
2745     gl_info->wrap_lookup[WINED3DTADDRESS_CLAMP - WINED3DTADDRESS_WRAP] = GL_CLAMP_TO_EDGE;
2746     gl_info->wrap_lookup[WINED3DTADDRESS_BORDER - WINED3DTADDRESS_WRAP] =
2747             gl_info->supported[ARB_TEXTURE_BORDER_CLAMP] ? GL_CLAMP_TO_BORDER_ARB : GL_REPEAT;
2748     gl_info->wrap_lookup[WINED3DTADDRESS_MIRRORONCE - WINED3DTADDRESS_WRAP] =
2749             gl_info->supported[ATI_TEXTURE_MIRROR_ONCE] ? GL_MIRROR_CLAMP_TO_EDGE_ATI : GL_REPEAT;
2750
2751     /* Make sure there's an active HDC else the WGL extensions will fail */
2752     hdc = pwglGetCurrentDC();
2753     if (hdc) {
2754         /* Not all GL drivers might offer WGL extensions e.g. VirtualBox */
2755         if(GL_EXTCALL(wglGetExtensionsStringARB))
2756             WGL_Extensions = GL_EXTCALL(wglGetExtensionsStringARB(hdc));
2757
2758         if (!WGL_Extensions)
2759         {
2760             ERR("   WGL_Extensions returns NULL\n");
2761         }
2762         else
2763         {
2764             TRACE_(d3d_caps)("WGL_Extensions reported:\n");
2765             while (*WGL_Extensions)
2766             {
2767                 const char *Start;
2768                 char ThisExtn[256];
2769
2770                 while (isspace(*WGL_Extensions)) WGL_Extensions++;
2771                 Start = WGL_Extensions;
2772                 while (!isspace(*WGL_Extensions) && *WGL_Extensions) ++WGL_Extensions;
2773
2774                 len = WGL_Extensions - Start;
2775                 if (!len || len >= sizeof(ThisExtn))
2776                     continue;
2777
2778                 memcpy(ThisExtn, Start, len);
2779                 ThisExtn[len] = '\0';
2780                 TRACE_(d3d_caps)("- %s\n", debugstr_a(ThisExtn));
2781
2782                 if (!strcmp(ThisExtn, "WGL_ARB_pixel_format")) {
2783                     gl_info->supported[WGL_ARB_PIXEL_FORMAT] = TRUE;
2784                     TRACE_(d3d_caps)("FOUND: WGL_ARB_pixel_format support\n");
2785                 }
2786                 if (!strcmp(ThisExtn, "WGL_EXT_swap_control")) {
2787                     gl_info->supported[WGL_EXT_SWAP_CONTROL] = TRUE;
2788                     TRACE_(d3d_caps)("FOUND: WGL_EXT_swap_control support\n");
2789                 }
2790                 if (!strcmp(ThisExtn, "WGL_WINE_pixel_format_passthrough")) {
2791                     gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH] = TRUE;
2792                     TRACE_(d3d_caps)("FOUND: WGL_WINE_pixel_format_passthrough support\n");
2793                 }
2794             }
2795         }
2796     }
2797
2798     fixup_extensions(gl_info, gl_renderer_str, gl_vendor, card_vendor, device);
2799     init_driver_info(driver_info, card_vendor, device);
2800     add_gl_compat_wrappers(gl_info);
2801
2802     return TRUE;
2803 }
2804
2805 UINT CDECL wined3d_get_adapter_count(const struct wined3d *wined3d)
2806 {
2807     TRACE_(d3d_caps)("wined3d %p, reporting %u adapters.\n",
2808             wined3d, wined3d->adapter_count);
2809
2810     return wined3d->adapter_count;
2811 }
2812
2813 HRESULT CDECL wined3d_register_software_device(struct wined3d *wined3d, void *init_function)
2814 {
2815     FIXME("wined3d %p, init_function %p stub!\n", wined3d, init_function);
2816
2817     return WINED3D_OK;
2818 }
2819
2820 HMONITOR CDECL wined3d_get_adapter_monitor(const struct wined3d *wined3d, UINT adapter_idx)
2821 {
2822     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u.\n", wined3d, adapter_idx);
2823
2824     if (adapter_idx >= wined3d->adapter_count)
2825         return NULL;
2826
2827     return MonitorFromPoint(wined3d->adapters[adapter_idx].monitorPoint, MONITOR_DEFAULTTOPRIMARY);
2828 }
2829
2830 /* FIXME: GetAdapterModeCount and EnumAdapterModes currently only returns modes
2831      of the same bpp but different resolutions                                  */
2832
2833 /* Note: dx9 supplies a format. Calls from d3d8 supply WINED3DFMT_UNKNOWN */
2834 UINT CDECL wined3d_get_adapter_mode_count(const struct wined3d *wined3d, UINT adapter_idx,
2835         enum wined3d_format_id format_id)
2836 {
2837     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u, format %s.\n", wined3d, adapter_idx, debug_d3dformat(format_id));
2838
2839     if (adapter_idx >= wined3d->adapter_count)
2840         return 0;
2841
2842     /* TODO: Store modes per adapter and read it from the adapter structure */
2843     if (!adapter_idx)
2844     {
2845         const struct wined3d_format *format = wined3d_get_format(&wined3d->adapters[adapter_idx].gl_info, format_id);
2846         UINT format_bits = format->byte_count * CHAR_BIT;
2847         unsigned int i = 0;
2848         unsigned int j = 0;
2849         DEVMODEW mode;
2850
2851         memset(&mode, 0, sizeof(mode));
2852         mode.dmSize = sizeof(mode);
2853
2854         while (EnumDisplaySettingsExW(NULL, j, &mode, 0))
2855         {
2856             ++j;
2857
2858             if (format_id == WINED3DFMT_UNKNOWN)
2859             {
2860                 /* This is for D3D8, do not enumerate P8 here */
2861                 if (mode.dmBitsPerPel == 32 || mode.dmBitsPerPel == 16) ++i;
2862             }
2863             else if (mode.dmBitsPerPel == format_bits)
2864             {
2865                 ++i;
2866             }
2867         }
2868
2869         TRACE_(d3d_caps)("Returning %u matching modes (out of %u total) for adapter %u.\n", i, j, adapter_idx);
2870
2871         return i;
2872     }
2873     else
2874     {
2875         FIXME_(d3d_caps)("Adapter not primary display.\n");
2876     }
2877
2878     return 0;
2879 }
2880
2881 /* Note: dx9 supplies a format. Calls from d3d8 supply WINED3DFMT_UNKNOWN */
2882 HRESULT CDECL wined3d_enum_adapter_modes(const struct wined3d *wined3d, UINT adapter_idx,
2883         enum wined3d_format_id format_id, UINT mode_idx, struct wined3d_display_mode *mode)
2884 {
2885     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u, format %s, mode_idx %u, mode %p.\n",
2886             wined3d, adapter_idx, debug_d3dformat(format_id), mode_idx, mode);
2887
2888     /* Validate the parameters as much as possible */
2889     if (!mode || adapter_idx >= wined3d->adapter_count
2890             || mode_idx >= wined3d_get_adapter_mode_count(wined3d, adapter_idx, format_id))
2891     {
2892         return WINED3DERR_INVALIDCALL;
2893     }
2894
2895     /* TODO: Store modes per adapter and read it from the adapter structure */
2896     if (!adapter_idx)
2897     {
2898         const struct wined3d_format *format = wined3d_get_format(&wined3d->adapters[adapter_idx].gl_info, format_id);
2899         UINT format_bits = format->byte_count * CHAR_BIT;
2900         DEVMODEW DevModeW;
2901         int ModeIdx = 0;
2902         UINT i = 0;
2903         int j = 0;
2904
2905         ZeroMemory(&DevModeW, sizeof(DevModeW));
2906         DevModeW.dmSize = sizeof(DevModeW);
2907
2908         /* If we are filtering to a specific format (D3D9), then need to skip
2909            all unrelated modes, but if mode is irrelevant (D3D8), then we can
2910            just count through the ones with valid bit depths */
2911         while (i <= mode_idx && EnumDisplaySettingsExW(NULL, j++, &DevModeW, 0))
2912         {
2913             if (format_id == WINED3DFMT_UNKNOWN)
2914             {
2915                 /* This is for D3D8, do not enumerate P8 here */
2916                 if (DevModeW.dmBitsPerPel == 32 || DevModeW.dmBitsPerPel == 16) ++i;
2917             }
2918             else if (DevModeW.dmBitsPerPel == format_bits)
2919             {
2920                 ++i;
2921             }
2922         }
2923
2924         if (!i)
2925         {
2926             TRACE_(d3d_caps)("No modes found for format (%x - %s)\n", format_id, debug_d3dformat(format_id));
2927             return WINED3DERR_INVALIDCALL;
2928         }
2929         ModeIdx = j - 1;
2930
2931         /* Now get the display mode via the calculated index */
2932         if (EnumDisplaySettingsExW(NULL, ModeIdx, &DevModeW, 0))
2933         {
2934             mode->width = DevModeW.dmPelsWidth;
2935             mode->height = DevModeW.dmPelsHeight;
2936             mode->refresh_rate = DEFAULT_REFRESH_RATE;
2937             if (DevModeW.dmFields & DM_DISPLAYFREQUENCY)
2938                 mode->refresh_rate = DevModeW.dmDisplayFrequency;
2939
2940             if (format_id == WINED3DFMT_UNKNOWN)
2941                 mode->format_id = pixelformat_for_depth(DevModeW.dmBitsPerPel);
2942             else
2943                 mode->format_id = format_id;
2944         }
2945         else
2946         {
2947             TRACE_(d3d_caps)("Requested mode %u out of range.\n", mode_idx);
2948             return WINED3DERR_INVALIDCALL;
2949         }
2950
2951         TRACE_(d3d_caps)("W %d H %d rr %d fmt (%x - %s) bpp %u\n",
2952                 mode->width, mode->height, mode->refresh_rate, mode->format_id,
2953                 debug_d3dformat(mode->format_id), DevModeW.dmBitsPerPel);
2954     }
2955     else
2956     {
2957         FIXME_(d3d_caps)("Adapter not primary display\n");
2958     }
2959
2960     return WINED3D_OK;
2961 }
2962
2963 HRESULT CDECL wined3d_get_adapter_display_mode(const struct wined3d *wined3d, UINT adapter_idx,
2964         struct wined3d_display_mode *mode)
2965 {
2966     TRACE("wined3d %p, adapter_idx %u, display_mode %p.\n", wined3d, adapter_idx, mode);
2967
2968     if (!mode || adapter_idx >= wined3d->adapter_count)
2969         return WINED3DERR_INVALIDCALL;
2970
2971     if (!adapter_idx)
2972     {
2973         DEVMODEW DevModeW;
2974         unsigned int bpp;
2975
2976         ZeroMemory(&DevModeW, sizeof(DevModeW));
2977         DevModeW.dmSize = sizeof(DevModeW);
2978
2979         EnumDisplaySettingsExW(NULL, ENUM_CURRENT_SETTINGS, &DevModeW, 0);
2980         mode->width = DevModeW.dmPelsWidth;
2981         mode->height = DevModeW.dmPelsHeight;
2982         bpp = DevModeW.dmBitsPerPel;
2983         mode->refresh_rate = DEFAULT_REFRESH_RATE;
2984         if (DevModeW.dmFields & DM_DISPLAYFREQUENCY)
2985             mode->refresh_rate = DevModeW.dmDisplayFrequency;
2986         mode->format_id = pixelformat_for_depth(bpp);
2987     }
2988     else
2989     {
2990         FIXME_(d3d_caps)("Adapter not primary display\n");
2991     }
2992
2993     TRACE_(d3d_caps)("returning w:%d, h:%d, ref:%d, fmt:%s\n", mode->width,
2994           mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
2995     return WINED3D_OK;
2996 }
2997
2998 /* NOTE: due to structure differences between dx8 and dx9 D3DADAPTER_IDENTIFIER,
2999    and fields being inserted in the middle, a new structure is used in place    */
3000 HRESULT CDECL wined3d_get_adapter_identifier(const struct wined3d *wined3d,
3001         UINT adapter_idx, DWORD flags, struct wined3d_adapter_identifier *identifier)
3002 {
3003     const struct wined3d_adapter *adapter;
3004     size_t len;
3005
3006     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u, flags %#x, indentifier %p.\n",
3007             wined3d, adapter_idx, flags, identifier);
3008
3009     if (adapter_idx >= wined3d->adapter_count)
3010         return WINED3DERR_INVALIDCALL;
3011
3012     adapter = &wined3d->adapters[adapter_idx];
3013
3014     /* Return the information requested */
3015     TRACE_(d3d_caps)("device/Vendor Name and Version detection using FillGLCaps\n");
3016
3017     if (identifier->driver_size)
3018     {
3019         const char *name = adapter->driver_info.name;
3020         len = min(strlen(name), identifier->driver_size - 1);
3021         memcpy(identifier->driver, name, len);
3022         identifier->driver[len] = '\0';
3023     }
3024
3025     if (identifier->description_size)
3026     {
3027         const char *description = adapter->driver_info.description;
3028         len = min(strlen(description), identifier->description_size - 1);
3029         memcpy(identifier->description, description, len);
3030         identifier->description[len] = '\0';
3031     }
3032
3033     /* Note that d3d8 doesn't supply a device name. */
3034     if (identifier->device_name_size)
3035     {
3036         static const char *device_name = "\\\\.\\DISPLAY1"; /* FIXME: May depend on desktop? */
3037
3038         len = strlen(device_name);
3039         if (len >= identifier->device_name_size)
3040         {
3041             ERR("Device name size too small.\n");
3042             return WINED3DERR_INVALIDCALL;
3043         }
3044
3045         memcpy(identifier->device_name, device_name, len);
3046         identifier->device_name[len] = '\0';
3047     }
3048
3049     identifier->driver_version.u.HighPart = adapter->driver_info.version_high;
3050     identifier->driver_version.u.LowPart = adapter->driver_info.version_low;
3051     identifier->vendor_id = adapter->driver_info.vendor;
3052     identifier->device_id = adapter->driver_info.device;
3053     identifier->subsystem_id = 0;
3054     identifier->revision = 0;
3055     memcpy(&identifier->device_identifier, &IID_D3DDEVICE_D3DUID, sizeof(identifier->device_identifier));
3056     identifier->whql_level = (flags & WINED3DENUM_NO_WHQL_LEVEL) ? 0 : 1;
3057     memcpy(&identifier->adapter_luid, &adapter->luid, sizeof(identifier->adapter_luid));
3058     identifier->video_memory = adapter->TextureRam;
3059
3060     return WINED3D_OK;
3061 }
3062
3063 static BOOL wined3d_check_pixel_format_color(const struct wined3d_gl_info *gl_info,
3064         const struct wined3d_pixel_format *cfg, const struct wined3d_format *format)
3065 {
3066     BYTE redSize, greenSize, blueSize, alphaSize, colorBits;
3067
3068     /* Float formats need FBOs. If FBOs are used this function isn't called */
3069     if (format->flags & WINED3DFMT_FLAG_FLOAT) return FALSE;
3070
3071     if(cfg->iPixelType == WGL_TYPE_RGBA_ARB) { /* Integer RGBA formats */
3072         if (!getColorBits(format, &redSize, &greenSize, &blueSize, &alphaSize, &colorBits))
3073         {
3074             ERR("Unable to check compatibility for format %s.\n", debug_d3dformat(format->id));
3075             return FALSE;
3076         }
3077
3078         if(cfg->redSize < redSize)
3079             return FALSE;
3080
3081         if(cfg->greenSize < greenSize)
3082             return FALSE;
3083
3084         if(cfg->blueSize < blueSize)
3085             return FALSE;
3086
3087         if(cfg->alphaSize < alphaSize)
3088             return FALSE;
3089
3090         return TRUE;
3091     }
3092
3093     /* Probably a RGBA_float or color index mode */
3094     return FALSE;
3095 }
3096
3097 static BOOL wined3d_check_pixel_format_depth(const struct wined3d_gl_info *gl_info,
3098         const struct wined3d_pixel_format *cfg, const struct wined3d_format *format)
3099 {
3100     BYTE depthSize, stencilSize;
3101     BOOL lockable = FALSE;
3102
3103     if (!getDepthStencilBits(format, &depthSize, &stencilSize))
3104     {
3105         ERR("Unable to check compatibility for format %s.\n", debug_d3dformat(format->id));
3106         return FALSE;
3107     }
3108
3109     /* Float formats need FBOs. If FBOs are used this function isn't called */
3110     if (format->flags & WINED3DFMT_FLAG_FLOAT) return FALSE;
3111
3112     if ((format->id == WINED3DFMT_D16_LOCKABLE) || (format->id == WINED3DFMT_D32_FLOAT))
3113         lockable = TRUE;
3114
3115     /* On some modern cards like the Geforce8/9 GLX doesn't offer some dephthstencil formats which D3D9 reports.
3116      * We can safely report 'compatible' formats (e.g. D24 can be used for D16) as long as we aren't dealing with
3117      * a lockable format. This also helps D3D <= 7 as they expect D16 which isn't offered without this on Geforce8 cards. */
3118     if(!(cfg->depthSize == depthSize || (!lockable && cfg->depthSize > depthSize)))
3119         return FALSE;
3120
3121     /* Some cards like Intel i915 ones only offer D24S8 but lots of games also need a format without stencil, so
3122      * allow more stencil bits than requested. */
3123     if(cfg->stencilSize < stencilSize)
3124         return FALSE;
3125
3126     return TRUE;
3127 }
3128
3129 HRESULT CDECL wined3d_check_depth_stencil_match(const struct wined3d *wined3d,
3130         UINT adapter_idx, WINED3DDEVTYPE device_type, enum wined3d_format_id adapter_format_id,
3131         enum wined3d_format_id render_target_format_id, enum wined3d_format_id depth_stencil_format_id)
3132 {
3133     const struct wined3d_format *rt_format;
3134     const struct wined3d_format *ds_format;
3135     const struct wined3d_adapter *adapter;
3136
3137     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u, device_type %s,\n"
3138             "adapter_format %s, render_target_format %s, depth_stencil_format %s.\n",
3139             wined3d, adapter_idx, debug_d3ddevicetype(device_type), debug_d3dformat(adapter_format_id),
3140             debug_d3dformat(render_target_format_id), debug_d3dformat(depth_stencil_format_id));
3141
3142     if (adapter_idx >= wined3d->adapter_count)
3143         return WINED3DERR_INVALIDCALL;
3144
3145     adapter = &wined3d->adapters[adapter_idx];
3146     rt_format = wined3d_get_format(&adapter->gl_info, render_target_format_id);
3147     ds_format = wined3d_get_format(&adapter->gl_info, depth_stencil_format_id);
3148     if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3149     {
3150         if ((rt_format->flags & WINED3DFMT_FLAG_RENDERTARGET)
3151                 && (ds_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
3152         {
3153             TRACE_(d3d_caps)("Formats match.\n");
3154             return WINED3D_OK;
3155         }
3156     }
3157     else
3158     {
3159         const struct wined3d_pixel_format *cfgs;
3160         unsigned int cfg_count;
3161         unsigned int i;
3162
3163         cfgs = adapter->cfgs;
3164         cfg_count = adapter->cfg_count;
3165         for (i = 0; i < cfg_count; ++i)
3166         {
3167             if (wined3d_check_pixel_format_color(&adapter->gl_info, &cfgs[i], rt_format)
3168                     && wined3d_check_pixel_format_depth(&adapter->gl_info, &cfgs[i], ds_format))
3169             {
3170                 TRACE_(d3d_caps)("Formats match.\n");
3171                 return WINED3D_OK;
3172             }
3173         }
3174     }
3175
3176     TRACE_(d3d_caps)("Unsupported format pair: %s and %s.\n",
3177             debug_d3dformat(render_target_format_id),
3178             debug_d3dformat(depth_stencil_format_id));
3179
3180     return WINED3DERR_NOTAVAILABLE;
3181 }
3182
3183 HRESULT CDECL wined3d_check_device_multisample_type(const struct wined3d *wined3d, UINT adapter_idx,
3184         WINED3DDEVTYPE device_type, enum wined3d_format_id surface_format_id, BOOL windowed,
3185         WINED3DMULTISAMPLE_TYPE multisample_type, DWORD *quality_levels)
3186 {
3187     const struct wined3d_gl_info *gl_info;
3188
3189     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u, device_type %s, surface_format %s,\n"
3190             "windowed %#x, multisample_type %#x, quality_levels %p.\n",
3191             wined3d, adapter_idx, debug_d3ddevicetype(device_type), debug_d3dformat(surface_format_id),
3192             windowed, multisample_type, quality_levels);
3193
3194     if (adapter_idx >= wined3d->adapter_count)
3195         return WINED3DERR_INVALIDCALL;
3196
3197     gl_info = &wined3d->adapters[adapter_idx].gl_info;
3198
3199     if (multisample_type > gl_info->limits.samples)
3200     {
3201         TRACE("Returning not supported.\n");
3202         if (quality_levels)
3203             *quality_levels = 0;
3204
3205         return WINED3DERR_NOTAVAILABLE;
3206     }
3207
3208     if (quality_levels)
3209     {
3210         if (multisample_type == WINED3DMULTISAMPLE_NONMASKABLE)
3211             /* FIXME: This is probably wrong. */
3212             *quality_levels = gl_info->limits.samples;
3213         else
3214             *quality_levels = 1;
3215     }
3216
3217     return WINED3D_OK;
3218 }
3219
3220 /* Check if we support bumpmapping for a format */
3221 static BOOL CheckBumpMapCapability(const struct wined3d_adapter *adapter, const struct wined3d_format *format)
3222 {
3223     /* Ask the fixed function pipeline implementation if it can deal
3224      * with the conversion. If we've got a GL extension giving native
3225      * support this will be an identity conversion. */
3226     return (format->flags & WINED3DFMT_FLAG_BUMPMAP)
3227             && adapter->fragment_pipe->color_fixup_supported(format->color_fixup);
3228 }
3229
3230 /* Check if the given DisplayFormat + DepthStencilFormat combination is valid for the Adapter */
3231 static BOOL CheckDepthStencilCapability(const struct wined3d_adapter *adapter,
3232         const struct wined3d_format *display_format, const struct wined3d_format *ds_format)
3233 {
3234     /* Only allow depth/stencil formats */
3235     if (!(ds_format->depth_size || ds_format->stencil_size)) return FALSE;
3236
3237     /* Blacklist formats not supported on Windows */
3238     switch (ds_format->id)
3239     {
3240         case WINED3DFMT_S1_UINT_D15_UNORM: /* Breaks the shadowvol2 dx7 sdk sample */
3241         case WINED3DFMT_S4X4_UINT_D24_UNORM:
3242             TRACE_(d3d_caps)("[FAILED] - not supported on windows\n");
3243             return FALSE;
3244
3245         default:
3246             break;
3247     }
3248
3249     if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3250     {
3251         /* With FBOs WGL limitations do not apply, but the format needs to be FBO attachable */
3252         if (ds_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)) return TRUE;
3253     }
3254     else
3255     {
3256         unsigned int i;
3257
3258         /* Walk through all WGL pixel formats to find a match */
3259         for (i = 0; i < adapter->cfg_count; ++i)
3260         {
3261             const struct wined3d_pixel_format *cfg = &adapter->cfgs[i];
3262             if (wined3d_check_pixel_format_color(&adapter->gl_info, cfg, display_format)
3263                     && wined3d_check_pixel_format_depth(&adapter->gl_info, cfg, ds_format))
3264                 return TRUE;
3265         }
3266     }
3267
3268     return FALSE;
3269 }
3270
3271 static BOOL CheckFilterCapability(const struct wined3d_adapter *adapter, const struct wined3d_format *format)
3272 {
3273     /* The flags entry of a format contains the filtering capability */
3274     if ((format->flags & WINED3DFMT_FLAG_FILTERING)
3275             || !(adapter->gl_info.quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING))
3276         return TRUE;
3277
3278     return FALSE;
3279 }
3280
3281 /* Check the render target capabilities of a format */
3282 static BOOL CheckRenderTargetCapability(const struct wined3d_adapter *adapter,
3283         const struct wined3d_format *adapter_format, const struct wined3d_format *check_format)
3284 {
3285     /* Filter out non-RT formats */
3286     if (!(check_format->flags & WINED3DFMT_FLAG_RENDERTARGET)) return FALSE;
3287     if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
3288     {
3289         BYTE AdapterRed, AdapterGreen, AdapterBlue, AdapterAlpha, AdapterTotalSize;
3290         BYTE CheckRed, CheckGreen, CheckBlue, CheckAlpha, CheckTotalSize;
3291         const struct wined3d_pixel_format *cfgs = adapter->cfgs;
3292         unsigned int i;
3293
3294         getColorBits(adapter_format, &AdapterRed, &AdapterGreen, &AdapterBlue, &AdapterAlpha, &AdapterTotalSize);
3295         getColorBits(check_format, &CheckRed, &CheckGreen, &CheckBlue, &CheckAlpha, &CheckTotalSize);
3296
3297         /* In backbuffer mode the front and backbuffer share the same WGL pixelformat.
3298          * The format must match in RGB, alpha is allowed to be different. (Only the backbuffer can have alpha) */
3299         if(!((AdapterRed == CheckRed) && (AdapterGreen == CheckGreen) && (AdapterBlue == CheckBlue))) {
3300             TRACE_(d3d_caps)("[FAILED]\n");
3301             return FALSE;
3302         }
3303
3304         /* Check if there is a WGL pixel format matching the requirements, the format should also be window
3305          * drawable (not offscreen; e.g. Nvidia offers R5G6B5 for pbuffers even when X is running at 24bit) */
3306         for (i = 0; i < adapter->cfg_count; ++i)
3307         {
3308             if (cfgs[i].windowDrawable
3309                     && wined3d_check_pixel_format_color(&adapter->gl_info, &cfgs[i], check_format))
3310             {
3311                 TRACE_(d3d_caps)("Pixel format %d is compatible with format %s.\n",
3312                         cfgs[i].iPixelFormat, debug_d3dformat(check_format->id));
3313                 return TRUE;
3314             }
3315         }
3316     }
3317     else if(wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3318     {
3319         /* For now return TRUE for FBOs until we have some proper checks.
3320          * Note that this function will only be called when the format is around for texturing. */
3321         return TRUE;
3322     }
3323     return FALSE;
3324 }
3325
3326 static BOOL CheckSrgbReadCapability(const struct wined3d_adapter *adapter, const struct wined3d_format *format)
3327 {
3328     return format->flags & WINED3DFMT_FLAG_SRGB_READ;
3329 }
3330
3331 static BOOL CheckSrgbWriteCapability(const struct wined3d_adapter *adapter, const struct wined3d_format *format)
3332 {
3333     /* Only offer SRGB writing on X8R8G8B8/A8R8G8B8 when we use ARB or GLSL shaders as we are
3334      * doing the color fixup in shaders.
3335      * Note Windows drivers (at least on the Geforce 8800) also offer this on R5G6B5. */
3336     if (format->flags & WINED3DFMT_FLAG_SRGB_WRITE)
3337     {
3338         int vs_selected_mode;
3339         int ps_selected_mode;
3340         select_shader_mode(&adapter->gl_info, &ps_selected_mode, &vs_selected_mode);
3341
3342         if((ps_selected_mode == SHADER_ARB) || (ps_selected_mode == SHADER_GLSL)) {
3343             TRACE_(d3d_caps)("[OK]\n");
3344             return TRUE;
3345         }
3346     }
3347
3348     TRACE_(d3d_caps)("[FAILED] - sRGB writes not supported by format %s.\n", debug_d3dformat(format->id));
3349     return FALSE;
3350 }
3351
3352 /* Check if a format support blending in combination with pixel shaders */
3353 static BOOL CheckPostPixelShaderBlendingCapability(const struct wined3d_adapter *adapter,
3354         const struct wined3d_format *format)
3355 {
3356     /* The flags entry of a format contains the post pixel shader blending capability */
3357     if (format->flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING) return TRUE;
3358
3359     return FALSE;
3360 }
3361
3362 static BOOL CheckWrapAndMipCapability(const struct wined3d_adapter *adapter, const struct wined3d_format *format)
3363 {
3364     /* OpenGL supports mipmapping on all formats basically. Wrapping is unsupported,
3365      * but we have to report mipmapping so we cannot reject this flag. Tests show that
3366      * windows reports WRAPANDMIP on unfilterable surfaces as well, apparently to show
3367      * that wrapping is supported. The lack of filtering will sort out the mipmapping
3368      * capability anyway.
3369      *
3370      * For now lets report this on all formats, but in the future we may want to
3371      * restrict it to some should games need that
3372      */
3373     return TRUE;
3374 }
3375
3376 /* Check if a texture format is supported on the given adapter */
3377 static BOOL CheckTextureCapability(const struct wined3d_adapter *adapter, const struct wined3d_format *format)
3378 {
3379     const struct wined3d_gl_info *gl_info = &adapter->gl_info;
3380
3381     switch (format->id)
3382     {
3383         /*****
3384          *  supported: RGB(A) formats
3385          */
3386         case WINED3DFMT_B8G8R8_UNORM:
3387             TRACE_(d3d_caps)("[FAILED] - Not enumerated on Windows\n");
3388             return FALSE;
3389         case WINED3DFMT_B8G8R8A8_UNORM:
3390         case WINED3DFMT_B8G8R8X8_UNORM:
3391         case WINED3DFMT_B5G6R5_UNORM:
3392         case WINED3DFMT_B5G5R5X1_UNORM:
3393         case WINED3DFMT_B5G5R5A1_UNORM:
3394         case WINED3DFMT_B4G4R4A4_UNORM:
3395         case WINED3DFMT_A8_UNORM:
3396         case WINED3DFMT_B4G4R4X4_UNORM:
3397         case WINED3DFMT_R8G8B8A8_UNORM:
3398         case WINED3DFMT_R8G8B8X8_UNORM:
3399         case WINED3DFMT_B10G10R10A2_UNORM:
3400         case WINED3DFMT_R10G10B10A2_UNORM:
3401         case WINED3DFMT_R16G16_UNORM:
3402             TRACE_(d3d_caps)("[OK]\n");
3403             return TRUE;
3404
3405         case WINED3DFMT_B2G3R3_UNORM:
3406             TRACE_(d3d_caps)("[FAILED] - Not supported on Windows\n");
3407             return FALSE;
3408
3409         /*****
3410          *  Not supported: Palettized
3411          *  Only some Geforce/Voodoo3/G400 cards offer 8-bit textures in case of <=Direct3D7.
3412          *  Since it is not widely available, don't offer it. Further no Windows driver offers
3413          *  WINED3DFMT_P8_UINT_A8_NORM, so don't offer it either.
3414          */
3415         case WINED3DFMT_P8_UINT:
3416         case WINED3DFMT_P8_UINT_A8_UNORM:
3417             return FALSE;
3418
3419         /*****
3420          *  Supported: (Alpha)-Luminance
3421          */
3422         case WINED3DFMT_L8_UNORM:
3423         case WINED3DFMT_L8A8_UNORM:
3424         case WINED3DFMT_L16_UNORM:
3425             TRACE_(d3d_caps)("[OK]\n");
3426             return TRUE;
3427
3428         /* Not supported on Windows, thus disabled */
3429         case WINED3DFMT_L4A4_UNORM:
3430             TRACE_(d3d_caps)("[FAILED] - not supported on windows\n");
3431             return FALSE;
3432
3433         /*****
3434          *  Supported: Depth/Stencil formats
3435          */
3436         case WINED3DFMT_D16_LOCKABLE:
3437         case WINED3DFMT_D16_UNORM:
3438         case WINED3DFMT_X8D24_UNORM:
3439         case WINED3DFMT_D24_UNORM_S8_UINT:
3440         case WINED3DFMT_S8_UINT_D24_FLOAT:
3441         case WINED3DFMT_D32_UNORM:
3442         case WINED3DFMT_D32_FLOAT:
3443             return TRUE;
3444
3445         case WINED3DFMT_INTZ:
3446             if (gl_info->supported[EXT_PACKED_DEPTH_STENCIL]
3447                     || gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
3448                 return TRUE;
3449             return FALSE;
3450
3451         /* Not supported on Windows */
3452         case WINED3DFMT_S1_UINT_D15_UNORM:
3453         case WINED3DFMT_S4X4_UINT_D24_UNORM:
3454             TRACE_(d3d_caps)("[FAILED] - not supported on windows\n");
3455             return FALSE;
3456
3457         /*****
3458          *  Not supported everywhere(depends on GL_ATI_envmap_bumpmap or
3459          *  GL_NV_texture_shader). Emulated by shaders
3460          */
3461         case WINED3DFMT_R8G8_SNORM:
3462         case WINED3DFMT_R8G8_SNORM_L8X8_UNORM:
3463         case WINED3DFMT_R5G5_SNORM_L6_UNORM:
3464         case WINED3DFMT_R8G8B8A8_SNORM:
3465         case WINED3DFMT_R16G16_SNORM:
3466             /* Ask the shader backend if it can deal with the conversion. If
3467              * we've got a GL extension giving native support this will be an
3468              * identity conversion. */
3469             if (adapter->shader_backend->shader_color_fixup_supported(format->color_fixup))
3470             {
3471                 TRACE_(d3d_caps)("[OK]\n");
3472                 return TRUE;
3473             }
3474             TRACE_(d3d_caps)("[FAILED]\n");
3475             return FALSE;
3476
3477         case WINED3DFMT_DXT1:
3478         case WINED3DFMT_DXT2:
3479         case WINED3DFMT_DXT3:
3480         case WINED3DFMT_DXT4:
3481         case WINED3DFMT_DXT5:
3482             if (gl_info->supported[EXT_TEXTURE_COMPRESSION_S3TC])
3483             {
3484                 TRACE_(d3d_caps)("[OK]\n");
3485                 return TRUE;
3486             }
3487             TRACE_(d3d_caps)("[FAILED]\n");
3488             return FALSE;
3489
3490
3491         /*****
3492          *  Odd formats - not supported
3493          */
3494         case WINED3DFMT_VERTEXDATA:
3495         case WINED3DFMT_R16_UINT:
3496         case WINED3DFMT_R32_UINT:
3497         case WINED3DFMT_R16G16B16A16_SNORM:
3498         case WINED3DFMT_R10G10B10_SNORM_A2_UNORM:
3499         case WINED3DFMT_R10G11B11_SNORM:
3500         case WINED3DFMT_R16:
3501         case WINED3DFMT_AL16:
3502             TRACE_(d3d_caps)("[FAILED]\n"); /* Enable when implemented */
3503             return FALSE;
3504
3505         /*****
3506          *  WINED3DFMT_R8G8_SNORM_Cx: Not supported right now
3507          */
3508         case WINED3DFMT_R8G8_SNORM_Cx:
3509             TRACE_(d3d_caps)("[FAILED]\n"); /* Enable when implemented */
3510             return FALSE;
3511
3512         /* YUV formats */
3513         case WINED3DFMT_UYVY:
3514         case WINED3DFMT_YUY2:
3515             if (gl_info->supported[APPLE_YCBCR_422])
3516             {
3517                 TRACE_(d3d_caps)("[OK]\n");
3518                 return TRUE;
3519             }
3520             TRACE_(d3d_caps)("[FAILED]\n");
3521             return FALSE;
3522         case WINED3DFMT_YV12:
3523             TRACE_(d3d_caps)("[FAILED]\n");
3524             return FALSE;
3525
3526         case WINED3DFMT_R16G16B16A16_UNORM:
3527             if (gl_info->quirks & WINED3D_QUIRK_BROKEN_RGBA16)
3528             {
3529                 TRACE_(d3d_caps)("[FAILED]\n");
3530                 return FALSE;
3531             }
3532             TRACE_(d3d_caps)("[OK]\n");
3533             return TRUE;
3534
3535             /* Not supported */
3536         case WINED3DFMT_B2G3R3A8_UNORM:
3537             TRACE_(d3d_caps)("[FAILED]\n"); /* Enable when implemented */
3538             return FALSE;
3539
3540             /* Floating point formats */
3541         case WINED3DFMT_R16_FLOAT:
3542         case WINED3DFMT_R16G16_FLOAT:
3543         case WINED3DFMT_R16G16B16A16_FLOAT:
3544             if (gl_info->supported[ARB_TEXTURE_FLOAT] && gl_info->supported[ARB_HALF_FLOAT_PIXEL])
3545             {
3546                 TRACE_(d3d_caps)("[OK]\n");
3547                 return TRUE;
3548             }
3549             TRACE_(d3d_caps)("[FAILED]\n");
3550             return FALSE;
3551
3552         case WINED3DFMT_R32_FLOAT:
3553         case WINED3DFMT_R32G32_FLOAT:
3554         case WINED3DFMT_R32G32B32A32_FLOAT:
3555             if (gl_info->supported[ARB_TEXTURE_FLOAT])
3556             {
3557                 TRACE_(d3d_caps)("[OK]\n");
3558                 return TRUE;
3559             }
3560             TRACE_(d3d_caps)("[FAILED]\n");
3561             return FALSE;
3562
3563         /* ATI instancing hack: Although ATI cards do not support Shader Model 3.0, they support
3564          * instancing. To query if the card supports instancing CheckDeviceFormat with the special format
3565          * MAKEFOURCC('I','N','S','T') is used. Should a (broken) app check for this provide a proper return value.
3566          * We can do instancing with all shader versions, but we need vertex shaders.
3567          *
3568          * Additionally applications have to set the D3DRS_POINTSIZE render state to MAKEFOURCC('I','N','S','T') once
3569          * to enable instancing. WineD3D doesn't need that and just ignores it.
3570          *
3571          * With Shader Model 3.0 capable cards Instancing 'just works' in Windows.
3572          */
3573         case WINED3DFMT_INST:
3574             TRACE("ATI Instancing check hack\n");
3575             if (gl_info->supported[ARB_VERTEX_PROGRAM] || gl_info->supported[ARB_VERTEX_SHADER])
3576             {
3577                 TRACE_(d3d_caps)("[OK]\n");
3578                 return TRUE;
3579             }
3580             TRACE_(d3d_caps)("[FAILED]\n");
3581             return FALSE;
3582
3583         /* Some weird FOURCC formats */
3584         case WINED3DFMT_R8G8_B8G8:
3585         case WINED3DFMT_G8R8_G8B8:
3586         case WINED3DFMT_MULTI2_ARGB8:
3587             TRACE_(d3d_caps)("[FAILED]\n");
3588             return FALSE;
3589
3590         /* Vendor specific formats */
3591         case WINED3DFMT_ATI2N:
3592             if (gl_info->supported[ATI_TEXTURE_COMPRESSION_3DC]
3593                     || gl_info->supported[ARB_TEXTURE_COMPRESSION_RGTC])
3594             {
3595                 if (adapter->shader_backend->shader_color_fixup_supported(format->color_fixup)
3596                         && adapter->fragment_pipe->color_fixup_supported(format->color_fixup))
3597                 {
3598                     TRACE_(d3d_caps)("[OK]\n");
3599                     return TRUE;
3600                 }
3601
3602                 TRACE_(d3d_caps)("[OK]\n");
3603                 return TRUE;
3604             }
3605             TRACE_(d3d_caps)("[FAILED]\n");
3606             return FALSE;
3607
3608         /* Depth bound test. To query if the card supports it CheckDeviceFormat with the special
3609          * format MAKEFOURCC('N','V','D','B') is used.
3610          * It is enabled by setting D3DRS_ADAPTIVETESS_X render state to MAKEFOURCC('N','V','D','B') and
3611          * then controlled by setting D3DRS_ADAPTIVETESS_Z (zMin) and D3DRS_ADAPTIVETESS_W (zMax)
3612          * to test value.
3613          */
3614         case WINED3DFMT_NVDB:
3615             if (gl_info->supported[EXT_DEPTH_BOUNDS_TEST])
3616             {
3617                 TRACE_(d3d_caps)("[OK]\n");
3618                 return TRUE;
3619             }
3620             TRACE_(d3d_caps)("[FAILED]\n");
3621             return FALSE;
3622
3623         case WINED3DFMT_NVHU:
3624         case WINED3DFMT_NVHS:
3625             /* These formats seem to be similar to the HILO formats in GL_NV_texture_shader. NVHU
3626              * is said to be GL_UNSIGNED_HILO16, NVHS GL_SIGNED_HILO16. Rumours say that d3d computes
3627              * a 3rd channel similarly to D3DFMT_CxV8U8(So NVHS could be called D3DFMT_CxV16U16).
3628              * ATI refused to support formats which can easily be emulated with pixel shaders, so
3629              * Applications have to deal with not having NVHS and NVHU.
3630              */
3631             TRACE_(d3d_caps)("[FAILED]\n");
3632             return FALSE;
3633
3634         case WINED3DFMT_NULL:
3635             if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
3636                 return TRUE;
3637             return FALSE;
3638
3639         case WINED3DFMT_UNKNOWN:
3640             return FALSE;
3641
3642         default:
3643             ERR("Unhandled format %s.\n", debug_d3dformat(format->id));
3644             break;
3645     }
3646     return FALSE;
3647 }
3648
3649 static BOOL CheckSurfaceCapability(const struct wined3d_adapter *adapter,
3650         const struct wined3d_format *adapter_format,
3651         const struct wined3d_format *check_format,
3652         WINED3DSURFTYPE SurfaceType)
3653 {
3654     if (SurfaceType == SURFACE_GDI)
3655     {
3656         switch (check_format->id)
3657         {
3658             case WINED3DFMT_B8G8R8_UNORM:
3659                 TRACE_(d3d_caps)("[FAILED] - Not enumerated on Windows\n");
3660                 return FALSE;
3661             case WINED3DFMT_B8G8R8A8_UNORM:
3662             case WINED3DFMT_B8G8R8X8_UNORM:
3663             case WINED3DFMT_B5G6R5_UNORM:
3664             case WINED3DFMT_B5G5R5X1_UNORM:
3665             case WINED3DFMT_B5G5R5A1_UNORM:
3666             case WINED3DFMT_B4G4R4A4_UNORM:
3667             case WINED3DFMT_B2G3R3_UNORM:
3668             case WINED3DFMT_A8_UNORM:
3669             case WINED3DFMT_B2G3R3A8_UNORM:
3670             case WINED3DFMT_B4G4R4X4_UNORM:
3671             case WINED3DFMT_R10G10B10A2_UNORM:
3672             case WINED3DFMT_R8G8B8A8_UNORM:
3673             case WINED3DFMT_R8G8B8X8_UNORM:
3674             case WINED3DFMT_R16G16_UNORM:
3675             case WINED3DFMT_B10G10R10A2_UNORM:
3676             case WINED3DFMT_R16G16B16A16_UNORM:
3677             case WINED3DFMT_P8_UINT:
3678                 TRACE_(d3d_caps)("[OK]\n");
3679                 return TRUE;
3680             default:
3681                 TRACE_(d3d_caps)("[FAILED] - not available on GDI surfaces\n");
3682                 return FALSE;
3683         }
3684     }
3685
3686     /* All format that are supported for textures are supported for surfaces as well */
3687     if (CheckTextureCapability(adapter, check_format)) return TRUE;
3688     /* All depth stencil formats are supported on surfaces */
3689     if (CheckDepthStencilCapability(adapter, adapter_format, check_format)) return TRUE;
3690
3691     /* If opengl can't process the format natively, the blitter may be able to convert it */
3692     if (adapter->blitter->blit_supported(&adapter->gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3693             NULL, WINED3DPOOL_DEFAULT, 0, check_format,
3694             NULL, WINED3DPOOL_DEFAULT, 0, adapter_format))
3695     {
3696         TRACE_(d3d_caps)("[OK]\n");
3697         return TRUE;
3698     }
3699
3700     /* Reject other formats */
3701     TRACE_(d3d_caps)("[FAILED]\n");
3702     return FALSE;
3703 }
3704
3705 static BOOL CheckVertexTextureCapability(const struct wined3d_adapter *adapter,
3706         const struct wined3d_format *format)
3707 {
3708     const struct wined3d_gl_info *gl_info = &adapter->gl_info;
3709
3710     if (!gl_info->limits.vertex_samplers || !(format->flags & WINED3DFMT_FLAG_VTF))
3711         return FALSE;
3712
3713     switch (format->id)
3714     {
3715         case WINED3DFMT_R32G32B32A32_FLOAT:
3716         case WINED3DFMT_R32_FLOAT:
3717             return TRUE;
3718         default:
3719             return !(gl_info->quirks & WINED3D_QUIRK_LIMITED_TEX_FILTERING);
3720     }
3721 }
3722
3723 HRESULT CDECL wined3d_check_device_format(const struct wined3d *wined3d, UINT adapter_idx,
3724         WINED3DDEVTYPE device_type, enum wined3d_format_id adapter_format_id, DWORD usage,
3725         WINED3DRESOURCETYPE resource_type, enum wined3d_format_id check_format_id, WINED3DSURFTYPE surface_type)
3726 {
3727     const struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
3728     const struct wined3d_gl_info *gl_info = &adapter->gl_info;
3729     const struct wined3d_format *adapter_format = wined3d_get_format(gl_info, adapter_format_id);
3730     const struct wined3d_format *format = wined3d_get_format(gl_info, check_format_id);
3731     DWORD usage_caps = 0;
3732
3733     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u, device_type %s, adapter_format %s, usage %s, %s,\n"
3734             "resource_type %s, check_format %s, surface_type %#x.\n",
3735             wined3d, adapter_idx, debug_d3ddevicetype(device_type), debug_d3dformat(adapter_format_id),
3736             debug_d3dusage(usage), debug_d3dusagequery(usage), debug_d3dresourcetype(resource_type),
3737             debug_d3dformat(check_format_id), surface_type);
3738
3739     if (adapter_idx >= wined3d->adapter_count)
3740         return WINED3DERR_INVALIDCALL;
3741
3742     switch (resource_type)
3743     {
3744         case WINED3DRTYPE_CUBETEXTURE:
3745             /* Cubetexture allows:
3746              *      - WINED3DUSAGE_AUTOGENMIPMAP
3747              *      - WINED3DUSAGE_DEPTHSTENCIL
3748              *      - WINED3DUSAGE_DYNAMIC
3749              *      - WINED3DUSAGE_NONSECURE (d3d9ex)
3750              *      - WINED3DUSAGE_RENDERTARGET
3751              *      - WINED3DUSAGE_SOFTWAREPROCESSING
3752              *      - WINED3DUSAGE_QUERY_WRAPANDMIP
3753              */
3754             if (surface_type != SURFACE_OPENGL)
3755             {
3756                 TRACE_(d3d_caps)("[FAILED]\n");
3757                 return WINED3DERR_NOTAVAILABLE;
3758             }
3759
3760             if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3761             {
3762                 TRACE_(d3d_caps)("[FAILED] - No cube texture support\n");
3763                 return WINED3DERR_NOTAVAILABLE;
3764             }
3765
3766             if (!CheckTextureCapability(adapter, format))
3767             {
3768                 TRACE_(d3d_caps)("[FAILED] - Cube texture format not supported\n");
3769                 return WINED3DERR_NOTAVAILABLE;
3770             }
3771
3772             if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
3773             {
3774                 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
3775                     /* When autogenmipmap isn't around continue and return
3776                      * WINED3DOK_NOAUTOGEN instead of D3D_OK. */
3777                     TRACE_(d3d_caps)("[FAILED] - No autogenmipmap support, but continuing\n");
3778                 else
3779                     usage_caps |= WINED3DUSAGE_AUTOGENMIPMAP;
3780             }
3781
3782             /* Always report dynamic locking. */
3783             if (usage & WINED3DUSAGE_DYNAMIC)
3784                 usage_caps |= WINED3DUSAGE_DYNAMIC;
3785
3786             if (usage & WINED3DUSAGE_RENDERTARGET)
3787             {
3788                 if (!CheckRenderTargetCapability(adapter, adapter_format, format))
3789                 {
3790                     TRACE_(d3d_caps)("[FAILED] - No rendertarget support\n");
3791                     return WINED3DERR_NOTAVAILABLE;
3792                 }
3793                 usage_caps |= WINED3DUSAGE_RENDERTARGET;
3794             }
3795
3796             /* Always report software processing. */
3797             if (usage & WINED3DUSAGE_SOFTWAREPROCESSING)
3798                 usage_caps |= WINED3DUSAGE_SOFTWAREPROCESSING;
3799
3800             if (usage & WINED3DUSAGE_QUERY_FILTER)
3801             {
3802                 if (!CheckFilterCapability(adapter, format))
3803                 {
3804                     TRACE_(d3d_caps)("[FAILED] - No query filter support\n");
3805                     return WINED3DERR_NOTAVAILABLE;
3806                 }
3807                 usage_caps |= WINED3DUSAGE_QUERY_FILTER;
3808             }
3809
3810             if (usage & WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING)
3811             {
3812                 if (!CheckPostPixelShaderBlendingCapability(adapter, format))
3813                 {
3814                     TRACE_(d3d_caps)("[FAILED] - No query post pixelshader blending support\n");
3815                     return WINED3DERR_NOTAVAILABLE;
3816                 }
3817                 usage_caps |= WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING;
3818             }
3819
3820             if (usage & WINED3DUSAGE_QUERY_SRGBREAD)
3821             {
3822                 if (!CheckSrgbReadCapability(adapter, format))
3823                 {
3824                     TRACE_(d3d_caps)("[FAILED] - No query srgbread support\n");
3825                     return WINED3DERR_NOTAVAILABLE;
3826                 }
3827                 usage_caps |= WINED3DUSAGE_QUERY_SRGBREAD;
3828             }
3829
3830             if (usage & WINED3DUSAGE_QUERY_SRGBWRITE)
3831             {
3832                 if (!CheckSrgbWriteCapability(adapter, format))
3833                 {
3834                     TRACE_(d3d_caps)("[FAILED] - No query srgbwrite support\n");
3835                     return WINED3DERR_NOTAVAILABLE;
3836                 }
3837                 usage_caps |= WINED3DUSAGE_QUERY_SRGBWRITE;
3838             }
3839
3840             if (usage & WINED3DUSAGE_QUERY_VERTEXTEXTURE)
3841             {
3842                 if (!CheckVertexTextureCapability(adapter, format))
3843                 {
3844                     TRACE_(d3d_caps)("[FAILED] - No query vertextexture support\n");
3845                     return WINED3DERR_NOTAVAILABLE;
3846                 }
3847                 usage_caps |= WINED3DUSAGE_QUERY_VERTEXTEXTURE;
3848             }
3849
3850             if (usage & WINED3DUSAGE_QUERY_WRAPANDMIP)
3851             {
3852                 if (!CheckWrapAndMipCapability(adapter, format))
3853                 {
3854                     TRACE_(d3d_caps)("[FAILED] - No wrapping and mipmapping support\n");
3855                     return WINED3DERR_NOTAVAILABLE;
3856                 }
3857                 usage_caps |= WINED3DUSAGE_QUERY_WRAPANDMIP;
3858             }
3859             break;
3860
3861         case WINED3DRTYPE_SURFACE:
3862             /* Surface allows:
3863              *      - WINED3DUSAGE_DEPTHSTENCIL
3864              *      - WINED3DUSAGE_NONSECURE (d3d9ex)
3865              *      - WINED3DUSAGE_RENDERTARGET
3866              */
3867             if (!CheckSurfaceCapability(adapter, adapter_format, format, surface_type))
3868             {
3869                 TRACE_(d3d_caps)("[FAILED] - Not supported for plain surfaces\n");
3870                 return WINED3DERR_NOTAVAILABLE;
3871             }
3872
3873             if (usage & WINED3DUSAGE_DEPTHSTENCIL)
3874             {
3875                 if (!CheckDepthStencilCapability(adapter, adapter_format, format))
3876                 {
3877                     TRACE_(d3d_caps)("[FAILED] - No depthstencil support\n");
3878                     return WINED3DERR_NOTAVAILABLE;
3879                 }
3880                 usage_caps |= WINED3DUSAGE_DEPTHSTENCIL;
3881             }
3882
3883             if (usage & WINED3DUSAGE_RENDERTARGET)
3884             {
3885                 if (!CheckRenderTargetCapability(adapter, adapter_format, format))
3886                 {
3887                     TRACE_(d3d_caps)("[FAILED] - No rendertarget support\n");
3888                     return WINED3DERR_NOTAVAILABLE;
3889                 }
3890                 usage_caps |= WINED3DUSAGE_RENDERTARGET;
3891             }
3892
3893             if (usage & WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING)
3894             {
3895                 if (!CheckPostPixelShaderBlendingCapability(adapter, format))
3896                 {
3897                     TRACE_(d3d_caps)("[FAILED] - No query post pixelshader blending support\n");
3898                     return WINED3DERR_NOTAVAILABLE;
3899                 }
3900                 usage_caps |= WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING;
3901             }
3902             break;
3903
3904         case WINED3DRTYPE_TEXTURE:
3905             /* Texture allows:
3906              *      - WINED3DUSAGE_AUTOGENMIPMAP
3907              *      - WINED3DUSAGE_DEPTHSTENCIL
3908              *      - WINED3DUSAGE_DMAP
3909              *      - WINED3DUSAGE_DYNAMIC
3910              *      - WINED3DUSAGE_NONSECURE (d3d9ex)
3911              *      - WINED3DUSAGE_RENDERTARGET
3912              *      - WINED3DUSAGE_SOFTWAREPROCESSING
3913              *      - WINED3DUSAGE_TEXTAPI (d3d9ex)
3914              *      - WINED3DUSAGE_QUERY_WRAPANDMIP
3915              */
3916             if (surface_type != SURFACE_OPENGL)
3917             {
3918                 TRACE_(d3d_caps)("[FAILED]\n");
3919                 return WINED3DERR_NOTAVAILABLE;
3920             }
3921
3922             if (!CheckTextureCapability(adapter, format))
3923             {
3924                 TRACE_(d3d_caps)("[FAILED] - Texture format not supported\n");
3925                 return WINED3DERR_NOTAVAILABLE;
3926             }
3927
3928             if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
3929             {
3930                 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
3931                     /* When autogenmipmap isn't around continue and return
3932                      * WINED3DOK_NOAUTOGEN instead of D3D_OK. */
3933                     TRACE_(d3d_caps)("[FAILED] - No autogenmipmap support, but continuing\n");
3934                 else
3935                     usage_caps |= WINED3DUSAGE_AUTOGENMIPMAP;
3936             }
3937
3938             /* Always report dynamic locking. */
3939             if (usage & WINED3DUSAGE_DYNAMIC)
3940                 usage_caps |= WINED3DUSAGE_DYNAMIC;
3941
3942             if (usage & WINED3DUSAGE_RENDERTARGET)
3943             {
3944                 if (!CheckRenderTargetCapability(adapter, adapter_format, format))
3945                 {
3946                     TRACE_(d3d_caps)("[FAILED] - No rendertarget support\n");
3947                     return WINED3DERR_NOTAVAILABLE;
3948                 }
3949                 usage_caps |= WINED3DUSAGE_RENDERTARGET;
3950             }
3951
3952             /* Always report software processing. */
3953             if (usage & WINED3DUSAGE_SOFTWAREPROCESSING)
3954                 usage_caps |= WINED3DUSAGE_SOFTWAREPROCESSING;
3955
3956             if (usage & WINED3DUSAGE_QUERY_FILTER)
3957             {
3958                 if (!CheckFilterCapability(adapter, format))
3959                 {
3960                     TRACE_(d3d_caps)("[FAILED] - No query filter support\n");
3961                     return WINED3DERR_NOTAVAILABLE;
3962                 }
3963                 usage_caps |= WINED3DUSAGE_QUERY_FILTER;
3964             }
3965
3966             if (usage & WINED3DUSAGE_QUERY_LEGACYBUMPMAP)
3967             {
3968                 if (!CheckBumpMapCapability(adapter, format))
3969                 {
3970                     TRACE_(d3d_caps)("[FAILED] - No legacy bumpmap support\n");
3971                     return WINED3DERR_NOTAVAILABLE;
3972                 }
3973                 usage_caps |= WINED3DUSAGE_QUERY_LEGACYBUMPMAP;
3974             }
3975
3976             if (usage & WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING)
3977             {
3978                 if (!CheckPostPixelShaderBlendingCapability(adapter, format))
3979                 {
3980                     TRACE_(d3d_caps)("[FAILED] - No query post pixelshader blending support\n");
3981                     return WINED3DERR_NOTAVAILABLE;
3982                 }
3983                 usage_caps |= WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING;
3984             }
3985
3986             if (usage & WINED3DUSAGE_QUERY_SRGBREAD)
3987             {
3988                 if (!CheckSrgbReadCapability(adapter, format))
3989                 {
3990                     TRACE_(d3d_caps)("[FAILED] - No query srgbread support\n");
3991                     return WINED3DERR_NOTAVAILABLE;
3992                 }
3993                 usage_caps |= WINED3DUSAGE_QUERY_SRGBREAD;
3994             }
3995
3996             if (usage & WINED3DUSAGE_QUERY_SRGBWRITE)
3997             {
3998                 if (!CheckSrgbWriteCapability(adapter, format))
3999                 {
4000                     TRACE_(d3d_caps)("[FAILED] - No query srgbwrite support\n");
4001                     return WINED3DERR_NOTAVAILABLE;
4002                 }
4003                 usage_caps |= WINED3DUSAGE_QUERY_SRGBWRITE;
4004             }
4005
4006             if (usage & WINED3DUSAGE_QUERY_VERTEXTEXTURE)
4007             {
4008                 if (!CheckVertexTextureCapability(adapter, format))
4009                 {
4010                     TRACE_(d3d_caps)("[FAILED] - No query vertextexture support\n");
4011                     return WINED3DERR_NOTAVAILABLE;
4012                 }
4013                 usage_caps |= WINED3DUSAGE_QUERY_VERTEXTEXTURE;
4014             }
4015
4016             if (usage & WINED3DUSAGE_QUERY_WRAPANDMIP)
4017             {
4018                 if (!CheckWrapAndMipCapability(adapter, format))
4019                 {
4020                     TRACE_(d3d_caps)("[FAILED] - No wrapping and mipmapping support\n");
4021                     return WINED3DERR_NOTAVAILABLE;
4022                 }
4023                 usage_caps |= WINED3DUSAGE_QUERY_WRAPANDMIP;
4024             }
4025
4026             if (usage & WINED3DUSAGE_DEPTHSTENCIL)
4027             {
4028                 if (!CheckDepthStencilCapability(adapter, adapter_format, format))
4029                 {
4030                     TRACE_(d3d_caps)("[FAILED] - No depth stencil support\n");
4031                     return WINED3DERR_NOTAVAILABLE;
4032                 }
4033                 if ((format->flags & WINED3DFMT_FLAG_SHADOW) && !gl_info->supported[ARB_SHADOW])
4034                 {
4035                     TRACE_(d3d_caps)("[FAILED] - No shadow sampler support.\n");
4036                     return WINED3DERR_NOTAVAILABLE;
4037                 }
4038                 usage_caps |= WINED3DUSAGE_DEPTHSTENCIL;
4039             }
4040             break;
4041
4042         case WINED3DRTYPE_VOLUMETEXTURE:
4043         case WINED3DRTYPE_VOLUME:
4044             /* Volume is to VolumeTexture what Surface is to Texture, but its
4045              * usage caps are not documented. Most driver seem to offer
4046              * (nearly) the same on Volume and VolumeTexture, so do that too.
4047              *
4048              * Volumetexture allows:
4049              *      - D3DUSAGE_DYNAMIC
4050              *      - D3DUSAGE_NONSECURE (d3d9ex)
4051              *      - D3DUSAGE_SOFTWAREPROCESSING
4052              *      - D3DUSAGE_QUERY_WRAPANDMIP
4053              */
4054             if (surface_type != SURFACE_OPENGL)
4055             {
4056                 TRACE_(d3d_caps)("[FAILED]\n");
4057                 return WINED3DERR_NOTAVAILABLE;
4058             }
4059
4060             if (!gl_info->supported[EXT_TEXTURE3D])
4061             {
4062                 TRACE_(d3d_caps)("[FAILED] - No volume texture support\n");
4063                 return WINED3DERR_NOTAVAILABLE;
4064             }
4065
4066             if (!CheckTextureCapability(adapter, format))
4067             {
4068                 TRACE_(d3d_caps)("[FAILED] - Format not supported\n");
4069                 return WINED3DERR_NOTAVAILABLE;
4070             }
4071
4072             /* Filter formats that need conversion; For one part, this
4073              * conversion is unimplemented, and volume textures are huge, so
4074              * it would be a big performance hit. Unless we hit an application
4075              * needing one of those formats, don't advertize them to avoid
4076              * leading applications into temptation. The windows drivers don't
4077              * support most of those formats on volumes anyway, except for
4078              * WINED3DFMT_R32_FLOAT. */
4079             switch (check_format_id)
4080             {
4081                 case WINED3DFMT_P8_UINT:
4082                 case WINED3DFMT_L4A4_UNORM:
4083                 case WINED3DFMT_R32_FLOAT:
4084                 case WINED3DFMT_R16_FLOAT:
4085                 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM:
4086                 case WINED3DFMT_R5G5_SNORM_L6_UNORM:
4087                 case WINED3DFMT_R16G16_UNORM:
4088                     TRACE_(d3d_caps)("[FAILED] - No converted formats on volumes\n");
4089                     return WINED3DERR_NOTAVAILABLE;
4090
4091                 case WINED3DFMT_R8G8B8A8_SNORM:
4092                 case WINED3DFMT_R16G16_SNORM:
4093                     if (!gl_info->supported[NV_TEXTURE_SHADER])
4094                     {
4095                         TRACE_(d3d_caps)("[FAILED] - No converted formats on volumes\n");
4096                         return WINED3DERR_NOTAVAILABLE;
4097                     }
4098                     break;
4099
4100                 case WINED3DFMT_R8G8_SNORM:
4101                     if (!gl_info->supported[NV_TEXTURE_SHADER])
4102                     {
4103                         TRACE_(d3d_caps)("[FAILED] - No converted formats on volumes\n");
4104                         return WINED3DERR_NOTAVAILABLE;
4105                     }
4106                     break;
4107
4108                 case WINED3DFMT_DXT1:
4109                 case WINED3DFMT_DXT2:
4110                 case WINED3DFMT_DXT3:
4111                 case WINED3DFMT_DXT4:
4112                 case WINED3DFMT_DXT5:
4113                     /* The GL_EXT_texture_compression_s3tc spec requires that
4114                      * loading an s3tc compressed texture results in an error.
4115                      * While the D3D refrast does support s3tc volumes, at
4116                      * least the nvidia windows driver does not, so we're free
4117                      * not to support this format. */
4118                     TRACE_(d3d_caps)("[FAILED] - DXTn does not support 3D textures\n");
4119                     return WINED3DERR_NOTAVAILABLE;
4120
4121                 default:
4122                     /* Do nothing, continue with checking the format below */
4123                     break;
4124             }
4125
4126             /* Always report dynamic locking. */
4127             if (usage & WINED3DUSAGE_DYNAMIC)
4128                 usage_caps |= WINED3DUSAGE_DYNAMIC;
4129
4130             /* Always report software processing. */
4131             if (usage & WINED3DUSAGE_SOFTWAREPROCESSING)
4132                 usage_caps |= WINED3DUSAGE_SOFTWAREPROCESSING;
4133
4134             if (usage & WINED3DUSAGE_QUERY_FILTER)
4135             {
4136                 if (!CheckFilterCapability(adapter, format))
4137                 {
4138                     TRACE_(d3d_caps)("[FAILED] - No query filter support\n");
4139                     return WINED3DERR_NOTAVAILABLE;
4140                 }
4141                 usage_caps |= WINED3DUSAGE_QUERY_FILTER;
4142             }
4143
4144             if (usage & WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING)
4145             {
4146                 if (!CheckPostPixelShaderBlendingCapability(adapter, format))
4147                 {
4148                     TRACE_(d3d_caps)("[FAILED] - No query post pixelshader blending support\n");
4149                     return WINED3DERR_NOTAVAILABLE;
4150                 }
4151                 usage_caps |= WINED3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING;
4152             }
4153
4154             if (usage & WINED3DUSAGE_QUERY_SRGBREAD)
4155             {
4156                 if (!CheckSrgbReadCapability(adapter, format))
4157                 {
4158                     TRACE_(d3d_caps)("[FAILED] - No query srgbread support\n");
4159                     return WINED3DERR_NOTAVAILABLE;
4160                 }
4161                 usage_caps |= WINED3DUSAGE_QUERY_SRGBREAD;
4162             }
4163
4164             if (usage & WINED3DUSAGE_QUERY_SRGBWRITE)
4165             {
4166                 if (!CheckSrgbWriteCapability(adapter, format))
4167                 {
4168                     TRACE_(d3d_caps)("[FAILED] - No query srgbwrite support\n");
4169                     return WINED3DERR_NOTAVAILABLE;
4170                 }
4171                 usage_caps |= WINED3DUSAGE_QUERY_SRGBWRITE;
4172             }
4173
4174             if (usage & WINED3DUSAGE_QUERY_VERTEXTEXTURE)
4175             {
4176                 if (!CheckVertexTextureCapability(adapter, format))
4177                 {
4178                     TRACE_(d3d_caps)("[FAILED] - No query vertextexture support\n");
4179                     return WINED3DERR_NOTAVAILABLE;
4180                 }
4181                 usage_caps |= WINED3DUSAGE_QUERY_VERTEXTEXTURE;
4182             }
4183
4184             if (usage & WINED3DUSAGE_QUERY_WRAPANDMIP)
4185             {
4186                 if (!CheckWrapAndMipCapability(adapter, format))
4187                 {
4188                     TRACE_(d3d_caps)("[FAILED] - No wrapping and mipmapping support\n");
4189                     return WINED3DERR_NOTAVAILABLE;
4190                 }
4191                 usage_caps |= WINED3DUSAGE_QUERY_WRAPANDMIP;
4192             }
4193             break;
4194
4195         default:
4196             FIXME_(d3d_caps)("Unhandled resource type %s.\n", debug_d3dresourcetype(resource_type));
4197             return WINED3DERR_NOTAVAILABLE;
4198     }
4199
4200     /* When the usage_caps exactly matches usage return WINED3D_OK except for
4201      * the situation in which WINED3DUSAGE_AUTOGENMIPMAP isn't around, then
4202      * WINED3DOK_NOAUTOGEN is returned if all the other usage flags match. */
4203     if (usage_caps == usage)
4204         return WINED3D_OK;
4205     if (usage_caps == (usage & ~WINED3DUSAGE_AUTOGENMIPMAP))
4206         return WINED3DOK_NOAUTOGEN;
4207
4208     TRACE_(d3d_caps)("[FAILED] - Usage %#x requested for format %s and resource_type %s but only %#x is available.\n",
4209             usage, debug_d3dformat(check_format_id), debug_d3dresourcetype(resource_type), usage_caps);
4210
4211     return WINED3DERR_NOTAVAILABLE;
4212 }
4213
4214 HRESULT CDECL wined3d_check_device_format_conversion(const struct wined3d *wined3d, UINT adapter_idx,
4215         WINED3DDEVTYPE device_type, enum wined3d_format_id src_format, enum wined3d_format_id dst_format)
4216 {
4217     FIXME("wined3d %p, adapter_idx %u, device_type %s, src_format %s, dst_format %s stub!\n",
4218             wined3d, adapter_idx, debug_d3ddevicetype(device_type), debug_d3dformat(src_format),
4219             debug_d3dformat(dst_format));
4220
4221     return WINED3D_OK;
4222 }
4223
4224 HRESULT CDECL wined3d_check_device_type(const struct wined3d *wined3d, UINT adapter_idx, WINED3DDEVTYPE device_type,
4225         enum wined3d_format_id display_format, enum wined3d_format_id backbuffer_format, BOOL windowed)
4226 {
4227     UINT mode_count;
4228     HRESULT hr;
4229
4230     TRACE("wined3d %p, adapter_idx %u, device_type %s, display_format %s, backbuffer_format %s, windowed %#x.\n",
4231             wined3d, adapter_idx, debug_d3ddevicetype(device_type), debug_d3dformat(display_format),
4232             debug_d3dformat(backbuffer_format), windowed);
4233
4234     if (adapter_idx >= wined3d->adapter_count)
4235         return WINED3DERR_INVALIDCALL;
4236
4237     /* The task of this function is to check whether a certain display / backbuffer format
4238      * combination is available on the given adapter. In fullscreen mode microsoft specified
4239      * that the display format shouldn't provide alpha and that ignoring alpha the backbuffer
4240      * and display format should match exactly.
4241      * In windowed mode format conversion can occur and this depends on the driver. When format
4242      * conversion is done, this function should nevertheless fail and applications need to use
4243      * CheckDeviceFormatConversion.
4244      * At the moment we assume that fullscreen and windowed have the same capabilities. */
4245
4246     /* There are only 4 display formats. */
4247     if (!(display_format == WINED3DFMT_B5G6R5_UNORM
4248             || display_format == WINED3DFMT_B5G5R5X1_UNORM
4249             || display_format == WINED3DFMT_B8G8R8X8_UNORM
4250             || display_format == WINED3DFMT_B10G10R10A2_UNORM))
4251     {
4252         TRACE_(d3d_caps)("Format %s is not supported as display format.\n", debug_d3dformat(display_format));
4253         return WINED3DERR_NOTAVAILABLE;
4254     }
4255
4256     /* If the requested display format is not available, don't continue. */
4257     mode_count = wined3d_get_adapter_mode_count(wined3d, adapter_idx, display_format);
4258     if (!mode_count)
4259     {
4260         TRACE_(d3d_caps)("No available modes for display format %s.\n", debug_d3dformat(display_format));
4261         return WINED3DERR_NOTAVAILABLE;
4262     }
4263
4264     /* Windowed mode allows you to specify WINED3DFMT_UNKNOWN for the backbuffer format,
4265      * it means 'reuse' the display format for the backbuffer. */
4266     if (!windowed && backbuffer_format == WINED3DFMT_UNKNOWN)
4267     {
4268         TRACE_(d3d_caps)("backbuffer_format WINED3FMT_UNKNOWN only available in windowed mode.\n");
4269         return WINED3DERR_NOTAVAILABLE;
4270     }
4271
4272     /* In FULLSCREEN mode WINED3DFMT_B5G6R5_UNORM can only be mixed with
4273      * backbuffer format WINED3DFMT_B5G6R5_UNORM. */
4274     if (display_format == WINED3DFMT_B5G6R5_UNORM && backbuffer_format != WINED3DFMT_B5G6R5_UNORM)
4275     {
4276         TRACE_(d3d_caps)("Unsupported display/backbuffer format combination %s / %s.\n",
4277                 debug_d3dformat(display_format), debug_d3dformat(backbuffer_format));
4278         return WINED3DERR_NOTAVAILABLE;
4279     }
4280
4281     /* In FULLSCREEN mode WINED3DFMT_B5G5R5X1_UNORM can only be mixed with
4282      * backbuffer formats WINED3DFMT_B5G5R5X1_UNORM and
4283      * WINED3DFMT_B5G5R5A1_UNORM. */
4284     if (display_format == WINED3DFMT_B5G5R5X1_UNORM
4285             && !(backbuffer_format == WINED3DFMT_B5G5R5X1_UNORM || backbuffer_format == WINED3DFMT_B5G5R5A1_UNORM))
4286     {
4287         TRACE_(d3d_caps)("Unsupported display/backbuffer format combination %s / %s.\n",
4288                 debug_d3dformat(display_format), debug_d3dformat(backbuffer_format));
4289         return WINED3DERR_NOTAVAILABLE;
4290     }
4291
4292     /* In FULLSCREEN mode WINED3DFMT_B8G8R8X8_UNORM can only be mixed with
4293      * backbuffer formats WINED3DFMT_B8G8R8X8_UNORM and
4294      * WINED3DFMT_B8G8R8A8_UNORM. */
4295     if (display_format == WINED3DFMT_B8G8R8X8_UNORM
4296             && !(backbuffer_format == WINED3DFMT_B8G8R8X8_UNORM || backbuffer_format == WINED3DFMT_B8G8R8A8_UNORM))
4297     {
4298         TRACE_(d3d_caps)("Unsupported display/backbuffer format combination %s / %s.\n",
4299                 debug_d3dformat(display_format), debug_d3dformat(backbuffer_format));
4300         return WINED3DERR_NOTAVAILABLE;
4301     }
4302
4303     /* WINED3DFMT_B10G10R10A2_UNORM is only allowed in fullscreen mode and it
4304      * can only be mixed with backbuffer format WINED3DFMT_B10G10R10A2_UNORM. */
4305     if (display_format == WINED3DFMT_B10G10R10A2_UNORM
4306             && (backbuffer_format != WINED3DFMT_B10G10R10A2_UNORM || windowed))
4307     {
4308         TRACE_(d3d_caps)("Unsupported display/backbuffer format combination %s / %s.\n",
4309                 debug_d3dformat(display_format), debug_d3dformat(backbuffer_format));
4310         return WINED3DERR_NOTAVAILABLE;
4311     }
4312
4313     /* Use CheckDeviceFormat to see if the backbuffer_format is usable with the given display_format */
4314     hr = wined3d_check_device_format(wined3d, adapter_idx, device_type, display_format,
4315             WINED3DUSAGE_RENDERTARGET, WINED3DRTYPE_SURFACE, backbuffer_format, SURFACE_OPENGL);
4316     if (FAILED(hr))
4317         TRACE_(d3d_caps)("Unsupported display/backbuffer format combination %s / %s.\n",
4318                 debug_d3dformat(display_format), debug_d3dformat(backbuffer_format));
4319
4320     return hr;
4321 }
4322
4323 HRESULT CDECL wined3d_get_device_caps(const struct wined3d *wined3d, UINT adapter_idx,
4324         WINED3DDEVTYPE device_type, WINED3DCAPS *caps)
4325 {
4326     const struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4327     const struct wined3d_gl_info *gl_info = &adapter->gl_info;
4328     int vs_selected_mode;
4329     int ps_selected_mode;
4330     struct shader_caps shader_caps;
4331     struct fragment_caps fragment_caps;
4332     DWORD ckey_caps, blit_caps, fx_caps, pal_caps;
4333
4334     TRACE_(d3d_caps)("wined3d %p, adapter_idx %u, device_type %s, caps %p.\n",
4335             wined3d, adapter_idx, debug_d3ddevicetype(device_type), caps);
4336
4337     if (adapter_idx >= wined3d->adapter_count)
4338         return WINED3DERR_INVALIDCALL;
4339
4340     select_shader_mode(&adapter->gl_info, &ps_selected_mode, &vs_selected_mode);
4341
4342     /* ------------------------------------------------
4343        The following fields apply to both d3d8 and d3d9
4344        ------------------------------------------------ */
4345     /* Not quite true, but use h/w supported by opengl I suppose */
4346     caps->DeviceType               = (device_type == WINED3DDEVTYPE_HAL) ? WINED3DDEVTYPE_HAL : WINED3DDEVTYPE_REF;
4347     caps->AdapterOrdinal           = adapter_idx;
4348
4349     caps->Caps                     = 0;
4350     caps->Caps2                    = WINED3DCAPS2_CANRENDERWINDOWED |
4351                                      WINED3DCAPS2_FULLSCREENGAMMA |
4352                                      WINED3DCAPS2_DYNAMICTEXTURES;
4353     if (gl_info->supported[SGIS_GENERATE_MIPMAP])
4354         caps->Caps2 |= WINED3DCAPS2_CANAUTOGENMIPMAP;
4355
4356     caps->Caps3                    = WINED3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD |
4357                                      WINED3DCAPS3_COPY_TO_VIDMEM                   |
4358                                      WINED3DCAPS3_COPY_TO_SYSTEMMEM;
4359
4360     caps->PresentationIntervals    = WINED3DPRESENT_INTERVAL_IMMEDIATE  |
4361                                      WINED3DPRESENT_INTERVAL_ONE;
4362
4363     caps->CursorCaps               = WINED3DCURSORCAPS_COLOR            |
4364                                      WINED3DCURSORCAPS_LOWRES;
4365
4366     caps->DevCaps                  = WINED3DDEVCAPS_FLOATTLVERTEX       |
4367                                      WINED3DDEVCAPS_EXECUTESYSTEMMEMORY |
4368                                      WINED3DDEVCAPS_TLVERTEXSYSTEMMEMORY|
4369                                      WINED3DDEVCAPS_TLVERTEXVIDEOMEMORY |
4370                                      WINED3DDEVCAPS_DRAWPRIMTLVERTEX    |
4371                                      WINED3DDEVCAPS_HWTRANSFORMANDLIGHT |
4372                                      WINED3DDEVCAPS_EXECUTEVIDEOMEMORY  |
4373                                      WINED3DDEVCAPS_PUREDEVICE          |
4374                                      WINED3DDEVCAPS_HWRASTERIZATION     |
4375                                      WINED3DDEVCAPS_TEXTUREVIDEOMEMORY  |
4376                                      WINED3DDEVCAPS_TEXTURESYSTEMMEMORY |
4377                                      WINED3DDEVCAPS_CANRENDERAFTERFLIP  |
4378                                      WINED3DDEVCAPS_DRAWPRIMITIVES2     |
4379                                      WINED3DDEVCAPS_DRAWPRIMITIVES2EX   |
4380                                      WINED3DDEVCAPS_RTPATCHES;
4381
4382     caps->PrimitiveMiscCaps        = WINED3DPMISCCAPS_CULLNONE              |
4383                                      WINED3DPMISCCAPS_CULLCCW               |
4384                                      WINED3DPMISCCAPS_CULLCW                |
4385                                      WINED3DPMISCCAPS_COLORWRITEENABLE      |
4386                                      WINED3DPMISCCAPS_CLIPTLVERTS           |
4387                                      WINED3DPMISCCAPS_CLIPPLANESCALEDPOINTS |
4388                                      WINED3DPMISCCAPS_MASKZ                 |
4389                                      WINED3DPMISCCAPS_BLENDOP               |
4390                                      WINED3DPMISCCAPS_MRTPOSTPIXELSHADERBLENDING;
4391                                     /* TODO:
4392                                         WINED3DPMISCCAPS_NULLREFERENCE
4393                                         WINED3DPMISCCAPS_FOGANDSPECULARALPHA
4394                                         WINED3DPMISCCAPS_MRTINDEPENDENTBITDEPTHS
4395                                         WINED3DPMISCCAPS_FOGVERTEXCLAMPED */
4396
4397     if (gl_info->supported[EXT_BLEND_EQUATION_SEPARATE] && gl_info->supported[EXT_BLEND_FUNC_SEPARATE])
4398         caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_SEPARATEALPHABLEND;
4399     if (gl_info->supported[EXT_DRAW_BUFFERS2])
4400         caps->PrimitiveMiscCaps |= WINED3DPMISCCAPS_INDEPENDENTWRITEMASKS;
4401
4402     caps->RasterCaps               = WINED3DPRASTERCAPS_DITHER    |
4403                                      WINED3DPRASTERCAPS_PAT       |
4404                                      WINED3DPRASTERCAPS_WFOG      |
4405                                      WINED3DPRASTERCAPS_ZFOG      |
4406                                      WINED3DPRASTERCAPS_FOGVERTEX |
4407                                      WINED3DPRASTERCAPS_FOGTABLE  |
4408                                      WINED3DPRASTERCAPS_STIPPLE   |
4409                                      WINED3DPRASTERCAPS_SUBPIXEL  |
4410                                      WINED3DPRASTERCAPS_ZTEST     |
4411                                      WINED3DPRASTERCAPS_SCISSORTEST   |
4412                                      WINED3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
4413                                      WINED3DPRASTERCAPS_DEPTHBIAS;
4414
4415     if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
4416     {
4417         caps->RasterCaps  |= WINED3DPRASTERCAPS_ANISOTROPY    |
4418                              WINED3DPRASTERCAPS_ZBIAS         |
4419                              WINED3DPRASTERCAPS_MIPMAPLODBIAS;
4420     }
4421     if (gl_info->supported[NV_FOG_DISTANCE])
4422     {
4423         caps->RasterCaps          |= WINED3DPRASTERCAPS_FOGRANGE;
4424     }
4425                         /* FIXME Add:
4426                            WINED3DPRASTERCAPS_COLORPERSPECTIVE
4427                            WINED3DPRASTERCAPS_STRETCHBLTMULTISAMPLE
4428                            WINED3DPRASTERCAPS_ANTIALIASEDGES
4429                            WINED3DPRASTERCAPS_ZBUFFERLESSHSR
4430                            WINED3DPRASTERCAPS_WBUFFER */
4431
4432     caps->ZCmpCaps =  WINED3DPCMPCAPS_ALWAYS       |
4433                       WINED3DPCMPCAPS_EQUAL        |
4434                       WINED3DPCMPCAPS_GREATER      |
4435                       WINED3DPCMPCAPS_GREATEREQUAL |
4436                       WINED3DPCMPCAPS_LESS         |
4437                       WINED3DPCMPCAPS_LESSEQUAL    |
4438                       WINED3DPCMPCAPS_NEVER        |
4439                       WINED3DPCMPCAPS_NOTEQUAL;
4440
4441     caps->SrcBlendCaps  =  WINED3DPBLENDCAPS_BOTHINVSRCALPHA |
4442                            WINED3DPBLENDCAPS_BOTHSRCALPHA    |
4443                            WINED3DPBLENDCAPS_DESTALPHA       |
4444                            WINED3DPBLENDCAPS_DESTCOLOR       |
4445                            WINED3DPBLENDCAPS_INVDESTALPHA    |
4446                            WINED3DPBLENDCAPS_INVDESTCOLOR    |
4447                            WINED3DPBLENDCAPS_INVSRCALPHA     |
4448                            WINED3DPBLENDCAPS_INVSRCCOLOR     |
4449                            WINED3DPBLENDCAPS_ONE             |
4450                            WINED3DPBLENDCAPS_SRCALPHA        |
4451                            WINED3DPBLENDCAPS_SRCALPHASAT     |
4452                            WINED3DPBLENDCAPS_SRCCOLOR        |
4453                            WINED3DPBLENDCAPS_ZERO;
4454
4455     caps->DestBlendCaps =  WINED3DPBLENDCAPS_DESTALPHA       |
4456                            WINED3DPBLENDCAPS_DESTCOLOR       |
4457                            WINED3DPBLENDCAPS_INVDESTALPHA    |
4458                            WINED3DPBLENDCAPS_INVDESTCOLOR    |
4459                            WINED3DPBLENDCAPS_INVSRCALPHA     |
4460                            WINED3DPBLENDCAPS_INVSRCCOLOR     |
4461                            WINED3DPBLENDCAPS_ONE             |
4462                            WINED3DPBLENDCAPS_SRCALPHA        |
4463                            WINED3DPBLENDCAPS_SRCCOLOR        |
4464                            WINED3DPBLENDCAPS_ZERO;
4465     /* NOTE: WINED3DPBLENDCAPS_SRCALPHASAT is not supported as dest blend factor,
4466      * according to the glBlendFunc manpage
4467      *
4468      * WINED3DPBLENDCAPS_BOTHINVSRCALPHA and WINED3DPBLENDCAPS_BOTHSRCALPHA are
4469      * legacy settings for srcblend only
4470      */
4471
4472     if (gl_info->supported[EXT_BLEND_COLOR])
4473     {
4474         caps->SrcBlendCaps |= WINED3DPBLENDCAPS_BLENDFACTOR;
4475         caps->DestBlendCaps |= WINED3DPBLENDCAPS_BLENDFACTOR;
4476     }
4477
4478
4479     caps->AlphaCmpCaps  = WINED3DPCMPCAPS_ALWAYS       |
4480                           WINED3DPCMPCAPS_EQUAL        |
4481                           WINED3DPCMPCAPS_GREATER      |
4482                           WINED3DPCMPCAPS_GREATEREQUAL |
4483                           WINED3DPCMPCAPS_LESS         |
4484                           WINED3DPCMPCAPS_LESSEQUAL    |
4485                           WINED3DPCMPCAPS_NEVER        |
4486                           WINED3DPCMPCAPS_NOTEQUAL;
4487
4488     caps->ShadeCaps      = WINED3DPSHADECAPS_SPECULARGOURAUDRGB |
4489                            WINED3DPSHADECAPS_COLORGOURAUDRGB    |
4490                            WINED3DPSHADECAPS_ALPHAFLATBLEND     |
4491                            WINED3DPSHADECAPS_ALPHAGOURAUDBLEND  |
4492                            WINED3DPSHADECAPS_COLORFLATRGB       |
4493                            WINED3DPSHADECAPS_FOGFLAT            |
4494                            WINED3DPSHADECAPS_FOGGOURAUD         |
4495                            WINED3DPSHADECAPS_SPECULARFLATRGB;
4496
4497     caps->TextureCaps   = WINED3DPTEXTURECAPS_ALPHA              |
4498                           WINED3DPTEXTURECAPS_ALPHAPALETTE       |
4499                           WINED3DPTEXTURECAPS_TRANSPARENCY       |
4500                           WINED3DPTEXTURECAPS_BORDER             |
4501                           WINED3DPTEXTURECAPS_MIPMAP             |
4502                           WINED3DPTEXTURECAPS_PROJECTED          |
4503                           WINED3DPTEXTURECAPS_PERSPECTIVE;
4504
4505     if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
4506     {
4507         caps->TextureCaps  |= WINED3DPTEXTURECAPS_POW2 |
4508                               WINED3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4509     }
4510
4511     if (gl_info->supported[EXT_TEXTURE3D])
4512     {
4513         caps->TextureCaps  |=  WINED3DPTEXTURECAPS_VOLUMEMAP      |
4514                                WINED3DPTEXTURECAPS_MIPVOLUMEMAP;
4515         if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
4516         {
4517             caps->TextureCaps |= WINED3DPTEXTURECAPS_VOLUMEMAP_POW2;
4518         }
4519     }
4520
4521     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4522     {
4523         caps->TextureCaps  |= WINED3DPTEXTURECAPS_CUBEMAP     |
4524                               WINED3DPTEXTURECAPS_MIPCUBEMAP;
4525         if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
4526         {
4527             caps->TextureCaps |= WINED3DPTEXTURECAPS_CUBEMAP_POW2;
4528         }
4529     }
4530
4531     caps->TextureFilterCaps =  WINED3DPTFILTERCAPS_MAGFLINEAR       |
4532                                WINED3DPTFILTERCAPS_MAGFPOINT        |
4533                                WINED3DPTFILTERCAPS_MINFLINEAR       |
4534                                WINED3DPTFILTERCAPS_MINFPOINT        |
4535                                WINED3DPTFILTERCAPS_MIPFLINEAR       |
4536                                WINED3DPTFILTERCAPS_MIPFPOINT        |
4537                                WINED3DPTFILTERCAPS_LINEAR           |
4538                                WINED3DPTFILTERCAPS_LINEARMIPLINEAR  |
4539                                WINED3DPTFILTERCAPS_LINEARMIPNEAREST |
4540                                WINED3DPTFILTERCAPS_MIPLINEAR        |
4541                                WINED3DPTFILTERCAPS_MIPNEAREST       |
4542                                WINED3DPTFILTERCAPS_NEAREST;
4543
4544     if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
4545     {
4546         caps->TextureFilterCaps  |= WINED3DPTFILTERCAPS_MAGFANISOTROPIC |
4547                                     WINED3DPTFILTERCAPS_MINFANISOTROPIC;
4548     }
4549
4550     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4551     {
4552         caps->CubeTextureFilterCaps =  WINED3DPTFILTERCAPS_MAGFLINEAR       |
4553                                        WINED3DPTFILTERCAPS_MAGFPOINT        |
4554                                        WINED3DPTFILTERCAPS_MINFLINEAR       |
4555                                        WINED3DPTFILTERCAPS_MINFPOINT        |
4556                                        WINED3DPTFILTERCAPS_MIPFLINEAR       |
4557                                        WINED3DPTFILTERCAPS_MIPFPOINT        |
4558                                        WINED3DPTFILTERCAPS_LINEAR           |
4559                                        WINED3DPTFILTERCAPS_LINEARMIPLINEAR  |
4560                                        WINED3DPTFILTERCAPS_LINEARMIPNEAREST |
4561                                        WINED3DPTFILTERCAPS_MIPLINEAR        |
4562                                        WINED3DPTFILTERCAPS_MIPNEAREST       |
4563                                        WINED3DPTFILTERCAPS_NEAREST;
4564
4565         if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
4566         {
4567             caps->CubeTextureFilterCaps  |= WINED3DPTFILTERCAPS_MAGFANISOTROPIC |
4568                                             WINED3DPTFILTERCAPS_MINFANISOTROPIC;
4569         }
4570     }
4571     else
4572     {
4573         caps->CubeTextureFilterCaps = 0;
4574     }
4575
4576     if (gl_info->supported[EXT_TEXTURE3D])
4577     {
4578         caps->VolumeTextureFilterCaps  = WINED3DPTFILTERCAPS_MAGFLINEAR       |
4579                                          WINED3DPTFILTERCAPS_MAGFPOINT        |
4580                                          WINED3DPTFILTERCAPS_MINFLINEAR       |
4581                                          WINED3DPTFILTERCAPS_MINFPOINT        |
4582                                          WINED3DPTFILTERCAPS_MIPFLINEAR       |
4583                                          WINED3DPTFILTERCAPS_MIPFPOINT        |
4584                                          WINED3DPTFILTERCAPS_LINEAR           |
4585                                          WINED3DPTFILTERCAPS_LINEARMIPLINEAR  |
4586                                          WINED3DPTFILTERCAPS_LINEARMIPNEAREST |
4587                                          WINED3DPTFILTERCAPS_MIPLINEAR        |
4588                                          WINED3DPTFILTERCAPS_MIPNEAREST       |
4589                                          WINED3DPTFILTERCAPS_NEAREST;
4590     }
4591     else
4592     {
4593         caps->VolumeTextureFilterCaps = 0;
4594     }
4595
4596     caps->TextureAddressCaps  =  WINED3DPTADDRESSCAPS_INDEPENDENTUV |
4597                                  WINED3DPTADDRESSCAPS_CLAMP  |
4598                                  WINED3DPTADDRESSCAPS_WRAP;
4599
4600     if (gl_info->supported[ARB_TEXTURE_BORDER_CLAMP])
4601     {
4602         caps->TextureAddressCaps |= WINED3DPTADDRESSCAPS_BORDER;
4603     }
4604     if (gl_info->supported[ARB_TEXTURE_MIRRORED_REPEAT])
4605     {
4606         caps->TextureAddressCaps |= WINED3DPTADDRESSCAPS_MIRROR;
4607     }
4608     if (gl_info->supported[ATI_TEXTURE_MIRROR_ONCE])
4609     {
4610         caps->TextureAddressCaps |= WINED3DPTADDRESSCAPS_MIRRORONCE;
4611     }
4612
4613     if (gl_info->supported[EXT_TEXTURE3D])
4614     {
4615         caps->VolumeTextureAddressCaps =   WINED3DPTADDRESSCAPS_INDEPENDENTUV |
4616                                            WINED3DPTADDRESSCAPS_CLAMP  |
4617                                            WINED3DPTADDRESSCAPS_WRAP;
4618         if (gl_info->supported[ARB_TEXTURE_BORDER_CLAMP])
4619         {
4620             caps->VolumeTextureAddressCaps |= WINED3DPTADDRESSCAPS_BORDER;
4621         }
4622         if (gl_info->supported[ARB_TEXTURE_MIRRORED_REPEAT])
4623         {
4624             caps->VolumeTextureAddressCaps |= WINED3DPTADDRESSCAPS_MIRROR;
4625         }
4626         if (gl_info->supported[ATI_TEXTURE_MIRROR_ONCE])
4627         {
4628             caps->VolumeTextureAddressCaps |= WINED3DPTADDRESSCAPS_MIRRORONCE;
4629         }
4630     }
4631     else
4632     {
4633         caps->VolumeTextureAddressCaps = 0;
4634     }
4635
4636     caps->LineCaps  = WINED3DLINECAPS_TEXTURE       |
4637                       WINED3DLINECAPS_ZTEST         |
4638                       WINED3DLINECAPS_BLEND         |
4639                       WINED3DLINECAPS_ALPHACMP      |
4640                       WINED3DLINECAPS_FOG;
4641     /* WINED3DLINECAPS_ANTIALIAS is not supported on Windows, and dx and gl seem to have a different
4642      * idea how generating the smoothing alpha values works; the result is different
4643      */
4644
4645     caps->MaxTextureWidth = gl_info->limits.texture_size;
4646     caps->MaxTextureHeight = gl_info->limits.texture_size;
4647
4648     if (gl_info->supported[EXT_TEXTURE3D])
4649         caps->MaxVolumeExtent = gl_info->limits.texture3d_size;
4650     else
4651         caps->MaxVolumeExtent = 0;
4652
4653     caps->MaxTextureRepeat = 32768;
4654     caps->MaxTextureAspectRatio = gl_info->limits.texture_size;
4655     caps->MaxVertexW = 1.0f;
4656
4657     caps->GuardBandLeft = 0.0f;
4658     caps->GuardBandTop = 0.0f;
4659     caps->GuardBandRight = 0.0f;
4660     caps->GuardBandBottom = 0.0f;
4661
4662     caps->ExtentsAdjust = 0.0f;
4663
4664     caps->StencilCaps   = WINED3DSTENCILCAPS_DECRSAT |
4665                           WINED3DSTENCILCAPS_INCRSAT |
4666                           WINED3DSTENCILCAPS_INVERT  |
4667                           WINED3DSTENCILCAPS_KEEP    |
4668                           WINED3DSTENCILCAPS_REPLACE |
4669                           WINED3DSTENCILCAPS_ZERO;
4670     if (gl_info->supported[EXT_STENCIL_WRAP])
4671     {
4672         caps->StencilCaps |= WINED3DSTENCILCAPS_DECR  |
4673                               WINED3DSTENCILCAPS_INCR;
4674     }
4675     if (gl_info->supported[EXT_STENCIL_TWO_SIDE] || gl_info->supported[ATI_SEPARATE_STENCIL])
4676     {
4677         caps->StencilCaps |= WINED3DSTENCILCAPS_TWOSIDED;
4678     }
4679
4680     caps->FVFCaps = WINED3DFVFCAPS_PSIZE | 0x0008; /* 8 texture coords */
4681
4682     caps->MaxUserClipPlanes = gl_info->limits.clipplanes;
4683     caps->MaxActiveLights = gl_info->limits.lights;
4684
4685     caps->MaxVertexBlendMatrices = gl_info->limits.blends;
4686     caps->MaxVertexBlendMatrixIndex   = 0;
4687
4688     caps->MaxAnisotropy = gl_info->limits.anisotropy;
4689     caps->MaxPointSize = gl_info->limits.pointsize_max;
4690
4691
4692     /* FIXME: Add D3DVTXPCAPS_TWEENING, D3DVTXPCAPS_TEXGEN_SPHEREMAP */
4693     caps->VertexProcessingCaps  = WINED3DVTXPCAPS_DIRECTIONALLIGHTS |
4694                                   WINED3DVTXPCAPS_MATERIALSOURCE7   |
4695                                   WINED3DVTXPCAPS_POSITIONALLIGHTS  |
4696                                   WINED3DVTXPCAPS_LOCALVIEWER       |
4697                                   WINED3DVTXPCAPS_VERTEXFOG         |
4698                                   WINED3DVTXPCAPS_TEXGEN;
4699
4700     caps->MaxPrimitiveCount   = 0xFFFFF; /* For now set 2^20-1 which is used by most >=Geforce3/Radeon8500 cards */
4701     caps->MaxVertexIndex      = 0xFFFFF;
4702     caps->MaxStreams          = MAX_STREAMS;
4703     caps->MaxStreamStride     = 1024;
4704
4705     /* d3d9.dll sets D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES here because StretchRects is implemented in d3d9 */
4706     caps->DevCaps2                          = WINED3DDEVCAPS2_STREAMOFFSET |
4707                                               WINED3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
4708     caps->MaxNpatchTessellationLevel        = 0;
4709     caps->MasterAdapterOrdinal              = 0;
4710     caps->AdapterOrdinalInGroup             = 0;
4711     caps->NumberOfAdaptersInGroup           = 1;
4712
4713     caps->NumSimultaneousRTs = gl_info->limits.buffers;
4714
4715     caps->StretchRectFilterCaps               = WINED3DPTFILTERCAPS_MINFPOINT  |
4716                                                 WINED3DPTFILTERCAPS_MAGFPOINT  |
4717                                                 WINED3DPTFILTERCAPS_MINFLINEAR |
4718                                                 WINED3DPTFILTERCAPS_MAGFLINEAR;
4719     caps->VertexTextureFilterCaps             = 0;
4720
4721     adapter->shader_backend->shader_get_caps(&adapter->gl_info, &shader_caps);
4722     adapter->fragment_pipe->get_caps(&adapter->gl_info, &fragment_caps);
4723
4724     /* Add shader misc caps. Only some of them belong to the shader parts of the pipeline */
4725     caps->PrimitiveMiscCaps |= fragment_caps.PrimitiveMiscCaps;
4726
4727     /* This takes care for disabling vertex shader or pixel shader caps while leaving the other one enabled.
4728      * Ignore shader model capabilities if disabled in config
4729      */
4730     if (vs_selected_mode == SHADER_NONE)
4731     {
4732         TRACE_(d3d_caps)("Vertex shader disabled in config, reporting version 0.0\n");
4733         caps->VertexShaderVersion          = 0;
4734         caps->MaxVertexShaderConst         = 0;
4735     }
4736     else
4737     {
4738         caps->VertexShaderVersion          = shader_caps.VertexShaderVersion;
4739         caps->MaxVertexShaderConst         = shader_caps.MaxVertexShaderConst;
4740     }
4741
4742     if (ps_selected_mode == SHADER_NONE)
4743     {
4744         TRACE_(d3d_caps)("Pixel shader disabled in config, reporting version 0.0\n");
4745         caps->PixelShaderVersion           = 0;
4746         caps->PixelShader1xMaxValue        = 0.0f;
4747     } else {
4748         caps->PixelShaderVersion           = shader_caps.PixelShaderVersion;
4749         caps->PixelShader1xMaxValue        = shader_caps.PixelShader1xMaxValue;
4750     }
4751
4752     caps->TextureOpCaps                    = fragment_caps.TextureOpCaps;
4753     caps->MaxTextureBlendStages            = fragment_caps.MaxTextureBlendStages;
4754     caps->MaxSimultaneousTextures          = fragment_caps.MaxSimultaneousTextures;
4755
4756     /* The following caps are shader specific, but they are things we cannot detect, or which
4757      * are the same among all shader models. So to avoid code duplication set the shader version
4758      * specific, but otherwise constant caps here
4759      */
4760     if (caps->VertexShaderVersion >= 3)
4761     {
4762         /* Where possible set the caps based on OpenGL extensions and if they
4763          * aren't set (in case of software rendering) use the VS 3.0 from
4764          * MSDN or else if there's OpenGL spec use a hardcoded value minimum
4765          * VS3.0 value. */
4766         caps->VS20Caps.caps = WINED3DVS20CAPS_PREDICATION;
4767         /* VS 3.0 requires MAX_DYNAMICFLOWCONTROLDEPTH (24) */
4768         caps->VS20Caps.dynamic_flow_control_depth = WINED3DVS20_MAX_DYNAMICFLOWCONTROLDEPTH;
4769         caps->VS20Caps.temp_count = max(32, adapter->gl_info.limits.arb_vs_temps);
4770         /* level of nesting in loops / if-statements; VS 3.0 requires MAX (4) */
4771         caps->VS20Caps.static_flow_control_depth = WINED3DVS20_MAX_STATICFLOWCONTROLDEPTH;
4772
4773         caps->MaxVShaderInstructionsExecuted    = 65535; /* VS 3.0 needs at least 65535, some cards even use 2^32-1 */
4774         caps->MaxVertexShader30InstructionSlots = max(512, adapter->gl_info.limits.arb_vs_instructions);
4775     }
4776     else if (caps->VertexShaderVersion == 2)
4777     {
4778         caps->VS20Caps.caps = 0;
4779         caps->VS20Caps.dynamic_flow_control_depth = WINED3DVS20_MIN_DYNAMICFLOWCONTROLDEPTH;
4780         caps->VS20Caps.temp_count = max(12, adapter->gl_info.limits.arb_vs_temps);
4781         caps->VS20Caps.static_flow_control_depth = 1;
4782
4783         caps->MaxVShaderInstructionsExecuted    = 65535;
4784         caps->MaxVertexShader30InstructionSlots = 0;
4785     }
4786     else
4787     { /* VS 1.x */
4788         caps->VS20Caps.caps = 0;
4789         caps->VS20Caps.dynamic_flow_control_depth = 0;
4790         caps->VS20Caps.temp_count = 0;
4791         caps->VS20Caps.static_flow_control_depth = 0;
4792
4793         caps->MaxVShaderInstructionsExecuted    = 0;
4794         caps->MaxVertexShader30InstructionSlots = 0;
4795     }
4796
4797     if (caps->PixelShaderVersion >= 3)
4798     {
4799         /* Where possible set the caps based on OpenGL extensions and if they
4800          * aren't set (in case of software rendering) use the PS 3.0 from
4801          * MSDN or else if there's OpenGL spec use a hardcoded value minimum
4802          * PS 3.0 value. */
4803
4804         /* Caps is more or less undocumented on MSDN but it appears to be
4805          * used for PS20Caps based on results from R9600/FX5900/Geforce6800
4806          * cards from Windows */
4807         caps->PS20Caps.caps = WINED3DPS20CAPS_ARBITRARYSWIZZLE |
4808                 WINED3DPS20CAPS_GRADIENTINSTRUCTIONS |
4809                 WINED3DPS20CAPS_PREDICATION          |
4810                 WINED3DPS20CAPS_NODEPENDENTREADLIMIT |
4811                 WINED3DPS20CAPS_NOTEXINSTRUCTIONLIMIT;
4812         /* PS 3.0 requires MAX_DYNAMICFLOWCONTROLDEPTH (24) */
4813         caps->PS20Caps.dynamic_flow_control_depth = WINED3DPS20_MAX_DYNAMICFLOWCONTROLDEPTH;
4814         caps->PS20Caps.temp_count = max(32, adapter->gl_info.limits.arb_ps_temps);
4815         /* PS 3.0 requires MAX_STATICFLOWCONTROLDEPTH (4) */
4816         caps->PS20Caps.static_flow_control_depth = WINED3DPS20_MAX_STATICFLOWCONTROLDEPTH;
4817         /* PS 3.0 requires MAX_NUMINSTRUCTIONSLOTS (512) */
4818         caps->PS20Caps.instruction_slot_count = WINED3DPS20_MAX_NUMINSTRUCTIONSLOTS;
4819
4820         caps->MaxPShaderInstructionsExecuted = 65535;
4821         caps->MaxPixelShader30InstructionSlots = max(WINED3DMIN30SHADERINSTRUCTIONS,
4822                 adapter->gl_info.limits.arb_ps_instructions);
4823     }
4824     else if(caps->PixelShaderVersion == 2)
4825     {
4826         /* Below we assume PS2.0 specs, not extended 2.0a(GeforceFX)/2.0b(Radeon R3xx) ones */
4827         caps->PS20Caps.caps = 0;
4828         caps->PS20Caps.dynamic_flow_control_depth = 0; /* WINED3DVS20_MIN_DYNAMICFLOWCONTROLDEPTH = 0 */
4829         caps->PS20Caps.temp_count = max(12, adapter->gl_info.limits.arb_ps_temps);
4830         caps->PS20Caps.static_flow_control_depth = WINED3DPS20_MIN_STATICFLOWCONTROLDEPTH; /* Minimum: 1 */
4831         /* Minimum number (64 ALU + 32 Texture), a GeforceFX uses 512 */
4832         caps->PS20Caps.instruction_slot_count = WINED3DPS20_MIN_NUMINSTRUCTIONSLOTS;
4833
4834         caps->MaxPShaderInstructionsExecuted    = 512; /* Minimum value, a GeforceFX uses 1024 */
4835         caps->MaxPixelShader30InstructionSlots  = 0;
4836     }
4837     else /* PS 1.x */
4838     {
4839         caps->PS20Caps.caps = 0;
4840         caps->PS20Caps.dynamic_flow_control_depth = 0;
4841         caps->PS20Caps.temp_count = 0;
4842         caps->PS20Caps.static_flow_control_depth = 0;
4843         caps->PS20Caps.instruction_slot_count = 0;
4844
4845         caps->MaxPShaderInstructionsExecuted    = 0;
4846         caps->MaxPixelShader30InstructionSlots  = 0;
4847     }
4848
4849     if (caps->VertexShaderVersion >= 2)
4850     {
4851         /* OpenGL supports all the formats below, perhaps not always
4852          * without conversion, but it supports them.
4853          * Further GLSL doesn't seem to have an official unsigned type so
4854          * don't advertise it yet as I'm not sure how we handle it.
4855          * We might need to add some clamping in the shader engine to
4856          * support it.
4857          * TODO: WINED3DDTCAPS_USHORT2N, WINED3DDTCAPS_USHORT4N, WINED3DDTCAPS_UDEC3, WINED3DDTCAPS_DEC3N */
4858         caps->DeclTypes = WINED3DDTCAPS_UBYTE4    |
4859                           WINED3DDTCAPS_UBYTE4N   |
4860                           WINED3DDTCAPS_SHORT2N   |
4861                           WINED3DDTCAPS_SHORT4N;
4862         if (gl_info->supported[ARB_HALF_FLOAT_VERTEX])
4863         {
4864             caps->DeclTypes |= WINED3DDTCAPS_FLOAT16_2 |
4865                                WINED3DDTCAPS_FLOAT16_4;
4866         }
4867     }
4868     else
4869     {
4870         caps->DeclTypes = 0;
4871     }
4872
4873     /* Set DirectDraw helper Caps */
4874     ckey_caps =                         WINEDDCKEYCAPS_DESTBLT              |
4875                                         WINEDDCKEYCAPS_SRCBLT;
4876     fx_caps =                           WINEDDFXCAPS_BLTALPHA               |
4877                                         WINEDDFXCAPS_BLTMIRRORLEFTRIGHT     |
4878                                         WINEDDFXCAPS_BLTMIRRORUPDOWN        |
4879                                         WINEDDFXCAPS_BLTROTATION90          |
4880                                         WINEDDFXCAPS_BLTSHRINKX             |
4881                                         WINEDDFXCAPS_BLTSHRINKXN            |
4882                                         WINEDDFXCAPS_BLTSHRINKY             |
4883                                         WINEDDFXCAPS_BLTSHRINKXN            |
4884                                         WINEDDFXCAPS_BLTSTRETCHX            |
4885                                         WINEDDFXCAPS_BLTSTRETCHXN           |
4886                                         WINEDDFXCAPS_BLTSTRETCHY            |
4887                                         WINEDDFXCAPS_BLTSTRETCHYN;
4888     blit_caps =                         WINEDDCAPS_BLT                      |
4889                                         WINEDDCAPS_BLTCOLORFILL             |
4890                                         WINEDDCAPS_BLTDEPTHFILL             |
4891                                         WINEDDCAPS_BLTSTRETCH               |
4892                                         WINEDDCAPS_CANBLTSYSMEM             |
4893                                         WINEDDCAPS_CANCLIP                  |
4894                                         WINEDDCAPS_CANCLIPSTRETCHED         |
4895                                         WINEDDCAPS_COLORKEY                 |
4896                                         WINEDDCAPS_COLORKEYHWASSIST         |
4897                                         WINEDDCAPS_ALIGNBOUNDARYSRC;
4898     pal_caps =                          WINEDDPCAPS_8BIT                    |
4899                                         WINEDDPCAPS_PRIMARYSURFACE;
4900
4901     /* Fill the ddraw caps structure */
4902     caps->DirectDrawCaps.Caps =         WINEDDCAPS_GDI                      |
4903                                         WINEDDCAPS_PALETTE                  |
4904                                         blit_caps;
4905     caps->DirectDrawCaps.Caps2 =        WINEDDCAPS2_CERTIFIED                |
4906                                         WINEDDCAPS2_NOPAGELOCKREQUIRED       |
4907                                         WINEDDCAPS2_PRIMARYGAMMA             |
4908                                         WINEDDCAPS2_WIDESURFACES             |
4909                                         WINEDDCAPS2_CANRENDERWINDOWED;
4910     caps->DirectDrawCaps.CKeyCaps =     ckey_caps;
4911     caps->DirectDrawCaps.FXCaps =       fx_caps;
4912     caps->DirectDrawCaps.PalCaps =      pal_caps;
4913     caps->DirectDrawCaps.SVBCaps =      blit_caps;
4914     caps->DirectDrawCaps.SVBCKeyCaps =  ckey_caps;
4915     caps->DirectDrawCaps.SVBFXCaps =    fx_caps;
4916     caps->DirectDrawCaps.VSBCaps =      blit_caps;
4917     caps->DirectDrawCaps.VSBCKeyCaps =  ckey_caps;
4918     caps->DirectDrawCaps.VSBFXCaps =    fx_caps;
4919     caps->DirectDrawCaps.SSBCaps =      blit_caps;
4920     caps->DirectDrawCaps.SSBCKeyCaps =  ckey_caps;
4921     caps->DirectDrawCaps.SSBFXCaps =    fx_caps;
4922
4923     caps->DirectDrawCaps.ddsCaps =      WINEDDSCAPS_ALPHA                   |
4924                                         WINEDDSCAPS_BACKBUFFER              |
4925                                         WINEDDSCAPS_FLIP                    |
4926                                         WINEDDSCAPS_FRONTBUFFER             |
4927                                         WINEDDSCAPS_OFFSCREENPLAIN          |
4928                                         WINEDDSCAPS_PALETTE                 |
4929                                         WINEDDSCAPS_PRIMARYSURFACE          |
4930                                         WINEDDSCAPS_SYSTEMMEMORY            |
4931                                         WINEDDSCAPS_VIDEOMEMORY             |
4932                                         WINEDDSCAPS_VISIBLE;
4933     caps->DirectDrawCaps.StrideAlign = DDRAW_PITCH_ALIGNMENT;
4934
4935     /* Set D3D caps if OpenGL is available. */
4936     if (adapter->opengl)
4937     {
4938         caps->DirectDrawCaps.ddsCaps |= WINEDDSCAPS_3DDEVICE                |
4939                                         WINEDDSCAPS_MIPMAP                  |
4940                                         WINEDDSCAPS_TEXTURE                 |
4941                                         WINEDDSCAPS_ZBUFFER;
4942         caps->DirectDrawCaps.Caps |=    WINEDDCAPS_3D;
4943     }
4944
4945     return WINED3D_OK;
4946 }
4947
4948 HRESULT CDECL wined3d_device_create(struct wined3d *wined3d, UINT adapter_idx, WINED3DDEVTYPE device_type,
4949         HWND focus_window, DWORD flags, BYTE surface_alignment, struct wined3d_device_parent *device_parent,
4950         struct wined3d_device **device)
4951 {
4952     struct wined3d_device *object;
4953     HRESULT hr;
4954
4955     TRACE("wined3d %p, adapter_idx %u, device_type %#x, focus_window %p, flags %#x, device_parent %p, device %p.\n",
4956             wined3d, adapter_idx, device_type, focus_window, flags, device_parent, device);
4957
4958     /* Validate the adapter number. If no adapters are available(no GL), ignore the adapter
4959      * number and create a device without a 3D adapter for 2D only operation. */
4960     if (wined3d->adapter_count && adapter_idx >= wined3d->adapter_count)
4961         return WINED3DERR_INVALIDCALL;
4962
4963     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4964     if (!object)
4965     {
4966         ERR("Failed to allocate device memory.\n");
4967         return E_OUTOFMEMORY;
4968     }
4969
4970     hr = device_init(object, wined3d, adapter_idx, device_type,
4971             focus_window, flags, surface_alignment, device_parent);
4972     if (FAILED(hr))
4973     {
4974         WARN("Failed to initialize device, hr %#x.\n", hr);
4975         HeapFree(GetProcessHeap(), 0, object);
4976         return hr;
4977     }
4978
4979     TRACE("Created device %p.\n", object);
4980     *device = object;
4981
4982     device_parent->ops->wined3d_device_created(device_parent, *device);
4983
4984     return WINED3D_OK;
4985 }
4986
4987 void * CDECL wined3d_get_parent(const struct wined3d *wined3d)
4988 {
4989     TRACE("wined3d %p.\n", wined3d);
4990
4991     return wined3d->parent;
4992 }
4993
4994 static void WINE_GLAPI invalid_func(const void *data)
4995 {
4996     ERR("Invalid vertex attribute function called\n");
4997     DebugBreak();
4998 }
4999
5000 static void WINE_GLAPI invalid_texcoord_func(GLenum unit, const void *data)
5001 {
5002     ERR("Invalid texcoord function called\n");
5003     DebugBreak();
5004 }
5005
5006 /* Helper functions for providing vertex data to opengl. The arrays are initialized based on
5007  * the extension detection and are used in drawStridedSlow
5008  */
5009 static void WINE_GLAPI position_d3dcolor(const void *data)
5010 {
5011     DWORD pos = *((const DWORD *)data);
5012
5013     FIXME("Add a test for fixed function position from d3dcolor type\n");
5014     glVertex4s(D3DCOLOR_B_R(pos),
5015                D3DCOLOR_B_G(pos),
5016                D3DCOLOR_B_B(pos),
5017                D3DCOLOR_B_A(pos));
5018 }
5019
5020 static void WINE_GLAPI position_float4(const void *data)
5021 {
5022     const GLfloat *pos = data;
5023
5024     if (pos[3] != 0.0f && pos[3] != 1.0f)
5025     {
5026         float w = 1.0f / pos[3];
5027
5028         glVertex4f(pos[0] * w, pos[1] * w, pos[2] * w, w);
5029     }
5030     else
5031     {
5032         glVertex3fv(pos);
5033     }
5034 }
5035
5036 static void WINE_GLAPI diffuse_d3dcolor(const void *data)
5037 {
5038     DWORD diffuseColor = *((const DWORD *)data);
5039
5040     glColor4ub(D3DCOLOR_B_R(diffuseColor),
5041                D3DCOLOR_B_G(diffuseColor),
5042                D3DCOLOR_B_B(diffuseColor),
5043                D3DCOLOR_B_A(diffuseColor));
5044 }
5045
5046 static void WINE_GLAPI specular_d3dcolor(const void *data)
5047 {
5048     DWORD specularColor = *((const DWORD *)data);
5049     GLbyte d[] = {D3DCOLOR_B_R(specularColor),
5050             D3DCOLOR_B_G(specularColor),
5051             D3DCOLOR_B_B(specularColor)};
5052
5053     specular_func_3ubv(d);
5054 }
5055
5056 static void WINE_GLAPI warn_no_specular_func(const void *data)
5057 {
5058     WARN("GL_EXT_secondary_color not supported\n");
5059 }
5060
5061 static void fillGLAttribFuncs(const struct wined3d_gl_info *gl_info)
5062 {
5063     position_funcs[WINED3D_FFP_EMIT_FLOAT1]      = invalid_func;
5064     position_funcs[WINED3D_FFP_EMIT_FLOAT2]      = invalid_func;
5065     position_funcs[WINED3D_FFP_EMIT_FLOAT3]      = (glAttribFunc)glVertex3fv;
5066     position_funcs[WINED3D_FFP_EMIT_FLOAT4]      = position_float4;
5067     position_funcs[WINED3D_FFP_EMIT_D3DCOLOR]    = position_d3dcolor;
5068     position_funcs[WINED3D_FFP_EMIT_UBYTE4]      = invalid_func;
5069     position_funcs[WINED3D_FFP_EMIT_SHORT2]      = invalid_func;
5070     position_funcs[WINED3D_FFP_EMIT_SHORT4]      = (glAttribFunc)glVertex2sv;
5071     position_funcs[WINED3D_FFP_EMIT_UBYTE4N]     = invalid_func;
5072     position_funcs[WINED3D_FFP_EMIT_SHORT2N]     = invalid_func;
5073     position_funcs[WINED3D_FFP_EMIT_SHORT4N]     = invalid_func;
5074     position_funcs[WINED3D_FFP_EMIT_USHORT2N]    = invalid_func;
5075     position_funcs[WINED3D_FFP_EMIT_USHORT4N]    = invalid_func;
5076     position_funcs[WINED3D_FFP_EMIT_UDEC3]       = invalid_func;
5077     position_funcs[WINED3D_FFP_EMIT_DEC3N]       = invalid_func;
5078     position_funcs[WINED3D_FFP_EMIT_FLOAT16_2]   = invalid_func;
5079     position_funcs[WINED3D_FFP_EMIT_FLOAT16_4]   = invalid_func;
5080
5081     diffuse_funcs[WINED3D_FFP_EMIT_FLOAT1]       = invalid_func;
5082     diffuse_funcs[WINED3D_FFP_EMIT_FLOAT2]       = invalid_func;
5083     diffuse_funcs[WINED3D_FFP_EMIT_FLOAT3]       = (glAttribFunc)glColor3fv;
5084     diffuse_funcs[WINED3D_FFP_EMIT_FLOAT4]       = (glAttribFunc)glColor4fv;
5085     diffuse_funcs[WINED3D_FFP_EMIT_D3DCOLOR]     = diffuse_d3dcolor;
5086     diffuse_funcs[WINED3D_FFP_EMIT_UBYTE4]       = invalid_func;
5087     diffuse_funcs[WINED3D_FFP_EMIT_SHORT2]       = invalid_func;
5088     diffuse_funcs[WINED3D_FFP_EMIT_SHORT4]       = invalid_func;
5089     diffuse_funcs[WINED3D_FFP_EMIT_UBYTE4N]      = (glAttribFunc)glColor4ubv;
5090     diffuse_funcs[WINED3D_FFP_EMIT_SHORT2N]      = invalid_func;
5091     diffuse_funcs[WINED3D_FFP_EMIT_SHORT4N]      = (glAttribFunc)glColor4sv;
5092     diffuse_funcs[WINED3D_FFP_EMIT_USHORT2N]     = invalid_func;
5093     diffuse_funcs[WINED3D_FFP_EMIT_USHORT4N]     = (glAttribFunc)glColor4usv;
5094     diffuse_funcs[WINED3D_FFP_EMIT_UDEC3]        = invalid_func;
5095     diffuse_funcs[WINED3D_FFP_EMIT_DEC3N]        = invalid_func;
5096     diffuse_funcs[WINED3D_FFP_EMIT_FLOAT16_2]    = invalid_func;
5097     diffuse_funcs[WINED3D_FFP_EMIT_FLOAT16_4]    = invalid_func;
5098
5099     /* No 4 component entry points here */
5100     specular_funcs[WINED3D_FFP_EMIT_FLOAT1]      = invalid_func;
5101     specular_funcs[WINED3D_FFP_EMIT_FLOAT2]      = invalid_func;
5102     if (gl_info->supported[EXT_SECONDARY_COLOR])
5103     {
5104         specular_funcs[WINED3D_FFP_EMIT_FLOAT3]  = (glAttribFunc)GL_EXTCALL(glSecondaryColor3fvEXT);
5105     }
5106     else
5107     {
5108         specular_funcs[WINED3D_FFP_EMIT_FLOAT3]  = warn_no_specular_func;
5109     }
5110     specular_funcs[WINED3D_FFP_EMIT_FLOAT4]      = invalid_func;
5111     if (gl_info->supported[EXT_SECONDARY_COLOR])
5112     {
5113         specular_func_3ubv = (glAttribFunc)GL_EXTCALL(glSecondaryColor3ubvEXT);
5114         specular_funcs[WINED3D_FFP_EMIT_D3DCOLOR] = specular_d3dcolor;
5115     }
5116     else
5117     {
5118         specular_funcs[WINED3D_FFP_EMIT_D3DCOLOR] = warn_no_specular_func;
5119     }
5120     specular_funcs[WINED3D_FFP_EMIT_UBYTE4]      = invalid_func;
5121     specular_funcs[WINED3D_FFP_EMIT_SHORT2]      = invalid_func;
5122     specular_funcs[WINED3D_FFP_EMIT_SHORT4]      = invalid_func;
5123     specular_funcs[WINED3D_FFP_EMIT_UBYTE4N]     = invalid_func;
5124     specular_funcs[WINED3D_FFP_EMIT_SHORT2N]     = invalid_func;
5125     specular_funcs[WINED3D_FFP_EMIT_SHORT4N]     = invalid_func;
5126     specular_funcs[WINED3D_FFP_EMIT_USHORT2N]    = invalid_func;
5127     specular_funcs[WINED3D_FFP_EMIT_USHORT4N]    = invalid_func;
5128     specular_funcs[WINED3D_FFP_EMIT_UDEC3]       = invalid_func;
5129     specular_funcs[WINED3D_FFP_EMIT_DEC3N]       = invalid_func;
5130     specular_funcs[WINED3D_FFP_EMIT_FLOAT16_2]   = invalid_func;
5131     specular_funcs[WINED3D_FFP_EMIT_FLOAT16_4]   = invalid_func;
5132
5133     /* Only 3 component entry points here. Test how others behave. Float4 normals are used
5134      * by one of our tests, trying to pass it to the pixel shader, which fails on Windows.
5135      */
5136     normal_funcs[WINED3D_FFP_EMIT_FLOAT1]         = invalid_func;
5137     normal_funcs[WINED3D_FFP_EMIT_FLOAT2]         = invalid_func;
5138     normal_funcs[WINED3D_FFP_EMIT_FLOAT3]         = (glAttribFunc)glNormal3fv;
5139     normal_funcs[WINED3D_FFP_EMIT_FLOAT4]         = (glAttribFunc)glNormal3fv; /* Just ignore the 4th value */
5140     normal_funcs[WINED3D_FFP_EMIT_D3DCOLOR]       = invalid_func;
5141     normal_funcs[WINED3D_FFP_EMIT_UBYTE4]         = invalid_func;
5142     normal_funcs[WINED3D_FFP_EMIT_SHORT2]         = invalid_func;
5143     normal_funcs[WINED3D_FFP_EMIT_SHORT4]         = invalid_func;
5144     normal_funcs[WINED3D_FFP_EMIT_UBYTE4N]        = invalid_func;
5145     normal_funcs[WINED3D_FFP_EMIT_SHORT2N]        = invalid_func;
5146     normal_funcs[WINED3D_FFP_EMIT_SHORT4N]        = invalid_func;
5147     normal_funcs[WINED3D_FFP_EMIT_USHORT2N]       = invalid_func;
5148     normal_funcs[WINED3D_FFP_EMIT_USHORT4N]       = invalid_func;
5149     normal_funcs[WINED3D_FFP_EMIT_UDEC3]          = invalid_func;
5150     normal_funcs[WINED3D_FFP_EMIT_DEC3N]          = invalid_func;
5151     normal_funcs[WINED3D_FFP_EMIT_FLOAT16_2]      = invalid_func;
5152     normal_funcs[WINED3D_FFP_EMIT_FLOAT16_4]      = invalid_func;
5153
5154     multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT1]    = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord1fvARB);
5155     multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT2]    = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord2fvARB);
5156     multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT3]    = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord3fvARB);
5157     multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT4]    = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord4fvARB);
5158     multi_texcoord_funcs[WINED3D_FFP_EMIT_D3DCOLOR]  = invalid_texcoord_func;
5159     multi_texcoord_funcs[WINED3D_FFP_EMIT_UBYTE4]    = invalid_texcoord_func;
5160     multi_texcoord_funcs[WINED3D_FFP_EMIT_SHORT2]    = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord2svARB);
5161     multi_texcoord_funcs[WINED3D_FFP_EMIT_SHORT4]    = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord4svARB);
5162     multi_texcoord_funcs[WINED3D_FFP_EMIT_UBYTE4N]   = invalid_texcoord_func;
5163     multi_texcoord_funcs[WINED3D_FFP_EMIT_SHORT2N]   = invalid_texcoord_func;
5164     multi_texcoord_funcs[WINED3D_FFP_EMIT_SHORT4N]   = invalid_texcoord_func;
5165     multi_texcoord_funcs[WINED3D_FFP_EMIT_USHORT2N]  = invalid_texcoord_func;
5166     multi_texcoord_funcs[WINED3D_FFP_EMIT_USHORT4N]  = invalid_texcoord_func;
5167     multi_texcoord_funcs[WINED3D_FFP_EMIT_UDEC3]     = invalid_texcoord_func;
5168     multi_texcoord_funcs[WINED3D_FFP_EMIT_DEC3N]     = invalid_texcoord_func;
5169     if (gl_info->supported[NV_HALF_FLOAT])
5170     {
5171         /* Not supported by ARB_HALF_FLOAT_VERTEX, so check for NV_HALF_FLOAT */
5172         multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT16_2] = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord2hvNV);
5173         multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT16_4] = (glMultiTexCoordFunc)GL_EXTCALL(glMultiTexCoord4hvNV);
5174     } else {
5175         multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT16_2] = invalid_texcoord_func;
5176         multi_texcoord_funcs[WINED3D_FFP_EMIT_FLOAT16_4] = invalid_texcoord_func;
5177     }
5178 }
5179
5180 /* Do not call while under the GL lock. */
5181 static BOOL InitAdapters(struct wined3d *wined3d)
5182 {
5183     static HMODULE mod_gl;
5184     BOOL ret;
5185     int ps_selected_mode, vs_selected_mode;
5186
5187     /* No need to hold any lock. The calling library makes sure only one thread calls
5188      * wined3d simultaneously
5189      */
5190
5191     TRACE("Initializing adapters\n");
5192
5193     if(!mod_gl) {
5194 #ifdef USE_WIN32_OPENGL
5195 #define USE_GL_FUNC(pfn) pfn = (void*)GetProcAddress(mod_gl, #pfn);
5196         mod_gl = LoadLibraryA("opengl32.dll");
5197         if(!mod_gl) {
5198             ERR("Can't load opengl32.dll!\n");
5199             goto nogl_adapter;
5200         }
5201 #else
5202 #define USE_GL_FUNC(pfn) pfn = (void*)pwglGetProcAddress(#pfn);
5203         /* To bypass the opengl32 thunks load wglGetProcAddress from gdi32 (glXGetProcAddress wrapper) instead of opengl32's */
5204         mod_gl = GetModuleHandleA("gdi32.dll");
5205 #endif
5206     }
5207
5208 /* Load WGL core functions from opengl32.dll */
5209 #define USE_WGL_FUNC(pfn) p##pfn = (void*)GetProcAddress(mod_gl, #pfn);
5210     WGL_FUNCS_GEN;
5211 #undef USE_WGL_FUNC
5212
5213     if(!pwglGetProcAddress) {
5214         ERR("Unable to load wglGetProcAddress!\n");
5215         goto nogl_adapter;
5216     }
5217
5218 /* Dynamically load all GL core functions */
5219     GL_FUNCS_GEN;
5220 #undef USE_GL_FUNC
5221
5222     /* Load glFinish and glFlush from opengl32.dll even if we're not using WIN32 opengl
5223      * otherwise because we have to use winex11.drv's override
5224      */
5225 #ifdef USE_WIN32_OPENGL
5226     wglFinish = (void*)GetProcAddress(mod_gl, "glFinish");
5227     wglFlush = (void*)GetProcAddress(mod_gl, "glFlush");
5228 #else
5229     wglFinish = (void*)pwglGetProcAddress("wglFinish");
5230     wglFlush = (void*)pwglGetProcAddress("wglFlush");
5231 #endif
5232
5233     glEnableWINE = glEnable;
5234     glDisableWINE = glDisable;
5235
5236     /* For now only one default adapter */
5237     {
5238         struct wined3d_adapter *adapter = &wined3d->adapters[0];
5239         const struct wined3d_gl_info *gl_info = &adapter->gl_info;
5240         struct wined3d_fake_gl_ctx fake_gl_ctx = {0};
5241         struct wined3d_pixel_format *cfgs;
5242         int iPixelFormat;
5243         int res;
5244         DISPLAY_DEVICEW DisplayDevice;
5245         HDC hdc;
5246
5247         TRACE("Initializing default adapter\n");
5248         adapter->ordinal = 0;
5249         adapter->monitorPoint.x = -1;
5250         adapter->monitorPoint.y = -1;
5251
5252         if (!AllocateLocallyUniqueId(&adapter->luid))
5253         {
5254             DWORD err = GetLastError();
5255             ERR("Failed to set adapter LUID (%#x).\n", err);
5256             goto nogl_adapter;
5257         }
5258         TRACE("Allocated LUID %08x:%08x for adapter.\n",
5259                 adapter->luid.HighPart, adapter->luid.LowPart);
5260
5261         if (!WineD3D_CreateFakeGLContext(&fake_gl_ctx))
5262         {
5263             ERR("Failed to get a gl context for default adapter\n");
5264             goto nogl_adapter;
5265         }
5266
5267         ret = wined3d_adapter_init_gl_caps(adapter);
5268         if(!ret) {
5269             ERR("Failed to initialize gl caps for default adapter\n");
5270             WineD3D_ReleaseFakeGLContext(&fake_gl_ctx);
5271             goto nogl_adapter;
5272         }
5273         ret = initPixelFormats(&adapter->gl_info, adapter->driver_info.vendor);
5274         if(!ret) {
5275             ERR("Failed to init gl formats\n");
5276             WineD3D_ReleaseFakeGLContext(&fake_gl_ctx);
5277             goto nogl_adapter;
5278         }
5279
5280         hdc = fake_gl_ctx.dc;
5281
5282         adapter->TextureRam = adapter->driver_info.vidmem;
5283         adapter->UsedTextureRam = 0;
5284         TRACE("Emulating %dMB of texture ram\n", adapter->TextureRam/(1024*1024));
5285
5286         /* Initialize the Adapter's DeviceName which is required for ChangeDisplaySettings and friends */
5287         DisplayDevice.cb = sizeof(DisplayDevice);
5288         EnumDisplayDevicesW(NULL, 0 /* Adapter 0 = iDevNum 0 */, &DisplayDevice, 0);
5289         TRACE("DeviceName: %s\n", debugstr_w(DisplayDevice.DeviceName));
5290         strcpyW(adapter->DeviceName, DisplayDevice.DeviceName);
5291
5292         if (gl_info->supported[WGL_ARB_PIXEL_FORMAT])
5293         {
5294             GLint cfg_count;
5295             int attribute;
5296             int attribs[11];
5297             int values[11];
5298             int nAttribs = 0;
5299
5300             attribute = WGL_NUMBER_PIXEL_FORMATS_ARB;
5301             GL_EXTCALL(wglGetPixelFormatAttribivARB(hdc, 0, 0, 1, &attribute, &cfg_count));
5302             adapter->cfg_count = cfg_count;
5303
5304             adapter->cfgs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, adapter->cfg_count * sizeof(*adapter->cfgs));
5305             cfgs = adapter->cfgs;
5306             attribs[nAttribs++] = WGL_RED_BITS_ARB;
5307             attribs[nAttribs++] = WGL_GREEN_BITS_ARB;
5308             attribs[nAttribs++] = WGL_BLUE_BITS_ARB;
5309             attribs[nAttribs++] = WGL_ALPHA_BITS_ARB;
5310             attribs[nAttribs++] = WGL_COLOR_BITS_ARB;
5311             attribs[nAttribs++] = WGL_DEPTH_BITS_ARB;
5312             attribs[nAttribs++] = WGL_STENCIL_BITS_ARB;
5313             attribs[nAttribs++] = WGL_DRAW_TO_WINDOW_ARB;
5314             attribs[nAttribs++] = WGL_PIXEL_TYPE_ARB;
5315             attribs[nAttribs++] = WGL_DOUBLE_BUFFER_ARB;
5316             attribs[nAttribs++] = WGL_AUX_BUFFERS_ARB;
5317
5318             for (iPixelFormat=1; iPixelFormat <= adapter->cfg_count; ++iPixelFormat)
5319             {
5320                 res = GL_EXTCALL(wglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0, nAttribs, attribs, values));
5321
5322                 if(!res)
5323                     continue;
5324
5325                 /* Cache the pixel format */
5326                 cfgs->iPixelFormat = iPixelFormat;
5327                 cfgs->redSize = values[0];
5328                 cfgs->greenSize = values[1];
5329                 cfgs->blueSize = values[2];
5330                 cfgs->alphaSize = values[3];
5331                 cfgs->colorSize = values[4];
5332                 cfgs->depthSize = values[5];
5333                 cfgs->stencilSize = values[6];
5334                 cfgs->windowDrawable = values[7];
5335                 cfgs->iPixelType = values[8];
5336                 cfgs->doubleBuffer = values[9];
5337                 cfgs->auxBuffers = values[10];
5338
5339                 cfgs->numSamples = 0;
5340                 /* Check multisample support */
5341                 if (gl_info->supported[ARB_MULTISAMPLE])
5342                 {
5343                     int attrib[2] = {WGL_SAMPLE_BUFFERS_ARB, WGL_SAMPLES_ARB};
5344                     int value[2];
5345                     if(GL_EXTCALL(wglGetPixelFormatAttribivARB(hdc, iPixelFormat, 0, 2, attrib, value))) {
5346                         /* value[0] = WGL_SAMPLE_BUFFERS_ARB which tells whether multisampling is supported.
5347                         * value[1] = number of multi sample buffers*/
5348                         if(value[0])
5349                             cfgs->numSamples = value[1];
5350                     }
5351                 }
5352
5353                 TRACE("iPixelFormat=%d, iPixelType=%#x, doubleBuffer=%d, RGBA=%d/%d/%d/%d, "
5354                         "depth=%d, stencil=%d, samples=%d, windowDrawable=%d\n",
5355                         cfgs->iPixelFormat, cfgs->iPixelType, cfgs->doubleBuffer,
5356                         cfgs->redSize, cfgs->greenSize, cfgs->blueSize, cfgs->alphaSize,
5357                         cfgs->depthSize, cfgs->stencilSize, cfgs->numSamples, cfgs->windowDrawable);
5358                 cfgs++;
5359             }
5360         }
5361         else
5362         {
5363             int nCfgs = DescribePixelFormat(hdc, 0, 0, 0);
5364             adapter->cfgs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nCfgs * sizeof(*adapter->cfgs));
5365             adapter->cfg_count = 0; /* We won't accept all formats e.g. software accelerated ones will be skipped */
5366
5367             cfgs = adapter->cfgs;
5368             for(iPixelFormat=1; iPixelFormat<=nCfgs; iPixelFormat++)
5369             {
5370                 PIXELFORMATDESCRIPTOR ppfd;
5371
5372                 res = DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &ppfd);
5373                 if(!res)
5374                     continue;
5375
5376                 /* We only want HW acceleration using an OpenGL ICD driver.
5377                  * PFD_GENERIC_FORMAT = slow opengl 1.1 gdi software rendering
5378                  * PFD_GENERIC_ACCELERATED = partial hw acceleration using a MCD driver (e.g. 3dfx minigl)
5379                  */
5380                 if(ppfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED))
5381                 {
5382                     TRACE("Skipping iPixelFormat=%d because it isn't ICD accelerated\n", iPixelFormat);
5383                     continue;
5384                 }
5385
5386                 cfgs->iPixelFormat = iPixelFormat;
5387                 cfgs->redSize = ppfd.cRedBits;
5388                 cfgs->greenSize = ppfd.cGreenBits;
5389                 cfgs->blueSize = ppfd.cBlueBits;
5390                 cfgs->alphaSize = ppfd.cAlphaBits;
5391                 cfgs->colorSize = ppfd.cColorBits;
5392                 cfgs->depthSize = ppfd.cDepthBits;
5393                 cfgs->stencilSize = ppfd.cStencilBits;
5394                 cfgs->windowDrawable = (ppfd.dwFlags & PFD_DRAW_TO_WINDOW) ? 1 : 0;
5395                 cfgs->iPixelType = (ppfd.iPixelType == PFD_TYPE_RGBA) ? WGL_TYPE_RGBA_ARB : WGL_TYPE_COLORINDEX_ARB;
5396                 cfgs->doubleBuffer = (ppfd.dwFlags & PFD_DOUBLEBUFFER) ? 1 : 0;
5397                 cfgs->auxBuffers = ppfd.cAuxBuffers;
5398                 cfgs->numSamples = 0;
5399
5400                 TRACE("iPixelFormat=%d, iPixelType=%#x, doubleBuffer=%d, RGBA=%d/%d/%d/%d, "
5401                         "depth=%d, stencil=%d, windowDrawable=%d\n",
5402                         cfgs->iPixelFormat, cfgs->iPixelType, cfgs->doubleBuffer,
5403                         cfgs->redSize, cfgs->greenSize, cfgs->blueSize, cfgs->alphaSize,
5404                         cfgs->depthSize, cfgs->stencilSize, cfgs->windowDrawable);
5405                 cfgs++;
5406                 adapter->cfg_count++;
5407             }
5408
5409             /* We haven't found any suitable formats. This should only happen
5410              * in case of GDI software rendering, which is pretty useless
5411              * anyway. */
5412             if (!adapter->cfg_count)
5413             {
5414                 ERR("Disabling Direct3D because no hardware accelerated pixel formats have been found!\n");
5415
5416                 WineD3D_ReleaseFakeGLContext(&fake_gl_ctx);
5417                 HeapFree(GetProcessHeap(), 0, adapter->cfgs);
5418                 goto nogl_adapter;
5419             }
5420         }
5421
5422         WineD3D_ReleaseFakeGLContext(&fake_gl_ctx);
5423
5424         select_shader_mode(&adapter->gl_info, &ps_selected_mode, &vs_selected_mode);
5425         fillGLAttribFuncs(&adapter->gl_info);
5426         adapter->opengl = TRUE;
5427     }
5428     wined3d->adapter_count = 1;
5429     TRACE("%u adapters successfully initialized.\n", wined3d->adapter_count);
5430
5431     return TRUE;
5432
5433 nogl_adapter:
5434     /* Initialize an adapter for ddraw-only memory counting */
5435     memset(wined3d->adapters, 0, sizeof(wined3d->adapters));
5436     wined3d->adapters[0].ordinal = 0;
5437     wined3d->adapters[0].opengl = FALSE;
5438     wined3d->adapters[0].monitorPoint.x = -1;
5439     wined3d->adapters[0].monitorPoint.y = -1;
5440
5441     wined3d->adapters[0].driver_info.name = "Display";
5442     wined3d->adapters[0].driver_info.description = "WineD3D DirectDraw Emulation";
5443     if (wined3d_settings.emulated_textureram)
5444         wined3d->adapters[0].TextureRam = wined3d_settings.emulated_textureram;
5445     else
5446         wined3d->adapters[0].TextureRam = 8 * 1024 * 1024; /* This is plenty for a DDraw-only card */
5447
5448     initPixelFormatsNoGL(&wined3d->adapters[0].gl_info);
5449
5450     wined3d->adapter_count = 1;
5451     return FALSE;
5452 }
5453
5454 static void STDMETHODCALLTYPE wined3d_null_wined3d_object_destroyed(void *parent) {}
5455
5456 const struct wined3d_parent_ops wined3d_null_parent_ops =
5457 {
5458     wined3d_null_wined3d_object_destroyed,
5459 };
5460
5461 /* Do not call while under the GL lock. */
5462 HRESULT wined3d_init(struct wined3d *wined3d, UINT version, DWORD flags, void *parent)
5463 {
5464     wined3d->dxVersion = version;
5465     wined3d->ref = 1;
5466     wined3d->parent = parent;
5467     wined3d->flags = flags;
5468
5469     if (!InitAdapters(wined3d))
5470     {
5471         WARN("Failed to initialize adapters.\n");
5472         if (version > 7)
5473         {
5474             MESSAGE("Direct3D%u is not available without OpenGL.\n", version);
5475             return E_FAIL;
5476         }
5477     }
5478
5479     return WINED3D_OK;
5480 }