char -> const char fixes.
[wine] / dlls / d3d8 / directx.c
1 /*
2  * IDirect3D8 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 #include "d3d8_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
40
41 /* x11drv GDI escapes */
42 #define X11DRV_ESCAPE 6789
43 enum x11drv_escape_codes
44 {
45     X11DRV_GET_DISPLAY,   /* get X11 display for a DC */
46     X11DRV_GET_DRAWABLE,  /* get current drawable for a DC */
47     X11DRV_GET_FONT,      /* get current X font for a DC */
48 };
49
50 #define NUM_FORMATS 7
51 static const D3DFORMAT device_formats[NUM_FORMATS] = {
52   D3DFMT_P8,
53   D3DFMT_R3G3B2,
54   D3DFMT_R5G6B5, 
55   D3DFMT_X1R5G5B5,
56   D3DFMT_X4R4G4B4,
57   D3DFMT_R8G8B8,
58   D3DFMT_X8R8G8B8
59 };
60
61 static void IDirect3D8Impl_FillGLCaps(LPDIRECT3D8 iface, Display* display);
62
63
64 /* retrieve the X display to use on a given DC */
65 inline static Display *get_display( HDC hdc )
66 {
67     Display *display;
68     enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;
69
70     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
71                     sizeof(display), (LPSTR)&display )) display = NULL;
72     return display;
73 }
74
75 /* IDirect3D IUnknown parts follow: */
76 HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface,REFIID riid,LPVOID *ppobj)
77 {
78     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
79
80     if (IsEqualGUID(riid, &IID_IUnknown)
81         || IsEqualGUID(riid, &IID_IDirect3D8)) {
82         IDirect3D8Impl_AddRef(iface);
83         *ppobj = This;
84         return D3D_OK;
85     }
86
87     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
88     return E_NOINTERFACE;
89 }
90
91 ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
92     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
93     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
94     return ++(This->ref);
95 }
96
97 ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
98     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
99     ULONG ref = --This->ref;
100     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
101     if (ref == 0) {
102         IWineD3D_Release(This->WineD3D);
103         HeapFree(GetProcessHeap(), 0, This);
104     }
105     return ref;
106 }
107
108 /* IDirect3D Interface follow: */
109 HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
110     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
111     return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
112 }
113
114 UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
115     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
116     return IWineD3D_GetAdapterCount(This->WineD3D);
117 }
118
119 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterIdentifier       (LPDIRECT3D8 iface,
120                                                             UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
121     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
122     WINED3DADAPTER_IDENTIFIER adapter_id;
123
124     /* dx8 and dx9 have different structures to be filled in, with incompatible 
125        layouts so pass in pointers to the places to be filled via an internal 
126        structure                                                                */
127     adapter_id.Driver           = pIdentifier->Driver;          
128     adapter_id.Description      = pIdentifier->Description;     
129     adapter_id.DeviceName       = NULL;      
130     adapter_id.DriverVersion    = &pIdentifier->DriverVersion;   
131     adapter_id.VendorId         = &pIdentifier->VendorId;        
132     adapter_id.DeviceId         = &pIdentifier->DeviceId;        
133     adapter_id.SubSysId         = &pIdentifier->SubSysId;        
134     adapter_id.Revision         = &pIdentifier->Revision;        
135     adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
136     adapter_id.WHQLLevel        = &pIdentifier->WHQLLevel;       
137
138     return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
139 }
140
141 UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
142     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
143     return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, D3DFMT_UNKNOWN);
144 }
145
146 HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
147     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
148     return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, D3DFMT_UNKNOWN, Mode, pMode);
149 }
150
151 HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
152     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
153     return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, pMode);
154 }
155
156 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
157                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
158                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
159     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
160     return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
161                                     BackBufferFormat, Windowed);
162 }
163
164 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
165                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
166                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
167     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
168     return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
169                                     Usage, RType, CheckFormat);
170 }
171
172 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
173                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
174                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
175     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
176     return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
177                                                Windowed, MultiSampleType, NULL);
178 }
179
180 HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface, 
181                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
182                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
183     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
184     return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
185                                            RenderTargetFormat, DepthStencilFormat);
186 }
187
188 HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
189     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
190     return IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, (void *)pCaps);
191 }
192
193 HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
194     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
195     return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
196 }
197
198 static void IDirect3D8Impl_FillGLCaps(LPDIRECT3D8 iface, Display* display) {
199     const char *GL_Extensions = NULL;
200     const char *GLX_Extensions = NULL;
201     GLint gl_max;
202     const char* gl_string = NULL;
203     const char* gl_string_cursor = NULL;
204     Bool test = 0;
205     int major, minor;
206     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
207
208     if (This->gl_info.bIsFilled) return;
209     This->gl_info.bIsFilled = 1;
210
211     TRACE_(d3d_caps)("(%p, %p)\n", This, display);
212
213     if (NULL != display) {
214       test = glXQueryVersion(display, &major, &minor);
215       This->gl_info.glx_version = ((major & 0x0000FFFF) << 16) | (minor & 0x0000FFFF);
216       gl_string = glXGetClientString(display, GLX_VENDOR);
217     } else {
218       gl_string = glGetString(GL_VENDOR);
219     }
220     
221     if (strstr(gl_string, "NVIDIA")) {
222       This->gl_info.gl_vendor = VENDOR_NVIDIA;
223     } else if (strstr(gl_string, "ATI")) {
224       This->gl_info.gl_vendor = VENDOR_ATI;
225     } else {
226       This->gl_info.gl_vendor = VENDOR_WINE;
227     }
228    
229     TRACE_(d3d_caps)("found GL_VENDOR (%s)->(0x%04x)\n", debugstr_a(gl_string), This->gl_info.gl_vendor);
230     
231     gl_string = glGetString(GL_VERSION);
232     switch (This->gl_info.gl_vendor) {
233     case VENDOR_NVIDIA:
234       gl_string_cursor = strstr(gl_string, "NVIDIA");
235       gl_string_cursor = strstr(gl_string_cursor, " ");
236       while (*gl_string_cursor && ' ' == *gl_string_cursor) ++gl_string_cursor;
237       if (*gl_string_cursor) {
238         char tmp[16];
239         int cursor = 0;
240
241         while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
242           tmp[cursor++] = *gl_string_cursor;
243           ++gl_string_cursor;
244         }
245         tmp[cursor] = 0;
246         major = atoi(tmp);
247         
248         if (*gl_string_cursor != '.') WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
249         ++gl_string_cursor;
250
251         while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
252           tmp[cursor++] = *gl_string_cursor;
253           ++gl_string_cursor;
254         }
255         tmp[cursor] = 0;
256         minor = atoi(tmp);
257         break;
258       }
259     case VENDOR_ATI:
260       major = minor = 0;
261       gl_string_cursor = strchr(gl_string, '-');
262       if (gl_string_cursor++) {
263         int error = 0;
264         /* Check if version number is of the form x.y.z */
265         if (*gl_string_cursor > '9' && *gl_string_cursor < '0')
266           error = 1;
267         if (!error && *(gl_string_cursor+2) > '9' && *(gl_string_cursor+2) < '0')
268           error = 1;
269         if (!error && *(gl_string_cursor+4) > '9' && *(gl_string_cursor+4) < '0')
270           error = 1;
271         if (!error && *(gl_string_cursor+1) != '.' && *(gl_string_cursor+3) != '.')
272           error = 1;
273         /* Mark version number as malformed */
274         if (error)
275           gl_string_cursor = 0;
276       }
277       if (!gl_string_cursor)
278         WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
279       else {
280         major = *gl_string_cursor - '0';
281         minor = (*(gl_string_cursor+2) - '0') * 256 + (*(gl_string_cursor+4) - '0');
282       }      
283       break;
284     default:
285       major = 0;
286       minor = 9;
287     }
288     This->gl_info.gl_driver_version = MAKEDWORD_VERSION(major, minor);
289
290     FIXME_(d3d_caps)("found GL_VERSION  (%s)->(0x%08lx)\n", debugstr_a(gl_string), This->gl_info.gl_driver_version);
291
292     gl_string = glGetString(GL_RENDERER);
293     strcpy(This->gl_info.gl_renderer, gl_string);
294
295     switch (This->gl_info.gl_vendor) {
296     case VENDOR_NVIDIA:
297       if (strstr(This->gl_info.gl_renderer, "GeForce4 Ti")) {
298         This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
299       } else if (strstr(This->gl_info.gl_renderer, "GeForceFX")) {
300         This->gl_info.gl_card = CARD_NVIDIA_GEFORCEFX_5900ULTRA;
301       } else {
302         This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
303       }
304       break;
305     case VENDOR_ATI:
306       if (strstr(This->gl_info.gl_renderer, "RADEON 9800 PRO")) {
307         This->gl_info.gl_card = CARD_ATI_RADEON_9800PRO;
308       } else if (strstr(This->gl_info.gl_renderer, "RADEON 9700 PRO")) {
309         This->gl_info.gl_card = CARD_ATI_RADEON_9700PRO;
310       } else {
311         This->gl_info.gl_card = CARD_ATI_RADEON_8500;
312       }
313       break;
314     default:
315       This->gl_info.gl_card = CARD_WINE;
316       break;
317     }
318
319     FIXME_(d3d_caps)("found GL_RENDERER (%s)->(0x%04x)\n", debugstr_a(This->gl_info.gl_renderer), This->gl_info.gl_card);
320
321     /*
322      * Initialize openGL extension related variables
323      *  with Default values
324      */
325     memset(&This->gl_info.supported, 0, sizeof(This->gl_info.supported));
326     This->gl_info.max_textures   = 1;
327     This->gl_info.ps_arb_version = PS_VERSION_NOT_SUPPORTED;
328     This->gl_info.vs_arb_version = VS_VERSION_NOT_SUPPORTED;
329     This->gl_info.vs_nv_version  = VS_VERSION_NOT_SUPPORTED;
330     This->gl_info.vs_ati_version = VS_VERSION_NOT_SUPPORTED;
331
332 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = NULL;
333     GL_EXT_FUNCS_GEN;
334 #undef USE_GL_FUNC
335
336     /* Retrieve opengl defaults */
337     glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
338     This->gl_info.max_clipplanes = min(MAX_CLIPPLANES, gl_max);
339     TRACE_(d3d_caps)("ClipPlanes support - num Planes=%d\n", gl_max);
340
341     glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
342     This->gl_info.max_lights = gl_max;
343     TRACE_(d3d_caps)("Lights support - max lights=%d\n", gl_max);
344
345     /* Parse the gl supported features, in theory enabling parts of our code appropriately */
346     GL_Extensions = glGetString(GL_EXTENSIONS);
347     TRACE_(d3d_caps)("GL_Extensions reported:\n");  
348     
349     if (NULL == GL_Extensions) {
350       ERR("   GL_Extensions returns NULL\n");      
351     } else {
352       while (*GL_Extensions != 0x00) {
353         const char *Start = GL_Extensions;
354         char ThisExtn[256];
355
356         memset(ThisExtn, 0x00, sizeof(ThisExtn));
357         while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
358           GL_Extensions++;
359         }
360         memcpy(ThisExtn, Start, (GL_Extensions - Start));
361         TRACE_(d3d_caps)("- %s\n", ThisExtn);
362
363         /**
364          * ARB 
365          */
366         if (strcmp(ThisExtn, "GL_ARB_fragment_program") == 0) {
367           This->gl_info.ps_arb_version = PS_VERSION_11;
368           TRACE_(d3d_caps)(" FOUND: ARB Pixel Shader support - version=%02x\n", This->gl_info.ps_arb_version);
369           This->gl_info.supported[ARB_FRAGMENT_PROGRAM] = TRUE;
370         } else if (strcmp(ThisExtn, "GL_ARB_multisample") == 0) {
371           TRACE_(d3d_caps)(" FOUND: ARB Multisample support\n");
372           This->gl_info.supported[ARB_MULTISAMPLE] = TRUE;
373         } else if (strcmp(ThisExtn, "GL_ARB_multitexture") == 0) {
374           glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
375           TRACE_(d3d_caps)(" FOUND: ARB Multitexture support - GL_MAX_TEXTURE_UNITS_ARB=%u\n", gl_max);
376           This->gl_info.supported[ARB_MULTITEXTURE] = TRUE;
377           This->gl_info.max_textures = min(8, gl_max);
378         } else if (strcmp(ThisExtn, "GL_ARB_texture_cube_map") == 0) {
379           TRACE_(d3d_caps)(" FOUND: ARB Texture Cube Map support\n");
380           This->gl_info.supported[ARB_TEXTURE_CUBE_MAP] = TRUE;
381           TRACE_(d3d_caps)(" IMPLIED: NVIDIA (NV) Texture Gen Reflection support\n");
382           This->gl_info.supported[NV_TEXGEN_REFLECTION] = TRUE;
383         } else if (strcmp(ThisExtn, "GL_ARB_texture_compression") == 0) {
384           TRACE_(d3d_caps)(" FOUND: ARB Texture Compression support\n");
385           This->gl_info.supported[ARB_TEXTURE_COMPRESSION] = TRUE;
386         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_add") == 0) {
387           TRACE_(d3d_caps)(" FOUND: ARB Texture Env Add support\n");
388           This->gl_info.supported[ARB_TEXTURE_ENV_ADD] = TRUE;
389         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_combine") == 0) {
390           TRACE_(d3d_caps)(" FOUND: ARB Texture Env combine support\n");
391           This->gl_info.supported[ARB_TEXTURE_ENV_COMBINE] = TRUE;
392         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_dot3") == 0) {
393           TRACE_(d3d_caps)(" FOUND: ARB Dot3 support\n");
394           This->gl_info.supported[ARB_TEXTURE_ENV_DOT3] = TRUE;
395         } else if (strcmp(ThisExtn, "GL_ARB_texture_border_clamp") == 0) {
396           TRACE_(d3d_caps)(" FOUND: ARB Texture border clamp support\n");
397           This->gl_info.supported[ARB_TEXTURE_BORDER_CLAMP] = TRUE;
398         } else if (strcmp(ThisExtn, "GL_ARB_texture_mirrored_repeat") == 0) {
399           TRACE_(d3d_caps)(" FOUND: ARB Texture mirrored repeat support\n");
400           This->gl_info.supported[ARB_TEXTURE_MIRRORED_REPEAT] = TRUE;
401         } else if (strstr(ThisExtn, "GL_ARB_vertex_program")) {
402           This->gl_info.vs_arb_version = VS_VERSION_11;
403           TRACE_(d3d_caps)(" FOUND: ARB Vertex Shader support - version=%02x\n", This->gl_info.vs_arb_version);
404           This->gl_info.supported[ARB_VERTEX_PROGRAM] = TRUE;
405
406         /**
407          * EXT
408          */
409         } else if (strcmp(ThisExtn, "GL_EXT_fog_coord") == 0) {
410           TRACE_(d3d_caps)(" FOUND: EXT Fog coord support\n");
411           This->gl_info.supported[EXT_FOG_COORD] = TRUE;
412         } else if (strcmp(ThisExtn, "GL_EXT_paletted_texture") == 0) { /* handle paletted texture extensions */
413           TRACE_(d3d_caps)(" FOUND: EXT Paletted texture support\n");
414           This->gl_info.supported[EXT_PALETTED_TEXTURE] = TRUE;
415         } else if (strcmp(ThisExtn, "GL_EXT_point_parameters") == 0) {
416           TRACE_(d3d_caps)(" FOUND: EXT Point parameters support\n");
417           This->gl_info.supported[EXT_POINT_PARAMETERS] = TRUE;
418         } else if (strcmp(ThisExtn, "GL_EXT_secondary_color") == 0) {
419           TRACE_(d3d_caps)(" FOUND: EXT Secondary coord support\n");
420           This->gl_info.supported[EXT_SECONDARY_COLOR] = TRUE;
421         } else if (strcmp(ThisExtn, "GL_EXT_stencil_wrap") == 0) {
422           TRACE_(d3d_caps)(" FOUND: EXT Stencil wrap support\n");
423           This->gl_info.supported[EXT_STENCIL_WRAP] = TRUE;
424         } else if (strcmp(ThisExtn, "GL_EXT_texture_compression_s3tc") == 0) {
425           TRACE_(d3d_caps)(" FOUND: EXT Texture S3TC compression support\n");
426           This->gl_info.supported[EXT_TEXTURE_COMPRESSION_S3TC] = TRUE;
427         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_add") == 0) {
428           TRACE_(d3d_caps)(" FOUND: EXT Texture Env Add support\n");
429           This->gl_info.supported[EXT_TEXTURE_ENV_ADD] = TRUE;
430         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_combine") == 0) {
431           TRACE_(d3d_caps)(" FOUND: EXT Texture Env combine support\n");
432           This->gl_info.supported[EXT_TEXTURE_ENV_COMBINE] = TRUE;
433         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_dot3") == 0) {
434           TRACE_(d3d_caps)(" FOUND: EXT Dot3 support\n");
435           This->gl_info.supported[EXT_TEXTURE_ENV_DOT3] = TRUE;
436         } else if (strcmp(ThisExtn, "GL_EXT_texture_filter_anisotropic") == 0) {
437           TRACE_(d3d_caps)(" FOUND: EXT Texture Anisotropic filter support\n");
438           This->gl_info.supported[EXT_TEXTURE_FILTER_ANISOTROPIC] = TRUE;
439         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod") == 0) {
440           TRACE_(d3d_caps)(" FOUND: EXT Texture LOD support\n");
441           This->gl_info.supported[EXT_TEXTURE_LOD] = TRUE;
442         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod_bias") == 0) {
443           TRACE_(d3d_caps)(" FOUND: EXT Texture LOD bias support\n");
444           This->gl_info.supported[EXT_TEXTURE_LOD_BIAS] = TRUE;
445         } else if (strcmp(ThisExtn, "GL_EXT_vertex_weighting") == 0) {
446           TRACE_(d3d_caps)(" FOUND: EXT Vertex weighting support\n");
447           This->gl_info.supported[EXT_VERTEX_WEIGHTING] = TRUE;
448
449         /**
450          * NVIDIA 
451          */
452         } else if (strstr(ThisExtn, "GL_NV_fog_distance")) {
453           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Fog Distance support\n");
454           This->gl_info.supported[NV_FOG_DISTANCE] = TRUE;
455         } else if (strstr(ThisExtn, "GL_NV_fragment_program")) {
456           This->gl_info.ps_nv_version = PS_VERSION_11;
457           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Pixel Shader support - version=%02x\n", This->gl_info.ps_nv_version);
458         } else if (strcmp(ThisExtn, "GL_NV_register_combiners") == 0) {
459           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (1) support\n");
460           This->gl_info.supported[NV_REGISTER_COMBINERS] = TRUE;
461         } else if (strcmp(ThisExtn, "GL_NV_register_combiners2") == 0) {
462           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (2) support\n");
463           This->gl_info.supported[NV_REGISTER_COMBINERS2] = TRUE;
464         } else if (strcmp(ThisExtn, "GL_NV_texgen_reflection") == 0) {
465           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Gen Reflection support\n");
466           This->gl_info.supported[NV_TEXGEN_REFLECTION] = TRUE;
467         } else if (strcmp(ThisExtn, "GL_NV_texture_env_combine4") == 0) {
468           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Env combine (4) support\n");
469           This->gl_info.supported[NV_TEXTURE_ENV_COMBINE4] = TRUE;
470         } else if (strcmp(ThisExtn, "GL_NV_texture_shader") == 0) {
471           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (1) support\n");
472           This->gl_info.supported[NV_TEXTURE_SHADER] = TRUE;
473         } else if (strcmp(ThisExtn, "GL_NV_texture_shader2") == 0) {
474           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (2) support\n");
475           This->gl_info.supported[NV_TEXTURE_SHADER2] = TRUE;
476         } else if (strcmp(ThisExtn, "GL_NV_texture_shader3") == 0) {
477           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (3) support\n");
478           This->gl_info.supported[NV_TEXTURE_SHADER3] = TRUE;
479         } else if (strstr(ThisExtn, "GL_NV_vertex_program")) {
480           This->gl_info.vs_nv_version = max(This->gl_info.vs_nv_version, (0 == strcmp(ThisExtn, "GL_NV_vertex_program1_1")) ? VS_VERSION_11 : VS_VERSION_10);
481           This->gl_info.vs_nv_version = max(This->gl_info.vs_nv_version, (0 == strcmp(ThisExtn, "GL_NV_vertex_program2"))   ? VS_VERSION_20 : VS_VERSION_10);
482           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Vertex Shader support - version=%02x\n", This->gl_info.vs_nv_version);
483           This->gl_info.supported[NV_VERTEX_PROGRAM] = TRUE;
484
485         /**
486          * ATI
487          */
488         /** TODO */
489         } else if (strcmp(ThisExtn, "GL_ATI_texture_env_combine3") == 0) {
490           TRACE_(d3d_caps)(" FOUND: ATI Texture Env combine (3) support\n");
491           This->gl_info.supported[ATI_TEXTURE_ENV_COMBINE3] = TRUE;
492         } else if (strcmp(ThisExtn, "GL_ATI_texture_mirror_once") == 0) {
493           TRACE_(d3d_caps)(" FOUND: ATI Texture Mirror Once support\n");
494           This->gl_info.supported[ATI_TEXTURE_MIRROR_ONCE] = TRUE;
495         } else if (strcmp(ThisExtn, "GL_EXT_vertex_shader") == 0) {
496           This->gl_info.vs_ati_version = VS_VERSION_11;
497           TRACE_(d3d_caps)(" FOUND: ATI (EXT) Vertex Shader support - version=%02x\n", This->gl_info.vs_ati_version);
498           This->gl_info.supported[EXT_VERTEX_SHADER] = TRUE;
499         }
500
501
502         if (*GL_Extensions == ' ') GL_Extensions++;
503       }
504     }
505
506 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
507     GL_EXT_FUNCS_GEN;
508 #undef USE_GL_FUNC
509
510     if (display != NULL) {
511         GLX_Extensions = glXQueryExtensionsString(display, DefaultScreen(display));
512         TRACE_(d3d_caps)("GLX_Extensions reported:\n");  
513     
514         if (NULL == GLX_Extensions) {
515           ERR("   GLX_Extensions returns NULL\n");      
516         } else {
517           while (*GLX_Extensions != 0x00) {
518             const char *Start = GLX_Extensions;
519             char ThisExtn[256];
520            
521             memset(ThisExtn, 0x00, sizeof(ThisExtn));
522             while (*GLX_Extensions != ' ' && *GLX_Extensions != 0x00) {
523               GLX_Extensions++;
524             }
525             memcpy(ThisExtn, Start, (GLX_Extensions - Start));
526             TRACE_(d3d_caps)("- %s\n", ThisExtn);
527             if (*GLX_Extensions == ' ') GLX_Extensions++;
528           }
529         }
530     }
531
532 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
533     GLX_EXT_FUNCS_GEN;
534 #undef USE_GL_FUNC
535
536     /* Only save the values obtained when a display is provided */
537     if (display != NULL) This->isGLInfoValid = TRUE;
538
539 }
540
541 /* Internal function called back during the CreateDevice to create a render target */
542 HRESULT WINAPI D3D8CB_CreateRenderTarget(IUnknown *device, UINT Width, UINT Height, 
543                                          D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, 
544                                          DWORD MultisampleQuality, BOOL Lockable, 
545                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
546     HRESULT res = D3D_OK;
547     IDirect3DSurface8Impl *d3dSurface = NULL;
548
549     /* Note - Throw away MultisampleQuality and SharedHandle - only relevant for d3d9  */
550     res = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)device, Width, Height, 
551                                          Format, MultiSample, Lockable, 
552                                          (IDirect3DSurface8 **)&d3dSurface);
553     if (res == D3D_OK) {
554         *ppSurface = d3dSurface->wineD3DSurface;
555     }
556     return res;
557 }
558
559 HRESULT  WINAPI  IDirect3D8Impl_CreateDevice               (LPDIRECT3D8 iface,
560                                                             UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
561                                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
562                                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
563     IDirect3DDevice8Impl *object;
564     HWND whichHWND;
565     int num;
566     XVisualInfo template;
567     HDC hDc;
568     WINED3DPRESENT_PARAMETERS localParameters;
569
570     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
571     TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
572           hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);
573
574     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
575         return D3DERR_INVALIDCALL;
576     }
577
578     /* Allocate the storage for the device */
579     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
580     if (NULL == object) {
581       return D3DERR_OUTOFVIDEOMEMORY;
582     }
583     object->lpVtbl = &Direct3DDevice8_Vtbl;
584     object->ref = 1;
585     object->direct3d8 = This;
586     /** The device AddRef the direct3d8 Interface else crash in propers clients codes */
587     IDirect3D8_AddRef((LPDIRECT3D8) object->direct3d8);
588
589     /* Allocate an associated WineD3DDevice object */
590     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;             
591     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
592     localParameters.BackBufferFormat               = &pPresentationParameters->BackBufferFormat;           
593     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;            
594     localParameters.MultiSampleType                = &pPresentationParameters->MultiSampleType;            
595     localParameters.MultiSampleQuality             = NULL; /* New at dx9 */
596     localParameters.SwapEffect                     = &pPresentationParameters->SwapEffect;                 
597     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;              
598     localParameters.Windowed                       = &pPresentationParameters->Windowed;                   
599     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;     
600     localParameters.AutoDepthStencilFormat         = &pPresentationParameters->AutoDepthStencilFormat;     
601     localParameters.Flags                          = &pPresentationParameters->Flags;                      
602     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz; 
603     localParameters.PresentationInterval           = &pPresentationParameters->FullScreen_PresentationInterval;    /* Renamed in dx9 */
604     IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &localParameters, &object->WineD3DDevice, (IUnknown *)object, D3D8CB_CreateRenderTarget);
605
606     /** use StateBlock Factory here, for creating the startup stateBlock */
607     object->StateBlock = NULL;
608     IDirect3DDeviceImpl_CreateStateBlock(object, D3DSBT_ALL, NULL);
609     object->UpdateStateBlock = object->StateBlock;
610
611     /* Save the creation parameters */
612     object->CreateParms.AdapterOrdinal = Adapter;
613     object->CreateParms.DeviceType = DeviceType;
614     object->CreateParms.hFocusWindow = hFocusWindow;
615     object->CreateParms.BehaviorFlags = BehaviourFlags;
616
617     *ppReturnedDeviceInterface = (LPDIRECT3DDEVICE8) object;
618
619     /* Initialize settings */
620     object->PresentParms.BackBufferCount = 1; /* Opengl only supports one? */
621     object->adapterNo = Adapter;
622     object->devType = DeviceType;
623
624     /* Initialize openGl - Note the visual is chosen as the window is created and the glcontext cannot
625          use different properties after that point in time. FIXME: How to handle when requested format 
626          doesn't match actual visual? Cannot choose one here - code removed as it ONLY works if the one
627          it chooses is identical to the one already being used!                                        */
628     /* FIXME: Handle stencil appropriately via EnableAutoDepthStencil / AutoDepthStencilFormat */
629
630     /* Which hwnd are we using? */
631     whichHWND = pPresentationParameters->hDeviceWindow;
632     if (!whichHWND) {
633         whichHWND = hFocusWindow;
634     }
635     object->win_handle = whichHWND;
636     object->win     = (Window)GetPropA( whichHWND, "__wine_x11_client_window" );
637
638     hDc = GetDC(whichHWND);
639     object->display = get_display(hDc);
640
641     TRACE("(%p)->(DepthStencil:(%u,%s), BackBufferFormat:(%u,%s))\n", This, 
642           pPresentationParameters->AutoDepthStencilFormat, debug_d3dformat(pPresentationParameters->AutoDepthStencilFormat),
643           pPresentationParameters->BackBufferFormat, debug_d3dformat(pPresentationParameters->BackBufferFormat));
644
645     ENTER_GL();
646
647     /* Create a context based off the properties of the existing visual */
648     template.visualid = (VisualID)GetPropA(GetDesktopWindow(), "__wine_x11_visual_id");
649     object->visInfo = XGetVisualInfo(object->display, VisualIDMask, &template, &num);
650     if (NULL == object->visInfo) {
651         ERR("cannot really get XVisual\n"); 
652         LEAVE_GL();
653         return D3DERR_NOTAVAILABLE;
654      }
655     object->glCtx = glXCreateContext(object->display, object->visInfo, NULL, GL_TRUE);
656     if (NULL == object->glCtx) {
657       ERR("cannot create glxContext\n"); 
658       LEAVE_GL();
659       return D3DERR_NOTAVAILABLE;
660      }
661     LEAVE_GL();
662
663     ReleaseDC(whichHWND, hDc);
664     
665     if (object->glCtx == NULL) {
666         ERR("Error in context creation !\n");
667         return D3DERR_INVALIDCALL;
668     } else {
669         TRACE("Context created (HWND=%p, glContext=%p, Window=%ld, VisInfo=%p)\n",
670               whichHWND, object->glCtx, object->win, object->visInfo);
671     }
672
673     /* If not windowed, need to go fullscreen, and resize the HWND to the appropriate  */
674     /*        dimensions                                                               */
675     if (!pPresentationParameters->Windowed) {
676 #if 1
677         DEVMODEW devmode;
678         HDC hdc;
679         int bpp = 0;
680         memset(&devmode, 0, sizeof(DEVMODEW));
681         devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; 
682         MultiByteToWideChar(CP_ACP, 0, "Gamers CG", -1, devmode.dmDeviceName, CCHDEVICENAME);
683         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
684         bpp = GetDeviceCaps(hdc, BITSPIXEL);
685         DeleteDC(hdc);
686         devmode.dmBitsPerPel = (bpp >= 24) ? 32 : bpp;/*Stupid XVidMode cannot change bpp D3DFmtGetBpp(object, pPresentationParameters->BackBufferFormat);*/
687         devmode.dmPelsWidth  = pPresentationParameters->BackBufferWidth;
688         devmode.dmPelsHeight = pPresentationParameters->BackBufferHeight;
689         ChangeDisplaySettingsExW(devmode.dmDeviceName, &devmode, object->win_handle, CDS_FULLSCREEN, NULL);
690 #else
691         FIXME("Requested full screen support not implemented, expect windowed operation\n");
692 #endif
693
694         /* Make popup window */
695         SetWindowLongA(whichHWND, GWL_STYLE, WS_POPUP);
696         SetWindowPos(object->win_handle, HWND_TOP, 0, 0, 
697                      pPresentationParameters->BackBufferWidth,
698                      pPresentationParameters->BackBufferHeight, SWP_SHOWWINDOW | SWP_FRAMECHANGED);
699     }
700
701     TRACE("Creating back buffer\n");
702     /* MSDN: If Windowed is TRUE and either of the BackBufferWidth/Height values is zero,
703        then the corresponding dimension of the client area of the hDeviceWindow
704        (or the focus window, if hDeviceWindow is NULL) is taken. */
705     if (pPresentationParameters->Windowed && ((pPresentationParameters->BackBufferWidth  == 0) ||
706                                               (pPresentationParameters->BackBufferHeight == 0))) {
707         RECT Rect;
708
709         GetClientRect(whichHWND, &Rect);
710
711         if (pPresentationParameters->BackBufferWidth == 0) {
712            pPresentationParameters->BackBufferWidth = Rect.right;
713            TRACE("Updating width to %d\n", pPresentationParameters->BackBufferWidth);
714         }
715         if (pPresentationParameters->BackBufferHeight == 0) {
716            pPresentationParameters->BackBufferHeight = Rect.bottom;
717            TRACE("Updating height to %d\n", pPresentationParameters->BackBufferHeight);
718         }
719     }
720
721     /* Save the presentation parms now filled in correctly */
722     memcpy(&object->PresentParms, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
723
724
725     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
726                                             pPresentationParameters->BackBufferWidth,
727                                             pPresentationParameters->BackBufferHeight,
728                                             pPresentationParameters->BackBufferFormat,
729                                             pPresentationParameters->MultiSampleType,
730                                             TRUE,
731                                             (LPDIRECT3DSURFACE8*) &object->frontBuffer);
732
733     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
734                                             pPresentationParameters->BackBufferWidth,
735                                             pPresentationParameters->BackBufferHeight,
736                                             pPresentationParameters->BackBufferFormat,
737                                             pPresentationParameters->MultiSampleType,
738                                             TRUE,
739                                             (LPDIRECT3DSURFACE8*) &object->backBuffer);
740
741     if (pPresentationParameters->EnableAutoDepthStencil) {
742        IDirect3DDevice8Impl_CreateDepthStencilSurface((LPDIRECT3DDEVICE8) object,
743                                                       pPresentationParameters->BackBufferWidth,
744                                                       pPresentationParameters->BackBufferHeight,
745                                                       pPresentationParameters->AutoDepthStencilFormat,
746                                                       D3DMULTISAMPLE_NONE,
747                                                       (LPDIRECT3DSURFACE8*) &object->depthStencilBuffer);
748     } else {
749       object->depthStencilBuffer = NULL;
750     }
751     TRACE("FrontBuf @ %p, BackBuf @ %p, DepthStencil @ %p\n",object->frontBuffer, object->backBuffer, object->depthStencilBuffer);
752
753     /* init the default renderTarget management */
754     object->drawable = object->win;
755     object->render_ctx = object->glCtx;
756     object->renderTarget = object->backBuffer;
757     IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->renderTarget);
758     object->stencilBufferTarget = object->depthStencilBuffer;
759     if (NULL != object->stencilBufferTarget) {
760       IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->stencilBufferTarget);
761     }
762
763     ENTER_GL();
764
765     if (glXMakeCurrent(object->display, object->win, object->glCtx) == False) {
766       ERR("Error in setting current context (context %p drawable %ld)!\n", object->glCtx, object->win);
767     }
768     checkGLcall("glXMakeCurrent");
769
770     /* Clear the screen */
771     glClearColor(1.0, 0.0, 0.0, 0.0);
772     checkGLcall("glClearColor");
773     glColor3f(1.0, 1.0, 1.0);
774     checkGLcall("glColor3f");
775
776     glEnable(GL_LIGHTING);
777     checkGLcall("glEnable");
778
779     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
780     checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
781
782     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
783     checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
784
785     glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
786     checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
787
788     /* 
789      * Initialize openGL extension related variables
790      *  with Default values 
791      */
792     IDirect3D8Impl_FillGLCaps(iface, object->display);
793
794     /* Setup all the devices defaults */
795     IDirect3DDeviceImpl_InitStartupStateBlock(object);
796
797     LEAVE_GL();
798
799     { /* Set a default viewport */
800        D3DVIEWPORT8 vp;
801        vp.X      = 0;
802        vp.Y      = 0;
803        vp.Width  = pPresentationParameters->BackBufferWidth;
804        vp.Height = pPresentationParameters->BackBufferHeight;
805        vp.MinZ   = 0.0f;
806        vp.MaxZ   = 1.0f;
807        IDirect3DDevice8Impl_SetViewport((LPDIRECT3DDEVICE8) object, &vp);
808     }
809
810     /* Initialize the current view state */
811     object->modelview_valid = 1;
812     object->proj_valid = 0;
813     object->view_ident = 1;
814     object->last_was_rhw = 0;
815     glGetIntegerv(GL_MAX_LIGHTS, &object->maxConcurrentLights);
816     TRACE("(%p,%d) All defaults now set up, leaving CreateDevice with %p\n", This, Adapter, object);
817
818     /* Clear the screen */
819     IDirect3DDevice8Impl_Clear((LPDIRECT3DDEVICE8) object, 0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER|D3DCLEAR_TARGET, 0x00, 1.0, 0);
820
821     return D3D_OK;
822 }
823
824 IDirect3D8Vtbl Direct3D8_Vtbl =
825 {
826     IDirect3D8Impl_QueryInterface,
827     IDirect3D8Impl_AddRef,
828     IDirect3D8Impl_Release,
829     IDirect3D8Impl_RegisterSoftwareDevice,
830     IDirect3D8Impl_GetAdapterCount,
831     IDirect3D8Impl_GetAdapterIdentifier,
832     IDirect3D8Impl_GetAdapterModeCount,
833     IDirect3D8Impl_EnumAdapterModes,
834     IDirect3D8Impl_GetAdapterDisplayMode,
835     IDirect3D8Impl_CheckDeviceType,
836     IDirect3D8Impl_CheckDeviceFormat,
837     IDirect3D8Impl_CheckDeviceMultiSampleType,
838     IDirect3D8Impl_CheckDepthStencilMatch,
839     IDirect3D8Impl_GetDeviceCaps,
840     IDirect3D8Impl_GetAdapterMonitor,
841     IDirect3D8Impl_CreateDevice
842 };