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