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