directx.c 55.1 KB
Newer Older
1 2 3
/*
 * IDirect3D8 implementation
 *
4
 * Copyright 2002-2004 Jason Edmeades
5
 * Copyright 2003-2004 Raphael Junqueira
6
 * Copyright 2004 Christian Costa
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

23 24
#include "config.h"

25 26
#include <stdarg.h>

27 28
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
29 30 31
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
32
#include "winuser.h"
33
#include "wine/debug.h"
34
#include "wine/unicode.h"
35 36 37 38

#include "d3d8_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39
WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
40

41 42 43 44 45 46 47 48 49
/* x11drv GDI escapes */
#define X11DRV_ESCAPE 6789
enum x11drv_escape_codes
{
    X11DRV_GET_DISPLAY,   /* get X11 display for a DC */
    X11DRV_GET_DRAWABLE,  /* get current drawable for a DC */
    X11DRV_GET_FONT,      /* get current X font for a DC */
};

50 51 52 53 54 55 56 57 58
#define NUM_FORMATS 7
static const D3DFORMAT device_formats[NUM_FORMATS] = {
  D3DFMT_P8,
  D3DFMT_R3G3B2,
  D3DFMT_R5G6B5, 
  D3DFMT_X1R5G5B5,
  D3DFMT_X4R4G4B4,
  D3DFMT_R8G8B8,
  D3DFMT_X8R8G8B8
59 60
};

61
static void IDirect3D8Impl_FillGLCaps(LPDIRECT3D8 iface, Display* display);
62

63

64 65 66 67 68 69 70 71 72 73 74
/* retrieve the X display to use on a given DC */
inline static Display *get_display( HDC hdc )
{
    Display *display;
    enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;

    if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
                    sizeof(display), (LPSTR)&display )) display = NULL;
    return display;
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/** 
 * Note: GL seems to trap if GetDeviceCaps is called before any HWND's created
 * ie there is no GL Context - Get a default rendering context to enable the 
 * function query some info from GL                                     
 */    
static
WineD3D_Context* WineD3DCreateFakeGLContext(void) {
  static WineD3D_Context ctx = { NULL, NULL, NULL, 0, 0 };
  WineD3D_Context* ret = NULL;

   if (glXGetCurrentContext() == NULL) {
     BOOL         gotContext  = FALSE;
     BOOL         created     = FALSE;
     XVisualInfo  template;
     HDC          device_context;
     Visual*      visual;
     BOOL         failed = FALSE;
     int          num;
     XWindowAttributes win_attr;
     
     TRACE_(d3d_caps)("Creating Fake GL Context\n");

     ctx.drawable = (Drawable) GetPropA(GetDesktopWindow(), "__wine_x11_whole_window");

     /* Get the display */
     device_context = GetDC(0);
     ctx.display = get_display(device_context);
     ReleaseDC(0, device_context);
     
     /* Get the X visual */
     ENTER_GL();
     if (XGetWindowAttributes(ctx.display, ctx.drawable, &win_attr)) {
       visual = win_attr.visual;
     } else {
       visual = DefaultVisual(ctx.display, DefaultScreen(ctx.display));
     }
     template.visualid = XVisualIDFromVisual(visual);
     ctx.visInfo = XGetVisualInfo(ctx.display, VisualIDMask, &template, &num);
     if (ctx.visInfo == NULL) {
       LEAVE_GL();
       WARN_(d3d_caps)("Error creating visual info for capabilities initialization\n");
       failed = TRUE;
     }
     
     /* Create a GL context */
     if (!failed) {
       ctx.glCtx = glXCreateContext(ctx.display, ctx.visInfo, NULL, GL_TRUE);
       
       if (ctx.glCtx == NULL) {
	 LEAVE_GL();
	 WARN_(d3d_caps)("Error creating default context for capabilities initialization\n");
	 failed = TRUE;
       }
     }
     
     /* Make it the current GL context */
     if (!failed && glXMakeCurrent(ctx.display, ctx.drawable, ctx.glCtx) == False) {
       glXDestroyContext(ctx.display, ctx.glCtx);
       LEAVE_GL();
       WARN_(d3d_caps)("Error setting default context as current for capabilities initialization\n");
       failed = TRUE;	
     }
     
     /* It worked! Wow... */
     if (!failed) {
       gotContext = TRUE;
       created = TRUE;
       ret = &ctx;
     } else {
       ret = NULL;
     }
   } else {
     if (ctx.ref > 0) ret = &ctx;
   }

   if (NULL != ret) ++ret->ref;

   return ret;
}
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static
void WineD3DReleaseFakeGLContext(WineD3D_Context* ctx) {
  /* If we created a dummy context, throw it away */
  if (NULL != ctx) {
    --ctx->ref;
    if (0 == ctx->ref) {
      glXMakeCurrent(ctx->display, None, NULL);
      glXDestroyContext(ctx->display, ctx->glCtx);
      ctx->display = NULL;
      ctx->glCtx = NULL;
      LEAVE_GL();
    }
  }
}
 
   
171 172 173 174 175 176
/* IDirect3D IUnknown parts follow: */
HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface,REFIID riid,LPVOID *ppobj)
{
    ICOM_THIS(IDirect3D8Impl,iface);

    if (IsEqualGUID(riid, &IID_IUnknown)
Raphael Junqueira's avatar
Raphael Junqueira committed
177
        || IsEqualGUID(riid, &IID_IDirect3D8)) {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        IDirect3D8Impl_AddRef(iface);
        *ppobj = This;
        return D3D_OK;
    }

    WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
    return E_NOINTERFACE;
}

ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
    ICOM_THIS(IDirect3D8Impl,iface);
    TRACE("(%p) : AddRef from %ld\n", This, This->ref);
    return ++(This->ref);
}

ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
    ICOM_THIS(IDirect3D8Impl,iface);
    ULONG ref = --This->ref;
    TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
    if (ref == 0)
        HeapFree(GetProcessHeap(), 0, This);
    return ref;
}

/* IDirect3D Interface follow: */
HRESULT  WINAPI  IDirect3D8Impl_RegisterSoftwareDevice     (LPDIRECT3D8 iface, void* pInitializeFunction) {
    ICOM_THIS(IDirect3D8Impl,iface);
205
    FIXME_(d3d_caps)("(%p)->(%p): stub\n", This, pInitializeFunction);
206 207 208 209 210 211
    return D3D_OK;
}

UINT     WINAPI  IDirect3D8Impl_GetAdapterCount            (LPDIRECT3D8 iface) {
    ICOM_THIS(IDirect3D8Impl,iface);
    /* FIXME: Set to one for now to imply the display */
212
    TRACE_(d3d_caps)("(%p): Mostly stub, only returns primary display\n", This);
213 214 215 216 217 218 219
    return 1;
}

HRESULT  WINAPI  IDirect3D8Impl_GetAdapterIdentifier       (LPDIRECT3D8 iface,
                                                            UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
    ICOM_THIS(IDirect3D8Impl,iface);

220
    TRACE_(d3d_caps)("(%p}->(Adapter: %d, Flags: %lx, pId=%p)\n", This, Adapter, Flags, pIdentifier);
221 222 223 224 225

    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

226 227 228 229
    if (Adapter == 0) { /* Display */   
        /* If we don't know the device settings, go query them now */
        if (This->isGLInfoValid == FALSE) {
	  WineD3D_Context* ctx = WineD3DCreateFakeGLContext();
230
	  if (NULL != ctx) IDirect3D8Impl_FillGLCaps(iface, ctx->display);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	  WineD3DReleaseFakeGLContext(ctx);
	}
        if (This->isGLInfoValid == TRUE) {
	  TRACE_(d3d_caps)("device/Vendor Name and Version detection using FillGLCaps\n");
	  strcpy(pIdentifier->Driver, "Display");
	  strcpy(pIdentifier->Description, "Direct3D HAL");
	  pIdentifier->DriverVersion.u.HighPart = 0xa;
	  pIdentifier->DriverVersion.u.LowPart = This->gl_info.gl_driver_version;
	  pIdentifier->VendorId = This->gl_info.gl_vendor;
	  pIdentifier->DeviceId = This->gl_info.gl_card;
	  pIdentifier->SubSysId = 0;
	  pIdentifier->Revision = 0;
	} else {
	  WARN_(d3d_caps)("Cannot get GLCaps for device/Vendor Name and Version detection using FillGLCaps, currently using NVIDIA identifiers\n");
	  strcpy(pIdentifier->Driver, "Display");
	  strcpy(pIdentifier->Description, "Direct3D HAL");
	  pIdentifier->DriverVersion.u.HighPart = 0xa;
	  pIdentifier->DriverVersion.u.LowPart = MAKEDWORD_VERSION(53, 96); /* last Linux Nvidia drivers */
	  pIdentifier->VendorId = VENDOR_NVIDIA;
	  pIdentifier->DeviceId = CARD_NVIDIA_GEFORCE4_TI4600;
	  pIdentifier->SubSysId = 0;
	  pIdentifier->Revision = 0;
	}
254
        /*FIXME: memcpy(&pIdentifier->DeviceIdentifier, ??, sizeof(??GUID)); */
255
        if (Flags & D3DENUM_NO_WHQL_LEVEL) {
256 257 258 259 260
            pIdentifier->WHQLLevel = 0;
        } else {
            pIdentifier->WHQLLevel = 1;
        }
    } else {
261
        FIXME_(d3d_caps)("Adapter not primary display\n");
262 263 264 265 266
    }

    return D3D_OK;
}

267 268 269 270

/*#define DEBUG_SINGLE_MODE*/
#undef DEBUG_SINGLE_MODE

271 272 273 274
UINT     WINAPI  IDirect3D8Impl_GetAdapterModeCount        (LPDIRECT3D8 iface,
                                                            UINT Adapter) {
    ICOM_THIS(IDirect3D8Impl,iface);

275
    TRACE_(d3d_caps)("(%p}->(Adapter: %d)\n", This, Adapter);
276 277 278 279 280 281

    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

    if (Adapter == 0) { /* Display */
282
        int i = 0;
283 284
#if !defined( DEBUG_SINGLE_MODE )
        DEVMODEW DevModeW;
285 286
        while (EnumDisplaySettingsExW(NULL, i, &DevModeW, 0)) {
            i++;
287
        }
288 289 290 291
#else
        i = 1;
#endif

292
        TRACE_(d3d_caps)("(%p}->(Adapter: %d) => %d\n", This, Adapter, i);
293
        return i;
294
    } else {
295
        FIXME_(d3d_caps)("Adapter not primary display\n");
296 297
    }

298
    return 0;
299 300 301 302 303 304
}

HRESULT  WINAPI  IDirect3D8Impl_EnumAdapterModes           (LPDIRECT3D8 iface,
                                                            UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
    ICOM_THIS(IDirect3D8Impl,iface);

305
    TRACE_(d3d_caps)("(%p}->(Adapter:%d, mode:%d, pMode:%p)\n", This, Adapter, Mode, pMode);
306

307 308 309
    if (NULL == pMode || 
	Adapter >= IDirect3D8Impl_GetAdapterCount(iface) ||
	Mode >= IDirect3D8Impl_GetAdapterModeCount(iface, Adapter)) {
310 311 312 313 314
        return D3DERR_INVALIDCALL;
    }

    if (Adapter == 0) { /* Display */
        int bpp = 0;
315 316
#if !defined( DEBUG_SINGLE_MODE )
        HDC hdc;
317 318 319 320 321 322
        DEVMODEW DevModeW;

        if (EnumDisplaySettingsExW(NULL, Mode, &DevModeW, 0)) 
        {
            pMode->Width        = DevModeW.dmPelsWidth;
            pMode->Height       = DevModeW.dmPelsHeight;
323
            bpp                 = DevModeW.dmBitsPerPel;
324
            pMode->RefreshRate  = D3DADAPTER_DEFAULT;
325 326 327 328
            if (DevModeW.dmFields&DM_DISPLAYFREQUENCY)
            {
                pMode->RefreshRate = DevModeW.dmDisplayFrequency;
            }
329 330 331
        }
        else
        {
332
            TRACE_(d3d_caps)("Requested mode out of range %d\n", Mode);
333 334 335 336
            return D3DERR_INVALIDCALL;
        }

        hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
337
        bpp = min(GetDeviceCaps(hdc, BITSPIXEL), bpp);
338 339 340
        DeleteDC(hdc);

        switch (bpp) {
341 342
        case  8: pMode->Format = D3DFMT_R3G3B2;   break;
        case 16: pMode->Format = D3DFMT_R5G6B5;   break;
343
        case 24: /* pMode->Format = D3DFMT_R5G6B5;   break;*/ /* Make 24bit appear as 32 bit */
344 345
        case 32: pMode->Format = D3DFMT_A8R8G8B8; break;
        default: pMode->Format = D3DFMT_UNKNOWN;
346 347 348 349 350 351 352 353 354 355
	}
#else
       if (Mode > 0) return D3DERR_INVALIDCALL;
       pMode->Width        = 800;
       pMode->Height       = 600;
       pMode->RefreshRate  = D3DADAPTER_DEFAULT;
       pMode->Format       = D3DFMT_A8R8G8B8;
       bpp = 32;
#endif
       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);
356 357

    } else {
358
        FIXME_(d3d_caps)("Adapter not primary display\n");
359 360 361 362 363 364 365 366
    }

    return D3D_OK;
}

HRESULT  WINAPI  IDirect3D8Impl_GetAdapterDisplayMode      (LPDIRECT3D8 iface,
                                                            UINT Adapter, D3DDISPLAYMODE* pMode) {
    ICOM_THIS(IDirect3D8Impl,iface);
367
    TRACE_(d3d_caps)("(%p}->(Adapter: %d, pMode: %p)\n", This, Adapter, pMode);
368

369 370
    if (NULL == pMode || 
	Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
371 372 373 374 375
        return D3DERR_INVALIDCALL;
    }

    if (Adapter == 0) { /* Display */
        int bpp = 0;
376
        DEVMODEW DevModeW;
377

378 379 380 381 382 383 384 385 386
        EnumDisplaySettingsExW(NULL, (DWORD)-1, &DevModeW, 0);
        pMode->Width        = DevModeW.dmPelsWidth;
        pMode->Height       = DevModeW.dmPelsHeight;
        bpp                 = DevModeW.dmBitsPerPel;
        pMode->RefreshRate  = D3DADAPTER_DEFAULT;
        if (DevModeW.dmFields&DM_DISPLAYFREQUENCY)
        {
            pMode->RefreshRate = DevModeW.dmDisplayFrequency;
        }
387 388 389

        switch (bpp) {
        case  8: pMode->Format       = D3DFMT_R3G3B2;   break;
390
        case 16: pMode->Format       = D3DFMT_R5G6B5;   break;
391
        case 24: /*pMode->Format       = D3DFMT_R5G6B5;   break;*/ /* Make 24bit appear as 32 bit */
392 393 394 395 396
        case 32: pMode->Format       = D3DFMT_A8R8G8B8; break;
        default: pMode->Format       = D3DFMT_UNKNOWN;
        }

    } else {
397
        FIXME_(d3d_caps)("Adapter not primary display\n");
398 399
    }

400
    TRACE_(d3d_caps)("returning w:%d, h:%d, ref:%d, fmt:%x\n", pMode->Width,
401 402 403 404 405 406 407 408
          pMode->Height, pMode->RefreshRate, pMode->Format);
    return D3D_OK;
}

HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
                                                            UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
                                                            D3DFORMAT BackBufferFormat, BOOL Windowed) {
    ICOM_THIS(IDirect3D8Impl,iface);
409
    TRACE_(d3d_caps)("(%p)->(Adptr:%d, CheckType:(%x,%s), DispFmt:(%x,%s), BackBuf:(%x,%s), Win?%d): stub\n", 
410 411 412 413 414 415
	  This, 
	  Adapter, 
	  CheckType, debug_d3ddevicetype(CheckType),
          DisplayFormat, debug_d3dformat(DisplayFormat),
	  BackBufferFormat, debug_d3dformat(BackBufferFormat),
	  Windowed);
416 417 418 419 420

    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

421 422 423 424 425 426 427 428
    /*
    switch (DisplayFormat) {
    case D3DFMT_A8R8G8B8:
      return D3DERR_NOTAVAILABLE;
    default:
      break;
    }
    */
429 430 431 432 433 434 435
    switch (DisplayFormat) {
      /*case D3DFMT_R5G6B5:*/
    case D3DFMT_R3G3B2:
      return D3DERR_NOTAVAILABLE;
    default:
      break;
    }
436 437 438 439 440 441 442
    return D3D_OK;
}

HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
                                                            DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
    ICOM_THIS(IDirect3D8Impl,iface);
443
    TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%u,%s), AdptFmt:(%u,%s), Use:(%lu,%s), ResTyp:(%x,%s), CheckFmt:(%u,%s)) ", 
444 445 446 447 448 449 450 451
          This, 
	  Adapter, 
	  DeviceType, debug_d3ddevicetype(DeviceType), 
	  AdapterFormat, debug_d3dformat(AdapterFormat), 
	  Usage, debug_d3dusage(Usage),
	  RType, debug_d3dressourcetype(RType), 
	  CheckFormat, debug_d3dformat(CheckFormat));

452 453 454 455
    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

456 457 458 459 460
    if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
        switch (CheckFormat) {
        case D3DFMT_DXT1:
        case D3DFMT_DXT3:
        case D3DFMT_DXT5:
461 462
	  TRACE_(d3d_caps)("[OK]\n");
	  return D3D_OK;
463 464
        default:
            break; /* Avoid compiler warnings */
465 466 467
        }
    }

468
    switch (CheckFormat) {
469 470 471
    /*****
     * check supported using GL_SUPPORT 
     */
472 473 474 475
    case D3DFMT_DXT1:
    case D3DFMT_DXT2:
    case D3DFMT_DXT3:
    case D3DFMT_DXT4:
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    case D3DFMT_DXT5: 

    /*****
     *  supported 
     */
      /*case D3DFMT_R5G6B5: */
      /*case D3DFMT_X1R5G5B5:*/
      /*case D3DFMT_A1R5G5B5: */
      /*case D3DFMT_A4R4G4B4:*/

    /*****
     * unsupported 
     */

      /* color buffer */
      /*case D3DFMT_X8R8G8B8:*/
    case D3DFMT_A8R3G3B2:

      /* Paletted */
495 496
    case D3DFMT_P8:
    case D3DFMT_A8P8:
497 498 499 500 501 502 503

      /* Luminance */
    case D3DFMT_L8:
    case D3DFMT_A8L8:
    case D3DFMT_A4L4:

      /* Bump */
504 505 506 507
#if 0
    case D3DFMT_V8U8:
    case D3DFMT_V16U16:
#endif
508 509 510 511 512 513 514 515 516 517 518
    case D3DFMT_L6V5U5:
    case D3DFMT_X8L8V8U8:
    case D3DFMT_Q8W8V8U8:
    case D3DFMT_W11V11U10:

    /****
     * currently hard to support 
     */
    case D3DFMT_UYVY:
    case D3DFMT_YUY2:

519
      /* Since we do not support these formats right now, don't pretend to. */
520
      TRACE_(d3d_caps)("[FAILED]\n");
521 522 523
      return D3DERR_NOTAVAILABLE;
    default:
      break;
524
    }
525

526
    TRACE_(d3d_caps)("[OK]\n");
527 528 529
    return D3D_OK;
}

530 531 532
HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
							   UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
							   BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
533
    ICOM_THIS(IDirect3D8Impl,iface);
534
    TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%x,%s), SurfFmt:(%x,%s), Win?%d, MultiSamp:%x)\n", 
535 536 537 538 539 540 541
	  This, 
	  Adapter, 
	  DeviceType, debug_d3ddevicetype(DeviceType),
          SurfaceFormat, debug_d3dformat(SurfaceFormat),
	  Windowed, 
	  MultiSampleType);
  
542 543 544 545
    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

546 547 548
    if (D3DMULTISAMPLE_NONE == MultiSampleType)
      return D3D_OK;
    return D3DERR_NOTAVAILABLE;
549 550
}

551 552 553
HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface, 
						       UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
						       D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
554
    ICOM_THIS(IDirect3D8Impl,iface);
555
    TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType:(%x,%s), AdptFmt:(%x,%s), RendrTgtFmt:(%x,%s), DepthStencilFmt:(%x,%s))\n", 
556 557 558 559 560 561 562
	  This, 
	  Adapter, 
	  DeviceType, debug_d3ddevicetype(DeviceType),
          AdapterFormat, debug_d3dformat(AdapterFormat),
	  RenderTargetFormat, debug_d3dformat(RenderTargetFormat), 
	  DepthStencilFormat, debug_d3dformat(DepthStencilFormat));

563 564 565 566
    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
#if 0
    switch (DepthStencilFormat) {
    case D3DFMT_D24X4S4:
    case D3DFMT_D24X8: 
    case D3DFMT_D24S8: 
    case D3DFMT_D32:
      /**
       * as i don't know how to really check hard caps of graphics cards
       * i prefer to not permit 32bit zbuffers enumeration (as few cards can do it)
       */
      return D3DERR_NOTAVAILABLE;
    default:
      break;
    }
#endif
582 583 584
    return D3D_OK;
}

585
HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
586 587 588

    BOOL        gotContext  = FALSE;
    GLint       gl_tex_size = 0;    
589
    WineD3D_Context* fake_ctx = NULL;
590
    ICOM_THIS(IDirect3D8Impl,iface);
591

592
    TRACE_(d3d_caps)("(%p)->(Adptr:%d, DevType: %x, pCaps: %p)\n", This, Adapter, DeviceType, pCaps);
593

594 595 596 597
    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

598
    /* Note: GL seems to trap if GetDeviceCaps is called before any HWND's created
599 600
       ie there is no GL Context - Get a default rendering context to enable the 
       function query some info from GL                                           */    
601
    if (glXGetCurrentContext() == NULL) {
602 603
      fake_ctx = WineD3DCreateFakeGLContext();
      if (NULL != fake_ctx) gotContext = TRUE;
604
    } else {
605
      gotContext = TRUE;
606 607 608 609
    }

    if (gotContext == FALSE) {

610
        FIXME_(d3d_caps)("GetDeviceCaps called but no GL Context - Returning dummy values\n");
611 612 613 614 615 616 617 618 619 620 621 622
        gl_tex_size=65535;
        pCaps->MaxTextureBlendStages = 2;
        pCaps->MaxSimultaneousTextures = 2;
        pCaps->MaxUserClipPlanes = 8;
        pCaps->MaxActiveLights = 8;
        pCaps->MaxVertexBlendMatrices = 0;
        pCaps->MaxVertexBlendMatrixIndex = 1;
        pCaps->MaxAnisotropy = 0;
        pCaps->MaxPointSize = 255.0;
    } else {
        glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_tex_size);
    }
623

624
    /* If we don't know the device settings, go query them now */
625 626
    if (This->isGLInfoValid == FALSE) IDirect3D8Impl_FillGLCaps(iface, NULL);

627
    pCaps->DeviceType = (DeviceType == D3DDEVTYPE_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;  /* Not quite true, but use h/w supported by opengl I suppose */
628 629 630 631 632 633 634 635 636
    pCaps->AdapterOrdinal = Adapter;

    pCaps->Caps = 0;
    pCaps->Caps2 = D3DCAPS2_CANRENDERWINDOWED;
    pCaps->Caps3 = D3DDEVCAPS_HWTRANSFORMANDLIGHT;
    pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE;

    pCaps->CursorCaps = 0;

637 638 639 640 641 642 643 644 645 646 647 648 649 650
    pCaps->DevCaps = D3DDEVCAPS_DRAWPRIMTLVERTEX    | 
                     D3DDEVCAPS_HWTRANSFORMANDLIGHT |
                     D3DDEVCAPS_PUREDEVICE;

    pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_CULLCCW               | 
                               D3DPMISCCAPS_CULLCW                | 
                               D3DPMISCCAPS_COLORWRITEENABLE      |
                               D3DPMISCCAPS_CLIPTLVERTS           |
                               D3DPMISCCAPS_CLIPPLANESCALEDPOINTS | 
                               D3DPMISCCAPS_MASKZ; 
                               /*NOT: D3DPMISCCAPS_TSSARGTEMP*/

    pCaps->RasterCaps = D3DPRASTERCAPS_DITHER   | 
                        D3DPRASTERCAPS_PAT      | 
651 652 653 654
			D3DPRASTERCAPS_WFOG |
			D3DPRASTERCAPS_ZFOG |
                        D3DPRASTERCAPS_FOGVERTEX |
			D3DPRASTERCAPS_FOGTABLE  |
655
                        D3DPRASTERCAPS_FOGRANGE;
656 657 658 659

    if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC)) {
      pCaps->RasterCaps |= D3DPRASTERCAPS_ANISOTROPY;
    }
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
                        /* FIXME Add:
			   D3DPRASTERCAPS_MIPMAPLODBIAS
			   D3DPRASTERCAPS_ZBIAS
			   D3DPRASTERCAPS_COLORPERSPECTIVE
			   D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE
			   D3DPRASTERCAPS_ANTIALIASEDGES
			   D3DPRASTERCAPS_ZBUFFERLESSHSR
			   D3DPRASTERCAPS_WBUFFER */

    pCaps->ZCmpCaps = D3DPCMPCAPS_ALWAYS       | 
                      D3DPCMPCAPS_EQUAL        | 
                      D3DPCMPCAPS_GREATER      | 
                      D3DPCMPCAPS_GREATEREQUAL |
                      D3DPCMPCAPS_LESS         | 
                      D3DPCMPCAPS_LESSEQUAL    | 
                      D3DPCMPCAPS_NEVER        |
                      D3DPCMPCAPS_NOTEQUAL;
677

678 679 680
    pCaps->SrcBlendCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
    pCaps->DestBlendCaps = 0xFFFFFFFF;   /*FIXME: Tidy up later */
    pCaps->AlphaCmpCaps  = 0xFFFFFFFF;   /*FIXME: Tidy up later */
681 682 683 684 685 686 687 688

    pCaps->ShadeCaps = D3DPSHADECAPS_SPECULARGOURAUDRGB | 
                       D3DPSHADECAPS_COLORGOURAUDRGB;

    pCaps->TextureCaps =  D3DPTEXTURECAPS_ALPHA        | 
                          D3DPTEXTURECAPS_ALPHAPALETTE | 
                          D3DPTEXTURECAPS_POW2         | 
                          D3DPTEXTURECAPS_VOLUMEMAP    | 
689 690
                          D3DPTEXTURECAPS_MIPMAP       |
                          D3DPTEXTURECAPS_PROJECTED;
691 692 693 694 695 696

    if (GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
      pCaps->TextureCaps |= D3DPTEXTURECAPS_CUBEMAP      | 
	                    D3DPTEXTURECAPS_MIPCUBEMAP   | 
	                    D3DPTEXTURECAPS_CUBEMAP_POW2;
    }
697 698 699 700 701 702 703 704

    pCaps->TextureFilterCaps = D3DPTFILTERCAPS_MAGFLINEAR | 
                               D3DPTFILTERCAPS_MAGFPOINT  | 
                               D3DPTFILTERCAPS_MINFLINEAR | 
                               D3DPTFILTERCAPS_MINFPOINT  |
                               D3DPTFILTERCAPS_MIPFLINEAR | 
                               D3DPTFILTERCAPS_MIPFPOINT;

705 706
    pCaps->CubeTextureFilterCaps = 0;
    pCaps->VolumeTextureFilterCaps = 0;
707 708 709 710

    pCaps->TextureAddressCaps =  D3DPTADDRESSCAPS_BORDER | 
                                 D3DPTADDRESSCAPS_CLAMP  | 
                                 D3DPTADDRESSCAPS_WRAP;
711 712 713 714 715 716 717 718 719 720

    if (GL_SUPPORT(ARB_TEXTURE_BORDER_CLAMP)) {
      pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_BORDER;
    }
    if (GL_SUPPORT(ARB_TEXTURE_MIRRORED_REPEAT)) {
      pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRROR;
    }
    if (GL_SUPPORT(ATI_TEXTURE_MIRROR_ONCE)) {
      pCaps->TextureAddressCaps |= D3DPTADDRESSCAPS_MIRRORONCE;
    }
721

722 723
    pCaps->VolumeTextureAddressCaps = 0;

724 725 726 727 728 729
    pCaps->LineCaps = D3DLINECAPS_TEXTURE | 
                      D3DLINECAPS_ZTEST;
                      /* FIXME: Add 
			 D3DLINECAPS_BLEND
			 D3DLINECAPS_ALPHACMP
			 D3DLINECAPS_FOG */
730

731 732
    pCaps->MaxTextureWidth = gl_tex_size;
    pCaps->MaxTextureHeight = gl_tex_size;
733

734 735 736 737 738 739 740 741 742 743 744 745 746
    pCaps->MaxVolumeExtent = 0;

    pCaps->MaxTextureRepeat = 32768;
    pCaps->MaxTextureAspectRatio = 32768;
    pCaps->MaxVertexW = 1.0;

    pCaps->GuardBandLeft = 0;
    pCaps->GuardBandTop = 0;
    pCaps->GuardBandRight = 0;
    pCaps->GuardBandBottom = 0;

    pCaps->ExtentsAdjust = 0;

747 748 749 750 751 752
    pCaps->StencilCaps =  D3DSTENCILCAPS_DECRSAT | 
                          D3DSTENCILCAPS_INCRSAT | 
                          D3DSTENCILCAPS_INVERT  | 
                          D3DSTENCILCAPS_KEEP    | 
                          D3DSTENCILCAPS_REPLACE | 
                          D3DSTENCILCAPS_ZERO;
753 754 755 756
    if (GL_SUPPORT(EXT_STENCIL_WRAP)) {
      pCaps->StencilCaps |= D3DSTENCILCAPS_DECR    | 
	                    D3DSTENCILCAPS_INCR;
    }
757

758
    pCaps->FVFCaps = D3DFVFCAPS_PSIZE | 0x0008; /* 8 texture coords */
759

760 761 762 763 764 765 766 767 768
    pCaps->TextureOpCaps =  D3DTEXOPCAPS_ADD         | 
                            D3DTEXOPCAPS_ADDSIGNED   | 
                            D3DTEXOPCAPS_ADDSIGNED2X |
                            D3DTEXOPCAPS_MODULATE    | 
                            D3DTEXOPCAPS_MODULATE2X  | 
                            D3DTEXOPCAPS_MODULATE4X  |
                            D3DTEXOPCAPS_SELECTARG1  | 
                            D3DTEXOPCAPS_SELECTARG2  | 
                            D3DTEXOPCAPS_DISABLE;
769
#if defined(GL_VERSION_1_3)
770 771
    pCaps->TextureOpCaps |= D3DTEXOPCAPS_DOTPRODUCT3 | 
                            D3DTEXOPCAPS_SUBTRACT;
772
#endif
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
    if (GL_SUPPORT(ARB_TEXTURE_ENV_COMBINE) || 
	GL_SUPPORT(EXT_TEXTURE_ENV_COMBINE) || 
	GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
      pCaps->TextureOpCaps |= D3DTEXOPCAPS_BLENDDIFFUSEALPHA |
	                      D3DTEXOPCAPS_BLENDTEXTUREALPHA | 
			      D3DTEXOPCAPS_BLENDFACTORALPHA  |
			      D3DTEXOPCAPS_BLENDCURRENTALPHA |
			      D3DTEXOPCAPS_LERP;
    }
    if (GL_SUPPORT(NV_TEXTURE_ENV_COMBINE4)) {
      pCaps->TextureOpCaps |= D3DTEXOPCAPS_ADDSMOOTH | 
			      D3DTEXOPCAPS_MULTIPLYADD |
			      D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
			      D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
                              D3DTEXOPCAPS_BLENDTEXTUREALPHAPM;
    }
789 790
    
#if 0
791
    pCaps->TextureOpCaps |= D3DTEXOPCAPS_BUMPENVMAP;
792 793 794
                            /* FIXME: Add 
			      D3DTEXOPCAPS_BUMPENVMAPLUMINANCE 
			      D3DTEXOPCAPS_PREMODULATE */
795
#endif
796

797
    if (gotContext) {
798
        GLint gl_max;
799
	GLfloat gl_float;
800 801 802
#if defined(GL_VERSION_1_3)
        glGetIntegerv(GL_MAX_TEXTURE_UNITS, &gl_max);
#else
803
        glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
804
#endif
805
        TRACE_(d3d_caps)("GLCaps: GL_MAX_TEXTURE_UNITS_ARB=%d\n", gl_max);
806 807 808 809 810
        pCaps->MaxTextureBlendStages = min(8, gl_max);
        pCaps->MaxSimultaneousTextures = min(8, gl_max);

        glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
        pCaps->MaxUserClipPlanes = min(MAX_CLIPPLANES, gl_max);
811
        TRACE_(d3d_caps)("GLCaps: GL_MAX_CLIP_PLANES=%ld\n", pCaps->MaxUserClipPlanes);
812 813

        glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
814
        pCaps->MaxActiveLights = gl_max;
815
        TRACE_(d3d_caps)("GLCaps: GL_MAX_LIGHTS=%ld\n", pCaps->MaxActiveLights);
816

817 818 819 820 821 822 823 824
        if (GL_SUPPORT(ARB_VERTEX_BLEND)) {
	   glGetIntegerv(GL_MAX_VERTEX_UNITS_ARB, &gl_max);
	   pCaps->MaxVertexBlendMatrices = gl_max;
	   pCaps->MaxVertexBlendMatrixIndex = 1;
        } else {
           pCaps->MaxVertexBlendMatrices = 0;
           pCaps->MaxVertexBlendMatrixIndex = 1;
        }
825

826 827 828 829 830 831 832
	if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC)) {
	  glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_max);
	  checkGLcall("glGetInterv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)");
	  pCaps->MaxAnisotropy = gl_max;
	} else {
	  pCaps->MaxAnisotropy = 0;
	}
833 834 835 836

	glGetFloatv(GL_POINT_SIZE_RANGE, &gl_float);
	pCaps->MaxPointSize = gl_float;
    }
837

838 839 840
    pCaps->VertexProcessingCaps = D3DVTXPCAPS_DIRECTIONALLIGHTS | 
                                  D3DVTXPCAPS_MATERIALSOURCE7   | 
                                  D3DVTXPCAPS_POSITIONALLIGHTS  | 
841
                                  D3DVTXPCAPS_LOCALVIEWER |
842 843 844
                                  D3DVTXPCAPS_TEXGEN;
                                  /* FIXME: Add 
				     D3DVTXPCAPS_TWEENING */
845 846 847

    pCaps->MaxPrimitiveCount = 0xFFFFFFFF;
    pCaps->MaxVertexIndex = 0xFFFFFFFF;
848
    pCaps->MaxStreams = MAX_STREAMS;
849 850
    pCaps->MaxStreamStride = 1024;

851 852 853 854 855 856 857 858 859 860 861 862 863
    if (((vs_mode == VS_HW) && GL_SUPPORT(ARB_VERTEX_PROGRAM)) || (vs_mode == VS_SW) || (DeviceType == D3DDEVTYPE_REF)) {
      pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
      
      if (This->gl_info.gl_vendor == VENDOR_MESA || 
	  This->gl_info.gl_vendor == VENDOR_WINE) {
	pCaps->MaxVertexShaderConst = 95;
      } else {
	pCaps->MaxVertexShaderConst = D3D8_VSHADER_MAX_CONSTANTS;
      }
    } else {
      pCaps->VertexShaderVersion = 0;
      pCaps->MaxVertexShaderConst = 0;
    }
864

865
    if ((ps_mode == PS_HW) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && (DeviceType != D3DDEVTYPE_REF)) {
866
        pCaps->PixelShaderVersion = D3DPS_VERSION(1,4);
867 868 869 870 871
        pCaps->MaxPixelShaderValue = 1.0;
    } else {
        pCaps->PixelShaderVersion = 0;
        pCaps->MaxPixelShaderValue = 0.0;
    }
872

873
    /* If we created a dummy context, throw it away */
874
    WineD3DReleaseFakeGLContext(fake_ctx);
875 876 877
    return D3D_OK;
}

878
HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
879
    ICOM_THIS(IDirect3D8Impl,iface);
880
    FIXME_(d3d_caps)("(%p)->(Adptr:%d)\n", This, Adapter);
881 882 883 884 885

    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return NULL;
    }

886 887 888
    return D3D_OK;
}

889 890 891 892 893

static void IDirect3D8Impl_FillGLCaps(LPDIRECT3D8 iface, Display* display) {
    const char *GL_Extensions = NULL;
    const char *GLX_Extensions = NULL;
    GLint gl_max;
894 895 896 897
    const char* gl_string = NULL;
    const char* gl_string_cursor = NULL;
    Bool test = 0;
    int major, minor;
898 899
    ICOM_THIS(IDirect3D8Impl,iface);

900
    if (This->gl_info.bIsFilled) return;
901
    This->gl_info.bIsFilled = 1;
902

903 904
    TRACE_(d3d_caps)("(%p, %p)\n", This, display);

905
    if (NULL != display) {
906
      test = glXQueryVersion(display, &major, &minor);
907
      This->gl_info.glx_version = ((major & 0x0000FFFF) << 16) | (minor & 0x0000FFFF);
908
      gl_string = glXGetClientString(display, GLX_VENDOR);
909 910 911
    } else {
      gl_string = glGetString(GL_VENDOR);
    }
912
    
913 914 915 916 917 918 919
    if (strstr(gl_string, "NVIDIA")) {
      This->gl_info.gl_vendor = VENDOR_NVIDIA;
    } else if (strstr(gl_string, "ATI")) {
      This->gl_info.gl_vendor = VENDOR_ATI;
    } else {
      This->gl_info.gl_vendor = VENDOR_WINE;
    }
920 921 922
   
    TRACE_(d3d_caps)("found GL_VENDOR (%s)->(0x%04x)\n", debugstr_a(gl_string), This->gl_info.gl_vendor);
    
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    gl_string = glGetString(GL_VERSION);
    switch (This->gl_info.gl_vendor) {
    case VENDOR_NVIDIA:
      gl_string_cursor = strstr(gl_string, "NVIDIA");
      gl_string_cursor = strstr(gl_string_cursor, " ");
      while (*gl_string_cursor && ' ' == *gl_string_cursor) ++gl_string_cursor;
      if (*gl_string_cursor) {
	char tmp[16];
	int cursor = 0;

	while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
	  tmp[cursor++] = *gl_string_cursor;
	  ++gl_string_cursor;
	}
	tmp[cursor] = 0;
	major = atoi(tmp);
	
940
	if (*gl_string_cursor != '.') WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
941 942 943 944 945 946 947 948 949 950 951
	++gl_string_cursor;

	while (*gl_string_cursor <= '9' && *gl_string_cursor >= '0') {
	  tmp[cursor++] = *gl_string_cursor;
	  ++gl_string_cursor;
	}
	tmp[cursor] = 0;
	minor = atoi(tmp);
	break;
      }
    case VENDOR_ATI:
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
      major = minor = 0;
      gl_string_cursor = strchr(gl_string, '-');
      if (gl_string_cursor++) {
        int error = 0;
        /* Check if version number is of the form x.y.z */
        if (*gl_string_cursor > '9' && *gl_string_cursor < '0')
          error = 1;
        if (!error && *(gl_string_cursor+2) > '9' && *(gl_string_cursor+2) < '0')
          error = 1;
        if (!error && *(gl_string_cursor+4) > '9' && *(gl_string_cursor+4) < '0')
          error = 1;
	if (!error && *(gl_string_cursor+1) != '.' && *(gl_string_cursor+3) != '.')
          error = 1;
        /* Mark version number as malformed */
        if (error)
          gl_string_cursor = 0;
      }
      if (!gl_string_cursor)
        WARN_(d3d_caps)("malformed GL_VERSION (%s)\n", debugstr_a(gl_string));
      else {
        major = *gl_string_cursor - '0';
        minor = (*(gl_string_cursor+2) - '0') * 256 + (*(gl_string_cursor+4) - '0');
      }      
      break;
976 977 978 979
    default:
      major = 0;
      minor = 9;
    }
980
    This->gl_info.gl_driver_version = MAKEDWORD_VERSION(major, minor);
981

982 983
    FIXME_(d3d_caps)("found GL_VERSION  (%s)->(0x%08lx)\n", debugstr_a(gl_string), This->gl_info.gl_driver_version);

984 985
    gl_string = glGetString(GL_RENDERER);
    strcpy(This->gl_info.gl_renderer, gl_string);
986

987 988 989 990 991 992 993 994 995 996 997
    switch (This->gl_info.gl_vendor) {
    case VENDOR_NVIDIA:
      if (strstr(This->gl_info.gl_renderer, "GeForce4 Ti")) {
	This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
      } else if (strstr(This->gl_info.gl_renderer, "GeForceFX")) {
	This->gl_info.gl_card = CARD_NVIDIA_GEFORCEFX_5900ULTRA;
      } else {
	This->gl_info.gl_card = CARD_NVIDIA_GEFORCE4_TI4600;
      }
      break;
    case VENDOR_ATI:
998 999 1000 1001 1002 1003 1004
      if (strstr(This->gl_info.gl_renderer, "RADEON 9800 PRO")) {
        This->gl_info.gl_card = CARD_ATI_RADEON_9800PRO;
      } else if (strstr(This->gl_info.gl_renderer, "RADEON 9700 PRO")) {
        This->gl_info.gl_card = CARD_ATI_RADEON_9700PRO;
      } else {
        This->gl_info.gl_card = CARD_ATI_RADEON_8500;
      }
1005 1006 1007 1008 1009 1010 1011
      break;
    default:
      This->gl_info.gl_card = CARD_WINE;
      break;
    }

    FIXME_(d3d_caps)("found GL_RENDERER (%s)->(0x%04x)\n", debugstr_a(This->gl_info.gl_renderer), This->gl_info.gl_card);
1012 1013

    /*
1014
     * Initialize openGL extension related variables
1015
     *  with Default values
1016 1017 1018 1019 1020 1021 1022 1023
     */
    memset(&This->gl_info.supported, 0, sizeof(This->gl_info.supported));
    This->gl_info.max_textures   = 1;
    This->gl_info.ps_arb_version = PS_VERSION_NOT_SUPPORTED;
    This->gl_info.vs_arb_version = VS_VERSION_NOT_SUPPORTED;
    This->gl_info.vs_nv_version  = VS_VERSION_NOT_SUPPORTED;
    This->gl_info.vs_ati_version = VS_VERSION_NOT_SUPPORTED;

1024 1025 1026 1027
#define USE_GL_FUNC(type, pfn) This->gl_info.pfn = NULL;
    GL_EXT_FUNCS_GEN;
#undef USE_GL_FUNC

1028 1029 1030
    /* Retrieve opengl defaults */
    glGetIntegerv(GL_MAX_CLIP_PLANES, &gl_max);
    This->gl_info.max_clipplanes = min(MAX_CLIPPLANES, gl_max);
1031
    TRACE_(d3d_caps)("ClipPlanes support - num Planes=%d\n", gl_max);
1032 1033

    glGetIntegerv(GL_MAX_LIGHTS, &gl_max);
1034
    This->gl_info.max_lights = gl_max;
1035
    TRACE_(d3d_caps)("Lights support - max lights=%d\n", gl_max);
1036 1037 1038

    /* Parse the gl supported features, in theory enabling parts of our code appropriately */
    GL_Extensions = glGetString(GL_EXTENSIONS);
1039
    TRACE_(d3d_caps)("GL_Extensions reported:\n");  
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    
    if (NULL == GL_Extensions) {
      ERR("   GL_Extensions returns NULL\n");      
    } else {
      while (*GL_Extensions != 0x00) {
        const char *Start = GL_Extensions;
        char ThisExtn[256];

        memset(ThisExtn, 0x00, sizeof(ThisExtn));
        while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
	  GL_Extensions++;
        }
        memcpy(ThisExtn, Start, (GL_Extensions - Start));
1053
        TRACE_(d3d_caps)("- %s\n", ThisExtn);
1054 1055 1056 1057 1058 1059

	/**
	 * ARB 
	 */
	if (strcmp(ThisExtn, "GL_ARB_fragment_program") == 0) {
	  This->gl_info.ps_arb_version = PS_VERSION_11;
1060
	  TRACE_(d3d_caps)(" FOUND: ARB Pixel Shader support - version=%02x\n", This->gl_info.ps_arb_version);
1061 1062
	  This->gl_info.supported[ARB_FRAGMENT_PROGRAM] = TRUE;
        } else if (strcmp(ThisExtn, "GL_ARB_multisample") == 0) {
1063
	  TRACE_(d3d_caps)(" FOUND: ARB Multisample support\n");
1064 1065 1066
	  This->gl_info.supported[ARB_MULTISAMPLE] = TRUE;
	} else if (strcmp(ThisExtn, "GL_ARB_multitexture") == 0) {
	  glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_max);
1067
	  TRACE_(d3d_caps)(" FOUND: ARB Multitexture support - GL_MAX_TEXTURE_UNITS_ARB=%u\n", gl_max);
1068 1069 1070
	  This->gl_info.supported[ARB_MULTITEXTURE] = TRUE;
	  This->gl_info.max_textures = min(8, gl_max);
        } else if (strcmp(ThisExtn, "GL_ARB_texture_cube_map") == 0) {
1071
	  TRACE_(d3d_caps)(" FOUND: ARB Texture Cube Map support\n");
1072
	  This->gl_info.supported[ARB_TEXTURE_CUBE_MAP] = TRUE;
1073 1074
	  TRACE_(d3d_caps)(" IMPLIED: NVIDIA (NV) Texture Gen Reflection support\n");
	  This->gl_info.supported[NV_TEXGEN_REFLECTION] = TRUE;
1075
        } else if (strcmp(ThisExtn, "GL_ARB_texture_compression") == 0) {
1076
	  TRACE_(d3d_caps)(" FOUND: ARB Texture Compression support\n");
1077
	  This->gl_info.supported[ARB_TEXTURE_COMPRESSION] = TRUE;
1078
        } else if (strcmp(ThisExtn, "GL_ARB_texture_env_add") == 0) {
1079
	  TRACE_(d3d_caps)(" FOUND: ARB Texture Env Add support\n");
1080
	  This->gl_info.supported[ARB_TEXTURE_ENV_ADD] = TRUE;
1081
        } else if (strcmp(ThisExtn, "GL_ARB_texture_env_combine") == 0) {
1082
	  TRACE_(d3d_caps)(" FOUND: ARB Texture Env combine support\n");
1083
	  This->gl_info.supported[ARB_TEXTURE_ENV_COMBINE] = TRUE;
1084
        } else if (strcmp(ThisExtn, "GL_ARB_texture_env_dot3") == 0) {
1085
	  TRACE_(d3d_caps)(" FOUND: ARB Dot3 support\n");
1086 1087
	  This->gl_info.supported[ARB_TEXTURE_ENV_DOT3] = TRUE;
	} else if (strcmp(ThisExtn, "GL_ARB_texture_border_clamp") == 0) {
1088
	  TRACE_(d3d_caps)(" FOUND: ARB Texture border clamp support\n");
1089
	  This->gl_info.supported[ARB_TEXTURE_BORDER_CLAMP] = TRUE;
1090
	} else if (strcmp(ThisExtn, "GL_ARB_texture_mirrored_repeat") == 0) {
1091
	  TRACE_(d3d_caps)(" FOUND: ARB Texture mirrored repeat support\n");
1092
	  This->gl_info.supported[ARB_TEXTURE_MIRRORED_REPEAT] = TRUE;
1093 1094
	} else if (strstr(ThisExtn, "GL_ARB_vertex_program")) {
	  This->gl_info.vs_arb_version = VS_VERSION_11;
1095
	  TRACE_(d3d_caps)(" FOUND: ARB Vertex Shader support - version=%02x\n", This->gl_info.vs_arb_version);
1096 1097 1098 1099 1100 1101
	  This->gl_info.supported[ARB_VERTEX_PROGRAM] = TRUE;

	/**
	 * EXT
	 */
        } else if (strcmp(ThisExtn, "GL_EXT_fog_coord") == 0) {
1102
	  TRACE_(d3d_caps)(" FOUND: EXT Fog coord support\n");
1103
	  This->gl_info.supported[EXT_FOG_COORD] = TRUE;
1104
        } else if (strcmp(ThisExtn, "GL_EXT_paletted_texture") == 0) { /* handle paletted texture extensions */
1105
	  TRACE_(d3d_caps)(" FOUND: EXT Paletted texture support\n");
1106
	  This->gl_info.supported[EXT_PALETTED_TEXTURE] = TRUE;
1107
        } else if (strcmp(ThisExtn, "GL_EXT_point_parameters") == 0) {
1108
	  TRACE_(d3d_caps)(" FOUND: EXT Point parameters support\n");
1109
	  This->gl_info.supported[EXT_POINT_PARAMETERS] = TRUE;
1110
	} else if (strcmp(ThisExtn, "GL_EXT_secondary_color") == 0) {
1111
	  TRACE_(d3d_caps)(" FOUND: EXT Secondary coord support\n");
1112
	  This->gl_info.supported[EXT_SECONDARY_COLOR] = TRUE;
1113 1114 1115
	} else if (strcmp(ThisExtn, "GL_EXT_stencil_wrap") == 0) {
	  TRACE_(d3d_caps)(" FOUND: EXT Stencil wrap support\n");
	  This->gl_info.supported[EXT_STENCIL_WRAP] = TRUE;
1116
	} else if (strcmp(ThisExtn, "GL_EXT_texture_compression_s3tc") == 0) {
1117
	  TRACE_(d3d_caps)(" FOUND: EXT Texture S3TC compression support\n");
1118
	  This->gl_info.supported[EXT_TEXTURE_COMPRESSION_S3TC] = TRUE;
1119
        } else if (strcmp(ThisExtn, "GL_EXT_texture_env_add") == 0) {
1120
	  TRACE_(d3d_caps)(" FOUND: EXT Texture Env Add support\n");
1121 1122
	  This->gl_info.supported[EXT_TEXTURE_ENV_ADD] = TRUE;
        } else if (strcmp(ThisExtn, "GL_EXT_texture_env_combine") == 0) {
1123
	  TRACE_(d3d_caps)(" FOUND: EXT Texture Env combine support\n");
1124
	  This->gl_info.supported[EXT_TEXTURE_ENV_COMBINE] = TRUE;
1125
        } else if (strcmp(ThisExtn, "GL_EXT_texture_env_dot3") == 0) {
1126
	  TRACE_(d3d_caps)(" FOUND: EXT Dot3 support\n");
1127
	  This->gl_info.supported[EXT_TEXTURE_ENV_DOT3] = TRUE;
1128
	} else if (strcmp(ThisExtn, "GL_EXT_texture_filter_anisotropic") == 0) {
1129
	  TRACE_(d3d_caps)(" FOUND: EXT Texture Anisotropic filter support\n");
1130 1131
	  This->gl_info.supported[EXT_TEXTURE_FILTER_ANISOTROPIC] = TRUE;
	} else if (strcmp(ThisExtn, "GL_EXT_texture_lod") == 0) {
1132
	  TRACE_(d3d_caps)(" FOUND: EXT Texture LOD support\n");
1133 1134
	  This->gl_info.supported[EXT_TEXTURE_LOD] = TRUE;
	} else if (strcmp(ThisExtn, "GL_EXT_texture_lod_bias") == 0) {
1135
	  TRACE_(d3d_caps)(" FOUND: EXT Texture LOD bias support\n");
1136 1137
	  This->gl_info.supported[EXT_TEXTURE_LOD_BIAS] = TRUE;
	} else if (strcmp(ThisExtn, "GL_EXT_vertex_weighting") == 0) {
1138
	  TRACE_(d3d_caps)(" FOUND: EXT Vertex weighting support\n");
1139 1140 1141 1142 1143
	  This->gl_info.supported[EXT_VERTEX_WEIGHTING] = TRUE;

	/**
	 * NVIDIA 
	 */
1144 1145 1146
	} else if (strstr(ThisExtn, "GL_NV_fog_distance")) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Fog Distance support\n");
	  This->gl_info.supported[NV_FOG_DISTANCE] = TRUE;
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	} else if (strstr(ThisExtn, "GL_NV_fragment_program")) {
	  This->gl_info.ps_nv_version = PS_VERSION_11;
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Pixel Shader support - version=%02x\n", This->gl_info.ps_nv_version);
        } else if (strcmp(ThisExtn, "GL_NV_register_combiners") == 0) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (1) support\n");
	  This->gl_info.supported[NV_REGISTER_COMBINERS] = TRUE;
        } else if (strcmp(ThisExtn, "GL_NV_register_combiners2") == 0) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Register combiners (2) support\n");
	  This->gl_info.supported[NV_REGISTER_COMBINERS2] = TRUE;
        } else if (strcmp(ThisExtn, "GL_NV_texgen_reflection") == 0) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Gen Reflection support\n");
	  This->gl_info.supported[NV_TEXGEN_REFLECTION] = TRUE;
        } else if (strcmp(ThisExtn, "GL_NV_texture_env_combine4") == 0) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Env combine (4) support\n");
	  This->gl_info.supported[NV_TEXTURE_ENV_COMBINE4] = TRUE;
        } else if (strcmp(ThisExtn, "GL_NV_texture_shader") == 0) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (1) support\n");
	  This->gl_info.supported[NV_TEXTURE_SHADER] = TRUE;
        } else if (strcmp(ThisExtn, "GL_NV_texture_shader2") == 0) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (2) support\n");
	  This->gl_info.supported[NV_TEXTURE_SHADER2] = TRUE;
        } else if (strcmp(ThisExtn, "GL_NV_texture_shader3") == 0) {
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Texture Shader (3) support\n");
	  This->gl_info.supported[NV_TEXTURE_SHADER3] = TRUE;
1171 1172 1173
	} else if (strstr(ThisExtn, "GL_NV_vertex_program")) {
	  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);
	  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);
1174
	  TRACE_(d3d_caps)(" FOUND: NVIDIA (NV) Vertex Shader support - version=%02x\n", This->gl_info.vs_nv_version);
1175 1176 1177 1178 1179 1180
	  This->gl_info.supported[NV_VERTEX_PROGRAM] = TRUE;

	/**
	 * ATI
	 */
	/** TODO */
1181
        } else if (strcmp(ThisExtn, "GL_ATI_texture_env_combine3") == 0) {
1182
	  TRACE_(d3d_caps)(" FOUND: ATI Texture Env combine (3) support\n");
1183 1184
	  This->gl_info.supported[ATI_TEXTURE_ENV_COMBINE3] = TRUE;
        } else if (strcmp(ThisExtn, "GL_ATI_texture_mirror_once") == 0) {
1185
	  TRACE_(d3d_caps)(" FOUND: ATI Texture Mirror Once support\n");
1186
	  This->gl_info.supported[ATI_TEXTURE_MIRROR_ONCE] = TRUE;
1187 1188
	} else if (strcmp(ThisExtn, "GL_EXT_vertex_shader") == 0) {
	  This->gl_info.vs_ati_version = VS_VERSION_11;
1189
	  TRACE_(d3d_caps)(" FOUND: ATI (EXT) Vertex Shader support - version=%02x\n", This->gl_info.vs_ati_version);
1190 1191 1192 1193 1194 1195 1196 1197
	  This->gl_info.supported[EXT_VERTEX_SHADER] = TRUE;
	}


        if (*GL_Extensions == ' ') GL_Extensions++;
      }
    }

1198 1199 1200 1201
#define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
    GL_EXT_FUNCS_GEN;
#undef USE_GL_FUNC

1202 1203
    if (display != NULL) {
        GLX_Extensions = glXQueryExtensionsString(display, DefaultScreen(display));
1204
        TRACE_(d3d_caps)("GLX_Extensions reported:\n");  
1205
    
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
        if (NULL == GLX_Extensions) {
          ERR("   GLX_Extensions returns NULL\n");      
        } else {
          while (*GLX_Extensions != 0x00) {
            const char *Start = GLX_Extensions;
            char ThisExtn[256];
           
            memset(ThisExtn, 0x00, sizeof(ThisExtn));
            while (*GLX_Extensions != ' ' && *GLX_Extensions != 0x00) {
              GLX_Extensions++;
            }
            memcpy(ThisExtn, Start, (GLX_Extensions - Start));
1218
            TRACE_(d3d_caps)("- %s\n", ThisExtn);
1219 1220
            if (*GLX_Extensions == ' ') GLX_Extensions++;
          }
1221 1222
        }
    }
1223 1224 1225 1226 1227

#define USE_GL_FUNC(type, pfn) This->gl_info.pfn = (type) glXGetProcAddressARB(#pfn);
    GLX_EXT_FUNCS_GEN;
#undef USE_GL_FUNC

1228 1229 1230
    /* Only save the values obtained when a display is provided */
    if (display != NULL) This->isGLInfoValid = TRUE;

1231 1232
}

1233 1234 1235 1236 1237 1238
HRESULT  WINAPI  IDirect3D8Impl_CreateDevice               (LPDIRECT3D8 iface,
                                                            UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
                                                            DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
                                                            IDirect3DDevice8** ppReturnedDeviceInterface) {
    IDirect3DDevice8Impl *object;
    HWND whichHWND;
1239 1240
    int num;
    XVisualInfo template;
1241
    HDC hDc;
1242 1243

    ICOM_THIS(IDirect3D8Impl,iface);
1244
    TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
1245 1246
          hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);

1247 1248 1249 1250
    if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
        return D3DERR_INVALIDCALL;
    }

1251 1252
    /* Allocate the storage for the device */
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
1253 1254 1255
    if (NULL == object) {
      return D3DERR_OUTOFVIDEOMEMORY;
    }
1256 1257 1258
    object->lpVtbl = &Direct3DDevice8_Vtbl;
    object->ref = 1;
    object->direct3d8 = This;
1259 1260
    /** The device AddRef the direct3d8 Interface else crash in propers clients codes */
    IDirect3D8_AddRef((LPDIRECT3D8) object->direct3d8);
1261 1262 1263 1264 1265

    /** use StateBlock Factory here, for creating the startup stateBlock */
    object->StateBlock = NULL;
    IDirect3DDeviceImpl_CreateStateBlock(object, D3DSBT_ALL, NULL);
    object->UpdateStateBlock = object->StateBlock;
1266 1267 1268 1269 1270 1271 1272

    /* Save the creation parameters */
    object->CreateParms.AdapterOrdinal = Adapter;
    object->CreateParms.DeviceType = DeviceType;
    object->CreateParms.hFocusWindow = hFocusWindow;
    object->CreateParms.BehaviorFlags = BehaviourFlags;

1273
    *ppReturnedDeviceInterface = (LPDIRECT3DDEVICE8) object;
1274 1275 1276 1277 1278 1279

    /* Initialize settings */
    object->PresentParms.BackBufferCount = 1; /* Opengl only supports one? */
    object->adapterNo = Adapter;
    object->devType = DeviceType;

1280 1281
    /* Initialize openGl - Note the visual is chosen as the window is created and the glcontext cannot
         use different properties after that point in time. FIXME: How to handle when requested format 
1282
         doesn't match actual visual? Cannot choose one here - code removed as it ONLY works if the one
1283 1284
         it chooses is identical to the one already being used!                                        */
    /* FIXME: Handle stencil appropriately via EnableAutoDepthStencil / AutoDepthStencilFormat */
1285

1286 1287 1288 1289 1290 1291 1292
    /* Which hwnd are we using? */
    whichHWND = pPresentationParameters->hDeviceWindow;
    if (!whichHWND) {
        whichHWND = hFocusWindow;
    }
    object->win_handle = whichHWND;
    object->win     = (Window)GetPropA( whichHWND, "__wine_x11_client_window" );
1293

1294 1295
    hDc = GetDC(whichHWND);
    object->display = get_display(hDc);
1296

1297 1298 1299
    TRACE("(%p)->(DepthStencil:(%u,%s), BackBufferFormat:(%u,%s))\n", This, 
          pPresentationParameters->AutoDepthStencilFormat, debug_d3dformat(pPresentationParameters->AutoDepthStencilFormat),
          pPresentationParameters->BackBufferFormat, debug_d3dformat(pPresentationParameters->BackBufferFormat));
1300

1301
    ENTER_GL();
1302

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    /* Create a context based off the properties of the existing visual */
    template.visualid = (VisualID)GetPropA(GetDesktopWindow(), "__wine_x11_visual_id");
    object->visInfo = XGetVisualInfo(object->display, VisualIDMask, &template, &num);
    if (NULL == object->visInfo) {
        ERR("cannot really get XVisual\n"); 
        LEAVE_GL();
        return D3DERR_NOTAVAILABLE;
     }
    object->glCtx = glXCreateContext(object->display, object->visInfo, NULL, GL_TRUE);
    if (NULL == object->glCtx) {
      ERR("cannot create glxContext\n"); 
      LEAVE_GL();
      return D3DERR_NOTAVAILABLE;
     }
    LEAVE_GL();
1318

1319 1320
    ReleaseDC(whichHWND, hDc);
    
1321 1322 1323 1324
    if (object->glCtx == NULL) {
        ERR("Error in context creation !\n");
        return D3DERR_INVALIDCALL;
    } else {
1325
        TRACE("Context created (HWND=%p, glContext=%p, Window=%ld, VisInfo=%p)\n",
1326
	      whichHWND, object->glCtx, object->win, object->visInfo);
1327 1328
    }

1329 1330 1331
    /* If not windowed, need to go fullscreen, and resize the HWND to the appropriate  */
    /*        dimensions                                                               */
    if (!pPresentationParameters->Windowed) {
1332
#if 1
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	DEVMODEW devmode;
	HDC hdc;
        int bpp = 0;
	memset(&devmode, 0, sizeof(DEVMODEW));
	devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; 
	MultiByteToWideChar(CP_ACP, 0, "Gamers CG", -1, devmode.dmDeviceName, CCHDEVICENAME);
        hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
        bpp = GetDeviceCaps(hdc, BITSPIXEL);
        DeleteDC(hdc);
	devmode.dmBitsPerPel = (bpp >= 24) ? 32 : bpp;/*Stupid XVidMode cannot change bpp D3DFmtGetBpp(object, pPresentationParameters->BackBufferFormat);*/
	devmode.dmPelsWidth  = pPresentationParameters->BackBufferWidth;
	devmode.dmPelsHeight = pPresentationParameters->BackBufferHeight;
	ChangeDisplaySettingsExW(devmode.dmDeviceName, &devmode, object->win_handle, CDS_FULLSCREEN, NULL);
#else
1347
        FIXME("Requested full screen support not implemented, expect windowed operation\n");
1348
#endif
1349 1350 1351 1352

        /* Make popup window */
        ShowWindow(whichHWND, SW_HIDE);
        SetWindowLongA(whichHWND, GWL_STYLE, WS_POPUP);
1353 1354 1355
        SetWindowPos(object->win_handle, HWND_TOP, 0, 0, 
		     pPresentationParameters->BackBufferWidth,
                     pPresentationParameters->BackBufferHeight, SWP_SHOWWINDOW | SWP_FRAMECHANGED);
1356
        ShowWindow(whichHWND, SW_SHOW);
1357 1358
    }

1359 1360 1361 1362 1363
    TRACE("Creating back buffer\n");
    /* MSDN: If Windowed is TRUE and either of the BackBufferWidth/Height values is zero,
       then the corresponding dimension of the client area of the hDeviceWindow
       (or the focus window, if hDeviceWindow is NULL) is taken. */
    if (pPresentationParameters->Windowed && ((pPresentationParameters->BackBufferWidth  == 0) ||
1364
                                              (pPresentationParameters->BackBufferHeight == 0))) {
1365 1366 1367 1368
        RECT Rect;

        GetClientRect(whichHWND, &Rect);

1369
        if (pPresentationParameters->BackBufferWidth == 0) {
1370 1371 1372
           pPresentationParameters->BackBufferWidth = Rect.right;
           TRACE("Updating width to %d\n", pPresentationParameters->BackBufferWidth);
        }
1373
        if (pPresentationParameters->BackBufferHeight == 0) {
1374 1375 1376 1377 1378
           pPresentationParameters->BackBufferHeight = Rect.bottom;
           TRACE("Updating height to %d\n", pPresentationParameters->BackBufferHeight);
        }
    }

1379 1380 1381 1382
    /* Save the presentation parms now filled in correctly */
    memcpy(&object->PresentParms, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));


1383 1384 1385 1386
    IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
                                            pPresentationParameters->BackBufferWidth,
                                            pPresentationParameters->BackBufferHeight,
                                            pPresentationParameters->BackBufferFormat,
1387 1388
					    pPresentationParameters->MultiSampleType,
					    TRUE,
1389 1390 1391
                                            (LPDIRECT3DSURFACE8*) &object->frontBuffer);

    IDirect3DDevice8Impl_CreateRenderTarget((LPDIRECT3DDEVICE8) object,
1392 1393 1394
                                            pPresentationParameters->BackBufferWidth,
                                            pPresentationParameters->BackBufferHeight,
                                            pPresentationParameters->BackBufferFormat,
1395 1396
					    pPresentationParameters->MultiSampleType,
					    TRUE,
1397 1398
                                            (LPDIRECT3DSURFACE8*) &object->backBuffer);

1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
    if (pPresentationParameters->EnableAutoDepthStencil) {
       IDirect3DDevice8Impl_CreateDepthStencilSurface((LPDIRECT3DDEVICE8) object,
	  					      pPresentationParameters->BackBufferWidth,
						      pPresentationParameters->BackBufferHeight,
						      pPresentationParameters->AutoDepthStencilFormat,
						      D3DMULTISAMPLE_NONE,
						      (LPDIRECT3DSURFACE8*) &object->depthStencilBuffer);
    } else {
      object->depthStencilBuffer = NULL;
    }
1409
    TRACE("FrontBuf @ %p, BackBuf @ %p, DepthStencil @ %p\n",object->frontBuffer, object->backBuffer, object->depthStencilBuffer);
1410 1411 1412 1413

    /* init the default renderTarget management */
    object->drawable = object->win;
    object->render_ctx = object->glCtx;
1414
    object->renderTarget = object->backBuffer;
1415 1416
    IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->renderTarget);
    object->stencilBufferTarget = object->depthStencilBuffer;
Raphael Junqueira's avatar
Raphael Junqueira committed
1417
    if (NULL != object->stencilBufferTarget) {
1418
      IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) object->stencilBufferTarget);
Raphael Junqueira's avatar
Raphael Junqueira committed
1419
    }
1420

1421
    ENTER_GL();
1422

1423
    if (glXMakeCurrent(object->display, object->win, object->glCtx) == False) {
1424
      ERR("Error in setting current context (context %p drawable %ld)!\n", object->glCtx, object->win);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
    }
    checkGLcall("glXMakeCurrent");

    /* Clear the screen */
    glClearColor(1.0, 0.0, 0.0, 0.0);
    checkGLcall("glClearColor");
    glColor3f(1.0, 1.0, 1.0);
    checkGLcall("glColor3f");

    glEnable(GL_LIGHTING);
    checkGLcall("glEnable");

1437 1438 1439
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
    checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");

1440
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1441
    checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1442

Raphael Junqueira's avatar
Raphael Junqueira committed
1443 1444
    glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
    checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1445

1446 1447 1448 1449
    /* 
     * Initialize openGL extension related variables
     *  with Default values 
     */
1450
    IDirect3D8Impl_FillGLCaps(iface, object->display);
1451

1452
    /* Setup all the devices defaults */
1453
    IDirect3DDeviceImpl_InitStartupStateBlock(object);
1454

1455
    LEAVE_GL();
1456

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
    { /* Set a default viewport */
       D3DVIEWPORT8 vp;
       vp.X      = 0;
       vp.Y      = 0;
       vp.Width  = pPresentationParameters->BackBufferWidth;
       vp.Height = pPresentationParameters->BackBufferHeight;
       vp.MinZ   = 0.0f;
       vp.MaxZ   = 1.0f;
       IDirect3DDevice8Impl_SetViewport((LPDIRECT3DDEVICE8) object, &vp);
    }

1468 1469 1470 1471 1472
    /* Initialize the current view state */
    object->modelview_valid = 1;
    object->proj_valid = 0;
    object->view_ident = 1;
    object->last_was_rhw = 0;
1473
    glGetIntegerv(GL_MAX_LIGHTS, &object->maxConcurrentLights);
1474
    TRACE("(%p,%d) All defaults now set up, leaving CreateDevice with %p\n", This, Adapter, object);
1475 1476 1477 1478

    /* Clear the screen */
    IDirect3DDevice8Impl_Clear((LPDIRECT3DDEVICE8) object, 0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER|D3DCLEAR_TARGET, 0x00, 1.0, 0);

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
    return D3D_OK;
}

ICOM_VTABLE(IDirect3D8) Direct3D8_Vtbl =
{
    ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
    IDirect3D8Impl_QueryInterface,
    IDirect3D8Impl_AddRef,
    IDirect3D8Impl_Release,
    IDirect3D8Impl_RegisterSoftwareDevice,
    IDirect3D8Impl_GetAdapterCount,
    IDirect3D8Impl_GetAdapterIdentifier,
    IDirect3D8Impl_GetAdapterModeCount,
    IDirect3D8Impl_EnumAdapterModes,
    IDirect3D8Impl_GetAdapterDisplayMode,
    IDirect3D8Impl_CheckDeviceType,
    IDirect3D8Impl_CheckDeviceFormat,
    IDirect3D8Impl_CheckDeviceMultiSampleType,
    IDirect3D8Impl_CheckDepthStencilMatch,
    IDirect3D8Impl_GetDeviceCaps,
    IDirect3D8Impl_GetAdapterMonitor,
    IDirect3D8Impl_CreateDevice
};