TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / d3d9 / device.c
blob16b27e5aca385371b44894cb02ff4916d7bc704c
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
30 static const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
32 d3d9_null_wined3d_object_destroyed,
35 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
37 BYTE *c = (BYTE *)&format;
39 /* Don't translate FOURCC formats */
40 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
42 switch(format)
44 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
45 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
46 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
47 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
48 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
49 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
50 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
51 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
52 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
53 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
54 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
55 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
56 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
57 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
58 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
59 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
60 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
61 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
73 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
74 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
75 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
76 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
77 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
78 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
79 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
80 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
81 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
82 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
83 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
84 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
85 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
86 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
87 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
88 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
89 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
90 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
91 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
92 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
93 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
94 default:
95 FIXME("Unhandled wined3d format %#x.\n", format);
96 return D3DFMT_UNKNOWN;
100 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
102 BYTE *c = (BYTE *)&format;
104 /* Don't translate FOURCC formats */
105 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
107 switch(format)
109 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
110 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
111 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
112 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
113 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
114 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
115 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
116 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
117 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
118 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
119 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
120 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
121 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
122 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
123 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
124 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
125 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
126 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
127 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
128 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
129 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
130 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
131 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
132 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
133 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
134 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
135 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
136 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
137 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
138 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
139 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
140 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
141 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
142 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
143 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
144 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
145 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
146 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
147 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
148 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
149 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
150 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
151 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
152 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
153 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
154 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
155 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
156 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
157 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
158 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
159 default:
160 FIXME("Unhandled D3DFORMAT %#x\n", format);
161 return WINED3DFMT_UNKNOWN;
165 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
167 switch(primitive_type)
169 case D3DPT_POINTLIST:
170 return primitive_count;
172 case D3DPT_LINELIST:
173 return primitive_count * 2;
175 case D3DPT_LINESTRIP:
176 return primitive_count + 1;
178 case D3DPT_TRIANGLELIST:
179 return primitive_count * 3;
181 case D3DPT_TRIANGLESTRIP:
182 case D3DPT_TRIANGLEFAN:
183 return primitive_count + 2;
185 default:
186 FIXME("Unhandled primitive type %#x\n", primitive_type);
187 return 0;
191 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
192 const struct wined3d_swapchain_desc *swapchain_desc)
194 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
195 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
196 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
197 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
198 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
199 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
200 present_parameters->SwapEffect = swapchain_desc->swap_effect;
201 present_parameters->hDeviceWindow = swapchain_desc->device_window;
202 present_parameters->Windowed = swapchain_desc->windowed;
203 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
204 present_parameters->AutoDepthStencilFormat
205 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
206 present_parameters->Flags = swapchain_desc->flags;
207 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
208 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
211 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
212 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
214 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
215 UINT highest_bb_count = extended ? 30 : 3;
217 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
219 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
220 return FALSE;
222 if (present_parameters->BackBufferCount > highest_bb_count
223 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
224 && present_parameters->BackBufferCount > 1))
226 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
227 return FALSE;
230 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
231 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
232 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
233 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
234 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
235 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
236 swapchain_desc->swap_effect = present_parameters->SwapEffect;
237 swapchain_desc->device_window = present_parameters->hDeviceWindow;
238 swapchain_desc->windowed = present_parameters->Windowed;
239 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
240 swapchain_desc->auto_depth_stencil_format
241 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
242 swapchain_desc->flags = present_parameters->Flags;
243 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
244 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
245 swapchain_desc->auto_restore_display_mode = TRUE;
247 return TRUE;
250 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
252 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
254 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
255 || IsEqualGUID(riid, &IID_IUnknown))
257 IDirect3DDevice9Ex_AddRef(iface);
258 *out = iface;
259 return S_OK;
262 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
264 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
266 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
267 * It doesn't matter with which function the device was created. */
268 if (!device->d3d_parent->extended)
270 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
271 *out = NULL;
272 return E_NOINTERFACE;
275 IDirect3DDevice9Ex_AddRef(iface);
276 *out = iface;
277 return S_OK;
280 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
282 *out = NULL;
283 return E_NOINTERFACE;
286 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
289 ULONG refcount = InterlockedIncrement(&device->refcount);
291 TRACE("%p increasing refcount to %u.\n", iface, refcount);
293 return refcount;
296 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
298 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
299 ULONG refcount;
301 if (device->in_destruction)
302 return 0;
304 refcount = InterlockedDecrement(&device->refcount);
306 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
308 if (!refcount)
310 unsigned i;
311 device->in_destruction = TRUE;
313 wined3d_mutex_lock();
314 for (i = 0; i < device->fvf_decl_count; ++i)
316 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
318 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
320 if (device->vertex_buffer)
321 wined3d_buffer_decref(device->vertex_buffer);
322 if (device->index_buffer)
323 wined3d_buffer_decref(device->index_buffer);
325 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
327 wined3d_device_uninit_3d(device->wined3d_device);
328 wined3d_device_release_focus_window(device->wined3d_device);
329 wined3d_device_decref(device->wined3d_device);
330 wined3d_mutex_unlock();
332 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
334 HeapFree(GetProcessHeap(), 0, device);
337 return refcount;
340 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
342 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
344 TRACE("iface %p.\n", iface);
346 TRACE("device state: %#x.\n", device->device_state);
348 if (device->d3d_parent->extended)
349 return D3D_OK;
351 switch (device->device_state)
353 default:
354 case D3D9_DEVICE_STATE_OK:
355 return D3D_OK;
356 case D3D9_DEVICE_STATE_LOST:
357 return D3DERR_DEVICELOST;
358 case D3D9_DEVICE_STATE_NOT_RESET:
359 return D3DERR_DEVICENOTRESET;
363 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
365 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
366 UINT ret;
368 TRACE("iface %p.\n", iface);
370 wined3d_mutex_lock();
371 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
372 wined3d_mutex_unlock();
374 return ret;
377 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
379 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
381 TRACE("iface %p.\n", iface);
383 wined3d_mutex_lock();
384 wined3d_device_evict_managed_resources(device->wined3d_device);
385 wined3d_mutex_unlock();
387 return D3D_OK;
390 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
392 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
394 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
396 if (!d3d9)
397 return D3DERR_INVALIDCALL;
399 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
402 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
404 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
405 WINED3DCAPS *wined3d_caps;
406 HRESULT hr;
408 TRACE("iface %p, caps %p.\n", iface, caps);
410 if (!caps)
411 return D3DERR_INVALIDCALL;
413 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
414 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
416 memset(caps, 0, sizeof(*caps));
418 wined3d_mutex_lock();
419 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
420 wined3d_mutex_unlock();
422 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
423 HeapFree(GetProcessHeap(), 0, wined3d_caps);
425 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
426 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
428 filter_caps(caps);
430 return hr;
433 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
435 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
436 struct wined3d_display_mode wined3d_mode;
437 HRESULT hr;
439 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
441 wined3d_mutex_lock();
442 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
443 wined3d_mutex_unlock();
445 if (SUCCEEDED(hr))
447 mode->Width = wined3d_mode.width;
448 mode->Height = wined3d_mode.height;
449 mode->RefreshRate = wined3d_mode.refresh_rate;
450 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
453 return hr;
456 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
457 D3DDEVICE_CREATION_PARAMETERS *parameters)
459 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
461 TRACE("iface %p, parameters %p.\n", iface, parameters);
463 wined3d_mutex_lock();
464 wined3d_device_get_creation_parameters(device->wined3d_device,
465 (struct wined3d_device_creation_parameters *)parameters);
466 wined3d_mutex_unlock();
468 return D3D_OK;
471 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
472 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
474 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
475 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
476 HRESULT hr;
478 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
479 iface, hotspot_x, hotspot_y, bitmap);
481 if (!bitmap)
483 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
484 return D3DERR_INVALIDCALL;
487 wined3d_mutex_lock();
488 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
489 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
490 wined3d_mutex_unlock();
492 return hr;
495 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
497 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
499 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
501 wined3d_mutex_lock();
502 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
503 wined3d_mutex_unlock();
506 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
508 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
509 BOOL ret;
511 TRACE("iface %p, show %#x.\n", iface, show);
513 wined3d_mutex_lock();
514 ret = wined3d_device_show_cursor(device->wined3d_device, show);
515 wined3d_mutex_unlock();
517 return ret;
520 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
521 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
523 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
524 struct wined3d_swapchain_desc desc;
525 struct d3d9_swapchain *object;
526 UINT i, count;
527 HRESULT hr;
529 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
530 iface, present_parameters, swapchain);
532 if (!present_parameters->Windowed)
534 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
535 return D3DERR_INVALIDCALL;
538 wined3d_mutex_lock();
539 count = wined3d_device_get_swapchain_count(device->wined3d_device);
540 for (i = 0; i < count; ++i)
542 struct wined3d_swapchain *wined3d_swapchain;
544 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
545 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
547 if (!desc.windowed)
549 wined3d_mutex_unlock();
550 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
551 return D3DERR_INVALIDCALL;
554 wined3d_mutex_unlock();
556 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
557 device->d3d_parent->extended))
558 return D3DERR_INVALIDCALL;
559 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
560 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
561 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
563 return hr;
566 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
567 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
570 HRESULT hr;
572 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
574 wined3d_mutex_lock();
575 if (swapchain_idx < device->implicit_swapchain_count)
577 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
578 IDirect3DSwapChain9Ex_AddRef(*swapchain);
579 hr = D3D_OK;
581 else
583 *swapchain = NULL;
584 hr = D3DERR_INVALIDCALL;
586 wined3d_mutex_unlock();
588 return hr;
591 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
593 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
594 UINT count;
596 TRACE("iface %p.\n", iface);
598 wined3d_mutex_lock();
599 count = wined3d_device_get_swapchain_count(device->wined3d_device);
600 wined3d_mutex_unlock();
602 return count;
605 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
607 struct wined3d_resource_desc desc;
609 wined3d_resource_get_desc(resource, &desc);
610 if (desc.pool == WINED3D_POOL_DEFAULT)
612 struct d3d9_surface *surface;
614 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
616 IUnknown *parent = wined3d_resource_get_parent(resource);
617 IDirect3DBaseTexture9 *texture;
619 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
621 IDirect3DBaseTexture9_Release(texture);
622 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
623 return D3DERR_INVALIDCALL;
626 return D3D_OK;
629 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
631 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
632 return D3DERR_INVALIDCALL;
635 surface = wined3d_resource_get_parent(resource);
636 if (surface->resource.refcount)
638 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
639 return D3DERR_INVALIDCALL;
642 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
645 return D3D_OK;
648 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
650 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
651 struct wined3d_swapchain *wined3d_swapchain;
653 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
654 new_swapchain_count * sizeof(*device->implicit_swapchains))))
655 return E_OUTOFMEMORY;
657 for (i = 0; i < new_swapchain_count; ++i)
659 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
660 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
662 device->implicit_swapchain_count = new_swapchain_count;
664 return D3D_OK;
667 static HRESULT d3d9_device_reset(struct d3d9_device *device,
668 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
670 struct wined3d_swapchain_desc swapchain_desc;
671 struct wined3d_display_mode wined3d_mode;
672 HRESULT hr;
674 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
676 WARN("App not active, returning D3DERR_DEVICELOST.\n");
677 return D3DERR_DEVICELOST;
680 if (mode)
682 wined3d_mode.width = mode->Width;
683 wined3d_mode.height = mode->Height;
684 wined3d_mode.refresh_rate = mode->RefreshRate;
685 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
686 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
689 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
690 device->d3d_parent->extended))
691 return D3DERR_INVALIDCALL;
693 wined3d_mutex_lock();
695 if (device->vertex_buffer)
697 wined3d_buffer_decref(device->vertex_buffer);
698 device->vertex_buffer = NULL;
699 device->vertex_buffer_size = 0;
702 if (device->index_buffer)
704 wined3d_buffer_decref(device->index_buffer);
705 device->index_buffer = NULL;
706 device->index_buffer_size = 0;
709 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
710 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
712 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
714 if (FAILED(hr = d3d9_device_get_swapchains(device)))
716 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
718 else
720 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
721 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
722 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
723 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
724 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
726 device->device_state = D3D9_DEVICE_STATE_OK;
729 else if (!device->d3d_parent->extended)
731 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
734 wined3d_mutex_unlock();
736 return hr;
739 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
740 D3DPRESENT_PARAMETERS *present_parameters)
742 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
744 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
746 return d3d9_device_reset(device, present_parameters, NULL);
749 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
750 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
752 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
753 UINT i;
754 HRESULT hr;
756 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
757 iface, src_rect, dst_rect, dst_window_override, dirty_region);
759 if (device->device_state != D3D9_DEVICE_STATE_OK)
760 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
762 wined3d_mutex_lock();
763 for (i = 0; i < device->implicit_swapchain_count; ++i)
765 hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain, src_rect,
766 dst_rect, dst_window_override, dirty_region, 0);
767 if (FAILED(hr))
769 wined3d_mutex_unlock();
770 return hr;
773 wined3d_mutex_unlock();
775 return D3D_OK;
778 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
779 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
781 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
782 HRESULT hr;
784 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
785 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
787 /* backbuffer_type is ignored by native. */
789 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
790 *backbuffer = NULL;
792 wined3d_mutex_lock();
793 if (swapchain >= device->implicit_swapchain_count)
795 wined3d_mutex_unlock();
796 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
797 return D3DERR_INVALIDCALL;
800 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
801 backbuffer_idx, backbuffer_type, backbuffer);
802 wined3d_mutex_unlock();
804 return hr;
807 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
808 UINT swapchain, D3DRASTER_STATUS *raster_status)
810 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
811 HRESULT hr;
813 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
815 wined3d_mutex_lock();
816 hr = wined3d_device_get_raster_status(device->wined3d_device,
817 swapchain, (struct wined3d_raster_status *)raster_status);
818 wined3d_mutex_unlock();
820 return hr;
823 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
825 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
826 HRESULT hr;
828 TRACE("iface %p, enable %#x.\n", iface, enable);
830 wined3d_mutex_lock();
831 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
832 wined3d_mutex_unlock();
834 return hr;
837 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
838 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
840 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
842 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
844 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
845 wined3d_mutex_lock();
846 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
847 wined3d_mutex_unlock();
850 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
852 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
854 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
856 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
857 wined3d_mutex_lock();
858 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
859 wined3d_mutex_unlock();
862 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
863 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
864 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
866 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
867 struct d3d9_texture *object;
868 BOOL set_mem = FALSE;
869 HRESULT hr;
871 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
872 iface, width, height, levels, usage, format, pool, texture, shared_handle);
874 *texture = NULL;
875 if (shared_handle)
877 if (!device->d3d_parent->extended)
879 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
880 return E_NOTIMPL;
883 if (pool == D3DPOOL_SYSTEMMEM)
885 if (levels != 1)
886 return D3DERR_INVALIDCALL;
887 set_mem = TRUE;
889 else
891 if (pool != D3DPOOL_DEFAULT)
893 WARN("Trying to create a shared texture in pool %#x.\n", pool);
894 return D3DERR_INVALIDCALL;
896 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
900 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
901 if (!object)
902 return D3DERR_OUTOFVIDEOMEMORY;
904 hr = texture_init(object, device, width, height, levels, usage, format, pool);
905 if (FAILED(hr))
907 WARN("Failed to initialize texture, hr %#x.\n", hr);
908 HeapFree(GetProcessHeap(), 0, object);
909 return hr;
912 if (set_mem)
913 wined3d_texture_update_desc(object->wined3d_texture, width, height,
914 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
915 *shared_handle, 0);
917 TRACE("Created texture %p.\n", object);
918 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
920 return D3D_OK;
923 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
924 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
925 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
927 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
928 struct d3d9_texture *object;
929 HRESULT hr;
931 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
932 iface, width, height, depth, levels);
933 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
934 usage, format, pool, texture, shared_handle);
936 *texture = NULL;
937 if (shared_handle)
939 if (!device->d3d_parent->extended)
941 WARN("Trying to create a shared volume texture on a non-ex device.\n");
942 return E_NOTIMPL;
945 if (pool != D3DPOOL_DEFAULT)
947 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
948 return D3DERR_INVALIDCALL;
950 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
953 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
954 if (!object)
955 return D3DERR_OUTOFVIDEOMEMORY;
957 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
958 if (FAILED(hr))
960 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
961 HeapFree(GetProcessHeap(), 0, object);
962 return hr;
965 TRACE("Created volume texture %p.\n", object);
966 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
968 return D3D_OK;
971 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
972 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
973 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
975 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
976 struct d3d9_texture *object;
977 HRESULT hr;
979 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
980 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
982 *texture = NULL;
983 if (shared_handle)
985 if (!device->d3d_parent->extended)
987 WARN("Trying to create a shared cube texture on a non-ex device.\n");
988 return E_NOTIMPL;
991 if (pool != D3DPOOL_DEFAULT)
993 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
994 return D3DERR_INVALIDCALL;
996 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
999 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1000 if (!object)
1001 return D3DERR_OUTOFVIDEOMEMORY;
1003 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1004 if (FAILED(hr))
1006 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1007 HeapFree(GetProcessHeap(), 0, object);
1008 return hr;
1011 TRACE("Created cube texture %p.\n", object);
1012 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1014 return D3D_OK;
1017 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1018 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1019 HANDLE *shared_handle)
1021 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1022 struct d3d9_vertexbuffer *object;
1023 HRESULT hr;
1025 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1026 iface, size, usage, fvf, pool, buffer, shared_handle);
1028 if (shared_handle)
1030 if (!device->d3d_parent->extended)
1032 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1033 return E_NOTIMPL;
1036 if (pool != D3DPOOL_DEFAULT)
1038 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1039 return D3DERR_NOTAVAILABLE;
1041 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1044 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1045 if (!object)
1046 return D3DERR_OUTOFVIDEOMEMORY;
1048 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1049 if (FAILED(hr))
1051 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1052 HeapFree(GetProcessHeap(), 0, object);
1053 return hr;
1056 TRACE("Created vertex buffer %p.\n", object);
1057 *buffer = &object->IDirect3DVertexBuffer9_iface;
1059 return D3D_OK;
1062 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1063 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1064 HANDLE *shared_handle)
1066 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1067 struct d3d9_indexbuffer *object;
1068 HRESULT hr;
1070 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1071 iface, size, usage, format, pool, buffer, shared_handle);
1073 if (shared_handle)
1075 if (!device->d3d_parent->extended)
1077 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1078 return E_NOTIMPL;
1081 if (pool != D3DPOOL_DEFAULT)
1083 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1084 return D3DERR_NOTAVAILABLE;
1086 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1089 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1090 if (!object)
1091 return D3DERR_OUTOFVIDEOMEMORY;
1093 hr = indexbuffer_init(object, device, size, usage, format, pool);
1094 if (FAILED(hr))
1096 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1097 HeapFree(GetProcessHeap(), 0, object);
1098 return hr;
1101 TRACE("Created index buffer %p.\n", object);
1102 *buffer = &object->IDirect3DIndexBuffer9_iface;
1104 return D3D_OK;
1107 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1108 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1109 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1111 struct wined3d_resource *sub_resource;
1112 struct wined3d_resource_desc desc;
1113 struct d3d9_surface *surface_impl;
1114 struct wined3d_texture *texture;
1115 HRESULT hr;
1117 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1118 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1119 device, width, height, format, flags, surface, usage, pool,
1120 multisample_type, multisample_quality);
1122 desc.resource_type = WINED3D_RTYPE_TEXTURE;
1123 desc.format = wined3dformat_from_d3dformat(format);
1124 desc.multisample_type = multisample_type;
1125 desc.multisample_quality = multisample_quality;
1126 desc.usage = usage & WINED3DUSAGE_MASK;
1127 desc.pool = pool;
1128 desc.width = width;
1129 desc.height = height;
1130 desc.depth = 1;
1131 desc.size = 0;
1133 wined3d_mutex_lock();
1135 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1136 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1138 wined3d_mutex_unlock();
1139 WARN("Failed to create texture, hr %#x.\n", hr);
1140 return hr;
1143 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1144 surface_impl = wined3d_resource_get_parent(sub_resource);
1145 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1146 *surface = &surface_impl->IDirect3DSurface9_iface;
1147 IDirect3DSurface9_AddRef(*surface);
1149 if (user_mem)
1150 wined3d_texture_update_desc(texture, width, height,
1151 desc.format, multisample_type, multisample_quality, user_mem, 0);
1153 wined3d_texture_decref(texture);
1155 wined3d_mutex_unlock();
1157 return D3D_OK;
1160 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1161 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1162 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1164 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1165 DWORD flags = 0;
1167 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1168 "lockable %#x, surface %p, shared_handle %p.\n",
1169 iface, width, height, format, multisample_type, multisample_quality,
1170 lockable, surface, shared_handle);
1172 *surface = NULL;
1173 if (shared_handle)
1175 if (!device->d3d_parent->extended)
1177 WARN("Trying to create a shared render target on a non-ex device.\n");
1178 return E_NOTIMPL;
1181 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1184 if (lockable)
1185 flags |= WINED3D_SURFACE_MAPPABLE;
1187 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1188 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1191 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1192 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1193 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1195 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1196 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1198 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1199 "discard %#x, surface %p, shared_handle %p.\n",
1200 iface, width, height, format, multisample_type, multisample_quality,
1201 discard, surface, shared_handle);
1203 *surface = NULL;
1204 if (shared_handle)
1206 if (!device->d3d_parent->extended)
1208 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1209 return E_NOTIMPL;
1212 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1215 if (discard)
1216 flags |= WINED3D_SURFACE_DISCARD;
1218 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1219 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1223 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1224 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1225 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1227 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1228 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1229 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1230 HRESULT hr;
1232 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1233 iface, src_surface, src_rect, dst_surface, dst_point);
1235 wined3d_mutex_lock();
1236 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1237 dst->wined3d_surface, dst_point);
1238 wined3d_mutex_unlock();
1240 return hr;
1243 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1244 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1246 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1247 struct d3d9_texture *src_impl, *dst_impl;
1248 HRESULT hr;
1250 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1252 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1253 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1255 wined3d_mutex_lock();
1256 hr = wined3d_device_update_texture(device->wined3d_device,
1257 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1258 wined3d_mutex_unlock();
1260 return hr;
1263 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1264 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1266 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1267 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1268 struct wined3d_resource_desc wined3d_desc;
1269 struct wined3d_resource *sub_resource;
1270 HRESULT hr;
1272 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1274 wined3d_mutex_lock();
1275 sub_resource = wined3d_texture_get_sub_resource(rt_impl->wined3d_texture, rt_impl->sub_resource_idx);
1276 wined3d_resource_get_desc(sub_resource, &wined3d_desc);
1278 /* TODO: Check surface sizes, pools, etc. */
1279 if (wined3d_desc.multisample_type)
1280 hr = D3DERR_INVALIDCALL;
1281 else
1282 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, NULL,
1283 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, NULL, 0, NULL, WINED3D_TEXF_POINT);
1284 wined3d_mutex_unlock();
1286 return hr;
1289 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1290 UINT swapchain, IDirect3DSurface9 *dst_surface)
1292 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1293 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1294 HRESULT hr = D3DERR_INVALIDCALL;
1296 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1298 wined3d_mutex_lock();
1299 if (swapchain < device->implicit_swapchain_count)
1300 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1301 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1302 wined3d_mutex_unlock();
1304 return hr;
1307 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1308 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1310 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1311 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1312 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1313 HRESULT hr = D3DERR_INVALIDCALL;
1314 struct wined3d_resource_desc src_desc, dst_desc;
1315 struct wined3d_resource *sub_resource;
1317 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1318 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1320 wined3d_mutex_lock();
1321 sub_resource = wined3d_texture_get_sub_resource(dst->wined3d_texture, dst->sub_resource_idx);
1322 wined3d_resource_get_desc(sub_resource, &dst_desc);
1324 sub_resource = wined3d_texture_get_sub_resource(src->wined3d_texture, src->sub_resource_idx);
1325 wined3d_resource_get_desc(sub_resource, &src_desc);
1327 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1329 if (device->in_scene)
1331 WARN("Rejecting depth / stencil blit while in scene.\n");
1332 goto done;
1335 if (src_rect)
1337 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1338 || src_rect->bottom != src_desc.height)
1340 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1341 wine_dbgstr_rect(src_rect));
1342 goto done;
1345 if (dst_rect)
1347 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1348 || dst_rect->bottom != dst_desc.height)
1350 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1351 wine_dbgstr_rect(dst_rect));
1352 goto done;
1355 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1357 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1358 goto done;
1362 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1363 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1364 if (hr == WINEDDERR_INVALIDRECT)
1365 hr = D3DERR_INVALIDCALL;
1367 done:
1368 wined3d_mutex_unlock();
1369 return hr;
1372 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1373 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1375 const struct wined3d_color c =
1377 ((color >> 16) & 0xff) / 255.0f,
1378 ((color >> 8) & 0xff) / 255.0f,
1379 (color & 0xff) / 255.0f,
1380 ((color >> 24) & 0xff) / 255.0f,
1382 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1383 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1384 struct wined3d_resource *wined3d_resource;
1385 struct wined3d_resource_desc desc;
1386 HRESULT hr;
1388 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1390 wined3d_mutex_lock();
1392 if (!(wined3d_resource = wined3d_texture_get_sub_resource(surface_impl->wined3d_texture, surface_impl->sub_resource_idx)))
1394 wined3d_mutex_unlock();
1395 return D3DERR_INVALIDCALL;
1397 wined3d_resource_get_desc(wined3d_resource, &desc);
1399 if (desc.pool != WINED3D_POOL_DEFAULT)
1401 wined3d_mutex_unlock();
1402 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1403 return D3DERR_INVALIDCALL;
1405 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1407 wined3d_mutex_unlock();
1408 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1409 return D3DERR_INVALIDCALL;
1411 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1413 wined3d_mutex_unlock();
1414 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1415 return D3DERR_INVALIDCALL;
1418 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1419 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1421 wined3d_mutex_unlock();
1423 return hr;
1426 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1427 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1428 HANDLE *shared_handle)
1430 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1431 void *user_mem = NULL;
1433 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1434 iface, width, height, format, pool, surface, shared_handle);
1436 *surface = NULL;
1437 if (pool == D3DPOOL_MANAGED)
1439 WARN("Attempting to create a managed offscreen plain surface.\n");
1440 return D3DERR_INVALIDCALL;
1443 if (shared_handle)
1445 if (!device->d3d_parent->extended)
1447 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1448 return E_NOTIMPL;
1451 if (pool == D3DPOOL_SYSTEMMEM)
1452 user_mem = *shared_handle;
1453 else
1455 if (pool != D3DPOOL_DEFAULT)
1457 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1458 return D3DERR_INVALIDCALL;
1460 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1464 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1465 * regardless of the pool they're created in. Should we set dynamic usage
1466 * here? */
1467 return d3d9_device_create_surface(device, width, height, format,
1468 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1471 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1473 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1474 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1475 HRESULT hr;
1477 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1479 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1481 WARN("Invalid index %u specified.\n", idx);
1482 return D3DERR_INVALIDCALL;
1485 if (!idx && !surface_impl)
1487 WARN("Trying to set render target 0 to NULL.\n");
1488 return D3DERR_INVALIDCALL;
1491 wined3d_mutex_lock();
1492 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1493 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1494 wined3d_mutex_unlock();
1496 return hr;
1499 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1501 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1502 struct wined3d_rendertarget_view *wined3d_rtv;
1503 struct d3d9_surface *surface_impl;
1504 HRESULT hr = D3D_OK;
1506 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1508 if (!surface)
1509 return D3DERR_INVALIDCALL;
1511 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1513 WARN("Invalid index %u specified.\n", idx);
1514 return D3DERR_INVALIDCALL;
1517 wined3d_mutex_lock();
1518 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1520 /* We want the sub resource parent here, since the view itself may be
1521 * internal to wined3d and may not have a parent. */
1522 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1523 *surface = &surface_impl->IDirect3DSurface9_iface;
1524 IDirect3DSurface9_AddRef(*surface);
1526 else
1528 hr = WINED3DERR_NOTFOUND;
1529 *surface = NULL;
1531 wined3d_mutex_unlock();
1533 return hr;
1536 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1538 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1539 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1541 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1543 wined3d_mutex_lock();
1544 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1545 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1546 wined3d_mutex_unlock();
1548 return D3D_OK;
1551 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1553 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1554 struct wined3d_rendertarget_view *wined3d_dsv;
1555 struct d3d9_surface *surface_impl;
1556 HRESULT hr = D3D_OK;
1558 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1560 if (!depth_stencil)
1561 return D3DERR_INVALIDCALL;
1563 wined3d_mutex_lock();
1564 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1566 /* We want the sub resource parent here, since the view itself may be
1567 * internal to wined3d and may not have a parent. */
1568 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1569 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1570 IDirect3DSurface9_AddRef(*depth_stencil);
1572 else
1574 hr = WINED3DERR_NOTFOUND;
1575 *depth_stencil = NULL;
1577 wined3d_mutex_unlock();
1579 return hr;
1582 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1584 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1585 HRESULT hr;
1587 TRACE("iface %p.\n", iface);
1589 wined3d_mutex_lock();
1590 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1591 device->in_scene = TRUE;
1592 wined3d_mutex_unlock();
1594 return hr;
1597 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1599 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1600 HRESULT hr;
1602 TRACE("iface %p.\n", iface);
1604 wined3d_mutex_lock();
1605 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1606 device->in_scene = FALSE;
1607 wined3d_mutex_unlock();
1609 return hr;
1612 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1613 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1615 const struct wined3d_color c =
1617 ((color >> 16) & 0xff) / 255.0f,
1618 ((color >> 8) & 0xff) / 255.0f,
1619 (color & 0xff) / 255.0f,
1620 ((color >> 24) & 0xff) / 255.0f,
1622 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1623 HRESULT hr;
1625 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1626 iface, rect_count, rects, flags, color, z, stencil);
1628 wined3d_mutex_lock();
1629 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1630 wined3d_mutex_unlock();
1632 return hr;
1635 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1636 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1638 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1640 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1642 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1643 wined3d_mutex_lock();
1644 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1645 wined3d_mutex_unlock();
1647 return D3D_OK;
1650 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1651 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1653 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1655 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1657 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1658 wined3d_mutex_lock();
1659 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1660 wined3d_mutex_unlock();
1662 return D3D_OK;
1665 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1666 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1668 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1670 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1672 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1673 wined3d_mutex_lock();
1674 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1675 wined3d_mutex_unlock();
1677 return D3D_OK;
1680 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1682 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1684 TRACE("iface %p, viewport %p.\n", iface, viewport);
1686 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1687 wined3d_mutex_lock();
1688 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1689 wined3d_mutex_unlock();
1691 return D3D_OK;
1694 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1696 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1698 TRACE("iface %p, viewport %p.\n", iface, viewport);
1700 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1701 wined3d_mutex_lock();
1702 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1703 wined3d_mutex_unlock();
1705 return D3D_OK;
1708 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1710 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1712 TRACE("iface %p, material %p.\n", iface, material);
1714 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1715 wined3d_mutex_lock();
1716 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1717 wined3d_mutex_unlock();
1719 return D3D_OK;
1722 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1724 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1726 TRACE("iface %p, material %p.\n", iface, material);
1728 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1729 wined3d_mutex_lock();
1730 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1731 wined3d_mutex_unlock();
1733 return D3D_OK;
1736 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1738 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1739 HRESULT hr;
1741 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1743 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1744 wined3d_mutex_lock();
1745 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1746 wined3d_mutex_unlock();
1748 return hr;
1751 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1754 HRESULT hr;
1756 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1758 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1759 wined3d_mutex_lock();
1760 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1761 wined3d_mutex_unlock();
1763 return hr;
1766 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1768 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1769 HRESULT hr;
1771 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1773 wined3d_mutex_lock();
1774 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1775 wined3d_mutex_unlock();
1777 return hr;
1780 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1782 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1783 HRESULT hr;
1785 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1787 wined3d_mutex_lock();
1788 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1789 wined3d_mutex_unlock();
1791 return hr;
1794 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1796 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1797 HRESULT hr;
1799 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1801 wined3d_mutex_lock();
1802 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1803 wined3d_mutex_unlock();
1805 return hr;
1808 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1810 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1811 HRESULT hr;
1813 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1815 wined3d_mutex_lock();
1816 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1817 wined3d_mutex_unlock();
1819 return hr;
1822 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1823 D3DRENDERSTATETYPE state, DWORD value)
1825 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1827 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1829 wined3d_mutex_lock();
1830 wined3d_device_set_render_state(device->wined3d_device, state, value);
1831 wined3d_mutex_unlock();
1833 return D3D_OK;
1836 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1837 D3DRENDERSTATETYPE state, DWORD *value)
1839 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1841 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1843 wined3d_mutex_lock();
1844 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1845 wined3d_mutex_unlock();
1847 return D3D_OK;
1850 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1851 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1853 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1854 struct d3d9_stateblock *object;
1855 HRESULT hr;
1857 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1859 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1861 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1862 return D3DERR_INVALIDCALL;
1865 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1866 if (!object)
1867 return E_OUTOFMEMORY;
1869 hr = stateblock_init(object, device, type, NULL);
1870 if (FAILED(hr))
1872 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1873 HeapFree(GetProcessHeap(), 0, object);
1874 return hr;
1877 TRACE("Created stateblock %p.\n", object);
1878 *stateblock = &object->IDirect3DStateBlock9_iface;
1880 return D3D_OK;
1883 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1885 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1886 HRESULT hr;
1888 TRACE("iface %p.\n", iface);
1890 wined3d_mutex_lock();
1891 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1892 wined3d_mutex_unlock();
1894 return hr;
1897 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1899 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1900 struct wined3d_stateblock *wined3d_stateblock;
1901 struct d3d9_stateblock *object;
1902 HRESULT hr;
1904 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1906 wined3d_mutex_lock();
1907 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1908 wined3d_mutex_unlock();
1909 if (FAILED(hr))
1911 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1912 return hr;
1915 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1916 if (!object)
1918 wined3d_mutex_lock();
1919 wined3d_stateblock_decref(wined3d_stateblock);
1920 wined3d_mutex_unlock();
1921 return E_OUTOFMEMORY;
1924 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1925 if (FAILED(hr))
1927 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1928 wined3d_mutex_lock();
1929 wined3d_stateblock_decref(wined3d_stateblock);
1930 wined3d_mutex_unlock();
1931 HeapFree(GetProcessHeap(), 0, object);
1932 return hr;
1935 TRACE("Created stateblock %p.\n", object);
1936 *stateblock = &object->IDirect3DStateBlock9_iface;
1938 return D3D_OK;
1941 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1944 HRESULT hr;
1946 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1948 wined3d_mutex_lock();
1949 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1950 wined3d_mutex_unlock();
1952 return hr;
1955 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1958 HRESULT hr;
1960 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1962 wined3d_mutex_lock();
1963 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1964 wined3d_mutex_unlock();
1966 return hr;
1969 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1972 struct wined3d_texture *wined3d_texture = NULL;
1973 struct d3d9_texture *texture_impl;
1975 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1977 if (!texture)
1978 return D3DERR_INVALIDCALL;
1980 wined3d_mutex_lock();
1981 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1983 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1984 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1985 IDirect3DBaseTexture9_AddRef(*texture);
1987 else
1989 *texture = NULL;
1991 wined3d_mutex_unlock();
1993 return D3D_OK;
1996 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1998 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1999 struct d3d9_texture *texture_impl;
2000 HRESULT hr;
2002 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2004 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2006 wined3d_mutex_lock();
2007 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2008 texture_impl ? texture_impl->wined3d_texture : NULL);
2009 wined3d_mutex_unlock();
2011 return hr;
2014 static const enum wined3d_texture_stage_state tss_lookup[] =
2016 WINED3D_TSS_INVALID, /* 0, unused */
2017 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2018 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2019 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2020 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2021 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2022 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2023 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2024 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2025 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2026 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2027 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2028 WINED3D_TSS_INVALID, /* 12, unused */
2029 WINED3D_TSS_INVALID, /* 13, unused */
2030 WINED3D_TSS_INVALID, /* 14, unused */
2031 WINED3D_TSS_INVALID, /* 15, unused */
2032 WINED3D_TSS_INVALID, /* 16, unused */
2033 WINED3D_TSS_INVALID, /* 17, unused */
2034 WINED3D_TSS_INVALID, /* 18, unused */
2035 WINED3D_TSS_INVALID, /* 19, unused */
2036 WINED3D_TSS_INVALID, /* 20, unused */
2037 WINED3D_TSS_INVALID, /* 21, unused */
2038 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2039 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2040 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2041 WINED3D_TSS_INVALID, /* 25, unused */
2042 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2043 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2044 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2045 WINED3D_TSS_INVALID, /* 29, unused */
2046 WINED3D_TSS_INVALID, /* 30, unused */
2047 WINED3D_TSS_INVALID, /* 31, unused */
2048 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2051 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2052 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2054 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2056 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2058 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2060 WARN("Invalid state %#x passed.\n", state);
2061 return D3D_OK;
2064 wined3d_mutex_lock();
2065 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2066 wined3d_mutex_unlock();
2068 return D3D_OK;
2071 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2072 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2074 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2076 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2078 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2080 WARN("Invalid state %#x passed.\n", state);
2081 return D3D_OK;
2084 wined3d_mutex_lock();
2085 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2086 wined3d_mutex_unlock();
2088 return D3D_OK;
2091 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2092 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2094 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2096 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2098 wined3d_mutex_lock();
2099 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2100 wined3d_mutex_unlock();
2102 return D3D_OK;
2105 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2106 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2108 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2110 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2112 wined3d_mutex_lock();
2113 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2114 wined3d_mutex_unlock();
2116 return D3D_OK;
2119 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2121 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2122 HRESULT hr;
2124 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2126 wined3d_mutex_lock();
2127 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2128 wined3d_mutex_unlock();
2130 return hr;
2133 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2134 UINT palette_idx, const PALETTEENTRY *entries)
2136 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2138 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2139 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2140 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2142 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2143 return D3D_OK;
2146 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2147 UINT palette_idx, PALETTEENTRY *entries)
2149 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2151 return D3DERR_INVALIDCALL;
2154 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2156 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2158 return D3D_OK;
2161 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2163 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2165 return D3DERR_INVALIDCALL;
2168 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2170 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2172 TRACE("iface %p, rect %p.\n", iface, rect);
2174 wined3d_mutex_lock();
2175 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2176 wined3d_mutex_unlock();
2178 return D3D_OK;
2181 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2183 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2185 TRACE("iface %p, rect %p.\n", iface, rect);
2187 wined3d_mutex_lock();
2188 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2189 wined3d_mutex_unlock();
2191 return D3D_OK;
2194 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2196 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2198 TRACE("iface %p, software %#x.\n", iface, software);
2200 wined3d_mutex_lock();
2201 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2202 wined3d_mutex_unlock();
2204 return D3D_OK;
2207 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2209 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2210 BOOL ret;
2212 TRACE("iface %p.\n", iface);
2214 wined3d_mutex_lock();
2215 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2216 wined3d_mutex_unlock();
2218 return ret;
2221 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2223 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2224 HRESULT hr;
2226 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2228 wined3d_mutex_lock();
2229 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2230 wined3d_mutex_unlock();
2232 return hr;
2235 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2237 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2238 float ret;
2240 TRACE("iface %p.\n", iface);
2242 wined3d_mutex_lock();
2243 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2244 wined3d_mutex_unlock();
2246 return ret;
2249 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2250 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2252 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2253 HRESULT hr;
2255 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2256 iface, primitive_type, start_vertex, primitive_count);
2258 wined3d_mutex_lock();
2259 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2260 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2261 vertex_count_from_primitive_count(primitive_type, primitive_count));
2262 wined3d_mutex_unlock();
2264 return hr;
2267 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2268 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2269 UINT vertex_count, UINT start_idx, UINT primitive_count)
2271 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2272 HRESULT hr;
2274 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2275 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2276 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2277 vertex_count, start_idx, primitive_count);
2279 wined3d_mutex_lock();
2280 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2281 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2282 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2283 vertex_count_from_primitive_count(primitive_type, primitive_count));
2284 wined3d_mutex_unlock();
2286 return hr;
2289 /* The caller is responsible for wined3d locking */
2290 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2292 HRESULT hr;
2294 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2296 UINT size = max(device->vertex_buffer_size * 2, min_size);
2297 struct wined3d_buffer *buffer;
2299 TRACE("Growing vertex buffer to %u bytes\n", size);
2301 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2302 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2303 if (FAILED(hr))
2305 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2306 return hr;
2309 if (device->vertex_buffer)
2310 wined3d_buffer_decref(device->vertex_buffer);
2312 device->vertex_buffer = buffer;
2313 device->vertex_buffer_size = size;
2314 device->vertex_buffer_pos = 0;
2316 return D3D_OK;
2319 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2320 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2322 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2323 HRESULT hr;
2324 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2325 UINT size = vtx_count * stride;
2326 UINT vb_pos, align;
2327 BYTE *buffer_data;
2329 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2330 iface, primitive_type, primitive_count, data, stride);
2332 if (!primitive_count)
2334 WARN("primitive_count is 0, returning D3D_OK\n");
2335 return D3D_OK;
2338 wined3d_mutex_lock();
2340 hr = d3d9_device_prepare_vertex_buffer(device, size);
2341 if (FAILED(hr))
2342 goto done;
2344 vb_pos = device->vertex_buffer_pos;
2345 align = vb_pos % stride;
2346 if (align) align = stride - align;
2347 if (vb_pos + size + align > device->vertex_buffer_size)
2348 vb_pos = 0;
2349 else
2350 vb_pos += align;
2352 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2353 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2354 if (FAILED(hr))
2355 goto done;
2356 memcpy(buffer_data, data, size);
2357 wined3d_buffer_unmap(device->vertex_buffer);
2358 device->vertex_buffer_pos = vb_pos + size;
2360 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2361 if (FAILED(hr))
2362 goto done;
2364 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2365 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2366 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2368 done:
2369 wined3d_mutex_unlock();
2370 return hr;
2373 /* The caller is responsible for wined3d locking */
2374 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2376 HRESULT hr;
2378 if (device->index_buffer_size < min_size || !device->index_buffer)
2380 UINT size = max(device->index_buffer_size * 2, min_size);
2381 struct wined3d_buffer *buffer;
2383 TRACE("Growing index buffer to %u bytes\n", size);
2385 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2386 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2387 if (FAILED(hr))
2389 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2390 return hr;
2393 if (device->index_buffer)
2394 wined3d_buffer_decref(device->index_buffer);
2396 device->index_buffer = buffer;
2397 device->index_buffer_size = size;
2398 device->index_buffer_pos = 0;
2400 return D3D_OK;
2403 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2404 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2405 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2406 const void *vertex_data, UINT vertex_stride)
2408 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2409 HRESULT hr;
2410 BYTE *buffer_data;
2412 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2413 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2414 UINT idx_size = idx_count * idx_fmt_size;
2415 UINT ib_pos;
2417 UINT vtx_size = vertex_count * vertex_stride;
2418 UINT vb_pos, align;
2420 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2421 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2422 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2423 index_data, index_format, vertex_data, vertex_stride);
2425 if (!primitive_count)
2427 WARN("primitive_count is 0, returning D3D_OK\n");
2428 return D3D_OK;
2431 wined3d_mutex_lock();
2433 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2434 if (FAILED(hr))
2435 goto done;
2437 vb_pos = device->vertex_buffer_pos;
2438 align = vb_pos % vertex_stride;
2439 if (align) align = vertex_stride - align;
2440 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2441 vb_pos = 0;
2442 else
2443 vb_pos += align;
2445 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2446 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2447 if (FAILED(hr))
2448 goto done;
2449 memcpy(buffer_data, vertex_data, vtx_size);
2450 wined3d_buffer_unmap(device->vertex_buffer);
2451 device->vertex_buffer_pos = vb_pos + vtx_size;
2453 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2454 if (FAILED(hr))
2455 goto done;
2457 ib_pos = device->index_buffer_pos;
2458 align = ib_pos % idx_fmt_size;
2459 if (align) align = idx_fmt_size - align;
2460 if (ib_pos + idx_size + align > device->index_buffer_size)
2461 ib_pos = 0;
2462 else
2463 ib_pos += align;
2465 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2466 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2467 if (FAILED(hr))
2468 goto done;
2469 memcpy(buffer_data, index_data, idx_size);
2470 wined3d_buffer_unmap(device->index_buffer);
2471 device->index_buffer_pos = ib_pos + idx_size;
2473 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2474 if (FAILED(hr))
2475 goto done;
2477 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2478 wined3dformat_from_d3dformat(index_format));
2479 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2481 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2482 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2484 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2485 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2486 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2488 done:
2489 wined3d_mutex_unlock();
2490 return hr;
2493 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2494 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2495 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2497 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2498 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2499 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2500 HRESULT hr;
2502 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2503 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2505 wined3d_mutex_lock();
2506 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2507 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2508 flags, dst_impl->fvf);
2509 wined3d_mutex_unlock();
2511 return hr;
2514 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2515 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2517 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2518 struct d3d9_vertex_declaration *object;
2519 HRESULT hr;
2521 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2523 if (!declaration)
2525 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2526 return D3DERR_INVALIDCALL;
2529 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2530 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2532 return hr;
2535 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2536 IDirect3DVertexDeclaration9 *declaration)
2538 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2539 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2541 TRACE("iface %p, declaration %p.\n", iface, declaration);
2543 wined3d_mutex_lock();
2544 wined3d_device_set_vertex_declaration(device->wined3d_device,
2545 decl_impl ? decl_impl->wined3d_declaration : NULL);
2546 wined3d_mutex_unlock();
2548 return D3D_OK;
2551 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2552 IDirect3DVertexDeclaration9 **declaration)
2554 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2555 struct wined3d_vertex_declaration *wined3d_declaration;
2556 struct d3d9_vertex_declaration *declaration_impl;
2558 TRACE("iface %p, declaration %p.\n", iface, declaration);
2560 if (!declaration) return D3DERR_INVALIDCALL;
2562 wined3d_mutex_lock();
2563 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2565 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2566 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2567 IDirect3DVertexDeclaration9_AddRef(*declaration);
2569 else
2571 *declaration = NULL;
2573 wined3d_mutex_unlock();
2575 TRACE("Returning %p.\n", *declaration);
2576 return D3D_OK;
2579 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2581 struct wined3d_vertex_declaration *wined3d_declaration;
2582 struct fvf_declaration *fvf_decls = device->fvf_decls;
2583 struct d3d9_vertex_declaration *d3d9_declaration;
2584 D3DVERTEXELEMENT9 *elements;
2585 int p, low, high; /* deliberately signed */
2586 HRESULT hr;
2588 TRACE("Searching for declaration for fvf %08x... ", fvf);
2590 low = 0;
2591 high = device->fvf_decl_count - 1;
2592 while (low <= high)
2594 p = (low + high) >> 1;
2595 TRACE("%d ", p);
2597 if (fvf_decls[p].fvf == fvf)
2599 TRACE("found %p.\n", fvf_decls[p].decl);
2600 return fvf_decls[p].decl;
2603 if (fvf_decls[p].fvf < fvf)
2604 low = p + 1;
2605 else
2606 high = p - 1;
2608 TRACE("not found. Creating and inserting at position %d.\n", low);
2610 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2611 return NULL;
2613 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2614 HeapFree(GetProcessHeap(), 0, elements);
2615 if (FAILED(hr))
2616 return NULL;
2618 if (device->fvf_decl_size == device->fvf_decl_count)
2620 UINT grow = max(device->fvf_decl_size / 2, 8);
2622 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2623 if (!fvf_decls)
2625 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2626 return NULL;
2628 device->fvf_decls = fvf_decls;
2629 device->fvf_decl_size += grow;
2632 d3d9_declaration->fvf = fvf;
2633 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2634 wined3d_vertex_declaration_incref(wined3d_declaration);
2635 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2637 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2638 fvf_decls[low].decl = wined3d_declaration;
2639 fvf_decls[low].fvf = fvf;
2640 ++device->fvf_decl_count;
2642 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2644 return wined3d_declaration;
2647 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2649 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2650 struct wined3d_vertex_declaration *decl;
2652 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2654 if (!fvf)
2656 WARN("%#x is not a valid FVF.\n", fvf);
2657 return D3D_OK;
2660 wined3d_mutex_lock();
2661 if (!(decl = device_get_fvf_declaration(device, fvf)))
2663 wined3d_mutex_unlock();
2664 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2665 return D3DERR_DRIVERINTERNALERROR;
2668 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2669 wined3d_mutex_unlock();
2671 return D3D_OK;
2674 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2676 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2677 struct wined3d_vertex_declaration *wined3d_declaration;
2678 struct d3d9_vertex_declaration *d3d9_declaration;
2680 TRACE("iface %p, fvf %p.\n", iface, fvf);
2682 wined3d_mutex_lock();
2683 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2685 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2686 *fvf = d3d9_declaration->fvf;
2688 else
2690 *fvf = 0;
2692 wined3d_mutex_unlock();
2694 TRACE("Returning FVF %#x.\n", *fvf);
2696 return D3D_OK;
2699 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2700 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2702 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2703 struct d3d9_vertexshader *object;
2704 HRESULT hr;
2706 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2708 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2709 if (!object)
2710 return E_OUTOFMEMORY;
2712 hr = vertexshader_init(object, device, byte_code);
2713 if (FAILED(hr))
2715 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2716 HeapFree(GetProcessHeap(), 0, object);
2717 return hr;
2720 TRACE("Created vertex shader %p.\n", object);
2721 *shader = &object->IDirect3DVertexShader9_iface;
2723 return D3D_OK;
2726 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2728 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2729 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2731 TRACE("iface %p, shader %p.\n", iface, shader);
2733 wined3d_mutex_lock();
2734 wined3d_device_set_vertex_shader(device->wined3d_device,
2735 shader_obj ? shader_obj->wined3d_shader : NULL);
2736 wined3d_mutex_unlock();
2738 return D3D_OK;
2741 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2744 struct d3d9_vertexshader *shader_impl;
2745 struct wined3d_shader *wined3d_shader;
2747 TRACE("iface %p, shader %p.\n", iface, shader);
2749 wined3d_mutex_lock();
2750 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2752 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2753 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2754 IDirect3DVertexShader9_AddRef(*shader);
2756 else
2758 *shader = NULL;
2760 wined3d_mutex_unlock();
2762 TRACE("Returning %p.\n", *shader);
2764 return D3D_OK;
2767 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2768 UINT reg_idx, const float *data, UINT count)
2770 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2771 HRESULT hr;
2773 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2775 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2777 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2778 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2779 return D3DERR_INVALIDCALL;
2782 wined3d_mutex_lock();
2783 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2784 wined3d_mutex_unlock();
2786 return hr;
2789 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2790 UINT reg_idx, float *data, UINT count)
2792 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2793 HRESULT hr;
2795 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2797 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2799 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2800 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2801 return D3DERR_INVALIDCALL;
2804 wined3d_mutex_lock();
2805 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2806 wined3d_mutex_unlock();
2808 return hr;
2811 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2812 UINT reg_idx, const int *data, UINT count)
2814 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2815 HRESULT hr;
2817 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2819 wined3d_mutex_lock();
2820 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2821 wined3d_mutex_unlock();
2823 return hr;
2826 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2827 UINT reg_idx, int *data, UINT count)
2829 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2830 HRESULT hr;
2832 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2834 wined3d_mutex_lock();
2835 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2836 wined3d_mutex_unlock();
2838 return hr;
2841 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2842 UINT reg_idx, const BOOL *data, UINT count)
2844 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2845 HRESULT hr;
2847 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2849 wined3d_mutex_lock();
2850 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2851 wined3d_mutex_unlock();
2853 return hr;
2856 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2857 UINT reg_idx, BOOL *data, UINT count)
2859 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2860 HRESULT hr;
2862 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2864 wined3d_mutex_lock();
2865 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2866 wined3d_mutex_unlock();
2868 return hr;
2871 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2872 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2874 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2875 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2876 HRESULT hr;
2878 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2879 iface, stream_idx, buffer, offset, stride);
2881 wined3d_mutex_lock();
2882 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2883 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2884 wined3d_mutex_unlock();
2886 return hr;
2889 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2890 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2892 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2893 struct d3d9_vertexbuffer *buffer_impl;
2894 struct wined3d_buffer *wined3d_buffer;
2895 HRESULT hr;
2897 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2898 iface, stream_idx, buffer, offset, stride);
2900 if (!buffer)
2901 return D3DERR_INVALIDCALL;
2903 wined3d_mutex_lock();
2904 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2905 if (SUCCEEDED(hr) && wined3d_buffer)
2907 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2908 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2909 IDirect3DVertexBuffer9_AddRef(*buffer);
2911 else
2913 if (FAILED(hr))
2914 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2915 *buffer = NULL;
2917 wined3d_mutex_unlock();
2919 return hr;
2922 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2924 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2925 HRESULT hr;
2927 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2929 wined3d_mutex_lock();
2930 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2931 wined3d_mutex_unlock();
2933 return hr;
2936 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2938 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2939 HRESULT hr;
2941 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2943 wined3d_mutex_lock();
2944 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2945 wined3d_mutex_unlock();
2947 return hr;
2950 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2952 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2953 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2955 TRACE("iface %p, buffer %p.\n", iface, buffer);
2957 wined3d_mutex_lock();
2958 wined3d_device_set_index_buffer(device->wined3d_device,
2959 ib ? ib->wined3d_buffer : NULL,
2960 ib ? ib->format : WINED3DFMT_UNKNOWN);
2961 wined3d_mutex_unlock();
2963 return D3D_OK;
2966 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2968 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2969 enum wined3d_format_id wined3d_format;
2970 struct wined3d_buffer *wined3d_buffer;
2971 struct d3d9_indexbuffer *buffer_impl;
2973 TRACE("iface %p, buffer %p.\n", iface, buffer);
2975 if (!buffer)
2976 return D3DERR_INVALIDCALL;
2978 wined3d_mutex_lock();
2979 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2981 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2982 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2983 IDirect3DIndexBuffer9_AddRef(*buffer);
2985 else
2987 *buffer = NULL;
2989 wined3d_mutex_unlock();
2991 return D3D_OK;
2994 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2995 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2997 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2998 struct d3d9_pixelshader *object;
2999 HRESULT hr;
3001 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3003 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3004 if (!object)
3006 FIXME("Failed to allocate pixel shader memory.\n");
3007 return E_OUTOFMEMORY;
3010 hr = pixelshader_init(object, device, byte_code);
3011 if (FAILED(hr))
3013 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3014 HeapFree(GetProcessHeap(), 0, object);
3015 return hr;
3018 TRACE("Created pixel shader %p.\n", object);
3019 *shader = &object->IDirect3DPixelShader9_iface;
3021 return D3D_OK;
3024 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3026 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3027 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3029 TRACE("iface %p, shader %p.\n", iface, shader);
3031 wined3d_mutex_lock();
3032 wined3d_device_set_pixel_shader(device->wined3d_device,
3033 shader_obj ? shader_obj->wined3d_shader : NULL);
3034 wined3d_mutex_unlock();
3036 return D3D_OK;
3039 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3041 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3042 struct d3d9_pixelshader *shader_impl;
3043 struct wined3d_shader *wined3d_shader;
3045 TRACE("iface %p, shader %p.\n", iface, shader);
3047 if (!shader) return D3DERR_INVALIDCALL;
3049 wined3d_mutex_lock();
3050 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3052 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3053 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3054 IDirect3DPixelShader9_AddRef(*shader);
3056 else
3058 *shader = NULL;
3060 wined3d_mutex_unlock();
3062 TRACE("Returning %p.\n", *shader);
3064 return D3D_OK;
3067 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3068 UINT reg_idx, const float *data, UINT count)
3070 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3071 HRESULT hr;
3073 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3075 wined3d_mutex_lock();
3076 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3077 wined3d_mutex_unlock();
3079 return hr;
3082 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3083 UINT reg_idx, float *data, UINT count)
3085 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3086 HRESULT hr;
3088 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3090 wined3d_mutex_lock();
3091 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3092 wined3d_mutex_unlock();
3094 return hr;
3097 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3098 UINT reg_idx, const int *data, UINT count)
3100 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3101 HRESULT hr;
3103 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3105 wined3d_mutex_lock();
3106 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3107 wined3d_mutex_unlock();
3109 return hr;
3112 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3113 UINT reg_idx, int *data, UINT count)
3115 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3116 HRESULT hr;
3118 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3120 wined3d_mutex_lock();
3121 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3122 wined3d_mutex_unlock();
3124 return hr;
3127 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3128 UINT reg_idx, const BOOL *data, UINT count)
3130 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3131 HRESULT hr;
3133 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3135 wined3d_mutex_lock();
3136 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3137 wined3d_mutex_unlock();
3139 return hr;
3142 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3143 UINT reg_idx, BOOL *data, UINT count)
3145 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3146 HRESULT hr;
3148 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3150 wined3d_mutex_lock();
3151 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3152 wined3d_mutex_unlock();
3154 return hr;
3157 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3158 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3160 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3161 iface, handle, segment_count, patch_info);
3162 return D3D_OK;
3165 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3166 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3168 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3169 iface, handle, segment_count, patch_info);
3170 return D3D_OK;
3173 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3175 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3176 return D3DERR_INVALIDCALL;
3179 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3181 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3182 struct d3d9_query *object;
3183 HRESULT hr;
3185 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3187 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3188 if (!object)
3189 return E_OUTOFMEMORY;
3191 hr = query_init(object, device, type);
3192 if (FAILED(hr))
3194 WARN("Failed to initialize query, hr %#x.\n", hr);
3195 HeapFree(GetProcessHeap(), 0, object);
3196 return hr;
3199 TRACE("Created query %p.\n", object);
3200 if (query) *query = &object->IDirect3DQuery9_iface;
3201 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3203 return D3D_OK;
3206 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3207 UINT width, UINT height, float *rows, float *columns)
3209 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3210 iface, width, height, rows, columns);
3212 return E_NOTIMPL;
3215 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3216 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3217 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3219 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3220 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3221 iface, src_surface, dst_surface, src_descs, rect_count,
3222 dst_descs, operation, offset_x, offset_y);
3224 return E_NOTIMPL;
3227 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3228 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3229 const RGNDATA *dirty_region, DWORD flags)
3231 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3232 UINT i;
3233 HRESULT hr;
3235 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3236 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3237 dst_window_override, dirty_region, flags);
3239 if (device->device_state != D3D9_DEVICE_STATE_OK)
3240 return S_PRESENT_OCCLUDED;
3242 wined3d_mutex_lock();
3243 for (i = 0; i < device->implicit_swapchain_count; ++i)
3245 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain, src_rect,
3246 dst_rect, dst_window_override, dirty_region, flags)))
3248 wined3d_mutex_unlock();
3249 return hr;
3252 wined3d_mutex_unlock();
3254 return D3D_OK;
3257 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3259 FIXME("iface %p, priority %p stub!\n", iface, priority);
3261 return E_NOTIMPL;
3264 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3266 FIXME("iface %p, priority %d stub!\n", iface, priority);
3268 return E_NOTIMPL;
3271 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3273 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3275 return E_NOTIMPL;
3278 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3279 IDirect3DResource9 **resources, UINT32 resource_count)
3281 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3282 iface, resources, resource_count);
3284 return E_NOTIMPL;
3287 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3289 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3291 return E_NOTIMPL;
3294 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3296 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3298 *max_latency = 2;
3300 return E_NOTIMPL;
3303 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3305 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3306 struct wined3d_swapchain_desc swapchain_desc;
3308 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3310 wined3d_mutex_lock();
3311 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3312 wined3d_mutex_unlock();
3314 if (swapchain_desc.windowed)
3315 return D3D_OK;
3317 /* FIXME: This is actually supposed to check if any other device is in
3318 * fullscreen mode. */
3319 if (dst_window != swapchain_desc.device_window)
3320 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3322 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3325 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3326 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3327 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3329 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3330 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3331 iface, width, height, format, multisample_type, multisample_quality,
3332 lockable, surface, shared_handle, usage);
3334 *surface = NULL;
3335 if (shared_handle)
3336 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3338 return E_NOTIMPL;
3341 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3342 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3343 HANDLE *shared_handle, DWORD usage)
3345 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3346 iface, width, height, format, pool, surface, shared_handle, usage);
3348 return E_NOTIMPL;
3351 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3352 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3353 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3355 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3356 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3357 iface, width, height, format, multisample_type, multisample_quality,
3358 discard, surface, shared_handle, usage);
3360 *surface = NULL;
3361 if (shared_handle)
3362 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3364 return E_NOTIMPL;
3367 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3368 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3370 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3372 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3374 return d3d9_device_reset(device, present_parameters, mode);
3377 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3378 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3380 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3381 struct wined3d_display_mode wined3d_mode;
3382 HRESULT hr;
3384 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3385 iface, swapchain_idx, mode, rotation);
3387 if (mode->Size != sizeof(*mode))
3388 return D3DERR_INVALIDCALL;
3390 wined3d_mutex_lock();
3391 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3392 (enum wined3d_display_rotation *)rotation);
3393 wined3d_mutex_unlock();
3395 if (SUCCEEDED(hr))
3397 mode->Width = wined3d_mode.width;
3398 mode->Height = wined3d_mode.height;
3399 mode->RefreshRate = wined3d_mode.refresh_rate;
3400 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3401 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3404 return hr;
3407 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3409 /* IUnknown */
3410 d3d9_device_QueryInterface,
3411 d3d9_device_AddRef,
3412 d3d9_device_Release,
3413 /* IDirect3DDevice9 */
3414 d3d9_device_TestCooperativeLevel,
3415 d3d9_device_GetAvailableTextureMem,
3416 d3d9_device_EvictManagedResources,
3417 d3d9_device_GetDirect3D,
3418 d3d9_device_GetDeviceCaps,
3419 d3d9_device_GetDisplayMode,
3420 d3d9_device_GetCreationParameters,
3421 d3d9_device_SetCursorProperties,
3422 d3d9_device_SetCursorPosition,
3423 d3d9_device_ShowCursor,
3424 d3d9_device_CreateAdditionalSwapChain,
3425 d3d9_device_GetSwapChain,
3426 d3d9_device_GetNumberOfSwapChains,
3427 d3d9_device_Reset,
3428 d3d9_device_Present,
3429 d3d9_device_GetBackBuffer,
3430 d3d9_device_GetRasterStatus,
3431 d3d9_device_SetDialogBoxMode,
3432 d3d9_device_SetGammaRamp,
3433 d3d9_device_GetGammaRamp,
3434 d3d9_device_CreateTexture,
3435 d3d9_device_CreateVolumeTexture,
3436 d3d9_device_CreateCubeTexture,
3437 d3d9_device_CreateVertexBuffer,
3438 d3d9_device_CreateIndexBuffer,
3439 d3d9_device_CreateRenderTarget,
3440 d3d9_device_CreateDepthStencilSurface,
3441 d3d9_device_UpdateSurface,
3442 d3d9_device_UpdateTexture,
3443 d3d9_device_GetRenderTargetData,
3444 d3d9_device_GetFrontBufferData,
3445 d3d9_device_StretchRect,
3446 d3d9_device_ColorFill,
3447 d3d9_device_CreateOffscreenPlainSurface,
3448 d3d9_device_SetRenderTarget,
3449 d3d9_device_GetRenderTarget,
3450 d3d9_device_SetDepthStencilSurface,
3451 d3d9_device_GetDepthStencilSurface,
3452 d3d9_device_BeginScene,
3453 d3d9_device_EndScene,
3454 d3d9_device_Clear,
3455 d3d9_device_SetTransform,
3456 d3d9_device_GetTransform,
3457 d3d9_device_MultiplyTransform,
3458 d3d9_device_SetViewport,
3459 d3d9_device_GetViewport,
3460 d3d9_device_SetMaterial,
3461 d3d9_device_GetMaterial,
3462 d3d9_device_SetLight,
3463 d3d9_device_GetLight,
3464 d3d9_device_LightEnable,
3465 d3d9_device_GetLightEnable,
3466 d3d9_device_SetClipPlane,
3467 d3d9_device_GetClipPlane,
3468 d3d9_device_SetRenderState,
3469 d3d9_device_GetRenderState,
3470 d3d9_device_CreateStateBlock,
3471 d3d9_device_BeginStateBlock,
3472 d3d9_device_EndStateBlock,
3473 d3d9_device_SetClipStatus,
3474 d3d9_device_GetClipStatus,
3475 d3d9_device_GetTexture,
3476 d3d9_device_SetTexture,
3477 d3d9_device_GetTextureStageState,
3478 d3d9_device_SetTextureStageState,
3479 d3d9_device_GetSamplerState,
3480 d3d9_device_SetSamplerState,
3481 d3d9_device_ValidateDevice,
3482 d3d9_device_SetPaletteEntries,
3483 d3d9_device_GetPaletteEntries,
3484 d3d9_device_SetCurrentTexturePalette,
3485 d3d9_device_GetCurrentTexturePalette,
3486 d3d9_device_SetScissorRect,
3487 d3d9_device_GetScissorRect,
3488 d3d9_device_SetSoftwareVertexProcessing,
3489 d3d9_device_GetSoftwareVertexProcessing,
3490 d3d9_device_SetNPatchMode,
3491 d3d9_device_GetNPatchMode,
3492 d3d9_device_DrawPrimitive,
3493 d3d9_device_DrawIndexedPrimitive,
3494 d3d9_device_DrawPrimitiveUP,
3495 d3d9_device_DrawIndexedPrimitiveUP,
3496 d3d9_device_ProcessVertices,
3497 d3d9_device_CreateVertexDeclaration,
3498 d3d9_device_SetVertexDeclaration,
3499 d3d9_device_GetVertexDeclaration,
3500 d3d9_device_SetFVF,
3501 d3d9_device_GetFVF,
3502 d3d9_device_CreateVertexShader,
3503 d3d9_device_SetVertexShader,
3504 d3d9_device_GetVertexShader,
3505 d3d9_device_SetVertexShaderConstantF,
3506 d3d9_device_GetVertexShaderConstantF,
3507 d3d9_device_SetVertexShaderConstantI,
3508 d3d9_device_GetVertexShaderConstantI,
3509 d3d9_device_SetVertexShaderConstantB,
3510 d3d9_device_GetVertexShaderConstantB,
3511 d3d9_device_SetStreamSource,
3512 d3d9_device_GetStreamSource,
3513 d3d9_device_SetStreamSourceFreq,
3514 d3d9_device_GetStreamSourceFreq,
3515 d3d9_device_SetIndices,
3516 d3d9_device_GetIndices,
3517 d3d9_device_CreatePixelShader,
3518 d3d9_device_SetPixelShader,
3519 d3d9_device_GetPixelShader,
3520 d3d9_device_SetPixelShaderConstantF,
3521 d3d9_device_GetPixelShaderConstantF,
3522 d3d9_device_SetPixelShaderConstantI,
3523 d3d9_device_GetPixelShaderConstantI,
3524 d3d9_device_SetPixelShaderConstantB,
3525 d3d9_device_GetPixelShaderConstantB,
3526 d3d9_device_DrawRectPatch,
3527 d3d9_device_DrawTriPatch,
3528 d3d9_device_DeletePatch,
3529 d3d9_device_CreateQuery,
3530 /* IDirect3DDevice9Ex */
3531 d3d9_device_SetConvolutionMonoKernel,
3532 d3d9_device_ComposeRects,
3533 d3d9_device_PresentEx,
3534 d3d9_device_GetGPUThreadPriority,
3535 d3d9_device_SetGPUThreadPriority,
3536 d3d9_device_WaitForVBlank,
3537 d3d9_device_CheckResourceResidency,
3538 d3d9_device_SetMaximumFrameLatency,
3539 d3d9_device_GetMaximumFrameLatency,
3540 d3d9_device_CheckDeviceState,
3541 d3d9_device_CreateRenderTargetEx,
3542 d3d9_device_CreateOffscreenPlainSurfaceEx,
3543 d3d9_device_CreateDepthStencilSurfaceEx,
3544 d3d9_device_ResetEx,
3545 d3d9_device_GetDisplayModeEx,
3548 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3550 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3553 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3554 struct wined3d_device *device)
3556 TRACE("device_parent %p, device %p.\n", device_parent, device);
3559 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3561 TRACE("device_parent %p.\n", device_parent);
3564 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3566 struct d3d9_device *device = device_from_device_parent(device_parent);
3568 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3570 if (!device->d3d_parent)
3571 return;
3573 if (!activate)
3574 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3575 else if (device->d3d_parent->extended)
3576 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3577 else
3578 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3581 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3582 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx, struct wined3d_surface *surface, void **parent,
3583 const struct wined3d_parent_ops **parent_ops)
3585 struct d3d9_surface *d3d_surface;
3587 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, surface %p, parent %p, parent_ops %p.\n",
3588 device_parent, wined3d_texture, sub_resource_idx, surface, parent, parent_ops);
3590 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3591 return E_OUTOFMEMORY;
3593 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, surface, parent_ops);
3594 *parent = d3d_surface;
3595 TRACE("Created surface %p.\n", d3d_surface);
3597 return D3D_OK;
3600 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3601 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3602 void **parent, const struct wined3d_parent_ops **parent_ops)
3604 struct d3d9_volume *d3d_volume;
3606 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3607 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3609 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3610 return E_OUTOFMEMORY;
3612 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3613 *parent = d3d_volume;
3614 TRACE("Created volume %p.\n", d3d_volume);
3616 return D3D_OK;
3619 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3620 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3622 struct d3d9_device *device = device_from_device_parent(device_parent);
3623 struct d3d9_surface *d3d_surface;
3624 HRESULT hr;
3626 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3627 device_parent, container_parent, desc, texture);
3629 if (container_parent == device_parent)
3630 container_parent = &device->IDirect3DDevice9Ex_iface;
3632 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1,
3633 WINED3D_SURFACE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3635 WARN("Failed to create texture, hr %#x.\n", hr);
3636 return hr;
3639 d3d_surface = wined3d_resource_get_parent(wined3d_texture_get_sub_resource(*texture, 0));
3640 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3642 return hr;
3645 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3646 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3648 struct d3d9_device *device = device_from_device_parent(device_parent);
3649 struct d3d9_swapchain *d3d_swapchain;
3650 HRESULT hr;
3652 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3654 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3655 if (FAILED(hr))
3657 WARN("Failed to create swapchain, hr %#x.\n", hr);
3658 *swapchain = NULL;
3659 return hr;
3662 *swapchain = d3d_swapchain->wined3d_swapchain;
3663 wined3d_swapchain_incref(*swapchain);
3664 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3666 return hr;
3669 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3671 device_parent_wined3d_device_created,
3672 device_parent_mode_changed,
3673 device_parent_activate,
3674 device_parent_surface_created,
3675 device_parent_volume_created,
3676 device_parent_create_swapchain_texture,
3677 device_parent_create_swapchain,
3680 static void setup_fpu(void)
3682 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3683 WORD cw;
3684 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3685 cw = (cw & ~0xf3f) | 0x3f;
3686 __asm__ volatile ("fldcw %0" : : "m" (cw));
3687 #elif defined(__i386__) && defined(_MSC_VER)
3688 WORD cw;
3689 __asm fnstcw cw;
3690 cw = (cw & ~0xf3f) | 0x3f;
3691 __asm fldcw cw;
3692 #else
3693 FIXME("FPU setup not implemented for this platform.\n");
3694 #endif
3697 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3698 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3699 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3701 struct wined3d_swapchain_desc *swapchain_desc;
3702 UINT i, count = 1;
3703 HRESULT hr;
3705 if (mode)
3706 FIXME("Ignoring display mode.\n");
3708 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3709 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3710 device->refcount = 1;
3712 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3714 wined3d_mutex_lock();
3715 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3716 &device->device_parent, &device->wined3d_device);
3717 if (FAILED(hr))
3719 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3720 wined3d_mutex_unlock();
3721 return hr;
3724 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3726 WINED3DCAPS caps;
3728 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3729 count = caps.NumberOfAdaptersInGroup;
3732 if (flags & D3DCREATE_MULTITHREADED)
3733 wined3d_device_set_multithreaded(device->wined3d_device);
3735 if (!parameters->Windowed)
3737 if (!focus_window)
3738 focus_window = parameters->hDeviceWindow;
3739 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3741 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3742 wined3d_device_decref(device->wined3d_device);
3743 wined3d_mutex_unlock();
3744 return hr;
3747 for (i = 0; i < count; ++i)
3749 HWND device_window = parameters[i].hDeviceWindow;
3751 if (!device_window) device_window = focus_window;
3752 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3753 parameters[i].BackBufferWidth,
3754 parameters[i].BackBufferHeight);
3758 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3759 if (!swapchain_desc)
3761 ERR("Failed to allocate wined3d parameters.\n");
3762 wined3d_device_release_focus_window(device->wined3d_device);
3763 wined3d_device_decref(device->wined3d_device);
3764 wined3d_mutex_unlock();
3765 return E_OUTOFMEMORY;
3768 for (i = 0; i < count; ++i)
3770 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3771 parent->extended))
3773 wined3d_device_release_focus_window(device->wined3d_device);
3774 wined3d_device_decref(device->wined3d_device);
3775 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3776 wined3d_mutex_unlock();
3777 return D3DERR_INVALIDCALL;
3781 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3782 if (FAILED(hr))
3784 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3785 wined3d_device_release_focus_window(device->wined3d_device);
3786 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3787 wined3d_device_decref(device->wined3d_device);
3788 wined3d_mutex_unlock();
3789 return hr;
3792 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3794 wined3d_device_uninit_3d(device->wined3d_device);
3795 wined3d_device_release_focus_window(device->wined3d_device);
3796 wined3d_device_decref(device->wined3d_device);
3797 wined3d_mutex_unlock();
3798 return E_OUTOFMEMORY;
3801 for (i = 0; i < count; ++i)
3803 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3806 wined3d_mutex_unlock();
3808 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3810 /* Initialize the converted declaration array. This creates a valid pointer
3811 * and when adding decls HeapReAlloc() can be used without further checking. */
3812 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3813 if (!device->fvf_decls)
3815 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3816 wined3d_mutex_lock();
3817 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3818 wined3d_device_uninit_3d(device->wined3d_device);
3819 wined3d_device_release_focus_window(device->wined3d_device);
3820 wined3d_device_decref(device->wined3d_device);
3821 wined3d_mutex_unlock();
3822 return E_OUTOFMEMORY;
3825 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3826 device->d3d_parent = parent;
3828 return D3D_OK;