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