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