wined3d: Use a separate STATE_VDECL state handler in the GLSL pipeline.
[wine/multimedia.git] / dlls / d3d9 / device.c
blobda462b1a6c69454fe61164d1ddae8bba8db13f81
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 WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
630 D3DPRESENT_PARAMETERS *present_parameters)
632 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
633 struct wined3d_swapchain_desc swapchain_desc;
634 HRESULT hr;
636 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
638 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
640 WARN("App not active, returning D3DERR_DEVICELOST.\n");
641 return D3DERR_DEVICELOST;
644 wined3d_mutex_lock();
646 if (device->vertex_buffer)
648 wined3d_buffer_decref(device->vertex_buffer);
649 device->vertex_buffer = NULL;
650 device->vertex_buffer_size = 0;
652 if (device->index_buffer)
654 wined3d_buffer_decref(device->index_buffer);
655 device->index_buffer = NULL;
656 device->index_buffer_size = 0;
659 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
660 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
661 NULL, reset_enum_callback, !device->d3d_parent->extended);
662 if (FAILED(hr) && !device->d3d_parent->extended)
663 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
664 else
665 device->device_state = D3D9_DEVICE_STATE_OK;
666 wined3d_mutex_unlock();
668 return hr;
671 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
672 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
674 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
675 HRESULT hr;
677 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
678 iface, src_rect, dst_rect, dst_window_override, dirty_region);
680 if (device->device_state != D3D9_DEVICE_STATE_OK)
681 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
683 wined3d_mutex_lock();
684 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
685 dst_window_override, dirty_region, 0);
686 wined3d_mutex_unlock();
688 return hr;
691 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
692 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
694 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
695 struct wined3d_surface *wined3d_surface = NULL;
696 struct d3d9_surface *surface_impl;
697 HRESULT hr;
699 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
700 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
702 wined3d_mutex_lock();
703 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
704 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
705 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
707 surface_impl = wined3d_surface_get_parent(wined3d_surface);
708 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
709 IDirect3DSurface9_AddRef(*backbuffer);
711 wined3d_mutex_unlock();
713 return hr;
715 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
716 UINT swapchain, D3DRASTER_STATUS *raster_status)
718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
719 HRESULT hr;
721 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
723 wined3d_mutex_lock();
724 hr = wined3d_device_get_raster_status(device->wined3d_device,
725 swapchain, (struct wined3d_raster_status *)raster_status);
726 wined3d_mutex_unlock();
728 return hr;
731 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
733 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
734 HRESULT hr;
736 TRACE("iface %p, enable %#x.\n", iface, enable);
738 wined3d_mutex_lock();
739 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
740 wined3d_mutex_unlock();
742 return hr;
745 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
746 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
750 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
752 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
753 wined3d_mutex_lock();
754 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
755 wined3d_mutex_unlock();
758 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
760 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
762 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
764 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
765 wined3d_mutex_lock();
766 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
767 wined3d_mutex_unlock();
770 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
771 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
772 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
774 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
775 struct d3d9_texture *object;
776 BOOL set_mem = FALSE;
777 HRESULT hr;
779 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
780 iface, width, height, levels, usage, format, pool, texture, shared_handle);
782 *texture = NULL;
783 if (shared_handle)
785 if (!device->d3d_parent->extended)
787 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
788 return E_NOTIMPL;
791 if (pool == D3DPOOL_SYSTEMMEM)
793 if (levels != 1)
794 return D3DERR_INVALIDCALL;
795 set_mem = TRUE;
797 else
799 if (pool != D3DPOOL_DEFAULT)
801 WARN("Trying to create a shared texture in pool %#x.\n", pool);
802 return D3DERR_INVALIDCALL;
804 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
808 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
809 if (!object)
810 return D3DERR_OUTOFVIDEOMEMORY;
812 hr = texture_init(object, device, width, height, levels, usage, format, pool);
813 if (FAILED(hr))
815 WARN("Failed to initialize texture, hr %#x.\n", hr);
816 HeapFree(GetProcessHeap(), 0, object);
817 return hr;
820 if (set_mem)
821 wined3d_texture_update_desc(object->wined3d_texture, width, height,
822 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
823 *shared_handle, 0);
825 TRACE("Created texture %p.\n", object);
826 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
828 return D3D_OK;
831 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
832 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
833 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
835 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
836 struct d3d9_texture *object;
837 HRESULT hr;
839 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
840 iface, width, height, depth, levels);
841 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
842 usage, format, pool, texture, shared_handle);
844 *texture = NULL;
845 if (shared_handle)
847 if (!device->d3d_parent->extended)
849 WARN("Trying to create a shared volume texture on a non-ex device.\n");
850 return E_NOTIMPL;
853 if (pool != D3DPOOL_DEFAULT)
855 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
856 return D3DERR_INVALIDCALL;
858 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
861 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
862 if (!object)
863 return D3DERR_OUTOFVIDEOMEMORY;
865 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
866 if (FAILED(hr))
868 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
869 HeapFree(GetProcessHeap(), 0, object);
870 return hr;
873 TRACE("Created volume texture %p.\n", object);
874 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
876 return D3D_OK;
879 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
880 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
881 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
883 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
884 struct d3d9_texture *object;
885 HRESULT hr;
887 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
888 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
890 *texture = NULL;
891 if (shared_handle)
893 if (!device->d3d_parent->extended)
895 WARN("Trying to create a shared cube texture on a non-ex device.\n");
896 return E_NOTIMPL;
899 if (pool != D3DPOOL_DEFAULT)
901 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
902 return D3DERR_INVALIDCALL;
904 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
907 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
908 if (!object)
909 return D3DERR_OUTOFVIDEOMEMORY;
911 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
912 if (FAILED(hr))
914 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
915 HeapFree(GetProcessHeap(), 0, object);
916 return hr;
919 TRACE("Created cube texture %p.\n", object);
920 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
922 return D3D_OK;
925 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
926 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
927 HANDLE *shared_handle)
929 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
930 struct d3d9_vertexbuffer *object;
931 HRESULT hr;
933 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
934 iface, size, usage, fvf, pool, buffer, shared_handle);
936 if (shared_handle)
938 if (!device->d3d_parent->extended)
940 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
941 return E_NOTIMPL;
944 if (pool != D3DPOOL_DEFAULT)
946 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
947 return D3DERR_NOTAVAILABLE;
949 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
952 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
953 if (!object)
954 return D3DERR_OUTOFVIDEOMEMORY;
956 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
957 if (FAILED(hr))
959 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
960 HeapFree(GetProcessHeap(), 0, object);
961 return hr;
964 TRACE("Created vertex buffer %p.\n", object);
965 *buffer = &object->IDirect3DVertexBuffer9_iface;
967 return D3D_OK;
970 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
971 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
972 HANDLE *shared_handle)
974 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
975 struct d3d9_indexbuffer *object;
976 HRESULT hr;
978 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
979 iface, size, usage, format, pool, buffer, shared_handle);
981 if (shared_handle)
983 if (!device->d3d_parent->extended)
985 WARN("Trying to create a shared index buffer on a non-ex device.\n");
986 return E_NOTIMPL;
989 if (pool != D3DPOOL_DEFAULT)
991 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
992 return D3DERR_NOTAVAILABLE;
994 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
997 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
998 if (!object)
999 return D3DERR_OUTOFVIDEOMEMORY;
1001 hr = indexbuffer_init(object, device, size, usage, format, pool);
1002 if (FAILED(hr))
1004 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1005 HeapFree(GetProcessHeap(), 0, object);
1006 return hr;
1009 TRACE("Created index buffer %p.\n", object);
1010 *buffer = &object->IDirect3DIndexBuffer9_iface;
1012 return D3D_OK;
1015 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1016 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1017 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1019 struct wined3d_resource *sub_resource;
1020 struct wined3d_resource_desc desc;
1021 struct d3d9_surface *surface_impl;
1022 struct wined3d_texture *texture;
1023 HRESULT hr;
1025 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1026 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1027 device, width, height, format, flags, surface, usage, pool,
1028 multisample_type, multisample_quality);
1030 desc.resource_type = WINED3D_RTYPE_TEXTURE;
1031 desc.format = wined3dformat_from_d3dformat(format);
1032 desc.multisample_type = multisample_type;
1033 desc.multisample_quality = multisample_quality;
1034 desc.usage = usage & WINED3DUSAGE_MASK;
1035 desc.pool = pool;
1036 desc.width = width;
1037 desc.height = height;
1038 desc.depth = 1;
1039 desc.size = 0;
1041 wined3d_mutex_lock();
1043 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1044 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1046 wined3d_mutex_unlock();
1047 WARN("Failed to create texture, hr %#x.\n", hr);
1048 return hr;
1051 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1052 surface_impl = wined3d_resource_get_parent(sub_resource);
1053 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1054 *surface = &surface_impl->IDirect3DSurface9_iface;
1055 IDirect3DSurface9_AddRef(*surface);
1057 if (user_mem)
1058 wined3d_texture_update_desc(texture, width, height,
1059 desc.format, multisample_type, multisample_quality, user_mem, 0);
1061 wined3d_texture_decref(texture);
1063 wined3d_mutex_unlock();
1065 return D3D_OK;
1068 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1069 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1070 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1072 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1073 DWORD flags = 0;
1075 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1076 "lockable %#x, surface %p, shared_handle %p.\n",
1077 iface, width, height, format, multisample_type, multisample_quality,
1078 lockable, surface, shared_handle);
1080 *surface = NULL;
1081 if (shared_handle)
1083 if (!device->d3d_parent->extended)
1085 WARN("Trying to create a shared render target on a non-ex device.\n");
1086 return E_NOTIMPL;
1089 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1092 if (lockable)
1093 flags |= WINED3D_SURFACE_MAPPABLE;
1095 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1096 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1099 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1100 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1101 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1103 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1104 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1106 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1107 "discard %#x, surface %p, shared_handle %p.\n",
1108 iface, width, height, format, multisample_type, multisample_quality,
1109 discard, surface, shared_handle);
1111 *surface = NULL;
1112 if (shared_handle)
1114 if (!device->d3d_parent->extended)
1116 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1117 return E_NOTIMPL;
1120 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1123 if (discard)
1124 flags |= WINED3D_SURFACE_DISCARD;
1126 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1127 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1131 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1132 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1133 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1135 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1136 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1137 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1138 HRESULT hr;
1140 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1141 iface, src_surface, src_rect, dst_surface, dst_point);
1143 wined3d_mutex_lock();
1144 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1145 dst->wined3d_surface, dst_point);
1146 wined3d_mutex_unlock();
1148 return hr;
1151 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1152 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1154 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1155 struct d3d9_texture *src_impl, *dst_impl;
1156 HRESULT hr;
1158 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1160 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1161 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1163 wined3d_mutex_lock();
1164 hr = wined3d_device_update_texture(device->wined3d_device,
1165 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1166 wined3d_mutex_unlock();
1168 return hr;
1171 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1172 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1174 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1175 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1176 HRESULT hr;
1178 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1180 wined3d_mutex_lock();
1181 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1182 wined3d_mutex_unlock();
1184 return hr;
1187 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1188 UINT swapchain, IDirect3DSurface9 *dst_surface)
1190 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1191 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1192 HRESULT hr;
1194 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1196 wined3d_mutex_lock();
1197 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1198 wined3d_mutex_unlock();
1200 return hr;
1203 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1204 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1206 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1207 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1208 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1209 HRESULT hr = D3DERR_INVALIDCALL;
1210 struct wined3d_resource_desc src_desc, dst_desc;
1211 struct wined3d_resource *wined3d_resource;
1213 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1214 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1216 wined3d_mutex_lock();
1217 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1218 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1220 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1221 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1223 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1225 if (device->in_scene)
1227 WARN("Rejecting depth / stencil blit while in scene.\n");
1228 goto done;
1231 if (src_rect)
1233 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1234 || src_rect->bottom != src_desc.height)
1236 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1237 wine_dbgstr_rect(src_rect));
1238 goto done;
1241 if (dst_rect)
1243 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1244 || dst_rect->bottom != dst_desc.height)
1246 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1247 wine_dbgstr_rect(dst_rect));
1248 goto done;
1251 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1253 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1254 goto done;
1258 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1259 if (hr == WINEDDERR_INVALIDRECT)
1260 hr = D3DERR_INVALIDCALL;
1262 done:
1263 wined3d_mutex_unlock();
1264 return hr;
1267 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1268 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1270 const struct wined3d_color c =
1272 ((color >> 16) & 0xff) / 255.0f,
1273 ((color >> 8) & 0xff) / 255.0f,
1274 (color & 0xff) / 255.0f,
1275 ((color >> 24) & 0xff) / 255.0f,
1277 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1278 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1279 struct wined3d_resource *wined3d_resource;
1280 struct wined3d_resource_desc desc;
1281 HRESULT hr;
1283 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1285 wined3d_mutex_lock();
1287 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1288 wined3d_resource_get_desc(wined3d_resource, &desc);
1290 if (desc.pool != WINED3D_POOL_DEFAULT)
1292 wined3d_mutex_unlock();
1293 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1294 return D3DERR_INVALIDCALL;
1296 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1298 wined3d_mutex_unlock();
1299 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1300 return D3DERR_INVALIDCALL;
1302 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1304 wined3d_mutex_unlock();
1305 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1306 return D3DERR_INVALIDCALL;
1309 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1310 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1312 wined3d_mutex_unlock();
1314 return hr;
1317 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1318 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1319 HANDLE *shared_handle)
1321 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1322 void *user_mem = NULL;
1324 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1325 iface, width, height, format, pool, surface, shared_handle);
1327 *surface = NULL;
1328 if (pool == D3DPOOL_MANAGED)
1330 WARN("Attempting to create a managed offscreen plain surface.\n");
1331 return D3DERR_INVALIDCALL;
1334 if (shared_handle)
1336 if (!device->d3d_parent->extended)
1338 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1339 return E_NOTIMPL;
1342 if (pool == D3DPOOL_SYSTEMMEM)
1343 user_mem = *shared_handle;
1344 else
1346 if (pool != D3DPOOL_DEFAULT)
1348 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1349 return D3DERR_INVALIDCALL;
1351 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1355 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1356 * regardless of the pool they're created in. Should we set dynamic usage
1357 * here? */
1358 return d3d9_device_create_surface(device, width, height, format,
1359 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1362 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1364 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1365 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1366 HRESULT hr;
1368 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1370 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1372 WARN("Invalid index %u specified.\n", idx);
1373 return D3DERR_INVALIDCALL;
1376 if (!idx && !surface_impl)
1378 WARN("Trying to set render target 0 to NULL.\n");
1379 return D3DERR_INVALIDCALL;
1382 wined3d_mutex_lock();
1383 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1384 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1385 wined3d_mutex_unlock();
1387 return hr;
1390 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1392 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1393 struct wined3d_rendertarget_view *wined3d_rtv;
1394 struct d3d9_surface *surface_impl;
1395 HRESULT hr = D3D_OK;
1397 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1399 if (!surface)
1400 return D3DERR_INVALIDCALL;
1402 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1404 WARN("Invalid index %u specified.\n", idx);
1405 return D3DERR_INVALIDCALL;
1408 wined3d_mutex_lock();
1409 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1411 /* We want the sub resource parent here, since the view itself may be
1412 * internal to wined3d and may not have a parent. */
1413 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1414 *surface = &surface_impl->IDirect3DSurface9_iface;
1415 IDirect3DSurface9_AddRef(*surface);
1417 else
1419 hr = WINED3DERR_NOTFOUND;
1420 *surface = NULL;
1422 wined3d_mutex_unlock();
1424 return hr;
1427 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1429 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1430 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1432 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1434 wined3d_mutex_lock();
1435 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1436 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1437 wined3d_mutex_unlock();
1439 return D3D_OK;
1442 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1444 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1445 struct wined3d_rendertarget_view *wined3d_dsv;
1446 struct d3d9_surface *surface_impl;
1447 HRESULT hr = D3D_OK;
1449 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1451 if (!depth_stencil)
1452 return D3DERR_INVALIDCALL;
1454 wined3d_mutex_lock();
1455 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1457 /* We want the sub resource parent here, since the view itself may be
1458 * internal to wined3d and may not have a parent. */
1459 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1460 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1461 IDirect3DSurface9_AddRef(*depth_stencil);
1463 else
1465 hr = WINED3DERR_NOTFOUND;
1466 *depth_stencil = NULL;
1468 wined3d_mutex_unlock();
1470 return hr;
1473 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1475 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1476 HRESULT hr;
1478 TRACE("iface %p.\n", iface);
1480 wined3d_mutex_lock();
1481 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1482 device->in_scene = TRUE;
1483 wined3d_mutex_unlock();
1485 return hr;
1488 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1490 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1491 HRESULT hr;
1493 TRACE("iface %p.\n", iface);
1495 wined3d_mutex_lock();
1496 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1497 device->in_scene = FALSE;
1498 wined3d_mutex_unlock();
1500 return hr;
1503 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1504 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1506 const struct wined3d_color c =
1508 ((color >> 16) & 0xff) / 255.0f,
1509 ((color >> 8) & 0xff) / 255.0f,
1510 (color & 0xff) / 255.0f,
1511 ((color >> 24) & 0xff) / 255.0f,
1513 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1514 HRESULT hr;
1516 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1517 iface, rect_count, rects, flags, color, z, stencil);
1519 wined3d_mutex_lock();
1520 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1521 wined3d_mutex_unlock();
1523 return hr;
1526 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1527 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1529 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1531 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1533 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1534 wined3d_mutex_lock();
1535 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1536 wined3d_mutex_unlock();
1538 return D3D_OK;
1541 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1542 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1544 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1546 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1548 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1549 wined3d_mutex_lock();
1550 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1551 wined3d_mutex_unlock();
1553 return D3D_OK;
1556 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1557 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1559 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1561 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1563 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1564 wined3d_mutex_lock();
1565 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1566 wined3d_mutex_unlock();
1568 return D3D_OK;
1571 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1573 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1575 TRACE("iface %p, viewport %p.\n", iface, viewport);
1577 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1578 wined3d_mutex_lock();
1579 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1580 wined3d_mutex_unlock();
1582 return D3D_OK;
1585 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1587 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1589 TRACE("iface %p, viewport %p.\n", iface, viewport);
1591 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1592 wined3d_mutex_lock();
1593 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1594 wined3d_mutex_unlock();
1596 return D3D_OK;
1599 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1601 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1603 TRACE("iface %p, material %p.\n", iface, material);
1605 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1606 wined3d_mutex_lock();
1607 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1608 wined3d_mutex_unlock();
1610 return D3D_OK;
1613 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1615 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1617 TRACE("iface %p, material %p.\n", iface, material);
1619 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1620 wined3d_mutex_lock();
1621 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1622 wined3d_mutex_unlock();
1624 return D3D_OK;
1627 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1630 HRESULT hr;
1632 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1634 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1635 wined3d_mutex_lock();
1636 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1637 wined3d_mutex_unlock();
1639 return hr;
1642 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1644 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1645 HRESULT hr;
1647 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1649 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1650 wined3d_mutex_lock();
1651 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1652 wined3d_mutex_unlock();
1654 return hr;
1657 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1659 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1660 HRESULT hr;
1662 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1664 wined3d_mutex_lock();
1665 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1666 wined3d_mutex_unlock();
1668 return hr;
1671 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1673 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1674 HRESULT hr;
1676 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1678 wined3d_mutex_lock();
1679 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1680 wined3d_mutex_unlock();
1682 return hr;
1685 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1687 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1688 HRESULT hr;
1690 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1692 wined3d_mutex_lock();
1693 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1694 wined3d_mutex_unlock();
1696 return hr;
1699 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1701 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1702 HRESULT hr;
1704 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1706 wined3d_mutex_lock();
1707 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1708 wined3d_mutex_unlock();
1710 return hr;
1713 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1714 D3DRENDERSTATETYPE state, DWORD value)
1716 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1718 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1720 wined3d_mutex_lock();
1721 wined3d_device_set_render_state(device->wined3d_device, state, value);
1722 wined3d_mutex_unlock();
1724 return D3D_OK;
1727 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1728 D3DRENDERSTATETYPE state, DWORD *value)
1730 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1732 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1734 wined3d_mutex_lock();
1735 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1736 wined3d_mutex_unlock();
1738 return D3D_OK;
1741 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1742 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1744 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1745 struct d3d9_stateblock *object;
1746 HRESULT hr;
1748 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1750 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1752 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1753 return D3DERR_INVALIDCALL;
1756 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1757 if (!object)
1758 return E_OUTOFMEMORY;
1760 hr = stateblock_init(object, device, type, NULL);
1761 if (FAILED(hr))
1763 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1764 HeapFree(GetProcessHeap(), 0, object);
1765 return hr;
1768 TRACE("Created stateblock %p.\n", object);
1769 *stateblock = &object->IDirect3DStateBlock9_iface;
1771 return D3D_OK;
1774 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1776 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1777 HRESULT hr;
1779 TRACE("iface %p.\n", iface);
1781 wined3d_mutex_lock();
1782 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1783 wined3d_mutex_unlock();
1785 return hr;
1788 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1790 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1791 struct wined3d_stateblock *wined3d_stateblock;
1792 struct d3d9_stateblock *object;
1793 HRESULT hr;
1795 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1797 wined3d_mutex_lock();
1798 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1799 wined3d_mutex_unlock();
1800 if (FAILED(hr))
1802 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1803 return hr;
1806 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1807 if (!object)
1809 wined3d_mutex_lock();
1810 wined3d_stateblock_decref(wined3d_stateblock);
1811 wined3d_mutex_unlock();
1812 return E_OUTOFMEMORY;
1815 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1816 if (FAILED(hr))
1818 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1819 wined3d_mutex_lock();
1820 wined3d_stateblock_decref(wined3d_stateblock);
1821 wined3d_mutex_unlock();
1822 HeapFree(GetProcessHeap(), 0, object);
1823 return hr;
1826 TRACE("Created stateblock %p.\n", object);
1827 *stateblock = &object->IDirect3DStateBlock9_iface;
1829 return D3D_OK;
1832 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1834 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1835 HRESULT hr;
1837 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1839 wined3d_mutex_lock();
1840 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1841 wined3d_mutex_unlock();
1843 return hr;
1846 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1848 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1849 HRESULT hr;
1851 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1853 wined3d_mutex_lock();
1854 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1855 wined3d_mutex_unlock();
1857 return hr;
1860 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1862 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1863 struct wined3d_texture *wined3d_texture = NULL;
1864 struct d3d9_texture *texture_impl;
1866 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1868 if (!texture)
1869 return D3DERR_INVALIDCALL;
1871 wined3d_mutex_lock();
1872 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1874 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1875 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1876 IDirect3DBaseTexture9_AddRef(*texture);
1878 else
1880 *texture = NULL;
1882 wined3d_mutex_unlock();
1884 return D3D_OK;
1887 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1889 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1890 struct d3d9_texture *texture_impl;
1891 HRESULT hr;
1893 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1895 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1897 wined3d_mutex_lock();
1898 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1899 texture_impl ? texture_impl->wined3d_texture : NULL);
1900 wined3d_mutex_unlock();
1902 return hr;
1905 static const enum wined3d_texture_stage_state tss_lookup[] =
1907 WINED3D_TSS_INVALID, /* 0, unused */
1908 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1909 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1910 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1911 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1912 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1913 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1914 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1915 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1916 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1917 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1918 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1919 WINED3D_TSS_INVALID, /* 12, unused */
1920 WINED3D_TSS_INVALID, /* 13, unused */
1921 WINED3D_TSS_INVALID, /* 14, unused */
1922 WINED3D_TSS_INVALID, /* 15, unused */
1923 WINED3D_TSS_INVALID, /* 16, unused */
1924 WINED3D_TSS_INVALID, /* 17, unused */
1925 WINED3D_TSS_INVALID, /* 18, unused */
1926 WINED3D_TSS_INVALID, /* 19, unused */
1927 WINED3D_TSS_INVALID, /* 20, unused */
1928 WINED3D_TSS_INVALID, /* 21, unused */
1929 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1930 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1931 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1932 WINED3D_TSS_INVALID, /* 25, unused */
1933 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1934 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1935 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1936 WINED3D_TSS_INVALID, /* 29, unused */
1937 WINED3D_TSS_INVALID, /* 30, unused */
1938 WINED3D_TSS_INVALID, /* 31, unused */
1939 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1942 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1943 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1945 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1947 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1949 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1951 WARN("Invalid state %#x passed.\n", state);
1952 return D3D_OK;
1955 wined3d_mutex_lock();
1956 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
1957 wined3d_mutex_unlock();
1959 return D3D_OK;
1962 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1963 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1965 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1967 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
1969 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1971 WARN("Invalid state %#x passed.\n", state);
1972 return D3D_OK;
1975 wined3d_mutex_lock();
1976 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1977 wined3d_mutex_unlock();
1979 return D3D_OK;
1982 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
1983 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
1985 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1987 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
1989 wined3d_mutex_lock();
1990 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
1991 wined3d_mutex_unlock();
1993 return D3D_OK;
1996 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
1997 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
1999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2001 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2003 wined3d_mutex_lock();
2004 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2005 wined3d_mutex_unlock();
2007 return D3D_OK;
2010 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2012 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2013 HRESULT hr;
2015 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2017 wined3d_mutex_lock();
2018 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2019 wined3d_mutex_unlock();
2021 return hr;
2024 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2025 UINT palette_idx, const PALETTEENTRY *entries)
2027 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2029 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2030 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2031 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2033 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2034 return D3D_OK;
2037 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2038 UINT palette_idx, PALETTEENTRY *entries)
2040 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2042 return D3DERR_INVALIDCALL;
2045 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2047 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2049 return D3D_OK;
2052 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2054 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2056 return D3DERR_INVALIDCALL;
2059 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2061 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2063 TRACE("iface %p, rect %p.\n", iface, rect);
2065 wined3d_mutex_lock();
2066 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2067 wined3d_mutex_unlock();
2069 return D3D_OK;
2072 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2074 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2076 TRACE("iface %p, rect %p.\n", iface, rect);
2078 wined3d_mutex_lock();
2079 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2080 wined3d_mutex_unlock();
2082 return D3D_OK;
2085 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2087 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2089 TRACE("iface %p, software %#x.\n", iface, software);
2091 wined3d_mutex_lock();
2092 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2093 wined3d_mutex_unlock();
2095 return D3D_OK;
2098 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2100 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2101 BOOL ret;
2103 TRACE("iface %p.\n", iface);
2105 wined3d_mutex_lock();
2106 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2107 wined3d_mutex_unlock();
2109 return ret;
2112 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2114 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2115 HRESULT hr;
2117 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2119 wined3d_mutex_lock();
2120 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2121 wined3d_mutex_unlock();
2123 return hr;
2126 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2128 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2129 float ret;
2131 TRACE("iface %p.\n", iface);
2133 wined3d_mutex_lock();
2134 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2135 wined3d_mutex_unlock();
2137 return ret;
2140 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2141 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2143 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2144 HRESULT hr;
2146 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2147 iface, primitive_type, start_vertex, primitive_count);
2149 wined3d_mutex_lock();
2150 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2151 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2152 vertex_count_from_primitive_count(primitive_type, primitive_count));
2153 wined3d_mutex_unlock();
2155 return hr;
2158 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2159 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2160 UINT vertex_count, UINT start_idx, UINT primitive_count)
2162 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2163 HRESULT hr;
2165 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2166 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2167 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2168 vertex_count, start_idx, primitive_count);
2170 wined3d_mutex_lock();
2171 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2172 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2173 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2174 vertex_count_from_primitive_count(primitive_type, primitive_count));
2175 wined3d_mutex_unlock();
2177 return hr;
2180 /* The caller is responsible for wined3d locking */
2181 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2183 HRESULT hr;
2185 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2187 UINT size = max(device->vertex_buffer_size * 2, min_size);
2188 struct wined3d_buffer *buffer;
2190 TRACE("Growing vertex buffer to %u bytes\n", size);
2192 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2193 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2194 if (FAILED(hr))
2196 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2197 return hr;
2200 if (device->vertex_buffer)
2201 wined3d_buffer_decref(device->vertex_buffer);
2203 device->vertex_buffer = buffer;
2204 device->vertex_buffer_size = size;
2205 device->vertex_buffer_pos = 0;
2207 return D3D_OK;
2210 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2211 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2213 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2214 HRESULT hr;
2215 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2216 UINT size = vtx_count * stride;
2217 UINT vb_pos, align;
2218 BYTE *buffer_data;
2220 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2221 iface, primitive_type, primitive_count, data, stride);
2223 if (!primitive_count)
2225 WARN("primitive_count is 0, returning D3D_OK\n");
2226 return D3D_OK;
2229 wined3d_mutex_lock();
2231 hr = d3d9_device_prepare_vertex_buffer(device, size);
2232 if (FAILED(hr))
2233 goto done;
2235 vb_pos = device->vertex_buffer_pos;
2236 align = vb_pos % stride;
2237 if (align) align = stride - align;
2238 if (vb_pos + size + align > device->vertex_buffer_size)
2239 vb_pos = 0;
2240 else
2241 vb_pos += align;
2243 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2244 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2245 if (FAILED(hr))
2246 goto done;
2247 memcpy(buffer_data, data, size);
2248 wined3d_buffer_unmap(device->vertex_buffer);
2249 device->vertex_buffer_pos = vb_pos + size;
2251 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2252 if (FAILED(hr))
2253 goto done;
2255 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2256 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2257 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2259 done:
2260 wined3d_mutex_unlock();
2261 return hr;
2264 /* The caller is responsible for wined3d locking */
2265 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2267 HRESULT hr;
2269 if (device->index_buffer_size < min_size || !device->index_buffer)
2271 UINT size = max(device->index_buffer_size * 2, min_size);
2272 struct wined3d_buffer *buffer;
2274 TRACE("Growing index buffer to %u bytes\n", size);
2276 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2277 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2278 if (FAILED(hr))
2280 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2281 return hr;
2284 if (device->index_buffer)
2285 wined3d_buffer_decref(device->index_buffer);
2287 device->index_buffer = buffer;
2288 device->index_buffer_size = size;
2289 device->index_buffer_pos = 0;
2291 return D3D_OK;
2294 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2295 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2296 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2297 const void *vertex_data, UINT vertex_stride)
2299 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2300 HRESULT hr;
2301 BYTE *buffer_data;
2303 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2304 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2305 UINT idx_size = idx_count * idx_fmt_size;
2306 UINT ib_pos;
2308 UINT vtx_size = vertex_count * vertex_stride;
2309 UINT vb_pos, align;
2311 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2312 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2313 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2314 index_data, index_format, vertex_data, vertex_stride);
2316 if (!primitive_count)
2318 WARN("primitive_count is 0, returning D3D_OK\n");
2319 return D3D_OK;
2322 wined3d_mutex_lock();
2324 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2325 if (FAILED(hr))
2326 goto done;
2328 vb_pos = device->vertex_buffer_pos;
2329 align = vb_pos % vertex_stride;
2330 if (align) align = vertex_stride - align;
2331 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2332 vb_pos = 0;
2333 else
2334 vb_pos += align;
2336 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2337 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2338 if (FAILED(hr))
2339 goto done;
2340 memcpy(buffer_data, vertex_data, vtx_size);
2341 wined3d_buffer_unmap(device->vertex_buffer);
2342 device->vertex_buffer_pos = vb_pos + vtx_size;
2344 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2345 if (FAILED(hr))
2346 goto done;
2348 ib_pos = device->index_buffer_pos;
2349 align = ib_pos % idx_fmt_size;
2350 if (align) align = idx_fmt_size - align;
2351 if (ib_pos + idx_size + align > device->index_buffer_size)
2352 ib_pos = 0;
2353 else
2354 ib_pos += align;
2356 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2357 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2358 if (FAILED(hr))
2359 goto done;
2360 memcpy(buffer_data, index_data, idx_size);
2361 wined3d_buffer_unmap(device->index_buffer);
2362 device->index_buffer_pos = ib_pos + idx_size;
2364 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2365 if (FAILED(hr))
2366 goto done;
2368 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2369 wined3dformat_from_d3dformat(index_format));
2370 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2372 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2373 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2375 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2376 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2377 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2379 done:
2380 wined3d_mutex_unlock();
2381 return hr;
2384 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2385 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2386 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2388 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2389 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2390 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2391 HRESULT hr;
2393 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2394 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2396 wined3d_mutex_lock();
2397 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2398 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2399 flags, dst_impl->fvf);
2400 wined3d_mutex_unlock();
2402 return hr;
2405 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2406 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2408 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2409 struct d3d9_vertex_declaration *object;
2410 HRESULT hr;
2412 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2414 if (!declaration)
2416 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2417 return D3DERR_INVALIDCALL;
2420 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2421 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2423 return hr;
2426 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2427 IDirect3DVertexDeclaration9 *declaration)
2429 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2430 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2432 TRACE("iface %p, declaration %p.\n", iface, declaration);
2434 wined3d_mutex_lock();
2435 wined3d_device_set_vertex_declaration(device->wined3d_device,
2436 decl_impl ? decl_impl->wined3d_declaration : NULL);
2437 wined3d_mutex_unlock();
2439 return D3D_OK;
2442 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2443 IDirect3DVertexDeclaration9 **declaration)
2445 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2446 struct wined3d_vertex_declaration *wined3d_declaration;
2447 struct d3d9_vertex_declaration *declaration_impl;
2449 TRACE("iface %p, declaration %p.\n", iface, declaration);
2451 if (!declaration) return D3DERR_INVALIDCALL;
2453 wined3d_mutex_lock();
2454 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2456 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2457 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2458 IDirect3DVertexDeclaration9_AddRef(*declaration);
2460 else
2462 *declaration = NULL;
2464 wined3d_mutex_unlock();
2466 TRACE("Returning %p.\n", *declaration);
2467 return D3D_OK;
2470 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2472 struct wined3d_vertex_declaration *wined3d_declaration;
2473 struct fvf_declaration *fvf_decls = device->fvf_decls;
2474 struct d3d9_vertex_declaration *d3d9_declaration;
2475 D3DVERTEXELEMENT9 *elements;
2476 int p, low, high; /* deliberately signed */
2477 HRESULT hr;
2479 TRACE("Searching for declaration for fvf %08x... ", fvf);
2481 low = 0;
2482 high = device->fvf_decl_count - 1;
2483 while (low <= high)
2485 p = (low + high) >> 1;
2486 TRACE("%d ", p);
2488 if (fvf_decls[p].fvf == fvf)
2490 TRACE("found %p.\n", fvf_decls[p].decl);
2491 return fvf_decls[p].decl;
2494 if (fvf_decls[p].fvf < fvf)
2495 low = p + 1;
2496 else
2497 high = p - 1;
2499 TRACE("not found. Creating and inserting at position %d.\n", low);
2501 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2502 return NULL;
2504 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2505 HeapFree(GetProcessHeap(), 0, elements);
2506 if (FAILED(hr))
2507 return NULL;
2509 if (device->fvf_decl_size == device->fvf_decl_count)
2511 UINT grow = max(device->fvf_decl_size / 2, 8);
2513 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2514 if (!fvf_decls)
2516 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2517 return NULL;
2519 device->fvf_decls = fvf_decls;
2520 device->fvf_decl_size += grow;
2523 d3d9_declaration->fvf = fvf;
2524 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2525 wined3d_vertex_declaration_incref(wined3d_declaration);
2526 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2528 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2529 fvf_decls[low].decl = wined3d_declaration;
2530 fvf_decls[low].fvf = fvf;
2531 ++device->fvf_decl_count;
2533 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2535 return wined3d_declaration;
2538 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2540 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2541 struct wined3d_vertex_declaration *decl;
2543 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2545 if (!fvf)
2547 WARN("%#x is not a valid FVF.\n", fvf);
2548 return D3D_OK;
2551 wined3d_mutex_lock();
2552 if (!(decl = device_get_fvf_declaration(device, fvf)))
2554 wined3d_mutex_unlock();
2555 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2556 return D3DERR_DRIVERINTERNALERROR;
2559 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2560 wined3d_mutex_unlock();
2562 return D3D_OK;
2565 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2567 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2568 struct wined3d_vertex_declaration *wined3d_declaration;
2569 struct d3d9_vertex_declaration *d3d9_declaration;
2571 TRACE("iface %p, fvf %p.\n", iface, fvf);
2573 wined3d_mutex_lock();
2574 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2576 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2577 *fvf = d3d9_declaration->fvf;
2579 else
2581 *fvf = 0;
2583 wined3d_mutex_unlock();
2585 TRACE("Returning FVF %#x.\n", *fvf);
2587 return D3D_OK;
2590 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2591 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2593 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2594 struct d3d9_vertexshader *object;
2595 HRESULT hr;
2597 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2599 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2600 if (!object)
2601 return E_OUTOFMEMORY;
2603 hr = vertexshader_init(object, device, byte_code);
2604 if (FAILED(hr))
2606 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2607 HeapFree(GetProcessHeap(), 0, object);
2608 return hr;
2611 TRACE("Created vertex shader %p.\n", object);
2612 *shader = &object->IDirect3DVertexShader9_iface;
2614 return D3D_OK;
2617 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2619 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2620 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2622 TRACE("iface %p, shader %p.\n", iface, shader);
2624 wined3d_mutex_lock();
2625 wined3d_device_set_vertex_shader(device->wined3d_device,
2626 shader_obj ? shader_obj->wined3d_shader : NULL);
2627 wined3d_mutex_unlock();
2629 return D3D_OK;
2632 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2634 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2635 struct d3d9_vertexshader *shader_impl;
2636 struct wined3d_shader *wined3d_shader;
2638 TRACE("iface %p, shader %p.\n", iface, shader);
2640 wined3d_mutex_lock();
2641 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2643 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2644 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2645 IDirect3DVertexShader9_AddRef(*shader);
2647 else
2649 *shader = NULL;
2651 wined3d_mutex_unlock();
2653 TRACE("Returning %p.\n", *shader);
2655 return D3D_OK;
2658 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2659 UINT reg_idx, const float *data, UINT count)
2661 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2662 HRESULT hr;
2664 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2666 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2668 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2669 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2670 return D3DERR_INVALIDCALL;
2673 wined3d_mutex_lock();
2674 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2675 wined3d_mutex_unlock();
2677 return hr;
2680 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2681 UINT reg_idx, float *data, UINT count)
2683 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2684 HRESULT hr;
2686 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2688 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2690 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2691 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2692 return D3DERR_INVALIDCALL;
2695 wined3d_mutex_lock();
2696 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2697 wined3d_mutex_unlock();
2699 return hr;
2702 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2703 UINT reg_idx, const int *data, UINT count)
2705 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2706 HRESULT hr;
2708 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2710 wined3d_mutex_lock();
2711 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2712 wined3d_mutex_unlock();
2714 return hr;
2717 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2718 UINT reg_idx, int *data, UINT count)
2720 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2721 HRESULT hr;
2723 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2725 wined3d_mutex_lock();
2726 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2727 wined3d_mutex_unlock();
2729 return hr;
2732 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2733 UINT reg_idx, const BOOL *data, UINT count)
2735 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2736 HRESULT hr;
2738 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2740 wined3d_mutex_lock();
2741 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2742 wined3d_mutex_unlock();
2744 return hr;
2747 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2748 UINT reg_idx, BOOL *data, UINT count)
2750 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2751 HRESULT hr;
2753 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2755 wined3d_mutex_lock();
2756 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2757 wined3d_mutex_unlock();
2759 return hr;
2762 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2763 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2765 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2766 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2767 HRESULT hr;
2769 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2770 iface, stream_idx, buffer, offset, stride);
2772 wined3d_mutex_lock();
2773 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2774 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2775 wined3d_mutex_unlock();
2777 return hr;
2780 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2781 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2783 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2784 struct d3d9_vertexbuffer *buffer_impl;
2785 struct wined3d_buffer *wined3d_buffer;
2786 HRESULT hr;
2788 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2789 iface, stream_idx, buffer, offset, stride);
2791 if (!buffer)
2792 return D3DERR_INVALIDCALL;
2794 wined3d_mutex_lock();
2795 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2796 if (SUCCEEDED(hr) && wined3d_buffer)
2798 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2799 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2800 IDirect3DVertexBuffer9_AddRef(*buffer);
2802 else
2804 if (FAILED(hr))
2805 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2806 *buffer = NULL;
2808 wined3d_mutex_unlock();
2810 return hr;
2813 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2815 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2816 HRESULT hr;
2818 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2820 wined3d_mutex_lock();
2821 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2822 wined3d_mutex_unlock();
2824 return hr;
2827 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2829 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2830 HRESULT hr;
2832 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2834 wined3d_mutex_lock();
2835 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2836 wined3d_mutex_unlock();
2838 return hr;
2841 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2843 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2844 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2846 TRACE("iface %p, buffer %p.\n", iface, buffer);
2848 wined3d_mutex_lock();
2849 wined3d_device_set_index_buffer(device->wined3d_device,
2850 ib ? ib->wined3d_buffer : NULL,
2851 ib ? ib->format : WINED3DFMT_UNKNOWN);
2852 wined3d_mutex_unlock();
2854 return D3D_OK;
2857 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2859 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2860 enum wined3d_format_id wined3d_format;
2861 struct wined3d_buffer *wined3d_buffer;
2862 struct d3d9_indexbuffer *buffer_impl;
2864 TRACE("iface %p, buffer %p.\n", iface, buffer);
2866 if (!buffer)
2867 return D3DERR_INVALIDCALL;
2869 wined3d_mutex_lock();
2870 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2872 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2873 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2874 IDirect3DIndexBuffer9_AddRef(*buffer);
2876 else
2878 *buffer = NULL;
2880 wined3d_mutex_unlock();
2882 return D3D_OK;
2885 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2886 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2888 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2889 struct d3d9_pixelshader *object;
2890 HRESULT hr;
2892 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2894 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2895 if (!object)
2897 FIXME("Failed to allocate pixel shader memory.\n");
2898 return E_OUTOFMEMORY;
2901 hr = pixelshader_init(object, device, byte_code);
2902 if (FAILED(hr))
2904 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2905 HeapFree(GetProcessHeap(), 0, object);
2906 return hr;
2909 TRACE("Created pixel shader %p.\n", object);
2910 *shader = &object->IDirect3DPixelShader9_iface;
2912 return D3D_OK;
2915 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2917 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2918 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2920 TRACE("iface %p, shader %p.\n", iface, shader);
2922 wined3d_mutex_lock();
2923 wined3d_device_set_pixel_shader(device->wined3d_device,
2924 shader_obj ? shader_obj->wined3d_shader : NULL);
2925 wined3d_mutex_unlock();
2927 return D3D_OK;
2930 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2932 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2933 struct d3d9_pixelshader *shader_impl;
2934 struct wined3d_shader *wined3d_shader;
2936 TRACE("iface %p, shader %p.\n", iface, shader);
2938 if (!shader) return D3DERR_INVALIDCALL;
2940 wined3d_mutex_lock();
2941 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2943 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2944 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2945 IDirect3DPixelShader9_AddRef(*shader);
2947 else
2949 *shader = NULL;
2951 wined3d_mutex_unlock();
2953 TRACE("Returning %p.\n", *shader);
2955 return D3D_OK;
2958 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2959 UINT reg_idx, const float *data, UINT count)
2961 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2962 HRESULT hr;
2964 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2966 wined3d_mutex_lock();
2967 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2968 wined3d_mutex_unlock();
2970 return hr;
2973 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2974 UINT reg_idx, float *data, UINT count)
2976 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2977 HRESULT hr;
2979 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2981 wined3d_mutex_lock();
2982 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2983 wined3d_mutex_unlock();
2985 return hr;
2988 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2989 UINT reg_idx, const int *data, UINT count)
2991 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2992 HRESULT hr;
2994 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2996 wined3d_mutex_lock();
2997 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2998 wined3d_mutex_unlock();
3000 return hr;
3003 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3004 UINT reg_idx, int *data, UINT count)
3006 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3007 HRESULT hr;
3009 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3011 wined3d_mutex_lock();
3012 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
3013 wined3d_mutex_unlock();
3015 return hr;
3018 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3019 UINT reg_idx, const BOOL *data, UINT count)
3021 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3022 HRESULT hr;
3024 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3026 wined3d_mutex_lock();
3027 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3028 wined3d_mutex_unlock();
3030 return hr;
3033 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3034 UINT reg_idx, BOOL *data, UINT count)
3036 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3037 HRESULT hr;
3039 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3041 wined3d_mutex_lock();
3042 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3043 wined3d_mutex_unlock();
3045 return hr;
3048 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3049 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3051 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3052 iface, handle, segment_count, patch_info);
3053 return D3D_OK;
3056 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3057 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3059 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3060 iface, handle, segment_count, patch_info);
3061 return D3D_OK;
3064 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3066 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3067 return D3DERR_INVALIDCALL;
3070 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3072 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3073 struct d3d9_query *object;
3074 HRESULT hr;
3076 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3078 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3079 if (!object)
3080 return E_OUTOFMEMORY;
3082 hr = query_init(object, device, type);
3083 if (FAILED(hr))
3085 WARN("Failed to initialize query, hr %#x.\n", hr);
3086 HeapFree(GetProcessHeap(), 0, object);
3087 return hr;
3090 TRACE("Created query %p.\n", object);
3091 if (query) *query = &object->IDirect3DQuery9_iface;
3092 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3094 return D3D_OK;
3097 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3098 UINT width, UINT height, float *rows, float *columns)
3100 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3101 iface, width, height, rows, columns);
3103 return E_NOTIMPL;
3106 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3107 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3108 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3110 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3111 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3112 iface, src_surface, dst_surface, src_descs, rect_count,
3113 dst_descs, operation, offset_x, offset_y);
3115 return E_NOTIMPL;
3118 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3119 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3120 const RGNDATA *dirty_region, DWORD flags)
3122 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3123 HRESULT hr;
3125 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3126 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3127 dst_window_override, dirty_region, flags);
3129 if (device->device_state != D3D9_DEVICE_STATE_OK)
3130 return S_PRESENT_OCCLUDED;
3132 wined3d_mutex_lock();
3133 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
3134 dst_window_override, dirty_region, flags);
3135 wined3d_mutex_unlock();
3137 return hr;
3140 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3142 FIXME("iface %p, priority %p stub!\n", iface, priority);
3144 return E_NOTIMPL;
3147 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3149 FIXME("iface %p, priority %d stub!\n", iface, priority);
3151 return E_NOTIMPL;
3154 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3156 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3158 return E_NOTIMPL;
3161 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3162 IDirect3DResource9 **resources, UINT32 resource_count)
3164 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3165 iface, resources, resource_count);
3167 return E_NOTIMPL;
3170 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3172 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3174 return E_NOTIMPL;
3177 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3179 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3181 *max_latency = 2;
3183 return E_NOTIMPL;
3186 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3188 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3189 struct wined3d_swapchain_desc swapchain_desc;
3190 struct wined3d_swapchain *swapchain;
3192 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3194 wined3d_mutex_lock();
3195 swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3196 wined3d_swapchain_get_desc(swapchain, &swapchain_desc);
3197 wined3d_mutex_unlock();
3199 if (swapchain_desc.windowed)
3200 return D3D_OK;
3202 /* FIXME: This is actually supposed to check if any other device is in
3203 * fullscreen mode. */
3204 if (dst_window != swapchain_desc.device_window)
3205 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3207 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3210 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3211 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3212 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3214 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3215 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3216 iface, width, height, format, multisample_type, multisample_quality,
3217 lockable, surface, shared_handle, usage);
3219 *surface = NULL;
3220 if (shared_handle)
3221 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3223 return E_NOTIMPL;
3226 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3227 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3228 HANDLE *shared_handle, DWORD usage)
3230 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3231 iface, width, height, format, pool, surface, shared_handle, usage);
3233 return E_NOTIMPL;
3236 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3237 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3238 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3240 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3241 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3242 iface, width, height, format, multisample_type, multisample_quality,
3243 discard, surface, shared_handle, usage);
3245 *surface = NULL;
3246 if (shared_handle)
3247 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3249 return E_NOTIMPL;
3252 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3253 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3255 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3256 struct wined3d_swapchain_desc swapchain_desc;
3257 struct wined3d_display_mode wined3d_mode;
3258 HRESULT hr;
3260 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3262 if (mode)
3264 wined3d_mode.width = mode->Width;
3265 wined3d_mode.height = mode->Height;
3266 wined3d_mode.refresh_rate = mode->RefreshRate;
3267 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
3268 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
3271 wined3d_mutex_lock();
3273 if (device->vertex_buffer)
3275 wined3d_buffer_decref(device->vertex_buffer);
3276 device->vertex_buffer = NULL;
3277 device->vertex_buffer_size = 0;
3279 if (device->index_buffer)
3281 wined3d_buffer_decref(device->index_buffer);
3282 device->index_buffer = NULL;
3283 device->index_buffer_size = 0;
3286 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
3287 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
3288 mode ? &wined3d_mode : NULL, reset_enum_callback, FALSE);
3289 wined3d_mutex_unlock();
3291 return hr;
3294 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3295 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3297 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3298 struct wined3d_display_mode wined3d_mode;
3299 HRESULT hr;
3301 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3302 iface, swapchain_idx, mode, rotation);
3304 if (mode->Size != sizeof(*mode))
3305 return D3DERR_INVALIDCALL;
3307 wined3d_mutex_lock();
3308 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3309 (enum wined3d_display_rotation *)rotation);
3310 wined3d_mutex_unlock();
3312 if (SUCCEEDED(hr))
3314 mode->Width = wined3d_mode.width;
3315 mode->Height = wined3d_mode.height;
3316 mode->RefreshRate = wined3d_mode.refresh_rate;
3317 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3318 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3321 return hr;
3324 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3326 /* IUnknown */
3327 d3d9_device_QueryInterface,
3328 d3d9_device_AddRef,
3329 d3d9_device_Release,
3330 /* IDirect3DDevice9 */
3331 d3d9_device_TestCooperativeLevel,
3332 d3d9_device_GetAvailableTextureMem,
3333 d3d9_device_EvictManagedResources,
3334 d3d9_device_GetDirect3D,
3335 d3d9_device_GetDeviceCaps,
3336 d3d9_device_GetDisplayMode,
3337 d3d9_device_GetCreationParameters,
3338 d3d9_device_SetCursorProperties,
3339 d3d9_device_SetCursorPosition,
3340 d3d9_device_ShowCursor,
3341 d3d9_device_CreateAdditionalSwapChain,
3342 d3d9_device_GetSwapChain,
3343 d3d9_device_GetNumberOfSwapChains,
3344 d3d9_device_Reset,
3345 d3d9_device_Present,
3346 d3d9_device_GetBackBuffer,
3347 d3d9_device_GetRasterStatus,
3348 d3d9_device_SetDialogBoxMode,
3349 d3d9_device_SetGammaRamp,
3350 d3d9_device_GetGammaRamp,
3351 d3d9_device_CreateTexture,
3352 d3d9_device_CreateVolumeTexture,
3353 d3d9_device_CreateCubeTexture,
3354 d3d9_device_CreateVertexBuffer,
3355 d3d9_device_CreateIndexBuffer,
3356 d3d9_device_CreateRenderTarget,
3357 d3d9_device_CreateDepthStencilSurface,
3358 d3d9_device_UpdateSurface,
3359 d3d9_device_UpdateTexture,
3360 d3d9_device_GetRenderTargetData,
3361 d3d9_device_GetFrontBufferData,
3362 d3d9_device_StretchRect,
3363 d3d9_device_ColorFill,
3364 d3d9_device_CreateOffscreenPlainSurface,
3365 d3d9_device_SetRenderTarget,
3366 d3d9_device_GetRenderTarget,
3367 d3d9_device_SetDepthStencilSurface,
3368 d3d9_device_GetDepthStencilSurface,
3369 d3d9_device_BeginScene,
3370 d3d9_device_EndScene,
3371 d3d9_device_Clear,
3372 d3d9_device_SetTransform,
3373 d3d9_device_GetTransform,
3374 d3d9_device_MultiplyTransform,
3375 d3d9_device_SetViewport,
3376 d3d9_device_GetViewport,
3377 d3d9_device_SetMaterial,
3378 d3d9_device_GetMaterial,
3379 d3d9_device_SetLight,
3380 d3d9_device_GetLight,
3381 d3d9_device_LightEnable,
3382 d3d9_device_GetLightEnable,
3383 d3d9_device_SetClipPlane,
3384 d3d9_device_GetClipPlane,
3385 d3d9_device_SetRenderState,
3386 d3d9_device_GetRenderState,
3387 d3d9_device_CreateStateBlock,
3388 d3d9_device_BeginStateBlock,
3389 d3d9_device_EndStateBlock,
3390 d3d9_device_SetClipStatus,
3391 d3d9_device_GetClipStatus,
3392 d3d9_device_GetTexture,
3393 d3d9_device_SetTexture,
3394 d3d9_device_GetTextureStageState,
3395 d3d9_device_SetTextureStageState,
3396 d3d9_device_GetSamplerState,
3397 d3d9_device_SetSamplerState,
3398 d3d9_device_ValidateDevice,
3399 d3d9_device_SetPaletteEntries,
3400 d3d9_device_GetPaletteEntries,
3401 d3d9_device_SetCurrentTexturePalette,
3402 d3d9_device_GetCurrentTexturePalette,
3403 d3d9_device_SetScissorRect,
3404 d3d9_device_GetScissorRect,
3405 d3d9_device_SetSoftwareVertexProcessing,
3406 d3d9_device_GetSoftwareVertexProcessing,
3407 d3d9_device_SetNPatchMode,
3408 d3d9_device_GetNPatchMode,
3409 d3d9_device_DrawPrimitive,
3410 d3d9_device_DrawIndexedPrimitive,
3411 d3d9_device_DrawPrimitiveUP,
3412 d3d9_device_DrawIndexedPrimitiveUP,
3413 d3d9_device_ProcessVertices,
3414 d3d9_device_CreateVertexDeclaration,
3415 d3d9_device_SetVertexDeclaration,
3416 d3d9_device_GetVertexDeclaration,
3417 d3d9_device_SetFVF,
3418 d3d9_device_GetFVF,
3419 d3d9_device_CreateVertexShader,
3420 d3d9_device_SetVertexShader,
3421 d3d9_device_GetVertexShader,
3422 d3d9_device_SetVertexShaderConstantF,
3423 d3d9_device_GetVertexShaderConstantF,
3424 d3d9_device_SetVertexShaderConstantI,
3425 d3d9_device_GetVertexShaderConstantI,
3426 d3d9_device_SetVertexShaderConstantB,
3427 d3d9_device_GetVertexShaderConstantB,
3428 d3d9_device_SetStreamSource,
3429 d3d9_device_GetStreamSource,
3430 d3d9_device_SetStreamSourceFreq,
3431 d3d9_device_GetStreamSourceFreq,
3432 d3d9_device_SetIndices,
3433 d3d9_device_GetIndices,
3434 d3d9_device_CreatePixelShader,
3435 d3d9_device_SetPixelShader,
3436 d3d9_device_GetPixelShader,
3437 d3d9_device_SetPixelShaderConstantF,
3438 d3d9_device_GetPixelShaderConstantF,
3439 d3d9_device_SetPixelShaderConstantI,
3440 d3d9_device_GetPixelShaderConstantI,
3441 d3d9_device_SetPixelShaderConstantB,
3442 d3d9_device_GetPixelShaderConstantB,
3443 d3d9_device_DrawRectPatch,
3444 d3d9_device_DrawTriPatch,
3445 d3d9_device_DeletePatch,
3446 d3d9_device_CreateQuery,
3447 /* IDirect3DDevice9Ex */
3448 d3d9_device_SetConvolutionMonoKernel,
3449 d3d9_device_ComposeRects,
3450 d3d9_device_PresentEx,
3451 d3d9_device_GetGPUThreadPriority,
3452 d3d9_device_SetGPUThreadPriority,
3453 d3d9_device_WaitForVBlank,
3454 d3d9_device_CheckResourceResidency,
3455 d3d9_device_SetMaximumFrameLatency,
3456 d3d9_device_GetMaximumFrameLatency,
3457 d3d9_device_CheckDeviceState,
3458 d3d9_device_CreateRenderTargetEx,
3459 d3d9_device_CreateOffscreenPlainSurfaceEx,
3460 d3d9_device_CreateDepthStencilSurfaceEx,
3461 d3d9_device_ResetEx,
3462 d3d9_device_GetDisplayModeEx,
3465 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3467 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3470 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3471 struct wined3d_device *device)
3473 TRACE("device_parent %p, device %p.\n", device_parent, device);
3476 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3478 TRACE("device_parent %p.\n", device_parent);
3481 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3483 struct d3d9_device *device = device_from_device_parent(device_parent);
3485 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3487 if (!device->d3d_parent)
3488 return;
3490 if (!activate)
3491 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3492 else if (device->d3d_parent->extended)
3493 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3494 else
3495 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3498 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3499 void *container_parent, struct wined3d_surface *surface, void **parent,
3500 const struct wined3d_parent_ops **parent_ops)
3502 struct d3d9_surface *d3d_surface;
3504 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3505 device_parent, container_parent, surface, parent, parent_ops);
3507 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3508 return E_OUTOFMEMORY;
3510 surface_init(d3d_surface, container_parent, surface, parent_ops);
3511 *parent = d3d_surface;
3512 TRACE("Created surface %p.\n", d3d_surface);
3514 return D3D_OK;
3517 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3518 void *container_parent, struct wined3d_volume *volume, void **parent,
3519 const struct wined3d_parent_ops **parent_ops)
3521 struct d3d9_volume *d3d_volume;
3523 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3524 device_parent, container_parent, volume, parent, parent_ops);
3526 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3527 return E_OUTOFMEMORY;
3529 volume_init(d3d_volume, container_parent, volume, parent_ops);
3530 *parent = d3d_volume;
3531 TRACE("Created volume %p.\n", d3d_volume);
3533 return D3D_OK;
3536 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3537 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3539 struct d3d9_device *device = device_from_device_parent(device_parent);
3540 struct wined3d_resource_desc texture_desc;
3541 struct d3d9_surface *d3d_surface;
3542 struct wined3d_texture *texture;
3543 HRESULT hr;
3545 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3546 device_parent, container_parent, desc, surface);
3548 if (container_parent == device_parent)
3549 container_parent = &device->IDirect3DDevice9Ex_iface;
3551 texture_desc = *desc;
3552 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3553 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3554 WINED3D_SURFACE_MAPPABLE, NULL, container_parent, &d3d9_null_wined3d_parent_ops, &texture)))
3556 WARN("Failed to create texture, hr %#x.\n", hr);
3557 return hr;
3560 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3561 wined3d_surface_incref(*surface);
3562 wined3d_texture_decref(texture);
3564 d3d_surface = wined3d_surface_get_parent(*surface);
3565 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3567 return hr;
3570 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3571 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3573 struct d3d9_device *device = device_from_device_parent(device_parent);
3574 struct d3d9_swapchain *d3d_swapchain;
3575 HRESULT hr;
3577 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3579 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3580 if (FAILED(hr))
3582 WARN("Failed to create swapchain, hr %#x.\n", hr);
3583 *swapchain = NULL;
3584 return hr;
3587 *swapchain = d3d_swapchain->wined3d_swapchain;
3588 wined3d_swapchain_incref(*swapchain);
3589 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3591 return hr;
3594 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3596 device_parent_wined3d_device_created,
3597 device_parent_mode_changed,
3598 device_parent_activate,
3599 device_parent_surface_created,
3600 device_parent_volume_created,
3601 device_parent_create_swapchain_surface,
3602 device_parent_create_swapchain,
3605 static void setup_fpu(void)
3607 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3608 WORD cw;
3609 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3610 cw = (cw & ~0xf3f) | 0x3f;
3611 __asm__ volatile ("fldcw %0" : : "m" (cw));
3612 #elif defined(__i386__) && defined(_MSC_VER)
3613 WORD cw;
3614 __asm fnstcw cw;
3615 cw = (cw & ~0xf3f) | 0x3f;
3616 __asm fldcw cw;
3617 #else
3618 FIXME("FPU setup not implemented for this platform.\n");
3619 #endif
3622 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3623 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3624 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3626 struct wined3d_swapchain_desc *swapchain_desc;
3627 UINT i, count = 1;
3628 HRESULT hr;
3630 if (mode)
3631 FIXME("Ignoring display mode.\n");
3633 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3634 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3635 device->refcount = 1;
3637 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3639 wined3d_mutex_lock();
3640 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3641 &device->device_parent, &device->wined3d_device);
3642 if (FAILED(hr))
3644 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3645 wined3d_mutex_unlock();
3646 return hr;
3649 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3651 WINED3DCAPS caps;
3653 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3654 count = caps.NumberOfAdaptersInGroup;
3657 if (flags & D3DCREATE_MULTITHREADED)
3658 wined3d_device_set_multithreaded(device->wined3d_device);
3660 if (!parameters->Windowed)
3662 if (!focus_window)
3663 focus_window = parameters->hDeviceWindow;
3664 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3666 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3667 wined3d_device_decref(device->wined3d_device);
3668 wined3d_mutex_unlock();
3669 return hr;
3672 for (i = 0; i < count; ++i)
3674 HWND device_window = parameters[i].hDeviceWindow;
3676 if (!device_window) device_window = focus_window;
3677 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3678 parameters[i].BackBufferWidth,
3679 parameters[i].BackBufferHeight);
3683 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3684 if (!swapchain_desc)
3686 ERR("Failed to allocate wined3d parameters.\n");
3687 wined3d_device_decref(device->wined3d_device);
3688 wined3d_mutex_unlock();
3689 return E_OUTOFMEMORY;
3692 for (i = 0; i < count; ++i)
3694 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i]);
3697 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3698 if (FAILED(hr))
3700 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3701 wined3d_device_release_focus_window(device->wined3d_device);
3702 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3703 wined3d_device_decref(device->wined3d_device);
3704 wined3d_mutex_unlock();
3705 return hr;
3708 wined3d_mutex_unlock();
3710 for (i = 0; i < count; ++i)
3712 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3714 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3716 /* Initialize the converted declaration array. This creates a valid pointer
3717 * and when adding decls HeapReAlloc() can be used without further checking. */
3718 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3719 if (!device->fvf_decls)
3721 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3722 wined3d_mutex_lock();
3723 wined3d_device_uninit_3d(device->wined3d_device);
3724 wined3d_device_release_focus_window(device->wined3d_device);
3725 wined3d_device_decref(device->wined3d_device);
3726 wined3d_mutex_unlock();
3727 return E_OUTOFMEMORY;
3730 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3731 device->d3d_parent = parent;
3733 return D3D_OK;