Authors: Christian Costa <titan.costa@wanadoo.fr>, Jason Edmeades <us@the-edmeades...
[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     case D3DFMT_P8:
445     case D3DFMT_A8P8:
446       /* Since we do not support these formats right now, don't pretend to. */
447       return D3DERR_NOTAVAILABLE;
448     default:
449       break;
450     }
451
452     return D3D_OK;
453 }
454
455 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
456                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
457                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
458     ICOM_THIS(IDirect3D8Impl,iface);
459     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%x,%s), SurfFmt:(%x,%s), Win?%d, MultiSamp:%x)\n", 
460           This, 
461           Adapter, 
462           DeviceType, debug_d3ddevicetype(DeviceType),
463           SurfaceFormat, debug_d3dformat(SurfaceFormat),
464           Windowed, 
465           MultiSampleType);
466   
467     if (D3DMULTISAMPLE_NONE == MultiSampleType)
468       return D3D_OK;
469     return D3DERR_NOTAVAILABLE;
470 }
471
472 HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface, 
473                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
474                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
475     ICOM_THIS(IDirect3D8Impl,iface);
476     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%x,%s), AdptFmt:(%x,%s), RendrTgtFmt:(%x,%s), DepthStencilFmt:(%x,%s))\n", 
477           This, 
478           Adapter, 
479           DeviceType, debug_d3ddevicetype(DeviceType),
480           AdapterFormat, debug_d3dformat(AdapterFormat),
481           RenderTargetFormat, debug_d3dformat(RenderTargetFormat), 
482           DepthStencilFormat, debug_d3dformat(DepthStencilFormat));
483
484 #if 0
485     switch (DepthStencilFormat) {
486     case D3DFMT_D24X4S4:
487     case D3DFMT_D24X8: 
488     case D3DFMT_D24S8: 
489     case D3DFMT_D32:
490       /**
491        * as i don't know how to really check hard caps of graphics cards
492        * i prefer to not permit 32bit zbuffers enumeration (as few cards can do it)
493        */
494       return D3DERR_NOTAVAILABLE;
495     default:
496       break;
497     }
498 #endif
499     return D3D_OK;
500 }
501
502 HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
503
504     BOOL        gotContext  = FALSE;
505     GLint       gl_tex_size = 0;    
506     WineD3D_Context* fake_ctx = NULL;
507     ICOM_THIS(IDirect3D8Impl,iface);
508
509     TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType: %x, pCaps: %p)\n", This, Adapter, DeviceType, pCaps);
510
511     /* Note: GL seems to trap if GetDeviceCaps is called before any HWND's created
512        ie there is no GL Context - Get a default rendering context to enable the 
513        function query some info from GL                                           */    
514     if (glXGetCurrentContext() == NULL) {
515       fake_ctx = WineD3DCreateFakeGLContext();
516       if (NULL != fake_ctx) gotContext = TRUE;
517     } else {
518       gotContext = TRUE;
519     }
520
521     if (gotContext == FALSE) {
522
523         FIXME_(d3d_caps)("GetDeviceCaps called but no GL Context - Returning dummy values\n");
524         gl_tex_size=65535;
525         pCaps->MaxTextureBlendStages = 2;
526         pCaps->MaxSimultaneousTextures = 2;
527         pCaps->MaxUserClipPlanes = 8;
528         pCaps->MaxActiveLights = 8;
529         pCaps->MaxVertexBlendMatrices = 0;
530         pCaps->MaxVertexBlendMatrixIndex = 1;
531         pCaps->MaxAnisotropy = 0;
532         pCaps->MaxPointSize = 255.0;
533     } else {
534         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_tex_size);
535     }
536
537     /* If we don't know the device settings, go query them now */
538     if (This->isGLInfoValid == FALSE) IDirect3D8Impl_FillGLCaps(iface, NULL);
539
540     pCaps->DeviceType = (DeviceType == D3DDEVTYPE_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;  /* Not quite true, but use h/w supported by opengl I suppose */
541     pCaps->AdapterOrdinal = Adapter;
542
543     pCaps->Caps = 0;
544     pCaps->Caps2 = D3DCAPS2_CANRENDERWINDOWED;
545     pCaps->Caps3 = D3DDEVCAPS_HWTRANSFORMANDLIGHT;
546     pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE;
547
548     pCaps->CursorCaps = 0;
549
550     pCaps->DevCaps = D3DDEVCAPS_DRAWPRIMTLVERTEX    | 
551                      D3DDEVCAPS_HWTRANSFORMANDLIGHT |
552                      D3DDEVCAPS_PUREDEVICE;
553
554     pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_CULLCCW               | 
555                                D3DPMISCCAPS_CULLCW                | 
556                                D3DPMISCCAPS_COLORWRITEENABLE      |
557                                D3DPMISCCAPS_CLIPTLVERTS           |
558                                D3DPMISCCAPS_CLIPPLANESCALEDPOINTS | 
559                                D3DPMISCCAPS_MASKZ; 
560                                /*NOT: D3DPMISCCAPS_TSSARGTEMP*/
561
562     pCaps->RasterCaps = D3DPRASTERCAPS_DITHER   | 
563                         D3DPRASTERCAPS_PAT      | 
564                         D3DPRASTERCAPS_WFOG |
565                         D3DPRASTERCAPS_ZFOG |
566                         D3DPRASTERCAPS_FOGVERTEX |
567                         D3DPRASTERCAPS_FOGTABLE  |
568                         D3DPRASTERCAPS_FOGRANGE;
569
570     if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC)) {
571       pCaps->RasterCaps |= D3DPRASTERCAPS_ANISOTROPY;
572     }
573                         /* FIXME Add:
574                            D3DPRASTERCAPS_MIPMAPLODBIAS
575                            D3DPRASTERCAPS_ZBIAS
576                            D3DPRASTERCAPS_COLORPERSPECTIVE
577                            D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE
578                            D3DPRASTERCAPS_ANTIALIASEDGES
579                            D3DPRASTERCAPS_ZBUFFERLESSHSR
580                            D3DPRASTERCAPS_WBUFFER */
581
582     pCaps->ZCmpCaps = D3DPCMPCAPS_ALWAYS       | 
583                       D3DPCMPCAPS_EQUAL        | 
584                       D3DPCMPCAPS_GREATER      | 
585                       D3DPCMPCAPS_GREATEREQUAL |
586                       D3DPCMPCAPS_LESS         | 
587                       D3DPCMPCAPS_LESSEQUAL    | 
588                       D3DPCMPCAPS_NEVER        |
589                       D3DPCMPCAPS_NOTEQUAL;
590
591     pCaps->SrcBlendCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
592     pCaps->DestBlendCaps = 0xFFFFFFFF;   /*FIXME: Tidy up later */
593     pCaps->AlphaCmpCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
594
595     pCaps->ShadeCaps = D3DPSHADECAPS_SPECULARGOURAUDRGB | 
596                        D3DPSHADECAPS_COLORGOURAUDRGB;
597
598     pCaps->TextureCaps =  D3DPTEXTURECAPS_ALPHA        | 
599                           D3DPTEXTURECAPS_ALPHAPALETTE | 
600                           D3DPTEXTURECAPS_POW2         | 
601                           D3DPTEXTURECAPS_VOLUMEMAP    | 
602                           D3DPTEXTURECAPS_MIPMAP;
603
604     if (GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
605       pCaps->TextureCaps |= D3DPTEXTURECAPS_CUBEMAP      | 
606                             D3DPTEXTURECAPS_MIPCUBEMAP   | 
607                             D3DPTEXTURECAPS_CUBEMAP_POW2;
608     }
609
610     pCaps->TextureFilterCaps = D3DPTFILTERCAPS_MAGFLINEAR | 
611                                D3DPTFILTERCAPS_MAGFPOINT  | 
612                                D3DPTFILTERCAPS_MINFLINEAR | 
613                                D3DPTFILTERCAPS_MINFPOINT  |
614                                D3DPTFILTERCAPS_MIPFLINEAR | 
615                                D3DPTFILTERCAPS_MIPFPOINT;
616
617     pCaps->CubeTextureFilterCaps = 0;
618     pCaps->VolumeTextureFilterCaps = 0;
619
620     pCaps->TextureAddressCaps =  D3DPTADDRESSCAPS_BORDER | 
621                                  D3DPTADDRESSCAPS_CLAMP  | 
622                                  D3DPTADDRESSCAPS_WRAP;
623
624     if (GL_SUPPORT(ARB_TEXTURE_BORDER_CLAMP)) {
625       pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_BORDER;
626     }
627     if (GL_SUPPORT(ARB_TEXTURE_MIRRORED_REPEAT)) {
628       pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRROR;
629     }
630     if (GL_SUPPORT(ATI_TEXTURE_MIRROR_ONCE)) {
631       pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRRORONCE;
632     }
633
634     pCaps->VolumeTextureAddressCaps = 0;
635
636     pCaps->LineCaps = D3DLINECAPS_TEXTURE | 
637                       D3DLINECAPS_ZTEST;
638                       /* FIXME: Add 
639                          D3DLINECAPS_BLEND
640                          D3DLINECAPS_ALPHACMP
641                          D3DLINECAPS_FOG */
642
643     pCaps->MaxTextureWidth = gl_tex_size;
644     pCaps->MaxTextureHeight = gl_tex_size;
645
646     pCaps->MaxVolumeExtent = 0;
647
648     pCaps->MaxTextureRepeat = 32768;
649     pCaps->MaxTextureAspectRatio = 32768;
650     pCaps->MaxVertexW = 1.0;
651
652     pCaps->GuardBandLeft = 0;
653     pCaps->GuardBandTop = 0;
654     pCaps->GuardBandRight = 0;
655     pCaps->GuardBandBottom = 0;
656
657     pCaps->ExtentsAdjust = 0;
658
659     pCaps->StencilCaps =  D3DSTENCILCAPS_DECRSAT | 
660                           D3DSTENCILCAPS_INCRSAT | 
661                           D3DSTENCILCAPS_INVERT  | 
662                           D3DSTENCILCAPS_KEEP    | 
663                           D3DSTENCILCAPS_REPLACE | 
664                           D3DSTENCILCAPS_ZERO;
665     if (GL_SUPPORT(EXT_STENCIL_WRAP)) {
666       pCaps->StencilCaps |= D3DSTENCILCAPS_DECR    | 
667                             D3DSTENCILCAPS_INCR;
668     }
669
670     pCaps->FVFCaps = D3DFVFCAPS_PSIZE | 0x80000;
671
672     pCaps->TextureOpCaps =  D3DTEXOPCAPS_ADD         | 
673                             D3DTEXOPCAPS_ADDSIGNED   | 
674                             D3DTEXOPCAPS_ADDSIGNED2X |
675                             D3DTEXOPCAPS_MODULATE    | 
676                             D3DTEXOPCAPS_MODULATE2X  | 
677                             D3DTEXOPCAPS_MODULATE4X  |
678                             D3DTEXOPCAPS_SELECTARG1  | 
679                             D3DTEXOPCAPS_SELECTARG2  | 
680                             D3DTEXOPCAPS_DISABLE;
681 #if defined(GL_VERSION_1_3)
682     pCaps->TextureOpCaps |= D3DTEXOPCAPS_DOTPRODUCT3 | 
683                             D3DTEXOPCAPS_SUBTRACT;
684 #endif
685     if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE) || 
686         GL_SUPPORT(EXT_TEXTURE_ENV_COMBINE) || 
687         GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
688       pCaps->TextureOpCaps |= D3DTEXOPCAPS_BLENDDIFFUSEALPHA |
689                               D3DTEXOPCAPS_BLENDTEXTUREALPHA | 
690                               D3DTEXOPCAPS_BLENDFACTORALPHA  |
691                               D3DTEXOPCAPS_BLENDCURRENTALPHA |
692                               D3DTEXOPCAPS_LERP;
693     }
694     if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
695       pCaps->TextureOpCaps |= D3DTEXOPCAPS_ADDSMOOTH | 
696                               D3DTEXOPCAPS_MULTIPLYADD |
697                               D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
698                               D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
699                               D3DTEXOPCAPS_BLENDTEXTUREALPHAPM;
700     }
701     pCaps->TextureOpCaps |= D3DTEXOPCAPS_BUMPENVMAP;
702                             /* FIXME: Add 
703                               D3DTEXOPCAPS_BUMPENVMAPLUMINANCE 
704                               D3DTEXOPCAPS_PREMODULATE */
705
706     if (gotContext) {
707         GLint gl_max;
708         GLfloat gl_float;
709 #if defined(GL_VERSION_1_3)
710         glGetIntegerv(GL_MAX_TEXTURE_UNITS, &gl_max);
711 #else
712         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
713 #endif
714         TRACE_(d3d_caps)("GLCaps: GL_MAX_TEXTURE_UNITS_ARB=%d\n", gl_max);
715         pCaps->MaxTextureBlendStages = min(8, gl_max);
716         pCaps->MaxSimultaneousTextures = min(8, gl_max);
717
718         glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
719         pCaps->MaxUserClipPlanes = min(MAX_CLIPPLANES, gl_max);
720         TRACE_(d3d_caps)("GLCaps: GL_MAX_CLIP_PLANES=%ld\n", pCaps->MaxUserClipPlanes);
721
722         glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
723         pCaps->MaxActiveLights = gl_max;
724         TRACE_(d3d_caps)("GLCaps: GL_MAX_LIGHTS=%ld\n", pCaps->MaxActiveLights);
725
726         if (GL_SUPPORT(ARB_VERTEX_BLEND)) {
727            glGetIntegerv(GL_MAX_VERTEX_UNITS_ARB, &gl_max);
728            pCaps->MaxVertexBlendMatrices = gl_max;
729            pCaps->MaxVertexBlendMatrixIndex = 1;
730         } else {
731            pCaps->MaxVertexBlendMatrices = 0;
732            pCaps->MaxVertexBlendMatrixIndex = 1;
733         }
734
735         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC)) {
736           glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_max);
737           checkGLcall("glGetInterv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)");
738           pCaps->MaxAnisotropy = gl_max;
739         } else {
740           pCaps->MaxAnisotropy = 0;
741         }
742
743         glGetFloatv(GL_POINT_SIZE_RANGE, &gl_float);
744         pCaps->MaxPointSize = gl_float;
745     }
746
747     pCaps->VertexProcessingCaps = D3DVTXPCAPS_DIRECTIONALLIGHTS | 
748                                   D3DVTXPCAPS_MATERIALSOURCE7   | 
749                                   D3DVTXPCAPS_POSITIONALLIGHTS  | 
750                                   D3DVTXPCAPS_LOCALVIEWER |
751                                   D3DVTXPCAPS_TEXGEN;
752                                   /* FIXME: Add 
753                                      D3DVTXPCAPS_TWEENING */
754
755     pCaps->MaxPrimitiveCount = 0xFFFFFFFF;
756     pCaps->MaxVertexIndex = 0xFFFFFFFF;
757     pCaps->MaxStreams = MAX_STREAMS;
758     pCaps->MaxStreamStride = 1024;
759
760     if (((vs_mode == VS_HW) && GL_SUPPORT(ARB_VERTEX_PROGRAM)) || (vs_mode == VS_SW) || (DeviceType == D3DDEVTYPE_REF))
761         pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
762     else
763         pCaps->VertexShaderVersion = 0;
764     pCaps->MaxVertexShaderConst = D3D8_VSHADER_MAX_CONSTANTS;
765
766 #if 0
767     pCaps->PixelShaderVersion = D3DPS_VERSION(1,1);
768     pCaps->MaxPixelShaderValue = 1.0;
769 #else
770     pCaps->PixelShaderVersion = 0;
771     pCaps->MaxPixelShaderValue = 0.0;
772 #endif
773
774     /* If we created a dummy context, throw it away */
775     WineD3DReleaseFakeGLContext(fake_ctx);
776     return D3D_OK;
777 }
778
779 HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
780     ICOM_THIS(IDirect3D8Impl,iface);
781     FIXME_(d3d_caps)("(%p)->(Adptr:%d)\n", This, Adapter);
782     return D3D_OK;
783 }
784
785
786 static void IDirect3D8Impl_FillGLCaps(LPDIRECT3D8 iface, Display* display) {
787     const char *GL_Extensions = NULL;
788     const char *GLX_Extensions = NULL;
789     GLint gl_max;
790     const char* gl_string = NULL;
791     const char* gl_string_cursor = NULL;
792     Bool test = 0;
793     int major, minor;
794     ICOM_THIS(IDirect3D8Impl,iface);
795
796     if (This->gl_info.bIsFilled) return ;
797     This->gl_info.bIsFilled = 1;
798
799     TRACE_(d3d_caps)("(%p, %p)\n", This, display);
800
801     if (NULL != display) {
802       test = glXQueryVersion(NULL, &major, &minor);
803       This->gl_info.glx_version = ((major & 0x0000FFFF) << 16) | (minor & 0x0000FFFF);
804       gl_string = glXGetClientString(NULL, GLX_VENDOR);
805     } else {
806       gl_string = glGetString(GL_VENDOR);
807     }
808     
809     if (strstr(gl_string, "NVIDIA")) {
810       This->gl_info.gl_vendor = VENDOR_NVIDIA;
811     } else if (strstr(gl_string, "ATI")) {
812       This->gl_info.gl_vendor = VENDOR_ATI;
813     } else {
814       This->gl_info.gl_vendor = VENDOR_WINE;
815     }
816    
817     TRACE_(d3d_caps)("found GL_VENDOR (%s)->(0x%04x)\n", debugstr_a(gl_string), This->gl_info.gl_vendor);
818     
819     gl_string = glGetString(GL_VERSION);
820     switch (This->gl_info.gl_vendor) {
821     case VENDOR_NVIDIA:
822       gl_string_cursor = strstr(gl_string, "NVIDIA");
823       gl_string_cursor = strstr(gl_string_cursor, " ");
824       while (*gl_string_cursor && ' ' == *gl_string_cursor) ++gl_string_cursor;
825       if (*gl_string_cursor) {
826         char tmp[16];
827         int cursor = 0;
828
829         while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
830           tmp[cursor++] = *gl_string_cursor;
831           ++gl_string_cursor;
832         }
833         tmp[cursor] = 0;
834         major = atoi(tmp);
835         
836         if (*gl_string_cursor != '.') WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
837         ++gl_string_cursor;
838
839         while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
840           tmp[cursor++] = *gl_string_cursor;
841           ++gl_string_cursor;
842         }
843         tmp[cursor] = 0;
844         minor = atoi(tmp);
845         break;
846       }
847     case VENDOR_ATI:
848     default:
849       major = 0;
850       minor = 9;
851     }
852     This->gl_info.gl_driver_version = MAKEDWORD_VERSION(major, minor);
853
854     gl_string = glGetString(GL_RENDERER);
855     strcpy(This->gl_info.gl_renderer, gl_string);
856
857     switch (This->gl_info.gl_vendor) {
858     case VENDOR_NVIDIA:
859       if (strstr(This->gl_info.gl_renderer, "GeForce4 Ti")) {
860         This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
861       } else if (strstr(This->gl_info.gl_renderer, "GeForceFX")) {
862         This->gl_info.gl_card = CARD_NVIDIA_GEFORCEFX_5900ULTRA;
863       } else {
864         This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
865       }
866       break;
867     case VENDOR_ATI:
868       This->gl_info.gl_card = CARD_ATI_RADEON_8500;
869       break;
870     default:
871       This->gl_info.gl_card = CARD_WINE;
872       break;
873     }
874
875     FIXME_(d3d_caps)("found GL_VERSION  (0x%08lx)\n", This->gl_info.gl_driver_version);
876     FIXME_(d3d_caps)("found GL_RENDERER (%s)->(0x%04x)\n", debugstr_a(This->gl_info.gl_renderer), This->gl_info.gl_card);
877     /* 
878      * Initialize openGL extension related variables
879      *  with Default values 
880      */
881     memset(&This->gl_info.supported, 0, sizeof(This->gl_info.supported));
882     This->gl_info.max_textures   = 1;
883     This->gl_info.ps_arb_version = PS_VERSION_NOT_SUPPORTED;
884     This->gl_info.vs_arb_version = VS_VERSION_NOT_SUPPORTED;
885     This->gl_info.vs_nv_version  = VS_VERSION_NOT_SUPPORTED;
886     This->gl_info.vs_ati_version = VS_VERSION_NOT_SUPPORTED;
887
888 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = NULL;
889     GL_EXT_FUNCS_GEN;
890 #undef USE_GL_FUNC
891
892     /* Retrieve opengl defaults */
893     glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
894     This->gl_info.max_clipplanes = min(MAX_CLIPPLANES, gl_max);
895     TRACE_(d3d_caps)("ClipPlanes support - num Planes=%d\n", gl_max);
896
897     glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
898     This->gl_info.max_lights = gl_max;
899     TRACE_(d3d_caps)("Lights support - max lights=%d\n", gl_max);
900
901     /* Parse the gl supported features, in theory enabling parts of our code appropriately */
902     GL_Extensions = glGetString(GL_EXTENSIONS);
903     TRACE_(d3d_caps)("GL_Extensions reported:\n");  
904     
905     if (NULL == GL_Extensions) {
906       ERR("   GL_Extensions returns NULL\n");      
907     } else {
908       while (*GL_Extensions != 0x00) {
909         const char *Start = GL_Extensions;
910         char ThisExtn[256];
911
912         memset(ThisExtn, 0x00, sizeof(ThisExtn));
913         while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
914           GL_Extensions++;
915         }
916         memcpy(ThisExtn, Start, (GL_Extensions - Start));
917         TRACE_(d3d_caps)("- %s\n", ThisExtn);
918
919         /**
920          * ARB 
921          */
922         if (strcmp(ThisExtn, "GL_ARB_fragment_program") == 0) {
923           This->gl_info.ps_arb_version = PS_VERSION_11;
924           TRACE_(d3d_caps)(" FOUND: ARB Pixel Shader support - version=%02x\n", This->gl_info.ps_arb_version);
925           This->gl_info.supported[ARB_FRAGMENT_PROGRAM] = TRUE;
926         } else if (strcmp(ThisExtn, "GL_ARB_multisample") == 0) {
927           TRACE_(d3d_caps)(" FOUND: ARB Multisample support\n");
928           This->gl_info.supported[ARB_MULTISAMPLE] = TRUE;
929         } else if (strcmp(ThisExtn, "GL_ARB_multitexture") == 0) {
930           glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
931           TRACE_(d3d_caps)(" FOUND: ARB Multitexture support - GL_MAX_TEXTURE_UNITS_ARB=%u\n", gl_max);
932           This->gl_info.supported[ARB_MULTITEXTURE] = TRUE;
933           This->gl_info.max_textures = min(8, gl_max);
934         } else if (strcmp(ThisExtn, "GL_ARB_texture_cube_map") == 0) {
935           TRACE_(d3d_caps)(" FOUND: ARB Texture Cube Map support\n");
936           This->gl_info.supported[ARB_TEXTURE_CUBE_MAP] = TRUE;
937           TRACE_(d3d_caps)(" IMPLIED: NVIDIA (NV) Texture Gen Reflection support\n");
938           This->gl_info.supported[NV_TEXGEN_REFLECTION] = TRUE;
939         } else if (strcmp(ThisExtn, "GL_ARB_texture_compression") == 0) {
940           TRACE_(d3d_caps)(" FOUND: ARB Texture Compression support\n");
941           This->gl_info.supported[ARB_TEXTURE_COMPRESSION] = TRUE;
942         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_add") == 0) {
943           TRACE_(d3d_caps)(" FOUND: ARB Texture Env Add support\n");
944           This->gl_info.supported[ARB_TEXTURE_ENV_ADD] = TRUE;
945         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_combine") == 0) {
946           TRACE_(d3d_caps)(" FOUND: ARB Texture Env combine support\n");
947           This->gl_info.supported[ARB_TEXTURE_ENV_COMBINE] = TRUE;
948         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_dot3") == 0) {
949           TRACE_(d3d_caps)(" FOUND: ARB Dot3 support\n");
950           This->gl_info.supported[ARB_TEXTURE_ENV_DOT3] = TRUE;
951         } else if (strcmp(ThisExtn, "GL_ARB_texture_border_clamp") == 0) {
952           TRACE_(d3d_caps)(" FOUND: ARB Texture border clamp support\n");
953           This->gl_info.supported[ARB_TEXTURE_BORDER_CLAMP] = TRUE;
954         } else if (strcmp(ThisExtn, "GL_ARB_texture_mirrored_repeat") == 0) {
955           TRACE_(d3d_caps)(" FOUND: ARB Texture mirrored repeat support\n");
956           This->gl_info.supported[ARB_TEXTURE_MIRRORED_REPEAT] = TRUE;
957         } else if (strstr(ThisExtn, "GL_ARB_vertex_program")) {
958           This->gl_info.vs_arb_version = VS_VERSION_11;
959           TRACE_(d3d_caps)(" FOUND: ARB Vertex Shader support - version=%02x\n", This->gl_info.vs_arb_version);
960           This->gl_info.supported[ARB_VERTEX_PROGRAM] = TRUE;
961
962         /**
963          * EXT
964          */
965         } else if (strcmp(ThisExtn, "GL_EXT_fog_coord") == 0) {
966           TRACE_(d3d_caps)(" FOUND: EXT Fog coord support\n");
967           This->gl_info.supported[EXT_FOG_COORD] = TRUE;
968         } else if (strcmp(ThisExtn, "GL_EXT_paletted_texture") == 0) { /* handle paletted texture extensions */
969           TRACE_(d3d_caps)(" FOUND: EXT Paletted texture support\n");
970           This->gl_info.supported[EXT_PALETTED_TEXTURE] = TRUE;
971         } else if (strcmp(ThisExtn, "GL_EXT_point_parameters") == 0) {
972           TRACE_(d3d_caps)(" FOUND: EXT Point parameters support\n");
973           This->gl_info.supported[EXT_POINT_PARAMETERS] = TRUE;
974         } else if (strcmp(ThisExtn, "GL_EXT_secondary_color") == 0) {
975           TRACE_(d3d_caps)(" FOUND: EXT Secondary coord support\n");
976           This->gl_info.supported[EXT_SECONDARY_COLOR] = TRUE;
977         } else if (strcmp(ThisExtn, "GL_EXT_stencil_wrap") == 0) {
978           TRACE_(d3d_caps)(" FOUND: EXT Stencil wrap support\n");
979           This->gl_info.supported[EXT_STENCIL_WRAP] = TRUE;
980         } else if (strcmp(ThisExtn, "GL_EXT_texture_compression_s3tc") == 0) {
981           TRACE_(d3d_caps)(" FOUND: EXT Texture S3TC compression support\n");
982           This->gl_info.supported[EXT_TEXTURE_COMPRESSION_S3TC] = TRUE;
983         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_add") == 0) {
984           TRACE_(d3d_caps)(" FOUND: EXT Texture Env Add support\n");
985           This->gl_info.supported[EXT_TEXTURE_ENV_ADD] = TRUE;
986         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_combine") == 0) {
987           TRACE_(d3d_caps)(" FOUND: EXT Texture Env combine support\n");
988           This->gl_info.supported[EXT_TEXTURE_ENV_COMBINE] = TRUE;
989         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_dot3") == 0) {
990           TRACE_(d3d_caps)(" FOUND: EXT Dot3 support\n");
991           This->gl_info.supported[EXT_TEXTURE_ENV_DOT3] = TRUE;
992         } else if (strcmp(ThisExtn, "GL_EXT_texture_filter_anisotropic") == 0) {
993           TRACE_(d3d_caps)(" FOUND: EXT Texture Anisotropic filter support\n");
994           This->gl_info.supported[EXT_TEXTURE_FILTER_ANISOTROPIC] = TRUE;
995         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod") == 0) {
996           TRACE_(d3d_caps)(" FOUND: EXT Texture LOD support\n");
997           This->gl_info.supported[EXT_TEXTURE_LOD] = TRUE;
998         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod_bias") == 0) {
999           TRACE_(d3d_caps)(" FOUND: EXT Texture LOD bias support\n");
1000           This->gl_info.supported[EXT_TEXTURE_LOD_BIAS] = TRUE;
1001         } else if (strcmp(ThisExtn, "GL_EXT_vertex_weighting") == 0) {
1002           TRACE_(d3d_caps)(" FOUND: EXT Vertex weighting support\n");
1003           This->gl_info.supported[EXT_VERTEX_WEIGHTING] = TRUE;
1004
1005         /**
1006          * NVIDIA 
1007          */
1008         } else if (strstr(ThisExtn, "GL_NV_fog_distance")) {
1009           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Fog Distance support\n");
1010           This->gl_info.supported[NV_FOG_DISTANCE] = TRUE;
1011         } else if (strstr(ThisExtn, "GL_NV_fragment_program")) {
1012           This->gl_info.ps_nv_version = PS_VERSION_11;
1013           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Pixel Shader support - version=%02x\n", This->gl_info.ps_nv_version);
1014         } else if (strcmp(ThisExtn, "GL_NV_register_combiners") == 0) {
1015           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (1) support\n");
1016           This->gl_info.supported[NV_REGISTER_COMBINERS] = TRUE;
1017         } else if (strcmp(ThisExtn, "GL_NV_register_combiners2") == 0) {
1018           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (2) support\n");
1019           This->gl_info.supported[NV_REGISTER_COMBINERS2] = TRUE;
1020         } else if (strcmp(ThisExtn, "GL_NV_texgen_reflection") == 0) {
1021           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Gen Reflection support\n");
1022           This->gl_info.supported[NV_TEXGEN_REFLECTION] = TRUE;
1023         } else if (strcmp(ThisExtn, "GL_NV_texture_env_combine4") == 0) {
1024           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Env combine (4) support\n");
1025           This->gl_info.supported[NV_TEXTURE_ENV_COMBINE4] = TRUE;
1026         } else if (strcmp(ThisExtn, "GL_NV_texture_shader") == 0) {
1027           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (1) support\n");
1028           This->gl_info.supported[NV_TEXTURE_SHADER] = TRUE;
1029         } else if (strcmp(ThisExtn, "GL_NV_texture_shader2") == 0) {
1030           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (2) support\n");
1031           This->gl_info.supported[NV_TEXTURE_SHADER2] = TRUE;
1032         } else if (strcmp(ThisExtn, "GL_NV_texture_shader3") == 0) {
1033           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (3) support\n");
1034           This->gl_info.supported[NV_TEXTURE_SHADER3] = TRUE;
1035         } else if (strstr(ThisExtn, "GL_NV_vertex_program")) {
1036           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);
1037           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);
1038           TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Vertex Shader support - version=%02x\n", This->gl_info.vs_nv_version);
1039           This->gl_info.supported[NV_VERTEX_PROGRAM] = TRUE;
1040
1041         /**
1042          * ATI
1043          */
1044         /** TODO */
1045         } else if (strcmp(ThisExtn, "GL_ATI_texture_env_combine3") == 0) {
1046           TRACE_(d3d_caps)(" FOUND: ATI Texture Env combine (3) support\n");
1047           This->gl_info.supported[ATI_TEXTURE_ENV_COMBINE3] = TRUE;
1048         } else if (strcmp(ThisExtn, "GL_ATI_texture_mirror_once") == 0) {
1049           TRACE_(d3d_caps)(" FOUND: ATI Texture Mirror Once support\n");
1050           This->gl_info.supported[ATI_TEXTURE_MIRROR_ONCE] = TRUE;
1051         } else if (strcmp(ThisExtn, "GL_EXT_vertex_shader") == 0) {
1052           This->gl_info.vs_ati_version = VS_VERSION_11;
1053           TRACE_(d3d_caps)(" FOUND: ATI (EXT) Vertex Shader support - version=%02x\n", This->gl_info.vs_ati_version);
1054           This->gl_info.supported[EXT_VERTEX_SHADER] = TRUE;
1055         }
1056
1057
1058         if (*GL_Extensions == ' ') GL_Extensions++;
1059       }
1060     }
1061
1062 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
1063     GL_EXT_FUNCS_GEN;
1064 #undef USE_GL_FUNC
1065
1066     if (display != NULL) {
1067         GLX_Extensions = glXQueryExtensionsString(display, DefaultScreen(display));
1068         TRACE_(d3d_caps)("GLX_Extensions reported:\n");  
1069     
1070         if (NULL == GLX_Extensions) {
1071           ERR("   GLX_Extensions returns NULL\n");      
1072         } else {
1073           while (*GLX_Extensions != 0x00) {
1074             const char *Start = GLX_Extensions;
1075             char ThisExtn[256];
1076            
1077             memset(ThisExtn, 0x00, sizeof(ThisExtn));
1078             while (*GLX_Extensions != ' ' && *GLX_Extensions != 0x00) {
1079               GLX_Extensions++;
1080             }
1081             memcpy(ThisExtn, Start, (GLX_Extensions - Start));
1082             TRACE_(d3d_caps)("- %s\n", ThisExtn);
1083             if (*GLX_Extensions == ' ') GLX_Extensions++;
1084           }
1085         }
1086     }
1087
1088 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
1089     GLX_EXT_FUNCS_GEN;
1090 #undef USE_GL_FUNC
1091
1092     /* Only save the values obtained when a display is provided */
1093     if (display != NULL) This->isGLInfoValid = TRUE;
1094
1095 }
1096
1097 HRESULT  WINAPI  IDirect3D8Impl_CreateDevice               (LPDIRECT3D8 iface,
1098                                                             UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
1099                                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
1100                                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
1101     IDirect3DDevice8Impl *object;
1102     HWND whichHWND;
1103     int num;
1104     XVisualInfo template;
1105     HDC hDc;
1106
1107     ICOM_THIS(IDirect3D8Impl,iface);
1108     TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
1109           hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);
1110
1111     /* Allocate the storage for the device */
1112     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
1113     if (NULL == object) {
1114       return D3DERR_OUTOFVIDEOMEMORY;
1115     }
1116     object->lpVtbl = &Direct3DDevice8_Vtbl;
1117     object->ref = 1;
1118     object->direct3d8 = This;
1119     /** The device AddRef the direct3d8 Interface else crash in propers clients codes */
1120     IDirect3D8_AddRef((LPDIRECT3D8) object->direct3d8);
1121
1122     /** use StateBlock Factory here, for creating the startup stateBlock */
1123     object->StateBlock = NULL;
1124     IDirect3DDeviceImpl_CreateStateBlock(object, D3DSBT_ALL, NULL);
1125     object->UpdateStateBlock = object->StateBlock;
1126
1127     /* Save the creation parameters */
1128     object->CreateParms.AdapterOrdinal = Adapter;
1129     object->CreateParms.DeviceType = DeviceType;
1130     object->CreateParms.hFocusWindow = hFocusWindow;
1131     object->CreateParms.BehaviorFlags = BehaviourFlags;
1132
1133     *ppReturnedDeviceInterface = (LPDIRECT3DDEVICE8) object;
1134
1135     /* Initialize settings */
1136     object->PresentParms.BackBufferCount = 1; /* Opengl only supports one? */
1137     object->adapterNo = Adapter;
1138     object->devType = DeviceType;
1139
1140     /* Initialize openGl - Note the visual is chosen as the window is created and the glcontext cannot
1141          use different properties after that point in time. FIXME: How to handle when requested format 
1142          doesn't match actual visual? Cannot choose one here - code removed as it ONLY works if the one
1143          it chooses is identical to the one already being used!                                        */
1144     /* FIXME: Handle stencil appropriately via EnableAutoDepthStencil / AutoDepthStencilFormat */
1145
1146     /* Which hwnd are we using? */
1147     whichHWND = pPresentationParameters->hDeviceWindow;
1148     if (!whichHWND) {
1149         whichHWND = hFocusWindow;
1150     }
1151     object->win_handle = whichHWND;
1152     object->win     = (Window)GetPropA( whichHWND, "__wine_x11_client_window" );
1153
1154     hDc = GetDC(whichHWND);
1155     object->display = get_display(hDc);
1156
1157     TRACE("(%p)->(DepthStencil:(%u,%s), BackBufferFormat:(%u,%s))\n", This, 
1158           pPresentationParameters->AutoDepthStencilFormat, debug_d3dformat(pPresentationParameters->AutoDepthStencilFormat),
1159           pPresentationParameters->BackBufferFormat, debug_d3dformat(pPresentationParameters->BackBufferFormat));
1160
1161     ENTER_GL();
1162
1163     /* Create a context based off the properties of the existing visual */
1164     template.visualid = (VisualID)GetPropA(GetDesktopWindow(), "__wine_x11_visual_id");
1165     object->visInfo = XGetVisualInfo(object->display, VisualIDMask, &template, &num);
1166     if (NULL == object->visInfo) {
1167         ERR("cannot really get XVisual\n"); 
1168         LEAVE_GL();
1169         return D3DERR_NOTAVAILABLE;
1170      }
1171     object->glCtx = glXCreateContext(object->display, object->visInfo, NULL, GL_TRUE);
1172     if (NULL == object->glCtx) {
1173       ERR("cannot create glxContext\n"); 
1174       LEAVE_GL();
1175       return D3DERR_NOTAVAILABLE;
1176      }
1177     LEAVE_GL();
1178
1179     ReleaseDC(whichHWND, hDc);
1180     
1181     if (object->glCtx == NULL) {
1182         ERR("Error in context creation !\n");
1183         return D3DERR_INVALIDCALL;
1184     } else {
1185         TRACE("Context created (HWND=%p, glContext=%p, Window=%ld, VisInfo=%p)\n",
1186               whichHWND, object->glCtx, object->win, object->visInfo);
1187     }
1188
1189     /* If not windowed, need to go fullscreen, and resize the HWND to the appropriate  */
1190     /*        dimensions                                                               */
1191     if (!pPresentationParameters->Windowed) {
1192 #if 1
1193         DEVMODEW devmode;
1194         HDC hdc;
1195         int bpp = 0;
1196         memset(&devmode, 0, sizeof(DEVMODEW));
1197         devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; 
1198         MultiByteToWideChar(CP_ACP, 0, "Gamers CG", -1, devmode.dmDeviceName, CCHDEVICENAME);
1199         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
1200         bpp = GetDeviceCaps(hdc, BITSPIXEL);
1201         DeleteDC(hdc);
1202         devmode.dmBitsPerPel = (bpp >= 24) ? 32 : bpp;/*Stupid XVidMode cannot change bpp D3DFmtGetBpp(object, pPresentationParameters->BackBufferFormat);*/
1203         devmode.dmPelsWidth  = pPresentationParameters->BackBufferWidth;
1204         devmode.dmPelsHeight = pPresentationParameters->BackBufferHeight;
1205         ChangeDisplaySettingsExW(devmode.dmDeviceName, &devmode, object->win_handle, CDS_FULLSCREEN, NULL);
1206 #else
1207         FIXME("Requested full screen support not implemented, expect windowed operation\n");
1208 #endif
1209
1210         /* Make popup window */
1211         ShowWindow(whichHWND, SW_HIDE);
1212         SetWindowLongA(whichHWND, GWL_STYLE, WS_POPUP);
1213         SetWindowPos(object->win_handle, HWND_TOP, 0, 0, 
1214                      pPresentationParameters->BackBufferWidth,
1215                      pPresentationParameters->BackBufferHeight, SWP_SHOWWINDOW | SWP_FRAMECHANGED);
1216         ShowWindow(whichHWND, SW_SHOW);
1217     }
1218
1219     TRACE("Creating back buffer\n");
1220     /* MSDN: If Windowed is TRUE and either of the BackBufferWidth/Height values is zero,
1221        then the corresponding dimension of the client area of the hDeviceWindow
1222        (or the focus window, if hDeviceWindow is NULL) is taken. */
1223     if (pPresentationParameters->Windowed && ((pPresentationParameters->BackBufferWidth  == 0) ||
1224                                               (pPresentationParameters->BackBufferHeight == 0))) {
1225         RECT Rect;
1226
1227         GetClientRect(whichHWND, &Rect);
1228
1229         if (pPresentationParameters->BackBufferWidth == 0) {
1230            pPresentationParameters->BackBufferWidth = Rect.right;
1231            TRACE("Updating width to %d\n", pPresentationParameters->BackBufferWidth);
1232         }
1233         if (pPresentationParameters->BackBufferHeight == 0) {
1234            pPresentationParameters->BackBufferHeight = Rect.bottom;
1235            TRACE("Updating height to %d\n", pPresentationParameters->BackBufferHeight);
1236         }
1237     }
1238
1239     /* Save the presentation parms now filled in correctly */
1240     memcpy(&object->PresentParms, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
1241
1242
1243     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
1244                                             pPresentationParameters->BackBufferWidth,
1245                                             pPresentationParameters->BackBufferHeight,
1246                                             pPresentationParameters->BackBufferFormat,
1247                                             pPresentationParameters->MultiSampleType,
1248                                             TRUE,
1249                                             (LPDIRECT3DSURFACE8*) &object->frontBuffer);
1250
1251     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
1252                                             pPresentationParameters->BackBufferWidth,
1253                                             pPresentationParameters->BackBufferHeight,
1254                                             pPresentationParameters->BackBufferFormat,
1255                                             pPresentationParameters->MultiSampleType,
1256                                             TRUE,
1257                                             (LPDIRECT3DSURFACE8*) &object->backBuffer);
1258
1259     if (pPresentationParameters->EnableAutoDepthStencil) {
1260        IDirect3DDevice8Impl_CreateDepthStencilSurface((LPDIRECT3DDEVICE8) object,
1261                                                       pPresentationParameters->BackBufferWidth,
1262                                                       pPresentationParameters->BackBufferHeight,
1263                                                       pPresentationParameters->AutoDepthStencilFormat,
1264                                                       D3DMULTISAMPLE_NONE,
1265                                                       (LPDIRECT3DSURFACE8*) &object->depthStencilBuffer);
1266     } else {
1267       object->depthStencilBuffer = NULL;
1268     }
1269     TRACE("FrontBuf @ %p, BackBuf @ %p, DepthStencil @ %p\n",object->frontBuffer, object->backBuffer, object->depthStencilBuffer);
1270
1271     /* init the default renderTarget management */
1272     object->drawable = object->win;
1273     object->render_ctx = object->glCtx;
1274     object->renderTarget = object->backBuffer;
1275     IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->renderTarget);
1276     object->stencilBufferTarget = object->depthStencilBuffer;
1277     if (NULL != object->stencilBufferTarget) {
1278       IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->stencilBufferTarget);
1279     }
1280
1281     ENTER_GL();
1282
1283     if (glXMakeCurrent(object->display, object->win, object->glCtx) == False) {
1284       ERR("Error in setting current context (context %p drawable %ld)!\n", object->glCtx, object->win);
1285     }
1286     checkGLcall("glXMakeCurrent");
1287
1288     /* Clear the screen */
1289     glClearColor(1.0, 0.0, 0.0, 0.0);
1290     checkGLcall("glClearColor");
1291     glColor3f(1.0, 1.0, 1.0);
1292     checkGLcall("glColor3f");
1293
1294     glEnable(GL_LIGHTING);
1295     checkGLcall("glEnable");
1296
1297     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1298     checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1299
1300     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1301     checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1302
1303     glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1304     checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1305
1306     /* 
1307      * Initialize openGL extension related variables
1308      *  with Default values 
1309      */
1310     IDirect3D8Impl_FillGLCaps(iface, object->display);
1311
1312     /* Setup all the devices defaults */
1313     IDirect3DDeviceImpl_InitStartupStateBlock(object);
1314
1315     LEAVE_GL();
1316
1317     { /* Set a default viewport */
1318        D3DVIEWPORT8 vp;
1319        vp.X      = 0;
1320        vp.Y      = 0;
1321        vp.Width  = pPresentationParameters->BackBufferWidth;
1322        vp.Height = pPresentationParameters->BackBufferHeight;
1323        vp.MinZ   = 0.0f;
1324        vp.MaxZ   = 1.0f;
1325        IDirect3DDevice8Impl_SetViewport((LPDIRECT3DDEVICE8) object, &vp);
1326     }
1327
1328     /* Initialize the current view state */
1329     object->modelview_valid = 1;
1330     object->proj_valid = 0;
1331     object->view_ident = 1;
1332     object->last_was_rhw = 0;
1333     glGetIntegerv(GL_MAX_LIGHTS, &object->maxConcurrentLights);
1334     TRACE("(%p,%d) All defaults now set up, leaving CreateDevice with %p\n", This, Adapter, object);
1335
1336     /* Clear the screen */
1337     IDirect3DDevice8Impl_Clear((LPDIRECT3DDEVICE8) object, 0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER|D3DCLEAR_TARGET, 0x00, 1.0, 0);
1338
1339     return D3D_OK;
1340 }
1341
1342 ICOM_VTABLE(IDirect3D8) Direct3D8_Vtbl =
1343 {
1344     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1345     IDirect3D8Impl_QueryInterface,
1346     IDirect3D8Impl_AddRef,
1347     IDirect3D8Impl_Release,
1348     IDirect3D8Impl_RegisterSoftwareDevice,
1349     IDirect3D8Impl_GetAdapterCount,
1350     IDirect3D8Impl_GetAdapterIdentifier,
1351     IDirect3D8Impl_GetAdapterModeCount,
1352     IDirect3D8Impl_EnumAdapterModes,
1353     IDirect3D8Impl_GetAdapterDisplayMode,
1354     IDirect3D8Impl_CheckDeviceType,
1355     IDirect3D8Impl_CheckDeviceFormat,
1356     IDirect3D8Impl_CheckDeviceMultiSampleType,
1357     IDirect3D8Impl_CheckDepthStencilMatch,
1358     IDirect3D8Impl_GetDeviceCaps,
1359     IDirect3D8Impl_GetAdapterMonitor,
1360     IDirect3D8Impl_CreateDevice
1361 };