d3d9: Use wined3d_texture_get_sub_resource_desc() in d3d9_device_GetRenderTargetData().
[wine.git] / dlls / d3d9 / device.c
blobbd94712abf271592d4c96581f40a11420fb13acc
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;
609 wined3d_resource_get_desc(resource, &desc);
610 if (desc.pool == WINED3D_POOL_DEFAULT)
612 struct d3d9_surface *surface;
614 if (desc.resource_type == WINED3D_RTYPE_TEXTURE_2D)
616 IUnknown *parent = wined3d_resource_get_parent(resource);
617 IDirect3DBaseTexture9 *texture;
619 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
621 IDirect3DBaseTexture9_Release(texture);
622 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
623 return D3DERR_INVALIDCALL;
626 return D3D_OK;
629 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
631 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
632 return D3DERR_INVALIDCALL;
635 surface = wined3d_resource_get_parent(resource);
636 if (surface->resource.refcount)
638 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
639 return D3DERR_INVALIDCALL;
642 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
645 return D3D_OK;
648 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
650 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
651 struct wined3d_swapchain *wined3d_swapchain;
653 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
654 new_swapchain_count * sizeof(*device->implicit_swapchains))))
655 return E_OUTOFMEMORY;
657 for (i = 0; i < new_swapchain_count; ++i)
659 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
660 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
662 device->implicit_swapchain_count = new_swapchain_count;
664 return D3D_OK;
667 static HRESULT d3d9_device_reset(struct d3d9_device *device,
668 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
670 struct wined3d_swapchain_desc swapchain_desc;
671 struct wined3d_display_mode wined3d_mode;
672 HRESULT hr;
674 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
676 WARN("App not active, returning D3DERR_DEVICELOST.\n");
677 return D3DERR_DEVICELOST;
680 if (mode)
682 wined3d_mode.width = mode->Width;
683 wined3d_mode.height = mode->Height;
684 wined3d_mode.refresh_rate = mode->RefreshRate;
685 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
686 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
689 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
690 device->d3d_parent->extended))
691 return D3DERR_INVALIDCALL;
693 wined3d_mutex_lock();
695 if (device->vertex_buffer)
697 wined3d_buffer_decref(device->vertex_buffer);
698 device->vertex_buffer = NULL;
699 device->vertex_buffer_size = 0;
702 if (device->index_buffer)
704 wined3d_buffer_decref(device->index_buffer);
705 device->index_buffer = NULL;
706 device->index_buffer_size = 0;
709 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
710 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
712 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
714 if (FAILED(hr = d3d9_device_get_swapchains(device)))
716 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
718 else
720 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
721 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
722 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
723 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
724 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
726 device->device_state = D3D9_DEVICE_STATE_OK;
729 else if (!device->d3d_parent->extended)
731 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
734 wined3d_mutex_unlock();
736 return hr;
739 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
740 D3DPRESENT_PARAMETERS *present_parameters)
742 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
744 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
746 return d3d9_device_reset(device, present_parameters, NULL);
749 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
750 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
752 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
753 UINT i;
754 HRESULT hr;
756 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
757 iface, src_rect, dst_rect, dst_window_override, dirty_region);
759 if (device->device_state != D3D9_DEVICE_STATE_OK)
760 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
762 wined3d_mutex_lock();
763 for (i = 0; i < device->implicit_swapchain_count; ++i)
765 hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain, src_rect,
766 dst_rect, dst_window_override, dirty_region, 0);
767 if (FAILED(hr))
769 wined3d_mutex_unlock();
770 return hr;
773 wined3d_mutex_unlock();
775 return D3D_OK;
778 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
779 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
781 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
782 HRESULT hr;
784 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
785 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
787 /* backbuffer_type is ignored by native. */
789 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
790 *backbuffer = NULL;
792 wined3d_mutex_lock();
793 if (swapchain >= device->implicit_swapchain_count)
795 wined3d_mutex_unlock();
796 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
797 return D3DERR_INVALIDCALL;
800 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
801 backbuffer_idx, backbuffer_type, backbuffer);
802 wined3d_mutex_unlock();
804 return hr;
807 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
808 UINT swapchain, D3DRASTER_STATUS *raster_status)
810 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
811 HRESULT hr;
813 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
815 wined3d_mutex_lock();
816 hr = wined3d_device_get_raster_status(device->wined3d_device,
817 swapchain, (struct wined3d_raster_status *)raster_status);
818 wined3d_mutex_unlock();
820 return hr;
823 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
825 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
826 HRESULT hr;
828 TRACE("iface %p, enable %#x.\n", iface, enable);
830 wined3d_mutex_lock();
831 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
832 wined3d_mutex_unlock();
834 return hr;
837 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
838 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
840 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
842 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
844 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
845 wined3d_mutex_lock();
846 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
847 wined3d_mutex_unlock();
850 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
852 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
854 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
856 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
857 wined3d_mutex_lock();
858 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
859 wined3d_mutex_unlock();
862 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
863 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
864 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
866 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
867 struct d3d9_texture *object;
868 BOOL set_mem = FALSE;
869 HRESULT hr;
871 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
872 iface, width, height, levels, usage, format, pool, texture, shared_handle);
874 *texture = NULL;
875 if (shared_handle)
877 if (!device->d3d_parent->extended)
879 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
880 return E_NOTIMPL;
883 if (pool == D3DPOOL_SYSTEMMEM)
885 if (levels != 1)
886 return D3DERR_INVALIDCALL;
887 set_mem = TRUE;
889 else
891 if (pool != D3DPOOL_DEFAULT)
893 WARN("Trying to create a shared texture in pool %#x.\n", pool);
894 return D3DERR_INVALIDCALL;
896 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
900 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
901 if (!object)
902 return D3DERR_OUTOFVIDEOMEMORY;
904 hr = texture_init(object, device, width, height, levels, usage, format, pool);
905 if (FAILED(hr))
907 WARN("Failed to initialize texture, hr %#x.\n", hr);
908 HeapFree(GetProcessHeap(), 0, object);
909 return hr;
912 if (set_mem)
913 wined3d_texture_update_desc(object->wined3d_texture, width, height,
914 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
915 *shared_handle, 0);
917 TRACE("Created texture %p.\n", object);
918 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
920 return D3D_OK;
923 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
924 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
925 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
927 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
928 struct d3d9_texture *object;
929 HRESULT hr;
931 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
932 iface, width, height, depth, levels);
933 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
934 usage, format, pool, texture, shared_handle);
936 *texture = NULL;
937 if (shared_handle)
939 if (!device->d3d_parent->extended)
941 WARN("Trying to create a shared volume texture on a non-ex device.\n");
942 return E_NOTIMPL;
945 if (pool != D3DPOOL_DEFAULT)
947 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
948 return D3DERR_INVALIDCALL;
950 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
953 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
954 if (!object)
955 return D3DERR_OUTOFVIDEOMEMORY;
957 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
958 if (FAILED(hr))
960 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
961 HeapFree(GetProcessHeap(), 0, object);
962 return hr;
965 TRACE("Created volume texture %p.\n", object);
966 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
968 return D3D_OK;
971 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
972 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
973 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
975 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
976 struct d3d9_texture *object;
977 HRESULT hr;
979 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
980 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
982 *texture = NULL;
983 if (shared_handle)
985 if (!device->d3d_parent->extended)
987 WARN("Trying to create a shared cube texture on a non-ex device.\n");
988 return E_NOTIMPL;
991 if (pool != D3DPOOL_DEFAULT)
993 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
994 return D3DERR_INVALIDCALL;
996 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
999 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1000 if (!object)
1001 return D3DERR_OUTOFVIDEOMEMORY;
1003 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1004 if (FAILED(hr))
1006 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1007 HeapFree(GetProcessHeap(), 0, object);
1008 return hr;
1011 TRACE("Created cube texture %p.\n", object);
1012 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1014 return D3D_OK;
1017 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1018 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1019 HANDLE *shared_handle)
1021 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1022 struct d3d9_vertexbuffer *object;
1023 HRESULT hr;
1025 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1026 iface, size, usage, fvf, pool, buffer, shared_handle);
1028 if (shared_handle)
1030 if (!device->d3d_parent->extended)
1032 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1033 return E_NOTIMPL;
1036 if (pool != D3DPOOL_DEFAULT)
1038 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1039 return D3DERR_NOTAVAILABLE;
1041 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1044 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1045 if (!object)
1046 return D3DERR_OUTOFVIDEOMEMORY;
1048 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1049 if (FAILED(hr))
1051 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1052 HeapFree(GetProcessHeap(), 0, object);
1053 return hr;
1056 TRACE("Created vertex buffer %p.\n", object);
1057 *buffer = &object->IDirect3DVertexBuffer9_iface;
1059 return D3D_OK;
1062 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1063 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1064 HANDLE *shared_handle)
1066 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1067 struct d3d9_indexbuffer *object;
1068 HRESULT hr;
1070 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1071 iface, size, usage, format, pool, buffer, shared_handle);
1073 if (shared_handle)
1075 if (!device->d3d_parent->extended)
1077 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1078 return E_NOTIMPL;
1081 if (pool != D3DPOOL_DEFAULT)
1083 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1084 return D3DERR_NOTAVAILABLE;
1086 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1089 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1090 if (!object)
1091 return D3DERR_OUTOFVIDEOMEMORY;
1093 hr = indexbuffer_init(object, device, size, usage, format, pool);
1094 if (FAILED(hr))
1096 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1097 HeapFree(GetProcessHeap(), 0, object);
1098 return hr;
1101 TRACE("Created index buffer %p.\n", object);
1102 *buffer = &object->IDirect3DIndexBuffer9_iface;
1104 return D3D_OK;
1107 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1108 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1109 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1111 struct wined3d_resource_desc desc;
1112 struct d3d9_surface *surface_impl;
1113 struct wined3d_texture *texture;
1114 HRESULT hr;
1116 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1117 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1118 device, width, height, format, flags, surface, usage, pool,
1119 multisample_type, multisample_quality);
1121 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1122 desc.format = wined3dformat_from_d3dformat(format);
1123 desc.multisample_type = multisample_type;
1124 desc.multisample_quality = multisample_quality;
1125 desc.usage = usage & WINED3DUSAGE_MASK;
1126 desc.pool = pool;
1127 desc.width = width;
1128 desc.height = height;
1129 desc.depth = 1;
1130 desc.size = 0;
1132 wined3d_mutex_lock();
1134 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1135 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1137 wined3d_mutex_unlock();
1138 WARN("Failed to create texture, hr %#x.\n", hr);
1139 if (hr == WINED3DERR_NOTAVAILABLE)
1140 hr = D3DERR_INVALIDCALL;
1141 return hr;
1144 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1145 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1146 *surface = &surface_impl->IDirect3DSurface9_iface;
1147 IDirect3DSurface9_AddRef(*surface);
1149 if (user_mem)
1150 wined3d_texture_update_desc(texture, width, height,
1151 desc.format, multisample_type, multisample_quality, user_mem, 0);
1153 wined3d_texture_decref(texture);
1155 wined3d_mutex_unlock();
1157 return D3D_OK;
1160 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1161 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1162 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1164 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1165 DWORD flags = 0;
1167 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1168 "lockable %#x, surface %p, shared_handle %p.\n",
1169 iface, width, height, format, multisample_type, multisample_quality,
1170 lockable, surface, shared_handle);
1172 *surface = NULL;
1173 if (shared_handle)
1175 if (!device->d3d_parent->extended)
1177 WARN("Trying to create a shared render target on a non-ex device.\n");
1178 return E_NOTIMPL;
1181 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1184 if (lockable)
1185 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1187 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1188 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1191 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1192 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1193 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1195 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1196 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1198 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1199 "discard %#x, surface %p, shared_handle %p.\n",
1200 iface, width, height, format, multisample_type, multisample_quality,
1201 discard, surface, shared_handle);
1203 *surface = NULL;
1204 if (shared_handle)
1206 if (!device->d3d_parent->extended)
1208 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1209 return E_NOTIMPL;
1212 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1215 if (discard)
1216 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1218 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1219 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1223 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1224 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1225 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1227 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1228 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1229 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1230 struct wined3d_box src_box;
1231 HRESULT hr;
1233 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1234 iface, src_surface, src_rect, dst_surface, dst_point);
1236 if (src_rect)
1238 src_box.left = src_rect->left;
1239 src_box.top = src_rect->top;
1240 src_box.right = src_rect->right;
1241 src_box.bottom = src_rect->bottom;
1242 src_box.front = 0;
1243 src_box.back = 1;
1246 wined3d_mutex_lock();
1247 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1248 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1249 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1250 src->sub_resource_idx, src_rect ? &src_box : NULL);
1251 wined3d_mutex_unlock();
1253 if (FAILED(hr))
1254 return D3DERR_INVALIDCALL;
1256 return hr;
1259 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1260 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1262 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1263 struct d3d9_texture *src_impl, *dst_impl;
1264 HRESULT hr;
1266 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1268 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1269 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1271 wined3d_mutex_lock();
1272 hr = wined3d_device_update_texture(device->wined3d_device,
1273 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1274 wined3d_mutex_unlock();
1276 return hr;
1279 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1280 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1282 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1283 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1284 struct wined3d_sub_resource_desc wined3d_desc;
1285 RECT dst_rect, src_rect;
1286 HRESULT hr;
1288 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1290 wined3d_mutex_lock();
1291 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1292 dst_rect.left = 0;
1293 dst_rect.top = 0;
1294 dst_rect.right = wined3d_desc.width;
1295 dst_rect.bottom = wined3d_desc.height;
1297 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1298 src_rect.left = 0;
1299 src_rect.top = 0;
1300 src_rect.right = wined3d_desc.width;
1301 src_rect.bottom = wined3d_desc.height;
1303 /* TODO: Check surface sizes, pools, etc. */
1304 if (wined3d_desc.multisample_type)
1305 hr = D3DERR_INVALIDCALL;
1306 else
1307 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1308 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1309 wined3d_mutex_unlock();
1311 return hr;
1314 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1315 UINT swapchain, IDirect3DSurface9 *dst_surface)
1317 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1318 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1319 HRESULT hr = D3DERR_INVALIDCALL;
1321 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1323 wined3d_mutex_lock();
1324 if (swapchain < device->implicit_swapchain_count)
1325 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1326 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1327 wined3d_mutex_unlock();
1329 return hr;
1332 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1333 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1335 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1336 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1337 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1338 HRESULT hr = D3DERR_INVALIDCALL;
1339 struct wined3d_resource_desc src_desc, dst_desc;
1340 struct wined3d_resource *sub_resource;
1341 RECT d, s;
1343 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1344 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1346 wined3d_mutex_lock();
1347 sub_resource = wined3d_texture_get_sub_resource(dst->wined3d_texture, dst->sub_resource_idx);
1348 wined3d_resource_get_desc(sub_resource, &dst_desc);
1349 if (!dst_rect)
1351 d.left = 0;
1352 d.top = 0;
1353 d.right = dst_desc.width;
1354 d.bottom = dst_desc.height;
1355 dst_rect = &d;
1358 sub_resource = wined3d_texture_get_sub_resource(src->wined3d_texture, src->sub_resource_idx);
1359 wined3d_resource_get_desc(sub_resource, &src_desc);
1360 if (!src_rect)
1362 s.left = 0;
1363 s.top = 0;
1364 s.right = src_desc.width;
1365 s.bottom = src_desc.height;
1366 src_rect = &s;
1369 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1371 if (device->in_scene)
1373 WARN("Rejecting depth / stencil blit while in scene.\n");
1374 goto done;
1377 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1378 || src_rect->bottom != src_desc.height)
1380 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1381 wine_dbgstr_rect(src_rect));
1382 goto done;
1385 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1386 || dst_rect->bottom != dst_desc.height)
1388 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1389 wine_dbgstr_rect(dst_rect));
1390 goto done;
1393 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1395 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1396 goto done;
1400 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1401 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1402 if (hr == WINEDDERR_INVALIDRECT)
1403 hr = D3DERR_INVALIDCALL;
1405 done:
1406 wined3d_mutex_unlock();
1407 return hr;
1410 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1411 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1413 const struct wined3d_color c =
1415 ((color >> 16) & 0xff) / 255.0f,
1416 ((color >> 8) & 0xff) / 255.0f,
1417 (color & 0xff) / 255.0f,
1418 ((color >> 24) & 0xff) / 255.0f,
1420 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1421 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1422 struct wined3d_resource *wined3d_resource;
1423 struct wined3d_resource_desc desc;
1424 HRESULT hr;
1426 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1428 wined3d_mutex_lock();
1430 if (!(wined3d_resource = wined3d_texture_get_sub_resource(surface_impl->wined3d_texture, surface_impl->sub_resource_idx)))
1432 wined3d_mutex_unlock();
1433 return D3DERR_INVALIDCALL;
1435 wined3d_resource_get_desc(wined3d_resource, &desc);
1437 if (desc.pool != WINED3D_POOL_DEFAULT)
1439 wined3d_mutex_unlock();
1440 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1441 return D3DERR_INVALIDCALL;
1443 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1445 wined3d_mutex_unlock();
1446 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1447 return D3DERR_INVALIDCALL;
1449 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1451 wined3d_mutex_unlock();
1452 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1453 return D3DERR_INVALIDCALL;
1456 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1457 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1459 wined3d_mutex_unlock();
1461 return hr;
1464 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1465 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1466 HANDLE *shared_handle)
1468 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1469 void *user_mem = NULL;
1471 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1472 iface, width, height, format, pool, surface, shared_handle);
1474 *surface = NULL;
1475 if (pool == D3DPOOL_MANAGED)
1477 WARN("Attempting to create a managed offscreen plain surface.\n");
1478 return D3DERR_INVALIDCALL;
1481 if (shared_handle)
1483 if (!device->d3d_parent->extended)
1485 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1486 return E_NOTIMPL;
1489 if (pool == D3DPOOL_SYSTEMMEM)
1490 user_mem = *shared_handle;
1491 else
1493 if (pool != D3DPOOL_DEFAULT)
1495 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1496 return D3DERR_INVALIDCALL;
1498 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1502 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1503 * regardless of the pool they're created in. Should we set dynamic usage
1504 * here? */
1505 return d3d9_device_create_surface(device, width, height, format,
1506 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1509 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1511 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1512 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1513 HRESULT hr;
1515 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1517 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1519 WARN("Invalid index %u specified.\n", idx);
1520 return D3DERR_INVALIDCALL;
1523 if (!idx && !surface_impl)
1525 WARN("Trying to set render target 0 to NULL.\n");
1526 return D3DERR_INVALIDCALL;
1529 wined3d_mutex_lock();
1530 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1531 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1532 wined3d_mutex_unlock();
1534 return hr;
1537 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1539 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1540 struct wined3d_rendertarget_view *wined3d_rtv;
1541 struct d3d9_surface *surface_impl;
1542 HRESULT hr = D3D_OK;
1544 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1546 if (!surface)
1547 return D3DERR_INVALIDCALL;
1549 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1551 WARN("Invalid index %u specified.\n", idx);
1552 return D3DERR_INVALIDCALL;
1555 wined3d_mutex_lock();
1556 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1558 /* We want the sub resource parent here, since the view itself may be
1559 * internal to wined3d and may not have a parent. */
1560 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1561 *surface = &surface_impl->IDirect3DSurface9_iface;
1562 IDirect3DSurface9_AddRef(*surface);
1564 else
1566 hr = D3DERR_NOTFOUND;
1567 *surface = NULL;
1569 wined3d_mutex_unlock();
1571 return hr;
1574 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1576 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1577 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1579 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1581 wined3d_mutex_lock();
1582 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1583 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1584 wined3d_mutex_unlock();
1586 return D3D_OK;
1589 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1591 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1592 struct wined3d_rendertarget_view *wined3d_dsv;
1593 struct d3d9_surface *surface_impl;
1594 HRESULT hr = D3D_OK;
1596 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1598 if (!depth_stencil)
1599 return D3DERR_INVALIDCALL;
1601 wined3d_mutex_lock();
1602 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1604 /* We want the sub resource parent here, since the view itself may be
1605 * internal to wined3d and may not have a parent. */
1606 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1607 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1608 IDirect3DSurface9_AddRef(*depth_stencil);
1610 else
1612 hr = D3DERR_NOTFOUND;
1613 *depth_stencil = NULL;
1615 wined3d_mutex_unlock();
1617 return hr;
1620 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1622 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1623 HRESULT hr;
1625 TRACE("iface %p.\n", iface);
1627 wined3d_mutex_lock();
1628 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1629 device->in_scene = TRUE;
1630 wined3d_mutex_unlock();
1632 return hr;
1635 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1637 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1638 HRESULT hr;
1640 TRACE("iface %p.\n", iface);
1642 wined3d_mutex_lock();
1643 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1644 device->in_scene = FALSE;
1645 wined3d_mutex_unlock();
1647 return hr;
1650 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1651 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1653 const struct wined3d_color c =
1655 ((color >> 16) & 0xff) / 255.0f,
1656 ((color >> 8) & 0xff) / 255.0f,
1657 (color & 0xff) / 255.0f,
1658 ((color >> 24) & 0xff) / 255.0f,
1660 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1661 HRESULT hr;
1663 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1664 iface, rect_count, rects, flags, color, z, stencil);
1666 wined3d_mutex_lock();
1667 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1668 wined3d_mutex_unlock();
1670 return hr;
1673 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1674 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1676 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1678 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1680 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1681 wined3d_mutex_lock();
1682 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1683 wined3d_mutex_unlock();
1685 return D3D_OK;
1688 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1689 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1691 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1693 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1695 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1696 wined3d_mutex_lock();
1697 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1698 wined3d_mutex_unlock();
1700 return D3D_OK;
1703 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1704 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1706 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1708 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1710 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1711 wined3d_mutex_lock();
1712 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1713 wined3d_mutex_unlock();
1715 return D3D_OK;
1718 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1720 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1722 TRACE("iface %p, viewport %p.\n", iface, viewport);
1724 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1725 wined3d_mutex_lock();
1726 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1727 wined3d_mutex_unlock();
1729 return D3D_OK;
1732 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1734 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1736 TRACE("iface %p, viewport %p.\n", iface, viewport);
1738 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1739 wined3d_mutex_lock();
1740 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1741 wined3d_mutex_unlock();
1743 return D3D_OK;
1746 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1750 TRACE("iface %p, material %p.\n", iface, material);
1752 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1753 wined3d_mutex_lock();
1754 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1755 wined3d_mutex_unlock();
1757 return D3D_OK;
1760 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1762 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1764 TRACE("iface %p, material %p.\n", iface, material);
1766 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1767 wined3d_mutex_lock();
1768 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1769 wined3d_mutex_unlock();
1771 return D3D_OK;
1774 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1776 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1777 HRESULT hr;
1779 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1781 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1782 wined3d_mutex_lock();
1783 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1784 wined3d_mutex_unlock();
1786 return hr;
1789 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1791 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1792 HRESULT hr;
1794 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1796 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1797 wined3d_mutex_lock();
1798 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1799 wined3d_mutex_unlock();
1801 return hr;
1804 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1806 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1807 HRESULT hr;
1809 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1811 wined3d_mutex_lock();
1812 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1813 wined3d_mutex_unlock();
1815 return hr;
1818 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1820 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1821 HRESULT hr;
1823 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1825 wined3d_mutex_lock();
1826 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1827 wined3d_mutex_unlock();
1829 return hr;
1832 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1834 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1835 HRESULT hr;
1837 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1839 wined3d_mutex_lock();
1840 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1841 wined3d_mutex_unlock();
1843 return hr;
1846 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1848 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1849 HRESULT hr;
1851 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1853 wined3d_mutex_lock();
1854 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1855 wined3d_mutex_unlock();
1857 return hr;
1860 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1861 D3DRENDERSTATETYPE state, DWORD value)
1863 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1865 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1867 wined3d_mutex_lock();
1868 wined3d_device_set_render_state(device->wined3d_device, state, value);
1869 wined3d_mutex_unlock();
1871 return D3D_OK;
1874 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1875 D3DRENDERSTATETYPE state, DWORD *value)
1877 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1879 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1881 wined3d_mutex_lock();
1882 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1883 wined3d_mutex_unlock();
1885 return D3D_OK;
1888 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1889 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1891 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1892 struct d3d9_stateblock *object;
1893 HRESULT hr;
1895 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1897 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1899 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1900 return D3DERR_INVALIDCALL;
1903 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1904 if (!object)
1905 return E_OUTOFMEMORY;
1907 hr = stateblock_init(object, device, type, NULL);
1908 if (FAILED(hr))
1910 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1911 HeapFree(GetProcessHeap(), 0, object);
1912 return hr;
1915 TRACE("Created stateblock %p.\n", object);
1916 *stateblock = &object->IDirect3DStateBlock9_iface;
1918 return D3D_OK;
1921 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1923 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1924 HRESULT hr;
1926 TRACE("iface %p.\n", iface);
1928 wined3d_mutex_lock();
1929 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1930 wined3d_mutex_unlock();
1932 return hr;
1935 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1937 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1938 struct wined3d_stateblock *wined3d_stateblock;
1939 struct d3d9_stateblock *object;
1940 HRESULT hr;
1942 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1944 wined3d_mutex_lock();
1945 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1946 wined3d_mutex_unlock();
1947 if (FAILED(hr))
1949 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1950 return hr;
1953 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1954 if (!object)
1956 wined3d_mutex_lock();
1957 wined3d_stateblock_decref(wined3d_stateblock);
1958 wined3d_mutex_unlock();
1959 return E_OUTOFMEMORY;
1962 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1963 if (FAILED(hr))
1965 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1966 wined3d_mutex_lock();
1967 wined3d_stateblock_decref(wined3d_stateblock);
1968 wined3d_mutex_unlock();
1969 HeapFree(GetProcessHeap(), 0, object);
1970 return hr;
1973 TRACE("Created stateblock %p.\n", object);
1974 *stateblock = &object->IDirect3DStateBlock9_iface;
1976 return D3D_OK;
1979 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1981 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1982 HRESULT hr;
1984 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1986 wined3d_mutex_lock();
1987 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1988 wined3d_mutex_unlock();
1990 return hr;
1993 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1995 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1996 HRESULT hr;
1998 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2000 wined3d_mutex_lock();
2001 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2002 wined3d_mutex_unlock();
2004 return hr;
2007 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2009 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2010 struct wined3d_texture *wined3d_texture = NULL;
2011 struct d3d9_texture *texture_impl;
2013 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2015 if (!texture)
2016 return D3DERR_INVALIDCALL;
2018 wined3d_mutex_lock();
2019 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2021 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2022 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2023 IDirect3DBaseTexture9_AddRef(*texture);
2025 else
2027 *texture = NULL;
2029 wined3d_mutex_unlock();
2031 return D3D_OK;
2034 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2036 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2037 struct d3d9_texture *texture_impl;
2038 HRESULT hr;
2040 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2042 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2044 wined3d_mutex_lock();
2045 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2046 texture_impl ? texture_impl->wined3d_texture : NULL);
2047 wined3d_mutex_unlock();
2049 return hr;
2052 static const enum wined3d_texture_stage_state tss_lookup[] =
2054 WINED3D_TSS_INVALID, /* 0, unused */
2055 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2056 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2057 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2058 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2059 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2060 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2061 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2062 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2063 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2064 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2065 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2066 WINED3D_TSS_INVALID, /* 12, unused */
2067 WINED3D_TSS_INVALID, /* 13, unused */
2068 WINED3D_TSS_INVALID, /* 14, unused */
2069 WINED3D_TSS_INVALID, /* 15, unused */
2070 WINED3D_TSS_INVALID, /* 16, unused */
2071 WINED3D_TSS_INVALID, /* 17, unused */
2072 WINED3D_TSS_INVALID, /* 18, unused */
2073 WINED3D_TSS_INVALID, /* 19, unused */
2074 WINED3D_TSS_INVALID, /* 20, unused */
2075 WINED3D_TSS_INVALID, /* 21, unused */
2076 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2077 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2078 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2079 WINED3D_TSS_INVALID, /* 25, unused */
2080 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2081 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2082 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2083 WINED3D_TSS_INVALID, /* 29, unused */
2084 WINED3D_TSS_INVALID, /* 30, unused */
2085 WINED3D_TSS_INVALID, /* 31, unused */
2086 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2089 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2090 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2092 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2094 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2096 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2098 WARN("Invalid state %#x passed.\n", state);
2099 return D3D_OK;
2102 wined3d_mutex_lock();
2103 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2104 wined3d_mutex_unlock();
2106 return D3D_OK;
2109 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2110 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2112 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2114 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2116 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2118 WARN("Invalid state %#x passed.\n", state);
2119 return D3D_OK;
2122 wined3d_mutex_lock();
2123 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2124 wined3d_mutex_unlock();
2126 return D3D_OK;
2129 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2130 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2132 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2134 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2136 wined3d_mutex_lock();
2137 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2138 wined3d_mutex_unlock();
2140 return D3D_OK;
2143 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2144 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2146 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2148 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2150 wined3d_mutex_lock();
2151 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2152 wined3d_mutex_unlock();
2154 return D3D_OK;
2157 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2159 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2160 HRESULT hr;
2162 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2164 wined3d_mutex_lock();
2165 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2166 wined3d_mutex_unlock();
2168 return hr;
2171 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2172 UINT palette_idx, const PALETTEENTRY *entries)
2174 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2176 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2177 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2178 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2180 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2181 return D3D_OK;
2184 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2185 UINT palette_idx, PALETTEENTRY *entries)
2187 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2189 return D3DERR_INVALIDCALL;
2192 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2194 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2196 return D3D_OK;
2199 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2201 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2203 return D3DERR_INVALIDCALL;
2206 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2208 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2210 TRACE("iface %p, rect %p.\n", iface, rect);
2212 wined3d_mutex_lock();
2213 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2214 wined3d_mutex_unlock();
2216 return D3D_OK;
2219 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2221 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2223 TRACE("iface %p, rect %p.\n", iface, rect);
2225 wined3d_mutex_lock();
2226 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2227 wined3d_mutex_unlock();
2229 return D3D_OK;
2232 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2234 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2236 TRACE("iface %p, software %#x.\n", iface, software);
2238 wined3d_mutex_lock();
2239 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2240 wined3d_mutex_unlock();
2242 return D3D_OK;
2245 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2247 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2248 BOOL ret;
2250 TRACE("iface %p.\n", iface);
2252 wined3d_mutex_lock();
2253 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2254 wined3d_mutex_unlock();
2256 return ret;
2259 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2261 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2262 HRESULT hr;
2264 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2266 wined3d_mutex_lock();
2267 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2268 wined3d_mutex_unlock();
2270 return hr;
2273 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2275 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2276 float ret;
2278 TRACE("iface %p.\n", iface);
2280 wined3d_mutex_lock();
2281 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2282 wined3d_mutex_unlock();
2284 return ret;
2287 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2288 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2290 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2291 HRESULT hr;
2293 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2294 iface, primitive_type, start_vertex, primitive_count);
2296 wined3d_mutex_lock();
2297 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2298 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2299 vertex_count_from_primitive_count(primitive_type, primitive_count));
2300 wined3d_mutex_unlock();
2302 return hr;
2305 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2306 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2307 UINT vertex_count, UINT start_idx, UINT primitive_count)
2309 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2310 HRESULT hr;
2312 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2313 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2314 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2315 vertex_count, start_idx, primitive_count);
2317 wined3d_mutex_lock();
2318 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2319 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2320 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2321 vertex_count_from_primitive_count(primitive_type, primitive_count));
2322 wined3d_mutex_unlock();
2324 return hr;
2327 /* The caller is responsible for wined3d locking */
2328 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2330 HRESULT hr;
2332 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2334 UINT size = max(device->vertex_buffer_size * 2, min_size);
2335 struct wined3d_buffer *buffer;
2337 TRACE("Growing vertex buffer to %u bytes\n", size);
2339 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2340 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2341 if (FAILED(hr))
2343 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2344 return hr;
2347 if (device->vertex_buffer)
2348 wined3d_buffer_decref(device->vertex_buffer);
2350 device->vertex_buffer = buffer;
2351 device->vertex_buffer_size = size;
2352 device->vertex_buffer_pos = 0;
2354 return D3D_OK;
2357 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2358 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2360 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2361 HRESULT hr;
2362 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2363 UINT size = vtx_count * stride;
2364 UINT vb_pos, align;
2365 BYTE *buffer_data;
2367 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2368 iface, primitive_type, primitive_count, data, stride);
2370 if (!primitive_count)
2372 WARN("primitive_count is 0, returning D3D_OK\n");
2373 return D3D_OK;
2376 wined3d_mutex_lock();
2378 hr = d3d9_device_prepare_vertex_buffer(device, size);
2379 if (FAILED(hr))
2380 goto done;
2382 vb_pos = device->vertex_buffer_pos;
2383 align = vb_pos % stride;
2384 if (align) align = stride - align;
2385 if (vb_pos + size + align > device->vertex_buffer_size)
2386 vb_pos = 0;
2387 else
2388 vb_pos += align;
2390 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2391 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2392 if (FAILED(hr))
2393 goto done;
2394 memcpy(buffer_data, data, size);
2395 wined3d_buffer_unmap(device->vertex_buffer);
2396 device->vertex_buffer_pos = vb_pos + size;
2398 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2399 if (FAILED(hr))
2400 goto done;
2402 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2403 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2404 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2406 done:
2407 wined3d_mutex_unlock();
2408 return hr;
2411 /* The caller is responsible for wined3d locking */
2412 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2414 HRESULT hr;
2416 if (device->index_buffer_size < min_size || !device->index_buffer)
2418 UINT size = max(device->index_buffer_size * 2, min_size);
2419 struct wined3d_buffer *buffer;
2421 TRACE("Growing index buffer to %u bytes\n", size);
2423 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2424 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2425 if (FAILED(hr))
2427 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2428 return hr;
2431 if (device->index_buffer)
2432 wined3d_buffer_decref(device->index_buffer);
2434 device->index_buffer = buffer;
2435 device->index_buffer_size = size;
2436 device->index_buffer_pos = 0;
2438 return D3D_OK;
2441 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2442 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2443 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2444 const void *vertex_data, UINT vertex_stride)
2446 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2447 HRESULT hr;
2448 BYTE *buffer_data;
2450 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2451 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2452 UINT idx_size = idx_count * idx_fmt_size;
2453 UINT ib_pos;
2455 UINT vtx_size = vertex_count * vertex_stride;
2456 UINT vb_pos, align;
2458 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2459 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2460 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2461 index_data, index_format, vertex_data, vertex_stride);
2463 if (!primitive_count)
2465 WARN("primitive_count is 0, returning D3D_OK\n");
2466 return D3D_OK;
2469 wined3d_mutex_lock();
2471 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2472 if (FAILED(hr))
2473 goto done;
2475 vb_pos = device->vertex_buffer_pos;
2476 align = vb_pos % vertex_stride;
2477 if (align) align = vertex_stride - align;
2478 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2479 vb_pos = 0;
2480 else
2481 vb_pos += align;
2483 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2484 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2485 if (FAILED(hr))
2486 goto done;
2487 memcpy(buffer_data, vertex_data, vtx_size);
2488 wined3d_buffer_unmap(device->vertex_buffer);
2489 device->vertex_buffer_pos = vb_pos + vtx_size;
2491 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2492 if (FAILED(hr))
2493 goto done;
2495 ib_pos = device->index_buffer_pos;
2496 align = ib_pos % idx_fmt_size;
2497 if (align) align = idx_fmt_size - align;
2498 if (ib_pos + idx_size + align > device->index_buffer_size)
2499 ib_pos = 0;
2500 else
2501 ib_pos += align;
2503 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2504 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2505 if (FAILED(hr))
2506 goto done;
2507 memcpy(buffer_data, index_data, idx_size);
2508 wined3d_buffer_unmap(device->index_buffer);
2509 device->index_buffer_pos = ib_pos + idx_size;
2511 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2512 if (FAILED(hr))
2513 goto done;
2515 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2516 wined3dformat_from_d3dformat(index_format));
2517 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2519 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2520 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2522 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2523 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2524 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2526 done:
2527 wined3d_mutex_unlock();
2528 return hr;
2531 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2532 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2533 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2535 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2536 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2537 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2538 HRESULT hr;
2540 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2541 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2543 wined3d_mutex_lock();
2544 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2545 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2546 flags, dst_impl->fvf);
2547 wined3d_mutex_unlock();
2549 return hr;
2552 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2553 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2555 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2556 struct d3d9_vertex_declaration *object;
2557 HRESULT hr;
2559 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2561 if (!declaration)
2563 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2564 return D3DERR_INVALIDCALL;
2567 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2568 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2570 return hr;
2573 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2574 IDirect3DVertexDeclaration9 *declaration)
2576 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2577 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2579 TRACE("iface %p, declaration %p.\n", iface, declaration);
2581 wined3d_mutex_lock();
2582 wined3d_device_set_vertex_declaration(device->wined3d_device,
2583 decl_impl ? decl_impl->wined3d_declaration : NULL);
2584 wined3d_mutex_unlock();
2586 return D3D_OK;
2589 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2590 IDirect3DVertexDeclaration9 **declaration)
2592 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2593 struct wined3d_vertex_declaration *wined3d_declaration;
2594 struct d3d9_vertex_declaration *declaration_impl;
2596 TRACE("iface %p, declaration %p.\n", iface, declaration);
2598 if (!declaration) return D3DERR_INVALIDCALL;
2600 wined3d_mutex_lock();
2601 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2603 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2604 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2605 IDirect3DVertexDeclaration9_AddRef(*declaration);
2607 else
2609 *declaration = NULL;
2611 wined3d_mutex_unlock();
2613 TRACE("Returning %p.\n", *declaration);
2614 return D3D_OK;
2617 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2619 struct wined3d_vertex_declaration *wined3d_declaration;
2620 struct fvf_declaration *fvf_decls = device->fvf_decls;
2621 struct d3d9_vertex_declaration *d3d9_declaration;
2622 D3DVERTEXELEMENT9 *elements;
2623 int p, low, high; /* deliberately signed */
2624 HRESULT hr;
2626 TRACE("Searching for declaration for fvf %08x... ", fvf);
2628 low = 0;
2629 high = device->fvf_decl_count - 1;
2630 while (low <= high)
2632 p = (low + high) >> 1;
2633 TRACE("%d ", p);
2635 if (fvf_decls[p].fvf == fvf)
2637 TRACE("found %p.\n", fvf_decls[p].decl);
2638 return fvf_decls[p].decl;
2641 if (fvf_decls[p].fvf < fvf)
2642 low = p + 1;
2643 else
2644 high = p - 1;
2646 TRACE("not found. Creating and inserting at position %d.\n", low);
2648 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2649 return NULL;
2651 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2652 HeapFree(GetProcessHeap(), 0, elements);
2653 if (FAILED(hr))
2654 return NULL;
2656 if (device->fvf_decl_size == device->fvf_decl_count)
2658 UINT grow = max(device->fvf_decl_size / 2, 8);
2660 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2661 if (!fvf_decls)
2663 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2664 return NULL;
2666 device->fvf_decls = fvf_decls;
2667 device->fvf_decl_size += grow;
2670 d3d9_declaration->fvf = fvf;
2671 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2672 wined3d_vertex_declaration_incref(wined3d_declaration);
2673 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2675 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2676 fvf_decls[low].decl = wined3d_declaration;
2677 fvf_decls[low].fvf = fvf;
2678 ++device->fvf_decl_count;
2680 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2682 return wined3d_declaration;
2685 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2687 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2688 struct wined3d_vertex_declaration *decl;
2690 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2692 if (!fvf)
2694 WARN("%#x is not a valid FVF.\n", fvf);
2695 return D3D_OK;
2698 wined3d_mutex_lock();
2699 if (!(decl = device_get_fvf_declaration(device, fvf)))
2701 wined3d_mutex_unlock();
2702 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2703 return D3DERR_DRIVERINTERNALERROR;
2706 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2707 wined3d_mutex_unlock();
2709 return D3D_OK;
2712 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2714 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2715 struct wined3d_vertex_declaration *wined3d_declaration;
2716 struct d3d9_vertex_declaration *d3d9_declaration;
2718 TRACE("iface %p, fvf %p.\n", iface, fvf);
2720 wined3d_mutex_lock();
2721 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2723 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2724 *fvf = d3d9_declaration->fvf;
2726 else
2728 *fvf = 0;
2730 wined3d_mutex_unlock();
2732 TRACE("Returning FVF %#x.\n", *fvf);
2734 return D3D_OK;
2737 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2738 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2740 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2741 struct d3d9_vertexshader *object;
2742 HRESULT hr;
2744 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2746 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2747 if (!object)
2748 return E_OUTOFMEMORY;
2750 hr = vertexshader_init(object, device, byte_code);
2751 if (FAILED(hr))
2753 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2754 HeapFree(GetProcessHeap(), 0, object);
2755 return hr;
2758 TRACE("Created vertex shader %p.\n", object);
2759 *shader = &object->IDirect3DVertexShader9_iface;
2761 return D3D_OK;
2764 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2766 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2767 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2769 TRACE("iface %p, shader %p.\n", iface, shader);
2771 wined3d_mutex_lock();
2772 wined3d_device_set_vertex_shader(device->wined3d_device,
2773 shader_obj ? shader_obj->wined3d_shader : NULL);
2774 wined3d_mutex_unlock();
2776 return D3D_OK;
2779 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2781 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2782 struct d3d9_vertexshader *shader_impl;
2783 struct wined3d_shader *wined3d_shader;
2785 TRACE("iface %p, shader %p.\n", iface, shader);
2787 wined3d_mutex_lock();
2788 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2790 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2791 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2792 IDirect3DVertexShader9_AddRef(*shader);
2794 else
2796 *shader = NULL;
2798 wined3d_mutex_unlock();
2800 TRACE("Returning %p.\n", *shader);
2802 return D3D_OK;
2805 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2806 UINT reg_idx, const float *data, UINT count)
2808 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2809 HRESULT hr;
2811 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2813 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2815 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2816 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2817 return D3DERR_INVALIDCALL;
2820 wined3d_mutex_lock();
2821 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2822 wined3d_mutex_unlock();
2824 return hr;
2827 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2828 UINT reg_idx, float *data, UINT count)
2830 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2831 HRESULT hr;
2833 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2835 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2837 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2838 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2839 return D3DERR_INVALIDCALL;
2842 wined3d_mutex_lock();
2843 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2844 wined3d_mutex_unlock();
2846 return hr;
2849 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2850 UINT reg_idx, const int *data, UINT count)
2852 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2853 HRESULT hr;
2855 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2857 wined3d_mutex_lock();
2858 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2859 wined3d_mutex_unlock();
2861 return hr;
2864 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2865 UINT reg_idx, int *data, UINT count)
2867 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2868 HRESULT hr;
2870 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2872 wined3d_mutex_lock();
2873 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2874 wined3d_mutex_unlock();
2876 return hr;
2879 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2880 UINT reg_idx, const BOOL *data, UINT count)
2882 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2883 HRESULT hr;
2885 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2887 wined3d_mutex_lock();
2888 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2889 wined3d_mutex_unlock();
2891 return hr;
2894 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2895 UINT reg_idx, BOOL *data, UINT count)
2897 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2898 HRESULT hr;
2900 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2902 wined3d_mutex_lock();
2903 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2904 wined3d_mutex_unlock();
2906 return hr;
2909 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2910 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2912 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2913 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2914 HRESULT hr;
2916 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2917 iface, stream_idx, buffer, offset, stride);
2919 wined3d_mutex_lock();
2920 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2921 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2922 wined3d_mutex_unlock();
2924 return hr;
2927 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2928 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2930 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2931 struct d3d9_vertexbuffer *buffer_impl;
2932 struct wined3d_buffer *wined3d_buffer;
2933 HRESULT hr;
2935 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2936 iface, stream_idx, buffer, offset, stride);
2938 if (!buffer)
2939 return D3DERR_INVALIDCALL;
2941 wined3d_mutex_lock();
2942 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2943 if (SUCCEEDED(hr) && wined3d_buffer)
2945 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2946 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2947 IDirect3DVertexBuffer9_AddRef(*buffer);
2949 else
2951 if (FAILED(hr))
2952 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2953 *buffer = NULL;
2955 wined3d_mutex_unlock();
2957 return hr;
2960 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2962 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2963 HRESULT hr;
2965 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2967 wined3d_mutex_lock();
2968 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2969 wined3d_mutex_unlock();
2971 return hr;
2974 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2976 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2977 HRESULT hr;
2979 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2981 wined3d_mutex_lock();
2982 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2983 wined3d_mutex_unlock();
2985 return hr;
2988 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2990 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2991 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2993 TRACE("iface %p, buffer %p.\n", iface, buffer);
2995 wined3d_mutex_lock();
2996 wined3d_device_set_index_buffer(device->wined3d_device,
2997 ib ? ib->wined3d_buffer : NULL,
2998 ib ? ib->format : WINED3DFMT_UNKNOWN);
2999 wined3d_mutex_unlock();
3001 return D3D_OK;
3004 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3006 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3007 enum wined3d_format_id wined3d_format;
3008 struct wined3d_buffer *wined3d_buffer;
3009 struct d3d9_indexbuffer *buffer_impl;
3011 TRACE("iface %p, buffer %p.\n", iface, buffer);
3013 if (!buffer)
3014 return D3DERR_INVALIDCALL;
3016 wined3d_mutex_lock();
3017 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
3019 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3020 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3021 IDirect3DIndexBuffer9_AddRef(*buffer);
3023 else
3025 *buffer = NULL;
3027 wined3d_mutex_unlock();
3029 return D3D_OK;
3032 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3033 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3035 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3036 struct d3d9_pixelshader *object;
3037 HRESULT hr;
3039 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3041 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3042 if (!object)
3044 FIXME("Failed to allocate pixel shader memory.\n");
3045 return E_OUTOFMEMORY;
3048 hr = pixelshader_init(object, device, byte_code);
3049 if (FAILED(hr))
3051 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3052 HeapFree(GetProcessHeap(), 0, object);
3053 return hr;
3056 TRACE("Created pixel shader %p.\n", object);
3057 *shader = &object->IDirect3DPixelShader9_iface;
3059 return D3D_OK;
3062 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3064 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3065 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3067 TRACE("iface %p, shader %p.\n", iface, shader);
3069 wined3d_mutex_lock();
3070 wined3d_device_set_pixel_shader(device->wined3d_device,
3071 shader_obj ? shader_obj->wined3d_shader : NULL);
3072 wined3d_mutex_unlock();
3074 return D3D_OK;
3077 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3079 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3080 struct d3d9_pixelshader *shader_impl;
3081 struct wined3d_shader *wined3d_shader;
3083 TRACE("iface %p, shader %p.\n", iface, shader);
3085 if (!shader) return D3DERR_INVALIDCALL;
3087 wined3d_mutex_lock();
3088 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3090 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3091 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3092 IDirect3DPixelShader9_AddRef(*shader);
3094 else
3096 *shader = NULL;
3098 wined3d_mutex_unlock();
3100 TRACE("Returning %p.\n", *shader);
3102 return D3D_OK;
3105 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3106 UINT reg_idx, const float *data, UINT count)
3108 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3109 HRESULT hr;
3111 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3113 wined3d_mutex_lock();
3114 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3115 wined3d_mutex_unlock();
3117 return hr;
3120 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3121 UINT reg_idx, float *data, UINT count)
3123 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3124 HRESULT hr;
3126 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3128 wined3d_mutex_lock();
3129 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3130 wined3d_mutex_unlock();
3132 return hr;
3135 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3136 UINT reg_idx, const int *data, UINT count)
3138 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3139 HRESULT hr;
3141 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3143 wined3d_mutex_lock();
3144 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3145 wined3d_mutex_unlock();
3147 return hr;
3150 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3151 UINT reg_idx, int *data, UINT count)
3153 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3154 HRESULT hr;
3156 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3158 wined3d_mutex_lock();
3159 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3160 wined3d_mutex_unlock();
3162 return hr;
3165 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3166 UINT reg_idx, const BOOL *data, UINT count)
3168 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3169 HRESULT hr;
3171 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3173 wined3d_mutex_lock();
3174 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3175 wined3d_mutex_unlock();
3177 return hr;
3180 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3181 UINT reg_idx, BOOL *data, UINT count)
3183 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3184 HRESULT hr;
3186 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3188 wined3d_mutex_lock();
3189 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3190 wined3d_mutex_unlock();
3192 return hr;
3195 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3196 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3198 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3199 iface, handle, segment_count, patch_info);
3200 return D3D_OK;
3203 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3204 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3206 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3207 iface, handle, segment_count, patch_info);
3208 return D3D_OK;
3211 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3213 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3214 return D3DERR_INVALIDCALL;
3217 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3219 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3220 struct d3d9_query *object;
3221 HRESULT hr;
3223 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3225 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3226 if (!object)
3227 return E_OUTOFMEMORY;
3229 hr = query_init(object, device, type);
3230 if (FAILED(hr))
3232 WARN("Failed to initialize query, hr %#x.\n", hr);
3233 HeapFree(GetProcessHeap(), 0, object);
3234 return hr;
3237 TRACE("Created query %p.\n", object);
3238 if (query) *query = &object->IDirect3DQuery9_iface;
3239 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3241 return D3D_OK;
3244 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3245 UINT width, UINT height, float *rows, float *columns)
3247 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3248 iface, width, height, rows, columns);
3250 return E_NOTIMPL;
3253 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3254 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3255 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3257 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3258 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3259 iface, src_surface, dst_surface, src_descs, rect_count,
3260 dst_descs, operation, offset_x, offset_y);
3262 return E_NOTIMPL;
3265 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3266 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3267 const RGNDATA *dirty_region, DWORD flags)
3269 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3270 UINT i;
3271 HRESULT hr;
3273 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3274 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3275 dst_window_override, dirty_region, flags);
3277 if (device->device_state != D3D9_DEVICE_STATE_OK)
3278 return S_PRESENT_OCCLUDED;
3280 wined3d_mutex_lock();
3281 for (i = 0; i < device->implicit_swapchain_count; ++i)
3283 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain, src_rect,
3284 dst_rect, dst_window_override, dirty_region, flags)))
3286 wined3d_mutex_unlock();
3287 return hr;
3290 wined3d_mutex_unlock();
3292 return D3D_OK;
3295 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3297 FIXME("iface %p, priority %p stub!\n", iface, priority);
3299 return E_NOTIMPL;
3302 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3304 FIXME("iface %p, priority %d stub!\n", iface, priority);
3306 return E_NOTIMPL;
3309 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3311 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3313 return E_NOTIMPL;
3316 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3317 IDirect3DResource9 **resources, UINT32 resource_count)
3319 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3320 iface, resources, resource_count);
3322 return E_NOTIMPL;
3325 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3327 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3329 return E_NOTIMPL;
3332 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3334 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3336 *max_latency = 2;
3338 return E_NOTIMPL;
3341 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3343 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3344 struct wined3d_swapchain_desc swapchain_desc;
3346 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3348 wined3d_mutex_lock();
3349 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3350 wined3d_mutex_unlock();
3352 if (swapchain_desc.windowed)
3353 return D3D_OK;
3355 /* FIXME: This is actually supposed to check if any other device is in
3356 * fullscreen mode. */
3357 if (dst_window != swapchain_desc.device_window)
3358 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3360 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3363 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3364 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3365 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3367 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3368 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3369 iface, width, height, format, multisample_type, multisample_quality,
3370 lockable, surface, shared_handle, usage);
3372 *surface = NULL;
3373 if (shared_handle)
3374 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3376 return E_NOTIMPL;
3379 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3380 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3381 HANDLE *shared_handle, DWORD usage)
3383 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3384 iface, width, height, format, pool, surface, shared_handle, usage);
3386 return E_NOTIMPL;
3389 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3390 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3391 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3393 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3394 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3396 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3397 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3398 iface, width, height, format, multisample_type, multisample_quality,
3399 discard, surface, shared_handle, usage);
3401 if (usage & D3DUSAGE_DEPTHSTENCIL)
3403 WARN("Invalid usage %#x.\n", usage);
3404 return D3DERR_INVALIDCALL;
3407 if (shared_handle)
3408 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3410 if (discard)
3411 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3413 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3414 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3417 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3418 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3420 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3422 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3424 return d3d9_device_reset(device, present_parameters, mode);
3427 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3428 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3430 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3431 struct wined3d_display_mode wined3d_mode;
3432 HRESULT hr;
3434 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3435 iface, swapchain_idx, mode, rotation);
3437 if (mode->Size != sizeof(*mode))
3438 return D3DERR_INVALIDCALL;
3440 wined3d_mutex_lock();
3441 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3442 (enum wined3d_display_rotation *)rotation);
3443 wined3d_mutex_unlock();
3445 if (SUCCEEDED(hr))
3447 mode->Width = wined3d_mode.width;
3448 mode->Height = wined3d_mode.height;
3449 mode->RefreshRate = wined3d_mode.refresh_rate;
3450 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3451 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3454 return hr;
3457 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3459 /* IUnknown */
3460 d3d9_device_QueryInterface,
3461 d3d9_device_AddRef,
3462 d3d9_device_Release,
3463 /* IDirect3DDevice9 */
3464 d3d9_device_TestCooperativeLevel,
3465 d3d9_device_GetAvailableTextureMem,
3466 d3d9_device_EvictManagedResources,
3467 d3d9_device_GetDirect3D,
3468 d3d9_device_GetDeviceCaps,
3469 d3d9_device_GetDisplayMode,
3470 d3d9_device_GetCreationParameters,
3471 d3d9_device_SetCursorProperties,
3472 d3d9_device_SetCursorPosition,
3473 d3d9_device_ShowCursor,
3474 d3d9_device_CreateAdditionalSwapChain,
3475 d3d9_device_GetSwapChain,
3476 d3d9_device_GetNumberOfSwapChains,
3477 d3d9_device_Reset,
3478 d3d9_device_Present,
3479 d3d9_device_GetBackBuffer,
3480 d3d9_device_GetRasterStatus,
3481 d3d9_device_SetDialogBoxMode,
3482 d3d9_device_SetGammaRamp,
3483 d3d9_device_GetGammaRamp,
3484 d3d9_device_CreateTexture,
3485 d3d9_device_CreateVolumeTexture,
3486 d3d9_device_CreateCubeTexture,
3487 d3d9_device_CreateVertexBuffer,
3488 d3d9_device_CreateIndexBuffer,
3489 d3d9_device_CreateRenderTarget,
3490 d3d9_device_CreateDepthStencilSurface,
3491 d3d9_device_UpdateSurface,
3492 d3d9_device_UpdateTexture,
3493 d3d9_device_GetRenderTargetData,
3494 d3d9_device_GetFrontBufferData,
3495 d3d9_device_StretchRect,
3496 d3d9_device_ColorFill,
3497 d3d9_device_CreateOffscreenPlainSurface,
3498 d3d9_device_SetRenderTarget,
3499 d3d9_device_GetRenderTarget,
3500 d3d9_device_SetDepthStencilSurface,
3501 d3d9_device_GetDepthStencilSurface,
3502 d3d9_device_BeginScene,
3503 d3d9_device_EndScene,
3504 d3d9_device_Clear,
3505 d3d9_device_SetTransform,
3506 d3d9_device_GetTransform,
3507 d3d9_device_MultiplyTransform,
3508 d3d9_device_SetViewport,
3509 d3d9_device_GetViewport,
3510 d3d9_device_SetMaterial,
3511 d3d9_device_GetMaterial,
3512 d3d9_device_SetLight,
3513 d3d9_device_GetLight,
3514 d3d9_device_LightEnable,
3515 d3d9_device_GetLightEnable,
3516 d3d9_device_SetClipPlane,
3517 d3d9_device_GetClipPlane,
3518 d3d9_device_SetRenderState,
3519 d3d9_device_GetRenderState,
3520 d3d9_device_CreateStateBlock,
3521 d3d9_device_BeginStateBlock,
3522 d3d9_device_EndStateBlock,
3523 d3d9_device_SetClipStatus,
3524 d3d9_device_GetClipStatus,
3525 d3d9_device_GetTexture,
3526 d3d9_device_SetTexture,
3527 d3d9_device_GetTextureStageState,
3528 d3d9_device_SetTextureStageState,
3529 d3d9_device_GetSamplerState,
3530 d3d9_device_SetSamplerState,
3531 d3d9_device_ValidateDevice,
3532 d3d9_device_SetPaletteEntries,
3533 d3d9_device_GetPaletteEntries,
3534 d3d9_device_SetCurrentTexturePalette,
3535 d3d9_device_GetCurrentTexturePalette,
3536 d3d9_device_SetScissorRect,
3537 d3d9_device_GetScissorRect,
3538 d3d9_device_SetSoftwareVertexProcessing,
3539 d3d9_device_GetSoftwareVertexProcessing,
3540 d3d9_device_SetNPatchMode,
3541 d3d9_device_GetNPatchMode,
3542 d3d9_device_DrawPrimitive,
3543 d3d9_device_DrawIndexedPrimitive,
3544 d3d9_device_DrawPrimitiveUP,
3545 d3d9_device_DrawIndexedPrimitiveUP,
3546 d3d9_device_ProcessVertices,
3547 d3d9_device_CreateVertexDeclaration,
3548 d3d9_device_SetVertexDeclaration,
3549 d3d9_device_GetVertexDeclaration,
3550 d3d9_device_SetFVF,
3551 d3d9_device_GetFVF,
3552 d3d9_device_CreateVertexShader,
3553 d3d9_device_SetVertexShader,
3554 d3d9_device_GetVertexShader,
3555 d3d9_device_SetVertexShaderConstantF,
3556 d3d9_device_GetVertexShaderConstantF,
3557 d3d9_device_SetVertexShaderConstantI,
3558 d3d9_device_GetVertexShaderConstantI,
3559 d3d9_device_SetVertexShaderConstantB,
3560 d3d9_device_GetVertexShaderConstantB,
3561 d3d9_device_SetStreamSource,
3562 d3d9_device_GetStreamSource,
3563 d3d9_device_SetStreamSourceFreq,
3564 d3d9_device_GetStreamSourceFreq,
3565 d3d9_device_SetIndices,
3566 d3d9_device_GetIndices,
3567 d3d9_device_CreatePixelShader,
3568 d3d9_device_SetPixelShader,
3569 d3d9_device_GetPixelShader,
3570 d3d9_device_SetPixelShaderConstantF,
3571 d3d9_device_GetPixelShaderConstantF,
3572 d3d9_device_SetPixelShaderConstantI,
3573 d3d9_device_GetPixelShaderConstantI,
3574 d3d9_device_SetPixelShaderConstantB,
3575 d3d9_device_GetPixelShaderConstantB,
3576 d3d9_device_DrawRectPatch,
3577 d3d9_device_DrawTriPatch,
3578 d3d9_device_DeletePatch,
3579 d3d9_device_CreateQuery,
3580 /* IDirect3DDevice9Ex */
3581 d3d9_device_SetConvolutionMonoKernel,
3582 d3d9_device_ComposeRects,
3583 d3d9_device_PresentEx,
3584 d3d9_device_GetGPUThreadPriority,
3585 d3d9_device_SetGPUThreadPriority,
3586 d3d9_device_WaitForVBlank,
3587 d3d9_device_CheckResourceResidency,
3588 d3d9_device_SetMaximumFrameLatency,
3589 d3d9_device_GetMaximumFrameLatency,
3590 d3d9_device_CheckDeviceState,
3591 d3d9_device_CreateRenderTargetEx,
3592 d3d9_device_CreateOffscreenPlainSurfaceEx,
3593 d3d9_device_CreateDepthStencilSurfaceEx,
3594 d3d9_device_ResetEx,
3595 d3d9_device_GetDisplayModeEx,
3598 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3600 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3603 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3604 struct wined3d_device *device)
3606 TRACE("device_parent %p, device %p.\n", device_parent, device);
3609 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3611 TRACE("device_parent %p.\n", device_parent);
3614 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3616 struct d3d9_device *device = device_from_device_parent(device_parent);
3618 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3620 if (!device->d3d_parent)
3621 return;
3623 if (!activate)
3624 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3625 else if (device->d3d_parent->extended)
3626 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3627 else
3628 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3631 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3632 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3633 void **parent, const struct wined3d_parent_ops **parent_ops)
3635 struct d3d9_surface *d3d_surface;
3637 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3638 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3640 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3641 return E_OUTOFMEMORY;
3643 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3644 *parent = d3d_surface;
3645 TRACE("Created surface %p.\n", d3d_surface);
3647 return D3D_OK;
3650 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3651 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3652 void **parent, const struct wined3d_parent_ops **parent_ops)
3654 struct d3d9_volume *d3d_volume;
3656 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3657 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3659 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3660 return E_OUTOFMEMORY;
3662 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3663 *parent = d3d_volume;
3664 TRACE("Created volume %p.\n", d3d_volume);
3666 return D3D_OK;
3669 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3670 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3672 struct d3d9_device *device = device_from_device_parent(device_parent);
3673 struct d3d9_surface *d3d_surface;
3674 HRESULT hr;
3676 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3677 device_parent, container_parent, desc, texture);
3679 if (container_parent == device_parent)
3680 container_parent = &device->IDirect3DDevice9Ex_iface;
3682 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1,
3683 WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3685 WARN("Failed to create texture, hr %#x.\n", hr);
3686 return hr;
3689 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3690 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3692 return hr;
3695 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3696 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3698 struct d3d9_device *device = device_from_device_parent(device_parent);
3699 struct d3d9_swapchain *d3d_swapchain;
3700 HRESULT hr;
3702 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3704 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3705 if (FAILED(hr))
3707 WARN("Failed to create swapchain, hr %#x.\n", hr);
3708 *swapchain = NULL;
3709 return hr;
3712 *swapchain = d3d_swapchain->wined3d_swapchain;
3713 wined3d_swapchain_incref(*swapchain);
3714 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3716 return hr;
3719 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3721 device_parent_wined3d_device_created,
3722 device_parent_mode_changed,
3723 device_parent_activate,
3724 device_parent_surface_created,
3725 device_parent_volume_created,
3726 device_parent_create_swapchain_texture,
3727 device_parent_create_swapchain,
3730 static void setup_fpu(void)
3732 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3733 WORD cw;
3734 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3735 cw = (cw & ~0xf3f) | 0x3f;
3736 __asm__ volatile ("fldcw %0" : : "m" (cw));
3737 #elif defined(__i386__) && defined(_MSC_VER)
3738 WORD cw;
3739 __asm fnstcw cw;
3740 cw = (cw & ~0xf3f) | 0x3f;
3741 __asm fldcw cw;
3742 #else
3743 FIXME("FPU setup not implemented for this platform.\n");
3744 #endif
3747 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3748 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3749 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3751 struct wined3d_swapchain_desc *swapchain_desc;
3752 UINT i, count = 1;
3753 HRESULT hr;
3755 if (mode)
3756 FIXME("Ignoring display mode.\n");
3758 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3759 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3760 device->refcount = 1;
3762 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3764 wined3d_mutex_lock();
3765 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3766 &device->device_parent, &device->wined3d_device);
3767 if (FAILED(hr))
3769 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3770 wined3d_mutex_unlock();
3771 return hr;
3774 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3776 WINED3DCAPS caps;
3778 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3779 count = caps.NumberOfAdaptersInGroup;
3782 if (flags & D3DCREATE_MULTITHREADED)
3783 wined3d_device_set_multithreaded(device->wined3d_device);
3785 if (!parameters->Windowed)
3787 if (!focus_window)
3788 focus_window = parameters->hDeviceWindow;
3789 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3791 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3792 wined3d_device_decref(device->wined3d_device);
3793 wined3d_mutex_unlock();
3794 return hr;
3797 for (i = 0; i < count; ++i)
3799 HWND device_window = parameters[i].hDeviceWindow;
3801 if (!device_window) device_window = focus_window;
3802 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3803 parameters[i].BackBufferWidth,
3804 parameters[i].BackBufferHeight);
3808 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3809 if (!swapchain_desc)
3811 ERR("Failed to allocate wined3d parameters.\n");
3812 wined3d_device_release_focus_window(device->wined3d_device);
3813 wined3d_device_decref(device->wined3d_device);
3814 wined3d_mutex_unlock();
3815 return E_OUTOFMEMORY;
3818 for (i = 0; i < count; ++i)
3820 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3821 parent->extended))
3823 wined3d_device_release_focus_window(device->wined3d_device);
3824 wined3d_device_decref(device->wined3d_device);
3825 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3826 wined3d_mutex_unlock();
3827 return D3DERR_INVALIDCALL;
3831 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3832 if (FAILED(hr))
3834 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3835 wined3d_device_release_focus_window(device->wined3d_device);
3836 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3837 wined3d_device_decref(device->wined3d_device);
3838 wined3d_mutex_unlock();
3839 return hr;
3842 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3844 wined3d_device_uninit_3d(device->wined3d_device);
3845 wined3d_device_release_focus_window(device->wined3d_device);
3846 wined3d_device_decref(device->wined3d_device);
3847 wined3d_mutex_unlock();
3848 return E_OUTOFMEMORY;
3851 for (i = 0; i < count; ++i)
3853 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3856 wined3d_mutex_unlock();
3858 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3860 /* Initialize the converted declaration array. This creates a valid pointer
3861 * and when adding decls HeapReAlloc() can be used without further checking. */
3862 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3863 if (!device->fvf_decls)
3865 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3866 wined3d_mutex_lock();
3867 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3868 wined3d_device_uninit_3d(device->wined3d_device);
3869 wined3d_device_release_focus_window(device->wined3d_device);
3870 wined3d_device_decref(device->wined3d_device);
3871 wined3d_mutex_unlock();
3872 return E_OUTOFMEMORY;
3875 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3876 device->d3d_parent = parent;
3878 return D3D_OK;