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