d3d9: Get rid of IDirect3DDevice9Impl.
[wine/multimedia.git] / dlls / d3d9 / device.c
blobc8df0fedc5ade895aec1eec8203ad3db5cfe034d
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
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"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
30 BYTE *c = (BYTE *)&format;
32 /* Don't translate FOURCC formats */
33 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
35 switch(format)
37 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
38 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
39 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
40 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
41 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
42 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
43 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
44 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
45 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
46 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
47 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
48 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
49 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
50 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
51 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
52 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
53 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
54 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
55 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
56 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
57 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
58 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
59 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
60 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
61 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
62 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
63 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
64 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
65 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
66 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
67 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
68 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
69 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
70 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
71 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
72 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
73 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
74 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
75 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
76 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
77 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
78 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
79 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
80 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
81 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
82 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
83 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
84 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
85 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
86 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
87 default:
88 FIXME("Unhandled wined3d format %#x.\n", format);
89 return D3DFMT_UNKNOWN;
93 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
95 BYTE *c = (BYTE *)&format;
97 /* Don't translate FOURCC formats */
98 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
100 switch(format)
102 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
103 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
104 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
105 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
106 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
107 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
108 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
109 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
110 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
111 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
112 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
113 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
114 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
115 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
116 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
117 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
119 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
120 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
131 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
132 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
133 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
134 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
135 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
136 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
137 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
138 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
139 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
140 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
141 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
142 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
143 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
144 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
145 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
146 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
147 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
148 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
149 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
150 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
151 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
152 default:
153 FIXME("Unhandled D3DFORMAT %#x\n", format);
154 return WINED3DFMT_UNKNOWN;
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
160 switch(primitive_type)
162 case D3DPT_POINTLIST:
163 return primitive_count;
165 case D3DPT_LINELIST:
166 return primitive_count * 2;
168 case D3DPT_LINESTRIP:
169 return primitive_count + 1;
171 case D3DPT_TRIANGLELIST:
172 return primitive_count * 3;
174 case D3DPT_TRIANGLESTRIP:
175 case D3DPT_TRIANGLEFAN:
176 return primitive_count + 2;
178 default:
179 FIXME("Unhandled primitive type %#x\n", primitive_type);
180 return 0;
184 static inline struct d3d9_device *impl_from_IDirect3DDevice9Ex(IDirect3DDevice9Ex *iface)
186 return CONTAINING_RECORD(iface, struct d3d9_device, IDirect3DDevice9Ex_iface);
189 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
191 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
193 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
194 || IsEqualGUID(riid, &IID_IUnknown))
196 IDirect3DDevice9Ex_AddRef(iface);
197 *out = iface;
198 return S_OK;
201 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
203 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
205 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
206 * It doesn't matter with which function the device was created. */
207 if (!device->d3d_parent->extended)
209 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
210 *out = NULL;
211 return E_NOINTERFACE;
214 IDirect3DDevice9Ex_AddRef(iface);
215 *out = iface;
216 return S_OK;
219 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
221 *out = NULL;
222 return E_NOINTERFACE;
225 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
227 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
228 ULONG refcount = InterlockedIncrement(&device->refcount);
230 TRACE("%p increasing refcount to %u.\n", iface, refcount);
232 return refcount;
235 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
237 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
238 ULONG refcount;
240 if (device->in_destruction)
241 return 0;
243 refcount = InterlockedDecrement(&device->refcount);
245 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
247 if (!refcount)
249 unsigned i;
250 device->in_destruction = TRUE;
252 wined3d_mutex_lock();
253 for (i = 0; i < device->fvf_decl_count; ++i)
255 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
257 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
259 wined3d_device_uninit_3d(device->wined3d_device);
260 wined3d_device_release_focus_window(device->wined3d_device);
261 wined3d_device_decref(device->wined3d_device);
262 wined3d_mutex_unlock();
264 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
266 HeapFree(GetProcessHeap(), 0, device);
269 return refcount;
272 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
274 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
276 TRACE("iface %p.\n", iface);
278 if (device->not_reset)
280 TRACE("D3D9 device is marked not reset.\n");
281 return D3DERR_DEVICENOTRESET;
284 return D3D_OK;
287 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
289 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
290 HRESULT hr;
292 TRACE("iface %p.\n", iface);
294 wined3d_mutex_lock();
295 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
296 wined3d_mutex_unlock();
298 return hr;
301 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
303 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
305 TRACE("iface %p.\n", iface);
307 wined3d_mutex_lock();
308 wined3d_device_evict_managed_resources(device->wined3d_device);
309 wined3d_mutex_unlock();
311 return D3D_OK;
314 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
316 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
318 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
320 if (!d3d9)
321 return D3DERR_INVALIDCALL;
323 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
326 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
328 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
329 WINED3DCAPS *wined3d_caps;
330 HRESULT hr;
332 TRACE("iface %p, caps %p.\n", iface, caps);
334 if (!caps)
335 return D3DERR_INVALIDCALL;
337 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
338 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
340 memset(caps, 0, sizeof(*caps));
342 wined3d_mutex_lock();
343 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
344 wined3d_mutex_unlock();
346 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
347 HeapFree(GetProcessHeap(), 0, wined3d_caps);
349 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
350 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
352 filter_caps(caps);
354 return hr;
357 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
359 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
360 HRESULT hr;
362 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
364 wined3d_mutex_lock();
365 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, (struct wined3d_display_mode *)mode);
366 wined3d_mutex_unlock();
368 if (SUCCEEDED(hr))
369 mode->Format = d3dformat_from_wined3dformat(mode->Format);
371 return hr;
374 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
375 D3DDEVICE_CREATION_PARAMETERS *parameters)
377 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
378 HRESULT hr;
380 TRACE("iface %p, parameters %p.\n", iface, parameters);
382 wined3d_mutex_lock();
383 hr = wined3d_device_get_creation_parameters(device->wined3d_device,
384 (struct wined3d_device_creation_parameters *)parameters);
385 wined3d_mutex_unlock();
387 return hr;
390 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
391 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
393 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
394 IDirect3DSurface9Impl *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
395 HRESULT hr;
397 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
398 iface, hotspot_x, hotspot_y, bitmap);
400 if (!bitmap)
402 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
403 return D3DERR_INVALIDCALL;
406 wined3d_mutex_lock();
407 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
408 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
409 wined3d_mutex_unlock();
411 return hr;
414 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
416 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
418 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
420 wined3d_mutex_lock();
421 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
422 wined3d_mutex_unlock();
425 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
427 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
428 BOOL ret;
430 TRACE("iface %p, show %#x.\n", iface, show);
432 wined3d_mutex_lock();
433 ret = wined3d_device_show_cursor(device->wined3d_device, show);
434 wined3d_mutex_unlock();
436 return ret;
439 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
440 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
442 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
443 IDirect3DSwapChain9Impl *object;
444 HRESULT hr;
446 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
447 iface, present_parameters, swapchain);
449 if (SUCCEEDED(hr = d3d9_swapchain_create(device, present_parameters, &object)))
450 *swapchain = &object->IDirect3DSwapChain9_iface;
452 return hr;
455 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
456 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
458 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
459 struct wined3d_swapchain *wined3d_swapchain = NULL;
460 IDirect3DSwapChain9Impl *swapchain_impl;
461 HRESULT hr;
463 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
465 wined3d_mutex_lock();
466 hr = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx, &wined3d_swapchain);
467 if (SUCCEEDED(hr) && wined3d_swapchain)
469 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
470 *swapchain = &swapchain_impl->IDirect3DSwapChain9_iface;
471 IDirect3DSwapChain9_AddRef(*swapchain);
472 wined3d_swapchain_decref(wined3d_swapchain);
474 else
476 *swapchain = NULL;
478 wined3d_mutex_unlock();
480 return hr;
483 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
485 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
486 UINT count;
488 TRACE("iface %p.\n", iface);
490 wined3d_mutex_lock();
491 count = wined3d_device_get_swapchain_count(device->wined3d_device);
492 wined3d_mutex_unlock();
494 return count;
497 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
499 struct wined3d_resource_desc desc;
501 wined3d_resource_get_desc(resource, &desc);
502 if (desc.pool == WINED3D_POOL_DEFAULT)
504 IDirect3DSurface9Impl *surface;
506 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
508 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
509 return D3DERR_INVALIDCALL;
512 surface = wined3d_resource_get_parent(resource);
513 if (surface->ref)
515 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
516 return D3DERR_INVALIDCALL;
519 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
522 return D3D_OK;
525 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
526 D3DPRESENT_PARAMETERS *present_parameters)
528 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
529 struct wined3d_swapchain_desc swapchain_desc;
530 HRESULT hr;
532 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
534 /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
535 * such objects can still be used for rendering after their external d3d9 object has been destroyed.
536 * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
537 * make use of the hidden reference and destroys the objects.
539 * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
540 * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
542 wined3d_mutex_lock();
544 swapchain_desc.backbuffer_width = present_parameters->BackBufferWidth;
545 swapchain_desc.backbuffer_height = present_parameters->BackBufferHeight;
546 swapchain_desc.backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
547 swapchain_desc.backbuffer_count = present_parameters->BackBufferCount;
548 swapchain_desc.multisample_type = present_parameters->MultiSampleType;
549 swapchain_desc.multisample_quality = present_parameters->MultiSampleQuality;
550 swapchain_desc.swap_effect = present_parameters->SwapEffect;
551 swapchain_desc.device_window = present_parameters->hDeviceWindow;
552 swapchain_desc.windowed = present_parameters->Windowed;
553 swapchain_desc.enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
554 swapchain_desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
555 swapchain_desc.flags = present_parameters->Flags;
556 swapchain_desc.refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
557 swapchain_desc.swap_interval = present_parameters->PresentationInterval;
558 swapchain_desc.auto_restore_display_mode = TRUE;
560 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc, reset_enum_callback);
561 if (FAILED(hr))
562 device->not_reset = TRUE;
563 else
564 device->not_reset = FALSE;
566 wined3d_mutex_unlock();
568 return hr;
571 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
572 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
574 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
575 HRESULT hr;
577 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
578 iface, src_rect, dst_rect, dst_window_override, dirty_region);
580 wined3d_mutex_lock();
581 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect, dst_window_override, dirty_region);
582 wined3d_mutex_unlock();
584 return hr;
587 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
588 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
590 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
591 struct wined3d_surface *wined3d_surface = NULL;
592 IDirect3DSurface9Impl *surface_impl;
593 HRESULT hr;
595 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
596 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
598 wined3d_mutex_lock();
599 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
600 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
601 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
603 surface_impl = wined3d_surface_get_parent(wined3d_surface);
604 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
605 IDirect3DSurface9_AddRef(*backbuffer);
606 wined3d_surface_decref(wined3d_surface);
608 wined3d_mutex_unlock();
610 return hr;
612 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
613 UINT swapchain, D3DRASTER_STATUS *raster_status)
615 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
616 HRESULT hr;
618 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
620 wined3d_mutex_lock();
621 hr = wined3d_device_get_raster_status(device->wined3d_device,
622 swapchain, (struct wined3d_raster_status *)raster_status);
623 wined3d_mutex_unlock();
625 return hr;
628 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
630 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
631 HRESULT hr;
633 TRACE("iface %p, enable %#x.\n", iface, enable);
635 wined3d_mutex_lock();
636 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
637 wined3d_mutex_unlock();
639 return hr;
642 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
643 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
645 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
647 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
649 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
650 wined3d_mutex_lock();
651 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
652 wined3d_mutex_unlock();
655 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
657 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
659 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
661 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
662 wined3d_mutex_lock();
663 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
664 wined3d_mutex_unlock();
667 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
668 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
669 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
671 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
672 struct d3d9_texture *object;
673 BOOL set_mem = FALSE;
674 HRESULT hr;
676 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
677 iface, width, height, levels, usage, format, pool, texture, shared_handle);
679 if (shared_handle)
681 if (pool == D3DPOOL_SYSTEMMEM)
683 if (levels != 1)
684 return D3DERR_INVALIDCALL;
685 set_mem = TRUE;
687 else
688 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
691 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
692 if (!object)
694 ERR("Failed to allocate texture memory.\n");
695 return D3DERR_OUTOFVIDEOMEMORY;
698 hr = texture_init(object, device, width, height, levels, usage, format, pool);
699 if (FAILED(hr))
701 WARN("Failed to initialize texture, hr %#x.\n", hr);
702 HeapFree(GetProcessHeap(), 0, object);
703 return hr;
706 if (set_mem)
708 struct wined3d_resource *resource;
709 IDirect3DSurface9Impl *surface;
711 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
712 surface = wined3d_resource_get_parent(resource);
713 wined3d_surface_set_mem(surface->wined3d_surface, *shared_handle);
716 TRACE("Created texture %p.\n", object);
717 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
719 return D3D_OK;
722 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
723 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
724 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
726 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
727 struct d3d9_texture *object;
728 HRESULT hr;
730 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
731 iface, width, height, depth, levels);
732 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
733 usage, format, pool, texture, shared_handle);
735 if (shared_handle)
736 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
738 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
739 if (!object)
741 ERR("Failed to allocate volume texture memory.\n");
742 return D3DERR_OUTOFVIDEOMEMORY;
745 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
746 if (FAILED(hr))
748 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
749 HeapFree(GetProcessHeap(), 0, object);
750 return hr;
753 TRACE("Created volume texture %p.\n", object);
754 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
756 return D3D_OK;
759 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
760 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
761 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
763 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
764 struct d3d9_texture *object;
765 HRESULT hr;
767 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
768 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
770 if (shared_handle)
771 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
773 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
774 if (!object)
776 ERR("Failed to allocate cube texture memory.\n");
777 return D3DERR_OUTOFVIDEOMEMORY;
780 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
781 if (FAILED(hr))
783 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
784 HeapFree(GetProcessHeap(), 0, object);
785 return hr;
788 TRACE("Created cube texture %p.\n", object);
789 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
791 return D3D_OK;
794 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
795 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
796 HANDLE *shared_handle)
798 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
799 IDirect3DVertexBuffer9Impl *object;
800 HRESULT hr;
802 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
803 iface, size, usage, fvf, pool, buffer, shared_handle);
805 if (shared_handle)
806 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
808 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
809 if (!object)
811 ERR("Failed to allocate buffer memory.\n");
812 return D3DERR_OUTOFVIDEOMEMORY;
815 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
816 if (FAILED(hr))
818 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
819 HeapFree(GetProcessHeap(), 0, object);
820 return hr;
823 TRACE("Created vertex buffer %p.\n", object);
824 *buffer = &object->IDirect3DVertexBuffer9_iface;
826 return D3D_OK;
829 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
830 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
831 HANDLE *shared_handle)
833 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
834 IDirect3DIndexBuffer9Impl *object;
835 HRESULT hr;
837 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
838 iface, size, usage, format, pool, buffer, shared_handle);
840 if (shared_handle)
841 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
843 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
844 if (!object)
846 ERR("Failed to allocate buffer memory.\n");
847 return D3DERR_OUTOFVIDEOMEMORY;
850 hr = indexbuffer_init(object, device, size, usage, format, pool);
851 if (FAILED(hr))
853 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
854 HeapFree(GetProcessHeap(), 0, object);
855 return hr;
858 TRACE("Created index buffer %p.\n", object);
859 *buffer = &object->IDirect3DIndexBuffer9_iface;
861 return D3D_OK;
864 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
865 D3DFORMAT format, BOOL lockable, BOOL discard, UINT level, IDirect3DSurface9 **surface,
866 UINT usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
868 IDirect3DSurface9Impl *object;
869 HRESULT hr;
871 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p.\n"
872 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
873 device, width, height, format, lockable, discard, level, surface, usage, pool,
874 multisample_type, multisample_quality);
876 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
877 if (!object)
879 FIXME("Failed to allocate surface memory.\n");
880 return D3DERR_OUTOFVIDEOMEMORY;
883 hr = surface_init(object, device, width, height, format, lockable, discard,
884 level, usage, pool, multisample_type, multisample_quality);
885 if (FAILED(hr))
887 WARN("Failed to initialize surface, hr %#x.\n", hr);
888 HeapFree(GetProcessHeap(), 0, object);
889 return hr;
892 TRACE("Created surface %p.\n", object);
893 *surface = &object->IDirect3DSurface9_iface;
895 return D3D_OK;
898 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
899 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
900 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
902 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
903 HRESULT hr;
905 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
906 "lockable %#x, surface %p, shared_handle %p.\n",
907 iface, width, height, format, multisample_type, multisample_quality,
908 lockable, surface, shared_handle);
910 if (shared_handle)
911 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
913 hr = d3d9_device_create_surface(device, width, height, format, lockable, FALSE, 0, surface,
914 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality);
916 return hr;
919 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
920 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
921 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
923 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
924 HRESULT hr;
926 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
927 "discard %#x, surface %p, shared_handle %p.\n",
928 iface, width, height, format, multisample_type, multisample_quality,
929 discard, surface, shared_handle);
931 if (shared_handle)
932 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
934 hr = d3d9_device_create_surface(device, width, height, format, TRUE, discard, 0, surface,
935 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality);
937 return hr;
941 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
942 IDirect3DSurface9 *src_surface, const RECT *src_rect,
943 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
945 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
946 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
947 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
948 HRESULT hr;
950 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
951 iface, src_surface, src_rect, dst_surface, dst_point);
953 wined3d_mutex_lock();
954 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
955 dst->wined3d_surface, dst_point);
956 wined3d_mutex_unlock();
958 return hr;
961 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
962 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
964 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
965 struct d3d9_texture *src_impl, *dst_impl;
966 HRESULT hr;
968 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
970 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
971 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
973 wined3d_mutex_lock();
974 hr = wined3d_device_update_texture(device->wined3d_device,
975 src_impl->wined3d_texture, dst_impl->wined3d_texture);
976 wined3d_mutex_unlock();
978 return hr;
981 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
982 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
984 IDirect3DSurface9Impl *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
985 IDirect3DSurface9Impl *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
986 HRESULT hr;
988 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
990 wined3d_mutex_lock();
991 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
992 wined3d_mutex_unlock();
994 return hr;
997 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
998 UINT swapchain, IDirect3DSurface9 *dst_surface)
1000 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1001 IDirect3DSurface9Impl *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1002 HRESULT hr;
1004 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1006 wined3d_mutex_lock();
1007 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1008 wined3d_mutex_unlock();
1010 return hr;
1013 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1014 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1016 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1017 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1018 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1019 HRESULT hr = D3DERR_INVALIDCALL;
1020 struct wined3d_resource_desc src_desc, dst_desc;
1021 struct wined3d_resource *wined3d_resource;
1023 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1024 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1026 wined3d_mutex_lock();
1027 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1028 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1030 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1031 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1033 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1035 if (device->in_scene)
1037 WARN("Rejecting depth / stencil blit while in scene.\n");
1038 goto done;
1041 if (src_rect)
1043 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1044 || src_rect->bottom != src_desc.height)
1046 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1047 wine_dbgstr_rect(src_rect));
1048 goto done;
1051 if (dst_rect)
1053 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1054 || dst_rect->bottom != dst_desc.height)
1056 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1057 wine_dbgstr_rect(dst_rect));
1058 goto done;
1061 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1063 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1064 goto done;
1068 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1069 if (hr == WINEDDERR_INVALIDRECT)
1070 hr = D3DERR_INVALIDCALL;
1072 done:
1073 wined3d_mutex_unlock();
1074 return hr;
1077 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1078 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1080 const struct wined3d_color c =
1082 ((color >> 16) & 0xff) / 255.0f,
1083 ((color >> 8) & 0xff) / 255.0f,
1084 (color & 0xff) / 255.0f,
1085 ((color >> 24) & 0xff) / 255.0f,
1087 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1088 IDirect3DSurface9Impl *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1089 struct wined3d_resource *wined3d_resource;
1090 struct wined3d_resource_desc desc;
1091 HRESULT hr;
1093 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1095 wined3d_mutex_lock();
1097 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1098 wined3d_resource_get_desc(wined3d_resource, &desc);
1100 /* This method is only allowed with surfaces that are render targets, or
1101 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1102 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1104 wined3d_mutex_unlock();
1105 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1106 return D3DERR_INVALIDCALL;
1109 /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1110 hr = wined3d_device_color_fill(device->wined3d_device, surface_impl->wined3d_surface, rect, &c);
1112 wined3d_mutex_unlock();
1114 return hr;
1117 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1118 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1119 HANDLE *shared_handle)
1121 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1123 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1124 iface, width, height, format, pool, surface, shared_handle);
1126 if (shared_handle)
1127 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1129 if (pool == D3DPOOL_MANAGED)
1131 WARN("Attempting to create a managed offscreen plain surface.\n");
1132 return D3DERR_INVALIDCALL;
1134 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1135 * regardless of the pool they're created in. Should we set dynamic usage
1136 * here? */
1137 return d3d9_device_create_surface(device, width, height, format, TRUE,
1138 FALSE, 0, surface, 0, pool, D3DMULTISAMPLE_NONE, 0);
1141 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1143 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1144 IDirect3DSurface9Impl *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1145 HRESULT hr;
1147 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1149 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1151 WARN("Invalid index %u specified.\n", idx);
1152 return D3DERR_INVALIDCALL;
1155 wined3d_mutex_lock();
1156 hr = wined3d_device_set_render_target(device->wined3d_device, idx,
1157 surface_impl ? surface_impl->wined3d_surface : NULL, TRUE);
1158 wined3d_mutex_unlock();
1160 return hr;
1163 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1165 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1166 struct wined3d_surface *wined3d_surface;
1167 IDirect3DSurface9Impl *surface_impl;
1168 HRESULT hr;
1170 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1172 if (!surface)
1173 return D3DERR_INVALIDCALL;
1175 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1177 WARN("Invalid index %u specified.\n", idx);
1178 return D3DERR_INVALIDCALL;
1181 wined3d_mutex_lock();
1182 hr = wined3d_device_get_render_target(device->wined3d_device, idx, &wined3d_surface);
1183 if (SUCCEEDED(hr))
1185 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1186 *surface = &surface_impl->IDirect3DSurface9_iface;
1187 IDirect3DSurface9_AddRef(*surface);
1188 wined3d_surface_decref(wined3d_surface);
1190 else
1192 if (hr != WINED3DERR_NOTFOUND)
1193 WARN("Failed to get render target %u, hr %#x.\n", idx, hr);
1194 *surface = NULL;
1196 wined3d_mutex_unlock();
1198 return hr;
1201 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1203 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1204 IDirect3DSurface9Impl *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1205 HRESULT hr;
1207 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1209 wined3d_mutex_lock();
1210 hr = wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1211 wined3d_mutex_unlock();
1213 return hr;
1216 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1218 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1219 struct wined3d_surface *wined3d_surface;
1220 IDirect3DSurface9Impl *surface_impl;
1221 HRESULT hr;
1223 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1225 if (!depth_stencil)
1226 return D3DERR_INVALIDCALL;
1228 wined3d_mutex_lock();
1229 hr = wined3d_device_get_depth_stencil(device->wined3d_device, &wined3d_surface);
1230 if (SUCCEEDED(hr))
1232 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1233 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1234 IDirect3DSurface9_AddRef(*depth_stencil);
1235 wined3d_surface_decref(wined3d_surface);
1237 else
1239 if (hr != WINED3DERR_NOTFOUND)
1240 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1241 *depth_stencil = NULL;
1243 wined3d_mutex_unlock();
1245 return hr;
1248 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1250 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1251 HRESULT hr;
1253 TRACE("iface %p.\n", iface);
1255 wined3d_mutex_lock();
1256 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1257 device->in_scene = TRUE;
1258 wined3d_mutex_unlock();
1260 return hr;
1263 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1265 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1266 HRESULT hr;
1268 TRACE("iface %p.\n", iface);
1270 wined3d_mutex_lock();
1271 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1272 device->in_scene = FALSE;
1273 wined3d_mutex_unlock();
1275 return hr;
1278 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1279 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1281 const struct wined3d_color c =
1283 ((color >> 16) & 0xff) / 255.0f,
1284 ((color >> 8) & 0xff) / 255.0f,
1285 (color & 0xff) / 255.0f,
1286 ((color >> 24) & 0xff) / 255.0f,
1288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1289 HRESULT hr;
1291 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1292 iface, rect_count, rects, flags, color, z, stencil);
1294 wined3d_mutex_lock();
1295 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1296 wined3d_mutex_unlock();
1298 return hr;
1301 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1302 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1304 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1305 HRESULT hr;
1307 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1309 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1310 wined3d_mutex_lock();
1311 hr = wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1312 wined3d_mutex_unlock();
1314 return hr;
1317 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1318 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1320 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1321 HRESULT hr;
1323 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1325 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1326 wined3d_mutex_lock();
1327 hr = wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1328 wined3d_mutex_unlock();
1330 return hr;
1333 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1334 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1336 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1337 HRESULT hr;
1339 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1341 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1342 wined3d_mutex_lock();
1343 hr = wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1344 wined3d_mutex_unlock();
1346 return hr;
1349 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1352 HRESULT hr;
1354 TRACE("iface %p, viewport %p.\n", iface, viewport);
1356 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1357 wined3d_mutex_lock();
1358 hr = wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1359 wined3d_mutex_unlock();
1361 return hr;
1364 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1366 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1367 HRESULT hr;
1369 TRACE("iface %p, viewport %p.\n", iface, viewport);
1371 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1372 wined3d_mutex_lock();
1373 hr = wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1374 wined3d_mutex_unlock();
1376 return hr;
1379 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1381 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1382 HRESULT hr;
1384 TRACE("iface %p, material %p.\n", iface, material);
1386 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1387 wined3d_mutex_lock();
1388 hr = wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1389 wined3d_mutex_unlock();
1391 return hr;
1394 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1396 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1397 HRESULT hr;
1399 TRACE("iface %p, material %p.\n", iface, material);
1401 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1402 wined3d_mutex_lock();
1403 hr = wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1404 wined3d_mutex_unlock();
1406 return hr;
1409 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1411 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1412 HRESULT hr;
1414 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1416 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1417 wined3d_mutex_lock();
1418 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1419 wined3d_mutex_unlock();
1421 return hr;
1424 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1426 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1427 HRESULT hr;
1429 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1431 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1432 wined3d_mutex_lock();
1433 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1434 wined3d_mutex_unlock();
1436 return hr;
1439 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1441 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1442 HRESULT hr;
1444 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1446 wined3d_mutex_lock();
1447 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1448 wined3d_mutex_unlock();
1450 return hr;
1453 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1455 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1456 HRESULT hr;
1458 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1460 wined3d_mutex_lock();
1461 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1462 wined3d_mutex_unlock();
1464 return hr;
1467 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1469 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1470 HRESULT hr;
1472 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1474 wined3d_mutex_lock();
1475 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1476 wined3d_mutex_unlock();
1478 return hr;
1481 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1483 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1484 HRESULT hr;
1486 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1488 wined3d_mutex_lock();
1489 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1490 wined3d_mutex_unlock();
1492 return hr;
1495 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1496 D3DRENDERSTATETYPE state, DWORD value)
1498 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1499 HRESULT hr;
1501 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1503 wined3d_mutex_lock();
1504 hr = wined3d_device_set_render_state(device->wined3d_device, state, value);
1505 wined3d_mutex_unlock();
1507 return hr;
1510 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1511 D3DRENDERSTATETYPE state, DWORD *value)
1513 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1514 HRESULT hr;
1516 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1518 wined3d_mutex_lock();
1519 hr = wined3d_device_get_render_state(device->wined3d_device, state, value);
1520 wined3d_mutex_unlock();
1522 return hr;
1525 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1526 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1528 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1529 IDirect3DStateBlock9Impl *object;
1530 HRESULT hr;
1532 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1534 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1536 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1537 return D3DERR_INVALIDCALL;
1540 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1541 if (!object)
1543 ERR("Failed to allocate stateblock memory.\n");
1544 return E_OUTOFMEMORY;
1547 hr = stateblock_init(object, device, type, NULL);
1548 if (FAILED(hr))
1550 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1551 HeapFree(GetProcessHeap(), 0, object);
1552 return hr;
1555 TRACE("Created stateblock %p.\n", object);
1556 *stateblock = &object->IDirect3DStateBlock9_iface;
1558 return D3D_OK;
1561 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1563 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1564 HRESULT hr;
1566 TRACE("iface %p.\n", iface);
1568 wined3d_mutex_lock();
1569 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1570 wined3d_mutex_unlock();
1572 return hr;
1575 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1577 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1578 struct wined3d_stateblock *wined3d_stateblock;
1579 IDirect3DStateBlock9Impl *object;
1580 HRESULT hr;
1582 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1584 wined3d_mutex_lock();
1585 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1586 wined3d_mutex_unlock();
1587 if (FAILED(hr))
1589 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1590 return hr;
1593 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1594 if (!object)
1596 ERR("Failed to allocate stateblock memory.\n");
1597 wined3d_mutex_lock();
1598 wined3d_stateblock_decref(wined3d_stateblock);
1599 wined3d_mutex_unlock();
1600 return E_OUTOFMEMORY;
1603 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1604 if (FAILED(hr))
1606 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1607 wined3d_mutex_lock();
1608 wined3d_stateblock_decref(wined3d_stateblock);
1609 wined3d_mutex_unlock();
1610 HeapFree(GetProcessHeap(), 0, object);
1611 return hr;
1614 TRACE("Created stateblock %p.\n", object);
1615 *stateblock = &object->IDirect3DStateBlock9_iface;
1617 return D3D_OK;
1620 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1622 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1623 HRESULT hr;
1625 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1627 wined3d_mutex_lock();
1628 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1629 wined3d_mutex_unlock();
1631 return hr;
1634 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1636 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1637 HRESULT hr;
1639 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1641 wined3d_mutex_lock();
1642 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1643 wined3d_mutex_unlock();
1645 return hr;
1648 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1650 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1651 struct wined3d_texture *wined3d_texture = NULL;
1652 struct d3d9_texture *texture_impl;
1653 HRESULT hr;
1655 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1657 if (!texture)
1658 return D3DERR_INVALIDCALL;
1660 wined3d_mutex_lock();
1661 hr = wined3d_device_get_texture(device->wined3d_device, stage, &wined3d_texture);
1662 if (SUCCEEDED(hr) && wined3d_texture)
1664 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1665 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1666 IDirect3DBaseTexture9_AddRef(*texture);
1667 wined3d_texture_decref(wined3d_texture);
1669 else
1671 if (FAILED(hr))
1672 WARN("Call to get texture (%u) failed (%p).\n", stage, wined3d_texture);
1673 *texture = NULL;
1675 wined3d_mutex_unlock();
1677 return hr;
1680 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1682 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1683 struct d3d9_texture *texture_impl;
1684 HRESULT hr;
1686 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1688 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1690 wined3d_mutex_lock();
1691 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1692 texture_impl ? texture_impl->wined3d_texture : NULL);
1693 wined3d_mutex_unlock();
1695 return hr;
1698 static const enum wined3d_texture_stage_state tss_lookup[] =
1700 WINED3D_TSS_INVALID, /* 0, unused */
1701 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1702 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1703 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1704 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1705 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1706 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1707 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1708 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1709 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1710 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1711 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1712 WINED3D_TSS_INVALID, /* 12, unused */
1713 WINED3D_TSS_INVALID, /* 13, unused */
1714 WINED3D_TSS_INVALID, /* 14, unused */
1715 WINED3D_TSS_INVALID, /* 15, unused */
1716 WINED3D_TSS_INVALID, /* 16, unused */
1717 WINED3D_TSS_INVALID, /* 17, unused */
1718 WINED3D_TSS_INVALID, /* 18, unused */
1719 WINED3D_TSS_INVALID, /* 19, unused */
1720 WINED3D_TSS_INVALID, /* 20, unused */
1721 WINED3D_TSS_INVALID, /* 21, unused */
1722 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1723 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1724 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1725 WINED3D_TSS_INVALID, /* 25, unused */
1726 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1727 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1728 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1729 WINED3D_TSS_INVALID, /* 29, unused */
1730 WINED3D_TSS_INVALID, /* 30, unused */
1731 WINED3D_TSS_INVALID, /* 31, unused */
1732 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1735 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1736 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1738 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1739 HRESULT hr;
1741 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1743 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1745 WARN("Invalid state %#x passed.\n", state);
1746 return D3D_OK;
1749 wined3d_mutex_lock();
1750 hr = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1751 wined3d_mutex_unlock();
1753 return hr;
1756 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1757 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1759 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1760 HRESULT hr;
1762 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
1764 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1766 WARN("Invalid state %#x passed.\n", state);
1767 return D3D_OK;
1770 wined3d_mutex_lock();
1771 hr = wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1772 wined3d_mutex_unlock();
1774 return hr;
1777 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
1778 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
1780 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1781 HRESULT hr;
1783 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
1785 wined3d_mutex_lock();
1786 hr = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state, value);
1787 wined3d_mutex_unlock();
1789 return hr;
1792 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
1793 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
1795 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1796 HRESULT hr;
1798 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
1800 wined3d_mutex_lock();
1801 hr = wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
1802 wined3d_mutex_unlock();
1804 return hr;
1807 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
1809 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1810 HRESULT hr;
1812 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1814 wined3d_mutex_lock();
1815 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1816 wined3d_mutex_unlock();
1818 return hr;
1821 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1822 UINT palette_idx, const PALETTEENTRY *entries)
1824 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1826 return D3DERR_INVALIDCALL;
1829 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1830 UINT palette_idx, PALETTEENTRY *entries)
1832 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1834 return D3DERR_INVALIDCALL;
1837 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
1839 FIXME("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1841 return D3DERR_INVALIDCALL;
1844 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
1846 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
1848 return D3DERR_INVALIDCALL;
1851 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
1853 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1854 HRESULT hr;
1856 TRACE("iface %p, rect %p.\n", iface, rect);
1858 wined3d_mutex_lock();
1859 hr = wined3d_device_set_scissor_rect(device->wined3d_device, rect);
1860 wined3d_mutex_unlock();
1862 return hr;
1865 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
1867 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1868 HRESULT hr;
1870 TRACE("iface %p, rect %p.\n", iface, rect);
1872 wined3d_mutex_lock();
1873 hr = wined3d_device_get_scissor_rect(device->wined3d_device, rect);
1874 wined3d_mutex_unlock();
1876 return hr;
1879 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
1881 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1882 HRESULT hr;
1884 TRACE("iface %p, software %#x.\n", iface, software);
1886 wined3d_mutex_lock();
1887 hr = wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
1888 wined3d_mutex_unlock();
1890 return hr;
1893 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
1895 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1896 BOOL ret;
1898 TRACE("iface %p.\n", iface);
1900 wined3d_mutex_lock();
1901 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
1902 wined3d_mutex_unlock();
1904 return ret;
1907 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
1909 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1910 HRESULT hr;
1912 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
1914 wined3d_mutex_lock();
1915 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
1916 wined3d_mutex_unlock();
1918 return hr;
1921 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
1923 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1924 float ret;
1926 TRACE("iface %p.\n", iface);
1928 wined3d_mutex_lock();
1929 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
1930 wined3d_mutex_unlock();
1932 return ret;
1935 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
1936 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1938 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1939 HRESULT hr;
1941 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1942 iface, primitive_type, start_vertex, primitive_count);
1944 wined3d_mutex_lock();
1945 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1946 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1947 vertex_count_from_primitive_count(primitive_type, primitive_count));
1948 wined3d_mutex_unlock();
1950 return hr;
1953 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
1954 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
1955 UINT vertex_count, UINT start_idx, UINT primitive_count)
1957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1958 HRESULT hr;
1960 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
1961 "vertex_count %u, start_idx %u, primitive_count %u.\n",
1962 iface, primitive_type, base_vertex_idx, min_vertex_idx,
1963 vertex_count, start_idx, primitive_count);
1965 wined3d_mutex_lock();
1966 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
1967 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1968 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1969 vertex_count_from_primitive_count(primitive_type, primitive_count));
1970 wined3d_mutex_unlock();
1972 return hr;
1975 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
1976 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
1978 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1979 HRESULT hr;
1981 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1982 iface, primitive_type, primitive_count, data, stride);
1984 wined3d_mutex_lock();
1985 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1986 hr = wined3d_device_draw_primitive_up(device->wined3d_device,
1987 vertex_count_from_primitive_count(primitive_type, primitive_count),
1988 data, stride);
1989 wined3d_mutex_unlock();
1991 return hr;
1994 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
1995 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT index_count,
1996 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
1997 const void *vertex_data, UINT vertex_stride)
1999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2000 HRESULT hr;
2002 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
2003 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2004 iface, primitive_type, min_vertex_idx, index_count, primitive_count,
2005 index_data, index_format, vertex_data, vertex_stride);
2007 wined3d_mutex_lock();
2008 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2009 hr = wined3d_device_draw_indexed_primitive_up(device->wined3d_device,
2010 vertex_count_from_primitive_count(primitive_type, primitive_count), index_data,
2011 wined3dformat_from_d3dformat(index_format), vertex_data, vertex_stride);
2012 wined3d_mutex_unlock();
2014 return hr;
2017 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2018 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2019 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2021 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2022 IDirect3DVertexBuffer9Impl *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2023 IDirect3DVertexDeclaration9Impl *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2024 HRESULT hr;
2026 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2027 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2029 wined3d_mutex_lock();
2030 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2031 dst_impl->wineD3DVertexBuffer, decl_impl ? decl_impl->wineD3DVertexDeclaration : NULL,
2032 flags, dst_impl->fvf);
2033 wined3d_mutex_unlock();
2035 return hr;
2038 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2039 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2041 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2042 IDirect3DVertexDeclaration9Impl *object;
2043 HRESULT hr;
2045 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2047 if (!declaration)
2049 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2050 return D3DERR_INVALIDCALL;
2053 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2054 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2056 return hr;
2059 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2060 IDirect3DVertexDeclaration9 *declaration)
2062 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2063 IDirect3DVertexDeclaration9Impl *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2064 HRESULT hr;
2066 TRACE("iface %p, declaration %p.\n", iface, declaration);
2068 wined3d_mutex_lock();
2069 hr = wined3d_device_set_vertex_declaration(device->wined3d_device,
2070 decl_impl ? decl_impl->wineD3DVertexDeclaration : NULL);
2071 wined3d_mutex_unlock();
2073 return hr;
2076 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2077 IDirect3DVertexDeclaration9 **declaration)
2079 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2080 struct wined3d_vertex_declaration *wined3d_declaration = NULL;
2081 IDirect3DVertexDeclaration9Impl *declaration_impl;
2082 HRESULT hr;
2084 TRACE("iface %p, declaration %p.\n", iface, declaration);
2086 if (!declaration) return D3DERR_INVALIDCALL;
2088 wined3d_mutex_lock();
2089 hr = wined3d_device_get_vertex_declaration(device->wined3d_device, &wined3d_declaration);
2090 if (SUCCEEDED(hr) && wined3d_declaration)
2092 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2093 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2094 IDirect3DVertexDeclaration9_AddRef(*declaration);
2095 wined3d_vertex_declaration_decref(wined3d_declaration);
2097 else
2099 *declaration = NULL;
2101 wined3d_mutex_unlock();
2103 TRACE("Returning %p.\n", *declaration);
2104 return hr;
2107 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2109 struct wined3d_vertex_declaration *wined3d_declaration;
2110 struct fvf_declaration *fvf_decls = device->fvf_decls;
2111 IDirect3DVertexDeclaration9Impl *d3d9_declaration;
2112 D3DVERTEXELEMENT9 *elements;
2113 int p, low, high; /* deliberately signed */
2114 HRESULT hr;
2116 TRACE("Searching for declaration for fvf %08x... ", fvf);
2118 low = 0;
2119 high = device->fvf_decl_count - 1;
2120 while (low <= high)
2122 p = (low + high) >> 1;
2123 TRACE("%d ", p);
2125 if (fvf_decls[p].fvf == fvf)
2127 TRACE("found %p.\n", fvf_decls[p].decl);
2128 return fvf_decls[p].decl;
2131 if (fvf_decls[p].fvf < fvf)
2132 low = p + 1;
2133 else
2134 high = p - 1;
2136 TRACE("not found. Creating and inserting at position %d.\n", low);
2138 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2139 return NULL;
2141 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2142 HeapFree(GetProcessHeap(), 0, elements);
2143 if (FAILED(hr))
2144 return NULL;
2146 if (device->fvf_decl_size == device->fvf_decl_count)
2148 UINT grow = max(device->fvf_decl_size / 2, 8);
2150 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2151 if (!fvf_decls)
2153 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2154 return NULL;
2156 device->fvf_decls = fvf_decls;
2157 device->fvf_decl_size += grow;
2160 d3d9_declaration->convFVF = fvf;
2161 wined3d_declaration = d3d9_declaration->wineD3DVertexDeclaration;
2162 wined3d_vertex_declaration_incref(wined3d_declaration);
2163 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2165 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2166 fvf_decls[low].decl = wined3d_declaration;
2167 fvf_decls[low].fvf = fvf;
2168 ++device->fvf_decl_count;
2170 TRACE("Returning %p. %u declatations in array.\n", wined3d_declaration, device->fvf_decl_count);
2172 return wined3d_declaration;
2175 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2177 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2178 struct wined3d_vertex_declaration *decl;
2179 HRESULT hr;
2181 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2183 if (!fvf)
2185 WARN("%#x is not a valid FVF.\n", fvf);
2186 return D3D_OK;
2189 wined3d_mutex_lock();
2190 if (!(decl = device_get_fvf_declaration(device, fvf)))
2192 wined3d_mutex_unlock();
2193 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2194 return D3DERR_DRIVERINTERNALERROR;
2197 hr = wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2198 if (FAILED(hr))
2199 ERR("Failed to set vertex declaration.\n");
2200 wined3d_mutex_unlock();
2202 return hr;
2205 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2207 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2208 struct wined3d_vertex_declaration *wined3d_declaration;
2209 IDirect3DVertexDeclaration9Impl *d3d9_declaration;
2210 HRESULT hr;
2212 TRACE("iface %p, fvf %p.\n", iface, fvf);
2214 wined3d_mutex_lock();
2215 if (FAILED(hr = wined3d_device_get_vertex_declaration(device->wined3d_device, &wined3d_declaration)))
2217 wined3d_mutex_unlock();
2218 WARN("Failed to get vertex declaration, hr %#x.\n", hr);
2219 *fvf = 0;
2220 return hr;
2223 if (wined3d_declaration)
2225 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2226 *fvf = d3d9_declaration->convFVF;
2227 wined3d_vertex_declaration_decref(wined3d_declaration);
2229 else
2231 *fvf = 0;
2233 wined3d_mutex_unlock();
2235 TRACE("Returning FVF %#x.\n", *fvf);
2237 return hr;
2240 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2241 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2243 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2244 IDirect3DVertexShader9Impl *object;
2245 HRESULT hr;
2247 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2249 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2250 if (!object)
2252 ERR("Failed to allocate vertex shader memory.\n");
2253 return E_OUTOFMEMORY;
2256 hr = vertexshader_init(object, device, byte_code);
2257 if (FAILED(hr))
2259 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2260 HeapFree(GetProcessHeap(), 0, object);
2261 return hr;
2264 TRACE("Created vertex shader %p.\n", object);
2265 *shader = &object->IDirect3DVertexShader9_iface;
2267 return D3D_OK;
2270 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2272 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2273 IDirect3DVertexShader9Impl *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2274 HRESULT hr;
2276 TRACE("iface %p, shader %p.\n", iface, shader);
2278 wined3d_mutex_lock();
2279 hr = wined3d_device_set_vertex_shader(device->wined3d_device,
2280 shader_obj ? shader_obj->wined3d_shader : NULL);
2281 wined3d_mutex_unlock();
2283 return hr;
2286 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2289 IDirect3DVertexShader9Impl *shader_impl;
2290 struct wined3d_shader *wined3d_shader;
2292 TRACE("iface %p, shader %p.\n", iface, shader);
2294 wined3d_mutex_lock();
2295 wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device);
2296 if (wined3d_shader)
2298 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2299 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2300 IDirect3DVertexShader9_AddRef(*shader);
2301 wined3d_shader_decref(wined3d_shader);
2303 else
2305 *shader = NULL;
2307 wined3d_mutex_unlock();
2309 TRACE("Returning %p.\n", *shader);
2311 return D3D_OK;
2314 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2315 UINT reg_idx, const float *data, UINT count)
2317 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2318 HRESULT hr;
2320 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2322 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2324 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2325 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2326 return D3DERR_INVALIDCALL;
2329 wined3d_mutex_lock();
2330 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2331 wined3d_mutex_unlock();
2333 return hr;
2336 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2337 UINT reg_idx, float *data, UINT count)
2339 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2340 HRESULT hr;
2342 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2344 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2346 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2347 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2348 return D3DERR_INVALIDCALL;
2351 wined3d_mutex_lock();
2352 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2353 wined3d_mutex_unlock();
2355 return hr;
2358 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2359 UINT reg_idx, const int *data, UINT count)
2361 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2362 HRESULT hr;
2364 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2366 wined3d_mutex_lock();
2367 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2368 wined3d_mutex_unlock();
2370 return hr;
2373 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2374 UINT reg_idx, int *data, UINT count)
2376 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2377 HRESULT hr;
2379 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2381 wined3d_mutex_lock();
2382 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2383 wined3d_mutex_unlock();
2385 return hr;
2388 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2389 UINT reg_idx, const BOOL *data, UINT count)
2391 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2392 HRESULT hr;
2394 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2396 wined3d_mutex_lock();
2397 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2398 wined3d_mutex_unlock();
2400 return hr;
2403 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2404 UINT reg_idx, BOOL *data, UINT count)
2406 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2407 HRESULT hr;
2409 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2411 wined3d_mutex_lock();
2412 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2413 wined3d_mutex_unlock();
2415 return hr;
2418 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2419 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2421 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2422 IDirect3DVertexBuffer9Impl *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2423 HRESULT hr;
2425 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2426 iface, stream_idx, buffer, offset, stride);
2428 wined3d_mutex_lock();
2429 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2430 buffer_impl ? buffer_impl->wineD3DVertexBuffer : NULL, offset, stride);
2431 wined3d_mutex_unlock();
2433 return hr;
2436 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2437 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2440 IDirect3DVertexBuffer9Impl *buffer_impl;
2441 struct wined3d_buffer *wined3d_buffer;
2442 HRESULT hr;
2444 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2445 iface, stream_idx, buffer, offset, stride);
2447 if (!buffer)
2448 return D3DERR_INVALIDCALL;
2450 wined3d_mutex_lock();
2451 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2452 if (SUCCEEDED(hr) && wined3d_buffer)
2454 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2455 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2456 IDirect3DVertexBuffer9_AddRef(*buffer);
2457 wined3d_buffer_decref(wined3d_buffer);
2459 else
2461 if (FAILED(hr))
2462 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2463 *buffer = NULL;
2465 wined3d_mutex_unlock();
2467 return hr;
2470 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2472 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2473 HRESULT hr;
2475 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2477 wined3d_mutex_lock();
2478 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2479 wined3d_mutex_unlock();
2481 return hr;
2484 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2486 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2487 HRESULT hr;
2489 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2491 wined3d_mutex_lock();
2492 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2493 wined3d_mutex_unlock();
2495 return hr;
2498 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2500 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2501 IDirect3DIndexBuffer9Impl *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2502 HRESULT hr;
2504 TRACE("iface %p, buffer %p.\n", iface, buffer);
2506 wined3d_mutex_lock();
2507 hr = wined3d_device_set_index_buffer(device->wined3d_device,
2508 ib ? ib->wineD3DIndexBuffer : NULL,
2509 ib ? ib->format : WINED3DFMT_UNKNOWN);
2510 wined3d_mutex_unlock();
2512 return hr;
2515 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2517 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2518 struct wined3d_buffer *retIndexData = NULL;
2519 IDirect3DIndexBuffer9Impl *buffer_impl;
2520 HRESULT hr;
2522 TRACE("iface %p, buffer %p.\n", iface, buffer);
2524 if (!buffer)
2525 return D3DERR_INVALIDCALL;
2527 wined3d_mutex_lock();
2528 hr = wined3d_device_get_index_buffer(device->wined3d_device, &retIndexData);
2529 if (SUCCEEDED(hr) && retIndexData)
2531 buffer_impl = wined3d_buffer_get_parent(retIndexData);
2532 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2533 IDirect3DIndexBuffer9_AddRef(*buffer);
2534 wined3d_buffer_decref(retIndexData);
2536 else
2538 if (FAILED(hr))
2539 FIXME("Call to GetIndices failed\n");
2540 *buffer = NULL;
2542 wined3d_mutex_unlock();
2544 return hr;
2547 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2548 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2550 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2551 IDirect3DPixelShader9Impl *object;
2552 HRESULT hr;
2554 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2556 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2557 if (!object)
2559 FIXME("Failed to allocate pixel shader memory.\n");
2560 return E_OUTOFMEMORY;
2563 hr = pixelshader_init(object, device, byte_code);
2564 if (FAILED(hr))
2566 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2567 HeapFree(GetProcessHeap(), 0, object);
2568 return hr;
2571 TRACE("Created pixel shader %p.\n", object);
2572 *shader = &object->IDirect3DPixelShader9_iface;
2574 return D3D_OK;
2577 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2579 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2580 IDirect3DPixelShader9Impl *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2581 HRESULT hr;
2583 TRACE("iface %p, shader %p.\n", iface, shader);
2585 wined3d_mutex_lock();
2586 hr = wined3d_device_set_pixel_shader(device->wined3d_device,
2587 shader_obj ? shader_obj->wined3d_shader : NULL);
2588 wined3d_mutex_unlock();
2590 return hr;
2593 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2595 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2596 IDirect3DPixelShader9Impl *shader_impl;
2597 struct wined3d_shader *wined3d_shader;
2599 TRACE("iface %p, shader %p.\n", iface, shader);
2601 if (!shader) return D3DERR_INVALIDCALL;
2603 wined3d_mutex_lock();
2604 wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device);
2605 if (wined3d_shader)
2607 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2608 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2609 IDirect3DPixelShader9_AddRef(*shader);
2610 wined3d_shader_decref(wined3d_shader);
2612 else
2614 *shader = NULL;
2616 wined3d_mutex_unlock();
2618 TRACE("Returning %p.\n", *shader);
2620 return D3D_OK;
2623 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2624 UINT reg_idx, const float *data, UINT count)
2626 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2627 HRESULT hr;
2629 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2631 wined3d_mutex_lock();
2632 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2633 wined3d_mutex_unlock();
2635 return hr;
2638 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2639 UINT reg_idx, float *data, UINT count)
2641 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2642 HRESULT hr;
2644 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2646 wined3d_mutex_lock();
2647 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2648 wined3d_mutex_unlock();
2650 return hr;
2653 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2654 UINT reg_idx, const int *data, UINT count)
2656 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2657 HRESULT hr;
2659 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2661 wined3d_mutex_lock();
2662 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2663 wined3d_mutex_unlock();
2665 return hr;
2668 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2669 UINT reg_idx, int *data, UINT count)
2671 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2672 HRESULT hr;
2674 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2676 wined3d_mutex_lock();
2677 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2678 wined3d_mutex_unlock();
2680 return hr;
2683 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2684 UINT reg_idx, const BOOL *data, UINT count)
2686 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2687 HRESULT hr;
2689 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2691 wined3d_mutex_lock();
2692 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2693 wined3d_mutex_unlock();
2695 return hr;
2698 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2699 UINT reg_idx, BOOL *data, UINT count)
2701 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2702 HRESULT hr;
2704 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2706 wined3d_mutex_lock();
2707 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2708 wined3d_mutex_unlock();
2710 return hr;
2713 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
2714 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2716 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2717 HRESULT hr;
2719 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2720 iface, handle, segment_count, patch_info);
2722 wined3d_mutex_lock();
2723 hr = wined3d_device_draw_rect_patch(device->wined3d_device, handle,
2724 segment_count, (const struct wined3d_rect_patch_info *)patch_info);
2725 wined3d_mutex_unlock();
2727 return hr;
2730 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
2731 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2733 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2734 HRESULT hr;
2736 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2737 iface, handle, segment_count, patch_info);
2739 wined3d_mutex_lock();
2740 hr = wined3d_device_draw_tri_patch(device->wined3d_device, handle,
2741 segment_count, (const struct wined3d_tri_patch_info *)patch_info);
2742 wined3d_mutex_unlock();
2744 return hr;
2747 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
2749 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2750 HRESULT hr;
2752 TRACE("iface %p, handle %#x.\n", iface, handle);
2754 wined3d_mutex_lock();
2755 hr = wined3d_device_delete_patch(device->wined3d_device, handle);
2756 wined3d_mutex_unlock();
2758 return hr;
2761 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
2763 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2764 IDirect3DQuery9Impl *object;
2765 HRESULT hr;
2767 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2769 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2770 if (!object)
2772 ERR("Failed to allocate query memory.\n");
2773 return E_OUTOFMEMORY;
2776 hr = query_init(object, device, type);
2777 if (FAILED(hr))
2779 WARN("Failed to initialize query, hr %#x.\n", hr);
2780 HeapFree(GetProcessHeap(), 0, object);
2781 return hr;
2784 TRACE("Created query %p.\n", object);
2785 if (query) *query = &object->IDirect3DQuery9_iface;
2786 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
2788 return D3D_OK;
2791 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2792 UINT width, UINT height, float *rows, float *columns)
2794 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2795 iface, width, height, rows, columns);
2797 return E_NOTIMPL;
2800 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
2801 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2802 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2804 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2805 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2806 iface, src_surface, dst_surface, src_descs, rect_count,
2807 dst_descs, operation, offset_x, offset_y);
2809 return E_NOTIMPL;
2812 static HRESULT WINAPI d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
2813 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2814 const RGNDATA *dirty_region, DWORD flags)
2816 FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2817 iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2819 return E_NOTIMPL;
2822 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2824 FIXME("iface %p, priority %p stub!\n", iface, priority);
2826 return E_NOTIMPL;
2829 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2831 FIXME("iface %p, priority %d stub!\n", iface, priority);
2833 return E_NOTIMPL;
2836 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2838 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2840 return E_NOTIMPL;
2843 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2844 IDirect3DResource9 **resources, UINT32 resource_count)
2846 FIXME("iface %p, resources %p, resource_count %u stub!\n",
2847 iface, resources, resource_count);
2849 return E_NOTIMPL;
2852 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2854 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2856 return E_NOTIMPL;
2859 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2861 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2863 *max_latency = 2;
2865 return E_NOTIMPL;
2868 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2870 static int i;
2872 TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
2874 if (!i++)
2875 FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2877 return D3D_OK;
2880 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2881 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2882 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2884 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2885 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2886 iface, width, height, format, multisample_type, multisample_quality,
2887 lockable, surface, shared_handle, usage);
2889 if (shared_handle)
2890 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2892 return E_NOTIMPL;
2895 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2896 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2897 HANDLE *shared_handle, DWORD usage)
2899 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2900 iface, width, height, format, pool, surface, shared_handle, usage);
2902 return E_NOTIMPL;
2905 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2906 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2907 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2909 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2910 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2911 iface, width, height, format, multisample_type, multisample_quality,
2912 discard, surface, shared_handle, usage);
2914 if (shared_handle)
2915 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2917 return E_NOTIMPL;
2920 static HRESULT WINAPI d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
2921 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2923 FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2925 return E_NOTIMPL;
2928 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2929 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2931 FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2933 return E_NOTIMPL;
2936 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
2938 /* IUnknown */
2939 d3d9_device_QueryInterface,
2940 d3d9_device_AddRef,
2941 d3d9_device_Release,
2942 /* IDirect3DDevice9 */
2943 d3d9_device_TestCooperativeLevel,
2944 d3d9_device_GetAvailableTextureMem,
2945 d3d9_device_EvictManagedResources,
2946 d3d9_device_GetDirect3D,
2947 d3d9_device_GetDeviceCaps,
2948 d3d9_device_GetDisplayMode,
2949 d3d9_device_GetCreationParameters,
2950 d3d9_device_SetCursorProperties,
2951 d3d9_device_SetCursorPosition,
2952 d3d9_device_ShowCursor,
2953 d3d9_device_CreateAdditionalSwapChain,
2954 d3d9_device_GetSwapChain,
2955 d3d9_device_GetNumberOfSwapChains,
2956 d3d9_device_Reset,
2957 d3d9_device_Present,
2958 d3d9_device_GetBackBuffer,
2959 d3d9_device_GetRasterStatus,
2960 d3d9_device_SetDialogBoxMode,
2961 d3d9_device_SetGammaRamp,
2962 d3d9_device_GetGammaRamp,
2963 d3d9_device_CreateTexture,
2964 d3d9_device_CreateVolumeTexture,
2965 d3d9_device_CreateCubeTexture,
2966 d3d9_device_CreateVertexBuffer,
2967 d3d9_device_CreateIndexBuffer,
2968 d3d9_device_CreateRenderTarget,
2969 d3d9_device_CreateDepthStencilSurface,
2970 d3d9_device_UpdateSurface,
2971 d3d9_device_UpdateTexture,
2972 d3d9_device_GetRenderTargetData,
2973 d3d9_device_GetFrontBufferData,
2974 d3d9_device_StretchRect,
2975 d3d9_device_ColorFill,
2976 d3d9_device_CreateOffscreenPlainSurface,
2977 d3d9_device_SetRenderTarget,
2978 d3d9_device_GetRenderTarget,
2979 d3d9_device_SetDepthStencilSurface,
2980 d3d9_device_GetDepthStencilSurface,
2981 d3d9_device_BeginScene,
2982 d3d9_device_EndScene,
2983 d3d9_device_Clear,
2984 d3d9_device_SetTransform,
2985 d3d9_device_GetTransform,
2986 d3d9_device_MultiplyTransform,
2987 d3d9_device_SetViewport,
2988 d3d9_device_GetViewport,
2989 d3d9_device_SetMaterial,
2990 d3d9_device_GetMaterial,
2991 d3d9_device_SetLight,
2992 d3d9_device_GetLight,
2993 d3d9_device_LightEnable,
2994 d3d9_device_GetLightEnable,
2995 d3d9_device_SetClipPlane,
2996 d3d9_device_GetClipPlane,
2997 d3d9_device_SetRenderState,
2998 d3d9_device_GetRenderState,
2999 d3d9_device_CreateStateBlock,
3000 d3d9_device_BeginStateBlock,
3001 d3d9_device_EndStateBlock,
3002 d3d9_device_SetClipStatus,
3003 d3d9_device_GetClipStatus,
3004 d3d9_device_GetTexture,
3005 d3d9_device_SetTexture,
3006 d3d9_device_GetTextureStageState,
3007 d3d9_device_SetTextureStageState,
3008 d3d9_device_GetSamplerState,
3009 d3d9_device_SetSamplerState,
3010 d3d9_device_ValidateDevice,
3011 d3d9_device_SetPaletteEntries,
3012 d3d9_device_GetPaletteEntries,
3013 d3d9_device_SetCurrentTexturePalette,
3014 d3d9_device_GetCurrentTexturePalette,
3015 d3d9_device_SetScissorRect,
3016 d3d9_device_GetScissorRect,
3017 d3d9_device_SetSoftwareVertexProcessing,
3018 d3d9_device_GetSoftwareVertexProcessing,
3019 d3d9_device_SetNPatchMode,
3020 d3d9_device_GetNPatchMode,
3021 d3d9_device_DrawPrimitive,
3022 d3d9_device_DrawIndexedPrimitive,
3023 d3d9_device_DrawPrimitiveUP,
3024 d3d9_device_DrawIndexedPrimitiveUP,
3025 d3d9_device_ProcessVertices,
3026 d3d9_device_CreateVertexDeclaration,
3027 d3d9_device_SetVertexDeclaration,
3028 d3d9_device_GetVertexDeclaration,
3029 d3d9_device_SetFVF,
3030 d3d9_device_GetFVF,
3031 d3d9_device_CreateVertexShader,
3032 d3d9_device_SetVertexShader,
3033 d3d9_device_GetVertexShader,
3034 d3d9_device_SetVertexShaderConstantF,
3035 d3d9_device_GetVertexShaderConstantF,
3036 d3d9_device_SetVertexShaderConstantI,
3037 d3d9_device_GetVertexShaderConstantI,
3038 d3d9_device_SetVertexShaderConstantB,
3039 d3d9_device_GetVertexShaderConstantB,
3040 d3d9_device_SetStreamSource,
3041 d3d9_device_GetStreamSource,
3042 d3d9_device_SetStreamSourceFreq,
3043 d3d9_device_GetStreamSourceFreq,
3044 d3d9_device_SetIndices,
3045 d3d9_device_GetIndices,
3046 d3d9_device_CreatePixelShader,
3047 d3d9_device_SetPixelShader,
3048 d3d9_device_GetPixelShader,
3049 d3d9_device_SetPixelShaderConstantF,
3050 d3d9_device_GetPixelShaderConstantF,
3051 d3d9_device_SetPixelShaderConstantI,
3052 d3d9_device_GetPixelShaderConstantI,
3053 d3d9_device_SetPixelShaderConstantB,
3054 d3d9_device_GetPixelShaderConstantB,
3055 d3d9_device_DrawRectPatch,
3056 d3d9_device_DrawTriPatch,
3057 d3d9_device_DeletePatch,
3058 d3d9_device_CreateQuery,
3059 /* IDirect3DDevice9Ex */
3060 d3d9_device_SetConvolutionMonoKernel,
3061 d3d9_device_ComposeRects,
3062 d3d9_device_PresentEx,
3063 d3d9_device_GetGPUThreadPriority,
3064 d3d9_device_SetGPUThreadPriority,
3065 d3d9_device_WaitForVBlank,
3066 d3d9_device_CheckResourceResidency,
3067 d3d9_device_SetMaximumFrameLatency,
3068 d3d9_device_GetMaximumFrameLatency,
3069 d3d9_device_CheckDeviceState,
3070 d3d9_device_CreateRenderTargetEx,
3071 d3d9_device_CreateOffscreenPlainSurfaceEx,
3072 d3d9_device_CreateDepthStencilSurfaceEx,
3073 d3d9_device_ResetEx,
3074 d3d9_device_GetDisplayModeEx,
3077 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3079 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3082 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3083 struct wined3d_device *device)
3085 TRACE("device_parent %p, device %p.\n", device_parent, device);
3088 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3090 TRACE("device_parent %p.\n", device_parent);
3093 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
3094 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
3095 enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
3097 struct d3d9_device *device = device_from_device_parent(device_parent);
3098 IDirect3DSurface9Impl *d3d_surface;
3099 BOOL lockable = TRUE;
3100 HRESULT hr;
3102 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
3103 "\tpool %#x, level %u, face %u, surface %p.\n",
3104 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
3106 if (pool == WINED3D_POOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
3107 lockable = FALSE;
3109 hr = d3d9_device_create_surface(device, width, height,
3110 d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
3111 (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
3112 if (FAILED(hr))
3114 WARN("Failed to create surface, hr %#x.\n", hr);
3115 return hr;
3118 *surface = d3d_surface->wined3d_surface;
3119 wined3d_surface_incref(*surface);
3121 d3d_surface->container = container_parent;
3122 IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
3123 d3d_surface->parentDevice = NULL;
3125 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3126 d3d_surface->forwardReference = container_parent;
3128 return hr;
3131 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
3132 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
3133 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, BOOL lockable,
3134 struct wined3d_surface **surface)
3136 struct d3d9_device *device = device_from_device_parent(device_parent);
3137 IDirect3DSurface9Impl *d3d_surface;
3138 HRESULT hr;
3140 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3141 "\tmultisample_quality %u, lockable %u, surface %p.\n",
3142 device_parent, container_parent, width, height, format, multisample_type,
3143 multisample_quality, lockable, surface);
3145 hr = d3d9_device_CreateRenderTarget(&device->IDirect3DDevice9Ex_iface, width, height,
3146 d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
3147 (IDirect3DSurface9 **)&d3d_surface, NULL);
3148 if (FAILED(hr))
3150 WARN("Failed to create rendertarget, hr %#x.\n", hr);
3151 return hr;
3154 *surface = d3d_surface->wined3d_surface;
3155 wined3d_surface_incref(*surface);
3157 d3d_surface->container = container_parent;
3158 /* Implicit surfaces are created with an refcount of 0 */
3159 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3161 return hr;
3164 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
3165 UINT width, UINT height, enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
3166 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
3168 struct d3d9_device *device = device_from_device_parent(device_parent);
3169 IDirect3DSurface9Impl *d3d_surface;
3170 HRESULT hr;
3172 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3173 "\tmultisample_quality %u, discard %u, surface %p.\n",
3174 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
3176 hr = d3d9_device_CreateDepthStencilSurface(&device->IDirect3DDevice9Ex_iface, width,
3177 height, d3dformat_from_wined3dformat(format), multisample_type, multisample_quality,
3178 discard, (IDirect3DSurface9 **)&d3d_surface, NULL);
3179 if (FAILED(hr))
3181 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
3182 return hr;
3185 *surface = d3d_surface->wined3d_surface;
3186 wined3d_surface_incref(*surface);
3187 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice9Ex_iface;
3188 /* Implicit surfaces are created with an refcount of 0 */
3189 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3191 return hr;
3194 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
3195 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
3196 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
3198 struct d3d9_device *device = device_from_device_parent(device_parent);
3199 IDirect3DVolume9Impl *object;
3200 HRESULT hr;
3202 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
3203 "format %#x, pool %#x, usage %#x, volume %p\n",
3204 device_parent, container_parent, width, height, depth,
3205 format, pool, usage, volume);
3207 /* Allocate the storage for the device */
3208 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3209 if (!object)
3211 FIXME("Allocation of memory failed\n");
3212 *volume = NULL;
3213 return D3DERR_OUTOFVIDEOMEMORY;
3216 hr = volume_init(object, device, width, height, depth, usage, format, pool);
3217 if (FAILED(hr))
3219 WARN("Failed to initialize volume, hr %#x.\n", hr);
3220 HeapFree(GetProcessHeap(), 0, object);
3221 return hr;
3224 *volume = object->wined3d_volume;
3225 wined3d_volume_incref(*volume);
3226 IDirect3DVolume9_Release(&object->IDirect3DVolume9_iface);
3228 object->container = container_parent;
3229 object->forwardReference = container_parent;
3231 TRACE("Created volume %p.\n", object);
3233 return hr;
3236 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3237 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3239 struct d3d9_device *device = device_from_device_parent(device_parent);
3240 D3DPRESENT_PARAMETERS local_parameters;
3241 IDirect3DSwapChain9Impl *d3d_swapchain;
3242 HRESULT hr;
3244 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3246 /* Copy the presentation parameters */
3247 local_parameters.BackBufferWidth = desc->backbuffer_width;
3248 local_parameters.BackBufferHeight = desc->backbuffer_height;
3249 local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(desc->backbuffer_format);
3250 local_parameters.BackBufferCount = desc->backbuffer_count;
3251 local_parameters.MultiSampleType = desc->multisample_type;
3252 local_parameters.MultiSampleQuality = desc->multisample_quality;
3253 local_parameters.SwapEffect = desc->swap_effect;
3254 local_parameters.hDeviceWindow = desc->device_window;
3255 local_parameters.Windowed = desc->windowed;
3256 local_parameters.EnableAutoDepthStencil = desc->enable_auto_depth_stencil;
3257 local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc->auto_depth_stencil_format);
3258 local_parameters.Flags = desc->flags;
3259 local_parameters.FullScreen_RefreshRateInHz = desc->refresh_rate;
3260 local_parameters.PresentationInterval = desc->swap_interval;
3262 hr = d3d9_swapchain_create(device, &local_parameters, &d3d_swapchain);
3263 if (FAILED(hr))
3265 WARN("Failed to create swapchain, hr %#x.\n", hr);
3266 *swapchain = NULL;
3267 return hr;
3270 *swapchain = d3d_swapchain->wined3d_swapchain;
3271 wined3d_swapchain_incref(*swapchain);
3272 IDirect3DSwapChain9_Release(&d3d_swapchain->IDirect3DSwapChain9_iface);
3274 /* Copy back the presentation parameters */
3275 desc->backbuffer_width = local_parameters.BackBufferWidth;
3276 desc->backbuffer_height = local_parameters.BackBufferHeight;
3277 desc->backbuffer_format = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3278 desc->backbuffer_count = local_parameters.BackBufferCount;
3279 desc->multisample_type = local_parameters.MultiSampleType;
3280 desc->multisample_quality = local_parameters.MultiSampleQuality;
3281 desc->swap_effect = local_parameters.SwapEffect;
3282 desc->device_window = local_parameters.hDeviceWindow;
3283 desc->windowed = local_parameters.Windowed;
3284 desc->enable_auto_depth_stencil = local_parameters.EnableAutoDepthStencil;
3285 desc->auto_depth_stencil_format = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3286 desc->flags = local_parameters.Flags;
3287 desc->refresh_rate = local_parameters.FullScreen_RefreshRateInHz;
3288 desc->swap_interval = local_parameters.PresentationInterval;
3290 return hr;
3293 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3295 device_parent_wined3d_device_created,
3296 device_parent_mode_changed,
3297 device_parent_create_surface,
3298 device_parent_create_rendertarget,
3299 device_parent_create_depth_stencil,
3300 device_parent_create_volume,
3301 device_parent_create_swapchain,
3304 static void setup_fpu(void)
3306 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3307 WORD cw;
3308 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3309 cw = (cw & ~0xf3f) | 0x3f;
3310 __asm__ volatile ("fldcw %0" : : "m" (cw));
3311 #elif defined(__i386__) && defined(_MSC_VER)
3312 WORD cw;
3313 __asm fnstcw cw;
3314 cw = (cw & ~0xf3f) | 0x3f;
3315 __asm fldcw cw;
3316 #else
3317 FIXME("FPU setup not implemented for this platform.\n");
3318 #endif
3321 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3322 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3323 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3325 struct wined3d_swapchain_desc *swapchain_desc;
3326 UINT i, count = 1;
3327 HRESULT hr;
3329 if (mode)
3330 FIXME("Ignoring display mode.\n");
3332 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3333 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3334 device->refcount = 1;
3336 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3338 wined3d_mutex_lock();
3339 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3340 &device->device_parent, &device->wined3d_device);
3341 if (FAILED(hr))
3343 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3344 wined3d_mutex_unlock();
3345 return hr;
3348 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3350 WINED3DCAPS caps;
3352 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3353 count = caps.NumberOfAdaptersInGroup;
3356 if (flags & D3DCREATE_MULTITHREADED)
3357 wined3d_device_set_multithreaded(device->wined3d_device);
3359 if (!parameters->Windowed)
3361 if (!focus_window)
3362 focus_window = parameters->hDeviceWindow;
3363 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3365 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3366 wined3d_device_decref(device->wined3d_device);
3367 wined3d_mutex_unlock();
3368 return hr;
3371 for (i = 0; i < count; ++i)
3373 HWND device_window = parameters[i].hDeviceWindow;
3375 if (!device_window) device_window = focus_window;
3376 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3377 parameters[i].BackBufferWidth,
3378 parameters[i].BackBufferHeight);
3382 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3383 if (!swapchain_desc)
3385 ERR("Failed to allocate wined3d parameters.\n");
3386 wined3d_device_decref(device->wined3d_device);
3387 wined3d_mutex_unlock();
3388 return E_OUTOFMEMORY;
3391 for (i = 0; i < count; ++i)
3393 swapchain_desc[i].backbuffer_width = parameters[i].BackBufferWidth;
3394 swapchain_desc[i].backbuffer_height = parameters[i].BackBufferHeight;
3395 swapchain_desc[i].backbuffer_format = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
3396 swapchain_desc[i].backbuffer_count = parameters[i].BackBufferCount;
3397 swapchain_desc[i].multisample_type = parameters[i].MultiSampleType;
3398 swapchain_desc[i].multisample_quality = parameters[i].MultiSampleQuality;
3399 swapchain_desc[i].swap_effect = parameters[i].SwapEffect;
3400 swapchain_desc[i].device_window = parameters[i].hDeviceWindow;
3401 swapchain_desc[i].windowed = parameters[i].Windowed;
3402 swapchain_desc[i].enable_auto_depth_stencil = parameters[i].EnableAutoDepthStencil;
3403 swapchain_desc[i].auto_depth_stencil_format =
3404 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
3405 swapchain_desc[i].flags = parameters[i].Flags;
3406 swapchain_desc[i].refresh_rate = parameters[i].FullScreen_RefreshRateInHz;
3407 swapchain_desc[i].swap_interval = parameters[i].PresentationInterval;
3408 swapchain_desc[i].auto_restore_display_mode = TRUE;
3411 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3412 if (FAILED(hr))
3414 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3415 wined3d_device_release_focus_window(device->wined3d_device);
3416 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3417 wined3d_device_decref(device->wined3d_device);
3418 wined3d_mutex_unlock();
3419 return hr;
3422 wined3d_mutex_unlock();
3424 for (i = 0; i < count; ++i)
3426 parameters[i].BackBufferWidth = swapchain_desc[i].backbuffer_width;
3427 parameters[i].BackBufferHeight = swapchain_desc[i].backbuffer_height;
3428 parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc[i].backbuffer_format);
3429 parameters[i].BackBufferCount = swapchain_desc[i].backbuffer_count;
3430 parameters[i].MultiSampleType = swapchain_desc[i].multisample_type;
3431 parameters[i].MultiSampleQuality = swapchain_desc[i].multisample_quality;
3432 parameters[i].SwapEffect = swapchain_desc[i].swap_effect;
3433 parameters[i].hDeviceWindow = swapchain_desc[i].device_window;
3434 parameters[i].Windowed = swapchain_desc[i].windowed;
3435 parameters[i].EnableAutoDepthStencil = swapchain_desc[i].enable_auto_depth_stencil;
3436 parameters[i].AutoDepthStencilFormat =
3437 d3dformat_from_wined3dformat(swapchain_desc[i].auto_depth_stencil_format);
3438 parameters[i].Flags = swapchain_desc[i].flags;
3439 parameters[i].FullScreen_RefreshRateInHz = swapchain_desc[i].refresh_rate;
3440 parameters[i].PresentationInterval = swapchain_desc[i].swap_interval;
3442 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3444 /* Initialize the converted declaration array. This creates a valid pointer
3445 * and when adding decls HeapReAlloc() can be used without further checking. */
3446 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3447 if (!device->fvf_decls)
3449 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3450 wined3d_mutex_lock();
3451 wined3d_device_uninit_3d(device->wined3d_device);
3452 wined3d_device_release_focus_window(device->wined3d_device);
3453 wined3d_device_decref(device->wined3d_device);
3454 wined3d_mutex_unlock();
3455 return E_OUTOFMEMORY;
3458 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3459 device->d3d_parent = parent;
3461 return D3D_OK;