wined3d: Replace the face and level parameters to device_parent_create_texture_surfac...
[wine/wine-gecko.git] / dlls / d3d9 / device.c
blobce5bf1a44b1a42a0bb776b4141f27cbaf65a705c
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 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
185 const struct wined3d_swapchain_desc *swapchain_desc)
187 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
188 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
189 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
190 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
191 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
192 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
193 present_parameters->SwapEffect = swapchain_desc->swap_effect;
194 present_parameters->hDeviceWindow = swapchain_desc->device_window;
195 present_parameters->Windowed = swapchain_desc->windowed;
196 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
197 present_parameters->AutoDepthStencilFormat
198 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
199 present_parameters->Flags = swapchain_desc->flags;
200 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
201 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
204 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
205 const D3DPRESENT_PARAMETERS *present_parameters)
207 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
208 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
209 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
210 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
211 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
212 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
213 swapchain_desc->swap_effect = present_parameters->SwapEffect;
214 swapchain_desc->device_window = present_parameters->hDeviceWindow;
215 swapchain_desc->windowed = present_parameters->Windowed;
216 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
217 swapchain_desc->auto_depth_stencil_format
218 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
219 swapchain_desc->flags = present_parameters->Flags;
220 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
221 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
222 swapchain_desc->auto_restore_display_mode = TRUE;
225 static inline struct d3d9_device *impl_from_IDirect3DDevice9Ex(IDirect3DDevice9Ex *iface)
227 return CONTAINING_RECORD(iface, struct d3d9_device, IDirect3DDevice9Ex_iface);
230 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
232 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
234 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
235 || IsEqualGUID(riid, &IID_IUnknown))
237 IDirect3DDevice9Ex_AddRef(iface);
238 *out = iface;
239 return S_OK;
242 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
244 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
246 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
247 * It doesn't matter with which function the device was created. */
248 if (!device->d3d_parent->extended)
250 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
251 *out = NULL;
252 return E_NOINTERFACE;
255 IDirect3DDevice9Ex_AddRef(iface);
256 *out = iface;
257 return S_OK;
260 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
262 *out = NULL;
263 return E_NOINTERFACE;
266 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
268 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
269 ULONG refcount = InterlockedIncrement(&device->refcount);
271 TRACE("%p increasing refcount to %u.\n", iface, refcount);
273 return refcount;
276 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
278 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
279 ULONG refcount;
281 if (device->in_destruction)
282 return 0;
284 refcount = InterlockedDecrement(&device->refcount);
286 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
288 if (!refcount)
290 unsigned i;
291 device->in_destruction = TRUE;
293 wined3d_mutex_lock();
294 for (i = 0; i < device->fvf_decl_count; ++i)
296 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
298 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
300 wined3d_device_uninit_3d(device->wined3d_device);
301 wined3d_device_release_focus_window(device->wined3d_device);
302 wined3d_device_decref(device->wined3d_device);
303 wined3d_mutex_unlock();
305 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
307 HeapFree(GetProcessHeap(), 0, device);
310 return refcount;
313 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
315 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
317 TRACE("iface %p.\n", iface);
319 if (device->not_reset)
321 TRACE("D3D9 device is marked not reset.\n");
322 return D3DERR_DEVICENOTRESET;
325 return D3D_OK;
328 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
330 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
331 HRESULT hr;
333 TRACE("iface %p.\n", iface);
335 wined3d_mutex_lock();
336 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
337 wined3d_mutex_unlock();
339 return hr;
342 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
344 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
346 TRACE("iface %p.\n", iface);
348 wined3d_mutex_lock();
349 wined3d_device_evict_managed_resources(device->wined3d_device);
350 wined3d_mutex_unlock();
352 return D3D_OK;
355 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
357 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
359 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
361 if (!d3d9)
362 return D3DERR_INVALIDCALL;
364 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
367 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
369 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
370 WINED3DCAPS *wined3d_caps;
371 HRESULT hr;
373 TRACE("iface %p, caps %p.\n", iface, caps);
375 if (!caps)
376 return D3DERR_INVALIDCALL;
378 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
379 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
381 memset(caps, 0, sizeof(*caps));
383 wined3d_mutex_lock();
384 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
385 wined3d_mutex_unlock();
387 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
388 HeapFree(GetProcessHeap(), 0, wined3d_caps);
390 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
391 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
393 filter_caps(caps);
395 return hr;
398 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
400 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
401 struct wined3d_display_mode wined3d_mode;
402 HRESULT hr;
404 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
406 wined3d_mutex_lock();
407 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
408 wined3d_mutex_unlock();
410 if (SUCCEEDED(hr))
412 mode->Width = wined3d_mode.width;
413 mode->Height = wined3d_mode.height;
414 mode->RefreshRate = wined3d_mode.refresh_rate;
415 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
418 return hr;
421 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
422 D3DDEVICE_CREATION_PARAMETERS *parameters)
424 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
426 TRACE("iface %p, parameters %p.\n", iface, parameters);
428 wined3d_mutex_lock();
429 wined3d_device_get_creation_parameters(device->wined3d_device,
430 (struct wined3d_device_creation_parameters *)parameters);
431 wined3d_mutex_unlock();
433 return D3D_OK;
436 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
437 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
440 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
441 HRESULT hr;
443 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
444 iface, hotspot_x, hotspot_y, bitmap);
446 if (!bitmap)
448 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
449 return D3DERR_INVALIDCALL;
452 wined3d_mutex_lock();
453 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
454 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
455 wined3d_mutex_unlock();
457 return hr;
460 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
462 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
464 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
466 wined3d_mutex_lock();
467 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
468 wined3d_mutex_unlock();
471 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
473 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
474 BOOL ret;
476 TRACE("iface %p, show %#x.\n", iface, show);
478 wined3d_mutex_lock();
479 ret = wined3d_device_show_cursor(device->wined3d_device, show);
480 wined3d_mutex_unlock();
482 return ret;
485 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
486 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
488 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
489 struct wined3d_swapchain_desc desc;
490 struct d3d9_swapchain *object;
491 HRESULT hr;
493 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
494 iface, present_parameters, swapchain);
496 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
497 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
498 *swapchain = &object->IDirect3DSwapChain9_iface;
499 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
501 return hr;
504 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
505 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
507 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
508 struct wined3d_swapchain *wined3d_swapchain;
509 struct d3d9_swapchain *swapchain_impl;
510 HRESULT hr;
512 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
514 wined3d_mutex_lock();
515 if ((wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx)))
517 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
518 *swapchain = &swapchain_impl->IDirect3DSwapChain9_iface;
519 IDirect3DSwapChain9_AddRef(*swapchain);
520 hr = D3D_OK;
522 else
524 *swapchain = NULL;
525 hr = D3DERR_INVALIDCALL;
527 wined3d_mutex_unlock();
529 return hr;
532 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
534 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
535 UINT count;
537 TRACE("iface %p.\n", iface);
539 wined3d_mutex_lock();
540 count = wined3d_device_get_swapchain_count(device->wined3d_device);
541 wined3d_mutex_unlock();
543 return count;
546 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
548 struct wined3d_resource_desc desc;
550 wined3d_resource_get_desc(resource, &desc);
551 if (desc.pool == WINED3D_POOL_DEFAULT)
553 struct d3d9_surface *surface;
555 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
557 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
558 return D3DERR_INVALIDCALL;
561 surface = wined3d_resource_get_parent(resource);
562 if (surface->refcount)
564 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
565 return D3DERR_INVALIDCALL;
568 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
571 return D3D_OK;
574 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
575 D3DPRESENT_PARAMETERS *present_parameters)
577 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
578 struct wined3d_swapchain_desc swapchain_desc;
579 HRESULT hr;
581 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
583 wined3d_mutex_lock();
584 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
585 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc, NULL, reset_enum_callback);
586 if (FAILED(hr))
587 device->not_reset = TRUE;
588 else
589 device->not_reset = FALSE;
590 wined3d_mutex_unlock();
592 return hr;
595 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
596 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
598 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
599 HRESULT hr;
601 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
602 iface, src_rect, dst_rect, dst_window_override, dirty_region);
604 wined3d_mutex_lock();
605 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
606 dst_window_override, dirty_region, 0);
607 wined3d_mutex_unlock();
609 return hr;
612 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
613 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
615 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
616 struct wined3d_surface *wined3d_surface = NULL;
617 struct d3d9_surface *surface_impl;
618 HRESULT hr;
620 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
621 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
623 wined3d_mutex_lock();
624 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
625 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
626 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
628 surface_impl = wined3d_surface_get_parent(wined3d_surface);
629 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
630 IDirect3DSurface9_AddRef(*backbuffer);
632 wined3d_mutex_unlock();
634 return hr;
636 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
637 UINT swapchain, D3DRASTER_STATUS *raster_status)
639 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
640 HRESULT hr;
642 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
644 wined3d_mutex_lock();
645 hr = wined3d_device_get_raster_status(device->wined3d_device,
646 swapchain, (struct wined3d_raster_status *)raster_status);
647 wined3d_mutex_unlock();
649 return hr;
652 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
654 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
655 HRESULT hr;
657 TRACE("iface %p, enable %#x.\n", iface, enable);
659 wined3d_mutex_lock();
660 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
661 wined3d_mutex_unlock();
663 return hr;
666 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
667 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
669 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
671 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
673 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
674 wined3d_mutex_lock();
675 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
676 wined3d_mutex_unlock();
679 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
681 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
683 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
685 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
686 wined3d_mutex_lock();
687 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
688 wined3d_mutex_unlock();
691 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
692 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
693 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
695 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
696 struct d3d9_texture *object;
697 BOOL set_mem = FALSE;
698 HRESULT hr;
700 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
701 iface, width, height, levels, usage, format, pool, texture, shared_handle);
703 if (shared_handle)
705 if (pool == D3DPOOL_SYSTEMMEM)
707 if (levels != 1)
708 return D3DERR_INVALIDCALL;
709 set_mem = TRUE;
711 else
712 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
715 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
716 if (!object)
718 ERR("Failed to allocate texture memory.\n");
719 return D3DERR_OUTOFVIDEOMEMORY;
722 hr = texture_init(object, device, width, height, levels, usage, format, pool);
723 if (FAILED(hr))
725 WARN("Failed to initialize texture, hr %#x.\n", hr);
726 HeapFree(GetProcessHeap(), 0, object);
727 return hr;
730 if (set_mem)
732 struct wined3d_resource *resource;
733 struct d3d9_surface *surface;
735 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
736 surface = wined3d_resource_get_parent(resource);
737 wined3d_surface_set_mem(surface->wined3d_surface, *shared_handle);
740 TRACE("Created texture %p.\n", object);
741 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
743 return D3D_OK;
746 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
747 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
748 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
750 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
751 struct d3d9_texture *object;
752 HRESULT hr;
754 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
755 iface, width, height, depth, levels);
756 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
757 usage, format, pool, texture, shared_handle);
759 if (shared_handle)
760 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
762 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
763 if (!object)
765 ERR("Failed to allocate volume texture memory.\n");
766 return D3DERR_OUTOFVIDEOMEMORY;
769 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
770 if (FAILED(hr))
772 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
773 HeapFree(GetProcessHeap(), 0, object);
774 return hr;
777 TRACE("Created volume texture %p.\n", object);
778 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
780 return D3D_OK;
783 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
784 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
785 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
787 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
788 struct d3d9_texture *object;
789 HRESULT hr;
791 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
792 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
794 if (shared_handle)
795 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
797 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
798 if (!object)
800 ERR("Failed to allocate cube texture memory.\n");
801 return D3DERR_OUTOFVIDEOMEMORY;
804 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
805 if (FAILED(hr))
807 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
808 HeapFree(GetProcessHeap(), 0, object);
809 return hr;
812 TRACE("Created cube texture %p.\n", object);
813 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
815 return D3D_OK;
818 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
819 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
820 HANDLE *shared_handle)
822 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
823 struct d3d9_vertexbuffer *object;
824 HRESULT hr;
826 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
827 iface, size, usage, fvf, pool, buffer, shared_handle);
829 if (shared_handle)
830 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
832 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
833 if (!object)
835 ERR("Failed to allocate buffer memory.\n");
836 return D3DERR_OUTOFVIDEOMEMORY;
839 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
840 if (FAILED(hr))
842 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
843 HeapFree(GetProcessHeap(), 0, object);
844 return hr;
847 TRACE("Created vertex buffer %p.\n", object);
848 *buffer = &object->IDirect3DVertexBuffer9_iface;
850 return D3D_OK;
853 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
854 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
855 HANDLE *shared_handle)
857 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
858 struct d3d9_indexbuffer *object;
859 HRESULT hr;
861 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
862 iface, size, usage, format, pool, buffer, shared_handle);
864 if (shared_handle)
865 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
867 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
868 if (!object)
870 ERR("Failed to allocate buffer memory.\n");
871 return D3DERR_OUTOFVIDEOMEMORY;
874 hr = indexbuffer_init(object, device, size, usage, format, pool);
875 if (FAILED(hr))
877 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
878 HeapFree(GetProcessHeap(), 0, object);
879 return hr;
882 TRACE("Created index buffer %p.\n", object);
883 *buffer = &object->IDirect3DIndexBuffer9_iface;
885 return D3D_OK;
888 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
889 D3DFORMAT format, BOOL lockable, BOOL discard, IDirect3DSurface9 **surface, UINT usage,
890 D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
892 struct d3d9_surface *object;
893 HRESULT hr;
895 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, surface %p.\n"
896 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
897 device, width, height, format, lockable, discard, surface, usage, pool,
898 multisample_type, multisample_quality);
900 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
902 FIXME("Failed to allocate surface memory.\n");
903 return D3DERR_OUTOFVIDEOMEMORY;
906 if (FAILED(hr = surface_init(object, device, width, height, format, lockable,
907 discard, usage, pool, multisample_type, multisample_quality)))
909 WARN("Failed to initialize surface, hr %#x.\n", hr);
910 HeapFree(GetProcessHeap(), 0, object);
911 return hr;
914 TRACE("Created surface %p.\n", object);
915 *surface = &object->IDirect3DSurface9_iface;
917 return D3D_OK;
920 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
921 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
922 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
924 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
926 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
927 "lockable %#x, surface %p, shared_handle %p.\n",
928 iface, width, height, format, multisample_type, multisample_quality,
929 lockable, surface, shared_handle);
931 if (shared_handle)
932 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
934 return d3d9_device_create_surface(device, width, height, format, lockable, FALSE, surface,
935 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality);
938 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
939 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
940 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
944 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
945 "discard %#x, surface %p, shared_handle %p.\n",
946 iface, width, height, format, multisample_type, multisample_quality,
947 discard, surface, shared_handle);
949 if (shared_handle)
950 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
952 return d3d9_device_create_surface(device, width, height, format, TRUE, discard, surface,
953 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality);
957 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
958 IDirect3DSurface9 *src_surface, const RECT *src_rect,
959 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
961 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
962 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
963 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
964 HRESULT hr;
966 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
967 iface, src_surface, src_rect, dst_surface, dst_point);
969 wined3d_mutex_lock();
970 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
971 dst->wined3d_surface, dst_point);
972 wined3d_mutex_unlock();
974 return hr;
977 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
978 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
980 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
981 struct d3d9_texture *src_impl, *dst_impl;
982 HRESULT hr;
984 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
986 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
987 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
989 wined3d_mutex_lock();
990 hr = wined3d_device_update_texture(device->wined3d_device,
991 src_impl->wined3d_texture, dst_impl->wined3d_texture);
992 wined3d_mutex_unlock();
994 return hr;
997 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
998 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1000 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1001 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1002 HRESULT hr;
1004 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1006 wined3d_mutex_lock();
1007 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1008 wined3d_mutex_unlock();
1010 return hr;
1013 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1014 UINT swapchain, IDirect3DSurface9 *dst_surface)
1016 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1017 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1018 HRESULT hr;
1020 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1022 wined3d_mutex_lock();
1023 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1024 wined3d_mutex_unlock();
1026 return hr;
1029 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1030 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1032 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1033 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1034 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1035 HRESULT hr = D3DERR_INVALIDCALL;
1036 struct wined3d_resource_desc src_desc, dst_desc;
1037 struct wined3d_resource *wined3d_resource;
1039 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1040 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1042 wined3d_mutex_lock();
1043 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1044 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1046 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1047 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1049 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1051 if (device->in_scene)
1053 WARN("Rejecting depth / stencil blit while in scene.\n");
1054 goto done;
1057 if (src_rect)
1059 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1060 || src_rect->bottom != src_desc.height)
1062 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1063 wine_dbgstr_rect(src_rect));
1064 goto done;
1067 if (dst_rect)
1069 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1070 || dst_rect->bottom != dst_desc.height)
1072 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1073 wine_dbgstr_rect(dst_rect));
1074 goto done;
1077 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1079 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1080 goto done;
1084 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1085 if (hr == WINEDDERR_INVALIDRECT)
1086 hr = D3DERR_INVALIDCALL;
1088 done:
1089 wined3d_mutex_unlock();
1090 return hr;
1093 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1094 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1096 const struct wined3d_color c =
1098 ((color >> 16) & 0xff) / 255.0f,
1099 ((color >> 8) & 0xff) / 255.0f,
1100 (color & 0xff) / 255.0f,
1101 ((color >> 24) & 0xff) / 255.0f,
1103 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1104 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1105 struct wined3d_resource *wined3d_resource;
1106 struct wined3d_resource_desc desc;
1107 HRESULT hr;
1109 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1111 wined3d_mutex_lock();
1113 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1114 wined3d_resource_get_desc(wined3d_resource, &desc);
1116 /* This method is only allowed with surfaces that are render targets, or
1117 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1118 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1120 wined3d_mutex_unlock();
1121 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1122 return D3DERR_INVALIDCALL;
1125 /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1126 hr = wined3d_device_color_fill(device->wined3d_device, surface_impl->wined3d_surface, rect, &c);
1128 wined3d_mutex_unlock();
1130 return hr;
1133 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1134 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1135 HANDLE *shared_handle)
1137 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1139 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1140 iface, width, height, format, pool, surface, shared_handle);
1142 if (shared_handle)
1143 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1145 if (pool == D3DPOOL_MANAGED)
1147 WARN("Attempting to create a managed offscreen plain surface.\n");
1148 return D3DERR_INVALIDCALL;
1150 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1151 * regardless of the pool they're created in. Should we set dynamic usage
1152 * here? */
1153 return d3d9_device_create_surface(device, width, height, format, TRUE,
1154 FALSE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0);
1157 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1159 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1160 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1161 HRESULT hr;
1163 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1165 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1167 WARN("Invalid index %u specified.\n", idx);
1168 return D3DERR_INVALIDCALL;
1171 wined3d_mutex_lock();
1172 hr = wined3d_device_set_render_target(device->wined3d_device, idx,
1173 surface_impl ? surface_impl->wined3d_surface : NULL, TRUE);
1174 wined3d_mutex_unlock();
1176 return hr;
1179 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1181 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1182 struct wined3d_surface *wined3d_surface;
1183 struct d3d9_surface *surface_impl;
1184 HRESULT hr = D3D_OK;
1186 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1188 if (!surface)
1189 return D3DERR_INVALIDCALL;
1191 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1193 WARN("Invalid index %u specified.\n", idx);
1194 return D3DERR_INVALIDCALL;
1197 wined3d_mutex_lock();
1198 if ((wined3d_surface = wined3d_device_get_render_target(device->wined3d_device, idx)))
1200 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1201 *surface = &surface_impl->IDirect3DSurface9_iface;
1202 IDirect3DSurface9_AddRef(*surface);
1204 else
1206 hr = WINED3DERR_NOTFOUND;
1207 *surface = NULL;
1209 wined3d_mutex_unlock();
1211 return hr;
1214 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1216 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1217 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1219 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1221 wined3d_mutex_lock();
1222 wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1223 wined3d_mutex_unlock();
1225 return D3D_OK;
1228 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1230 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1231 struct wined3d_surface *wined3d_surface;
1232 struct d3d9_surface *surface_impl;
1233 HRESULT hr = D3D_OK;
1235 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1237 if (!depth_stencil)
1238 return D3DERR_INVALIDCALL;
1240 wined3d_mutex_lock();
1241 if ((wined3d_surface = wined3d_device_get_depth_stencil(device->wined3d_device)))
1243 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1244 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1245 IDirect3DSurface9_AddRef(*depth_stencil);
1247 else
1249 hr = WINED3DERR_NOTFOUND;
1250 *depth_stencil = NULL;
1252 wined3d_mutex_unlock();
1254 return hr;
1257 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1259 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1260 HRESULT hr;
1262 TRACE("iface %p.\n", iface);
1264 wined3d_mutex_lock();
1265 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1266 device->in_scene = TRUE;
1267 wined3d_mutex_unlock();
1269 return hr;
1272 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1274 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1275 HRESULT hr;
1277 TRACE("iface %p.\n", iface);
1279 wined3d_mutex_lock();
1280 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1281 device->in_scene = FALSE;
1282 wined3d_mutex_unlock();
1284 return hr;
1287 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1288 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1290 const struct wined3d_color c =
1292 ((color >> 16) & 0xff) / 255.0f,
1293 ((color >> 8) & 0xff) / 255.0f,
1294 (color & 0xff) / 255.0f,
1295 ((color >> 24) & 0xff) / 255.0f,
1297 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1298 HRESULT hr;
1300 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1301 iface, rect_count, rects, flags, color, z, stencil);
1303 wined3d_mutex_lock();
1304 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1305 wined3d_mutex_unlock();
1307 return hr;
1310 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1311 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1313 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1315 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1317 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1318 wined3d_mutex_lock();
1319 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1320 wined3d_mutex_unlock();
1322 return D3D_OK;
1325 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1326 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1328 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1330 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1332 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1333 wined3d_mutex_lock();
1334 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1335 wined3d_mutex_unlock();
1337 return D3D_OK;
1340 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1341 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1343 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1345 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1347 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1348 wined3d_mutex_lock();
1349 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1350 wined3d_mutex_unlock();
1352 return D3D_OK;
1355 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1357 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1359 TRACE("iface %p, viewport %p.\n", iface, viewport);
1361 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1362 wined3d_mutex_lock();
1363 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1364 wined3d_mutex_unlock();
1366 return D3D_OK;
1369 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1371 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1373 TRACE("iface %p, viewport %p.\n", iface, viewport);
1375 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1376 wined3d_mutex_lock();
1377 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1378 wined3d_mutex_unlock();
1380 return D3D_OK;
1383 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1385 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1387 TRACE("iface %p, material %p.\n", iface, material);
1389 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1390 wined3d_mutex_lock();
1391 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1392 wined3d_mutex_unlock();
1394 return D3D_OK;
1397 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1399 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1401 TRACE("iface %p, material %p.\n", iface, material);
1403 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1404 wined3d_mutex_lock();
1405 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1406 wined3d_mutex_unlock();
1408 return D3D_OK;
1411 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1413 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1414 HRESULT hr;
1416 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1418 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1419 wined3d_mutex_lock();
1420 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1421 wined3d_mutex_unlock();
1423 return hr;
1426 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1428 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1429 HRESULT hr;
1431 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1433 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1434 wined3d_mutex_lock();
1435 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1436 wined3d_mutex_unlock();
1438 return hr;
1441 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1443 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1444 HRESULT hr;
1446 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1448 wined3d_mutex_lock();
1449 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1450 wined3d_mutex_unlock();
1452 return hr;
1455 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1457 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1458 HRESULT hr;
1460 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1462 wined3d_mutex_lock();
1463 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1464 wined3d_mutex_unlock();
1466 return hr;
1469 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1471 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1472 HRESULT hr;
1474 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1476 wined3d_mutex_lock();
1477 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1478 wined3d_mutex_unlock();
1480 return hr;
1483 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1485 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1486 HRESULT hr;
1488 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1490 wined3d_mutex_lock();
1491 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1492 wined3d_mutex_unlock();
1494 return hr;
1497 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1498 D3DRENDERSTATETYPE state, DWORD value)
1500 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1502 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1504 wined3d_mutex_lock();
1505 wined3d_device_set_render_state(device->wined3d_device, state, value);
1506 wined3d_mutex_unlock();
1508 return D3D_OK;
1511 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1512 D3DRENDERSTATETYPE state, DWORD *value)
1514 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1516 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1518 wined3d_mutex_lock();
1519 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1520 wined3d_mutex_unlock();
1522 return D3D_OK;
1525 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1526 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1528 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1529 struct d3d9_stateblock *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 struct d3d9_stateblock *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;
1654 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1656 if (!texture)
1657 return D3DERR_INVALIDCALL;
1659 wined3d_mutex_lock();
1660 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1662 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1663 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1664 IDirect3DBaseTexture9_AddRef(*texture);
1666 else
1668 *texture = NULL;
1670 wined3d_mutex_unlock();
1672 return D3D_OK;
1675 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1677 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1678 struct d3d9_texture *texture_impl;
1679 HRESULT hr;
1681 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1683 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1685 wined3d_mutex_lock();
1686 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1687 texture_impl ? texture_impl->wined3d_texture : NULL);
1688 wined3d_mutex_unlock();
1690 return hr;
1693 static const enum wined3d_texture_stage_state tss_lookup[] =
1695 WINED3D_TSS_INVALID, /* 0, unused */
1696 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1697 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1698 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1699 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1700 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1701 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1702 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1703 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1704 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1705 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1706 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1707 WINED3D_TSS_INVALID, /* 12, unused */
1708 WINED3D_TSS_INVALID, /* 13, unused */
1709 WINED3D_TSS_INVALID, /* 14, unused */
1710 WINED3D_TSS_INVALID, /* 15, unused */
1711 WINED3D_TSS_INVALID, /* 16, unused */
1712 WINED3D_TSS_INVALID, /* 17, unused */
1713 WINED3D_TSS_INVALID, /* 18, unused */
1714 WINED3D_TSS_INVALID, /* 19, unused */
1715 WINED3D_TSS_INVALID, /* 20, unused */
1716 WINED3D_TSS_INVALID, /* 21, unused */
1717 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1718 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1719 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1720 WINED3D_TSS_INVALID, /* 25, unused */
1721 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1722 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1723 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1724 WINED3D_TSS_INVALID, /* 29, unused */
1725 WINED3D_TSS_INVALID, /* 30, unused */
1726 WINED3D_TSS_INVALID, /* 31, unused */
1727 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1730 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1731 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1733 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1735 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1737 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1739 WARN("Invalid state %#x passed.\n", state);
1740 return D3D_OK;
1743 wined3d_mutex_lock();
1744 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
1745 wined3d_mutex_unlock();
1747 return D3D_OK;
1750 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1751 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1755 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
1757 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1759 WARN("Invalid state %#x passed.\n", state);
1760 return D3D_OK;
1763 wined3d_mutex_lock();
1764 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1765 wined3d_mutex_unlock();
1767 return D3D_OK;
1770 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
1771 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
1773 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1775 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
1777 wined3d_mutex_lock();
1778 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
1779 wined3d_mutex_unlock();
1781 return D3D_OK;
1784 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
1785 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
1787 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1789 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
1791 wined3d_mutex_lock();
1792 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
1793 wined3d_mutex_unlock();
1795 return D3D_OK;
1798 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
1800 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1801 HRESULT hr;
1803 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1805 wined3d_mutex_lock();
1806 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1807 wined3d_mutex_unlock();
1809 return hr;
1812 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1813 UINT palette_idx, const PALETTEENTRY *entries)
1815 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1817 return D3DERR_INVALIDCALL;
1820 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1821 UINT palette_idx, PALETTEENTRY *entries)
1823 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1825 return D3DERR_INVALIDCALL;
1828 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
1830 FIXME("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1832 return D3DERR_INVALIDCALL;
1835 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
1837 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
1839 return D3DERR_INVALIDCALL;
1842 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
1844 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1846 TRACE("iface %p, rect %p.\n", iface, rect);
1848 wined3d_mutex_lock();
1849 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
1850 wined3d_mutex_unlock();
1852 return D3D_OK;
1855 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
1857 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1859 TRACE("iface %p, rect %p.\n", iface, rect);
1861 wined3d_mutex_lock();
1862 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
1863 wined3d_mutex_unlock();
1865 return D3D_OK;
1868 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
1870 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1872 TRACE("iface %p, software %#x.\n", iface, software);
1874 wined3d_mutex_lock();
1875 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
1876 wined3d_mutex_unlock();
1878 return D3D_OK;
1881 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
1883 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1884 BOOL ret;
1886 TRACE("iface %p.\n", iface);
1888 wined3d_mutex_lock();
1889 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
1890 wined3d_mutex_unlock();
1892 return ret;
1895 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
1897 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1898 HRESULT hr;
1900 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
1902 wined3d_mutex_lock();
1903 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
1904 wined3d_mutex_unlock();
1906 return hr;
1909 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
1911 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1912 float ret;
1914 TRACE("iface %p.\n", iface);
1916 wined3d_mutex_lock();
1917 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
1918 wined3d_mutex_unlock();
1920 return ret;
1923 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
1924 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1926 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1927 HRESULT hr;
1929 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1930 iface, primitive_type, start_vertex, primitive_count);
1932 wined3d_mutex_lock();
1933 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1934 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1935 vertex_count_from_primitive_count(primitive_type, primitive_count));
1936 wined3d_mutex_unlock();
1938 return hr;
1941 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
1942 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
1943 UINT vertex_count, UINT start_idx, UINT primitive_count)
1945 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1946 HRESULT hr;
1948 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
1949 "vertex_count %u, start_idx %u, primitive_count %u.\n",
1950 iface, primitive_type, base_vertex_idx, min_vertex_idx,
1951 vertex_count, start_idx, primitive_count);
1953 wined3d_mutex_lock();
1954 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
1955 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1956 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1957 vertex_count_from_primitive_count(primitive_type, primitive_count));
1958 wined3d_mutex_unlock();
1960 return hr;
1963 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
1964 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
1966 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1967 HRESULT hr;
1969 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1970 iface, primitive_type, primitive_count, data, stride);
1972 wined3d_mutex_lock();
1973 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1974 hr = wined3d_device_draw_primitive_up(device->wined3d_device,
1975 vertex_count_from_primitive_count(primitive_type, primitive_count),
1976 data, stride);
1977 wined3d_mutex_unlock();
1979 return hr;
1982 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
1983 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT index_count,
1984 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
1985 const void *vertex_data, UINT vertex_stride)
1987 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1988 HRESULT hr;
1990 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1991 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1992 iface, primitive_type, min_vertex_idx, index_count, primitive_count,
1993 index_data, index_format, vertex_data, vertex_stride);
1995 wined3d_mutex_lock();
1996 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1997 hr = wined3d_device_draw_indexed_primitive_up(device->wined3d_device,
1998 vertex_count_from_primitive_count(primitive_type, primitive_count), index_data,
1999 wined3dformat_from_d3dformat(index_format), vertex_data, vertex_stride);
2000 wined3d_mutex_unlock();
2002 return hr;
2005 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2006 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2007 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2009 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2010 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2011 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2012 HRESULT hr;
2014 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2015 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2017 wined3d_mutex_lock();
2018 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2019 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2020 flags, dst_impl->fvf);
2021 wined3d_mutex_unlock();
2023 return hr;
2026 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2027 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2029 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2030 struct d3d9_vertex_declaration *object;
2031 HRESULT hr;
2033 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2035 if (!declaration)
2037 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2038 return D3DERR_INVALIDCALL;
2041 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2042 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2044 return hr;
2047 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2048 IDirect3DVertexDeclaration9 *declaration)
2050 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2051 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2053 TRACE("iface %p, declaration %p.\n", iface, declaration);
2055 wined3d_mutex_lock();
2056 wined3d_device_set_vertex_declaration(device->wined3d_device,
2057 decl_impl ? decl_impl->wined3d_declaration : NULL);
2058 wined3d_mutex_unlock();
2060 return D3D_OK;
2063 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2064 IDirect3DVertexDeclaration9 **declaration)
2066 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2067 struct wined3d_vertex_declaration *wined3d_declaration;
2068 struct d3d9_vertex_declaration *declaration_impl;
2070 TRACE("iface %p, declaration %p.\n", iface, declaration);
2072 if (!declaration) return D3DERR_INVALIDCALL;
2074 wined3d_mutex_lock();
2075 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2077 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2078 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2079 IDirect3DVertexDeclaration9_AddRef(*declaration);
2081 else
2083 *declaration = NULL;
2085 wined3d_mutex_unlock();
2087 TRACE("Returning %p.\n", *declaration);
2088 return D3D_OK;
2091 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2093 struct wined3d_vertex_declaration *wined3d_declaration;
2094 struct fvf_declaration *fvf_decls = device->fvf_decls;
2095 struct d3d9_vertex_declaration *d3d9_declaration;
2096 D3DVERTEXELEMENT9 *elements;
2097 int p, low, high; /* deliberately signed */
2098 HRESULT hr;
2100 TRACE("Searching for declaration for fvf %08x... ", fvf);
2102 low = 0;
2103 high = device->fvf_decl_count - 1;
2104 while (low <= high)
2106 p = (low + high) >> 1;
2107 TRACE("%d ", p);
2109 if (fvf_decls[p].fvf == fvf)
2111 TRACE("found %p.\n", fvf_decls[p].decl);
2112 return fvf_decls[p].decl;
2115 if (fvf_decls[p].fvf < fvf)
2116 low = p + 1;
2117 else
2118 high = p - 1;
2120 TRACE("not found. Creating and inserting at position %d.\n", low);
2122 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2123 return NULL;
2125 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2126 HeapFree(GetProcessHeap(), 0, elements);
2127 if (FAILED(hr))
2128 return NULL;
2130 if (device->fvf_decl_size == device->fvf_decl_count)
2132 UINT grow = max(device->fvf_decl_size / 2, 8);
2134 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2135 if (!fvf_decls)
2137 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2138 return NULL;
2140 device->fvf_decls = fvf_decls;
2141 device->fvf_decl_size += grow;
2144 d3d9_declaration->fvf = fvf;
2145 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2146 wined3d_vertex_declaration_incref(wined3d_declaration);
2147 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2149 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2150 fvf_decls[low].decl = wined3d_declaration;
2151 fvf_decls[low].fvf = fvf;
2152 ++device->fvf_decl_count;
2154 TRACE("Returning %p. %u declatations in array.\n", wined3d_declaration, device->fvf_decl_count);
2156 return wined3d_declaration;
2159 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2161 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2162 struct wined3d_vertex_declaration *decl;
2164 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2166 if (!fvf)
2168 WARN("%#x is not a valid FVF.\n", fvf);
2169 return D3D_OK;
2172 wined3d_mutex_lock();
2173 if (!(decl = device_get_fvf_declaration(device, fvf)))
2175 wined3d_mutex_unlock();
2176 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2177 return D3DERR_DRIVERINTERNALERROR;
2180 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2181 wined3d_mutex_unlock();
2183 return D3D_OK;
2186 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2188 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2189 struct wined3d_vertex_declaration *wined3d_declaration;
2190 struct d3d9_vertex_declaration *d3d9_declaration;
2192 TRACE("iface %p, fvf %p.\n", iface, fvf);
2194 wined3d_mutex_lock();
2195 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2197 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2198 *fvf = d3d9_declaration->fvf;
2200 else
2202 *fvf = 0;
2204 wined3d_mutex_unlock();
2206 TRACE("Returning FVF %#x.\n", *fvf);
2208 return D3D_OK;
2211 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2212 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2214 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2215 struct d3d9_vertexshader *object;
2216 HRESULT hr;
2218 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2220 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2221 if (!object)
2223 ERR("Failed to allocate vertex shader memory.\n");
2224 return E_OUTOFMEMORY;
2227 hr = vertexshader_init(object, device, byte_code);
2228 if (FAILED(hr))
2230 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2231 HeapFree(GetProcessHeap(), 0, object);
2232 return hr;
2235 TRACE("Created vertex shader %p.\n", object);
2236 *shader = &object->IDirect3DVertexShader9_iface;
2238 return D3D_OK;
2241 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2243 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2244 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2246 TRACE("iface %p, shader %p.\n", iface, shader);
2248 wined3d_mutex_lock();
2249 wined3d_device_set_vertex_shader(device->wined3d_device,
2250 shader_obj ? shader_obj->wined3d_shader : NULL);
2251 wined3d_mutex_unlock();
2253 return D3D_OK;
2256 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2258 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2259 struct d3d9_vertexshader *shader_impl;
2260 struct wined3d_shader *wined3d_shader;
2262 TRACE("iface %p, shader %p.\n", iface, shader);
2264 wined3d_mutex_lock();
2265 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2267 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2268 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2269 IDirect3DVertexShader9_AddRef(*shader);
2271 else
2273 *shader = NULL;
2275 wined3d_mutex_unlock();
2277 TRACE("Returning %p.\n", *shader);
2279 return D3D_OK;
2282 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2283 UINT reg_idx, const float *data, UINT count)
2285 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2286 HRESULT hr;
2288 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2290 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2292 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2293 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2294 return D3DERR_INVALIDCALL;
2297 wined3d_mutex_lock();
2298 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2299 wined3d_mutex_unlock();
2301 return hr;
2304 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2305 UINT reg_idx, float *data, UINT count)
2307 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2308 HRESULT hr;
2310 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2312 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2314 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2315 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2316 return D3DERR_INVALIDCALL;
2319 wined3d_mutex_lock();
2320 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2321 wined3d_mutex_unlock();
2323 return hr;
2326 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2327 UINT reg_idx, const int *data, UINT count)
2329 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2330 HRESULT hr;
2332 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2334 wined3d_mutex_lock();
2335 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2336 wined3d_mutex_unlock();
2338 return hr;
2341 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2342 UINT reg_idx, int *data, UINT count)
2344 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2345 HRESULT hr;
2347 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2349 wined3d_mutex_lock();
2350 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2351 wined3d_mutex_unlock();
2353 return hr;
2356 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2357 UINT reg_idx, const BOOL *data, UINT count)
2359 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2360 HRESULT hr;
2362 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2364 wined3d_mutex_lock();
2365 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2366 wined3d_mutex_unlock();
2368 return hr;
2371 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2372 UINT reg_idx, BOOL *data, UINT count)
2374 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2375 HRESULT hr;
2377 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2379 wined3d_mutex_lock();
2380 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2381 wined3d_mutex_unlock();
2383 return hr;
2386 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2387 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2389 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2390 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2391 HRESULT hr;
2393 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2394 iface, stream_idx, buffer, offset, stride);
2396 wined3d_mutex_lock();
2397 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2398 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2399 wined3d_mutex_unlock();
2401 return hr;
2404 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2405 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2407 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2408 struct d3d9_vertexbuffer *buffer_impl;
2409 struct wined3d_buffer *wined3d_buffer;
2410 HRESULT hr;
2412 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2413 iface, stream_idx, buffer, offset, stride);
2415 if (!buffer)
2416 return D3DERR_INVALIDCALL;
2418 wined3d_mutex_lock();
2419 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2420 if (SUCCEEDED(hr) && wined3d_buffer)
2422 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2423 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2424 IDirect3DVertexBuffer9_AddRef(*buffer);
2425 wined3d_buffer_decref(wined3d_buffer);
2427 else
2429 if (FAILED(hr))
2430 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2431 *buffer = NULL;
2433 wined3d_mutex_unlock();
2435 return hr;
2438 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2440 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2441 HRESULT hr;
2443 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2445 wined3d_mutex_lock();
2446 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2447 wined3d_mutex_unlock();
2449 return hr;
2452 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2454 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2455 HRESULT hr;
2457 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2459 wined3d_mutex_lock();
2460 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2461 wined3d_mutex_unlock();
2463 return hr;
2466 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2468 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2469 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2471 TRACE("iface %p, buffer %p.\n", iface, buffer);
2473 wined3d_mutex_lock();
2474 wined3d_device_set_index_buffer(device->wined3d_device,
2475 ib ? ib->wined3d_buffer : NULL,
2476 ib ? ib->format : WINED3DFMT_UNKNOWN);
2477 wined3d_mutex_unlock();
2479 return D3D_OK;
2482 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2484 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2485 struct wined3d_buffer *wined3d_buffer;
2486 struct d3d9_indexbuffer *buffer_impl;
2488 TRACE("iface %p, buffer %p.\n", iface, buffer);
2490 if (!buffer)
2491 return D3DERR_INVALIDCALL;
2493 wined3d_mutex_lock();
2494 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device)))
2496 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2497 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2498 IDirect3DIndexBuffer9_AddRef(*buffer);
2500 else
2502 *buffer = NULL;
2504 wined3d_mutex_unlock();
2506 return D3D_OK;
2509 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2510 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2512 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2513 struct d3d9_pixelshader *object;
2514 HRESULT hr;
2516 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2518 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2519 if (!object)
2521 FIXME("Failed to allocate pixel shader memory.\n");
2522 return E_OUTOFMEMORY;
2525 hr = pixelshader_init(object, device, byte_code);
2526 if (FAILED(hr))
2528 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2529 HeapFree(GetProcessHeap(), 0, object);
2530 return hr;
2533 TRACE("Created pixel shader %p.\n", object);
2534 *shader = &object->IDirect3DPixelShader9_iface;
2536 return D3D_OK;
2539 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2541 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2542 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2544 TRACE("iface %p, shader %p.\n", iface, shader);
2546 wined3d_mutex_lock();
2547 wined3d_device_set_pixel_shader(device->wined3d_device,
2548 shader_obj ? shader_obj->wined3d_shader : NULL);
2549 wined3d_mutex_unlock();
2551 return D3D_OK;
2554 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2556 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2557 struct d3d9_pixelshader *shader_impl;
2558 struct wined3d_shader *wined3d_shader;
2560 TRACE("iface %p, shader %p.\n", iface, shader);
2562 if (!shader) return D3DERR_INVALIDCALL;
2564 wined3d_mutex_lock();
2565 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2567 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2568 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2569 IDirect3DPixelShader9_AddRef(*shader);
2571 else
2573 *shader = NULL;
2575 wined3d_mutex_unlock();
2577 TRACE("Returning %p.\n", *shader);
2579 return D3D_OK;
2582 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2583 UINT reg_idx, const float *data, UINT count)
2585 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2586 HRESULT hr;
2588 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2590 wined3d_mutex_lock();
2591 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2592 wined3d_mutex_unlock();
2594 return hr;
2597 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2598 UINT reg_idx, float *data, UINT count)
2600 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2601 HRESULT hr;
2603 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2605 wined3d_mutex_lock();
2606 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2607 wined3d_mutex_unlock();
2609 return hr;
2612 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2613 UINT reg_idx, const int *data, UINT count)
2615 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2616 HRESULT hr;
2618 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2620 wined3d_mutex_lock();
2621 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2622 wined3d_mutex_unlock();
2624 return hr;
2627 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2628 UINT reg_idx, int *data, UINT count)
2630 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2631 HRESULT hr;
2633 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2635 wined3d_mutex_lock();
2636 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2637 wined3d_mutex_unlock();
2639 return hr;
2642 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2643 UINT reg_idx, const BOOL *data, UINT count)
2645 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2646 HRESULT hr;
2648 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2650 wined3d_mutex_lock();
2651 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2652 wined3d_mutex_unlock();
2654 return hr;
2657 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2658 UINT reg_idx, BOOL *data, UINT count)
2660 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2661 HRESULT hr;
2663 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2665 wined3d_mutex_lock();
2666 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2667 wined3d_mutex_unlock();
2669 return hr;
2672 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
2673 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2675 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2676 HRESULT hr;
2678 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2679 iface, handle, segment_count, patch_info);
2681 wined3d_mutex_lock();
2682 hr = wined3d_device_draw_rect_patch(device->wined3d_device, handle,
2683 segment_count, (const struct wined3d_rect_patch_info *)patch_info);
2684 wined3d_mutex_unlock();
2686 return hr;
2689 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
2690 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2692 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2693 HRESULT hr;
2695 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2696 iface, handle, segment_count, patch_info);
2698 wined3d_mutex_lock();
2699 hr = wined3d_device_draw_tri_patch(device->wined3d_device, handle,
2700 segment_count, (const struct wined3d_tri_patch_info *)patch_info);
2701 wined3d_mutex_unlock();
2703 return hr;
2706 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
2708 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2709 HRESULT hr;
2711 TRACE("iface %p, handle %#x.\n", iface, handle);
2713 wined3d_mutex_lock();
2714 hr = wined3d_device_delete_patch(device->wined3d_device, handle);
2715 wined3d_mutex_unlock();
2717 return hr;
2720 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
2722 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2723 struct d3d9_query *object;
2724 HRESULT hr;
2726 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2728 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2729 if (!object)
2731 ERR("Failed to allocate query memory.\n");
2732 return E_OUTOFMEMORY;
2735 hr = query_init(object, device, type);
2736 if (FAILED(hr))
2738 WARN("Failed to initialize query, hr %#x.\n", hr);
2739 HeapFree(GetProcessHeap(), 0, object);
2740 return hr;
2743 TRACE("Created query %p.\n", object);
2744 if (query) *query = &object->IDirect3DQuery9_iface;
2745 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
2747 return D3D_OK;
2750 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2751 UINT width, UINT height, float *rows, float *columns)
2753 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2754 iface, width, height, rows, columns);
2756 return E_NOTIMPL;
2759 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
2760 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2761 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2763 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2764 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2765 iface, src_surface, dst_surface, src_descs, rect_count,
2766 dst_descs, operation, offset_x, offset_y);
2768 return E_NOTIMPL;
2771 static HRESULT WINAPI d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
2772 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2773 const RGNDATA *dirty_region, DWORD flags)
2775 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2776 HRESULT hr;
2778 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
2779 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
2780 dst_window_override, dirty_region, flags);
2782 wined3d_mutex_lock();
2783 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
2784 dst_window_override, dirty_region, flags);
2785 wined3d_mutex_unlock();
2787 return hr;
2790 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2792 FIXME("iface %p, priority %p stub!\n", iface, priority);
2794 return E_NOTIMPL;
2797 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2799 FIXME("iface %p, priority %d stub!\n", iface, priority);
2801 return E_NOTIMPL;
2804 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2806 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2808 return E_NOTIMPL;
2811 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2812 IDirect3DResource9 **resources, UINT32 resource_count)
2814 FIXME("iface %p, resources %p, resource_count %u stub!\n",
2815 iface, resources, resource_count);
2817 return E_NOTIMPL;
2820 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2822 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2824 return E_NOTIMPL;
2827 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2829 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2831 *max_latency = 2;
2833 return E_NOTIMPL;
2836 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2838 static int i;
2840 TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
2842 if (!i++)
2843 FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2845 return D3D_OK;
2848 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2849 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2850 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2852 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2853 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2854 iface, width, height, format, multisample_type, multisample_quality,
2855 lockable, surface, shared_handle, usage);
2857 if (shared_handle)
2858 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2860 return E_NOTIMPL;
2863 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2864 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2865 HANDLE *shared_handle, DWORD usage)
2867 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2868 iface, width, height, format, pool, surface, shared_handle, usage);
2870 return E_NOTIMPL;
2873 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2874 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2875 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2877 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2878 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2879 iface, width, height, format, multisample_type, multisample_quality,
2880 discard, surface, shared_handle, usage);
2882 if (shared_handle)
2883 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2885 return E_NOTIMPL;
2888 static HRESULT WINAPI d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
2889 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2891 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2892 struct wined3d_swapchain_desc swapchain_desc;
2893 struct wined3d_display_mode wined3d_mode;
2894 HRESULT hr;
2896 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
2898 if (mode)
2900 wined3d_mode.width = mode->Width;
2901 wined3d_mode.height = mode->Height;
2902 wined3d_mode.refresh_rate = mode->RefreshRate;
2903 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
2904 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
2907 wined3d_mutex_lock();
2908 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
2909 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
2910 mode ? &wined3d_mode : NULL, reset_enum_callback);
2911 if (FAILED(hr))
2912 device->not_reset = TRUE;
2913 else
2914 device->not_reset = FALSE;
2915 wined3d_mutex_unlock();
2917 return hr;
2920 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2921 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2923 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2924 struct wined3d_display_mode wined3d_mode;
2925 HRESULT hr;
2927 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
2928 iface, swapchain_idx, mode, rotation);
2930 if (mode->Size != sizeof(*mode))
2931 return D3DERR_INVALIDCALL;
2933 wined3d_mutex_lock();
2934 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
2935 (enum wined3d_display_rotation *)rotation);
2936 wined3d_mutex_unlock();
2938 if (SUCCEEDED(hr))
2940 mode->Width = wined3d_mode.width;
2941 mode->Height = wined3d_mode.height;
2942 mode->RefreshRate = wined3d_mode.refresh_rate;
2943 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
2944 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
2947 return hr;
2950 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
2952 /* IUnknown */
2953 d3d9_device_QueryInterface,
2954 d3d9_device_AddRef,
2955 d3d9_device_Release,
2956 /* IDirect3DDevice9 */
2957 d3d9_device_TestCooperativeLevel,
2958 d3d9_device_GetAvailableTextureMem,
2959 d3d9_device_EvictManagedResources,
2960 d3d9_device_GetDirect3D,
2961 d3d9_device_GetDeviceCaps,
2962 d3d9_device_GetDisplayMode,
2963 d3d9_device_GetCreationParameters,
2964 d3d9_device_SetCursorProperties,
2965 d3d9_device_SetCursorPosition,
2966 d3d9_device_ShowCursor,
2967 d3d9_device_CreateAdditionalSwapChain,
2968 d3d9_device_GetSwapChain,
2969 d3d9_device_GetNumberOfSwapChains,
2970 d3d9_device_Reset,
2971 d3d9_device_Present,
2972 d3d9_device_GetBackBuffer,
2973 d3d9_device_GetRasterStatus,
2974 d3d9_device_SetDialogBoxMode,
2975 d3d9_device_SetGammaRamp,
2976 d3d9_device_GetGammaRamp,
2977 d3d9_device_CreateTexture,
2978 d3d9_device_CreateVolumeTexture,
2979 d3d9_device_CreateCubeTexture,
2980 d3d9_device_CreateVertexBuffer,
2981 d3d9_device_CreateIndexBuffer,
2982 d3d9_device_CreateRenderTarget,
2983 d3d9_device_CreateDepthStencilSurface,
2984 d3d9_device_UpdateSurface,
2985 d3d9_device_UpdateTexture,
2986 d3d9_device_GetRenderTargetData,
2987 d3d9_device_GetFrontBufferData,
2988 d3d9_device_StretchRect,
2989 d3d9_device_ColorFill,
2990 d3d9_device_CreateOffscreenPlainSurface,
2991 d3d9_device_SetRenderTarget,
2992 d3d9_device_GetRenderTarget,
2993 d3d9_device_SetDepthStencilSurface,
2994 d3d9_device_GetDepthStencilSurface,
2995 d3d9_device_BeginScene,
2996 d3d9_device_EndScene,
2997 d3d9_device_Clear,
2998 d3d9_device_SetTransform,
2999 d3d9_device_GetTransform,
3000 d3d9_device_MultiplyTransform,
3001 d3d9_device_SetViewport,
3002 d3d9_device_GetViewport,
3003 d3d9_device_SetMaterial,
3004 d3d9_device_GetMaterial,
3005 d3d9_device_SetLight,
3006 d3d9_device_GetLight,
3007 d3d9_device_LightEnable,
3008 d3d9_device_GetLightEnable,
3009 d3d9_device_SetClipPlane,
3010 d3d9_device_GetClipPlane,
3011 d3d9_device_SetRenderState,
3012 d3d9_device_GetRenderState,
3013 d3d9_device_CreateStateBlock,
3014 d3d9_device_BeginStateBlock,
3015 d3d9_device_EndStateBlock,
3016 d3d9_device_SetClipStatus,
3017 d3d9_device_GetClipStatus,
3018 d3d9_device_GetTexture,
3019 d3d9_device_SetTexture,
3020 d3d9_device_GetTextureStageState,
3021 d3d9_device_SetTextureStageState,
3022 d3d9_device_GetSamplerState,
3023 d3d9_device_SetSamplerState,
3024 d3d9_device_ValidateDevice,
3025 d3d9_device_SetPaletteEntries,
3026 d3d9_device_GetPaletteEntries,
3027 d3d9_device_SetCurrentTexturePalette,
3028 d3d9_device_GetCurrentTexturePalette,
3029 d3d9_device_SetScissorRect,
3030 d3d9_device_GetScissorRect,
3031 d3d9_device_SetSoftwareVertexProcessing,
3032 d3d9_device_GetSoftwareVertexProcessing,
3033 d3d9_device_SetNPatchMode,
3034 d3d9_device_GetNPatchMode,
3035 d3d9_device_DrawPrimitive,
3036 d3d9_device_DrawIndexedPrimitive,
3037 d3d9_device_DrawPrimitiveUP,
3038 d3d9_device_DrawIndexedPrimitiveUP,
3039 d3d9_device_ProcessVertices,
3040 d3d9_device_CreateVertexDeclaration,
3041 d3d9_device_SetVertexDeclaration,
3042 d3d9_device_GetVertexDeclaration,
3043 d3d9_device_SetFVF,
3044 d3d9_device_GetFVF,
3045 d3d9_device_CreateVertexShader,
3046 d3d9_device_SetVertexShader,
3047 d3d9_device_GetVertexShader,
3048 d3d9_device_SetVertexShaderConstantF,
3049 d3d9_device_GetVertexShaderConstantF,
3050 d3d9_device_SetVertexShaderConstantI,
3051 d3d9_device_GetVertexShaderConstantI,
3052 d3d9_device_SetVertexShaderConstantB,
3053 d3d9_device_GetVertexShaderConstantB,
3054 d3d9_device_SetStreamSource,
3055 d3d9_device_GetStreamSource,
3056 d3d9_device_SetStreamSourceFreq,
3057 d3d9_device_GetStreamSourceFreq,
3058 d3d9_device_SetIndices,
3059 d3d9_device_GetIndices,
3060 d3d9_device_CreatePixelShader,
3061 d3d9_device_SetPixelShader,
3062 d3d9_device_GetPixelShader,
3063 d3d9_device_SetPixelShaderConstantF,
3064 d3d9_device_GetPixelShaderConstantF,
3065 d3d9_device_SetPixelShaderConstantI,
3066 d3d9_device_GetPixelShaderConstantI,
3067 d3d9_device_SetPixelShaderConstantB,
3068 d3d9_device_GetPixelShaderConstantB,
3069 d3d9_device_DrawRectPatch,
3070 d3d9_device_DrawTriPatch,
3071 d3d9_device_DeletePatch,
3072 d3d9_device_CreateQuery,
3073 /* IDirect3DDevice9Ex */
3074 d3d9_device_SetConvolutionMonoKernel,
3075 d3d9_device_ComposeRects,
3076 d3d9_device_PresentEx,
3077 d3d9_device_GetGPUThreadPriority,
3078 d3d9_device_SetGPUThreadPriority,
3079 d3d9_device_WaitForVBlank,
3080 d3d9_device_CheckResourceResidency,
3081 d3d9_device_SetMaximumFrameLatency,
3082 d3d9_device_GetMaximumFrameLatency,
3083 d3d9_device_CheckDeviceState,
3084 d3d9_device_CreateRenderTargetEx,
3085 d3d9_device_CreateOffscreenPlainSurfaceEx,
3086 d3d9_device_CreateDepthStencilSurfaceEx,
3087 d3d9_device_ResetEx,
3088 d3d9_device_GetDisplayModeEx,
3091 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3093 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3096 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3097 struct wined3d_device *device)
3099 TRACE("device_parent %p, device %p.\n", device_parent, device);
3102 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3104 TRACE("device_parent %p.\n", device_parent);
3107 static HRESULT CDECL device_parent_create_texture_surface(struct wined3d_device_parent *device_parent,
3108 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
3109 enum wined3d_pool pool, UINT sub_resource_idx, struct wined3d_surface **surface)
3111 struct d3d9_device *device = device_from_device_parent(device_parent);
3112 struct d3d9_surface *d3d_surface;
3113 BOOL lockable = TRUE;
3114 HRESULT hr;
3116 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
3117 "\tpool %#x, sub_resource_idx %u, surface %p.\n",
3118 device_parent, container_parent, width, height, format, usage, pool, sub_resource_idx, surface);
3120 if (pool == WINED3D_POOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
3121 lockable = FALSE;
3123 if (FAILED(hr = d3d9_device_create_surface(device, width, height, d3dformat_from_wined3dformat(format),
3124 lockable, FALSE, (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0)))
3126 WARN("Failed to create surface, hr %#x.\n", hr);
3127 return hr;
3130 *surface = d3d_surface->wined3d_surface;
3131 wined3d_surface_incref(*surface);
3133 d3d_surface->container = container_parent;
3134 IDirect3DDevice9Ex_Release(d3d_surface->parent_device);
3135 d3d_surface->parent_device = NULL;
3137 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3138 d3d_surface->forwardReference = container_parent;
3140 return hr;
3143 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3144 void *container_parent, UINT width, UINT height, enum wined3d_format_id format_id, DWORD usage,
3145 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, struct wined3d_surface **surface)
3147 struct d3d9_device *device = device_from_device_parent(device_parent);
3148 struct d3d9_surface *d3d_surface;
3149 HRESULT hr;
3151 TRACE("device_parent %p, container_parent %p, width %u, height %u, format_id %#x, usage %#x,\n"
3152 "\tmultisample_type %#x, multisample_quality %u, surface %p.\n",
3153 device_parent, container_parent, width, height, format_id, usage,
3154 multisample_type, multisample_quality, surface);
3156 if (FAILED(hr = d3d9_device_create_surface(device, width, height, d3dformat_from_wined3dformat(format_id),
3157 TRUE, FALSE, (IDirect3DSurface9 **)&d3d_surface, usage, D3DPOOL_DEFAULT, multisample_type,
3158 multisample_quality)))
3160 WARN("Failed to create surface, hr %#x.\n", hr);
3161 return hr;
3164 *surface = d3d_surface->wined3d_surface;
3165 wined3d_surface_incref(*surface);
3167 if (container_parent == device_parent)
3168 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice9Ex_iface;
3169 else
3170 d3d_surface->container = container_parent;
3171 /* Implicit surfaces are created with an refcount of 0 */
3172 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3174 return hr;
3177 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
3178 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
3179 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
3181 struct d3d9_device *device = device_from_device_parent(device_parent);
3182 struct d3d9_volume *object;
3183 HRESULT hr;
3185 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
3186 "format %#x, pool %#x, usage %#x, volume %p\n",
3187 device_parent, container_parent, width, height, depth,
3188 format, pool, usage, volume);
3190 /* Allocate the storage for the device */
3191 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3192 if (!object)
3194 FIXME("Allocation of memory failed\n");
3195 *volume = NULL;
3196 return D3DERR_OUTOFVIDEOMEMORY;
3199 hr = volume_init(object, device, width, height, depth, usage, format, pool);
3200 if (FAILED(hr))
3202 WARN("Failed to initialize volume, hr %#x.\n", hr);
3203 HeapFree(GetProcessHeap(), 0, object);
3204 return hr;
3207 *volume = object->wined3d_volume;
3208 wined3d_volume_incref(*volume);
3209 IDirect3DVolume9_Release(&object->IDirect3DVolume9_iface);
3211 object->container = container_parent;
3212 object->forwardReference = container_parent;
3214 TRACE("Created volume %p.\n", object);
3216 return hr;
3219 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3220 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3222 struct d3d9_device *device = device_from_device_parent(device_parent);
3223 struct d3d9_swapchain *d3d_swapchain;
3224 HRESULT hr;
3226 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3228 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3229 if (FAILED(hr))
3231 WARN("Failed to create swapchain, hr %#x.\n", hr);
3232 *swapchain = NULL;
3233 return hr;
3236 *swapchain = d3d_swapchain->wined3d_swapchain;
3237 wined3d_swapchain_incref(*swapchain);
3238 IDirect3DSwapChain9_Release(&d3d_swapchain->IDirect3DSwapChain9_iface);
3240 return hr;
3243 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3245 device_parent_wined3d_device_created,
3246 device_parent_mode_changed,
3247 device_parent_create_swapchain_surface,
3248 device_parent_create_texture_surface,
3249 device_parent_create_volume,
3250 device_parent_create_swapchain,
3253 static void setup_fpu(void)
3255 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3256 WORD cw;
3257 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3258 cw = (cw & ~0xf3f) | 0x3f;
3259 __asm__ volatile ("fldcw %0" : : "m" (cw));
3260 #elif defined(__i386__) && defined(_MSC_VER)
3261 WORD cw;
3262 __asm fnstcw cw;
3263 cw = (cw & ~0xf3f) | 0x3f;
3264 __asm fldcw cw;
3265 #else
3266 FIXME("FPU setup not implemented for this platform.\n");
3267 #endif
3270 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3271 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3272 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3274 struct wined3d_swapchain_desc *swapchain_desc;
3275 UINT i, count = 1;
3276 HRESULT hr;
3278 if (mode)
3279 FIXME("Ignoring display mode.\n");
3281 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3282 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3283 device->refcount = 1;
3285 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3287 wined3d_mutex_lock();
3288 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3289 &device->device_parent, &device->wined3d_device);
3290 if (FAILED(hr))
3292 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3293 wined3d_mutex_unlock();
3294 return hr;
3297 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3299 WINED3DCAPS caps;
3301 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3302 count = caps.NumberOfAdaptersInGroup;
3305 if (flags & D3DCREATE_MULTITHREADED)
3306 wined3d_device_set_multithreaded(device->wined3d_device);
3308 if (!parameters->Windowed)
3310 if (!focus_window)
3311 focus_window = parameters->hDeviceWindow;
3312 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3314 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3315 wined3d_device_decref(device->wined3d_device);
3316 wined3d_mutex_unlock();
3317 return hr;
3320 for (i = 0; i < count; ++i)
3322 HWND device_window = parameters[i].hDeviceWindow;
3324 if (!device_window) device_window = focus_window;
3325 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3326 parameters[i].BackBufferWidth,
3327 parameters[i].BackBufferHeight);
3331 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3332 if (!swapchain_desc)
3334 ERR("Failed to allocate wined3d parameters.\n");
3335 wined3d_device_decref(device->wined3d_device);
3336 wined3d_mutex_unlock();
3337 return E_OUTOFMEMORY;
3340 for (i = 0; i < count; ++i)
3342 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i]);
3345 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3346 if (FAILED(hr))
3348 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3349 wined3d_device_release_focus_window(device->wined3d_device);
3350 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3351 wined3d_device_decref(device->wined3d_device);
3352 wined3d_mutex_unlock();
3353 return hr;
3356 wined3d_mutex_unlock();
3358 for (i = 0; i < count; ++i)
3360 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3362 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3364 /* Initialize the converted declaration array. This creates a valid pointer
3365 * and when adding decls HeapReAlloc() can be used without further checking. */
3366 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3367 if (!device->fvf_decls)
3369 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3370 wined3d_mutex_lock();
3371 wined3d_device_uninit_3d(device->wined3d_device);
3372 wined3d_device_release_focus_window(device->wined3d_device);
3373 wined3d_device_decref(device->wined3d_device);
3374 wined3d_mutex_unlock();
3375 return E_OUTOFMEMORY;
3378 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3379 device->d3d_parent = parent;
3381 return D3D_OK;