VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d8/directx.c@ 23793

Last change on this file since 23793 was 23571, checked in by vboxsync, 15 years ago

crOpenGL: update to wine 1.1.30

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette