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