Assorted spelling 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 /** 
76  * Note: GL seems to trap if GetDeviceCaps is called before any HWND's created
77  * ie there is no GL Context - Get a default rendering context to enable the 
78  * function query some info from GL                                     
79  */    
80 static
81 WineD3D_Context* WineD3DCreateFakeGLContext(void) {
82   static WineD3D_Context ctx = { NULL, NULL, NULL, 0, 0 };
83   WineD3D_Context* ret = NULL;
84
85    if (glXGetCurrentContext() == NULL) {
86      BOOL         gotContext  = FALSE;
87      BOOL         created     = FALSE;
88      XVisualInfo  template;
89      HDC          device_context;
90      Visual*      visual;
91      BOOL         failed = FALSE;
92      int          num;
93      XWindowAttributes win_attr;
94      
95      TRACE_(d3d_caps)("Creating Fake GL Context\n");
96
97      ctx.drawable = (Drawable) GetPropA(GetDesktopWindow(), "__wine_x11_whole_window");
98
99      /* Get the display */
100      device_context = GetDC(0);
101      ctx.display = get_display(device_context);
102      ReleaseDC(0, device_context);
103      
104      /* Get the X visual */
105      ENTER_GL();
106      if (XGetWindowAttributes(ctx.display, ctx.drawable, &win_attr)) {
107        visual = win_attr.visual;
108      } else {
109        visual = DefaultVisual(ctx.display, DefaultScreen(ctx.display));
110      }
111      template.visualid = XVisualIDFromVisual(visual);
112      ctx.visInfo = XGetVisualInfo(ctx.display, VisualIDMask, &template, &num);
113      if (ctx.visInfo == NULL) {
114        LEAVE_GL();
115        WARN_(d3d_caps)("Error creating visual info for capabilities initialization\n");
116        failed = TRUE;
117      }
118      
119      /* Create a GL context */
120      if (!failed) {
121        ctx.glCtx = glXCreateContext(ctx.display, ctx.visInfo, NULL, GL_TRUE);
122        
123        if (ctx.glCtx == NULL) {
124          LEAVE_GL();
125          WARN_(d3d_caps)("Error creating default context for capabilities initialization\n");
126          failed = TRUE;
127        }
128      }
129      
130      /* Make it the current GL context */
131      if (!failed && glXMakeCurrent(ctx.display, ctx.drawable, ctx.glCtx) == False) {
132        glXDestroyContext(ctx.display, ctx.glCtx);
133        LEAVE_GL();
134        WARN_(d3d_caps)("Error setting default context as current for capabilities initialization\n");
135        failed = TRUE;   
136      }
137      
138      /* It worked! Wow... */
139      if (!failed) {
140        gotContext = TRUE;
141        created = TRUE;
142        ret = &ctx;
143      } else {
144        ret = NULL;
145      }
146    } else {
147      if (ctx.ref > 0) ret = &ctx;
148    }
149
150    if (NULL != ret) ++ret->ref;
151
152    return ret;
153 }
154
155 static
156 void WineD3DReleaseFakeGLContext(WineD3D_Context* ctx) {
157   /* If we created a dummy context, throw it away */
158   if (NULL != ctx) {
159     --ctx->ref;
160     if (0 == ctx->ref) {
161       glXMakeCurrent(ctx->display, None, NULL);
162       glXDestroyContext(ctx->display, ctx->glCtx);
163       ctx->display = NULL;
164       ctx->glCtx = NULL;
165       LEAVE_GL();
166     }
167   }
168 }
169  
170    
171 /* IDirect3D IUnknown parts follow: */
172 HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface,REFIID riid,LPVOID *ppobj)
173 {
174     ICOM_THIS(IDirect3D8Impl,iface);
175
176     if (IsEqualGUID(riid, &IID_IUnknown)
177         || IsEqualGUID(riid, &IID_IDirect3D8)) {
178         IDirect3D8Impl_AddRef(iface);
179         *ppobj = This;
180         return D3D_OK;
181     }
182
183     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
184     return E_NOINTERFACE;
185 }
186
187 ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
188     ICOM_THIS(IDirect3D8Impl,iface);
189     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
190     return ++(This->ref);
191 }
192
193 ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
194     ICOM_THIS(IDirect3D8Impl,iface);
195     ULONG ref = --This->ref;
196     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
197     if (ref == 0)
198         HeapFree(GetProcessHeap(), 0, This);
199     return ref;
200 }
201
202 /* IDirect3D Interface follow: */
203 HRESULT  WINAPI  IDirect3D8Impl_RegisterSoftwareDevice     (LPDIRECT3D8 iface, void* pInitializeFunction) {
204     ICOM_THIS(IDirect3D8Impl,iface);
205     FIXME_(d3d_caps)("(%p)->(%p): stub\n", This, pInitializeFunction);
206     return D3D_OK;
207 }
208
209 UINT     WINAPI  IDirect3D8Impl_GetAdapterCount            (LPDIRECT3D8 iface) {
210     ICOM_THIS(IDirect3D8Impl,iface);
211     /* FIXME: Set to one for now to imply the display */
212     TRACE_(d3d_caps)("(%p): Mostly stub, only returns primary display\n", This);
213     return 1;
214 }
215
216 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterIdentifier       (LPDIRECT3D8 iface,
217                                                             UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
218     ICOM_THIS(IDirect3D8Impl,iface);
219
220     TRACE_(d3d_caps)("(%p}->(Adapter: %d, Flags: %lx, pId=%p)\n", This, Adapter, Flags, pIdentifier);
221
222     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
223         return D3DERR_INVALIDCALL;
224     }
225
226     if (Adapter == 0) { /* Display */   
227         /* If we don't know the device settings, go query them now */
228         if (This->isGLInfoValid == FALSE) {
229           WineD3D_Context* ctx = WineD3DCreateFakeGLContext();
230           if (NULL != ctx) IDirect3D8Impl_FillGLCaps(iface, NULL);
231           WineD3DReleaseFakeGLContext(ctx);
232         }
233         if (This->isGLInfoValid == TRUE) {
234           TRACE_(d3d_caps)("device/Vendor Name and Version detection using FillGLCaps\n");
235           strcpy(pIdentifier->Driver, "Display");
236           strcpy(pIdentifier->Description, "Direct3D HAL");
237           pIdentifier->DriverVersion.u.HighPart = 0xa;
238           pIdentifier->DriverVersion.u.LowPart = This->gl_info.gl_driver_version;
239           pIdentifier->VendorId = This->gl_info.gl_vendor;
240           pIdentifier->DeviceId = This->gl_info.gl_card;
241           pIdentifier->SubSysId = 0;
242           pIdentifier->Revision = 0;
243         } else {
244           WARN_(d3d_caps)("Cannot get GLCaps for device/Vendor Name and Version detection using FillGLCaps, currently using NVIDIA identifiers\n");
245           strcpy(pIdentifier->Driver, "Display");
246           strcpy(pIdentifier->Description, "Direct3D HAL");
247           pIdentifier->DriverVersion.u.HighPart = 0xa;
248           pIdentifier->DriverVersion.u.LowPart = MAKEDWORD_VERSION(53, 96); /* last Linux Nvidia drivers */
249           pIdentifier->VendorId = VENDOR_NVIDIA;
250           pIdentifier->DeviceId = CARD_NVIDIA_GEFORCE4_TI4600;
251           pIdentifier->SubSysId = 0;
252           pIdentifier->Revision = 0;
253         }
254         /*FIXME: memcpy(&pIdentifier->DeviceIdentifier, ??, sizeof(??GUID)); */
255         if (Flags & D3DENUM_NO_WHQL_LEVEL) {
256             pIdentifier->WHQLLevel = 0;
257         } else {
258             pIdentifier->WHQLLevel = 1;
259         }
260     } else {
261         FIXME_(d3d_caps)("Adapter not primary display\n");
262     }
263
264     return D3D_OK;
265 }
266
267 UINT     WINAPI  IDirect3D8Impl_GetAdapterModeCount        (LPDIRECT3D8 iface,
268                                                             UINT Adapter) {
269     ICOM_THIS(IDirect3D8Impl,iface);
270
271     TRACE_(d3d_caps)("(%p}->(Adapter: %d)\n", This, Adapter);
272
273     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
274         return D3DERR_INVALIDCALL;
275     }
276
277     if (Adapter == 0) { /* Display */
278         DEVMODEW DevModeW;
279         int i = 0;
280
281         while (EnumDisplaySettingsExW(NULL, i, &DevModeW, 0)) {
282             i++;
283         }
284         TRACE_(d3d_caps)("(%p}->(Adapter: %d) => %d\n", This, Adapter, i);
285         return i;
286     } else {
287         FIXME_(d3d_caps)("Adapter not primary display\n");
288     }
289
290     return 0;
291 }
292
293 HRESULT  WINAPI  IDirect3D8Impl_EnumAdapterModes           (LPDIRECT3D8 iface,
294                                                             UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
295     ICOM_THIS(IDirect3D8Impl,iface);
296
297     TRACE_(d3d_caps)("(%p}->(Adapter:%d, mode:%d, pMode:%p)\n", This, Adapter, Mode, pMode);
298
299     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
300         return D3DERR_INVALIDCALL;
301     }
302
303     if (Adapter == 0) { /* Display */
304         HDC hdc;
305         int bpp = 0;
306         DEVMODEW DevModeW;
307
308         if (EnumDisplaySettingsExW(NULL, Mode, &DevModeW, 0)) 
309         {
310             pMode->Width        = DevModeW.dmPelsWidth;
311             pMode->Height       = DevModeW.dmPelsHeight;
312             bpp                 = DevModeW.dmBitsPerPel;
313             pMode->RefreshRate  = D3DADAPTER_DEFAULT;
314             if (DevModeW.dmFields&DM_DISPLAYFREQUENCY)
315             {
316                 pMode->RefreshRate = DevModeW.dmDisplayFrequency;
317             }
318         }
319         else
320         {
321             TRACE_(d3d_caps)("Requested mode out of range %d\n", Mode);
322             return D3DERR_INVALIDCALL;
323         }
324
325         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
326         bpp = min(GetDeviceCaps(hdc, BITSPIXEL), bpp);
327         DeleteDC(hdc);
328
329         switch (bpp) {
330         case  8: pMode->Format = D3DFMT_R3G3B2;   break;
331         case 16: pMode->Format = D3DFMT_R5G6B5;   break;
332         case 24: /* pMode->Format = D3DFMT_R5G6B5;   break;*/ /* Make 24bit appear as 32 bit */
333         case 32: pMode->Format = D3DFMT_A8R8G8B8; break;
334         default: pMode->Format = D3DFMT_UNKNOWN;
335         }
336         TRACE_(d3d_caps)("W %d H %d rr %d fmt (%x,%s) bpp %u\n", pMode->Width, pMode->Height, pMode->RefreshRate, pMode->Format, debug_d3dformat(pMode->Format), bpp);
337
338     } else {
339         FIXME_(d3d_caps)("Adapter not primary display\n");
340     }
341
342     return D3D_OK;
343 }
344
345 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterDisplayMode      (LPDIRECT3D8 iface,
346                                                             UINT Adapter, D3DDISPLAYMODE* pMode) {
347     ICOM_THIS(IDirect3D8Impl,iface);
348     TRACE_(d3d_caps)("(%p}->(Adapter: %d, pMode: %p)\n", This, Adapter, pMode);
349
350     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
351         return D3DERR_INVALIDCALL;
352     }
353
354     if (Adapter == 0) { /* Display */
355         int bpp = 0;
356         DEVMODEW DevModeW;
357
358         EnumDisplaySettingsExW(NULL, (DWORD)-1, &DevModeW, 0);
359         pMode->Width        = DevModeW.dmPelsWidth;
360         pMode->Height       = DevModeW.dmPelsHeight;
361         bpp                 = DevModeW.dmBitsPerPel;
362         pMode->RefreshRate  = D3DADAPTER_DEFAULT;
363         if (DevModeW.dmFields&DM_DISPLAYFREQUENCY)
364         {
365             pMode->RefreshRate = DevModeW.dmDisplayFrequency;
366         }
367
368         switch (bpp) {
369         case  8: pMode->Format       = D3DFMT_R3G3B2;   break;
370         case 16: pMode->Format       = D3DFMT_R5G6B5;   break;
371         case 24: /*pMode->Format       = D3DFMT_R5G6B5;   break;*/ /* Make 24bit appear as 32 bit */
372         case 32: pMode->Format       = D3DFMT_A8R8G8B8; break;
373         default: pMode->Format       = D3DFMT_UNKNOWN;
374         }
375
376     } else {
377         FIXME_(d3d_caps)("Adapter not primary display\n");
378     }
379
380     TRACE_(d3d_caps)("returning w:%d, h:%d, ref:%d, fmt:%x\n", pMode->Width,
381           pMode->Height, pMode->RefreshRate, pMode->Format);
382     return D3D_OK;
383 }
384
385 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
386                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
387                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
388     ICOM_THIS(IDirect3D8Impl,iface);
389     TRACE_(d3d_caps)("(%p)->(Adptr:%d, CheckType:(%x,%s), DispFmt:(%x,%s), BackBuf:(%x,%s), Win?%d): stub\n", 
390           This, 
391           Adapter, 
392           CheckType, debug_d3ddevicetype(CheckType),
393           DisplayFormat, debug_d3dformat(DisplayFormat),
394           BackBufferFormat, debug_d3dformat(BackBufferFormat),
395           Windowed);
396     /*
397     switch (DisplayFormat) {
398     case D3DFMT_A8R8G8B8:
399       return D3DERR_NOTAVAILABLE;
400     default:
401       break;
402     }
403     */
404
405     return D3D_OK;
406 }
407
408 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
409                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
410                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
411     ICOM_THIS(IDirect3D8Impl,iface);
412     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%u,%s), AdptFmt:(%u,%s), Use:(%lu,%s), ResTyp:(%x,%s), CheckFmt:(%u,%s))\n", 
413           This, 
414           Adapter, 
415           DeviceType, debug_d3ddevicetype(DeviceType), 
416           AdapterFormat, debug_d3dformat(AdapterFormat), 
417           Usage, debug_d3dusage(Usage),
418           RType, debug_d3dressourcetype(RType), 
419           CheckFormat, debug_d3dformat(CheckFormat));
420
421     if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
422         switch (CheckFormat) {
423         case D3DFMT_DXT1:
424         case D3DFMT_DXT3:
425         case D3DFMT_DXT5:
426             return D3D_OK;
427         default:
428             break; /* Avoid compiler warnings */
429         }
430     }
431
432     switch (CheckFormat) {
433     case D3DFMT_UYVY:
434     case D3DFMT_YUY2:
435     case D3DFMT_DXT1:
436     case D3DFMT_DXT2:
437     case D3DFMT_DXT3:
438     case D3DFMT_DXT4:
439     case D3DFMT_DXT5:
440     case D3DFMT_X8L8V8U8:
441     case D3DFMT_L6V5U5:
442     case D3DFMT_V8U8:
443     case D3DFMT_L8:
444       /* Since we do not support these formats right now, don't pretend to. */
445       return D3DERR_NOTAVAILABLE;
446     default:
447       break;
448     }
449
450     return D3D_OK;
451 }
452
453 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
454                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
455                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
456     ICOM_THIS(IDirect3D8Impl,iface);
457     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%x,%s), SurfFmt:(%x,%s), Win?%d, MultiSamp:%x)\n", 
458           This, 
459           Adapter, 
460           DeviceType, debug_d3ddevicetype(DeviceType),
461           SurfaceFormat, debug_d3dformat(SurfaceFormat),
462           Windowed, 
463           MultiSampleType);
464   
465     if (D3DMULTISAMPLE_NONE == MultiSampleType)
466       return D3D_OK;
467     return D3DERR_NOTAVAILABLE;
468 }
469
470 HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface, 
471                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
472                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
473     ICOM_THIS(IDirect3D8Impl,iface);
474     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%x,%s), AdptFmt:(%x,%s), RendrTgtFmt:(%x,%s), DepthStencilFmt:(%x,%s))\n", 
475           This, 
476           Adapter, 
477           DeviceType, debug_d3ddevicetype(DeviceType),
478           AdapterFormat, debug_d3dformat(AdapterFormat),
479           RenderTargetFormat, debug_d3dformat(RenderTargetFormat), 
480           DepthStencilFormat, debug_d3dformat(DepthStencilFormat));
481
482 #if 0
483     switch (DepthStencilFormat) {
484     case D3DFMT_D24X4S4:
485     case D3DFMT_D24X8: 
486     case D3DFMT_D24S8: 
487     case D3DFMT_D32:
488       /**
489        * as i don't know how to really check hard caps of graphics cards
490        * i prefer to not permit 32bit zbuffers enumeration (as few cards can do it)
491        */
492       return D3DERR_NOTAVAILABLE;
493     default:
494       break;
495     }
496 #endif
497     return D3D_OK;
498 }
499
500 HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
501
502     BOOL        gotContext  = FALSE;
503     GLint       gl_tex_size = 0;    
504     WineD3D_Context* fake_ctx = NULL;
505     ICOM_THIS(IDirect3D8Impl,iface);
506
507     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType: %x, pCaps: %p)\n", This, Adapter, DeviceType, pCaps);
508
509     /* Note: GL seems to trap if GetDeviceCaps is called before any HWND's created
510        ie there is no GL Context - Get a default rendering context to enable the 
511        function query some info from GL                                           */    
512     if (glXGetCurrentContext() == NULL) {
513       fake_ctx = WineD3DCreateFakeGLContext();
514       if (NULL != fake_ctx) gotContext = TRUE;
515     } else {
516       gotContext = TRUE;
517     }
518
519     if (gotContext == FALSE) {
520
521         FIXME_(d3d_caps)("GetDeviceCaps called but no GL Context - Returning dummy values\n");
522         gl_tex_size=65535;
523         pCaps->MaxTextureBlendStages = 2;
524         pCaps->MaxSimultaneousTextures = 2;
525         pCaps->MaxUserClipPlanes = 8;
526         pCaps->MaxActiveLights = 8;
527         pCaps->MaxVertexBlendMatrices = 0;
528         pCaps->MaxVertexBlendMatrixIndex = 1;
529         pCaps->MaxAnisotropy = 0;
530         pCaps->MaxPointSize = 255.0;
531     } else {
532         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_tex_size);
533     }
534
535     /* If we don't know the device settings, go query them now */
536     if (This->isGLInfoValid == FALSE) IDirect3D8Impl_FillGLCaps(iface, NULL);
537
538     pCaps->DeviceType = (DeviceType == D3DDEVTYPE_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;  /* Not quite true, but use h/w supported by opengl I suppose */
539     pCaps->AdapterOrdinal = Adapter;
540
541     pCaps->Caps = 0;
542     pCaps->Caps2 = D3DCAPS2_CANRENDERWINDOWED;
543     pCaps->Caps3 = D3DDEVCAPS_HWTRANSFORMANDLIGHT;
544     pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE;
545
546     pCaps->CursorCaps = 0;
547
548     pCaps->DevCaps = D3DDEVCAPS_DRAWPRIMTLVERTEX    | 
549                      D3DDEVCAPS_HWTRANSFORMANDLIGHT |
550                      D3DDEVCAPS_PUREDEVICE;
551
552     pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_CULLCCW               | 
553                                D3DPMISCCAPS_CULLCW                | 
554                                D3DPMISCCAPS_COLORWRITEENABLE      |
555                                D3DPMISCCAPS_CLIPTLVERTS           |
556                                D3DPMISCCAPS_CLIPPLANESCALEDPOINTS | 
557                                D3DPMISCCAPS_MASKZ; 
558                                /*NOT: D3DPMISCCAPS_TSSARGTEMP*/
559
560     pCaps->RasterCaps = D3DPRASTERCAPS_DITHER   | 
561                         D3DPRASTERCAPS_PAT      | 
562                         D3DPRASTERCAPS_WFOG |
563                         D3DPRASTERCAPS_ZFOG |
564                         D3DPRASTERCAPS_FOGVERTEX |
565                         D3DPRASTERCAPS_FOGTABLE  |
566                         D3DPRASTERCAPS_FOGRANGE;
567
568     if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC)) {
569       pCaps->RasterCaps |= D3DPRASTERCAPS_ANISOTROPY;
570     }
571                         /* FIXME Add:
572                            D3DPRASTERCAPS_MIPMAPLODBIAS
573                            D3DPRASTERCAPS_ZBIAS
574                            D3DPRASTERCAPS_COLORPERSPECTIVE
575                            D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE
576                            D3DPRASTERCAPS_ANTIALIASEDGES
577                            D3DPRASTERCAPS_ZBUFFERLESSHSR
578                            D3DPRASTERCAPS_WBUFFER */
579
580     pCaps->ZCmpCaps = D3DPCMPCAPS_ALWAYS       | 
581                       D3DPCMPCAPS_EQUAL        | 
582                       D3DPCMPCAPS_GREATER      | 
583                       D3DPCMPCAPS_GREATEREQUAL |
584                       D3DPCMPCAPS_LESS         | 
585                       D3DPCMPCAPS_LESSEQUAL    | 
586                       D3DPCMPCAPS_NEVER        |
587                       D3DPCMPCAPS_NOTEQUAL;
588
589     pCaps->SrcBlendCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
590     pCaps->DestBlendCaps = 0xFFFFFFFF;   /*FIXME: Tidy up later */
591     pCaps->AlphaCmpCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
592
593     pCaps->ShadeCaps = D3DPSHADECAPS_SPECULARGOURAUDRGB | 
594                        D3DPSHADECAPS_COLORGOURAUDRGB;
595
596     pCaps->TextureCaps =  D3DPTEXTURECAPS_ALPHA        | 
597                           D3DPTEXTURECAPS_ALPHAPALETTE | 
598                           D3DPTEXTURECAPS_POW2         | 
599                           D3DPTEXTURECAPS_VOLUMEMAP    | 
600                           D3DPTEXTURECAPS_MIPMAP;
601
602     if (GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
603       pCaps->TextureCaps |= D3DPTEXTURECAPS_CUBEMAP      | 
604                             D3DPTEXTURECAPS_MIPCUBEMAP   | 
605                             D3DPTEXTURECAPS_CUBEMAP_POW2;
606     }
607
608     pCaps->TextureFilterCaps = D3DPTFILTERCAPS_MAGFLINEAR | 
609                                D3DPTFILTERCAPS_MAGFPOINT  | 
610                                D3DPTFILTERCAPS_MINFLINEAR | 
611                                D3DPTFILTERCAPS_MINFPOINT  |
612                                D3DPTFILTERCAPS_MIPFLINEAR | 
613                                D3DPTFILTERCAPS_MIPFPOINT;
614
615     pCaps->CubeTextureFilterCaps = 0;
616     pCaps->VolumeTextureFilterCaps = 0;
617
618     pCaps->TextureAddressCaps =  D3DPTADDRESSCAPS_BORDER | 
619                                  D3DPTADDRESSCAPS_CLAMP  | 
620                                  D3DPTADDRESSCAPS_WRAP;
621
622     if (GL_SUPPORT(ARB_TEXTURE_BORDER_CLAMP)) {
623       pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_BORDER;
624     }
625     if (GL_SUPPORT(ARB_TEXTURE_MIRRORED_REPEAT)) {
626       pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRROR;
627     }
628     if (GL_SUPPORT(ATI_TEXTURE_MIRROR_ONCE)) {
629       pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRRORONCE;
630     }
631
632     pCaps->VolumeTextureAddressCaps = 0;
633
634     pCaps->LineCaps = D3DLINECAPS_TEXTURE | 
635                       D3DLINECAPS_ZTEST;
636                       /* FIXME: Add 
637                          D3DLINECAPS_BLEND
638                          D3DLINECAPS_ALPHACMP
639                          D3DLINECAPS_FOG */
640
641     pCaps->MaxTextureWidth = gl_tex_size;
642     pCaps->MaxTextureHeight = gl_tex_size;
643
644     pCaps->MaxVolumeExtent = 0;
645
646     pCaps->MaxTextureRepeat = 32768;
647     pCaps->MaxTextureAspectRatio = 32768;
648     pCaps->MaxVertexW = 1.0;
649
650     pCaps->GuardBandLeft = 0;
651     pCaps->GuardBandTop = 0;
652     pCaps->GuardBandRight = 0;
653     pCaps->GuardBandBottom = 0;
654
655     pCaps->ExtentsAdjust = 0;
656
657     pCaps->StencilCaps =  D3DSTENCILCAPS_DECRSAT | 
658                           D3DSTENCILCAPS_INCRSAT | 
659                           D3DSTENCILCAPS_INVERT  | 
660                           D3DSTENCILCAPS_KEEP    | 
661                           D3DSTENCILCAPS_REPLACE | 
662                           D3DSTENCILCAPS_ZERO;
663 #if defined(GL_VERSION_1_4) || defined(GL_EXT_stencil_wrap)
664     pCaps->StencilCaps |= D3DSTENCILCAPS_DECR    | 
665                           D3DSTENCILCAPS_INCR;
666 #endif
667
668     pCaps->FVFCaps = D3DFVFCAPS_PSIZE | 0x80000;
669
670     pCaps->TextureOpCaps =  D3DTEXOPCAPS_ADD         | 
671                             D3DTEXOPCAPS_ADDSIGNED   | 
672                             D3DTEXOPCAPS_ADDSIGNED2X |
673                             D3DTEXOPCAPS_MODULATE    | 
674                             D3DTEXOPCAPS_MODULATE2X  | 
675                             D3DTEXOPCAPS_MODULATE4X  |
676                             D3DTEXOPCAPS_SELECTARG1  | 
677                             D3DTEXOPCAPS_SELECTARG2  | 
678                             D3DTEXOPCAPS_DISABLE;
679 #if defined(GL_VERSION_1_3)
680     pCaps->TextureOpCaps |= D3DTEXOPCAPS_DOTPRODUCT3 | 
681                             D3DTEXOPCAPS_SUBTRACT;
682 #endif
683     if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE) || 
684         GL_SUPPORT(EXT_TEXTURE_ENV_COMBINE) || 
685         GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
686       pCaps->TextureOpCaps |= D3DTEXOPCAPS_BLENDDIFFUSEALPHA |
687                               D3DTEXOPCAPS_BLENDTEXTUREALPHA | 
688                               D3DTEXOPCAPS_BLENDFACTORALPHA  |
689                               D3DTEXOPCAPS_BLENDCURRENTALPHA |
690                               D3DTEXOPCAPS_LERP;
691     }
692     if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
693       pCaps->TextureOpCaps |= D3DTEXOPCAPS_ADDSMOOTH | 
694                               D3DTEXOPCAPS_MULTIPLYADD |
695                               D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
696                               D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
697                               D3DTEXOPCAPS_BLENDTEXTUREALPHAPM;
698     }
699                             /* FIXME: Add 
700                               D3DTEXOPCAPS_BUMPENVMAP
701                               D3DTEXOPCAPS_BUMPENVMAPLUMINANCE 
702                               D3DTEXOPCAPS_PREMODULATE */
703
704     if (gotContext) {
705         GLint gl_max;
706         GLfloat gl_float;
707 #if defined(GL_VERSION_1_3)
708         glGetIntegerv(GL_MAX_TEXTURE_UNITS, &gl_max);
709 #else
710         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
711 #endif
712         TRACE_(d3d_caps)("GLCaps: GL_MAX_TEXTURE_UNITS_ARB=%d\n", gl_max);
713         pCaps->MaxTextureBlendStages = min(8, gl_max);
714         pCaps->MaxSimultaneousTextures = min(8, gl_max);
715
716         glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
717         pCaps->MaxUserClipPlanes = min(MAX_CLIPPLANES, gl_max);
718         TRACE_(d3d_caps)("GLCaps: GL_MAX_CLIP_PLANES=%ld\n", pCaps->MaxUserClipPlanes);
719
720         glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
721         pCaps->MaxActiveLights = gl_max;
722         TRACE_(d3d_caps)("GLCaps: GL_MAX_LIGHTS=%ld\n", pCaps->MaxActiveLights);
723
724         if (GL_SUPPORT(ARB_VERTEX_BLEND)) {
725            glGetIntegerv(GL_MAX_VERTEX_UNITS_ARB, &gl_max);
726            pCaps->MaxVertexBlendMatrices = gl_max;
727            pCaps->MaxVertexBlendMatrixIndex = 1;
728         } else {
729            pCaps->MaxVertexBlendMatrices = 0;
730            pCaps->MaxVertexBlendMatrixIndex = 1;
731         }
732
733         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC)) {
734           glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_max);
735           checkGLcall("glGetInterv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)");
736           pCaps->MaxAnisotropy = gl_max;
737         } else {
738           pCaps->MaxAnisotropy = 0;
739         }
740
741         glGetFloatv(GL_POINT_SIZE_RANGE, &gl_float);
742         pCaps->MaxPointSize = gl_float;
743     }
744
745     pCaps->VertexProcessingCaps = D3DVTXPCAPS_DIRECTIONALLIGHTS | 
746                                   D3DVTXPCAPS_MATERIALSOURCE7   | 
747                                   D3DVTXPCAPS_POSITIONALLIGHTS  | 
748                                   D3DVTXPCAPS_TEXGEN;
749                                   /* FIXME: Add 
750                                      D3DVTXPCAPS_LOCALVIEWER 
751                                      D3DVTXPCAPS_TWEENING */
752
753     pCaps->MaxPrimitiveCount = 0xFFFFFFFF;
754     pCaps->MaxVertexIndex = 0xFFFFFFFF;
755     pCaps->MaxStreams = MAX_STREAMS;
756     pCaps->MaxStreamStride = 1024;
757
758     if (((vs_mode == VS_HW) && GL_SUPPORT(ARB_VERTEX_PROGRAM)) || (vs_mode == VS_SW) || (DeviceType == D3DDEVTYPE_REF))
759         pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
760     else
761         pCaps->VertexShaderVersion = 0;
762     pCaps->MaxVertexShaderConst = D3D8_VSHADER_MAX_CONSTANTS;
763
764 #if 0
765     pCaps->PixelShaderVersion = D3DPS_VERSION(1,1);
766     pCaps->MaxPixelShaderValue = 1.0;
767 #else
768     pCaps->PixelShaderVersion = 0;
769     pCaps->MaxPixelShaderValue = 0.0;
770 #endif
771
772     /* If we created a dummy context, throw it away */
773     WineD3DReleaseFakeGLContext(fake_ctx);
774     return D3D_OK;
775 }
776
777 HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
778     ICOM_THIS(IDirect3D8Impl,iface);
779     FIXME_(d3d_caps)("(%p)->(Adptr:%d)\n", This, Adapter);
780     return D3D_OK;
781 }
782
783
784 static void IDirect3D8Impl_FillGLCaps(LPDIRECT3D8 iface, Display* display) {
785     const char *GL_Extensions = NULL;
786     const char *GLX_Extensions = NULL;
787     GLint gl_max;
788     const char* gl_string = NULL;
789     const char* gl_string_cursor = NULL;
790     Bool test = 0;
791     int major, minor;
792     ICOM_THIS(IDirect3D8Impl,iface);
793
794     if (This->gl_info.bIsFilled) return ;
795     This->gl_info.bIsFilled = 1;
796
797     TRACE_(d3d_caps)("(%p, %p)\n", This, display);
798
799     if (NULL != display) {
800       test = glXQueryVersion(NULL, &major, &minor);
801       This->gl_info.glx_version = ((major & 0x0000FFFF) << 16) | (minor & 0x0000FFFF);
802       gl_string = glXGetClientString(NULL, GLX_VENDOR);
803     } else {
804       gl_string = glGetString(GL_VENDOR);
805     }
806     
807     if (strstr(gl_string, "NVIDIA")) {
808       This->gl_info.gl_vendor = VENDOR_NVIDIA;
809     } else if (strstr(gl_string, "ATI")) {
810       This->gl_info.gl_vendor = VENDOR_ATI;
811     } else {
812       This->gl_info.gl_vendor = VENDOR_WINE;
813     }
814    
815     TRACE_(d3d_caps)("found GL_VENDOR (%s)->(0x%04x)\n", debugstr_a(gl_string), This->gl_info.gl_vendor);
816     
817     gl_string = glGetString(GL_VERSION);
818     switch (This->gl_info.gl_vendor) {
819     case VENDOR_NVIDIA:
820       gl_string_cursor = strstr(gl_string, "NVIDIA");
821       gl_string_cursor = strstr(gl_string_cursor, " ");
822       while (*gl_string_cursor && ' ' == *gl_string_cursor) ++gl_string_cursor;
823       if (*gl_string_cursor) {
824         char tmp[16];
825         int cursor = 0;
826
827         while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
828           tmp[cursor++] = *gl_string_cursor;
829           ++gl_string_cursor;
830         }
831         tmp[cursor] = 0;
832         major = atoi(tmp);
833         
834         if (*gl_string_cursor != '.') WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
835         ++gl_string_cursor;
836
837         while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
838           tmp[cursor++] = *gl_string_cursor;
839           ++gl_string_cursor;
840         }
841         tmp[cursor] = 0;
842         minor = atoi(tmp);
843         break;
844       }
845     case VENDOR_ATI:
846     default:
847       major = 0;
848       minor = 9;
849     }
850     This->gl_info.gl_driver_version = MAKEDWORD_VERSION(major, minor);
851
852     gl_string = glGetString(GL_RENDERER);
853     strcpy(This->gl_info.gl_renderer, gl_string);
854
855     switch (This->gl_info.gl_vendor) {
856     case VENDOR_NVIDIA:
857       if (strstr(This->gl_info.gl_renderer, "GeForce4 Ti")) {
858         This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
859       } else if (strstr(This->gl_info.gl_renderer, "GeForceFX")) {
860         This->gl_info.gl_card = CARD_NVIDIA_GEFORCEFX_5900ULTRA;
861       } else {
862         This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
863       }
864       break;
865     case VENDOR_ATI:
866       This->gl_info.gl_card = CARD_ATI_RADEON_8500;
867       break;
868     default:
869       This->gl_info.gl_card = CARD_WINE;
870       break;
871     }
872
873     FIXME_(d3d_caps)("found GL_VERSION  (0x%08lx)\n", This->gl_info.gl_driver_version);
874     FIXME_(d3d_caps)("found GL_RENDERER (%s)->(0x%04x)\n", debugstr_a(This->gl_info.gl_renderer), This->gl_info.gl_card);
875     /* 
876      * Initialize openGL extension related variables
877      *  with Default values 
878      */
879     memset(&This->gl_info.supported, 0, sizeof(This->gl_info.supported));
880     This->gl_info.max_textures   = 1;
881     This->gl_info.ps_arb_version = PS_VERSION_NOT_SUPPORTED;
882     This->gl_info.vs_arb_version = VS_VERSION_NOT_SUPPORTED;
883     This->gl_info.vs_nv_version  = VS_VERSION_NOT_SUPPORTED;
884     This->gl_info.vs_ati_version = VS_VERSION_NOT_SUPPORTED;
885
886 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = NULL;
887     GL_EXT_FUNCS_GEN;
888 #undef USE_GL_FUNC
889
890     /* Retrieve opengl defaults */
891     glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
892     This->gl_info.max_clipplanes = min(MAX_CLIPPLANES, gl_max);
893     TRACE_(d3d_caps)("ClipPlanes support - num Planes=%d\n", gl_max);
894
895     glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
896     This->gl_info.max_lights = gl_max;
897     TRACE_(d3d_caps)("Lights support - max lights=%d\n", gl_max);
898
899     /* Parse the gl supported features, in theory enabling parts of our code appropriately */
900     GL_Extensions = glGetString(GL_EXTENSIONS);
901     TRACE_(d3d_caps)("GL_Extensions reported:\n");  
902     
903     if (NULL == GL_Extensions) {
904       ERR("   GL_Extensions returns NULL\n");      
905     } else {
906       while (*GL_Extensions != 0x00) {
907         const char *Start = GL_Extensions;
908         char ThisExtn[256];
909
910         memset(ThisExtn, 0x00, sizeof(ThisExtn));
911         while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
912           GL_Extensions++;
913         }
914         memcpy(ThisExtn, Start, (GL_Extensions - Start));
915         TRACE_(d3d_caps)("- %s\n", ThisExtn);
916
917         /**
918          * ARB 
919          */
920         if (strcmp(ThisExtn, "GL_ARB_fragment_program") == 0) {
921           This->gl_info.ps_arb_version = PS_VERSION_11;
922           TRACE_(d3d_caps)(" FOUND: ARB Pixel Shader support - version=%02x\n", This->gl_info.ps_arb_version);
923           This->gl_info.supported[ARB_FRAGMENT_PROGRAM] = TRUE;
924         } else if (strcmp(ThisExtn, "GL_ARB_multisample") == 0) {
925           TRACE_(d3d_caps)(" FOUND: ARB Multisample support\n");
926           This->gl_info.supported[ARB_MULTISAMPLE] = TRUE;
927         } else if (strcmp(ThisExtn, "GL_ARB_multitexture") == 0) {
928           glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
929           TRACE_(d3d_caps)(" FOUND: ARB Multitexture support - GL_MAX_TEXTURE_UNITS_ARB=%u\n", gl_max);
930           This->gl_info.supported[ARB_MULTITEXTURE] = TRUE;
931           This->gl_info.max_textures = min(8, gl_max);
932         } else if (strcmp(ThisExtn, "GL_ARB_texture_cube_map") == 0) {
933           TRACE_(d3d_caps)(" FOUND: ARB Texture Cube Map support\n");
934           This->gl_info.supported[ARB_TEXTURE_CUBE_MAP] = TRUE;
935         } else if (strcmp(ThisExtn, "GL_ARB_texture_compression") == 0) {
936           TRACE_(d3d_caps)(" FOUND: ARB Texture Compression support\n");
937           This->gl_info.supported[ARB_TEXTURE_COMPRESSION] = TRUE;
938         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_add") == 0) {
939           TRACE_(d3d_caps)(" FOUND: ARB Texture Env Add support\n");
940           This->gl_info.supported[ARB_TEXTURE_ENV_ADD] = TRUE;
941         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_combine") == 0) {
942           TRACE_(d3d_caps)(" FOUND: ARB Texture Env combine support\n");
943           This->gl_info.supported[ARB_TEXTURE_ENV_COMBINE] = TRUE;
944         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_dot3") == 0) {
945           TRACE_(d3d_caps)(" FOUND: ARB Dot3 support\n");
946           This->gl_info.supported[ARB_TEXTURE_ENV_DOT3] = TRUE;
947         } else if (strcmp(ThisExtn, "GL_ARB_texture_border_clamp") == 0) {
948           TRACE_(d3d_caps)(" FOUND: ARB Texture border clamp support\n");
949           This->gl_info.supported[ARB_TEXTURE_BORDER_CLAMP] = TRUE;
950         } else if (strcmp(ThisExtn, "GL_ARB_texture_mirrored_repeat") == 0) {
951           TRACE_(d3d_caps)(" FOUND: ARB Texture mirrored repeat support\n");
952           This->gl_info.supported[ARB_TEXTURE_MIRRORED_REPEAT] = TRUE;
953         } else if (strstr(ThisExtn, "GL_ARB_vertex_program")) {
954           This->gl_info.vs_arb_version = VS_VERSION_11;
955           TRACE_(d3d_caps)(" FOUND: ARB Vertex Shader support - version=%02x\n", This->gl_info.vs_arb_version);
956           This->gl_info.supported[ARB_VERTEX_PROGRAM] = TRUE;
957
958         /**
959          * EXT
960          */
961         } else if (strcmp(ThisExtn, "GL_EXT_fog_coord") == 0) {
962           TRACE_(d3d_caps)(" FOUND: EXT Fog coord support\n");
963           This->gl_info.supported[EXT_FOG_COORD] = TRUE;
964         } else if (strcmp(ThisExtn, "GL_EXT_paletted_texture") == 0) { /* handle paletted texture extensions */
965           TRACE_(d3d_caps)(" FOUND: EXT Paletted texture support\n");
966           This->gl_info.supported[EXT_PALETTED_TEXTURE] = TRUE;
967         } else if (strcmp(ThisExtn, "GL_EXT_point_parameters") == 0) {
968           TRACE_(d3d_caps)(" FOUND: EXT Point parameters support\n");
969           This->gl_info.supported[EXT_POINT_PARAMETERS] = TRUE;
970         } else if (strcmp(ThisExtn, "GL_EXT_secondary_color") == 0) {
971           TRACE_(d3d_caps)(" FOUND: EXT Secondary coord support\n");
972           This->gl_info.supported[EXT_SECONDARY_COLOR] = TRUE;
973         } else if (strcmp(ThisExtn, "GL_EXT_texture_compression_s3tc") == 0) {
974           TRACE_(d3d_caps)(" FOUND: EXT Texture S3TC compression support\n");
975           This->gl_info.supported[EXT_TEXTURE_COMPRESSION_S3TC] = TRUE;
976         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_add") == 0) {
977           TRACE_(d3d_caps)(" FOUND: EXT Texture Env Add support\n");
978           This->gl_info.supported[EXT_TEXTURE_ENV_ADD] = TRUE;
979         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_combine") == 0) {
980           TRACE_(d3d_caps)(" FOUND: EXT Texture Env combine support\n");
981           This->gl_info.supported[EXT_TEXTURE_ENV_COMBINE] = TRUE;
982         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_dot3") == 0) {
983           TRACE_(d3d_caps)(" FOUND: EXT Dot3 support\n");
984           This->gl_info.supported[EXT_TEXTURE_ENV_DOT3] = TRUE;
985         } else if (strcmp(ThisExtn, "GL_EXT_texture_filter_anisotropic") == 0) {
986           TRACE_(d3d_caps)(" FOUND: EXT Texture Anisotropic filter support\n");
987           This->gl_info.supported[EXT_TEXTURE_FILTER_ANISOTROPIC] = TRUE;
988         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod") == 0) {
989           TRACE_(d3d_caps)(" FOUND: EXT Texture LOD support\n");
990           This->gl_info.supported[EXT_TEXTURE_LOD] = TRUE;
991         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod_bias") == 0) {
992           TRACE_(d3d_caps)(" FOUND: EXT Texture LOD bias support\n");
993           This->gl_info.supported[EXT_TEXTURE_LOD_BIAS] = TRUE;
994         } else if (strcmp(ThisExtn, "GL_EXT_vertex_weighting") == 0) {
995           TRACE_(d3d_caps)(" FOUND: EXT Vertex weighting support\n");
996           This->gl_info.supported[EXT_VERTEX_WEIGHTING] = TRUE;
997
998         /**
999          * NVIDIA 
1000          */
1001         } else if (strcmp(ThisExtn, "GL_NV_texture_env_combine4") == 0) {
1002           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Env combine (4) support\n");
1003           This->gl_info.supported[NV_TEXTURE_ENV_COMBINE4] = TRUE;
1004         } else if (strstr(ThisExtn, "GL_NV_fragment_program")) {
1005           This->gl_info.ps_nv_version = PS_VERSION_11;
1006           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Pixel Shader support - version=%02x\n", This->gl_info.ps_nv_version);
1007         } else if (strstr(ThisExtn, "GL_NV_fog_distance")) {
1008           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Fog Distance support\n");
1009           This->gl_info.supported[NV_FOG_DISTANCE] = TRUE;
1010         } else if (strstr(ThisExtn, "GL_NV_vertex_program")) {
1011           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);
1012           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);
1013           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Vertex Shader support - version=%02x\n", This->gl_info.vs_nv_version);
1014           This->gl_info.supported[NV_VERTEX_PROGRAM] = TRUE;
1015
1016         /**
1017          * ATI
1018          */
1019         /** TODO */
1020         } else if (strcmp(ThisExtn, "GL_ATI_texture_env_combine3") == 0) {
1021           TRACE_(d3d_caps)(" FOUND: ATI Texture Env combine (3) support\n");
1022           This->gl_info.supported[ATI_TEXTURE_ENV_COMBINE3] = TRUE;
1023         } else if (strcmp(ThisExtn, "GL_ATI_texture_mirror_once") == 0) {
1024           TRACE_(d3d_caps)(" FOUND: ATI Texture Mirror Once support\n");
1025           This->gl_info.supported[ATI_TEXTURE_MIRROR_ONCE] = TRUE;
1026         } else if (strcmp(ThisExtn, "GL_EXT_vertex_shader") == 0) {
1027           This->gl_info.vs_ati_version = VS_VERSION_11;
1028           TRACE_(d3d_caps)(" FOUND: ATI (EXT) Vertex Shader support - version=%02x\n", This->gl_info.vs_ati_version);
1029           This->gl_info.supported[EXT_VERTEX_SHADER] = TRUE;
1030         }
1031
1032
1033         if (*GL_Extensions == ' ') GL_Extensions++;
1034       }
1035     }
1036
1037 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
1038     GL_EXT_FUNCS_GEN;
1039 #undef USE_GL_FUNC
1040
1041     if (display != NULL) {
1042         GLX_Extensions = glXQueryExtensionsString(display, DefaultScreen(display));
1043         TRACE_(d3d_caps)("GLX_Extensions reported:\n");  
1044     
1045         if (NULL == GLX_Extensions) {
1046           ERR("   GLX_Extensions returns NULL\n");      
1047         } else {
1048           while (*GLX_Extensions != 0x00) {
1049             const char *Start = GLX_Extensions;
1050             char ThisExtn[256];
1051            
1052             memset(ThisExtn, 0x00, sizeof(ThisExtn));
1053             while (*GLX_Extensions != ' ' && *GLX_Extensions != 0x00) {
1054               GLX_Extensions++;
1055             }
1056             memcpy(ThisExtn, Start, (GLX_Extensions - Start));
1057             TRACE_(d3d_caps)("- %s\n", ThisExtn);
1058             if (*GLX_Extensions == ' ') GLX_Extensions++;
1059           }
1060         }
1061     }
1062
1063 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
1064     GLX_EXT_FUNCS_GEN;
1065 #undef USE_GL_FUNC
1066
1067     /* Only save the values obtained when a display is provided */
1068     if (display != NULL) This->isGLInfoValid = TRUE;
1069
1070 }
1071
1072 HRESULT  WINAPI  IDirect3D8Impl_CreateDevice               (LPDIRECT3D8 iface,
1073                                                             UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
1074                                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
1075                                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
1076     IDirect3DDevice8Impl *object;
1077     HWND whichHWND;
1078     int num;
1079     XVisualInfo template;
1080     HDC hDc;
1081
1082     ICOM_THIS(IDirect3D8Impl,iface);
1083     TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
1084           hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);
1085
1086     /* Allocate the storage for the device */
1087     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
1088     if (NULL == object) {
1089       return D3DERR_OUTOFVIDEOMEMORY;
1090     }
1091     object->lpVtbl = &Direct3DDevice8_Vtbl;
1092     object->ref = 1;
1093     object->direct3d8 = This;
1094     /** The device AddRef the direct3d8 Interface else crash in propers clients codes */
1095     IDirect3D8_AddRef((LPDIRECT3D8) object->direct3d8);
1096
1097     /** use StateBlock Factory here, for creating the startup stateBlock */
1098     object->StateBlock = NULL;
1099     IDirect3DDeviceImpl_CreateStateBlock(object, D3DSBT_ALL, NULL);
1100     object->UpdateStateBlock = object->StateBlock;
1101
1102     /* Save the creation parameters */
1103     object->CreateParms.AdapterOrdinal = Adapter;
1104     object->CreateParms.DeviceType = DeviceType;
1105     object->CreateParms.hFocusWindow = hFocusWindow;
1106     object->CreateParms.BehaviorFlags = BehaviourFlags;
1107
1108     *ppReturnedDeviceInterface = (LPDIRECT3DDEVICE8) object;
1109
1110     /* Initialize settings */
1111     object->PresentParms.BackBufferCount = 1; /* Opengl only supports one? */
1112     object->adapterNo = Adapter;
1113     object->devType = DeviceType;
1114
1115     /* Initialize openGl - Note the visual is chosen as the window is created and the glcontext cannot
1116          use different properties after that point in time. FIXME: How to handle when requested format 
1117          doesn't match actual visual? Cannot choose one here - code removed as it ONLY works if the one
1118          it chooses is identical to the one already being used!                                        */
1119     /* FIXME: Handle stencil appropriately via EnableAutoDepthStencil / AutoDepthStencilFormat */
1120
1121     /* Which hwnd are we using? */
1122     whichHWND = pPresentationParameters->hDeviceWindow;
1123     if (!whichHWND) {
1124         whichHWND = hFocusWindow;
1125     }
1126     object->win_handle = whichHWND;
1127     object->win     = (Window)GetPropA( whichHWND, "__wine_x11_client_window" );
1128
1129     hDc = GetDC(whichHWND);
1130     object->display = get_display(hDc);
1131
1132     TRACE("(%p)->(DepthStencil:(%u,%s), BackBufferFormat:(%u,%s))\n", This, 
1133           pPresentationParameters->AutoDepthStencilFormat, debug_d3dformat(pPresentationParameters->AutoDepthStencilFormat),
1134           pPresentationParameters->BackBufferFormat, debug_d3dformat(pPresentationParameters->BackBufferFormat));
1135
1136     ENTER_GL();
1137
1138     /* Create a context based off the properties of the existing visual */
1139     template.visualid = (VisualID)GetPropA(GetDesktopWindow(), "__wine_x11_visual_id");
1140     object->visInfo = XGetVisualInfo(object->display, VisualIDMask, &template, &num);
1141     if (NULL == object->visInfo) {
1142         ERR("cannot really get XVisual\n"); 
1143         LEAVE_GL();
1144         return D3DERR_NOTAVAILABLE;
1145      }
1146     object->glCtx = glXCreateContext(object->display, object->visInfo, NULL, GL_TRUE);
1147     if (NULL == object->glCtx) {
1148       ERR("cannot create glxContext\n"); 
1149       LEAVE_GL();
1150       return D3DERR_NOTAVAILABLE;
1151      }
1152     LEAVE_GL();
1153
1154     ReleaseDC(whichHWND, hDc);
1155     
1156     if (object->glCtx == NULL) {
1157         ERR("Error in context creation !\n");
1158         return D3DERR_INVALIDCALL;
1159     } else {
1160         TRACE("Context created (HWND=%p, glContext=%p, Window=%ld, VisInfo=%p)\n",
1161               whichHWND, object->glCtx, object->win, object->visInfo);
1162     }
1163
1164     /* If not windowed, need to go fullscreen, and resize the HWND to the appropriate  */
1165     /*        dimensions                                                               */
1166     if (!pPresentationParameters->Windowed) {
1167 #if 1
1168         DEVMODEW devmode;
1169         HDC hdc;
1170         int bpp = 0;
1171         memset(&devmode, 0, sizeof(DEVMODEW));
1172         devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; 
1173         MultiByteToWideChar(CP_ACP, 0, "Gamers CG", -1, devmode.dmDeviceName, CCHDEVICENAME);
1174         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
1175         bpp = GetDeviceCaps(hdc, BITSPIXEL);
1176         DeleteDC(hdc);
1177         devmode.dmBitsPerPel = (bpp >= 24) ? 32 : bpp;/*Stupid XVidMode cannot change bpp D3DFmtGetBpp(object, pPresentationParameters->BackBufferFormat);*/
1178         devmode.dmPelsWidth  = pPresentationParameters->BackBufferWidth;
1179         devmode.dmPelsHeight = pPresentationParameters->BackBufferHeight;
1180         ChangeDisplaySettingsExW(devmode.dmDeviceName, &devmode, object->win_handle, CDS_FULLSCREEN, NULL);
1181 #else
1182         FIXME("Requested full screen support not implemented, expect windowed operation\n");
1183 #endif
1184
1185         /* Make popup window */
1186         ShowWindow(whichHWND, SW_HIDE);
1187         SetWindowLongA(whichHWND, GWL_STYLE, WS_POPUP);
1188         SetWindowPos(object->win_handle, HWND_TOP, 0, 0, 
1189                      pPresentationParameters->BackBufferWidth,
1190                      pPresentationParameters->BackBufferHeight, SWP_SHOWWINDOW | SWP_FRAMECHANGED);
1191         ShowWindow(whichHWND, SW_SHOW);
1192     }
1193
1194     TRACE("Creating back buffer\n");
1195     /* MSDN: If Windowed is TRUE and either of the BackBufferWidth/Height values is zero,
1196        then the corresponding dimension of the client area of the hDeviceWindow
1197        (or the focus window, if hDeviceWindow is NULL) is taken. */
1198     if (pPresentationParameters->Windowed && ((pPresentationParameters->BackBufferWidth  == 0) ||
1199                                               (pPresentationParameters->BackBufferHeight == 0))) {
1200         RECT Rect;
1201
1202         GetClientRect(whichHWND, &Rect);
1203
1204         if (pPresentationParameters->BackBufferWidth == 0) {
1205            pPresentationParameters->BackBufferWidth = Rect.right;
1206            TRACE("Updating width to %d\n", pPresentationParameters->BackBufferWidth);
1207         }
1208         if (pPresentationParameters->BackBufferHeight == 0) {
1209            pPresentationParameters->BackBufferHeight = Rect.bottom;
1210            TRACE("Updating height to %d\n", pPresentationParameters->BackBufferHeight);
1211         }
1212     }
1213
1214     /* Save the presentation parms now filled in correctly */
1215     memcpy(&object->PresentParms, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
1216
1217
1218     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
1219                                             pPresentationParameters->BackBufferWidth,
1220                                             pPresentationParameters->BackBufferHeight,
1221                                             pPresentationParameters->BackBufferFormat,
1222                                             pPresentationParameters->MultiSampleType,
1223                                             TRUE,
1224                                             (LPDIRECT3DSURFACE8*) &object->frontBuffer);
1225
1226     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
1227                                             pPresentationParameters->BackBufferWidth,
1228                                             pPresentationParameters->BackBufferHeight,
1229                                             pPresentationParameters->BackBufferFormat,
1230                                             pPresentationParameters->MultiSampleType,
1231                                             TRUE,
1232                                             (LPDIRECT3DSURFACE8*) &object->backBuffer);
1233
1234     if (pPresentationParameters->EnableAutoDepthStencil) {
1235        IDirect3DDevice8Impl_CreateDepthStencilSurface((LPDIRECT3DDEVICE8) object,
1236                                                       pPresentationParameters->BackBufferWidth,
1237                                                       pPresentationParameters->BackBufferHeight,
1238                                                       pPresentationParameters->AutoDepthStencilFormat,
1239                                                       D3DMULTISAMPLE_NONE,
1240                                                       (LPDIRECT3DSURFACE8*) &object->depthStencilBuffer);
1241     } else {
1242       object->depthStencilBuffer = NULL;
1243     }
1244     TRACE("FrontBuf @ %p, BackBuf @ %p, DepthStencil @ %p\n",object->frontBuffer, object->backBuffer, object->depthStencilBuffer);
1245
1246     /* init the default renderTarget management */
1247     object->drawable = object->win;
1248     object->render_ctx = object->glCtx;
1249     object->renderTarget = object->frontBuffer;
1250     IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->renderTarget);
1251     object->stencilBufferTarget = object->depthStencilBuffer;
1252     if (NULL != object->stencilBufferTarget) {
1253       IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->stencilBufferTarget);
1254     }
1255
1256     ENTER_GL();
1257
1258     if (glXMakeCurrent(object->display, object->win, object->glCtx) == False) {
1259       ERR("Error in setting current context (context %p drawable %ld)!\n", object->glCtx, object->win);
1260     }
1261     checkGLcall("glXMakeCurrent");
1262
1263     /* Clear the screen */
1264     glClearColor(1.0, 0.0, 0.0, 0.0);
1265     checkGLcall("glClearColor");
1266     glColor3f(1.0, 1.0, 1.0);
1267     checkGLcall("glColor3f");
1268
1269     glEnable(GL_LIGHTING);
1270     checkGLcall("glEnable");
1271
1272     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1273     checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1274
1275     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1276     checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1277
1278     glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1279     checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1280
1281     /* 
1282      * Initialize openGL extension related variables
1283      *  with Default values 
1284      */
1285     IDirect3D8Impl_FillGLCaps(iface, object->display);
1286
1287     /* Setup all the devices defaults */
1288     IDirect3DDeviceImpl_InitStartupStateBlock(object);
1289
1290     LEAVE_GL();
1291
1292     { /* Set a default viewport */
1293        D3DVIEWPORT8 vp;
1294        vp.X      = 0;
1295        vp.Y      = 0;
1296        vp.Width  = pPresentationParameters->BackBufferWidth;
1297        vp.Height = pPresentationParameters->BackBufferHeight;
1298        vp.MinZ   = 0.0f;
1299        vp.MaxZ   = 1.0f;
1300        IDirect3DDevice8Impl_SetViewport((LPDIRECT3DDEVICE8) object, &vp);
1301     }
1302
1303     /* Initialize the current view state */
1304     object->modelview_valid = 1;
1305     object->proj_valid = 0;
1306     object->view_ident = 1;
1307     object->last_was_rhw = 0;
1308     glGetIntegerv(GL_MAX_LIGHTS, &object->maxConcurrentLights);
1309     TRACE("(%p,%d) All defaults now set up, leaving CreateDevice with %p\n", This, Adapter, object);
1310     return D3D_OK;
1311 }
1312
1313 ICOM_VTABLE(IDirect3D8) Direct3D8_Vtbl =
1314 {
1315     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1316     IDirect3D8Impl_QueryInterface,
1317     IDirect3D8Impl_AddRef,
1318     IDirect3D8Impl_Release,
1319     IDirect3D8Impl_RegisterSoftwareDevice,
1320     IDirect3D8Impl_GetAdapterCount,
1321     IDirect3D8Impl_GetAdapterIdentifier,
1322     IDirect3D8Impl_GetAdapterModeCount,
1323     IDirect3D8Impl_EnumAdapterModes,
1324     IDirect3D8Impl_GetAdapterDisplayMode,
1325     IDirect3D8Impl_CheckDeviceType,
1326     IDirect3D8Impl_CheckDeviceFormat,
1327     IDirect3D8Impl_CheckDeviceMultiSampleType,
1328     IDirect3D8Impl_CheckDepthStencilMatch,
1329     IDirect3D8Impl_GetDeviceCaps,
1330     IDirect3D8Impl_GetAdapterMonitor,
1331     IDirect3D8Impl_CreateDevice
1332 };