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