~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/wined3d/directx.c

Version: ~ [ wine-1.5.30 ] ~ [ wine-1.5.29 ] ~ [ wine-1.5.28 ] ~ [ wine-1.5.27 ] ~ [ wine-1.5.26 ] ~ [ wine-1.5.25 ] ~ [ wine-1.5.24 ] ~ [ wine-1.5.23 ] ~ [ wine-1.5.22 ] ~ [ wine-1.5.21 ] ~ [ wine-1.5.20 ] ~ [ wine-1.5.19 ] ~ [ wine-1.5.18 ] ~ [ wine-1.5.17 ] ~ [ wine-1.5.16 ] ~ [ wine-1.5.15 ] ~ [ wine-1.5.14 ] ~ [ wine-1.5.13 ] ~ [ wine-1.5.12 ] ~ [ wine-1.5.11 ] ~ [ wine-1.5.10 ] ~ [ wine-1.5.9 ] ~ [ wine-1.5.8 ] ~ [ wine-1.5.7 ] ~ [ wine-1.4.1 ] ~ [ wine-1.5.6 ] ~ [ wine-1.5.5 ] ~ [ wine-1.5.4 ] ~ [ wine-1.5.3 ] ~ [ wine-1.5.2 ] ~ [ wine-1.5.1 ] ~ [ wine-1.5.0 ] ~ [ wine-1.4 ] ~ [ wine-1.4-rc6 ] ~ [ wine-1.4-rc5 ] ~ [ wine-1.4-rc4 ] ~ [ wine-1.4-rc3 ] ~ [ wine-1.4-rc2 ] ~ [ wine-1.4-rc1 ] ~ [ wine-1.3.37 ] ~ [ wine-1.3.36 ] ~ [ wine-1.3.35 ] ~ [ wine-1.3.34 ] ~ [ wine-1.3.33 ] ~ [ wine-1.3.32 ] ~ [ wine-1.3.31 ] ~ [ wine-1.3.30 ] ~ [ wine-1.3.29 ] ~ [ wine-1.3.28 ] ~ [ wine-1.3.27 ] ~ [ wine-1.3.26 ] ~ [ wine-1.3.25 ] ~ [ wine-1.3.24 ] ~ [ wine-1.3.23 ] ~ [ wine-1.3.22 ] ~ [ wine-1.3.21 ] ~ [ wine-1.3.20 ] ~ [ wine-1.3.19 ] ~ [ wine-1.3.18 ] ~ [ wine-1.2.3 ] ~ [ wine-1.3.17 ] ~ [ wine-1.3.16 ] ~ [ wine-1.3.15 ] ~ [ wine-1.3.14 ] ~ [ wine-1.3.13 ] ~ [ wine-1.3.12 ] ~ [ wine-1.3.11 ] ~ [ wine-1.3.10 ] ~ [ wine-1.3.9 ] ~ [ wine-1.2.2 ] ~ [ wine-1.3.8 ] ~ [ wine-1.3.7 ] ~ [ wine-1.3.6 ] ~ [ wine-1.3.5 ] ~ [ wine-1.2.1 ] ~ [ wine-1.3.4 ] ~ [ wine-1.3.3 ] ~ [ wine-1.3.2 ] ~ [ wine-1.3.1 ] ~ [ wine-1.3.0 ] ~ [ wine-1.2 ] ~ [ wine-1.2-rc7 ] ~ [ wine-1.2-rc6 ] ~ [ wine-1.2-rc5 ] ~ [ wine-1.2-rc4 ] ~ [ wine-1.2-rc3 ] ~ [ wine-1.2-rc2 ] ~ [ wine-1.2-rc1 ] ~ [ wine-1.1.44 ] ~ [ wine-1.1.43 ] ~ [ wine-1.1.42 ] ~ [ wine-1.1.41 ] ~ [ wine-1.1.40 ] ~ [ wine-1.1.39 ] ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.