wined3d: Don't enumerate sub-resources in wined3d_device_reset().
[wine.git] / dlls / d3d9 / device.c
blobf6bf66498d07cff5a7b189997e620ec77522a107
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 BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
212 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
214 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
215 UINT highest_bb_count = extended ? 30 : 3;
217 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
219 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
220 return FALSE;
222 if (present_parameters->BackBufferCount > highest_bb_count
223 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
224 && present_parameters->BackBufferCount > 1))
226 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
227 return FALSE;
230 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
231 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
232 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
233 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
234 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
235 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
236 swapchain_desc->swap_effect = present_parameters->SwapEffect;
237 swapchain_desc->device_window = present_parameters->hDeviceWindow;
238 swapchain_desc->windowed = present_parameters->Windowed;
239 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
240 swapchain_desc->auto_depth_stencil_format
241 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
242 swapchain_desc->flags = present_parameters->Flags;
243 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
244 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
245 swapchain_desc->auto_restore_display_mode = TRUE;
247 return TRUE;
250 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
252 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
254 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
255 || IsEqualGUID(riid, &IID_IUnknown))
257 IDirect3DDevice9Ex_AddRef(iface);
258 *out = iface;
259 return S_OK;
262 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
264 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
266 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
267 * It doesn't matter with which function the device was created. */
268 if (!device->d3d_parent->extended)
270 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
271 *out = NULL;
272 return E_NOINTERFACE;
275 IDirect3DDevice9Ex_AddRef(iface);
276 *out = iface;
277 return S_OK;
280 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
282 *out = NULL;
283 return E_NOINTERFACE;
286 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
289 ULONG refcount = InterlockedIncrement(&device->refcount);
291 TRACE("%p increasing refcount to %u.\n", iface, refcount);
293 return refcount;
296 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
298 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
299 ULONG refcount;
301 if (device->in_destruction)
302 return 0;
304 refcount = InterlockedDecrement(&device->refcount);
306 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
308 if (!refcount)
310 unsigned i;
311 device->in_destruction = TRUE;
313 wined3d_mutex_lock();
314 for (i = 0; i < device->fvf_decl_count; ++i)
316 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
318 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
320 if (device->vertex_buffer)
321 wined3d_buffer_decref(device->vertex_buffer);
322 if (device->index_buffer)
323 wined3d_buffer_decref(device->index_buffer);
325 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
327 wined3d_device_uninit_3d(device->wined3d_device);
328 wined3d_device_release_focus_window(device->wined3d_device);
329 wined3d_device_decref(device->wined3d_device);
330 wined3d_mutex_unlock();
332 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
334 HeapFree(GetProcessHeap(), 0, device);
337 return refcount;
340 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
342 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
344 TRACE("iface %p.\n", iface);
346 TRACE("device state: %#x.\n", device->device_state);
348 if (device->d3d_parent->extended)
349 return D3D_OK;
351 switch (device->device_state)
353 default:
354 case D3D9_DEVICE_STATE_OK:
355 return D3D_OK;
356 case D3D9_DEVICE_STATE_LOST:
357 return D3DERR_DEVICELOST;
358 case D3D9_DEVICE_STATE_NOT_RESET:
359 return D3DERR_DEVICENOTRESET;
363 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
365 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
366 UINT ret;
368 TRACE("iface %p.\n", iface);
370 wined3d_mutex_lock();
371 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
372 wined3d_mutex_unlock();
374 return ret;
377 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
379 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
381 TRACE("iface %p.\n", iface);
383 wined3d_mutex_lock();
384 wined3d_device_evict_managed_resources(device->wined3d_device);
385 wined3d_mutex_unlock();
387 return D3D_OK;
390 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
392 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
394 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
396 if (!d3d9)
397 return D3DERR_INVALIDCALL;
399 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
402 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
404 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
405 WINED3DCAPS *wined3d_caps;
406 HRESULT hr;
408 TRACE("iface %p, caps %p.\n", iface, caps);
410 if (!caps)
411 return D3DERR_INVALIDCALL;
413 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
414 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
416 memset(caps, 0, sizeof(*caps));
418 wined3d_mutex_lock();
419 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
420 wined3d_mutex_unlock();
422 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
423 HeapFree(GetProcessHeap(), 0, wined3d_caps);
425 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
426 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
428 filter_caps(caps);
430 return hr;
433 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
435 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
436 struct wined3d_display_mode wined3d_mode;
437 HRESULT hr;
439 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
441 wined3d_mutex_lock();
442 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
443 wined3d_mutex_unlock();
445 if (SUCCEEDED(hr))
447 mode->Width = wined3d_mode.width;
448 mode->Height = wined3d_mode.height;
449 mode->RefreshRate = wined3d_mode.refresh_rate;
450 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
453 return hr;
456 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
457 D3DDEVICE_CREATION_PARAMETERS *parameters)
459 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
461 TRACE("iface %p, parameters %p.\n", iface, parameters);
463 wined3d_mutex_lock();
464 wined3d_device_get_creation_parameters(device->wined3d_device,
465 (struct wined3d_device_creation_parameters *)parameters);
466 wined3d_mutex_unlock();
468 return D3D_OK;
471 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
472 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
474 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
475 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
476 HRESULT hr;
478 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
479 iface, hotspot_x, hotspot_y, bitmap);
481 if (!bitmap)
483 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
484 return D3DERR_INVALIDCALL;
487 wined3d_mutex_lock();
488 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
489 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
490 wined3d_mutex_unlock();
492 return hr;
495 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
497 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
499 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
501 wined3d_mutex_lock();
502 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
503 wined3d_mutex_unlock();
506 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
508 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
509 BOOL ret;
511 TRACE("iface %p, show %#x.\n", iface, show);
513 wined3d_mutex_lock();
514 ret = wined3d_device_show_cursor(device->wined3d_device, show);
515 wined3d_mutex_unlock();
517 return ret;
520 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
521 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
523 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
524 struct wined3d_swapchain_desc desc;
525 struct d3d9_swapchain *object;
526 UINT i, count;
527 HRESULT hr;
529 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
530 iface, present_parameters, swapchain);
532 if (!present_parameters->Windowed)
534 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
535 return D3DERR_INVALIDCALL;
538 wined3d_mutex_lock();
539 count = wined3d_device_get_swapchain_count(device->wined3d_device);
540 for (i = 0; i < count; ++i)
542 struct wined3d_swapchain *wined3d_swapchain;
544 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
545 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
547 if (!desc.windowed)
549 wined3d_mutex_unlock();
550 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
551 return D3DERR_INVALIDCALL;
554 wined3d_mutex_unlock();
556 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
557 device->d3d_parent->extended))
558 return D3DERR_INVALIDCALL;
559 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
560 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
561 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
563 return hr;
566 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
567 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
570 HRESULT hr;
572 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
574 wined3d_mutex_lock();
575 if (swapchain_idx < device->implicit_swapchain_count)
577 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
578 IDirect3DSwapChain9Ex_AddRef(*swapchain);
579 hr = D3D_OK;
581 else
583 *swapchain = NULL;
584 hr = D3DERR_INVALIDCALL;
586 wined3d_mutex_unlock();
588 return hr;
591 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
593 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
594 UINT count;
596 TRACE("iface %p.\n", iface);
598 wined3d_mutex_lock();
599 count = wined3d_device_get_swapchain_count(device->wined3d_device);
600 wined3d_mutex_unlock();
602 return count;
605 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
607 struct wined3d_resource_desc desc;
608 IDirect3DBaseTexture9 *texture;
609 struct d3d9_surface *surface;
610 IUnknown *parent;
612 wined3d_resource_get_desc(resource, &desc);
613 if (desc.pool != WINED3D_POOL_DEFAULT)
614 return D3D_OK;
616 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
618 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
619 return D3DERR_INVALIDCALL;
622 parent = wined3d_resource_get_parent(resource);
623 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
625 IDirect3DBaseTexture9_Release(texture);
626 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
627 return D3DERR_INVALIDCALL;
630 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
631 if (!surface->resource.refcount)
632 return D3D_OK;
634 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
635 return D3DERR_INVALIDCALL;
638 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
640 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
641 struct wined3d_swapchain *wined3d_swapchain;
643 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
644 new_swapchain_count * sizeof(*device->implicit_swapchains))))
645 return E_OUTOFMEMORY;
647 for (i = 0; i < new_swapchain_count; ++i)
649 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
650 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
652 device->implicit_swapchain_count = new_swapchain_count;
654 return D3D_OK;
657 static HRESULT d3d9_device_reset(struct d3d9_device *device,
658 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
660 struct wined3d_swapchain_desc swapchain_desc;
661 struct wined3d_display_mode wined3d_mode;
662 HRESULT hr;
664 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
666 WARN("App not active, returning D3DERR_DEVICELOST.\n");
667 return D3DERR_DEVICELOST;
670 if (mode)
672 wined3d_mode.width = mode->Width;
673 wined3d_mode.height = mode->Height;
674 wined3d_mode.refresh_rate = mode->RefreshRate;
675 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
676 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
679 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
680 device->d3d_parent->extended))
681 return D3DERR_INVALIDCALL;
683 wined3d_mutex_lock();
685 if (device->vertex_buffer)
687 wined3d_buffer_decref(device->vertex_buffer);
688 device->vertex_buffer = NULL;
689 device->vertex_buffer_size = 0;
692 if (device->index_buffer)
694 wined3d_buffer_decref(device->index_buffer);
695 device->index_buffer = NULL;
696 device->index_buffer_size = 0;
699 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
700 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
702 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
704 if (FAILED(hr = d3d9_device_get_swapchains(device)))
706 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
708 else
710 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
711 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
712 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
713 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
714 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
716 device->device_state = D3D9_DEVICE_STATE_OK;
719 else if (!device->d3d_parent->extended)
721 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
724 wined3d_mutex_unlock();
726 return hr;
729 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
730 D3DPRESENT_PARAMETERS *present_parameters)
732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
734 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
736 return d3d9_device_reset(device, present_parameters, NULL);
739 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
740 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
742 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
743 UINT i;
744 HRESULT hr;
746 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
747 iface, src_rect, dst_rect, dst_window_override, dirty_region);
749 if (device->device_state != D3D9_DEVICE_STATE_OK)
750 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
752 if (dirty_region)
753 FIXME("Ignoring dirty_region %p.\n", dirty_region);
755 wined3d_mutex_lock();
756 for (i = 0; i < device->implicit_swapchain_count; ++i)
758 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
759 src_rect, dst_rect, dst_window_override, 0)))
761 wined3d_mutex_unlock();
762 return hr;
765 wined3d_mutex_unlock();
767 return D3D_OK;
770 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
771 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
773 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
774 HRESULT hr;
776 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
777 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
779 /* backbuffer_type is ignored by native. */
781 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
782 *backbuffer = NULL;
784 wined3d_mutex_lock();
785 if (swapchain >= device->implicit_swapchain_count)
787 wined3d_mutex_unlock();
788 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
789 return D3DERR_INVALIDCALL;
792 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
793 backbuffer_idx, backbuffer_type, backbuffer);
794 wined3d_mutex_unlock();
796 return hr;
799 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
800 UINT swapchain, D3DRASTER_STATUS *raster_status)
802 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
803 HRESULT hr;
805 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
807 wined3d_mutex_lock();
808 hr = wined3d_device_get_raster_status(device->wined3d_device,
809 swapchain, (struct wined3d_raster_status *)raster_status);
810 wined3d_mutex_unlock();
812 return hr;
815 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
817 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
818 HRESULT hr;
820 TRACE("iface %p, enable %#x.\n", iface, enable);
822 wined3d_mutex_lock();
823 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
824 wined3d_mutex_unlock();
826 return hr;
829 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
830 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
832 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
834 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
836 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
837 wined3d_mutex_lock();
838 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
839 wined3d_mutex_unlock();
842 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
844 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
846 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
848 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
849 wined3d_mutex_lock();
850 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
851 wined3d_mutex_unlock();
854 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
855 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
856 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
858 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
859 struct d3d9_texture *object;
860 BOOL set_mem = FALSE;
861 HRESULT hr;
863 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
864 iface, width, height, levels, usage, format, pool, texture, shared_handle);
866 *texture = NULL;
867 if (shared_handle)
869 if (!device->d3d_parent->extended)
871 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
872 return E_NOTIMPL;
875 if (pool == D3DPOOL_SYSTEMMEM)
877 if (levels != 1)
878 return D3DERR_INVALIDCALL;
879 set_mem = TRUE;
881 else
883 if (pool != D3DPOOL_DEFAULT)
885 WARN("Trying to create a shared texture in pool %#x.\n", pool);
886 return D3DERR_INVALIDCALL;
888 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
892 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
893 if (!object)
894 return D3DERR_OUTOFVIDEOMEMORY;
896 hr = texture_init(object, device, width, height, levels, usage, format, pool);
897 if (FAILED(hr))
899 WARN("Failed to initialize texture, hr %#x.\n", hr);
900 HeapFree(GetProcessHeap(), 0, object);
901 return hr;
904 if (set_mem)
905 wined3d_texture_update_desc(object->wined3d_texture, width, height,
906 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
907 *shared_handle, 0);
909 TRACE("Created texture %p.\n", object);
910 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
912 return D3D_OK;
915 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
916 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
917 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
919 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
920 struct d3d9_texture *object;
921 HRESULT hr;
923 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
924 iface, width, height, depth, levels);
925 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
926 usage, format, pool, texture, shared_handle);
928 *texture = NULL;
929 if (shared_handle)
931 if (!device->d3d_parent->extended)
933 WARN("Trying to create a shared volume texture on a non-ex device.\n");
934 return E_NOTIMPL;
937 if (pool != D3DPOOL_DEFAULT)
939 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
940 return D3DERR_INVALIDCALL;
942 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
945 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
946 if (!object)
947 return D3DERR_OUTOFVIDEOMEMORY;
949 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
950 if (FAILED(hr))
952 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
953 HeapFree(GetProcessHeap(), 0, object);
954 return hr;
957 TRACE("Created volume texture %p.\n", object);
958 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
960 return D3D_OK;
963 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
964 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
965 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
967 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
968 struct d3d9_texture *object;
969 HRESULT hr;
971 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
972 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
974 *texture = NULL;
975 if (shared_handle)
977 if (!device->d3d_parent->extended)
979 WARN("Trying to create a shared cube texture on a non-ex device.\n");
980 return E_NOTIMPL;
983 if (pool != D3DPOOL_DEFAULT)
985 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
986 return D3DERR_INVALIDCALL;
988 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
991 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
992 if (!object)
993 return D3DERR_OUTOFVIDEOMEMORY;
995 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
996 if (FAILED(hr))
998 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
999 HeapFree(GetProcessHeap(), 0, object);
1000 return hr;
1003 TRACE("Created cube texture %p.\n", object);
1004 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1006 return D3D_OK;
1009 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1010 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1011 HANDLE *shared_handle)
1013 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1014 struct d3d9_vertexbuffer *object;
1015 HRESULT hr;
1017 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1018 iface, size, usage, fvf, pool, buffer, shared_handle);
1020 if (shared_handle)
1022 if (!device->d3d_parent->extended)
1024 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1025 return E_NOTIMPL;
1028 if (pool != D3DPOOL_DEFAULT)
1030 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1031 return D3DERR_NOTAVAILABLE;
1033 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1036 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1037 if (!object)
1038 return D3DERR_OUTOFVIDEOMEMORY;
1040 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1041 if (FAILED(hr))
1043 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1044 HeapFree(GetProcessHeap(), 0, object);
1045 return hr;
1048 TRACE("Created vertex buffer %p.\n", object);
1049 *buffer = &object->IDirect3DVertexBuffer9_iface;
1051 return D3D_OK;
1054 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1055 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1056 HANDLE *shared_handle)
1058 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1059 struct d3d9_indexbuffer *object;
1060 HRESULT hr;
1062 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1063 iface, size, usage, format, pool, buffer, shared_handle);
1065 if (shared_handle)
1067 if (!device->d3d_parent->extended)
1069 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1070 return E_NOTIMPL;
1073 if (pool != D3DPOOL_DEFAULT)
1075 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1076 return D3DERR_NOTAVAILABLE;
1078 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1081 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1082 if (!object)
1083 return D3DERR_OUTOFVIDEOMEMORY;
1085 hr = indexbuffer_init(object, device, size, usage, format, pool);
1086 if (FAILED(hr))
1088 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1089 HeapFree(GetProcessHeap(), 0, object);
1090 return hr;
1093 TRACE("Created index buffer %p.\n", object);
1094 *buffer = &object->IDirect3DIndexBuffer9_iface;
1096 return D3D_OK;
1099 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1100 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1101 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1103 struct wined3d_resource_desc desc;
1104 struct d3d9_surface *surface_impl;
1105 struct wined3d_texture *texture;
1106 HRESULT hr;
1108 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1109 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1110 device, width, height, format, flags, surface, usage, pool,
1111 multisample_type, multisample_quality);
1113 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1114 desc.format = wined3dformat_from_d3dformat(format);
1115 desc.multisample_type = multisample_type;
1116 desc.multisample_quality = multisample_quality;
1117 desc.usage = usage & WINED3DUSAGE_MASK;
1118 desc.pool = pool;
1119 desc.width = width;
1120 desc.height = height;
1121 desc.depth = 1;
1122 desc.size = 0;
1124 wined3d_mutex_lock();
1126 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1127 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1129 wined3d_mutex_unlock();
1130 WARN("Failed to create texture, hr %#x.\n", hr);
1131 if (hr == WINED3DERR_NOTAVAILABLE)
1132 hr = D3DERR_INVALIDCALL;
1133 return hr;
1136 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1137 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1138 *surface = &surface_impl->IDirect3DSurface9_iface;
1139 IDirect3DSurface9_AddRef(*surface);
1141 if (user_mem)
1142 wined3d_texture_update_desc(texture, width, height,
1143 desc.format, multisample_type, multisample_quality, user_mem, 0);
1145 wined3d_texture_decref(texture);
1147 wined3d_mutex_unlock();
1149 return D3D_OK;
1152 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1153 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1154 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1156 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1157 DWORD flags = 0;
1159 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1160 "lockable %#x, surface %p, shared_handle %p.\n",
1161 iface, width, height, format, multisample_type, multisample_quality,
1162 lockable, surface, shared_handle);
1164 *surface = NULL;
1165 if (shared_handle)
1167 if (!device->d3d_parent->extended)
1169 WARN("Trying to create a shared render target on a non-ex device.\n");
1170 return E_NOTIMPL;
1173 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1176 if (lockable)
1177 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1179 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1180 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1183 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1184 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1185 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1187 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1188 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1190 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1191 "discard %#x, surface %p, shared_handle %p.\n",
1192 iface, width, height, format, multisample_type, multisample_quality,
1193 discard, surface, shared_handle);
1195 *surface = NULL;
1196 if (shared_handle)
1198 if (!device->d3d_parent->extended)
1200 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1201 return E_NOTIMPL;
1204 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1207 if (discard)
1208 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1210 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1211 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1215 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1216 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1217 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1219 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1220 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1221 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1222 struct wined3d_box src_box;
1223 HRESULT hr;
1225 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1226 iface, src_surface, src_rect, dst_surface, dst_point);
1228 if (src_rect)
1230 src_box.left = src_rect->left;
1231 src_box.top = src_rect->top;
1232 src_box.right = src_rect->right;
1233 src_box.bottom = src_rect->bottom;
1234 src_box.front = 0;
1235 src_box.back = 1;
1238 wined3d_mutex_lock();
1239 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1240 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1241 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1242 src->sub_resource_idx, src_rect ? &src_box : NULL);
1243 wined3d_mutex_unlock();
1245 if (FAILED(hr))
1246 return D3DERR_INVALIDCALL;
1248 return hr;
1251 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1252 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1254 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1255 struct d3d9_texture *src_impl, *dst_impl;
1256 HRESULT hr;
1258 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1260 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1261 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1263 wined3d_mutex_lock();
1264 hr = wined3d_device_update_texture(device->wined3d_device,
1265 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1266 wined3d_mutex_unlock();
1268 return hr;
1271 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1272 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1274 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1275 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1276 struct wined3d_sub_resource_desc wined3d_desc;
1277 RECT dst_rect, src_rect;
1278 HRESULT hr;
1280 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1282 wined3d_mutex_lock();
1283 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1284 dst_rect.left = 0;
1285 dst_rect.top = 0;
1286 dst_rect.right = wined3d_desc.width;
1287 dst_rect.bottom = wined3d_desc.height;
1289 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1290 src_rect.left = 0;
1291 src_rect.top = 0;
1292 src_rect.right = wined3d_desc.width;
1293 src_rect.bottom = wined3d_desc.height;
1295 /* TODO: Check surface sizes, pools, etc. */
1296 if (wined3d_desc.multisample_type)
1297 hr = D3DERR_INVALIDCALL;
1298 else
1299 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1300 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1301 wined3d_mutex_unlock();
1303 return hr;
1306 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1307 UINT swapchain, IDirect3DSurface9 *dst_surface)
1309 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1310 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1311 HRESULT hr = D3DERR_INVALIDCALL;
1313 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1315 wined3d_mutex_lock();
1316 if (swapchain < device->implicit_swapchain_count)
1317 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1318 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1319 wined3d_mutex_unlock();
1321 return hr;
1324 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1325 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1327 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1328 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1329 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1330 struct wined3d_sub_resource_desc src_desc, dst_desc;
1331 HRESULT hr = D3DERR_INVALIDCALL;
1332 RECT d, s;
1334 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1335 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1337 wined3d_mutex_lock();
1338 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1339 if (!dst_rect)
1341 d.left = 0;
1342 d.top = 0;
1343 d.right = dst_desc.width;
1344 d.bottom = dst_desc.height;
1345 dst_rect = &d;
1348 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1349 if (!src_rect)
1351 s.left = 0;
1352 s.top = 0;
1353 s.right = src_desc.width;
1354 s.bottom = src_desc.height;
1355 src_rect = &s;
1358 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1360 if (device->in_scene)
1362 WARN("Rejecting depth / stencil blit while in scene.\n");
1363 goto done;
1366 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1367 || src_rect->bottom != src_desc.height)
1369 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1370 wine_dbgstr_rect(src_rect));
1371 goto done;
1374 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1375 || dst_rect->bottom != dst_desc.height)
1377 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1378 wine_dbgstr_rect(dst_rect));
1379 goto done;
1382 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1384 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1385 goto done;
1389 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1390 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1391 if (hr == WINEDDERR_INVALIDRECT)
1392 hr = D3DERR_INVALIDCALL;
1394 done:
1395 wined3d_mutex_unlock();
1396 return hr;
1399 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1400 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1402 const struct wined3d_color c =
1404 ((color >> 16) & 0xff) / 255.0f,
1405 ((color >> 8) & 0xff) / 255.0f,
1406 (color & 0xff) / 255.0f,
1407 ((color >> 24) & 0xff) / 255.0f,
1409 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1410 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1411 struct wined3d_sub_resource_desc desc;
1412 HRESULT hr;
1414 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1416 wined3d_mutex_lock();
1418 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1419 surface_impl->sub_resource_idx, &desc)))
1421 wined3d_mutex_unlock();
1422 return D3DERR_INVALIDCALL;
1425 if (desc.pool != WINED3D_POOL_DEFAULT)
1427 wined3d_mutex_unlock();
1428 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1429 return D3DERR_INVALIDCALL;
1431 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1433 wined3d_mutex_unlock();
1434 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1435 return D3DERR_INVALIDCALL;
1437 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1439 wined3d_mutex_unlock();
1440 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1441 return D3DERR_INVALIDCALL;
1444 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1445 d3d9_surface_get_rendertarget_view(surface_impl), rect,
1446 WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1448 wined3d_mutex_unlock();
1450 return hr;
1453 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1454 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1455 HANDLE *shared_handle)
1457 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1458 void *user_mem = NULL;
1460 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1461 iface, width, height, format, pool, surface, shared_handle);
1463 *surface = NULL;
1464 if (pool == D3DPOOL_MANAGED)
1466 WARN("Attempting to create a managed offscreen plain surface.\n");
1467 return D3DERR_INVALIDCALL;
1470 if (shared_handle)
1472 if (!device->d3d_parent->extended)
1474 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1475 return E_NOTIMPL;
1478 if (pool == D3DPOOL_SYSTEMMEM)
1479 user_mem = *shared_handle;
1480 else
1482 if (pool != D3DPOOL_DEFAULT)
1484 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1485 return D3DERR_INVALIDCALL;
1487 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1491 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1492 * regardless of the pool they're created in. Should we set dynamic usage
1493 * here? */
1494 return d3d9_device_create_surface(device, width, height, format,
1495 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1498 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1500 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1501 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1502 HRESULT hr;
1504 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1506 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1508 WARN("Invalid index %u specified.\n", idx);
1509 return D3DERR_INVALIDCALL;
1512 if (!idx && !surface_impl)
1514 WARN("Trying to set render target 0 to NULL.\n");
1515 return D3DERR_INVALIDCALL;
1518 wined3d_mutex_lock();
1519 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1520 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1521 wined3d_mutex_unlock();
1523 return hr;
1526 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1528 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1529 struct wined3d_rendertarget_view *wined3d_rtv;
1530 struct d3d9_surface *surface_impl;
1531 HRESULT hr = D3D_OK;
1533 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1535 if (!surface)
1536 return D3DERR_INVALIDCALL;
1538 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1540 WARN("Invalid index %u specified.\n", idx);
1541 return D3DERR_INVALIDCALL;
1544 wined3d_mutex_lock();
1545 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1547 /* We want the sub resource parent here, since the view itself may be
1548 * internal to wined3d and may not have a parent. */
1549 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1550 *surface = &surface_impl->IDirect3DSurface9_iface;
1551 IDirect3DSurface9_AddRef(*surface);
1553 else
1555 hr = D3DERR_NOTFOUND;
1556 *surface = NULL;
1558 wined3d_mutex_unlock();
1560 return hr;
1563 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1565 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1566 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1568 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1570 wined3d_mutex_lock();
1571 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1572 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1573 wined3d_mutex_unlock();
1575 return D3D_OK;
1578 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1580 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1581 struct wined3d_rendertarget_view *wined3d_dsv;
1582 struct d3d9_surface *surface_impl;
1583 HRESULT hr = D3D_OK;
1585 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1587 if (!depth_stencil)
1588 return D3DERR_INVALIDCALL;
1590 wined3d_mutex_lock();
1591 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1593 /* We want the sub resource parent here, since the view itself may be
1594 * internal to wined3d and may not have a parent. */
1595 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1596 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1597 IDirect3DSurface9_AddRef(*depth_stencil);
1599 else
1601 hr = D3DERR_NOTFOUND;
1602 *depth_stencil = NULL;
1604 wined3d_mutex_unlock();
1606 return hr;
1609 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1611 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1612 HRESULT hr;
1614 TRACE("iface %p.\n", iface);
1616 wined3d_mutex_lock();
1617 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1618 device->in_scene = TRUE;
1619 wined3d_mutex_unlock();
1621 return hr;
1624 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1626 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1627 HRESULT hr;
1629 TRACE("iface %p.\n", iface);
1631 wined3d_mutex_lock();
1632 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1633 device->in_scene = FALSE;
1634 wined3d_mutex_unlock();
1636 return hr;
1639 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1640 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1642 const struct wined3d_color c =
1644 ((color >> 16) & 0xff) / 255.0f,
1645 ((color >> 8) & 0xff) / 255.0f,
1646 (color & 0xff) / 255.0f,
1647 ((color >> 24) & 0xff) / 255.0f,
1649 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1650 HRESULT hr;
1652 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1653 iface, rect_count, rects, flags, color, z, stencil);
1655 if (rect_count && !rects)
1657 WARN("count %u with NULL rects.\n", rect_count);
1658 rect_count = 0;
1661 wined3d_mutex_lock();
1662 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1663 wined3d_mutex_unlock();
1665 return hr;
1668 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1669 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1671 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1673 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1675 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1676 wined3d_mutex_lock();
1677 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1678 wined3d_mutex_unlock();
1680 return D3D_OK;
1683 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1684 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1686 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1688 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1690 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1691 wined3d_mutex_lock();
1692 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1693 wined3d_mutex_unlock();
1695 return D3D_OK;
1698 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1699 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1701 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1703 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1705 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1706 wined3d_mutex_lock();
1707 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1708 wined3d_mutex_unlock();
1710 return D3D_OK;
1713 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1715 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1717 TRACE("iface %p, viewport %p.\n", iface, viewport);
1719 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1720 wined3d_mutex_lock();
1721 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1722 wined3d_mutex_unlock();
1724 return D3D_OK;
1727 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1729 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1731 TRACE("iface %p, viewport %p.\n", iface, viewport);
1733 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1734 wined3d_mutex_lock();
1735 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1736 wined3d_mutex_unlock();
1738 return D3D_OK;
1741 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1745 TRACE("iface %p, material %p.\n", iface, material);
1747 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1748 wined3d_mutex_lock();
1749 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1750 wined3d_mutex_unlock();
1752 return D3D_OK;
1755 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1757 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1759 TRACE("iface %p, material %p.\n", iface, material);
1761 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1762 wined3d_mutex_lock();
1763 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1764 wined3d_mutex_unlock();
1766 return D3D_OK;
1769 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1771 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1772 HRESULT hr;
1774 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1776 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1777 wined3d_mutex_lock();
1778 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1779 wined3d_mutex_unlock();
1781 return hr;
1784 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1786 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1787 HRESULT hr;
1789 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1791 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1792 wined3d_mutex_lock();
1793 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1794 wined3d_mutex_unlock();
1796 return hr;
1799 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1801 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1802 HRESULT hr;
1804 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1806 wined3d_mutex_lock();
1807 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1808 wined3d_mutex_unlock();
1810 return hr;
1813 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1815 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1816 HRESULT hr;
1818 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1820 wined3d_mutex_lock();
1821 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1822 wined3d_mutex_unlock();
1824 return hr;
1827 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1829 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1830 HRESULT hr;
1832 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1834 wined3d_mutex_lock();
1835 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1836 wined3d_mutex_unlock();
1838 return hr;
1841 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1843 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1844 HRESULT hr;
1846 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1848 wined3d_mutex_lock();
1849 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1850 wined3d_mutex_unlock();
1852 return hr;
1855 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1856 D3DRENDERSTATETYPE state, DWORD value)
1858 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1860 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1862 wined3d_mutex_lock();
1863 wined3d_device_set_render_state(device->wined3d_device, state, value);
1864 wined3d_mutex_unlock();
1866 return D3D_OK;
1869 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1870 D3DRENDERSTATETYPE state, DWORD *value)
1872 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1874 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1876 wined3d_mutex_lock();
1877 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1878 wined3d_mutex_unlock();
1880 return D3D_OK;
1883 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1884 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1886 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1887 struct d3d9_stateblock *object;
1888 HRESULT hr;
1890 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1892 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1894 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1895 return D3DERR_INVALIDCALL;
1898 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1899 if (!object)
1900 return E_OUTOFMEMORY;
1902 hr = stateblock_init(object, device, type, NULL);
1903 if (FAILED(hr))
1905 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1906 HeapFree(GetProcessHeap(), 0, object);
1907 return hr;
1910 TRACE("Created stateblock %p.\n", object);
1911 *stateblock = &object->IDirect3DStateBlock9_iface;
1913 return D3D_OK;
1916 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1918 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1919 HRESULT hr;
1921 TRACE("iface %p.\n", iface);
1923 wined3d_mutex_lock();
1924 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1925 wined3d_mutex_unlock();
1927 return hr;
1930 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1932 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1933 struct wined3d_stateblock *wined3d_stateblock;
1934 struct d3d9_stateblock *object;
1935 HRESULT hr;
1937 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1939 wined3d_mutex_lock();
1940 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1941 wined3d_mutex_unlock();
1942 if (FAILED(hr))
1944 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1945 return hr;
1948 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1949 if (!object)
1951 wined3d_mutex_lock();
1952 wined3d_stateblock_decref(wined3d_stateblock);
1953 wined3d_mutex_unlock();
1954 return E_OUTOFMEMORY;
1957 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1958 if (FAILED(hr))
1960 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1961 wined3d_mutex_lock();
1962 wined3d_stateblock_decref(wined3d_stateblock);
1963 wined3d_mutex_unlock();
1964 HeapFree(GetProcessHeap(), 0, object);
1965 return hr;
1968 TRACE("Created stateblock %p.\n", object);
1969 *stateblock = &object->IDirect3DStateBlock9_iface;
1971 return D3D_OK;
1974 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1976 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1977 HRESULT hr;
1979 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1981 wined3d_mutex_lock();
1982 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1983 wined3d_mutex_unlock();
1985 return hr;
1988 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1990 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1991 HRESULT hr;
1993 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1995 wined3d_mutex_lock();
1996 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1997 wined3d_mutex_unlock();
1999 return hr;
2002 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2004 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2005 struct wined3d_texture *wined3d_texture = NULL;
2006 struct d3d9_texture *texture_impl;
2008 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2010 if (!texture)
2011 return D3DERR_INVALIDCALL;
2013 wined3d_mutex_lock();
2014 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2016 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2017 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2018 IDirect3DBaseTexture9_AddRef(*texture);
2020 else
2022 *texture = NULL;
2024 wined3d_mutex_unlock();
2026 return D3D_OK;
2029 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2031 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2032 struct d3d9_texture *texture_impl;
2033 HRESULT hr;
2035 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2037 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2039 wined3d_mutex_lock();
2040 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2041 texture_impl ? texture_impl->wined3d_texture : NULL);
2042 wined3d_mutex_unlock();
2044 return hr;
2047 static const enum wined3d_texture_stage_state tss_lookup[] =
2049 WINED3D_TSS_INVALID, /* 0, unused */
2050 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2051 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2052 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2053 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2054 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2055 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2056 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2057 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2058 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2059 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2060 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2061 WINED3D_TSS_INVALID, /* 12, unused */
2062 WINED3D_TSS_INVALID, /* 13, unused */
2063 WINED3D_TSS_INVALID, /* 14, unused */
2064 WINED3D_TSS_INVALID, /* 15, unused */
2065 WINED3D_TSS_INVALID, /* 16, unused */
2066 WINED3D_TSS_INVALID, /* 17, unused */
2067 WINED3D_TSS_INVALID, /* 18, unused */
2068 WINED3D_TSS_INVALID, /* 19, unused */
2069 WINED3D_TSS_INVALID, /* 20, unused */
2070 WINED3D_TSS_INVALID, /* 21, unused */
2071 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2072 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2073 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2074 WINED3D_TSS_INVALID, /* 25, unused */
2075 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2076 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2077 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2078 WINED3D_TSS_INVALID, /* 29, unused */
2079 WINED3D_TSS_INVALID, /* 30, unused */
2080 WINED3D_TSS_INVALID, /* 31, unused */
2081 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2084 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2085 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2087 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2089 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2091 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2093 WARN("Invalid state %#x passed.\n", state);
2094 return D3D_OK;
2097 wined3d_mutex_lock();
2098 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2099 wined3d_mutex_unlock();
2101 return D3D_OK;
2104 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2105 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2107 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2109 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2111 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2113 WARN("Invalid state %#x passed.\n", state);
2114 return D3D_OK;
2117 wined3d_mutex_lock();
2118 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2119 wined3d_mutex_unlock();
2121 return D3D_OK;
2124 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2125 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2127 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2129 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2131 wined3d_mutex_lock();
2132 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2133 wined3d_mutex_unlock();
2135 return D3D_OK;
2138 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2139 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2141 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2143 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2145 wined3d_mutex_lock();
2146 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2147 wined3d_mutex_unlock();
2149 return D3D_OK;
2152 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2154 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2155 HRESULT hr;
2157 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2159 wined3d_mutex_lock();
2160 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2161 wined3d_mutex_unlock();
2163 return hr;
2166 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2167 UINT palette_idx, const PALETTEENTRY *entries)
2169 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2171 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2172 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2173 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2175 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2176 return D3D_OK;
2179 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2180 UINT palette_idx, PALETTEENTRY *entries)
2182 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2184 return D3DERR_INVALIDCALL;
2187 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2189 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2191 return D3D_OK;
2194 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2196 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2198 return D3DERR_INVALIDCALL;
2201 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2203 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2205 TRACE("iface %p, rect %p.\n", iface, rect);
2207 wined3d_mutex_lock();
2208 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2209 wined3d_mutex_unlock();
2211 return D3D_OK;
2214 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2216 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2218 TRACE("iface %p, rect %p.\n", iface, rect);
2220 wined3d_mutex_lock();
2221 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2222 wined3d_mutex_unlock();
2224 return D3D_OK;
2227 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2229 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2231 TRACE("iface %p, software %#x.\n", iface, software);
2233 wined3d_mutex_lock();
2234 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2235 wined3d_mutex_unlock();
2237 return D3D_OK;
2240 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2242 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2243 BOOL ret;
2245 TRACE("iface %p.\n", iface);
2247 wined3d_mutex_lock();
2248 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2249 wined3d_mutex_unlock();
2251 return ret;
2254 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2256 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2257 HRESULT hr;
2259 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2261 wined3d_mutex_lock();
2262 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2263 wined3d_mutex_unlock();
2265 return hr;
2268 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2271 float ret;
2273 TRACE("iface %p.\n", iface);
2275 wined3d_mutex_lock();
2276 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2277 wined3d_mutex_unlock();
2279 return ret;
2282 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2283 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2285 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2286 HRESULT hr;
2288 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2289 iface, primitive_type, start_vertex, primitive_count);
2291 wined3d_mutex_lock();
2292 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2293 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2294 vertex_count_from_primitive_count(primitive_type, primitive_count));
2295 wined3d_mutex_unlock();
2297 return hr;
2300 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2301 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2302 UINT vertex_count, UINT start_idx, UINT primitive_count)
2304 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2305 HRESULT hr;
2307 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2308 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2309 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2310 vertex_count, start_idx, primitive_count);
2312 wined3d_mutex_lock();
2313 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2314 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2315 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2316 vertex_count_from_primitive_count(primitive_type, primitive_count));
2317 wined3d_mutex_unlock();
2319 return hr;
2322 /* The caller is responsible for wined3d locking */
2323 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2325 HRESULT hr;
2327 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2329 UINT size = max(device->vertex_buffer_size * 2, min_size);
2330 struct wined3d_buffer *buffer;
2332 TRACE("Growing vertex buffer to %u bytes.\n", size);
2334 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2335 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2336 if (FAILED(hr))
2338 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2339 return hr;
2342 if (device->vertex_buffer)
2343 wined3d_buffer_decref(device->vertex_buffer);
2345 device->vertex_buffer = buffer;
2346 device->vertex_buffer_size = size;
2347 device->vertex_buffer_pos = 0;
2349 return D3D_OK;
2352 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2353 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2355 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2356 HRESULT hr;
2357 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2358 UINT size = vtx_count * stride;
2359 UINT vb_pos, align;
2360 BYTE *buffer_data;
2362 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2363 iface, primitive_type, primitive_count, data, stride);
2365 if (!primitive_count)
2367 WARN("primitive_count is 0, returning D3D_OK\n");
2368 return D3D_OK;
2371 wined3d_mutex_lock();
2373 hr = d3d9_device_prepare_vertex_buffer(device, size);
2374 if (FAILED(hr))
2375 goto done;
2377 vb_pos = device->vertex_buffer_pos;
2378 align = vb_pos % stride;
2379 if (align) align = stride - align;
2380 if (vb_pos + size + align > device->vertex_buffer_size)
2381 vb_pos = 0;
2382 else
2383 vb_pos += align;
2385 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2386 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2387 if (FAILED(hr))
2388 goto done;
2389 memcpy(buffer_data, data, size);
2390 wined3d_buffer_unmap(device->vertex_buffer);
2391 device->vertex_buffer_pos = vb_pos + size;
2393 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2394 if (FAILED(hr))
2395 goto done;
2397 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2398 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2399 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2401 done:
2402 wined3d_mutex_unlock();
2403 return hr;
2406 /* The caller is responsible for wined3d locking */
2407 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2409 HRESULT hr;
2411 if (device->index_buffer_size < min_size || !device->index_buffer)
2413 UINT size = max(device->index_buffer_size * 2, min_size);
2414 struct wined3d_buffer *buffer;
2416 TRACE("Growing index buffer to %u bytes.\n", size);
2418 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2419 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2420 if (FAILED(hr))
2422 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2423 return hr;
2426 if (device->index_buffer)
2427 wined3d_buffer_decref(device->index_buffer);
2429 device->index_buffer = buffer;
2430 device->index_buffer_size = size;
2431 device->index_buffer_pos = 0;
2433 return D3D_OK;
2436 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2437 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2438 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2439 const void *vertex_data, UINT vertex_stride)
2441 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2442 HRESULT hr;
2443 BYTE *buffer_data;
2445 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2446 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2447 UINT idx_size = idx_count * idx_fmt_size;
2448 UINT ib_pos;
2450 UINT vtx_size = vertex_count * vertex_stride;
2451 UINT vb_pos, align;
2453 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2454 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2455 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2456 index_data, index_format, vertex_data, vertex_stride);
2458 if (!primitive_count)
2460 WARN("primitive_count is 0, returning D3D_OK.\n");
2461 return D3D_OK;
2464 wined3d_mutex_lock();
2466 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2467 if (FAILED(hr))
2468 goto done;
2470 vb_pos = device->vertex_buffer_pos;
2471 align = vb_pos % vertex_stride;
2472 if (align) align = vertex_stride - align;
2473 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2474 vb_pos = 0;
2475 else
2476 vb_pos += align;
2478 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2479 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2480 if (FAILED(hr))
2481 goto done;
2482 memcpy(buffer_data, vertex_data, vtx_size);
2483 wined3d_buffer_unmap(device->vertex_buffer);
2484 device->vertex_buffer_pos = vb_pos + vtx_size;
2486 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2487 if (FAILED(hr))
2488 goto done;
2490 ib_pos = device->index_buffer_pos;
2491 align = ib_pos % idx_fmt_size;
2492 if (align) align = idx_fmt_size - align;
2493 if (ib_pos + idx_size + align > device->index_buffer_size)
2494 ib_pos = 0;
2495 else
2496 ib_pos += align;
2498 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2499 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2500 if (FAILED(hr))
2501 goto done;
2502 memcpy(buffer_data, index_data, idx_size);
2503 wined3d_buffer_unmap(device->index_buffer);
2504 device->index_buffer_pos = ib_pos + idx_size;
2506 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2507 if (FAILED(hr))
2508 goto done;
2510 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2511 wined3dformat_from_d3dformat(index_format));
2512 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2514 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2515 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2517 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2518 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2519 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2521 done:
2522 wined3d_mutex_unlock();
2523 return hr;
2526 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2527 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2528 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2530 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2531 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2532 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2533 HRESULT hr;
2535 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2536 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2538 wined3d_mutex_lock();
2539 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2540 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2541 flags, dst_impl->fvf);
2542 wined3d_mutex_unlock();
2544 return hr;
2547 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2548 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2550 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2551 struct d3d9_vertex_declaration *object;
2552 HRESULT hr;
2554 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2556 if (!declaration)
2558 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2559 return D3DERR_INVALIDCALL;
2562 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2563 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2565 return hr;
2568 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2569 IDirect3DVertexDeclaration9 *declaration)
2571 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2572 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2574 TRACE("iface %p, declaration %p.\n", iface, declaration);
2576 wined3d_mutex_lock();
2577 wined3d_device_set_vertex_declaration(device->wined3d_device,
2578 decl_impl ? decl_impl->wined3d_declaration : NULL);
2579 wined3d_mutex_unlock();
2581 return D3D_OK;
2584 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2585 IDirect3DVertexDeclaration9 **declaration)
2587 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2588 struct wined3d_vertex_declaration *wined3d_declaration;
2589 struct d3d9_vertex_declaration *declaration_impl;
2591 TRACE("iface %p, declaration %p.\n", iface, declaration);
2593 if (!declaration) return D3DERR_INVALIDCALL;
2595 wined3d_mutex_lock();
2596 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2598 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2599 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2600 IDirect3DVertexDeclaration9_AddRef(*declaration);
2602 else
2604 *declaration = NULL;
2606 wined3d_mutex_unlock();
2608 TRACE("Returning %p.\n", *declaration);
2609 return D3D_OK;
2612 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2614 struct wined3d_vertex_declaration *wined3d_declaration;
2615 struct fvf_declaration *fvf_decls = device->fvf_decls;
2616 struct d3d9_vertex_declaration *d3d9_declaration;
2617 D3DVERTEXELEMENT9 *elements;
2618 int p, low, high; /* deliberately signed */
2619 HRESULT hr;
2621 TRACE("Searching for declaration for fvf %08x... ", fvf);
2623 low = 0;
2624 high = device->fvf_decl_count - 1;
2625 while (low <= high)
2627 p = (low + high) >> 1;
2628 TRACE("%d ", p);
2630 if (fvf_decls[p].fvf == fvf)
2632 TRACE("found %p.\n", fvf_decls[p].decl);
2633 return fvf_decls[p].decl;
2636 if (fvf_decls[p].fvf < fvf)
2637 low = p + 1;
2638 else
2639 high = p - 1;
2641 TRACE("not found. Creating and inserting at position %d.\n", low);
2643 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2644 return NULL;
2646 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2647 HeapFree(GetProcessHeap(), 0, elements);
2648 if (FAILED(hr))
2649 return NULL;
2651 if (device->fvf_decl_size == device->fvf_decl_count)
2653 UINT grow = max(device->fvf_decl_size / 2, 8);
2655 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2656 if (!fvf_decls)
2658 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2659 return NULL;
2661 device->fvf_decls = fvf_decls;
2662 device->fvf_decl_size += grow;
2665 d3d9_declaration->fvf = fvf;
2666 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2667 wined3d_vertex_declaration_incref(wined3d_declaration);
2668 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2670 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2671 fvf_decls[low].decl = wined3d_declaration;
2672 fvf_decls[low].fvf = fvf;
2673 ++device->fvf_decl_count;
2675 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2677 return wined3d_declaration;
2680 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2682 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2683 struct wined3d_vertex_declaration *decl;
2685 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2687 if (!fvf)
2689 WARN("%#x is not a valid FVF.\n", fvf);
2690 return D3D_OK;
2693 wined3d_mutex_lock();
2694 if (!(decl = device_get_fvf_declaration(device, fvf)))
2696 wined3d_mutex_unlock();
2697 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2698 return D3DERR_DRIVERINTERNALERROR;
2701 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2702 wined3d_mutex_unlock();
2704 return D3D_OK;
2707 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2709 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2710 struct wined3d_vertex_declaration *wined3d_declaration;
2711 struct d3d9_vertex_declaration *d3d9_declaration;
2713 TRACE("iface %p, fvf %p.\n", iface, fvf);
2715 wined3d_mutex_lock();
2716 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2718 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2719 *fvf = d3d9_declaration->fvf;
2721 else
2723 *fvf = 0;
2725 wined3d_mutex_unlock();
2727 TRACE("Returning FVF %#x.\n", *fvf);
2729 return D3D_OK;
2732 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2733 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2735 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2736 struct d3d9_vertexshader *object;
2737 HRESULT hr;
2739 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2741 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2742 if (!object)
2743 return E_OUTOFMEMORY;
2745 hr = vertexshader_init(object, device, byte_code);
2746 if (FAILED(hr))
2748 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2749 HeapFree(GetProcessHeap(), 0, object);
2750 return hr;
2753 TRACE("Created vertex shader %p.\n", object);
2754 *shader = &object->IDirect3DVertexShader9_iface;
2756 return D3D_OK;
2759 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2761 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2762 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2764 TRACE("iface %p, shader %p.\n", iface, shader);
2766 wined3d_mutex_lock();
2767 wined3d_device_set_vertex_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_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2776 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2777 struct d3d9_vertexshader *shader_impl;
2778 struct wined3d_shader *wined3d_shader;
2780 TRACE("iface %p, shader %p.\n", iface, shader);
2782 wined3d_mutex_lock();
2783 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2785 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2786 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2787 IDirect3DVertexShader9_AddRef(*shader);
2789 else
2791 *shader = NULL;
2793 wined3d_mutex_unlock();
2795 TRACE("Returning %p.\n", *shader);
2797 return D3D_OK;
2800 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2801 UINT reg_idx, const float *data, UINT count)
2803 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2804 HRESULT hr;
2806 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2808 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2810 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2811 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2812 return D3DERR_INVALIDCALL;
2815 wined3d_mutex_lock();
2816 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2817 wined3d_mutex_unlock();
2819 return hr;
2822 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2823 UINT reg_idx, float *data, UINT count)
2825 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2826 HRESULT hr;
2828 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2830 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2832 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2833 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2834 return D3DERR_INVALIDCALL;
2837 wined3d_mutex_lock();
2838 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2839 wined3d_mutex_unlock();
2841 return hr;
2844 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2845 UINT reg_idx, const int *data, UINT count)
2847 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2848 HRESULT hr;
2850 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2852 wined3d_mutex_lock();
2853 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2854 wined3d_mutex_unlock();
2856 return hr;
2859 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2860 UINT reg_idx, int *data, UINT count)
2862 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2863 HRESULT hr;
2865 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2867 wined3d_mutex_lock();
2868 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2869 wined3d_mutex_unlock();
2871 return hr;
2874 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2875 UINT reg_idx, const BOOL *data, UINT count)
2877 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2878 HRESULT hr;
2880 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2882 wined3d_mutex_lock();
2883 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2884 wined3d_mutex_unlock();
2886 return hr;
2889 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2890 UINT reg_idx, BOOL *data, UINT count)
2892 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2893 HRESULT hr;
2895 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2897 wined3d_mutex_lock();
2898 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2899 wined3d_mutex_unlock();
2901 return hr;
2904 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2905 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2907 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2908 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2909 HRESULT hr;
2911 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2912 iface, stream_idx, buffer, offset, stride);
2914 wined3d_mutex_lock();
2915 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2916 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2917 wined3d_mutex_unlock();
2919 return hr;
2922 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2923 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2925 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2926 struct d3d9_vertexbuffer *buffer_impl;
2927 struct wined3d_buffer *wined3d_buffer;
2928 HRESULT hr;
2930 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2931 iface, stream_idx, buffer, offset, stride);
2933 if (!buffer)
2934 return D3DERR_INVALIDCALL;
2936 wined3d_mutex_lock();
2937 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2938 if (SUCCEEDED(hr) && wined3d_buffer)
2940 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2941 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2942 IDirect3DVertexBuffer9_AddRef(*buffer);
2944 else
2946 if (FAILED(hr))
2947 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2948 *buffer = NULL;
2950 wined3d_mutex_unlock();
2952 return hr;
2955 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2958 HRESULT hr;
2960 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2962 wined3d_mutex_lock();
2963 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2964 wined3d_mutex_unlock();
2966 return hr;
2969 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2972 HRESULT hr;
2974 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2976 wined3d_mutex_lock();
2977 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2978 wined3d_mutex_unlock();
2980 return hr;
2983 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2985 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2986 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2988 TRACE("iface %p, buffer %p.\n", iface, buffer);
2990 wined3d_mutex_lock();
2991 wined3d_device_set_index_buffer(device->wined3d_device,
2992 ib ? ib->wined3d_buffer : NULL,
2993 ib ? ib->format : WINED3DFMT_UNKNOWN);
2994 wined3d_mutex_unlock();
2996 return D3D_OK;
2999 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3001 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3002 enum wined3d_format_id wined3d_format;
3003 struct wined3d_buffer *wined3d_buffer;
3004 struct d3d9_indexbuffer *buffer_impl;
3006 TRACE("iface %p, buffer %p.\n", iface, buffer);
3008 if (!buffer)
3009 return D3DERR_INVALIDCALL;
3011 wined3d_mutex_lock();
3012 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
3014 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3015 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3016 IDirect3DIndexBuffer9_AddRef(*buffer);
3018 else
3020 *buffer = NULL;
3022 wined3d_mutex_unlock();
3024 return D3D_OK;
3027 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3028 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3030 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3031 struct d3d9_pixelshader *object;
3032 HRESULT hr;
3034 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3036 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3037 if (!object)
3039 FIXME("Failed to allocate pixel shader memory.\n");
3040 return E_OUTOFMEMORY;
3043 hr = pixelshader_init(object, device, byte_code);
3044 if (FAILED(hr))
3046 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3047 HeapFree(GetProcessHeap(), 0, object);
3048 return hr;
3051 TRACE("Created pixel shader %p.\n", object);
3052 *shader = &object->IDirect3DPixelShader9_iface;
3054 return D3D_OK;
3057 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3059 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3060 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3062 TRACE("iface %p, shader %p.\n", iface, shader);
3064 wined3d_mutex_lock();
3065 wined3d_device_set_pixel_shader(device->wined3d_device,
3066 shader_obj ? shader_obj->wined3d_shader : NULL);
3067 wined3d_mutex_unlock();
3069 return D3D_OK;
3072 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3074 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3075 struct d3d9_pixelshader *shader_impl;
3076 struct wined3d_shader *wined3d_shader;
3078 TRACE("iface %p, shader %p.\n", iface, shader);
3080 if (!shader) return D3DERR_INVALIDCALL;
3082 wined3d_mutex_lock();
3083 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3085 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3086 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3087 IDirect3DPixelShader9_AddRef(*shader);
3089 else
3091 *shader = NULL;
3093 wined3d_mutex_unlock();
3095 TRACE("Returning %p.\n", *shader);
3097 return D3D_OK;
3100 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3101 UINT reg_idx, const float *data, UINT count)
3103 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3104 HRESULT hr;
3106 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3108 wined3d_mutex_lock();
3109 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3110 wined3d_mutex_unlock();
3112 return hr;
3115 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3116 UINT reg_idx, float *data, UINT count)
3118 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3119 HRESULT hr;
3121 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3123 wined3d_mutex_lock();
3124 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3125 wined3d_mutex_unlock();
3127 return hr;
3130 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3131 UINT reg_idx, const int *data, UINT count)
3133 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3134 HRESULT hr;
3136 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3138 wined3d_mutex_lock();
3139 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3140 wined3d_mutex_unlock();
3142 return hr;
3145 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3146 UINT reg_idx, int *data, UINT count)
3148 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3149 HRESULT hr;
3151 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3153 wined3d_mutex_lock();
3154 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3155 wined3d_mutex_unlock();
3157 return hr;
3160 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3161 UINT reg_idx, const BOOL *data, UINT count)
3163 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3164 HRESULT hr;
3166 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3168 wined3d_mutex_lock();
3169 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3170 wined3d_mutex_unlock();
3172 return hr;
3175 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3176 UINT reg_idx, BOOL *data, UINT count)
3178 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3179 HRESULT hr;
3181 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3183 wined3d_mutex_lock();
3184 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3185 wined3d_mutex_unlock();
3187 return hr;
3190 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3191 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3193 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3194 iface, handle, segment_count, patch_info);
3195 return D3D_OK;
3198 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3199 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3201 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3202 iface, handle, segment_count, patch_info);
3203 return D3D_OK;
3206 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3208 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3209 return D3DERR_INVALIDCALL;
3212 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3214 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3215 struct d3d9_query *object;
3216 HRESULT hr;
3218 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3220 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3221 if (!object)
3222 return E_OUTOFMEMORY;
3224 hr = query_init(object, device, type);
3225 if (FAILED(hr))
3227 WARN("Failed to initialize query, hr %#x.\n", hr);
3228 HeapFree(GetProcessHeap(), 0, object);
3229 return hr;
3232 TRACE("Created query %p.\n", object);
3233 if (query) *query = &object->IDirect3DQuery9_iface;
3234 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3236 return D3D_OK;
3239 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3240 UINT width, UINT height, float *rows, float *columns)
3242 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3243 iface, width, height, rows, columns);
3245 return E_NOTIMPL;
3248 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3249 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3250 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3252 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3253 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3254 iface, src_surface, dst_surface, src_descs, rect_count,
3255 dst_descs, operation, offset_x, offset_y);
3257 return E_NOTIMPL;
3260 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3261 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3262 const RGNDATA *dirty_region, DWORD flags)
3264 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3265 UINT i;
3266 HRESULT hr;
3268 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3269 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3270 dst_window_override, dirty_region, flags);
3272 if (device->device_state != D3D9_DEVICE_STATE_OK)
3273 return S_PRESENT_OCCLUDED;
3275 if (dirty_region)
3276 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3278 wined3d_mutex_lock();
3279 for (i = 0; i < device->implicit_swapchain_count; ++i)
3281 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3282 src_rect, dst_rect, dst_window_override, flags)))
3284 wined3d_mutex_unlock();
3285 return hr;
3288 wined3d_mutex_unlock();
3290 return D3D_OK;
3293 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3295 FIXME("iface %p, priority %p stub!\n", iface, priority);
3297 return E_NOTIMPL;
3300 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3302 FIXME("iface %p, priority %d stub!\n", iface, priority);
3304 return E_NOTIMPL;
3307 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3309 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3311 return E_NOTIMPL;
3314 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3315 IDirect3DResource9 **resources, UINT32 resource_count)
3317 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3318 iface, resources, resource_count);
3320 return E_NOTIMPL;
3323 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3325 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3327 return E_NOTIMPL;
3330 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3332 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3334 *max_latency = 2;
3336 return E_NOTIMPL;
3339 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3341 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3342 struct wined3d_swapchain_desc swapchain_desc;
3344 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3346 wined3d_mutex_lock();
3347 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3348 wined3d_mutex_unlock();
3350 if (swapchain_desc.windowed)
3351 return D3D_OK;
3353 /* FIXME: This is actually supposed to check if any other device is in
3354 * fullscreen mode. */
3355 if (dst_window != swapchain_desc.device_window)
3356 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3358 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3361 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3362 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3363 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3365 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3366 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3367 iface, width, height, format, multisample_type, multisample_quality,
3368 lockable, surface, shared_handle, usage);
3370 *surface = NULL;
3371 if (shared_handle)
3372 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3374 return E_NOTIMPL;
3377 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3378 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3379 HANDLE *shared_handle, DWORD usage)
3381 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3382 iface, width, height, format, pool, surface, shared_handle, usage);
3384 return E_NOTIMPL;
3387 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3388 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3389 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3391 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3392 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3394 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3395 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3396 iface, width, height, format, multisample_type, multisample_quality,
3397 discard, surface, shared_handle, usage);
3399 if (usage & D3DUSAGE_DEPTHSTENCIL)
3401 WARN("Invalid usage %#x.\n", usage);
3402 return D3DERR_INVALIDCALL;
3405 if (shared_handle)
3406 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3408 if (discard)
3409 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3411 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3412 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3415 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3416 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3418 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3420 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3422 return d3d9_device_reset(device, present_parameters, mode);
3425 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3426 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3428 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3429 struct wined3d_display_mode wined3d_mode;
3430 HRESULT hr;
3432 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3433 iface, swapchain_idx, mode, rotation);
3435 if (mode->Size != sizeof(*mode))
3436 return D3DERR_INVALIDCALL;
3438 wined3d_mutex_lock();
3439 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3440 (enum wined3d_display_rotation *)rotation);
3441 wined3d_mutex_unlock();
3443 if (SUCCEEDED(hr))
3445 mode->Width = wined3d_mode.width;
3446 mode->Height = wined3d_mode.height;
3447 mode->RefreshRate = wined3d_mode.refresh_rate;
3448 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3449 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3452 return hr;
3455 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3457 /* IUnknown */
3458 d3d9_device_QueryInterface,
3459 d3d9_device_AddRef,
3460 d3d9_device_Release,
3461 /* IDirect3DDevice9 */
3462 d3d9_device_TestCooperativeLevel,
3463 d3d9_device_GetAvailableTextureMem,
3464 d3d9_device_EvictManagedResources,
3465 d3d9_device_GetDirect3D,
3466 d3d9_device_GetDeviceCaps,
3467 d3d9_device_GetDisplayMode,
3468 d3d9_device_GetCreationParameters,
3469 d3d9_device_SetCursorProperties,
3470 d3d9_device_SetCursorPosition,
3471 d3d9_device_ShowCursor,
3472 d3d9_device_CreateAdditionalSwapChain,
3473 d3d9_device_GetSwapChain,
3474 d3d9_device_GetNumberOfSwapChains,
3475 d3d9_device_Reset,
3476 d3d9_device_Present,
3477 d3d9_device_GetBackBuffer,
3478 d3d9_device_GetRasterStatus,
3479 d3d9_device_SetDialogBoxMode,
3480 d3d9_device_SetGammaRamp,
3481 d3d9_device_GetGammaRamp,
3482 d3d9_device_CreateTexture,
3483 d3d9_device_CreateVolumeTexture,
3484 d3d9_device_CreateCubeTexture,
3485 d3d9_device_CreateVertexBuffer,
3486 d3d9_device_CreateIndexBuffer,
3487 d3d9_device_CreateRenderTarget,
3488 d3d9_device_CreateDepthStencilSurface,
3489 d3d9_device_UpdateSurface,
3490 d3d9_device_UpdateTexture,
3491 d3d9_device_GetRenderTargetData,
3492 d3d9_device_GetFrontBufferData,
3493 d3d9_device_StretchRect,
3494 d3d9_device_ColorFill,
3495 d3d9_device_CreateOffscreenPlainSurface,
3496 d3d9_device_SetRenderTarget,
3497 d3d9_device_GetRenderTarget,
3498 d3d9_device_SetDepthStencilSurface,
3499 d3d9_device_GetDepthStencilSurface,
3500 d3d9_device_BeginScene,
3501 d3d9_device_EndScene,
3502 d3d9_device_Clear,
3503 d3d9_device_SetTransform,
3504 d3d9_device_GetTransform,
3505 d3d9_device_MultiplyTransform,
3506 d3d9_device_SetViewport,
3507 d3d9_device_GetViewport,
3508 d3d9_device_SetMaterial,
3509 d3d9_device_GetMaterial,
3510 d3d9_device_SetLight,
3511 d3d9_device_GetLight,
3512 d3d9_device_LightEnable,
3513 d3d9_device_GetLightEnable,
3514 d3d9_device_SetClipPlane,
3515 d3d9_device_GetClipPlane,
3516 d3d9_device_SetRenderState,
3517 d3d9_device_GetRenderState,
3518 d3d9_device_CreateStateBlock,
3519 d3d9_device_BeginStateBlock,
3520 d3d9_device_EndStateBlock,
3521 d3d9_device_SetClipStatus,
3522 d3d9_device_GetClipStatus,
3523 d3d9_device_GetTexture,
3524 d3d9_device_SetTexture,
3525 d3d9_device_GetTextureStageState,
3526 d3d9_device_SetTextureStageState,
3527 d3d9_device_GetSamplerState,
3528 d3d9_device_SetSamplerState,
3529 d3d9_device_ValidateDevice,
3530 d3d9_device_SetPaletteEntries,
3531 d3d9_device_GetPaletteEntries,
3532 d3d9_device_SetCurrentTexturePalette,
3533 d3d9_device_GetCurrentTexturePalette,
3534 d3d9_device_SetScissorRect,
3535 d3d9_device_GetScissorRect,
3536 d3d9_device_SetSoftwareVertexProcessing,
3537 d3d9_device_GetSoftwareVertexProcessing,
3538 d3d9_device_SetNPatchMode,
3539 d3d9_device_GetNPatchMode,
3540 d3d9_device_DrawPrimitive,
3541 d3d9_device_DrawIndexedPrimitive,
3542 d3d9_device_DrawPrimitiveUP,
3543 d3d9_device_DrawIndexedPrimitiveUP,
3544 d3d9_device_ProcessVertices,
3545 d3d9_device_CreateVertexDeclaration,
3546 d3d9_device_SetVertexDeclaration,
3547 d3d9_device_GetVertexDeclaration,
3548 d3d9_device_SetFVF,
3549 d3d9_device_GetFVF,
3550 d3d9_device_CreateVertexShader,
3551 d3d9_device_SetVertexShader,
3552 d3d9_device_GetVertexShader,
3553 d3d9_device_SetVertexShaderConstantF,
3554 d3d9_device_GetVertexShaderConstantF,
3555 d3d9_device_SetVertexShaderConstantI,
3556 d3d9_device_GetVertexShaderConstantI,
3557 d3d9_device_SetVertexShaderConstantB,
3558 d3d9_device_GetVertexShaderConstantB,
3559 d3d9_device_SetStreamSource,
3560 d3d9_device_GetStreamSource,
3561 d3d9_device_SetStreamSourceFreq,
3562 d3d9_device_GetStreamSourceFreq,
3563 d3d9_device_SetIndices,
3564 d3d9_device_GetIndices,
3565 d3d9_device_CreatePixelShader,
3566 d3d9_device_SetPixelShader,
3567 d3d9_device_GetPixelShader,
3568 d3d9_device_SetPixelShaderConstantF,
3569 d3d9_device_GetPixelShaderConstantF,
3570 d3d9_device_SetPixelShaderConstantI,
3571 d3d9_device_GetPixelShaderConstantI,
3572 d3d9_device_SetPixelShaderConstantB,
3573 d3d9_device_GetPixelShaderConstantB,
3574 d3d9_device_DrawRectPatch,
3575 d3d9_device_DrawTriPatch,
3576 d3d9_device_DeletePatch,
3577 d3d9_device_CreateQuery,
3578 /* IDirect3DDevice9Ex */
3579 d3d9_device_SetConvolutionMonoKernel,
3580 d3d9_device_ComposeRects,
3581 d3d9_device_PresentEx,
3582 d3d9_device_GetGPUThreadPriority,
3583 d3d9_device_SetGPUThreadPriority,
3584 d3d9_device_WaitForVBlank,
3585 d3d9_device_CheckResourceResidency,
3586 d3d9_device_SetMaximumFrameLatency,
3587 d3d9_device_GetMaximumFrameLatency,
3588 d3d9_device_CheckDeviceState,
3589 d3d9_device_CreateRenderTargetEx,
3590 d3d9_device_CreateOffscreenPlainSurfaceEx,
3591 d3d9_device_CreateDepthStencilSurfaceEx,
3592 d3d9_device_ResetEx,
3593 d3d9_device_GetDisplayModeEx,
3596 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3598 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3601 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3602 struct wined3d_device *device)
3604 TRACE("device_parent %p, device %p.\n", device_parent, device);
3607 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3609 TRACE("device_parent %p.\n", device_parent);
3612 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3614 struct d3d9_device *device = device_from_device_parent(device_parent);
3616 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3618 if (!device->d3d_parent)
3619 return;
3621 if (!activate)
3622 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3623 else if (device->d3d_parent->extended)
3624 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3625 else
3626 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3629 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3630 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3631 void **parent, const struct wined3d_parent_ops **parent_ops)
3633 struct d3d9_surface *d3d_surface;
3635 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3636 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3638 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3639 return E_OUTOFMEMORY;
3641 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3642 *parent = d3d_surface;
3643 TRACE("Created surface %p.\n", d3d_surface);
3645 return D3D_OK;
3648 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3649 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3650 void **parent, const struct wined3d_parent_ops **parent_ops)
3652 struct d3d9_volume *d3d_volume;
3654 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3655 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3657 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3658 return E_OUTOFMEMORY;
3660 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3661 *parent = d3d_volume;
3662 TRACE("Created volume %p.\n", d3d_volume);
3664 return D3D_OK;
3667 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3668 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3670 struct d3d9_device *device = device_from_device_parent(device_parent);
3671 struct d3d9_surface *d3d_surface;
3672 HRESULT hr;
3674 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3675 device_parent, container_parent, desc, texture);
3677 if (container_parent == device_parent)
3678 container_parent = &device->IDirect3DDevice9Ex_iface;
3680 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1,
3681 WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3683 WARN("Failed to create texture, hr %#x.\n", hr);
3684 return hr;
3687 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3688 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3690 return hr;
3693 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3694 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3696 struct d3d9_device *device = device_from_device_parent(device_parent);
3697 struct d3d9_swapchain *d3d_swapchain;
3698 HRESULT hr;
3700 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3702 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3703 if (FAILED(hr))
3705 WARN("Failed to create swapchain, hr %#x.\n", hr);
3706 *swapchain = NULL;
3707 return hr;
3710 *swapchain = d3d_swapchain->wined3d_swapchain;
3711 wined3d_swapchain_incref(*swapchain);
3712 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3714 return hr;
3717 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3719 device_parent_wined3d_device_created,
3720 device_parent_mode_changed,
3721 device_parent_activate,
3722 device_parent_surface_created,
3723 device_parent_volume_created,
3724 device_parent_create_swapchain_texture,
3725 device_parent_create_swapchain,
3728 static void setup_fpu(void)
3730 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3731 WORD cw;
3732 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3733 cw = (cw & ~0xf3f) | 0x3f;
3734 __asm__ volatile ("fldcw %0" : : "m" (cw));
3735 #elif defined(__i386__) && defined(_MSC_VER)
3736 WORD cw;
3737 __asm fnstcw cw;
3738 cw = (cw & ~0xf3f) | 0x3f;
3739 __asm fldcw cw;
3740 #else
3741 FIXME("FPU setup not implemented for this platform.\n");
3742 #endif
3745 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3746 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3747 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3749 struct wined3d_swapchain_desc *swapchain_desc;
3750 UINT i, count = 1;
3751 HRESULT hr;
3753 if (mode)
3754 FIXME("Ignoring display mode.\n");
3756 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3757 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3758 device->refcount = 1;
3760 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3762 wined3d_mutex_lock();
3763 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3764 &device->device_parent, &device->wined3d_device);
3765 if (FAILED(hr))
3767 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3768 wined3d_mutex_unlock();
3769 return hr;
3772 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3774 WINED3DCAPS caps;
3776 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3777 count = caps.NumberOfAdaptersInGroup;
3780 if (flags & D3DCREATE_MULTITHREADED)
3781 wined3d_device_set_multithreaded(device->wined3d_device);
3783 if (!parameters->Windowed)
3785 if (!focus_window)
3786 focus_window = parameters->hDeviceWindow;
3787 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3789 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3790 wined3d_device_decref(device->wined3d_device);
3791 wined3d_mutex_unlock();
3792 return hr;
3795 for (i = 0; i < count; ++i)
3797 HWND device_window = parameters[i].hDeviceWindow;
3799 if (!device_window) device_window = focus_window;
3800 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3801 parameters[i].BackBufferWidth,
3802 parameters[i].BackBufferHeight);
3806 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3807 if (!swapchain_desc)
3809 ERR("Failed to allocate wined3d parameters.\n");
3810 wined3d_device_release_focus_window(device->wined3d_device);
3811 wined3d_device_decref(device->wined3d_device);
3812 wined3d_mutex_unlock();
3813 return E_OUTOFMEMORY;
3816 for (i = 0; i < count; ++i)
3818 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3819 parent->extended))
3821 wined3d_device_release_focus_window(device->wined3d_device);
3822 wined3d_device_decref(device->wined3d_device);
3823 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3824 wined3d_mutex_unlock();
3825 return D3DERR_INVALIDCALL;
3829 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3830 if (FAILED(hr))
3832 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3833 wined3d_device_release_focus_window(device->wined3d_device);
3834 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3835 wined3d_device_decref(device->wined3d_device);
3836 wined3d_mutex_unlock();
3837 return hr;
3840 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3842 wined3d_device_uninit_3d(device->wined3d_device);
3843 wined3d_device_release_focus_window(device->wined3d_device);
3844 wined3d_device_decref(device->wined3d_device);
3845 wined3d_mutex_unlock();
3846 return E_OUTOFMEMORY;
3849 for (i = 0; i < count; ++i)
3851 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3854 wined3d_mutex_unlock();
3856 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3858 /* Initialize the converted declaration array. This creates a valid pointer
3859 * and when adding decls HeapReAlloc() can be used without further checking. */
3860 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3861 if (!device->fvf_decls)
3863 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3864 wined3d_mutex_lock();
3865 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3866 wined3d_device_uninit_3d(device->wined3d_device);
3867 wined3d_device_release_focus_window(device->wined3d_device);
3868 wined3d_device_decref(device->wined3d_device);
3869 wined3d_mutex_unlock();
3870 return E_OUTOFMEMORY;
3873 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3874 device->d3d_parent = parent;
3876 return D3D_OK;