dbghelp: Add debug logging to some error paths in the Mach-O support.
[wine/multimedia.git] / dlls / d3d9 / device.c
blob170d9e0ae91ce1cd043d06d3e40e32347770d2d9
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 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
30 static const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
32 d3d9_null_wined3d_object_destroyed,
35 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
37 BYTE *c = (BYTE *)&format;
39 /* Don't translate FOURCC formats */
40 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
42 switch(format)
44 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
45 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
46 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
47 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
48 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
49 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
50 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
51 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
52 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
53 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
54 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
55 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
56 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
57 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
58 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
59 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
60 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
61 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
73 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
74 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
75 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
76 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
77 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
78 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
79 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
80 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
81 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
82 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
83 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
84 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
85 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
86 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
87 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
88 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
89 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
90 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
91 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
92 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
93 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
94 default:
95 FIXME("Unhandled wined3d format %#x.\n", format);
96 return D3DFMT_UNKNOWN;
100 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
102 BYTE *c = (BYTE *)&format;
104 /* Don't translate FOURCC formats */
105 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
107 switch(format)
109 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
110 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
111 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
112 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
113 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
114 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
115 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
116 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
117 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
118 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
119 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
120 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
121 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
122 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
123 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
124 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
125 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
126 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
127 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
128 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
129 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
130 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
131 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
132 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
133 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
134 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
135 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
136 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
137 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
138 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
139 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
140 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
141 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
142 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
143 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
144 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
145 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
146 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
147 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
148 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
149 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
150 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
151 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
152 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
153 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
154 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
155 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
156 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
157 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
158 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
159 default:
160 FIXME("Unhandled D3DFORMAT %#x\n", format);
161 return WINED3DFMT_UNKNOWN;
165 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
167 switch(primitive_type)
169 case D3DPT_POINTLIST:
170 return primitive_count;
172 case D3DPT_LINELIST:
173 return primitive_count * 2;
175 case D3DPT_LINESTRIP:
176 return primitive_count + 1;
178 case D3DPT_TRIANGLELIST:
179 return primitive_count * 3;
181 case D3DPT_TRIANGLESTRIP:
182 case D3DPT_TRIANGLEFAN:
183 return primitive_count + 2;
185 default:
186 FIXME("Unhandled primitive type %#x\n", primitive_type);
187 return 0;
191 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
192 const struct wined3d_swapchain_desc *swapchain_desc)
194 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
195 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
196 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
197 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
198 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
199 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
200 present_parameters->SwapEffect = swapchain_desc->swap_effect;
201 present_parameters->hDeviceWindow = swapchain_desc->device_window;
202 present_parameters->Windowed = swapchain_desc->windowed;
203 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
204 present_parameters->AutoDepthStencilFormat
205 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
206 present_parameters->Flags = swapchain_desc->flags;
207 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
208 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
211 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
212 const D3DPRESENT_PARAMETERS *present_parameters)
214 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
215 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
216 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
217 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
218 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
219 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
220 swapchain_desc->swap_effect = present_parameters->SwapEffect;
221 swapchain_desc->device_window = present_parameters->hDeviceWindow;
222 swapchain_desc->windowed = present_parameters->Windowed;
223 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
224 swapchain_desc->auto_depth_stencil_format
225 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
226 swapchain_desc->flags = present_parameters->Flags;
227 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
228 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
229 swapchain_desc->auto_restore_display_mode = TRUE;
232 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
234 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
236 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
237 || IsEqualGUID(riid, &IID_IUnknown))
239 IDirect3DDevice9Ex_AddRef(iface);
240 *out = iface;
241 return S_OK;
244 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
246 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
248 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
249 * It doesn't matter with which function the device was created. */
250 if (!device->d3d_parent->extended)
252 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
253 *out = NULL;
254 return E_NOINTERFACE;
257 IDirect3DDevice9Ex_AddRef(iface);
258 *out = iface;
259 return S_OK;
262 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
264 *out = NULL;
265 return E_NOINTERFACE;
268 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
271 ULONG refcount = InterlockedIncrement(&device->refcount);
273 TRACE("%p increasing refcount to %u.\n", iface, refcount);
275 return refcount;
278 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
280 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
281 ULONG refcount;
283 if (device->in_destruction)
284 return 0;
286 refcount = InterlockedDecrement(&device->refcount);
288 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
290 if (!refcount)
292 unsigned i;
293 device->in_destruction = TRUE;
295 wined3d_mutex_lock();
296 for (i = 0; i < device->fvf_decl_count; ++i)
298 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
300 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
302 if (device->vertex_buffer)
303 wined3d_buffer_decref(device->vertex_buffer);
304 if (device->index_buffer)
305 wined3d_buffer_decref(device->index_buffer);
307 wined3d_device_uninit_3d(device->wined3d_device);
308 wined3d_device_release_focus_window(device->wined3d_device);
309 wined3d_device_decref(device->wined3d_device);
310 wined3d_mutex_unlock();
312 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
314 HeapFree(GetProcessHeap(), 0, device);
317 return refcount;
320 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
322 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
324 TRACE("iface %p.\n", iface);
326 if (device->not_reset)
328 TRACE("D3D9 device is marked not reset.\n");
329 return D3DERR_DEVICENOTRESET;
332 return D3D_OK;
335 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
337 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
338 HRESULT hr;
340 TRACE("iface %p.\n", iface);
342 wined3d_mutex_lock();
343 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
344 wined3d_mutex_unlock();
346 return hr;
349 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
353 TRACE("iface %p.\n", iface);
355 wined3d_mutex_lock();
356 wined3d_device_evict_managed_resources(device->wined3d_device);
357 wined3d_mutex_unlock();
359 return D3D_OK;
362 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
364 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
366 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
368 if (!d3d9)
369 return D3DERR_INVALIDCALL;
371 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
374 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
376 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
377 WINED3DCAPS *wined3d_caps;
378 HRESULT hr;
380 TRACE("iface %p, caps %p.\n", iface, caps);
382 if (!caps)
383 return D3DERR_INVALIDCALL;
385 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
386 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
388 memset(caps, 0, sizeof(*caps));
390 wined3d_mutex_lock();
391 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
392 wined3d_mutex_unlock();
394 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
395 HeapFree(GetProcessHeap(), 0, wined3d_caps);
397 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
398 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
400 filter_caps(caps);
402 return hr;
405 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
407 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
408 struct wined3d_display_mode wined3d_mode;
409 HRESULT hr;
411 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
413 wined3d_mutex_lock();
414 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
415 wined3d_mutex_unlock();
417 if (SUCCEEDED(hr))
419 mode->Width = wined3d_mode.width;
420 mode->Height = wined3d_mode.height;
421 mode->RefreshRate = wined3d_mode.refresh_rate;
422 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
425 return hr;
428 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
429 D3DDEVICE_CREATION_PARAMETERS *parameters)
431 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
433 TRACE("iface %p, parameters %p.\n", iface, parameters);
435 wined3d_mutex_lock();
436 wined3d_device_get_creation_parameters(device->wined3d_device,
437 (struct wined3d_device_creation_parameters *)parameters);
438 wined3d_mutex_unlock();
440 return D3D_OK;
443 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
444 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
446 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
447 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
448 HRESULT hr;
450 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
451 iface, hotspot_x, hotspot_y, bitmap);
453 if (!bitmap)
455 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
456 return D3DERR_INVALIDCALL;
459 wined3d_mutex_lock();
460 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
461 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
462 wined3d_mutex_unlock();
464 return hr;
467 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
469 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
471 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
473 wined3d_mutex_lock();
474 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
475 wined3d_mutex_unlock();
478 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
480 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
481 BOOL ret;
483 TRACE("iface %p, show %#x.\n", iface, show);
485 wined3d_mutex_lock();
486 ret = wined3d_device_show_cursor(device->wined3d_device, show);
487 wined3d_mutex_unlock();
489 return ret;
492 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
493 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
495 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
496 struct wined3d_swapchain_desc desc;
497 struct d3d9_swapchain *object;
498 HRESULT hr;
500 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
501 iface, present_parameters, swapchain);
503 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
504 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
505 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
506 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
508 return hr;
511 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
512 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
514 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
515 struct wined3d_swapchain *wined3d_swapchain;
516 struct d3d9_swapchain *swapchain_impl;
517 HRESULT hr;
519 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
521 wined3d_mutex_lock();
522 if ((wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx)))
524 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
525 *swapchain = (IDirect3DSwapChain9 *)&swapchain_impl->IDirect3DSwapChain9Ex_iface;
526 IDirect3DSwapChain9Ex_AddRef(*swapchain);
527 hr = D3D_OK;
529 else
531 *swapchain = NULL;
532 hr = D3DERR_INVALIDCALL;
534 wined3d_mutex_unlock();
536 return hr;
539 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
541 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
542 UINT count;
544 TRACE("iface %p.\n", iface);
546 wined3d_mutex_lock();
547 count = wined3d_device_get_swapchain_count(device->wined3d_device);
548 wined3d_mutex_unlock();
550 return count;
553 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
555 struct wined3d_resource_desc desc;
557 wined3d_resource_get_desc(resource, &desc);
558 if (desc.pool == WINED3D_POOL_DEFAULT)
560 struct d3d9_surface *surface;
562 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
564 IUnknown *parent = wined3d_resource_get_parent(resource);
565 IDirect3DBaseTexture9 *texture;
567 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
569 IDirect3DBaseTexture9_Release(texture);
570 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
571 return D3DERR_INVALIDCALL;
574 return D3D_OK;
577 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
579 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
580 return D3DERR_INVALIDCALL;
583 surface = wined3d_resource_get_parent(resource);
584 if (surface->refcount)
586 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
587 return D3DERR_INVALIDCALL;
590 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
593 return D3D_OK;
596 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
597 D3DPRESENT_PARAMETERS *present_parameters)
599 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
600 struct wined3d_swapchain_desc swapchain_desc;
601 HRESULT hr;
603 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
605 wined3d_mutex_lock();
607 if (device->vertex_buffer)
609 wined3d_buffer_decref(device->vertex_buffer);
610 device->vertex_buffer = NULL;
611 device->vertex_buffer_size = 0;
613 if (device->index_buffer)
615 wined3d_buffer_decref(device->index_buffer);
616 device->index_buffer = NULL;
617 device->index_buffer_size = 0;
620 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
621 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
622 NULL, reset_enum_callback, !device->d3d_parent->extended);
623 if (FAILED(hr) && !device->d3d_parent->extended)
624 device->not_reset = TRUE;
625 else
626 device->not_reset = FALSE;
627 wined3d_mutex_unlock();
629 return hr;
632 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
633 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
635 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
636 HRESULT hr;
638 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
639 iface, src_rect, dst_rect, dst_window_override, dirty_region);
641 wined3d_mutex_lock();
642 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
643 dst_window_override, dirty_region, 0);
644 wined3d_mutex_unlock();
646 return hr;
649 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
650 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
653 struct wined3d_surface *wined3d_surface = NULL;
654 struct d3d9_surface *surface_impl;
655 HRESULT hr;
657 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
658 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
660 wined3d_mutex_lock();
661 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
662 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
663 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
665 surface_impl = wined3d_surface_get_parent(wined3d_surface);
666 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
667 IDirect3DSurface9_AddRef(*backbuffer);
669 wined3d_mutex_unlock();
671 return hr;
673 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
674 UINT swapchain, D3DRASTER_STATUS *raster_status)
676 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
677 HRESULT hr;
679 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
681 wined3d_mutex_lock();
682 hr = wined3d_device_get_raster_status(device->wined3d_device,
683 swapchain, (struct wined3d_raster_status *)raster_status);
684 wined3d_mutex_unlock();
686 return hr;
689 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
691 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
692 HRESULT hr;
694 TRACE("iface %p, enable %#x.\n", iface, enable);
696 wined3d_mutex_lock();
697 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
698 wined3d_mutex_unlock();
700 return hr;
703 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
704 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
706 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
708 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
710 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
711 wined3d_mutex_lock();
712 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
713 wined3d_mutex_unlock();
716 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
720 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
722 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
723 wined3d_mutex_lock();
724 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
725 wined3d_mutex_unlock();
728 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
729 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
730 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
733 struct d3d9_texture *object;
734 BOOL set_mem = FALSE;
735 HRESULT hr;
737 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
738 iface, width, height, levels, usage, format, pool, texture, shared_handle);
740 *texture = NULL;
741 if (shared_handle)
743 if (pool == D3DPOOL_SYSTEMMEM)
745 if (levels != 1)
746 return D3DERR_INVALIDCALL;
747 set_mem = TRUE;
749 else
750 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
753 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
754 if (!object)
755 return D3DERR_OUTOFVIDEOMEMORY;
757 hr = texture_init(object, device, width, height, levels, usage, format, pool);
758 if (FAILED(hr))
760 WARN("Failed to initialize texture, hr %#x.\n", hr);
761 HeapFree(GetProcessHeap(), 0, object);
762 return hr;
765 if (set_mem)
767 struct wined3d_resource *resource;
768 struct d3d9_surface *surface;
770 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
771 surface = wined3d_resource_get_parent(resource);
772 wined3d_surface_set_mem(surface->wined3d_surface, *shared_handle, 0);
775 TRACE("Created texture %p.\n", object);
776 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
778 return D3D_OK;
781 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
782 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
783 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
785 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
786 struct d3d9_texture *object;
787 HRESULT hr;
789 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
790 iface, width, height, depth, levels);
791 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
792 usage, format, pool, texture, shared_handle);
794 *texture = NULL;
795 if (shared_handle)
796 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
798 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
799 if (!object)
800 return D3DERR_OUTOFVIDEOMEMORY;
802 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
803 if (FAILED(hr))
805 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
806 HeapFree(GetProcessHeap(), 0, object);
807 return hr;
810 TRACE("Created volume texture %p.\n", object);
811 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
813 return D3D_OK;
816 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
817 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
818 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
820 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
821 struct d3d9_texture *object;
822 HRESULT hr;
824 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
825 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
827 *texture = NULL;
828 if (shared_handle)
829 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
831 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
832 if (!object)
833 return D3DERR_OUTOFVIDEOMEMORY;
835 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
836 if (FAILED(hr))
838 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
839 HeapFree(GetProcessHeap(), 0, object);
840 return hr;
843 TRACE("Created cube texture %p.\n", object);
844 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
846 return D3D_OK;
849 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
850 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
851 HANDLE *shared_handle)
853 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
854 struct d3d9_vertexbuffer *object;
855 HRESULT hr;
857 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
858 iface, size, usage, fvf, pool, buffer, shared_handle);
860 if (shared_handle)
861 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
863 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
864 if (!object)
865 return D3DERR_OUTOFVIDEOMEMORY;
867 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
868 if (FAILED(hr))
870 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
871 HeapFree(GetProcessHeap(), 0, object);
872 return hr;
875 TRACE("Created vertex buffer %p.\n", object);
876 *buffer = &object->IDirect3DVertexBuffer9_iface;
878 return D3D_OK;
881 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
882 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
883 HANDLE *shared_handle)
885 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
886 struct d3d9_indexbuffer *object;
887 HRESULT hr;
889 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
890 iface, size, usage, format, pool, buffer, shared_handle);
892 if (shared_handle)
893 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
895 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
896 if (!object)
897 return D3DERR_OUTOFVIDEOMEMORY;
899 hr = indexbuffer_init(object, device, size, usage, format, pool);
900 if (FAILED(hr))
902 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
903 HeapFree(GetProcessHeap(), 0, object);
904 return hr;
907 TRACE("Created index buffer %p.\n", object);
908 *buffer = &object->IDirect3DIndexBuffer9_iface;
910 return D3D_OK;
913 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
914 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
915 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
917 struct wined3d_resource *sub_resource;
918 struct wined3d_resource_desc desc;
919 struct d3d9_surface *surface_impl;
920 struct wined3d_texture *texture;
921 HRESULT hr;
923 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
924 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
925 device, width, height, format, flags, surface, usage, pool,
926 multisample_type, multisample_quality);
928 desc.resource_type = WINED3D_RTYPE_TEXTURE;
929 desc.format = wined3dformat_from_d3dformat(format);
930 desc.multisample_type = multisample_type;
931 desc.multisample_quality = multisample_quality;
932 desc.usage = usage & WINED3DUSAGE_MASK;
933 desc.pool = pool;
934 desc.width = width;
935 desc.height = height;
936 desc.depth = 1;
937 desc.size = 0;
939 wined3d_mutex_lock();
941 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
942 1, flags, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
944 wined3d_mutex_unlock();
945 WARN("Failed to create texture, hr %#x.\n", hr);
946 return hr;
949 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
950 surface_impl = wined3d_resource_get_parent(sub_resource);
951 surface_impl->forwardReference = NULL;
952 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
953 *surface = &surface_impl->IDirect3DSurface9_iface;
954 IDirect3DSurface9_AddRef(*surface);
955 wined3d_texture_decref(texture);
957 wined3d_mutex_unlock();
959 return D3D_OK;
962 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
963 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
964 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
966 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
967 DWORD flags = 0;
969 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
970 "lockable %#x, surface %p, shared_handle %p.\n",
971 iface, width, height, format, multisample_type, multisample_quality,
972 lockable, surface, shared_handle);
974 *surface = NULL;
975 if (shared_handle)
976 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
978 if (lockable)
979 flags |= WINED3D_SURFACE_MAPPABLE;
981 return d3d9_device_create_surface(device, width, height, format, flags, surface,
982 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality);
985 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
986 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
987 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
989 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
990 DWORD flags = WINED3D_SURFACE_MAPPABLE;
992 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
993 "discard %#x, surface %p, shared_handle %p.\n",
994 iface, width, height, format, multisample_type, multisample_quality,
995 discard, surface, shared_handle);
997 *surface = NULL;
998 if (shared_handle)
999 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1001 if (discard)
1002 flags |= WINED3D_SURFACE_DISCARD;
1004 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1005 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality);
1009 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1010 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1011 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1013 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1014 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1015 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1016 HRESULT hr;
1018 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1019 iface, src_surface, src_rect, dst_surface, dst_point);
1021 wined3d_mutex_lock();
1022 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1023 dst->wined3d_surface, dst_point);
1024 wined3d_mutex_unlock();
1026 return hr;
1029 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1030 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1032 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1033 struct d3d9_texture *src_impl, *dst_impl;
1034 HRESULT hr;
1036 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1038 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1039 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1041 wined3d_mutex_lock();
1042 hr = wined3d_device_update_texture(device->wined3d_device,
1043 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1044 wined3d_mutex_unlock();
1046 return hr;
1049 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1050 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1052 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1053 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1054 HRESULT hr;
1056 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1058 wined3d_mutex_lock();
1059 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1060 wined3d_mutex_unlock();
1062 return hr;
1065 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1066 UINT swapchain, IDirect3DSurface9 *dst_surface)
1068 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1069 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1070 HRESULT hr;
1072 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1074 wined3d_mutex_lock();
1075 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1076 wined3d_mutex_unlock();
1078 return hr;
1081 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1082 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1084 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1085 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1086 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1087 HRESULT hr = D3DERR_INVALIDCALL;
1088 struct wined3d_resource_desc src_desc, dst_desc;
1089 struct wined3d_resource *wined3d_resource;
1091 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1092 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1094 wined3d_mutex_lock();
1095 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1096 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1098 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1099 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1101 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1103 if (device->in_scene)
1105 WARN("Rejecting depth / stencil blit while in scene.\n");
1106 goto done;
1109 if (src_rect)
1111 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1112 || src_rect->bottom != src_desc.height)
1114 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1115 wine_dbgstr_rect(src_rect));
1116 goto done;
1119 if (dst_rect)
1121 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1122 || dst_rect->bottom != dst_desc.height)
1124 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1125 wine_dbgstr_rect(dst_rect));
1126 goto done;
1129 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1131 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1132 goto done;
1136 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1137 if (hr == WINEDDERR_INVALIDRECT)
1138 hr = D3DERR_INVALIDCALL;
1140 done:
1141 wined3d_mutex_unlock();
1142 return hr;
1145 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1146 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1148 const struct wined3d_color c =
1150 ((color >> 16) & 0xff) / 255.0f,
1151 ((color >> 8) & 0xff) / 255.0f,
1152 (color & 0xff) / 255.0f,
1153 ((color >> 24) & 0xff) / 255.0f,
1155 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1156 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1157 struct wined3d_resource *wined3d_resource;
1158 struct wined3d_resource_desc desc;
1159 HRESULT hr;
1161 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1163 wined3d_mutex_lock();
1165 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1166 wined3d_resource_get_desc(wined3d_resource, &desc);
1168 /* This method is only allowed with surfaces that are render targets, or
1169 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1170 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1172 wined3d_mutex_unlock();
1173 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1174 return D3DERR_INVALIDCALL;
1177 /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1178 hr = wined3d_device_color_fill(device->wined3d_device, surface_impl->wined3d_surface, rect, &c);
1180 wined3d_mutex_unlock();
1182 return hr;
1185 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1186 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1187 HANDLE *shared_handle)
1189 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1191 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1192 iface, width, height, format, pool, surface, shared_handle);
1194 *surface = NULL;
1195 if (shared_handle)
1196 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1198 if (pool == D3DPOOL_MANAGED)
1200 WARN("Attempting to create a managed offscreen plain surface.\n");
1201 return D3DERR_INVALIDCALL;
1203 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1204 * regardless of the pool they're created in. Should we set dynamic usage
1205 * here? */
1206 return d3d9_device_create_surface(device, width, height, format,
1207 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0);
1210 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1212 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1213 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1214 HRESULT hr;
1216 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1218 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1220 WARN("Invalid index %u specified.\n", idx);
1221 return D3DERR_INVALIDCALL;
1224 if (!idx && !surface_impl)
1226 WARN("Trying to set render target 0 to NULL.\n");
1227 return D3DERR_INVALIDCALL;
1230 wined3d_mutex_lock();
1231 hr = wined3d_device_set_render_target(device->wined3d_device, idx,
1232 surface_impl ? surface_impl->wined3d_surface : NULL, TRUE);
1233 wined3d_mutex_unlock();
1235 return hr;
1238 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1240 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1241 struct wined3d_surface *wined3d_surface;
1242 struct d3d9_surface *surface_impl;
1243 HRESULT hr = D3D_OK;
1245 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1247 if (!surface)
1248 return D3DERR_INVALIDCALL;
1250 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1252 WARN("Invalid index %u specified.\n", idx);
1253 return D3DERR_INVALIDCALL;
1256 wined3d_mutex_lock();
1257 if ((wined3d_surface = wined3d_device_get_render_target(device->wined3d_device, idx)))
1259 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1260 *surface = &surface_impl->IDirect3DSurface9_iface;
1261 IDirect3DSurface9_AddRef(*surface);
1263 else
1265 hr = WINED3DERR_NOTFOUND;
1266 *surface = NULL;
1268 wined3d_mutex_unlock();
1270 return hr;
1273 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1275 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1276 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1278 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1280 wined3d_mutex_lock();
1281 wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1282 wined3d_mutex_unlock();
1284 return D3D_OK;
1287 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1289 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1290 struct wined3d_surface *wined3d_surface;
1291 struct d3d9_surface *surface_impl;
1292 HRESULT hr = D3D_OK;
1294 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1296 if (!depth_stencil)
1297 return D3DERR_INVALIDCALL;
1299 wined3d_mutex_lock();
1300 if ((wined3d_surface = wined3d_device_get_depth_stencil(device->wined3d_device)))
1302 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1303 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1304 IDirect3DSurface9_AddRef(*depth_stencil);
1306 else
1308 hr = WINED3DERR_NOTFOUND;
1309 *depth_stencil = NULL;
1311 wined3d_mutex_unlock();
1313 return hr;
1316 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1318 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1319 HRESULT hr;
1321 TRACE("iface %p.\n", iface);
1323 wined3d_mutex_lock();
1324 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1325 device->in_scene = TRUE;
1326 wined3d_mutex_unlock();
1328 return hr;
1331 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1333 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1334 HRESULT hr;
1336 TRACE("iface %p.\n", iface);
1338 wined3d_mutex_lock();
1339 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1340 device->in_scene = FALSE;
1341 wined3d_mutex_unlock();
1343 return hr;
1346 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1347 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1349 const struct wined3d_color c =
1351 ((color >> 16) & 0xff) / 255.0f,
1352 ((color >> 8) & 0xff) / 255.0f,
1353 (color & 0xff) / 255.0f,
1354 ((color >> 24) & 0xff) / 255.0f,
1356 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1357 HRESULT hr;
1359 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1360 iface, rect_count, rects, flags, color, z, stencil);
1362 wined3d_mutex_lock();
1363 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1364 wined3d_mutex_unlock();
1366 return hr;
1369 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1370 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1372 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1374 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1376 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1377 wined3d_mutex_lock();
1378 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1379 wined3d_mutex_unlock();
1381 return D3D_OK;
1384 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1385 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1387 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1389 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1391 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1392 wined3d_mutex_lock();
1393 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1394 wined3d_mutex_unlock();
1396 return D3D_OK;
1399 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1400 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1402 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1404 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1406 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1407 wined3d_mutex_lock();
1408 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1409 wined3d_mutex_unlock();
1411 return D3D_OK;
1414 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1416 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1418 TRACE("iface %p, viewport %p.\n", iface, viewport);
1420 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1421 wined3d_mutex_lock();
1422 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1423 wined3d_mutex_unlock();
1425 return D3D_OK;
1428 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1430 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1432 TRACE("iface %p, viewport %p.\n", iface, viewport);
1434 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1435 wined3d_mutex_lock();
1436 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1437 wined3d_mutex_unlock();
1439 return D3D_OK;
1442 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1444 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1446 TRACE("iface %p, material %p.\n", iface, material);
1448 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1449 wined3d_mutex_lock();
1450 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1451 wined3d_mutex_unlock();
1453 return D3D_OK;
1456 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1458 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1460 TRACE("iface %p, material %p.\n", iface, material);
1462 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1463 wined3d_mutex_lock();
1464 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1465 wined3d_mutex_unlock();
1467 return D3D_OK;
1470 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1472 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1473 HRESULT hr;
1475 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1477 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1478 wined3d_mutex_lock();
1479 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1480 wined3d_mutex_unlock();
1482 return hr;
1485 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1487 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1488 HRESULT hr;
1490 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1492 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1493 wined3d_mutex_lock();
1494 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1495 wined3d_mutex_unlock();
1497 return hr;
1500 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1502 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1503 HRESULT hr;
1505 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1507 wined3d_mutex_lock();
1508 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1509 wined3d_mutex_unlock();
1511 return hr;
1514 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1516 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1517 HRESULT hr;
1519 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1521 wined3d_mutex_lock();
1522 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1523 wined3d_mutex_unlock();
1525 return hr;
1528 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1530 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1531 HRESULT hr;
1533 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1535 wined3d_mutex_lock();
1536 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1537 wined3d_mutex_unlock();
1539 return hr;
1542 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1544 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1545 HRESULT hr;
1547 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1549 wined3d_mutex_lock();
1550 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1551 wined3d_mutex_unlock();
1553 return hr;
1556 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1557 D3DRENDERSTATETYPE state, DWORD value)
1559 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1561 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1563 wined3d_mutex_lock();
1564 wined3d_device_set_render_state(device->wined3d_device, state, value);
1565 wined3d_mutex_unlock();
1567 return D3D_OK;
1570 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1571 D3DRENDERSTATETYPE state, DWORD *value)
1573 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1575 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1577 wined3d_mutex_lock();
1578 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1579 wined3d_mutex_unlock();
1581 return D3D_OK;
1584 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1585 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1587 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1588 struct d3d9_stateblock *object;
1589 HRESULT hr;
1591 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1593 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1595 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1596 return D3DERR_INVALIDCALL;
1599 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1600 if (!object)
1601 return E_OUTOFMEMORY;
1603 hr = stateblock_init(object, device, type, NULL);
1604 if (FAILED(hr))
1606 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1607 HeapFree(GetProcessHeap(), 0, object);
1608 return hr;
1611 TRACE("Created stateblock %p.\n", object);
1612 *stateblock = &object->IDirect3DStateBlock9_iface;
1614 return D3D_OK;
1617 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1619 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1620 HRESULT hr;
1622 TRACE("iface %p.\n", iface);
1624 wined3d_mutex_lock();
1625 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1626 wined3d_mutex_unlock();
1628 return hr;
1631 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1633 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1634 struct wined3d_stateblock *wined3d_stateblock;
1635 struct d3d9_stateblock *object;
1636 HRESULT hr;
1638 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1640 wined3d_mutex_lock();
1641 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1642 wined3d_mutex_unlock();
1643 if (FAILED(hr))
1645 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1646 return hr;
1649 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1650 if (!object)
1652 wined3d_mutex_lock();
1653 wined3d_stateblock_decref(wined3d_stateblock);
1654 wined3d_mutex_unlock();
1655 return E_OUTOFMEMORY;
1658 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1659 if (FAILED(hr))
1661 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1662 wined3d_mutex_lock();
1663 wined3d_stateblock_decref(wined3d_stateblock);
1664 wined3d_mutex_unlock();
1665 HeapFree(GetProcessHeap(), 0, object);
1666 return hr;
1669 TRACE("Created stateblock %p.\n", object);
1670 *stateblock = &object->IDirect3DStateBlock9_iface;
1672 return D3D_OK;
1675 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1677 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1678 HRESULT hr;
1680 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1682 wined3d_mutex_lock();
1683 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1684 wined3d_mutex_unlock();
1686 return hr;
1689 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1691 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1692 HRESULT hr;
1694 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1696 wined3d_mutex_lock();
1697 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1698 wined3d_mutex_unlock();
1700 return hr;
1703 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1705 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1706 struct wined3d_texture *wined3d_texture = NULL;
1707 struct d3d9_texture *texture_impl;
1709 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1711 if (!texture)
1712 return D3DERR_INVALIDCALL;
1714 wined3d_mutex_lock();
1715 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1717 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1718 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1719 IDirect3DBaseTexture9_AddRef(*texture);
1721 else
1723 *texture = NULL;
1725 wined3d_mutex_unlock();
1727 return D3D_OK;
1730 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1733 struct d3d9_texture *texture_impl;
1734 HRESULT hr;
1736 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1738 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1740 wined3d_mutex_lock();
1741 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1742 texture_impl ? texture_impl->wined3d_texture : NULL);
1743 wined3d_mutex_unlock();
1745 return hr;
1748 static const enum wined3d_texture_stage_state tss_lookup[] =
1750 WINED3D_TSS_INVALID, /* 0, unused */
1751 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1752 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1753 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1754 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1755 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1756 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1757 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1758 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1759 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1760 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1761 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1762 WINED3D_TSS_INVALID, /* 12, unused */
1763 WINED3D_TSS_INVALID, /* 13, unused */
1764 WINED3D_TSS_INVALID, /* 14, unused */
1765 WINED3D_TSS_INVALID, /* 15, unused */
1766 WINED3D_TSS_INVALID, /* 16, unused */
1767 WINED3D_TSS_INVALID, /* 17, unused */
1768 WINED3D_TSS_INVALID, /* 18, unused */
1769 WINED3D_TSS_INVALID, /* 19, unused */
1770 WINED3D_TSS_INVALID, /* 20, unused */
1771 WINED3D_TSS_INVALID, /* 21, unused */
1772 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1773 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1774 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1775 WINED3D_TSS_INVALID, /* 25, unused */
1776 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1777 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1778 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1779 WINED3D_TSS_INVALID, /* 29, unused */
1780 WINED3D_TSS_INVALID, /* 30, unused */
1781 WINED3D_TSS_INVALID, /* 31, unused */
1782 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1785 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1786 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1788 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1790 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1792 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1794 WARN("Invalid state %#x passed.\n", state);
1795 return D3D_OK;
1798 wined3d_mutex_lock();
1799 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
1800 wined3d_mutex_unlock();
1802 return D3D_OK;
1805 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1806 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1808 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1810 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
1812 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1814 WARN("Invalid state %#x passed.\n", state);
1815 return D3D_OK;
1818 wined3d_mutex_lock();
1819 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1820 wined3d_mutex_unlock();
1822 return D3D_OK;
1825 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
1826 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
1828 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1830 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
1832 wined3d_mutex_lock();
1833 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
1834 wined3d_mutex_unlock();
1836 return D3D_OK;
1839 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
1840 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
1842 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1844 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
1846 wined3d_mutex_lock();
1847 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
1848 wined3d_mutex_unlock();
1850 return D3D_OK;
1853 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
1855 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1856 HRESULT hr;
1858 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1860 wined3d_mutex_lock();
1861 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1862 wined3d_mutex_unlock();
1864 return hr;
1867 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1868 UINT palette_idx, const PALETTEENTRY *entries)
1870 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1872 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
1873 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
1874 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
1876 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
1877 return D3D_OK;
1880 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1881 UINT palette_idx, PALETTEENTRY *entries)
1883 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1885 return D3DERR_INVALIDCALL;
1888 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
1890 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1892 return D3D_OK;
1895 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
1897 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
1899 return D3DERR_INVALIDCALL;
1902 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
1904 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1906 TRACE("iface %p, rect %p.\n", iface, rect);
1908 wined3d_mutex_lock();
1909 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
1910 wined3d_mutex_unlock();
1912 return D3D_OK;
1915 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
1917 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1919 TRACE("iface %p, rect %p.\n", iface, rect);
1921 wined3d_mutex_lock();
1922 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
1923 wined3d_mutex_unlock();
1925 return D3D_OK;
1928 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
1930 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1932 TRACE("iface %p, software %#x.\n", iface, software);
1934 wined3d_mutex_lock();
1935 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
1936 wined3d_mutex_unlock();
1938 return D3D_OK;
1941 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
1943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1944 BOOL ret;
1946 TRACE("iface %p.\n", iface);
1948 wined3d_mutex_lock();
1949 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
1950 wined3d_mutex_unlock();
1952 return ret;
1955 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
1957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1958 HRESULT hr;
1960 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
1962 wined3d_mutex_lock();
1963 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
1964 wined3d_mutex_unlock();
1966 return hr;
1969 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
1971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1972 float ret;
1974 TRACE("iface %p.\n", iface);
1976 wined3d_mutex_lock();
1977 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
1978 wined3d_mutex_unlock();
1980 return ret;
1983 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
1984 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1986 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1987 HRESULT hr;
1989 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1990 iface, primitive_type, start_vertex, primitive_count);
1992 wined3d_mutex_lock();
1993 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1994 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1995 vertex_count_from_primitive_count(primitive_type, primitive_count));
1996 wined3d_mutex_unlock();
1998 return hr;
2001 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2002 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2003 UINT vertex_count, UINT start_idx, UINT primitive_count)
2005 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2006 HRESULT hr;
2008 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2009 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2010 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2011 vertex_count, start_idx, primitive_count);
2013 wined3d_mutex_lock();
2014 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2015 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2016 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2017 vertex_count_from_primitive_count(primitive_type, primitive_count));
2018 wined3d_mutex_unlock();
2020 return hr;
2023 /* The caller is responsible for wined3d locking */
2024 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2026 HRESULT hr;
2028 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2030 UINT size = max(device->vertex_buffer_size * 2, min_size);
2031 struct wined3d_buffer *buffer;
2033 TRACE("Growing vertex buffer to %u bytes\n", size);
2035 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2036 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2037 if (FAILED(hr))
2039 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2040 return hr;
2043 if (device->vertex_buffer)
2044 wined3d_buffer_decref(device->vertex_buffer);
2046 device->vertex_buffer = buffer;
2047 device->vertex_buffer_size = size;
2048 device->vertex_buffer_pos = 0;
2050 return D3D_OK;
2053 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2054 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2056 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2057 HRESULT hr;
2058 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2059 UINT size = vtx_count * stride;
2060 UINT vb_pos, align;
2061 BYTE *buffer_data;
2063 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2064 iface, primitive_type, primitive_count, data, stride);
2066 if (!primitive_count)
2068 WARN("primitive_count is 0, returning D3D_OK\n");
2069 return D3D_OK;
2072 wined3d_mutex_lock();
2074 hr = d3d9_device_prepare_vertex_buffer(device, size);
2075 if (FAILED(hr))
2076 goto done;
2078 vb_pos = device->vertex_buffer_pos;
2079 align = vb_pos % stride;
2080 if (align) align = stride - align;
2081 if (vb_pos + size + align > device->vertex_buffer_size)
2082 vb_pos = 0;
2083 else
2084 vb_pos += align;
2086 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2087 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2088 if (FAILED(hr))
2089 goto done;
2090 memcpy(buffer_data, data, size);
2091 wined3d_buffer_unmap(device->vertex_buffer);
2092 device->vertex_buffer_pos = vb_pos + size;
2094 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2095 if (FAILED(hr))
2096 goto done;
2098 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2099 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2100 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2102 done:
2103 wined3d_mutex_unlock();
2104 return hr;
2107 /* The caller is responsible for wined3d locking */
2108 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2110 HRESULT hr;
2112 if (device->index_buffer_size < min_size || !device->index_buffer)
2114 UINT size = max(device->index_buffer_size * 2, min_size);
2115 struct wined3d_buffer *buffer;
2117 TRACE("Growing index buffer to %u bytes\n", size);
2119 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2120 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2121 if (FAILED(hr))
2123 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2124 return hr;
2127 if (device->index_buffer)
2128 wined3d_buffer_decref(device->index_buffer);
2130 device->index_buffer = buffer;
2131 device->index_buffer_size = size;
2132 device->index_buffer_pos = 0;
2134 return D3D_OK;
2137 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2138 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2139 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2140 const void *vertex_data, UINT vertex_stride)
2142 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2143 HRESULT hr;
2144 BYTE *buffer_data;
2146 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2147 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2148 UINT idx_size = idx_count * idx_fmt_size;
2149 UINT ib_pos;
2151 UINT vtx_size = vertex_count * vertex_stride;
2152 UINT vb_pos, align;
2154 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2155 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2156 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2157 index_data, index_format, vertex_data, vertex_stride);
2159 if (!primitive_count)
2161 WARN("primitive_count is 0, returning D3D_OK\n");
2162 return D3D_OK;
2165 wined3d_mutex_lock();
2167 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2168 if (FAILED(hr))
2169 goto done;
2171 vb_pos = device->vertex_buffer_pos;
2172 align = vb_pos % vertex_stride;
2173 if (align) align = vertex_stride - align;
2174 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2175 vb_pos = 0;
2176 else
2177 vb_pos += align;
2179 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2180 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2181 if (FAILED(hr))
2182 goto done;
2183 memcpy(buffer_data, vertex_data, vtx_size);
2184 wined3d_buffer_unmap(device->vertex_buffer);
2185 device->vertex_buffer_pos = vb_pos + vtx_size;
2187 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2188 if (FAILED(hr))
2189 goto done;
2191 ib_pos = device->index_buffer_pos;
2192 align = ib_pos % idx_fmt_size;
2193 if (align) align = idx_fmt_size - align;
2194 if (ib_pos + idx_size + align > device->index_buffer_size)
2195 ib_pos = 0;
2196 else
2197 ib_pos += align;
2199 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2200 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2201 if (FAILED(hr))
2202 goto done;
2203 memcpy(buffer_data, index_data, idx_size);
2204 wined3d_buffer_unmap(device->index_buffer);
2205 device->index_buffer_pos = ib_pos + idx_size;
2207 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2208 if (FAILED(hr))
2209 goto done;
2211 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2212 wined3dformat_from_d3dformat(index_format));
2213 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2215 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2216 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2218 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2219 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2220 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2222 done:
2223 wined3d_mutex_unlock();
2224 return hr;
2227 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2228 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2229 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2231 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2232 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2233 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2234 HRESULT hr;
2236 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2237 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2239 wined3d_mutex_lock();
2240 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2241 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2242 flags, dst_impl->fvf);
2243 wined3d_mutex_unlock();
2245 return hr;
2248 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2249 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2251 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2252 struct d3d9_vertex_declaration *object;
2253 HRESULT hr;
2255 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2257 if (!declaration)
2259 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2260 return D3DERR_INVALIDCALL;
2263 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2264 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2266 return hr;
2269 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2270 IDirect3DVertexDeclaration9 *declaration)
2272 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2273 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2275 TRACE("iface %p, declaration %p.\n", iface, declaration);
2277 wined3d_mutex_lock();
2278 wined3d_device_set_vertex_declaration(device->wined3d_device,
2279 decl_impl ? decl_impl->wined3d_declaration : NULL);
2280 wined3d_mutex_unlock();
2282 return D3D_OK;
2285 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2286 IDirect3DVertexDeclaration9 **declaration)
2288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2289 struct wined3d_vertex_declaration *wined3d_declaration;
2290 struct d3d9_vertex_declaration *declaration_impl;
2292 TRACE("iface %p, declaration %p.\n", iface, declaration);
2294 if (!declaration) return D3DERR_INVALIDCALL;
2296 wined3d_mutex_lock();
2297 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2299 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2300 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2301 IDirect3DVertexDeclaration9_AddRef(*declaration);
2303 else
2305 *declaration = NULL;
2307 wined3d_mutex_unlock();
2309 TRACE("Returning %p.\n", *declaration);
2310 return D3D_OK;
2313 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2315 struct wined3d_vertex_declaration *wined3d_declaration;
2316 struct fvf_declaration *fvf_decls = device->fvf_decls;
2317 struct d3d9_vertex_declaration *d3d9_declaration;
2318 D3DVERTEXELEMENT9 *elements;
2319 int p, low, high; /* deliberately signed */
2320 HRESULT hr;
2322 TRACE("Searching for declaration for fvf %08x... ", fvf);
2324 low = 0;
2325 high = device->fvf_decl_count - 1;
2326 while (low <= high)
2328 p = (low + high) >> 1;
2329 TRACE("%d ", p);
2331 if (fvf_decls[p].fvf == fvf)
2333 TRACE("found %p.\n", fvf_decls[p].decl);
2334 return fvf_decls[p].decl;
2337 if (fvf_decls[p].fvf < fvf)
2338 low = p + 1;
2339 else
2340 high = p - 1;
2342 TRACE("not found. Creating and inserting at position %d.\n", low);
2344 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2345 return NULL;
2347 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2348 HeapFree(GetProcessHeap(), 0, elements);
2349 if (FAILED(hr))
2350 return NULL;
2352 if (device->fvf_decl_size == device->fvf_decl_count)
2354 UINT grow = max(device->fvf_decl_size / 2, 8);
2356 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2357 if (!fvf_decls)
2359 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2360 return NULL;
2362 device->fvf_decls = fvf_decls;
2363 device->fvf_decl_size += grow;
2366 d3d9_declaration->fvf = fvf;
2367 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2368 wined3d_vertex_declaration_incref(wined3d_declaration);
2369 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2371 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2372 fvf_decls[low].decl = wined3d_declaration;
2373 fvf_decls[low].fvf = fvf;
2374 ++device->fvf_decl_count;
2376 TRACE("Returning %p. %u declatations in array.\n", wined3d_declaration, device->fvf_decl_count);
2378 return wined3d_declaration;
2381 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2383 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2384 struct wined3d_vertex_declaration *decl;
2386 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2388 if (!fvf)
2390 WARN("%#x is not a valid FVF.\n", fvf);
2391 return D3D_OK;
2394 wined3d_mutex_lock();
2395 if (!(decl = device_get_fvf_declaration(device, fvf)))
2397 wined3d_mutex_unlock();
2398 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2399 return D3DERR_DRIVERINTERNALERROR;
2402 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2403 wined3d_mutex_unlock();
2405 return D3D_OK;
2408 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2410 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2411 struct wined3d_vertex_declaration *wined3d_declaration;
2412 struct d3d9_vertex_declaration *d3d9_declaration;
2414 TRACE("iface %p, fvf %p.\n", iface, fvf);
2416 wined3d_mutex_lock();
2417 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2419 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2420 *fvf = d3d9_declaration->fvf;
2422 else
2424 *fvf = 0;
2426 wined3d_mutex_unlock();
2428 TRACE("Returning FVF %#x.\n", *fvf);
2430 return D3D_OK;
2433 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2434 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2436 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2437 struct d3d9_vertexshader *object;
2438 HRESULT hr;
2440 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2442 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2443 if (!object)
2444 return E_OUTOFMEMORY;
2446 hr = vertexshader_init(object, device, byte_code);
2447 if (FAILED(hr))
2449 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2450 HeapFree(GetProcessHeap(), 0, object);
2451 return hr;
2454 TRACE("Created vertex shader %p.\n", object);
2455 *shader = &object->IDirect3DVertexShader9_iface;
2457 return D3D_OK;
2460 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2462 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2463 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2465 TRACE("iface %p, shader %p.\n", iface, shader);
2467 wined3d_mutex_lock();
2468 wined3d_device_set_vertex_shader(device->wined3d_device,
2469 shader_obj ? shader_obj->wined3d_shader : NULL);
2470 wined3d_mutex_unlock();
2472 return D3D_OK;
2475 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2477 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2478 struct d3d9_vertexshader *shader_impl;
2479 struct wined3d_shader *wined3d_shader;
2481 TRACE("iface %p, shader %p.\n", iface, shader);
2483 wined3d_mutex_lock();
2484 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2486 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2487 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2488 IDirect3DVertexShader9_AddRef(*shader);
2490 else
2492 *shader = NULL;
2494 wined3d_mutex_unlock();
2496 TRACE("Returning %p.\n", *shader);
2498 return D3D_OK;
2501 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2502 UINT reg_idx, const float *data, UINT count)
2504 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2505 HRESULT hr;
2507 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2509 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2511 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2512 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2513 return D3DERR_INVALIDCALL;
2516 wined3d_mutex_lock();
2517 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2518 wined3d_mutex_unlock();
2520 return hr;
2523 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2524 UINT reg_idx, float *data, UINT count)
2526 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2527 HRESULT hr;
2529 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2531 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2533 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2534 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2535 return D3DERR_INVALIDCALL;
2538 wined3d_mutex_lock();
2539 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2540 wined3d_mutex_unlock();
2542 return hr;
2545 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2546 UINT reg_idx, const int *data, UINT count)
2548 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2549 HRESULT hr;
2551 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2553 wined3d_mutex_lock();
2554 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2555 wined3d_mutex_unlock();
2557 return hr;
2560 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2561 UINT reg_idx, int *data, UINT count)
2563 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2564 HRESULT hr;
2566 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2568 wined3d_mutex_lock();
2569 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2570 wined3d_mutex_unlock();
2572 return hr;
2575 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2576 UINT reg_idx, const BOOL *data, UINT count)
2578 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2579 HRESULT hr;
2581 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2583 wined3d_mutex_lock();
2584 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2585 wined3d_mutex_unlock();
2587 return hr;
2590 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2591 UINT reg_idx, BOOL *data, UINT count)
2593 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2594 HRESULT hr;
2596 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2598 wined3d_mutex_lock();
2599 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2600 wined3d_mutex_unlock();
2602 return hr;
2605 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2606 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2608 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2609 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2610 HRESULT hr;
2612 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2613 iface, stream_idx, buffer, offset, stride);
2615 wined3d_mutex_lock();
2616 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2617 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2618 wined3d_mutex_unlock();
2620 return hr;
2623 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2624 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2626 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2627 struct d3d9_vertexbuffer *buffer_impl;
2628 struct wined3d_buffer *wined3d_buffer;
2629 HRESULT hr;
2631 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2632 iface, stream_idx, buffer, offset, stride);
2634 if (!buffer)
2635 return D3DERR_INVALIDCALL;
2637 wined3d_mutex_lock();
2638 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2639 if (SUCCEEDED(hr) && wined3d_buffer)
2641 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2642 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2643 IDirect3DVertexBuffer9_AddRef(*buffer);
2644 wined3d_buffer_decref(wined3d_buffer);
2646 else
2648 if (FAILED(hr))
2649 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2650 *buffer = NULL;
2652 wined3d_mutex_unlock();
2654 return hr;
2657 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2659 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2660 HRESULT hr;
2662 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2664 wined3d_mutex_lock();
2665 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2666 wined3d_mutex_unlock();
2668 return hr;
2671 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2673 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2674 HRESULT hr;
2676 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2678 wined3d_mutex_lock();
2679 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2680 wined3d_mutex_unlock();
2682 return hr;
2685 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2687 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2688 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2690 TRACE("iface %p, buffer %p.\n", iface, buffer);
2692 wined3d_mutex_lock();
2693 wined3d_device_set_index_buffer(device->wined3d_device,
2694 ib ? ib->wined3d_buffer : NULL,
2695 ib ? ib->format : WINED3DFMT_UNKNOWN);
2696 wined3d_mutex_unlock();
2698 return D3D_OK;
2701 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2703 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2704 enum wined3d_format_id wined3d_format;
2705 struct wined3d_buffer *wined3d_buffer;
2706 struct d3d9_indexbuffer *buffer_impl;
2708 TRACE("iface %p, buffer %p.\n", iface, buffer);
2710 if (!buffer)
2711 return D3DERR_INVALIDCALL;
2713 wined3d_mutex_lock();
2714 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2716 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2717 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2718 IDirect3DIndexBuffer9_AddRef(*buffer);
2720 else
2722 *buffer = NULL;
2724 wined3d_mutex_unlock();
2726 return D3D_OK;
2729 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2730 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2733 struct d3d9_pixelshader *object;
2734 HRESULT hr;
2736 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2738 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2739 if (!object)
2741 FIXME("Failed to allocate pixel shader memory.\n");
2742 return E_OUTOFMEMORY;
2745 hr = pixelshader_init(object, device, byte_code);
2746 if (FAILED(hr))
2748 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2749 HeapFree(GetProcessHeap(), 0, object);
2750 return hr;
2753 TRACE("Created pixel shader %p.\n", object);
2754 *shader = &object->IDirect3DPixelShader9_iface;
2756 return D3D_OK;
2759 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2761 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2762 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2764 TRACE("iface %p, shader %p.\n", iface, shader);
2766 wined3d_mutex_lock();
2767 wined3d_device_set_pixel_shader(device->wined3d_device,
2768 shader_obj ? shader_obj->wined3d_shader : NULL);
2769 wined3d_mutex_unlock();
2771 return D3D_OK;
2774 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2776 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2777 struct d3d9_pixelshader *shader_impl;
2778 struct wined3d_shader *wined3d_shader;
2780 TRACE("iface %p, shader %p.\n", iface, shader);
2782 if (!shader) return D3DERR_INVALIDCALL;
2784 wined3d_mutex_lock();
2785 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2787 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2788 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2789 IDirect3DPixelShader9_AddRef(*shader);
2791 else
2793 *shader = NULL;
2795 wined3d_mutex_unlock();
2797 TRACE("Returning %p.\n", *shader);
2799 return D3D_OK;
2802 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2803 UINT reg_idx, const float *data, UINT count)
2805 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2806 HRESULT hr;
2808 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2810 wined3d_mutex_lock();
2811 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2812 wined3d_mutex_unlock();
2814 return hr;
2817 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2818 UINT reg_idx, float *data, UINT count)
2820 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2821 HRESULT hr;
2823 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2825 wined3d_mutex_lock();
2826 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2827 wined3d_mutex_unlock();
2829 return hr;
2832 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2833 UINT reg_idx, const int *data, UINT count)
2835 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2836 HRESULT hr;
2838 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2840 wined3d_mutex_lock();
2841 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2842 wined3d_mutex_unlock();
2844 return hr;
2847 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2848 UINT reg_idx, int *data, UINT count)
2850 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2851 HRESULT hr;
2853 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2855 wined3d_mutex_lock();
2856 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2857 wined3d_mutex_unlock();
2859 return hr;
2862 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2863 UINT reg_idx, const BOOL *data, UINT count)
2865 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2866 HRESULT hr;
2868 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2870 wined3d_mutex_lock();
2871 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2872 wined3d_mutex_unlock();
2874 return hr;
2877 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2878 UINT reg_idx, BOOL *data, UINT count)
2880 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2881 HRESULT hr;
2883 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2885 wined3d_mutex_lock();
2886 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2887 wined3d_mutex_unlock();
2889 return hr;
2892 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
2893 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2895 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2896 iface, handle, segment_count, patch_info);
2897 return D3D_OK;
2900 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
2901 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2903 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2904 iface, handle, segment_count, patch_info);
2905 return D3D_OK;
2908 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
2910 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2911 return D3DERR_INVALIDCALL;
2914 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
2916 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2917 struct d3d9_query *object;
2918 HRESULT hr;
2920 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2922 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2923 if (!object)
2924 return E_OUTOFMEMORY;
2926 hr = query_init(object, device, type);
2927 if (FAILED(hr))
2929 WARN("Failed to initialize query, hr %#x.\n", hr);
2930 HeapFree(GetProcessHeap(), 0, object);
2931 return hr;
2934 TRACE("Created query %p.\n", object);
2935 if (query) *query = &object->IDirect3DQuery9_iface;
2936 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
2938 return D3D_OK;
2941 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2942 UINT width, UINT height, float *rows, float *columns)
2944 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2945 iface, width, height, rows, columns);
2947 return E_NOTIMPL;
2950 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
2951 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2952 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2954 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2955 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2956 iface, src_surface, dst_surface, src_descs, rect_count,
2957 dst_descs, operation, offset_x, offset_y);
2959 return E_NOTIMPL;
2962 static HRESULT WINAPI d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
2963 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2964 const RGNDATA *dirty_region, DWORD flags)
2966 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2967 HRESULT hr;
2969 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
2970 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
2971 dst_window_override, dirty_region, flags);
2973 wined3d_mutex_lock();
2974 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
2975 dst_window_override, dirty_region, flags);
2976 wined3d_mutex_unlock();
2978 return hr;
2981 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2983 FIXME("iface %p, priority %p stub!\n", iface, priority);
2985 return E_NOTIMPL;
2988 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2990 FIXME("iface %p, priority %d stub!\n", iface, priority);
2992 return E_NOTIMPL;
2995 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2997 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2999 return E_NOTIMPL;
3002 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3003 IDirect3DResource9 **resources, UINT32 resource_count)
3005 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3006 iface, resources, resource_count);
3008 return E_NOTIMPL;
3011 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3013 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3015 return E_NOTIMPL;
3018 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3020 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3022 *max_latency = 2;
3024 return E_NOTIMPL;
3027 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3029 static int i;
3031 TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
3033 if (!i++)
3034 FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
3036 return D3D_OK;
3039 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3040 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3041 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3043 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3044 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3045 iface, width, height, format, multisample_type, multisample_quality,
3046 lockable, surface, shared_handle, usage);
3048 *surface = NULL;
3049 if (shared_handle)
3050 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3052 return E_NOTIMPL;
3055 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3056 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3057 HANDLE *shared_handle, DWORD usage)
3059 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3060 iface, width, height, format, pool, surface, shared_handle, usage);
3062 return E_NOTIMPL;
3065 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3066 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3067 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3069 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3070 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3071 iface, width, height, format, multisample_type, multisample_quality,
3072 discard, surface, shared_handle, usage);
3074 *surface = NULL;
3075 if (shared_handle)
3076 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3078 return E_NOTIMPL;
3081 static HRESULT WINAPI d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3082 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3084 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3085 struct wined3d_swapchain_desc swapchain_desc;
3086 struct wined3d_display_mode wined3d_mode;
3087 HRESULT hr;
3089 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3091 if (mode)
3093 wined3d_mode.width = mode->Width;
3094 wined3d_mode.height = mode->Height;
3095 wined3d_mode.refresh_rate = mode->RefreshRate;
3096 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
3097 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
3100 wined3d_mutex_lock();
3102 if (device->vertex_buffer)
3104 wined3d_buffer_decref(device->vertex_buffer);
3105 device->vertex_buffer = NULL;
3106 device->vertex_buffer_size = 0;
3108 if (device->index_buffer)
3110 wined3d_buffer_decref(device->index_buffer);
3111 device->index_buffer = NULL;
3112 device->index_buffer_size = 0;
3115 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
3116 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
3117 mode ? &wined3d_mode : NULL, reset_enum_callback, FALSE);
3118 wined3d_mutex_unlock();
3120 return hr;
3123 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3124 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3126 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3127 struct wined3d_display_mode wined3d_mode;
3128 HRESULT hr;
3130 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3131 iface, swapchain_idx, mode, rotation);
3133 if (mode->Size != sizeof(*mode))
3134 return D3DERR_INVALIDCALL;
3136 wined3d_mutex_lock();
3137 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3138 (enum wined3d_display_rotation *)rotation);
3139 wined3d_mutex_unlock();
3141 if (SUCCEEDED(hr))
3143 mode->Width = wined3d_mode.width;
3144 mode->Height = wined3d_mode.height;
3145 mode->RefreshRate = wined3d_mode.refresh_rate;
3146 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3147 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3150 return hr;
3153 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3155 /* IUnknown */
3156 d3d9_device_QueryInterface,
3157 d3d9_device_AddRef,
3158 d3d9_device_Release,
3159 /* IDirect3DDevice9 */
3160 d3d9_device_TestCooperativeLevel,
3161 d3d9_device_GetAvailableTextureMem,
3162 d3d9_device_EvictManagedResources,
3163 d3d9_device_GetDirect3D,
3164 d3d9_device_GetDeviceCaps,
3165 d3d9_device_GetDisplayMode,
3166 d3d9_device_GetCreationParameters,
3167 d3d9_device_SetCursorProperties,
3168 d3d9_device_SetCursorPosition,
3169 d3d9_device_ShowCursor,
3170 d3d9_device_CreateAdditionalSwapChain,
3171 d3d9_device_GetSwapChain,
3172 d3d9_device_GetNumberOfSwapChains,
3173 d3d9_device_Reset,
3174 d3d9_device_Present,
3175 d3d9_device_GetBackBuffer,
3176 d3d9_device_GetRasterStatus,
3177 d3d9_device_SetDialogBoxMode,
3178 d3d9_device_SetGammaRamp,
3179 d3d9_device_GetGammaRamp,
3180 d3d9_device_CreateTexture,
3181 d3d9_device_CreateVolumeTexture,
3182 d3d9_device_CreateCubeTexture,
3183 d3d9_device_CreateVertexBuffer,
3184 d3d9_device_CreateIndexBuffer,
3185 d3d9_device_CreateRenderTarget,
3186 d3d9_device_CreateDepthStencilSurface,
3187 d3d9_device_UpdateSurface,
3188 d3d9_device_UpdateTexture,
3189 d3d9_device_GetRenderTargetData,
3190 d3d9_device_GetFrontBufferData,
3191 d3d9_device_StretchRect,
3192 d3d9_device_ColorFill,
3193 d3d9_device_CreateOffscreenPlainSurface,
3194 d3d9_device_SetRenderTarget,
3195 d3d9_device_GetRenderTarget,
3196 d3d9_device_SetDepthStencilSurface,
3197 d3d9_device_GetDepthStencilSurface,
3198 d3d9_device_BeginScene,
3199 d3d9_device_EndScene,
3200 d3d9_device_Clear,
3201 d3d9_device_SetTransform,
3202 d3d9_device_GetTransform,
3203 d3d9_device_MultiplyTransform,
3204 d3d9_device_SetViewport,
3205 d3d9_device_GetViewport,
3206 d3d9_device_SetMaterial,
3207 d3d9_device_GetMaterial,
3208 d3d9_device_SetLight,
3209 d3d9_device_GetLight,
3210 d3d9_device_LightEnable,
3211 d3d9_device_GetLightEnable,
3212 d3d9_device_SetClipPlane,
3213 d3d9_device_GetClipPlane,
3214 d3d9_device_SetRenderState,
3215 d3d9_device_GetRenderState,
3216 d3d9_device_CreateStateBlock,
3217 d3d9_device_BeginStateBlock,
3218 d3d9_device_EndStateBlock,
3219 d3d9_device_SetClipStatus,
3220 d3d9_device_GetClipStatus,
3221 d3d9_device_GetTexture,
3222 d3d9_device_SetTexture,
3223 d3d9_device_GetTextureStageState,
3224 d3d9_device_SetTextureStageState,
3225 d3d9_device_GetSamplerState,
3226 d3d9_device_SetSamplerState,
3227 d3d9_device_ValidateDevice,
3228 d3d9_device_SetPaletteEntries,
3229 d3d9_device_GetPaletteEntries,
3230 d3d9_device_SetCurrentTexturePalette,
3231 d3d9_device_GetCurrentTexturePalette,
3232 d3d9_device_SetScissorRect,
3233 d3d9_device_GetScissorRect,
3234 d3d9_device_SetSoftwareVertexProcessing,
3235 d3d9_device_GetSoftwareVertexProcessing,
3236 d3d9_device_SetNPatchMode,
3237 d3d9_device_GetNPatchMode,
3238 d3d9_device_DrawPrimitive,
3239 d3d9_device_DrawIndexedPrimitive,
3240 d3d9_device_DrawPrimitiveUP,
3241 d3d9_device_DrawIndexedPrimitiveUP,
3242 d3d9_device_ProcessVertices,
3243 d3d9_device_CreateVertexDeclaration,
3244 d3d9_device_SetVertexDeclaration,
3245 d3d9_device_GetVertexDeclaration,
3246 d3d9_device_SetFVF,
3247 d3d9_device_GetFVF,
3248 d3d9_device_CreateVertexShader,
3249 d3d9_device_SetVertexShader,
3250 d3d9_device_GetVertexShader,
3251 d3d9_device_SetVertexShaderConstantF,
3252 d3d9_device_GetVertexShaderConstantF,
3253 d3d9_device_SetVertexShaderConstantI,
3254 d3d9_device_GetVertexShaderConstantI,
3255 d3d9_device_SetVertexShaderConstantB,
3256 d3d9_device_GetVertexShaderConstantB,
3257 d3d9_device_SetStreamSource,
3258 d3d9_device_GetStreamSource,
3259 d3d9_device_SetStreamSourceFreq,
3260 d3d9_device_GetStreamSourceFreq,
3261 d3d9_device_SetIndices,
3262 d3d9_device_GetIndices,
3263 d3d9_device_CreatePixelShader,
3264 d3d9_device_SetPixelShader,
3265 d3d9_device_GetPixelShader,
3266 d3d9_device_SetPixelShaderConstantF,
3267 d3d9_device_GetPixelShaderConstantF,
3268 d3d9_device_SetPixelShaderConstantI,
3269 d3d9_device_GetPixelShaderConstantI,
3270 d3d9_device_SetPixelShaderConstantB,
3271 d3d9_device_GetPixelShaderConstantB,
3272 d3d9_device_DrawRectPatch,
3273 d3d9_device_DrawTriPatch,
3274 d3d9_device_DeletePatch,
3275 d3d9_device_CreateQuery,
3276 /* IDirect3DDevice9Ex */
3277 d3d9_device_SetConvolutionMonoKernel,
3278 d3d9_device_ComposeRects,
3279 d3d9_device_PresentEx,
3280 d3d9_device_GetGPUThreadPriority,
3281 d3d9_device_SetGPUThreadPriority,
3282 d3d9_device_WaitForVBlank,
3283 d3d9_device_CheckResourceResidency,
3284 d3d9_device_SetMaximumFrameLatency,
3285 d3d9_device_GetMaximumFrameLatency,
3286 d3d9_device_CheckDeviceState,
3287 d3d9_device_CreateRenderTargetEx,
3288 d3d9_device_CreateOffscreenPlainSurfaceEx,
3289 d3d9_device_CreateDepthStencilSurfaceEx,
3290 d3d9_device_ResetEx,
3291 d3d9_device_GetDisplayModeEx,
3294 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3296 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3299 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3300 struct wined3d_device *device)
3302 TRACE("device_parent %p, device %p.\n", device_parent, device);
3305 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3307 TRACE("device_parent %p.\n", device_parent);
3310 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3311 void *container_parent, struct wined3d_surface *surface, void **parent,
3312 const struct wined3d_parent_ops **parent_ops)
3314 struct d3d9_device *device = device_from_device_parent(device_parent);
3315 struct d3d9_surface *d3d_surface;
3317 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3318 device_parent, container_parent, surface, parent, parent_ops);
3320 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3322 FIXME("Failed to allocate surface memory.\n");
3323 return D3DERR_OUTOFVIDEOMEMORY;
3326 surface_init(d3d_surface, surface, device, parent_ops);
3327 *parent = d3d_surface;
3328 TRACE("Created surface %p.\n", d3d_surface);
3330 d3d_surface->container = container_parent;
3331 IDirect3DDevice9Ex_Release(d3d_surface->parent_device);
3332 d3d_surface->parent_device = NULL;
3334 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3335 d3d_surface->forwardReference = container_parent;
3337 return D3D_OK;
3340 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3341 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3343 struct d3d9_device *device = device_from_device_parent(device_parent);
3344 struct wined3d_resource_desc texture_desc;
3345 struct d3d9_surface *d3d_surface;
3346 struct wined3d_texture *texture;
3347 HRESULT hr;
3349 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3350 device_parent, container_parent, desc, surface);
3352 if (container_parent == device_parent)
3353 container_parent = &device->IDirect3DDevice9Ex_iface;
3355 texture_desc = *desc;
3356 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3357 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3358 WINED3D_SURFACE_MAPPABLE, container_parent, &d3d9_null_wined3d_parent_ops, &texture)))
3360 WARN("Failed to create texture, hr %#x.\n", hr);
3361 return hr;
3364 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3365 wined3d_surface_incref(*surface);
3366 wined3d_texture_decref(texture);
3368 d3d_surface = wined3d_surface_get_parent(*surface);
3369 d3d_surface->forwardReference = NULL;
3370 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3372 return hr;
3375 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
3376 void *container_parent, UINT width, UINT height, UINT depth, UINT level,
3377 enum wined3d_format_id format, enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
3379 struct d3d9_device *device = device_from_device_parent(device_parent);
3380 struct d3d9_volume *object;
3381 HRESULT hr;
3383 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
3384 "format %#x, pool %#x, usage %#x, volume %p\n",
3385 device_parent, container_parent, width, height, depth,
3386 format, pool, usage, volume);
3388 /* Allocate the storage for the device */
3389 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3390 if (!object)
3392 FIXME("Allocation of memory failed\n");
3393 *volume = NULL;
3394 return D3DERR_OUTOFVIDEOMEMORY;
3397 hr = volume_init(object, device, width, height, depth, level, usage, format, pool);
3398 if (FAILED(hr))
3400 WARN("Failed to initialize volume, hr %#x.\n", hr);
3401 HeapFree(GetProcessHeap(), 0, object);
3402 return hr;
3405 *volume = object->wined3d_volume;
3406 wined3d_volume_incref(*volume);
3407 IDirect3DVolume9_Release(&object->IDirect3DVolume9_iface);
3409 object->container = container_parent;
3410 object->forwardReference = container_parent;
3412 TRACE("Created volume %p.\n", object);
3414 return hr;
3417 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3418 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3420 struct d3d9_device *device = device_from_device_parent(device_parent);
3421 struct d3d9_swapchain *d3d_swapchain;
3422 HRESULT hr;
3424 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3426 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3427 if (FAILED(hr))
3429 WARN("Failed to create swapchain, hr %#x.\n", hr);
3430 *swapchain = NULL;
3431 return hr;
3434 *swapchain = d3d_swapchain->wined3d_swapchain;
3435 wined3d_swapchain_incref(*swapchain);
3436 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3438 return hr;
3441 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3443 device_parent_wined3d_device_created,
3444 device_parent_mode_changed,
3445 device_parent_surface_created,
3446 device_parent_create_swapchain_surface,
3447 device_parent_create_volume,
3448 device_parent_create_swapchain,
3451 static void setup_fpu(void)
3453 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3454 WORD cw;
3455 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3456 cw = (cw & ~0xf3f) | 0x3f;
3457 __asm__ volatile ("fldcw %0" : : "m" (cw));
3458 #elif defined(__i386__) && defined(_MSC_VER)
3459 WORD cw;
3460 __asm fnstcw cw;
3461 cw = (cw & ~0xf3f) | 0x3f;
3462 __asm fldcw cw;
3463 #else
3464 FIXME("FPU setup not implemented for this platform.\n");
3465 #endif
3468 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3469 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3470 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3472 struct wined3d_swapchain_desc *swapchain_desc;
3473 UINT i, count = 1;
3474 HRESULT hr;
3476 if (mode)
3477 FIXME("Ignoring display mode.\n");
3479 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3480 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3481 device->refcount = 1;
3483 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3485 wined3d_mutex_lock();
3486 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3487 &device->device_parent, &device->wined3d_device);
3488 if (FAILED(hr))
3490 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3491 wined3d_mutex_unlock();
3492 return hr;
3495 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3497 WINED3DCAPS caps;
3499 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3500 count = caps.NumberOfAdaptersInGroup;
3503 if (flags & D3DCREATE_MULTITHREADED)
3504 wined3d_device_set_multithreaded(device->wined3d_device);
3506 if (!parameters->Windowed)
3508 if (!focus_window)
3509 focus_window = parameters->hDeviceWindow;
3510 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3512 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3513 wined3d_device_decref(device->wined3d_device);
3514 wined3d_mutex_unlock();
3515 return hr;
3518 for (i = 0; i < count; ++i)
3520 HWND device_window = parameters[i].hDeviceWindow;
3522 if (!device_window) device_window = focus_window;
3523 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3524 parameters[i].BackBufferWidth,
3525 parameters[i].BackBufferHeight);
3529 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3530 if (!swapchain_desc)
3532 ERR("Failed to allocate wined3d parameters.\n");
3533 wined3d_device_decref(device->wined3d_device);
3534 wined3d_mutex_unlock();
3535 return E_OUTOFMEMORY;
3538 for (i = 0; i < count; ++i)
3540 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i]);
3543 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3544 if (FAILED(hr))
3546 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3547 wined3d_device_release_focus_window(device->wined3d_device);
3548 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3549 wined3d_device_decref(device->wined3d_device);
3550 wined3d_mutex_unlock();
3551 return hr;
3554 wined3d_mutex_unlock();
3556 for (i = 0; i < count; ++i)
3558 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3560 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3562 /* Initialize the converted declaration array. This creates a valid pointer
3563 * and when adding decls HeapReAlloc() can be used without further checking. */
3564 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3565 if (!device->fvf_decls)
3567 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3568 wined3d_mutex_lock();
3569 wined3d_device_uninit_3d(device->wined3d_device);
3570 wined3d_device_release_focus_window(device->wined3d_device);
3571 wined3d_device_decref(device->wined3d_device);
3572 wined3d_mutex_unlock();
3573 return E_OUTOFMEMORY;
3576 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3577 device->d3d_parent = parent;
3579 return D3D_OK;