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