winex11.drv: Correct sizes in COMPOSITIONSTRING structure when updating fields.
[wine.git] / dlls / d3d9 / device.c
bloba26e07535438b6dabb71e4a3a52f53c4b5de0c7a
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
30 static const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
32 d3d9_null_wined3d_object_destroyed,
35 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
37 BYTE *c = (BYTE *)&format;
39 /* Don't translate FOURCC formats */
40 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
42 switch(format)
44 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
45 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
46 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
47 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
48 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
49 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
50 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
51 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
52 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
53 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
54 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
55 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
56 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
57 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
58 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
59 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
60 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
61 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
73 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
74 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
75 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
76 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
77 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
78 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
79 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
80 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
81 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
82 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
83 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
84 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
85 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
86 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
87 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
88 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
89 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
90 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
91 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
92 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
93 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
94 default:
95 FIXME("Unhandled wined3d format %#x.\n", format);
96 return D3DFMT_UNKNOWN;
100 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
102 BYTE *c = (BYTE *)&format;
104 /* Don't translate FOURCC formats */
105 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
107 switch(format)
109 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
110 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
111 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
112 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
113 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
114 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
115 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
116 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
117 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
118 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
119 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
120 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
121 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
122 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
123 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
124 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
125 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
126 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
127 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
128 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
129 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
130 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
131 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
132 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
133 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
134 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
135 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
136 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
137 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
138 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
139 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
140 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
141 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
142 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
143 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
144 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
145 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
146 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
147 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
148 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
149 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
150 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
151 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
152 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
153 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
154 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
155 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
156 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
157 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
158 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
159 default:
160 FIXME("Unhandled D3DFORMAT %#x\n", format);
161 return WINED3DFMT_UNKNOWN;
165 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
167 switch(primitive_type)
169 case D3DPT_POINTLIST:
170 return primitive_count;
172 case D3DPT_LINELIST:
173 return primitive_count * 2;
175 case D3DPT_LINESTRIP:
176 return primitive_count + 1;
178 case D3DPT_TRIANGLELIST:
179 return primitive_count * 3;
181 case D3DPT_TRIANGLESTRIP:
182 case D3DPT_TRIANGLEFAN:
183 return primitive_count + 2;
185 default:
186 FIXME("Unhandled primitive type %#x\n", primitive_type);
187 return 0;
191 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
192 const struct wined3d_swapchain_desc *swapchain_desc)
194 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
195 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
196 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
197 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
198 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
199 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
200 present_parameters->SwapEffect = swapchain_desc->swap_effect;
201 present_parameters->hDeviceWindow = swapchain_desc->device_window;
202 present_parameters->Windowed = swapchain_desc->windowed;
203 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
204 present_parameters->AutoDepthStencilFormat
205 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
206 present_parameters->Flags = swapchain_desc->flags;
207 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
208 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
211 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
212 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
214 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
215 UINT highest_bb_count = extended ? 30 : 3;
217 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
219 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
220 return FALSE;
222 if (present_parameters->BackBufferCount > highest_bb_count
223 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
224 && present_parameters->BackBufferCount > 1))
226 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
227 return FALSE;
230 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
231 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
232 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
233 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
234 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
235 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
236 swapchain_desc->swap_effect = present_parameters->SwapEffect;
237 swapchain_desc->device_window = present_parameters->hDeviceWindow;
238 swapchain_desc->windowed = present_parameters->Windowed;
239 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
240 swapchain_desc->auto_depth_stencil_format
241 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
242 swapchain_desc->flags = present_parameters->Flags;
243 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
244 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
245 swapchain_desc->auto_restore_display_mode = TRUE;
247 return TRUE;
250 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
252 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
254 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
255 || IsEqualGUID(riid, &IID_IUnknown))
257 IDirect3DDevice9Ex_AddRef(iface);
258 *out = iface;
259 return S_OK;
262 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
264 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
266 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
267 * It doesn't matter with which function the device was created. */
268 if (!device->d3d_parent->extended)
270 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
271 *out = NULL;
272 return E_NOINTERFACE;
275 IDirect3DDevice9Ex_AddRef(iface);
276 *out = iface;
277 return S_OK;
280 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
282 *out = NULL;
283 return E_NOINTERFACE;
286 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
289 ULONG refcount = InterlockedIncrement(&device->refcount);
291 TRACE("%p increasing refcount to %u.\n", iface, refcount);
293 return refcount;
296 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
298 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
299 ULONG refcount;
301 if (device->in_destruction)
302 return 0;
304 refcount = InterlockedDecrement(&device->refcount);
306 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
308 if (!refcount)
310 unsigned i;
311 device->in_destruction = TRUE;
313 wined3d_mutex_lock();
314 for (i = 0; i < device->fvf_decl_count; ++i)
316 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
318 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
320 if (device->vertex_buffer)
321 wined3d_buffer_decref(device->vertex_buffer);
322 if (device->index_buffer)
323 wined3d_buffer_decref(device->index_buffer);
325 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
327 wined3d_device_uninit_3d(device->wined3d_device);
328 wined3d_device_release_focus_window(device->wined3d_device);
329 wined3d_device_decref(device->wined3d_device);
330 wined3d_mutex_unlock();
332 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
334 HeapFree(GetProcessHeap(), 0, device);
337 return refcount;
340 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
342 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
344 TRACE("iface %p.\n", iface);
346 TRACE("device state: %#x.\n", device->device_state);
348 if (device->d3d_parent->extended)
349 return D3D_OK;
351 switch (device->device_state)
353 default:
354 case D3D9_DEVICE_STATE_OK:
355 return D3D_OK;
356 case D3D9_DEVICE_STATE_LOST:
357 return D3DERR_DEVICELOST;
358 case D3D9_DEVICE_STATE_NOT_RESET:
359 return D3DERR_DEVICENOTRESET;
363 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
365 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
366 UINT ret;
368 TRACE("iface %p.\n", iface);
370 wined3d_mutex_lock();
371 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
372 wined3d_mutex_unlock();
374 return ret;
377 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
379 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
381 TRACE("iface %p.\n", iface);
383 wined3d_mutex_lock();
384 wined3d_device_evict_managed_resources(device->wined3d_device);
385 wined3d_mutex_unlock();
387 return D3D_OK;
390 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
392 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
394 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
396 if (!d3d9)
397 return D3DERR_INVALIDCALL;
399 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
402 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
404 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
405 WINED3DCAPS *wined3d_caps;
406 HRESULT hr;
408 TRACE("iface %p, caps %p.\n", iface, caps);
410 if (!caps)
411 return D3DERR_INVALIDCALL;
413 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
414 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
416 memset(caps, 0, sizeof(*caps));
418 wined3d_mutex_lock();
419 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
420 wined3d_mutex_unlock();
422 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
423 HeapFree(GetProcessHeap(), 0, wined3d_caps);
425 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
426 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
428 filter_caps(caps);
430 return hr;
433 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
435 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
436 struct wined3d_display_mode wined3d_mode;
437 HRESULT hr;
439 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
441 wined3d_mutex_lock();
442 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
443 wined3d_mutex_unlock();
445 if (SUCCEEDED(hr))
447 mode->Width = wined3d_mode.width;
448 mode->Height = wined3d_mode.height;
449 mode->RefreshRate = wined3d_mode.refresh_rate;
450 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
453 return hr;
456 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
457 D3DDEVICE_CREATION_PARAMETERS *parameters)
459 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
461 TRACE("iface %p, parameters %p.\n", iface, parameters);
463 wined3d_mutex_lock();
464 wined3d_device_get_creation_parameters(device->wined3d_device,
465 (struct wined3d_device_creation_parameters *)parameters);
466 wined3d_mutex_unlock();
468 return D3D_OK;
471 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
472 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
474 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
475 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
476 HRESULT hr;
478 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
479 iface, hotspot_x, hotspot_y, bitmap);
481 if (!bitmap)
483 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
484 return D3DERR_INVALIDCALL;
487 wined3d_mutex_lock();
488 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
489 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
490 wined3d_mutex_unlock();
492 return hr;
495 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
497 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
499 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
501 wined3d_mutex_lock();
502 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
503 wined3d_mutex_unlock();
506 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
508 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
509 BOOL ret;
511 TRACE("iface %p, show %#x.\n", iface, show);
513 wined3d_mutex_lock();
514 ret = wined3d_device_show_cursor(device->wined3d_device, show);
515 wined3d_mutex_unlock();
517 return ret;
520 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
521 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
523 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
524 struct wined3d_swapchain_desc desc;
525 struct d3d9_swapchain *object;
526 UINT i, count;
527 HRESULT hr;
529 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
530 iface, present_parameters, swapchain);
532 if (!present_parameters->Windowed)
534 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
535 return D3DERR_INVALIDCALL;
538 wined3d_mutex_lock();
539 count = wined3d_device_get_swapchain_count(device->wined3d_device);
540 for (i = 0; i < count; ++i)
542 struct wined3d_swapchain *wined3d_swapchain;
544 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
545 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
547 if (!desc.windowed)
549 wined3d_mutex_unlock();
550 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
551 return D3DERR_INVALIDCALL;
554 wined3d_mutex_unlock();
556 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
557 device->d3d_parent->extended))
558 return D3DERR_INVALIDCALL;
559 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
560 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
561 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
563 return hr;
566 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
567 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
570 HRESULT hr;
572 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
574 wined3d_mutex_lock();
575 if (swapchain_idx < device->implicit_swapchain_count)
577 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
578 IDirect3DSwapChain9Ex_AddRef(*swapchain);
579 hr = D3D_OK;
581 else
583 *swapchain = NULL;
584 hr = D3DERR_INVALIDCALL;
586 wined3d_mutex_unlock();
588 return hr;
591 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
593 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
594 UINT count;
596 TRACE("iface %p.\n", iface);
598 wined3d_mutex_lock();
599 count = wined3d_device_get_swapchain_count(device->wined3d_device);
600 wined3d_mutex_unlock();
602 return count;
605 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
607 struct wined3d_resource_desc desc;
608 IDirect3DBaseTexture9 *texture;
609 struct d3d9_surface *surface;
610 IUnknown *parent;
612 wined3d_resource_get_desc(resource, &desc);
613 if (desc.pool != WINED3D_POOL_DEFAULT)
614 return D3D_OK;
616 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
618 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
619 return D3DERR_INVALIDCALL;
622 parent = wined3d_resource_get_parent(resource);
623 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
625 IDirect3DBaseTexture9_Release(texture);
626 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
627 return D3DERR_INVALIDCALL;
630 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
631 if (!surface->resource.refcount)
632 return D3D_OK;
634 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
635 return D3DERR_INVALIDCALL;
638 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
640 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
641 struct wined3d_swapchain *wined3d_swapchain;
643 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
644 new_swapchain_count * sizeof(*device->implicit_swapchains))))
645 return E_OUTOFMEMORY;
647 for (i = 0; i < new_swapchain_count; ++i)
649 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
650 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
652 device->implicit_swapchain_count = new_swapchain_count;
654 return D3D_OK;
657 static HRESULT d3d9_device_reset(struct d3d9_device *device,
658 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
660 struct wined3d_swapchain_desc swapchain_desc;
661 struct wined3d_display_mode wined3d_mode;
662 HRESULT hr;
664 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
666 WARN("App not active, returning D3DERR_DEVICELOST.\n");
667 return D3DERR_DEVICELOST;
670 if (mode)
672 wined3d_mode.width = mode->Width;
673 wined3d_mode.height = mode->Height;
674 wined3d_mode.refresh_rate = mode->RefreshRate;
675 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
676 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
679 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
680 device->d3d_parent->extended))
681 return D3DERR_INVALIDCALL;
683 wined3d_mutex_lock();
685 if (device->vertex_buffer)
687 wined3d_buffer_decref(device->vertex_buffer);
688 device->vertex_buffer = NULL;
689 device->vertex_buffer_size = 0;
692 if (device->index_buffer)
694 wined3d_buffer_decref(device->index_buffer);
695 device->index_buffer = NULL;
696 device->index_buffer_size = 0;
699 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
700 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
702 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
704 if (FAILED(hr = d3d9_device_get_swapchains(device)))
706 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
708 else
710 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
711 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
712 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
713 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
714 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
716 device->device_state = D3D9_DEVICE_STATE_OK;
719 else if (!device->d3d_parent->extended)
721 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
724 wined3d_mutex_unlock();
726 return hr;
729 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
730 D3DPRESENT_PARAMETERS *present_parameters)
732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
734 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
736 return d3d9_device_reset(device, present_parameters, NULL);
739 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
740 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
742 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
743 UINT i;
744 HRESULT hr;
746 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
747 iface, src_rect, dst_rect, dst_window_override, dirty_region);
749 if (device->device_state != D3D9_DEVICE_STATE_OK)
750 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
752 if (dirty_region)
753 FIXME("Ignoring dirty_region %p.\n", dirty_region);
755 wined3d_mutex_lock();
756 for (i = 0; i < device->implicit_swapchain_count; ++i)
758 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
759 src_rect, dst_rect, dst_window_override, 0)))
761 wined3d_mutex_unlock();
762 return hr;
765 wined3d_mutex_unlock();
767 return D3D_OK;
770 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
771 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
773 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
774 HRESULT hr;
776 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
777 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
779 /* backbuffer_type is ignored by native. */
781 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
782 *backbuffer = NULL;
784 wined3d_mutex_lock();
785 if (swapchain >= device->implicit_swapchain_count)
787 wined3d_mutex_unlock();
788 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
789 return D3DERR_INVALIDCALL;
792 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
793 backbuffer_idx, backbuffer_type, backbuffer);
794 wined3d_mutex_unlock();
796 return hr;
799 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
800 UINT swapchain, D3DRASTER_STATUS *raster_status)
802 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
803 HRESULT hr;
805 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
807 wined3d_mutex_lock();
808 hr = wined3d_device_get_raster_status(device->wined3d_device,
809 swapchain, (struct wined3d_raster_status *)raster_status);
810 wined3d_mutex_unlock();
812 return hr;
815 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
817 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
818 HRESULT hr;
820 TRACE("iface %p, enable %#x.\n", iface, enable);
822 wined3d_mutex_lock();
823 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
824 wined3d_mutex_unlock();
826 return hr;
829 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
830 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
832 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
834 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
836 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
837 wined3d_mutex_lock();
838 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
839 wined3d_mutex_unlock();
842 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
844 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
846 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
848 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
849 wined3d_mutex_lock();
850 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
851 wined3d_mutex_unlock();
854 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
855 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
856 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
858 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
859 struct d3d9_texture *object;
860 BOOL set_mem = FALSE;
861 HRESULT hr;
863 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
864 iface, width, height, levels, usage, format, pool, texture, shared_handle);
866 *texture = NULL;
867 if (shared_handle)
869 if (!device->d3d_parent->extended)
871 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
872 return E_NOTIMPL;
875 if (pool == D3DPOOL_SYSTEMMEM)
877 if (levels != 1)
878 return D3DERR_INVALIDCALL;
879 set_mem = TRUE;
881 else
883 if (pool != D3DPOOL_DEFAULT)
885 WARN("Trying to create a shared texture in pool %#x.\n", pool);
886 return D3DERR_INVALIDCALL;
888 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
892 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
893 if (!object)
894 return D3DERR_OUTOFVIDEOMEMORY;
896 hr = texture_init(object, device, width, height, levels, usage, format, pool);
897 if (FAILED(hr))
899 WARN("Failed to initialize texture, hr %#x.\n", hr);
900 HeapFree(GetProcessHeap(), 0, object);
901 return hr;
904 if (set_mem)
905 wined3d_texture_update_desc(object->wined3d_texture, width, height,
906 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
907 *shared_handle, 0);
909 TRACE("Created texture %p.\n", object);
910 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
912 return D3D_OK;
915 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
916 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
917 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
919 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
920 struct d3d9_texture *object;
921 HRESULT hr;
923 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
924 iface, width, height, depth, levels);
925 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
926 usage, format, pool, texture, shared_handle);
928 *texture = NULL;
929 if (shared_handle)
931 if (!device->d3d_parent->extended)
933 WARN("Trying to create a shared volume texture on a non-ex device.\n");
934 return E_NOTIMPL;
937 if (pool != D3DPOOL_DEFAULT)
939 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
940 return D3DERR_INVALIDCALL;
942 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
945 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
946 if (!object)
947 return D3DERR_OUTOFVIDEOMEMORY;
949 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
950 if (FAILED(hr))
952 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
953 HeapFree(GetProcessHeap(), 0, object);
954 return hr;
957 TRACE("Created volume texture %p.\n", object);
958 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
960 return D3D_OK;
963 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
964 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
965 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
967 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
968 struct d3d9_texture *object;
969 HRESULT hr;
971 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
972 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
974 *texture = NULL;
975 if (shared_handle)
977 if (!device->d3d_parent->extended)
979 WARN("Trying to create a shared cube texture on a non-ex device.\n");
980 return E_NOTIMPL;
983 if (pool != D3DPOOL_DEFAULT)
985 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
986 return D3DERR_INVALIDCALL;
988 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
991 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
992 if (!object)
993 return D3DERR_OUTOFVIDEOMEMORY;
995 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
996 if (FAILED(hr))
998 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
999 HeapFree(GetProcessHeap(), 0, object);
1000 return hr;
1003 TRACE("Created cube texture %p.\n", object);
1004 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1006 return D3D_OK;
1009 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1010 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1011 HANDLE *shared_handle)
1013 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1014 struct d3d9_vertexbuffer *object;
1015 HRESULT hr;
1017 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1018 iface, size, usage, fvf, pool, buffer, shared_handle);
1020 if (shared_handle)
1022 if (!device->d3d_parent->extended)
1024 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1025 return E_NOTIMPL;
1028 if (pool != D3DPOOL_DEFAULT)
1030 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1031 return D3DERR_NOTAVAILABLE;
1033 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1036 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1037 if (!object)
1038 return D3DERR_OUTOFVIDEOMEMORY;
1040 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1041 if (FAILED(hr))
1043 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1044 HeapFree(GetProcessHeap(), 0, object);
1045 return hr;
1048 TRACE("Created vertex buffer %p.\n", object);
1049 *buffer = &object->IDirect3DVertexBuffer9_iface;
1051 return D3D_OK;
1054 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1055 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1056 HANDLE *shared_handle)
1058 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1059 struct d3d9_indexbuffer *object;
1060 HRESULT hr;
1062 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1063 iface, size, usage, format, pool, buffer, shared_handle);
1065 if (shared_handle)
1067 if (!device->d3d_parent->extended)
1069 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1070 return E_NOTIMPL;
1073 if (pool != D3DPOOL_DEFAULT)
1075 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1076 return D3DERR_NOTAVAILABLE;
1078 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1081 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1082 if (!object)
1083 return D3DERR_OUTOFVIDEOMEMORY;
1085 hr = indexbuffer_init(object, device, size, usage, format, pool);
1086 if (FAILED(hr))
1088 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1089 HeapFree(GetProcessHeap(), 0, object);
1090 return hr;
1093 TRACE("Created index buffer %p.\n", object);
1094 *buffer = &object->IDirect3DIndexBuffer9_iface;
1096 return D3D_OK;
1099 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1100 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1101 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1103 struct wined3d_resource_desc desc;
1104 struct d3d9_surface *surface_impl;
1105 struct wined3d_texture *texture;
1106 HRESULT hr;
1108 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1109 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1110 device, width, height, format, flags, surface, usage, pool,
1111 multisample_type, multisample_quality);
1113 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1114 desc.format = wined3dformat_from_d3dformat(format);
1115 desc.multisample_type = multisample_type;
1116 desc.multisample_quality = multisample_quality;
1117 desc.usage = usage & WINED3DUSAGE_MASK;
1118 desc.pool = pool;
1119 desc.width = width;
1120 desc.height = height;
1121 desc.depth = 1;
1122 desc.size = 0;
1124 wined3d_mutex_lock();
1126 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1127 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1129 wined3d_mutex_unlock();
1130 WARN("Failed to create texture, hr %#x.\n", hr);
1131 if (hr == WINED3DERR_NOTAVAILABLE)
1132 hr = D3DERR_INVALIDCALL;
1133 return hr;
1136 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1137 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1138 *surface = &surface_impl->IDirect3DSurface9_iface;
1139 IDirect3DSurface9_AddRef(*surface);
1141 if (user_mem)
1142 wined3d_texture_update_desc(texture, width, height,
1143 desc.format, multisample_type, multisample_quality, user_mem, 0);
1145 wined3d_texture_decref(texture);
1147 wined3d_mutex_unlock();
1149 return D3D_OK;
1152 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1153 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1154 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1156 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1157 DWORD flags = 0;
1159 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1160 "lockable %#x, surface %p, shared_handle %p.\n",
1161 iface, width, height, format, multisample_type, multisample_quality,
1162 lockable, surface, shared_handle);
1164 *surface = NULL;
1165 if (shared_handle)
1167 if (!device->d3d_parent->extended)
1169 WARN("Trying to create a shared render target on a non-ex device.\n");
1170 return E_NOTIMPL;
1173 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1176 if (lockable)
1177 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1179 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1180 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1183 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1184 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1185 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1187 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1188 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1190 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1191 "discard %#x, surface %p, shared_handle %p.\n",
1192 iface, width, height, format, multisample_type, multisample_quality,
1193 discard, surface, shared_handle);
1195 *surface = NULL;
1196 if (shared_handle)
1198 if (!device->d3d_parent->extended)
1200 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1201 return E_NOTIMPL;
1204 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1207 if (discard)
1208 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1210 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1211 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1215 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1216 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1217 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1219 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1220 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1221 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1222 struct wined3d_box src_box;
1223 HRESULT hr;
1225 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1226 iface, src_surface, src_rect, dst_surface, dst_point);
1228 if (src_rect)
1230 src_box.left = src_rect->left;
1231 src_box.top = src_rect->top;
1232 src_box.right = src_rect->right;
1233 src_box.bottom = src_rect->bottom;
1234 src_box.front = 0;
1235 src_box.back = 1;
1238 wined3d_mutex_lock();
1239 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1240 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1241 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1242 src->sub_resource_idx, src_rect ? &src_box : NULL);
1243 wined3d_mutex_unlock();
1245 if (FAILED(hr))
1246 return D3DERR_INVALIDCALL;
1248 return hr;
1251 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1252 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1254 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1255 struct d3d9_texture *src_impl, *dst_impl;
1256 HRESULT hr;
1258 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1260 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1261 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1263 wined3d_mutex_lock();
1264 hr = wined3d_device_update_texture(device->wined3d_device,
1265 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1266 wined3d_mutex_unlock();
1268 return hr;
1271 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1272 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1274 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1275 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1276 struct wined3d_sub_resource_desc wined3d_desc;
1277 RECT dst_rect, src_rect;
1278 HRESULT hr;
1280 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1282 if (!render_target || !dst_surface)
1283 return D3DERR_INVALIDCALL;
1285 wined3d_mutex_lock();
1286 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1287 dst_rect.left = 0;
1288 dst_rect.top = 0;
1289 dst_rect.right = wined3d_desc.width;
1290 dst_rect.bottom = wined3d_desc.height;
1292 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1293 src_rect.left = 0;
1294 src_rect.top = 0;
1295 src_rect.right = wined3d_desc.width;
1296 src_rect.bottom = wined3d_desc.height;
1298 /* TODO: Check surface sizes, pools, etc. */
1299 if (wined3d_desc.multisample_type)
1300 hr = D3DERR_INVALIDCALL;
1301 else
1302 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1303 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1304 wined3d_mutex_unlock();
1306 return hr;
1309 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1310 UINT swapchain, IDirect3DSurface9 *dst_surface)
1312 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1313 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1314 HRESULT hr = D3DERR_INVALIDCALL;
1316 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1318 wined3d_mutex_lock();
1319 if (swapchain < device->implicit_swapchain_count)
1320 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1321 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1322 wined3d_mutex_unlock();
1324 return hr;
1327 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1328 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1330 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1331 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1332 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1333 struct wined3d_sub_resource_desc src_desc, dst_desc;
1334 HRESULT hr = D3DERR_INVALIDCALL;
1335 RECT d, s;
1337 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1338 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1340 wined3d_mutex_lock();
1341 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1342 if (!dst_rect)
1344 d.left = 0;
1345 d.top = 0;
1346 d.right = dst_desc.width;
1347 d.bottom = dst_desc.height;
1348 dst_rect = &d;
1351 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1352 if (!src_rect)
1354 s.left = 0;
1355 s.top = 0;
1356 s.right = src_desc.width;
1357 s.bottom = src_desc.height;
1358 src_rect = &s;
1361 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1363 if (device->in_scene)
1365 WARN("Rejecting depth / stencil blit while in scene.\n");
1366 goto done;
1369 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1370 || src_rect->bottom != src_desc.height)
1372 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1373 wine_dbgstr_rect(src_rect));
1374 goto done;
1377 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1378 || dst_rect->bottom != dst_desc.height)
1380 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1381 wine_dbgstr_rect(dst_rect));
1382 goto done;
1385 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1387 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1388 goto done;
1392 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1393 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1394 if (hr == WINEDDERR_INVALIDRECT)
1395 hr = D3DERR_INVALIDCALL;
1397 done:
1398 wined3d_mutex_unlock();
1399 return hr;
1402 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1403 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1405 const struct wined3d_color c =
1407 ((color >> 16) & 0xff) / 255.0f,
1408 ((color >> 8) & 0xff) / 255.0f,
1409 (color & 0xff) / 255.0f,
1410 ((color >> 24) & 0xff) / 255.0f,
1412 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1413 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1414 struct wined3d_sub_resource_desc desc;
1415 HRESULT hr;
1417 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1419 wined3d_mutex_lock();
1421 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1422 surface_impl->sub_resource_idx, &desc)))
1424 wined3d_mutex_unlock();
1425 return D3DERR_INVALIDCALL;
1428 if (desc.pool != WINED3D_POOL_DEFAULT)
1430 wined3d_mutex_unlock();
1431 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1432 return D3DERR_INVALIDCALL;
1434 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1436 wined3d_mutex_unlock();
1437 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1438 return D3DERR_INVALIDCALL;
1440 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1442 wined3d_mutex_unlock();
1443 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1444 return D3DERR_INVALIDCALL;
1447 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1448 d3d9_surface_get_rendertarget_view(surface_impl), rect,
1449 WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1451 wined3d_mutex_unlock();
1453 return hr;
1456 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1457 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1458 HANDLE *shared_handle)
1460 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1461 void *user_mem = NULL;
1463 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1464 iface, width, height, format, pool, surface, shared_handle);
1466 *surface = NULL;
1467 if (pool == D3DPOOL_MANAGED)
1469 WARN("Attempting to create a managed offscreen plain surface.\n");
1470 return D3DERR_INVALIDCALL;
1473 if (shared_handle)
1475 if (!device->d3d_parent->extended)
1477 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1478 return E_NOTIMPL;
1481 if (pool == D3DPOOL_SYSTEMMEM)
1482 user_mem = *shared_handle;
1483 else
1485 if (pool != D3DPOOL_DEFAULT)
1487 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1488 return D3DERR_INVALIDCALL;
1490 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1494 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1495 * regardless of the pool they're created in. Should we set dynamic usage
1496 * here? */
1497 return d3d9_device_create_surface(device, width, height, format,
1498 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1501 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1503 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1504 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1505 HRESULT hr;
1507 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1509 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1511 WARN("Invalid index %u specified.\n", idx);
1512 return D3DERR_INVALIDCALL;
1515 if (!idx && !surface_impl)
1517 WARN("Trying to set render target 0 to NULL.\n");
1518 return D3DERR_INVALIDCALL;
1521 wined3d_mutex_lock();
1522 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1523 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1524 wined3d_mutex_unlock();
1526 return hr;
1529 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1531 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1532 struct wined3d_rendertarget_view *wined3d_rtv;
1533 struct d3d9_surface *surface_impl;
1534 HRESULT hr = D3D_OK;
1536 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1538 if (!surface)
1539 return D3DERR_INVALIDCALL;
1541 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1543 WARN("Invalid index %u specified.\n", idx);
1544 return D3DERR_INVALIDCALL;
1547 wined3d_mutex_lock();
1548 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1550 /* We want the sub resource parent here, since the view itself may be
1551 * internal to wined3d and may not have a parent. */
1552 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1553 *surface = &surface_impl->IDirect3DSurface9_iface;
1554 IDirect3DSurface9_AddRef(*surface);
1556 else
1558 hr = D3DERR_NOTFOUND;
1559 *surface = NULL;
1561 wined3d_mutex_unlock();
1563 return hr;
1566 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1568 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1569 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1571 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1573 wined3d_mutex_lock();
1574 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1575 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1576 wined3d_mutex_unlock();
1578 return D3D_OK;
1581 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1583 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1584 struct wined3d_rendertarget_view *wined3d_dsv;
1585 struct d3d9_surface *surface_impl;
1586 HRESULT hr = D3D_OK;
1588 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1590 if (!depth_stencil)
1591 return D3DERR_INVALIDCALL;
1593 wined3d_mutex_lock();
1594 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1596 /* We want the sub resource parent here, since the view itself may be
1597 * internal to wined3d and may not have a parent. */
1598 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1599 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1600 IDirect3DSurface9_AddRef(*depth_stencil);
1602 else
1604 hr = D3DERR_NOTFOUND;
1605 *depth_stencil = NULL;
1607 wined3d_mutex_unlock();
1609 return hr;
1612 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1614 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1615 HRESULT hr;
1617 TRACE("iface %p.\n", iface);
1619 wined3d_mutex_lock();
1620 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1621 device->in_scene = TRUE;
1622 wined3d_mutex_unlock();
1624 return hr;
1627 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1630 HRESULT hr;
1632 TRACE("iface %p.\n", iface);
1634 wined3d_mutex_lock();
1635 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1636 device->in_scene = FALSE;
1637 wined3d_mutex_unlock();
1639 return hr;
1642 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1643 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1645 const struct wined3d_color c =
1647 ((color >> 16) & 0xff) / 255.0f,
1648 ((color >> 8) & 0xff) / 255.0f,
1649 (color & 0xff) / 255.0f,
1650 ((color >> 24) & 0xff) / 255.0f,
1652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1653 HRESULT hr;
1655 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1656 iface, rect_count, rects, flags, color, z, stencil);
1658 if (rect_count && !rects)
1660 WARN("count %u with NULL rects.\n", rect_count);
1661 rect_count = 0;
1664 wined3d_mutex_lock();
1665 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1666 wined3d_mutex_unlock();
1668 return hr;
1671 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1672 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1674 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1676 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1678 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1679 wined3d_mutex_lock();
1680 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1681 wined3d_mutex_unlock();
1683 return D3D_OK;
1686 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1687 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1689 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1691 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1693 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1694 wined3d_mutex_lock();
1695 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1696 wined3d_mutex_unlock();
1698 return D3D_OK;
1701 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1702 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1704 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1706 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1708 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1709 wined3d_mutex_lock();
1710 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1711 wined3d_mutex_unlock();
1713 return D3D_OK;
1716 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1720 TRACE("iface %p, viewport %p.\n", iface, viewport);
1722 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1723 wined3d_mutex_lock();
1724 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1725 wined3d_mutex_unlock();
1727 return D3D_OK;
1730 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1734 TRACE("iface %p, viewport %p.\n", iface, viewport);
1736 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1737 wined3d_mutex_lock();
1738 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1739 wined3d_mutex_unlock();
1741 return D3D_OK;
1744 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1746 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1748 TRACE("iface %p, material %p.\n", iface, material);
1750 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1751 wined3d_mutex_lock();
1752 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1753 wined3d_mutex_unlock();
1755 return D3D_OK;
1758 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1760 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1762 TRACE("iface %p, material %p.\n", iface, material);
1764 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1765 wined3d_mutex_lock();
1766 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1767 wined3d_mutex_unlock();
1769 return D3D_OK;
1772 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1774 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1775 HRESULT hr;
1777 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1779 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1780 wined3d_mutex_lock();
1781 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1782 wined3d_mutex_unlock();
1784 return hr;
1787 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1789 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1790 HRESULT hr;
1792 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1794 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1795 wined3d_mutex_lock();
1796 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1797 wined3d_mutex_unlock();
1799 return hr;
1802 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1804 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1805 HRESULT hr;
1807 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1809 wined3d_mutex_lock();
1810 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1811 wined3d_mutex_unlock();
1813 return hr;
1816 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1818 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1819 HRESULT hr;
1821 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1823 wined3d_mutex_lock();
1824 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1825 wined3d_mutex_unlock();
1827 return hr;
1830 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1832 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1833 HRESULT hr;
1835 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1837 wined3d_mutex_lock();
1838 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1839 wined3d_mutex_unlock();
1841 return hr;
1844 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1846 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1847 HRESULT hr;
1849 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1851 wined3d_mutex_lock();
1852 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1853 wined3d_mutex_unlock();
1855 return hr;
1858 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1859 D3DRENDERSTATETYPE state, DWORD value)
1861 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1863 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1865 wined3d_mutex_lock();
1866 wined3d_device_set_render_state(device->wined3d_device, state, value);
1867 wined3d_mutex_unlock();
1869 return D3D_OK;
1872 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1873 D3DRENDERSTATETYPE state, DWORD *value)
1875 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1877 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1879 wined3d_mutex_lock();
1880 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1881 wined3d_mutex_unlock();
1883 return D3D_OK;
1886 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1887 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1889 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1890 struct d3d9_stateblock *object;
1891 HRESULT hr;
1893 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1895 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1897 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1898 return D3DERR_INVALIDCALL;
1901 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1902 if (!object)
1903 return E_OUTOFMEMORY;
1905 hr = stateblock_init(object, device, type, NULL);
1906 if (FAILED(hr))
1908 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1909 HeapFree(GetProcessHeap(), 0, object);
1910 return hr;
1913 TRACE("Created stateblock %p.\n", object);
1914 *stateblock = &object->IDirect3DStateBlock9_iface;
1916 return D3D_OK;
1919 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1921 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1922 HRESULT hr;
1924 TRACE("iface %p.\n", iface);
1926 wined3d_mutex_lock();
1927 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1928 wined3d_mutex_unlock();
1930 return hr;
1933 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1935 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1936 struct wined3d_stateblock *wined3d_stateblock;
1937 struct d3d9_stateblock *object;
1938 HRESULT hr;
1940 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1942 wined3d_mutex_lock();
1943 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1944 wined3d_mutex_unlock();
1945 if (FAILED(hr))
1947 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1948 return hr;
1951 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1952 if (!object)
1954 wined3d_mutex_lock();
1955 wined3d_stateblock_decref(wined3d_stateblock);
1956 wined3d_mutex_unlock();
1957 return E_OUTOFMEMORY;
1960 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1961 if (FAILED(hr))
1963 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1964 wined3d_mutex_lock();
1965 wined3d_stateblock_decref(wined3d_stateblock);
1966 wined3d_mutex_unlock();
1967 HeapFree(GetProcessHeap(), 0, object);
1968 return hr;
1971 TRACE("Created stateblock %p.\n", object);
1972 *stateblock = &object->IDirect3DStateBlock9_iface;
1974 return D3D_OK;
1977 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1979 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1980 HRESULT hr;
1982 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1984 wined3d_mutex_lock();
1985 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1986 wined3d_mutex_unlock();
1988 return hr;
1991 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1993 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1994 HRESULT hr;
1996 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1998 wined3d_mutex_lock();
1999 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2000 wined3d_mutex_unlock();
2002 return hr;
2005 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2007 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2008 struct wined3d_texture *wined3d_texture = NULL;
2009 struct d3d9_texture *texture_impl;
2011 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2013 if (!texture)
2014 return D3DERR_INVALIDCALL;
2016 wined3d_mutex_lock();
2017 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2019 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2020 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2021 IDirect3DBaseTexture9_AddRef(*texture);
2023 else
2025 *texture = NULL;
2027 wined3d_mutex_unlock();
2029 return D3D_OK;
2032 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2034 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2035 struct d3d9_texture *texture_impl;
2036 HRESULT hr;
2038 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2040 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2042 wined3d_mutex_lock();
2043 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2044 texture_impl ? texture_impl->wined3d_texture : NULL);
2045 wined3d_mutex_unlock();
2047 return hr;
2050 static const enum wined3d_texture_stage_state tss_lookup[] =
2052 WINED3D_TSS_INVALID, /* 0, unused */
2053 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2054 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2055 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2056 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2057 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2058 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2059 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2060 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2061 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2062 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2063 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2064 WINED3D_TSS_INVALID, /* 12, unused */
2065 WINED3D_TSS_INVALID, /* 13, unused */
2066 WINED3D_TSS_INVALID, /* 14, unused */
2067 WINED3D_TSS_INVALID, /* 15, unused */
2068 WINED3D_TSS_INVALID, /* 16, unused */
2069 WINED3D_TSS_INVALID, /* 17, unused */
2070 WINED3D_TSS_INVALID, /* 18, unused */
2071 WINED3D_TSS_INVALID, /* 19, unused */
2072 WINED3D_TSS_INVALID, /* 20, unused */
2073 WINED3D_TSS_INVALID, /* 21, unused */
2074 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2075 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2076 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2077 WINED3D_TSS_INVALID, /* 25, unused */
2078 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2079 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2080 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2081 WINED3D_TSS_INVALID, /* 29, unused */
2082 WINED3D_TSS_INVALID, /* 30, unused */
2083 WINED3D_TSS_INVALID, /* 31, unused */
2084 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2087 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2088 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2090 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2092 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2094 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2096 WARN("Invalid state %#x passed.\n", state);
2097 return D3D_OK;
2100 wined3d_mutex_lock();
2101 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2102 wined3d_mutex_unlock();
2104 return D3D_OK;
2107 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2108 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2110 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2112 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2114 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2116 WARN("Invalid state %#x passed.\n", state);
2117 return D3D_OK;
2120 wined3d_mutex_lock();
2121 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2122 wined3d_mutex_unlock();
2124 return D3D_OK;
2127 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2128 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2130 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2132 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2134 wined3d_mutex_lock();
2135 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2136 wined3d_mutex_unlock();
2138 return D3D_OK;
2141 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2142 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2144 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2146 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2148 wined3d_mutex_lock();
2149 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2150 wined3d_mutex_unlock();
2152 return D3D_OK;
2155 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2157 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2158 HRESULT hr;
2160 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2162 wined3d_mutex_lock();
2163 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2164 wined3d_mutex_unlock();
2166 return hr;
2169 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2170 UINT palette_idx, const PALETTEENTRY *entries)
2172 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2174 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2175 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2176 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2178 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2179 return D3D_OK;
2182 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2183 UINT palette_idx, PALETTEENTRY *entries)
2185 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2187 return D3DERR_INVALIDCALL;
2190 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2192 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2194 return D3D_OK;
2197 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2199 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2201 return D3DERR_INVALIDCALL;
2204 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2206 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2208 TRACE("iface %p, rect %p.\n", iface, rect);
2210 wined3d_mutex_lock();
2211 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2212 wined3d_mutex_unlock();
2214 return D3D_OK;
2217 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2219 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2221 TRACE("iface %p, rect %p.\n", iface, rect);
2223 wined3d_mutex_lock();
2224 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2225 wined3d_mutex_unlock();
2227 return D3D_OK;
2230 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2232 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2234 TRACE("iface %p, software %#x.\n", iface, software);
2236 wined3d_mutex_lock();
2237 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2238 wined3d_mutex_unlock();
2240 return D3D_OK;
2243 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2245 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2246 BOOL ret;
2248 TRACE("iface %p.\n", iface);
2250 wined3d_mutex_lock();
2251 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2252 wined3d_mutex_unlock();
2254 return ret;
2257 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2259 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2260 HRESULT hr;
2262 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2264 wined3d_mutex_lock();
2265 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2266 wined3d_mutex_unlock();
2268 return hr;
2271 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2273 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2274 float ret;
2276 TRACE("iface %p.\n", iface);
2278 wined3d_mutex_lock();
2279 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2280 wined3d_mutex_unlock();
2282 return ret;
2285 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2286 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2289 HRESULT hr;
2291 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2292 iface, primitive_type, start_vertex, primitive_count);
2294 wined3d_mutex_lock();
2295 if (!device->has_vertex_declaration)
2297 wined3d_mutex_unlock();
2298 WARN("Called without a valid vertex declaration set.\n");
2299 return D3DERR_INVALIDCALL;
2301 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2302 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2303 vertex_count_from_primitive_count(primitive_type, primitive_count));
2304 wined3d_mutex_unlock();
2306 return hr;
2309 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2310 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2311 UINT vertex_count, UINT start_idx, UINT primitive_count)
2313 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2314 HRESULT hr;
2316 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2317 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2318 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2319 vertex_count, start_idx, primitive_count);
2321 wined3d_mutex_lock();
2322 if (!device->has_vertex_declaration)
2324 wined3d_mutex_unlock();
2325 WARN("Called without a valid vertex declaration set.\n");
2326 return D3DERR_INVALIDCALL;
2328 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2329 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2330 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2331 vertex_count_from_primitive_count(primitive_type, primitive_count));
2332 wined3d_mutex_unlock();
2334 return hr;
2337 /* The caller is responsible for wined3d locking */
2338 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2340 HRESULT hr;
2342 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2344 UINT size = max(device->vertex_buffer_size * 2, min_size);
2345 struct wined3d_buffer *buffer;
2347 TRACE("Growing vertex buffer to %u bytes.\n", size);
2349 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2350 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2351 if (FAILED(hr))
2353 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2354 return hr;
2357 if (device->vertex_buffer)
2358 wined3d_buffer_decref(device->vertex_buffer);
2360 device->vertex_buffer = buffer;
2361 device->vertex_buffer_size = size;
2362 device->vertex_buffer_pos = 0;
2364 return D3D_OK;
2367 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2368 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2370 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2371 HRESULT hr;
2372 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2373 UINT size = vtx_count * stride;
2374 UINT vb_pos, align;
2375 BYTE *buffer_data;
2377 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2378 iface, primitive_type, primitive_count, data, stride);
2380 if (!primitive_count)
2382 WARN("primitive_count is 0, returning D3D_OK\n");
2383 return D3D_OK;
2386 wined3d_mutex_lock();
2388 if (!device->has_vertex_declaration)
2390 wined3d_mutex_unlock();
2391 WARN("Called without a valid vertex declaration set.\n");
2392 return D3DERR_INVALIDCALL;
2395 hr = d3d9_device_prepare_vertex_buffer(device, size);
2396 if (FAILED(hr))
2397 goto done;
2399 vb_pos = device->vertex_buffer_pos;
2400 align = vb_pos % stride;
2401 if (align) align = stride - align;
2402 if (vb_pos + size + align > device->vertex_buffer_size)
2403 vb_pos = 0;
2404 else
2405 vb_pos += align;
2407 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2408 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2409 if (FAILED(hr))
2410 goto done;
2411 memcpy(buffer_data, data, size);
2412 wined3d_buffer_unmap(device->vertex_buffer);
2413 device->vertex_buffer_pos = vb_pos + size;
2415 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2416 if (FAILED(hr))
2417 goto done;
2419 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2420 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2421 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2423 done:
2424 wined3d_mutex_unlock();
2425 return hr;
2428 /* The caller is responsible for wined3d locking */
2429 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2431 HRESULT hr;
2433 if (device->index_buffer_size < min_size || !device->index_buffer)
2435 UINT size = max(device->index_buffer_size * 2, min_size);
2436 struct wined3d_buffer *buffer;
2438 TRACE("Growing index buffer to %u bytes.\n", size);
2440 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2441 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2442 if (FAILED(hr))
2444 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2445 return hr;
2448 if (device->index_buffer)
2449 wined3d_buffer_decref(device->index_buffer);
2451 device->index_buffer = buffer;
2452 device->index_buffer_size = size;
2453 device->index_buffer_pos = 0;
2455 return D3D_OK;
2458 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2459 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2460 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2461 const void *vertex_data, UINT vertex_stride)
2463 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2464 HRESULT hr;
2465 BYTE *buffer_data;
2467 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2468 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2469 UINT idx_size = idx_count * idx_fmt_size;
2470 UINT ib_pos;
2472 UINT vtx_size = vertex_count * vertex_stride;
2473 UINT vb_pos, align;
2475 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2476 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2477 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2478 index_data, index_format, vertex_data, vertex_stride);
2480 if (!primitive_count)
2482 WARN("primitive_count is 0, returning D3D_OK.\n");
2483 return D3D_OK;
2486 wined3d_mutex_lock();
2488 if (!device->has_vertex_declaration)
2490 wined3d_mutex_unlock();
2491 WARN("Called without a valid vertex declaration set.\n");
2492 return D3DERR_INVALIDCALL;
2495 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2496 if (FAILED(hr))
2497 goto done;
2499 vb_pos = device->vertex_buffer_pos;
2500 align = vb_pos % vertex_stride;
2501 if (align) align = vertex_stride - align;
2502 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2503 vb_pos = 0;
2504 else
2505 vb_pos += align;
2507 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2508 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2509 if (FAILED(hr))
2510 goto done;
2511 memcpy(buffer_data, vertex_data, vtx_size);
2512 wined3d_buffer_unmap(device->vertex_buffer);
2513 device->vertex_buffer_pos = vb_pos + vtx_size;
2515 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2516 if (FAILED(hr))
2517 goto done;
2519 ib_pos = device->index_buffer_pos;
2520 align = ib_pos % idx_fmt_size;
2521 if (align) align = idx_fmt_size - align;
2522 if (ib_pos + idx_size + align > device->index_buffer_size)
2523 ib_pos = 0;
2524 else
2525 ib_pos += align;
2527 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2528 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2529 if (FAILED(hr))
2530 goto done;
2531 memcpy(buffer_data, index_data, idx_size);
2532 wined3d_buffer_unmap(device->index_buffer);
2533 device->index_buffer_pos = ib_pos + idx_size;
2535 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2536 if (FAILED(hr))
2537 goto done;
2539 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2540 wined3dformat_from_d3dformat(index_format), 0);
2541 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2543 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2544 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2546 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2547 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2548 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2550 done:
2551 wined3d_mutex_unlock();
2552 return hr;
2555 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2556 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2557 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2559 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2560 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2561 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2562 HRESULT hr;
2564 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2565 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2567 wined3d_mutex_lock();
2568 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2569 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2570 flags, dst_impl->fvf);
2571 wined3d_mutex_unlock();
2573 return hr;
2576 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2577 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2579 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2580 struct d3d9_vertex_declaration *object;
2581 HRESULT hr;
2583 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2585 if (!declaration)
2587 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2588 return D3DERR_INVALIDCALL;
2591 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2592 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2594 return hr;
2597 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2598 IDirect3DVertexDeclaration9 *declaration)
2600 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2601 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2603 TRACE("iface %p, declaration %p.\n", iface, declaration);
2605 wined3d_mutex_lock();
2606 wined3d_device_set_vertex_declaration(device->wined3d_device,
2607 decl_impl ? decl_impl->wined3d_declaration : NULL);
2608 device->has_vertex_declaration = !!decl_impl;
2609 wined3d_mutex_unlock();
2611 return D3D_OK;
2614 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2615 IDirect3DVertexDeclaration9 **declaration)
2617 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2618 struct wined3d_vertex_declaration *wined3d_declaration;
2619 struct d3d9_vertex_declaration *declaration_impl;
2621 TRACE("iface %p, declaration %p.\n", iface, declaration);
2623 if (!declaration) return D3DERR_INVALIDCALL;
2625 wined3d_mutex_lock();
2626 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2628 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2629 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2630 IDirect3DVertexDeclaration9_AddRef(*declaration);
2632 else
2634 *declaration = NULL;
2636 wined3d_mutex_unlock();
2638 TRACE("Returning %p.\n", *declaration);
2639 return D3D_OK;
2642 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2644 struct wined3d_vertex_declaration *wined3d_declaration;
2645 struct fvf_declaration *fvf_decls = device->fvf_decls;
2646 struct d3d9_vertex_declaration *d3d9_declaration;
2647 D3DVERTEXELEMENT9 *elements;
2648 int p, low, high; /* deliberately signed */
2649 HRESULT hr;
2651 TRACE("Searching for declaration for fvf %08x... ", fvf);
2653 low = 0;
2654 high = device->fvf_decl_count - 1;
2655 while (low <= high)
2657 p = (low + high) >> 1;
2658 TRACE("%d ", p);
2660 if (fvf_decls[p].fvf == fvf)
2662 TRACE("found %p.\n", fvf_decls[p].decl);
2663 return fvf_decls[p].decl;
2666 if (fvf_decls[p].fvf < fvf)
2667 low = p + 1;
2668 else
2669 high = p - 1;
2671 TRACE("not found. Creating and inserting at position %d.\n", low);
2673 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2674 return NULL;
2676 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2677 HeapFree(GetProcessHeap(), 0, elements);
2678 if (FAILED(hr))
2679 return NULL;
2681 if (device->fvf_decl_size == device->fvf_decl_count)
2683 UINT grow = max(device->fvf_decl_size / 2, 8);
2685 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2686 if (!fvf_decls)
2688 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2689 return NULL;
2691 device->fvf_decls = fvf_decls;
2692 device->fvf_decl_size += grow;
2695 d3d9_declaration->fvf = fvf;
2696 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2697 wined3d_vertex_declaration_incref(wined3d_declaration);
2698 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2700 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2701 fvf_decls[low].decl = wined3d_declaration;
2702 fvf_decls[low].fvf = fvf;
2703 ++device->fvf_decl_count;
2705 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2707 return wined3d_declaration;
2710 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2712 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2713 struct wined3d_vertex_declaration *decl;
2715 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2717 if (!fvf)
2719 WARN("%#x is not a valid FVF.\n", fvf);
2720 return D3D_OK;
2723 wined3d_mutex_lock();
2724 if (!(decl = device_get_fvf_declaration(device, fvf)))
2726 wined3d_mutex_unlock();
2727 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2728 return D3DERR_DRIVERINTERNALERROR;
2731 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2732 device->has_vertex_declaration = TRUE;
2733 wined3d_mutex_unlock();
2735 return D3D_OK;
2738 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2740 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2741 struct wined3d_vertex_declaration *wined3d_declaration;
2742 struct d3d9_vertex_declaration *d3d9_declaration;
2744 TRACE("iface %p, fvf %p.\n", iface, fvf);
2746 wined3d_mutex_lock();
2747 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2749 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2750 *fvf = d3d9_declaration->fvf;
2752 else
2754 *fvf = 0;
2756 wined3d_mutex_unlock();
2758 TRACE("Returning FVF %#x.\n", *fvf);
2760 return D3D_OK;
2763 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2764 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2766 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2767 struct d3d9_vertexshader *object;
2768 HRESULT hr;
2770 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2772 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2773 if (!object)
2774 return E_OUTOFMEMORY;
2776 hr = vertexshader_init(object, device, byte_code);
2777 if (FAILED(hr))
2779 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2780 HeapFree(GetProcessHeap(), 0, object);
2781 return hr;
2784 TRACE("Created vertex shader %p.\n", object);
2785 *shader = &object->IDirect3DVertexShader9_iface;
2787 return D3D_OK;
2790 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2792 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2793 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2795 TRACE("iface %p, shader %p.\n", iface, shader);
2797 wined3d_mutex_lock();
2798 wined3d_device_set_vertex_shader(device->wined3d_device,
2799 shader_obj ? shader_obj->wined3d_shader : NULL);
2800 wined3d_mutex_unlock();
2802 return D3D_OK;
2805 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2807 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2808 struct d3d9_vertexshader *shader_impl;
2809 struct wined3d_shader *wined3d_shader;
2811 TRACE("iface %p, shader %p.\n", iface, shader);
2813 wined3d_mutex_lock();
2814 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2816 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2817 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2818 IDirect3DVertexShader9_AddRef(*shader);
2820 else
2822 *shader = NULL;
2824 wined3d_mutex_unlock();
2826 TRACE("Returning %p.\n", *shader);
2828 return D3D_OK;
2831 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2832 UINT reg_idx, const float *data, UINT count)
2834 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2835 HRESULT hr;
2837 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2839 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2841 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2842 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2843 return D3DERR_INVALIDCALL;
2846 wined3d_mutex_lock();
2847 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
2848 reg_idx, count, (const struct wined3d_vec4 *)data);
2849 wined3d_mutex_unlock();
2851 return hr;
2854 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2855 UINT reg_idx, float *data, UINT count)
2857 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2858 HRESULT hr;
2860 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2862 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2864 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2865 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2866 return D3DERR_INVALIDCALL;
2869 wined3d_mutex_lock();
2870 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
2871 reg_idx, count, (struct wined3d_vec4 *)data);
2872 wined3d_mutex_unlock();
2874 return hr;
2877 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2878 UINT reg_idx, const int *data, UINT count)
2880 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2881 HRESULT hr;
2883 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2885 wined3d_mutex_lock();
2886 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
2887 reg_idx, count, (const struct wined3d_ivec4 *)data);
2888 wined3d_mutex_unlock();
2890 return hr;
2893 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2894 UINT reg_idx, int *data, UINT count)
2896 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2897 HRESULT hr;
2899 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2901 wined3d_mutex_lock();
2902 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
2903 reg_idx, count, (struct wined3d_ivec4 *)data);
2904 wined3d_mutex_unlock();
2906 return hr;
2909 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2910 UINT reg_idx, const BOOL *data, UINT count)
2912 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2913 HRESULT hr;
2915 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2917 wined3d_mutex_lock();
2918 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2919 wined3d_mutex_unlock();
2921 return hr;
2924 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2925 UINT reg_idx, BOOL *data, UINT count)
2927 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2928 HRESULT hr;
2930 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2932 wined3d_mutex_lock();
2933 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
2934 wined3d_mutex_unlock();
2936 return hr;
2939 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2940 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2943 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2944 HRESULT hr;
2946 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2947 iface, stream_idx, buffer, offset, stride);
2949 wined3d_mutex_lock();
2950 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2951 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2952 wined3d_mutex_unlock();
2954 return hr;
2957 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2958 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2960 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2961 struct d3d9_vertexbuffer *buffer_impl;
2962 struct wined3d_buffer *wined3d_buffer;
2963 HRESULT hr;
2965 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2966 iface, stream_idx, buffer, offset, stride);
2968 if (!buffer)
2969 return D3DERR_INVALIDCALL;
2971 wined3d_mutex_lock();
2972 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2973 if (SUCCEEDED(hr) && wined3d_buffer)
2975 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2976 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2977 IDirect3DVertexBuffer9_AddRef(*buffer);
2979 else
2981 if (FAILED(hr))
2982 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2983 *buffer = NULL;
2985 wined3d_mutex_unlock();
2987 return hr;
2990 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2992 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2993 HRESULT hr;
2995 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2997 wined3d_mutex_lock();
2998 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2999 wined3d_mutex_unlock();
3001 return hr;
3004 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3006 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3007 HRESULT hr;
3009 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3011 wined3d_mutex_lock();
3012 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3013 wined3d_mutex_unlock();
3015 return hr;
3018 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3020 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3021 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3023 TRACE("iface %p, buffer %p.\n", iface, buffer);
3025 wined3d_mutex_lock();
3026 wined3d_device_set_index_buffer(device->wined3d_device,
3027 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3028 wined3d_mutex_unlock();
3030 return D3D_OK;
3033 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3035 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3036 enum wined3d_format_id wined3d_format;
3037 struct wined3d_buffer *wined3d_buffer;
3038 struct d3d9_indexbuffer *buffer_impl;
3040 TRACE("iface %p, buffer %p.\n", iface, buffer);
3042 if (!buffer)
3043 return D3DERR_INVALIDCALL;
3045 wined3d_mutex_lock();
3046 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3048 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3049 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3050 IDirect3DIndexBuffer9_AddRef(*buffer);
3052 else
3054 *buffer = NULL;
3056 wined3d_mutex_unlock();
3058 return D3D_OK;
3061 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3062 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3064 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3065 struct d3d9_pixelshader *object;
3066 HRESULT hr;
3068 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3070 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3071 if (!object)
3073 FIXME("Failed to allocate pixel shader memory.\n");
3074 return E_OUTOFMEMORY;
3077 hr = pixelshader_init(object, device, byte_code);
3078 if (FAILED(hr))
3080 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3081 HeapFree(GetProcessHeap(), 0, object);
3082 return hr;
3085 TRACE("Created pixel shader %p.\n", object);
3086 *shader = &object->IDirect3DPixelShader9_iface;
3088 return D3D_OK;
3091 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3093 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3094 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3096 TRACE("iface %p, shader %p.\n", iface, shader);
3098 wined3d_mutex_lock();
3099 wined3d_device_set_pixel_shader(device->wined3d_device,
3100 shader_obj ? shader_obj->wined3d_shader : NULL);
3101 wined3d_mutex_unlock();
3103 return D3D_OK;
3106 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3108 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3109 struct d3d9_pixelshader *shader_impl;
3110 struct wined3d_shader *wined3d_shader;
3112 TRACE("iface %p, shader %p.\n", iface, shader);
3114 if (!shader) return D3DERR_INVALIDCALL;
3116 wined3d_mutex_lock();
3117 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3119 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3120 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3121 IDirect3DPixelShader9_AddRef(*shader);
3123 else
3125 *shader = NULL;
3127 wined3d_mutex_unlock();
3129 TRACE("Returning %p.\n", *shader);
3131 return D3D_OK;
3134 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3135 UINT reg_idx, const float *data, UINT count)
3137 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3138 HRESULT hr;
3140 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3142 wined3d_mutex_lock();
3143 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3144 reg_idx, count, (const struct wined3d_vec4 *)data);
3145 wined3d_mutex_unlock();
3147 return hr;
3150 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3151 UINT reg_idx, float *data, UINT count)
3153 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3154 HRESULT hr;
3156 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3158 wined3d_mutex_lock();
3159 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3160 reg_idx, count, (struct wined3d_vec4 *)data);
3161 wined3d_mutex_unlock();
3163 return hr;
3166 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3167 UINT reg_idx, const int *data, UINT count)
3169 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3170 HRESULT hr;
3172 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3174 wined3d_mutex_lock();
3175 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3176 reg_idx, count, (const struct wined3d_ivec4 *)data);
3177 wined3d_mutex_unlock();
3179 return hr;
3182 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3183 UINT reg_idx, int *data, UINT count)
3185 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3186 HRESULT hr;
3188 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3190 wined3d_mutex_lock();
3191 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3192 reg_idx, count, (struct wined3d_ivec4 *)data);
3193 wined3d_mutex_unlock();
3195 return hr;
3198 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3199 UINT reg_idx, const BOOL *data, UINT count)
3201 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3202 HRESULT hr;
3204 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3206 wined3d_mutex_lock();
3207 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3208 wined3d_mutex_unlock();
3210 return hr;
3213 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3214 UINT reg_idx, BOOL *data, UINT count)
3216 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3217 HRESULT hr;
3219 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3221 wined3d_mutex_lock();
3222 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3223 wined3d_mutex_unlock();
3225 return hr;
3228 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3229 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3231 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3232 iface, handle, segment_count, patch_info);
3233 return D3D_OK;
3236 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3237 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3239 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3240 iface, handle, segment_count, patch_info);
3241 return D3D_OK;
3244 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3246 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3247 return D3DERR_INVALIDCALL;
3250 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3252 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3253 struct d3d9_query *object;
3254 HRESULT hr;
3256 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3258 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3259 if (!object)
3260 return E_OUTOFMEMORY;
3262 hr = query_init(object, device, type);
3263 if (FAILED(hr))
3265 WARN("Failed to initialize query, hr %#x.\n", hr);
3266 HeapFree(GetProcessHeap(), 0, object);
3267 return hr;
3270 TRACE("Created query %p.\n", object);
3271 if (query) *query = &object->IDirect3DQuery9_iface;
3272 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3274 return D3D_OK;
3277 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3278 UINT width, UINT height, float *rows, float *columns)
3280 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3281 iface, width, height, rows, columns);
3283 return E_NOTIMPL;
3286 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3287 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3288 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3290 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3291 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3292 iface, src_surface, dst_surface, src_descs, rect_count,
3293 dst_descs, operation, offset_x, offset_y);
3295 return E_NOTIMPL;
3298 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3299 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3300 const RGNDATA *dirty_region, DWORD flags)
3302 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3303 UINT i;
3304 HRESULT hr;
3306 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3307 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3308 dst_window_override, dirty_region, flags);
3310 if (device->device_state != D3D9_DEVICE_STATE_OK)
3311 return S_PRESENT_OCCLUDED;
3313 if (dirty_region)
3314 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3316 wined3d_mutex_lock();
3317 for (i = 0; i < device->implicit_swapchain_count; ++i)
3319 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3320 src_rect, dst_rect, dst_window_override, flags)))
3322 wined3d_mutex_unlock();
3323 return hr;
3326 wined3d_mutex_unlock();
3328 return D3D_OK;
3331 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3333 FIXME("iface %p, priority %p stub!\n", iface, priority);
3335 return E_NOTIMPL;
3338 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3340 FIXME("iface %p, priority %d stub!\n", iface, priority);
3342 return E_NOTIMPL;
3345 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3347 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3349 return E_NOTIMPL;
3352 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3353 IDirect3DResource9 **resources, UINT32 resource_count)
3355 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3356 iface, resources, resource_count);
3358 return E_NOTIMPL;
3361 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3363 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3365 return E_NOTIMPL;
3368 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3370 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3372 *max_latency = 2;
3374 return E_NOTIMPL;
3377 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3379 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3380 struct wined3d_swapchain_desc swapchain_desc;
3382 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3384 wined3d_mutex_lock();
3385 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3386 wined3d_mutex_unlock();
3388 if (swapchain_desc.windowed)
3389 return D3D_OK;
3391 /* FIXME: This is actually supposed to check if any other device is in
3392 * fullscreen mode. */
3393 if (dst_window != swapchain_desc.device_window)
3394 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3396 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3399 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3400 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3401 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3403 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3404 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3405 iface, width, height, format, multisample_type, multisample_quality,
3406 lockable, surface, shared_handle, usage);
3408 *surface = NULL;
3409 if (shared_handle)
3410 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3412 return E_NOTIMPL;
3415 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3416 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3417 HANDLE *shared_handle, DWORD usage)
3419 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3420 iface, width, height, format, pool, surface, shared_handle, usage);
3422 return E_NOTIMPL;
3425 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3426 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3427 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3429 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3430 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3432 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3433 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3434 iface, width, height, format, multisample_type, multisample_quality,
3435 discard, surface, shared_handle, usage);
3437 if (usage & D3DUSAGE_DEPTHSTENCIL)
3439 WARN("Invalid usage %#x.\n", usage);
3440 return D3DERR_INVALIDCALL;
3443 if (shared_handle)
3444 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3446 if (discard)
3447 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3449 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3450 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3453 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3454 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3456 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3458 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3460 return d3d9_device_reset(device, present_parameters, mode);
3463 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3464 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3466 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3467 struct wined3d_display_mode wined3d_mode;
3468 HRESULT hr;
3470 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3471 iface, swapchain_idx, mode, rotation);
3473 if (mode->Size != sizeof(*mode))
3474 return D3DERR_INVALIDCALL;
3476 wined3d_mutex_lock();
3477 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3478 (enum wined3d_display_rotation *)rotation);
3479 wined3d_mutex_unlock();
3481 if (SUCCEEDED(hr))
3483 mode->Width = wined3d_mode.width;
3484 mode->Height = wined3d_mode.height;
3485 mode->RefreshRate = wined3d_mode.refresh_rate;
3486 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3487 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3490 return hr;
3493 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3495 /* IUnknown */
3496 d3d9_device_QueryInterface,
3497 d3d9_device_AddRef,
3498 d3d9_device_Release,
3499 /* IDirect3DDevice9 */
3500 d3d9_device_TestCooperativeLevel,
3501 d3d9_device_GetAvailableTextureMem,
3502 d3d9_device_EvictManagedResources,
3503 d3d9_device_GetDirect3D,
3504 d3d9_device_GetDeviceCaps,
3505 d3d9_device_GetDisplayMode,
3506 d3d9_device_GetCreationParameters,
3507 d3d9_device_SetCursorProperties,
3508 d3d9_device_SetCursorPosition,
3509 d3d9_device_ShowCursor,
3510 d3d9_device_CreateAdditionalSwapChain,
3511 d3d9_device_GetSwapChain,
3512 d3d9_device_GetNumberOfSwapChains,
3513 d3d9_device_Reset,
3514 d3d9_device_Present,
3515 d3d9_device_GetBackBuffer,
3516 d3d9_device_GetRasterStatus,
3517 d3d9_device_SetDialogBoxMode,
3518 d3d9_device_SetGammaRamp,
3519 d3d9_device_GetGammaRamp,
3520 d3d9_device_CreateTexture,
3521 d3d9_device_CreateVolumeTexture,
3522 d3d9_device_CreateCubeTexture,
3523 d3d9_device_CreateVertexBuffer,
3524 d3d9_device_CreateIndexBuffer,
3525 d3d9_device_CreateRenderTarget,
3526 d3d9_device_CreateDepthStencilSurface,
3527 d3d9_device_UpdateSurface,
3528 d3d9_device_UpdateTexture,
3529 d3d9_device_GetRenderTargetData,
3530 d3d9_device_GetFrontBufferData,
3531 d3d9_device_StretchRect,
3532 d3d9_device_ColorFill,
3533 d3d9_device_CreateOffscreenPlainSurface,
3534 d3d9_device_SetRenderTarget,
3535 d3d9_device_GetRenderTarget,
3536 d3d9_device_SetDepthStencilSurface,
3537 d3d9_device_GetDepthStencilSurface,
3538 d3d9_device_BeginScene,
3539 d3d9_device_EndScene,
3540 d3d9_device_Clear,
3541 d3d9_device_SetTransform,
3542 d3d9_device_GetTransform,
3543 d3d9_device_MultiplyTransform,
3544 d3d9_device_SetViewport,
3545 d3d9_device_GetViewport,
3546 d3d9_device_SetMaterial,
3547 d3d9_device_GetMaterial,
3548 d3d9_device_SetLight,
3549 d3d9_device_GetLight,
3550 d3d9_device_LightEnable,
3551 d3d9_device_GetLightEnable,
3552 d3d9_device_SetClipPlane,
3553 d3d9_device_GetClipPlane,
3554 d3d9_device_SetRenderState,
3555 d3d9_device_GetRenderState,
3556 d3d9_device_CreateStateBlock,
3557 d3d9_device_BeginStateBlock,
3558 d3d9_device_EndStateBlock,
3559 d3d9_device_SetClipStatus,
3560 d3d9_device_GetClipStatus,
3561 d3d9_device_GetTexture,
3562 d3d9_device_SetTexture,
3563 d3d9_device_GetTextureStageState,
3564 d3d9_device_SetTextureStageState,
3565 d3d9_device_GetSamplerState,
3566 d3d9_device_SetSamplerState,
3567 d3d9_device_ValidateDevice,
3568 d3d9_device_SetPaletteEntries,
3569 d3d9_device_GetPaletteEntries,
3570 d3d9_device_SetCurrentTexturePalette,
3571 d3d9_device_GetCurrentTexturePalette,
3572 d3d9_device_SetScissorRect,
3573 d3d9_device_GetScissorRect,
3574 d3d9_device_SetSoftwareVertexProcessing,
3575 d3d9_device_GetSoftwareVertexProcessing,
3576 d3d9_device_SetNPatchMode,
3577 d3d9_device_GetNPatchMode,
3578 d3d9_device_DrawPrimitive,
3579 d3d9_device_DrawIndexedPrimitive,
3580 d3d9_device_DrawPrimitiveUP,
3581 d3d9_device_DrawIndexedPrimitiveUP,
3582 d3d9_device_ProcessVertices,
3583 d3d9_device_CreateVertexDeclaration,
3584 d3d9_device_SetVertexDeclaration,
3585 d3d9_device_GetVertexDeclaration,
3586 d3d9_device_SetFVF,
3587 d3d9_device_GetFVF,
3588 d3d9_device_CreateVertexShader,
3589 d3d9_device_SetVertexShader,
3590 d3d9_device_GetVertexShader,
3591 d3d9_device_SetVertexShaderConstantF,
3592 d3d9_device_GetVertexShaderConstantF,
3593 d3d9_device_SetVertexShaderConstantI,
3594 d3d9_device_GetVertexShaderConstantI,
3595 d3d9_device_SetVertexShaderConstantB,
3596 d3d9_device_GetVertexShaderConstantB,
3597 d3d9_device_SetStreamSource,
3598 d3d9_device_GetStreamSource,
3599 d3d9_device_SetStreamSourceFreq,
3600 d3d9_device_GetStreamSourceFreq,
3601 d3d9_device_SetIndices,
3602 d3d9_device_GetIndices,
3603 d3d9_device_CreatePixelShader,
3604 d3d9_device_SetPixelShader,
3605 d3d9_device_GetPixelShader,
3606 d3d9_device_SetPixelShaderConstantF,
3607 d3d9_device_GetPixelShaderConstantF,
3608 d3d9_device_SetPixelShaderConstantI,
3609 d3d9_device_GetPixelShaderConstantI,
3610 d3d9_device_SetPixelShaderConstantB,
3611 d3d9_device_GetPixelShaderConstantB,
3612 d3d9_device_DrawRectPatch,
3613 d3d9_device_DrawTriPatch,
3614 d3d9_device_DeletePatch,
3615 d3d9_device_CreateQuery,
3616 /* IDirect3DDevice9Ex */
3617 d3d9_device_SetConvolutionMonoKernel,
3618 d3d9_device_ComposeRects,
3619 d3d9_device_PresentEx,
3620 d3d9_device_GetGPUThreadPriority,
3621 d3d9_device_SetGPUThreadPriority,
3622 d3d9_device_WaitForVBlank,
3623 d3d9_device_CheckResourceResidency,
3624 d3d9_device_SetMaximumFrameLatency,
3625 d3d9_device_GetMaximumFrameLatency,
3626 d3d9_device_CheckDeviceState,
3627 d3d9_device_CreateRenderTargetEx,
3628 d3d9_device_CreateOffscreenPlainSurfaceEx,
3629 d3d9_device_CreateDepthStencilSurfaceEx,
3630 d3d9_device_ResetEx,
3631 d3d9_device_GetDisplayModeEx,
3634 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3636 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3639 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3640 struct wined3d_device *device)
3642 TRACE("device_parent %p, device %p.\n", device_parent, device);
3645 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3647 TRACE("device_parent %p.\n", device_parent);
3650 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3652 struct d3d9_device *device = device_from_device_parent(device_parent);
3654 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3656 if (!device->d3d_parent)
3657 return;
3659 if (!activate)
3660 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3661 else if (device->d3d_parent->extended)
3662 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3663 else
3664 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3667 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3668 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3669 void **parent, const struct wined3d_parent_ops **parent_ops)
3671 struct d3d9_surface *d3d_surface;
3673 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3674 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3676 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3677 return E_OUTOFMEMORY;
3679 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3680 *parent = d3d_surface;
3681 TRACE("Created surface %p.\n", d3d_surface);
3683 return D3D_OK;
3686 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3687 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3688 void **parent, const struct wined3d_parent_ops **parent_ops)
3690 struct d3d9_volume *d3d_volume;
3692 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3693 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3695 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3696 return E_OUTOFMEMORY;
3698 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3699 *parent = d3d_volume;
3700 TRACE("Created volume %p.\n", d3d_volume);
3702 return D3D_OK;
3705 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3706 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3708 struct d3d9_device *device = device_from_device_parent(device_parent);
3709 struct d3d9_surface *d3d_surface;
3710 HRESULT hr;
3712 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3713 device_parent, container_parent, desc, texture);
3715 if (container_parent == device_parent)
3716 container_parent = &device->IDirect3DDevice9Ex_iface;
3718 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3719 WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
3721 WARN("Failed to create texture, hr %#x.\n", hr);
3722 return hr;
3725 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3726 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3728 return hr;
3731 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3732 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3734 struct d3d9_device *device = device_from_device_parent(device_parent);
3735 struct d3d9_swapchain *d3d_swapchain;
3736 HRESULT hr;
3738 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3740 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3741 if (FAILED(hr))
3743 WARN("Failed to create swapchain, hr %#x.\n", hr);
3744 *swapchain = NULL;
3745 return hr;
3748 *swapchain = d3d_swapchain->wined3d_swapchain;
3749 wined3d_swapchain_incref(*swapchain);
3750 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3752 return hr;
3755 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3757 device_parent_wined3d_device_created,
3758 device_parent_mode_changed,
3759 device_parent_activate,
3760 device_parent_surface_created,
3761 device_parent_volume_created,
3762 device_parent_create_swapchain_texture,
3763 device_parent_create_swapchain,
3766 static void setup_fpu(void)
3768 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3769 WORD cw;
3770 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3771 cw = (cw & ~0xf3f) | 0x3f;
3772 __asm__ volatile ("fldcw %0" : : "m" (cw));
3773 #elif defined(__i386__) && defined(_MSC_VER)
3774 WORD cw;
3775 __asm fnstcw cw;
3776 cw = (cw & ~0xf3f) | 0x3f;
3777 __asm fldcw cw;
3778 #else
3779 FIXME("FPU setup not implemented for this platform.\n");
3780 #endif
3783 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3784 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3785 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3787 struct wined3d_swapchain_desc *swapchain_desc;
3788 UINT i, count = 1;
3789 HRESULT hr;
3791 if (mode)
3792 FIXME("Ignoring display mode.\n");
3794 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3795 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3796 device->refcount = 1;
3798 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3800 wined3d_mutex_lock();
3801 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3802 &device->device_parent, &device->wined3d_device);
3803 if (FAILED(hr))
3805 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3806 wined3d_mutex_unlock();
3807 return hr;
3810 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3812 WINED3DCAPS caps;
3814 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3815 count = caps.NumberOfAdaptersInGroup;
3818 if (flags & D3DCREATE_MULTITHREADED)
3819 wined3d_device_set_multithreaded(device->wined3d_device);
3821 if (!parameters->Windowed)
3823 if (!focus_window)
3824 focus_window = parameters->hDeviceWindow;
3825 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3827 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3828 wined3d_device_decref(device->wined3d_device);
3829 wined3d_mutex_unlock();
3830 return hr;
3833 for (i = 0; i < count; ++i)
3835 HWND device_window = parameters[i].hDeviceWindow;
3837 if (!device_window) device_window = focus_window;
3838 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3839 parameters[i].BackBufferWidth,
3840 parameters[i].BackBufferHeight);
3844 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3845 if (!swapchain_desc)
3847 ERR("Failed to allocate wined3d parameters.\n");
3848 wined3d_device_release_focus_window(device->wined3d_device);
3849 wined3d_device_decref(device->wined3d_device);
3850 wined3d_mutex_unlock();
3851 return E_OUTOFMEMORY;
3854 for (i = 0; i < count; ++i)
3856 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
3857 parent->extended))
3859 wined3d_device_release_focus_window(device->wined3d_device);
3860 wined3d_device_decref(device->wined3d_device);
3861 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3862 wined3d_mutex_unlock();
3863 return D3DERR_INVALIDCALL;
3867 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3868 if (FAILED(hr))
3870 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3871 wined3d_device_release_focus_window(device->wined3d_device);
3872 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3873 wined3d_device_decref(device->wined3d_device);
3874 wined3d_mutex_unlock();
3875 return hr;
3878 if (FAILED(hr = d3d9_device_get_swapchains(device)))
3880 wined3d_device_uninit_3d(device->wined3d_device);
3881 wined3d_device_release_focus_window(device->wined3d_device);
3882 wined3d_device_decref(device->wined3d_device);
3883 wined3d_mutex_unlock();
3884 return E_OUTOFMEMORY;
3887 for (i = 0; i < count; ++i)
3889 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3892 wined3d_mutex_unlock();
3894 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3896 /* Initialize the converted declaration array. This creates a valid pointer
3897 * and when adding decls HeapReAlloc() can be used without further checking. */
3898 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3899 if (!device->fvf_decls)
3901 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3902 wined3d_mutex_lock();
3903 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
3904 wined3d_device_uninit_3d(device->wined3d_device);
3905 wined3d_device_release_focus_window(device->wined3d_device);
3906 wined3d_device_decref(device->wined3d_device);
3907 wined3d_mutex_unlock();
3908 return E_OUTOFMEMORY;
3911 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3912 device->d3d_parent = parent;
3914 return D3D_OK;