d3d9: Reject render target surfaces created for other devices.
[wine.git] / dlls / d3d9 / device.c
blobd47c705bb9549f99ae09f687e5b8b6ae2ae06ba1
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_R16_UINT: return D3DFMT_INDEX16;
84 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
85 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
86 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
87 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
88 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
89 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
90 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
91 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
92 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
93 default:
94 FIXME("Unhandled wined3d format %#x.\n", format);
95 return D3DFMT_UNKNOWN;
99 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
101 BYTE *c = (BYTE *)&format;
103 /* Don't translate FOURCC formats */
104 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
106 switch(format)
108 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
109 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
110 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
111 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
112 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
113 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
114 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
115 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
116 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
117 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
118 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
119 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
120 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
121 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
122 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
123 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
124 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
125 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
126 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
127 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
128 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
129 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
130 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
131 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
132 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
133 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
134 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
135 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
136 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
137 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
138 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
139 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
140 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
141 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
142 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
143 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
144 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
145 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
146 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
147 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
148 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
149 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
150 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
151 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
152 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
153 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
154 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
155 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
156 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
157 default:
158 FIXME("Unhandled D3DFORMAT %#x.\n", format);
159 return WINED3DFMT_UNKNOWN;
163 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
165 switch(primitive_type)
167 case D3DPT_POINTLIST:
168 return primitive_count;
170 case D3DPT_LINELIST:
171 return primitive_count * 2;
173 case D3DPT_LINESTRIP:
174 return primitive_count + 1;
176 case D3DPT_TRIANGLELIST:
177 return primitive_count * 3;
179 case D3DPT_TRIANGLESTRIP:
180 case D3DPT_TRIANGLEFAN:
181 return primitive_count + 2;
183 default:
184 FIXME("Unhandled primitive type %#x\n", primitive_type);
185 return 0;
189 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
190 const struct wined3d_swapchain_desc *swapchain_desc)
192 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
193 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
194 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
195 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
196 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
197 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
198 present_parameters->SwapEffect = swapchain_desc->swap_effect;
199 present_parameters->hDeviceWindow = swapchain_desc->device_window;
200 present_parameters->Windowed = swapchain_desc->windowed;
201 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
202 present_parameters->AutoDepthStencilFormat
203 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
204 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
205 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
206 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
209 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
210 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
212 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
213 UINT highest_bb_count = extended ? 30 : 3;
215 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
217 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
218 return FALSE;
220 if (present_parameters->BackBufferCount > highest_bb_count
221 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
222 && present_parameters->BackBufferCount > 1))
224 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
225 return FALSE;
228 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
229 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
230 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
231 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
232 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
233 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
234 swapchain_desc->swap_effect = present_parameters->SwapEffect;
235 swapchain_desc->device_window = present_parameters->hDeviceWindow;
236 swapchain_desc->windowed = present_parameters->Windowed;
237 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
238 swapchain_desc->auto_depth_stencil_format
239 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
240 swapchain_desc->flags
241 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
242 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
243 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
244 swapchain_desc->auto_restore_display_mode = TRUE;
246 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
247 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
249 return TRUE;
252 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
254 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
256 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
257 || IsEqualGUID(riid, &IID_IUnknown))
259 IDirect3DDevice9Ex_AddRef(iface);
260 *out = iface;
261 return S_OK;
264 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
266 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
268 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
269 * It doesn't matter with which function the device was created. */
270 if (!device->d3d_parent->extended)
272 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
273 *out = NULL;
274 return E_NOINTERFACE;
277 IDirect3DDevice9Ex_AddRef(iface);
278 *out = iface;
279 return S_OK;
282 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
284 *out = NULL;
285 return E_NOINTERFACE;
288 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
290 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
291 ULONG refcount = InterlockedIncrement(&device->refcount);
293 TRACE("%p increasing refcount to %u.\n", iface, refcount);
295 return refcount;
298 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
300 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
301 ULONG refcount;
303 if (device->in_destruction)
304 return 0;
306 refcount = InterlockedDecrement(&device->refcount);
308 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
310 if (!refcount)
312 unsigned i;
313 device->in_destruction = TRUE;
315 wined3d_mutex_lock();
316 for (i = 0; i < device->fvf_decl_count; ++i)
318 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
320 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
322 if (device->vertex_buffer)
323 wined3d_buffer_decref(device->vertex_buffer);
324 if (device->index_buffer)
325 wined3d_buffer_decref(device->index_buffer);
327 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
329 wined3d_device_uninit_3d(device->wined3d_device);
330 wined3d_device_release_focus_window(device->wined3d_device);
331 wined3d_device_decref(device->wined3d_device);
332 wined3d_mutex_unlock();
334 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
336 HeapFree(GetProcessHeap(), 0, device);
339 return refcount;
342 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
344 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
346 TRACE("iface %p.\n", iface);
348 TRACE("device state: %#x.\n", device->device_state);
350 if (device->d3d_parent->extended)
351 return D3D_OK;
353 switch (device->device_state)
355 default:
356 case D3D9_DEVICE_STATE_OK:
357 return D3D_OK;
358 case D3D9_DEVICE_STATE_LOST:
359 return D3DERR_DEVICELOST;
360 case D3D9_DEVICE_STATE_NOT_RESET:
361 return D3DERR_DEVICENOTRESET;
365 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
367 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
368 UINT ret;
370 TRACE("iface %p.\n", iface);
372 wined3d_mutex_lock();
373 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
374 wined3d_mutex_unlock();
376 return ret;
379 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
381 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
383 TRACE("iface %p.\n", iface);
385 wined3d_mutex_lock();
386 wined3d_device_evict_managed_resources(device->wined3d_device);
387 wined3d_mutex_unlock();
389 return D3D_OK;
392 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
394 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
396 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
398 if (!d3d9)
399 return D3DERR_INVALIDCALL;
401 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
404 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
406 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
407 WINED3DCAPS *wined3d_caps;
408 HRESULT hr;
410 TRACE("iface %p, caps %p.\n", iface, caps);
412 if (!caps)
413 return D3DERR_INVALIDCALL;
415 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
416 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
418 memset(caps, 0, sizeof(*caps));
420 wined3d_mutex_lock();
421 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
422 wined3d_mutex_unlock();
424 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
425 HeapFree(GetProcessHeap(), 0, wined3d_caps);
427 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
428 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
430 filter_caps(caps);
432 return hr;
435 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
437 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
438 struct wined3d_display_mode wined3d_mode;
439 HRESULT hr;
441 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
443 wined3d_mutex_lock();
444 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
445 wined3d_mutex_unlock();
447 if (SUCCEEDED(hr))
449 mode->Width = wined3d_mode.width;
450 mode->Height = wined3d_mode.height;
451 mode->RefreshRate = wined3d_mode.refresh_rate;
452 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
455 return hr;
458 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
459 D3DDEVICE_CREATION_PARAMETERS *parameters)
461 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
463 TRACE("iface %p, parameters %p.\n", iface, parameters);
465 wined3d_mutex_lock();
466 wined3d_device_get_creation_parameters(device->wined3d_device,
467 (struct wined3d_device_creation_parameters *)parameters);
468 wined3d_mutex_unlock();
470 return D3D_OK;
473 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
474 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
476 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
477 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
478 HRESULT hr;
480 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
481 iface, hotspot_x, hotspot_y, bitmap);
483 if (!bitmap)
485 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
486 return D3DERR_INVALIDCALL;
489 wined3d_mutex_lock();
490 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
491 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
492 wined3d_mutex_unlock();
494 return hr;
497 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
499 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
501 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
503 wined3d_mutex_lock();
504 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
505 wined3d_mutex_unlock();
508 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
510 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
511 BOOL ret;
513 TRACE("iface %p, show %#x.\n", iface, show);
515 wined3d_mutex_lock();
516 ret = wined3d_device_show_cursor(device->wined3d_device, show);
517 wined3d_mutex_unlock();
519 return ret;
522 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
523 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
525 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
526 struct wined3d_swapchain_desc desc;
527 struct d3d9_swapchain *object;
528 UINT i, count;
529 HRESULT hr;
531 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
532 iface, present_parameters, swapchain);
534 if (!present_parameters->Windowed)
536 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
537 return D3DERR_INVALIDCALL;
540 wined3d_mutex_lock();
541 count = wined3d_device_get_swapchain_count(device->wined3d_device);
542 for (i = 0; i < count; ++i)
544 struct wined3d_swapchain *wined3d_swapchain;
546 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
547 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
549 if (!desc.windowed)
551 wined3d_mutex_unlock();
552 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
553 return D3DERR_INVALIDCALL;
556 wined3d_mutex_unlock();
558 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
559 device->d3d_parent->extended))
560 return D3DERR_INVALIDCALL;
561 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
562 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
563 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
565 return hr;
568 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
569 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
571 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
572 HRESULT hr;
574 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
576 wined3d_mutex_lock();
577 if (swapchain_idx < device->implicit_swapchain_count)
579 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
580 IDirect3DSwapChain9Ex_AddRef(*swapchain);
581 hr = D3D_OK;
583 else
585 *swapchain = NULL;
586 hr = D3DERR_INVALIDCALL;
588 wined3d_mutex_unlock();
590 return hr;
593 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
595 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
596 UINT count;
598 TRACE("iface %p.\n", iface);
600 wined3d_mutex_lock();
601 count = wined3d_device_get_swapchain_count(device->wined3d_device);
602 wined3d_mutex_unlock();
604 return count;
607 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
609 struct wined3d_resource_desc desc;
610 IDirect3DBaseTexture9 *texture;
611 struct d3d9_surface *surface;
612 IUnknown *parent;
614 wined3d_resource_get_desc(resource, &desc);
615 if (desc.pool != WINED3D_POOL_DEFAULT)
616 return D3D_OK;
618 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
620 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
621 return D3DERR_INVALIDCALL;
624 parent = wined3d_resource_get_parent(resource);
625 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
627 IDirect3DBaseTexture9_Release(texture);
628 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
629 return D3DERR_INVALIDCALL;
632 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
633 if (!surface->resource.refcount)
634 return D3D_OK;
636 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
637 return D3DERR_INVALIDCALL;
640 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
642 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
643 struct wined3d_swapchain *wined3d_swapchain;
645 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
646 new_swapchain_count * sizeof(*device->implicit_swapchains))))
647 return E_OUTOFMEMORY;
649 for (i = 0; i < new_swapchain_count; ++i)
651 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
652 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
654 device->implicit_swapchain_count = new_swapchain_count;
656 return D3D_OK;
659 static HRESULT d3d9_device_reset(struct d3d9_device *device,
660 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
662 struct wined3d_swapchain_desc swapchain_desc;
663 struct wined3d_display_mode wined3d_mode;
664 HRESULT hr;
666 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
668 WARN("App not active, returning D3DERR_DEVICELOST.\n");
669 return D3DERR_DEVICELOST;
672 if (mode)
674 wined3d_mode.width = mode->Width;
675 wined3d_mode.height = mode->Height;
676 wined3d_mode.refresh_rate = mode->RefreshRate;
677 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
678 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
681 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
682 device->d3d_parent->extended))
683 return D3DERR_INVALIDCALL;
685 wined3d_mutex_lock();
687 if (device->vertex_buffer)
689 wined3d_buffer_decref(device->vertex_buffer);
690 device->vertex_buffer = NULL;
691 device->vertex_buffer_size = 0;
694 if (device->index_buffer)
696 wined3d_buffer_decref(device->index_buffer);
697 device->index_buffer = NULL;
698 device->index_buffer_size = 0;
701 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
702 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
704 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
706 if (FAILED(hr = d3d9_device_get_swapchains(device)))
708 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
710 else
712 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
713 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
714 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
715 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
716 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
718 device->device_state = D3D9_DEVICE_STATE_OK;
721 else if (!device->d3d_parent->extended)
723 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
726 wined3d_mutex_unlock();
728 return hr;
731 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
732 D3DPRESENT_PARAMETERS *present_parameters)
734 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
736 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
738 return d3d9_device_reset(device, present_parameters, NULL);
741 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
742 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
744 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
745 UINT i;
746 HRESULT hr;
748 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
749 iface, src_rect, dst_rect, dst_window_override, dirty_region);
751 if (device->device_state != D3D9_DEVICE_STATE_OK)
752 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
754 if (dirty_region)
755 FIXME("Ignoring dirty_region %p.\n", dirty_region);
757 wined3d_mutex_lock();
758 for (i = 0; i < device->implicit_swapchain_count; ++i)
760 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
761 src_rect, dst_rect, dst_window_override, 0)))
763 wined3d_mutex_unlock();
764 return hr;
767 wined3d_mutex_unlock();
769 return D3D_OK;
772 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
773 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
775 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
776 HRESULT hr;
778 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
779 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
781 /* backbuffer_type is ignored by native. */
783 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
784 *backbuffer = NULL;
786 wined3d_mutex_lock();
787 if (swapchain >= device->implicit_swapchain_count)
789 wined3d_mutex_unlock();
790 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
791 return D3DERR_INVALIDCALL;
794 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
795 backbuffer_idx, backbuffer_type, backbuffer);
796 wined3d_mutex_unlock();
798 return hr;
801 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
802 UINT swapchain, D3DRASTER_STATUS *raster_status)
804 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
805 HRESULT hr;
807 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
809 wined3d_mutex_lock();
810 hr = wined3d_device_get_raster_status(device->wined3d_device,
811 swapchain, (struct wined3d_raster_status *)raster_status);
812 wined3d_mutex_unlock();
814 return hr;
817 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
819 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
820 HRESULT hr;
822 TRACE("iface %p, enable %#x.\n", iface, enable);
824 wined3d_mutex_lock();
825 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
826 wined3d_mutex_unlock();
828 return hr;
831 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
832 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
834 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
836 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
838 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
839 wined3d_mutex_lock();
840 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
841 wined3d_mutex_unlock();
844 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
846 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
848 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
850 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
851 wined3d_mutex_lock();
852 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
853 wined3d_mutex_unlock();
856 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
857 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
858 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
860 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
861 struct d3d9_texture *object;
862 BOOL set_mem = FALSE;
863 HRESULT hr;
865 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
866 iface, width, height, levels, usage, format, pool, texture, shared_handle);
868 *texture = NULL;
869 if (shared_handle)
871 if (!device->d3d_parent->extended)
873 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
874 return E_NOTIMPL;
877 if (pool == D3DPOOL_SYSTEMMEM)
879 if (levels != 1)
880 return D3DERR_INVALIDCALL;
881 set_mem = TRUE;
883 else
885 if (pool != D3DPOOL_DEFAULT)
887 WARN("Trying to create a shared texture in pool %#x.\n", pool);
888 return D3DERR_INVALIDCALL;
890 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
894 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
895 if (!object)
896 return D3DERR_OUTOFVIDEOMEMORY;
898 hr = texture_init(object, device, width, height, levels, usage, format, pool);
899 if (FAILED(hr))
901 WARN("Failed to initialize texture, hr %#x.\n", hr);
902 HeapFree(GetProcessHeap(), 0, object);
903 return hr;
906 if (set_mem)
907 wined3d_texture_update_desc(object->wined3d_texture, width, height,
908 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
909 *shared_handle, 0);
911 TRACE("Created texture %p.\n", object);
912 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
914 return D3D_OK;
917 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
918 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
919 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
921 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
922 struct d3d9_texture *object;
923 HRESULT hr;
925 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
926 iface, width, height, depth, levels);
927 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
928 usage, format, pool, texture, shared_handle);
930 *texture = NULL;
931 if (shared_handle)
933 if (!device->d3d_parent->extended)
935 WARN("Trying to create a shared volume texture on a non-ex device.\n");
936 return E_NOTIMPL;
939 if (pool != D3DPOOL_DEFAULT)
941 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
942 return D3DERR_INVALIDCALL;
944 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
947 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
948 if (!object)
949 return D3DERR_OUTOFVIDEOMEMORY;
951 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
952 if (FAILED(hr))
954 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
955 HeapFree(GetProcessHeap(), 0, object);
956 return hr;
959 TRACE("Created volume texture %p.\n", object);
960 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
962 return D3D_OK;
965 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
966 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
967 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
969 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
970 struct d3d9_texture *object;
971 HRESULT hr;
973 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
974 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
976 *texture = NULL;
977 if (shared_handle)
979 if (!device->d3d_parent->extended)
981 WARN("Trying to create a shared cube texture on a non-ex device.\n");
982 return E_NOTIMPL;
985 if (pool != D3DPOOL_DEFAULT)
987 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
988 return D3DERR_INVALIDCALL;
990 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
993 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
994 if (!object)
995 return D3DERR_OUTOFVIDEOMEMORY;
997 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
998 if (FAILED(hr))
1000 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1001 HeapFree(GetProcessHeap(), 0, object);
1002 return hr;
1005 TRACE("Created cube texture %p.\n", object);
1006 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1008 return D3D_OK;
1011 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1012 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1013 HANDLE *shared_handle)
1015 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1016 struct d3d9_vertexbuffer *object;
1017 HRESULT hr;
1019 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1020 iface, size, usage, fvf, pool, buffer, shared_handle);
1022 if (shared_handle)
1024 if (!device->d3d_parent->extended)
1026 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1027 return E_NOTIMPL;
1030 if (pool != D3DPOOL_DEFAULT)
1032 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1033 return D3DERR_NOTAVAILABLE;
1035 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1038 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1039 if (!object)
1040 return D3DERR_OUTOFVIDEOMEMORY;
1042 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1043 if (FAILED(hr))
1045 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1046 HeapFree(GetProcessHeap(), 0, object);
1047 return hr;
1050 TRACE("Created vertex buffer %p.\n", object);
1051 *buffer = &object->IDirect3DVertexBuffer9_iface;
1053 return D3D_OK;
1056 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1057 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1058 HANDLE *shared_handle)
1060 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1061 struct d3d9_indexbuffer *object;
1062 HRESULT hr;
1064 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1065 iface, size, usage, format, pool, buffer, shared_handle);
1067 if (shared_handle)
1069 if (!device->d3d_parent->extended)
1071 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1072 return E_NOTIMPL;
1075 if (pool != D3DPOOL_DEFAULT)
1077 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1078 return D3DERR_NOTAVAILABLE;
1080 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1083 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1084 if (!object)
1085 return D3DERR_OUTOFVIDEOMEMORY;
1087 hr = indexbuffer_init(object, device, size, usage, format, pool);
1088 if (FAILED(hr))
1090 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1091 HeapFree(GetProcessHeap(), 0, object);
1092 return hr;
1095 TRACE("Created index buffer %p.\n", object);
1096 *buffer = &object->IDirect3DIndexBuffer9_iface;
1098 return D3D_OK;
1101 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1102 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1103 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1105 struct wined3d_resource_desc desc;
1106 struct d3d9_surface *surface_impl;
1107 struct wined3d_texture *texture;
1108 HRESULT hr;
1110 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1111 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1112 device, width, height, format, flags, surface, usage, pool,
1113 multisample_type, multisample_quality);
1115 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1116 desc.format = wined3dformat_from_d3dformat(format);
1117 desc.multisample_type = multisample_type;
1118 desc.multisample_quality = multisample_quality;
1119 desc.usage = usage & WINED3DUSAGE_MASK;
1120 desc.pool = pool;
1121 desc.width = width;
1122 desc.height = height;
1123 desc.depth = 1;
1124 desc.size = 0;
1126 if (is_gdi_compat_format(format))
1127 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1129 wined3d_mutex_lock();
1131 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1132 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1134 wined3d_mutex_unlock();
1135 WARN("Failed to create texture, hr %#x.\n", hr);
1136 if (hr == WINED3DERR_NOTAVAILABLE)
1137 hr = D3DERR_INVALIDCALL;
1138 return hr;
1141 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1142 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1143 *surface = &surface_impl->IDirect3DSurface9_iface;
1144 IDirect3DSurface9_AddRef(*surface);
1146 if (user_mem)
1147 wined3d_texture_update_desc(texture, width, height,
1148 desc.format, multisample_type, multisample_quality, user_mem, 0);
1150 wined3d_texture_decref(texture);
1152 wined3d_mutex_unlock();
1154 return D3D_OK;
1157 BOOL is_gdi_compat_format(D3DFORMAT format)
1159 switch (format)
1161 case D3DFMT_A8R8G8B8:
1162 case D3DFMT_X8R8G8B8:
1163 case D3DFMT_R5G6B5:
1164 case D3DFMT_X1R5G5B5:
1165 case D3DFMT_A1R5G5B5:
1166 case D3DFMT_R8G8B8:
1167 return TRUE;
1168 default:
1169 return FALSE;
1173 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1174 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1175 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1177 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1178 DWORD flags = 0;
1180 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1181 "lockable %#x, surface %p, shared_handle %p.\n",
1182 iface, width, height, format, multisample_type, multisample_quality,
1183 lockable, surface, shared_handle);
1185 *surface = NULL;
1186 if (shared_handle)
1188 if (!device->d3d_parent->extended)
1190 WARN("Trying to create a shared render target on a non-ex device.\n");
1191 return E_NOTIMPL;
1194 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1197 if (lockable)
1198 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1200 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1201 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1204 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1205 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1206 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1208 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1209 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1211 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1212 "discard %#x, surface %p, shared_handle %p.\n",
1213 iface, width, height, format, multisample_type, multisample_quality,
1214 discard, surface, shared_handle);
1216 *surface = NULL;
1217 if (shared_handle)
1219 if (!device->d3d_parent->extended)
1221 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1222 return E_NOTIMPL;
1225 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1228 if (discard)
1229 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1231 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1232 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1236 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1237 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1238 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1240 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1241 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1242 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1243 struct wined3d_box src_box;
1244 HRESULT hr;
1246 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1247 iface, src_surface, src_rect, dst_surface, dst_point);
1249 if (src_rect)
1251 src_box.left = src_rect->left;
1252 src_box.top = src_rect->top;
1253 src_box.right = src_rect->right;
1254 src_box.bottom = src_rect->bottom;
1255 src_box.front = 0;
1256 src_box.back = 1;
1259 wined3d_mutex_lock();
1260 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1261 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1262 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1263 src->sub_resource_idx, src_rect ? &src_box : NULL);
1264 wined3d_mutex_unlock();
1266 if (FAILED(hr))
1267 return D3DERR_INVALIDCALL;
1269 return hr;
1272 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1273 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1275 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1276 struct d3d9_texture *src_impl, *dst_impl;
1277 HRESULT hr;
1279 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1281 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1282 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1284 wined3d_mutex_lock();
1285 hr = wined3d_device_update_texture(device->wined3d_device,
1286 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1287 wined3d_mutex_unlock();
1289 return hr;
1292 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1293 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1295 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1296 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1297 struct wined3d_sub_resource_desc wined3d_desc;
1298 RECT dst_rect, src_rect;
1299 HRESULT hr;
1301 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1303 if (!render_target || !dst_surface)
1304 return D3DERR_INVALIDCALL;
1306 wined3d_mutex_lock();
1307 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1308 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1310 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1311 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1313 /* TODO: Check surface sizes, pools, etc. */
1314 if (wined3d_desc.multisample_type)
1315 hr = D3DERR_INVALIDCALL;
1316 else
1317 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1318 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1319 wined3d_mutex_unlock();
1321 return hr;
1324 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1325 UINT swapchain, IDirect3DSurface9 *dst_surface)
1327 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1328 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1329 HRESULT hr = D3DERR_INVALIDCALL;
1331 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1333 wined3d_mutex_lock();
1334 if (swapchain < device->implicit_swapchain_count)
1335 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1336 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1337 wined3d_mutex_unlock();
1339 return hr;
1342 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1343 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1345 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1346 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1347 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1348 struct wined3d_sub_resource_desc src_desc, dst_desc;
1349 HRESULT hr = D3DERR_INVALIDCALL;
1350 RECT d, s;
1352 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1353 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1355 wined3d_mutex_lock();
1356 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1357 if (!dst_rect)
1359 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1360 dst_rect = &d;
1363 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1364 if (!src_rect)
1366 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1367 src_rect = &s;
1370 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1372 if (device->in_scene)
1374 WARN("Rejecting depth / stencil blit while in scene.\n");
1375 goto done;
1378 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1379 || src_rect->bottom != src_desc.height)
1381 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1382 wine_dbgstr_rect(src_rect));
1383 goto done;
1386 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1387 || dst_rect->bottom != dst_desc.height)
1389 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1390 wine_dbgstr_rect(dst_rect));
1391 goto done;
1394 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1396 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1397 goto done;
1401 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1402 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1403 if (hr == WINEDDERR_INVALIDRECT)
1404 hr = D3DERR_INVALIDCALL;
1406 done:
1407 wined3d_mutex_unlock();
1408 return hr;
1411 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1412 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1414 const struct wined3d_color c =
1416 ((color >> 16) & 0xff) / 255.0f,
1417 ((color >> 8) & 0xff) / 255.0f,
1418 (color & 0xff) / 255.0f,
1419 ((color >> 24) & 0xff) / 255.0f,
1421 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1422 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1423 struct wined3d_sub_resource_desc desc;
1424 HRESULT hr;
1426 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1428 wined3d_mutex_lock();
1430 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1431 surface_impl->sub_resource_idx, &desc)))
1433 wined3d_mutex_unlock();
1434 return D3DERR_INVALIDCALL;
1437 if (desc.pool != WINED3D_POOL_DEFAULT)
1439 wined3d_mutex_unlock();
1440 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1441 return D3DERR_INVALIDCALL;
1443 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1445 wined3d_mutex_unlock();
1446 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1447 return D3DERR_INVALIDCALL;
1449 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1451 wined3d_mutex_unlock();
1452 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1453 return D3DERR_INVALIDCALL;
1456 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1457 d3d9_surface_get_rendertarget_view(surface_impl), rect,
1458 WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1460 wined3d_mutex_unlock();
1462 return hr;
1465 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1466 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1467 HANDLE *shared_handle)
1469 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1470 void *user_mem = NULL;
1472 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1473 iface, width, height, format, pool, surface, shared_handle);
1475 *surface = NULL;
1476 if (pool == D3DPOOL_MANAGED)
1478 WARN("Attempting to create a managed offscreen plain surface.\n");
1479 return D3DERR_INVALIDCALL;
1482 if (shared_handle)
1484 if (!device->d3d_parent->extended)
1486 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1487 return E_NOTIMPL;
1490 if (pool == D3DPOOL_SYSTEMMEM)
1491 user_mem = *shared_handle;
1492 else
1494 if (pool != D3DPOOL_DEFAULT)
1496 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1497 return D3DERR_INVALIDCALL;
1499 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1503 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1504 * regardless of the pool they're created in. Should we set dynamic usage
1505 * here? */
1506 return d3d9_device_create_surface(device, width, height, format,
1507 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1510 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1512 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1513 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1514 HRESULT hr;
1516 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1518 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1520 WARN("Invalid index %u specified.\n", idx);
1521 return D3DERR_INVALIDCALL;
1524 if (!idx && !surface_impl)
1526 WARN("Trying to set render target 0 to NULL.\n");
1527 return D3DERR_INVALIDCALL;
1530 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1532 WARN("Render target surface does not match device.\n");
1533 return D3DERR_INVALIDCALL;
1536 wined3d_mutex_lock();
1537 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1538 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1539 wined3d_mutex_unlock();
1541 return hr;
1544 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1546 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1547 struct wined3d_rendertarget_view *wined3d_rtv;
1548 struct d3d9_surface *surface_impl;
1549 HRESULT hr = D3D_OK;
1551 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1553 if (!surface)
1554 return D3DERR_INVALIDCALL;
1556 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1558 WARN("Invalid index %u specified.\n", idx);
1559 return D3DERR_INVALIDCALL;
1562 wined3d_mutex_lock();
1563 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1565 /* We want the sub resource parent here, since the view itself may be
1566 * internal to wined3d and may not have a parent. */
1567 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1568 *surface = &surface_impl->IDirect3DSurface9_iface;
1569 IDirect3DSurface9_AddRef(*surface);
1571 else
1573 hr = D3DERR_NOTFOUND;
1574 *surface = NULL;
1576 wined3d_mutex_unlock();
1578 return hr;
1581 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1583 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1584 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1586 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1588 wined3d_mutex_lock();
1589 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1590 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1591 wined3d_mutex_unlock();
1593 return D3D_OK;
1596 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1598 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1599 struct wined3d_rendertarget_view *wined3d_dsv;
1600 struct d3d9_surface *surface_impl;
1601 HRESULT hr = D3D_OK;
1603 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1605 if (!depth_stencil)
1606 return D3DERR_INVALIDCALL;
1608 wined3d_mutex_lock();
1609 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1611 /* We want the sub resource parent here, since the view itself may be
1612 * internal to wined3d and may not have a parent. */
1613 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1614 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1615 IDirect3DSurface9_AddRef(*depth_stencil);
1617 else
1619 hr = D3DERR_NOTFOUND;
1620 *depth_stencil = NULL;
1622 wined3d_mutex_unlock();
1624 return hr;
1627 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1630 HRESULT hr;
1632 TRACE("iface %p.\n", iface);
1634 wined3d_mutex_lock();
1635 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1636 device->in_scene = TRUE;
1637 wined3d_mutex_unlock();
1639 return hr;
1642 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1644 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1645 HRESULT hr;
1647 TRACE("iface %p.\n", iface);
1649 wined3d_mutex_lock();
1650 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1651 device->in_scene = FALSE;
1652 wined3d_mutex_unlock();
1654 return hr;
1657 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1658 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1660 const struct wined3d_color c =
1662 ((color >> 16) & 0xff) / 255.0f,
1663 ((color >> 8) & 0xff) / 255.0f,
1664 (color & 0xff) / 255.0f,
1665 ((color >> 24) & 0xff) / 255.0f,
1667 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1668 HRESULT hr;
1670 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1671 iface, rect_count, rects, flags, color, z, stencil);
1673 if (rect_count && !rects)
1675 WARN("count %u with NULL rects.\n", rect_count);
1676 rect_count = 0;
1679 wined3d_mutex_lock();
1680 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1681 wined3d_mutex_unlock();
1683 return hr;
1686 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1687 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1689 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1691 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1693 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1694 wined3d_mutex_lock();
1695 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1696 wined3d_mutex_unlock();
1698 return D3D_OK;
1701 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1702 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1704 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1706 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1708 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1709 wined3d_mutex_lock();
1710 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1711 wined3d_mutex_unlock();
1713 return D3D_OK;
1716 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1717 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1719 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1721 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1723 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1724 wined3d_mutex_lock();
1725 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1726 wined3d_mutex_unlock();
1728 return D3D_OK;
1731 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1733 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1735 TRACE("iface %p, viewport %p.\n", iface, viewport);
1737 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1738 wined3d_mutex_lock();
1739 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1740 wined3d_mutex_unlock();
1742 return D3D_OK;
1745 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1747 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1749 TRACE("iface %p, viewport %p.\n", iface, viewport);
1751 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1752 wined3d_mutex_lock();
1753 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1754 wined3d_mutex_unlock();
1756 return D3D_OK;
1759 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1761 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1763 TRACE("iface %p, material %p.\n", iface, material);
1765 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1766 wined3d_mutex_lock();
1767 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1768 wined3d_mutex_unlock();
1770 return D3D_OK;
1773 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1775 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1777 TRACE("iface %p, material %p.\n", iface, material);
1779 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1780 wined3d_mutex_lock();
1781 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1782 wined3d_mutex_unlock();
1784 return D3D_OK;
1787 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1789 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1790 HRESULT hr;
1792 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1794 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1795 wined3d_mutex_lock();
1796 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1797 wined3d_mutex_unlock();
1799 return hr;
1802 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1804 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1805 HRESULT hr;
1807 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1809 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1810 wined3d_mutex_lock();
1811 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1812 wined3d_mutex_unlock();
1814 return hr;
1817 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1819 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1820 HRESULT hr;
1822 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1824 wined3d_mutex_lock();
1825 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1826 wined3d_mutex_unlock();
1828 return hr;
1831 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1833 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1834 HRESULT hr;
1836 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1838 wined3d_mutex_lock();
1839 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1840 wined3d_mutex_unlock();
1842 return hr;
1845 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1847 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1848 HRESULT hr;
1850 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1852 wined3d_mutex_lock();
1853 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1854 wined3d_mutex_unlock();
1856 return hr;
1859 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1861 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1862 HRESULT hr;
1864 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1866 wined3d_mutex_lock();
1867 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1868 wined3d_mutex_unlock();
1870 return hr;
1873 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1874 D3DRENDERSTATETYPE state, DWORD value)
1876 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1878 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1880 wined3d_mutex_lock();
1881 wined3d_device_set_render_state(device->wined3d_device, state, value);
1882 wined3d_mutex_unlock();
1884 return D3D_OK;
1887 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1888 D3DRENDERSTATETYPE state, DWORD *value)
1890 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1892 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1894 wined3d_mutex_lock();
1895 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1896 wined3d_mutex_unlock();
1898 return D3D_OK;
1901 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1902 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1904 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1905 struct d3d9_stateblock *object;
1906 HRESULT hr;
1908 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1910 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1912 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1913 return D3DERR_INVALIDCALL;
1916 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1917 if (!object)
1918 return E_OUTOFMEMORY;
1920 hr = stateblock_init(object, device, type, NULL);
1921 if (FAILED(hr))
1923 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1924 HeapFree(GetProcessHeap(), 0, object);
1925 return hr;
1928 TRACE("Created stateblock %p.\n", object);
1929 *stateblock = &object->IDirect3DStateBlock9_iface;
1931 return D3D_OK;
1934 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1936 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1937 HRESULT hr;
1939 TRACE("iface %p.\n", iface);
1941 wined3d_mutex_lock();
1942 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1943 wined3d_mutex_unlock();
1945 return hr;
1948 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1950 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1951 struct wined3d_stateblock *wined3d_stateblock;
1952 struct d3d9_stateblock *object;
1953 HRESULT hr;
1955 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1957 wined3d_mutex_lock();
1958 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1959 wined3d_mutex_unlock();
1960 if (FAILED(hr))
1962 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1963 return hr;
1966 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1967 if (!object)
1969 wined3d_mutex_lock();
1970 wined3d_stateblock_decref(wined3d_stateblock);
1971 wined3d_mutex_unlock();
1972 return E_OUTOFMEMORY;
1975 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1976 if (FAILED(hr))
1978 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1979 wined3d_mutex_lock();
1980 wined3d_stateblock_decref(wined3d_stateblock);
1981 wined3d_mutex_unlock();
1982 HeapFree(GetProcessHeap(), 0, object);
1983 return hr;
1986 TRACE("Created stateblock %p.\n", object);
1987 *stateblock = &object->IDirect3DStateBlock9_iface;
1989 return D3D_OK;
1992 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1994 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1995 HRESULT hr;
1997 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1999 wined3d_mutex_lock();
2000 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2001 wined3d_mutex_unlock();
2003 return hr;
2006 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2008 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2009 HRESULT hr;
2011 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2013 wined3d_mutex_lock();
2014 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2015 wined3d_mutex_unlock();
2017 return hr;
2020 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2022 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2023 struct wined3d_texture *wined3d_texture = NULL;
2024 struct d3d9_texture *texture_impl;
2026 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2028 if (!texture)
2029 return D3DERR_INVALIDCALL;
2031 wined3d_mutex_lock();
2032 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2034 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2035 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2036 IDirect3DBaseTexture9_AddRef(*texture);
2038 else
2040 *texture = NULL;
2042 wined3d_mutex_unlock();
2044 return D3D_OK;
2047 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2049 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2050 struct d3d9_texture *texture_impl;
2051 HRESULT hr;
2053 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2055 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2057 wined3d_mutex_lock();
2058 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2059 texture_impl ? texture_impl->wined3d_texture : NULL);
2060 wined3d_mutex_unlock();
2062 return hr;
2065 static const enum wined3d_texture_stage_state tss_lookup[] =
2067 WINED3D_TSS_INVALID, /* 0, unused */
2068 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2069 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2070 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2071 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2072 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2073 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2074 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2075 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2076 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2077 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2078 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2079 WINED3D_TSS_INVALID, /* 12, unused */
2080 WINED3D_TSS_INVALID, /* 13, unused */
2081 WINED3D_TSS_INVALID, /* 14, unused */
2082 WINED3D_TSS_INVALID, /* 15, unused */
2083 WINED3D_TSS_INVALID, /* 16, unused */
2084 WINED3D_TSS_INVALID, /* 17, unused */
2085 WINED3D_TSS_INVALID, /* 18, unused */
2086 WINED3D_TSS_INVALID, /* 19, unused */
2087 WINED3D_TSS_INVALID, /* 20, unused */
2088 WINED3D_TSS_INVALID, /* 21, unused */
2089 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2090 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2091 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2092 WINED3D_TSS_INVALID, /* 25, unused */
2093 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2094 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2095 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2096 WINED3D_TSS_INVALID, /* 29, unused */
2097 WINED3D_TSS_INVALID, /* 30, unused */
2098 WINED3D_TSS_INVALID, /* 31, unused */
2099 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2102 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2103 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2105 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2107 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2109 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2111 WARN("Invalid state %#x passed.\n", state);
2112 return D3D_OK;
2115 wined3d_mutex_lock();
2116 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2117 wined3d_mutex_unlock();
2119 return D3D_OK;
2122 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2123 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2125 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2127 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2129 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2131 WARN("Invalid state %#x passed.\n", state);
2132 return D3D_OK;
2135 wined3d_mutex_lock();
2136 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2137 wined3d_mutex_unlock();
2139 return D3D_OK;
2142 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2143 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2145 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2147 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2149 wined3d_mutex_lock();
2150 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2151 wined3d_mutex_unlock();
2153 return D3D_OK;
2156 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2157 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2159 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2161 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2163 wined3d_mutex_lock();
2164 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2165 wined3d_mutex_unlock();
2167 return D3D_OK;
2170 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2172 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2173 HRESULT hr;
2175 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2177 wined3d_mutex_lock();
2178 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2179 wined3d_mutex_unlock();
2181 return hr;
2184 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2185 UINT palette_idx, const PALETTEENTRY *entries)
2187 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2189 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2190 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2191 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2193 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2194 return D3D_OK;
2197 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2198 UINT palette_idx, PALETTEENTRY *entries)
2200 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2202 return D3DERR_INVALIDCALL;
2205 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2207 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2209 return D3D_OK;
2212 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2214 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2216 return D3DERR_INVALIDCALL;
2219 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2221 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2223 TRACE("iface %p, rect %p.\n", iface, rect);
2225 wined3d_mutex_lock();
2226 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2227 wined3d_mutex_unlock();
2229 return D3D_OK;
2232 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2234 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2236 TRACE("iface %p, rect %p.\n", iface, rect);
2238 wined3d_mutex_lock();
2239 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2240 wined3d_mutex_unlock();
2242 return D3D_OK;
2245 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2247 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2249 TRACE("iface %p, software %#x.\n", iface, software);
2251 wined3d_mutex_lock();
2252 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2253 wined3d_mutex_unlock();
2255 return D3D_OK;
2258 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2260 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2261 BOOL ret;
2263 TRACE("iface %p.\n", iface);
2265 wined3d_mutex_lock();
2266 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2267 wined3d_mutex_unlock();
2269 return ret;
2272 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2274 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2275 HRESULT hr;
2277 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2279 wined3d_mutex_lock();
2280 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2281 wined3d_mutex_unlock();
2283 return hr;
2286 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2289 float ret;
2291 TRACE("iface %p.\n", iface);
2293 wined3d_mutex_lock();
2294 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2295 wined3d_mutex_unlock();
2297 return ret;
2300 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2301 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2303 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2304 HRESULT hr;
2306 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2307 iface, primitive_type, start_vertex, primitive_count);
2309 wined3d_mutex_lock();
2310 if (!device->has_vertex_declaration)
2312 wined3d_mutex_unlock();
2313 WARN("Called without a valid vertex declaration set.\n");
2314 return D3DERR_INVALIDCALL;
2316 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2317 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2318 vertex_count_from_primitive_count(primitive_type, primitive_count));
2319 wined3d_mutex_unlock();
2321 return hr;
2324 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2325 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2326 UINT vertex_count, UINT start_idx, UINT primitive_count)
2328 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2329 HRESULT hr;
2331 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2332 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2333 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2334 vertex_count, start_idx, primitive_count);
2336 wined3d_mutex_lock();
2337 if (!device->has_vertex_declaration)
2339 wined3d_mutex_unlock();
2340 WARN("Called without a valid vertex declaration set.\n");
2341 return D3DERR_INVALIDCALL;
2343 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2344 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2345 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2346 vertex_count_from_primitive_count(primitive_type, primitive_count));
2347 wined3d_mutex_unlock();
2349 return hr;
2352 /* The caller is responsible for wined3d locking */
2353 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2355 HRESULT hr;
2357 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2359 UINT size = max(device->vertex_buffer_size * 2, min_size);
2360 struct wined3d_buffer *buffer;
2362 TRACE("Growing vertex buffer to %u bytes.\n", size);
2364 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2365 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2366 if (FAILED(hr))
2368 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2369 return hr;
2372 if (device->vertex_buffer)
2373 wined3d_buffer_decref(device->vertex_buffer);
2375 device->vertex_buffer = buffer;
2376 device->vertex_buffer_size = size;
2377 device->vertex_buffer_pos = 0;
2379 return D3D_OK;
2382 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2383 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2385 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2386 HRESULT hr;
2387 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2388 struct wined3d_map_desc wined3d_map_desc;
2389 struct wined3d_box wined3d_box = {0};
2390 UINT size = vtx_count * stride;
2391 struct wined3d_resource *vb;
2392 UINT vb_pos, align;
2394 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2395 iface, primitive_type, primitive_count, data, stride);
2397 if (!primitive_count)
2399 WARN("primitive_count is 0, returning D3D_OK\n");
2400 return D3D_OK;
2403 wined3d_mutex_lock();
2405 if (!device->has_vertex_declaration)
2407 wined3d_mutex_unlock();
2408 WARN("Called without a valid vertex declaration set.\n");
2409 return D3DERR_INVALIDCALL;
2412 hr = d3d9_device_prepare_vertex_buffer(device, size);
2413 if (FAILED(hr))
2414 goto done;
2416 vb_pos = device->vertex_buffer_pos;
2417 align = vb_pos % stride;
2418 if (align) align = stride - align;
2419 if (vb_pos + size + align > device->vertex_buffer_size)
2420 vb_pos = 0;
2421 else
2422 vb_pos += align;
2424 wined3d_box.left = vb_pos;
2425 wined3d_box.right = vb_pos + size;
2426 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2427 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2428 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2429 goto done;
2430 memcpy(wined3d_map_desc.data, data, size);
2431 wined3d_resource_unmap(vb, 0);
2432 device->vertex_buffer_pos = vb_pos + size;
2434 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2435 if (FAILED(hr))
2436 goto done;
2438 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2439 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2440 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2442 done:
2443 wined3d_mutex_unlock();
2444 return hr;
2447 /* The caller is responsible for wined3d locking */
2448 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2450 HRESULT hr;
2452 if (device->index_buffer_size < min_size || !device->index_buffer)
2454 UINT size = max(device->index_buffer_size * 2, min_size);
2455 struct wined3d_buffer *buffer;
2457 TRACE("Growing index buffer to %u bytes.\n", size);
2459 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2460 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2461 if (FAILED(hr))
2463 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2464 return hr;
2467 if (device->index_buffer)
2468 wined3d_buffer_decref(device->index_buffer);
2470 device->index_buffer = buffer;
2471 device->index_buffer_size = size;
2472 device->index_buffer_pos = 0;
2474 return D3D_OK;
2477 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2478 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2479 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2480 const void *vertex_data, UINT vertex_stride)
2482 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2483 HRESULT hr;
2484 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2485 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2486 UINT idx_size = idx_count * idx_fmt_size;
2487 struct wined3d_map_desc wined3d_map_desc;
2488 struct wined3d_box wined3d_box = {0};
2489 struct wined3d_resource *ib, *vb;
2490 UINT ib_pos;
2492 UINT vtx_size = vertex_count * vertex_stride;
2493 UINT vb_pos, align;
2495 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2496 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2497 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2498 index_data, index_format, vertex_data, vertex_stride);
2500 if (!primitive_count)
2502 WARN("primitive_count is 0, returning D3D_OK.\n");
2503 return D3D_OK;
2506 wined3d_mutex_lock();
2508 if (!device->has_vertex_declaration)
2510 wined3d_mutex_unlock();
2511 WARN("Called without a valid vertex declaration set.\n");
2512 return D3DERR_INVALIDCALL;
2515 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2516 if (FAILED(hr))
2517 goto done;
2519 vb_pos = device->vertex_buffer_pos;
2520 align = vb_pos % vertex_stride;
2521 if (align) align = vertex_stride - align;
2522 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2523 vb_pos = 0;
2524 else
2525 vb_pos += align;
2527 wined3d_box.left = vb_pos;
2528 wined3d_box.right = vb_pos + vtx_size;
2529 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2530 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2531 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2532 goto done;
2533 memcpy(wined3d_map_desc.data, vertex_data, vtx_size);
2534 wined3d_resource_unmap(vb, 0);
2535 device->vertex_buffer_pos = vb_pos + vtx_size;
2537 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2538 if (FAILED(hr))
2539 goto done;
2541 ib_pos = device->index_buffer_pos;
2542 align = ib_pos % idx_fmt_size;
2543 if (align) align = idx_fmt_size - align;
2544 if (ib_pos + idx_size + align > device->index_buffer_size)
2545 ib_pos = 0;
2546 else
2547 ib_pos += align;
2549 wined3d_box.left = ib_pos;
2550 wined3d_box.right = ib_pos + idx_size;
2551 ib = wined3d_buffer_get_resource(device->index_buffer);
2552 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2553 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2554 goto done;
2555 memcpy(wined3d_map_desc.data, index_data, idx_size);
2556 wined3d_resource_unmap(ib, 0);
2557 device->index_buffer_pos = ib_pos + idx_size;
2559 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2560 if (FAILED(hr))
2561 goto done;
2563 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2564 wined3dformat_from_d3dformat(index_format), 0);
2565 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2567 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2568 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2570 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2571 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2572 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2574 done:
2575 wined3d_mutex_unlock();
2576 return hr;
2579 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2580 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2581 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2583 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2584 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2585 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2586 HRESULT hr;
2588 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2589 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2591 wined3d_mutex_lock();
2592 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2593 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2594 flags, dst_impl->fvf);
2595 wined3d_mutex_unlock();
2597 return hr;
2600 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2601 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2603 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2604 struct d3d9_vertex_declaration *object;
2605 HRESULT hr;
2607 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2609 if (!declaration)
2611 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2612 return D3DERR_INVALIDCALL;
2615 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2616 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2618 return hr;
2621 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2622 IDirect3DVertexDeclaration9 *declaration)
2624 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2625 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2627 TRACE("iface %p, declaration %p.\n", iface, declaration);
2629 wined3d_mutex_lock();
2630 wined3d_device_set_vertex_declaration(device->wined3d_device,
2631 decl_impl ? decl_impl->wined3d_declaration : NULL);
2632 device->has_vertex_declaration = !!decl_impl;
2633 wined3d_mutex_unlock();
2635 return D3D_OK;
2638 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2639 IDirect3DVertexDeclaration9 **declaration)
2641 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2642 struct wined3d_vertex_declaration *wined3d_declaration;
2643 struct d3d9_vertex_declaration *declaration_impl;
2645 TRACE("iface %p, declaration %p.\n", iface, declaration);
2647 if (!declaration) return D3DERR_INVALIDCALL;
2649 wined3d_mutex_lock();
2650 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2652 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2653 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2654 IDirect3DVertexDeclaration9_AddRef(*declaration);
2656 else
2658 *declaration = NULL;
2660 wined3d_mutex_unlock();
2662 TRACE("Returning %p.\n", *declaration);
2663 return D3D_OK;
2666 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2668 struct wined3d_vertex_declaration *wined3d_declaration;
2669 struct fvf_declaration *fvf_decls = device->fvf_decls;
2670 struct d3d9_vertex_declaration *d3d9_declaration;
2671 D3DVERTEXELEMENT9 *elements;
2672 int p, low, high; /* deliberately signed */
2673 HRESULT hr;
2675 TRACE("Searching for declaration for fvf %08x... ", fvf);
2677 low = 0;
2678 high = device->fvf_decl_count - 1;
2679 while (low <= high)
2681 p = (low + high) >> 1;
2682 TRACE("%d ", p);
2684 if (fvf_decls[p].fvf == fvf)
2686 TRACE("found %p.\n", fvf_decls[p].decl);
2687 return fvf_decls[p].decl;
2690 if (fvf_decls[p].fvf < fvf)
2691 low = p + 1;
2692 else
2693 high = p - 1;
2695 TRACE("not found. Creating and inserting at position %d.\n", low);
2697 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2698 return NULL;
2700 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2701 HeapFree(GetProcessHeap(), 0, elements);
2702 if (FAILED(hr))
2703 return NULL;
2705 if (device->fvf_decl_size == device->fvf_decl_count)
2707 UINT grow = max(device->fvf_decl_size / 2, 8);
2709 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2710 if (!fvf_decls)
2712 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2713 return NULL;
2715 device->fvf_decls = fvf_decls;
2716 device->fvf_decl_size += grow;
2719 d3d9_declaration->fvf = fvf;
2720 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2721 wined3d_vertex_declaration_incref(wined3d_declaration);
2722 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2724 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2725 fvf_decls[low].decl = wined3d_declaration;
2726 fvf_decls[low].fvf = fvf;
2727 ++device->fvf_decl_count;
2729 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2731 return wined3d_declaration;
2734 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2736 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2737 struct wined3d_vertex_declaration *decl;
2739 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2741 if (!fvf)
2743 WARN("%#x is not a valid FVF.\n", fvf);
2744 return D3D_OK;
2747 wined3d_mutex_lock();
2748 if (!(decl = device_get_fvf_declaration(device, fvf)))
2750 wined3d_mutex_unlock();
2751 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2752 return D3DERR_DRIVERINTERNALERROR;
2755 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2756 device->has_vertex_declaration = TRUE;
2757 wined3d_mutex_unlock();
2759 return D3D_OK;
2762 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2764 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2765 struct wined3d_vertex_declaration *wined3d_declaration;
2766 struct d3d9_vertex_declaration *d3d9_declaration;
2768 TRACE("iface %p, fvf %p.\n", iface, fvf);
2770 wined3d_mutex_lock();
2771 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2773 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2774 *fvf = d3d9_declaration->fvf;
2776 else
2778 *fvf = 0;
2780 wined3d_mutex_unlock();
2782 TRACE("Returning FVF %#x.\n", *fvf);
2784 return D3D_OK;
2787 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2788 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2790 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2791 struct d3d9_vertexshader *object;
2792 HRESULT hr;
2794 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2796 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2797 if (!object)
2798 return E_OUTOFMEMORY;
2800 hr = vertexshader_init(object, device, byte_code);
2801 if (FAILED(hr))
2803 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2804 HeapFree(GetProcessHeap(), 0, object);
2805 return hr;
2808 TRACE("Created vertex shader %p.\n", object);
2809 *shader = &object->IDirect3DVertexShader9_iface;
2811 return D3D_OK;
2814 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2816 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2817 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2819 TRACE("iface %p, shader %p.\n", iface, shader);
2821 wined3d_mutex_lock();
2822 wined3d_device_set_vertex_shader(device->wined3d_device,
2823 shader_obj ? shader_obj->wined3d_shader : NULL);
2824 wined3d_mutex_unlock();
2826 return D3D_OK;
2829 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2831 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2832 struct d3d9_vertexshader *shader_impl;
2833 struct wined3d_shader *wined3d_shader;
2835 TRACE("iface %p, shader %p.\n", iface, shader);
2837 wined3d_mutex_lock();
2838 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2840 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2841 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2842 IDirect3DVertexShader9_AddRef(*shader);
2844 else
2846 *shader = NULL;
2848 wined3d_mutex_unlock();
2850 TRACE("Returning %p.\n", *shader);
2852 return D3D_OK;
2855 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2856 UINT reg_idx, const float *data, UINT count)
2858 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2859 HRESULT hr;
2861 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2863 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2865 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2866 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2867 return D3DERR_INVALIDCALL;
2870 wined3d_mutex_lock();
2871 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
2872 reg_idx, count, (const struct wined3d_vec4 *)data);
2873 wined3d_mutex_unlock();
2875 return hr;
2878 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2879 UINT reg_idx, float *data, UINT count)
2881 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2882 HRESULT hr;
2884 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2886 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2888 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2889 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2890 return D3DERR_INVALIDCALL;
2893 wined3d_mutex_lock();
2894 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
2895 reg_idx, count, (struct wined3d_vec4 *)data);
2896 wined3d_mutex_unlock();
2898 return hr;
2901 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2902 UINT reg_idx, const int *data, UINT count)
2904 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2905 HRESULT hr;
2907 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2909 wined3d_mutex_lock();
2910 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
2911 reg_idx, count, (const struct wined3d_ivec4 *)data);
2912 wined3d_mutex_unlock();
2914 return hr;
2917 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2918 UINT reg_idx, int *data, UINT count)
2920 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2921 HRESULT hr;
2923 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2925 wined3d_mutex_lock();
2926 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
2927 reg_idx, count, (struct wined3d_ivec4 *)data);
2928 wined3d_mutex_unlock();
2930 return hr;
2933 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2934 UINT reg_idx, const BOOL *data, UINT count)
2936 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2937 HRESULT hr;
2939 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2941 wined3d_mutex_lock();
2942 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2943 wined3d_mutex_unlock();
2945 return hr;
2948 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2949 UINT reg_idx, BOOL *data, UINT count)
2951 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2952 HRESULT hr;
2954 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2956 wined3d_mutex_lock();
2957 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2958 wined3d_mutex_unlock();
2960 return hr;
2963 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2964 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2966 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2967 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2968 HRESULT hr;
2970 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2971 iface, stream_idx, buffer, offset, stride);
2973 wined3d_mutex_lock();
2974 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2975 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2976 wined3d_mutex_unlock();
2978 return hr;
2981 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2982 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2984 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2985 struct d3d9_vertexbuffer *buffer_impl;
2986 struct wined3d_buffer *wined3d_buffer;
2987 HRESULT hr;
2989 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2990 iface, stream_idx, buffer, offset, stride);
2992 if (!buffer)
2993 return D3DERR_INVALIDCALL;
2995 wined3d_mutex_lock();
2996 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2997 if (SUCCEEDED(hr) && wined3d_buffer)
2999 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3000 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3001 IDirect3DVertexBuffer9_AddRef(*buffer);
3003 else
3005 if (FAILED(hr))
3006 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3007 *buffer = NULL;
3009 wined3d_mutex_unlock();
3011 return hr;
3014 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3016 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3017 HRESULT hr;
3019 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3021 wined3d_mutex_lock();
3022 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3023 wined3d_mutex_unlock();
3025 return hr;
3028 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3030 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3031 HRESULT hr;
3033 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3035 wined3d_mutex_lock();
3036 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3037 wined3d_mutex_unlock();
3039 return hr;
3042 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3044 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3045 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3047 TRACE("iface %p, buffer %p.\n", iface, buffer);
3049 wined3d_mutex_lock();
3050 wined3d_device_set_index_buffer(device->wined3d_device,
3051 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3052 wined3d_mutex_unlock();
3054 return D3D_OK;
3057 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3059 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3060 enum wined3d_format_id wined3d_format;
3061 struct wined3d_buffer *wined3d_buffer;
3062 struct d3d9_indexbuffer *buffer_impl;
3064 TRACE("iface %p, buffer %p.\n", iface, buffer);
3066 if (!buffer)
3067 return D3DERR_INVALIDCALL;
3069 wined3d_mutex_lock();
3070 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3072 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3073 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3074 IDirect3DIndexBuffer9_AddRef(*buffer);
3076 else
3078 *buffer = NULL;
3080 wined3d_mutex_unlock();
3082 return D3D_OK;
3085 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3086 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3088 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3089 struct d3d9_pixelshader *object;
3090 HRESULT hr;
3092 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3094 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3095 if (!object)
3097 FIXME("Failed to allocate pixel shader memory.\n");
3098 return E_OUTOFMEMORY;
3101 hr = pixelshader_init(object, device, byte_code);
3102 if (FAILED(hr))
3104 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3105 HeapFree(GetProcessHeap(), 0, object);
3106 return hr;
3109 TRACE("Created pixel shader %p.\n", object);
3110 *shader = &object->IDirect3DPixelShader9_iface;
3112 return D3D_OK;
3115 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3117 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3118 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3120 TRACE("iface %p, shader %p.\n", iface, shader);
3122 wined3d_mutex_lock();
3123 wined3d_device_set_pixel_shader(device->wined3d_device,
3124 shader_obj ? shader_obj->wined3d_shader : NULL);
3125 wined3d_mutex_unlock();
3127 return D3D_OK;
3130 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3132 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3133 struct d3d9_pixelshader *shader_impl;
3134 struct wined3d_shader *wined3d_shader;
3136 TRACE("iface %p, shader %p.\n", iface, shader);
3138 if (!shader) return D3DERR_INVALIDCALL;
3140 wined3d_mutex_lock();
3141 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3143 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3144 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3145 IDirect3DPixelShader9_AddRef(*shader);
3147 else
3149 *shader = NULL;
3151 wined3d_mutex_unlock();
3153 TRACE("Returning %p.\n", *shader);
3155 return D3D_OK;
3158 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3159 UINT reg_idx, const float *data, UINT count)
3161 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3162 HRESULT hr;
3164 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3166 wined3d_mutex_lock();
3167 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3168 reg_idx, count, (const struct wined3d_vec4 *)data);
3169 wined3d_mutex_unlock();
3171 return hr;
3174 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3175 UINT reg_idx, float *data, UINT count)
3177 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3178 HRESULT hr;
3180 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3182 wined3d_mutex_lock();
3183 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3184 reg_idx, count, (struct wined3d_vec4 *)data);
3185 wined3d_mutex_unlock();
3187 return hr;
3190 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3191 UINT reg_idx, const int *data, UINT count)
3193 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3194 HRESULT hr;
3196 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3198 wined3d_mutex_lock();
3199 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3200 reg_idx, count, (const struct wined3d_ivec4 *)data);
3201 wined3d_mutex_unlock();
3203 return hr;
3206 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3207 UINT reg_idx, int *data, UINT count)
3209 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3210 HRESULT hr;
3212 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3214 wined3d_mutex_lock();
3215 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3216 reg_idx, count, (struct wined3d_ivec4 *)data);
3217 wined3d_mutex_unlock();
3219 return hr;
3222 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3223 UINT reg_idx, const BOOL *data, UINT count)
3225 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3226 HRESULT hr;
3228 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3230 wined3d_mutex_lock();
3231 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3232 wined3d_mutex_unlock();
3234 return hr;
3237 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3238 UINT reg_idx, BOOL *data, UINT count)
3240 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3241 HRESULT hr;
3243 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3245 wined3d_mutex_lock();
3246 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3247 wined3d_mutex_unlock();
3249 return hr;
3252 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3253 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3255 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3256 iface, handle, segment_count, patch_info);
3257 return D3D_OK;
3260 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3261 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3263 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3264 iface, handle, segment_count, patch_info);
3265 return D3D_OK;
3268 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3270 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3271 return D3DERR_INVALIDCALL;
3274 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3276 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3277 struct d3d9_query *object;
3278 HRESULT hr;
3280 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3282 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3283 if (!object)
3284 return E_OUTOFMEMORY;
3286 hr = query_init(object, device, type);
3287 if (FAILED(hr))
3289 WARN("Failed to initialize query, hr %#x.\n", hr);
3290 HeapFree(GetProcessHeap(), 0, object);
3291 return hr;
3294 TRACE("Created query %p.\n", object);
3295 if (query) *query = &object->IDirect3DQuery9_iface;
3296 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3298 return D3D_OK;
3301 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3302 UINT width, UINT height, float *rows, float *columns)
3304 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3305 iface, width, height, rows, columns);
3307 return E_NOTIMPL;
3310 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3311 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3312 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3314 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3315 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3316 iface, src_surface, dst_surface, src_descs, rect_count,
3317 dst_descs, operation, offset_x, offset_y);
3319 return E_NOTIMPL;
3322 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3323 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3324 const RGNDATA *dirty_region, DWORD flags)
3326 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3327 UINT i;
3328 HRESULT hr;
3330 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3331 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3332 dst_window_override, dirty_region, flags);
3334 if (device->device_state != D3D9_DEVICE_STATE_OK)
3335 return S_PRESENT_OCCLUDED;
3337 if (dirty_region)
3338 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3340 wined3d_mutex_lock();
3341 for (i = 0; i < device->implicit_swapchain_count; ++i)
3343 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3344 src_rect, dst_rect, dst_window_override, flags)))
3346 wined3d_mutex_unlock();
3347 return hr;
3350 wined3d_mutex_unlock();
3352 return D3D_OK;
3355 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3357 FIXME("iface %p, priority %p stub!\n", iface, priority);
3359 return E_NOTIMPL;
3362 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3364 FIXME("iface %p, priority %d stub!\n", iface, priority);
3366 return E_NOTIMPL;
3369 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3371 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3373 return E_NOTIMPL;
3376 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3377 IDirect3DResource9 **resources, UINT32 resource_count)
3379 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3380 iface, resources, resource_count);
3382 return E_NOTIMPL;
3385 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3387 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3389 return E_NOTIMPL;
3392 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3394 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3396 *max_latency = 2;
3398 return E_NOTIMPL;
3401 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3403 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3404 struct wined3d_swapchain_desc swapchain_desc;
3406 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3408 wined3d_mutex_lock();
3409 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3410 wined3d_mutex_unlock();
3412 if (swapchain_desc.windowed)
3413 return D3D_OK;
3415 /* FIXME: This is actually supposed to check if any other device is in
3416 * fullscreen mode. */
3417 if (dst_window != swapchain_desc.device_window)
3418 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3420 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3423 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3424 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3425 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3427 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3428 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3429 iface, width, height, format, multisample_type, multisample_quality,
3430 lockable, surface, shared_handle, usage);
3432 *surface = NULL;
3433 if (shared_handle)
3434 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3436 return E_NOTIMPL;
3439 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3440 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3441 HANDLE *shared_handle, DWORD usage)
3443 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3444 iface, width, height, format, pool, surface, shared_handle, usage);
3446 return E_NOTIMPL;
3449 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3450 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3451 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3453 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3454 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3456 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3457 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3458 iface, width, height, format, multisample_type, multisample_quality,
3459 discard, surface, shared_handle, usage);
3461 if (usage & D3DUSAGE_DEPTHSTENCIL)
3463 WARN("Invalid usage %#x.\n", usage);
3464 return D3DERR_INVALIDCALL;
3467 if (shared_handle)
3468 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3470 if (discard)
3471 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3473 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3474 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3477 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3478 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3480 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3482 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3484 if (!present_parameters->Windowed == !mode)
3486 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3487 return D3DERR_INVALIDCALL;
3490 if (mode && (mode->Width != present_parameters->BackBufferWidth
3491 || mode->Height != present_parameters->BackBufferHeight))
3493 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3494 mode->Width, mode->Height,
3495 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3496 return D3DERR_INVALIDCALL;
3499 return d3d9_device_reset(device, present_parameters, mode);
3502 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3503 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3505 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3506 struct wined3d_display_mode wined3d_mode;
3507 HRESULT hr;
3509 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3510 iface, swapchain_idx, mode, rotation);
3512 if (mode->Size != sizeof(*mode))
3513 return D3DERR_INVALIDCALL;
3515 wined3d_mutex_lock();
3516 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3517 (enum wined3d_display_rotation *)rotation);
3518 wined3d_mutex_unlock();
3520 if (SUCCEEDED(hr))
3522 mode->Width = wined3d_mode.width;
3523 mode->Height = wined3d_mode.height;
3524 mode->RefreshRate = wined3d_mode.refresh_rate;
3525 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3526 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3529 return hr;
3532 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3534 /* IUnknown */
3535 d3d9_device_QueryInterface,
3536 d3d9_device_AddRef,
3537 d3d9_device_Release,
3538 /* IDirect3DDevice9 */
3539 d3d9_device_TestCooperativeLevel,
3540 d3d9_device_GetAvailableTextureMem,
3541 d3d9_device_EvictManagedResources,
3542 d3d9_device_GetDirect3D,
3543 d3d9_device_GetDeviceCaps,
3544 d3d9_device_GetDisplayMode,
3545 d3d9_device_GetCreationParameters,
3546 d3d9_device_SetCursorProperties,
3547 d3d9_device_SetCursorPosition,
3548 d3d9_device_ShowCursor,
3549 d3d9_device_CreateAdditionalSwapChain,
3550 d3d9_device_GetSwapChain,
3551 d3d9_device_GetNumberOfSwapChains,
3552 d3d9_device_Reset,
3553 d3d9_device_Present,
3554 d3d9_device_GetBackBuffer,
3555 d3d9_device_GetRasterStatus,
3556 d3d9_device_SetDialogBoxMode,
3557 d3d9_device_SetGammaRamp,
3558 d3d9_device_GetGammaRamp,
3559 d3d9_device_CreateTexture,
3560 d3d9_device_CreateVolumeTexture,
3561 d3d9_device_CreateCubeTexture,
3562 d3d9_device_CreateVertexBuffer,
3563 d3d9_device_CreateIndexBuffer,
3564 d3d9_device_CreateRenderTarget,
3565 d3d9_device_CreateDepthStencilSurface,
3566 d3d9_device_UpdateSurface,
3567 d3d9_device_UpdateTexture,
3568 d3d9_device_GetRenderTargetData,
3569 d3d9_device_GetFrontBufferData,
3570 d3d9_device_StretchRect,
3571 d3d9_device_ColorFill,
3572 d3d9_device_CreateOffscreenPlainSurface,
3573 d3d9_device_SetRenderTarget,
3574 d3d9_device_GetRenderTarget,
3575 d3d9_device_SetDepthStencilSurface,
3576 d3d9_device_GetDepthStencilSurface,
3577 d3d9_device_BeginScene,
3578 d3d9_device_EndScene,
3579 d3d9_device_Clear,
3580 d3d9_device_SetTransform,
3581 d3d9_device_GetTransform,
3582 d3d9_device_MultiplyTransform,
3583 d3d9_device_SetViewport,
3584 d3d9_device_GetViewport,
3585 d3d9_device_SetMaterial,
3586 d3d9_device_GetMaterial,
3587 d3d9_device_SetLight,
3588 d3d9_device_GetLight,
3589 d3d9_device_LightEnable,
3590 d3d9_device_GetLightEnable,
3591 d3d9_device_SetClipPlane,
3592 d3d9_device_GetClipPlane,
3593 d3d9_device_SetRenderState,
3594 d3d9_device_GetRenderState,
3595 d3d9_device_CreateStateBlock,
3596 d3d9_device_BeginStateBlock,
3597 d3d9_device_EndStateBlock,
3598 d3d9_device_SetClipStatus,
3599 d3d9_device_GetClipStatus,
3600 d3d9_device_GetTexture,
3601 d3d9_device_SetTexture,
3602 d3d9_device_GetTextureStageState,
3603 d3d9_device_SetTextureStageState,
3604 d3d9_device_GetSamplerState,
3605 d3d9_device_SetSamplerState,
3606 d3d9_device_ValidateDevice,
3607 d3d9_device_SetPaletteEntries,
3608 d3d9_device_GetPaletteEntries,
3609 d3d9_device_SetCurrentTexturePalette,
3610 d3d9_device_GetCurrentTexturePalette,
3611 d3d9_device_SetScissorRect,
3612 d3d9_device_GetScissorRect,
3613 d3d9_device_SetSoftwareVertexProcessing,
3614 d3d9_device_GetSoftwareVertexProcessing,
3615 d3d9_device_SetNPatchMode,
3616 d3d9_device_GetNPatchMode,
3617 d3d9_device_DrawPrimitive,
3618 d3d9_device_DrawIndexedPrimitive,
3619 d3d9_device_DrawPrimitiveUP,
3620 d3d9_device_DrawIndexedPrimitiveUP,
3621 d3d9_device_ProcessVertices,
3622 d3d9_device_CreateVertexDeclaration,
3623 d3d9_device_SetVertexDeclaration,
3624 d3d9_device_GetVertexDeclaration,
3625 d3d9_device_SetFVF,
3626 d3d9_device_GetFVF,
3627 d3d9_device_CreateVertexShader,
3628 d3d9_device_SetVertexShader,
3629 d3d9_device_GetVertexShader,
3630 d3d9_device_SetVertexShaderConstantF,
3631 d3d9_device_GetVertexShaderConstantF,
3632 d3d9_device_SetVertexShaderConstantI,
3633 d3d9_device_GetVertexShaderConstantI,
3634 d3d9_device_SetVertexShaderConstantB,
3635 d3d9_device_GetVertexShaderConstantB,
3636 d3d9_device_SetStreamSource,
3637 d3d9_device_GetStreamSource,
3638 d3d9_device_SetStreamSourceFreq,
3639 d3d9_device_GetStreamSourceFreq,
3640 d3d9_device_SetIndices,
3641 d3d9_device_GetIndices,
3642 d3d9_device_CreatePixelShader,
3643 d3d9_device_SetPixelShader,
3644 d3d9_device_GetPixelShader,
3645 d3d9_device_SetPixelShaderConstantF,
3646 d3d9_device_GetPixelShaderConstantF,
3647 d3d9_device_SetPixelShaderConstantI,
3648 d3d9_device_GetPixelShaderConstantI,
3649 d3d9_device_SetPixelShaderConstantB,
3650 d3d9_device_GetPixelShaderConstantB,
3651 d3d9_device_DrawRectPatch,
3652 d3d9_device_DrawTriPatch,
3653 d3d9_device_DeletePatch,
3654 d3d9_device_CreateQuery,
3655 /* IDirect3DDevice9Ex */
3656 d3d9_device_SetConvolutionMonoKernel,
3657 d3d9_device_ComposeRects,
3658 d3d9_device_PresentEx,
3659 d3d9_device_GetGPUThreadPriority,
3660 d3d9_device_SetGPUThreadPriority,
3661 d3d9_device_WaitForVBlank,
3662 d3d9_device_CheckResourceResidency,
3663 d3d9_device_SetMaximumFrameLatency,
3664 d3d9_device_GetMaximumFrameLatency,
3665 d3d9_device_CheckDeviceState,
3666 d3d9_device_CreateRenderTargetEx,
3667 d3d9_device_CreateOffscreenPlainSurfaceEx,
3668 d3d9_device_CreateDepthStencilSurfaceEx,
3669 d3d9_device_ResetEx,
3670 d3d9_device_GetDisplayModeEx,
3673 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3675 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3678 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3679 struct wined3d_device *device)
3681 TRACE("device_parent %p, device %p.\n", device_parent, device);
3684 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3686 TRACE("device_parent %p.\n", device_parent);
3689 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3691 struct d3d9_device *device = device_from_device_parent(device_parent);
3693 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3695 if (!device->d3d_parent)
3696 return;
3698 if (!activate)
3699 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3700 else if (device->d3d_parent->extended)
3701 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3702 else
3703 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3706 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3707 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3708 void **parent, const struct wined3d_parent_ops **parent_ops)
3710 struct d3d9_surface *d3d_surface;
3712 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3713 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3715 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3716 return E_OUTOFMEMORY;
3718 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3719 *parent = d3d_surface;
3720 TRACE("Created surface %p.\n", d3d_surface);
3722 return D3D_OK;
3725 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3726 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3727 void **parent, const struct wined3d_parent_ops **parent_ops)
3729 struct d3d9_volume *d3d_volume;
3731 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3732 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3734 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3735 return E_OUTOFMEMORY;
3737 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3738 *parent = d3d_volume;
3739 TRACE("Created volume %p.\n", d3d_volume);
3741 return D3D_OK;
3744 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3745 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3747 struct d3d9_device *device = device_from_device_parent(device_parent);
3748 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3749 struct d3d9_surface *d3d_surface;
3750 HRESULT hr;
3752 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3753 device_parent, container_parent, desc, texture);
3755 if (container_parent == device_parent)
3756 container_parent = &device->IDirect3DDevice9Ex_iface;
3758 if (is_gdi_compat_format(d3dformat_from_wined3dformat(desc->format)))
3759 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
3761 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3762 flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3764 WARN("Failed to create texture, hr %#x.\n", hr);
3765 return hr;
3768 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3769 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3771 return hr;
3774 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3775 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3777 struct d3d9_device *device = device_from_device_parent(device_parent);
3778 struct d3d9_swapchain *d3d_swapchain;
3779 HRESULT hr;
3781 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3783 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3784 if (FAILED(hr))
3786 WARN("Failed to create swapchain, hr %#x.\n", hr);
3787 *swapchain = NULL;
3788 return hr;
3791 *swapchain = d3d_swapchain->wined3d_swapchain;
3792 wined3d_swapchain_incref(*swapchain);
3793 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3795 return hr;
3798 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3800 device_parent_wined3d_device_created,
3801 device_parent_mode_changed,
3802 device_parent_activate,
3803 device_parent_surface_created,
3804 device_parent_volume_created,
3805 device_parent_create_swapchain_texture,
3806 device_parent_create_swapchain,
3809 static void setup_fpu(void)
3811 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3812 WORD cw;
3813 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3814 cw = (cw & ~0xf3f) | 0x3f;
3815 __asm__ volatile ("fldcw %0" : : "m" (cw));
3816 #elif defined(__i386__) && defined(_MSC_VER)
3817 WORD cw;
3818 __asm fnstcw cw;
3819 cw = (cw & ~0xf3f) | 0x3f;
3820 __asm fldcw cw;
3821 #else
3822 FIXME("FPU setup not implemented for this platform.\n");
3823 #endif
3826 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3827 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3828 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3830 struct wined3d_swapchain_desc *swapchain_desc;
3831 UINT i, count = 1;
3832 HRESULT hr;
3834 if (mode)
3835 FIXME("Ignoring display mode.\n");
3837 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3838 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3839 device->refcount = 1;
3841 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3843 wined3d_mutex_lock();
3844 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3845 &device->device_parent, &device->wined3d_device);
3846 if (FAILED(hr))
3848 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3849 wined3d_mutex_unlock();
3850 return hr;
3853 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3855 WINED3DCAPS caps;
3857 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3858 count = caps.NumberOfAdaptersInGroup;
3861 if (flags & D3DCREATE_MULTITHREADED)
3862 wined3d_device_set_multithreaded(device->wined3d_device);
3864 if (!parameters->Windowed)
3866 if (!focus_window)
3867 focus_window = parameters->hDeviceWindow;
3868 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3870 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3871 wined3d_device_decref(device->wined3d_device);
3872 wined3d_mutex_unlock();
3873 return hr;
3876 for (i = 0; i < count; ++i)
3878 HWND device_window = parameters[i].hDeviceWindow;
3880 if (!device_window) device_window = focus_window;
3881 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3882 parameters[i].BackBufferWidth,
3883 parameters[i].BackBufferHeight);
3887 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3888 if (!swapchain_desc)
3890 ERR("Failed to allocate wined3d parameters.\n");
3891 wined3d_device_release_focus_window(device->wined3d_device);
3892 wined3d_device_decref(device->wined3d_device);
3893 wined3d_mutex_unlock();
3894 return E_OUTOFMEMORY;
3897 for (i = 0; i < count; ++i)
3899 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3900 parent->extended))
3902 wined3d_device_release_focus_window(device->wined3d_device);
3903 wined3d_device_decref(device->wined3d_device);
3904 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3905 wined3d_mutex_unlock();
3906 return D3DERR_INVALIDCALL;
3910 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3911 if (FAILED(hr))
3913 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3914 wined3d_device_release_focus_window(device->wined3d_device);
3915 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3916 wined3d_device_decref(device->wined3d_device);
3917 wined3d_mutex_unlock();
3918 return hr;
3921 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3923 wined3d_device_uninit_3d(device->wined3d_device);
3924 wined3d_device_release_focus_window(device->wined3d_device);
3925 wined3d_device_decref(device->wined3d_device);
3926 wined3d_mutex_unlock();
3927 return E_OUTOFMEMORY;
3930 for (i = 0; i < count; ++i)
3932 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3935 wined3d_mutex_unlock();
3937 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3939 /* Initialize the converted declaration array. This creates a valid pointer
3940 * and when adding decls HeapReAlloc() can be used without further checking. */
3941 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3942 if (!device->fvf_decls)
3944 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3945 wined3d_mutex_lock();
3946 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3947 wined3d_device_uninit_3d(device->wined3d_device);
3948 wined3d_device_release_focus_window(device->wined3d_device);
3949 wined3d_device_decref(device->wined3d_device);
3950 wined3d_mutex_unlock();
3951 return E_OUTOFMEMORY;
3954 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3955 device->d3d_parent = parent;
3957 return D3D_OK;