2 * IDirect3D8 implementation
4 * Copyright 2002 Jason Edmeades
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.
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.
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
25 #include "wine/debug.h"
30 #include "d3d8_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 static const float idmatrix[16] = {
42 static const int modes[NUM_MODES][3] = {
55 /* retrieve the X display to use on a given DC */
56 inline static Display *get_display( HDC hdc )
59 enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;
61 if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
62 sizeof(display), (LPSTR)&display )) display = NULL;
67 /* IDirect3D IUnknown parts follow: */
68 HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface,REFIID riid,LPVOID *ppobj)
70 ICOM_THIS(IDirect3D8Impl,iface);
72 if (IsEqualGUID(riid, &IID_IUnknown)
73 || IsEqualGUID(riid, &IID_IClassFactory)) {
74 IDirect3D8Impl_AddRef(iface);
79 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
83 ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
84 ICOM_THIS(IDirect3D8Impl,iface);
85 TRACE("(%p) : AddRef from %ld\n", This, This->ref);
89 ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
90 ICOM_THIS(IDirect3D8Impl,iface);
91 ULONG ref = --This->ref;
92 TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
94 HeapFree(GetProcessHeap(), 0, This);
98 /* IDirect3D Interface follow: */
99 HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
100 ICOM_THIS(IDirect3D8Impl,iface);
101 FIXME("(%p)->(%p): stub\n", This, pInitializeFunction);
105 UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
106 ICOM_THIS(IDirect3D8Impl,iface);
107 /* FIXME: Set to one for now to imply the display */
108 TRACE("(%p): Mostly stub, only returns primary display\n", This);
112 HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier (LPDIRECT3D8 iface,
113 UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
114 ICOM_THIS(IDirect3D8Impl,iface);
116 TRACE("(%p}->(Adapter: %d, Flags: %lx, pId=%p)\n", This, Adapter, Flags, pIdentifier);
118 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
119 return D3DERR_INVALIDCALL;
122 if (Adapter == 0) { /* Display */
123 strcpy(pIdentifier->Driver, "Display");
124 strcpy(pIdentifier->Description, "Direct3D Display");
125 pIdentifier->DriverVersion.s.HighPart = 1;
126 pIdentifier->DriverVersion.s.LowPart = 0;
127 pIdentifier->VendorId = 0;
128 pIdentifier->DeviceId = 0;
129 pIdentifier->SubSysId = 0;
130 pIdentifier->Revision = 0;
131 /*FIXME: memcpy(&pIdentifier->DeviceIdentifier, ??, sizeof(??GUID)); */
132 if (Flags & D3DENUM_NO_WHQL_LEVEL ) {
133 pIdentifier->WHQLLevel = 0;
135 pIdentifier->WHQLLevel = 1;
138 FIXME("Adapter not primary display\n");
144 UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,
146 ICOM_THIS(IDirect3D8Impl,iface);
148 TRACE("(%p}->(Adapter: %d)\n", This, Adapter);
150 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
151 return D3DERR_INVALIDCALL;
154 if (Adapter == 0) { /* Display */
155 int maxWidth = GetSystemMetrics(SM_CXSCREEN);
156 int maxHeight = GetSystemMetrics(SM_CYSCREEN);
159 for (i=0; i<NUM_MODES; i++) {
160 if (modes[i][0] > maxWidth || modes[i][1] > maxHeight) {
166 FIXME("Adapter not primary display\n");
172 HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface,
173 UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
174 ICOM_THIS(IDirect3D8Impl,iface);
176 TRACE("(%p}->(Adapter: %d, mode: %d, pMode=%p)\n", This, Adapter, Mode, pMode);
178 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
179 return D3DERR_INVALIDCALL;
182 if (Adapter == 0) { /* Display */
187 pMode->Width = GetSystemMetrics(SM_CXSCREEN);
188 pMode->Height = GetSystemMetrics(SM_CYSCREEN);
189 pMode->RefreshRate = 85; /*FIXME: How to identify? */
190 } else if (Mode < (NUM_MODES+1)) {
191 pMode->Width = modes[Mode-1][0];
192 pMode->Height = modes[Mode-1][1];
193 pMode->RefreshRate = modes[Mode-1][2];
195 TRACE("Requested mode out of range %d\n", Mode);
196 return D3DERR_INVALIDCALL;
199 hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
200 bpp = GetDeviceCaps(hdc, BITSPIXEL);
204 case 8: pMode->Format = D3DFMT_R3G3B2; break;
205 case 16: pMode->Format = D3DFMT_A4R4G4B4; break;
206 case 24: pMode->Format = D3DFMT_R8G8B8; break;
207 case 32: pMode->Format = D3DFMT_A8R8G8B8; break;
208 default: pMode->Format = D3DFMT_UNKNOWN;
210 TRACE("W %d H %d rr %d fmt %x\n", pMode->Width, pMode->Height, pMode->RefreshRate, pMode->Format);
213 FIXME("Adapter not primary display\n");
219 HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface,
220 UINT Adapter, D3DDISPLAYMODE* pMode) {
221 ICOM_THIS(IDirect3D8Impl,iface);
222 TRACE("(%p}->(Adapter: %d, pMode: %p)\n", This, Adapter, pMode);
224 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
225 return D3DERR_INVALIDCALL;
228 if (Adapter == 0) { /* Display */
232 pMode->Width = GetSystemMetrics(SM_CXSCREEN);
233 pMode->Height = GetSystemMetrics(SM_CYSCREEN);
234 pMode->RefreshRate = 85; /*FIXME: How to identify? */
236 hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
237 bpp = GetDeviceCaps(hdc, BITSPIXEL);
241 case 8: pMode->Format = D3DFMT_R3G3B2; break;
242 case 16: pMode->Format = D3DFMT_A4R4G4B4; break;
243 case 24: pMode->Format = D3DFMT_R8G8B8; break;
244 case 32: pMode->Format = D3DFMT_A8R8G8B8; break;
245 default: pMode->Format = D3DFMT_UNKNOWN;
249 FIXME("Adapter not primary display\n");
252 TRACE("returning w:%d, h:%d, ref:%d, fmt:%d\n", pMode->Width,
253 pMode->Height, pMode->RefreshRate, pMode->Format);
257 HRESULT WINAPI IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface,
258 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
259 D3DFORMAT BackBufferFormat, BOOL Windowed) {
260 ICOM_THIS(IDirect3D8Impl,iface);
261 FIXME("(%p)->(Adptr:%d, CheckType:%x, DispFmt:%x, BackBuf:%x, Win? %d): stub\n", This, Adapter, CheckType,
262 DisplayFormat, BackBufferFormat, Windowed);
266 HRESULT WINAPI IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface,
267 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
268 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
269 ICOM_THIS(IDirect3D8Impl,iface);
270 FIXME("(%p)->(Adptr:%d, DevType: %x, AdptFmt: %d, Use: %ld, ResTyp: %x, CheckFmt: %d)\n", This, Adapter, DeviceType,
271 AdapterFormat, Usage, RType, CheckFormat);
275 HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType (LPDIRECT3D8 iface,
276 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
277 BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
278 ICOM_THIS(IDirect3D8Impl,iface);
279 FIXME("(%p)->(Adptr:%d, DevType: %x, SurfFmt: %x, Win? %d, MultiSamp: %x)\n", This, Adapter, DeviceType,
280 SurfaceFormat, Windowed, MultiSampleType);
284 HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch (LPDIRECT3D8 iface,
285 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
286 D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
287 ICOM_THIS(IDirect3D8Impl,iface);
288 FIXME("(%p)->(Adptr:%d, DevType: %x, AdptFmt: %x, RendrTgtFmt: %x, DepthStencilFmt: %x)\n", This, Adapter, DeviceType,
289 AdapterFormat, RenderTargetFormat, DepthStencilFormat);
293 HRESULT WINAPI IDirect3D8Impl_GetDeviceCaps (LPDIRECT3D8 iface,
294 UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
295 ICOM_THIS(IDirect3D8Impl,iface);
296 TRACE("(%p)->(Adptr:%d, DevType: %x, pCaps: %p)\n", This, Adapter, DeviceType, pCaps);
299 /* NOTE: Most of the values here are complete garbage for now */
300 pCaps->DeviceType = D3DDEVTYPE_HAL; /* Not quite true, but use h/w supported by opengl I suppose */
301 pCaps->AdapterOrdinal = Adapter;
304 pCaps->Caps2 = D3DCAPS2_CANRENDERWINDOWED;
305 pCaps->Caps3 = D3DDEVCAPS_HWTRANSFORMANDLIGHT;
306 pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE;
308 pCaps->CursorCaps = 0;
310 pCaps->DevCaps = D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_PUREDEVICE;
312 pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_CULLCCW | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPTLVERTS |
313 D3DPMISCCAPS_CLIPPLANESCALEDPOINTS | D3DPMISCCAPS_MASKZ; /*NOT: D3DPMISCCAPS_TSSARGTEMP*/
314 pCaps->RasterCaps = D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT;
315 pCaps->ZCmpCaps = D3DPCMPCAPS_ALWAYS | D3DPCMPCAPS_EQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_GREATEREQUAL |
316 D3DPCMPCAPS_LESS | D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_NEVER | D3DPCMPCAPS_NOTEQUAL;
318 pCaps->SrcBlendCaps = 0;
319 pCaps->DestBlendCaps = 0;
320 pCaps->AlphaCmpCaps = 0;
321 pCaps->ShadeCaps = D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_COLORGOURAUDRGB ;
322 pCaps->TextureCaps = D3DPTEXTURECAPS_ALPHA | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_POW2;
323 pCaps->TextureFilterCaps = 0;
324 pCaps->CubeTextureFilterCaps = 0;
325 pCaps->VolumeTextureFilterCaps = 0;
326 pCaps->TextureAddressCaps = D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_CLAMP | D3DPTADDRESSCAPS_WRAP;
327 pCaps->VolumeTextureAddressCaps = 0;
329 pCaps->LineCaps = D3DLINECAPS_TEXTURE | D3DLINECAPS_ZTEST;
331 pCaps->MaxTextureWidth = 16384;
332 pCaps->MaxTextureHeight = 16384;
333 pCaps->MaxVolumeExtent = 0;
335 pCaps->MaxTextureRepeat = 32768;
336 pCaps->MaxTextureAspectRatio = 32768;
337 pCaps->MaxAnisotropy = 0;
338 pCaps->MaxVertexW = 1.0;
340 pCaps->GuardBandLeft = 0;
341 pCaps->GuardBandTop = 0;
342 pCaps->GuardBandRight = 0;
343 pCaps->GuardBandBottom = 0;
345 pCaps->ExtentsAdjust = 0;
347 pCaps->StencilCaps = 0;
349 pCaps->FVFCaps = D3DFVFCAPS_PSIZE | 0x80000;
350 pCaps->TextureOpCaps = 0xFFFFFFFF;
351 pCaps->MaxTextureBlendStages = 256;
352 pCaps->MaxSimultaneousTextures = 256;
354 pCaps->VertexProcessingCaps = D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_TEXGEN;
356 pCaps->MaxActiveLights = 8;
357 pCaps->MaxUserClipPlanes = 1;
358 pCaps->MaxVertexBlendMatrices = 1;
359 pCaps->MaxVertexBlendMatrixIndex = 1;
361 pCaps->MaxPointSize = 128.0;
363 pCaps->MaxPrimitiveCount = 0xFFFFFFFF;
364 pCaps->MaxVertexIndex = 0xFFFFFFFF;
365 pCaps->MaxStreams = 1;
366 pCaps->MaxStreamStride = 1024;
368 pCaps->VertexShaderVersion = 01;
369 pCaps->MaxVertexShaderConst = 1;
371 pCaps->PixelShaderVersion = 01;
372 pCaps->MaxPixelShaderValue = 1.0;
377 HMONITOR WINAPI IDirect3D8Impl_GetAdapterMonitor (LPDIRECT3D8 iface,
379 ICOM_THIS(IDirect3D8Impl,iface);
380 FIXME("(%p)->(Adptr:%d)\n", This, Adapter);
384 HRESULT WINAPI IDirect3D8Impl_CreateDevice (LPDIRECT3D8 iface,
385 UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
386 DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
387 IDirect3DDevice8** ppReturnedDeviceInterface) {
388 IDirect3DDevice8Impl *object;
391 ICOM_THIS(IDirect3D8Impl,iface);
392 TRACE("(%p)->(Adptr:%d, DevType: %x, FocusHwnd: %p, BehFlags: %lx, PresParms: %p, RetDevInt: %p)\n", This, Adapter, DeviceType,
393 hFocusWindow, BehaviourFlags, pPresentationParameters, ppReturnedDeviceInterface);
395 /* Allocate the storage for the device */
396 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
397 object->lpVtbl = &Direct3DDevice8_Vtbl;
399 object->direct3d8 = This;
400 object->UpdateStateBlock = &object->StateBlock;
402 /* Save the creation parameters */
403 object->CreateParms.AdapterOrdinal = Adapter;
404 object->CreateParms.DeviceType = DeviceType;
405 object->CreateParms.hFocusWindow = hFocusWindow;
406 object->CreateParms.BehaviorFlags = BehaviourFlags;
408 CreateStateBlock((LPDIRECT3DDEVICE8) object);
410 *ppReturnedDeviceInterface = (LPDIRECT3DDEVICE8)object;
412 /* Initialize settings */
413 memcpy(&object->PresentParms, pPresentationParameters, sizeof(D3DPRESENT_PARAMETERS));
414 memcpy(&object->StateBlock.transforms[D3DTS_WORLDMATRIX(0)], &idmatrix, sizeof(idmatrix));
415 memcpy(&object->StateBlock.transforms[D3DTS_PROJECTION], &idmatrix, sizeof(idmatrix));
416 memcpy(&object->StateBlock.transforms[D3DTS_VIEW], &idmatrix, sizeof(idmatrix));
418 object->PresentParms.BackBufferCount = 1; /* Opengl only supports one? */
419 pPresentationParameters->BackBufferCount = 1;
421 object->adapterNo = Adapter;
422 object->devType = DeviceType;
424 /* Initialize openGl */
427 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
428 /*int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16, None}; // Useful for debugging */
430 /* Which hwnd are we using? */
431 /* if (pPresentationParameters->Windowed) { */
432 whichHWND = pPresentationParameters->hDeviceWindow;
434 whichHWND = hFocusWindow;
436 object->win = (Window)GetPropA( whichHWND, "__wine_x11_client_window" );
439 * whichHWND = (HWND) GetDesktopWindow();
440 * object->win = (Window)GetPropA(whichHWND, "__wine_x11_whole_window" );
445 hDc = GetDC(whichHWND);
446 object->display = get_display(hDc);
449 object->visInfo = glXChooseVisual(object->display, DefaultScreen(object->display), dblBuf);
450 object->glCtx = glXCreateContext(object->display, object->visInfo, NULL, GL_TRUE);
452 ReleaseDC(whichHWND, hDc);
456 if (object->glCtx == NULL) {
457 ERR("Error in context creation !\n");
458 return D3DERR_INVALIDCALL;
460 TRACE("Context created (HWND=%p, glContext=%p, Window=%ld, VisInfo=%p)\n",
461 whichHWND, object->glCtx, object->win, object->visInfo);
464 TRACE("Creating back buffer\n");
465 /* MSDN: If Windowed is TRUE and either of the BackBufferWidth/Height values is zero,
466 then the corresponding dimension of the client area of the hDeviceWindow
467 (or the focus window, if hDeviceWindow is NULL) is taken. */
468 if (pPresentationParameters->Windowed && ((pPresentationParameters->BackBufferWidth == 0) ||
469 (pPresentationParameters->BackBufferHeight == 0))) {
472 GetClientRect(whichHWND, &Rect);
474 if (pPresentationParameters->BackBufferWidth == 0) {
475 pPresentationParameters->BackBufferWidth = Rect.right;
476 TRACE("Updating width to %d\n", pPresentationParameters->BackBufferWidth);
478 if (pPresentationParameters->BackBufferHeight == 0) {
479 pPresentationParameters->BackBufferHeight = Rect.bottom;
480 TRACE("Updating height to %d\n", pPresentationParameters->BackBufferHeight);
484 IDirect3DDevice8Impl_CreateImageSurface((LPDIRECT3DDEVICE8) object,
485 pPresentationParameters->BackBufferWidth,
486 pPresentationParameters->BackBufferHeight,
487 pPresentationParameters->BackBufferFormat,
488 (LPDIRECT3DSURFACE8*) &object->backBuffer);
490 /* Now override the surface's Flip method (if in double buffering) ?COPIED from DDRAW!?
491 ((x11_ds_private *) surface->private)->opengl_flip = TRUE;
494 struct _surface_chain *chain = surface->s.chain;
495 for (i=0;i<chain->nrofsurfaces;i++)
496 if (chain->surfaces[i]->s.surface_desc.ddsCaps.dwCaps & DDSCAPS_FLIP)
497 ((x11_ds_private *) chain->surfaces[i]->private)->opengl_flip = TRUE;
502 if (glXMakeCurrent(object->display, object->win, object->glCtx) == False) {
503 ERR("Error in setting current context (context %p drawable %ld)!\n",
504 object->glCtx, object->win);
506 checkGLcall("glXMakeCurrent");
508 /* Clear the screen */
509 glClearColor(1.0, 0.0, 0.0, 0.0);
510 checkGLcall("glClearColor");
511 glColor3f(1.0, 1.0, 1.0);
512 checkGLcall("glColor3f");
514 glEnable(GL_LIGHTING);
515 checkGLcall("glEnable");
517 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
518 checkGLcall("glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
522 { /* Set a default viewport */
526 vp.Width = pPresentationParameters->BackBufferWidth;
527 vp.Height = pPresentationParameters->BackBufferHeight;
530 IDirect3DDevice8Impl_SetViewport((LPDIRECT3DDEVICE8) object, &vp);
533 TRACE("(%p,%d) incomplete\n", This, Adapter);
537 ICOM_VTABLE(IDirect3D8) Direct3D8_Vtbl =
539 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
540 IDirect3D8Impl_QueryInterface,
541 IDirect3D8Impl_AddRef,
542 IDirect3D8Impl_Release,
543 IDirect3D8Impl_RegisterSoftwareDevice,
544 IDirect3D8Impl_GetAdapterCount,
545 IDirect3D8Impl_GetAdapterIdentifier,
546 IDirect3D8Impl_GetAdapterModeCount,
547 IDirect3D8Impl_EnumAdapterModes,
548 IDirect3D8Impl_GetAdapterDisplayMode,
549 IDirect3D8Impl_CheckDeviceType,
550 IDirect3D8Impl_CheckDeviceFormat,
551 IDirect3D8Impl_CheckDeviceMultiSampleType,
552 IDirect3D8Impl_CheckDepthStencilMatch,
553 IDirect3D8Impl_GetDeviceCaps,
554 IDirect3D8Impl_GetAdapterMonitor,
555 IDirect3D8Impl_CreateDevice