Fixed a race condition on RPC worker thread creation, and a typo.
[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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/debug.h"
28
29 #include "config.h"
30 #include "x11drv.h"
31
32 #include "d3d8_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
35
36 #define NUM_MODES 10
37 static const int modes[NUM_MODES][3] = {
38     {640, 480, 85},
39     {800, 600, 85},
40     {1024, 768, 85},
41     {1152, 864, 85},
42     {1280, 768, 85},
43     {1280, 960, 85},
44     {1280, 1024, 85},
45     {1600, 900, 85},
46     {1600, 1024, 85},
47     {1600, 1200, 85}
48 };
49
50 /* retrieve the X display to use on a given DC */
51 inline static Display *get_display( HDC hdc )
52 {
53     Display *display;
54     enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;
55
56     if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
57                     sizeof(display), (LPSTR)&display )) display = NULL;
58     return display;
59 }
60
61
62 /* IDirect3D IUnknown parts follow: */
63 HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface,REFIID riid,LPVOID *ppobj)
64 {
65     ICOM_THIS(IDirect3D8Impl,iface);
66
67     if (IsEqualGUID(riid, &IID_IUnknown)
68         || IsEqualGUID(riid, &IID_IDirect3D8)) {
69         IDirect3D8Impl_AddRef(iface);
70         *ppobj = This;
71         return D3D_OK;
72     }
73
74     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
75     return E_NOINTERFACE;
76 }
77
78 ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
79     ICOM_THIS(IDirect3D8Impl,iface);
80     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
81     return ++(This->ref);
82 }
83
84 ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
85     ICOM_THIS(IDirect3D8Impl,iface);
86     ULONG ref = --This->ref;
87     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
88     if (ref == 0)
89         HeapFree(GetProcessHeap(), 0, This);
90     return ref;
91 }
92
93 /* IDirect3D Interface follow: */
94 HRESULT  WINAPI  IDirect3D8Impl_RegisterSoftwareDevice     (LPDIRECT3D8 iface, void* pInitializeFunction) {
95     ICOM_THIS(IDirect3D8Impl,iface);
96     FIXME("(%p)->(%p): stub\n", This, pInitializeFunction);
97     return D3D_OK;
98 }
99
100 UINT     WINAPI  IDirect3D8Impl_GetAdapterCount            (LPDIRECT3D8 iface) {
101     ICOM_THIS(IDirect3D8Impl,iface);
102     /* FIXME: Set to one for now to imply the display */
103     TRACE("(%p): Mostly stub, only returns primary display\n", This);
104     return 1;
105 }
106
107 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterIdentifier       (LPDIRECT3D8 iface,
108                                                             UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
109     ICOM_THIS(IDirect3D8Impl,iface);
110
111     TRACE("(%p}->(Adapter: %d, Flags: %lx, pId=%p)\n", This, Adapter, Flags, pIdentifier);
112
113     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
114         return D3DERR_INVALIDCALL;
115     }
116
117     if (Adapter == 0) { /* Display */
118         strcpy(pIdentifier->Driver, "Display");
119         strcpy(pIdentifier->Description, "Direct3D Display");
120         pIdentifier->DriverVersion.s.HighPart = 1;
121         pIdentifier->DriverVersion.s.LowPart = 0;
122         pIdentifier->VendorId = 0;
123         pIdentifier->DeviceId = 0;
124         pIdentifier->SubSysId = 0;
125         pIdentifier->Revision = 0;
126         /*FIXME: memcpy(&pIdentifier->DeviceIdentifier, ??, sizeof(??GUID)); */
127         if (Flags & D3DENUM_NO_WHQL_LEVEL ) {
128             pIdentifier->WHQLLevel = 0;
129         } else {
130             pIdentifier->WHQLLevel = 1;
131         }
132     } else {
133         FIXME("Adapter not primary display\n");
134     }
135
136     return D3D_OK;
137 }
138
139 UINT     WINAPI  IDirect3D8Impl_GetAdapterModeCount        (LPDIRECT3D8 iface,
140                                                             UINT Adapter) {
141     ICOM_THIS(IDirect3D8Impl,iface);
142
143     TRACE("(%p}->(Adapter: %d)\n", This, Adapter);
144
145     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
146         return D3DERR_INVALIDCALL;
147     }
148
149     if (Adapter == 0) { /* Display */
150         int maxWidth        = GetSystemMetrics(SM_CXSCREEN);
151         int maxHeight       = GetSystemMetrics(SM_CYSCREEN);
152         int i;
153
154         for (i=0; i<NUM_MODES; i++) {
155             if (modes[i][0] > maxWidth || modes[i][1] > maxHeight) {
156                 return i+1;
157             }
158         }
159         return NUM_MODES+1;
160     } else {
161         FIXME("Adapter not primary display\n");
162     }
163
164     return 0;
165 }
166
167 HRESULT  WINAPI  IDirect3D8Impl_EnumAdapterModes           (LPDIRECT3D8 iface,
168                                                             UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
169     ICOM_THIS(IDirect3D8Impl,iface);
170
171     TRACE("(%p}->(Adapter: %d, mode: %d, pMode=%p)\n", This, Adapter, Mode, pMode);
172
173     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
174         return D3DERR_INVALIDCALL;
175     }
176
177     if (Adapter == 0) { /* Display */
178         HDC hdc;
179         int bpp = 0;
180
181         if (Mode == 0) {
182             pMode->Width        = GetSystemMetrics(SM_CXSCREEN);
183             pMode->Height       = GetSystemMetrics(SM_CYSCREEN);
184             pMode->RefreshRate  = 85; /*FIXME: How to identify? */
185         } else if (Mode < (NUM_MODES+1)) {
186             pMode->Width        = modes[Mode-1][0];
187             pMode->Height       = modes[Mode-1][1];
188             pMode->RefreshRate  = modes[Mode-1][2];
189         } else {
190             TRACE("Requested mode out of range %d\n", Mode);
191             return D3DERR_INVALIDCALL;
192         }
193
194         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
195         bpp = GetDeviceCaps(hdc, BITSPIXEL);
196         DeleteDC(hdc);
197
198         switch (bpp) {
199         case  8: pMode->Format       = D3DFMT_R3G3B2;   break;
200         /*case 16: pMode->Format       = D3DFMT_A4R4G4B4; break;*/
201         case 16: pMode->Format       = D3DFMT_R5G6B5;   break;
202         case 24: pMode->Format       = D3DFMT_R8G8B8;   break;
203         case 32: pMode->Format       = D3DFMT_A8R8G8B8; break;
204         default: pMode->Format       = D3DFMT_UNKNOWN;
205         }
206         TRACE("W %d H %d rr %d fmt %x\n", pMode->Width, pMode->Height, pMode->RefreshRate, pMode->Format);
207
208     } else {
209         FIXME("Adapter not primary display\n");
210     }
211
212     return D3D_OK;
213 }
214
215 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterDisplayMode      (LPDIRECT3D8 iface,
216                                                             UINT Adapter, D3DDISPLAYMODE* pMode) {
217     ICOM_THIS(IDirect3D8Impl,iface);
218     TRACE("(%p}->(Adapter: %d, pMode: %p)\n", This, Adapter, pMode);
219
220     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
221         return D3DERR_INVALIDCALL;
222     }
223
224     if (Adapter == 0) { /* Display */
225         HDC hdc;
226         int bpp = 0;
227
228         pMode->Width        = GetSystemMetrics(SM_CXSCREEN);
229         pMode->Height       = GetSystemMetrics(SM_CYSCREEN);
230         pMode->RefreshRate  = 85; /*FIXME: How to identify? */
231
232         hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
233         bpp = GetDeviceCaps(hdc, BITSPIXEL);
234         DeleteDC(hdc);
235
236         switch (bpp) {
237         case  8: pMode->Format       = D3DFMT_R3G3B2;   break;
238         case 16: pMode->Format       = D3DFMT_R5G6B5;   break;
239         /*case 16: pMode->Format       = D3DFMT_A4R4G4B4; break;*/
240         case 24: pMode->Format       = D3DFMT_R8G8B8;   break;
241         case 32: pMode->Format       = D3DFMT_A8R8G8B8; break;
242         default: pMode->Format       = D3DFMT_UNKNOWN;
243         }
244
245     } else {
246         FIXME("Adapter not primary display\n");
247     }
248
249     TRACE("returning w:%d, h:%d, ref:%d, fmt:%x\n", pMode->Width,
250           pMode->Height, pMode->RefreshRate, pMode->Format);
251     return D3D_OK;
252 }
253
254 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
255                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
256                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
257     ICOM_THIS(IDirect3D8Impl,iface);
258     FIXME("(%p)->(Adptr:%d, CheckType:%x, DispFmt:%x, BackBuf:%x, Win? %d): stub\n", This, Adapter, CheckType,
259           DisplayFormat, BackBufferFormat, Windowed);
260     return D3D_OK;
261 }
262
263 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
264                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
265                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
266     ICOM_THIS(IDirect3D8Impl,iface);
267     FIXME("(%p)->(Adptr:%d, DevType: %x, AdptFmt: %d, Use: %ld, ResTyp: %x, CheckFmt: %d)\n", This, Adapter, DeviceType,
268           AdapterFormat, Usage, RType, CheckFormat);
269     return D3D_OK;
270 }
271
272 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType (LPDIRECT3D8 iface,
273                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
274                                                             BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
275     ICOM_THIS(IDirect3D8Impl,iface);
276     FIXME("(%p)->(Adptr:%d, DevType: %x, SurfFmt: %x, Win? %d, MultiSamp: %x)\n", This, Adapter, DeviceType,
277           SurfaceFormat, Windowed, MultiSampleType);
278     return D3D_OK;
279 }
280
281 HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch     (LPDIRECT3D8 iface,
282                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
283                                                             D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
284     ICOM_THIS(IDirect3D8Impl,iface);
285     FIXME("(%p)->(Adptr:%d, DevType: %x, AdptFmt: %x, RendrTgtFmt: %x, DepthStencilFmt: %x)\n", This, Adapter, DeviceType,
286           AdapterFormat, RenderTargetFormat, DepthStencilFormat);
287     return D3D_OK;
288 }
289
290 HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps              (LPDIRECT3D8 iface,
291                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
292     ICOM_THIS(IDirect3D8Impl,iface);
293     TRACE("(%p)->(Adptr:%d, DevType: %x, pCaps: %p)\n", This, Adapter, DeviceType, pCaps);
294
295
296     /* NOTE: Most of the values here are complete garbage for now */
297     pCaps->DeviceType = (DeviceType == D3DDEVTYPE_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;  /* Not quite true, but use h/w supported by opengl I suppose */
298     pCaps->AdapterOrdinal = Adapter;
299
300     pCaps->Caps = 0;
301     pCaps->Caps2 = D3DCAPS2_CANRENDERWINDOWED;
302     pCaps->Caps3 = D3DDEVCAPS_HWTRANSFORMANDLIGHT;
303     pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE;
304
305     pCaps->CursorCaps = 0;
306
307     pCaps->DevCaps = D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_PUREDEVICE;
308
309     pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_CULLCCW | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPTLVERTS  |
310                                D3DPMISCCAPS_CLIPPLANESCALEDPOINTS | D3DPMISCCAPS_MASKZ; /*NOT: D3DPMISCCAPS_TSSARGTEMP*/
311     pCaps->RasterCaps = D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT;
312     pCaps->ZCmpCaps = D3DPCMPCAPS_ALWAYS | D3DPCMPCAPS_EQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_GREATEREQUAL |
313                       D3DPCMPCAPS_LESS | D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_NEVER | D3DPCMPCAPS_NOTEQUAL;
314
315     pCaps->SrcBlendCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
316     pCaps->DestBlendCaps = 0xFFFFFFFF;   /*FIXME: Tidy up later */
317     pCaps->AlphaCmpCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
318     pCaps->ShadeCaps = D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_COLORGOURAUDRGB ;
319     pCaps->TextureCaps = D3DPTEXTURECAPS_ALPHA | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_VOLUMEMAP | D3DPTEXTURECAPS_MIPMAP;
320     pCaps->TextureFilterCaps = D3DPTFILTERCAPS_MAGFLINEAR  | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFPOINT |
321                                D3DPTFILTERCAPS_MIPFLINEAR  | D3DPTFILTERCAPS_MIPFPOINT ;
322     pCaps->CubeTextureFilterCaps = 0;
323     pCaps->VolumeTextureFilterCaps = 0;
324     pCaps->TextureAddressCaps = D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_CLAMP | D3DPTADDRESSCAPS_WRAP;
325     pCaps->VolumeTextureAddressCaps = 0;
326
327     pCaps->LineCaps = D3DLINECAPS_TEXTURE | D3DLINECAPS_ZTEST;
328
329     pCaps->MaxTextureWidth = 16384;
330     pCaps->MaxTextureHeight = 16384;
331     pCaps->MaxVolumeExtent = 0;
332
333     pCaps->MaxTextureRepeat = 32768;
334     pCaps->MaxTextureAspectRatio = 32768;
335     pCaps->MaxAnisotropy = 0;
336     pCaps->MaxVertexW = 1.0;
337
338     pCaps->GuardBandLeft = 0;
339     pCaps->GuardBandTop = 0;
340     pCaps->GuardBandRight = 0;
341     pCaps->GuardBandBottom = 0;
342
343     pCaps->ExtentsAdjust = 0;
344
345     pCaps->StencilCaps = D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INCRSAT | 
346                          D3DSTENCILCAPS_INVERT  | D3DSTENCILCAPS_KEEP    | 
347                          D3DSTENCILCAPS_REPLACE | D3DSTENCILCAPS_ZERO /* | D3DSTENCILCAPS_DECR | D3DSTENCILCAPS_INCR */;
348
349     pCaps->FVFCaps = D3DFVFCAPS_PSIZE | 0x80000;
350     pCaps->TextureOpCaps = 0xFFFFFFFF;
351
352     {
353         GLint gl_max;
354
355         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
356         TRACE("GLCaps: GL_MAX_TEXTURE_UNITS_ARB=%d\n", gl_max);
357         pCaps->MaxTextureBlendStages = min(8, gl_max);
358         pCaps->MaxSimultaneousTextures = min(8, gl_max);
359
360         glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
361         pCaps->MaxUserClipPlanes = min(MAX_CLIPPLANES, gl_max);
362         TRACE("GLCaps: GL_MAX_CLIP_PLANES=%d\n", gl_max);
363
364         glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
365         pCaps->MaxActiveLights = min(MAX_ACTIVE_LIGHTS, gl_max);
366         TRACE("GLCaps: GL_MAX_LIGHTS=%d\n", gl_max);
367     }
368
369     pCaps->VertexProcessingCaps = D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_TEXGEN;
370
371     pCaps->MaxVertexBlendMatrices = 1;
372     pCaps->MaxVertexBlendMatrixIndex = 1;
373
374     pCaps->MaxPointSize = 128.0;
375
376     pCaps->MaxPrimitiveCount = 0xFFFFFFFF;
377     pCaps->MaxVertexIndex = 0xFFFFFFFF;
378     pCaps->MaxStreams = 2; /* HACK: Some games want at least 2 */ 
379     pCaps->MaxStreamStride = 1024;
380
381     pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
382     pCaps->MaxVertexShaderConst = D3D8_VSHADER_MAX_CONSTANTS;
383
384     pCaps->PixelShaderVersion = D3DPS_VERSION(1,1);
385     pCaps->MaxPixelShaderValue = 1.0;
386
387     return D3D_OK;
388 }
389
390 HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor          (LPDIRECT3D8 iface,
391                                                             UINT Adapter) {
392     ICOM_THIS(IDirect3D8Impl,iface);
393     FIXME("(%p)->(Adptr:%d)\n", This, Adapter);
394     return D3D_OK;
395 }
396
397 HRESULT  WINAPI  IDirect3D8Impl_CreateDevice               (LPDIRECT3D8 iface,
398                                                             UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
399                                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
400                                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
401     IDirect3DDevice8Impl *object;
402     HWND whichHWND;
403     int num;
404     XVisualInfo template;
405     const char *GL_Extensions = NULL;
406     const char *GLX_Extensions = NULL;
407     GLint gl_max;
408
409     ICOM_THIS(IDirect3D8Impl,iface);
410     TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
411           hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);
412
413     /* Allocate the storage for the device */
414     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
415     if (NULL == object) {
416       return D3DERR_OUTOFVIDEOMEMORY;
417     }
418     object->lpVtbl = &Direct3DDevice8_Vtbl;
419     object->ref = 1;
420     object->direct3d8 = This;
421     /** The device AddRef the direct3d8 Interface else crash in propers clients codes */
422     IDirect3D8_AddRef((LPDIRECT3D8) object->direct3d8);
423
424     /** use StateBlock Factory here, for creating the startup stateBlock */
425     object->StateBlock = NULL;
426     IDirect3DDeviceImpl_CreateStateBlock(object, D3DSBT_ALL, NULL);
427     object->UpdateStateBlock = object->StateBlock;
428
429     /* Save the creation parameters */
430     object->CreateParms.AdapterOrdinal = Adapter;
431     object->CreateParms.DeviceType = DeviceType;
432     object->CreateParms.hFocusWindow = hFocusWindow;
433     object->CreateParms.BehaviorFlags = BehaviourFlags;
434
435     *ppReturnedDeviceInterface = (LPDIRECT3DDEVICE8)object;
436
437     /* Initialize settings */
438     memcpy(&object->PresentParms, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
439
440     object->PresentParms.BackBufferCount = 1; /* Opengl only supports one? */
441     pPresentationParameters->BackBufferCount = 1;
442
443     object->adapterNo = Adapter;
444     object->devType = DeviceType;
445
446     /* Initialize openGl */
447     {
448         HDC hDc;
449         int          dblBuf[]={GLX_STENCIL_SIZE,8,GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
450         /* FIXME: Handle stencil appropriately via EnableAutoDepthStencil / AutoDepthStencilFormat */
451         /*int          dblBuf[]={GLX_RGBA,GLX_RED_SIZE,4,GLX_GREEN_SIZE,4,GLX_BLUE_SIZE,4,GLX_DOUBLEBUFFER,None}; */
452
453         /* Which hwnd are we using? */
454 /*      if (pPresentationParameters->Windowed) { */
455            whichHWND = pPresentationParameters->hDeviceWindow;
456            if (!whichHWND) {
457                whichHWND = hFocusWindow;
458            }
459            object->win     = (Window)GetPropA( whichHWND, "__wine_x11_client_window" );
460 /*
461  *      } else {
462  *           whichHWND       = (HWND) GetDesktopWindow();
463  *           object->win     = (Window)GetPropA(whichHWND, "__wine_x11_whole_window" );
464  *         root_window
465  *        }
466  */
467
468         hDc = GetDC(whichHWND);
469         object->display = get_display(hDc);
470
471         ENTER_GL();
472         object->visInfo = glXChooseVisual(object->display, DefaultScreen(object->display), dblBuf);
473         if (NULL == object->visInfo) {
474           FIXME("cannot choose needed glxVisual with Stencil Buffer\n"); 
475
476           /**
477            * second try using wine initialized visual ...
478            * must be fixed reworking wine-glx init
479            */
480           template.visualid = (VisualID)GetPropA( GetDesktopWindow(), "__wine_x11_visual_id" );
481           object->visInfo = XGetVisualInfo(object->display, VisualIDMask, &template, &num);
482           if (NULL == object->visInfo) {
483             ERR("cannot really get XVisual\n"); 
484             LEAVE_GL();
485             return D3DERR_NOTAVAILABLE;
486           }
487         }
488         object->glCtx = glXCreateContext(object->display, object->visInfo, NULL, GL_TRUE);
489         if (NULL == object->glCtx) {
490           ERR("cannot create glxContext\n"); 
491           LEAVE_GL();
492           return D3DERR_NOTAVAILABLE;
493         }
494         LEAVE_GL();
495
496         ReleaseDC(whichHWND, hDc);
497     }
498
499     if (object->glCtx == NULL) {
500         ERR("Error in context creation !\n");
501         return D3DERR_INVALIDCALL;
502     } else {
503         TRACE("Context created (HWND=%p, glContext=%p, Window=%ld, VisInfo=%p)\n",
504                         whichHWND, object->glCtx, object->win, object->visInfo);
505     }
506
507     TRACE("Creating back buffer\n");
508     /* MSDN: If Windowed is TRUE and either of the BackBufferWidth/Height values is zero,
509        then the corresponding dimension of the client area of the hDeviceWindow
510        (or the focus window, if hDeviceWindow is NULL) is taken. */
511     if (pPresentationParameters->Windowed && ((pPresentationParameters->BackBufferWidth  == 0) ||
512                                               (pPresentationParameters->BackBufferHeight  == 0))) {
513         RECT Rect;
514
515         GetClientRect(whichHWND, &Rect);
516
517         if (pPresentationParameters->BackBufferWidth  == 0) {
518            pPresentationParameters->BackBufferWidth = Rect.right;
519            TRACE("Updating width to %d\n", pPresentationParameters->BackBufferWidth);
520         }
521         if (pPresentationParameters->BackBufferHeight  == 0) {
522            pPresentationParameters->BackBufferHeight = Rect.bottom;
523            TRACE("Updating height to %d\n", pPresentationParameters->BackBufferHeight);
524         }
525     }
526
527     IDirect3DDevice8Impl_CreateImageSurface((LPDIRECT3DDEVICE8) object,
528                                             pPresentationParameters->BackBufferWidth,
529                                             pPresentationParameters->BackBufferHeight,
530                                             pPresentationParameters->BackBufferFormat,
531                                             (LPDIRECT3DSURFACE8*) &object->backBuffer);
532
533     if (pPresentationParameters->EnableAutoDepthStencil)
534         IDirect3DDevice8Impl_CreateImageSurface((LPDIRECT3DDEVICE8) object,
535                                                 pPresentationParameters->BackBufferWidth,
536                                                 pPresentationParameters->BackBufferHeight,
537                                                 pPresentationParameters->AutoDepthStencilFormat,
538                                                 (LPDIRECT3DSURFACE8*) &object->depthStencilBuffer);
539     
540     /* Now override the surface's Flip method (if in double buffering) ?COPIED from DDRAW!?
541     ((x11_ds_private *) surface->private)->opengl_flip = TRUE;
542     {
543     int i;
544     struct _surface_chain *chain = surface->s.chain;
545     for (i=0;i<chain->nrofsurfaces;i++)
546       if (chain->surfaces[i]->s.surface_desc.ddsCaps.dwCaps & DDSCAPS_FLIP)
547           ((x11_ds_private *) chain->surfaces[i]->private)->opengl_flip = TRUE;
548     }
549     */
550
551     ENTER_GL();
552
553     /*TRACE("hereeee. %x %x %x\n", object->display, object->win, object->glCtx);*/
554     if (glXMakeCurrent(object->display, object->win, object->glCtx) == False) {
555       ERR("Error in setting current context (context %p drawable %ld)!\n", object->glCtx, object->win);
556     }
557     checkGLcall("glXMakeCurrent");
558
559     /* Clear the screen */
560     glClearColor(1.0, 0.0, 0.0, 0.0);
561     checkGLcall("glClearColor");
562     glColor3f(1.0, 1.0, 1.0);
563     checkGLcall("glColor3f");
564
565     glEnable(GL_LIGHTING);
566     checkGLcall("glEnable");
567
568     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
569     checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
570
571     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
572     checkGLcall("glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
573
574     /* Initialize openGL extension related variables */
575     object->isMultiTexture = FALSE;
576     object->TextureUnits   = 1;
577
578     /* Retrieve opengl defaults */
579     glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
580     object->clipPlanes   = min(MAX_CLIPPLANES, gl_max);
581     TRACE("ClipPlanes support - num Planes=%d\n", gl_max);
582
583     glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
584     object->maxLights = min(MAX_ACTIVE_LIGHTS, gl_max);
585     TRACE("Lights support - max lights=%d\n", gl_max);
586
587     /* Parse the gl supported features, in theory enabling parts of our code appropriately */
588     GL_Extensions = glGetString(GL_EXTENSIONS);
589     TRACE("GL_Extensions reported:\n");  
590     
591     if (NULL == GL_Extensions) {
592       ERR("   GL_Extensions returns NULL\n");      
593     } else {
594       while (*GL_Extensions != 0x00) {
595         const char *Start = GL_Extensions;
596         char ThisExtn[256];
597
598         memset(ThisExtn, 0x00, sizeof(ThisExtn));
599         while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
600           GL_Extensions++;
601         }
602         memcpy(ThisExtn, Start, (GL_Extensions - Start));
603         TRACE ("   %s\n", ThisExtn);
604
605         if (strcmp(ThisExtn, "GL_ARB_multitexture") == 0) {
606             glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
607             object->isMultiTexture = TRUE;
608             object->TextureUnits   = min(8, gl_max);
609             TRACE("FOUND: Multitexture support - GL_MAX_TEXTURE_UNITS_ARB=%d\n", gl_max);
610         }
611
612         if (*GL_Extensions == ' ') GL_Extensions++;
613       }
614     }
615
616     GLX_Extensions = glXQueryExtensionsString(object->display, DefaultScreen(object->display));
617     TRACE("GLX_Extensions reported:\n");  
618     
619     if (NULL == GLX_Extensions) {
620       ERR("   GLX_Extensions returns NULL\n");      
621     } else {
622       while (*GLX_Extensions != 0x00) {
623         const char *Start = GLX_Extensions;
624         char ThisExtn[256];
625         
626         memset(ThisExtn, 0x00, sizeof(ThisExtn));
627         while (*GLX_Extensions != ' ' && *GLX_Extensions != 0x00) {
628           GLX_Extensions++;
629         }
630         memcpy(ThisExtn, Start, (GLX_Extensions - Start));
631         TRACE ("   %s\n", ThisExtn);
632         if (*GLX_Extensions == ' ') GLX_Extensions++;
633       }
634     }
635
636     /* Setup all the devices defaults */
637     IDirect3DDeviceImpl_InitStartupStateBlock(object);
638
639     LEAVE_GL();
640
641     { /* Set a default viewport */
642        D3DVIEWPORT8 vp;
643        vp.X      = 0;
644        vp.Y      = 0;
645        vp.Width  = pPresentationParameters->BackBufferWidth;
646        vp.Height = pPresentationParameters->BackBufferHeight;
647        vp.MinZ   = 0.0f;
648        vp.MaxZ   = 1.0f;
649        IDirect3DDevice8Impl_SetViewport((LPDIRECT3DDEVICE8) object, &vp);
650     }
651
652     TRACE("(%p,%d)\n", This, Adapter);
653     return D3D_OK;
654 }
655
656 ICOM_VTABLE(IDirect3D8) Direct3D8_Vtbl =
657 {
658     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
659     IDirect3D8Impl_QueryInterface,
660     IDirect3D8Impl_AddRef,
661     IDirect3D8Impl_Release,
662     IDirect3D8Impl_RegisterSoftwareDevice,
663     IDirect3D8Impl_GetAdapterCount,
664     IDirect3D8Impl_GetAdapterIdentifier,
665     IDirect3D8Impl_GetAdapterModeCount,
666     IDirect3D8Impl_EnumAdapterModes,
667     IDirect3D8Impl_GetAdapterDisplayMode,
668     IDirect3D8Impl_CheckDeviceType,
669     IDirect3D8Impl_CheckDeviceFormat,
670     IDirect3D8Impl_CheckDeviceMultiSampleType,
671     IDirect3D8Impl_CheckDepthStencilMatch,
672     IDirect3D8Impl_GetDeviceCaps,
673     IDirect3D8Impl_GetAdapterMonitor,
674     IDirect3D8Impl_CreateDevice
675 };