mshtml: Use proper name for OnRedirectVerifyCallback implementation.
[wine.git] / dlls / d3d9 / device.c
blob3a0376dac403029a59617a54b775eca3dd119b0e
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 wined3d_device_uninit_3d(device->wined3d_device);
326 wined3d_device_release_focus_window(device->wined3d_device);
327 wined3d_device_decref(device->wined3d_device);
328 wined3d_mutex_unlock();
330 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
332 HeapFree(GetProcessHeap(), 0, device);
335 return refcount;
338 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
340 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
342 TRACE("iface %p.\n", iface);
344 TRACE("device state: %#x.\n", device->device_state);
346 if (device->d3d_parent->extended)
347 return D3D_OK;
349 switch (device->device_state)
351 default:
352 case D3D9_DEVICE_STATE_OK:
353 return D3D_OK;
354 case D3D9_DEVICE_STATE_LOST:
355 return D3DERR_DEVICELOST;
356 case D3D9_DEVICE_STATE_NOT_RESET:
357 return D3DERR_DEVICENOTRESET;
361 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
363 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
364 UINT ret;
366 TRACE("iface %p.\n", iface);
368 wined3d_mutex_lock();
369 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
370 wined3d_mutex_unlock();
372 return ret;
375 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
377 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
379 TRACE("iface %p.\n", iface);
381 wined3d_mutex_lock();
382 wined3d_device_evict_managed_resources(device->wined3d_device);
383 wined3d_mutex_unlock();
385 return D3D_OK;
388 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
390 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
392 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
394 if (!d3d9)
395 return D3DERR_INVALIDCALL;
397 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
400 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
402 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
403 WINED3DCAPS *wined3d_caps;
404 HRESULT hr;
406 TRACE("iface %p, caps %p.\n", iface, caps);
408 if (!caps)
409 return D3DERR_INVALIDCALL;
411 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
412 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
414 memset(caps, 0, sizeof(*caps));
416 wined3d_mutex_lock();
417 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
418 wined3d_mutex_unlock();
420 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
421 HeapFree(GetProcessHeap(), 0, wined3d_caps);
423 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
424 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
426 filter_caps(caps);
428 return hr;
431 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
433 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
434 struct wined3d_display_mode wined3d_mode;
435 HRESULT hr;
437 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
439 wined3d_mutex_lock();
440 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
441 wined3d_mutex_unlock();
443 if (SUCCEEDED(hr))
445 mode->Width = wined3d_mode.width;
446 mode->Height = wined3d_mode.height;
447 mode->RefreshRate = wined3d_mode.refresh_rate;
448 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
451 return hr;
454 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
455 D3DDEVICE_CREATION_PARAMETERS *parameters)
457 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
459 TRACE("iface %p, parameters %p.\n", iface, parameters);
461 wined3d_mutex_lock();
462 wined3d_device_get_creation_parameters(device->wined3d_device,
463 (struct wined3d_device_creation_parameters *)parameters);
464 wined3d_mutex_unlock();
466 return D3D_OK;
469 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
470 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
472 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
473 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
474 HRESULT hr;
476 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
477 iface, hotspot_x, hotspot_y, bitmap);
479 if (!bitmap)
481 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
482 return D3DERR_INVALIDCALL;
485 wined3d_mutex_lock();
486 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
487 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
488 wined3d_mutex_unlock();
490 return hr;
493 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
495 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
497 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
499 wined3d_mutex_lock();
500 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
501 wined3d_mutex_unlock();
504 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
506 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
507 BOOL ret;
509 TRACE("iface %p, show %#x.\n", iface, show);
511 wined3d_mutex_lock();
512 ret = wined3d_device_show_cursor(device->wined3d_device, show);
513 wined3d_mutex_unlock();
515 return ret;
518 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
519 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
521 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
522 struct wined3d_swapchain_desc desc;
523 struct d3d9_swapchain *object;
524 UINT i, count;
525 HRESULT hr;
527 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
528 iface, present_parameters, swapchain);
530 if (!present_parameters->Windowed)
532 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
533 return D3DERR_INVALIDCALL;
536 wined3d_mutex_lock();
537 count = wined3d_device_get_swapchain_count(device->wined3d_device);
538 for (i = 0; i < count; ++i)
540 struct wined3d_swapchain *wined3d_swapchain;
542 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
543 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
545 if (!desc.windowed)
547 wined3d_mutex_unlock();
548 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
549 return D3DERR_INVALIDCALL;
552 wined3d_mutex_unlock();
554 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
555 device->d3d_parent->extended))
556 return D3DERR_INVALIDCALL;
557 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
558 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
559 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
561 return hr;
564 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
565 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
567 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
568 struct wined3d_swapchain *wined3d_swapchain;
569 struct d3d9_swapchain *swapchain_impl;
570 HRESULT hr;
572 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
574 wined3d_mutex_lock();
575 if ((wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx)))
577 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
578 *swapchain = (IDirect3DSwapChain9 *)&swapchain_impl->IDirect3DSwapChain9Ex_iface;
579 IDirect3DSwapChain9Ex_AddRef(*swapchain);
580 hr = D3D_OK;
582 else
584 *swapchain = NULL;
585 hr = D3DERR_INVALIDCALL;
587 wined3d_mutex_unlock();
589 return hr;
592 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
594 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
595 UINT count;
597 TRACE("iface %p.\n", iface);
599 wined3d_mutex_lock();
600 count = wined3d_device_get_swapchain_count(device->wined3d_device);
601 wined3d_mutex_unlock();
603 return count;
606 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
608 struct wined3d_resource_desc desc;
610 wined3d_resource_get_desc(resource, &desc);
611 if (desc.pool == WINED3D_POOL_DEFAULT)
613 struct d3d9_surface *surface;
615 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
617 IUnknown *parent = wined3d_resource_get_parent(resource);
618 IDirect3DBaseTexture9 *texture;
620 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
622 IDirect3DBaseTexture9_Release(texture);
623 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
624 return D3DERR_INVALIDCALL;
627 return D3D_OK;
630 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
632 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
633 return D3DERR_INVALIDCALL;
636 surface = wined3d_resource_get_parent(resource);
637 if (surface->resource.refcount)
639 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
640 return D3DERR_INVALIDCALL;
643 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
646 return D3D_OK;
649 static HRESULT d3d9_device_reset(struct d3d9_device *device,
650 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
652 struct wined3d_swapchain_desc swapchain_desc;
653 struct wined3d_display_mode wined3d_mode;
654 HRESULT hr;
656 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
658 WARN("App not active, returning D3DERR_DEVICELOST.\n");
659 return D3DERR_DEVICELOST;
662 if (mode)
664 wined3d_mode.width = mode->Width;
665 wined3d_mode.height = mode->Height;
666 wined3d_mode.refresh_rate = mode->RefreshRate;
667 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
668 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
671 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
672 device->d3d_parent->extended))
673 return D3DERR_INVALIDCALL;
675 wined3d_mutex_lock();
677 if (device->vertex_buffer)
679 wined3d_buffer_decref(device->vertex_buffer);
680 device->vertex_buffer = NULL;
681 device->vertex_buffer_size = 0;
684 if (device->index_buffer)
686 wined3d_buffer_decref(device->index_buffer);
687 device->index_buffer = NULL;
688 device->index_buffer_size = 0;
691 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
692 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
694 struct wined3d_swapchain *wined3d_swapchain;
696 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
697 wined3d_swapchain_get_desc(wined3d_swapchain, &swapchain_desc);
698 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
699 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
700 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
701 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
703 device->device_state = D3D9_DEVICE_STATE_OK;
705 else if (!device->d3d_parent->extended)
707 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
710 wined3d_mutex_unlock();
712 return hr;
715 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
716 D3DPRESENT_PARAMETERS *present_parameters)
718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
720 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
722 return d3d9_device_reset(device, present_parameters, NULL);
725 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
726 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
728 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
729 HRESULT hr;
731 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
732 iface, src_rect, dst_rect, dst_window_override, dirty_region);
734 if (device->device_state != D3D9_DEVICE_STATE_OK)
735 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
737 wined3d_mutex_lock();
738 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
739 dst_window_override, dirty_region, 0);
740 wined3d_mutex_unlock();
742 return hr;
745 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
746 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
749 struct wined3d_swapchain *wined3d_swapchain;
750 struct wined3d_resource *wined3d_resource;
751 struct wined3d_texture *wined3d_texture;
752 struct d3d9_surface *surface_impl;
754 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
755 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
757 /* backbuffer_type is ignored by native. */
759 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
760 wined3d_mutex_lock();
762 if (!(wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain)))
764 wined3d_mutex_unlock();
765 *backbuffer = NULL;
766 return D3DERR_INVALIDCALL;
769 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
771 wined3d_mutex_unlock();
772 *backbuffer = NULL;
773 return D3DERR_INVALIDCALL;
776 wined3d_resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
777 surface_impl = wined3d_resource_get_parent(wined3d_resource);
778 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
779 IDirect3DSurface9_AddRef(*backbuffer);
781 wined3d_mutex_unlock();
782 return D3D_OK;
785 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
786 UINT swapchain, D3DRASTER_STATUS *raster_status)
788 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
789 HRESULT hr;
791 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
793 wined3d_mutex_lock();
794 hr = wined3d_device_get_raster_status(device->wined3d_device,
795 swapchain, (struct wined3d_raster_status *)raster_status);
796 wined3d_mutex_unlock();
798 return hr;
801 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
803 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
804 HRESULT hr;
806 TRACE("iface %p, enable %#x.\n", iface, enable);
808 wined3d_mutex_lock();
809 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
810 wined3d_mutex_unlock();
812 return hr;
815 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
816 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
818 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
820 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
822 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
823 wined3d_mutex_lock();
824 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
825 wined3d_mutex_unlock();
828 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
830 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
832 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
834 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
835 wined3d_mutex_lock();
836 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
837 wined3d_mutex_unlock();
840 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
841 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
842 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
844 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
845 struct d3d9_texture *object;
846 BOOL set_mem = FALSE;
847 HRESULT hr;
849 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
850 iface, width, height, levels, usage, format, pool, texture, shared_handle);
852 *texture = NULL;
853 if (shared_handle)
855 if (!device->d3d_parent->extended)
857 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
858 return E_NOTIMPL;
861 if (pool == D3DPOOL_SYSTEMMEM)
863 if (levels != 1)
864 return D3DERR_INVALIDCALL;
865 set_mem = TRUE;
867 else
869 if (pool != D3DPOOL_DEFAULT)
871 WARN("Trying to create a shared texture in pool %#x.\n", pool);
872 return D3DERR_INVALIDCALL;
874 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
878 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
879 if (!object)
880 return D3DERR_OUTOFVIDEOMEMORY;
882 hr = texture_init(object, device, width, height, levels, usage, format, pool);
883 if (FAILED(hr))
885 WARN("Failed to initialize texture, hr %#x.\n", hr);
886 HeapFree(GetProcessHeap(), 0, object);
887 return hr;
890 if (set_mem)
891 wined3d_texture_update_desc(object->wined3d_texture, width, height,
892 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
893 *shared_handle, 0);
895 TRACE("Created texture %p.\n", object);
896 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
898 return D3D_OK;
901 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
902 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
903 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
905 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
906 struct d3d9_texture *object;
907 HRESULT hr;
909 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
910 iface, width, height, depth, levels);
911 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
912 usage, format, pool, texture, shared_handle);
914 *texture = NULL;
915 if (shared_handle)
917 if (!device->d3d_parent->extended)
919 WARN("Trying to create a shared volume texture on a non-ex device.\n");
920 return E_NOTIMPL;
923 if (pool != D3DPOOL_DEFAULT)
925 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
926 return D3DERR_INVALIDCALL;
928 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
931 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
932 if (!object)
933 return D3DERR_OUTOFVIDEOMEMORY;
935 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
936 if (FAILED(hr))
938 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
939 HeapFree(GetProcessHeap(), 0, object);
940 return hr;
943 TRACE("Created volume texture %p.\n", object);
944 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
946 return D3D_OK;
949 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
950 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
951 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
953 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
954 struct d3d9_texture *object;
955 HRESULT hr;
957 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
958 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
960 *texture = NULL;
961 if (shared_handle)
963 if (!device->d3d_parent->extended)
965 WARN("Trying to create a shared cube texture on a non-ex device.\n");
966 return E_NOTIMPL;
969 if (pool != D3DPOOL_DEFAULT)
971 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
972 return D3DERR_INVALIDCALL;
974 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
977 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
978 if (!object)
979 return D3DERR_OUTOFVIDEOMEMORY;
981 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
982 if (FAILED(hr))
984 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
985 HeapFree(GetProcessHeap(), 0, object);
986 return hr;
989 TRACE("Created cube texture %p.\n", object);
990 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
992 return D3D_OK;
995 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
996 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
997 HANDLE *shared_handle)
999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1000 struct d3d9_vertexbuffer *object;
1001 HRESULT hr;
1003 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1004 iface, size, usage, fvf, pool, buffer, shared_handle);
1006 if (shared_handle)
1008 if (!device->d3d_parent->extended)
1010 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1011 return E_NOTIMPL;
1014 if (pool != D3DPOOL_DEFAULT)
1016 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1017 return D3DERR_NOTAVAILABLE;
1019 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1022 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1023 if (!object)
1024 return D3DERR_OUTOFVIDEOMEMORY;
1026 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1027 if (FAILED(hr))
1029 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1030 HeapFree(GetProcessHeap(), 0, object);
1031 return hr;
1034 TRACE("Created vertex buffer %p.\n", object);
1035 *buffer = &object->IDirect3DVertexBuffer9_iface;
1037 return D3D_OK;
1040 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1041 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1042 HANDLE *shared_handle)
1044 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1045 struct d3d9_indexbuffer *object;
1046 HRESULT hr;
1048 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1049 iface, size, usage, format, pool, buffer, shared_handle);
1051 if (shared_handle)
1053 if (!device->d3d_parent->extended)
1055 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1056 return E_NOTIMPL;
1059 if (pool != D3DPOOL_DEFAULT)
1061 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1062 return D3DERR_NOTAVAILABLE;
1064 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1067 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1068 if (!object)
1069 return D3DERR_OUTOFVIDEOMEMORY;
1071 hr = indexbuffer_init(object, device, size, usage, format, pool);
1072 if (FAILED(hr))
1074 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1075 HeapFree(GetProcessHeap(), 0, object);
1076 return hr;
1079 TRACE("Created index buffer %p.\n", object);
1080 *buffer = &object->IDirect3DIndexBuffer9_iface;
1082 return D3D_OK;
1085 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1086 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1087 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1089 struct wined3d_resource *sub_resource;
1090 struct wined3d_resource_desc desc;
1091 struct d3d9_surface *surface_impl;
1092 struct wined3d_texture *texture;
1093 HRESULT hr;
1095 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1096 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1097 device, width, height, format, flags, surface, usage, pool,
1098 multisample_type, multisample_quality);
1100 desc.resource_type = WINED3D_RTYPE_TEXTURE;
1101 desc.format = wined3dformat_from_d3dformat(format);
1102 desc.multisample_type = multisample_type;
1103 desc.multisample_quality = multisample_quality;
1104 desc.usage = usage & WINED3DUSAGE_MASK;
1105 desc.pool = pool;
1106 desc.width = width;
1107 desc.height = height;
1108 desc.depth = 1;
1109 desc.size = 0;
1111 wined3d_mutex_lock();
1113 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1114 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1116 wined3d_mutex_unlock();
1117 WARN("Failed to create texture, hr %#x.\n", hr);
1118 return hr;
1121 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1122 surface_impl = wined3d_resource_get_parent(sub_resource);
1123 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1124 *surface = &surface_impl->IDirect3DSurface9_iface;
1125 IDirect3DSurface9_AddRef(*surface);
1127 if (user_mem)
1128 wined3d_texture_update_desc(texture, width, height,
1129 desc.format, multisample_type, multisample_quality, user_mem, 0);
1131 wined3d_texture_decref(texture);
1133 wined3d_mutex_unlock();
1135 return D3D_OK;
1138 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1139 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1140 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1142 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1143 DWORD flags = 0;
1145 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1146 "lockable %#x, surface %p, shared_handle %p.\n",
1147 iface, width, height, format, multisample_type, multisample_quality,
1148 lockable, surface, shared_handle);
1150 *surface = NULL;
1151 if (shared_handle)
1153 if (!device->d3d_parent->extended)
1155 WARN("Trying to create a shared render target on a non-ex device.\n");
1156 return E_NOTIMPL;
1159 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1162 if (lockable)
1163 flags |= WINED3D_SURFACE_MAPPABLE;
1165 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1166 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1169 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1170 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1171 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1173 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1174 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1176 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1177 "discard %#x, surface %p, shared_handle %p.\n",
1178 iface, width, height, format, multisample_type, multisample_quality,
1179 discard, surface, shared_handle);
1181 *surface = NULL;
1182 if (shared_handle)
1184 if (!device->d3d_parent->extended)
1186 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1187 return E_NOTIMPL;
1190 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1193 if (discard)
1194 flags |= WINED3D_SURFACE_DISCARD;
1196 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1197 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1201 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1202 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1203 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1205 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1206 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1207 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1208 HRESULT hr;
1210 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1211 iface, src_surface, src_rect, dst_surface, dst_point);
1213 wined3d_mutex_lock();
1214 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1215 dst->wined3d_surface, dst_point);
1216 wined3d_mutex_unlock();
1218 return hr;
1221 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1222 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1224 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1225 struct d3d9_texture *src_impl, *dst_impl;
1226 HRESULT hr;
1228 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1230 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1231 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1233 wined3d_mutex_lock();
1234 hr = wined3d_device_update_texture(device->wined3d_device,
1235 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1236 wined3d_mutex_unlock();
1238 return hr;
1241 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1242 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1244 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1245 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1246 HRESULT hr;
1248 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1250 wined3d_mutex_lock();
1251 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1252 wined3d_mutex_unlock();
1254 return hr;
1257 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1258 UINT swapchain, IDirect3DSurface9 *dst_surface)
1260 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1261 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1262 HRESULT hr;
1264 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1266 wined3d_mutex_lock();
1267 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1268 wined3d_mutex_unlock();
1270 return hr;
1273 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1274 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1276 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1277 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1278 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1279 HRESULT hr = D3DERR_INVALIDCALL;
1280 struct wined3d_resource_desc src_desc, dst_desc;
1281 struct wined3d_resource *wined3d_resource;
1283 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1284 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1286 wined3d_mutex_lock();
1287 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1288 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1290 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1291 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1293 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1295 if (device->in_scene)
1297 WARN("Rejecting depth / stencil blit while in scene.\n");
1298 goto done;
1301 if (src_rect)
1303 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1304 || src_rect->bottom != src_desc.height)
1306 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1307 wine_dbgstr_rect(src_rect));
1308 goto done;
1311 if (dst_rect)
1313 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1314 || dst_rect->bottom != dst_desc.height)
1316 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1317 wine_dbgstr_rect(dst_rect));
1318 goto done;
1321 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1323 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1324 goto done;
1328 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1329 if (hr == WINEDDERR_INVALIDRECT)
1330 hr = D3DERR_INVALIDCALL;
1332 done:
1333 wined3d_mutex_unlock();
1334 return hr;
1337 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1338 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1340 const struct wined3d_color c =
1342 ((color >> 16) & 0xff) / 255.0f,
1343 ((color >> 8) & 0xff) / 255.0f,
1344 (color & 0xff) / 255.0f,
1345 ((color >> 24) & 0xff) / 255.0f,
1347 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1348 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1349 struct wined3d_resource *wined3d_resource;
1350 struct wined3d_resource_desc desc;
1351 HRESULT hr;
1353 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1355 wined3d_mutex_lock();
1357 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1358 wined3d_resource_get_desc(wined3d_resource, &desc);
1360 if (desc.pool != WINED3D_POOL_DEFAULT)
1362 wined3d_mutex_unlock();
1363 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1364 return D3DERR_INVALIDCALL;
1366 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1368 wined3d_mutex_unlock();
1369 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1370 return D3DERR_INVALIDCALL;
1372 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1374 wined3d_mutex_unlock();
1375 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1376 return D3DERR_INVALIDCALL;
1379 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1380 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1382 wined3d_mutex_unlock();
1384 return hr;
1387 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1388 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1389 HANDLE *shared_handle)
1391 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1392 void *user_mem = NULL;
1394 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1395 iface, width, height, format, pool, surface, shared_handle);
1397 *surface = NULL;
1398 if (pool == D3DPOOL_MANAGED)
1400 WARN("Attempting to create a managed offscreen plain surface.\n");
1401 return D3DERR_INVALIDCALL;
1404 if (shared_handle)
1406 if (!device->d3d_parent->extended)
1408 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1409 return E_NOTIMPL;
1412 if (pool == D3DPOOL_SYSTEMMEM)
1413 user_mem = *shared_handle;
1414 else
1416 if (pool != D3DPOOL_DEFAULT)
1418 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1419 return D3DERR_INVALIDCALL;
1421 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1425 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1426 * regardless of the pool they're created in. Should we set dynamic usage
1427 * here? */
1428 return d3d9_device_create_surface(device, width, height, format,
1429 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1432 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1434 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1435 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1436 HRESULT hr;
1438 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1440 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1442 WARN("Invalid index %u specified.\n", idx);
1443 return D3DERR_INVALIDCALL;
1446 if (!idx && !surface_impl)
1448 WARN("Trying to set render target 0 to NULL.\n");
1449 return D3DERR_INVALIDCALL;
1452 wined3d_mutex_lock();
1453 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1454 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1455 wined3d_mutex_unlock();
1457 return hr;
1460 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1462 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1463 struct wined3d_rendertarget_view *wined3d_rtv;
1464 struct d3d9_surface *surface_impl;
1465 HRESULT hr = D3D_OK;
1467 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1469 if (!surface)
1470 return D3DERR_INVALIDCALL;
1472 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1474 WARN("Invalid index %u specified.\n", idx);
1475 return D3DERR_INVALIDCALL;
1478 wined3d_mutex_lock();
1479 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1481 /* We want the sub resource parent here, since the view itself may be
1482 * internal to wined3d and may not have a parent. */
1483 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1484 *surface = &surface_impl->IDirect3DSurface9_iface;
1485 IDirect3DSurface9_AddRef(*surface);
1487 else
1489 hr = WINED3DERR_NOTFOUND;
1490 *surface = NULL;
1492 wined3d_mutex_unlock();
1494 return hr;
1497 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1499 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1500 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1502 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1504 wined3d_mutex_lock();
1505 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1506 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1507 wined3d_mutex_unlock();
1509 return D3D_OK;
1512 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1514 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1515 struct wined3d_rendertarget_view *wined3d_dsv;
1516 struct d3d9_surface *surface_impl;
1517 HRESULT hr = D3D_OK;
1519 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1521 if (!depth_stencil)
1522 return D3DERR_INVALIDCALL;
1524 wined3d_mutex_lock();
1525 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1527 /* We want the sub resource parent here, since the view itself may be
1528 * internal to wined3d and may not have a parent. */
1529 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1530 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1531 IDirect3DSurface9_AddRef(*depth_stencil);
1533 else
1535 hr = WINED3DERR_NOTFOUND;
1536 *depth_stencil = NULL;
1538 wined3d_mutex_unlock();
1540 return hr;
1543 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1545 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1546 HRESULT hr;
1548 TRACE("iface %p.\n", iface);
1550 wined3d_mutex_lock();
1551 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1552 device->in_scene = TRUE;
1553 wined3d_mutex_unlock();
1555 return hr;
1558 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1560 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1561 HRESULT hr;
1563 TRACE("iface %p.\n", iface);
1565 wined3d_mutex_lock();
1566 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1567 device->in_scene = FALSE;
1568 wined3d_mutex_unlock();
1570 return hr;
1573 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1574 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1576 const struct wined3d_color c =
1578 ((color >> 16) & 0xff) / 255.0f,
1579 ((color >> 8) & 0xff) / 255.0f,
1580 (color & 0xff) / 255.0f,
1581 ((color >> 24) & 0xff) / 255.0f,
1583 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1584 HRESULT hr;
1586 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1587 iface, rect_count, rects, flags, color, z, stencil);
1589 wined3d_mutex_lock();
1590 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1591 wined3d_mutex_unlock();
1593 return hr;
1596 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1597 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1599 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1601 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1603 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1604 wined3d_mutex_lock();
1605 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1606 wined3d_mutex_unlock();
1608 return D3D_OK;
1611 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1612 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1614 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1616 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1618 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1619 wined3d_mutex_lock();
1620 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1621 wined3d_mutex_unlock();
1623 return D3D_OK;
1626 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1627 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1631 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1633 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1634 wined3d_mutex_lock();
1635 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1636 wined3d_mutex_unlock();
1638 return D3D_OK;
1641 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1643 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1645 TRACE("iface %p, viewport %p.\n", iface, viewport);
1647 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1648 wined3d_mutex_lock();
1649 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1650 wined3d_mutex_unlock();
1652 return D3D_OK;
1655 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1657 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1659 TRACE("iface %p, viewport %p.\n", iface, viewport);
1661 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1662 wined3d_mutex_lock();
1663 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1664 wined3d_mutex_unlock();
1666 return D3D_OK;
1669 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1671 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1673 TRACE("iface %p, material %p.\n", iface, material);
1675 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1676 wined3d_mutex_lock();
1677 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1678 wined3d_mutex_unlock();
1680 return D3D_OK;
1683 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1685 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1687 TRACE("iface %p, material %p.\n", iface, material);
1689 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1690 wined3d_mutex_lock();
1691 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1692 wined3d_mutex_unlock();
1694 return D3D_OK;
1697 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1699 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1700 HRESULT hr;
1702 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1704 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1705 wined3d_mutex_lock();
1706 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1707 wined3d_mutex_unlock();
1709 return hr;
1712 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1714 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1715 HRESULT hr;
1717 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1719 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1720 wined3d_mutex_lock();
1721 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1722 wined3d_mutex_unlock();
1724 return hr;
1727 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1729 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1730 HRESULT hr;
1732 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1734 wined3d_mutex_lock();
1735 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1736 wined3d_mutex_unlock();
1738 return hr;
1741 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1744 HRESULT hr;
1746 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1748 wined3d_mutex_lock();
1749 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1750 wined3d_mutex_unlock();
1752 return hr;
1755 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1757 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1758 HRESULT hr;
1760 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1762 wined3d_mutex_lock();
1763 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1764 wined3d_mutex_unlock();
1766 return hr;
1769 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1771 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1772 HRESULT hr;
1774 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1776 wined3d_mutex_lock();
1777 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1778 wined3d_mutex_unlock();
1780 return hr;
1783 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1784 D3DRENDERSTATETYPE state, DWORD value)
1786 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1788 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1790 wined3d_mutex_lock();
1791 wined3d_device_set_render_state(device->wined3d_device, state, value);
1792 wined3d_mutex_unlock();
1794 return D3D_OK;
1797 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1798 D3DRENDERSTATETYPE state, DWORD *value)
1800 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1802 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1804 wined3d_mutex_lock();
1805 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1806 wined3d_mutex_unlock();
1808 return D3D_OK;
1811 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1812 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1814 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1815 struct d3d9_stateblock *object;
1816 HRESULT hr;
1818 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1820 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1822 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1823 return D3DERR_INVALIDCALL;
1826 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1827 if (!object)
1828 return E_OUTOFMEMORY;
1830 hr = stateblock_init(object, device, type, NULL);
1831 if (FAILED(hr))
1833 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1834 HeapFree(GetProcessHeap(), 0, object);
1835 return hr;
1838 TRACE("Created stateblock %p.\n", object);
1839 *stateblock = &object->IDirect3DStateBlock9_iface;
1841 return D3D_OK;
1844 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1846 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1847 HRESULT hr;
1849 TRACE("iface %p.\n", iface);
1851 wined3d_mutex_lock();
1852 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1853 wined3d_mutex_unlock();
1855 return hr;
1858 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1860 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1861 struct wined3d_stateblock *wined3d_stateblock;
1862 struct d3d9_stateblock *object;
1863 HRESULT hr;
1865 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1867 wined3d_mutex_lock();
1868 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1869 wined3d_mutex_unlock();
1870 if (FAILED(hr))
1872 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1873 return hr;
1876 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1877 if (!object)
1879 wined3d_mutex_lock();
1880 wined3d_stateblock_decref(wined3d_stateblock);
1881 wined3d_mutex_unlock();
1882 return E_OUTOFMEMORY;
1885 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1886 if (FAILED(hr))
1888 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1889 wined3d_mutex_lock();
1890 wined3d_stateblock_decref(wined3d_stateblock);
1891 wined3d_mutex_unlock();
1892 HeapFree(GetProcessHeap(), 0, object);
1893 return hr;
1896 TRACE("Created stateblock %p.\n", object);
1897 *stateblock = &object->IDirect3DStateBlock9_iface;
1899 return D3D_OK;
1902 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1904 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1905 HRESULT hr;
1907 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1909 wined3d_mutex_lock();
1910 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1911 wined3d_mutex_unlock();
1913 return hr;
1916 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1918 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1919 HRESULT hr;
1921 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1923 wined3d_mutex_lock();
1924 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1925 wined3d_mutex_unlock();
1927 return hr;
1930 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1932 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1933 struct wined3d_texture *wined3d_texture = NULL;
1934 struct d3d9_texture *texture_impl;
1936 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1938 if (!texture)
1939 return D3DERR_INVALIDCALL;
1941 wined3d_mutex_lock();
1942 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1944 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1945 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1946 IDirect3DBaseTexture9_AddRef(*texture);
1948 else
1950 *texture = NULL;
1952 wined3d_mutex_unlock();
1954 return D3D_OK;
1957 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1959 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1960 struct d3d9_texture *texture_impl;
1961 HRESULT hr;
1963 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1965 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1967 wined3d_mutex_lock();
1968 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1969 texture_impl ? texture_impl->wined3d_texture : NULL);
1970 wined3d_mutex_unlock();
1972 return hr;
1975 static const enum wined3d_texture_stage_state tss_lookup[] =
1977 WINED3D_TSS_INVALID, /* 0, unused */
1978 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1979 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1980 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1981 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1982 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1983 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1984 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1985 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1986 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1987 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1988 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1989 WINED3D_TSS_INVALID, /* 12, unused */
1990 WINED3D_TSS_INVALID, /* 13, unused */
1991 WINED3D_TSS_INVALID, /* 14, unused */
1992 WINED3D_TSS_INVALID, /* 15, unused */
1993 WINED3D_TSS_INVALID, /* 16, unused */
1994 WINED3D_TSS_INVALID, /* 17, unused */
1995 WINED3D_TSS_INVALID, /* 18, unused */
1996 WINED3D_TSS_INVALID, /* 19, unused */
1997 WINED3D_TSS_INVALID, /* 20, unused */
1998 WINED3D_TSS_INVALID, /* 21, unused */
1999 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2000 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2001 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2002 WINED3D_TSS_INVALID, /* 25, unused */
2003 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2004 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2005 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2006 WINED3D_TSS_INVALID, /* 29, unused */
2007 WINED3D_TSS_INVALID, /* 30, unused */
2008 WINED3D_TSS_INVALID, /* 31, unused */
2009 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2012 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2013 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2015 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2017 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2019 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2021 WARN("Invalid state %#x passed.\n", state);
2022 return D3D_OK;
2025 wined3d_mutex_lock();
2026 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2027 wined3d_mutex_unlock();
2029 return D3D_OK;
2032 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2033 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2035 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2037 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2039 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2041 WARN("Invalid state %#x passed.\n", state);
2042 return D3D_OK;
2045 wined3d_mutex_lock();
2046 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2047 wined3d_mutex_unlock();
2049 return D3D_OK;
2052 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2053 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2055 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2057 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2059 wined3d_mutex_lock();
2060 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2061 wined3d_mutex_unlock();
2063 return D3D_OK;
2066 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2067 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2069 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2071 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2073 wined3d_mutex_lock();
2074 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2075 wined3d_mutex_unlock();
2077 return D3D_OK;
2080 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2082 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2083 HRESULT hr;
2085 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2087 wined3d_mutex_lock();
2088 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2089 wined3d_mutex_unlock();
2091 return hr;
2094 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2095 UINT palette_idx, const PALETTEENTRY *entries)
2097 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2099 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2100 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2101 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2103 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2104 return D3D_OK;
2107 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2108 UINT palette_idx, PALETTEENTRY *entries)
2110 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2112 return D3DERR_INVALIDCALL;
2115 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2117 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2119 return D3D_OK;
2122 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2124 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2126 return D3DERR_INVALIDCALL;
2129 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2131 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2133 TRACE("iface %p, rect %p.\n", iface, rect);
2135 wined3d_mutex_lock();
2136 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2137 wined3d_mutex_unlock();
2139 return D3D_OK;
2142 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2144 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2146 TRACE("iface %p, rect %p.\n", iface, rect);
2148 wined3d_mutex_lock();
2149 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2150 wined3d_mutex_unlock();
2152 return D3D_OK;
2155 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2157 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2159 TRACE("iface %p, software %#x.\n", iface, software);
2161 wined3d_mutex_lock();
2162 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2163 wined3d_mutex_unlock();
2165 return D3D_OK;
2168 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2170 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2171 BOOL ret;
2173 TRACE("iface %p.\n", iface);
2175 wined3d_mutex_lock();
2176 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2177 wined3d_mutex_unlock();
2179 return ret;
2182 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2184 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2185 HRESULT hr;
2187 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2189 wined3d_mutex_lock();
2190 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2191 wined3d_mutex_unlock();
2193 return hr;
2196 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2198 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2199 float ret;
2201 TRACE("iface %p.\n", iface);
2203 wined3d_mutex_lock();
2204 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2205 wined3d_mutex_unlock();
2207 return ret;
2210 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2211 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2213 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2214 HRESULT hr;
2216 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2217 iface, primitive_type, start_vertex, primitive_count);
2219 wined3d_mutex_lock();
2220 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2221 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2222 vertex_count_from_primitive_count(primitive_type, primitive_count));
2223 wined3d_mutex_unlock();
2225 return hr;
2228 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2229 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2230 UINT vertex_count, UINT start_idx, UINT primitive_count)
2232 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2233 HRESULT hr;
2235 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2236 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2237 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2238 vertex_count, start_idx, primitive_count);
2240 wined3d_mutex_lock();
2241 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2242 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2243 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2244 vertex_count_from_primitive_count(primitive_type, primitive_count));
2245 wined3d_mutex_unlock();
2247 return hr;
2250 /* The caller is responsible for wined3d locking */
2251 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2253 HRESULT hr;
2255 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2257 UINT size = max(device->vertex_buffer_size * 2, min_size);
2258 struct wined3d_buffer *buffer;
2260 TRACE("Growing vertex buffer to %u bytes\n", size);
2262 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2263 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2264 if (FAILED(hr))
2266 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2267 return hr;
2270 if (device->vertex_buffer)
2271 wined3d_buffer_decref(device->vertex_buffer);
2273 device->vertex_buffer = buffer;
2274 device->vertex_buffer_size = size;
2275 device->vertex_buffer_pos = 0;
2277 return D3D_OK;
2280 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2281 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2283 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2284 HRESULT hr;
2285 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2286 UINT size = vtx_count * stride;
2287 UINT vb_pos, align;
2288 BYTE *buffer_data;
2290 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2291 iface, primitive_type, primitive_count, data, stride);
2293 if (!primitive_count)
2295 WARN("primitive_count is 0, returning D3D_OK\n");
2296 return D3D_OK;
2299 wined3d_mutex_lock();
2301 hr = d3d9_device_prepare_vertex_buffer(device, size);
2302 if (FAILED(hr))
2303 goto done;
2305 vb_pos = device->vertex_buffer_pos;
2306 align = vb_pos % stride;
2307 if (align) align = stride - align;
2308 if (vb_pos + size + align > device->vertex_buffer_size)
2309 vb_pos = 0;
2310 else
2311 vb_pos += align;
2313 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2314 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2315 if (FAILED(hr))
2316 goto done;
2317 memcpy(buffer_data, data, size);
2318 wined3d_buffer_unmap(device->vertex_buffer);
2319 device->vertex_buffer_pos = vb_pos + size;
2321 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2322 if (FAILED(hr))
2323 goto done;
2325 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2326 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2327 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2329 done:
2330 wined3d_mutex_unlock();
2331 return hr;
2334 /* The caller is responsible for wined3d locking */
2335 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2337 HRESULT hr;
2339 if (device->index_buffer_size < min_size || !device->index_buffer)
2341 UINT size = max(device->index_buffer_size * 2, min_size);
2342 struct wined3d_buffer *buffer;
2344 TRACE("Growing index buffer to %u bytes\n", size);
2346 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2347 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2348 if (FAILED(hr))
2350 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2351 return hr;
2354 if (device->index_buffer)
2355 wined3d_buffer_decref(device->index_buffer);
2357 device->index_buffer = buffer;
2358 device->index_buffer_size = size;
2359 device->index_buffer_pos = 0;
2361 return D3D_OK;
2364 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2365 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2366 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2367 const void *vertex_data, UINT vertex_stride)
2369 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2370 HRESULT hr;
2371 BYTE *buffer_data;
2373 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2374 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2375 UINT idx_size = idx_count * idx_fmt_size;
2376 UINT ib_pos;
2378 UINT vtx_size = vertex_count * vertex_stride;
2379 UINT vb_pos, align;
2381 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2382 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2383 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2384 index_data, index_format, vertex_data, vertex_stride);
2386 if (!primitive_count)
2388 WARN("primitive_count is 0, returning D3D_OK\n");
2389 return D3D_OK;
2392 wined3d_mutex_lock();
2394 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2395 if (FAILED(hr))
2396 goto done;
2398 vb_pos = device->vertex_buffer_pos;
2399 align = vb_pos % vertex_stride;
2400 if (align) align = vertex_stride - align;
2401 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2402 vb_pos = 0;
2403 else
2404 vb_pos += align;
2406 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2407 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2408 if (FAILED(hr))
2409 goto done;
2410 memcpy(buffer_data, vertex_data, vtx_size);
2411 wined3d_buffer_unmap(device->vertex_buffer);
2412 device->vertex_buffer_pos = vb_pos + vtx_size;
2414 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2415 if (FAILED(hr))
2416 goto done;
2418 ib_pos = device->index_buffer_pos;
2419 align = ib_pos % idx_fmt_size;
2420 if (align) align = idx_fmt_size - align;
2421 if (ib_pos + idx_size + align > device->index_buffer_size)
2422 ib_pos = 0;
2423 else
2424 ib_pos += align;
2426 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2427 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2428 if (FAILED(hr))
2429 goto done;
2430 memcpy(buffer_data, index_data, idx_size);
2431 wined3d_buffer_unmap(device->index_buffer);
2432 device->index_buffer_pos = ib_pos + idx_size;
2434 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2435 if (FAILED(hr))
2436 goto done;
2438 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2439 wined3dformat_from_d3dformat(index_format));
2440 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2442 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2443 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2445 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2446 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2447 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2449 done:
2450 wined3d_mutex_unlock();
2451 return hr;
2454 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2455 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2456 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2458 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2459 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2460 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2461 HRESULT hr;
2463 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2464 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2466 wined3d_mutex_lock();
2467 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2468 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2469 flags, dst_impl->fvf);
2470 wined3d_mutex_unlock();
2472 return hr;
2475 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2476 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2478 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2479 struct d3d9_vertex_declaration *object;
2480 HRESULT hr;
2482 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2484 if (!declaration)
2486 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2487 return D3DERR_INVALIDCALL;
2490 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2491 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2493 return hr;
2496 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2497 IDirect3DVertexDeclaration9 *declaration)
2499 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2500 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2502 TRACE("iface %p, declaration %p.\n", iface, declaration);
2504 wined3d_mutex_lock();
2505 wined3d_device_set_vertex_declaration(device->wined3d_device,
2506 decl_impl ? decl_impl->wined3d_declaration : NULL);
2507 wined3d_mutex_unlock();
2509 return D3D_OK;
2512 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2513 IDirect3DVertexDeclaration9 **declaration)
2515 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2516 struct wined3d_vertex_declaration *wined3d_declaration;
2517 struct d3d9_vertex_declaration *declaration_impl;
2519 TRACE("iface %p, declaration %p.\n", iface, declaration);
2521 if (!declaration) return D3DERR_INVALIDCALL;
2523 wined3d_mutex_lock();
2524 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2526 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2527 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2528 IDirect3DVertexDeclaration9_AddRef(*declaration);
2530 else
2532 *declaration = NULL;
2534 wined3d_mutex_unlock();
2536 TRACE("Returning %p.\n", *declaration);
2537 return D3D_OK;
2540 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2542 struct wined3d_vertex_declaration *wined3d_declaration;
2543 struct fvf_declaration *fvf_decls = device->fvf_decls;
2544 struct d3d9_vertex_declaration *d3d9_declaration;
2545 D3DVERTEXELEMENT9 *elements;
2546 int p, low, high; /* deliberately signed */
2547 HRESULT hr;
2549 TRACE("Searching for declaration for fvf %08x... ", fvf);
2551 low = 0;
2552 high = device->fvf_decl_count - 1;
2553 while (low <= high)
2555 p = (low + high) >> 1;
2556 TRACE("%d ", p);
2558 if (fvf_decls[p].fvf == fvf)
2560 TRACE("found %p.\n", fvf_decls[p].decl);
2561 return fvf_decls[p].decl;
2564 if (fvf_decls[p].fvf < fvf)
2565 low = p + 1;
2566 else
2567 high = p - 1;
2569 TRACE("not found. Creating and inserting at position %d.\n", low);
2571 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2572 return NULL;
2574 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2575 HeapFree(GetProcessHeap(), 0, elements);
2576 if (FAILED(hr))
2577 return NULL;
2579 if (device->fvf_decl_size == device->fvf_decl_count)
2581 UINT grow = max(device->fvf_decl_size / 2, 8);
2583 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2584 if (!fvf_decls)
2586 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2587 return NULL;
2589 device->fvf_decls = fvf_decls;
2590 device->fvf_decl_size += grow;
2593 d3d9_declaration->fvf = fvf;
2594 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2595 wined3d_vertex_declaration_incref(wined3d_declaration);
2596 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2598 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2599 fvf_decls[low].decl = wined3d_declaration;
2600 fvf_decls[low].fvf = fvf;
2601 ++device->fvf_decl_count;
2603 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2605 return wined3d_declaration;
2608 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2610 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2611 struct wined3d_vertex_declaration *decl;
2613 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2615 if (!fvf)
2617 WARN("%#x is not a valid FVF.\n", fvf);
2618 return D3D_OK;
2621 wined3d_mutex_lock();
2622 if (!(decl = device_get_fvf_declaration(device, fvf)))
2624 wined3d_mutex_unlock();
2625 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2626 return D3DERR_DRIVERINTERNALERROR;
2629 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2630 wined3d_mutex_unlock();
2632 return D3D_OK;
2635 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2637 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2638 struct wined3d_vertex_declaration *wined3d_declaration;
2639 struct d3d9_vertex_declaration *d3d9_declaration;
2641 TRACE("iface %p, fvf %p.\n", iface, fvf);
2643 wined3d_mutex_lock();
2644 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2646 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2647 *fvf = d3d9_declaration->fvf;
2649 else
2651 *fvf = 0;
2653 wined3d_mutex_unlock();
2655 TRACE("Returning FVF %#x.\n", *fvf);
2657 return D3D_OK;
2660 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2661 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2663 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2664 struct d3d9_vertexshader *object;
2665 HRESULT hr;
2667 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2669 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2670 if (!object)
2671 return E_OUTOFMEMORY;
2673 hr = vertexshader_init(object, device, byte_code);
2674 if (FAILED(hr))
2676 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2677 HeapFree(GetProcessHeap(), 0, object);
2678 return hr;
2681 TRACE("Created vertex shader %p.\n", object);
2682 *shader = &object->IDirect3DVertexShader9_iface;
2684 return D3D_OK;
2687 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2689 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2690 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2692 TRACE("iface %p, shader %p.\n", iface, shader);
2694 wined3d_mutex_lock();
2695 wined3d_device_set_vertex_shader(device->wined3d_device,
2696 shader_obj ? shader_obj->wined3d_shader : NULL);
2697 wined3d_mutex_unlock();
2699 return D3D_OK;
2702 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2704 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2705 struct d3d9_vertexshader *shader_impl;
2706 struct wined3d_shader *wined3d_shader;
2708 TRACE("iface %p, shader %p.\n", iface, shader);
2710 wined3d_mutex_lock();
2711 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2713 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2714 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2715 IDirect3DVertexShader9_AddRef(*shader);
2717 else
2719 *shader = NULL;
2721 wined3d_mutex_unlock();
2723 TRACE("Returning %p.\n", *shader);
2725 return D3D_OK;
2728 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2729 UINT reg_idx, const float *data, UINT count)
2731 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2732 HRESULT hr;
2734 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2736 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2738 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2739 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2740 return D3DERR_INVALIDCALL;
2743 wined3d_mutex_lock();
2744 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2745 wined3d_mutex_unlock();
2747 return hr;
2750 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2751 UINT reg_idx, float *data, UINT count)
2753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2754 HRESULT hr;
2756 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2758 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2760 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2761 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2762 return D3DERR_INVALIDCALL;
2765 wined3d_mutex_lock();
2766 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2767 wined3d_mutex_unlock();
2769 return hr;
2772 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2773 UINT reg_idx, const int *data, UINT count)
2775 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2776 HRESULT hr;
2778 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2780 wined3d_mutex_lock();
2781 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2782 wined3d_mutex_unlock();
2784 return hr;
2787 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2788 UINT reg_idx, int *data, UINT count)
2790 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2791 HRESULT hr;
2793 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2795 wined3d_mutex_lock();
2796 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2797 wined3d_mutex_unlock();
2799 return hr;
2802 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2803 UINT reg_idx, const BOOL *data, UINT count)
2805 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2806 HRESULT hr;
2808 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2810 wined3d_mutex_lock();
2811 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2812 wined3d_mutex_unlock();
2814 return hr;
2817 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2818 UINT reg_idx, BOOL *data, UINT count)
2820 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2821 HRESULT hr;
2823 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2825 wined3d_mutex_lock();
2826 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2827 wined3d_mutex_unlock();
2829 return hr;
2832 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2833 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2835 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2836 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2837 HRESULT hr;
2839 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2840 iface, stream_idx, buffer, offset, stride);
2842 wined3d_mutex_lock();
2843 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2844 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2845 wined3d_mutex_unlock();
2847 return hr;
2850 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2851 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2853 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2854 struct d3d9_vertexbuffer *buffer_impl;
2855 struct wined3d_buffer *wined3d_buffer;
2856 HRESULT hr;
2858 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2859 iface, stream_idx, buffer, offset, stride);
2861 if (!buffer)
2862 return D3DERR_INVALIDCALL;
2864 wined3d_mutex_lock();
2865 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2866 if (SUCCEEDED(hr) && wined3d_buffer)
2868 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2869 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2870 IDirect3DVertexBuffer9_AddRef(*buffer);
2872 else
2874 if (FAILED(hr))
2875 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2876 *buffer = NULL;
2878 wined3d_mutex_unlock();
2880 return hr;
2883 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2885 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2886 HRESULT hr;
2888 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2890 wined3d_mutex_lock();
2891 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2892 wined3d_mutex_unlock();
2894 return hr;
2897 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2899 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2900 HRESULT hr;
2902 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2904 wined3d_mutex_lock();
2905 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2906 wined3d_mutex_unlock();
2908 return hr;
2911 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2913 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2914 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2916 TRACE("iface %p, buffer %p.\n", iface, buffer);
2918 wined3d_mutex_lock();
2919 wined3d_device_set_index_buffer(device->wined3d_device,
2920 ib ? ib->wined3d_buffer : NULL,
2921 ib ? ib->format : WINED3DFMT_UNKNOWN);
2922 wined3d_mutex_unlock();
2924 return D3D_OK;
2927 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2929 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2930 enum wined3d_format_id wined3d_format;
2931 struct wined3d_buffer *wined3d_buffer;
2932 struct d3d9_indexbuffer *buffer_impl;
2934 TRACE("iface %p, buffer %p.\n", iface, buffer);
2936 if (!buffer)
2937 return D3DERR_INVALIDCALL;
2939 wined3d_mutex_lock();
2940 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2942 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2943 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2944 IDirect3DIndexBuffer9_AddRef(*buffer);
2946 else
2948 *buffer = NULL;
2950 wined3d_mutex_unlock();
2952 return D3D_OK;
2955 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2956 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2958 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2959 struct d3d9_pixelshader *object;
2960 HRESULT hr;
2962 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2964 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2965 if (!object)
2967 FIXME("Failed to allocate pixel shader memory.\n");
2968 return E_OUTOFMEMORY;
2971 hr = pixelshader_init(object, device, byte_code);
2972 if (FAILED(hr))
2974 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2975 HeapFree(GetProcessHeap(), 0, object);
2976 return hr;
2979 TRACE("Created pixel shader %p.\n", object);
2980 *shader = &object->IDirect3DPixelShader9_iface;
2982 return D3D_OK;
2985 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2987 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2988 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2990 TRACE("iface %p, shader %p.\n", iface, shader);
2992 wined3d_mutex_lock();
2993 wined3d_device_set_pixel_shader(device->wined3d_device,
2994 shader_obj ? shader_obj->wined3d_shader : NULL);
2995 wined3d_mutex_unlock();
2997 return D3D_OK;
3000 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3002 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3003 struct d3d9_pixelshader *shader_impl;
3004 struct wined3d_shader *wined3d_shader;
3006 TRACE("iface %p, shader %p.\n", iface, shader);
3008 if (!shader) return D3DERR_INVALIDCALL;
3010 wined3d_mutex_lock();
3011 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3013 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3014 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3015 IDirect3DPixelShader9_AddRef(*shader);
3017 else
3019 *shader = NULL;
3021 wined3d_mutex_unlock();
3023 TRACE("Returning %p.\n", *shader);
3025 return D3D_OK;
3028 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3029 UINT reg_idx, const float *data, UINT count)
3031 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3032 HRESULT hr;
3034 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3036 wined3d_mutex_lock();
3037 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3038 wined3d_mutex_unlock();
3040 return hr;
3043 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3044 UINT reg_idx, float *data, UINT count)
3046 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3047 HRESULT hr;
3049 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3051 wined3d_mutex_lock();
3052 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3053 wined3d_mutex_unlock();
3055 return hr;
3058 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3059 UINT reg_idx, const int *data, UINT count)
3061 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3062 HRESULT hr;
3064 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3066 wined3d_mutex_lock();
3067 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3068 wined3d_mutex_unlock();
3070 return hr;
3073 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3074 UINT reg_idx, int *data, UINT count)
3076 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3077 HRESULT hr;
3079 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3081 wined3d_mutex_lock();
3082 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3083 wined3d_mutex_unlock();
3085 return hr;
3088 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3089 UINT reg_idx, const BOOL *data, UINT count)
3091 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3092 HRESULT hr;
3094 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3096 wined3d_mutex_lock();
3097 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3098 wined3d_mutex_unlock();
3100 return hr;
3103 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3104 UINT reg_idx, BOOL *data, UINT count)
3106 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3107 HRESULT hr;
3109 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3111 wined3d_mutex_lock();
3112 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3113 wined3d_mutex_unlock();
3115 return hr;
3118 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3119 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3121 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3122 iface, handle, segment_count, patch_info);
3123 return D3D_OK;
3126 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3127 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3129 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3130 iface, handle, segment_count, patch_info);
3131 return D3D_OK;
3134 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3136 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3137 return D3DERR_INVALIDCALL;
3140 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3142 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3143 struct d3d9_query *object;
3144 HRESULT hr;
3146 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3148 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3149 if (!object)
3150 return E_OUTOFMEMORY;
3152 hr = query_init(object, device, type);
3153 if (FAILED(hr))
3155 WARN("Failed to initialize query, hr %#x.\n", hr);
3156 HeapFree(GetProcessHeap(), 0, object);
3157 return hr;
3160 TRACE("Created query %p.\n", object);
3161 if (query) *query = &object->IDirect3DQuery9_iface;
3162 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3164 return D3D_OK;
3167 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3168 UINT width, UINT height, float *rows, float *columns)
3170 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3171 iface, width, height, rows, columns);
3173 return E_NOTIMPL;
3176 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3177 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3178 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3180 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3181 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3182 iface, src_surface, dst_surface, src_descs, rect_count,
3183 dst_descs, operation, offset_x, offset_y);
3185 return E_NOTIMPL;
3188 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3189 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3190 const RGNDATA *dirty_region, DWORD flags)
3192 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3193 HRESULT hr;
3195 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3196 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3197 dst_window_override, dirty_region, flags);
3199 if (device->device_state != D3D9_DEVICE_STATE_OK)
3200 return S_PRESENT_OCCLUDED;
3202 wined3d_mutex_lock();
3203 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
3204 dst_window_override, dirty_region, flags);
3205 wined3d_mutex_unlock();
3207 return hr;
3210 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3212 FIXME("iface %p, priority %p stub!\n", iface, priority);
3214 return E_NOTIMPL;
3217 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3219 FIXME("iface %p, priority %d stub!\n", iface, priority);
3221 return E_NOTIMPL;
3224 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3226 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3228 return E_NOTIMPL;
3231 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3232 IDirect3DResource9 **resources, UINT32 resource_count)
3234 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3235 iface, resources, resource_count);
3237 return E_NOTIMPL;
3240 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3242 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3244 return E_NOTIMPL;
3247 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3249 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3251 *max_latency = 2;
3253 return E_NOTIMPL;
3256 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3258 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3259 struct wined3d_swapchain_desc swapchain_desc;
3260 struct wined3d_swapchain *swapchain;
3262 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3264 wined3d_mutex_lock();
3265 swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3266 wined3d_swapchain_get_desc(swapchain, &swapchain_desc);
3267 wined3d_mutex_unlock();
3269 if (swapchain_desc.windowed)
3270 return D3D_OK;
3272 /* FIXME: This is actually supposed to check if any other device is in
3273 * fullscreen mode. */
3274 if (dst_window != swapchain_desc.device_window)
3275 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3277 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3280 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3281 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3282 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3284 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3285 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3286 iface, width, height, format, multisample_type, multisample_quality,
3287 lockable, surface, shared_handle, usage);
3289 *surface = NULL;
3290 if (shared_handle)
3291 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3293 return E_NOTIMPL;
3296 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3297 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3298 HANDLE *shared_handle, DWORD usage)
3300 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3301 iface, width, height, format, pool, surface, shared_handle, usage);
3303 return E_NOTIMPL;
3306 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3307 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3308 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3310 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3311 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3312 iface, width, height, format, multisample_type, multisample_quality,
3313 discard, surface, shared_handle, usage);
3315 *surface = NULL;
3316 if (shared_handle)
3317 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3319 return E_NOTIMPL;
3322 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3323 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3325 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3327 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3329 return d3d9_device_reset(device, present_parameters, mode);
3332 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3333 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3335 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3336 struct wined3d_display_mode wined3d_mode;
3337 HRESULT hr;
3339 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3340 iface, swapchain_idx, mode, rotation);
3342 if (mode->Size != sizeof(*mode))
3343 return D3DERR_INVALIDCALL;
3345 wined3d_mutex_lock();
3346 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3347 (enum wined3d_display_rotation *)rotation);
3348 wined3d_mutex_unlock();
3350 if (SUCCEEDED(hr))
3352 mode->Width = wined3d_mode.width;
3353 mode->Height = wined3d_mode.height;
3354 mode->RefreshRate = wined3d_mode.refresh_rate;
3355 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3356 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3359 return hr;
3362 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3364 /* IUnknown */
3365 d3d9_device_QueryInterface,
3366 d3d9_device_AddRef,
3367 d3d9_device_Release,
3368 /* IDirect3DDevice9 */
3369 d3d9_device_TestCooperativeLevel,
3370 d3d9_device_GetAvailableTextureMem,
3371 d3d9_device_EvictManagedResources,
3372 d3d9_device_GetDirect3D,
3373 d3d9_device_GetDeviceCaps,
3374 d3d9_device_GetDisplayMode,
3375 d3d9_device_GetCreationParameters,
3376 d3d9_device_SetCursorProperties,
3377 d3d9_device_SetCursorPosition,
3378 d3d9_device_ShowCursor,
3379 d3d9_device_CreateAdditionalSwapChain,
3380 d3d9_device_GetSwapChain,
3381 d3d9_device_GetNumberOfSwapChains,
3382 d3d9_device_Reset,
3383 d3d9_device_Present,
3384 d3d9_device_GetBackBuffer,
3385 d3d9_device_GetRasterStatus,
3386 d3d9_device_SetDialogBoxMode,
3387 d3d9_device_SetGammaRamp,
3388 d3d9_device_GetGammaRamp,
3389 d3d9_device_CreateTexture,
3390 d3d9_device_CreateVolumeTexture,
3391 d3d9_device_CreateCubeTexture,
3392 d3d9_device_CreateVertexBuffer,
3393 d3d9_device_CreateIndexBuffer,
3394 d3d9_device_CreateRenderTarget,
3395 d3d9_device_CreateDepthStencilSurface,
3396 d3d9_device_UpdateSurface,
3397 d3d9_device_UpdateTexture,
3398 d3d9_device_GetRenderTargetData,
3399 d3d9_device_GetFrontBufferData,
3400 d3d9_device_StretchRect,
3401 d3d9_device_ColorFill,
3402 d3d9_device_CreateOffscreenPlainSurface,
3403 d3d9_device_SetRenderTarget,
3404 d3d9_device_GetRenderTarget,
3405 d3d9_device_SetDepthStencilSurface,
3406 d3d9_device_GetDepthStencilSurface,
3407 d3d9_device_BeginScene,
3408 d3d9_device_EndScene,
3409 d3d9_device_Clear,
3410 d3d9_device_SetTransform,
3411 d3d9_device_GetTransform,
3412 d3d9_device_MultiplyTransform,
3413 d3d9_device_SetViewport,
3414 d3d9_device_GetViewport,
3415 d3d9_device_SetMaterial,
3416 d3d9_device_GetMaterial,
3417 d3d9_device_SetLight,
3418 d3d9_device_GetLight,
3419 d3d9_device_LightEnable,
3420 d3d9_device_GetLightEnable,
3421 d3d9_device_SetClipPlane,
3422 d3d9_device_GetClipPlane,
3423 d3d9_device_SetRenderState,
3424 d3d9_device_GetRenderState,
3425 d3d9_device_CreateStateBlock,
3426 d3d9_device_BeginStateBlock,
3427 d3d9_device_EndStateBlock,
3428 d3d9_device_SetClipStatus,
3429 d3d9_device_GetClipStatus,
3430 d3d9_device_GetTexture,
3431 d3d9_device_SetTexture,
3432 d3d9_device_GetTextureStageState,
3433 d3d9_device_SetTextureStageState,
3434 d3d9_device_GetSamplerState,
3435 d3d9_device_SetSamplerState,
3436 d3d9_device_ValidateDevice,
3437 d3d9_device_SetPaletteEntries,
3438 d3d9_device_GetPaletteEntries,
3439 d3d9_device_SetCurrentTexturePalette,
3440 d3d9_device_GetCurrentTexturePalette,
3441 d3d9_device_SetScissorRect,
3442 d3d9_device_GetScissorRect,
3443 d3d9_device_SetSoftwareVertexProcessing,
3444 d3d9_device_GetSoftwareVertexProcessing,
3445 d3d9_device_SetNPatchMode,
3446 d3d9_device_GetNPatchMode,
3447 d3d9_device_DrawPrimitive,
3448 d3d9_device_DrawIndexedPrimitive,
3449 d3d9_device_DrawPrimitiveUP,
3450 d3d9_device_DrawIndexedPrimitiveUP,
3451 d3d9_device_ProcessVertices,
3452 d3d9_device_CreateVertexDeclaration,
3453 d3d9_device_SetVertexDeclaration,
3454 d3d9_device_GetVertexDeclaration,
3455 d3d9_device_SetFVF,
3456 d3d9_device_GetFVF,
3457 d3d9_device_CreateVertexShader,
3458 d3d9_device_SetVertexShader,
3459 d3d9_device_GetVertexShader,
3460 d3d9_device_SetVertexShaderConstantF,
3461 d3d9_device_GetVertexShaderConstantF,
3462 d3d9_device_SetVertexShaderConstantI,
3463 d3d9_device_GetVertexShaderConstantI,
3464 d3d9_device_SetVertexShaderConstantB,
3465 d3d9_device_GetVertexShaderConstantB,
3466 d3d9_device_SetStreamSource,
3467 d3d9_device_GetStreamSource,
3468 d3d9_device_SetStreamSourceFreq,
3469 d3d9_device_GetStreamSourceFreq,
3470 d3d9_device_SetIndices,
3471 d3d9_device_GetIndices,
3472 d3d9_device_CreatePixelShader,
3473 d3d9_device_SetPixelShader,
3474 d3d9_device_GetPixelShader,
3475 d3d9_device_SetPixelShaderConstantF,
3476 d3d9_device_GetPixelShaderConstantF,
3477 d3d9_device_SetPixelShaderConstantI,
3478 d3d9_device_GetPixelShaderConstantI,
3479 d3d9_device_SetPixelShaderConstantB,
3480 d3d9_device_GetPixelShaderConstantB,
3481 d3d9_device_DrawRectPatch,
3482 d3d9_device_DrawTriPatch,
3483 d3d9_device_DeletePatch,
3484 d3d9_device_CreateQuery,
3485 /* IDirect3DDevice9Ex */
3486 d3d9_device_SetConvolutionMonoKernel,
3487 d3d9_device_ComposeRects,
3488 d3d9_device_PresentEx,
3489 d3d9_device_GetGPUThreadPriority,
3490 d3d9_device_SetGPUThreadPriority,
3491 d3d9_device_WaitForVBlank,
3492 d3d9_device_CheckResourceResidency,
3493 d3d9_device_SetMaximumFrameLatency,
3494 d3d9_device_GetMaximumFrameLatency,
3495 d3d9_device_CheckDeviceState,
3496 d3d9_device_CreateRenderTargetEx,
3497 d3d9_device_CreateOffscreenPlainSurfaceEx,
3498 d3d9_device_CreateDepthStencilSurfaceEx,
3499 d3d9_device_ResetEx,
3500 d3d9_device_GetDisplayModeEx,
3503 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3505 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3508 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3509 struct wined3d_device *device)
3511 TRACE("device_parent %p, device %p.\n", device_parent, device);
3514 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3516 TRACE("device_parent %p.\n", device_parent);
3519 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3521 struct d3d9_device *device = device_from_device_parent(device_parent);
3523 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3525 if (!device->d3d_parent)
3526 return;
3528 if (!activate)
3529 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3530 else if (device->d3d_parent->extended)
3531 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3532 else
3533 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3536 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3537 void *container_parent, struct wined3d_surface *surface, void **parent,
3538 const struct wined3d_parent_ops **parent_ops)
3540 struct d3d9_surface *d3d_surface;
3542 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3543 device_parent, container_parent, surface, parent, parent_ops);
3545 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3546 return E_OUTOFMEMORY;
3548 surface_init(d3d_surface, container_parent, surface, parent_ops);
3549 *parent = d3d_surface;
3550 TRACE("Created surface %p.\n", d3d_surface);
3552 return D3D_OK;
3555 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3556 void *container_parent, struct wined3d_volume *volume, void **parent,
3557 const struct wined3d_parent_ops **parent_ops)
3559 struct d3d9_volume *d3d_volume;
3561 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3562 device_parent, container_parent, volume, parent, parent_ops);
3564 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3565 return E_OUTOFMEMORY;
3567 volume_init(d3d_volume, container_parent, volume, parent_ops);
3568 *parent = d3d_volume;
3569 TRACE("Created volume %p.\n", d3d_volume);
3571 return D3D_OK;
3574 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3575 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3577 struct d3d9_device *device = device_from_device_parent(device_parent);
3578 struct d3d9_surface *d3d_surface;
3579 HRESULT hr;
3581 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3582 device_parent, container_parent, desc, texture);
3584 if (container_parent == device_parent)
3585 container_parent = &device->IDirect3DDevice9Ex_iface;
3587 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1,
3588 WINED3D_SURFACE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3590 WARN("Failed to create texture, hr %#x.\n", hr);
3591 return hr;
3594 d3d_surface = wined3d_resource_get_parent(wined3d_texture_get_sub_resource(*texture, 0));
3595 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3597 return hr;
3600 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3601 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3603 struct d3d9_device *device = device_from_device_parent(device_parent);
3604 struct d3d9_swapchain *d3d_swapchain;
3605 HRESULT hr;
3607 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3609 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3610 if (FAILED(hr))
3612 WARN("Failed to create swapchain, hr %#x.\n", hr);
3613 *swapchain = NULL;
3614 return hr;
3617 *swapchain = d3d_swapchain->wined3d_swapchain;
3618 wined3d_swapchain_incref(*swapchain);
3619 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3621 return hr;
3624 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3626 device_parent_wined3d_device_created,
3627 device_parent_mode_changed,
3628 device_parent_activate,
3629 device_parent_surface_created,
3630 device_parent_volume_created,
3631 device_parent_create_swapchain_texture,
3632 device_parent_create_swapchain,
3635 static void setup_fpu(void)
3637 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3638 WORD cw;
3639 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3640 cw = (cw & ~0xf3f) | 0x3f;
3641 __asm__ volatile ("fldcw %0" : : "m" (cw));
3642 #elif defined(__i386__) && defined(_MSC_VER)
3643 WORD cw;
3644 __asm fnstcw cw;
3645 cw = (cw & ~0xf3f) | 0x3f;
3646 __asm fldcw cw;
3647 #else
3648 FIXME("FPU setup not implemented for this platform.\n");
3649 #endif
3652 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3653 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3654 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3656 struct wined3d_swapchain_desc *swapchain_desc;
3657 UINT i, count = 1;
3658 HRESULT hr;
3660 if (mode)
3661 FIXME("Ignoring display mode.\n");
3663 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3664 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3665 device->refcount = 1;
3667 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3669 wined3d_mutex_lock();
3670 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3671 &device->device_parent, &device->wined3d_device);
3672 if (FAILED(hr))
3674 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3675 wined3d_mutex_unlock();
3676 return hr;
3679 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3681 WINED3DCAPS caps;
3683 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3684 count = caps.NumberOfAdaptersInGroup;
3687 if (flags & D3DCREATE_MULTITHREADED)
3688 wined3d_device_set_multithreaded(device->wined3d_device);
3690 if (!parameters->Windowed)
3692 if (!focus_window)
3693 focus_window = parameters->hDeviceWindow;
3694 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3696 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3697 wined3d_device_decref(device->wined3d_device);
3698 wined3d_mutex_unlock();
3699 return hr;
3702 for (i = 0; i < count; ++i)
3704 HWND device_window = parameters[i].hDeviceWindow;
3706 if (!device_window) device_window = focus_window;
3707 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3708 parameters[i].BackBufferWidth,
3709 parameters[i].BackBufferHeight);
3713 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3714 if (!swapchain_desc)
3716 ERR("Failed to allocate wined3d parameters.\n");
3717 wined3d_device_release_focus_window(device->wined3d_device);
3718 wined3d_device_decref(device->wined3d_device);
3719 wined3d_mutex_unlock();
3720 return E_OUTOFMEMORY;
3723 for (i = 0; i < count; ++i)
3725 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3726 parent->extended))
3728 wined3d_device_release_focus_window(device->wined3d_device);
3729 wined3d_device_decref(device->wined3d_device);
3730 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3731 wined3d_mutex_unlock();
3732 return D3DERR_INVALIDCALL;
3736 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3737 if (FAILED(hr))
3739 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3740 wined3d_device_release_focus_window(device->wined3d_device);
3741 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3742 wined3d_device_decref(device->wined3d_device);
3743 wined3d_mutex_unlock();
3744 return hr;
3747 wined3d_mutex_unlock();
3749 for (i = 0; i < count; ++i)
3751 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3753 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3755 /* Initialize the converted declaration array. This creates a valid pointer
3756 * and when adding decls HeapReAlloc() can be used without further checking. */
3757 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3758 if (!device->fvf_decls)
3760 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3761 wined3d_mutex_lock();
3762 wined3d_device_uninit_3d(device->wined3d_device);
3763 wined3d_device_release_focus_window(device->wined3d_device);
3764 wined3d_device_decref(device->wined3d_device);
3765 wined3d_mutex_unlock();
3766 return E_OUTOFMEMORY;
3769 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3770 device->d3d_parent = parent;
3772 return D3D_OK;