Sync ddraw, d3d8 and d3d9 + wined3d to wine 1.1.28
[reactos.git] / reactos / dll / directx / wine / d3d8 / directx.c
1 /*
2 * IDirect3D8 implementation
3 *
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 #include "d3d8_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39
40 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
42 {
43 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
44
45 if (IsEqualGUID(riid, &IID_IUnknown)
46 || IsEqualGUID(riid, &IID_IDirect3D8)) {
47 IUnknown_AddRef(iface);
48 *ppobj = This;
49 return S_OK;
50 }
51
52 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
53 *ppobj = NULL;
54 return E_NOINTERFACE;
55 }
56
57 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
58 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
59 ULONG ref = InterlockedIncrement(&This->ref);
60
61 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
62
63 return ref;
64 }
65
66 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
67 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
68 ULONG ref = InterlockedDecrement(&This->ref);
69
70 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
71
72 if (ref == 0) {
73 TRACE("Releasing wined3d %p\n", This->WineD3D);
74
75 wined3d_mutex_lock();
76 IWineD3D_Release(This->WineD3D);
77 wined3d_mutex_unlock();
78
79 HeapFree(GetProcessHeap(), 0, This);
80 }
81
82 return ref;
83 }
84
85 /* IDirect3D8 Interface follow: */
86 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
87 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
88 HRESULT hr;
89 TRACE("(%p)->(%p)\n", This, pInitializeFunction);
90
91 wined3d_mutex_lock();
92 hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
93 wined3d_mutex_unlock();
94
95 return hr;
96 }
97
98 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
99 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
100 HRESULT hr;
101 TRACE("(%p)\n", This);
102
103 wined3d_mutex_lock();
104 hr = IWineD3D_GetAdapterCount(This->WineD3D);
105 wined3d_mutex_unlock();
106
107 return hr;
108 }
109
110 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface,
111 UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8 *pIdentifier)
112 {
113 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
114 WINED3DADAPTER_IDENTIFIER adapter_id;
115 HRESULT hr;
116
117 TRACE("(%p)->(%d,%08x, %p\n", This, Adapter, Flags, pIdentifier);
118
119 adapter_id.driver = pIdentifier->Driver;
120 adapter_id.driver_size = sizeof(pIdentifier->Driver);
121 adapter_id.description = pIdentifier->Description;
122 adapter_id.description_size = sizeof(pIdentifier->Description);
123 adapter_id.device_name = NULL; /* d3d9 only */
124 adapter_id.device_name_size = 0; /* d3d9 only */
125
126 wined3d_mutex_lock();
127 hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
128 wined3d_mutex_unlock();
129
130 pIdentifier->DriverVersion = adapter_id.driver_version;
131 pIdentifier->VendorId = adapter_id.vendor_id;
132 pIdentifier->DeviceId = adapter_id.device_id;
133 pIdentifier->SubSysId = adapter_id.subsystem_id;
134 pIdentifier->Revision = adapter_id.revision;
135 memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
136 pIdentifier->WHQLLevel = adapter_id.whql_level;
137
138 return hr;
139 }
140
141 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
142 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
143 HRESULT hr;
144 TRACE("(%p)->(%d)\n", This, Adapter);
145
146 wined3d_mutex_lock();
147 hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
148 wined3d_mutex_unlock();
149
150 return hr;
151 }
152
153 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
154 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
155 HRESULT hr;
156 TRACE("(%p)->(%d, %d, %p)\n", This, Adapter, Mode, pMode);
157
158 wined3d_mutex_lock();
159 hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
160 wined3d_mutex_unlock();
161
162 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
163
164 return hr;
165 }
166
167 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
168 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
169 HRESULT hr;
170 TRACE("(%p)->(%d,%p)\n", This, Adapter, pMode);
171
172 wined3d_mutex_lock();
173 hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
174 wined3d_mutex_unlock();
175
176 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
177
178 return hr;
179 }
180
181 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface,
182 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
183 D3DFORMAT BackBufferFormat, BOOL Windowed) {
184 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
185 HRESULT hr;
186 TRACE("(%p)->(%d, %d, %d, %d, %s)\n", This, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed ? "true" : "false");
187
188 wined3d_mutex_lock();
189 hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
190 wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
191 wined3d_mutex_unlock();
192
193 return hr;
194 }
195
196 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface,
197 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
198 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
199 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
200 HRESULT hr;
201 WINED3DRESOURCETYPE WineD3DRType;
202 TRACE("(%p)->(%d, %d, %d, %08x, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
203
204 if(CheckFormat == D3DFMT_R8G8B8)
205 {
206 /* See comment in dlls/d3d9/directx.c, IDirect3D9Impl_CheckDeviceFormat for details */
207 WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
208 return D3DERR_NOTAVAILABLE;
209 }
210
211
212 switch(RType) {
213 case D3DRTYPE_VERTEXBUFFER:
214 case D3DRTYPE_INDEXBUFFER:
215 WineD3DRType = WINED3DRTYPE_BUFFER;
216 break;
217
218 default:
219 WineD3DRType = RType;
220 break;
221 }
222
223 wined3d_mutex_lock();
224 hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
225 Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
226 wined3d_mutex_unlock();
227
228 return hr;
229 }
230
231 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT Adapter,
232 D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType)
233 {
234 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
235 HRESULT hr;
236 TRACE("(%p)-<(%d, %d, %d, %s, %d)\n", This, Adapter, DeviceType, SurfaceFormat, Windowed ? "true" : "false", MultiSampleType);
237
238 wined3d_mutex_lock();
239 hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
240 wined3dformat_from_d3dformat(SurfaceFormat), Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
241 wined3d_mutex_unlock();
242
243 return hr;
244 }
245
246 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8 *iface, UINT Adapter, D3DDEVTYPE DeviceType,
247 D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
248 {
249 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
250 HRESULT hr;
251 TRACE("(%p)-<(%d, %d, %d, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
252
253 wined3d_mutex_lock();
254 hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
255 wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
256 wined3dformat_from_d3dformat(DepthStencilFormat));
257 wined3d_mutex_unlock();
258
259 return hr;
260 }
261
262 void fixup_caps(WINED3DCAPS *caps)
263 {
264 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
265 if (caps->PixelShaderVersion > D3DPS_VERSION(1,4)) {
266 caps->PixelShaderVersion = D3DPS_VERSION(1,4);
267 }
268 if (caps->VertexShaderVersion > D3DVS_VERSION(1,1)) {
269 caps->VertexShaderVersion = D3DVS_VERSION(1,1);
270 }
271 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
272
273 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
274 }
275
276 static HRESULT WINAPI IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
277 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
278 HRESULT hrc = D3D_OK;
279 WINED3DCAPS *pWineCaps;
280
281 TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
282
283 if(NULL == pCaps){
284 return D3DERR_INVALIDCALL;
285 }
286 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
287 if(pWineCaps == NULL){
288 return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
289 }
290
291 wined3d_mutex_lock();
292 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
293 wined3d_mutex_unlock();
294
295 fixup_caps(pWineCaps);
296 WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
297 HeapFree(GetProcessHeap(), 0, pWineCaps);
298
299 TRACE("(%p) returning %p\n", This, pCaps);
300 return hrc;
301 }
302
303 static HMONITOR WINAPI IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
304 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
305 HMONITOR ret;
306 TRACE("(%p)->(%d)\n", This, Adapter);
307
308 wined3d_mutex_lock();
309 ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
310 wined3d_mutex_unlock();
311
312 return ret;
313 }
314
315 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
316 IDirect3DSurface8Impl* surfaceParent;
317 TRACE("(%p) call back\n", pSurface);
318
319 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
320 surfaceParent->isImplicit = FALSE;
321 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
322 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
323 }
324
325 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
326 IUnknown* swapChainParent;
327 TRACE("(%p) call back\n", pSwapChain);
328
329 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
330 IUnknown_Release(swapChainParent);
331 return IUnknown_Release(swapChainParent);
332 }
333
334 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
335 IDirect3DSurface8Impl* surfaceParent;
336 TRACE("(%p) call back\n", pSurface);
337
338 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
339 surfaceParent->isImplicit = FALSE;
340 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
341 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
342 }
343
344 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
345 DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
346 IDirect3DDevice8** ppReturnedDeviceInterface) {
347
348 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
349 IDirect3DDevice8Impl *object = NULL;
350 WINED3DPRESENT_PARAMETERS localParameters;
351 HRESULT hr;
352 TRACE("(%p) Relay\n", This);
353
354 /* Check the validity range of the adapter parameter */
355 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
356 *ppReturnedDeviceInterface = NULL;
357 return D3DERR_INVALIDCALL;
358 }
359
360 /* Allocate the storage for the device object */
361 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
362 if (NULL == object) {
363 FIXME("Allocation of memory failed\n");
364 *ppReturnedDeviceInterface = NULL;
365 return D3DERR_OUTOFVIDEOMEMORY;
366 }
367
368 object->lpVtbl = &Direct3DDevice8_Vtbl;
369 object->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
370 object->ref = 1;
371 object->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
372 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*object->handle_table.entries));
373 object->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
374 *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
375
376 /* Allocate an associated WineD3DDevice object */
377 wined3d_mutex_lock();
378 hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
379 (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
380
381 if (hr != D3D_OK) {
382 HeapFree(GetProcessHeap(), 0, object);
383 *ppReturnedDeviceInterface = NULL;
384 wined3d_mutex_unlock();
385
386 return hr;
387 }
388
389 TRACE("(%p) : Created Device %p\n", This, object);
390
391 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
392 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
393 localParameters.BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
394 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
395 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
396 localParameters.MultiSampleQuality = 0; /* d3d9 only */
397 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
398 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
399 localParameters.Windowed = pPresentationParameters->Windowed;
400 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
401 localParameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
402 localParameters.Flags = pPresentationParameters->Flags;
403 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
404 localParameters.PresentationInterval = pPresentationParameters->FullScreen_PresentationInterval;
405 localParameters.AutoRestoreDisplayMode = TRUE;
406
407 if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
408 IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
409 }
410
411 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
412 wined3d_mutex_unlock();
413
414 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
415 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
416 pPresentationParameters->BackBufferFormat = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
417 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
418 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
419 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
420 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
421 pPresentationParameters->Windowed = localParameters.Windowed;
422 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
423 pPresentationParameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
424 pPresentationParameters->Flags = localParameters.Flags;
425 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
426 pPresentationParameters->FullScreen_PresentationInterval = localParameters.PresentationInterval;
427
428 if (hr != D3D_OK) {
429 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
430 HeapFree(GetProcessHeap(), 0, object);
431 *ppReturnedDeviceInterface = NULL;
432 }
433
434 object->declArraySize = 16;
435 object->decls = HeapAlloc(GetProcessHeap(), 0, object->declArraySize * sizeof(*object->decls));
436 if(!object->decls) {
437 ERR("Out of memory\n");
438
439 wined3d_mutex_lock();
440 IWineD3DDevice_Release(object->WineD3DDevice);
441 wined3d_mutex_unlock();
442
443 HeapFree(GetProcessHeap(), 0, object);
444 *ppReturnedDeviceInterface = NULL;
445 hr = E_OUTOFMEMORY;
446 }
447 return hr;
448 }
449
450 const IDirect3D8Vtbl Direct3D8_Vtbl =
451 {
452 /* IUnknown */
453 IDirect3D8Impl_QueryInterface,
454 IDirect3D8Impl_AddRef,
455 IDirect3D8Impl_Release,
456 /* IDirect3D8 */
457 IDirect3D8Impl_RegisterSoftwareDevice,
458 IDirect3D8Impl_GetAdapterCount,
459 IDirect3D8Impl_GetAdapterIdentifier,
460 IDirect3D8Impl_GetAdapterModeCount,
461 IDirect3D8Impl_EnumAdapterModes,
462 IDirect3D8Impl_GetAdapterDisplayMode,
463 IDirect3D8Impl_CheckDeviceType,
464 IDirect3D8Impl_CheckDeviceFormat,
465 IDirect3D8Impl_CheckDeviceMultiSampleType,
466 IDirect3D8Impl_CheckDepthStencilMatch,
467 IDirect3D8Impl_GetDeviceCaps,
468 IDirect3D8Impl_GetAdapterMonitor,
469 IDirect3D8Impl_CreateDevice
470 };