d3d9: Fix DrawIndexedPrimitiveUP with non-zero min_vertex_idx.
[wine.git] / dlls / d3d9 / device.c
blobe646cb76c767a2e624fc43401a548cffc0a30aaa
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, "
926 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
927 iface, width, height, depth, levels,
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 struct wined3d_rendertarget_view *rtv;
1425 HRESULT hr;
1427 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1429 wined3d_mutex_lock();
1431 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1432 surface_impl->sub_resource_idx, &desc)))
1434 wined3d_mutex_unlock();
1435 return D3DERR_INVALIDCALL;
1438 if (desc.pool != WINED3D_POOL_DEFAULT)
1440 wined3d_mutex_unlock();
1441 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1442 return D3DERR_INVALIDCALL;
1444 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1446 wined3d_mutex_unlock();
1447 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1448 return D3DERR_INVALIDCALL;
1450 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1452 wined3d_mutex_unlock();
1453 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1454 return D3DERR_INVALIDCALL;
1457 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1458 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1459 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1460 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1462 wined3d_mutex_unlock();
1464 return hr;
1467 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1468 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1469 HANDLE *shared_handle)
1471 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1472 void *user_mem = NULL;
1474 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1475 iface, width, height, format, pool, surface, shared_handle);
1477 *surface = NULL;
1478 if (pool == D3DPOOL_MANAGED)
1480 WARN("Attempting to create a managed offscreen plain surface.\n");
1481 return D3DERR_INVALIDCALL;
1484 if (shared_handle)
1486 if (!device->d3d_parent->extended)
1488 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1489 return E_NOTIMPL;
1492 if (pool == D3DPOOL_SYSTEMMEM)
1493 user_mem = *shared_handle;
1494 else
1496 if (pool != D3DPOOL_DEFAULT)
1498 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1499 return D3DERR_INVALIDCALL;
1501 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1505 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1506 * regardless of the pool they're created in. Should we set dynamic usage
1507 * here? */
1508 return d3d9_device_create_surface(device, width, height, format,
1509 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1512 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1514 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1515 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1516 struct wined3d_rendertarget_view *rtv;
1517 HRESULT hr;
1519 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1521 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1523 WARN("Invalid index %u specified.\n", idx);
1524 return D3DERR_INVALIDCALL;
1527 if (!idx && !surface_impl)
1529 WARN("Trying to set render target 0 to NULL.\n");
1530 return D3DERR_INVALIDCALL;
1533 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1535 WARN("Render target surface does not match device.\n");
1536 return D3DERR_INVALIDCALL;
1539 wined3d_mutex_lock();
1540 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1541 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1542 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1543 wined3d_mutex_unlock();
1545 return hr;
1548 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1550 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1551 struct wined3d_rendertarget_view *wined3d_rtv;
1552 struct d3d9_surface *surface_impl;
1553 HRESULT hr = D3D_OK;
1555 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1557 if (!surface)
1558 return D3DERR_INVALIDCALL;
1560 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1562 WARN("Invalid index %u specified.\n", idx);
1563 return D3DERR_INVALIDCALL;
1566 wined3d_mutex_lock();
1567 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1569 /* We want the sub resource parent here, since the view itself may be
1570 * internal to wined3d and may not have a parent. */
1571 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1572 *surface = &surface_impl->IDirect3DSurface9_iface;
1573 IDirect3DSurface9_AddRef(*surface);
1575 else
1577 hr = D3DERR_NOTFOUND;
1578 *surface = NULL;
1580 wined3d_mutex_unlock();
1582 return hr;
1585 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1587 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1588 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1589 struct wined3d_rendertarget_view *rtv;
1591 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1593 wined3d_mutex_lock();
1594 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1595 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1596 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1597 wined3d_mutex_unlock();
1599 return D3D_OK;
1602 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1604 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1605 struct wined3d_rendertarget_view *wined3d_dsv;
1606 struct d3d9_surface *surface_impl;
1607 HRESULT hr = D3D_OK;
1609 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1611 if (!depth_stencil)
1612 return D3DERR_INVALIDCALL;
1614 wined3d_mutex_lock();
1615 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1617 /* We want the sub resource parent here, since the view itself may be
1618 * internal to wined3d and may not have a parent. */
1619 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1620 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1621 IDirect3DSurface9_AddRef(*depth_stencil);
1623 else
1625 hr = D3DERR_NOTFOUND;
1626 *depth_stencil = NULL;
1628 wined3d_mutex_unlock();
1630 return hr;
1633 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1635 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1636 HRESULT hr;
1638 TRACE("iface %p.\n", iface);
1640 wined3d_mutex_lock();
1641 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1642 device->in_scene = TRUE;
1643 wined3d_mutex_unlock();
1645 return hr;
1648 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1650 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1651 HRESULT hr;
1653 TRACE("iface %p.\n", iface);
1655 wined3d_mutex_lock();
1656 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1657 device->in_scene = FALSE;
1658 wined3d_mutex_unlock();
1660 return hr;
1663 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1664 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1666 const struct wined3d_color c =
1668 ((color >> 16) & 0xff) / 255.0f,
1669 ((color >> 8) & 0xff) / 255.0f,
1670 (color & 0xff) / 255.0f,
1671 ((color >> 24) & 0xff) / 255.0f,
1673 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1674 HRESULT hr;
1676 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1677 iface, rect_count, rects, flags, color, z, stencil);
1679 if (rect_count && !rects)
1681 WARN("count %u with NULL rects.\n", rect_count);
1682 rect_count = 0;
1685 wined3d_mutex_lock();
1686 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1687 wined3d_mutex_unlock();
1689 return hr;
1692 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1693 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1695 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1697 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1699 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1700 wined3d_mutex_lock();
1701 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1702 wined3d_mutex_unlock();
1704 return D3D_OK;
1707 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1708 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1710 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1712 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1714 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1715 wined3d_mutex_lock();
1716 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1717 wined3d_mutex_unlock();
1719 return D3D_OK;
1722 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1723 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1725 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1727 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1729 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1730 wined3d_mutex_lock();
1731 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1732 wined3d_mutex_unlock();
1734 return D3D_OK;
1737 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1739 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1741 TRACE("iface %p, viewport %p.\n", iface, viewport);
1743 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1744 wined3d_mutex_lock();
1745 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1746 wined3d_mutex_unlock();
1748 return D3D_OK;
1751 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1755 TRACE("iface %p, viewport %p.\n", iface, viewport);
1757 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1758 wined3d_mutex_lock();
1759 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1760 wined3d_mutex_unlock();
1762 return D3D_OK;
1765 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1767 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1769 TRACE("iface %p, material %p.\n", iface, material);
1771 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1772 wined3d_mutex_lock();
1773 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1774 wined3d_mutex_unlock();
1776 return D3D_OK;
1779 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1781 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1783 TRACE("iface %p, material %p.\n", iface, material);
1785 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1786 wined3d_mutex_lock();
1787 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1788 wined3d_mutex_unlock();
1790 return D3D_OK;
1793 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1795 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1796 HRESULT hr;
1798 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1800 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1801 wined3d_mutex_lock();
1802 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1803 wined3d_mutex_unlock();
1805 return hr;
1808 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1810 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1811 HRESULT hr;
1813 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1815 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1816 wined3d_mutex_lock();
1817 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1818 wined3d_mutex_unlock();
1820 return hr;
1823 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1825 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1826 HRESULT hr;
1828 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1830 wined3d_mutex_lock();
1831 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1832 wined3d_mutex_unlock();
1834 return hr;
1837 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1839 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1840 HRESULT hr;
1842 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1844 wined3d_mutex_lock();
1845 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1846 wined3d_mutex_unlock();
1848 return hr;
1851 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1853 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1854 HRESULT hr;
1856 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1858 wined3d_mutex_lock();
1859 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1860 wined3d_mutex_unlock();
1862 return hr;
1865 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1867 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1868 HRESULT hr;
1870 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1872 wined3d_mutex_lock();
1873 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1874 wined3d_mutex_unlock();
1876 return hr;
1879 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1880 D3DRENDERSTATETYPE state, DWORD value)
1882 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1884 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1886 wined3d_mutex_lock();
1887 wined3d_device_set_render_state(device->wined3d_device, state, value);
1888 wined3d_mutex_unlock();
1890 return D3D_OK;
1893 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1894 D3DRENDERSTATETYPE state, DWORD *value)
1896 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1898 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1900 wined3d_mutex_lock();
1901 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1902 wined3d_mutex_unlock();
1904 return D3D_OK;
1907 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1908 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1910 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1911 struct d3d9_stateblock *object;
1912 HRESULT hr;
1914 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1916 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1918 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1919 return D3DERR_INVALIDCALL;
1922 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1923 if (!object)
1924 return E_OUTOFMEMORY;
1926 hr = stateblock_init(object, device, type, NULL);
1927 if (FAILED(hr))
1929 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1930 HeapFree(GetProcessHeap(), 0, object);
1931 return hr;
1934 TRACE("Created stateblock %p.\n", object);
1935 *stateblock = &object->IDirect3DStateBlock9_iface;
1937 return D3D_OK;
1940 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1943 HRESULT hr;
1945 TRACE("iface %p.\n", iface);
1947 wined3d_mutex_lock();
1948 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1949 wined3d_mutex_unlock();
1951 return hr;
1954 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1956 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1957 struct wined3d_stateblock *wined3d_stateblock;
1958 struct d3d9_stateblock *object;
1959 HRESULT hr;
1961 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1963 wined3d_mutex_lock();
1964 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1965 wined3d_mutex_unlock();
1966 if (FAILED(hr))
1968 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1969 return hr;
1972 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1973 if (!object)
1975 wined3d_mutex_lock();
1976 wined3d_stateblock_decref(wined3d_stateblock);
1977 wined3d_mutex_unlock();
1978 return E_OUTOFMEMORY;
1981 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1982 if (FAILED(hr))
1984 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1985 wined3d_mutex_lock();
1986 wined3d_stateblock_decref(wined3d_stateblock);
1987 wined3d_mutex_unlock();
1988 HeapFree(GetProcessHeap(), 0, object);
1989 return hr;
1992 TRACE("Created stateblock %p.\n", object);
1993 *stateblock = &object->IDirect3DStateBlock9_iface;
1995 return D3D_OK;
1998 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2000 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2001 HRESULT hr;
2003 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2005 wined3d_mutex_lock();
2006 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2007 wined3d_mutex_unlock();
2009 return hr;
2012 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2014 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2015 HRESULT hr;
2017 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2019 wined3d_mutex_lock();
2020 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2021 wined3d_mutex_unlock();
2023 return hr;
2026 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2028 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2029 struct wined3d_texture *wined3d_texture = NULL;
2030 struct d3d9_texture *texture_impl;
2032 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2034 if (!texture)
2035 return D3DERR_INVALIDCALL;
2037 wined3d_mutex_lock();
2038 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2040 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2041 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2042 IDirect3DBaseTexture9_AddRef(*texture);
2044 else
2046 *texture = NULL;
2048 wined3d_mutex_unlock();
2050 return D3D_OK;
2053 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2055 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2056 struct d3d9_texture *texture_impl;
2057 HRESULT hr;
2059 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2061 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2063 wined3d_mutex_lock();
2064 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2065 texture_impl ? texture_impl->wined3d_texture : NULL);
2066 wined3d_mutex_unlock();
2068 return hr;
2071 static const enum wined3d_texture_stage_state tss_lookup[] =
2073 WINED3D_TSS_INVALID, /* 0, unused */
2074 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2075 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2076 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2077 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2078 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2079 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2080 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2081 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2082 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2083 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2084 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2085 WINED3D_TSS_INVALID, /* 12, unused */
2086 WINED3D_TSS_INVALID, /* 13, unused */
2087 WINED3D_TSS_INVALID, /* 14, unused */
2088 WINED3D_TSS_INVALID, /* 15, unused */
2089 WINED3D_TSS_INVALID, /* 16, unused */
2090 WINED3D_TSS_INVALID, /* 17, unused */
2091 WINED3D_TSS_INVALID, /* 18, unused */
2092 WINED3D_TSS_INVALID, /* 19, unused */
2093 WINED3D_TSS_INVALID, /* 20, unused */
2094 WINED3D_TSS_INVALID, /* 21, unused */
2095 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2096 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2097 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2098 WINED3D_TSS_INVALID, /* 25, unused */
2099 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2100 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2101 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2102 WINED3D_TSS_INVALID, /* 29, unused */
2103 WINED3D_TSS_INVALID, /* 30, unused */
2104 WINED3D_TSS_INVALID, /* 31, unused */
2105 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2108 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2109 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2111 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2113 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2115 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2117 WARN("Invalid state %#x passed.\n", state);
2118 return D3D_OK;
2121 wined3d_mutex_lock();
2122 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2123 wined3d_mutex_unlock();
2125 return D3D_OK;
2128 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2129 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2131 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2133 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2135 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2137 WARN("Invalid state %#x passed.\n", state);
2138 return D3D_OK;
2141 wined3d_mutex_lock();
2142 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2143 wined3d_mutex_unlock();
2145 return D3D_OK;
2148 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2149 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2151 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2153 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2155 wined3d_mutex_lock();
2156 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2157 wined3d_mutex_unlock();
2159 return D3D_OK;
2162 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2163 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2165 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2167 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2169 wined3d_mutex_lock();
2170 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2171 wined3d_mutex_unlock();
2173 return D3D_OK;
2176 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2178 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2179 HRESULT hr;
2181 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2183 wined3d_mutex_lock();
2184 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2185 wined3d_mutex_unlock();
2187 return hr;
2190 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2191 UINT palette_idx, const PALETTEENTRY *entries)
2193 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2195 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2196 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2197 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2199 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2200 return D3D_OK;
2203 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2204 UINT palette_idx, PALETTEENTRY *entries)
2206 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2208 return D3DERR_INVALIDCALL;
2211 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2213 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2215 return D3D_OK;
2218 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2220 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2222 return D3DERR_INVALIDCALL;
2225 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2227 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2229 TRACE("iface %p, rect %p.\n", iface, rect);
2231 wined3d_mutex_lock();
2232 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2233 wined3d_mutex_unlock();
2235 return D3D_OK;
2238 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2240 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2242 TRACE("iface %p, rect %p.\n", iface, rect);
2244 wined3d_mutex_lock();
2245 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2246 wined3d_mutex_unlock();
2248 return D3D_OK;
2251 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2253 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2255 TRACE("iface %p, software %#x.\n", iface, software);
2257 wined3d_mutex_lock();
2258 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2259 wined3d_mutex_unlock();
2261 return D3D_OK;
2264 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2266 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2267 BOOL ret;
2269 TRACE("iface %p.\n", iface);
2271 wined3d_mutex_lock();
2272 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2273 wined3d_mutex_unlock();
2275 return ret;
2278 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2280 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2281 HRESULT hr;
2283 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2285 wined3d_mutex_lock();
2286 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2287 wined3d_mutex_unlock();
2289 return hr;
2292 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2294 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2295 float ret;
2297 TRACE("iface %p.\n", iface);
2299 wined3d_mutex_lock();
2300 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2301 wined3d_mutex_unlock();
2303 return ret;
2306 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2307 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2309 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2310 HRESULT hr;
2312 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2313 iface, primitive_type, start_vertex, primitive_count);
2315 wined3d_mutex_lock();
2316 if (!device->has_vertex_declaration)
2318 wined3d_mutex_unlock();
2319 WARN("Called without a valid vertex declaration set.\n");
2320 return D3DERR_INVALIDCALL;
2322 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2323 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2324 vertex_count_from_primitive_count(primitive_type, primitive_count));
2325 wined3d_mutex_unlock();
2327 return hr;
2330 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2331 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2332 UINT vertex_count, UINT start_idx, UINT primitive_count)
2334 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2335 HRESULT hr;
2337 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2338 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2339 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2340 vertex_count, start_idx, primitive_count);
2342 wined3d_mutex_lock();
2343 if (!device->has_vertex_declaration)
2345 wined3d_mutex_unlock();
2346 WARN("Called without a valid vertex declaration set.\n");
2347 return D3DERR_INVALIDCALL;
2349 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2350 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2351 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2352 vertex_count_from_primitive_count(primitive_type, primitive_count));
2353 wined3d_mutex_unlock();
2355 return hr;
2358 /* The caller is responsible for wined3d locking */
2359 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2361 HRESULT hr;
2363 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2365 UINT size = max(device->vertex_buffer_size * 2, min_size);
2366 struct wined3d_buffer *buffer;
2368 TRACE("Growing vertex buffer to %u bytes.\n", size);
2370 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2371 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2372 if (FAILED(hr))
2374 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2375 return hr;
2378 if (device->vertex_buffer)
2379 wined3d_buffer_decref(device->vertex_buffer);
2381 device->vertex_buffer = buffer;
2382 device->vertex_buffer_size = size;
2383 device->vertex_buffer_pos = 0;
2385 return D3D_OK;
2388 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2389 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2391 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2392 HRESULT hr;
2393 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2394 struct wined3d_map_desc wined3d_map_desc;
2395 struct wined3d_box wined3d_box = {0};
2396 UINT size = vtx_count * stride;
2397 struct wined3d_resource *vb;
2398 UINT vb_pos, align;
2400 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2401 iface, primitive_type, primitive_count, data, stride);
2403 if (!primitive_count)
2405 WARN("primitive_count is 0, returning D3D_OK\n");
2406 return D3D_OK;
2409 wined3d_mutex_lock();
2411 if (!device->has_vertex_declaration)
2413 wined3d_mutex_unlock();
2414 WARN("Called without a valid vertex declaration set.\n");
2415 return D3DERR_INVALIDCALL;
2418 hr = d3d9_device_prepare_vertex_buffer(device, size);
2419 if (FAILED(hr))
2420 goto done;
2422 vb_pos = device->vertex_buffer_pos;
2423 align = vb_pos % stride;
2424 if (align) align = stride - align;
2425 if (vb_pos + size + align > device->vertex_buffer_size)
2426 vb_pos = 0;
2427 else
2428 vb_pos += align;
2430 wined3d_box.left = vb_pos;
2431 wined3d_box.right = vb_pos + size;
2432 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2433 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2434 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2435 goto done;
2436 memcpy(wined3d_map_desc.data, data, size);
2437 wined3d_resource_unmap(vb, 0);
2438 device->vertex_buffer_pos = vb_pos + size;
2440 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2441 if (FAILED(hr))
2442 goto done;
2444 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2445 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2446 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2448 done:
2449 wined3d_mutex_unlock();
2450 return hr;
2453 /* The caller is responsible for wined3d locking */
2454 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2456 HRESULT hr;
2458 if (device->index_buffer_size < min_size || !device->index_buffer)
2460 UINT size = max(device->index_buffer_size * 2, min_size);
2461 struct wined3d_buffer *buffer;
2463 TRACE("Growing index buffer to %u bytes.\n", size);
2465 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2466 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2467 if (FAILED(hr))
2469 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2470 return hr;
2473 if (device->index_buffer)
2474 wined3d_buffer_decref(device->index_buffer);
2476 device->index_buffer = buffer;
2477 device->index_buffer_size = size;
2478 device->index_buffer_pos = 0;
2480 return D3D_OK;
2483 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2484 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2485 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2486 const void *vertex_data, UINT vertex_stride)
2488 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2489 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2490 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2491 UINT vtx_size = vertex_count * vertex_stride;
2492 UINT idx_size = idx_count * idx_fmt_size;
2493 struct wined3d_map_desc wined3d_map_desc;
2494 struct wined3d_box wined3d_box = {0};
2495 struct wined3d_resource *ib, *vb;
2496 UINT vb_pos, ib_pos, align;
2497 HRESULT hr;
2499 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2500 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2501 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2502 index_data, index_format, vertex_data, vertex_stride);
2504 if (!primitive_count)
2506 WARN("primitive_count is 0, returning D3D_OK.\n");
2507 return D3D_OK;
2510 wined3d_mutex_lock();
2512 if (!device->has_vertex_declaration)
2514 wined3d_mutex_unlock();
2515 WARN("Called without a valid vertex declaration set.\n");
2516 return D3DERR_INVALIDCALL;
2519 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2520 if (FAILED(hr))
2521 goto done;
2523 vb_pos = device->vertex_buffer_pos;
2524 align = vb_pos % vertex_stride;
2525 if (align) align = vertex_stride - align;
2526 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2527 vb_pos = 0;
2528 else
2529 vb_pos += align;
2531 wined3d_box.left = vb_pos;
2532 wined3d_box.right = vb_pos + vtx_size;
2533 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2534 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2535 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2536 goto done;
2537 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2538 wined3d_resource_unmap(vb, 0);
2539 device->vertex_buffer_pos = vb_pos + vtx_size;
2541 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2542 if (FAILED(hr))
2543 goto done;
2545 ib_pos = device->index_buffer_pos;
2546 align = ib_pos % idx_fmt_size;
2547 if (align) align = idx_fmt_size - align;
2548 if (ib_pos + idx_size + align > device->index_buffer_size)
2549 ib_pos = 0;
2550 else
2551 ib_pos += align;
2553 wined3d_box.left = ib_pos;
2554 wined3d_box.right = ib_pos + idx_size;
2555 ib = wined3d_buffer_get_resource(device->index_buffer);
2556 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2557 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2558 goto done;
2559 memcpy(wined3d_map_desc.data, index_data, idx_size);
2560 wined3d_resource_unmap(ib, 0);
2561 device->index_buffer_pos = ib_pos + idx_size;
2563 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2564 if (FAILED(hr))
2565 goto done;
2567 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2568 wined3dformat_from_d3dformat(index_format), 0);
2569 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2571 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2572 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2574 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2575 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2576 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2578 done:
2579 wined3d_mutex_unlock();
2580 return hr;
2583 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2584 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2585 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2587 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2588 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2589 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2590 HRESULT hr;
2592 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2593 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2595 wined3d_mutex_lock();
2596 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2597 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2598 flags, dst_impl->fvf);
2599 wined3d_mutex_unlock();
2601 return hr;
2604 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2605 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2607 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2608 struct d3d9_vertex_declaration *object;
2609 HRESULT hr;
2611 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2613 if (!declaration)
2615 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2616 return D3DERR_INVALIDCALL;
2619 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2620 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2622 return hr;
2625 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2626 IDirect3DVertexDeclaration9 *declaration)
2628 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2629 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2631 TRACE("iface %p, declaration %p.\n", iface, declaration);
2633 wined3d_mutex_lock();
2634 wined3d_device_set_vertex_declaration(device->wined3d_device,
2635 decl_impl ? decl_impl->wined3d_declaration : NULL);
2636 device->has_vertex_declaration = !!decl_impl;
2637 wined3d_mutex_unlock();
2639 return D3D_OK;
2642 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2643 IDirect3DVertexDeclaration9 **declaration)
2645 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2646 struct wined3d_vertex_declaration *wined3d_declaration;
2647 struct d3d9_vertex_declaration *declaration_impl;
2649 TRACE("iface %p, declaration %p.\n", iface, declaration);
2651 if (!declaration) return D3DERR_INVALIDCALL;
2653 wined3d_mutex_lock();
2654 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2656 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2657 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2658 IDirect3DVertexDeclaration9_AddRef(*declaration);
2660 else
2662 *declaration = NULL;
2664 wined3d_mutex_unlock();
2666 TRACE("Returning %p.\n", *declaration);
2667 return D3D_OK;
2670 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2672 struct wined3d_vertex_declaration *wined3d_declaration;
2673 struct fvf_declaration *fvf_decls = device->fvf_decls;
2674 struct d3d9_vertex_declaration *d3d9_declaration;
2675 D3DVERTEXELEMENT9 *elements;
2676 int p, low, high; /* deliberately signed */
2677 HRESULT hr;
2679 TRACE("Searching for declaration for fvf %08x... ", fvf);
2681 low = 0;
2682 high = device->fvf_decl_count - 1;
2683 while (low <= high)
2685 p = (low + high) >> 1;
2686 TRACE("%d ", p);
2688 if (fvf_decls[p].fvf == fvf)
2690 TRACE("found %p.\n", fvf_decls[p].decl);
2691 return fvf_decls[p].decl;
2694 if (fvf_decls[p].fvf < fvf)
2695 low = p + 1;
2696 else
2697 high = p - 1;
2699 TRACE("not found. Creating and inserting at position %d.\n", low);
2701 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2702 return NULL;
2704 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2705 HeapFree(GetProcessHeap(), 0, elements);
2706 if (FAILED(hr))
2707 return NULL;
2709 if (device->fvf_decl_size == device->fvf_decl_count)
2711 UINT grow = max(device->fvf_decl_size / 2, 8);
2713 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2714 if (!fvf_decls)
2716 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2717 return NULL;
2719 device->fvf_decls = fvf_decls;
2720 device->fvf_decl_size += grow;
2723 d3d9_declaration->fvf = fvf;
2724 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2725 wined3d_vertex_declaration_incref(wined3d_declaration);
2726 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2728 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2729 fvf_decls[low].decl = wined3d_declaration;
2730 fvf_decls[low].fvf = fvf;
2731 ++device->fvf_decl_count;
2733 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2735 return wined3d_declaration;
2738 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2740 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2741 struct wined3d_vertex_declaration *decl;
2743 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2745 if (!fvf)
2747 WARN("%#x is not a valid FVF.\n", fvf);
2748 return D3D_OK;
2751 wined3d_mutex_lock();
2752 if (!(decl = device_get_fvf_declaration(device, fvf)))
2754 wined3d_mutex_unlock();
2755 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2756 return D3DERR_DRIVERINTERNALERROR;
2759 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2760 device->has_vertex_declaration = TRUE;
2761 wined3d_mutex_unlock();
2763 return D3D_OK;
2766 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2768 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2769 struct wined3d_vertex_declaration *wined3d_declaration;
2770 struct d3d9_vertex_declaration *d3d9_declaration;
2772 TRACE("iface %p, fvf %p.\n", iface, fvf);
2774 wined3d_mutex_lock();
2775 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2777 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2778 *fvf = d3d9_declaration->fvf;
2780 else
2782 *fvf = 0;
2784 wined3d_mutex_unlock();
2786 TRACE("Returning FVF %#x.\n", *fvf);
2788 return D3D_OK;
2791 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2792 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2794 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2795 struct d3d9_vertexshader *object;
2796 HRESULT hr;
2798 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2800 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2801 if (!object)
2802 return E_OUTOFMEMORY;
2804 hr = vertexshader_init(object, device, byte_code);
2805 if (FAILED(hr))
2807 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2808 HeapFree(GetProcessHeap(), 0, object);
2809 return hr;
2812 TRACE("Created vertex shader %p.\n", object);
2813 *shader = &object->IDirect3DVertexShader9_iface;
2815 return D3D_OK;
2818 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2820 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2821 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2823 TRACE("iface %p, shader %p.\n", iface, shader);
2825 wined3d_mutex_lock();
2826 wined3d_device_set_vertex_shader(device->wined3d_device,
2827 shader_obj ? shader_obj->wined3d_shader : NULL);
2828 wined3d_mutex_unlock();
2830 return D3D_OK;
2833 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2835 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2836 struct d3d9_vertexshader *shader_impl;
2837 struct wined3d_shader *wined3d_shader;
2839 TRACE("iface %p, shader %p.\n", iface, shader);
2841 wined3d_mutex_lock();
2842 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2844 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2845 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2846 IDirect3DVertexShader9_AddRef(*shader);
2848 else
2850 *shader = NULL;
2852 wined3d_mutex_unlock();
2854 TRACE("Returning %p.\n", *shader);
2856 return D3D_OK;
2859 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2860 UINT reg_idx, const float *data, UINT count)
2862 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2863 HRESULT hr;
2865 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2867 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2869 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2870 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2871 return D3DERR_INVALIDCALL;
2874 wined3d_mutex_lock();
2875 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
2876 reg_idx, count, (const struct wined3d_vec4 *)data);
2877 wined3d_mutex_unlock();
2879 return hr;
2882 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2883 UINT reg_idx, float *data, UINT count)
2885 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2886 HRESULT hr;
2888 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2890 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2892 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2893 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2894 return D3DERR_INVALIDCALL;
2897 wined3d_mutex_lock();
2898 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
2899 reg_idx, count, (struct wined3d_vec4 *)data);
2900 wined3d_mutex_unlock();
2902 return hr;
2905 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2906 UINT reg_idx, const int *data, UINT count)
2908 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2909 HRESULT hr;
2911 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2913 wined3d_mutex_lock();
2914 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
2915 reg_idx, count, (const struct wined3d_ivec4 *)data);
2916 wined3d_mutex_unlock();
2918 return hr;
2921 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2922 UINT reg_idx, int *data, UINT count)
2924 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2925 HRESULT hr;
2927 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2929 wined3d_mutex_lock();
2930 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
2931 reg_idx, count, (struct wined3d_ivec4 *)data);
2932 wined3d_mutex_unlock();
2934 return hr;
2937 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2938 UINT reg_idx, const BOOL *data, UINT count)
2940 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2941 HRESULT hr;
2943 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2945 wined3d_mutex_lock();
2946 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2947 wined3d_mutex_unlock();
2949 return hr;
2952 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2953 UINT reg_idx, BOOL *data, UINT count)
2955 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2956 HRESULT hr;
2958 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2960 wined3d_mutex_lock();
2961 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2962 wined3d_mutex_unlock();
2964 return hr;
2967 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2968 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2970 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2971 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2972 HRESULT hr;
2974 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2975 iface, stream_idx, buffer, offset, stride);
2977 wined3d_mutex_lock();
2978 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2979 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2980 wined3d_mutex_unlock();
2982 return hr;
2985 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2986 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2988 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2989 struct d3d9_vertexbuffer *buffer_impl;
2990 struct wined3d_buffer *wined3d_buffer;
2991 HRESULT hr;
2993 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2994 iface, stream_idx, buffer, offset, stride);
2996 if (!buffer)
2997 return D3DERR_INVALIDCALL;
2999 wined3d_mutex_lock();
3000 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
3001 if (SUCCEEDED(hr) && wined3d_buffer)
3003 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3004 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3005 IDirect3DVertexBuffer9_AddRef(*buffer);
3007 else
3009 if (FAILED(hr))
3010 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3011 *buffer = NULL;
3013 wined3d_mutex_unlock();
3015 return hr;
3018 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3020 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3021 HRESULT hr;
3023 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3025 wined3d_mutex_lock();
3026 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3027 wined3d_mutex_unlock();
3029 return hr;
3032 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3034 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3035 HRESULT hr;
3037 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3039 wined3d_mutex_lock();
3040 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3041 wined3d_mutex_unlock();
3043 return hr;
3046 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3048 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3049 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3051 TRACE("iface %p, buffer %p.\n", iface, buffer);
3053 wined3d_mutex_lock();
3054 wined3d_device_set_index_buffer(device->wined3d_device,
3055 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3056 wined3d_mutex_unlock();
3058 return D3D_OK;
3061 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3063 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3064 enum wined3d_format_id wined3d_format;
3065 struct wined3d_buffer *wined3d_buffer;
3066 struct d3d9_indexbuffer *buffer_impl;
3068 TRACE("iface %p, buffer %p.\n", iface, buffer);
3070 if (!buffer)
3071 return D3DERR_INVALIDCALL;
3073 wined3d_mutex_lock();
3074 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3076 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3077 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3078 IDirect3DIndexBuffer9_AddRef(*buffer);
3080 else
3082 *buffer = NULL;
3084 wined3d_mutex_unlock();
3086 return D3D_OK;
3089 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3090 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3092 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3093 struct d3d9_pixelshader *object;
3094 HRESULT hr;
3096 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3098 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3099 if (!object)
3101 FIXME("Failed to allocate pixel shader memory.\n");
3102 return E_OUTOFMEMORY;
3105 hr = pixelshader_init(object, device, byte_code);
3106 if (FAILED(hr))
3108 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3109 HeapFree(GetProcessHeap(), 0, object);
3110 return hr;
3113 TRACE("Created pixel shader %p.\n", object);
3114 *shader = &object->IDirect3DPixelShader9_iface;
3116 return D3D_OK;
3119 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3121 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3122 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3124 TRACE("iface %p, shader %p.\n", iface, shader);
3126 wined3d_mutex_lock();
3127 wined3d_device_set_pixel_shader(device->wined3d_device,
3128 shader_obj ? shader_obj->wined3d_shader : NULL);
3129 wined3d_mutex_unlock();
3131 return D3D_OK;
3134 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3136 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3137 struct d3d9_pixelshader *shader_impl;
3138 struct wined3d_shader *wined3d_shader;
3140 TRACE("iface %p, shader %p.\n", iface, shader);
3142 if (!shader) return D3DERR_INVALIDCALL;
3144 wined3d_mutex_lock();
3145 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3147 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3148 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3149 IDirect3DPixelShader9_AddRef(*shader);
3151 else
3153 *shader = NULL;
3155 wined3d_mutex_unlock();
3157 TRACE("Returning %p.\n", *shader);
3159 return D3D_OK;
3162 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3163 UINT reg_idx, const float *data, UINT count)
3165 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3166 HRESULT hr;
3168 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3170 wined3d_mutex_lock();
3171 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3172 reg_idx, count, (const struct wined3d_vec4 *)data);
3173 wined3d_mutex_unlock();
3175 return hr;
3178 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3179 UINT reg_idx, float *data, UINT count)
3181 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3182 HRESULT hr;
3184 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3186 wined3d_mutex_lock();
3187 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3188 reg_idx, count, (struct wined3d_vec4 *)data);
3189 wined3d_mutex_unlock();
3191 return hr;
3194 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3195 UINT reg_idx, const int *data, UINT count)
3197 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3198 HRESULT hr;
3200 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3202 wined3d_mutex_lock();
3203 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3204 reg_idx, count, (const struct wined3d_ivec4 *)data);
3205 wined3d_mutex_unlock();
3207 return hr;
3210 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3211 UINT reg_idx, int *data, UINT count)
3213 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3214 HRESULT hr;
3216 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3218 wined3d_mutex_lock();
3219 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3220 reg_idx, count, (struct wined3d_ivec4 *)data);
3221 wined3d_mutex_unlock();
3223 return hr;
3226 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3227 UINT reg_idx, const BOOL *data, UINT count)
3229 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3230 HRESULT hr;
3232 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3234 wined3d_mutex_lock();
3235 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3236 wined3d_mutex_unlock();
3238 return hr;
3241 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3242 UINT reg_idx, BOOL *data, UINT count)
3244 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3245 HRESULT hr;
3247 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3249 wined3d_mutex_lock();
3250 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3251 wined3d_mutex_unlock();
3253 return hr;
3256 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3257 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3259 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3260 iface, handle, segment_count, patch_info);
3261 return D3D_OK;
3264 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3265 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3267 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3268 iface, handle, segment_count, patch_info);
3269 return D3D_OK;
3272 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3274 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3275 return D3DERR_INVALIDCALL;
3278 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3280 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3281 struct d3d9_query *object;
3282 HRESULT hr;
3284 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3286 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3287 if (!object)
3288 return E_OUTOFMEMORY;
3290 hr = query_init(object, device, type);
3291 if (FAILED(hr))
3293 WARN("Failed to initialize query, hr %#x.\n", hr);
3294 HeapFree(GetProcessHeap(), 0, object);
3295 return hr;
3298 TRACE("Created query %p.\n", object);
3299 if (query) *query = &object->IDirect3DQuery9_iface;
3300 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3302 return D3D_OK;
3305 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3306 UINT width, UINT height, float *rows, float *columns)
3308 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3309 iface, width, height, rows, columns);
3311 return E_NOTIMPL;
3314 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3315 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3316 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3318 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3319 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3320 iface, src_surface, dst_surface, src_descs, rect_count,
3321 dst_descs, operation, offset_x, offset_y);
3323 return E_NOTIMPL;
3326 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3327 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3328 const RGNDATA *dirty_region, DWORD flags)
3330 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3331 UINT i;
3332 HRESULT hr;
3334 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3335 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3336 dst_window_override, dirty_region, flags);
3338 if (device->device_state != D3D9_DEVICE_STATE_OK)
3339 return S_PRESENT_OCCLUDED;
3341 if (dirty_region)
3342 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3344 wined3d_mutex_lock();
3345 for (i = 0; i < device->implicit_swapchain_count; ++i)
3347 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3348 src_rect, dst_rect, dst_window_override, flags)))
3350 wined3d_mutex_unlock();
3351 return hr;
3354 wined3d_mutex_unlock();
3356 return D3D_OK;
3359 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3361 FIXME("iface %p, priority %p stub!\n", iface, priority);
3363 return E_NOTIMPL;
3366 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3368 FIXME("iface %p, priority %d stub!\n", iface, priority);
3370 return E_NOTIMPL;
3373 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3375 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3377 return E_NOTIMPL;
3380 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3381 IDirect3DResource9 **resources, UINT32 resource_count)
3383 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3384 iface, resources, resource_count);
3386 return E_NOTIMPL;
3389 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3391 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3393 return E_NOTIMPL;
3396 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3398 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3400 *max_latency = 2;
3402 return E_NOTIMPL;
3405 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3407 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3408 struct wined3d_swapchain_desc swapchain_desc;
3410 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3412 wined3d_mutex_lock();
3413 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3414 wined3d_mutex_unlock();
3416 if (swapchain_desc.windowed)
3417 return D3D_OK;
3419 /* FIXME: This is actually supposed to check if any other device is in
3420 * fullscreen mode. */
3421 if (dst_window != swapchain_desc.device_window)
3422 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3424 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3427 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3428 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3429 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3431 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3432 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3433 iface, width, height, format, multisample_type, multisample_quality,
3434 lockable, surface, shared_handle, usage);
3436 *surface = NULL;
3437 if (shared_handle)
3438 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3440 return E_NOTIMPL;
3443 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3444 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3445 HANDLE *shared_handle, DWORD usage)
3447 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3448 iface, width, height, format, pool, surface, shared_handle, usage);
3450 return E_NOTIMPL;
3453 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3454 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3455 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3457 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3458 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3460 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3461 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3462 iface, width, height, format, multisample_type, multisample_quality,
3463 discard, surface, shared_handle, usage);
3465 if (usage & D3DUSAGE_DEPTHSTENCIL)
3467 WARN("Invalid usage %#x.\n", usage);
3468 return D3DERR_INVALIDCALL;
3471 if (shared_handle)
3472 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3474 if (discard)
3475 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3477 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3478 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3481 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3482 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3484 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3486 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3488 if (!present_parameters->Windowed == !mode)
3490 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3491 return D3DERR_INVALIDCALL;
3494 if (mode && (mode->Width != present_parameters->BackBufferWidth
3495 || mode->Height != present_parameters->BackBufferHeight))
3497 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3498 mode->Width, mode->Height,
3499 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3500 return D3DERR_INVALIDCALL;
3503 return d3d9_device_reset(device, present_parameters, mode);
3506 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3507 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3509 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3510 struct wined3d_display_mode wined3d_mode;
3511 HRESULT hr;
3513 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3514 iface, swapchain_idx, mode, rotation);
3516 if (mode->Size != sizeof(*mode))
3517 return D3DERR_INVALIDCALL;
3519 wined3d_mutex_lock();
3520 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3521 (enum wined3d_display_rotation *)rotation);
3522 wined3d_mutex_unlock();
3524 if (SUCCEEDED(hr))
3526 mode->Width = wined3d_mode.width;
3527 mode->Height = wined3d_mode.height;
3528 mode->RefreshRate = wined3d_mode.refresh_rate;
3529 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3530 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3533 return hr;
3536 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3538 /* IUnknown */
3539 d3d9_device_QueryInterface,
3540 d3d9_device_AddRef,
3541 d3d9_device_Release,
3542 /* IDirect3DDevice9 */
3543 d3d9_device_TestCooperativeLevel,
3544 d3d9_device_GetAvailableTextureMem,
3545 d3d9_device_EvictManagedResources,
3546 d3d9_device_GetDirect3D,
3547 d3d9_device_GetDeviceCaps,
3548 d3d9_device_GetDisplayMode,
3549 d3d9_device_GetCreationParameters,
3550 d3d9_device_SetCursorProperties,
3551 d3d9_device_SetCursorPosition,
3552 d3d9_device_ShowCursor,
3553 d3d9_device_CreateAdditionalSwapChain,
3554 d3d9_device_GetSwapChain,
3555 d3d9_device_GetNumberOfSwapChains,
3556 d3d9_device_Reset,
3557 d3d9_device_Present,
3558 d3d9_device_GetBackBuffer,
3559 d3d9_device_GetRasterStatus,
3560 d3d9_device_SetDialogBoxMode,
3561 d3d9_device_SetGammaRamp,
3562 d3d9_device_GetGammaRamp,
3563 d3d9_device_CreateTexture,
3564 d3d9_device_CreateVolumeTexture,
3565 d3d9_device_CreateCubeTexture,
3566 d3d9_device_CreateVertexBuffer,
3567 d3d9_device_CreateIndexBuffer,
3568 d3d9_device_CreateRenderTarget,
3569 d3d9_device_CreateDepthStencilSurface,
3570 d3d9_device_UpdateSurface,
3571 d3d9_device_UpdateTexture,
3572 d3d9_device_GetRenderTargetData,
3573 d3d9_device_GetFrontBufferData,
3574 d3d9_device_StretchRect,
3575 d3d9_device_ColorFill,
3576 d3d9_device_CreateOffscreenPlainSurface,
3577 d3d9_device_SetRenderTarget,
3578 d3d9_device_GetRenderTarget,
3579 d3d9_device_SetDepthStencilSurface,
3580 d3d9_device_GetDepthStencilSurface,
3581 d3d9_device_BeginScene,
3582 d3d9_device_EndScene,
3583 d3d9_device_Clear,
3584 d3d9_device_SetTransform,
3585 d3d9_device_GetTransform,
3586 d3d9_device_MultiplyTransform,
3587 d3d9_device_SetViewport,
3588 d3d9_device_GetViewport,
3589 d3d9_device_SetMaterial,
3590 d3d9_device_GetMaterial,
3591 d3d9_device_SetLight,
3592 d3d9_device_GetLight,
3593 d3d9_device_LightEnable,
3594 d3d9_device_GetLightEnable,
3595 d3d9_device_SetClipPlane,
3596 d3d9_device_GetClipPlane,
3597 d3d9_device_SetRenderState,
3598 d3d9_device_GetRenderState,
3599 d3d9_device_CreateStateBlock,
3600 d3d9_device_BeginStateBlock,
3601 d3d9_device_EndStateBlock,
3602 d3d9_device_SetClipStatus,
3603 d3d9_device_GetClipStatus,
3604 d3d9_device_GetTexture,
3605 d3d9_device_SetTexture,
3606 d3d9_device_GetTextureStageState,
3607 d3d9_device_SetTextureStageState,
3608 d3d9_device_GetSamplerState,
3609 d3d9_device_SetSamplerState,
3610 d3d9_device_ValidateDevice,
3611 d3d9_device_SetPaletteEntries,
3612 d3d9_device_GetPaletteEntries,
3613 d3d9_device_SetCurrentTexturePalette,
3614 d3d9_device_GetCurrentTexturePalette,
3615 d3d9_device_SetScissorRect,
3616 d3d9_device_GetScissorRect,
3617 d3d9_device_SetSoftwareVertexProcessing,
3618 d3d9_device_GetSoftwareVertexProcessing,
3619 d3d9_device_SetNPatchMode,
3620 d3d9_device_GetNPatchMode,
3621 d3d9_device_DrawPrimitive,
3622 d3d9_device_DrawIndexedPrimitive,
3623 d3d9_device_DrawPrimitiveUP,
3624 d3d9_device_DrawIndexedPrimitiveUP,
3625 d3d9_device_ProcessVertices,
3626 d3d9_device_CreateVertexDeclaration,
3627 d3d9_device_SetVertexDeclaration,
3628 d3d9_device_GetVertexDeclaration,
3629 d3d9_device_SetFVF,
3630 d3d9_device_GetFVF,
3631 d3d9_device_CreateVertexShader,
3632 d3d9_device_SetVertexShader,
3633 d3d9_device_GetVertexShader,
3634 d3d9_device_SetVertexShaderConstantF,
3635 d3d9_device_GetVertexShaderConstantF,
3636 d3d9_device_SetVertexShaderConstantI,
3637 d3d9_device_GetVertexShaderConstantI,
3638 d3d9_device_SetVertexShaderConstantB,
3639 d3d9_device_GetVertexShaderConstantB,
3640 d3d9_device_SetStreamSource,
3641 d3d9_device_GetStreamSource,
3642 d3d9_device_SetStreamSourceFreq,
3643 d3d9_device_GetStreamSourceFreq,
3644 d3d9_device_SetIndices,
3645 d3d9_device_GetIndices,
3646 d3d9_device_CreatePixelShader,
3647 d3d9_device_SetPixelShader,
3648 d3d9_device_GetPixelShader,
3649 d3d9_device_SetPixelShaderConstantF,
3650 d3d9_device_GetPixelShaderConstantF,
3651 d3d9_device_SetPixelShaderConstantI,
3652 d3d9_device_GetPixelShaderConstantI,
3653 d3d9_device_SetPixelShaderConstantB,
3654 d3d9_device_GetPixelShaderConstantB,
3655 d3d9_device_DrawRectPatch,
3656 d3d9_device_DrawTriPatch,
3657 d3d9_device_DeletePatch,
3658 d3d9_device_CreateQuery,
3659 /* IDirect3DDevice9Ex */
3660 d3d9_device_SetConvolutionMonoKernel,
3661 d3d9_device_ComposeRects,
3662 d3d9_device_PresentEx,
3663 d3d9_device_GetGPUThreadPriority,
3664 d3d9_device_SetGPUThreadPriority,
3665 d3d9_device_WaitForVBlank,
3666 d3d9_device_CheckResourceResidency,
3667 d3d9_device_SetMaximumFrameLatency,
3668 d3d9_device_GetMaximumFrameLatency,
3669 d3d9_device_CheckDeviceState,
3670 d3d9_device_CreateRenderTargetEx,
3671 d3d9_device_CreateOffscreenPlainSurfaceEx,
3672 d3d9_device_CreateDepthStencilSurfaceEx,
3673 d3d9_device_ResetEx,
3674 d3d9_device_GetDisplayModeEx,
3677 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3679 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3682 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3683 struct wined3d_device *device)
3685 TRACE("device_parent %p, device %p.\n", device_parent, device);
3688 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3690 TRACE("device_parent %p.\n", device_parent);
3693 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3695 struct d3d9_device *device = device_from_device_parent(device_parent);
3697 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3699 if (!device->d3d_parent)
3700 return;
3702 if (!activate)
3703 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3704 else if (device->d3d_parent->extended)
3705 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3706 else
3707 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3710 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3711 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3712 void **parent, const struct wined3d_parent_ops **parent_ops)
3714 struct d3d9_surface *d3d_surface;
3716 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3717 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3719 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3720 return E_OUTOFMEMORY;
3722 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3723 *parent = d3d_surface;
3724 TRACE("Created surface %p.\n", d3d_surface);
3726 return D3D_OK;
3729 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3730 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3731 void **parent, const struct wined3d_parent_ops **parent_ops)
3733 struct d3d9_volume *d3d_volume;
3735 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3736 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3738 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3739 return E_OUTOFMEMORY;
3741 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3742 *parent = d3d_volume;
3743 TRACE("Created volume %p.\n", d3d_volume);
3745 return D3D_OK;
3748 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3749 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3751 struct d3d9_device *device = device_from_device_parent(device_parent);
3752 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3753 struct d3d9_surface *d3d_surface;
3754 HRESULT hr;
3756 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3757 device_parent, container_parent, desc, texture);
3759 if (container_parent == device_parent)
3760 container_parent = &device->IDirect3DDevice9Ex_iface;
3762 if (is_gdi_compat_format(d3dformat_from_wined3dformat(desc->format)))
3763 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
3765 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3766 flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3768 WARN("Failed to create texture, hr %#x.\n", hr);
3769 return hr;
3772 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3773 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3775 return hr;
3778 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3779 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3781 struct d3d9_device *device = device_from_device_parent(device_parent);
3782 struct d3d9_swapchain *d3d_swapchain;
3783 HRESULT hr;
3785 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3787 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3788 if (FAILED(hr))
3790 WARN("Failed to create swapchain, hr %#x.\n", hr);
3791 *swapchain = NULL;
3792 return hr;
3795 *swapchain = d3d_swapchain->wined3d_swapchain;
3796 wined3d_swapchain_incref(*swapchain);
3797 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3799 return hr;
3802 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3804 device_parent_wined3d_device_created,
3805 device_parent_mode_changed,
3806 device_parent_activate,
3807 device_parent_surface_created,
3808 device_parent_volume_created,
3809 device_parent_create_swapchain_texture,
3810 device_parent_create_swapchain,
3813 static void setup_fpu(void)
3815 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3816 WORD cw;
3817 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3818 cw = (cw & ~0xf3f) | 0x3f;
3819 __asm__ volatile ("fldcw %0" : : "m" (cw));
3820 #elif defined(__i386__) && defined(_MSC_VER)
3821 WORD cw;
3822 __asm fnstcw cw;
3823 cw = (cw & ~0xf3f) | 0x3f;
3824 __asm fldcw cw;
3825 #else
3826 FIXME("FPU setup not implemented for this platform.\n");
3827 #endif
3830 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3831 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3832 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3834 struct wined3d_swapchain_desc *swapchain_desc;
3835 UINT i, count = 1;
3836 HRESULT hr;
3838 if (mode)
3839 FIXME("Ignoring display mode.\n");
3841 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3842 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3843 device->refcount = 1;
3845 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3847 wined3d_mutex_lock();
3848 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3849 &device->device_parent, &device->wined3d_device);
3850 if (FAILED(hr))
3852 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3853 wined3d_mutex_unlock();
3854 return hr;
3857 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3859 WINED3DCAPS caps;
3861 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3862 count = caps.NumberOfAdaptersInGroup;
3865 if (flags & D3DCREATE_MULTITHREADED)
3866 wined3d_device_set_multithreaded(device->wined3d_device);
3868 if (!parameters->Windowed)
3870 if (!focus_window)
3871 focus_window = parameters->hDeviceWindow;
3872 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3874 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3875 wined3d_device_decref(device->wined3d_device);
3876 wined3d_mutex_unlock();
3877 return hr;
3880 for (i = 0; i < count; ++i)
3882 HWND device_window = parameters[i].hDeviceWindow;
3884 if (!device_window) device_window = focus_window;
3885 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3886 parameters[i].BackBufferWidth,
3887 parameters[i].BackBufferHeight);
3891 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3892 if (!swapchain_desc)
3894 ERR("Failed to allocate wined3d parameters.\n");
3895 wined3d_device_release_focus_window(device->wined3d_device);
3896 wined3d_device_decref(device->wined3d_device);
3897 wined3d_mutex_unlock();
3898 return E_OUTOFMEMORY;
3901 for (i = 0; i < count; ++i)
3903 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3904 parent->extended))
3906 wined3d_device_release_focus_window(device->wined3d_device);
3907 wined3d_device_decref(device->wined3d_device);
3908 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3909 wined3d_mutex_unlock();
3910 return D3DERR_INVALIDCALL;
3914 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3915 if (FAILED(hr))
3917 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3918 wined3d_device_release_focus_window(device->wined3d_device);
3919 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3920 wined3d_device_decref(device->wined3d_device);
3921 wined3d_mutex_unlock();
3922 return hr;
3925 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3927 wined3d_device_uninit_3d(device->wined3d_device);
3928 wined3d_device_release_focus_window(device->wined3d_device);
3929 wined3d_device_decref(device->wined3d_device);
3930 wined3d_mutex_unlock();
3931 return E_OUTOFMEMORY;
3934 for (i = 0; i < count; ++i)
3936 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3939 wined3d_mutex_unlock();
3941 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3943 /* Initialize the converted declaration array. This creates a valid pointer
3944 * and when adding decls HeapReAlloc() can be used without further checking. */
3945 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3946 if (!device->fvf_decls)
3948 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3949 wined3d_mutex_lock();
3950 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3951 wined3d_device_uninit_3d(device->wined3d_device);
3952 wined3d_device_release_focus_window(device->wined3d_device);
3953 wined3d_device_decref(device->wined3d_device);
3954 wined3d_mutex_unlock();
3955 return E_OUTOFMEMORY;
3958 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3959 device->d3d_parent = parent;
3961 return D3D_OK;