winmm: Fix handle leak in timer tests.
[wine/multimedia.git] / dlls / d3d8 / directx.c
blob8bcd5abf9c9fe6a91916355525f41dc8a3e774f9
1 /*
2 * IDirect3D8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
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.
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.
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
23 #include "config.h"
25 #include <stdarg.h>
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"
36 #include "d3d8_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
40 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
43 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
45 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
47 if (IsEqualGUID(riid, &IID_IUnknown)
48 || IsEqualGUID(riid, &IID_IDirect3D8)) {
49 IUnknown_AddRef(iface);
50 *ppobj = This;
51 return S_OK;
54 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
55 *ppobj = NULL;
56 return E_NOINTERFACE;
59 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
60 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
61 ULONG ref = InterlockedIncrement(&This->ref);
63 TRACE("%p increasing refcount to %u.\n", iface, ref);
65 return ref;
68 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
69 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
70 ULONG ref = InterlockedDecrement(&This->ref);
72 TRACE("%p decreasing refcount to %u.\n", iface, ref);
74 if (ref == 0) {
75 TRACE("Releasing wined3d %p\n", This->WineD3D);
77 wined3d_mutex_lock();
78 IWineD3D_Release(This->WineD3D);
79 wined3d_mutex_unlock();
81 HeapFree(GetProcessHeap(), 0, This);
84 return ref;
87 /* IDirect3D8 Interface follow: */
88 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
89 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
90 HRESULT hr;
92 TRACE("iface %p, init_function %p.\n", iface, pInitializeFunction);
94 wined3d_mutex_lock();
95 hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
96 wined3d_mutex_unlock();
98 return hr;
101 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
102 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
103 HRESULT hr;
105 TRACE("iface %p.\n", iface);
107 wined3d_mutex_lock();
108 hr = IWineD3D_GetAdapterCount(This->WineD3D);
109 wined3d_mutex_unlock();
111 return hr;
114 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface,
115 UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8 *pIdentifier)
117 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
118 WINED3DADAPTER_IDENTIFIER adapter_id;
119 HRESULT hr;
121 TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
122 iface, Adapter, Flags, pIdentifier);
124 adapter_id.driver = pIdentifier->Driver;
125 adapter_id.driver_size = sizeof(pIdentifier->Driver);
126 adapter_id.description = pIdentifier->Description;
127 adapter_id.description_size = sizeof(pIdentifier->Description);
128 adapter_id.device_name = NULL; /* d3d9 only */
129 adapter_id.device_name_size = 0; /* d3d9 only */
131 wined3d_mutex_lock();
132 hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
133 wined3d_mutex_unlock();
135 pIdentifier->DriverVersion = adapter_id.driver_version;
136 pIdentifier->VendorId = adapter_id.vendor_id;
137 pIdentifier->DeviceId = adapter_id.device_id;
138 pIdentifier->SubSysId = adapter_id.subsystem_id;
139 pIdentifier->Revision = adapter_id.revision;
140 memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
141 pIdentifier->WHQLLevel = adapter_id.whql_level;
143 return hr;
146 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
147 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
148 HRESULT hr;
150 TRACE("iface %p, adapter %u.\n", iface, Adapter);
152 wined3d_mutex_lock();
153 hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
154 wined3d_mutex_unlock();
156 return hr;
159 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
160 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
161 HRESULT hr;
163 TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
164 iface, Adapter, Mode, pMode);
166 wined3d_mutex_lock();
167 hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
168 wined3d_mutex_unlock();
170 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
172 return hr;
175 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
176 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
177 HRESULT hr;
179 TRACE("iface %p, adapter %u, mode %p.\n",
180 iface, Adapter, pMode);
182 wined3d_mutex_lock();
183 hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
184 wined3d_mutex_unlock();
186 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
188 return hr;
191 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface,
192 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
193 D3DFORMAT BackBufferFormat, BOOL Windowed) {
194 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
195 HRESULT hr;
197 TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
198 iface, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed);
200 wined3d_mutex_lock();
201 hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
202 wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
203 wined3d_mutex_unlock();
205 return hr;
208 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface,
209 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
210 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
211 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
212 HRESULT hr;
213 WINED3DRESOURCETYPE WineD3DRType;
215 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
216 iface, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
218 if(CheckFormat == D3DFMT_R8G8B8)
220 /* See comment in dlls/d3d9/directx.c, IDirect3D9Impl_CheckDeviceFormat for details */
221 WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
222 return D3DERR_NOTAVAILABLE;
226 switch(RType) {
227 case D3DRTYPE_VERTEXBUFFER:
228 case D3DRTYPE_INDEXBUFFER:
229 WineD3DRType = WINED3DRTYPE_BUFFER;
230 break;
232 default:
233 WineD3DRType = RType;
234 break;
237 wined3d_mutex_lock();
238 hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
239 Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
240 wined3d_mutex_unlock();
242 return hr;
245 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT Adapter,
246 D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType)
248 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
249 HRESULT hr;
251 TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
252 iface, Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType);
254 wined3d_mutex_lock();
255 hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
256 wined3dformat_from_d3dformat(SurfaceFormat), Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
257 wined3d_mutex_unlock();
259 return hr;
262 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8 *iface, UINT Adapter, D3DDEVTYPE DeviceType,
263 D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
265 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
266 HRESULT hr;
268 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
269 iface, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
271 wined3d_mutex_lock();
272 hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
273 wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
274 wined3dformat_from_d3dformat(DepthStencilFormat));
275 wined3d_mutex_unlock();
277 return hr;
280 void fixup_caps(WINED3DCAPS *caps)
282 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
283 if (caps->PixelShaderVersion > D3DPS_VERSION(1,4)) {
284 caps->PixelShaderVersion = D3DPS_VERSION(1,4);
286 if (caps->VertexShaderVersion > D3DVS_VERSION(1,1)) {
287 caps->VertexShaderVersion = D3DVS_VERSION(1,1);
289 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
291 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
294 static HRESULT WINAPI IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
295 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
296 HRESULT hrc = D3D_OK;
297 WINED3DCAPS *pWineCaps;
299 TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, Adapter, DeviceType, pCaps);
301 if(NULL == pCaps){
302 return D3DERR_INVALIDCALL;
304 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
305 if(pWineCaps == NULL){
306 return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
309 wined3d_mutex_lock();
310 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
311 wined3d_mutex_unlock();
313 fixup_caps(pWineCaps);
314 WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
315 HeapFree(GetProcessHeap(), 0, pWineCaps);
317 TRACE("(%p) returning %p\n", This, pCaps);
318 return hrc;
321 static HMONITOR WINAPI IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
322 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
323 HMONITOR ret;
325 TRACE("iface %p, adapter %u.\n", iface, Adapter);
327 wined3d_mutex_lock();
328 ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
329 wined3d_mutex_unlock();
331 return ret;
334 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
335 HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters, IDirect3DDevice8 **device)
337 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
338 IDirect3DDevice8Impl *object;
339 HRESULT hr;
341 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
342 iface, adapter, device_type, focus_window, flags, parameters, device);
344 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
345 if (!object)
347 ERR("Failed to allocate device memory.\n");
348 return E_OUTOFMEMORY;
351 hr = device_init(object, This->WineD3D, adapter, device_type, focus_window, flags, parameters);
352 if (FAILED(hr))
354 WARN("Failed to initialize device, hr %#x.\n", hr);
355 HeapFree(GetProcessHeap(), 0, object);
356 return hr;
359 TRACE("Created device %p.\n", object);
360 *device = (IDirect3DDevice8 *)object;
362 return D3D_OK;
365 const IDirect3D8Vtbl Direct3D8_Vtbl =
367 /* IUnknown */
368 IDirect3D8Impl_QueryInterface,
369 IDirect3D8Impl_AddRef,
370 IDirect3D8Impl_Release,
371 /* IDirect3D8 */
372 IDirect3D8Impl_RegisterSoftwareDevice,
373 IDirect3D8Impl_GetAdapterCount,
374 IDirect3D8Impl_GetAdapterIdentifier,
375 IDirect3D8Impl_GetAdapterModeCount,
376 IDirect3D8Impl_EnumAdapterModes,
377 IDirect3D8Impl_GetAdapterDisplayMode,
378 IDirect3D8Impl_CheckDeviceType,
379 IDirect3D8Impl_CheckDeviceFormat,
380 IDirect3D8Impl_CheckDeviceMultiSampleType,
381 IDirect3D8Impl_CheckDepthStencilMatch,
382 IDirect3D8Impl_GetDeviceCaps,
383 IDirect3D8Impl_GetAdapterMonitor,
384 IDirect3D8Impl_CreateDevice