d3d9: Validate swap effect and backbuffer count.
[wine.git] / dlls / d3d9 / device.c
blob95d1e02910a3c8412b57da3c1520d5b0696e049b
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_surface *wined3d_surface = NULL;
750 struct d3d9_surface *surface_impl;
751 HRESULT hr;
753 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
754 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
756 wined3d_mutex_lock();
757 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
758 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
759 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
761 surface_impl = wined3d_surface_get_parent(wined3d_surface);
762 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
763 IDirect3DSurface9_AddRef(*backbuffer);
765 wined3d_mutex_unlock();
767 return hr;
769 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
770 UINT swapchain, D3DRASTER_STATUS *raster_status)
772 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
773 HRESULT hr;
775 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
777 wined3d_mutex_lock();
778 hr = wined3d_device_get_raster_status(device->wined3d_device,
779 swapchain, (struct wined3d_raster_status *)raster_status);
780 wined3d_mutex_unlock();
782 return hr;
785 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
787 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
788 HRESULT hr;
790 TRACE("iface %p, enable %#x.\n", iface, enable);
792 wined3d_mutex_lock();
793 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
794 wined3d_mutex_unlock();
796 return hr;
799 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
800 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
802 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
804 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
806 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
807 wined3d_mutex_lock();
808 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
809 wined3d_mutex_unlock();
812 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
814 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
816 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
818 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
819 wined3d_mutex_lock();
820 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
821 wined3d_mutex_unlock();
824 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
825 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
826 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
828 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
829 struct d3d9_texture *object;
830 BOOL set_mem = FALSE;
831 HRESULT hr;
833 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
834 iface, width, height, levels, usage, format, pool, texture, shared_handle);
836 *texture = NULL;
837 if (shared_handle)
839 if (!device->d3d_parent->extended)
841 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
842 return E_NOTIMPL;
845 if (pool == D3DPOOL_SYSTEMMEM)
847 if (levels != 1)
848 return D3DERR_INVALIDCALL;
849 set_mem = TRUE;
851 else
853 if (pool != D3DPOOL_DEFAULT)
855 WARN("Trying to create a shared texture in pool %#x.\n", pool);
856 return D3DERR_INVALIDCALL;
858 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
862 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
863 if (!object)
864 return D3DERR_OUTOFVIDEOMEMORY;
866 hr = texture_init(object, device, width, height, levels, usage, format, pool);
867 if (FAILED(hr))
869 WARN("Failed to initialize texture, hr %#x.\n", hr);
870 HeapFree(GetProcessHeap(), 0, object);
871 return hr;
874 if (set_mem)
875 wined3d_texture_update_desc(object->wined3d_texture, width, height,
876 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
877 *shared_handle, 0);
879 TRACE("Created texture %p.\n", object);
880 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
882 return D3D_OK;
885 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
886 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
887 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
889 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
890 struct d3d9_texture *object;
891 HRESULT hr;
893 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
894 iface, width, height, depth, levels);
895 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
896 usage, format, pool, texture, shared_handle);
898 *texture = NULL;
899 if (shared_handle)
901 if (!device->d3d_parent->extended)
903 WARN("Trying to create a shared volume texture on a non-ex device.\n");
904 return E_NOTIMPL;
907 if (pool != D3DPOOL_DEFAULT)
909 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
910 return D3DERR_INVALIDCALL;
912 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
915 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
916 if (!object)
917 return D3DERR_OUTOFVIDEOMEMORY;
919 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
920 if (FAILED(hr))
922 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
923 HeapFree(GetProcessHeap(), 0, object);
924 return hr;
927 TRACE("Created volume texture %p.\n", object);
928 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
930 return D3D_OK;
933 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
934 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
935 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
937 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
938 struct d3d9_texture *object;
939 HRESULT hr;
941 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
942 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
944 *texture = NULL;
945 if (shared_handle)
947 if (!device->d3d_parent->extended)
949 WARN("Trying to create a shared cube texture on a non-ex device.\n");
950 return E_NOTIMPL;
953 if (pool != D3DPOOL_DEFAULT)
955 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
956 return D3DERR_INVALIDCALL;
958 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
961 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
962 if (!object)
963 return D3DERR_OUTOFVIDEOMEMORY;
965 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
966 if (FAILED(hr))
968 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
969 HeapFree(GetProcessHeap(), 0, object);
970 return hr;
973 TRACE("Created cube texture %p.\n", object);
974 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
976 return D3D_OK;
979 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
980 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
981 HANDLE *shared_handle)
983 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
984 struct d3d9_vertexbuffer *object;
985 HRESULT hr;
987 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
988 iface, size, usage, fvf, pool, buffer, shared_handle);
990 if (shared_handle)
992 if (!device->d3d_parent->extended)
994 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
995 return E_NOTIMPL;
998 if (pool != D3DPOOL_DEFAULT)
1000 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1001 return D3DERR_NOTAVAILABLE;
1003 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1006 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1007 if (!object)
1008 return D3DERR_OUTOFVIDEOMEMORY;
1010 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1011 if (FAILED(hr))
1013 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1014 HeapFree(GetProcessHeap(), 0, object);
1015 return hr;
1018 TRACE("Created vertex buffer %p.\n", object);
1019 *buffer = &object->IDirect3DVertexBuffer9_iface;
1021 return D3D_OK;
1024 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1025 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1026 HANDLE *shared_handle)
1028 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1029 struct d3d9_indexbuffer *object;
1030 HRESULT hr;
1032 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1033 iface, size, usage, format, pool, buffer, shared_handle);
1035 if (shared_handle)
1037 if (!device->d3d_parent->extended)
1039 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1040 return E_NOTIMPL;
1043 if (pool != D3DPOOL_DEFAULT)
1045 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1046 return D3DERR_NOTAVAILABLE;
1048 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1051 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1052 if (!object)
1053 return D3DERR_OUTOFVIDEOMEMORY;
1055 hr = indexbuffer_init(object, device, size, usage, format, pool);
1056 if (FAILED(hr))
1058 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1059 HeapFree(GetProcessHeap(), 0, object);
1060 return hr;
1063 TRACE("Created index buffer %p.\n", object);
1064 *buffer = &object->IDirect3DIndexBuffer9_iface;
1066 return D3D_OK;
1069 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1070 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1071 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1073 struct wined3d_resource *sub_resource;
1074 struct wined3d_resource_desc desc;
1075 struct d3d9_surface *surface_impl;
1076 struct wined3d_texture *texture;
1077 HRESULT hr;
1079 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1080 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1081 device, width, height, format, flags, surface, usage, pool,
1082 multisample_type, multisample_quality);
1084 desc.resource_type = WINED3D_RTYPE_TEXTURE;
1085 desc.format = wined3dformat_from_d3dformat(format);
1086 desc.multisample_type = multisample_type;
1087 desc.multisample_quality = multisample_quality;
1088 desc.usage = usage & WINED3DUSAGE_MASK;
1089 desc.pool = pool;
1090 desc.width = width;
1091 desc.height = height;
1092 desc.depth = 1;
1093 desc.size = 0;
1095 wined3d_mutex_lock();
1097 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1098 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1100 wined3d_mutex_unlock();
1101 WARN("Failed to create texture, hr %#x.\n", hr);
1102 return hr;
1105 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1106 surface_impl = wined3d_resource_get_parent(sub_resource);
1107 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1108 *surface = &surface_impl->IDirect3DSurface9_iface;
1109 IDirect3DSurface9_AddRef(*surface);
1111 if (user_mem)
1112 wined3d_texture_update_desc(texture, width, height,
1113 desc.format, multisample_type, multisample_quality, user_mem, 0);
1115 wined3d_texture_decref(texture);
1117 wined3d_mutex_unlock();
1119 return D3D_OK;
1122 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1123 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1124 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1126 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1127 DWORD flags = 0;
1129 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1130 "lockable %#x, surface %p, shared_handle %p.\n",
1131 iface, width, height, format, multisample_type, multisample_quality,
1132 lockable, surface, shared_handle);
1134 *surface = NULL;
1135 if (shared_handle)
1137 if (!device->d3d_parent->extended)
1139 WARN("Trying to create a shared render target on a non-ex device.\n");
1140 return E_NOTIMPL;
1143 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1146 if (lockable)
1147 flags |= WINED3D_SURFACE_MAPPABLE;
1149 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1150 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1153 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1154 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1155 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1157 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1158 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1160 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1161 "discard %#x, surface %p, shared_handle %p.\n",
1162 iface, width, height, format, multisample_type, multisample_quality,
1163 discard, surface, shared_handle);
1165 *surface = NULL;
1166 if (shared_handle)
1168 if (!device->d3d_parent->extended)
1170 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1171 return E_NOTIMPL;
1174 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1177 if (discard)
1178 flags |= WINED3D_SURFACE_DISCARD;
1180 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1181 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1185 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1186 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1187 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1189 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1190 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1191 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1192 HRESULT hr;
1194 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1195 iface, src_surface, src_rect, dst_surface, dst_point);
1197 wined3d_mutex_lock();
1198 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1199 dst->wined3d_surface, dst_point);
1200 wined3d_mutex_unlock();
1202 return hr;
1205 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1206 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1208 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1209 struct d3d9_texture *src_impl, *dst_impl;
1210 HRESULT hr;
1212 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1214 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1215 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1217 wined3d_mutex_lock();
1218 hr = wined3d_device_update_texture(device->wined3d_device,
1219 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1220 wined3d_mutex_unlock();
1222 return hr;
1225 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1226 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1228 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1229 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1230 HRESULT hr;
1232 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1234 wined3d_mutex_lock();
1235 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1236 wined3d_mutex_unlock();
1238 return hr;
1241 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1242 UINT swapchain, IDirect3DSurface9 *dst_surface)
1244 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1245 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1246 HRESULT hr;
1248 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1250 wined3d_mutex_lock();
1251 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1252 wined3d_mutex_unlock();
1254 return hr;
1257 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1258 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1260 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1261 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1262 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1263 HRESULT hr = D3DERR_INVALIDCALL;
1264 struct wined3d_resource_desc src_desc, dst_desc;
1265 struct wined3d_resource *wined3d_resource;
1267 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1268 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1270 wined3d_mutex_lock();
1271 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1272 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1274 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1275 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1277 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1279 if (device->in_scene)
1281 WARN("Rejecting depth / stencil blit while in scene.\n");
1282 goto done;
1285 if (src_rect)
1287 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1288 || src_rect->bottom != src_desc.height)
1290 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1291 wine_dbgstr_rect(src_rect));
1292 goto done;
1295 if (dst_rect)
1297 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1298 || dst_rect->bottom != dst_desc.height)
1300 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1301 wine_dbgstr_rect(dst_rect));
1302 goto done;
1305 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1307 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1308 goto done;
1312 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1313 if (hr == WINEDDERR_INVALIDRECT)
1314 hr = D3DERR_INVALIDCALL;
1316 done:
1317 wined3d_mutex_unlock();
1318 return hr;
1321 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1322 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1324 const struct wined3d_color c =
1326 ((color >> 16) & 0xff) / 255.0f,
1327 ((color >> 8) & 0xff) / 255.0f,
1328 (color & 0xff) / 255.0f,
1329 ((color >> 24) & 0xff) / 255.0f,
1331 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1332 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1333 struct wined3d_resource *wined3d_resource;
1334 struct wined3d_resource_desc desc;
1335 HRESULT hr;
1337 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1339 wined3d_mutex_lock();
1341 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1342 wined3d_resource_get_desc(wined3d_resource, &desc);
1344 if (desc.pool != WINED3D_POOL_DEFAULT)
1346 wined3d_mutex_unlock();
1347 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1348 return D3DERR_INVALIDCALL;
1350 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1352 wined3d_mutex_unlock();
1353 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1354 return D3DERR_INVALIDCALL;
1356 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1358 wined3d_mutex_unlock();
1359 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1360 return D3DERR_INVALIDCALL;
1363 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1364 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1366 wined3d_mutex_unlock();
1368 return hr;
1371 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1372 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1373 HANDLE *shared_handle)
1375 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1376 void *user_mem = NULL;
1378 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1379 iface, width, height, format, pool, surface, shared_handle);
1381 *surface = NULL;
1382 if (pool == D3DPOOL_MANAGED)
1384 WARN("Attempting to create a managed offscreen plain surface.\n");
1385 return D3DERR_INVALIDCALL;
1388 if (shared_handle)
1390 if (!device->d3d_parent->extended)
1392 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1393 return E_NOTIMPL;
1396 if (pool == D3DPOOL_SYSTEMMEM)
1397 user_mem = *shared_handle;
1398 else
1400 if (pool != D3DPOOL_DEFAULT)
1402 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1403 return D3DERR_INVALIDCALL;
1405 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1409 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1410 * regardless of the pool they're created in. Should we set dynamic usage
1411 * here? */
1412 return d3d9_device_create_surface(device, width, height, format,
1413 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1416 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1418 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1419 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1420 HRESULT hr;
1422 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1424 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1426 WARN("Invalid index %u specified.\n", idx);
1427 return D3DERR_INVALIDCALL;
1430 if (!idx && !surface_impl)
1432 WARN("Trying to set render target 0 to NULL.\n");
1433 return D3DERR_INVALIDCALL;
1436 wined3d_mutex_lock();
1437 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1438 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1439 wined3d_mutex_unlock();
1441 return hr;
1444 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1446 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1447 struct wined3d_rendertarget_view *wined3d_rtv;
1448 struct d3d9_surface *surface_impl;
1449 HRESULT hr = D3D_OK;
1451 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1453 if (!surface)
1454 return D3DERR_INVALIDCALL;
1456 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1458 WARN("Invalid index %u specified.\n", idx);
1459 return D3DERR_INVALIDCALL;
1462 wined3d_mutex_lock();
1463 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1465 /* We want the sub resource parent here, since the view itself may be
1466 * internal to wined3d and may not have a parent. */
1467 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1468 *surface = &surface_impl->IDirect3DSurface9_iface;
1469 IDirect3DSurface9_AddRef(*surface);
1471 else
1473 hr = WINED3DERR_NOTFOUND;
1474 *surface = NULL;
1476 wined3d_mutex_unlock();
1478 return hr;
1481 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1483 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1484 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1486 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1488 wined3d_mutex_lock();
1489 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1490 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1491 wined3d_mutex_unlock();
1493 return D3D_OK;
1496 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1498 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1499 struct wined3d_rendertarget_view *wined3d_dsv;
1500 struct d3d9_surface *surface_impl;
1501 HRESULT hr = D3D_OK;
1503 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1505 if (!depth_stencil)
1506 return D3DERR_INVALIDCALL;
1508 wined3d_mutex_lock();
1509 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1511 /* We want the sub resource parent here, since the view itself may be
1512 * internal to wined3d and may not have a parent. */
1513 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1514 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1515 IDirect3DSurface9_AddRef(*depth_stencil);
1517 else
1519 hr = WINED3DERR_NOTFOUND;
1520 *depth_stencil = NULL;
1522 wined3d_mutex_unlock();
1524 return hr;
1527 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1529 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1530 HRESULT hr;
1532 TRACE("iface %p.\n", iface);
1534 wined3d_mutex_lock();
1535 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1536 device->in_scene = TRUE;
1537 wined3d_mutex_unlock();
1539 return hr;
1542 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1544 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1545 HRESULT hr;
1547 TRACE("iface %p.\n", iface);
1549 wined3d_mutex_lock();
1550 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1551 device->in_scene = FALSE;
1552 wined3d_mutex_unlock();
1554 return hr;
1557 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1558 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1560 const struct wined3d_color c =
1562 ((color >> 16) & 0xff) / 255.0f,
1563 ((color >> 8) & 0xff) / 255.0f,
1564 (color & 0xff) / 255.0f,
1565 ((color >> 24) & 0xff) / 255.0f,
1567 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1568 HRESULT hr;
1570 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1571 iface, rect_count, rects, flags, color, z, stencil);
1573 wined3d_mutex_lock();
1574 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1575 wined3d_mutex_unlock();
1577 return hr;
1580 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1581 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1583 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1585 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1587 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1588 wined3d_mutex_lock();
1589 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1590 wined3d_mutex_unlock();
1592 return D3D_OK;
1595 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1596 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1598 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1600 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1602 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1603 wined3d_mutex_lock();
1604 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1605 wined3d_mutex_unlock();
1607 return D3D_OK;
1610 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1611 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1613 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1615 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1617 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1618 wined3d_mutex_lock();
1619 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1620 wined3d_mutex_unlock();
1622 return D3D_OK;
1625 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1627 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1629 TRACE("iface %p, viewport %p.\n", iface, viewport);
1631 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1632 wined3d_mutex_lock();
1633 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1634 wined3d_mutex_unlock();
1636 return D3D_OK;
1639 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1641 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1643 TRACE("iface %p, viewport %p.\n", iface, viewport);
1645 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1646 wined3d_mutex_lock();
1647 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1648 wined3d_mutex_unlock();
1650 return D3D_OK;
1653 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1655 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1657 TRACE("iface %p, material %p.\n", iface, material);
1659 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1660 wined3d_mutex_lock();
1661 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1662 wined3d_mutex_unlock();
1664 return D3D_OK;
1667 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1669 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1671 TRACE("iface %p, material %p.\n", iface, material);
1673 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1674 wined3d_mutex_lock();
1675 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1676 wined3d_mutex_unlock();
1678 return D3D_OK;
1681 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1683 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1684 HRESULT hr;
1686 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1688 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1689 wined3d_mutex_lock();
1690 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1691 wined3d_mutex_unlock();
1693 return hr;
1696 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1698 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1699 HRESULT hr;
1701 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1703 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1704 wined3d_mutex_lock();
1705 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1706 wined3d_mutex_unlock();
1708 return hr;
1711 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1713 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1714 HRESULT hr;
1716 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1718 wined3d_mutex_lock();
1719 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1720 wined3d_mutex_unlock();
1722 return hr;
1725 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1727 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1728 HRESULT hr;
1730 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1732 wined3d_mutex_lock();
1733 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1734 wined3d_mutex_unlock();
1736 return hr;
1739 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1741 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1742 HRESULT hr;
1744 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1746 wined3d_mutex_lock();
1747 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1748 wined3d_mutex_unlock();
1750 return hr;
1753 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1755 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1756 HRESULT hr;
1758 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1760 wined3d_mutex_lock();
1761 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1762 wined3d_mutex_unlock();
1764 return hr;
1767 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1768 D3DRENDERSTATETYPE state, DWORD value)
1770 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1772 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1774 wined3d_mutex_lock();
1775 wined3d_device_set_render_state(device->wined3d_device, state, value);
1776 wined3d_mutex_unlock();
1778 return D3D_OK;
1781 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1782 D3DRENDERSTATETYPE state, DWORD *value)
1784 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1786 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1788 wined3d_mutex_lock();
1789 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1790 wined3d_mutex_unlock();
1792 return D3D_OK;
1795 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1796 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1798 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1799 struct d3d9_stateblock *object;
1800 HRESULT hr;
1802 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1804 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1806 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1807 return D3DERR_INVALIDCALL;
1810 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1811 if (!object)
1812 return E_OUTOFMEMORY;
1814 hr = stateblock_init(object, device, type, NULL);
1815 if (FAILED(hr))
1817 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1818 HeapFree(GetProcessHeap(), 0, object);
1819 return hr;
1822 TRACE("Created stateblock %p.\n", object);
1823 *stateblock = &object->IDirect3DStateBlock9_iface;
1825 return D3D_OK;
1828 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1830 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1831 HRESULT hr;
1833 TRACE("iface %p.\n", iface);
1835 wined3d_mutex_lock();
1836 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1837 wined3d_mutex_unlock();
1839 return hr;
1842 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1844 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1845 struct wined3d_stateblock *wined3d_stateblock;
1846 struct d3d9_stateblock *object;
1847 HRESULT hr;
1849 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1851 wined3d_mutex_lock();
1852 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1853 wined3d_mutex_unlock();
1854 if (FAILED(hr))
1856 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1857 return hr;
1860 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1861 if (!object)
1863 wined3d_mutex_lock();
1864 wined3d_stateblock_decref(wined3d_stateblock);
1865 wined3d_mutex_unlock();
1866 return E_OUTOFMEMORY;
1869 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1870 if (FAILED(hr))
1872 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1873 wined3d_mutex_lock();
1874 wined3d_stateblock_decref(wined3d_stateblock);
1875 wined3d_mutex_unlock();
1876 HeapFree(GetProcessHeap(), 0, object);
1877 return hr;
1880 TRACE("Created stateblock %p.\n", object);
1881 *stateblock = &object->IDirect3DStateBlock9_iface;
1883 return D3D_OK;
1886 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1888 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1889 HRESULT hr;
1891 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1893 wined3d_mutex_lock();
1894 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1895 wined3d_mutex_unlock();
1897 return hr;
1900 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1902 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1903 HRESULT hr;
1905 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1907 wined3d_mutex_lock();
1908 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1909 wined3d_mutex_unlock();
1911 return hr;
1914 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1916 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1917 struct wined3d_texture *wined3d_texture = NULL;
1918 struct d3d9_texture *texture_impl;
1920 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1922 if (!texture)
1923 return D3DERR_INVALIDCALL;
1925 wined3d_mutex_lock();
1926 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1928 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1929 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1930 IDirect3DBaseTexture9_AddRef(*texture);
1932 else
1934 *texture = NULL;
1936 wined3d_mutex_unlock();
1938 return D3D_OK;
1941 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1944 struct d3d9_texture *texture_impl;
1945 HRESULT hr;
1947 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1949 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1951 wined3d_mutex_lock();
1952 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1953 texture_impl ? texture_impl->wined3d_texture : NULL);
1954 wined3d_mutex_unlock();
1956 return hr;
1959 static const enum wined3d_texture_stage_state tss_lookup[] =
1961 WINED3D_TSS_INVALID, /* 0, unused */
1962 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1963 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1964 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1965 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1966 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1967 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1968 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1969 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1970 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1971 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1972 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1973 WINED3D_TSS_INVALID, /* 12, unused */
1974 WINED3D_TSS_INVALID, /* 13, unused */
1975 WINED3D_TSS_INVALID, /* 14, unused */
1976 WINED3D_TSS_INVALID, /* 15, unused */
1977 WINED3D_TSS_INVALID, /* 16, unused */
1978 WINED3D_TSS_INVALID, /* 17, unused */
1979 WINED3D_TSS_INVALID, /* 18, unused */
1980 WINED3D_TSS_INVALID, /* 19, unused */
1981 WINED3D_TSS_INVALID, /* 20, unused */
1982 WINED3D_TSS_INVALID, /* 21, unused */
1983 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1984 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1985 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1986 WINED3D_TSS_INVALID, /* 25, unused */
1987 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1988 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1989 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1990 WINED3D_TSS_INVALID, /* 29, unused */
1991 WINED3D_TSS_INVALID, /* 30, unused */
1992 WINED3D_TSS_INVALID, /* 31, unused */
1993 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1996 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1997 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2001 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2003 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2005 WARN("Invalid state %#x passed.\n", state);
2006 return D3D_OK;
2009 wined3d_mutex_lock();
2010 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2011 wined3d_mutex_unlock();
2013 return D3D_OK;
2016 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2017 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2019 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2021 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2023 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2025 WARN("Invalid state %#x passed.\n", state);
2026 return D3D_OK;
2029 wined3d_mutex_lock();
2030 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2031 wined3d_mutex_unlock();
2033 return D3D_OK;
2036 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2037 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2039 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2041 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2043 wined3d_mutex_lock();
2044 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2045 wined3d_mutex_unlock();
2047 return D3D_OK;
2050 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2051 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2053 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2055 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2057 wined3d_mutex_lock();
2058 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2059 wined3d_mutex_unlock();
2061 return D3D_OK;
2064 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2066 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2067 HRESULT hr;
2069 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2071 wined3d_mutex_lock();
2072 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2073 wined3d_mutex_unlock();
2075 return hr;
2078 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2079 UINT palette_idx, const PALETTEENTRY *entries)
2081 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2083 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2084 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2085 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2087 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2088 return D3D_OK;
2091 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2092 UINT palette_idx, PALETTEENTRY *entries)
2094 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2096 return D3DERR_INVALIDCALL;
2099 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2101 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2103 return D3D_OK;
2106 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2108 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2110 return D3DERR_INVALIDCALL;
2113 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2115 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2117 TRACE("iface %p, rect %p.\n", iface, rect);
2119 wined3d_mutex_lock();
2120 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2121 wined3d_mutex_unlock();
2123 return D3D_OK;
2126 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2128 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2130 TRACE("iface %p, rect %p.\n", iface, rect);
2132 wined3d_mutex_lock();
2133 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2134 wined3d_mutex_unlock();
2136 return D3D_OK;
2139 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2141 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2143 TRACE("iface %p, software %#x.\n", iface, software);
2145 wined3d_mutex_lock();
2146 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2147 wined3d_mutex_unlock();
2149 return D3D_OK;
2152 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2154 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2155 BOOL ret;
2157 TRACE("iface %p.\n", iface);
2159 wined3d_mutex_lock();
2160 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2161 wined3d_mutex_unlock();
2163 return ret;
2166 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2168 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2169 HRESULT hr;
2171 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2173 wined3d_mutex_lock();
2174 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2175 wined3d_mutex_unlock();
2177 return hr;
2180 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2182 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2183 float ret;
2185 TRACE("iface %p.\n", iface);
2187 wined3d_mutex_lock();
2188 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2189 wined3d_mutex_unlock();
2191 return ret;
2194 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2195 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2197 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2198 HRESULT hr;
2200 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2201 iface, primitive_type, start_vertex, primitive_count);
2203 wined3d_mutex_lock();
2204 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2205 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2206 vertex_count_from_primitive_count(primitive_type, primitive_count));
2207 wined3d_mutex_unlock();
2209 return hr;
2212 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2213 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2214 UINT vertex_count, UINT start_idx, UINT primitive_count)
2216 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2217 HRESULT hr;
2219 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2220 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2221 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2222 vertex_count, start_idx, primitive_count);
2224 wined3d_mutex_lock();
2225 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2226 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2227 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2228 vertex_count_from_primitive_count(primitive_type, primitive_count));
2229 wined3d_mutex_unlock();
2231 return hr;
2234 /* The caller is responsible for wined3d locking */
2235 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2237 HRESULT hr;
2239 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2241 UINT size = max(device->vertex_buffer_size * 2, min_size);
2242 struct wined3d_buffer *buffer;
2244 TRACE("Growing vertex buffer to %u bytes\n", size);
2246 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2247 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2248 if (FAILED(hr))
2250 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2251 return hr;
2254 if (device->vertex_buffer)
2255 wined3d_buffer_decref(device->vertex_buffer);
2257 device->vertex_buffer = buffer;
2258 device->vertex_buffer_size = size;
2259 device->vertex_buffer_pos = 0;
2261 return D3D_OK;
2264 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2265 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2267 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2268 HRESULT hr;
2269 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2270 UINT size = vtx_count * stride;
2271 UINT vb_pos, align;
2272 BYTE *buffer_data;
2274 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2275 iface, primitive_type, primitive_count, data, stride);
2277 if (!primitive_count)
2279 WARN("primitive_count is 0, returning D3D_OK\n");
2280 return D3D_OK;
2283 wined3d_mutex_lock();
2285 hr = d3d9_device_prepare_vertex_buffer(device, size);
2286 if (FAILED(hr))
2287 goto done;
2289 vb_pos = device->vertex_buffer_pos;
2290 align = vb_pos % stride;
2291 if (align) align = stride - align;
2292 if (vb_pos + size + align > device->vertex_buffer_size)
2293 vb_pos = 0;
2294 else
2295 vb_pos += align;
2297 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2298 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2299 if (FAILED(hr))
2300 goto done;
2301 memcpy(buffer_data, data, size);
2302 wined3d_buffer_unmap(device->vertex_buffer);
2303 device->vertex_buffer_pos = vb_pos + size;
2305 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2306 if (FAILED(hr))
2307 goto done;
2309 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2310 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2311 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2313 done:
2314 wined3d_mutex_unlock();
2315 return hr;
2318 /* The caller is responsible for wined3d locking */
2319 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2321 HRESULT hr;
2323 if (device->index_buffer_size < min_size || !device->index_buffer)
2325 UINT size = max(device->index_buffer_size * 2, min_size);
2326 struct wined3d_buffer *buffer;
2328 TRACE("Growing index buffer to %u bytes\n", size);
2330 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2331 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2332 if (FAILED(hr))
2334 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2335 return hr;
2338 if (device->index_buffer)
2339 wined3d_buffer_decref(device->index_buffer);
2341 device->index_buffer = buffer;
2342 device->index_buffer_size = size;
2343 device->index_buffer_pos = 0;
2345 return D3D_OK;
2348 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2349 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2350 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2351 const void *vertex_data, UINT vertex_stride)
2353 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2354 HRESULT hr;
2355 BYTE *buffer_data;
2357 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2358 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2359 UINT idx_size = idx_count * idx_fmt_size;
2360 UINT ib_pos;
2362 UINT vtx_size = vertex_count * vertex_stride;
2363 UINT vb_pos, align;
2365 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2366 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2367 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2368 index_data, index_format, vertex_data, vertex_stride);
2370 if (!primitive_count)
2372 WARN("primitive_count is 0, returning D3D_OK\n");
2373 return D3D_OK;
2376 wined3d_mutex_lock();
2378 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2379 if (FAILED(hr))
2380 goto done;
2382 vb_pos = device->vertex_buffer_pos;
2383 align = vb_pos % vertex_stride;
2384 if (align) align = vertex_stride - align;
2385 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2386 vb_pos = 0;
2387 else
2388 vb_pos += align;
2390 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2391 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2392 if (FAILED(hr))
2393 goto done;
2394 memcpy(buffer_data, vertex_data, vtx_size);
2395 wined3d_buffer_unmap(device->vertex_buffer);
2396 device->vertex_buffer_pos = vb_pos + vtx_size;
2398 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2399 if (FAILED(hr))
2400 goto done;
2402 ib_pos = device->index_buffer_pos;
2403 align = ib_pos % idx_fmt_size;
2404 if (align) align = idx_fmt_size - align;
2405 if (ib_pos + idx_size + align > device->index_buffer_size)
2406 ib_pos = 0;
2407 else
2408 ib_pos += align;
2410 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2411 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2412 if (FAILED(hr))
2413 goto done;
2414 memcpy(buffer_data, index_data, idx_size);
2415 wined3d_buffer_unmap(device->index_buffer);
2416 device->index_buffer_pos = ib_pos + idx_size;
2418 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2419 if (FAILED(hr))
2420 goto done;
2422 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2423 wined3dformat_from_d3dformat(index_format));
2424 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2426 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2427 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2429 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2430 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2431 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2433 done:
2434 wined3d_mutex_unlock();
2435 return hr;
2438 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2439 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2440 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2442 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2443 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2444 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2445 HRESULT hr;
2447 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2448 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2450 wined3d_mutex_lock();
2451 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2452 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2453 flags, dst_impl->fvf);
2454 wined3d_mutex_unlock();
2456 return hr;
2459 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2460 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2462 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2463 struct d3d9_vertex_declaration *object;
2464 HRESULT hr;
2466 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2468 if (!declaration)
2470 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2471 return D3DERR_INVALIDCALL;
2474 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2475 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2477 return hr;
2480 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2481 IDirect3DVertexDeclaration9 *declaration)
2483 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2484 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2486 TRACE("iface %p, declaration %p.\n", iface, declaration);
2488 wined3d_mutex_lock();
2489 wined3d_device_set_vertex_declaration(device->wined3d_device,
2490 decl_impl ? decl_impl->wined3d_declaration : NULL);
2491 wined3d_mutex_unlock();
2493 return D3D_OK;
2496 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2497 IDirect3DVertexDeclaration9 **declaration)
2499 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2500 struct wined3d_vertex_declaration *wined3d_declaration;
2501 struct d3d9_vertex_declaration *declaration_impl;
2503 TRACE("iface %p, declaration %p.\n", iface, declaration);
2505 if (!declaration) return D3DERR_INVALIDCALL;
2507 wined3d_mutex_lock();
2508 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2510 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2511 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2512 IDirect3DVertexDeclaration9_AddRef(*declaration);
2514 else
2516 *declaration = NULL;
2518 wined3d_mutex_unlock();
2520 TRACE("Returning %p.\n", *declaration);
2521 return D3D_OK;
2524 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2526 struct wined3d_vertex_declaration *wined3d_declaration;
2527 struct fvf_declaration *fvf_decls = device->fvf_decls;
2528 struct d3d9_vertex_declaration *d3d9_declaration;
2529 D3DVERTEXELEMENT9 *elements;
2530 int p, low, high; /* deliberately signed */
2531 HRESULT hr;
2533 TRACE("Searching for declaration for fvf %08x... ", fvf);
2535 low = 0;
2536 high = device->fvf_decl_count - 1;
2537 while (low <= high)
2539 p = (low + high) >> 1;
2540 TRACE("%d ", p);
2542 if (fvf_decls[p].fvf == fvf)
2544 TRACE("found %p.\n", fvf_decls[p].decl);
2545 return fvf_decls[p].decl;
2548 if (fvf_decls[p].fvf < fvf)
2549 low = p + 1;
2550 else
2551 high = p - 1;
2553 TRACE("not found. Creating and inserting at position %d.\n", low);
2555 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2556 return NULL;
2558 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2559 HeapFree(GetProcessHeap(), 0, elements);
2560 if (FAILED(hr))
2561 return NULL;
2563 if (device->fvf_decl_size == device->fvf_decl_count)
2565 UINT grow = max(device->fvf_decl_size / 2, 8);
2567 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2568 if (!fvf_decls)
2570 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2571 return NULL;
2573 device->fvf_decls = fvf_decls;
2574 device->fvf_decl_size += grow;
2577 d3d9_declaration->fvf = fvf;
2578 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2579 wined3d_vertex_declaration_incref(wined3d_declaration);
2580 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2582 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2583 fvf_decls[low].decl = wined3d_declaration;
2584 fvf_decls[low].fvf = fvf;
2585 ++device->fvf_decl_count;
2587 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2589 return wined3d_declaration;
2592 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2594 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2595 struct wined3d_vertex_declaration *decl;
2597 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2599 if (!fvf)
2601 WARN("%#x is not a valid FVF.\n", fvf);
2602 return D3D_OK;
2605 wined3d_mutex_lock();
2606 if (!(decl = device_get_fvf_declaration(device, fvf)))
2608 wined3d_mutex_unlock();
2609 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2610 return D3DERR_DRIVERINTERNALERROR;
2613 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2614 wined3d_mutex_unlock();
2616 return D3D_OK;
2619 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2621 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2622 struct wined3d_vertex_declaration *wined3d_declaration;
2623 struct d3d9_vertex_declaration *d3d9_declaration;
2625 TRACE("iface %p, fvf %p.\n", iface, fvf);
2627 wined3d_mutex_lock();
2628 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2630 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2631 *fvf = d3d9_declaration->fvf;
2633 else
2635 *fvf = 0;
2637 wined3d_mutex_unlock();
2639 TRACE("Returning FVF %#x.\n", *fvf);
2641 return D3D_OK;
2644 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2645 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2647 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2648 struct d3d9_vertexshader *object;
2649 HRESULT hr;
2651 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2653 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2654 if (!object)
2655 return E_OUTOFMEMORY;
2657 hr = vertexshader_init(object, device, byte_code);
2658 if (FAILED(hr))
2660 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2661 HeapFree(GetProcessHeap(), 0, object);
2662 return hr;
2665 TRACE("Created vertex shader %p.\n", object);
2666 *shader = &object->IDirect3DVertexShader9_iface;
2668 return D3D_OK;
2671 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2673 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2674 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2676 TRACE("iface %p, shader %p.\n", iface, shader);
2678 wined3d_mutex_lock();
2679 wined3d_device_set_vertex_shader(device->wined3d_device,
2680 shader_obj ? shader_obj->wined3d_shader : NULL);
2681 wined3d_mutex_unlock();
2683 return D3D_OK;
2686 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2688 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2689 struct d3d9_vertexshader *shader_impl;
2690 struct wined3d_shader *wined3d_shader;
2692 TRACE("iface %p, shader %p.\n", iface, shader);
2694 wined3d_mutex_lock();
2695 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2697 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2698 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2699 IDirect3DVertexShader9_AddRef(*shader);
2701 else
2703 *shader = NULL;
2705 wined3d_mutex_unlock();
2707 TRACE("Returning %p.\n", *shader);
2709 return D3D_OK;
2712 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2713 UINT reg_idx, const float *data, UINT count)
2715 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2716 HRESULT hr;
2718 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2720 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2722 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2723 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2724 return D3DERR_INVALIDCALL;
2727 wined3d_mutex_lock();
2728 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2729 wined3d_mutex_unlock();
2731 return hr;
2734 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2735 UINT reg_idx, float *data, UINT count)
2737 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2738 HRESULT hr;
2740 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2742 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2744 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2745 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2746 return D3DERR_INVALIDCALL;
2749 wined3d_mutex_lock();
2750 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2751 wined3d_mutex_unlock();
2753 return hr;
2756 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2757 UINT reg_idx, const int *data, UINT count)
2759 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2760 HRESULT hr;
2762 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2764 wined3d_mutex_lock();
2765 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2766 wined3d_mutex_unlock();
2768 return hr;
2771 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2772 UINT reg_idx, int *data, UINT count)
2774 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2775 HRESULT hr;
2777 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2779 wined3d_mutex_lock();
2780 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2781 wined3d_mutex_unlock();
2783 return hr;
2786 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2787 UINT reg_idx, const BOOL *data, UINT count)
2789 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2790 HRESULT hr;
2792 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2794 wined3d_mutex_lock();
2795 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2796 wined3d_mutex_unlock();
2798 return hr;
2801 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2802 UINT reg_idx, BOOL *data, UINT count)
2804 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2805 HRESULT hr;
2807 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2809 wined3d_mutex_lock();
2810 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2811 wined3d_mutex_unlock();
2813 return hr;
2816 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2817 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2819 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2820 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2821 HRESULT hr;
2823 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2824 iface, stream_idx, buffer, offset, stride);
2826 wined3d_mutex_lock();
2827 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2828 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2829 wined3d_mutex_unlock();
2831 return hr;
2834 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2835 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2837 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2838 struct d3d9_vertexbuffer *buffer_impl;
2839 struct wined3d_buffer *wined3d_buffer;
2840 HRESULT hr;
2842 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2843 iface, stream_idx, buffer, offset, stride);
2845 if (!buffer)
2846 return D3DERR_INVALIDCALL;
2848 wined3d_mutex_lock();
2849 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2850 if (SUCCEEDED(hr) && wined3d_buffer)
2852 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2853 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2854 IDirect3DVertexBuffer9_AddRef(*buffer);
2856 else
2858 if (FAILED(hr))
2859 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2860 *buffer = NULL;
2862 wined3d_mutex_unlock();
2864 return hr;
2867 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2869 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2870 HRESULT hr;
2872 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2874 wined3d_mutex_lock();
2875 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2876 wined3d_mutex_unlock();
2878 return hr;
2881 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2883 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2884 HRESULT hr;
2886 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2888 wined3d_mutex_lock();
2889 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2890 wined3d_mutex_unlock();
2892 return hr;
2895 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2897 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2898 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2900 TRACE("iface %p, buffer %p.\n", iface, buffer);
2902 wined3d_mutex_lock();
2903 wined3d_device_set_index_buffer(device->wined3d_device,
2904 ib ? ib->wined3d_buffer : NULL,
2905 ib ? ib->format : WINED3DFMT_UNKNOWN);
2906 wined3d_mutex_unlock();
2908 return D3D_OK;
2911 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2913 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2914 enum wined3d_format_id wined3d_format;
2915 struct wined3d_buffer *wined3d_buffer;
2916 struct d3d9_indexbuffer *buffer_impl;
2918 TRACE("iface %p, buffer %p.\n", iface, buffer);
2920 if (!buffer)
2921 return D3DERR_INVALIDCALL;
2923 wined3d_mutex_lock();
2924 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2926 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2927 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2928 IDirect3DIndexBuffer9_AddRef(*buffer);
2930 else
2932 *buffer = NULL;
2934 wined3d_mutex_unlock();
2936 return D3D_OK;
2939 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2940 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2943 struct d3d9_pixelshader *object;
2944 HRESULT hr;
2946 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2948 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2949 if (!object)
2951 FIXME("Failed to allocate pixel shader memory.\n");
2952 return E_OUTOFMEMORY;
2955 hr = pixelshader_init(object, device, byte_code);
2956 if (FAILED(hr))
2958 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2959 HeapFree(GetProcessHeap(), 0, object);
2960 return hr;
2963 TRACE("Created pixel shader %p.\n", object);
2964 *shader = &object->IDirect3DPixelShader9_iface;
2966 return D3D_OK;
2969 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2972 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2974 TRACE("iface %p, shader %p.\n", iface, shader);
2976 wined3d_mutex_lock();
2977 wined3d_device_set_pixel_shader(device->wined3d_device,
2978 shader_obj ? shader_obj->wined3d_shader : NULL);
2979 wined3d_mutex_unlock();
2981 return D3D_OK;
2984 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2986 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2987 struct d3d9_pixelshader *shader_impl;
2988 struct wined3d_shader *wined3d_shader;
2990 TRACE("iface %p, shader %p.\n", iface, shader);
2992 if (!shader) return D3DERR_INVALIDCALL;
2994 wined3d_mutex_lock();
2995 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2997 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2998 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2999 IDirect3DPixelShader9_AddRef(*shader);
3001 else
3003 *shader = NULL;
3005 wined3d_mutex_unlock();
3007 TRACE("Returning %p.\n", *shader);
3009 return D3D_OK;
3012 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3013 UINT reg_idx, const float *data, UINT count)
3015 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3016 HRESULT hr;
3018 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3020 wined3d_mutex_lock();
3021 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3022 wined3d_mutex_unlock();
3024 return hr;
3027 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3028 UINT reg_idx, float *data, UINT count)
3030 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3031 HRESULT hr;
3033 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3035 wined3d_mutex_lock();
3036 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3037 wined3d_mutex_unlock();
3039 return hr;
3042 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3043 UINT reg_idx, const int *data, UINT count)
3045 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3046 HRESULT hr;
3048 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3050 wined3d_mutex_lock();
3051 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3052 wined3d_mutex_unlock();
3054 return hr;
3057 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3058 UINT reg_idx, int *data, UINT count)
3060 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3061 HRESULT hr;
3063 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3065 wined3d_mutex_lock();
3066 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3067 wined3d_mutex_unlock();
3069 return hr;
3072 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3073 UINT reg_idx, const BOOL *data, UINT count)
3075 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3076 HRESULT hr;
3078 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3080 wined3d_mutex_lock();
3081 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3082 wined3d_mutex_unlock();
3084 return hr;
3087 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3088 UINT reg_idx, BOOL *data, UINT count)
3090 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3091 HRESULT hr;
3093 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3095 wined3d_mutex_lock();
3096 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3097 wined3d_mutex_unlock();
3099 return hr;
3102 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3103 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3105 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3106 iface, handle, segment_count, patch_info);
3107 return D3D_OK;
3110 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3111 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3113 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3114 iface, handle, segment_count, patch_info);
3115 return D3D_OK;
3118 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3120 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3121 return D3DERR_INVALIDCALL;
3124 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3126 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3127 struct d3d9_query *object;
3128 HRESULT hr;
3130 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3132 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3133 if (!object)
3134 return E_OUTOFMEMORY;
3136 hr = query_init(object, device, type);
3137 if (FAILED(hr))
3139 WARN("Failed to initialize query, hr %#x.\n", hr);
3140 HeapFree(GetProcessHeap(), 0, object);
3141 return hr;
3144 TRACE("Created query %p.\n", object);
3145 if (query) *query = &object->IDirect3DQuery9_iface;
3146 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3148 return D3D_OK;
3151 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3152 UINT width, UINT height, float *rows, float *columns)
3154 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3155 iface, width, height, rows, columns);
3157 return E_NOTIMPL;
3160 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3161 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3162 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3164 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3165 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3166 iface, src_surface, dst_surface, src_descs, rect_count,
3167 dst_descs, operation, offset_x, offset_y);
3169 return E_NOTIMPL;
3172 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3173 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3174 const RGNDATA *dirty_region, DWORD flags)
3176 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3177 HRESULT hr;
3179 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3180 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3181 dst_window_override, dirty_region, flags);
3183 if (device->device_state != D3D9_DEVICE_STATE_OK)
3184 return S_PRESENT_OCCLUDED;
3186 wined3d_mutex_lock();
3187 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
3188 dst_window_override, dirty_region, flags);
3189 wined3d_mutex_unlock();
3191 return hr;
3194 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3196 FIXME("iface %p, priority %p stub!\n", iface, priority);
3198 return E_NOTIMPL;
3201 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3203 FIXME("iface %p, priority %d stub!\n", iface, priority);
3205 return E_NOTIMPL;
3208 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3210 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3212 return E_NOTIMPL;
3215 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3216 IDirect3DResource9 **resources, UINT32 resource_count)
3218 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3219 iface, resources, resource_count);
3221 return E_NOTIMPL;
3224 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3226 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3228 return E_NOTIMPL;
3231 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3233 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3235 *max_latency = 2;
3237 return E_NOTIMPL;
3240 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3242 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3243 struct wined3d_swapchain_desc swapchain_desc;
3244 struct wined3d_swapchain *swapchain;
3246 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3248 wined3d_mutex_lock();
3249 swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3250 wined3d_swapchain_get_desc(swapchain, &swapchain_desc);
3251 wined3d_mutex_unlock();
3253 if (swapchain_desc.windowed)
3254 return D3D_OK;
3256 /* FIXME: This is actually supposed to check if any other device is in
3257 * fullscreen mode. */
3258 if (dst_window != swapchain_desc.device_window)
3259 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3261 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3264 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3265 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3266 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3268 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3269 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3270 iface, width, height, format, multisample_type, multisample_quality,
3271 lockable, surface, shared_handle, usage);
3273 *surface = NULL;
3274 if (shared_handle)
3275 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3277 return E_NOTIMPL;
3280 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3281 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3282 HANDLE *shared_handle, DWORD usage)
3284 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3285 iface, width, height, format, pool, surface, shared_handle, usage);
3287 return E_NOTIMPL;
3290 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3291 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3292 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3294 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3295 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3296 iface, width, height, format, multisample_type, multisample_quality,
3297 discard, surface, shared_handle, usage);
3299 *surface = NULL;
3300 if (shared_handle)
3301 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3303 return E_NOTIMPL;
3306 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3307 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3309 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3311 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3313 return d3d9_device_reset(device, present_parameters, mode);
3316 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3317 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3319 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3320 struct wined3d_display_mode wined3d_mode;
3321 HRESULT hr;
3323 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3324 iface, swapchain_idx, mode, rotation);
3326 if (mode->Size != sizeof(*mode))
3327 return D3DERR_INVALIDCALL;
3329 wined3d_mutex_lock();
3330 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3331 (enum wined3d_display_rotation *)rotation);
3332 wined3d_mutex_unlock();
3334 if (SUCCEEDED(hr))
3336 mode->Width = wined3d_mode.width;
3337 mode->Height = wined3d_mode.height;
3338 mode->RefreshRate = wined3d_mode.refresh_rate;
3339 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3340 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3343 return hr;
3346 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3348 /* IUnknown */
3349 d3d9_device_QueryInterface,
3350 d3d9_device_AddRef,
3351 d3d9_device_Release,
3352 /* IDirect3DDevice9 */
3353 d3d9_device_TestCooperativeLevel,
3354 d3d9_device_GetAvailableTextureMem,
3355 d3d9_device_EvictManagedResources,
3356 d3d9_device_GetDirect3D,
3357 d3d9_device_GetDeviceCaps,
3358 d3d9_device_GetDisplayMode,
3359 d3d9_device_GetCreationParameters,
3360 d3d9_device_SetCursorProperties,
3361 d3d9_device_SetCursorPosition,
3362 d3d9_device_ShowCursor,
3363 d3d9_device_CreateAdditionalSwapChain,
3364 d3d9_device_GetSwapChain,
3365 d3d9_device_GetNumberOfSwapChains,
3366 d3d9_device_Reset,
3367 d3d9_device_Present,
3368 d3d9_device_GetBackBuffer,
3369 d3d9_device_GetRasterStatus,
3370 d3d9_device_SetDialogBoxMode,
3371 d3d9_device_SetGammaRamp,
3372 d3d9_device_GetGammaRamp,
3373 d3d9_device_CreateTexture,
3374 d3d9_device_CreateVolumeTexture,
3375 d3d9_device_CreateCubeTexture,
3376 d3d9_device_CreateVertexBuffer,
3377 d3d9_device_CreateIndexBuffer,
3378 d3d9_device_CreateRenderTarget,
3379 d3d9_device_CreateDepthStencilSurface,
3380 d3d9_device_UpdateSurface,
3381 d3d9_device_UpdateTexture,
3382 d3d9_device_GetRenderTargetData,
3383 d3d9_device_GetFrontBufferData,
3384 d3d9_device_StretchRect,
3385 d3d9_device_ColorFill,
3386 d3d9_device_CreateOffscreenPlainSurface,
3387 d3d9_device_SetRenderTarget,
3388 d3d9_device_GetRenderTarget,
3389 d3d9_device_SetDepthStencilSurface,
3390 d3d9_device_GetDepthStencilSurface,
3391 d3d9_device_BeginScene,
3392 d3d9_device_EndScene,
3393 d3d9_device_Clear,
3394 d3d9_device_SetTransform,
3395 d3d9_device_GetTransform,
3396 d3d9_device_MultiplyTransform,
3397 d3d9_device_SetViewport,
3398 d3d9_device_GetViewport,
3399 d3d9_device_SetMaterial,
3400 d3d9_device_GetMaterial,
3401 d3d9_device_SetLight,
3402 d3d9_device_GetLight,
3403 d3d9_device_LightEnable,
3404 d3d9_device_GetLightEnable,
3405 d3d9_device_SetClipPlane,
3406 d3d9_device_GetClipPlane,
3407 d3d9_device_SetRenderState,
3408 d3d9_device_GetRenderState,
3409 d3d9_device_CreateStateBlock,
3410 d3d9_device_BeginStateBlock,
3411 d3d9_device_EndStateBlock,
3412 d3d9_device_SetClipStatus,
3413 d3d9_device_GetClipStatus,
3414 d3d9_device_GetTexture,
3415 d3d9_device_SetTexture,
3416 d3d9_device_GetTextureStageState,
3417 d3d9_device_SetTextureStageState,
3418 d3d9_device_GetSamplerState,
3419 d3d9_device_SetSamplerState,
3420 d3d9_device_ValidateDevice,
3421 d3d9_device_SetPaletteEntries,
3422 d3d9_device_GetPaletteEntries,
3423 d3d9_device_SetCurrentTexturePalette,
3424 d3d9_device_GetCurrentTexturePalette,
3425 d3d9_device_SetScissorRect,
3426 d3d9_device_GetScissorRect,
3427 d3d9_device_SetSoftwareVertexProcessing,
3428 d3d9_device_GetSoftwareVertexProcessing,
3429 d3d9_device_SetNPatchMode,
3430 d3d9_device_GetNPatchMode,
3431 d3d9_device_DrawPrimitive,
3432 d3d9_device_DrawIndexedPrimitive,
3433 d3d9_device_DrawPrimitiveUP,
3434 d3d9_device_DrawIndexedPrimitiveUP,
3435 d3d9_device_ProcessVertices,
3436 d3d9_device_CreateVertexDeclaration,
3437 d3d9_device_SetVertexDeclaration,
3438 d3d9_device_GetVertexDeclaration,
3439 d3d9_device_SetFVF,
3440 d3d9_device_GetFVF,
3441 d3d9_device_CreateVertexShader,
3442 d3d9_device_SetVertexShader,
3443 d3d9_device_GetVertexShader,
3444 d3d9_device_SetVertexShaderConstantF,
3445 d3d9_device_GetVertexShaderConstantF,
3446 d3d9_device_SetVertexShaderConstantI,
3447 d3d9_device_GetVertexShaderConstantI,
3448 d3d9_device_SetVertexShaderConstantB,
3449 d3d9_device_GetVertexShaderConstantB,
3450 d3d9_device_SetStreamSource,
3451 d3d9_device_GetStreamSource,
3452 d3d9_device_SetStreamSourceFreq,
3453 d3d9_device_GetStreamSourceFreq,
3454 d3d9_device_SetIndices,
3455 d3d9_device_GetIndices,
3456 d3d9_device_CreatePixelShader,
3457 d3d9_device_SetPixelShader,
3458 d3d9_device_GetPixelShader,
3459 d3d9_device_SetPixelShaderConstantF,
3460 d3d9_device_GetPixelShaderConstantF,
3461 d3d9_device_SetPixelShaderConstantI,
3462 d3d9_device_GetPixelShaderConstantI,
3463 d3d9_device_SetPixelShaderConstantB,
3464 d3d9_device_GetPixelShaderConstantB,
3465 d3d9_device_DrawRectPatch,
3466 d3d9_device_DrawTriPatch,
3467 d3d9_device_DeletePatch,
3468 d3d9_device_CreateQuery,
3469 /* IDirect3DDevice9Ex */
3470 d3d9_device_SetConvolutionMonoKernel,
3471 d3d9_device_ComposeRects,
3472 d3d9_device_PresentEx,
3473 d3d9_device_GetGPUThreadPriority,
3474 d3d9_device_SetGPUThreadPriority,
3475 d3d9_device_WaitForVBlank,
3476 d3d9_device_CheckResourceResidency,
3477 d3d9_device_SetMaximumFrameLatency,
3478 d3d9_device_GetMaximumFrameLatency,
3479 d3d9_device_CheckDeviceState,
3480 d3d9_device_CreateRenderTargetEx,
3481 d3d9_device_CreateOffscreenPlainSurfaceEx,
3482 d3d9_device_CreateDepthStencilSurfaceEx,
3483 d3d9_device_ResetEx,
3484 d3d9_device_GetDisplayModeEx,
3487 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3489 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3492 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3493 struct wined3d_device *device)
3495 TRACE("device_parent %p, device %p.\n", device_parent, device);
3498 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3500 TRACE("device_parent %p.\n", device_parent);
3503 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3505 struct d3d9_device *device = device_from_device_parent(device_parent);
3507 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3509 if (!device->d3d_parent)
3510 return;
3512 if (!activate)
3513 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3514 else if (device->d3d_parent->extended)
3515 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3516 else
3517 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3520 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3521 void *container_parent, struct wined3d_surface *surface, void **parent,
3522 const struct wined3d_parent_ops **parent_ops)
3524 struct d3d9_surface *d3d_surface;
3526 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3527 device_parent, container_parent, surface, parent, parent_ops);
3529 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3530 return E_OUTOFMEMORY;
3532 surface_init(d3d_surface, container_parent, surface, parent_ops);
3533 *parent = d3d_surface;
3534 TRACE("Created surface %p.\n", d3d_surface);
3536 return D3D_OK;
3539 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3540 void *container_parent, struct wined3d_volume *volume, void **parent,
3541 const struct wined3d_parent_ops **parent_ops)
3543 struct d3d9_volume *d3d_volume;
3545 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3546 device_parent, container_parent, volume, parent, parent_ops);
3548 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3549 return E_OUTOFMEMORY;
3551 volume_init(d3d_volume, container_parent, volume, parent_ops);
3552 *parent = d3d_volume;
3553 TRACE("Created volume %p.\n", d3d_volume);
3555 return D3D_OK;
3558 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3559 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3561 struct d3d9_device *device = device_from_device_parent(device_parent);
3562 struct wined3d_resource_desc texture_desc;
3563 struct d3d9_surface *d3d_surface;
3564 struct wined3d_texture *texture;
3565 HRESULT hr;
3567 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3568 device_parent, container_parent, desc, surface);
3570 if (container_parent == device_parent)
3571 container_parent = &device->IDirect3DDevice9Ex_iface;
3573 texture_desc = *desc;
3574 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3575 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3576 WINED3D_SURFACE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, &texture)))
3578 WARN("Failed to create texture, hr %#x.\n", hr);
3579 return hr;
3582 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3583 wined3d_surface_incref(*surface);
3584 wined3d_texture_decref(texture);
3586 d3d_surface = wined3d_surface_get_parent(*surface);
3587 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3589 return hr;
3592 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3593 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3595 struct d3d9_device *device = device_from_device_parent(device_parent);
3596 struct d3d9_swapchain *d3d_swapchain;
3597 HRESULT hr;
3599 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3601 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3602 if (FAILED(hr))
3604 WARN("Failed to create swapchain, hr %#x.\n", hr);
3605 *swapchain = NULL;
3606 return hr;
3609 *swapchain = d3d_swapchain->wined3d_swapchain;
3610 wined3d_swapchain_incref(*swapchain);
3611 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3613 return hr;
3616 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3618 device_parent_wined3d_device_created,
3619 device_parent_mode_changed,
3620 device_parent_activate,
3621 device_parent_surface_created,
3622 device_parent_volume_created,
3623 device_parent_create_swapchain_surface,
3624 device_parent_create_swapchain,
3627 static void setup_fpu(void)
3629 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3630 WORD cw;
3631 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3632 cw = (cw & ~0xf3f) | 0x3f;
3633 __asm__ volatile ("fldcw %0" : : "m" (cw));
3634 #elif defined(__i386__) && defined(_MSC_VER)
3635 WORD cw;
3636 __asm fnstcw cw;
3637 cw = (cw & ~0xf3f) | 0x3f;
3638 __asm fldcw cw;
3639 #else
3640 FIXME("FPU setup not implemented for this platform.\n");
3641 #endif
3644 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3645 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3646 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3648 struct wined3d_swapchain_desc *swapchain_desc;
3649 UINT i, count = 1;
3650 HRESULT hr;
3652 if (mode)
3653 FIXME("Ignoring display mode.\n");
3655 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3656 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3657 device->refcount = 1;
3659 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3661 wined3d_mutex_lock();
3662 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3663 &device->device_parent, &device->wined3d_device);
3664 if (FAILED(hr))
3666 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3667 wined3d_mutex_unlock();
3668 return hr;
3671 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3673 WINED3DCAPS caps;
3675 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3676 count = caps.NumberOfAdaptersInGroup;
3679 if (flags & D3DCREATE_MULTITHREADED)
3680 wined3d_device_set_multithreaded(device->wined3d_device);
3682 if (!parameters->Windowed)
3684 if (!focus_window)
3685 focus_window = parameters->hDeviceWindow;
3686 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3688 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3689 wined3d_device_decref(device->wined3d_device);
3690 wined3d_mutex_unlock();
3691 return hr;
3694 for (i = 0; i < count; ++i)
3696 HWND device_window = parameters[i].hDeviceWindow;
3698 if (!device_window) device_window = focus_window;
3699 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3700 parameters[i].BackBufferWidth,
3701 parameters[i].BackBufferHeight);
3705 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3706 if (!swapchain_desc)
3708 ERR("Failed to allocate wined3d parameters.\n");
3709 wined3d_device_release_focus_window(device->wined3d_device);
3710 wined3d_device_decref(device->wined3d_device);
3711 wined3d_mutex_unlock();
3712 return E_OUTOFMEMORY;
3715 for (i = 0; i < count; ++i)
3717 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3718 parent->extended))
3720 wined3d_device_release_focus_window(device->wined3d_device);
3721 wined3d_device_decref(device->wined3d_device);
3722 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3723 wined3d_mutex_unlock();
3724 return D3DERR_INVALIDCALL;
3728 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3729 if (FAILED(hr))
3731 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3732 wined3d_device_release_focus_window(device->wined3d_device);
3733 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3734 wined3d_device_decref(device->wined3d_device);
3735 wined3d_mutex_unlock();
3736 return hr;
3739 wined3d_mutex_unlock();
3741 for (i = 0; i < count; ++i)
3743 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3745 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3747 /* Initialize the converted declaration array. This creates a valid pointer
3748 * and when adding decls HeapReAlloc() can be used without further checking. */
3749 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3750 if (!device->fvf_decls)
3752 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3753 wined3d_mutex_lock();
3754 wined3d_device_uninit_3d(device->wined3d_device);
3755 wined3d_device_release_focus_window(device->wined3d_device);
3756 wined3d_device_decref(device->wined3d_device);
3757 wined3d_mutex_unlock();
3758 return E_OUTOFMEMORY;
3761 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3762 device->d3d_parent = parent;
3764 return D3D_OK;