gdi32: Don't overflow the buffer in GetGlyphOutline.
[wine/multimedia.git] / dlls / d3d9 / device.c
blobca902a238fddd3bad7ea52e15933a3cb0d790f27
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 void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
212 const D3DPRESENT_PARAMETERS *present_parameters)
214 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
215 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
216 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
217 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
218 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
219 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
220 swapchain_desc->swap_effect = present_parameters->SwapEffect;
221 swapchain_desc->device_window = present_parameters->hDeviceWindow;
222 swapchain_desc->windowed = present_parameters->Windowed;
223 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
224 swapchain_desc->auto_depth_stencil_format
225 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
226 swapchain_desc->flags = present_parameters->Flags;
227 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
228 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
229 swapchain_desc->auto_restore_display_mode = TRUE;
232 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
234 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
236 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
237 || IsEqualGUID(riid, &IID_IUnknown))
239 IDirect3DDevice9Ex_AddRef(iface);
240 *out = iface;
241 return S_OK;
244 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
246 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
248 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
249 * It doesn't matter with which function the device was created. */
250 if (!device->d3d_parent->extended)
252 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
253 *out = NULL;
254 return E_NOINTERFACE;
257 IDirect3DDevice9Ex_AddRef(iface);
258 *out = iface;
259 return S_OK;
262 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
264 *out = NULL;
265 return E_NOINTERFACE;
268 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
271 ULONG refcount = InterlockedIncrement(&device->refcount);
273 TRACE("%p increasing refcount to %u.\n", iface, refcount);
275 return refcount;
278 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
280 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
281 ULONG refcount;
283 if (device->in_destruction)
284 return 0;
286 refcount = InterlockedDecrement(&device->refcount);
288 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
290 if (!refcount)
292 unsigned i;
293 device->in_destruction = TRUE;
295 wined3d_mutex_lock();
296 for (i = 0; i < device->fvf_decl_count; ++i)
298 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
300 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
302 if (device->vertex_buffer)
303 wined3d_buffer_decref(device->vertex_buffer);
304 if (device->index_buffer)
305 wined3d_buffer_decref(device->index_buffer);
307 wined3d_device_uninit_3d(device->wined3d_device);
308 wined3d_device_release_focus_window(device->wined3d_device);
309 wined3d_device_decref(device->wined3d_device);
310 wined3d_mutex_unlock();
312 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
314 HeapFree(GetProcessHeap(), 0, device);
317 return refcount;
320 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
322 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
324 TRACE("iface %p.\n", iface);
326 TRACE("device state: %#x.\n", device->device_state);
328 if (device->d3d_parent->extended)
329 return D3D_OK;
331 switch (device->device_state)
333 default:
334 case D3D9_DEVICE_STATE_OK:
335 return D3D_OK;
336 case D3D9_DEVICE_STATE_LOST:
337 return D3DERR_DEVICELOST;
338 case D3D9_DEVICE_STATE_NOT_RESET:
339 return D3DERR_DEVICENOTRESET;
343 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
345 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
346 UINT ret;
348 TRACE("iface %p.\n", iface);
350 wined3d_mutex_lock();
351 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
352 wined3d_mutex_unlock();
354 return ret;
357 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
359 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
361 TRACE("iface %p.\n", iface);
363 wined3d_mutex_lock();
364 wined3d_device_evict_managed_resources(device->wined3d_device);
365 wined3d_mutex_unlock();
367 return D3D_OK;
370 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
372 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
374 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
376 if (!d3d9)
377 return D3DERR_INVALIDCALL;
379 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
382 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
384 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
385 WINED3DCAPS *wined3d_caps;
386 HRESULT hr;
388 TRACE("iface %p, caps %p.\n", iface, caps);
390 if (!caps)
391 return D3DERR_INVALIDCALL;
393 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
394 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
396 memset(caps, 0, sizeof(*caps));
398 wined3d_mutex_lock();
399 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
400 wined3d_mutex_unlock();
402 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
403 HeapFree(GetProcessHeap(), 0, wined3d_caps);
405 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
406 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
408 filter_caps(caps);
410 return hr;
413 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
415 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
416 struct wined3d_display_mode wined3d_mode;
417 HRESULT hr;
419 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
421 wined3d_mutex_lock();
422 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
423 wined3d_mutex_unlock();
425 if (SUCCEEDED(hr))
427 mode->Width = wined3d_mode.width;
428 mode->Height = wined3d_mode.height;
429 mode->RefreshRate = wined3d_mode.refresh_rate;
430 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
433 return hr;
436 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
437 D3DDEVICE_CREATION_PARAMETERS *parameters)
439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
441 TRACE("iface %p, parameters %p.\n", iface, parameters);
443 wined3d_mutex_lock();
444 wined3d_device_get_creation_parameters(device->wined3d_device,
445 (struct wined3d_device_creation_parameters *)parameters);
446 wined3d_mutex_unlock();
448 return D3D_OK;
451 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
452 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
454 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
455 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
456 HRESULT hr;
458 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
459 iface, hotspot_x, hotspot_y, bitmap);
461 if (!bitmap)
463 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
464 return D3DERR_INVALIDCALL;
467 wined3d_mutex_lock();
468 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
469 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
470 wined3d_mutex_unlock();
472 return hr;
475 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
477 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
479 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
481 wined3d_mutex_lock();
482 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
483 wined3d_mutex_unlock();
486 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
488 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
489 BOOL ret;
491 TRACE("iface %p, show %#x.\n", iface, show);
493 wined3d_mutex_lock();
494 ret = wined3d_device_show_cursor(device->wined3d_device, show);
495 wined3d_mutex_unlock();
497 return ret;
500 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
501 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
503 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
504 struct wined3d_swapchain_desc desc;
505 struct d3d9_swapchain *object;
506 HRESULT hr;
508 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
509 iface, present_parameters, swapchain);
511 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
512 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
513 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
514 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
516 return hr;
519 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
520 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
522 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
523 struct wined3d_swapchain *wined3d_swapchain;
524 struct d3d9_swapchain *swapchain_impl;
525 HRESULT hr;
527 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
529 wined3d_mutex_lock();
530 if ((wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx)))
532 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
533 *swapchain = (IDirect3DSwapChain9 *)&swapchain_impl->IDirect3DSwapChain9Ex_iface;
534 IDirect3DSwapChain9Ex_AddRef(*swapchain);
535 hr = D3D_OK;
537 else
539 *swapchain = NULL;
540 hr = D3DERR_INVALIDCALL;
542 wined3d_mutex_unlock();
544 return hr;
547 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
549 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
550 UINT count;
552 TRACE("iface %p.\n", iface);
554 wined3d_mutex_lock();
555 count = wined3d_device_get_swapchain_count(device->wined3d_device);
556 wined3d_mutex_unlock();
558 return count;
561 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
563 struct wined3d_resource_desc desc;
565 wined3d_resource_get_desc(resource, &desc);
566 if (desc.pool == WINED3D_POOL_DEFAULT)
568 struct d3d9_surface *surface;
570 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
572 IUnknown *parent = wined3d_resource_get_parent(resource);
573 IDirect3DBaseTexture9 *texture;
575 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
577 IDirect3DBaseTexture9_Release(texture);
578 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
579 return D3DERR_INVALIDCALL;
582 return D3D_OK;
585 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
587 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
588 return D3DERR_INVALIDCALL;
591 surface = wined3d_resource_get_parent(resource);
592 if (surface->resource.refcount)
594 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
595 return D3DERR_INVALIDCALL;
598 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
601 return D3D_OK;
604 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
605 D3DPRESENT_PARAMETERS *present_parameters)
607 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
608 struct wined3d_swapchain_desc swapchain_desc;
609 HRESULT hr;
611 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
613 wined3d_mutex_lock();
615 if (device->vertex_buffer)
617 wined3d_buffer_decref(device->vertex_buffer);
618 device->vertex_buffer = NULL;
619 device->vertex_buffer_size = 0;
621 if (device->index_buffer)
623 wined3d_buffer_decref(device->index_buffer);
624 device->index_buffer = NULL;
625 device->index_buffer_size = 0;
628 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
629 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
630 NULL, reset_enum_callback, !device->d3d_parent->extended);
631 if (FAILED(hr) && !device->d3d_parent->extended)
632 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
633 else
634 device->device_state = D3D9_DEVICE_STATE_OK;
635 wined3d_mutex_unlock();
637 return hr;
640 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
641 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
643 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
644 HRESULT hr;
646 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
647 iface, src_rect, dst_rect, dst_window_override, dirty_region);
649 if (device->device_state != D3D9_DEVICE_STATE_OK)
650 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
652 wined3d_mutex_lock();
653 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
654 dst_window_override, dirty_region, 0);
655 wined3d_mutex_unlock();
657 return hr;
660 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
661 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
663 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
664 struct wined3d_surface *wined3d_surface = NULL;
665 struct d3d9_surface *surface_impl;
666 HRESULT hr;
668 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
669 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
671 wined3d_mutex_lock();
672 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
673 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
674 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
676 surface_impl = wined3d_surface_get_parent(wined3d_surface);
677 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
678 IDirect3DSurface9_AddRef(*backbuffer);
680 wined3d_mutex_unlock();
682 return hr;
684 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
685 UINT swapchain, D3DRASTER_STATUS *raster_status)
687 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
688 HRESULT hr;
690 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
692 wined3d_mutex_lock();
693 hr = wined3d_device_get_raster_status(device->wined3d_device,
694 swapchain, (struct wined3d_raster_status *)raster_status);
695 wined3d_mutex_unlock();
697 return hr;
700 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
702 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
703 HRESULT hr;
705 TRACE("iface %p, enable %#x.\n", iface, enable);
707 wined3d_mutex_lock();
708 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
709 wined3d_mutex_unlock();
711 return hr;
714 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
715 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
717 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
719 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
721 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
722 wined3d_mutex_lock();
723 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
724 wined3d_mutex_unlock();
727 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
729 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
731 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
733 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
734 wined3d_mutex_lock();
735 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
736 wined3d_mutex_unlock();
739 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
740 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
741 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
744 struct d3d9_texture *object;
745 BOOL set_mem = FALSE;
746 HRESULT hr;
748 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
749 iface, width, height, levels, usage, format, pool, texture, shared_handle);
751 *texture = NULL;
752 if (shared_handle)
754 if (!device->d3d_parent->extended)
756 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
757 return E_NOTIMPL;
760 if (pool == D3DPOOL_SYSTEMMEM)
762 if (levels != 1)
763 return D3DERR_INVALIDCALL;
764 set_mem = TRUE;
766 else
768 if (pool != D3DPOOL_DEFAULT)
770 WARN("Trying to create a shared texture in pool %#x.\n", pool);
771 return D3DERR_INVALIDCALL;
773 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
777 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
778 if (!object)
779 return D3DERR_OUTOFVIDEOMEMORY;
781 hr = texture_init(object, device, width, height, levels, usage, format, pool);
782 if (FAILED(hr))
784 WARN("Failed to initialize texture, hr %#x.\n", hr);
785 HeapFree(GetProcessHeap(), 0, object);
786 return hr;
789 if (set_mem)
790 wined3d_texture_update_desc(object->wined3d_texture, width, height,
791 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
792 *shared_handle, 0);
794 TRACE("Created texture %p.\n", object);
795 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
797 return D3D_OK;
800 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
801 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
802 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
804 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
805 struct d3d9_texture *object;
806 HRESULT hr;
808 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
809 iface, width, height, depth, levels);
810 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
811 usage, format, pool, texture, shared_handle);
813 *texture = NULL;
814 if (shared_handle)
816 if (!device->d3d_parent->extended)
818 WARN("Trying to create a shared volume texture on a non-ex device.\n");
819 return E_NOTIMPL;
822 if (pool != D3DPOOL_DEFAULT)
824 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
825 return D3DERR_INVALIDCALL;
827 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
830 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
831 if (!object)
832 return D3DERR_OUTOFVIDEOMEMORY;
834 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
835 if (FAILED(hr))
837 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
838 HeapFree(GetProcessHeap(), 0, object);
839 return hr;
842 TRACE("Created volume texture %p.\n", object);
843 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
845 return D3D_OK;
848 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
849 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
850 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
852 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
853 struct d3d9_texture *object;
854 HRESULT hr;
856 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
857 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
859 *texture = NULL;
860 if (shared_handle)
862 if (!device->d3d_parent->extended)
864 WARN("Trying to create a shared cube texture on a non-ex device.\n");
865 return E_NOTIMPL;
868 if (pool != D3DPOOL_DEFAULT)
870 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
871 return D3DERR_INVALIDCALL;
873 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
876 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
877 if (!object)
878 return D3DERR_OUTOFVIDEOMEMORY;
880 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
881 if (FAILED(hr))
883 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
884 HeapFree(GetProcessHeap(), 0, object);
885 return hr;
888 TRACE("Created cube texture %p.\n", object);
889 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
891 return D3D_OK;
894 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
895 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
896 HANDLE *shared_handle)
898 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
899 struct d3d9_vertexbuffer *object;
900 HRESULT hr;
902 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
903 iface, size, usage, fvf, pool, buffer, shared_handle);
905 if (shared_handle)
907 if (!device->d3d_parent->extended)
909 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
910 return E_NOTIMPL;
913 if (pool != D3DPOOL_DEFAULT)
915 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
916 return D3DERR_NOTAVAILABLE;
918 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
921 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
922 if (!object)
923 return D3DERR_OUTOFVIDEOMEMORY;
925 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
926 if (FAILED(hr))
928 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
929 HeapFree(GetProcessHeap(), 0, object);
930 return hr;
933 TRACE("Created vertex buffer %p.\n", object);
934 *buffer = &object->IDirect3DVertexBuffer9_iface;
936 return D3D_OK;
939 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
940 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
941 HANDLE *shared_handle)
943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
944 struct d3d9_indexbuffer *object;
945 HRESULT hr;
947 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
948 iface, size, usage, format, pool, buffer, shared_handle);
950 if (shared_handle)
952 if (!device->d3d_parent->extended)
954 WARN("Trying to create a shared index buffer on a non-ex device.\n");
955 return E_NOTIMPL;
958 if (pool != D3DPOOL_DEFAULT)
960 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
961 return D3DERR_NOTAVAILABLE;
963 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
966 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
967 if (!object)
968 return D3DERR_OUTOFVIDEOMEMORY;
970 hr = indexbuffer_init(object, device, size, usage, format, pool);
971 if (FAILED(hr))
973 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
974 HeapFree(GetProcessHeap(), 0, object);
975 return hr;
978 TRACE("Created index buffer %p.\n", object);
979 *buffer = &object->IDirect3DIndexBuffer9_iface;
981 return D3D_OK;
984 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
985 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
986 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
988 struct wined3d_resource *sub_resource;
989 struct wined3d_resource_desc desc;
990 struct d3d9_surface *surface_impl;
991 struct wined3d_texture *texture;
992 HRESULT hr;
994 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
995 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
996 device, width, height, format, flags, surface, usage, pool,
997 multisample_type, multisample_quality);
999 desc.resource_type = WINED3D_RTYPE_TEXTURE;
1000 desc.format = wined3dformat_from_d3dformat(format);
1001 desc.multisample_type = multisample_type;
1002 desc.multisample_quality = multisample_quality;
1003 desc.usage = usage & WINED3DUSAGE_MASK;
1004 desc.pool = pool;
1005 desc.width = width;
1006 desc.height = height;
1007 desc.depth = 1;
1008 desc.size = 0;
1010 wined3d_mutex_lock();
1012 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1013 1, flags, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1015 wined3d_mutex_unlock();
1016 WARN("Failed to create texture, hr %#x.\n", hr);
1017 return hr;
1020 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1021 surface_impl = wined3d_resource_get_parent(sub_resource);
1022 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1023 *surface = &surface_impl->IDirect3DSurface9_iface;
1024 IDirect3DSurface9_AddRef(*surface);
1026 if (user_mem)
1027 wined3d_texture_update_desc(texture, width, height,
1028 desc.format, multisample_type, multisample_quality, user_mem, 0);
1030 wined3d_texture_decref(texture);
1032 wined3d_mutex_unlock();
1034 return D3D_OK;
1037 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1038 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1039 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1041 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1042 DWORD flags = 0;
1044 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1045 "lockable %#x, surface %p, shared_handle %p.\n",
1046 iface, width, height, format, multisample_type, multisample_quality,
1047 lockable, surface, shared_handle);
1049 *surface = NULL;
1050 if (shared_handle)
1052 if (!device->d3d_parent->extended)
1054 WARN("Trying to create a shared render target on a non-ex device.\n");
1055 return E_NOTIMPL;
1058 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1061 if (lockable)
1062 flags |= WINED3D_SURFACE_MAPPABLE;
1064 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1065 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1068 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1069 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1070 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1072 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1073 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1075 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1076 "discard %#x, surface %p, shared_handle %p.\n",
1077 iface, width, height, format, multisample_type, multisample_quality,
1078 discard, surface, shared_handle);
1080 *surface = NULL;
1081 if (shared_handle)
1083 if (!device->d3d_parent->extended)
1085 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1086 return E_NOTIMPL;
1089 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1092 if (discard)
1093 flags |= WINED3D_SURFACE_DISCARD;
1095 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1096 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1100 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1101 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1102 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1104 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1105 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1106 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1107 HRESULT hr;
1109 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1110 iface, src_surface, src_rect, dst_surface, dst_point);
1112 wined3d_mutex_lock();
1113 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1114 dst->wined3d_surface, dst_point);
1115 wined3d_mutex_unlock();
1117 return hr;
1120 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1121 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1123 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1124 struct d3d9_texture *src_impl, *dst_impl;
1125 HRESULT hr;
1127 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1129 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1130 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1132 wined3d_mutex_lock();
1133 hr = wined3d_device_update_texture(device->wined3d_device,
1134 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1135 wined3d_mutex_unlock();
1137 return hr;
1140 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1141 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1143 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1144 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1145 HRESULT hr;
1147 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1149 wined3d_mutex_lock();
1150 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1151 wined3d_mutex_unlock();
1153 return hr;
1156 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1157 UINT swapchain, IDirect3DSurface9 *dst_surface)
1159 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1160 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1161 HRESULT hr;
1163 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1165 wined3d_mutex_lock();
1166 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1167 wined3d_mutex_unlock();
1169 return hr;
1172 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1173 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1175 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1176 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1177 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1178 HRESULT hr = D3DERR_INVALIDCALL;
1179 struct wined3d_resource_desc src_desc, dst_desc;
1180 struct wined3d_resource *wined3d_resource;
1182 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1183 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1185 wined3d_mutex_lock();
1186 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1187 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1189 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1190 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1192 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1194 if (device->in_scene)
1196 WARN("Rejecting depth / stencil blit while in scene.\n");
1197 goto done;
1200 if (src_rect)
1202 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1203 || src_rect->bottom != src_desc.height)
1205 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1206 wine_dbgstr_rect(src_rect));
1207 goto done;
1210 if (dst_rect)
1212 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1213 || dst_rect->bottom != dst_desc.height)
1215 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1216 wine_dbgstr_rect(dst_rect));
1217 goto done;
1220 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1222 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1223 goto done;
1227 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1228 if (hr == WINEDDERR_INVALIDRECT)
1229 hr = D3DERR_INVALIDCALL;
1231 done:
1232 wined3d_mutex_unlock();
1233 return hr;
1236 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1237 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1239 const struct wined3d_color c =
1241 ((color >> 16) & 0xff) / 255.0f,
1242 ((color >> 8) & 0xff) / 255.0f,
1243 (color & 0xff) / 255.0f,
1244 ((color >> 24) & 0xff) / 255.0f,
1246 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1247 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1248 struct wined3d_resource *wined3d_resource;
1249 struct wined3d_resource_desc desc;
1250 HRESULT hr;
1252 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1254 wined3d_mutex_lock();
1256 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1257 wined3d_resource_get_desc(wined3d_resource, &desc);
1259 /* This method is only allowed with surfaces that are render targets, or
1260 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1261 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1263 wined3d_mutex_unlock();
1264 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1265 return D3DERR_INVALIDCALL;
1268 if (desc.pool != WINED3D_POOL_DEFAULT && desc.pool != WINED3D_POOL_SYSTEM_MEM)
1270 WARN("Color-fill not allowed on surfaces in pool %#x.\n", desc.pool);
1271 return D3DERR_INVALIDCALL;
1274 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1275 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1277 wined3d_mutex_unlock();
1279 return hr;
1282 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1283 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1284 HANDLE *shared_handle)
1286 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1287 void *user_mem = NULL;
1289 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1290 iface, width, height, format, pool, surface, shared_handle);
1292 *surface = NULL;
1293 if (pool == D3DPOOL_MANAGED)
1295 WARN("Attempting to create a managed offscreen plain surface.\n");
1296 return D3DERR_INVALIDCALL;
1299 if (shared_handle)
1301 if (!device->d3d_parent->extended)
1303 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1304 return E_NOTIMPL;
1307 if (pool == D3DPOOL_SYSTEMMEM)
1308 user_mem = *shared_handle;
1309 else
1311 if (pool != D3DPOOL_DEFAULT)
1313 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1314 return D3DERR_INVALIDCALL;
1316 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1320 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1321 * regardless of the pool they're created in. Should we set dynamic usage
1322 * here? */
1323 return d3d9_device_create_surface(device, width, height, format,
1324 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1327 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1329 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1330 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1331 HRESULT hr;
1333 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1335 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1337 WARN("Invalid index %u specified.\n", idx);
1338 return D3DERR_INVALIDCALL;
1341 if (!idx && !surface_impl)
1343 WARN("Trying to set render target 0 to NULL.\n");
1344 return D3DERR_INVALIDCALL;
1347 wined3d_mutex_lock();
1348 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1349 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1350 wined3d_mutex_unlock();
1352 return hr;
1355 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1357 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1358 struct wined3d_rendertarget_view *wined3d_rtv;
1359 struct d3d9_surface *surface_impl;
1360 HRESULT hr = D3D_OK;
1362 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1364 if (!surface)
1365 return D3DERR_INVALIDCALL;
1367 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1369 WARN("Invalid index %u specified.\n", idx);
1370 return D3DERR_INVALIDCALL;
1373 wined3d_mutex_lock();
1374 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1376 /* We want the sub resource parent here, since the view itself may be
1377 * internal to wined3d and may not have a parent. */
1378 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1379 *surface = &surface_impl->IDirect3DSurface9_iface;
1380 IDirect3DSurface9_AddRef(*surface);
1382 else
1384 hr = WINED3DERR_NOTFOUND;
1385 *surface = NULL;
1387 wined3d_mutex_unlock();
1389 return hr;
1392 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1394 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1395 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1397 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1399 wined3d_mutex_lock();
1400 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1401 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1402 wined3d_mutex_unlock();
1404 return D3D_OK;
1407 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1409 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1410 struct wined3d_rendertarget_view *wined3d_dsv;
1411 struct d3d9_surface *surface_impl;
1412 HRESULT hr = D3D_OK;
1414 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1416 if (!depth_stencil)
1417 return D3DERR_INVALIDCALL;
1419 wined3d_mutex_lock();
1420 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1422 /* We want the sub resource parent here, since the view itself may be
1423 * internal to wined3d and may not have a parent. */
1424 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1425 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1426 IDirect3DSurface9_AddRef(*depth_stencil);
1428 else
1430 hr = WINED3DERR_NOTFOUND;
1431 *depth_stencil = NULL;
1433 wined3d_mutex_unlock();
1435 return hr;
1438 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1440 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1441 HRESULT hr;
1443 TRACE("iface %p.\n", iface);
1445 wined3d_mutex_lock();
1446 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1447 device->in_scene = TRUE;
1448 wined3d_mutex_unlock();
1450 return hr;
1453 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1455 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1456 HRESULT hr;
1458 TRACE("iface %p.\n", iface);
1460 wined3d_mutex_lock();
1461 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1462 device->in_scene = FALSE;
1463 wined3d_mutex_unlock();
1465 return hr;
1468 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1469 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1471 const struct wined3d_color c =
1473 ((color >> 16) & 0xff) / 255.0f,
1474 ((color >> 8) & 0xff) / 255.0f,
1475 (color & 0xff) / 255.0f,
1476 ((color >> 24) & 0xff) / 255.0f,
1478 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1479 HRESULT hr;
1481 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1482 iface, rect_count, rects, flags, color, z, stencil);
1484 wined3d_mutex_lock();
1485 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1486 wined3d_mutex_unlock();
1488 return hr;
1491 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1492 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1494 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1496 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1498 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1499 wined3d_mutex_lock();
1500 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1501 wined3d_mutex_unlock();
1503 return D3D_OK;
1506 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1507 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1509 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1511 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1513 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1514 wined3d_mutex_lock();
1515 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1516 wined3d_mutex_unlock();
1518 return D3D_OK;
1521 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1522 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1524 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1526 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1528 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1529 wined3d_mutex_lock();
1530 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1531 wined3d_mutex_unlock();
1533 return D3D_OK;
1536 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1538 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1540 TRACE("iface %p, viewport %p.\n", iface, viewport);
1542 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1543 wined3d_mutex_lock();
1544 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1545 wined3d_mutex_unlock();
1547 return D3D_OK;
1550 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1552 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1554 TRACE("iface %p, viewport %p.\n", iface, viewport);
1556 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1557 wined3d_mutex_lock();
1558 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1559 wined3d_mutex_unlock();
1561 return D3D_OK;
1564 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1566 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1568 TRACE("iface %p, material %p.\n", iface, material);
1570 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1571 wined3d_mutex_lock();
1572 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1573 wined3d_mutex_unlock();
1575 return D3D_OK;
1578 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1580 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1582 TRACE("iface %p, material %p.\n", iface, material);
1584 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1585 wined3d_mutex_lock();
1586 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1587 wined3d_mutex_unlock();
1589 return D3D_OK;
1592 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1594 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1595 HRESULT hr;
1597 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1599 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1600 wined3d_mutex_lock();
1601 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1602 wined3d_mutex_unlock();
1604 return hr;
1607 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1609 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1610 HRESULT hr;
1612 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1614 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1615 wined3d_mutex_lock();
1616 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1617 wined3d_mutex_unlock();
1619 return hr;
1622 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1624 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1625 HRESULT hr;
1627 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1629 wined3d_mutex_lock();
1630 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1631 wined3d_mutex_unlock();
1633 return hr;
1636 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1638 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1639 HRESULT hr;
1641 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1643 wined3d_mutex_lock();
1644 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1645 wined3d_mutex_unlock();
1647 return hr;
1650 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1653 HRESULT hr;
1655 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1657 wined3d_mutex_lock();
1658 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1659 wined3d_mutex_unlock();
1661 return hr;
1664 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1666 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1667 HRESULT hr;
1669 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1671 wined3d_mutex_lock();
1672 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1673 wined3d_mutex_unlock();
1675 return hr;
1678 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1679 D3DRENDERSTATETYPE state, DWORD value)
1681 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1683 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1685 wined3d_mutex_lock();
1686 wined3d_device_set_render_state(device->wined3d_device, state, value);
1687 wined3d_mutex_unlock();
1689 return D3D_OK;
1692 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1693 D3DRENDERSTATETYPE state, DWORD *value)
1695 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1697 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1699 wined3d_mutex_lock();
1700 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1701 wined3d_mutex_unlock();
1703 return D3D_OK;
1706 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1707 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1709 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1710 struct d3d9_stateblock *object;
1711 HRESULT hr;
1713 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1715 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1717 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1718 return D3DERR_INVALIDCALL;
1721 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1722 if (!object)
1723 return E_OUTOFMEMORY;
1725 hr = stateblock_init(object, device, type, NULL);
1726 if (FAILED(hr))
1728 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1729 HeapFree(GetProcessHeap(), 0, object);
1730 return hr;
1733 TRACE("Created stateblock %p.\n", object);
1734 *stateblock = &object->IDirect3DStateBlock9_iface;
1736 return D3D_OK;
1739 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1741 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1742 HRESULT hr;
1744 TRACE("iface %p.\n", iface);
1746 wined3d_mutex_lock();
1747 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1748 wined3d_mutex_unlock();
1750 return hr;
1753 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1755 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1756 struct wined3d_stateblock *wined3d_stateblock;
1757 struct d3d9_stateblock *object;
1758 HRESULT hr;
1760 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1762 wined3d_mutex_lock();
1763 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1764 wined3d_mutex_unlock();
1765 if (FAILED(hr))
1767 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1768 return hr;
1771 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1772 if (!object)
1774 wined3d_mutex_lock();
1775 wined3d_stateblock_decref(wined3d_stateblock);
1776 wined3d_mutex_unlock();
1777 return E_OUTOFMEMORY;
1780 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1781 if (FAILED(hr))
1783 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1784 wined3d_mutex_lock();
1785 wined3d_stateblock_decref(wined3d_stateblock);
1786 wined3d_mutex_unlock();
1787 HeapFree(GetProcessHeap(), 0, object);
1788 return hr;
1791 TRACE("Created stateblock %p.\n", object);
1792 *stateblock = &object->IDirect3DStateBlock9_iface;
1794 return D3D_OK;
1797 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1799 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1800 HRESULT hr;
1802 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1804 wined3d_mutex_lock();
1805 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1806 wined3d_mutex_unlock();
1808 return hr;
1811 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1813 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1814 HRESULT hr;
1816 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1818 wined3d_mutex_lock();
1819 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1820 wined3d_mutex_unlock();
1822 return hr;
1825 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1827 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1828 struct wined3d_texture *wined3d_texture = NULL;
1829 struct d3d9_texture *texture_impl;
1831 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1833 if (!texture)
1834 return D3DERR_INVALIDCALL;
1836 wined3d_mutex_lock();
1837 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1839 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1840 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1841 IDirect3DBaseTexture9_AddRef(*texture);
1843 else
1845 *texture = NULL;
1847 wined3d_mutex_unlock();
1849 return D3D_OK;
1852 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1854 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1855 struct d3d9_texture *texture_impl;
1856 HRESULT hr;
1858 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1860 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1862 wined3d_mutex_lock();
1863 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1864 texture_impl ? texture_impl->wined3d_texture : NULL);
1865 wined3d_mutex_unlock();
1867 return hr;
1870 static const enum wined3d_texture_stage_state tss_lookup[] =
1872 WINED3D_TSS_INVALID, /* 0, unused */
1873 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1874 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1875 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1876 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1877 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1878 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1879 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1880 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1881 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1882 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1883 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1884 WINED3D_TSS_INVALID, /* 12, unused */
1885 WINED3D_TSS_INVALID, /* 13, unused */
1886 WINED3D_TSS_INVALID, /* 14, unused */
1887 WINED3D_TSS_INVALID, /* 15, unused */
1888 WINED3D_TSS_INVALID, /* 16, unused */
1889 WINED3D_TSS_INVALID, /* 17, unused */
1890 WINED3D_TSS_INVALID, /* 18, unused */
1891 WINED3D_TSS_INVALID, /* 19, unused */
1892 WINED3D_TSS_INVALID, /* 20, unused */
1893 WINED3D_TSS_INVALID, /* 21, unused */
1894 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1895 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1896 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1897 WINED3D_TSS_INVALID, /* 25, unused */
1898 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1899 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1900 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1901 WINED3D_TSS_INVALID, /* 29, unused */
1902 WINED3D_TSS_INVALID, /* 30, unused */
1903 WINED3D_TSS_INVALID, /* 31, unused */
1904 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1907 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1908 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1910 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1912 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1914 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1916 WARN("Invalid state %#x passed.\n", state);
1917 return D3D_OK;
1920 wined3d_mutex_lock();
1921 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
1922 wined3d_mutex_unlock();
1924 return D3D_OK;
1927 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1928 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1930 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1932 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
1934 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1936 WARN("Invalid state %#x passed.\n", state);
1937 return D3D_OK;
1940 wined3d_mutex_lock();
1941 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1942 wined3d_mutex_unlock();
1944 return D3D_OK;
1947 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
1948 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
1950 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1952 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
1954 wined3d_mutex_lock();
1955 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
1956 wined3d_mutex_unlock();
1958 return D3D_OK;
1961 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
1962 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
1964 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1966 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
1968 wined3d_mutex_lock();
1969 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
1970 wined3d_mutex_unlock();
1972 return D3D_OK;
1975 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
1977 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1978 HRESULT hr;
1980 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1982 wined3d_mutex_lock();
1983 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1984 wined3d_mutex_unlock();
1986 return hr;
1989 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1990 UINT palette_idx, const PALETTEENTRY *entries)
1992 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1994 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
1995 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
1996 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
1998 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
1999 return D3D_OK;
2002 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2003 UINT palette_idx, PALETTEENTRY *entries)
2005 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2007 return D3DERR_INVALIDCALL;
2010 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2012 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2014 return D3D_OK;
2017 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2019 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2021 return D3DERR_INVALIDCALL;
2024 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2026 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2028 TRACE("iface %p, rect %p.\n", iface, rect);
2030 wined3d_mutex_lock();
2031 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2032 wined3d_mutex_unlock();
2034 return D3D_OK;
2037 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2039 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2041 TRACE("iface %p, rect %p.\n", iface, rect);
2043 wined3d_mutex_lock();
2044 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2045 wined3d_mutex_unlock();
2047 return D3D_OK;
2050 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2052 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2054 TRACE("iface %p, software %#x.\n", iface, software);
2056 wined3d_mutex_lock();
2057 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2058 wined3d_mutex_unlock();
2060 return D3D_OK;
2063 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2065 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2066 BOOL ret;
2068 TRACE("iface %p.\n", iface);
2070 wined3d_mutex_lock();
2071 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2072 wined3d_mutex_unlock();
2074 return ret;
2077 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2079 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2080 HRESULT hr;
2082 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2084 wined3d_mutex_lock();
2085 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2086 wined3d_mutex_unlock();
2088 return hr;
2091 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2093 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2094 float ret;
2096 TRACE("iface %p.\n", iface);
2098 wined3d_mutex_lock();
2099 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2100 wined3d_mutex_unlock();
2102 return ret;
2105 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2106 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2108 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2109 HRESULT hr;
2111 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2112 iface, primitive_type, start_vertex, primitive_count);
2114 wined3d_mutex_lock();
2115 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2116 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2117 vertex_count_from_primitive_count(primitive_type, primitive_count));
2118 wined3d_mutex_unlock();
2120 return hr;
2123 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2124 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2125 UINT vertex_count, UINT start_idx, UINT primitive_count)
2127 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2128 HRESULT hr;
2130 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2131 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2132 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2133 vertex_count, start_idx, primitive_count);
2135 wined3d_mutex_lock();
2136 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2137 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2138 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2139 vertex_count_from_primitive_count(primitive_type, primitive_count));
2140 wined3d_mutex_unlock();
2142 return hr;
2145 /* The caller is responsible for wined3d locking */
2146 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2148 HRESULT hr;
2150 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2152 UINT size = max(device->vertex_buffer_size * 2, min_size);
2153 struct wined3d_buffer *buffer;
2155 TRACE("Growing vertex buffer to %u bytes\n", size);
2157 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2158 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2159 if (FAILED(hr))
2161 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2162 return hr;
2165 if (device->vertex_buffer)
2166 wined3d_buffer_decref(device->vertex_buffer);
2168 device->vertex_buffer = buffer;
2169 device->vertex_buffer_size = size;
2170 device->vertex_buffer_pos = 0;
2172 return D3D_OK;
2175 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2176 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2178 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2179 HRESULT hr;
2180 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2181 UINT size = vtx_count * stride;
2182 UINT vb_pos, align;
2183 BYTE *buffer_data;
2185 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2186 iface, primitive_type, primitive_count, data, stride);
2188 if (!primitive_count)
2190 WARN("primitive_count is 0, returning D3D_OK\n");
2191 return D3D_OK;
2194 wined3d_mutex_lock();
2196 hr = d3d9_device_prepare_vertex_buffer(device, size);
2197 if (FAILED(hr))
2198 goto done;
2200 vb_pos = device->vertex_buffer_pos;
2201 align = vb_pos % stride;
2202 if (align) align = stride - align;
2203 if (vb_pos + size + align > device->vertex_buffer_size)
2204 vb_pos = 0;
2205 else
2206 vb_pos += align;
2208 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2209 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2210 if (FAILED(hr))
2211 goto done;
2212 memcpy(buffer_data, data, size);
2213 wined3d_buffer_unmap(device->vertex_buffer);
2214 device->vertex_buffer_pos = vb_pos + size;
2216 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2217 if (FAILED(hr))
2218 goto done;
2220 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2221 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2222 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2224 done:
2225 wined3d_mutex_unlock();
2226 return hr;
2229 /* The caller is responsible for wined3d locking */
2230 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2232 HRESULT hr;
2234 if (device->index_buffer_size < min_size || !device->index_buffer)
2236 UINT size = max(device->index_buffer_size * 2, min_size);
2237 struct wined3d_buffer *buffer;
2239 TRACE("Growing index buffer to %u bytes\n", size);
2241 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2242 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2243 if (FAILED(hr))
2245 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2246 return hr;
2249 if (device->index_buffer)
2250 wined3d_buffer_decref(device->index_buffer);
2252 device->index_buffer = buffer;
2253 device->index_buffer_size = size;
2254 device->index_buffer_pos = 0;
2256 return D3D_OK;
2259 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2260 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2261 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2262 const void *vertex_data, UINT vertex_stride)
2264 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2265 HRESULT hr;
2266 BYTE *buffer_data;
2268 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2269 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2270 UINT idx_size = idx_count * idx_fmt_size;
2271 UINT ib_pos;
2273 UINT vtx_size = vertex_count * vertex_stride;
2274 UINT vb_pos, align;
2276 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2277 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2278 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2279 index_data, index_format, vertex_data, vertex_stride);
2281 if (!primitive_count)
2283 WARN("primitive_count is 0, returning D3D_OK\n");
2284 return D3D_OK;
2287 wined3d_mutex_lock();
2289 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2290 if (FAILED(hr))
2291 goto done;
2293 vb_pos = device->vertex_buffer_pos;
2294 align = vb_pos % vertex_stride;
2295 if (align) align = vertex_stride - align;
2296 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2297 vb_pos = 0;
2298 else
2299 vb_pos += align;
2301 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2302 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2303 if (FAILED(hr))
2304 goto done;
2305 memcpy(buffer_data, vertex_data, vtx_size);
2306 wined3d_buffer_unmap(device->vertex_buffer);
2307 device->vertex_buffer_pos = vb_pos + vtx_size;
2309 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2310 if (FAILED(hr))
2311 goto done;
2313 ib_pos = device->index_buffer_pos;
2314 align = ib_pos % idx_fmt_size;
2315 if (align) align = idx_fmt_size - align;
2316 if (ib_pos + idx_size + align > device->index_buffer_size)
2317 ib_pos = 0;
2318 else
2319 ib_pos += align;
2321 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2322 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2323 if (FAILED(hr))
2324 goto done;
2325 memcpy(buffer_data, index_data, idx_size);
2326 wined3d_buffer_unmap(device->index_buffer);
2327 device->index_buffer_pos = ib_pos + idx_size;
2329 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2330 if (FAILED(hr))
2331 goto done;
2333 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2334 wined3dformat_from_d3dformat(index_format));
2335 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2337 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2338 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2340 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2341 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2342 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2344 done:
2345 wined3d_mutex_unlock();
2346 return hr;
2349 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2350 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2351 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2353 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2354 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2355 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2356 HRESULT hr;
2358 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2359 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2361 wined3d_mutex_lock();
2362 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2363 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2364 flags, dst_impl->fvf);
2365 wined3d_mutex_unlock();
2367 return hr;
2370 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2371 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2373 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2374 struct d3d9_vertex_declaration *object;
2375 HRESULT hr;
2377 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2379 if (!declaration)
2381 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2382 return D3DERR_INVALIDCALL;
2385 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2386 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2388 return hr;
2391 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2392 IDirect3DVertexDeclaration9 *declaration)
2394 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2395 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2397 TRACE("iface %p, declaration %p.\n", iface, declaration);
2399 wined3d_mutex_lock();
2400 wined3d_device_set_vertex_declaration(device->wined3d_device,
2401 decl_impl ? decl_impl->wined3d_declaration : NULL);
2402 wined3d_mutex_unlock();
2404 return D3D_OK;
2407 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2408 IDirect3DVertexDeclaration9 **declaration)
2410 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2411 struct wined3d_vertex_declaration *wined3d_declaration;
2412 struct d3d9_vertex_declaration *declaration_impl;
2414 TRACE("iface %p, declaration %p.\n", iface, declaration);
2416 if (!declaration) return D3DERR_INVALIDCALL;
2418 wined3d_mutex_lock();
2419 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2421 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2422 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2423 IDirect3DVertexDeclaration9_AddRef(*declaration);
2425 else
2427 *declaration = NULL;
2429 wined3d_mutex_unlock();
2431 TRACE("Returning %p.\n", *declaration);
2432 return D3D_OK;
2435 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2437 struct wined3d_vertex_declaration *wined3d_declaration;
2438 struct fvf_declaration *fvf_decls = device->fvf_decls;
2439 struct d3d9_vertex_declaration *d3d9_declaration;
2440 D3DVERTEXELEMENT9 *elements;
2441 int p, low, high; /* deliberately signed */
2442 HRESULT hr;
2444 TRACE("Searching for declaration for fvf %08x... ", fvf);
2446 low = 0;
2447 high = device->fvf_decl_count - 1;
2448 while (low <= high)
2450 p = (low + high) >> 1;
2451 TRACE("%d ", p);
2453 if (fvf_decls[p].fvf == fvf)
2455 TRACE("found %p.\n", fvf_decls[p].decl);
2456 return fvf_decls[p].decl;
2459 if (fvf_decls[p].fvf < fvf)
2460 low = p + 1;
2461 else
2462 high = p - 1;
2464 TRACE("not found. Creating and inserting at position %d.\n", low);
2466 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2467 return NULL;
2469 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2470 HeapFree(GetProcessHeap(), 0, elements);
2471 if (FAILED(hr))
2472 return NULL;
2474 if (device->fvf_decl_size == device->fvf_decl_count)
2476 UINT grow = max(device->fvf_decl_size / 2, 8);
2478 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2479 if (!fvf_decls)
2481 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2482 return NULL;
2484 device->fvf_decls = fvf_decls;
2485 device->fvf_decl_size += grow;
2488 d3d9_declaration->fvf = fvf;
2489 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2490 wined3d_vertex_declaration_incref(wined3d_declaration);
2491 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2493 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2494 fvf_decls[low].decl = wined3d_declaration;
2495 fvf_decls[low].fvf = fvf;
2496 ++device->fvf_decl_count;
2498 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2500 return wined3d_declaration;
2503 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2505 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2506 struct wined3d_vertex_declaration *decl;
2508 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2510 if (!fvf)
2512 WARN("%#x is not a valid FVF.\n", fvf);
2513 return D3D_OK;
2516 wined3d_mutex_lock();
2517 if (!(decl = device_get_fvf_declaration(device, fvf)))
2519 wined3d_mutex_unlock();
2520 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2521 return D3DERR_DRIVERINTERNALERROR;
2524 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2525 wined3d_mutex_unlock();
2527 return D3D_OK;
2530 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2532 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2533 struct wined3d_vertex_declaration *wined3d_declaration;
2534 struct d3d9_vertex_declaration *d3d9_declaration;
2536 TRACE("iface %p, fvf %p.\n", iface, fvf);
2538 wined3d_mutex_lock();
2539 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2541 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2542 *fvf = d3d9_declaration->fvf;
2544 else
2546 *fvf = 0;
2548 wined3d_mutex_unlock();
2550 TRACE("Returning FVF %#x.\n", *fvf);
2552 return D3D_OK;
2555 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2556 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2558 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2559 struct d3d9_vertexshader *object;
2560 HRESULT hr;
2562 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2564 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2565 if (!object)
2566 return E_OUTOFMEMORY;
2568 hr = vertexshader_init(object, device, byte_code);
2569 if (FAILED(hr))
2571 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2572 HeapFree(GetProcessHeap(), 0, object);
2573 return hr;
2576 TRACE("Created vertex shader %p.\n", object);
2577 *shader = &object->IDirect3DVertexShader9_iface;
2579 return D3D_OK;
2582 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2584 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2585 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2587 TRACE("iface %p, shader %p.\n", iface, shader);
2589 wined3d_mutex_lock();
2590 wined3d_device_set_vertex_shader(device->wined3d_device,
2591 shader_obj ? shader_obj->wined3d_shader : NULL);
2592 wined3d_mutex_unlock();
2594 return D3D_OK;
2597 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2599 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2600 struct d3d9_vertexshader *shader_impl;
2601 struct wined3d_shader *wined3d_shader;
2603 TRACE("iface %p, shader %p.\n", iface, shader);
2605 wined3d_mutex_lock();
2606 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2608 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2609 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2610 IDirect3DVertexShader9_AddRef(*shader);
2612 else
2614 *shader = NULL;
2616 wined3d_mutex_unlock();
2618 TRACE("Returning %p.\n", *shader);
2620 return D3D_OK;
2623 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2624 UINT reg_idx, const float *data, UINT count)
2626 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2627 HRESULT hr;
2629 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2631 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2633 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2634 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2635 return D3DERR_INVALIDCALL;
2638 wined3d_mutex_lock();
2639 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2640 wined3d_mutex_unlock();
2642 return hr;
2645 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2646 UINT reg_idx, float *data, UINT count)
2648 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2649 HRESULT hr;
2651 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2653 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2655 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2656 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2657 return D3DERR_INVALIDCALL;
2660 wined3d_mutex_lock();
2661 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2662 wined3d_mutex_unlock();
2664 return hr;
2667 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2668 UINT reg_idx, const int *data, UINT count)
2670 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2671 HRESULT hr;
2673 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2675 wined3d_mutex_lock();
2676 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2677 wined3d_mutex_unlock();
2679 return hr;
2682 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2683 UINT reg_idx, int *data, UINT count)
2685 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2686 HRESULT hr;
2688 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2690 wined3d_mutex_lock();
2691 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2692 wined3d_mutex_unlock();
2694 return hr;
2697 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2698 UINT reg_idx, const BOOL *data, UINT count)
2700 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2701 HRESULT hr;
2703 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2705 wined3d_mutex_lock();
2706 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2707 wined3d_mutex_unlock();
2709 return hr;
2712 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2713 UINT reg_idx, BOOL *data, UINT count)
2715 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2716 HRESULT hr;
2718 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2720 wined3d_mutex_lock();
2721 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2722 wined3d_mutex_unlock();
2724 return hr;
2727 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2728 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2730 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2731 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2732 HRESULT hr;
2734 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2735 iface, stream_idx, buffer, offset, stride);
2737 wined3d_mutex_lock();
2738 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2739 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2740 wined3d_mutex_unlock();
2742 return hr;
2745 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2746 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2749 struct d3d9_vertexbuffer *buffer_impl;
2750 struct wined3d_buffer *wined3d_buffer;
2751 HRESULT hr;
2753 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2754 iface, stream_idx, buffer, offset, stride);
2756 if (!buffer)
2757 return D3DERR_INVALIDCALL;
2759 wined3d_mutex_lock();
2760 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2761 if (SUCCEEDED(hr) && wined3d_buffer)
2763 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2764 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2765 IDirect3DVertexBuffer9_AddRef(*buffer);
2767 else
2769 if (FAILED(hr))
2770 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2771 *buffer = NULL;
2773 wined3d_mutex_unlock();
2775 return hr;
2778 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2780 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2781 HRESULT hr;
2783 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2785 wined3d_mutex_lock();
2786 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2787 wined3d_mutex_unlock();
2789 return hr;
2792 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2794 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2795 HRESULT hr;
2797 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2799 wined3d_mutex_lock();
2800 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2801 wined3d_mutex_unlock();
2803 return hr;
2806 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2808 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2809 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2811 TRACE("iface %p, buffer %p.\n", iface, buffer);
2813 wined3d_mutex_lock();
2814 wined3d_device_set_index_buffer(device->wined3d_device,
2815 ib ? ib->wined3d_buffer : NULL,
2816 ib ? ib->format : WINED3DFMT_UNKNOWN);
2817 wined3d_mutex_unlock();
2819 return D3D_OK;
2822 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2824 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2825 enum wined3d_format_id wined3d_format;
2826 struct wined3d_buffer *wined3d_buffer;
2827 struct d3d9_indexbuffer *buffer_impl;
2829 TRACE("iface %p, buffer %p.\n", iface, buffer);
2831 if (!buffer)
2832 return D3DERR_INVALIDCALL;
2834 wined3d_mutex_lock();
2835 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2837 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2838 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2839 IDirect3DIndexBuffer9_AddRef(*buffer);
2841 else
2843 *buffer = NULL;
2845 wined3d_mutex_unlock();
2847 return D3D_OK;
2850 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2851 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2853 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2854 struct d3d9_pixelshader *object;
2855 HRESULT hr;
2857 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2859 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2860 if (!object)
2862 FIXME("Failed to allocate pixel shader memory.\n");
2863 return E_OUTOFMEMORY;
2866 hr = pixelshader_init(object, device, byte_code);
2867 if (FAILED(hr))
2869 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2870 HeapFree(GetProcessHeap(), 0, object);
2871 return hr;
2874 TRACE("Created pixel shader %p.\n", object);
2875 *shader = &object->IDirect3DPixelShader9_iface;
2877 return D3D_OK;
2880 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2882 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2883 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2885 TRACE("iface %p, shader %p.\n", iface, shader);
2887 wined3d_mutex_lock();
2888 wined3d_device_set_pixel_shader(device->wined3d_device,
2889 shader_obj ? shader_obj->wined3d_shader : NULL);
2890 wined3d_mutex_unlock();
2892 return D3D_OK;
2895 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2897 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2898 struct d3d9_pixelshader *shader_impl;
2899 struct wined3d_shader *wined3d_shader;
2901 TRACE("iface %p, shader %p.\n", iface, shader);
2903 if (!shader) return D3DERR_INVALIDCALL;
2905 wined3d_mutex_lock();
2906 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2908 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2909 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2910 IDirect3DPixelShader9_AddRef(*shader);
2912 else
2914 *shader = NULL;
2916 wined3d_mutex_unlock();
2918 TRACE("Returning %p.\n", *shader);
2920 return D3D_OK;
2923 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2924 UINT reg_idx, const float *data, UINT count)
2926 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2927 HRESULT hr;
2929 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2931 wined3d_mutex_lock();
2932 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2933 wined3d_mutex_unlock();
2935 return hr;
2938 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2939 UINT reg_idx, float *data, UINT count)
2941 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2942 HRESULT hr;
2944 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2946 wined3d_mutex_lock();
2947 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2948 wined3d_mutex_unlock();
2950 return hr;
2953 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2954 UINT reg_idx, const int *data, UINT count)
2956 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2957 HRESULT hr;
2959 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2961 wined3d_mutex_lock();
2962 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2963 wined3d_mutex_unlock();
2965 return hr;
2968 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2969 UINT reg_idx, int *data, UINT count)
2971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2972 HRESULT hr;
2974 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2976 wined3d_mutex_lock();
2977 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2978 wined3d_mutex_unlock();
2980 return hr;
2983 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2984 UINT reg_idx, const BOOL *data, UINT count)
2986 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2987 HRESULT hr;
2989 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2991 wined3d_mutex_lock();
2992 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2993 wined3d_mutex_unlock();
2995 return hr;
2998 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2999 UINT reg_idx, BOOL *data, UINT count)
3001 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3002 HRESULT hr;
3004 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3006 wined3d_mutex_lock();
3007 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3008 wined3d_mutex_unlock();
3010 return hr;
3013 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3014 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3016 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3017 iface, handle, segment_count, patch_info);
3018 return D3D_OK;
3021 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3022 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3024 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3025 iface, handle, segment_count, patch_info);
3026 return D3D_OK;
3029 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3031 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3032 return D3DERR_INVALIDCALL;
3035 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3037 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3038 struct d3d9_query *object;
3039 HRESULT hr;
3041 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3043 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3044 if (!object)
3045 return E_OUTOFMEMORY;
3047 hr = query_init(object, device, type);
3048 if (FAILED(hr))
3050 WARN("Failed to initialize query, hr %#x.\n", hr);
3051 HeapFree(GetProcessHeap(), 0, object);
3052 return hr;
3055 TRACE("Created query %p.\n", object);
3056 if (query) *query = &object->IDirect3DQuery9_iface;
3057 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3059 return D3D_OK;
3062 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3063 UINT width, UINT height, float *rows, float *columns)
3065 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3066 iface, width, height, rows, columns);
3068 return E_NOTIMPL;
3071 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3072 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3073 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3075 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3076 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3077 iface, src_surface, dst_surface, src_descs, rect_count,
3078 dst_descs, operation, offset_x, offset_y);
3080 return E_NOTIMPL;
3083 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3084 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3085 const RGNDATA *dirty_region, DWORD flags)
3087 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3088 HRESULT hr;
3090 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3091 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3092 dst_window_override, dirty_region, flags);
3094 if (device->device_state != D3D9_DEVICE_STATE_OK)
3095 return S_PRESENT_OCCLUDED;
3097 wined3d_mutex_lock();
3098 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
3099 dst_window_override, dirty_region, flags);
3100 wined3d_mutex_unlock();
3102 return hr;
3105 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3107 FIXME("iface %p, priority %p stub!\n", iface, priority);
3109 return E_NOTIMPL;
3112 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3114 FIXME("iface %p, priority %d stub!\n", iface, priority);
3116 return E_NOTIMPL;
3119 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3121 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3123 return E_NOTIMPL;
3126 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3127 IDirect3DResource9 **resources, UINT32 resource_count)
3129 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3130 iface, resources, resource_count);
3132 return E_NOTIMPL;
3135 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3137 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3139 return E_NOTIMPL;
3142 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3144 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3146 *max_latency = 2;
3148 return E_NOTIMPL;
3151 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3153 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3154 struct wined3d_swapchain_desc swapchain_desc;
3155 struct wined3d_swapchain *swapchain;
3157 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3159 wined3d_mutex_lock();
3160 swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3161 wined3d_swapchain_get_desc(swapchain, &swapchain_desc);
3162 wined3d_mutex_unlock();
3164 if (swapchain_desc.windowed)
3165 return D3D_OK;
3167 /* FIXME: This is actually supposed to check if any other device is in
3168 * fullscreen mode. */
3169 if (dst_window != swapchain_desc.device_window)
3170 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3172 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3175 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3176 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3177 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3179 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3180 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3181 iface, width, height, format, multisample_type, multisample_quality,
3182 lockable, surface, shared_handle, usage);
3184 *surface = NULL;
3185 if (shared_handle)
3186 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3188 return E_NOTIMPL;
3191 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3192 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3193 HANDLE *shared_handle, DWORD usage)
3195 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3196 iface, width, height, format, pool, surface, shared_handle, usage);
3198 return E_NOTIMPL;
3201 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3202 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3203 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3205 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3206 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3207 iface, width, height, format, multisample_type, multisample_quality,
3208 discard, surface, shared_handle, usage);
3210 *surface = NULL;
3211 if (shared_handle)
3212 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3214 return E_NOTIMPL;
3217 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3218 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3220 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3221 struct wined3d_swapchain_desc swapchain_desc;
3222 struct wined3d_display_mode wined3d_mode;
3223 HRESULT hr;
3225 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3227 if (mode)
3229 wined3d_mode.width = mode->Width;
3230 wined3d_mode.height = mode->Height;
3231 wined3d_mode.refresh_rate = mode->RefreshRate;
3232 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
3233 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
3236 wined3d_mutex_lock();
3238 if (device->vertex_buffer)
3240 wined3d_buffer_decref(device->vertex_buffer);
3241 device->vertex_buffer = NULL;
3242 device->vertex_buffer_size = 0;
3244 if (device->index_buffer)
3246 wined3d_buffer_decref(device->index_buffer);
3247 device->index_buffer = NULL;
3248 device->index_buffer_size = 0;
3251 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
3252 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
3253 mode ? &wined3d_mode : NULL, reset_enum_callback, FALSE);
3254 wined3d_mutex_unlock();
3256 return hr;
3259 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3260 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3262 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3263 struct wined3d_display_mode wined3d_mode;
3264 HRESULT hr;
3266 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3267 iface, swapchain_idx, mode, rotation);
3269 if (mode->Size != sizeof(*mode))
3270 return D3DERR_INVALIDCALL;
3272 wined3d_mutex_lock();
3273 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3274 (enum wined3d_display_rotation *)rotation);
3275 wined3d_mutex_unlock();
3277 if (SUCCEEDED(hr))
3279 mode->Width = wined3d_mode.width;
3280 mode->Height = wined3d_mode.height;
3281 mode->RefreshRate = wined3d_mode.refresh_rate;
3282 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3283 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3286 return hr;
3289 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3291 /* IUnknown */
3292 d3d9_device_QueryInterface,
3293 d3d9_device_AddRef,
3294 d3d9_device_Release,
3295 /* IDirect3DDevice9 */
3296 d3d9_device_TestCooperativeLevel,
3297 d3d9_device_GetAvailableTextureMem,
3298 d3d9_device_EvictManagedResources,
3299 d3d9_device_GetDirect3D,
3300 d3d9_device_GetDeviceCaps,
3301 d3d9_device_GetDisplayMode,
3302 d3d9_device_GetCreationParameters,
3303 d3d9_device_SetCursorProperties,
3304 d3d9_device_SetCursorPosition,
3305 d3d9_device_ShowCursor,
3306 d3d9_device_CreateAdditionalSwapChain,
3307 d3d9_device_GetSwapChain,
3308 d3d9_device_GetNumberOfSwapChains,
3309 d3d9_device_Reset,
3310 d3d9_device_Present,
3311 d3d9_device_GetBackBuffer,
3312 d3d9_device_GetRasterStatus,
3313 d3d9_device_SetDialogBoxMode,
3314 d3d9_device_SetGammaRamp,
3315 d3d9_device_GetGammaRamp,
3316 d3d9_device_CreateTexture,
3317 d3d9_device_CreateVolumeTexture,
3318 d3d9_device_CreateCubeTexture,
3319 d3d9_device_CreateVertexBuffer,
3320 d3d9_device_CreateIndexBuffer,
3321 d3d9_device_CreateRenderTarget,
3322 d3d9_device_CreateDepthStencilSurface,
3323 d3d9_device_UpdateSurface,
3324 d3d9_device_UpdateTexture,
3325 d3d9_device_GetRenderTargetData,
3326 d3d9_device_GetFrontBufferData,
3327 d3d9_device_StretchRect,
3328 d3d9_device_ColorFill,
3329 d3d9_device_CreateOffscreenPlainSurface,
3330 d3d9_device_SetRenderTarget,
3331 d3d9_device_GetRenderTarget,
3332 d3d9_device_SetDepthStencilSurface,
3333 d3d9_device_GetDepthStencilSurface,
3334 d3d9_device_BeginScene,
3335 d3d9_device_EndScene,
3336 d3d9_device_Clear,
3337 d3d9_device_SetTransform,
3338 d3d9_device_GetTransform,
3339 d3d9_device_MultiplyTransform,
3340 d3d9_device_SetViewport,
3341 d3d9_device_GetViewport,
3342 d3d9_device_SetMaterial,
3343 d3d9_device_GetMaterial,
3344 d3d9_device_SetLight,
3345 d3d9_device_GetLight,
3346 d3d9_device_LightEnable,
3347 d3d9_device_GetLightEnable,
3348 d3d9_device_SetClipPlane,
3349 d3d9_device_GetClipPlane,
3350 d3d9_device_SetRenderState,
3351 d3d9_device_GetRenderState,
3352 d3d9_device_CreateStateBlock,
3353 d3d9_device_BeginStateBlock,
3354 d3d9_device_EndStateBlock,
3355 d3d9_device_SetClipStatus,
3356 d3d9_device_GetClipStatus,
3357 d3d9_device_GetTexture,
3358 d3d9_device_SetTexture,
3359 d3d9_device_GetTextureStageState,
3360 d3d9_device_SetTextureStageState,
3361 d3d9_device_GetSamplerState,
3362 d3d9_device_SetSamplerState,
3363 d3d9_device_ValidateDevice,
3364 d3d9_device_SetPaletteEntries,
3365 d3d9_device_GetPaletteEntries,
3366 d3d9_device_SetCurrentTexturePalette,
3367 d3d9_device_GetCurrentTexturePalette,
3368 d3d9_device_SetScissorRect,
3369 d3d9_device_GetScissorRect,
3370 d3d9_device_SetSoftwareVertexProcessing,
3371 d3d9_device_GetSoftwareVertexProcessing,
3372 d3d9_device_SetNPatchMode,
3373 d3d9_device_GetNPatchMode,
3374 d3d9_device_DrawPrimitive,
3375 d3d9_device_DrawIndexedPrimitive,
3376 d3d9_device_DrawPrimitiveUP,
3377 d3d9_device_DrawIndexedPrimitiveUP,
3378 d3d9_device_ProcessVertices,
3379 d3d9_device_CreateVertexDeclaration,
3380 d3d9_device_SetVertexDeclaration,
3381 d3d9_device_GetVertexDeclaration,
3382 d3d9_device_SetFVF,
3383 d3d9_device_GetFVF,
3384 d3d9_device_CreateVertexShader,
3385 d3d9_device_SetVertexShader,
3386 d3d9_device_GetVertexShader,
3387 d3d9_device_SetVertexShaderConstantF,
3388 d3d9_device_GetVertexShaderConstantF,
3389 d3d9_device_SetVertexShaderConstantI,
3390 d3d9_device_GetVertexShaderConstantI,
3391 d3d9_device_SetVertexShaderConstantB,
3392 d3d9_device_GetVertexShaderConstantB,
3393 d3d9_device_SetStreamSource,
3394 d3d9_device_GetStreamSource,
3395 d3d9_device_SetStreamSourceFreq,
3396 d3d9_device_GetStreamSourceFreq,
3397 d3d9_device_SetIndices,
3398 d3d9_device_GetIndices,
3399 d3d9_device_CreatePixelShader,
3400 d3d9_device_SetPixelShader,
3401 d3d9_device_GetPixelShader,
3402 d3d9_device_SetPixelShaderConstantF,
3403 d3d9_device_GetPixelShaderConstantF,
3404 d3d9_device_SetPixelShaderConstantI,
3405 d3d9_device_GetPixelShaderConstantI,
3406 d3d9_device_SetPixelShaderConstantB,
3407 d3d9_device_GetPixelShaderConstantB,
3408 d3d9_device_DrawRectPatch,
3409 d3d9_device_DrawTriPatch,
3410 d3d9_device_DeletePatch,
3411 d3d9_device_CreateQuery,
3412 /* IDirect3DDevice9Ex */
3413 d3d9_device_SetConvolutionMonoKernel,
3414 d3d9_device_ComposeRects,
3415 d3d9_device_PresentEx,
3416 d3d9_device_GetGPUThreadPriority,
3417 d3d9_device_SetGPUThreadPriority,
3418 d3d9_device_WaitForVBlank,
3419 d3d9_device_CheckResourceResidency,
3420 d3d9_device_SetMaximumFrameLatency,
3421 d3d9_device_GetMaximumFrameLatency,
3422 d3d9_device_CheckDeviceState,
3423 d3d9_device_CreateRenderTargetEx,
3424 d3d9_device_CreateOffscreenPlainSurfaceEx,
3425 d3d9_device_CreateDepthStencilSurfaceEx,
3426 d3d9_device_ResetEx,
3427 d3d9_device_GetDisplayModeEx,
3430 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3432 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3435 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3436 struct wined3d_device *device)
3438 TRACE("device_parent %p, device %p.\n", device_parent, device);
3441 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3443 TRACE("device_parent %p.\n", device_parent);
3446 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3448 struct d3d9_device *device = device_from_device_parent(device_parent);
3450 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3452 if (!device->d3d_parent)
3453 return;
3455 if (!activate)
3456 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3457 else if (device->d3d_parent->extended)
3458 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3459 else
3460 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3463 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3464 void *container_parent, struct wined3d_surface *surface, void **parent,
3465 const struct wined3d_parent_ops **parent_ops)
3467 struct d3d9_surface *d3d_surface;
3469 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3470 device_parent, container_parent, surface, parent, parent_ops);
3472 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3473 return E_OUTOFMEMORY;
3475 surface_init(d3d_surface, container_parent, surface, parent_ops);
3476 *parent = d3d_surface;
3477 TRACE("Created surface %p.\n", d3d_surface);
3479 return D3D_OK;
3482 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3483 void *container_parent, struct wined3d_volume *volume, void **parent,
3484 const struct wined3d_parent_ops **parent_ops)
3486 struct d3d9_volume *d3d_volume;
3488 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3489 device_parent, container_parent, volume, parent, parent_ops);
3491 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3492 return E_OUTOFMEMORY;
3494 volume_init(d3d_volume, container_parent, volume, parent_ops);
3495 *parent = d3d_volume;
3496 TRACE("Created volume %p.\n", d3d_volume);
3498 return D3D_OK;
3501 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3502 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3504 struct d3d9_device *device = device_from_device_parent(device_parent);
3505 struct wined3d_resource_desc texture_desc;
3506 struct d3d9_surface *d3d_surface;
3507 struct wined3d_texture *texture;
3508 HRESULT hr;
3510 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3511 device_parent, container_parent, desc, surface);
3513 if (container_parent == device_parent)
3514 container_parent = &device->IDirect3DDevice9Ex_iface;
3516 texture_desc = *desc;
3517 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3518 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3519 WINED3D_SURFACE_MAPPABLE, container_parent, &d3d9_null_wined3d_parent_ops, &texture)))
3521 WARN("Failed to create texture, hr %#x.\n", hr);
3522 return hr;
3525 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3526 wined3d_surface_incref(*surface);
3527 wined3d_texture_decref(texture);
3529 d3d_surface = wined3d_surface_get_parent(*surface);
3530 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3532 return hr;
3535 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3536 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3538 struct d3d9_device *device = device_from_device_parent(device_parent);
3539 struct d3d9_swapchain *d3d_swapchain;
3540 HRESULT hr;
3542 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3544 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3545 if (FAILED(hr))
3547 WARN("Failed to create swapchain, hr %#x.\n", hr);
3548 *swapchain = NULL;
3549 return hr;
3552 *swapchain = d3d_swapchain->wined3d_swapchain;
3553 wined3d_swapchain_incref(*swapchain);
3554 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3556 return hr;
3559 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3561 device_parent_wined3d_device_created,
3562 device_parent_mode_changed,
3563 device_parent_activate,
3564 device_parent_surface_created,
3565 device_parent_volume_created,
3566 device_parent_create_swapchain_surface,
3567 device_parent_create_swapchain,
3570 static void setup_fpu(void)
3572 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3573 WORD cw;
3574 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3575 cw = (cw & ~0xf3f) | 0x3f;
3576 __asm__ volatile ("fldcw %0" : : "m" (cw));
3577 #elif defined(__i386__) && defined(_MSC_VER)
3578 WORD cw;
3579 __asm fnstcw cw;
3580 cw = (cw & ~0xf3f) | 0x3f;
3581 __asm fldcw cw;
3582 #else
3583 FIXME("FPU setup not implemented for this platform.\n");
3584 #endif
3587 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3588 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3589 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3591 struct wined3d_swapchain_desc *swapchain_desc;
3592 UINT i, count = 1;
3593 HRESULT hr;
3595 if (mode)
3596 FIXME("Ignoring display mode.\n");
3598 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3599 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3600 device->refcount = 1;
3602 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3604 wined3d_mutex_lock();
3605 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3606 &device->device_parent, &device->wined3d_device);
3607 if (FAILED(hr))
3609 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3610 wined3d_mutex_unlock();
3611 return hr;
3614 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3616 WINED3DCAPS caps;
3618 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3619 count = caps.NumberOfAdaptersInGroup;
3622 if (flags & D3DCREATE_MULTITHREADED)
3623 wined3d_device_set_multithreaded(device->wined3d_device);
3625 if (!parameters->Windowed)
3627 if (!focus_window)
3628 focus_window = parameters->hDeviceWindow;
3629 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3631 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3632 wined3d_device_decref(device->wined3d_device);
3633 wined3d_mutex_unlock();
3634 return hr;
3637 for (i = 0; i < count; ++i)
3639 HWND device_window = parameters[i].hDeviceWindow;
3641 if (!device_window) device_window = focus_window;
3642 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3643 parameters[i].BackBufferWidth,
3644 parameters[i].BackBufferHeight);
3648 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3649 if (!swapchain_desc)
3651 ERR("Failed to allocate wined3d parameters.\n");
3652 wined3d_device_decref(device->wined3d_device);
3653 wined3d_mutex_unlock();
3654 return E_OUTOFMEMORY;
3657 for (i = 0; i < count; ++i)
3659 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i]);
3662 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3663 if (FAILED(hr))
3665 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3666 wined3d_device_release_focus_window(device->wined3d_device);
3667 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3668 wined3d_device_decref(device->wined3d_device);
3669 wined3d_mutex_unlock();
3670 return hr;
3673 wined3d_mutex_unlock();
3675 for (i = 0; i < count; ++i)
3677 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3679 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3681 /* Initialize the converted declaration array. This creates a valid pointer
3682 * and when adding decls HeapReAlloc() can be used without further checking. */
3683 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3684 if (!device->fvf_decls)
3686 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3687 wined3d_mutex_lock();
3688 wined3d_device_uninit_3d(device->wined3d_device);
3689 wined3d_device_release_focus_window(device->wined3d_device);
3690 wined3d_device_decref(device->wined3d_device);
3691 wined3d_mutex_unlock();
3692 return E_OUTOFMEMORY;
3695 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3696 device->d3d_parent = parent;
3698 return D3D_OK;