quartz: Clarify debug strings.
[wine.git] / dlls / d3d9 / device.c
blobb8c46381316570f06263baa3b4418d158f089329
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
30 static const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
32 d3d9_null_wined3d_object_destroyed,
35 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
37 BYTE *c = (BYTE *)&format;
39 /* Don't translate FOURCC formats */
40 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
42 switch(format)
44 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
45 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
46 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
47 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
48 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
49 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
50 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
51 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
52 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
53 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
54 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
55 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
56 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
57 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
58 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
59 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
60 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
61 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
73 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
74 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
75 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
76 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
77 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
78 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
79 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
80 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
81 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
82 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
83 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
84 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
85 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
86 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
87 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
88 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
89 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
90 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
91 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
92 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
93 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
94 default:
95 FIXME("Unhandled wined3d format %#x.\n", format);
96 return D3DFMT_UNKNOWN;
100 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
102 BYTE *c = (BYTE *)&format;
104 /* Don't translate FOURCC formats */
105 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
107 switch(format)
109 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
110 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
111 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
112 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
113 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
114 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
115 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
116 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
117 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
118 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
119 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
120 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
121 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
122 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
123 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
124 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
125 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
126 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
127 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
128 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
129 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
130 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
131 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
132 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
133 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
134 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
135 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
136 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
137 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
138 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
139 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
140 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
141 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
142 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
143 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
144 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
145 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
146 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
147 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
148 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
149 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
150 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
151 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
152 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
153 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
154 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
155 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
156 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
157 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
158 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
159 default:
160 FIXME("Unhandled D3DFORMAT %#x\n", format);
161 return WINED3DFMT_UNKNOWN;
165 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
167 switch(primitive_type)
169 case D3DPT_POINTLIST:
170 return primitive_count;
172 case D3DPT_LINELIST:
173 return primitive_count * 2;
175 case D3DPT_LINESTRIP:
176 return primitive_count + 1;
178 case D3DPT_TRIANGLELIST:
179 return primitive_count * 3;
181 case D3DPT_TRIANGLESTRIP:
182 case D3DPT_TRIANGLEFAN:
183 return primitive_count + 2;
185 default:
186 FIXME("Unhandled primitive type %#x\n", primitive_type);
187 return 0;
191 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
192 const struct wined3d_swapchain_desc *swapchain_desc)
194 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
195 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
196 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
197 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
198 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
199 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
200 present_parameters->SwapEffect = swapchain_desc->swap_effect;
201 present_parameters->hDeviceWindow = swapchain_desc->device_window;
202 present_parameters->Windowed = swapchain_desc->windowed;
203 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
204 present_parameters->AutoDepthStencilFormat
205 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
206 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
207 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
208 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
211 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
212 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
214 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
215 UINT highest_bb_count = extended ? 30 : 3;
217 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
219 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
220 return FALSE;
222 if (present_parameters->BackBufferCount > highest_bb_count
223 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
224 && present_parameters->BackBufferCount > 1))
226 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
227 return FALSE;
230 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
231 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
232 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
233 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
234 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
235 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
236 swapchain_desc->swap_effect = present_parameters->SwapEffect;
237 swapchain_desc->device_window = present_parameters->hDeviceWindow;
238 swapchain_desc->windowed = present_parameters->Windowed;
239 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
240 swapchain_desc->auto_depth_stencil_format
241 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
242 swapchain_desc->flags
243 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
244 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
245 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
246 swapchain_desc->auto_restore_display_mode = TRUE;
248 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
249 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
251 return TRUE;
254 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
256 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
258 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
259 || IsEqualGUID(riid, &IID_IUnknown))
261 IDirect3DDevice9Ex_AddRef(iface);
262 *out = iface;
263 return S_OK;
266 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
268 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
270 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
271 * It doesn't matter with which function the device was created. */
272 if (!device->d3d_parent->extended)
274 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
275 *out = NULL;
276 return E_NOINTERFACE;
279 IDirect3DDevice9Ex_AddRef(iface);
280 *out = iface;
281 return S_OK;
284 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
286 *out = NULL;
287 return E_NOINTERFACE;
290 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
292 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
293 ULONG refcount = InterlockedIncrement(&device->refcount);
295 TRACE("%p increasing refcount to %u.\n", iface, refcount);
297 return refcount;
300 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
302 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
303 ULONG refcount;
305 if (device->in_destruction)
306 return 0;
308 refcount = InterlockedDecrement(&device->refcount);
310 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
312 if (!refcount)
314 unsigned i;
315 device->in_destruction = TRUE;
317 wined3d_mutex_lock();
318 for (i = 0; i < device->fvf_decl_count; ++i)
320 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
322 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
324 if (device->vertex_buffer)
325 wined3d_buffer_decref(device->vertex_buffer);
326 if (device->index_buffer)
327 wined3d_buffer_decref(device->index_buffer);
329 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
331 wined3d_device_uninit_3d(device->wined3d_device);
332 wined3d_device_release_focus_window(device->wined3d_device);
333 wined3d_device_decref(device->wined3d_device);
334 wined3d_mutex_unlock();
336 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
338 HeapFree(GetProcessHeap(), 0, device);
341 return refcount;
344 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
346 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
348 TRACE("iface %p.\n", iface);
350 TRACE("device state: %#x.\n", device->device_state);
352 if (device->d3d_parent->extended)
353 return D3D_OK;
355 switch (device->device_state)
357 default:
358 case D3D9_DEVICE_STATE_OK:
359 return D3D_OK;
360 case D3D9_DEVICE_STATE_LOST:
361 return D3DERR_DEVICELOST;
362 case D3D9_DEVICE_STATE_NOT_RESET:
363 return D3DERR_DEVICENOTRESET;
367 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
369 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
370 UINT ret;
372 TRACE("iface %p.\n", iface);
374 wined3d_mutex_lock();
375 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
376 wined3d_mutex_unlock();
378 return ret;
381 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
383 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
385 TRACE("iface %p.\n", iface);
387 wined3d_mutex_lock();
388 wined3d_device_evict_managed_resources(device->wined3d_device);
389 wined3d_mutex_unlock();
391 return D3D_OK;
394 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
396 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
398 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
400 if (!d3d9)
401 return D3DERR_INVALIDCALL;
403 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
406 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
408 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
409 WINED3DCAPS *wined3d_caps;
410 HRESULT hr;
412 TRACE("iface %p, caps %p.\n", iface, caps);
414 if (!caps)
415 return D3DERR_INVALIDCALL;
417 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
418 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
420 memset(caps, 0, sizeof(*caps));
422 wined3d_mutex_lock();
423 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
424 wined3d_mutex_unlock();
426 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
427 HeapFree(GetProcessHeap(), 0, wined3d_caps);
429 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
430 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
432 filter_caps(caps);
434 return hr;
437 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
440 struct wined3d_display_mode wined3d_mode;
441 HRESULT hr;
443 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
445 wined3d_mutex_lock();
446 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
447 wined3d_mutex_unlock();
449 if (SUCCEEDED(hr))
451 mode->Width = wined3d_mode.width;
452 mode->Height = wined3d_mode.height;
453 mode->RefreshRate = wined3d_mode.refresh_rate;
454 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
457 return hr;
460 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
461 D3DDEVICE_CREATION_PARAMETERS *parameters)
463 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
465 TRACE("iface %p, parameters %p.\n", iface, parameters);
467 wined3d_mutex_lock();
468 wined3d_device_get_creation_parameters(device->wined3d_device,
469 (struct wined3d_device_creation_parameters *)parameters);
470 wined3d_mutex_unlock();
472 return D3D_OK;
475 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
476 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
478 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
479 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
480 HRESULT hr;
482 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
483 iface, hotspot_x, hotspot_y, bitmap);
485 if (!bitmap)
487 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
488 return D3DERR_INVALIDCALL;
491 wined3d_mutex_lock();
492 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
493 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
494 wined3d_mutex_unlock();
496 return hr;
499 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
501 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
503 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
505 wined3d_mutex_lock();
506 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
507 wined3d_mutex_unlock();
510 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
512 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
513 BOOL ret;
515 TRACE("iface %p, show %#x.\n", iface, show);
517 wined3d_mutex_lock();
518 ret = wined3d_device_show_cursor(device->wined3d_device, show);
519 wined3d_mutex_unlock();
521 return ret;
524 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
525 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
527 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
528 struct wined3d_swapchain_desc desc;
529 struct d3d9_swapchain *object;
530 UINT i, count;
531 HRESULT hr;
533 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
534 iface, present_parameters, swapchain);
536 if (!present_parameters->Windowed)
538 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
539 return D3DERR_INVALIDCALL;
542 wined3d_mutex_lock();
543 count = wined3d_device_get_swapchain_count(device->wined3d_device);
544 for (i = 0; i < count; ++i)
546 struct wined3d_swapchain *wined3d_swapchain;
548 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
549 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
551 if (!desc.windowed)
553 wined3d_mutex_unlock();
554 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
555 return D3DERR_INVALIDCALL;
558 wined3d_mutex_unlock();
560 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
561 device->d3d_parent->extended))
562 return D3DERR_INVALIDCALL;
563 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
564 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
565 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
567 return hr;
570 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
571 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
573 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
574 HRESULT hr;
576 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
578 wined3d_mutex_lock();
579 if (swapchain_idx < device->implicit_swapchain_count)
581 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
582 IDirect3DSwapChain9Ex_AddRef(*swapchain);
583 hr = D3D_OK;
585 else
587 *swapchain = NULL;
588 hr = D3DERR_INVALIDCALL;
590 wined3d_mutex_unlock();
592 return hr;
595 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
597 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
598 UINT count;
600 TRACE("iface %p.\n", iface);
602 wined3d_mutex_lock();
603 count = wined3d_device_get_swapchain_count(device->wined3d_device);
604 wined3d_mutex_unlock();
606 return count;
609 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
611 struct wined3d_resource_desc desc;
612 IDirect3DBaseTexture9 *texture;
613 struct d3d9_surface *surface;
614 IUnknown *parent;
616 wined3d_resource_get_desc(resource, &desc);
617 if (desc.pool != WINED3D_POOL_DEFAULT)
618 return D3D_OK;
620 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
622 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
623 return D3DERR_INVALIDCALL;
626 parent = wined3d_resource_get_parent(resource);
627 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
629 IDirect3DBaseTexture9_Release(texture);
630 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
631 return D3DERR_INVALIDCALL;
634 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
635 if (!surface->resource.refcount)
636 return D3D_OK;
638 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
639 return D3DERR_INVALIDCALL;
642 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
644 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
645 struct wined3d_swapchain *wined3d_swapchain;
647 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
648 new_swapchain_count * sizeof(*device->implicit_swapchains))))
649 return E_OUTOFMEMORY;
651 for (i = 0; i < new_swapchain_count; ++i)
653 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
654 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
656 device->implicit_swapchain_count = new_swapchain_count;
658 return D3D_OK;
661 static HRESULT d3d9_device_reset(struct d3d9_device *device,
662 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
664 struct wined3d_swapchain_desc swapchain_desc;
665 struct wined3d_display_mode wined3d_mode;
666 HRESULT hr;
668 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
670 WARN("App not active, returning D3DERR_DEVICELOST.\n");
671 return D3DERR_DEVICELOST;
674 if (mode)
676 wined3d_mode.width = mode->Width;
677 wined3d_mode.height = mode->Height;
678 wined3d_mode.refresh_rate = mode->RefreshRate;
679 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
680 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
683 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
684 device->d3d_parent->extended))
685 return D3DERR_INVALIDCALL;
687 wined3d_mutex_lock();
689 if (device->vertex_buffer)
691 wined3d_buffer_decref(device->vertex_buffer);
692 device->vertex_buffer = NULL;
693 device->vertex_buffer_size = 0;
696 if (device->index_buffer)
698 wined3d_buffer_decref(device->index_buffer);
699 device->index_buffer = NULL;
700 device->index_buffer_size = 0;
703 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
704 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
706 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
708 if (FAILED(hr = d3d9_device_get_swapchains(device)))
710 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
712 else
714 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
715 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
716 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
717 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
718 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
720 device->device_state = D3D9_DEVICE_STATE_OK;
723 else if (!device->d3d_parent->extended)
725 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
728 wined3d_mutex_unlock();
730 return hr;
733 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
734 D3DPRESENT_PARAMETERS *present_parameters)
736 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
738 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
740 return d3d9_device_reset(device, present_parameters, NULL);
743 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
744 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
746 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
747 UINT i;
748 HRESULT hr;
750 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
751 iface, src_rect, dst_rect, dst_window_override, dirty_region);
753 if (device->device_state != D3D9_DEVICE_STATE_OK)
754 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
756 if (dirty_region)
757 FIXME("Ignoring dirty_region %p.\n", dirty_region);
759 wined3d_mutex_lock();
760 for (i = 0; i < device->implicit_swapchain_count; ++i)
762 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
763 src_rect, dst_rect, dst_window_override, 0)))
765 wined3d_mutex_unlock();
766 return hr;
769 wined3d_mutex_unlock();
771 return D3D_OK;
774 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
775 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
777 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
778 HRESULT hr;
780 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
781 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
783 /* backbuffer_type is ignored by native. */
785 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
786 *backbuffer = NULL;
788 wined3d_mutex_lock();
789 if (swapchain >= device->implicit_swapchain_count)
791 wined3d_mutex_unlock();
792 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
793 return D3DERR_INVALIDCALL;
796 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
797 backbuffer_idx, backbuffer_type, backbuffer);
798 wined3d_mutex_unlock();
800 return hr;
803 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
804 UINT swapchain, D3DRASTER_STATUS *raster_status)
806 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
807 HRESULT hr;
809 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
811 wined3d_mutex_lock();
812 hr = wined3d_device_get_raster_status(device->wined3d_device,
813 swapchain, (struct wined3d_raster_status *)raster_status);
814 wined3d_mutex_unlock();
816 return hr;
819 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
821 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
822 HRESULT hr;
824 TRACE("iface %p, enable %#x.\n", iface, enable);
826 wined3d_mutex_lock();
827 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
828 wined3d_mutex_unlock();
830 return hr;
833 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
834 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
836 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
838 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
840 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
841 wined3d_mutex_lock();
842 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
843 wined3d_mutex_unlock();
846 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
848 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
850 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
852 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
853 wined3d_mutex_lock();
854 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
855 wined3d_mutex_unlock();
858 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
859 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
860 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
862 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
863 struct d3d9_texture *object;
864 BOOL set_mem = FALSE;
865 HRESULT hr;
867 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
868 iface, width, height, levels, usage, format, pool, texture, shared_handle);
870 *texture = NULL;
871 if (shared_handle)
873 if (!device->d3d_parent->extended)
875 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
876 return E_NOTIMPL;
879 if (pool == D3DPOOL_SYSTEMMEM)
881 if (levels != 1)
882 return D3DERR_INVALIDCALL;
883 set_mem = TRUE;
885 else
887 if (pool != D3DPOOL_DEFAULT)
889 WARN("Trying to create a shared texture in pool %#x.\n", pool);
890 return D3DERR_INVALIDCALL;
892 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
896 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
897 if (!object)
898 return D3DERR_OUTOFVIDEOMEMORY;
900 hr = texture_init(object, device, width, height, levels, usage, format, pool);
901 if (FAILED(hr))
903 WARN("Failed to initialize texture, hr %#x.\n", hr);
904 HeapFree(GetProcessHeap(), 0, object);
905 return hr;
908 if (set_mem)
909 wined3d_texture_update_desc(object->wined3d_texture, width, height,
910 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
911 *shared_handle, 0);
913 TRACE("Created texture %p.\n", object);
914 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
916 return D3D_OK;
919 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
920 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
921 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
923 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
924 struct d3d9_texture *object;
925 HRESULT hr;
927 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
928 iface, width, height, depth, levels);
929 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
930 usage, format, pool, texture, shared_handle);
932 *texture = NULL;
933 if (shared_handle)
935 if (!device->d3d_parent->extended)
937 WARN("Trying to create a shared volume texture on a non-ex device.\n");
938 return E_NOTIMPL;
941 if (pool != D3DPOOL_DEFAULT)
943 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
944 return D3DERR_INVALIDCALL;
946 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
949 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
950 if (!object)
951 return D3DERR_OUTOFVIDEOMEMORY;
953 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
954 if (FAILED(hr))
956 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
957 HeapFree(GetProcessHeap(), 0, object);
958 return hr;
961 TRACE("Created volume texture %p.\n", object);
962 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
964 return D3D_OK;
967 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
968 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
969 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
972 struct d3d9_texture *object;
973 HRESULT hr;
975 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
976 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
978 *texture = NULL;
979 if (shared_handle)
981 if (!device->d3d_parent->extended)
983 WARN("Trying to create a shared cube texture on a non-ex device.\n");
984 return E_NOTIMPL;
987 if (pool != D3DPOOL_DEFAULT)
989 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
990 return D3DERR_INVALIDCALL;
992 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
995 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
996 if (!object)
997 return D3DERR_OUTOFVIDEOMEMORY;
999 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1000 if (FAILED(hr))
1002 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1003 HeapFree(GetProcessHeap(), 0, object);
1004 return hr;
1007 TRACE("Created cube texture %p.\n", object);
1008 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1010 return D3D_OK;
1013 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1014 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1015 HANDLE *shared_handle)
1017 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1018 struct d3d9_vertexbuffer *object;
1019 HRESULT hr;
1021 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1022 iface, size, usage, fvf, pool, buffer, shared_handle);
1024 if (shared_handle)
1026 if (!device->d3d_parent->extended)
1028 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1029 return E_NOTIMPL;
1032 if (pool != D3DPOOL_DEFAULT)
1034 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1035 return D3DERR_NOTAVAILABLE;
1037 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1040 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1041 if (!object)
1042 return D3DERR_OUTOFVIDEOMEMORY;
1044 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1045 if (FAILED(hr))
1047 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1048 HeapFree(GetProcessHeap(), 0, object);
1049 return hr;
1052 TRACE("Created vertex buffer %p.\n", object);
1053 *buffer = &object->IDirect3DVertexBuffer9_iface;
1055 return D3D_OK;
1058 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1059 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1060 HANDLE *shared_handle)
1062 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1063 struct d3d9_indexbuffer *object;
1064 HRESULT hr;
1066 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1067 iface, size, usage, format, pool, buffer, shared_handle);
1069 if (shared_handle)
1071 if (!device->d3d_parent->extended)
1073 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1074 return E_NOTIMPL;
1077 if (pool != D3DPOOL_DEFAULT)
1079 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1080 return D3DERR_NOTAVAILABLE;
1082 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1085 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1086 if (!object)
1087 return D3DERR_OUTOFVIDEOMEMORY;
1089 hr = indexbuffer_init(object, device, size, usage, format, pool);
1090 if (FAILED(hr))
1092 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1093 HeapFree(GetProcessHeap(), 0, object);
1094 return hr;
1097 TRACE("Created index buffer %p.\n", object);
1098 *buffer = &object->IDirect3DIndexBuffer9_iface;
1100 return D3D_OK;
1103 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1104 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1105 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1107 struct wined3d_resource_desc desc;
1108 struct d3d9_surface *surface_impl;
1109 struct wined3d_texture *texture;
1110 HRESULT hr;
1112 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1113 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1114 device, width, height, format, flags, surface, usage, pool,
1115 multisample_type, multisample_quality);
1117 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1118 desc.format = wined3dformat_from_d3dformat(format);
1119 desc.multisample_type = multisample_type;
1120 desc.multisample_quality = multisample_quality;
1121 desc.usage = usage & WINED3DUSAGE_MASK;
1122 desc.pool = pool;
1123 desc.width = width;
1124 desc.height = height;
1125 desc.depth = 1;
1126 desc.size = 0;
1128 if (is_gdi_compat_format(format))
1129 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1131 wined3d_mutex_lock();
1133 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1134 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1136 wined3d_mutex_unlock();
1137 WARN("Failed to create texture, hr %#x.\n", hr);
1138 if (hr == WINED3DERR_NOTAVAILABLE)
1139 hr = D3DERR_INVALIDCALL;
1140 return hr;
1143 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1144 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1145 *surface = &surface_impl->IDirect3DSurface9_iface;
1146 IDirect3DSurface9_AddRef(*surface);
1148 if (user_mem)
1149 wined3d_texture_update_desc(texture, width, height,
1150 desc.format, multisample_type, multisample_quality, user_mem, 0);
1152 wined3d_texture_decref(texture);
1154 wined3d_mutex_unlock();
1156 return D3D_OK;
1159 BOOL is_gdi_compat_format(D3DFORMAT format)
1161 switch (format)
1163 case D3DFMT_A8R8G8B8:
1164 case D3DFMT_X8R8G8B8:
1165 case D3DFMT_R5G6B5:
1166 case D3DFMT_X1R5G5B5:
1167 case D3DFMT_A1R5G5B5:
1168 case D3DFMT_R8G8B8:
1169 return TRUE;
1170 default:
1171 return FALSE;
1175 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1176 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1177 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1179 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1180 DWORD flags = 0;
1182 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1183 "lockable %#x, surface %p, shared_handle %p.\n",
1184 iface, width, height, format, multisample_type, multisample_quality,
1185 lockable, surface, shared_handle);
1187 *surface = NULL;
1188 if (shared_handle)
1190 if (!device->d3d_parent->extended)
1192 WARN("Trying to create a shared render target on a non-ex device.\n");
1193 return E_NOTIMPL;
1196 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1199 if (lockable)
1200 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1202 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1203 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1206 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1207 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1208 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1210 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1211 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1213 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1214 "discard %#x, surface %p, shared_handle %p.\n",
1215 iface, width, height, format, multisample_type, multisample_quality,
1216 discard, surface, shared_handle);
1218 *surface = NULL;
1219 if (shared_handle)
1221 if (!device->d3d_parent->extended)
1223 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1224 return E_NOTIMPL;
1227 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1230 if (discard)
1231 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1233 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1234 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1238 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1239 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1240 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1242 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1243 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1244 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1245 struct wined3d_box src_box;
1246 HRESULT hr;
1248 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1249 iface, src_surface, src_rect, dst_surface, dst_point);
1251 if (src_rect)
1253 src_box.left = src_rect->left;
1254 src_box.top = src_rect->top;
1255 src_box.right = src_rect->right;
1256 src_box.bottom = src_rect->bottom;
1257 src_box.front = 0;
1258 src_box.back = 1;
1261 wined3d_mutex_lock();
1262 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1263 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1264 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1265 src->sub_resource_idx, src_rect ? &src_box : NULL);
1266 wined3d_mutex_unlock();
1268 if (FAILED(hr))
1269 return D3DERR_INVALIDCALL;
1271 return hr;
1274 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1275 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1277 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1278 struct d3d9_texture *src_impl, *dst_impl;
1279 HRESULT hr;
1281 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1283 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1284 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1286 wined3d_mutex_lock();
1287 hr = wined3d_device_update_texture(device->wined3d_device,
1288 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1289 wined3d_mutex_unlock();
1291 return hr;
1294 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1295 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1297 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1298 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1299 struct wined3d_sub_resource_desc wined3d_desc;
1300 RECT dst_rect, src_rect;
1301 HRESULT hr;
1303 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1305 if (!render_target || !dst_surface)
1306 return D3DERR_INVALIDCALL;
1308 wined3d_mutex_lock();
1309 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1310 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1312 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1313 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1315 /* TODO: Check surface sizes, pools, etc. */
1316 if (wined3d_desc.multisample_type)
1317 hr = D3DERR_INVALIDCALL;
1318 else
1319 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1320 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1321 wined3d_mutex_unlock();
1323 return hr;
1326 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1327 UINT swapchain, IDirect3DSurface9 *dst_surface)
1329 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1330 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1331 HRESULT hr = D3DERR_INVALIDCALL;
1333 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1335 wined3d_mutex_lock();
1336 if (swapchain < device->implicit_swapchain_count)
1337 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1338 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1339 wined3d_mutex_unlock();
1341 return hr;
1344 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1345 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1347 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1348 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1349 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1350 struct wined3d_sub_resource_desc src_desc, dst_desc;
1351 HRESULT hr = D3DERR_INVALIDCALL;
1352 RECT d, s;
1354 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1355 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1357 wined3d_mutex_lock();
1358 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1359 if (!dst_rect)
1361 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1362 dst_rect = &d;
1365 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1366 if (!src_rect)
1368 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1369 src_rect = &s;
1372 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1374 if (device->in_scene)
1376 WARN("Rejecting depth / stencil blit while in scene.\n");
1377 goto done;
1380 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1381 || src_rect->bottom != src_desc.height)
1383 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1384 wine_dbgstr_rect(src_rect));
1385 goto done;
1388 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1389 || dst_rect->bottom != dst_desc.height)
1391 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1392 wine_dbgstr_rect(dst_rect));
1393 goto done;
1396 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1398 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1399 goto done;
1403 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1404 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1405 if (hr == WINEDDERR_INVALIDRECT)
1406 hr = D3DERR_INVALIDCALL;
1408 done:
1409 wined3d_mutex_unlock();
1410 return hr;
1413 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1414 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1416 const struct wined3d_color c =
1418 ((color >> 16) & 0xff) / 255.0f,
1419 ((color >> 8) & 0xff) / 255.0f,
1420 (color & 0xff) / 255.0f,
1421 ((color >> 24) & 0xff) / 255.0f,
1423 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1424 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1425 struct wined3d_sub_resource_desc desc;
1426 HRESULT hr;
1428 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1430 wined3d_mutex_lock();
1432 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1433 surface_impl->sub_resource_idx, &desc)))
1435 wined3d_mutex_unlock();
1436 return D3DERR_INVALIDCALL;
1439 if (desc.pool != WINED3D_POOL_DEFAULT)
1441 wined3d_mutex_unlock();
1442 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1443 return D3DERR_INVALIDCALL;
1445 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1447 wined3d_mutex_unlock();
1448 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1449 return D3DERR_INVALIDCALL;
1451 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1453 wined3d_mutex_unlock();
1454 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1455 return D3DERR_INVALIDCALL;
1458 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1459 d3d9_surface_get_rendertarget_view(surface_impl), rect,
1460 WINED3DCLEAR_TARGET, &c, 0.0f, 0);
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 HRESULT hr;
1518 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1520 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1522 WARN("Invalid index %u specified.\n", idx);
1523 return D3DERR_INVALIDCALL;
1526 if (!idx && !surface_impl)
1528 WARN("Trying to set render target 0 to NULL.\n");
1529 return D3DERR_INVALIDCALL;
1532 wined3d_mutex_lock();
1533 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1534 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1535 wined3d_mutex_unlock();
1537 return hr;
1540 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1542 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1543 struct wined3d_rendertarget_view *wined3d_rtv;
1544 struct d3d9_surface *surface_impl;
1545 HRESULT hr = D3D_OK;
1547 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1549 if (!surface)
1550 return D3DERR_INVALIDCALL;
1552 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1554 WARN("Invalid index %u specified.\n", idx);
1555 return D3DERR_INVALIDCALL;
1558 wined3d_mutex_lock();
1559 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1561 /* We want the sub resource parent here, since the view itself may be
1562 * internal to wined3d and may not have a parent. */
1563 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1564 *surface = &surface_impl->IDirect3DSurface9_iface;
1565 IDirect3DSurface9_AddRef(*surface);
1567 else
1569 hr = D3DERR_NOTFOUND;
1570 *surface = NULL;
1572 wined3d_mutex_unlock();
1574 return hr;
1577 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1579 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1580 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1582 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1584 wined3d_mutex_lock();
1585 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1586 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1587 wined3d_mutex_unlock();
1589 return D3D_OK;
1592 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1594 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1595 struct wined3d_rendertarget_view *wined3d_dsv;
1596 struct d3d9_surface *surface_impl;
1597 HRESULT hr = D3D_OK;
1599 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1601 if (!depth_stencil)
1602 return D3DERR_INVALIDCALL;
1604 wined3d_mutex_lock();
1605 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1607 /* We want the sub resource parent here, since the view itself may be
1608 * internal to wined3d and may not have a parent. */
1609 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1610 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1611 IDirect3DSurface9_AddRef(*depth_stencil);
1613 else
1615 hr = D3DERR_NOTFOUND;
1616 *depth_stencil = NULL;
1618 wined3d_mutex_unlock();
1620 return hr;
1623 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1625 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1626 HRESULT hr;
1628 TRACE("iface %p.\n", iface);
1630 wined3d_mutex_lock();
1631 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1632 device->in_scene = TRUE;
1633 wined3d_mutex_unlock();
1635 return hr;
1638 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1640 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1641 HRESULT hr;
1643 TRACE("iface %p.\n", iface);
1645 wined3d_mutex_lock();
1646 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1647 device->in_scene = FALSE;
1648 wined3d_mutex_unlock();
1650 return hr;
1653 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1654 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1656 const struct wined3d_color c =
1658 ((color >> 16) & 0xff) / 255.0f,
1659 ((color >> 8) & 0xff) / 255.0f,
1660 (color & 0xff) / 255.0f,
1661 ((color >> 24) & 0xff) / 255.0f,
1663 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1664 HRESULT hr;
1666 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1667 iface, rect_count, rects, flags, color, z, stencil);
1669 if (rect_count && !rects)
1671 WARN("count %u with NULL rects.\n", rect_count);
1672 rect_count = 0;
1675 wined3d_mutex_lock();
1676 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1677 wined3d_mutex_unlock();
1679 return hr;
1682 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1683 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1685 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1687 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1689 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1690 wined3d_mutex_lock();
1691 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1692 wined3d_mutex_unlock();
1694 return D3D_OK;
1697 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1698 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1700 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1702 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1704 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1705 wined3d_mutex_lock();
1706 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1707 wined3d_mutex_unlock();
1709 return D3D_OK;
1712 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1713 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1715 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1717 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1719 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1720 wined3d_mutex_lock();
1721 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1722 wined3d_mutex_unlock();
1724 return D3D_OK;
1727 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1729 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1731 TRACE("iface %p, viewport %p.\n", iface, viewport);
1733 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1734 wined3d_mutex_lock();
1735 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1736 wined3d_mutex_unlock();
1738 return D3D_OK;
1741 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1745 TRACE("iface %p, viewport %p.\n", iface, viewport);
1747 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1748 wined3d_mutex_lock();
1749 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1750 wined3d_mutex_unlock();
1752 return D3D_OK;
1755 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1757 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1759 TRACE("iface %p, material %p.\n", iface, material);
1761 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1762 wined3d_mutex_lock();
1763 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1764 wined3d_mutex_unlock();
1766 return D3D_OK;
1769 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1771 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1773 TRACE("iface %p, material %p.\n", iface, material);
1775 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1776 wined3d_mutex_lock();
1777 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1778 wined3d_mutex_unlock();
1780 return D3D_OK;
1783 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1785 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1786 HRESULT hr;
1788 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1790 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1791 wined3d_mutex_lock();
1792 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1793 wined3d_mutex_unlock();
1795 return hr;
1798 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1800 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1801 HRESULT hr;
1803 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1805 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1806 wined3d_mutex_lock();
1807 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1808 wined3d_mutex_unlock();
1810 return hr;
1813 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1815 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1816 HRESULT hr;
1818 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1820 wined3d_mutex_lock();
1821 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1822 wined3d_mutex_unlock();
1824 return hr;
1827 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1829 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1830 HRESULT hr;
1832 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1834 wined3d_mutex_lock();
1835 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1836 wined3d_mutex_unlock();
1838 return hr;
1841 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1843 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1844 HRESULT hr;
1846 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1848 wined3d_mutex_lock();
1849 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1850 wined3d_mutex_unlock();
1852 return hr;
1855 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1857 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1858 HRESULT hr;
1860 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1862 wined3d_mutex_lock();
1863 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1864 wined3d_mutex_unlock();
1866 return hr;
1869 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1870 D3DRENDERSTATETYPE state, DWORD value)
1872 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1874 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1876 wined3d_mutex_lock();
1877 wined3d_device_set_render_state(device->wined3d_device, state, value);
1878 wined3d_mutex_unlock();
1880 return D3D_OK;
1883 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1884 D3DRENDERSTATETYPE state, DWORD *value)
1886 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1888 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1890 wined3d_mutex_lock();
1891 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1892 wined3d_mutex_unlock();
1894 return D3D_OK;
1897 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1898 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1900 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1901 struct d3d9_stateblock *object;
1902 HRESULT hr;
1904 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1906 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1908 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1909 return D3DERR_INVALIDCALL;
1912 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1913 if (!object)
1914 return E_OUTOFMEMORY;
1916 hr = stateblock_init(object, device, type, NULL);
1917 if (FAILED(hr))
1919 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1920 HeapFree(GetProcessHeap(), 0, object);
1921 return hr;
1924 TRACE("Created stateblock %p.\n", object);
1925 *stateblock = &object->IDirect3DStateBlock9_iface;
1927 return D3D_OK;
1930 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1932 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1933 HRESULT hr;
1935 TRACE("iface %p.\n", iface);
1937 wined3d_mutex_lock();
1938 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1939 wined3d_mutex_unlock();
1941 return hr;
1944 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1946 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1947 struct wined3d_stateblock *wined3d_stateblock;
1948 struct d3d9_stateblock *object;
1949 HRESULT hr;
1951 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1953 wined3d_mutex_lock();
1954 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1955 wined3d_mutex_unlock();
1956 if (FAILED(hr))
1958 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1959 return hr;
1962 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1963 if (!object)
1965 wined3d_mutex_lock();
1966 wined3d_stateblock_decref(wined3d_stateblock);
1967 wined3d_mutex_unlock();
1968 return E_OUTOFMEMORY;
1971 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1972 if (FAILED(hr))
1974 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1975 wined3d_mutex_lock();
1976 wined3d_stateblock_decref(wined3d_stateblock);
1977 wined3d_mutex_unlock();
1978 HeapFree(GetProcessHeap(), 0, object);
1979 return hr;
1982 TRACE("Created stateblock %p.\n", object);
1983 *stateblock = &object->IDirect3DStateBlock9_iface;
1985 return D3D_OK;
1988 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1990 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1991 HRESULT hr;
1993 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1995 wined3d_mutex_lock();
1996 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1997 wined3d_mutex_unlock();
1999 return hr;
2002 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2004 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2005 HRESULT hr;
2007 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2009 wined3d_mutex_lock();
2010 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2011 wined3d_mutex_unlock();
2013 return hr;
2016 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2018 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2019 struct wined3d_texture *wined3d_texture = NULL;
2020 struct d3d9_texture *texture_impl;
2022 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2024 if (!texture)
2025 return D3DERR_INVALIDCALL;
2027 wined3d_mutex_lock();
2028 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2030 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2031 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2032 IDirect3DBaseTexture9_AddRef(*texture);
2034 else
2036 *texture = NULL;
2038 wined3d_mutex_unlock();
2040 return D3D_OK;
2043 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2045 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2046 struct d3d9_texture *texture_impl;
2047 HRESULT hr;
2049 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2051 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2053 wined3d_mutex_lock();
2054 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2055 texture_impl ? texture_impl->wined3d_texture : NULL);
2056 wined3d_mutex_unlock();
2058 return hr;
2061 static const enum wined3d_texture_stage_state tss_lookup[] =
2063 WINED3D_TSS_INVALID, /* 0, unused */
2064 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2065 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2066 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2067 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2068 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2069 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2070 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2071 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2072 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2073 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2074 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2075 WINED3D_TSS_INVALID, /* 12, unused */
2076 WINED3D_TSS_INVALID, /* 13, unused */
2077 WINED3D_TSS_INVALID, /* 14, unused */
2078 WINED3D_TSS_INVALID, /* 15, unused */
2079 WINED3D_TSS_INVALID, /* 16, unused */
2080 WINED3D_TSS_INVALID, /* 17, unused */
2081 WINED3D_TSS_INVALID, /* 18, unused */
2082 WINED3D_TSS_INVALID, /* 19, unused */
2083 WINED3D_TSS_INVALID, /* 20, unused */
2084 WINED3D_TSS_INVALID, /* 21, unused */
2085 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2086 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2087 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2088 WINED3D_TSS_INVALID, /* 25, unused */
2089 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2090 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2091 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2092 WINED3D_TSS_INVALID, /* 29, unused */
2093 WINED3D_TSS_INVALID, /* 30, unused */
2094 WINED3D_TSS_INVALID, /* 31, unused */
2095 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2098 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2099 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2101 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2103 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2105 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2107 WARN("Invalid state %#x passed.\n", state);
2108 return D3D_OK;
2111 wined3d_mutex_lock();
2112 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2113 wined3d_mutex_unlock();
2115 return D3D_OK;
2118 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2119 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2121 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2123 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2125 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2127 WARN("Invalid state %#x passed.\n", state);
2128 return D3D_OK;
2131 wined3d_mutex_lock();
2132 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2133 wined3d_mutex_unlock();
2135 return D3D_OK;
2138 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2139 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2141 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2143 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2145 wined3d_mutex_lock();
2146 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2147 wined3d_mutex_unlock();
2149 return D3D_OK;
2152 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2153 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2155 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2157 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2159 wined3d_mutex_lock();
2160 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2161 wined3d_mutex_unlock();
2163 return D3D_OK;
2166 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2168 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2169 HRESULT hr;
2171 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2173 wined3d_mutex_lock();
2174 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2175 wined3d_mutex_unlock();
2177 return hr;
2180 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2181 UINT palette_idx, const PALETTEENTRY *entries)
2183 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2185 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2186 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2187 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2189 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2190 return D3D_OK;
2193 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2194 UINT palette_idx, PALETTEENTRY *entries)
2196 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2198 return D3DERR_INVALIDCALL;
2201 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2203 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2205 return D3D_OK;
2208 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2210 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2212 return D3DERR_INVALIDCALL;
2215 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2217 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2219 TRACE("iface %p, rect %p.\n", iface, rect);
2221 wined3d_mutex_lock();
2222 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2223 wined3d_mutex_unlock();
2225 return D3D_OK;
2228 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2230 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2232 TRACE("iface %p, rect %p.\n", iface, rect);
2234 wined3d_mutex_lock();
2235 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2236 wined3d_mutex_unlock();
2238 return D3D_OK;
2241 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2243 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2245 TRACE("iface %p, software %#x.\n", iface, software);
2247 wined3d_mutex_lock();
2248 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2249 wined3d_mutex_unlock();
2251 return D3D_OK;
2254 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2256 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2257 BOOL ret;
2259 TRACE("iface %p.\n", iface);
2261 wined3d_mutex_lock();
2262 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2263 wined3d_mutex_unlock();
2265 return ret;
2268 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2271 HRESULT hr;
2273 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2275 wined3d_mutex_lock();
2276 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2277 wined3d_mutex_unlock();
2279 return hr;
2282 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2284 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2285 float ret;
2287 TRACE("iface %p.\n", iface);
2289 wined3d_mutex_lock();
2290 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2291 wined3d_mutex_unlock();
2293 return ret;
2296 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2297 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2299 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2300 HRESULT hr;
2302 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2303 iface, primitive_type, start_vertex, primitive_count);
2305 wined3d_mutex_lock();
2306 if (!device->has_vertex_declaration)
2308 wined3d_mutex_unlock();
2309 WARN("Called without a valid vertex declaration set.\n");
2310 return D3DERR_INVALIDCALL;
2312 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2313 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2314 vertex_count_from_primitive_count(primitive_type, primitive_count));
2315 wined3d_mutex_unlock();
2317 return hr;
2320 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2321 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2322 UINT vertex_count, UINT start_idx, UINT primitive_count)
2324 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2325 HRESULT hr;
2327 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2328 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2329 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2330 vertex_count, start_idx, primitive_count);
2332 wined3d_mutex_lock();
2333 if (!device->has_vertex_declaration)
2335 wined3d_mutex_unlock();
2336 WARN("Called without a valid vertex declaration set.\n");
2337 return D3DERR_INVALIDCALL;
2339 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2340 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2341 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2342 vertex_count_from_primitive_count(primitive_type, primitive_count));
2343 wined3d_mutex_unlock();
2345 return hr;
2348 /* The caller is responsible for wined3d locking */
2349 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2351 HRESULT hr;
2353 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2355 UINT size = max(device->vertex_buffer_size * 2, min_size);
2356 struct wined3d_buffer *buffer;
2358 TRACE("Growing vertex buffer to %u bytes.\n", size);
2360 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2361 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2362 if (FAILED(hr))
2364 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2365 return hr;
2368 if (device->vertex_buffer)
2369 wined3d_buffer_decref(device->vertex_buffer);
2371 device->vertex_buffer = buffer;
2372 device->vertex_buffer_size = size;
2373 device->vertex_buffer_pos = 0;
2375 return D3D_OK;
2378 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2379 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2381 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2382 HRESULT hr;
2383 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2384 struct wined3d_map_desc wined3d_map_desc;
2385 struct wined3d_box wined3d_box = {0};
2386 UINT size = vtx_count * stride;
2387 struct wined3d_resource *vb;
2388 UINT vb_pos, align;
2390 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2391 iface, primitive_type, primitive_count, data, stride);
2393 if (!primitive_count)
2395 WARN("primitive_count is 0, returning D3D_OK\n");
2396 return D3D_OK;
2399 wined3d_mutex_lock();
2401 if (!device->has_vertex_declaration)
2403 wined3d_mutex_unlock();
2404 WARN("Called without a valid vertex declaration set.\n");
2405 return D3DERR_INVALIDCALL;
2408 hr = d3d9_device_prepare_vertex_buffer(device, size);
2409 if (FAILED(hr))
2410 goto done;
2412 vb_pos = device->vertex_buffer_pos;
2413 align = vb_pos % stride;
2414 if (align) align = stride - align;
2415 if (vb_pos + size + align > device->vertex_buffer_size)
2416 vb_pos = 0;
2417 else
2418 vb_pos += align;
2420 wined3d_box.left = vb_pos;
2421 wined3d_box.right = vb_pos + size;
2422 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2423 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2424 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2425 goto done;
2426 memcpy(wined3d_map_desc.data, data, size);
2427 wined3d_resource_unmap(vb, 0);
2428 device->vertex_buffer_pos = vb_pos + size;
2430 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2431 if (FAILED(hr))
2432 goto done;
2434 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2435 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2436 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2438 done:
2439 wined3d_mutex_unlock();
2440 return hr;
2443 /* The caller is responsible for wined3d locking */
2444 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2446 HRESULT hr;
2448 if (device->index_buffer_size < min_size || !device->index_buffer)
2450 UINT size = max(device->index_buffer_size * 2, min_size);
2451 struct wined3d_buffer *buffer;
2453 TRACE("Growing index buffer to %u bytes.\n", size);
2455 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2456 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2457 if (FAILED(hr))
2459 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2460 return hr;
2463 if (device->index_buffer)
2464 wined3d_buffer_decref(device->index_buffer);
2466 device->index_buffer = buffer;
2467 device->index_buffer_size = size;
2468 device->index_buffer_pos = 0;
2470 return D3D_OK;
2473 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2474 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2475 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2476 const void *vertex_data, UINT vertex_stride)
2478 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2479 HRESULT hr;
2480 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2481 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2482 UINT idx_size = idx_count * idx_fmt_size;
2483 struct wined3d_map_desc wined3d_map_desc;
2484 struct wined3d_box wined3d_box = {0};
2485 struct wined3d_resource *ib, *vb;
2486 UINT ib_pos;
2488 UINT vtx_size = vertex_count * vertex_stride;
2489 UINT vb_pos, align;
2491 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2492 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2493 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2494 index_data, index_format, vertex_data, vertex_stride);
2496 if (!primitive_count)
2498 WARN("primitive_count is 0, returning D3D_OK.\n");
2499 return D3D_OK;
2502 wined3d_mutex_lock();
2504 if (!device->has_vertex_declaration)
2506 wined3d_mutex_unlock();
2507 WARN("Called without a valid vertex declaration set.\n");
2508 return D3DERR_INVALIDCALL;
2511 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2512 if (FAILED(hr))
2513 goto done;
2515 vb_pos = device->vertex_buffer_pos;
2516 align = vb_pos % vertex_stride;
2517 if (align) align = vertex_stride - align;
2518 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2519 vb_pos = 0;
2520 else
2521 vb_pos += align;
2523 wined3d_box.left = vb_pos;
2524 wined3d_box.right = vb_pos + vtx_size;
2525 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2526 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2527 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2528 goto done;
2529 memcpy(wined3d_map_desc.data, vertex_data, vtx_size);
2530 wined3d_resource_unmap(vb, 0);
2531 device->vertex_buffer_pos = vb_pos + vtx_size;
2533 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2534 if (FAILED(hr))
2535 goto done;
2537 ib_pos = device->index_buffer_pos;
2538 align = ib_pos % idx_fmt_size;
2539 if (align) align = idx_fmt_size - align;
2540 if (ib_pos + idx_size + align > device->index_buffer_size)
2541 ib_pos = 0;
2542 else
2543 ib_pos += align;
2545 wined3d_box.left = ib_pos;
2546 wined3d_box.right = ib_pos + idx_size;
2547 ib = wined3d_buffer_get_resource(device->index_buffer);
2548 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2549 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2550 goto done;
2551 memcpy(wined3d_map_desc.data, index_data, idx_size);
2552 wined3d_resource_unmap(ib, 0);
2553 device->index_buffer_pos = ib_pos + idx_size;
2555 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2556 if (FAILED(hr))
2557 goto done;
2559 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2560 wined3dformat_from_d3dformat(index_format), 0);
2561 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2563 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2564 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2566 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2567 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2568 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2570 done:
2571 wined3d_mutex_unlock();
2572 return hr;
2575 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2576 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2577 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2579 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2580 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2581 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2582 HRESULT hr;
2584 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2585 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2587 wined3d_mutex_lock();
2588 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2589 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2590 flags, dst_impl->fvf);
2591 wined3d_mutex_unlock();
2593 return hr;
2596 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2597 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2599 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2600 struct d3d9_vertex_declaration *object;
2601 HRESULT hr;
2603 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2605 if (!declaration)
2607 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2608 return D3DERR_INVALIDCALL;
2611 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2612 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2614 return hr;
2617 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2618 IDirect3DVertexDeclaration9 *declaration)
2620 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2621 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2623 TRACE("iface %p, declaration %p.\n", iface, declaration);
2625 wined3d_mutex_lock();
2626 wined3d_device_set_vertex_declaration(device->wined3d_device,
2627 decl_impl ? decl_impl->wined3d_declaration : NULL);
2628 device->has_vertex_declaration = !!decl_impl;
2629 wined3d_mutex_unlock();
2631 return D3D_OK;
2634 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2635 IDirect3DVertexDeclaration9 **declaration)
2637 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2638 struct wined3d_vertex_declaration *wined3d_declaration;
2639 struct d3d9_vertex_declaration *declaration_impl;
2641 TRACE("iface %p, declaration %p.\n", iface, declaration);
2643 if (!declaration) return D3DERR_INVALIDCALL;
2645 wined3d_mutex_lock();
2646 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2648 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2649 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2650 IDirect3DVertexDeclaration9_AddRef(*declaration);
2652 else
2654 *declaration = NULL;
2656 wined3d_mutex_unlock();
2658 TRACE("Returning %p.\n", *declaration);
2659 return D3D_OK;
2662 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2664 struct wined3d_vertex_declaration *wined3d_declaration;
2665 struct fvf_declaration *fvf_decls = device->fvf_decls;
2666 struct d3d9_vertex_declaration *d3d9_declaration;
2667 D3DVERTEXELEMENT9 *elements;
2668 int p, low, high; /* deliberately signed */
2669 HRESULT hr;
2671 TRACE("Searching for declaration for fvf %08x... ", fvf);
2673 low = 0;
2674 high = device->fvf_decl_count - 1;
2675 while (low <= high)
2677 p = (low + high) >> 1;
2678 TRACE("%d ", p);
2680 if (fvf_decls[p].fvf == fvf)
2682 TRACE("found %p.\n", fvf_decls[p].decl);
2683 return fvf_decls[p].decl;
2686 if (fvf_decls[p].fvf < fvf)
2687 low = p + 1;
2688 else
2689 high = p - 1;
2691 TRACE("not found. Creating and inserting at position %d.\n", low);
2693 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2694 return NULL;
2696 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2697 HeapFree(GetProcessHeap(), 0, elements);
2698 if (FAILED(hr))
2699 return NULL;
2701 if (device->fvf_decl_size == device->fvf_decl_count)
2703 UINT grow = max(device->fvf_decl_size / 2, 8);
2705 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2706 if (!fvf_decls)
2708 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2709 return NULL;
2711 device->fvf_decls = fvf_decls;
2712 device->fvf_decl_size += grow;
2715 d3d9_declaration->fvf = fvf;
2716 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2717 wined3d_vertex_declaration_incref(wined3d_declaration);
2718 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2720 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2721 fvf_decls[low].decl = wined3d_declaration;
2722 fvf_decls[low].fvf = fvf;
2723 ++device->fvf_decl_count;
2725 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2727 return wined3d_declaration;
2730 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2733 struct wined3d_vertex_declaration *decl;
2735 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2737 if (!fvf)
2739 WARN("%#x is not a valid FVF.\n", fvf);
2740 return D3D_OK;
2743 wined3d_mutex_lock();
2744 if (!(decl = device_get_fvf_declaration(device, fvf)))
2746 wined3d_mutex_unlock();
2747 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2748 return D3DERR_DRIVERINTERNALERROR;
2751 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2752 device->has_vertex_declaration = TRUE;
2753 wined3d_mutex_unlock();
2755 return D3D_OK;
2758 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2760 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2761 struct wined3d_vertex_declaration *wined3d_declaration;
2762 struct d3d9_vertex_declaration *d3d9_declaration;
2764 TRACE("iface %p, fvf %p.\n", iface, fvf);
2766 wined3d_mutex_lock();
2767 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2769 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2770 *fvf = d3d9_declaration->fvf;
2772 else
2774 *fvf = 0;
2776 wined3d_mutex_unlock();
2778 TRACE("Returning FVF %#x.\n", *fvf);
2780 return D3D_OK;
2783 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2784 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2786 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2787 struct d3d9_vertexshader *object;
2788 HRESULT hr;
2790 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2792 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2793 if (!object)
2794 return E_OUTOFMEMORY;
2796 hr = vertexshader_init(object, device, byte_code);
2797 if (FAILED(hr))
2799 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2800 HeapFree(GetProcessHeap(), 0, object);
2801 return hr;
2804 TRACE("Created vertex shader %p.\n", object);
2805 *shader = &object->IDirect3DVertexShader9_iface;
2807 return D3D_OK;
2810 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2812 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2813 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2815 TRACE("iface %p, shader %p.\n", iface, shader);
2817 wined3d_mutex_lock();
2818 wined3d_device_set_vertex_shader(device->wined3d_device,
2819 shader_obj ? shader_obj->wined3d_shader : NULL);
2820 wined3d_mutex_unlock();
2822 return D3D_OK;
2825 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2827 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2828 struct d3d9_vertexshader *shader_impl;
2829 struct wined3d_shader *wined3d_shader;
2831 TRACE("iface %p, shader %p.\n", iface, shader);
2833 wined3d_mutex_lock();
2834 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2836 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2837 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2838 IDirect3DVertexShader9_AddRef(*shader);
2840 else
2842 *shader = NULL;
2844 wined3d_mutex_unlock();
2846 TRACE("Returning %p.\n", *shader);
2848 return D3D_OK;
2851 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2852 UINT reg_idx, const float *data, UINT count)
2854 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2855 HRESULT hr;
2857 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2859 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2861 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2862 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2863 return D3DERR_INVALIDCALL;
2866 wined3d_mutex_lock();
2867 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
2868 reg_idx, count, (const struct wined3d_vec4 *)data);
2869 wined3d_mutex_unlock();
2871 return hr;
2874 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2875 UINT reg_idx, float *data, UINT count)
2877 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2878 HRESULT hr;
2880 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2882 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2884 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2885 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2886 return D3DERR_INVALIDCALL;
2889 wined3d_mutex_lock();
2890 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
2891 reg_idx, count, (struct wined3d_vec4 *)data);
2892 wined3d_mutex_unlock();
2894 return hr;
2897 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2898 UINT reg_idx, const int *data, UINT count)
2900 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2901 HRESULT hr;
2903 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2905 wined3d_mutex_lock();
2906 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
2907 reg_idx, count, (const struct wined3d_ivec4 *)data);
2908 wined3d_mutex_unlock();
2910 return hr;
2913 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2914 UINT reg_idx, int *data, UINT count)
2916 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2917 HRESULT hr;
2919 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2921 wined3d_mutex_lock();
2922 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
2923 reg_idx, count, (struct wined3d_ivec4 *)data);
2924 wined3d_mutex_unlock();
2926 return hr;
2929 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2930 UINT reg_idx, const BOOL *data, UINT count)
2932 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2933 HRESULT hr;
2935 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2937 wined3d_mutex_lock();
2938 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2939 wined3d_mutex_unlock();
2941 return hr;
2944 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2945 UINT reg_idx, BOOL *data, UINT count)
2947 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2948 HRESULT hr;
2950 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2952 wined3d_mutex_lock();
2953 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2954 wined3d_mutex_unlock();
2956 return hr;
2959 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2960 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2962 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2963 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2964 HRESULT hr;
2966 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2967 iface, stream_idx, buffer, offset, stride);
2969 wined3d_mutex_lock();
2970 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2971 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2972 wined3d_mutex_unlock();
2974 return hr;
2977 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2978 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2980 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2981 struct d3d9_vertexbuffer *buffer_impl;
2982 struct wined3d_buffer *wined3d_buffer;
2983 HRESULT hr;
2985 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2986 iface, stream_idx, buffer, offset, stride);
2988 if (!buffer)
2989 return D3DERR_INVALIDCALL;
2991 wined3d_mutex_lock();
2992 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2993 if (SUCCEEDED(hr) && wined3d_buffer)
2995 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2996 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2997 IDirect3DVertexBuffer9_AddRef(*buffer);
2999 else
3001 if (FAILED(hr))
3002 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3003 *buffer = NULL;
3005 wined3d_mutex_unlock();
3007 return hr;
3010 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3012 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3013 HRESULT hr;
3015 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3017 wined3d_mutex_lock();
3018 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3019 wined3d_mutex_unlock();
3021 return hr;
3024 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3026 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3027 HRESULT hr;
3029 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3031 wined3d_mutex_lock();
3032 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3033 wined3d_mutex_unlock();
3035 return hr;
3038 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3040 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3041 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3043 TRACE("iface %p, buffer %p.\n", iface, buffer);
3045 wined3d_mutex_lock();
3046 wined3d_device_set_index_buffer(device->wined3d_device,
3047 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3048 wined3d_mutex_unlock();
3050 return D3D_OK;
3053 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3055 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3056 enum wined3d_format_id wined3d_format;
3057 struct wined3d_buffer *wined3d_buffer;
3058 struct d3d9_indexbuffer *buffer_impl;
3060 TRACE("iface %p, buffer %p.\n", iface, buffer);
3062 if (!buffer)
3063 return D3DERR_INVALIDCALL;
3065 wined3d_mutex_lock();
3066 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3068 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3069 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3070 IDirect3DIndexBuffer9_AddRef(*buffer);
3072 else
3074 *buffer = NULL;
3076 wined3d_mutex_unlock();
3078 return D3D_OK;
3081 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3082 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3084 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3085 struct d3d9_pixelshader *object;
3086 HRESULT hr;
3088 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3090 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3091 if (!object)
3093 FIXME("Failed to allocate pixel shader memory.\n");
3094 return E_OUTOFMEMORY;
3097 hr = pixelshader_init(object, device, byte_code);
3098 if (FAILED(hr))
3100 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3101 HeapFree(GetProcessHeap(), 0, object);
3102 return hr;
3105 TRACE("Created pixel shader %p.\n", object);
3106 *shader = &object->IDirect3DPixelShader9_iface;
3108 return D3D_OK;
3111 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3113 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3114 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3116 TRACE("iface %p, shader %p.\n", iface, shader);
3118 wined3d_mutex_lock();
3119 wined3d_device_set_pixel_shader(device->wined3d_device,
3120 shader_obj ? shader_obj->wined3d_shader : NULL);
3121 wined3d_mutex_unlock();
3123 return D3D_OK;
3126 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3128 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3129 struct d3d9_pixelshader *shader_impl;
3130 struct wined3d_shader *wined3d_shader;
3132 TRACE("iface %p, shader %p.\n", iface, shader);
3134 if (!shader) return D3DERR_INVALIDCALL;
3136 wined3d_mutex_lock();
3137 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3139 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3140 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3141 IDirect3DPixelShader9_AddRef(*shader);
3143 else
3145 *shader = NULL;
3147 wined3d_mutex_unlock();
3149 TRACE("Returning %p.\n", *shader);
3151 return D3D_OK;
3154 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3155 UINT reg_idx, const float *data, UINT count)
3157 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3158 HRESULT hr;
3160 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3162 wined3d_mutex_lock();
3163 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3164 reg_idx, count, (const struct wined3d_vec4 *)data);
3165 wined3d_mutex_unlock();
3167 return hr;
3170 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3171 UINT reg_idx, float *data, UINT count)
3173 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3174 HRESULT hr;
3176 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3178 wined3d_mutex_lock();
3179 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3180 reg_idx, count, (struct wined3d_vec4 *)data);
3181 wined3d_mutex_unlock();
3183 return hr;
3186 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3187 UINT reg_idx, const int *data, UINT count)
3189 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3190 HRESULT hr;
3192 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3194 wined3d_mutex_lock();
3195 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3196 reg_idx, count, (const struct wined3d_ivec4 *)data);
3197 wined3d_mutex_unlock();
3199 return hr;
3202 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3203 UINT reg_idx, int *data, UINT count)
3205 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3206 HRESULT hr;
3208 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3210 wined3d_mutex_lock();
3211 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3212 reg_idx, count, (struct wined3d_ivec4 *)data);
3213 wined3d_mutex_unlock();
3215 return hr;
3218 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3219 UINT reg_idx, const BOOL *data, UINT count)
3221 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3222 HRESULT hr;
3224 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3226 wined3d_mutex_lock();
3227 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3228 wined3d_mutex_unlock();
3230 return hr;
3233 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3234 UINT reg_idx, BOOL *data, UINT count)
3236 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3237 HRESULT hr;
3239 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3241 wined3d_mutex_lock();
3242 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3243 wined3d_mutex_unlock();
3245 return hr;
3248 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3249 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3251 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3252 iface, handle, segment_count, patch_info);
3253 return D3D_OK;
3256 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3257 const float *segment_count, const D3DTRIPATCH_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_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3266 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3267 return D3DERR_INVALIDCALL;
3270 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3272 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3273 struct d3d9_query *object;
3274 HRESULT hr;
3276 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3278 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3279 if (!object)
3280 return E_OUTOFMEMORY;
3282 hr = query_init(object, device, type);
3283 if (FAILED(hr))
3285 WARN("Failed to initialize query, hr %#x.\n", hr);
3286 HeapFree(GetProcessHeap(), 0, object);
3287 return hr;
3290 TRACE("Created query %p.\n", object);
3291 if (query) *query = &object->IDirect3DQuery9_iface;
3292 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3294 return D3D_OK;
3297 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3298 UINT width, UINT height, float *rows, float *columns)
3300 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3301 iface, width, height, rows, columns);
3303 return E_NOTIMPL;
3306 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3307 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3308 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3310 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3311 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3312 iface, src_surface, dst_surface, src_descs, rect_count,
3313 dst_descs, operation, offset_x, offset_y);
3315 return E_NOTIMPL;
3318 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3319 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3320 const RGNDATA *dirty_region, DWORD flags)
3322 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3323 UINT i;
3324 HRESULT hr;
3326 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3327 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3328 dst_window_override, dirty_region, flags);
3330 if (device->device_state != D3D9_DEVICE_STATE_OK)
3331 return S_PRESENT_OCCLUDED;
3333 if (dirty_region)
3334 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3336 wined3d_mutex_lock();
3337 for (i = 0; i < device->implicit_swapchain_count; ++i)
3339 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3340 src_rect, dst_rect, dst_window_override, flags)))
3342 wined3d_mutex_unlock();
3343 return hr;
3346 wined3d_mutex_unlock();
3348 return D3D_OK;
3351 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3353 FIXME("iface %p, priority %p stub!\n", iface, priority);
3355 return E_NOTIMPL;
3358 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3360 FIXME("iface %p, priority %d stub!\n", iface, priority);
3362 return E_NOTIMPL;
3365 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3367 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3369 return E_NOTIMPL;
3372 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3373 IDirect3DResource9 **resources, UINT32 resource_count)
3375 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3376 iface, resources, resource_count);
3378 return E_NOTIMPL;
3381 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3383 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3385 return E_NOTIMPL;
3388 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3390 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3392 *max_latency = 2;
3394 return E_NOTIMPL;
3397 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3399 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3400 struct wined3d_swapchain_desc swapchain_desc;
3402 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3404 wined3d_mutex_lock();
3405 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3406 wined3d_mutex_unlock();
3408 if (swapchain_desc.windowed)
3409 return D3D_OK;
3411 /* FIXME: This is actually supposed to check if any other device is in
3412 * fullscreen mode. */
3413 if (dst_window != swapchain_desc.device_window)
3414 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3416 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3419 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3420 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3421 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3423 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3424 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3425 iface, width, height, format, multisample_type, multisample_quality,
3426 lockable, surface, shared_handle, usage);
3428 *surface = NULL;
3429 if (shared_handle)
3430 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3432 return E_NOTIMPL;
3435 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3436 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3437 HANDLE *shared_handle, DWORD usage)
3439 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3440 iface, width, height, format, pool, surface, shared_handle, usage);
3442 return E_NOTIMPL;
3445 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3446 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3447 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3449 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3450 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3452 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3453 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3454 iface, width, height, format, multisample_type, multisample_quality,
3455 discard, surface, shared_handle, usage);
3457 if (usage & D3DUSAGE_DEPTHSTENCIL)
3459 WARN("Invalid usage %#x.\n", usage);
3460 return D3DERR_INVALIDCALL;
3463 if (shared_handle)
3464 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3466 if (discard)
3467 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3469 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3470 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3473 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3474 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3476 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3478 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3480 if (!present_parameters->Windowed == !mode)
3482 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3483 return D3DERR_INVALIDCALL;
3486 if (mode && (mode->Width != present_parameters->BackBufferWidth
3487 || mode->Height != present_parameters->BackBufferHeight))
3489 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3490 mode->Width, mode->Height,
3491 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3492 return D3DERR_INVALIDCALL;
3495 return d3d9_device_reset(device, present_parameters, mode);
3498 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3499 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3501 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3502 struct wined3d_display_mode wined3d_mode;
3503 HRESULT hr;
3505 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3506 iface, swapchain_idx, mode, rotation);
3508 if (mode->Size != sizeof(*mode))
3509 return D3DERR_INVALIDCALL;
3511 wined3d_mutex_lock();
3512 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3513 (enum wined3d_display_rotation *)rotation);
3514 wined3d_mutex_unlock();
3516 if (SUCCEEDED(hr))
3518 mode->Width = wined3d_mode.width;
3519 mode->Height = wined3d_mode.height;
3520 mode->RefreshRate = wined3d_mode.refresh_rate;
3521 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3522 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3525 return hr;
3528 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3530 /* IUnknown */
3531 d3d9_device_QueryInterface,
3532 d3d9_device_AddRef,
3533 d3d9_device_Release,
3534 /* IDirect3DDevice9 */
3535 d3d9_device_TestCooperativeLevel,
3536 d3d9_device_GetAvailableTextureMem,
3537 d3d9_device_EvictManagedResources,
3538 d3d9_device_GetDirect3D,
3539 d3d9_device_GetDeviceCaps,
3540 d3d9_device_GetDisplayMode,
3541 d3d9_device_GetCreationParameters,
3542 d3d9_device_SetCursorProperties,
3543 d3d9_device_SetCursorPosition,
3544 d3d9_device_ShowCursor,
3545 d3d9_device_CreateAdditionalSwapChain,
3546 d3d9_device_GetSwapChain,
3547 d3d9_device_GetNumberOfSwapChains,
3548 d3d9_device_Reset,
3549 d3d9_device_Present,
3550 d3d9_device_GetBackBuffer,
3551 d3d9_device_GetRasterStatus,
3552 d3d9_device_SetDialogBoxMode,
3553 d3d9_device_SetGammaRamp,
3554 d3d9_device_GetGammaRamp,
3555 d3d9_device_CreateTexture,
3556 d3d9_device_CreateVolumeTexture,
3557 d3d9_device_CreateCubeTexture,
3558 d3d9_device_CreateVertexBuffer,
3559 d3d9_device_CreateIndexBuffer,
3560 d3d9_device_CreateRenderTarget,
3561 d3d9_device_CreateDepthStencilSurface,
3562 d3d9_device_UpdateSurface,
3563 d3d9_device_UpdateTexture,
3564 d3d9_device_GetRenderTargetData,
3565 d3d9_device_GetFrontBufferData,
3566 d3d9_device_StretchRect,
3567 d3d9_device_ColorFill,
3568 d3d9_device_CreateOffscreenPlainSurface,
3569 d3d9_device_SetRenderTarget,
3570 d3d9_device_GetRenderTarget,
3571 d3d9_device_SetDepthStencilSurface,
3572 d3d9_device_GetDepthStencilSurface,
3573 d3d9_device_BeginScene,
3574 d3d9_device_EndScene,
3575 d3d9_device_Clear,
3576 d3d9_device_SetTransform,
3577 d3d9_device_GetTransform,
3578 d3d9_device_MultiplyTransform,
3579 d3d9_device_SetViewport,
3580 d3d9_device_GetViewport,
3581 d3d9_device_SetMaterial,
3582 d3d9_device_GetMaterial,
3583 d3d9_device_SetLight,
3584 d3d9_device_GetLight,
3585 d3d9_device_LightEnable,
3586 d3d9_device_GetLightEnable,
3587 d3d9_device_SetClipPlane,
3588 d3d9_device_GetClipPlane,
3589 d3d9_device_SetRenderState,
3590 d3d9_device_GetRenderState,
3591 d3d9_device_CreateStateBlock,
3592 d3d9_device_BeginStateBlock,
3593 d3d9_device_EndStateBlock,
3594 d3d9_device_SetClipStatus,
3595 d3d9_device_GetClipStatus,
3596 d3d9_device_GetTexture,
3597 d3d9_device_SetTexture,
3598 d3d9_device_GetTextureStageState,
3599 d3d9_device_SetTextureStageState,
3600 d3d9_device_GetSamplerState,
3601 d3d9_device_SetSamplerState,
3602 d3d9_device_ValidateDevice,
3603 d3d9_device_SetPaletteEntries,
3604 d3d9_device_GetPaletteEntries,
3605 d3d9_device_SetCurrentTexturePalette,
3606 d3d9_device_GetCurrentTexturePalette,
3607 d3d9_device_SetScissorRect,
3608 d3d9_device_GetScissorRect,
3609 d3d9_device_SetSoftwareVertexProcessing,
3610 d3d9_device_GetSoftwareVertexProcessing,
3611 d3d9_device_SetNPatchMode,
3612 d3d9_device_GetNPatchMode,
3613 d3d9_device_DrawPrimitive,
3614 d3d9_device_DrawIndexedPrimitive,
3615 d3d9_device_DrawPrimitiveUP,
3616 d3d9_device_DrawIndexedPrimitiveUP,
3617 d3d9_device_ProcessVertices,
3618 d3d9_device_CreateVertexDeclaration,
3619 d3d9_device_SetVertexDeclaration,
3620 d3d9_device_GetVertexDeclaration,
3621 d3d9_device_SetFVF,
3622 d3d9_device_GetFVF,
3623 d3d9_device_CreateVertexShader,
3624 d3d9_device_SetVertexShader,
3625 d3d9_device_GetVertexShader,
3626 d3d9_device_SetVertexShaderConstantF,
3627 d3d9_device_GetVertexShaderConstantF,
3628 d3d9_device_SetVertexShaderConstantI,
3629 d3d9_device_GetVertexShaderConstantI,
3630 d3d9_device_SetVertexShaderConstantB,
3631 d3d9_device_GetVertexShaderConstantB,
3632 d3d9_device_SetStreamSource,
3633 d3d9_device_GetStreamSource,
3634 d3d9_device_SetStreamSourceFreq,
3635 d3d9_device_GetStreamSourceFreq,
3636 d3d9_device_SetIndices,
3637 d3d9_device_GetIndices,
3638 d3d9_device_CreatePixelShader,
3639 d3d9_device_SetPixelShader,
3640 d3d9_device_GetPixelShader,
3641 d3d9_device_SetPixelShaderConstantF,
3642 d3d9_device_GetPixelShaderConstantF,
3643 d3d9_device_SetPixelShaderConstantI,
3644 d3d9_device_GetPixelShaderConstantI,
3645 d3d9_device_SetPixelShaderConstantB,
3646 d3d9_device_GetPixelShaderConstantB,
3647 d3d9_device_DrawRectPatch,
3648 d3d9_device_DrawTriPatch,
3649 d3d9_device_DeletePatch,
3650 d3d9_device_CreateQuery,
3651 /* IDirect3DDevice9Ex */
3652 d3d9_device_SetConvolutionMonoKernel,
3653 d3d9_device_ComposeRects,
3654 d3d9_device_PresentEx,
3655 d3d9_device_GetGPUThreadPriority,
3656 d3d9_device_SetGPUThreadPriority,
3657 d3d9_device_WaitForVBlank,
3658 d3d9_device_CheckResourceResidency,
3659 d3d9_device_SetMaximumFrameLatency,
3660 d3d9_device_GetMaximumFrameLatency,
3661 d3d9_device_CheckDeviceState,
3662 d3d9_device_CreateRenderTargetEx,
3663 d3d9_device_CreateOffscreenPlainSurfaceEx,
3664 d3d9_device_CreateDepthStencilSurfaceEx,
3665 d3d9_device_ResetEx,
3666 d3d9_device_GetDisplayModeEx,
3669 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3671 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3674 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3675 struct wined3d_device *device)
3677 TRACE("device_parent %p, device %p.\n", device_parent, device);
3680 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3682 TRACE("device_parent %p.\n", device_parent);
3685 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3687 struct d3d9_device *device = device_from_device_parent(device_parent);
3689 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3691 if (!device->d3d_parent)
3692 return;
3694 if (!activate)
3695 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3696 else if (device->d3d_parent->extended)
3697 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3698 else
3699 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3702 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3703 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3704 void **parent, const struct wined3d_parent_ops **parent_ops)
3706 struct d3d9_surface *d3d_surface;
3708 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3709 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3711 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3712 return E_OUTOFMEMORY;
3714 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3715 *parent = d3d_surface;
3716 TRACE("Created surface %p.\n", d3d_surface);
3718 return D3D_OK;
3721 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3722 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3723 void **parent, const struct wined3d_parent_ops **parent_ops)
3725 struct d3d9_volume *d3d_volume;
3727 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3728 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3730 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3731 return E_OUTOFMEMORY;
3733 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3734 *parent = d3d_volume;
3735 TRACE("Created volume %p.\n", d3d_volume);
3737 return D3D_OK;
3740 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3741 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3743 struct d3d9_device *device = device_from_device_parent(device_parent);
3744 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3745 struct d3d9_surface *d3d_surface;
3746 HRESULT hr;
3748 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3749 device_parent, container_parent, desc, texture);
3751 if (container_parent == device_parent)
3752 container_parent = &device->IDirect3DDevice9Ex_iface;
3754 if (is_gdi_compat_format(d3dformat_from_wined3dformat(desc->format)))
3755 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
3757 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3758 flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3760 WARN("Failed to create texture, hr %#x.\n", hr);
3761 return hr;
3764 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3765 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3767 return hr;
3770 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3771 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3773 struct d3d9_device *device = device_from_device_parent(device_parent);
3774 struct d3d9_swapchain *d3d_swapchain;
3775 HRESULT hr;
3777 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3779 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3780 if (FAILED(hr))
3782 WARN("Failed to create swapchain, hr %#x.\n", hr);
3783 *swapchain = NULL;
3784 return hr;
3787 *swapchain = d3d_swapchain->wined3d_swapchain;
3788 wined3d_swapchain_incref(*swapchain);
3789 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3791 return hr;
3794 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3796 device_parent_wined3d_device_created,
3797 device_parent_mode_changed,
3798 device_parent_activate,
3799 device_parent_surface_created,
3800 device_parent_volume_created,
3801 device_parent_create_swapchain_texture,
3802 device_parent_create_swapchain,
3805 static void setup_fpu(void)
3807 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3808 WORD cw;
3809 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3810 cw = (cw & ~0xf3f) | 0x3f;
3811 __asm__ volatile ("fldcw %0" : : "m" (cw));
3812 #elif defined(__i386__) && defined(_MSC_VER)
3813 WORD cw;
3814 __asm fnstcw cw;
3815 cw = (cw & ~0xf3f) | 0x3f;
3816 __asm fldcw cw;
3817 #else
3818 FIXME("FPU setup not implemented for this platform.\n");
3819 #endif
3822 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3823 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3824 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3826 struct wined3d_swapchain_desc *swapchain_desc;
3827 UINT i, count = 1;
3828 HRESULT hr;
3830 if (mode)
3831 FIXME("Ignoring display mode.\n");
3833 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3834 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3835 device->refcount = 1;
3837 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3839 wined3d_mutex_lock();
3840 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3841 &device->device_parent, &device->wined3d_device);
3842 if (FAILED(hr))
3844 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3845 wined3d_mutex_unlock();
3846 return hr;
3849 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3851 WINED3DCAPS caps;
3853 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3854 count = caps.NumberOfAdaptersInGroup;
3857 if (flags & D3DCREATE_MULTITHREADED)
3858 wined3d_device_set_multithreaded(device->wined3d_device);
3860 if (!parameters->Windowed)
3862 if (!focus_window)
3863 focus_window = parameters->hDeviceWindow;
3864 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3866 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3867 wined3d_device_decref(device->wined3d_device);
3868 wined3d_mutex_unlock();
3869 return hr;
3872 for (i = 0; i < count; ++i)
3874 HWND device_window = parameters[i].hDeviceWindow;
3876 if (!device_window) device_window = focus_window;
3877 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3878 parameters[i].BackBufferWidth,
3879 parameters[i].BackBufferHeight);
3883 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3884 if (!swapchain_desc)
3886 ERR("Failed to allocate wined3d parameters.\n");
3887 wined3d_device_release_focus_window(device->wined3d_device);
3888 wined3d_device_decref(device->wined3d_device);
3889 wined3d_mutex_unlock();
3890 return E_OUTOFMEMORY;
3893 for (i = 0; i < count; ++i)
3895 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3896 parent->extended))
3898 wined3d_device_release_focus_window(device->wined3d_device);
3899 wined3d_device_decref(device->wined3d_device);
3900 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3901 wined3d_mutex_unlock();
3902 return D3DERR_INVALIDCALL;
3906 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3907 if (FAILED(hr))
3909 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3910 wined3d_device_release_focus_window(device->wined3d_device);
3911 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3912 wined3d_device_decref(device->wined3d_device);
3913 wined3d_mutex_unlock();
3914 return hr;
3917 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3919 wined3d_device_uninit_3d(device->wined3d_device);
3920 wined3d_device_release_focus_window(device->wined3d_device);
3921 wined3d_device_decref(device->wined3d_device);
3922 wined3d_mutex_unlock();
3923 return E_OUTOFMEMORY;
3926 for (i = 0; i < count; ++i)
3928 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3931 wined3d_mutex_unlock();
3933 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3935 /* Initialize the converted declaration array. This creates a valid pointer
3936 * and when adding decls HeapReAlloc() can be used without further checking. */
3937 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3938 if (!device->fvf_decls)
3940 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3941 wined3d_mutex_lock();
3942 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3943 wined3d_device_uninit_3d(device->wined3d_device);
3944 wined3d_device_release_focus_window(device->wined3d_device);
3945 wined3d_device_decref(device->wined3d_device);
3946 wined3d_mutex_unlock();
3947 return E_OUTOFMEMORY;
3950 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3951 device->d3d_parent = parent;
3953 return D3D_OK;