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