d3d8: Avoid '\n' in middle of TRACE() messages.
[wine.git] / dlls / d3d9 / device.c
blob47f4f7b27920a6f95fe98e2959218b14e7cbda86
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 if (dirty_region)
763 FIXME("Ignoring dirty_region %p.\n", dirty_region);
765 wined3d_mutex_lock();
766 for (i = 0; i < device->implicit_swapchain_count; ++i)
768 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
769 src_rect, dst_rect, dst_window_override, 0)))
771 wined3d_mutex_unlock();
772 return hr;
775 wined3d_mutex_unlock();
777 return D3D_OK;
780 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
781 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
783 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
784 HRESULT hr;
786 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
787 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
789 /* backbuffer_type is ignored by native. */
791 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
792 *backbuffer = NULL;
794 wined3d_mutex_lock();
795 if (swapchain >= device->implicit_swapchain_count)
797 wined3d_mutex_unlock();
798 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
799 return D3DERR_INVALIDCALL;
802 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
803 backbuffer_idx, backbuffer_type, backbuffer);
804 wined3d_mutex_unlock();
806 return hr;
809 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
810 UINT swapchain, D3DRASTER_STATUS *raster_status)
812 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
813 HRESULT hr;
815 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
817 wined3d_mutex_lock();
818 hr = wined3d_device_get_raster_status(device->wined3d_device,
819 swapchain, (struct wined3d_raster_status *)raster_status);
820 wined3d_mutex_unlock();
822 return hr;
825 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
827 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
828 HRESULT hr;
830 TRACE("iface %p, enable %#x.\n", iface, enable);
832 wined3d_mutex_lock();
833 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
834 wined3d_mutex_unlock();
836 return hr;
839 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
840 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
842 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
844 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
846 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
847 wined3d_mutex_lock();
848 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
849 wined3d_mutex_unlock();
852 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
854 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
856 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
858 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
859 wined3d_mutex_lock();
860 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
861 wined3d_mutex_unlock();
864 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
865 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
866 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
868 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
869 struct d3d9_texture *object;
870 BOOL set_mem = FALSE;
871 HRESULT hr;
873 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
874 iface, width, height, levels, usage, format, pool, texture, shared_handle);
876 *texture = NULL;
877 if (shared_handle)
879 if (!device->d3d_parent->extended)
881 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
882 return E_NOTIMPL;
885 if (pool == D3DPOOL_SYSTEMMEM)
887 if (levels != 1)
888 return D3DERR_INVALIDCALL;
889 set_mem = TRUE;
891 else
893 if (pool != D3DPOOL_DEFAULT)
895 WARN("Trying to create a shared texture in pool %#x.\n", pool);
896 return D3DERR_INVALIDCALL;
898 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
902 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
903 if (!object)
904 return D3DERR_OUTOFVIDEOMEMORY;
906 hr = texture_init(object, device, width, height, levels, usage, format, pool);
907 if (FAILED(hr))
909 WARN("Failed to initialize texture, hr %#x.\n", hr);
910 HeapFree(GetProcessHeap(), 0, object);
911 return hr;
914 if (set_mem)
915 wined3d_texture_update_desc(object->wined3d_texture, width, height,
916 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
917 *shared_handle, 0);
919 TRACE("Created texture %p.\n", object);
920 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
922 return D3D_OK;
925 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
926 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
927 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
929 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
930 struct d3d9_texture *object;
931 HRESULT hr;
933 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
934 iface, width, height, depth, levels);
935 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
936 usage, format, pool, texture, shared_handle);
938 *texture = NULL;
939 if (shared_handle)
941 if (!device->d3d_parent->extended)
943 WARN("Trying to create a shared volume texture on a non-ex device.\n");
944 return E_NOTIMPL;
947 if (pool != D3DPOOL_DEFAULT)
949 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
950 return D3DERR_INVALIDCALL;
952 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
955 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
956 if (!object)
957 return D3DERR_OUTOFVIDEOMEMORY;
959 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
960 if (FAILED(hr))
962 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
963 HeapFree(GetProcessHeap(), 0, object);
964 return hr;
967 TRACE("Created volume texture %p.\n", object);
968 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
970 return D3D_OK;
973 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
974 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
975 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
977 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
978 struct d3d9_texture *object;
979 HRESULT hr;
981 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
982 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
984 *texture = NULL;
985 if (shared_handle)
987 if (!device->d3d_parent->extended)
989 WARN("Trying to create a shared cube texture on a non-ex device.\n");
990 return E_NOTIMPL;
993 if (pool != D3DPOOL_DEFAULT)
995 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
996 return D3DERR_INVALIDCALL;
998 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1001 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1002 if (!object)
1003 return D3DERR_OUTOFVIDEOMEMORY;
1005 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1006 if (FAILED(hr))
1008 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1009 HeapFree(GetProcessHeap(), 0, object);
1010 return hr;
1013 TRACE("Created cube texture %p.\n", object);
1014 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1016 return D3D_OK;
1019 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1020 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1021 HANDLE *shared_handle)
1023 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1024 struct d3d9_vertexbuffer *object;
1025 HRESULT hr;
1027 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1028 iface, size, usage, fvf, pool, buffer, shared_handle);
1030 if (shared_handle)
1032 if (!device->d3d_parent->extended)
1034 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1035 return E_NOTIMPL;
1038 if (pool != D3DPOOL_DEFAULT)
1040 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1041 return D3DERR_NOTAVAILABLE;
1043 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1046 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1047 if (!object)
1048 return D3DERR_OUTOFVIDEOMEMORY;
1050 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1051 if (FAILED(hr))
1053 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1054 HeapFree(GetProcessHeap(), 0, object);
1055 return hr;
1058 TRACE("Created vertex buffer %p.\n", object);
1059 *buffer = &object->IDirect3DVertexBuffer9_iface;
1061 return D3D_OK;
1064 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1065 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1066 HANDLE *shared_handle)
1068 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1069 struct d3d9_indexbuffer *object;
1070 HRESULT hr;
1072 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1073 iface, size, usage, format, pool, buffer, shared_handle);
1075 if (shared_handle)
1077 if (!device->d3d_parent->extended)
1079 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1080 return E_NOTIMPL;
1083 if (pool != D3DPOOL_DEFAULT)
1085 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1086 return D3DERR_NOTAVAILABLE;
1088 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1091 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1092 if (!object)
1093 return D3DERR_OUTOFVIDEOMEMORY;
1095 hr = indexbuffer_init(object, device, size, usage, format, pool);
1096 if (FAILED(hr))
1098 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1099 HeapFree(GetProcessHeap(), 0, object);
1100 return hr;
1103 TRACE("Created index buffer %p.\n", object);
1104 *buffer = &object->IDirect3DIndexBuffer9_iface;
1106 return D3D_OK;
1109 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1110 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1111 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1113 struct wined3d_resource_desc desc;
1114 struct d3d9_surface *surface_impl;
1115 struct wined3d_texture *texture;
1116 HRESULT hr;
1118 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1119 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1120 device, width, height, format, flags, surface, usage, pool,
1121 multisample_type, multisample_quality);
1123 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1124 desc.format = wined3dformat_from_d3dformat(format);
1125 desc.multisample_type = multisample_type;
1126 desc.multisample_quality = multisample_quality;
1127 desc.usage = usage & WINED3DUSAGE_MASK;
1128 desc.pool = pool;
1129 desc.width = width;
1130 desc.height = height;
1131 desc.depth = 1;
1132 desc.size = 0;
1134 wined3d_mutex_lock();
1136 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1137 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1139 wined3d_mutex_unlock();
1140 WARN("Failed to create texture, hr %#x.\n", hr);
1141 if (hr == WINED3DERR_NOTAVAILABLE)
1142 hr = D3DERR_INVALIDCALL;
1143 return hr;
1146 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1147 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1148 *surface = &surface_impl->IDirect3DSurface9_iface;
1149 IDirect3DSurface9_AddRef(*surface);
1151 if (user_mem)
1152 wined3d_texture_update_desc(texture, width, height,
1153 desc.format, multisample_type, multisample_quality, user_mem, 0);
1155 wined3d_texture_decref(texture);
1157 wined3d_mutex_unlock();
1159 return D3D_OK;
1162 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1163 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1164 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1166 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1167 DWORD flags = 0;
1169 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1170 "lockable %#x, surface %p, shared_handle %p.\n",
1171 iface, width, height, format, multisample_type, multisample_quality,
1172 lockable, surface, shared_handle);
1174 *surface = NULL;
1175 if (shared_handle)
1177 if (!device->d3d_parent->extended)
1179 WARN("Trying to create a shared render target on a non-ex device.\n");
1180 return E_NOTIMPL;
1183 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1186 if (lockable)
1187 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1189 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1190 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1193 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1194 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1195 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1197 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1198 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1200 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1201 "discard %#x, surface %p, shared_handle %p.\n",
1202 iface, width, height, format, multisample_type, multisample_quality,
1203 discard, surface, shared_handle);
1205 *surface = NULL;
1206 if (shared_handle)
1208 if (!device->d3d_parent->extended)
1210 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1211 return E_NOTIMPL;
1214 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1217 if (discard)
1218 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1220 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1221 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1225 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1226 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1227 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1229 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1230 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1231 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1232 struct wined3d_box src_box;
1233 HRESULT hr;
1235 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1236 iface, src_surface, src_rect, dst_surface, dst_point);
1238 if (src_rect)
1240 src_box.left = src_rect->left;
1241 src_box.top = src_rect->top;
1242 src_box.right = src_rect->right;
1243 src_box.bottom = src_rect->bottom;
1244 src_box.front = 0;
1245 src_box.back = 1;
1248 wined3d_mutex_lock();
1249 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1250 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1251 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1252 src->sub_resource_idx, src_rect ? &src_box : NULL);
1253 wined3d_mutex_unlock();
1255 if (FAILED(hr))
1256 return D3DERR_INVALIDCALL;
1258 return hr;
1261 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1262 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1264 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1265 struct d3d9_texture *src_impl, *dst_impl;
1266 HRESULT hr;
1268 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1270 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1271 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1273 wined3d_mutex_lock();
1274 hr = wined3d_device_update_texture(device->wined3d_device,
1275 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1276 wined3d_mutex_unlock();
1278 return hr;
1281 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1282 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1284 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1285 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1286 struct wined3d_sub_resource_desc wined3d_desc;
1287 RECT dst_rect, src_rect;
1288 HRESULT hr;
1290 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1292 wined3d_mutex_lock();
1293 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1294 dst_rect.left = 0;
1295 dst_rect.top = 0;
1296 dst_rect.right = wined3d_desc.width;
1297 dst_rect.bottom = wined3d_desc.height;
1299 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1300 src_rect.left = 0;
1301 src_rect.top = 0;
1302 src_rect.right = wined3d_desc.width;
1303 src_rect.bottom = wined3d_desc.height;
1305 /* TODO: Check surface sizes, pools, etc. */
1306 if (wined3d_desc.multisample_type)
1307 hr = D3DERR_INVALIDCALL;
1308 else
1309 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1310 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1311 wined3d_mutex_unlock();
1313 return hr;
1316 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1317 UINT swapchain, IDirect3DSurface9 *dst_surface)
1319 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1320 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1321 HRESULT hr = D3DERR_INVALIDCALL;
1323 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1325 wined3d_mutex_lock();
1326 if (swapchain < device->implicit_swapchain_count)
1327 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1328 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1329 wined3d_mutex_unlock();
1331 return hr;
1334 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1335 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1337 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1338 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1339 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1340 struct wined3d_sub_resource_desc src_desc, dst_desc;
1341 HRESULT hr = D3DERR_INVALIDCALL;
1342 RECT d, s;
1344 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1345 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1347 wined3d_mutex_lock();
1348 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &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 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1359 if (!src_rect)
1361 s.left = 0;
1362 s.top = 0;
1363 s.right = src_desc.width;
1364 s.bottom = src_desc.height;
1365 src_rect = &s;
1368 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1370 if (device->in_scene)
1372 WARN("Rejecting depth / stencil blit while in scene.\n");
1373 goto done;
1376 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1377 || src_rect->bottom != src_desc.height)
1379 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1380 wine_dbgstr_rect(src_rect));
1381 goto done;
1384 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1385 || dst_rect->bottom != dst_desc.height)
1387 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1388 wine_dbgstr_rect(dst_rect));
1389 goto done;
1392 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1394 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1395 goto done;
1399 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1400 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1401 if (hr == WINEDDERR_INVALIDRECT)
1402 hr = D3DERR_INVALIDCALL;
1404 done:
1405 wined3d_mutex_unlock();
1406 return hr;
1409 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1410 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1412 const struct wined3d_color c =
1414 ((color >> 16) & 0xff) / 255.0f,
1415 ((color >> 8) & 0xff) / 255.0f,
1416 (color & 0xff) / 255.0f,
1417 ((color >> 24) & 0xff) / 255.0f,
1419 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1420 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1421 struct wined3d_sub_resource_desc desc;
1422 HRESULT hr;
1424 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1426 wined3d_mutex_lock();
1428 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1429 surface_impl->sub_resource_idx, &desc)))
1431 wined3d_mutex_unlock();
1432 return D3DERR_INVALIDCALL;
1435 if (desc.pool != WINED3D_POOL_DEFAULT)
1437 wined3d_mutex_unlock();
1438 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1439 return D3DERR_INVALIDCALL;
1441 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1443 wined3d_mutex_unlock();
1444 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1445 return D3DERR_INVALIDCALL;
1447 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1449 wined3d_mutex_unlock();
1450 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1451 return D3DERR_INVALIDCALL;
1454 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1455 d3d9_surface_get_rendertarget_view(surface_impl), rect,
1456 WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1458 wined3d_mutex_unlock();
1460 return hr;
1463 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1464 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1465 HANDLE *shared_handle)
1467 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1468 void *user_mem = NULL;
1470 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1471 iface, width, height, format, pool, surface, shared_handle);
1473 *surface = NULL;
1474 if (pool == D3DPOOL_MANAGED)
1476 WARN("Attempting to create a managed offscreen plain surface.\n");
1477 return D3DERR_INVALIDCALL;
1480 if (shared_handle)
1482 if (!device->d3d_parent->extended)
1484 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1485 return E_NOTIMPL;
1488 if (pool == D3DPOOL_SYSTEMMEM)
1489 user_mem = *shared_handle;
1490 else
1492 if (pool != D3DPOOL_DEFAULT)
1494 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1495 return D3DERR_INVALIDCALL;
1497 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1501 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1502 * regardless of the pool they're created in. Should we set dynamic usage
1503 * here? */
1504 return d3d9_device_create_surface(device, width, height, format,
1505 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1508 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1510 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1511 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1512 HRESULT hr;
1514 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1516 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1518 WARN("Invalid index %u specified.\n", idx);
1519 return D3DERR_INVALIDCALL;
1522 if (!idx && !surface_impl)
1524 WARN("Trying to set render target 0 to NULL.\n");
1525 return D3DERR_INVALIDCALL;
1528 wined3d_mutex_lock();
1529 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1530 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1531 wined3d_mutex_unlock();
1533 return hr;
1536 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1538 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1539 struct wined3d_rendertarget_view *wined3d_rtv;
1540 struct d3d9_surface *surface_impl;
1541 HRESULT hr = D3D_OK;
1543 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1545 if (!surface)
1546 return D3DERR_INVALIDCALL;
1548 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1550 WARN("Invalid index %u specified.\n", idx);
1551 return D3DERR_INVALIDCALL;
1554 wined3d_mutex_lock();
1555 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1557 /* We want the sub resource parent here, since the view itself may be
1558 * internal to wined3d and may not have a parent. */
1559 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1560 *surface = &surface_impl->IDirect3DSurface9_iface;
1561 IDirect3DSurface9_AddRef(*surface);
1563 else
1565 hr = D3DERR_NOTFOUND;
1566 *surface = NULL;
1568 wined3d_mutex_unlock();
1570 return hr;
1573 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1575 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1576 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1578 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1580 wined3d_mutex_lock();
1581 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1582 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1583 wined3d_mutex_unlock();
1585 return D3D_OK;
1588 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1590 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1591 struct wined3d_rendertarget_view *wined3d_dsv;
1592 struct d3d9_surface *surface_impl;
1593 HRESULT hr = D3D_OK;
1595 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1597 if (!depth_stencil)
1598 return D3DERR_INVALIDCALL;
1600 wined3d_mutex_lock();
1601 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1603 /* We want the sub resource parent here, since the view itself may be
1604 * internal to wined3d and may not have a parent. */
1605 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1606 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1607 IDirect3DSurface9_AddRef(*depth_stencil);
1609 else
1611 hr = D3DERR_NOTFOUND;
1612 *depth_stencil = NULL;
1614 wined3d_mutex_unlock();
1616 return hr;
1619 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1621 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1622 HRESULT hr;
1624 TRACE("iface %p.\n", iface);
1626 wined3d_mutex_lock();
1627 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1628 device->in_scene = TRUE;
1629 wined3d_mutex_unlock();
1631 return hr;
1634 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1636 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1637 HRESULT hr;
1639 TRACE("iface %p.\n", iface);
1641 wined3d_mutex_lock();
1642 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1643 device->in_scene = FALSE;
1644 wined3d_mutex_unlock();
1646 return hr;
1649 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1650 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1652 const struct wined3d_color c =
1654 ((color >> 16) & 0xff) / 255.0f,
1655 ((color >> 8) & 0xff) / 255.0f,
1656 (color & 0xff) / 255.0f,
1657 ((color >> 24) & 0xff) / 255.0f,
1659 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1660 HRESULT hr;
1662 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1663 iface, rect_count, rects, flags, color, z, stencil);
1665 if (rect_count && !rects)
1667 WARN("count %u with NULL rects.\n", rect_count);
1668 rect_count = 0;
1671 wined3d_mutex_lock();
1672 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1673 wined3d_mutex_unlock();
1675 return hr;
1678 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1679 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1681 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1683 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1685 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1686 wined3d_mutex_lock();
1687 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1688 wined3d_mutex_unlock();
1690 return D3D_OK;
1693 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1694 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1696 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1698 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1700 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1701 wined3d_mutex_lock();
1702 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1703 wined3d_mutex_unlock();
1705 return D3D_OK;
1708 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1709 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1711 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1713 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1715 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1716 wined3d_mutex_lock();
1717 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1718 wined3d_mutex_unlock();
1720 return D3D_OK;
1723 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1725 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1727 TRACE("iface %p, viewport %p.\n", iface, viewport);
1729 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1730 wined3d_mutex_lock();
1731 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1732 wined3d_mutex_unlock();
1734 return D3D_OK;
1737 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1739 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1741 TRACE("iface %p, viewport %p.\n", iface, viewport);
1743 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1744 wined3d_mutex_lock();
1745 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1746 wined3d_mutex_unlock();
1748 return D3D_OK;
1751 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1755 TRACE("iface %p, material %p.\n", iface, material);
1757 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1758 wined3d_mutex_lock();
1759 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1760 wined3d_mutex_unlock();
1762 return D3D_OK;
1765 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1767 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1769 TRACE("iface %p, material %p.\n", iface, material);
1771 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1772 wined3d_mutex_lock();
1773 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1774 wined3d_mutex_unlock();
1776 return D3D_OK;
1779 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1781 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1782 HRESULT hr;
1784 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1786 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1787 wined3d_mutex_lock();
1788 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1789 wined3d_mutex_unlock();
1791 return hr;
1794 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1796 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1797 HRESULT hr;
1799 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1801 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1802 wined3d_mutex_lock();
1803 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1804 wined3d_mutex_unlock();
1806 return hr;
1809 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1811 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1812 HRESULT hr;
1814 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1816 wined3d_mutex_lock();
1817 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1818 wined3d_mutex_unlock();
1820 return hr;
1823 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1825 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1826 HRESULT hr;
1828 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1830 wined3d_mutex_lock();
1831 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1832 wined3d_mutex_unlock();
1834 return hr;
1837 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1839 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1840 HRESULT hr;
1842 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1844 wined3d_mutex_lock();
1845 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1846 wined3d_mutex_unlock();
1848 return hr;
1851 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1853 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1854 HRESULT hr;
1856 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1858 wined3d_mutex_lock();
1859 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1860 wined3d_mutex_unlock();
1862 return hr;
1865 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1866 D3DRENDERSTATETYPE state, DWORD value)
1868 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1870 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1872 wined3d_mutex_lock();
1873 wined3d_device_set_render_state(device->wined3d_device, state, value);
1874 wined3d_mutex_unlock();
1876 return D3D_OK;
1879 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1880 D3DRENDERSTATETYPE state, DWORD *value)
1882 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1884 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1886 wined3d_mutex_lock();
1887 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1888 wined3d_mutex_unlock();
1890 return D3D_OK;
1893 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1894 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1896 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1897 struct d3d9_stateblock *object;
1898 HRESULT hr;
1900 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1902 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1904 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1905 return D3DERR_INVALIDCALL;
1908 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1909 if (!object)
1910 return E_OUTOFMEMORY;
1912 hr = stateblock_init(object, device, type, NULL);
1913 if (FAILED(hr))
1915 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1916 HeapFree(GetProcessHeap(), 0, object);
1917 return hr;
1920 TRACE("Created stateblock %p.\n", object);
1921 *stateblock = &object->IDirect3DStateBlock9_iface;
1923 return D3D_OK;
1926 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1928 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1929 HRESULT hr;
1931 TRACE("iface %p.\n", iface);
1933 wined3d_mutex_lock();
1934 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1935 wined3d_mutex_unlock();
1937 return hr;
1940 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1943 struct wined3d_stateblock *wined3d_stateblock;
1944 struct d3d9_stateblock *object;
1945 HRESULT hr;
1947 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1949 wined3d_mutex_lock();
1950 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1951 wined3d_mutex_unlock();
1952 if (FAILED(hr))
1954 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1955 return hr;
1958 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1959 if (!object)
1961 wined3d_mutex_lock();
1962 wined3d_stateblock_decref(wined3d_stateblock);
1963 wined3d_mutex_unlock();
1964 return E_OUTOFMEMORY;
1967 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1968 if (FAILED(hr))
1970 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1971 wined3d_mutex_lock();
1972 wined3d_stateblock_decref(wined3d_stateblock);
1973 wined3d_mutex_unlock();
1974 HeapFree(GetProcessHeap(), 0, object);
1975 return hr;
1978 TRACE("Created stateblock %p.\n", object);
1979 *stateblock = &object->IDirect3DStateBlock9_iface;
1981 return D3D_OK;
1984 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1986 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1987 HRESULT hr;
1989 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1991 wined3d_mutex_lock();
1992 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1993 wined3d_mutex_unlock();
1995 return hr;
1998 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2000 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2001 HRESULT hr;
2003 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2005 wined3d_mutex_lock();
2006 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2007 wined3d_mutex_unlock();
2009 return hr;
2012 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2014 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2015 struct wined3d_texture *wined3d_texture = NULL;
2016 struct d3d9_texture *texture_impl;
2018 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2020 if (!texture)
2021 return D3DERR_INVALIDCALL;
2023 wined3d_mutex_lock();
2024 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2026 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2027 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2028 IDirect3DBaseTexture9_AddRef(*texture);
2030 else
2032 *texture = NULL;
2034 wined3d_mutex_unlock();
2036 return D3D_OK;
2039 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2041 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2042 struct d3d9_texture *texture_impl;
2043 HRESULT hr;
2045 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2047 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2049 wined3d_mutex_lock();
2050 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2051 texture_impl ? texture_impl->wined3d_texture : NULL);
2052 wined3d_mutex_unlock();
2054 return hr;
2057 static const enum wined3d_texture_stage_state tss_lookup[] =
2059 WINED3D_TSS_INVALID, /* 0, unused */
2060 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2061 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2062 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2063 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2064 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2065 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2066 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2067 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2068 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2069 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2070 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2071 WINED3D_TSS_INVALID, /* 12, unused */
2072 WINED3D_TSS_INVALID, /* 13, unused */
2073 WINED3D_TSS_INVALID, /* 14, unused */
2074 WINED3D_TSS_INVALID, /* 15, unused */
2075 WINED3D_TSS_INVALID, /* 16, unused */
2076 WINED3D_TSS_INVALID, /* 17, unused */
2077 WINED3D_TSS_INVALID, /* 18, unused */
2078 WINED3D_TSS_INVALID, /* 19, unused */
2079 WINED3D_TSS_INVALID, /* 20, unused */
2080 WINED3D_TSS_INVALID, /* 21, unused */
2081 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2082 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2083 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2084 WINED3D_TSS_INVALID, /* 25, unused */
2085 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2086 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2087 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2088 WINED3D_TSS_INVALID, /* 29, unused */
2089 WINED3D_TSS_INVALID, /* 30, unused */
2090 WINED3D_TSS_INVALID, /* 31, unused */
2091 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2094 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2095 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2097 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2099 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2101 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2103 WARN("Invalid state %#x passed.\n", state);
2104 return D3D_OK;
2107 wined3d_mutex_lock();
2108 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2109 wined3d_mutex_unlock();
2111 return D3D_OK;
2114 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2115 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2117 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2119 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2121 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2123 WARN("Invalid state %#x passed.\n", state);
2124 return D3D_OK;
2127 wined3d_mutex_lock();
2128 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2129 wined3d_mutex_unlock();
2131 return D3D_OK;
2134 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2135 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2137 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2139 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2141 wined3d_mutex_lock();
2142 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2143 wined3d_mutex_unlock();
2145 return D3D_OK;
2148 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2149 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2151 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2153 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2155 wined3d_mutex_lock();
2156 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2157 wined3d_mutex_unlock();
2159 return D3D_OK;
2162 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2164 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2165 HRESULT hr;
2167 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2169 wined3d_mutex_lock();
2170 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2171 wined3d_mutex_unlock();
2173 return hr;
2176 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2177 UINT palette_idx, const PALETTEENTRY *entries)
2179 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2181 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2182 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2183 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2185 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2186 return D3D_OK;
2189 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2190 UINT palette_idx, PALETTEENTRY *entries)
2192 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2194 return D3DERR_INVALIDCALL;
2197 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2199 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2201 return D3D_OK;
2204 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2206 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2208 return D3DERR_INVALIDCALL;
2211 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2213 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2215 TRACE("iface %p, rect %p.\n", iface, rect);
2217 wined3d_mutex_lock();
2218 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2219 wined3d_mutex_unlock();
2221 return D3D_OK;
2224 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2226 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2228 TRACE("iface %p, rect %p.\n", iface, rect);
2230 wined3d_mutex_lock();
2231 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2232 wined3d_mutex_unlock();
2234 return D3D_OK;
2237 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2239 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2241 TRACE("iface %p, software %#x.\n", iface, software);
2243 wined3d_mutex_lock();
2244 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2245 wined3d_mutex_unlock();
2247 return D3D_OK;
2250 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2252 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2253 BOOL ret;
2255 TRACE("iface %p.\n", iface);
2257 wined3d_mutex_lock();
2258 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2259 wined3d_mutex_unlock();
2261 return ret;
2264 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2266 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2267 HRESULT hr;
2269 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2271 wined3d_mutex_lock();
2272 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2273 wined3d_mutex_unlock();
2275 return hr;
2278 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2280 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2281 float ret;
2283 TRACE("iface %p.\n", iface);
2285 wined3d_mutex_lock();
2286 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2287 wined3d_mutex_unlock();
2289 return ret;
2292 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2293 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2295 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2296 HRESULT hr;
2298 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2299 iface, primitive_type, start_vertex, primitive_count);
2301 wined3d_mutex_lock();
2302 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2303 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2304 vertex_count_from_primitive_count(primitive_type, primitive_count));
2305 wined3d_mutex_unlock();
2307 return hr;
2310 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2311 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2312 UINT vertex_count, UINT start_idx, UINT primitive_count)
2314 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2315 HRESULT hr;
2317 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2318 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2319 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2320 vertex_count, start_idx, primitive_count);
2322 wined3d_mutex_lock();
2323 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2324 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2325 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2326 vertex_count_from_primitive_count(primitive_type, primitive_count));
2327 wined3d_mutex_unlock();
2329 return hr;
2332 /* The caller is responsible for wined3d locking */
2333 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2335 HRESULT hr;
2337 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2339 UINT size = max(device->vertex_buffer_size * 2, min_size);
2340 struct wined3d_buffer *buffer;
2342 TRACE("Growing vertex buffer to %u bytes\n", size);
2344 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2345 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2346 if (FAILED(hr))
2348 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2349 return hr;
2352 if (device->vertex_buffer)
2353 wined3d_buffer_decref(device->vertex_buffer);
2355 device->vertex_buffer = buffer;
2356 device->vertex_buffer_size = size;
2357 device->vertex_buffer_pos = 0;
2359 return D3D_OK;
2362 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2363 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2365 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2366 HRESULT hr;
2367 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2368 UINT size = vtx_count * stride;
2369 UINT vb_pos, align;
2370 BYTE *buffer_data;
2372 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2373 iface, primitive_type, primitive_count, data, stride);
2375 if (!primitive_count)
2377 WARN("primitive_count is 0, returning D3D_OK\n");
2378 return D3D_OK;
2381 wined3d_mutex_lock();
2383 hr = d3d9_device_prepare_vertex_buffer(device, size);
2384 if (FAILED(hr))
2385 goto done;
2387 vb_pos = device->vertex_buffer_pos;
2388 align = vb_pos % stride;
2389 if (align) align = stride - align;
2390 if (vb_pos + size + align > device->vertex_buffer_size)
2391 vb_pos = 0;
2392 else
2393 vb_pos += align;
2395 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2396 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2397 if (FAILED(hr))
2398 goto done;
2399 memcpy(buffer_data, data, size);
2400 wined3d_buffer_unmap(device->vertex_buffer);
2401 device->vertex_buffer_pos = vb_pos + size;
2403 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2404 if (FAILED(hr))
2405 goto done;
2407 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2408 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2409 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2411 done:
2412 wined3d_mutex_unlock();
2413 return hr;
2416 /* The caller is responsible for wined3d locking */
2417 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2419 HRESULT hr;
2421 if (device->index_buffer_size < min_size || !device->index_buffer)
2423 UINT size = max(device->index_buffer_size * 2, min_size);
2424 struct wined3d_buffer *buffer;
2426 TRACE("Growing index buffer to %u bytes\n", size);
2428 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2429 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2430 if (FAILED(hr))
2432 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2433 return hr;
2436 if (device->index_buffer)
2437 wined3d_buffer_decref(device->index_buffer);
2439 device->index_buffer = buffer;
2440 device->index_buffer_size = size;
2441 device->index_buffer_pos = 0;
2443 return D3D_OK;
2446 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2447 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2448 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2449 const void *vertex_data, UINT vertex_stride)
2451 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2452 HRESULT hr;
2453 BYTE *buffer_data;
2455 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2456 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2457 UINT idx_size = idx_count * idx_fmt_size;
2458 UINT ib_pos;
2460 UINT vtx_size = vertex_count * vertex_stride;
2461 UINT vb_pos, align;
2463 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2464 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2465 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2466 index_data, index_format, vertex_data, vertex_stride);
2468 if (!primitive_count)
2470 WARN("primitive_count is 0, returning D3D_OK\n");
2471 return D3D_OK;
2474 wined3d_mutex_lock();
2476 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2477 if (FAILED(hr))
2478 goto done;
2480 vb_pos = device->vertex_buffer_pos;
2481 align = vb_pos % vertex_stride;
2482 if (align) align = vertex_stride - align;
2483 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2484 vb_pos = 0;
2485 else
2486 vb_pos += align;
2488 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2489 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2490 if (FAILED(hr))
2491 goto done;
2492 memcpy(buffer_data, vertex_data, vtx_size);
2493 wined3d_buffer_unmap(device->vertex_buffer);
2494 device->vertex_buffer_pos = vb_pos + vtx_size;
2496 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2497 if (FAILED(hr))
2498 goto done;
2500 ib_pos = device->index_buffer_pos;
2501 align = ib_pos % idx_fmt_size;
2502 if (align) align = idx_fmt_size - align;
2503 if (ib_pos + idx_size + align > device->index_buffer_size)
2504 ib_pos = 0;
2505 else
2506 ib_pos += align;
2508 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2509 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2510 if (FAILED(hr))
2511 goto done;
2512 memcpy(buffer_data, index_data, idx_size);
2513 wined3d_buffer_unmap(device->index_buffer);
2514 device->index_buffer_pos = ib_pos + idx_size;
2516 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2517 if (FAILED(hr))
2518 goto done;
2520 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2521 wined3dformat_from_d3dformat(index_format));
2522 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2524 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2525 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2527 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2528 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2529 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2531 done:
2532 wined3d_mutex_unlock();
2533 return hr;
2536 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2537 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2538 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2540 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2541 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2542 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2543 HRESULT hr;
2545 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2546 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2548 wined3d_mutex_lock();
2549 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2550 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2551 flags, dst_impl->fvf);
2552 wined3d_mutex_unlock();
2554 return hr;
2557 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2558 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2560 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2561 struct d3d9_vertex_declaration *object;
2562 HRESULT hr;
2564 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2566 if (!declaration)
2568 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2569 return D3DERR_INVALIDCALL;
2572 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2573 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2575 return hr;
2578 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2579 IDirect3DVertexDeclaration9 *declaration)
2581 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2582 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2584 TRACE("iface %p, declaration %p.\n", iface, declaration);
2586 wined3d_mutex_lock();
2587 wined3d_device_set_vertex_declaration(device->wined3d_device,
2588 decl_impl ? decl_impl->wined3d_declaration : NULL);
2589 wined3d_mutex_unlock();
2591 return D3D_OK;
2594 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2595 IDirect3DVertexDeclaration9 **declaration)
2597 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2598 struct wined3d_vertex_declaration *wined3d_declaration;
2599 struct d3d9_vertex_declaration *declaration_impl;
2601 TRACE("iface %p, declaration %p.\n", iface, declaration);
2603 if (!declaration) return D3DERR_INVALIDCALL;
2605 wined3d_mutex_lock();
2606 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2608 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2609 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2610 IDirect3DVertexDeclaration9_AddRef(*declaration);
2612 else
2614 *declaration = NULL;
2616 wined3d_mutex_unlock();
2618 TRACE("Returning %p.\n", *declaration);
2619 return D3D_OK;
2622 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2624 struct wined3d_vertex_declaration *wined3d_declaration;
2625 struct fvf_declaration *fvf_decls = device->fvf_decls;
2626 struct d3d9_vertex_declaration *d3d9_declaration;
2627 D3DVERTEXELEMENT9 *elements;
2628 int p, low, high; /* deliberately signed */
2629 HRESULT hr;
2631 TRACE("Searching for declaration for fvf %08x... ", fvf);
2633 low = 0;
2634 high = device->fvf_decl_count - 1;
2635 while (low <= high)
2637 p = (low + high) >> 1;
2638 TRACE("%d ", p);
2640 if (fvf_decls[p].fvf == fvf)
2642 TRACE("found %p.\n", fvf_decls[p].decl);
2643 return fvf_decls[p].decl;
2646 if (fvf_decls[p].fvf < fvf)
2647 low = p + 1;
2648 else
2649 high = p - 1;
2651 TRACE("not found. Creating and inserting at position %d.\n", low);
2653 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2654 return NULL;
2656 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2657 HeapFree(GetProcessHeap(), 0, elements);
2658 if (FAILED(hr))
2659 return NULL;
2661 if (device->fvf_decl_size == device->fvf_decl_count)
2663 UINT grow = max(device->fvf_decl_size / 2, 8);
2665 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2666 if (!fvf_decls)
2668 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2669 return NULL;
2671 device->fvf_decls = fvf_decls;
2672 device->fvf_decl_size += grow;
2675 d3d9_declaration->fvf = fvf;
2676 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2677 wined3d_vertex_declaration_incref(wined3d_declaration);
2678 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2680 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2681 fvf_decls[low].decl = wined3d_declaration;
2682 fvf_decls[low].fvf = fvf;
2683 ++device->fvf_decl_count;
2685 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2687 return wined3d_declaration;
2690 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2692 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2693 struct wined3d_vertex_declaration *decl;
2695 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2697 if (!fvf)
2699 WARN("%#x is not a valid FVF.\n", fvf);
2700 return D3D_OK;
2703 wined3d_mutex_lock();
2704 if (!(decl = device_get_fvf_declaration(device, fvf)))
2706 wined3d_mutex_unlock();
2707 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2708 return D3DERR_DRIVERINTERNALERROR;
2711 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2712 wined3d_mutex_unlock();
2714 return D3D_OK;
2717 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2719 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2720 struct wined3d_vertex_declaration *wined3d_declaration;
2721 struct d3d9_vertex_declaration *d3d9_declaration;
2723 TRACE("iface %p, fvf %p.\n", iface, fvf);
2725 wined3d_mutex_lock();
2726 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2728 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2729 *fvf = d3d9_declaration->fvf;
2731 else
2733 *fvf = 0;
2735 wined3d_mutex_unlock();
2737 TRACE("Returning FVF %#x.\n", *fvf);
2739 return D3D_OK;
2742 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2743 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2745 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2746 struct d3d9_vertexshader *object;
2747 HRESULT hr;
2749 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2751 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2752 if (!object)
2753 return E_OUTOFMEMORY;
2755 hr = vertexshader_init(object, device, byte_code);
2756 if (FAILED(hr))
2758 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2759 HeapFree(GetProcessHeap(), 0, object);
2760 return hr;
2763 TRACE("Created vertex shader %p.\n", object);
2764 *shader = &object->IDirect3DVertexShader9_iface;
2766 return D3D_OK;
2769 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2771 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2772 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2774 TRACE("iface %p, shader %p.\n", iface, shader);
2776 wined3d_mutex_lock();
2777 wined3d_device_set_vertex_shader(device->wined3d_device,
2778 shader_obj ? shader_obj->wined3d_shader : NULL);
2779 wined3d_mutex_unlock();
2781 return D3D_OK;
2784 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2786 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2787 struct d3d9_vertexshader *shader_impl;
2788 struct wined3d_shader *wined3d_shader;
2790 TRACE("iface %p, shader %p.\n", iface, shader);
2792 wined3d_mutex_lock();
2793 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2795 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2796 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2797 IDirect3DVertexShader9_AddRef(*shader);
2799 else
2801 *shader = NULL;
2803 wined3d_mutex_unlock();
2805 TRACE("Returning %p.\n", *shader);
2807 return D3D_OK;
2810 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2811 UINT reg_idx, const float *data, UINT count)
2813 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2814 HRESULT hr;
2816 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2818 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2820 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2821 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2822 return D3DERR_INVALIDCALL;
2825 wined3d_mutex_lock();
2826 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2827 wined3d_mutex_unlock();
2829 return hr;
2832 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2833 UINT reg_idx, float *data, UINT count)
2835 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2836 HRESULT hr;
2838 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2840 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2842 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2843 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2844 return D3DERR_INVALIDCALL;
2847 wined3d_mutex_lock();
2848 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2849 wined3d_mutex_unlock();
2851 return hr;
2854 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2855 UINT reg_idx, const int *data, UINT count)
2857 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2858 HRESULT hr;
2860 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2862 wined3d_mutex_lock();
2863 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2864 wined3d_mutex_unlock();
2866 return hr;
2869 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2870 UINT reg_idx, int *data, UINT count)
2872 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2873 HRESULT hr;
2875 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2877 wined3d_mutex_lock();
2878 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2879 wined3d_mutex_unlock();
2881 return hr;
2884 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2885 UINT reg_idx, const BOOL *data, UINT count)
2887 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2888 HRESULT hr;
2890 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2892 wined3d_mutex_lock();
2893 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2894 wined3d_mutex_unlock();
2896 return hr;
2899 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2900 UINT reg_idx, BOOL *data, UINT count)
2902 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2903 HRESULT hr;
2905 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2907 wined3d_mutex_lock();
2908 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2909 wined3d_mutex_unlock();
2911 return hr;
2914 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2915 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2917 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2918 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2919 HRESULT hr;
2921 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2922 iface, stream_idx, buffer, offset, stride);
2924 wined3d_mutex_lock();
2925 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2926 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2927 wined3d_mutex_unlock();
2929 return hr;
2932 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2933 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2935 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2936 struct d3d9_vertexbuffer *buffer_impl;
2937 struct wined3d_buffer *wined3d_buffer;
2938 HRESULT hr;
2940 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2941 iface, stream_idx, buffer, offset, stride);
2943 if (!buffer)
2944 return D3DERR_INVALIDCALL;
2946 wined3d_mutex_lock();
2947 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2948 if (SUCCEEDED(hr) && wined3d_buffer)
2950 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2951 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2952 IDirect3DVertexBuffer9_AddRef(*buffer);
2954 else
2956 if (FAILED(hr))
2957 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2958 *buffer = NULL;
2960 wined3d_mutex_unlock();
2962 return hr;
2965 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2967 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2968 HRESULT hr;
2970 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2972 wined3d_mutex_lock();
2973 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2974 wined3d_mutex_unlock();
2976 return hr;
2979 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2981 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2982 HRESULT hr;
2984 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2986 wined3d_mutex_lock();
2987 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2988 wined3d_mutex_unlock();
2990 return hr;
2993 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2995 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2996 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2998 TRACE("iface %p, buffer %p.\n", iface, buffer);
3000 wined3d_mutex_lock();
3001 wined3d_device_set_index_buffer(device->wined3d_device,
3002 ib ? ib->wined3d_buffer : NULL,
3003 ib ? ib->format : WINED3DFMT_UNKNOWN);
3004 wined3d_mutex_unlock();
3006 return D3D_OK;
3009 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3011 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3012 enum wined3d_format_id wined3d_format;
3013 struct wined3d_buffer *wined3d_buffer;
3014 struct d3d9_indexbuffer *buffer_impl;
3016 TRACE("iface %p, buffer %p.\n", iface, buffer);
3018 if (!buffer)
3019 return D3DERR_INVALIDCALL;
3021 wined3d_mutex_lock();
3022 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
3024 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3025 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3026 IDirect3DIndexBuffer9_AddRef(*buffer);
3028 else
3030 *buffer = NULL;
3032 wined3d_mutex_unlock();
3034 return D3D_OK;
3037 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3038 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3040 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3041 struct d3d9_pixelshader *object;
3042 HRESULT hr;
3044 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3046 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3047 if (!object)
3049 FIXME("Failed to allocate pixel shader memory.\n");
3050 return E_OUTOFMEMORY;
3053 hr = pixelshader_init(object, device, byte_code);
3054 if (FAILED(hr))
3056 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3057 HeapFree(GetProcessHeap(), 0, object);
3058 return hr;
3061 TRACE("Created pixel shader %p.\n", object);
3062 *shader = &object->IDirect3DPixelShader9_iface;
3064 return D3D_OK;
3067 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3069 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3070 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3072 TRACE("iface %p, shader %p.\n", iface, shader);
3074 wined3d_mutex_lock();
3075 wined3d_device_set_pixel_shader(device->wined3d_device,
3076 shader_obj ? shader_obj->wined3d_shader : NULL);
3077 wined3d_mutex_unlock();
3079 return D3D_OK;
3082 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3084 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3085 struct d3d9_pixelshader *shader_impl;
3086 struct wined3d_shader *wined3d_shader;
3088 TRACE("iface %p, shader %p.\n", iface, shader);
3090 if (!shader) return D3DERR_INVALIDCALL;
3092 wined3d_mutex_lock();
3093 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3095 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3096 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3097 IDirect3DPixelShader9_AddRef(*shader);
3099 else
3101 *shader = NULL;
3103 wined3d_mutex_unlock();
3105 TRACE("Returning %p.\n", *shader);
3107 return D3D_OK;
3110 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3111 UINT reg_idx, const float *data, UINT count)
3113 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3114 HRESULT hr;
3116 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3118 wined3d_mutex_lock();
3119 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3120 wined3d_mutex_unlock();
3122 return hr;
3125 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3126 UINT reg_idx, float *data, UINT count)
3128 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3129 HRESULT hr;
3131 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3133 wined3d_mutex_lock();
3134 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3135 wined3d_mutex_unlock();
3137 return hr;
3140 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3141 UINT reg_idx, const int *data, UINT count)
3143 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3144 HRESULT hr;
3146 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3148 wined3d_mutex_lock();
3149 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3150 wined3d_mutex_unlock();
3152 return hr;
3155 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3156 UINT reg_idx, int *data, UINT count)
3158 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3159 HRESULT hr;
3161 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3163 wined3d_mutex_lock();
3164 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3165 wined3d_mutex_unlock();
3167 return hr;
3170 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3171 UINT reg_idx, const BOOL *data, UINT count)
3173 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3174 HRESULT hr;
3176 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3178 wined3d_mutex_lock();
3179 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3180 wined3d_mutex_unlock();
3182 return hr;
3185 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3186 UINT reg_idx, BOOL *data, UINT count)
3188 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3189 HRESULT hr;
3191 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3193 wined3d_mutex_lock();
3194 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3195 wined3d_mutex_unlock();
3197 return hr;
3200 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3201 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3203 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3204 iface, handle, segment_count, patch_info);
3205 return D3D_OK;
3208 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3209 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3211 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3212 iface, handle, segment_count, patch_info);
3213 return D3D_OK;
3216 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3218 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3219 return D3DERR_INVALIDCALL;
3222 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3224 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3225 struct d3d9_query *object;
3226 HRESULT hr;
3228 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3230 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3231 if (!object)
3232 return E_OUTOFMEMORY;
3234 hr = query_init(object, device, type);
3235 if (FAILED(hr))
3237 WARN("Failed to initialize query, hr %#x.\n", hr);
3238 HeapFree(GetProcessHeap(), 0, object);
3239 return hr;
3242 TRACE("Created query %p.\n", object);
3243 if (query) *query = &object->IDirect3DQuery9_iface;
3244 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3246 return D3D_OK;
3249 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3250 UINT width, UINT height, float *rows, float *columns)
3252 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3253 iface, width, height, rows, columns);
3255 return E_NOTIMPL;
3258 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3259 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3260 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3262 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3263 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3264 iface, src_surface, dst_surface, src_descs, rect_count,
3265 dst_descs, operation, offset_x, offset_y);
3267 return E_NOTIMPL;
3270 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3271 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3272 const RGNDATA *dirty_region, DWORD flags)
3274 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3275 UINT i;
3276 HRESULT hr;
3278 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3279 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3280 dst_window_override, dirty_region, flags);
3282 if (device->device_state != D3D9_DEVICE_STATE_OK)
3283 return S_PRESENT_OCCLUDED;
3285 if (dirty_region)
3286 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3288 wined3d_mutex_lock();
3289 for (i = 0; i < device->implicit_swapchain_count; ++i)
3291 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3292 src_rect, dst_rect, dst_window_override, flags)))
3294 wined3d_mutex_unlock();
3295 return hr;
3298 wined3d_mutex_unlock();
3300 return D3D_OK;
3303 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3305 FIXME("iface %p, priority %p stub!\n", iface, priority);
3307 return E_NOTIMPL;
3310 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3312 FIXME("iface %p, priority %d stub!\n", iface, priority);
3314 return E_NOTIMPL;
3317 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3319 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3321 return E_NOTIMPL;
3324 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3325 IDirect3DResource9 **resources, UINT32 resource_count)
3327 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3328 iface, resources, resource_count);
3330 return E_NOTIMPL;
3333 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3335 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3337 return E_NOTIMPL;
3340 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3342 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3344 *max_latency = 2;
3346 return E_NOTIMPL;
3349 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3352 struct wined3d_swapchain_desc swapchain_desc;
3354 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3356 wined3d_mutex_lock();
3357 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3358 wined3d_mutex_unlock();
3360 if (swapchain_desc.windowed)
3361 return D3D_OK;
3363 /* FIXME: This is actually supposed to check if any other device is in
3364 * fullscreen mode. */
3365 if (dst_window != swapchain_desc.device_window)
3366 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3368 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3371 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3372 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3373 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3375 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3376 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3377 iface, width, height, format, multisample_type, multisample_quality,
3378 lockable, surface, shared_handle, usage);
3380 *surface = NULL;
3381 if (shared_handle)
3382 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3384 return E_NOTIMPL;
3387 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3388 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3389 HANDLE *shared_handle, DWORD usage)
3391 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3392 iface, width, height, format, pool, surface, shared_handle, usage);
3394 return E_NOTIMPL;
3397 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3398 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3399 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3401 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3402 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3404 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3405 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3406 iface, width, height, format, multisample_type, multisample_quality,
3407 discard, surface, shared_handle, usage);
3409 if (usage & D3DUSAGE_DEPTHSTENCIL)
3411 WARN("Invalid usage %#x.\n", usage);
3412 return D3DERR_INVALIDCALL;
3415 if (shared_handle)
3416 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3418 if (discard)
3419 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3421 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3422 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3425 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3426 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3428 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3430 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3432 return d3d9_device_reset(device, present_parameters, mode);
3435 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3436 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3438 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3439 struct wined3d_display_mode wined3d_mode;
3440 HRESULT hr;
3442 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3443 iface, swapchain_idx, mode, rotation);
3445 if (mode->Size != sizeof(*mode))
3446 return D3DERR_INVALIDCALL;
3448 wined3d_mutex_lock();
3449 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3450 (enum wined3d_display_rotation *)rotation);
3451 wined3d_mutex_unlock();
3453 if (SUCCEEDED(hr))
3455 mode->Width = wined3d_mode.width;
3456 mode->Height = wined3d_mode.height;
3457 mode->RefreshRate = wined3d_mode.refresh_rate;
3458 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3459 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3462 return hr;
3465 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3467 /* IUnknown */
3468 d3d9_device_QueryInterface,
3469 d3d9_device_AddRef,
3470 d3d9_device_Release,
3471 /* IDirect3DDevice9 */
3472 d3d9_device_TestCooperativeLevel,
3473 d3d9_device_GetAvailableTextureMem,
3474 d3d9_device_EvictManagedResources,
3475 d3d9_device_GetDirect3D,
3476 d3d9_device_GetDeviceCaps,
3477 d3d9_device_GetDisplayMode,
3478 d3d9_device_GetCreationParameters,
3479 d3d9_device_SetCursorProperties,
3480 d3d9_device_SetCursorPosition,
3481 d3d9_device_ShowCursor,
3482 d3d9_device_CreateAdditionalSwapChain,
3483 d3d9_device_GetSwapChain,
3484 d3d9_device_GetNumberOfSwapChains,
3485 d3d9_device_Reset,
3486 d3d9_device_Present,
3487 d3d9_device_GetBackBuffer,
3488 d3d9_device_GetRasterStatus,
3489 d3d9_device_SetDialogBoxMode,
3490 d3d9_device_SetGammaRamp,
3491 d3d9_device_GetGammaRamp,
3492 d3d9_device_CreateTexture,
3493 d3d9_device_CreateVolumeTexture,
3494 d3d9_device_CreateCubeTexture,
3495 d3d9_device_CreateVertexBuffer,
3496 d3d9_device_CreateIndexBuffer,
3497 d3d9_device_CreateRenderTarget,
3498 d3d9_device_CreateDepthStencilSurface,
3499 d3d9_device_UpdateSurface,
3500 d3d9_device_UpdateTexture,
3501 d3d9_device_GetRenderTargetData,
3502 d3d9_device_GetFrontBufferData,
3503 d3d9_device_StretchRect,
3504 d3d9_device_ColorFill,
3505 d3d9_device_CreateOffscreenPlainSurface,
3506 d3d9_device_SetRenderTarget,
3507 d3d9_device_GetRenderTarget,
3508 d3d9_device_SetDepthStencilSurface,
3509 d3d9_device_GetDepthStencilSurface,
3510 d3d9_device_BeginScene,
3511 d3d9_device_EndScene,
3512 d3d9_device_Clear,
3513 d3d9_device_SetTransform,
3514 d3d9_device_GetTransform,
3515 d3d9_device_MultiplyTransform,
3516 d3d9_device_SetViewport,
3517 d3d9_device_GetViewport,
3518 d3d9_device_SetMaterial,
3519 d3d9_device_GetMaterial,
3520 d3d9_device_SetLight,
3521 d3d9_device_GetLight,
3522 d3d9_device_LightEnable,
3523 d3d9_device_GetLightEnable,
3524 d3d9_device_SetClipPlane,
3525 d3d9_device_GetClipPlane,
3526 d3d9_device_SetRenderState,
3527 d3d9_device_GetRenderState,
3528 d3d9_device_CreateStateBlock,
3529 d3d9_device_BeginStateBlock,
3530 d3d9_device_EndStateBlock,
3531 d3d9_device_SetClipStatus,
3532 d3d9_device_GetClipStatus,
3533 d3d9_device_GetTexture,
3534 d3d9_device_SetTexture,
3535 d3d9_device_GetTextureStageState,
3536 d3d9_device_SetTextureStageState,
3537 d3d9_device_GetSamplerState,
3538 d3d9_device_SetSamplerState,
3539 d3d9_device_ValidateDevice,
3540 d3d9_device_SetPaletteEntries,
3541 d3d9_device_GetPaletteEntries,
3542 d3d9_device_SetCurrentTexturePalette,
3543 d3d9_device_GetCurrentTexturePalette,
3544 d3d9_device_SetScissorRect,
3545 d3d9_device_GetScissorRect,
3546 d3d9_device_SetSoftwareVertexProcessing,
3547 d3d9_device_GetSoftwareVertexProcessing,
3548 d3d9_device_SetNPatchMode,
3549 d3d9_device_GetNPatchMode,
3550 d3d9_device_DrawPrimitive,
3551 d3d9_device_DrawIndexedPrimitive,
3552 d3d9_device_DrawPrimitiveUP,
3553 d3d9_device_DrawIndexedPrimitiveUP,
3554 d3d9_device_ProcessVertices,
3555 d3d9_device_CreateVertexDeclaration,
3556 d3d9_device_SetVertexDeclaration,
3557 d3d9_device_GetVertexDeclaration,
3558 d3d9_device_SetFVF,
3559 d3d9_device_GetFVF,
3560 d3d9_device_CreateVertexShader,
3561 d3d9_device_SetVertexShader,
3562 d3d9_device_GetVertexShader,
3563 d3d9_device_SetVertexShaderConstantF,
3564 d3d9_device_GetVertexShaderConstantF,
3565 d3d9_device_SetVertexShaderConstantI,
3566 d3d9_device_GetVertexShaderConstantI,
3567 d3d9_device_SetVertexShaderConstantB,
3568 d3d9_device_GetVertexShaderConstantB,
3569 d3d9_device_SetStreamSource,
3570 d3d9_device_GetStreamSource,
3571 d3d9_device_SetStreamSourceFreq,
3572 d3d9_device_GetStreamSourceFreq,
3573 d3d9_device_SetIndices,
3574 d3d9_device_GetIndices,
3575 d3d9_device_CreatePixelShader,
3576 d3d9_device_SetPixelShader,
3577 d3d9_device_GetPixelShader,
3578 d3d9_device_SetPixelShaderConstantF,
3579 d3d9_device_GetPixelShaderConstantF,
3580 d3d9_device_SetPixelShaderConstantI,
3581 d3d9_device_GetPixelShaderConstantI,
3582 d3d9_device_SetPixelShaderConstantB,
3583 d3d9_device_GetPixelShaderConstantB,
3584 d3d9_device_DrawRectPatch,
3585 d3d9_device_DrawTriPatch,
3586 d3d9_device_DeletePatch,
3587 d3d9_device_CreateQuery,
3588 /* IDirect3DDevice9Ex */
3589 d3d9_device_SetConvolutionMonoKernel,
3590 d3d9_device_ComposeRects,
3591 d3d9_device_PresentEx,
3592 d3d9_device_GetGPUThreadPriority,
3593 d3d9_device_SetGPUThreadPriority,
3594 d3d9_device_WaitForVBlank,
3595 d3d9_device_CheckResourceResidency,
3596 d3d9_device_SetMaximumFrameLatency,
3597 d3d9_device_GetMaximumFrameLatency,
3598 d3d9_device_CheckDeviceState,
3599 d3d9_device_CreateRenderTargetEx,
3600 d3d9_device_CreateOffscreenPlainSurfaceEx,
3601 d3d9_device_CreateDepthStencilSurfaceEx,
3602 d3d9_device_ResetEx,
3603 d3d9_device_GetDisplayModeEx,
3606 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3608 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3611 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3612 struct wined3d_device *device)
3614 TRACE("device_parent %p, device %p.\n", device_parent, device);
3617 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3619 TRACE("device_parent %p.\n", device_parent);
3622 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3624 struct d3d9_device *device = device_from_device_parent(device_parent);
3626 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3628 if (!device->d3d_parent)
3629 return;
3631 if (!activate)
3632 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3633 else if (device->d3d_parent->extended)
3634 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3635 else
3636 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3639 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3640 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3641 void **parent, const struct wined3d_parent_ops **parent_ops)
3643 struct d3d9_surface *d3d_surface;
3645 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3646 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3648 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3649 return E_OUTOFMEMORY;
3651 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3652 *parent = d3d_surface;
3653 TRACE("Created surface %p.\n", d3d_surface);
3655 return D3D_OK;
3658 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3659 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3660 void **parent, const struct wined3d_parent_ops **parent_ops)
3662 struct d3d9_volume *d3d_volume;
3664 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3665 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3667 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3668 return E_OUTOFMEMORY;
3670 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3671 *parent = d3d_volume;
3672 TRACE("Created volume %p.\n", d3d_volume);
3674 return D3D_OK;
3677 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3678 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3680 struct d3d9_device *device = device_from_device_parent(device_parent);
3681 struct d3d9_surface *d3d_surface;
3682 HRESULT hr;
3684 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3685 device_parent, container_parent, desc, texture);
3687 if (container_parent == device_parent)
3688 container_parent = &device->IDirect3DDevice9Ex_iface;
3690 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1,
3691 WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3693 WARN("Failed to create texture, hr %#x.\n", hr);
3694 return hr;
3697 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3698 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3700 return hr;
3703 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3704 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3706 struct d3d9_device *device = device_from_device_parent(device_parent);
3707 struct d3d9_swapchain *d3d_swapchain;
3708 HRESULT hr;
3710 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3712 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3713 if (FAILED(hr))
3715 WARN("Failed to create swapchain, hr %#x.\n", hr);
3716 *swapchain = NULL;
3717 return hr;
3720 *swapchain = d3d_swapchain->wined3d_swapchain;
3721 wined3d_swapchain_incref(*swapchain);
3722 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3724 return hr;
3727 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3729 device_parent_wined3d_device_created,
3730 device_parent_mode_changed,
3731 device_parent_activate,
3732 device_parent_surface_created,
3733 device_parent_volume_created,
3734 device_parent_create_swapchain_texture,
3735 device_parent_create_swapchain,
3738 static void setup_fpu(void)
3740 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3741 WORD cw;
3742 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3743 cw = (cw & ~0xf3f) | 0x3f;
3744 __asm__ volatile ("fldcw %0" : : "m" (cw));
3745 #elif defined(__i386__) && defined(_MSC_VER)
3746 WORD cw;
3747 __asm fnstcw cw;
3748 cw = (cw & ~0xf3f) | 0x3f;
3749 __asm fldcw cw;
3750 #else
3751 FIXME("FPU setup not implemented for this platform.\n");
3752 #endif
3755 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3756 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3757 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3759 struct wined3d_swapchain_desc *swapchain_desc;
3760 UINT i, count = 1;
3761 HRESULT hr;
3763 if (mode)
3764 FIXME("Ignoring display mode.\n");
3766 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3767 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3768 device->refcount = 1;
3770 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3772 wined3d_mutex_lock();
3773 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3774 &device->device_parent, &device->wined3d_device);
3775 if (FAILED(hr))
3777 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3778 wined3d_mutex_unlock();
3779 return hr;
3782 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3784 WINED3DCAPS caps;
3786 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3787 count = caps.NumberOfAdaptersInGroup;
3790 if (flags & D3DCREATE_MULTITHREADED)
3791 wined3d_device_set_multithreaded(device->wined3d_device);
3793 if (!parameters->Windowed)
3795 if (!focus_window)
3796 focus_window = parameters->hDeviceWindow;
3797 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3799 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3800 wined3d_device_decref(device->wined3d_device);
3801 wined3d_mutex_unlock();
3802 return hr;
3805 for (i = 0; i < count; ++i)
3807 HWND device_window = parameters[i].hDeviceWindow;
3809 if (!device_window) device_window = focus_window;
3810 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3811 parameters[i].BackBufferWidth,
3812 parameters[i].BackBufferHeight);
3816 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3817 if (!swapchain_desc)
3819 ERR("Failed to allocate wined3d parameters.\n");
3820 wined3d_device_release_focus_window(device->wined3d_device);
3821 wined3d_device_decref(device->wined3d_device);
3822 wined3d_mutex_unlock();
3823 return E_OUTOFMEMORY;
3826 for (i = 0; i < count; ++i)
3828 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3829 parent->extended))
3831 wined3d_device_release_focus_window(device->wined3d_device);
3832 wined3d_device_decref(device->wined3d_device);
3833 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3834 wined3d_mutex_unlock();
3835 return D3DERR_INVALIDCALL;
3839 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3840 if (FAILED(hr))
3842 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3843 wined3d_device_release_focus_window(device->wined3d_device);
3844 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3845 wined3d_device_decref(device->wined3d_device);
3846 wined3d_mutex_unlock();
3847 return hr;
3850 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3852 wined3d_device_uninit_3d(device->wined3d_device);
3853 wined3d_device_release_focus_window(device->wined3d_device);
3854 wined3d_device_decref(device->wined3d_device);
3855 wined3d_mutex_unlock();
3856 return E_OUTOFMEMORY;
3859 for (i = 0; i < count; ++i)
3861 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3864 wined3d_mutex_unlock();
3866 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3868 /* Initialize the converted declaration array. This creates a valid pointer
3869 * and when adding decls HeapReAlloc() can be used without further checking. */
3870 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3871 if (!device->fvf_decls)
3873 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3874 wined3d_mutex_lock();
3875 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3876 wined3d_device_uninit_3d(device->wined3d_device);
3877 wined3d_device_release_focus_window(device->wined3d_device);
3878 wined3d_device_decref(device->wined3d_device);
3879 wined3d_mutex_unlock();
3880 return E_OUTOFMEMORY;
3883 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3884 device->d3d_parent = parent;
3886 return D3D_OK;