po: Follow GNU specification for Language and Language-Team headers.
[wine.git] / dlls / d3d9 / device.c
blob0b1a77aa34e0549626941a8ea0316b5b1451908a
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 UINT i, count;
507 HRESULT hr;
509 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
510 iface, present_parameters, swapchain);
512 if (!present_parameters->Windowed)
514 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
515 return D3DERR_INVALIDCALL;
518 wined3d_mutex_lock();
519 count = wined3d_device_get_swapchain_count(device->wined3d_device);
520 for (i = 0; i < count; ++i)
522 struct wined3d_swapchain *wined3d_swapchain;
524 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
525 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
527 if (!desc.windowed)
529 wined3d_mutex_unlock();
530 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
531 return D3DERR_INVALIDCALL;
534 wined3d_mutex_unlock();
536 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
537 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
538 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
539 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
541 return hr;
544 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
545 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
547 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
548 struct wined3d_swapchain *wined3d_swapchain;
549 struct d3d9_swapchain *swapchain_impl;
550 HRESULT hr;
552 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
554 wined3d_mutex_lock();
555 if ((wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx)))
557 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
558 *swapchain = (IDirect3DSwapChain9 *)&swapchain_impl->IDirect3DSwapChain9Ex_iface;
559 IDirect3DSwapChain9Ex_AddRef(*swapchain);
560 hr = D3D_OK;
562 else
564 *swapchain = NULL;
565 hr = D3DERR_INVALIDCALL;
567 wined3d_mutex_unlock();
569 return hr;
572 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
574 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
575 UINT count;
577 TRACE("iface %p.\n", iface);
579 wined3d_mutex_lock();
580 count = wined3d_device_get_swapchain_count(device->wined3d_device);
581 wined3d_mutex_unlock();
583 return count;
586 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
588 struct wined3d_resource_desc desc;
590 wined3d_resource_get_desc(resource, &desc);
591 if (desc.pool == WINED3D_POOL_DEFAULT)
593 struct d3d9_surface *surface;
595 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
597 IUnknown *parent = wined3d_resource_get_parent(resource);
598 IDirect3DBaseTexture9 *texture;
600 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
602 IDirect3DBaseTexture9_Release(texture);
603 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
604 return D3DERR_INVALIDCALL;
607 return D3D_OK;
610 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
612 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
613 return D3DERR_INVALIDCALL;
616 surface = wined3d_resource_get_parent(resource);
617 if (surface->resource.refcount)
619 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
620 return D3DERR_INVALIDCALL;
623 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
626 return D3D_OK;
629 static HRESULT d3d9_device_reset(struct d3d9_device *device,
630 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
632 struct wined3d_swapchain_desc swapchain_desc;
633 struct wined3d_display_mode wined3d_mode;
634 HRESULT hr;
636 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
638 WARN("App not active, returning D3DERR_DEVICELOST.\n");
639 return D3DERR_DEVICELOST;
642 if (mode)
644 wined3d_mode.width = mode->Width;
645 wined3d_mode.height = mode->Height;
646 wined3d_mode.refresh_rate = mode->RefreshRate;
647 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
648 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
651 wined3d_mutex_lock();
653 if (device->vertex_buffer)
655 wined3d_buffer_decref(device->vertex_buffer);
656 device->vertex_buffer = NULL;
657 device->vertex_buffer_size = 0;
660 if (device->index_buffer)
662 wined3d_buffer_decref(device->index_buffer);
663 device->index_buffer = NULL;
664 device->index_buffer_size = 0;
667 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
668 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
669 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
671 struct wined3d_swapchain *wined3d_swapchain;
673 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
674 wined3d_swapchain_get_desc(wined3d_swapchain, &swapchain_desc);
675 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
676 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
677 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
678 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
680 device->device_state = D3D9_DEVICE_STATE_OK;
682 else if (!device->d3d_parent->extended)
684 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
687 wined3d_mutex_unlock();
689 return hr;
692 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
693 D3DPRESENT_PARAMETERS *present_parameters)
695 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
697 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
699 return d3d9_device_reset(device, present_parameters, NULL);
702 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
703 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
705 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
706 HRESULT hr;
708 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
709 iface, src_rect, dst_rect, dst_window_override, dirty_region);
711 if (device->device_state != D3D9_DEVICE_STATE_OK)
712 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
714 wined3d_mutex_lock();
715 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
716 dst_window_override, dirty_region, 0);
717 wined3d_mutex_unlock();
719 return hr;
722 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
723 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
725 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
726 struct wined3d_surface *wined3d_surface = NULL;
727 struct d3d9_surface *surface_impl;
728 HRESULT hr;
730 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
731 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
733 wined3d_mutex_lock();
734 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
735 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
736 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
738 surface_impl = wined3d_surface_get_parent(wined3d_surface);
739 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
740 IDirect3DSurface9_AddRef(*backbuffer);
742 wined3d_mutex_unlock();
744 return hr;
746 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
747 UINT swapchain, D3DRASTER_STATUS *raster_status)
749 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
750 HRESULT hr;
752 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
754 wined3d_mutex_lock();
755 hr = wined3d_device_get_raster_status(device->wined3d_device,
756 swapchain, (struct wined3d_raster_status *)raster_status);
757 wined3d_mutex_unlock();
759 return hr;
762 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
764 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
765 HRESULT hr;
767 TRACE("iface %p, enable %#x.\n", iface, enable);
769 wined3d_mutex_lock();
770 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
771 wined3d_mutex_unlock();
773 return hr;
776 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
777 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
779 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
781 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
783 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
784 wined3d_mutex_lock();
785 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
786 wined3d_mutex_unlock();
789 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
791 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
793 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
795 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
796 wined3d_mutex_lock();
797 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
798 wined3d_mutex_unlock();
801 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
802 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
803 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
805 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
806 struct d3d9_texture *object;
807 BOOL set_mem = FALSE;
808 HRESULT hr;
810 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
811 iface, width, height, levels, 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 or user memory texture on a non-ex device.\n");
819 return E_NOTIMPL;
822 if (pool == D3DPOOL_SYSTEMMEM)
824 if (levels != 1)
825 return D3DERR_INVALIDCALL;
826 set_mem = TRUE;
828 else
830 if (pool != D3DPOOL_DEFAULT)
832 WARN("Trying to create a shared texture in pool %#x.\n", pool);
833 return D3DERR_INVALIDCALL;
835 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
839 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
840 if (!object)
841 return D3DERR_OUTOFVIDEOMEMORY;
843 hr = texture_init(object, device, width, height, levels, usage, format, pool);
844 if (FAILED(hr))
846 WARN("Failed to initialize texture, hr %#x.\n", hr);
847 HeapFree(GetProcessHeap(), 0, object);
848 return hr;
851 if (set_mem)
852 wined3d_texture_update_desc(object->wined3d_texture, width, height,
853 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
854 *shared_handle, 0);
856 TRACE("Created texture %p.\n", object);
857 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
859 return D3D_OK;
862 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
863 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
864 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
866 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
867 struct d3d9_texture *object;
868 HRESULT hr;
870 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
871 iface, width, height, depth, levels);
872 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
873 usage, format, pool, texture, shared_handle);
875 *texture = NULL;
876 if (shared_handle)
878 if (!device->d3d_parent->extended)
880 WARN("Trying to create a shared volume texture on a non-ex device.\n");
881 return E_NOTIMPL;
884 if (pool != D3DPOOL_DEFAULT)
886 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
887 return D3DERR_INVALIDCALL;
889 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
892 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
893 if (!object)
894 return D3DERR_OUTOFVIDEOMEMORY;
896 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
897 if (FAILED(hr))
899 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
900 HeapFree(GetProcessHeap(), 0, object);
901 return hr;
904 TRACE("Created volume texture %p.\n", object);
905 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
907 return D3D_OK;
910 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
911 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
912 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
914 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
915 struct d3d9_texture *object;
916 HRESULT hr;
918 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
919 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
921 *texture = NULL;
922 if (shared_handle)
924 if (!device->d3d_parent->extended)
926 WARN("Trying to create a shared cube texture on a non-ex device.\n");
927 return E_NOTIMPL;
930 if (pool != D3DPOOL_DEFAULT)
932 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
933 return D3DERR_INVALIDCALL;
935 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
938 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
939 if (!object)
940 return D3DERR_OUTOFVIDEOMEMORY;
942 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
943 if (FAILED(hr))
945 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
946 HeapFree(GetProcessHeap(), 0, object);
947 return hr;
950 TRACE("Created cube texture %p.\n", object);
951 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
953 return D3D_OK;
956 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
957 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
958 HANDLE *shared_handle)
960 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
961 struct d3d9_vertexbuffer *object;
962 HRESULT hr;
964 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
965 iface, size, usage, fvf, pool, buffer, shared_handle);
967 if (shared_handle)
969 if (!device->d3d_parent->extended)
971 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
972 return E_NOTIMPL;
975 if (pool != D3DPOOL_DEFAULT)
977 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
978 return D3DERR_NOTAVAILABLE;
980 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
983 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
984 if (!object)
985 return D3DERR_OUTOFVIDEOMEMORY;
987 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
988 if (FAILED(hr))
990 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
991 HeapFree(GetProcessHeap(), 0, object);
992 return hr;
995 TRACE("Created vertex buffer %p.\n", object);
996 *buffer = &object->IDirect3DVertexBuffer9_iface;
998 return D3D_OK;
1001 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1002 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1003 HANDLE *shared_handle)
1005 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1006 struct d3d9_indexbuffer *object;
1007 HRESULT hr;
1009 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1010 iface, size, usage, format, pool, buffer, shared_handle);
1012 if (shared_handle)
1014 if (!device->d3d_parent->extended)
1016 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1017 return E_NOTIMPL;
1020 if (pool != D3DPOOL_DEFAULT)
1022 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1023 return D3DERR_NOTAVAILABLE;
1025 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1028 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1029 if (!object)
1030 return D3DERR_OUTOFVIDEOMEMORY;
1032 hr = indexbuffer_init(object, device, size, usage, format, pool);
1033 if (FAILED(hr))
1035 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1036 HeapFree(GetProcessHeap(), 0, object);
1037 return hr;
1040 TRACE("Created index buffer %p.\n", object);
1041 *buffer = &object->IDirect3DIndexBuffer9_iface;
1043 return D3D_OK;
1046 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1047 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1048 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1050 struct wined3d_resource *sub_resource;
1051 struct wined3d_resource_desc desc;
1052 struct d3d9_surface *surface_impl;
1053 struct wined3d_texture *texture;
1054 HRESULT hr;
1056 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1057 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1058 device, width, height, format, flags, surface, usage, pool,
1059 multisample_type, multisample_quality);
1061 desc.resource_type = WINED3D_RTYPE_TEXTURE;
1062 desc.format = wined3dformat_from_d3dformat(format);
1063 desc.multisample_type = multisample_type;
1064 desc.multisample_quality = multisample_quality;
1065 desc.usage = usage & WINED3DUSAGE_MASK;
1066 desc.pool = pool;
1067 desc.width = width;
1068 desc.height = height;
1069 desc.depth = 1;
1070 desc.size = 0;
1072 wined3d_mutex_lock();
1074 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1075 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1077 wined3d_mutex_unlock();
1078 WARN("Failed to create texture, hr %#x.\n", hr);
1079 return hr;
1082 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1083 surface_impl = wined3d_resource_get_parent(sub_resource);
1084 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1085 *surface = &surface_impl->IDirect3DSurface9_iface;
1086 IDirect3DSurface9_AddRef(*surface);
1088 if (user_mem)
1089 wined3d_texture_update_desc(texture, width, height,
1090 desc.format, multisample_type, multisample_quality, user_mem, 0);
1092 wined3d_texture_decref(texture);
1094 wined3d_mutex_unlock();
1096 return D3D_OK;
1099 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1100 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1101 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1103 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1104 DWORD flags = 0;
1106 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1107 "lockable %#x, surface %p, shared_handle %p.\n",
1108 iface, width, height, format, multisample_type, multisample_quality,
1109 lockable, surface, shared_handle);
1111 *surface = NULL;
1112 if (shared_handle)
1114 if (!device->d3d_parent->extended)
1116 WARN("Trying to create a shared render target on a non-ex device.\n");
1117 return E_NOTIMPL;
1120 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1123 if (lockable)
1124 flags |= WINED3D_SURFACE_MAPPABLE;
1126 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1127 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1130 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1131 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1132 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1134 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1135 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1137 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1138 "discard %#x, surface %p, shared_handle %p.\n",
1139 iface, width, height, format, multisample_type, multisample_quality,
1140 discard, surface, shared_handle);
1142 *surface = NULL;
1143 if (shared_handle)
1145 if (!device->d3d_parent->extended)
1147 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1148 return E_NOTIMPL;
1151 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1154 if (discard)
1155 flags |= WINED3D_SURFACE_DISCARD;
1157 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1158 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1162 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1163 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1164 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1166 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1167 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1168 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1169 HRESULT hr;
1171 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1172 iface, src_surface, src_rect, dst_surface, dst_point);
1174 wined3d_mutex_lock();
1175 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1176 dst->wined3d_surface, dst_point);
1177 wined3d_mutex_unlock();
1179 return hr;
1182 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1183 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1185 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1186 struct d3d9_texture *src_impl, *dst_impl;
1187 HRESULT hr;
1189 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1191 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1192 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1194 wined3d_mutex_lock();
1195 hr = wined3d_device_update_texture(device->wined3d_device,
1196 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1197 wined3d_mutex_unlock();
1199 return hr;
1202 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1203 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1205 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1206 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1207 HRESULT hr;
1209 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1211 wined3d_mutex_lock();
1212 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1213 wined3d_mutex_unlock();
1215 return hr;
1218 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1219 UINT swapchain, IDirect3DSurface9 *dst_surface)
1221 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1222 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1223 HRESULT hr;
1225 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1227 wined3d_mutex_lock();
1228 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1229 wined3d_mutex_unlock();
1231 return hr;
1234 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1235 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1237 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1238 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1239 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1240 HRESULT hr = D3DERR_INVALIDCALL;
1241 struct wined3d_resource_desc src_desc, dst_desc;
1242 struct wined3d_resource *wined3d_resource;
1244 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1245 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1247 wined3d_mutex_lock();
1248 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1249 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1251 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1252 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1254 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1256 if (device->in_scene)
1258 WARN("Rejecting depth / stencil blit while in scene.\n");
1259 goto done;
1262 if (src_rect)
1264 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1265 || src_rect->bottom != src_desc.height)
1267 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1268 wine_dbgstr_rect(src_rect));
1269 goto done;
1272 if (dst_rect)
1274 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1275 || dst_rect->bottom != dst_desc.height)
1277 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1278 wine_dbgstr_rect(dst_rect));
1279 goto done;
1282 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1284 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1285 goto done;
1289 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1290 if (hr == WINEDDERR_INVALIDRECT)
1291 hr = D3DERR_INVALIDCALL;
1293 done:
1294 wined3d_mutex_unlock();
1295 return hr;
1298 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1299 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1301 const struct wined3d_color c =
1303 ((color >> 16) & 0xff) / 255.0f,
1304 ((color >> 8) & 0xff) / 255.0f,
1305 (color & 0xff) / 255.0f,
1306 ((color >> 24) & 0xff) / 255.0f,
1308 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1309 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1310 struct wined3d_resource *wined3d_resource;
1311 struct wined3d_resource_desc desc;
1312 HRESULT hr;
1314 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1316 wined3d_mutex_lock();
1318 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1319 wined3d_resource_get_desc(wined3d_resource, &desc);
1321 if (desc.pool != WINED3D_POOL_DEFAULT)
1323 wined3d_mutex_unlock();
1324 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1325 return D3DERR_INVALIDCALL;
1327 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1329 wined3d_mutex_unlock();
1330 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1331 return D3DERR_INVALIDCALL;
1333 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1335 wined3d_mutex_unlock();
1336 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1337 return D3DERR_INVALIDCALL;
1340 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1341 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1343 wined3d_mutex_unlock();
1345 return hr;
1348 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1349 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1350 HANDLE *shared_handle)
1352 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1353 void *user_mem = NULL;
1355 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1356 iface, width, height, format, pool, surface, shared_handle);
1358 *surface = NULL;
1359 if (pool == D3DPOOL_MANAGED)
1361 WARN("Attempting to create a managed offscreen plain surface.\n");
1362 return D3DERR_INVALIDCALL;
1365 if (shared_handle)
1367 if (!device->d3d_parent->extended)
1369 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1370 return E_NOTIMPL;
1373 if (pool == D3DPOOL_SYSTEMMEM)
1374 user_mem = *shared_handle;
1375 else
1377 if (pool != D3DPOOL_DEFAULT)
1379 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1380 return D3DERR_INVALIDCALL;
1382 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1386 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1387 * regardless of the pool they're created in. Should we set dynamic usage
1388 * here? */
1389 return d3d9_device_create_surface(device, width, height, format,
1390 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1393 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1395 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1396 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1397 HRESULT hr;
1399 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1401 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1403 WARN("Invalid index %u specified.\n", idx);
1404 return D3DERR_INVALIDCALL;
1407 if (!idx && !surface_impl)
1409 WARN("Trying to set render target 0 to NULL.\n");
1410 return D3DERR_INVALIDCALL;
1413 wined3d_mutex_lock();
1414 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1415 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1416 wined3d_mutex_unlock();
1418 return hr;
1421 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1423 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1424 struct wined3d_rendertarget_view *wined3d_rtv;
1425 struct d3d9_surface *surface_impl;
1426 HRESULT hr = D3D_OK;
1428 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1430 if (!surface)
1431 return D3DERR_INVALIDCALL;
1433 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1435 WARN("Invalid index %u specified.\n", idx);
1436 return D3DERR_INVALIDCALL;
1439 wined3d_mutex_lock();
1440 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1442 /* We want the sub resource parent here, since the view itself may be
1443 * internal to wined3d and may not have a parent. */
1444 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1445 *surface = &surface_impl->IDirect3DSurface9_iface;
1446 IDirect3DSurface9_AddRef(*surface);
1448 else
1450 hr = WINED3DERR_NOTFOUND;
1451 *surface = NULL;
1453 wined3d_mutex_unlock();
1455 return hr;
1458 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1460 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1461 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1463 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1465 wined3d_mutex_lock();
1466 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1467 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1468 wined3d_mutex_unlock();
1470 return D3D_OK;
1473 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1475 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1476 struct wined3d_rendertarget_view *wined3d_dsv;
1477 struct d3d9_surface *surface_impl;
1478 HRESULT hr = D3D_OK;
1480 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1482 if (!depth_stencil)
1483 return D3DERR_INVALIDCALL;
1485 wined3d_mutex_lock();
1486 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1488 /* We want the sub resource parent here, since the view itself may be
1489 * internal to wined3d and may not have a parent. */
1490 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1491 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1492 IDirect3DSurface9_AddRef(*depth_stencil);
1494 else
1496 hr = WINED3DERR_NOTFOUND;
1497 *depth_stencil = NULL;
1499 wined3d_mutex_unlock();
1501 return hr;
1504 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1506 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1507 HRESULT hr;
1509 TRACE("iface %p.\n", iface);
1511 wined3d_mutex_lock();
1512 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1513 device->in_scene = TRUE;
1514 wined3d_mutex_unlock();
1516 return hr;
1519 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1521 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1522 HRESULT hr;
1524 TRACE("iface %p.\n", iface);
1526 wined3d_mutex_lock();
1527 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1528 device->in_scene = FALSE;
1529 wined3d_mutex_unlock();
1531 return hr;
1534 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1535 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1537 const struct wined3d_color c =
1539 ((color >> 16) & 0xff) / 255.0f,
1540 ((color >> 8) & 0xff) / 255.0f,
1541 (color & 0xff) / 255.0f,
1542 ((color >> 24) & 0xff) / 255.0f,
1544 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1545 HRESULT hr;
1547 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1548 iface, rect_count, rects, flags, color, z, stencil);
1550 wined3d_mutex_lock();
1551 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1552 wined3d_mutex_unlock();
1554 return hr;
1557 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1558 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1560 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1562 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1564 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1565 wined3d_mutex_lock();
1566 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1567 wined3d_mutex_unlock();
1569 return D3D_OK;
1572 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1573 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1575 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1577 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1579 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1580 wined3d_mutex_lock();
1581 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1582 wined3d_mutex_unlock();
1584 return D3D_OK;
1587 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1588 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1590 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1592 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1594 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1595 wined3d_mutex_lock();
1596 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1597 wined3d_mutex_unlock();
1599 return D3D_OK;
1602 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1604 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1606 TRACE("iface %p, viewport %p.\n", iface, viewport);
1608 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1609 wined3d_mutex_lock();
1610 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1611 wined3d_mutex_unlock();
1613 return D3D_OK;
1616 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1618 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1620 TRACE("iface %p, viewport %p.\n", iface, viewport);
1622 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1623 wined3d_mutex_lock();
1624 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1625 wined3d_mutex_unlock();
1627 return D3D_OK;
1630 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1632 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1634 TRACE("iface %p, material %p.\n", iface, material);
1636 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1637 wined3d_mutex_lock();
1638 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1639 wined3d_mutex_unlock();
1641 return D3D_OK;
1644 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1646 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1648 TRACE("iface %p, material %p.\n", iface, material);
1650 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1651 wined3d_mutex_lock();
1652 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1653 wined3d_mutex_unlock();
1655 return D3D_OK;
1658 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1660 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1661 HRESULT hr;
1663 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1665 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1666 wined3d_mutex_lock();
1667 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1668 wined3d_mutex_unlock();
1670 return hr;
1673 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1675 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1676 HRESULT hr;
1678 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1680 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1681 wined3d_mutex_lock();
1682 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1683 wined3d_mutex_unlock();
1685 return hr;
1688 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1690 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1691 HRESULT hr;
1693 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1695 wined3d_mutex_lock();
1696 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1697 wined3d_mutex_unlock();
1699 return hr;
1702 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1704 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1705 HRESULT hr;
1707 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1709 wined3d_mutex_lock();
1710 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1711 wined3d_mutex_unlock();
1713 return hr;
1716 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1719 HRESULT hr;
1721 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1723 wined3d_mutex_lock();
1724 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1725 wined3d_mutex_unlock();
1727 return hr;
1730 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1733 HRESULT hr;
1735 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1737 wined3d_mutex_lock();
1738 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1739 wined3d_mutex_unlock();
1741 return hr;
1744 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1745 D3DRENDERSTATETYPE state, DWORD value)
1747 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1749 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1751 wined3d_mutex_lock();
1752 wined3d_device_set_render_state(device->wined3d_device, state, value);
1753 wined3d_mutex_unlock();
1755 return D3D_OK;
1758 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1759 D3DRENDERSTATETYPE state, DWORD *value)
1761 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1763 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1765 wined3d_mutex_lock();
1766 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1767 wined3d_mutex_unlock();
1769 return D3D_OK;
1772 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1773 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1775 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1776 struct d3d9_stateblock *object;
1777 HRESULT hr;
1779 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1781 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1783 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1784 return D3DERR_INVALIDCALL;
1787 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1788 if (!object)
1789 return E_OUTOFMEMORY;
1791 hr = stateblock_init(object, device, type, NULL);
1792 if (FAILED(hr))
1794 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1795 HeapFree(GetProcessHeap(), 0, object);
1796 return hr;
1799 TRACE("Created stateblock %p.\n", object);
1800 *stateblock = &object->IDirect3DStateBlock9_iface;
1802 return D3D_OK;
1805 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1807 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1808 HRESULT hr;
1810 TRACE("iface %p.\n", iface);
1812 wined3d_mutex_lock();
1813 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1814 wined3d_mutex_unlock();
1816 return hr;
1819 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1821 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1822 struct wined3d_stateblock *wined3d_stateblock;
1823 struct d3d9_stateblock *object;
1824 HRESULT hr;
1826 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1828 wined3d_mutex_lock();
1829 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1830 wined3d_mutex_unlock();
1831 if (FAILED(hr))
1833 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1834 return hr;
1837 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1838 if (!object)
1840 wined3d_mutex_lock();
1841 wined3d_stateblock_decref(wined3d_stateblock);
1842 wined3d_mutex_unlock();
1843 return E_OUTOFMEMORY;
1846 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1847 if (FAILED(hr))
1849 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1850 wined3d_mutex_lock();
1851 wined3d_stateblock_decref(wined3d_stateblock);
1852 wined3d_mutex_unlock();
1853 HeapFree(GetProcessHeap(), 0, object);
1854 return hr;
1857 TRACE("Created stateblock %p.\n", object);
1858 *stateblock = &object->IDirect3DStateBlock9_iface;
1860 return D3D_OK;
1863 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1865 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1866 HRESULT hr;
1868 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1870 wined3d_mutex_lock();
1871 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1872 wined3d_mutex_unlock();
1874 return hr;
1877 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1879 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1880 HRESULT hr;
1882 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1884 wined3d_mutex_lock();
1885 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1886 wined3d_mutex_unlock();
1888 return hr;
1891 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1893 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1894 struct wined3d_texture *wined3d_texture = NULL;
1895 struct d3d9_texture *texture_impl;
1897 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1899 if (!texture)
1900 return D3DERR_INVALIDCALL;
1902 wined3d_mutex_lock();
1903 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1905 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1906 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1907 IDirect3DBaseTexture9_AddRef(*texture);
1909 else
1911 *texture = NULL;
1913 wined3d_mutex_unlock();
1915 return D3D_OK;
1918 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1920 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1921 struct d3d9_texture *texture_impl;
1922 HRESULT hr;
1924 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1926 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1928 wined3d_mutex_lock();
1929 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1930 texture_impl ? texture_impl->wined3d_texture : NULL);
1931 wined3d_mutex_unlock();
1933 return hr;
1936 static const enum wined3d_texture_stage_state tss_lookup[] =
1938 WINED3D_TSS_INVALID, /* 0, unused */
1939 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1940 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1941 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1942 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1943 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1944 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1945 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1946 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1947 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1948 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1949 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1950 WINED3D_TSS_INVALID, /* 12, unused */
1951 WINED3D_TSS_INVALID, /* 13, unused */
1952 WINED3D_TSS_INVALID, /* 14, unused */
1953 WINED3D_TSS_INVALID, /* 15, unused */
1954 WINED3D_TSS_INVALID, /* 16, unused */
1955 WINED3D_TSS_INVALID, /* 17, unused */
1956 WINED3D_TSS_INVALID, /* 18, unused */
1957 WINED3D_TSS_INVALID, /* 19, unused */
1958 WINED3D_TSS_INVALID, /* 20, unused */
1959 WINED3D_TSS_INVALID, /* 21, unused */
1960 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1961 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1962 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1963 WINED3D_TSS_INVALID, /* 25, unused */
1964 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1965 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1966 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1967 WINED3D_TSS_INVALID, /* 29, unused */
1968 WINED3D_TSS_INVALID, /* 30, unused */
1969 WINED3D_TSS_INVALID, /* 31, unused */
1970 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1973 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1974 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1976 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1978 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1980 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1982 WARN("Invalid state %#x passed.\n", state);
1983 return D3D_OK;
1986 wined3d_mutex_lock();
1987 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
1988 wined3d_mutex_unlock();
1990 return D3D_OK;
1993 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1994 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1996 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1998 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2000 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2002 WARN("Invalid state %#x passed.\n", state);
2003 return D3D_OK;
2006 wined3d_mutex_lock();
2007 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2008 wined3d_mutex_unlock();
2010 return D3D_OK;
2013 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2014 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2016 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2018 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2020 wined3d_mutex_lock();
2021 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2022 wined3d_mutex_unlock();
2024 return D3D_OK;
2027 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2028 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2030 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2032 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2034 wined3d_mutex_lock();
2035 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2036 wined3d_mutex_unlock();
2038 return D3D_OK;
2041 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2043 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2044 HRESULT hr;
2046 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2048 wined3d_mutex_lock();
2049 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2050 wined3d_mutex_unlock();
2052 return hr;
2055 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2056 UINT palette_idx, const PALETTEENTRY *entries)
2058 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2060 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2061 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2062 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2064 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2065 return D3D_OK;
2068 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2069 UINT palette_idx, PALETTEENTRY *entries)
2071 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2073 return D3DERR_INVALIDCALL;
2076 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2078 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2080 return D3D_OK;
2083 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2085 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2087 return D3DERR_INVALIDCALL;
2090 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2092 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2094 TRACE("iface %p, rect %p.\n", iface, rect);
2096 wined3d_mutex_lock();
2097 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2098 wined3d_mutex_unlock();
2100 return D3D_OK;
2103 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2105 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2107 TRACE("iface %p, rect %p.\n", iface, rect);
2109 wined3d_mutex_lock();
2110 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2111 wined3d_mutex_unlock();
2113 return D3D_OK;
2116 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2118 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2120 TRACE("iface %p, software %#x.\n", iface, software);
2122 wined3d_mutex_lock();
2123 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2124 wined3d_mutex_unlock();
2126 return D3D_OK;
2129 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2131 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2132 BOOL ret;
2134 TRACE("iface %p.\n", iface);
2136 wined3d_mutex_lock();
2137 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2138 wined3d_mutex_unlock();
2140 return ret;
2143 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2145 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2146 HRESULT hr;
2148 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2150 wined3d_mutex_lock();
2151 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2152 wined3d_mutex_unlock();
2154 return hr;
2157 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2159 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2160 float ret;
2162 TRACE("iface %p.\n", iface);
2164 wined3d_mutex_lock();
2165 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2166 wined3d_mutex_unlock();
2168 return ret;
2171 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2172 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2174 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2175 HRESULT hr;
2177 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2178 iface, primitive_type, start_vertex, primitive_count);
2180 wined3d_mutex_lock();
2181 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2182 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2183 vertex_count_from_primitive_count(primitive_type, primitive_count));
2184 wined3d_mutex_unlock();
2186 return hr;
2189 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2190 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2191 UINT vertex_count, UINT start_idx, UINT primitive_count)
2193 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2194 HRESULT hr;
2196 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2197 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2198 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2199 vertex_count, start_idx, primitive_count);
2201 wined3d_mutex_lock();
2202 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2203 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2204 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2205 vertex_count_from_primitive_count(primitive_type, primitive_count));
2206 wined3d_mutex_unlock();
2208 return hr;
2211 /* The caller is responsible for wined3d locking */
2212 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2214 HRESULT hr;
2216 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2218 UINT size = max(device->vertex_buffer_size * 2, min_size);
2219 struct wined3d_buffer *buffer;
2221 TRACE("Growing vertex buffer to %u bytes\n", size);
2223 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2224 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2225 if (FAILED(hr))
2227 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2228 return hr;
2231 if (device->vertex_buffer)
2232 wined3d_buffer_decref(device->vertex_buffer);
2234 device->vertex_buffer = buffer;
2235 device->vertex_buffer_size = size;
2236 device->vertex_buffer_pos = 0;
2238 return D3D_OK;
2241 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2242 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2244 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2245 HRESULT hr;
2246 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2247 UINT size = vtx_count * stride;
2248 UINT vb_pos, align;
2249 BYTE *buffer_data;
2251 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2252 iface, primitive_type, primitive_count, data, stride);
2254 if (!primitive_count)
2256 WARN("primitive_count is 0, returning D3D_OK\n");
2257 return D3D_OK;
2260 wined3d_mutex_lock();
2262 hr = d3d9_device_prepare_vertex_buffer(device, size);
2263 if (FAILED(hr))
2264 goto done;
2266 vb_pos = device->vertex_buffer_pos;
2267 align = vb_pos % stride;
2268 if (align) align = stride - align;
2269 if (vb_pos + size + align > device->vertex_buffer_size)
2270 vb_pos = 0;
2271 else
2272 vb_pos += align;
2274 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2275 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2276 if (FAILED(hr))
2277 goto done;
2278 memcpy(buffer_data, data, size);
2279 wined3d_buffer_unmap(device->vertex_buffer);
2280 device->vertex_buffer_pos = vb_pos + size;
2282 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2283 if (FAILED(hr))
2284 goto done;
2286 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2287 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2288 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2290 done:
2291 wined3d_mutex_unlock();
2292 return hr;
2295 /* The caller is responsible for wined3d locking */
2296 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2298 HRESULT hr;
2300 if (device->index_buffer_size < min_size || !device->index_buffer)
2302 UINT size = max(device->index_buffer_size * 2, min_size);
2303 struct wined3d_buffer *buffer;
2305 TRACE("Growing index buffer to %u bytes\n", size);
2307 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2308 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2309 if (FAILED(hr))
2311 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2312 return hr;
2315 if (device->index_buffer)
2316 wined3d_buffer_decref(device->index_buffer);
2318 device->index_buffer = buffer;
2319 device->index_buffer_size = size;
2320 device->index_buffer_pos = 0;
2322 return D3D_OK;
2325 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2326 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2327 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2328 const void *vertex_data, UINT vertex_stride)
2330 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2331 HRESULT hr;
2332 BYTE *buffer_data;
2334 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2335 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2336 UINT idx_size = idx_count * idx_fmt_size;
2337 UINT ib_pos;
2339 UINT vtx_size = vertex_count * vertex_stride;
2340 UINT vb_pos, align;
2342 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2343 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2344 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2345 index_data, index_format, vertex_data, vertex_stride);
2347 if (!primitive_count)
2349 WARN("primitive_count is 0, returning D3D_OK\n");
2350 return D3D_OK;
2353 wined3d_mutex_lock();
2355 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2356 if (FAILED(hr))
2357 goto done;
2359 vb_pos = device->vertex_buffer_pos;
2360 align = vb_pos % vertex_stride;
2361 if (align) align = vertex_stride - align;
2362 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2363 vb_pos = 0;
2364 else
2365 vb_pos += align;
2367 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2368 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2369 if (FAILED(hr))
2370 goto done;
2371 memcpy(buffer_data, vertex_data, vtx_size);
2372 wined3d_buffer_unmap(device->vertex_buffer);
2373 device->vertex_buffer_pos = vb_pos + vtx_size;
2375 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2376 if (FAILED(hr))
2377 goto done;
2379 ib_pos = device->index_buffer_pos;
2380 align = ib_pos % idx_fmt_size;
2381 if (align) align = idx_fmt_size - align;
2382 if (ib_pos + idx_size + align > device->index_buffer_size)
2383 ib_pos = 0;
2384 else
2385 ib_pos += align;
2387 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2388 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2389 if (FAILED(hr))
2390 goto done;
2391 memcpy(buffer_data, index_data, idx_size);
2392 wined3d_buffer_unmap(device->index_buffer);
2393 device->index_buffer_pos = ib_pos + idx_size;
2395 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2396 if (FAILED(hr))
2397 goto done;
2399 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2400 wined3dformat_from_d3dformat(index_format));
2401 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2403 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2404 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2406 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2407 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2408 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2410 done:
2411 wined3d_mutex_unlock();
2412 return hr;
2415 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2416 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2417 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2419 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2420 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2421 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2422 HRESULT hr;
2424 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2425 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2427 wined3d_mutex_lock();
2428 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2429 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2430 flags, dst_impl->fvf);
2431 wined3d_mutex_unlock();
2433 return hr;
2436 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2437 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2440 struct d3d9_vertex_declaration *object;
2441 HRESULT hr;
2443 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2445 if (!declaration)
2447 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2448 return D3DERR_INVALIDCALL;
2451 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2452 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2454 return hr;
2457 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2458 IDirect3DVertexDeclaration9 *declaration)
2460 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2461 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2463 TRACE("iface %p, declaration %p.\n", iface, declaration);
2465 wined3d_mutex_lock();
2466 wined3d_device_set_vertex_declaration(device->wined3d_device,
2467 decl_impl ? decl_impl->wined3d_declaration : NULL);
2468 wined3d_mutex_unlock();
2470 return D3D_OK;
2473 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2474 IDirect3DVertexDeclaration9 **declaration)
2476 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2477 struct wined3d_vertex_declaration *wined3d_declaration;
2478 struct d3d9_vertex_declaration *declaration_impl;
2480 TRACE("iface %p, declaration %p.\n", iface, declaration);
2482 if (!declaration) return D3DERR_INVALIDCALL;
2484 wined3d_mutex_lock();
2485 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2487 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2488 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2489 IDirect3DVertexDeclaration9_AddRef(*declaration);
2491 else
2493 *declaration = NULL;
2495 wined3d_mutex_unlock();
2497 TRACE("Returning %p.\n", *declaration);
2498 return D3D_OK;
2501 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2503 struct wined3d_vertex_declaration *wined3d_declaration;
2504 struct fvf_declaration *fvf_decls = device->fvf_decls;
2505 struct d3d9_vertex_declaration *d3d9_declaration;
2506 D3DVERTEXELEMENT9 *elements;
2507 int p, low, high; /* deliberately signed */
2508 HRESULT hr;
2510 TRACE("Searching for declaration for fvf %08x... ", fvf);
2512 low = 0;
2513 high = device->fvf_decl_count - 1;
2514 while (low <= high)
2516 p = (low + high) >> 1;
2517 TRACE("%d ", p);
2519 if (fvf_decls[p].fvf == fvf)
2521 TRACE("found %p.\n", fvf_decls[p].decl);
2522 return fvf_decls[p].decl;
2525 if (fvf_decls[p].fvf < fvf)
2526 low = p + 1;
2527 else
2528 high = p - 1;
2530 TRACE("not found. Creating and inserting at position %d.\n", low);
2532 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2533 return NULL;
2535 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2536 HeapFree(GetProcessHeap(), 0, elements);
2537 if (FAILED(hr))
2538 return NULL;
2540 if (device->fvf_decl_size == device->fvf_decl_count)
2542 UINT grow = max(device->fvf_decl_size / 2, 8);
2544 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2545 if (!fvf_decls)
2547 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2548 return NULL;
2550 device->fvf_decls = fvf_decls;
2551 device->fvf_decl_size += grow;
2554 d3d9_declaration->fvf = fvf;
2555 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2556 wined3d_vertex_declaration_incref(wined3d_declaration);
2557 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2559 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2560 fvf_decls[low].decl = wined3d_declaration;
2561 fvf_decls[low].fvf = fvf;
2562 ++device->fvf_decl_count;
2564 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2566 return wined3d_declaration;
2569 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2571 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2572 struct wined3d_vertex_declaration *decl;
2574 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2576 if (!fvf)
2578 WARN("%#x is not a valid FVF.\n", fvf);
2579 return D3D_OK;
2582 wined3d_mutex_lock();
2583 if (!(decl = device_get_fvf_declaration(device, fvf)))
2585 wined3d_mutex_unlock();
2586 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2587 return D3DERR_DRIVERINTERNALERROR;
2590 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2591 wined3d_mutex_unlock();
2593 return D3D_OK;
2596 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2598 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2599 struct wined3d_vertex_declaration *wined3d_declaration;
2600 struct d3d9_vertex_declaration *d3d9_declaration;
2602 TRACE("iface %p, fvf %p.\n", iface, fvf);
2604 wined3d_mutex_lock();
2605 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2607 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2608 *fvf = d3d9_declaration->fvf;
2610 else
2612 *fvf = 0;
2614 wined3d_mutex_unlock();
2616 TRACE("Returning FVF %#x.\n", *fvf);
2618 return D3D_OK;
2621 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2622 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2624 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2625 struct d3d9_vertexshader *object;
2626 HRESULT hr;
2628 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2630 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2631 if (!object)
2632 return E_OUTOFMEMORY;
2634 hr = vertexshader_init(object, device, byte_code);
2635 if (FAILED(hr))
2637 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2638 HeapFree(GetProcessHeap(), 0, object);
2639 return hr;
2642 TRACE("Created vertex shader %p.\n", object);
2643 *shader = &object->IDirect3DVertexShader9_iface;
2645 return D3D_OK;
2648 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2650 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2651 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2653 TRACE("iface %p, shader %p.\n", iface, shader);
2655 wined3d_mutex_lock();
2656 wined3d_device_set_vertex_shader(device->wined3d_device,
2657 shader_obj ? shader_obj->wined3d_shader : NULL);
2658 wined3d_mutex_unlock();
2660 return D3D_OK;
2663 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2665 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2666 struct d3d9_vertexshader *shader_impl;
2667 struct wined3d_shader *wined3d_shader;
2669 TRACE("iface %p, shader %p.\n", iface, shader);
2671 wined3d_mutex_lock();
2672 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2674 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2675 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2676 IDirect3DVertexShader9_AddRef(*shader);
2678 else
2680 *shader = NULL;
2682 wined3d_mutex_unlock();
2684 TRACE("Returning %p.\n", *shader);
2686 return D3D_OK;
2689 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2690 UINT reg_idx, const float *data, UINT count)
2692 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2693 HRESULT hr;
2695 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2697 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2699 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2700 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2701 return D3DERR_INVALIDCALL;
2704 wined3d_mutex_lock();
2705 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2706 wined3d_mutex_unlock();
2708 return hr;
2711 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2712 UINT reg_idx, float *data, UINT count)
2714 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2715 HRESULT hr;
2717 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2719 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2721 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2722 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2723 return D3DERR_INVALIDCALL;
2726 wined3d_mutex_lock();
2727 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2728 wined3d_mutex_unlock();
2730 return hr;
2733 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2734 UINT reg_idx, const int *data, UINT count)
2736 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2737 HRESULT hr;
2739 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2741 wined3d_mutex_lock();
2742 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2743 wined3d_mutex_unlock();
2745 return hr;
2748 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2749 UINT reg_idx, int *data, UINT count)
2751 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2752 HRESULT hr;
2754 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2756 wined3d_mutex_lock();
2757 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2758 wined3d_mutex_unlock();
2760 return hr;
2763 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2764 UINT reg_idx, const BOOL *data, UINT count)
2766 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2767 HRESULT hr;
2769 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2771 wined3d_mutex_lock();
2772 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2773 wined3d_mutex_unlock();
2775 return hr;
2778 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2779 UINT reg_idx, BOOL *data, UINT count)
2781 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2782 HRESULT hr;
2784 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2786 wined3d_mutex_lock();
2787 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2788 wined3d_mutex_unlock();
2790 return hr;
2793 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2794 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2796 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2797 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2798 HRESULT hr;
2800 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2801 iface, stream_idx, buffer, offset, stride);
2803 wined3d_mutex_lock();
2804 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2805 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2806 wined3d_mutex_unlock();
2808 return hr;
2811 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2812 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2814 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2815 struct d3d9_vertexbuffer *buffer_impl;
2816 struct wined3d_buffer *wined3d_buffer;
2817 HRESULT hr;
2819 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2820 iface, stream_idx, buffer, offset, stride);
2822 if (!buffer)
2823 return D3DERR_INVALIDCALL;
2825 wined3d_mutex_lock();
2826 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2827 if (SUCCEEDED(hr) && wined3d_buffer)
2829 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2830 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2831 IDirect3DVertexBuffer9_AddRef(*buffer);
2833 else
2835 if (FAILED(hr))
2836 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2837 *buffer = NULL;
2839 wined3d_mutex_unlock();
2841 return hr;
2844 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2846 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2847 HRESULT hr;
2849 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2851 wined3d_mutex_lock();
2852 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2853 wined3d_mutex_unlock();
2855 return hr;
2858 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2860 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2861 HRESULT hr;
2863 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2865 wined3d_mutex_lock();
2866 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2867 wined3d_mutex_unlock();
2869 return hr;
2872 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2874 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2875 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2877 TRACE("iface %p, buffer %p.\n", iface, buffer);
2879 wined3d_mutex_lock();
2880 wined3d_device_set_index_buffer(device->wined3d_device,
2881 ib ? ib->wined3d_buffer : NULL,
2882 ib ? ib->format : WINED3DFMT_UNKNOWN);
2883 wined3d_mutex_unlock();
2885 return D3D_OK;
2888 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2890 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2891 enum wined3d_format_id wined3d_format;
2892 struct wined3d_buffer *wined3d_buffer;
2893 struct d3d9_indexbuffer *buffer_impl;
2895 TRACE("iface %p, buffer %p.\n", iface, buffer);
2897 if (!buffer)
2898 return D3DERR_INVALIDCALL;
2900 wined3d_mutex_lock();
2901 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2903 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2904 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2905 IDirect3DIndexBuffer9_AddRef(*buffer);
2907 else
2909 *buffer = NULL;
2911 wined3d_mutex_unlock();
2913 return D3D_OK;
2916 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2917 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2919 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2920 struct d3d9_pixelshader *object;
2921 HRESULT hr;
2923 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2925 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2926 if (!object)
2928 FIXME("Failed to allocate pixel shader memory.\n");
2929 return E_OUTOFMEMORY;
2932 hr = pixelshader_init(object, device, byte_code);
2933 if (FAILED(hr))
2935 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2936 HeapFree(GetProcessHeap(), 0, object);
2937 return hr;
2940 TRACE("Created pixel shader %p.\n", object);
2941 *shader = &object->IDirect3DPixelShader9_iface;
2943 return D3D_OK;
2946 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2948 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2949 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2951 TRACE("iface %p, shader %p.\n", iface, shader);
2953 wined3d_mutex_lock();
2954 wined3d_device_set_pixel_shader(device->wined3d_device,
2955 shader_obj ? shader_obj->wined3d_shader : NULL);
2956 wined3d_mutex_unlock();
2958 return D3D_OK;
2961 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2963 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2964 struct d3d9_pixelshader *shader_impl;
2965 struct wined3d_shader *wined3d_shader;
2967 TRACE("iface %p, shader %p.\n", iface, shader);
2969 if (!shader) return D3DERR_INVALIDCALL;
2971 wined3d_mutex_lock();
2972 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2974 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2975 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2976 IDirect3DPixelShader9_AddRef(*shader);
2978 else
2980 *shader = NULL;
2982 wined3d_mutex_unlock();
2984 TRACE("Returning %p.\n", *shader);
2986 return D3D_OK;
2989 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2990 UINT reg_idx, const float *data, UINT count)
2992 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2993 HRESULT hr;
2995 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2997 wined3d_mutex_lock();
2998 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2999 wined3d_mutex_unlock();
3001 return hr;
3004 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3005 UINT reg_idx, float *data, UINT count)
3007 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3008 HRESULT hr;
3010 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3012 wined3d_mutex_lock();
3013 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
3014 wined3d_mutex_unlock();
3016 return hr;
3019 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3020 UINT reg_idx, const int *data, UINT count)
3022 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3023 HRESULT hr;
3025 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3027 wined3d_mutex_lock();
3028 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3029 wined3d_mutex_unlock();
3031 return hr;
3034 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3035 UINT reg_idx, int *data, UINT count)
3037 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3038 HRESULT hr;
3040 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3042 wined3d_mutex_lock();
3043 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3044 wined3d_mutex_unlock();
3046 return hr;
3049 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3050 UINT reg_idx, const BOOL *data, UINT count)
3052 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3053 HRESULT hr;
3055 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3057 wined3d_mutex_lock();
3058 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3059 wined3d_mutex_unlock();
3061 return hr;
3064 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3065 UINT reg_idx, BOOL *data, UINT count)
3067 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3068 HRESULT hr;
3070 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3072 wined3d_mutex_lock();
3073 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3074 wined3d_mutex_unlock();
3076 return hr;
3079 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3080 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3082 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3083 iface, handle, segment_count, patch_info);
3084 return D3D_OK;
3087 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3088 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3090 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3091 iface, handle, segment_count, patch_info);
3092 return D3D_OK;
3095 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3097 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3098 return D3DERR_INVALIDCALL;
3101 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3103 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3104 struct d3d9_query *object;
3105 HRESULT hr;
3107 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3109 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3110 if (!object)
3111 return E_OUTOFMEMORY;
3113 hr = query_init(object, device, type);
3114 if (FAILED(hr))
3116 WARN("Failed to initialize query, hr %#x.\n", hr);
3117 HeapFree(GetProcessHeap(), 0, object);
3118 return hr;
3121 TRACE("Created query %p.\n", object);
3122 if (query) *query = &object->IDirect3DQuery9_iface;
3123 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3125 return D3D_OK;
3128 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3129 UINT width, UINT height, float *rows, float *columns)
3131 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3132 iface, width, height, rows, columns);
3134 return E_NOTIMPL;
3137 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3138 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3139 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3141 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3142 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3143 iface, src_surface, dst_surface, src_descs, rect_count,
3144 dst_descs, operation, offset_x, offset_y);
3146 return E_NOTIMPL;
3149 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3150 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3151 const RGNDATA *dirty_region, DWORD flags)
3153 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3154 HRESULT hr;
3156 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3157 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3158 dst_window_override, dirty_region, flags);
3160 if (device->device_state != D3D9_DEVICE_STATE_OK)
3161 return S_PRESENT_OCCLUDED;
3163 wined3d_mutex_lock();
3164 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
3165 dst_window_override, dirty_region, flags);
3166 wined3d_mutex_unlock();
3168 return hr;
3171 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3173 FIXME("iface %p, priority %p stub!\n", iface, priority);
3175 return E_NOTIMPL;
3178 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3180 FIXME("iface %p, priority %d stub!\n", iface, priority);
3182 return E_NOTIMPL;
3185 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3187 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3189 return E_NOTIMPL;
3192 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3193 IDirect3DResource9 **resources, UINT32 resource_count)
3195 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3196 iface, resources, resource_count);
3198 return E_NOTIMPL;
3201 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3203 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3205 return E_NOTIMPL;
3208 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3210 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3212 *max_latency = 2;
3214 return E_NOTIMPL;
3217 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3219 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3220 struct wined3d_swapchain_desc swapchain_desc;
3221 struct wined3d_swapchain *swapchain;
3223 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3225 wined3d_mutex_lock();
3226 swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3227 wined3d_swapchain_get_desc(swapchain, &swapchain_desc);
3228 wined3d_mutex_unlock();
3230 if (swapchain_desc.windowed)
3231 return D3D_OK;
3233 /* FIXME: This is actually supposed to check if any other device is in
3234 * fullscreen mode. */
3235 if (dst_window != swapchain_desc.device_window)
3236 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3238 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3241 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3242 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3243 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3245 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3246 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3247 iface, width, height, format, multisample_type, multisample_quality,
3248 lockable, surface, shared_handle, usage);
3250 *surface = NULL;
3251 if (shared_handle)
3252 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3254 return E_NOTIMPL;
3257 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3258 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3259 HANDLE *shared_handle, DWORD usage)
3261 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3262 iface, width, height, format, pool, surface, shared_handle, usage);
3264 return E_NOTIMPL;
3267 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3268 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3269 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3271 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3272 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3273 iface, width, height, format, multisample_type, multisample_quality,
3274 discard, surface, shared_handle, usage);
3276 *surface = NULL;
3277 if (shared_handle)
3278 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3280 return E_NOTIMPL;
3283 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3284 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3286 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3288 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3290 return d3d9_device_reset(device, present_parameters, mode);
3293 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3294 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3296 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3297 struct wined3d_display_mode wined3d_mode;
3298 HRESULT hr;
3300 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3301 iface, swapchain_idx, mode, rotation);
3303 if (mode->Size != sizeof(*mode))
3304 return D3DERR_INVALIDCALL;
3306 wined3d_mutex_lock();
3307 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3308 (enum wined3d_display_rotation *)rotation);
3309 wined3d_mutex_unlock();
3311 if (SUCCEEDED(hr))
3313 mode->Width = wined3d_mode.width;
3314 mode->Height = wined3d_mode.height;
3315 mode->RefreshRate = wined3d_mode.refresh_rate;
3316 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3317 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3320 return hr;
3323 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3325 /* IUnknown */
3326 d3d9_device_QueryInterface,
3327 d3d9_device_AddRef,
3328 d3d9_device_Release,
3329 /* IDirect3DDevice9 */
3330 d3d9_device_TestCooperativeLevel,
3331 d3d9_device_GetAvailableTextureMem,
3332 d3d9_device_EvictManagedResources,
3333 d3d9_device_GetDirect3D,
3334 d3d9_device_GetDeviceCaps,
3335 d3d9_device_GetDisplayMode,
3336 d3d9_device_GetCreationParameters,
3337 d3d9_device_SetCursorProperties,
3338 d3d9_device_SetCursorPosition,
3339 d3d9_device_ShowCursor,
3340 d3d9_device_CreateAdditionalSwapChain,
3341 d3d9_device_GetSwapChain,
3342 d3d9_device_GetNumberOfSwapChains,
3343 d3d9_device_Reset,
3344 d3d9_device_Present,
3345 d3d9_device_GetBackBuffer,
3346 d3d9_device_GetRasterStatus,
3347 d3d9_device_SetDialogBoxMode,
3348 d3d9_device_SetGammaRamp,
3349 d3d9_device_GetGammaRamp,
3350 d3d9_device_CreateTexture,
3351 d3d9_device_CreateVolumeTexture,
3352 d3d9_device_CreateCubeTexture,
3353 d3d9_device_CreateVertexBuffer,
3354 d3d9_device_CreateIndexBuffer,
3355 d3d9_device_CreateRenderTarget,
3356 d3d9_device_CreateDepthStencilSurface,
3357 d3d9_device_UpdateSurface,
3358 d3d9_device_UpdateTexture,
3359 d3d9_device_GetRenderTargetData,
3360 d3d9_device_GetFrontBufferData,
3361 d3d9_device_StretchRect,
3362 d3d9_device_ColorFill,
3363 d3d9_device_CreateOffscreenPlainSurface,
3364 d3d9_device_SetRenderTarget,
3365 d3d9_device_GetRenderTarget,
3366 d3d9_device_SetDepthStencilSurface,
3367 d3d9_device_GetDepthStencilSurface,
3368 d3d9_device_BeginScene,
3369 d3d9_device_EndScene,
3370 d3d9_device_Clear,
3371 d3d9_device_SetTransform,
3372 d3d9_device_GetTransform,
3373 d3d9_device_MultiplyTransform,
3374 d3d9_device_SetViewport,
3375 d3d9_device_GetViewport,
3376 d3d9_device_SetMaterial,
3377 d3d9_device_GetMaterial,
3378 d3d9_device_SetLight,
3379 d3d9_device_GetLight,
3380 d3d9_device_LightEnable,
3381 d3d9_device_GetLightEnable,
3382 d3d9_device_SetClipPlane,
3383 d3d9_device_GetClipPlane,
3384 d3d9_device_SetRenderState,
3385 d3d9_device_GetRenderState,
3386 d3d9_device_CreateStateBlock,
3387 d3d9_device_BeginStateBlock,
3388 d3d9_device_EndStateBlock,
3389 d3d9_device_SetClipStatus,
3390 d3d9_device_GetClipStatus,
3391 d3d9_device_GetTexture,
3392 d3d9_device_SetTexture,
3393 d3d9_device_GetTextureStageState,
3394 d3d9_device_SetTextureStageState,
3395 d3d9_device_GetSamplerState,
3396 d3d9_device_SetSamplerState,
3397 d3d9_device_ValidateDevice,
3398 d3d9_device_SetPaletteEntries,
3399 d3d9_device_GetPaletteEntries,
3400 d3d9_device_SetCurrentTexturePalette,
3401 d3d9_device_GetCurrentTexturePalette,
3402 d3d9_device_SetScissorRect,
3403 d3d9_device_GetScissorRect,
3404 d3d9_device_SetSoftwareVertexProcessing,
3405 d3d9_device_GetSoftwareVertexProcessing,
3406 d3d9_device_SetNPatchMode,
3407 d3d9_device_GetNPatchMode,
3408 d3d9_device_DrawPrimitive,
3409 d3d9_device_DrawIndexedPrimitive,
3410 d3d9_device_DrawPrimitiveUP,
3411 d3d9_device_DrawIndexedPrimitiveUP,
3412 d3d9_device_ProcessVertices,
3413 d3d9_device_CreateVertexDeclaration,
3414 d3d9_device_SetVertexDeclaration,
3415 d3d9_device_GetVertexDeclaration,
3416 d3d9_device_SetFVF,
3417 d3d9_device_GetFVF,
3418 d3d9_device_CreateVertexShader,
3419 d3d9_device_SetVertexShader,
3420 d3d9_device_GetVertexShader,
3421 d3d9_device_SetVertexShaderConstantF,
3422 d3d9_device_GetVertexShaderConstantF,
3423 d3d9_device_SetVertexShaderConstantI,
3424 d3d9_device_GetVertexShaderConstantI,
3425 d3d9_device_SetVertexShaderConstantB,
3426 d3d9_device_GetVertexShaderConstantB,
3427 d3d9_device_SetStreamSource,
3428 d3d9_device_GetStreamSource,
3429 d3d9_device_SetStreamSourceFreq,
3430 d3d9_device_GetStreamSourceFreq,
3431 d3d9_device_SetIndices,
3432 d3d9_device_GetIndices,
3433 d3d9_device_CreatePixelShader,
3434 d3d9_device_SetPixelShader,
3435 d3d9_device_GetPixelShader,
3436 d3d9_device_SetPixelShaderConstantF,
3437 d3d9_device_GetPixelShaderConstantF,
3438 d3d9_device_SetPixelShaderConstantI,
3439 d3d9_device_GetPixelShaderConstantI,
3440 d3d9_device_SetPixelShaderConstantB,
3441 d3d9_device_GetPixelShaderConstantB,
3442 d3d9_device_DrawRectPatch,
3443 d3d9_device_DrawTriPatch,
3444 d3d9_device_DeletePatch,
3445 d3d9_device_CreateQuery,
3446 /* IDirect3DDevice9Ex */
3447 d3d9_device_SetConvolutionMonoKernel,
3448 d3d9_device_ComposeRects,
3449 d3d9_device_PresentEx,
3450 d3d9_device_GetGPUThreadPriority,
3451 d3d9_device_SetGPUThreadPriority,
3452 d3d9_device_WaitForVBlank,
3453 d3d9_device_CheckResourceResidency,
3454 d3d9_device_SetMaximumFrameLatency,
3455 d3d9_device_GetMaximumFrameLatency,
3456 d3d9_device_CheckDeviceState,
3457 d3d9_device_CreateRenderTargetEx,
3458 d3d9_device_CreateOffscreenPlainSurfaceEx,
3459 d3d9_device_CreateDepthStencilSurfaceEx,
3460 d3d9_device_ResetEx,
3461 d3d9_device_GetDisplayModeEx,
3464 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3466 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3469 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3470 struct wined3d_device *device)
3472 TRACE("device_parent %p, device %p.\n", device_parent, device);
3475 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3477 TRACE("device_parent %p.\n", device_parent);
3480 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3482 struct d3d9_device *device = device_from_device_parent(device_parent);
3484 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3486 if (!device->d3d_parent)
3487 return;
3489 if (!activate)
3490 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3491 else if (device->d3d_parent->extended)
3492 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3493 else
3494 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3497 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3498 void *container_parent, struct wined3d_surface *surface, void **parent,
3499 const struct wined3d_parent_ops **parent_ops)
3501 struct d3d9_surface *d3d_surface;
3503 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3504 device_parent, container_parent, surface, parent, parent_ops);
3506 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3507 return E_OUTOFMEMORY;
3509 surface_init(d3d_surface, container_parent, surface, parent_ops);
3510 *parent = d3d_surface;
3511 TRACE("Created surface %p.\n", d3d_surface);
3513 return D3D_OK;
3516 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3517 void *container_parent, struct wined3d_volume *volume, void **parent,
3518 const struct wined3d_parent_ops **parent_ops)
3520 struct d3d9_volume *d3d_volume;
3522 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3523 device_parent, container_parent, volume, parent, parent_ops);
3525 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3526 return E_OUTOFMEMORY;
3528 volume_init(d3d_volume, container_parent, volume, parent_ops);
3529 *parent = d3d_volume;
3530 TRACE("Created volume %p.\n", d3d_volume);
3532 return D3D_OK;
3535 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3536 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3538 struct d3d9_device *device = device_from_device_parent(device_parent);
3539 struct wined3d_resource_desc texture_desc;
3540 struct d3d9_surface *d3d_surface;
3541 struct wined3d_texture *texture;
3542 HRESULT hr;
3544 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3545 device_parent, container_parent, desc, surface);
3547 if (container_parent == device_parent)
3548 container_parent = &device->IDirect3DDevice9Ex_iface;
3550 texture_desc = *desc;
3551 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3552 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3553 WINED3D_SURFACE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, &texture)))
3555 WARN("Failed to create texture, hr %#x.\n", hr);
3556 return hr;
3559 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3560 wined3d_surface_incref(*surface);
3561 wined3d_texture_decref(texture);
3563 d3d_surface = wined3d_surface_get_parent(*surface);
3564 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3566 return hr;
3569 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3570 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3572 struct d3d9_device *device = device_from_device_parent(device_parent);
3573 struct d3d9_swapchain *d3d_swapchain;
3574 HRESULT hr;
3576 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3578 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3579 if (FAILED(hr))
3581 WARN("Failed to create swapchain, hr %#x.\n", hr);
3582 *swapchain = NULL;
3583 return hr;
3586 *swapchain = d3d_swapchain->wined3d_swapchain;
3587 wined3d_swapchain_incref(*swapchain);
3588 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3590 return hr;
3593 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3595 device_parent_wined3d_device_created,
3596 device_parent_mode_changed,
3597 device_parent_activate,
3598 device_parent_surface_created,
3599 device_parent_volume_created,
3600 device_parent_create_swapchain_surface,
3601 device_parent_create_swapchain,
3604 static void setup_fpu(void)
3606 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3607 WORD cw;
3608 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3609 cw = (cw & ~0xf3f) | 0x3f;
3610 __asm__ volatile ("fldcw %0" : : "m" (cw));
3611 #elif defined(__i386__) && defined(_MSC_VER)
3612 WORD cw;
3613 __asm fnstcw cw;
3614 cw = (cw & ~0xf3f) | 0x3f;
3615 __asm fldcw cw;
3616 #else
3617 FIXME("FPU setup not implemented for this platform.\n");
3618 #endif
3621 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3622 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3623 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3625 struct wined3d_swapchain_desc *swapchain_desc;
3626 UINT i, count = 1;
3627 HRESULT hr;
3629 if (mode)
3630 FIXME("Ignoring display mode.\n");
3632 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3633 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3634 device->refcount = 1;
3636 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3638 wined3d_mutex_lock();
3639 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3640 &device->device_parent, &device->wined3d_device);
3641 if (FAILED(hr))
3643 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3644 wined3d_mutex_unlock();
3645 return hr;
3648 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3650 WINED3DCAPS caps;
3652 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3653 count = caps.NumberOfAdaptersInGroup;
3656 if (flags & D3DCREATE_MULTITHREADED)
3657 wined3d_device_set_multithreaded(device->wined3d_device);
3659 if (!parameters->Windowed)
3661 if (!focus_window)
3662 focus_window = parameters->hDeviceWindow;
3663 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3665 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3666 wined3d_device_decref(device->wined3d_device);
3667 wined3d_mutex_unlock();
3668 return hr;
3671 for (i = 0; i < count; ++i)
3673 HWND device_window = parameters[i].hDeviceWindow;
3675 if (!device_window) device_window = focus_window;
3676 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3677 parameters[i].BackBufferWidth,
3678 parameters[i].BackBufferHeight);
3682 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3683 if (!swapchain_desc)
3685 ERR("Failed to allocate wined3d parameters.\n");
3686 wined3d_device_decref(device->wined3d_device);
3687 wined3d_mutex_unlock();
3688 return E_OUTOFMEMORY;
3691 for (i = 0; i < count; ++i)
3693 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i]);
3696 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3697 if (FAILED(hr))
3699 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3700 wined3d_device_release_focus_window(device->wined3d_device);
3701 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3702 wined3d_device_decref(device->wined3d_device);
3703 wined3d_mutex_unlock();
3704 return hr;
3707 wined3d_mutex_unlock();
3709 for (i = 0; i < count; ++i)
3711 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3713 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3715 /* Initialize the converted declaration array. This creates a valid pointer
3716 * and when adding decls HeapReAlloc() can be used without further checking. */
3717 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3718 if (!device->fvf_decls)
3720 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3721 wined3d_mutex_lock();
3722 wined3d_device_uninit_3d(device->wined3d_device);
3723 wined3d_device_release_focus_window(device->wined3d_device);
3724 wined3d_device_decref(device->wined3d_device);
3725 wined3d_mutex_unlock();
3726 return E_OUTOFMEMORY;
3729 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3730 device->d3d_parent = parent;
3732 return D3D_OK;