Removed Wine-specific definition from exported header (spotted by
[wine] / dlls / d3d8 / directx.c
1 /*
2  * IDirect3D8 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31
32 #include "d3d8_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
35
36 /* x11drv GDI escapes */
37 #define X11DRV_ESCAPE 6789
38 enum x11drv_escape_codes
39 {
40     X11DRV_GET_DISPLAY,   /* get X11 display for a DC */
41     X11DRV_GET_DRAWABLE,  /* get current drawable for a DC */
42     X11DRV_GET_FONT,      /* get current X font for a DC */
43 };
44
45 #define NUM_MODES 20
46 static const int modes[NUM_MODES][4] = {
47     {640, 480, 85, 16},
48     {640, 480, 85, 32},
49
50     {800, 600, 85, 16},
51     {800, 600, 85, 32},
52
53     {1024, 768, 85, 16},
54     {1024, 768, 85, 32},
55
56     {1152, 864, 85, 16},
57     {1152, 864, 85, 32},
58
59     {1280, 768, 85, 16},
60     {1280, 768, 85, 32},
61
62     {1280, 960, 85, 16},
63     {1280, 960, 85, 32},
64
65     {1280, 1024, 85, 16},
66     {1280, 1024, 85, 32},
67
68     {1600, 900, 85, 16},
69     {1600, 900, 85, 32},
70
71     {1600, 1024, 85, 16},
72     {1600, 1024, 85, 32},
73
74     {1600, 1200, 85, 16},
75     {1600, 1200, 85, 32}
76 };
77
78 #define NUM_FORMATS 7
79 static const D3DFORMAT device_formats[NUM_FORMATS] = {
80   D3DFMT_P8,
81   D3DFMT_R3G3B2,
82   D3DFMT_R5G6B5, 
83   D3DFMT_X1R5G5B5,
84   D3DFMT_X4R4G4B4,
85   D3DFMT_R8G8B8,
86   D3DFMT_X8R8G8B8
87 };
88
89
90 /* retrieve the X display to use on a given DC */
91 inline static Display *get_display( HDC hdc )
92 {
93     Display *display;
94     enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;
95
96     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
97                     sizeof(display), (LPSTR)&display )) display = NULL;
98     return display;
99 }
100
101
102 /* IDirect3D IUnknown parts follow: */
103 HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface,REFIID riid,LPVOID *ppobj)
104 {
105     ICOM_THIS(IDirect3D8Impl,iface);
106
107     if (IsEqualGUID(riid, &IID_IUnknown)
108         || IsEqualGUID(riid, &IID_IDirect3D8)) {
109         IDirect3D8Impl_AddRef(iface);
110         *ppobj = This;
111         return D3D_OK;
112     }
113
114     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
115     return E_NOINTERFACE;
116 }
117
118 ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
119     ICOM_THIS(IDirect3D8Impl,iface);
120     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
121     return ++(This->ref);
122 }
123
124 ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
125     ICOM_THIS(IDirect3D8Impl,iface);
126     ULONG ref = --This->ref;
127     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
128     if (ref == 0)
129         HeapFree(GetProcessHeap(), 0, This);
130     return ref;
131 }
132
133 /* IDirect3D Interface follow: */
134 HRESULT  WINAPI  IDirect3D8Impl_RegisterSoftwareDevice     (LPDIRECT3D8 iface, void* pInitializeFunction) {
135     ICOM_THIS(IDirect3D8Impl,iface);
136     FIXME("(%p)->(%p): stub\n", This, pInitializeFunction);
137     return D3D_OK;
138 }
139
140 UINT     WINAPI  IDirect3D8Impl_GetAdapterCount            (LPDIRECT3D8 iface) {
141     ICOM_THIS(IDirect3D8Impl,iface);
142     /* FIXME: Set to one for now to imply the display */
143     TRACE("(%p): Mostly stub, only returns primary display\n", This);
144     return 1;
145 }
146
147 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterIdentifier       (LPDIRECT3D8 iface,
148                                                             UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
149     ICOM_THIS(IDirect3D8Impl,iface);
150
151     TRACE("(%p}->(Adapter: %d, Flags: %lx, pId=%p)\n", This, Adapter, Flags, pIdentifier);
152
153     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
154         return D3DERR_INVALIDCALL;
155     }
156
157     if (Adapter == 0) { /* Display */
158         strcpy(pIdentifier->Driver, "Display");
159         strcpy(pIdentifier->Description, "Direct3D Display");
160         pIdentifier->DriverVersion.s.HighPart = 1;
161         pIdentifier->DriverVersion.s.LowPart = 0;
162         pIdentifier->VendorId = 0;
163         pIdentifier->DeviceId = 0;
164         pIdentifier->SubSysId = 0;
165         pIdentifier->Revision = 0;
166         /*FIXME: memcpy(&pIdentifier->DeviceIdentifier, ??, sizeof(??GUID)); */
167         if (Flags & D3DENUM_NO_WHQL_LEVEL) {
168             pIdentifier->WHQLLevel = 0;
169         } else {
170             pIdentifier->WHQLLevel = 1;
171         }
172     } else {
173         FIXME("Adapter not primary display\n");
174     }
175
176     return D3D_OK;
177 }
178
179 UINT     WINAPI  IDirect3D8Impl_GetAdapterModeCount        (LPDIRECT3D8 iface,
180                                                             UINT Adapter) {
181     ICOM_THIS(IDirect3D8Impl,iface);
182
183     TRACE("(%p}->(Adapter: %d)\n", This, Adapter);
184
185     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
186         return D3DERR_INVALIDCALL;
187     }
188
189     if (Adapter == 0) { /* Display */
190         int maxWidth        = GetSystemMetrics(SM_CXSCREEN);
191         int maxHeight       = GetSystemMetrics(SM_CYSCREEN);
192         int i;
193
194         for (i = 0; i < NUM_MODES; i++) {
195             if (modes[i][0] > maxWidth || modes[i][1] > maxHeight) {
196                 TRACE("(%p}->(Adapter: %d) => %d\n", This, Adapter, i + 1);
197                 return i + 1;
198             }
199         }
200         TRACE("(%p}->(Adapter: %d) => %d\n", This, Adapter, NUM_MODES);
201         return NUM_MODES + 1;
202     } else {
203         FIXME("Adapter not primary display\n");
204     }
205
206     return 0;
207 }
208
209 HRESULT  WINAPI  IDirect3D8Impl_EnumAdapterModes           (LPDIRECT3D8 iface,
210                                                             UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
211     ICOM_THIS(IDirect3D8Impl,iface);
212
213     TRACE("(%p}->(Adapter:%d, mode:%d, pMode:%p)\n", This, Adapter, Mode, pMode);
214
215     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
216         return D3DERR_INVALIDCALL;
217     }
218
219     if (Adapter == 0) { /* Display */
220         HDC hdc;
221         int bpp = 0;
222
223         if (Mode == 0) {
224             pMode->Width        = GetSystemMetrics(SM_CXSCREEN);
225             pMode->Height       = GetSystemMetrics(SM_CYSCREEN);
226             pMode->RefreshRate  = 85; /*FIXME: How to identify? */
227             bpp                 = 32;
228         } else if (Mode < (NUM_MODES + 1)) {
229             pMode->Width        = modes[Mode - 1][0];
230             pMode->Height       = modes[Mode - 1][1];
231             pMode->RefreshRate  = modes[Mode - 1][2];
232             bpp                 = modes[Mode - 1][3];
233         } else {
234             TRACE("Requested mode out of range %d\n", Mode);
235             return D3DERR_INVALIDCALL;
236         }
237
238         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
239         bpp = min(GetDeviceCaps(hdc, BITSPIXEL), bpp);
240         DeleteDC(hdc);
241
242         switch (bpp) {
243         case  8: pMode->Format = D3DFMT_R3G3B2;   break;
244         case 16: pMode->Format = D3DFMT_R5G6B5;   break;
245         case 24: /* pMode->Format = D3DFMT_R5G6B5;   break;*/ /* Make 24bit appear as 32 bit */
246         case 32: pMode->Format = D3DFMT_A8R8G8B8; break;
247         default: pMode->Format = D3DFMT_UNKNOWN;
248         }
249         TRACE("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);
250
251     } else {
252         FIXME("Adapter not primary display\n");
253     }
254
255     return D3D_OK;
256 }
257
258 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterDisplayMode      (LPDIRECT3D8 iface,
259                                                             UINT Adapter, D3DDISPLAYMODE* pMode) {
260     ICOM_THIS(IDirect3D8Impl,iface);
261     TRACE("(%p}->(Adapter: %d, pMode: %p)\n", This, Adapter, pMode);
262
263     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
264         return D3DERR_INVALIDCALL;
265     }
266
267     if (Adapter == 0) { /* Display */
268         HDC hdc;
269         int bpp = 0;
270
271         pMode->Width        = GetSystemMetrics(SM_CXSCREEN);
272         pMode->Height       = GetSystemMetrics(SM_CYSCREEN);
273         pMode->RefreshRate  = 85; /*FIXME: How to identify? */
274
275         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
276         bpp = GetDeviceCaps(hdc, BITSPIXEL);
277         DeleteDC(hdc);
278
279         switch (bpp) {
280         case  8: pMode->Format       = D3DFMT_R3G3B2;   break;
281         case 16: pMode->Format       = D3DFMT_R5G6B5;   break;
282         case 24: /*pMode->Format       = D3DFMT_R5G6B5;   break;*/ /* Make 24bit appear as 32 bit */
283         case 32: pMode->Format       = D3DFMT_A8R8G8B8; break;
284         default: pMode->Format       = D3DFMT_UNKNOWN;
285         }
286
287     } else {
288         FIXME("Adapter not primary display\n");
289     }
290
291     TRACE("returning w:%d, h:%d, ref:%d, fmt:%x\n", pMode->Width,
292           pMode->Height, pMode->RefreshRate, pMode->Format);
293     return D3D_OK;
294 }
295
296 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
297                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
298                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
299     ICOM_THIS(IDirect3D8Impl,iface);
300     FIXME("(%p)->(Adptr:%d, CheckType:(%x,%s), DispFmt:(%x,%s), BackBuf:(%x,%s), Win?%d): stub\n", 
301           This, 
302           Adapter, 
303           CheckType, debug_d3ddevicetype(CheckType),
304           DisplayFormat, debug_d3dformat(DisplayFormat),
305           BackBufferFormat, debug_d3dformat(BackBufferFormat),
306           Windowed);
307     return D3D_OK;
308 }
309
310 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
311                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
312                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
313     ICOM_THIS(IDirect3D8Impl,iface);
314     FIXME("(%p)->(Adptr:%d, DevType:(%u,%s), AdptFmt:(%u,%s), Use:(%lu,%s), ResTyp:(%x,%s), CheckFmt:(%u,%s))\n", 
315           This, 
316           Adapter, 
317           DeviceType, debug_d3ddevicetype(DeviceType), 
318           AdapterFormat, debug_d3dformat(AdapterFormat), 
319           Usage, debug_d3dusage(Usage),
320           RType, debug_d3dressourcetype(RType), 
321           CheckFormat, debug_d3dformat(CheckFormat));
322
323     switch (CheckFormat) {
324     case D3DFMT_UYVY:
325     case D3DFMT_YUY2:
326     case D3DFMT_DXT1:
327     case D3DFMT_DXT2:
328     case D3DFMT_DXT3:
329     case D3DFMT_DXT4:
330     case D3DFMT_DXT5:
331     case D3DFMT_X8L8V8U8:
332     case D3DFMT_L6V5U5:
333     case D3DFMT_V8U8:
334       /* Since we do not support these formats right now, don't pretend to. */
335       return D3DERR_NOTAVAILABLE;
336     default:
337       break;
338     }
339
340     return D3D_OK;
341 }
342
343 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
344                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
345                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
346     ICOM_THIS(IDirect3D8Impl,iface);
347     FIXME("(%p)->(Adptr:%d, DevType:(%x,%s), SurfFmt:(%x,%s), Win?%d, MultiSamp:%x)\n", 
348           This, 
349           Adapter, 
350           DeviceType, debug_d3ddevicetype(DeviceType),
351           SurfaceFormat, debug_d3dformat(SurfaceFormat),
352           Windowed, 
353           MultiSampleType);
354   
355     if (D3DMULTISAMPLE_NONE == MultiSampleType)
356       return D3D_OK;
357     return D3DERR_NOTAVAILABLE;
358 }
359
360 HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface, 
361                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
362                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
363     ICOM_THIS(IDirect3D8Impl,iface);
364     FIXME("(%p)->(Adptr:%d, DevType:(%x,%s), AdptFmt:(%x,%s), RendrTgtFmt:(%x,%s), DepthStencilFmt:(%x,%s))\n", 
365           This, 
366           Adapter, 
367           DeviceType, debug_d3ddevicetype(DeviceType),
368           AdapterFormat, debug_d3dformat(AdapterFormat),
369           RenderTargetFormat, debug_d3dformat(RenderTargetFormat), 
370           DepthStencilFormat, debug_d3dformat(DepthStencilFormat));
371
372 #if 0
373     switch (DepthStencilFormat) {
374     case D3DFMT_D24X4S4:
375     case D3DFMT_D24X8: 
376     case D3DFMT_D24S8: 
377     case D3DFMT_D32:
378       /**
379        * as i don't know how to really check hard caps of graphics cards
380        * i prefer to not permit 32bit zbuffers enumeration (as few cards can do it)
381        */
382       return D3DERR_NOTAVAILABLE;
383     default:
384       break;
385     }
386 #endif
387     return D3D_OK;
388 }
389
390 HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
391     ICOM_THIS(IDirect3D8Impl,iface);
392     TRACE("(%p)->(Adptr:%d, DevType: %x, pCaps: %p)\n", This, Adapter, DeviceType, pCaps);
393
394     pCaps->DeviceType = (DeviceType == D3DDEVTYPE_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;  /* Not quite true, but use h/w supported by opengl I suppose */
395     pCaps->AdapterOrdinal = Adapter;
396
397     pCaps->Caps = 0;
398     pCaps->Caps2 = D3DCAPS2_CANRENDERWINDOWED;
399     pCaps->Caps3 = D3DDEVCAPS_HWTRANSFORMANDLIGHT;
400     pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE;
401
402     pCaps->CursorCaps = 0;
403
404     pCaps->DevCaps = D3DDEVCAPS_DRAWPRIMTLVERTEX    | 
405                      D3DDEVCAPS_HWTRANSFORMANDLIGHT |
406                      D3DDEVCAPS_PUREDEVICE;
407
408     pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_CULLCCW               | 
409                                D3DPMISCCAPS_CULLCW                | 
410                                D3DPMISCCAPS_COLORWRITEENABLE      |
411                                D3DPMISCCAPS_CLIPTLVERTS           |
412                                D3DPMISCCAPS_CLIPPLANESCALEDPOINTS | 
413                                D3DPMISCCAPS_MASKZ; 
414                                /*NOT: D3DPMISCCAPS_TSSARGTEMP*/
415
416     pCaps->RasterCaps = D3DPRASTERCAPS_DITHER   | 
417                         D3DPRASTERCAPS_PAT      | 
418                         D3DPRASTERCAPS_FOGRANGE;
419                         /* FIXME Add:
420                            D3DPRASTERCAPS_FOGVERTEX
421                            D3DPRASTERCAPS_FOGTABLE
422                            D3DPRASTERCAPS_MIPMAPLODBIAS
423                            D3DPRASTERCAPS_ZBIAS
424                            D3DPRASTERCAPS_ANISOTROPY
425                            D3DPRASTERCAPS_WFOG
426                            D3DPRASTERCAPS_ZFOG 
427                            D3DPRASTERCAPS_COLORPERSPECTIVE
428                            D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE
429                            D3DPRASTERCAPS_ANTIALIASEDGES
430                            D3DPRASTERCAPS_ZBUFFERLESSHSR
431                            D3DPRASTERCAPS_WBUFFER */
432
433     pCaps->ZCmpCaps = D3DPCMPCAPS_ALWAYS       | 
434                       D3DPCMPCAPS_EQUAL        | 
435                       D3DPCMPCAPS_GREATER      | 
436                       D3DPCMPCAPS_GREATEREQUAL |
437                       D3DPCMPCAPS_LESS         | 
438                       D3DPCMPCAPS_LESSEQUAL    | 
439                       D3DPCMPCAPS_NEVER        |
440                       D3DPCMPCAPS_NOTEQUAL;
441
442     pCaps->SrcBlendCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
443     pCaps->DestBlendCaps = 0xFFFFFFFF;   /*FIXME: Tidy up later */
444     pCaps->AlphaCmpCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
445
446     pCaps->ShadeCaps = D3DPSHADECAPS_SPECULARGOURAUDRGB | 
447                        D3DPSHADECAPS_COLORGOURAUDRGB;
448
449     pCaps->TextureCaps =  D3DPTEXTURECAPS_ALPHA        | 
450                           D3DPTEXTURECAPS_ALPHAPALETTE | 
451                           D3DPTEXTURECAPS_POW2         | 
452                           D3DPTEXTURECAPS_VOLUMEMAP    | 
453                           D3DPTEXTURECAPS_MIPMAP;
454 #if defined(GL_VERSION_1_3) || defined(GL_ARB_texture_cube_map)
455     pCaps->TextureCaps |= D3DPTEXTURECAPS_CUBEMAP      | 
456                           D3DPTEXTURECAPS_MIPCUBEMAP   | 
457                           D3DPTEXTURECAPS_CUBEMAP_POW2;
458 #endif
459
460     pCaps->TextureFilterCaps = D3DPTFILTERCAPS_MAGFLINEAR | 
461                                D3DPTFILTERCAPS_MAGFPOINT  | 
462                                D3DPTFILTERCAPS_MINFLINEAR | 
463                                D3DPTFILTERCAPS_MINFPOINT  |
464                                D3DPTFILTERCAPS_MIPFLINEAR | 
465                                D3DPTFILTERCAPS_MIPFPOINT;
466
467     pCaps->CubeTextureFilterCaps = 0;
468     pCaps->VolumeTextureFilterCaps = 0;
469
470     pCaps->TextureAddressCaps =  D3DPTADDRESSCAPS_BORDER | 
471                                  D3DPTADDRESSCAPS_CLAMP  | 
472                                  D3DPTADDRESSCAPS_WRAP;
473 #if defined(GL_VERSION_1_3)
474     pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRROR;
475 #endif
476                                  /* FIXME: Add 
477                                     D3DPTADDRESSCAPS_BORDER
478                                     D3DPTADDRESSCAPS_MIRRORONCE */
479
480     pCaps->VolumeTextureAddressCaps = 0;
481
482     pCaps->LineCaps = D3DLINECAPS_TEXTURE | 
483                       D3DLINECAPS_ZTEST;
484                       /* FIXME: Add 
485                          D3DLINECAPS_BLEND
486                          D3DLINECAPS_ALPHACMP
487                          D3DLINECAPS_FOG */
488
489     {
490       GLint gl_tex_size;    
491       glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_tex_size);
492       pCaps->MaxTextureWidth = gl_tex_size;
493       pCaps->MaxTextureHeight = gl_tex_size;
494     }
495
496     pCaps->MaxVolumeExtent = 0;
497
498     pCaps->MaxTextureRepeat = 32768;
499     pCaps->MaxTextureAspectRatio = 32768;
500     pCaps->MaxVertexW = 1.0;
501
502     pCaps->GuardBandLeft = 0;
503     pCaps->GuardBandTop = 0;
504     pCaps->GuardBandRight = 0;
505     pCaps->GuardBandBottom = 0;
506
507     pCaps->ExtentsAdjust = 0;
508
509     pCaps->StencilCaps =  D3DSTENCILCAPS_DECRSAT | 
510                           D3DSTENCILCAPS_INCRSAT | 
511                           D3DSTENCILCAPS_INVERT  | 
512                           D3DSTENCILCAPS_KEEP    | 
513                           D3DSTENCILCAPS_REPLACE | 
514                           D3DSTENCILCAPS_ZERO;
515 #if defined(GL_VERSION_1_4) || defined(GL_EXT_stencil_wrap)
516     pCaps->StencilCaps |= D3DSTENCILCAPS_DECR    | 
517                           D3DSTENCILCAPS_INCR;
518 #endif
519
520     pCaps->FVFCaps = D3DFVFCAPS_PSIZE | 0x80000;
521
522     pCaps->TextureOpCaps =  D3DTEXOPCAPS_ADD         | 
523                             D3DTEXOPCAPS_ADDSIGNED   | 
524                             D3DTEXOPCAPS_ADDSIGNED2X |
525                             D3DTEXOPCAPS_MODULATE    | 
526                             D3DTEXOPCAPS_MODULATE2X  | 
527                             D3DTEXOPCAPS_MODULATE4X  |
528                             D3DTEXOPCAPS_SELECTARG1  | 
529                             D3DTEXOPCAPS_SELECTARG2  | 
530                             D3DTEXOPCAPS_DISABLE;
531 #if defined(GL_VERSION_1_3)
532     pCaps->TextureOpCaps |= D3DTEXOPCAPS_DOTPRODUCT3 | 
533                             D3DTEXOPCAPS_SUBTRACT;
534 #endif
535                             /* FIXME: Add 
536                               D3DTEXOPCAPS_ADDSMOOTH
537                               D3DTEXOPCAPS_BLENDCURRENTALPHA 
538                               D3DTEXOPCAPS_BLENDDIFFUSEALPHA 
539                               D3DTEXOPCAPS_BLENDFACTORALPHA 
540                               D3DTEXOPCAPS_BLENDTEXTUREALPHA 
541                               D3DTEXOPCAPS_BLENDTEXTUREALPHAPM 
542                               D3DTEXOPCAPS_BUMPENVMAP
543                               D3DTEXOPCAPS_BUMPENVMAPLUMINANCE 
544                               D3DTEXOPCAPS_LERP 
545                               D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR 
546                               D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA 
547                               D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR 
548                               D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
549                               D3DTEXOPCAPS_MULTIPLYADD 
550                               D3DTEXOPCAPS_PREMODULATE */
551
552     {
553         GLint gl_max;
554         GLfloat gl_float;
555
556 #if defined(GL_VERSION_1_3)
557         glGetIntegerv(GL_MAX_TEXTURE_UNITS, &gl_max);
558 #else
559         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
560 #endif
561         TRACE("GLCaps: GL_MAX_TEXTURE_UNITS_ARB=%d\n", gl_max);
562         pCaps->MaxTextureBlendStages = min(8, gl_max);
563         pCaps->MaxSimultaneousTextures = min(8, gl_max);
564
565         glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
566         pCaps->MaxUserClipPlanes = min(MAX_CLIPPLANES, gl_max);
567         TRACE("GLCaps: GL_MAX_CLIP_PLANES=%ld\n", pCaps->MaxUserClipPlanes);
568
569         glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
570         pCaps->MaxActiveLights = min(MAX_ACTIVE_LIGHTS, gl_max);
571         TRACE("GLCaps: GL_MAX_LIGHTS=%ld\n", pCaps->MaxActiveLights);
572
573 #if defined(GL_ARB_vertex_blend)
574         glGetIntegerv(GL_MAX_VERTEX_UNITS_ARB, &gl_max);
575         pCaps->MaxVertexBlendMatrices = gl_max;
576         pCaps->MaxVertexBlendMatrixIndex = 1;
577 #else
578         pCaps->MaxVertexBlendMatrices = 0;
579         pCaps->MaxVertexBlendMatrixIndex = 1;
580 #endif
581
582 #if defined(GL_EXT_texture_filter_anisotropic)
583         glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_max);
584         pCaps->MaxAnisotropy = gl_max;
585 #else
586         pCaps->MaxAnisotropy = 0;
587 #endif
588
589         glGetFloatv(GL_POINT_SIZE_RANGE, &gl_float);
590         pCaps->MaxPointSize = gl_float;
591     }
592
593     pCaps->VertexProcessingCaps = D3DVTXPCAPS_DIRECTIONALLIGHTS | 
594                                   D3DVTXPCAPS_MATERIALSOURCE7   | 
595                                   D3DVTXPCAPS_POSITIONALLIGHTS  | 
596                                   D3DVTXPCAPS_TEXGEN;
597                                   /* FIXME: Add 
598                                      D3DVTXPCAPS_LOCALVIEWER 
599                                      D3DVTXPCAPS_TWEENING */
600
601     pCaps->MaxPrimitiveCount = 0xFFFFFFFF;
602     pCaps->MaxVertexIndex = 0xFFFFFFFF;
603     pCaps->MaxStreams = MAX_STREAMS;
604     pCaps->MaxStreamStride = 1024;
605
606 #if 1
607     pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
608     pCaps->MaxVertexShaderConst = D3D8_VSHADER_MAX_CONSTANTS;
609 #else
610     pCaps->VertexShaderVersion = 0;
611     pCaps->MaxVertexShaderConst = D3D8_VSHADER_MAX_CONSTANTS;
612 #endif
613
614 #if 0
615     pCaps->PixelShaderVersion = D3DPS_VERSION(1,1);
616     pCaps->MaxPixelShaderValue = 1.0;
617 #else
618     pCaps->PixelShaderVersion = 0;
619     pCaps->MaxPixelShaderValue = 0.0;
620 #endif
621
622     return D3D_OK;
623 }
624
625 HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
626     ICOM_THIS(IDirect3D8Impl,iface);
627     FIXME("(%p)->(Adptr:%d)\n", This, Adapter);
628     return D3D_OK;
629 }
630
631
632 static void IDirect3D8Impl_FillGLCaps(LPDIRECT3D8 iface, Display* display) {
633     const char *GL_Extensions = NULL;
634     const char *GLX_Extensions = NULL;
635     GLint gl_max;
636     ICOM_THIS(IDirect3D8Impl,iface);
637
638     /* 
639      * Initialize openGL extension related variables
640      *  with Default values 
641      */
642     memset(&This->gl_info.supported, 0, sizeof(This->gl_info.supported));
643     This->gl_info.max_textures   = 1;
644     This->gl_info.ps_arb_version = PS_VERSION_NOT_SUPPORTED;
645     This->gl_info.vs_arb_version = VS_VERSION_NOT_SUPPORTED;
646     This->gl_info.vs_nv_version  = VS_VERSION_NOT_SUPPORTED;
647     This->gl_info.vs_ati_version = VS_VERSION_NOT_SUPPORTED;
648
649 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = NULL;
650     GL_EXT_FUNCS_GEN;
651 #undef USE_GL_FUNC
652
653     /* Retrieve opengl defaults */
654     glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
655     This->gl_info.max_clipplanes = min(MAX_CLIPPLANES, gl_max);
656     TRACE("ClipPlanes support - num Planes=%d\n", gl_max);
657
658     glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
659     This->gl_info.max_lights = min(MAX_ACTIVE_LIGHTS, gl_max);
660     TRACE("Lights support - max lights=%d\n", gl_max);
661
662     /* Parse the gl supported features, in theory enabling parts of our code appropriately */
663     GL_Extensions = glGetString(GL_EXTENSIONS);
664     FIXME("GL_Extensions reported:\n");  
665     
666     if (NULL == GL_Extensions) {
667       ERR("   GL_Extensions returns NULL\n");      
668     } else {
669       while (*GL_Extensions != 0x00) {
670         const char *Start = GL_Extensions;
671         char ThisExtn[256];
672
673         memset(ThisExtn, 0x00, sizeof(ThisExtn));
674         while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
675           GL_Extensions++;
676         }
677         memcpy(ThisExtn, Start, (GL_Extensions - Start));
678         FIXME("- %s\n", ThisExtn);
679
680         /**
681          * ARB 
682          */
683         if (strcmp(ThisExtn, "GL_ARB_fragment_program") == 0) {
684           This->gl_info.ps_arb_version = PS_VERSION_11;
685           FIXME(" FOUND: ARB Pixel Shader support - version=%02x\n", This->gl_info.ps_arb_version);
686           This->gl_info.supported[ARB_FRAGMENT_PROGRAM] = TRUE;
687         } else if (strcmp(ThisExtn, "GL_ARB_multisample") == 0) {
688           FIXME(" FOUND: ARB Multisample support\n");
689           This->gl_info.supported[ARB_MULTISAMPLE] = TRUE;
690         } else if (strcmp(ThisExtn, "GL_ARB_multitexture") == 0) {
691           glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
692           FIXME(" FOUND: ARB Multitexture support - GL_MAX_TEXTURE_UNITS_ARB=%u\n", gl_max);
693           This->gl_info.supported[ARB_MULTITEXTURE] = TRUE;
694           This->gl_info.max_textures = min(8, gl_max);
695         } else if (strcmp(ThisExtn, "GL_ARB_texture_cube_map") == 0) {
696           FIXME(" FOUND: ARB Texture Cube Map support\n");
697           This->gl_info.supported[ARB_TEXTURE_CUBE_MAP] = TRUE;
698         } else if (strcmp(ThisExtn, "GL_ARB_texture_compression") == 0) {
699           FIXME(" FOUND: ARB Texture Compression support\n");
700           This->gl_info.supported[ARB_TEXTURE_COMPRESSION] = TRUE;
701         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_combine") == 0) {
702           FIXME(" FOUND: EXT Texture Env combine support\n");
703           This->gl_info.supported[ARB_TEXTURE_ENV_COMBINE] = TRUE;
704         } else if (strcmp(ThisExtn, "GL_ARB_texture_env_dot3") == 0) {
705           if (FALSE == This->gl_info.supported[ARB_TEXTURE_ENV_DOT3]) {
706             FIXME(" FOUND: EXT Dot3 support\n");
707             This->gl_info.supported[ARB_TEXTURE_ENV_DOT3] = TRUE;
708           }
709         } else if (strstr(ThisExtn, "GL_ARB_vertex_program")) {
710           This->gl_info.vs_arb_version = VS_VERSION_11;
711           FIXME(" FOUND: ARB Vertex Shader support - version=%02x\n", This->gl_info.vs_arb_version);
712           This->gl_info.supported[ARB_VERTEX_PROGRAM] = TRUE;
713
714         /**
715          * EXT
716          */
717         } else if (strcmp(ThisExtn, "GL_EXT_fog_coord") == 0) {
718           FIXME(" FOUND: EXT Fog coord support\n");
719           This->gl_info.supported[EXT_FOG_COORD] = TRUE;
720         } else if (strcmp(ThisExtn, "GL_EXT_paletted_texture") == 0) { /* handle paletted texture extensions */
721           FIXME(" FOUND: EXT Paletted texture support\n");
722           This->gl_info.supported[EXT_PALETTED_TEXTURE] = TRUE;
723         } else if (strcmp(ThisExtn, "GL_EXT_point_parameters") == 0) {
724           FIXME(" FOUND: EXT Point parameters support\n");
725           This->gl_info.supported[EXT_POINT_PARAMETERS] = TRUE;
726         } else if (strcmp(ThisExtn, "GL_EXT_secondary_color") == 0) {
727           FIXME(" FOUND: EXT Secondary coord support\n");
728           This->gl_info.supported[EXT_SECONDARY_COLOR] = TRUE;
729         } else if (strcmp(ThisExtn, "GL_EXT_texture_compression_s3tc") == 0) {
730           FIXME(" FOUND: EXT Texture S3TC compression support\n");
731           This->gl_info.supported[EXT_TEXTURE_COMPRESSION_S3TC] = TRUE;
732         } else if (strcmp(ThisExtn, "GL_EXT_texture_env_dot3") == 0) {
733           if (FALSE == This->gl_info.supported[ARB_TEXTURE_ENV_DOT3]) {
734             FIXME(" FOUND: EXT Dot3 support\n");
735             This->gl_info.supported[ARB_TEXTURE_ENV_DOT3] = TRUE;
736           }
737         } else if (strcmp(ThisExtn, "GL_EXT_texture_filter_anisotropic") == 0) {
738           FIXME(" FOUND: EXT Texture Anisotropic filter support\n");
739           This->gl_info.supported[EXT_TEXTURE_FILTER_ANISOTROPIC] = TRUE;
740         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod") == 0) {
741           FIXME(" FOUND: EXT Texture LOD support\n");
742           This->gl_info.supported[EXT_TEXTURE_LOD] = TRUE;
743         } else if (strcmp(ThisExtn, "GL_EXT_texture_lod_bias") == 0) {
744           FIXME(" FOUND: EXT Texture LOD bias support\n");
745           This->gl_info.supported[EXT_TEXTURE_LOD_BIAS] = TRUE;
746         } else if (strcmp(ThisExtn, "GL_EXT_vertex_weighting") == 0) {
747           FIXME(" FOUND: EXT Vertex weighting support\n");
748           This->gl_info.supported[EXT_VERTEX_WEIGHTING] = TRUE;
749
750         /**
751          * NVIDIA 
752          */
753         } else if (strstr(ThisExtn, "GL_NV_fragment_program")) {
754           This->gl_info.ps_nv_version = PS_VERSION_11;
755           FIXME(" FOUND: NVIDIA (NV) Pixel Shader support - version=%02x\n", This->gl_info.ps_nv_version);
756           This->gl_info.supported[NV_FRAGMENT_PROGRAM] = TRUE;
757         } else if (strstr(ThisExtn, "GL_NV_vertex_program")) {
758           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);
759           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);
760           FIXME(" FOUND: NVIDIA (NV) Vertex Shader support - version=%02x\n", This->gl_info.vs_nv_version);
761           This->gl_info.supported[NV_VERTEX_PROGRAM] = TRUE;
762
763         /**
764          * ATI
765          */
766         /** TODO */
767         } else if (strcmp(ThisExtn, "GL_EXT_vertex_shader") == 0) {
768           This->gl_info.vs_ati_version = VS_VERSION_11;
769           FIXME(" FOUND: ATI (EXT) Vertex Shader support - version=%02x\n", This->gl_info.vs_ati_version);
770           This->gl_info.supported[EXT_VERTEX_SHADER] = TRUE;
771         }
772
773
774         if (*GL_Extensions == ' ') GL_Extensions++;
775       }
776     }
777
778 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
779     GL_EXT_FUNCS_GEN;
780 #undef USE_GL_FUNC
781
782     GLX_Extensions = glXQueryExtensionsString(display, DefaultScreen(display));
783     FIXME("GLX_Extensions reported:\n");  
784     
785     if (NULL == GLX_Extensions) {
786       ERR("   GLX_Extensions returns NULL\n");      
787     } else {
788       while (*GLX_Extensions != 0x00) {
789         const char *Start = GLX_Extensions;
790         char ThisExtn[256];
791         
792         memset(ThisExtn, 0x00, sizeof(ThisExtn));
793         while (*GLX_Extensions != ' ' && *GLX_Extensions != 0x00) {
794           GLX_Extensions++;
795         }
796         memcpy(ThisExtn, Start, (GLX_Extensions - Start));
797         FIXME("- %s\n", ThisExtn);
798         if (*GLX_Extensions == ' ') GLX_Extensions++;
799       }
800     }
801
802 #define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
803     GLX_EXT_FUNCS_GEN;
804 #undef USE_GL_FUNC
805
806 }
807
808 HRESULT  WINAPI  IDirect3D8Impl_CreateDevice               (LPDIRECT3D8 iface,
809                                                             UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
810                                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
811                                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
812     IDirect3DDevice8Impl *object;
813     HWND whichHWND;
814     int num;
815     XVisualInfo template;
816
817     ICOM_THIS(IDirect3D8Impl,iface);
818     TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
819           hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);
820
821     /* Allocate the storage for the device */
822     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
823     if (NULL == object) {
824       return D3DERR_OUTOFVIDEOMEMORY;
825     }
826     object->lpVtbl = &Direct3DDevice8_Vtbl;
827     object->ref = 1;
828     object->direct3d8 = This;
829     /** The device AddRef the direct3d8 Interface else crash in propers clients codes */
830     IDirect3D8_AddRef((LPDIRECT3D8) object->direct3d8);
831
832     /** use StateBlock Factory here, for creating the startup stateBlock */
833     object->StateBlock = NULL;
834     IDirect3DDeviceImpl_CreateStateBlock(object, D3DSBT_ALL, NULL);
835     object->UpdateStateBlock = object->StateBlock;
836
837     /* Save the creation parameters */
838     object->CreateParms.AdapterOrdinal = Adapter;
839     object->CreateParms.DeviceType = DeviceType;
840     object->CreateParms.hFocusWindow = hFocusWindow;
841     object->CreateParms.BehaviorFlags = BehaviourFlags;
842
843     *ppReturnedDeviceInterface = (LPDIRECT3DDEVICE8) object;
844
845     /* Initialize settings */
846     object->PresentParms.BackBufferCount = 1; /* Opengl only supports one? */
847     object->adapterNo = Adapter;
848     object->devType = DeviceType;
849
850     /* Initialize openGl */
851     {
852         HDC hDc;
853         /*int dblBuf[] = {GLX_STENCIL_SIZE,8,GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};*/
854         int dblBuf[] = {GLX_RGBA, 
855                         GLX_STENCIL_SIZE, 8, /*  2 */
856                         GLX_DEPTH_SIZE,  16, /*  4 */
857                         GLX_DOUBLEBUFFER, None};
858         /* FIXME: Handle stencil appropriately via EnableAutoDepthStencil / AutoDepthStencilFormat */
859
860         /* Which hwnd are we using? */
861 /*      if (pPresentationParameters->Windowed) { */
862            whichHWND = pPresentationParameters->hDeviceWindow;
863            if (!whichHWND) {
864                whichHWND = hFocusWindow;
865            }
866            object->win_handle = whichHWND;
867            object->win     = (Window)GetPropA( whichHWND, "__wine_x11_client_window" );
868 /*
869  *      } else {
870  *           whichHWND       = (HWND) GetDesktopWindow();
871  *           object->win     = (Window)GetPropA(whichHWND, "__wine_x11_whole_window" );
872  *         root_window
873  *        }
874  */
875
876         hDc = GetDC(whichHWND);
877         object->display = get_display(hDc);
878
879
880         TRACE("(%p)->(DepthStencil:(%u,%s), BackBufferFormat:(%u,%s))\n", This, 
881               pPresentationParameters->AutoDepthStencilFormat, debug_d3dformat(pPresentationParameters->AutoDepthStencilFormat),
882               pPresentationParameters->BackBufferFormat, debug_d3dformat(pPresentationParameters->BackBufferFormat));
883
884         
885 #if 0
886         if (TRUE == pPresentationParameters->EnableAutoDepthStencil) {
887           switch (pPresentationParameters->AutoDepthStencilFormat) {
888           case D3DFMT_D16_LOCKABLE: dblBuf[2] =  8; dblBuf[4] = 16; break;
889           case D3DFMT_D16:          dblBuf[2] =  8; dblBuf[4] = 16; break;
890           case D3DFMT_D15S1:        dblBuf[2] =  1; dblBuf[4] = 16; break;
891           case D3DFMT_D24X4S4:      dblBuf[2] =  4; dblBuf[4] = 24; break;
892           case D3DFMT_D24S8:        dblBuf[2] =  8; dblBuf[4] = 24; break;
893           case D3DFMT_D24X8:        dblBuf[2] =  8; dblBuf[4] = 24; break;
894           case D3DFMT_D32:          dblBuf[2] =  8; dblBuf[4] = 32; break;
895           default:                  dblBuf[2] =  8; dblBuf[4] = 16; break;
896           }
897         }
898         
899         switch (pPresentationParameters->BackBufferFormat) {
900         case D3DFMT_R3G3B2:   dblBuf[6] = 3; dblBuf[8] = 3; dblBuf[10] = 2; dblBuf[12] = 0; break;
901         case D3DFMT_R5G6B5:   dblBuf[6] = 5; dblBuf[8] = 6; dblBuf[10] = 5; dblBuf[12] = 0; break;
902         case D3DFMT_X1R5G5B5: dblBuf[6] = 5; dblBuf[8] = 5; dblBuf[10] = 5; dblBuf[12] = 0; break;
903         case D3DFMT_A1R5G5B5: dblBuf[6] = 5; dblBuf[8] = 5; dblBuf[10] = 5; dblBuf[12] = 1; break;
904         case D3DFMT_X4R4G4B4: dblBuf[6] = 4; dblBuf[8] = 4; dblBuf[10] = 4; dblBuf[12] = 0; break;
905         case D3DFMT_R8G8B8:   dblBuf[6] = 8; dblBuf[8] = 8; dblBuf[10] = 8; dblBuf[12] = 0; break;
906         case D3DFMT_X8R8G8B8: dblBuf[6] = 8; dblBuf[8] = 8; dblBuf[10] = 8; dblBuf[12] = 0; break;
907         case D3DFMT_A8R8G8B8: dblBuf[6] = 8; dblBuf[8] = 8; dblBuf[10] = 8; dblBuf[12] = 8; break;
908         default:              dblBuf[6] = 5; dblBuf[8] = 6; dblBuf[10] = 5; dblBuf[12] = 0; break;
909         }
910 #endif
911
912         ENTER_GL();
913         object->visInfo = glXChooseVisual(object->display, DefaultScreen(object->display), dblBuf);
914         if (NULL == object->visInfo) {
915           FIXME("cannot choose needed glxVisual with Stencil Buffer\n"); 
916
917           /**
918            * second try using wine initialized visual ...
919            * must be fixed reworking wine-glx init
920            */
921           template.visualid = (VisualID)GetPropA(GetDesktopWindow(), "__wine_x11_visual_id");
922           object->visInfo = XGetVisualInfo(object->display, VisualIDMask, &template, &num);
923           if (NULL == object->visInfo) {
924             ERR("cannot really get XVisual\n"); 
925             LEAVE_GL();
926             return D3DERR_NOTAVAILABLE;
927           }
928         }
929         object->glCtx = glXCreateContext(object->display, object->visInfo, NULL, GL_TRUE);
930         if (NULL == object->glCtx) {
931           ERR("cannot create glxContext\n"); 
932           LEAVE_GL();
933           return D3DERR_NOTAVAILABLE;
934         }
935         LEAVE_GL();
936
937         ReleaseDC(whichHWND, hDc);
938     }
939
940     if (object->glCtx == NULL) {
941         ERR("Error in context creation !\n");
942         return D3DERR_INVALIDCALL;
943     } else {
944         TRACE("Context created (HWND=%p, glContext=%p, Window=%ld, VisInfo=%p)\n",
945               whichHWND, object->glCtx, object->win, object->visInfo);
946     }
947
948     /* If not windowed, need to go fullscreen, and resize the HWND to the appropriate  */
949     /*        dimensions                                                               */
950     if (!pPresentationParameters->Windowed) {
951 #if 0
952         DEVMODEW devmode;
953         HDC hdc;
954         int bpp = 0;
955         memset(&devmode, 0, sizeof(DEVMODEW));
956         devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; 
957         MultiByteToWideChar(CP_ACP, 0, "Gamers CG", -1, devmode.dmDeviceName, CCHDEVICENAME);
958         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
959         bpp = GetDeviceCaps(hdc, BITSPIXEL);
960         DeleteDC(hdc);
961         devmode.dmBitsPerPel = (bpp >= 24) ? 32 : bpp;/*Stupid XVidMode cannot change bpp D3DFmtGetBpp(object, pPresentationParameters->BackBufferFormat);*/
962         devmode.dmPelsWidth  = pPresentationParameters->BackBufferWidth;
963         devmode.dmPelsHeight = pPresentationParameters->BackBufferHeight;
964         ChangeDisplaySettingsExW(devmode.dmDeviceName, &devmode, object->win_handle, CDS_FULLSCREEN, NULL);
965 #else
966         FIXME("Requested full screen support not implemented, expect windowed operation\n");
967 #endif
968
969         /* Make popup window */
970         ShowWindow(whichHWND, SW_HIDE);
971         SetWindowLongA(whichHWND, GWL_STYLE, WS_POPUP);
972         SetWindowPos(object->win_handle, HWND_TOP, 0, 0, 
973                      pPresentationParameters->BackBufferWidth,
974                      pPresentationParameters->BackBufferHeight, SWP_SHOWWINDOW | SWP_FRAMECHANGED);
975         ShowWindow(whichHWND, SW_SHOW);
976     }
977
978     TRACE("Creating back buffer\n");
979     /* MSDN: If Windowed is TRUE and either of the BackBufferWidth/Height values is zero,
980        then the corresponding dimension of the client area of the hDeviceWindow
981        (or the focus window, if hDeviceWindow is NULL) is taken. */
982     if (pPresentationParameters->Windowed && ((pPresentationParameters->BackBufferWidth  == 0) ||
983                                               (pPresentationParameters->BackBufferHeight == 0))) {
984         RECT Rect;
985
986         GetClientRect(whichHWND, &Rect);
987
988         if (pPresentationParameters->BackBufferWidth == 0) {
989            pPresentationParameters->BackBufferWidth = Rect.right;
990            TRACE("Updating width to %d\n", pPresentationParameters->BackBufferWidth);
991         }
992         if (pPresentationParameters->BackBufferHeight == 0) {
993            pPresentationParameters->BackBufferHeight = Rect.bottom;
994            TRACE("Updating height to %d\n", pPresentationParameters->BackBufferHeight);
995         }
996     }
997
998     /* Save the presentation parms now filled in correctly */
999     memcpy(&object->PresentParms, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
1000
1001
1002     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
1003                                             pPresentationParameters->BackBufferWidth,
1004                                             pPresentationParameters->BackBufferHeight,
1005                                             pPresentationParameters->BackBufferFormat,
1006                                             pPresentationParameters->MultiSampleType,
1007                                             TRUE,
1008                                             (LPDIRECT3DSURFACE8*) &object->frontBuffer);
1009
1010     IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
1011                                             pPresentationParameters->BackBufferWidth,
1012                                             pPresentationParameters->BackBufferHeight,
1013                                             pPresentationParameters->BackBufferFormat,
1014                                             pPresentationParameters->MultiSampleType,
1015                                             TRUE,
1016                                             (LPDIRECT3DSURFACE8*) &object->backBuffer);
1017
1018     if (pPresentationParameters->EnableAutoDepthStencil) {
1019        IDirect3DDevice8Impl_CreateDepthStencilSurface((LPDIRECT3DDEVICE8) object,
1020                                                       pPresentationParameters->BackBufferWidth,
1021                                                       pPresentationParameters->BackBufferHeight,
1022                                                       pPresentationParameters->AutoDepthStencilFormat,
1023                                                       D3DMULTISAMPLE_NONE,
1024                                                       (LPDIRECT3DSURFACE8*) &object->depthStencilBuffer);
1025     } else {
1026       object->depthStencilBuffer = NULL;
1027     }
1028
1029     /* init the default renderTarget management */
1030     object->drawable = object->win;
1031     object->render_ctx = object->glCtx;
1032     object->renderTarget = object->frontBuffer;
1033     IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->renderTarget);
1034     object->stencilBufferTarget = object->depthStencilBuffer;
1035     if (NULL != object->stencilBufferTarget) {
1036       IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->stencilBufferTarget);
1037     }
1038
1039     ENTER_GL();
1040
1041     if (glXMakeCurrent(object->display, object->win, object->glCtx) == False) {
1042       ERR("Error in setting current context (context %p drawable %ld)!\n", object->glCtx, object->win);
1043     }
1044     checkGLcall("glXMakeCurrent");
1045
1046     /* Clear the screen */
1047     glClearColor(1.0, 0.0, 0.0, 0.0);
1048     checkGLcall("glClearColor");
1049     glColor3f(1.0, 1.0, 1.0);
1050     checkGLcall("glColor3f");
1051
1052     glEnable(GL_LIGHTING);
1053     checkGLcall("glEnable");
1054
1055     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1056     checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1057
1058     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1059     checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1060
1061     glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1062     checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1063
1064     /* 
1065      * Initialize openGL extension related variables
1066      *  with Default values 
1067      */
1068     IDirect3D8Impl_FillGLCaps(iface, object->display);
1069
1070     /* Setup all the devices defaults */
1071     IDirect3DDeviceImpl_InitStartupStateBlock(object);
1072
1073     LEAVE_GL();
1074
1075     { /* Set a default viewport */
1076        D3DVIEWPORT8 vp;
1077        vp.X      = 0;
1078        vp.Y      = 0;
1079        vp.Width  = pPresentationParameters->BackBufferWidth;
1080        vp.Height = pPresentationParameters->BackBufferHeight;
1081        vp.MinZ   = 0.0f;
1082        vp.MaxZ   = 1.0f;
1083        IDirect3DDevice8Impl_SetViewport((LPDIRECT3DDEVICE8) object, &vp);
1084     }
1085
1086     TRACE("(%p,%d) All defaults now set up, leaving CreateDevice\n", This, Adapter);
1087     return D3D_OK;
1088 }
1089
1090 ICOM_VTABLE(IDirect3D8) Direct3D8_Vtbl =
1091 {
1092     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1093     IDirect3D8Impl_QueryInterface,
1094     IDirect3D8Impl_AddRef,
1095     IDirect3D8Impl_Release,
1096     IDirect3D8Impl_RegisterSoftwareDevice,
1097     IDirect3D8Impl_GetAdapterCount,
1098     IDirect3D8Impl_GetAdapterIdentifier,
1099     IDirect3D8Impl_GetAdapterModeCount,
1100     IDirect3D8Impl_EnumAdapterModes,
1101     IDirect3D8Impl_GetAdapterDisplayMode,
1102     IDirect3D8Impl_CheckDeviceType,
1103     IDirect3D8Impl_CheckDeviceFormat,
1104     IDirect3D8Impl_CheckDeviceMultiSampleType,
1105     IDirect3D8Impl_CheckDepthStencilMatch,
1106     IDirect3D8Impl_GetDeviceCaps,
1107     IDirect3D8Impl_GetAdapterMonitor,
1108     IDirect3D8Impl_CreateDevice
1109 };