oleacc: Add default window accessible object stub.
[wine.git] / dlls / d3d9 / device.c
blob263ac12c0152f9686ab892a3d4d88551b49bf518
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 if (device->not_reset)
328 TRACE("D3D9 device is marked not reset.\n");
329 return D3DERR_DEVICENOTRESET;
332 return D3D_OK;
335 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
337 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
338 HRESULT hr;
340 TRACE("iface %p.\n", iface);
342 wined3d_mutex_lock();
343 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
344 wined3d_mutex_unlock();
346 return hr;
349 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
353 TRACE("iface %p.\n", iface);
355 wined3d_mutex_lock();
356 wined3d_device_evict_managed_resources(device->wined3d_device);
357 wined3d_mutex_unlock();
359 return D3D_OK;
362 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
364 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
366 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
368 if (!d3d9)
369 return D3DERR_INVALIDCALL;
371 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
374 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
376 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
377 WINED3DCAPS *wined3d_caps;
378 HRESULT hr;
380 TRACE("iface %p, caps %p.\n", iface, caps);
382 if (!caps)
383 return D3DERR_INVALIDCALL;
385 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
386 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
388 memset(caps, 0, sizeof(*caps));
390 wined3d_mutex_lock();
391 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
392 wined3d_mutex_unlock();
394 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
395 HeapFree(GetProcessHeap(), 0, wined3d_caps);
397 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
398 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
400 filter_caps(caps);
402 return hr;
405 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
407 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
408 struct wined3d_display_mode wined3d_mode;
409 HRESULT hr;
411 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
413 wined3d_mutex_lock();
414 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
415 wined3d_mutex_unlock();
417 if (SUCCEEDED(hr))
419 mode->Width = wined3d_mode.width;
420 mode->Height = wined3d_mode.height;
421 mode->RefreshRate = wined3d_mode.refresh_rate;
422 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
425 return hr;
428 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
429 D3DDEVICE_CREATION_PARAMETERS *parameters)
431 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
433 TRACE("iface %p, parameters %p.\n", iface, parameters);
435 wined3d_mutex_lock();
436 wined3d_device_get_creation_parameters(device->wined3d_device,
437 (struct wined3d_device_creation_parameters *)parameters);
438 wined3d_mutex_unlock();
440 return D3D_OK;
443 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
444 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
446 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
447 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
448 HRESULT hr;
450 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
451 iface, hotspot_x, hotspot_y, bitmap);
453 if (!bitmap)
455 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
456 return D3DERR_INVALIDCALL;
459 wined3d_mutex_lock();
460 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
461 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
462 wined3d_mutex_unlock();
464 return hr;
467 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
469 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
471 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
473 wined3d_mutex_lock();
474 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
475 wined3d_mutex_unlock();
478 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
480 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
481 BOOL ret;
483 TRACE("iface %p, show %#x.\n", iface, show);
485 wined3d_mutex_lock();
486 ret = wined3d_device_show_cursor(device->wined3d_device, show);
487 wined3d_mutex_unlock();
489 return ret;
492 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
493 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
495 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
496 struct wined3d_swapchain_desc desc;
497 struct d3d9_swapchain *object;
498 HRESULT hr;
500 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
501 iface, present_parameters, swapchain);
503 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
504 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
505 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
506 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
508 return hr;
511 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
512 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
514 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
515 struct wined3d_swapchain *wined3d_swapchain;
516 struct d3d9_swapchain *swapchain_impl;
517 HRESULT hr;
519 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
521 wined3d_mutex_lock();
522 if ((wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx)))
524 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
525 *swapchain = (IDirect3DSwapChain9 *)&swapchain_impl->IDirect3DSwapChain9Ex_iface;
526 IDirect3DSwapChain9Ex_AddRef(*swapchain);
527 hr = D3D_OK;
529 else
531 *swapchain = NULL;
532 hr = D3DERR_INVALIDCALL;
534 wined3d_mutex_unlock();
536 return hr;
539 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
541 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
542 UINT count;
544 TRACE("iface %p.\n", iface);
546 wined3d_mutex_lock();
547 count = wined3d_device_get_swapchain_count(device->wined3d_device);
548 wined3d_mutex_unlock();
550 return count;
553 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
555 struct wined3d_resource_desc desc;
557 wined3d_resource_get_desc(resource, &desc);
558 if (desc.pool == WINED3D_POOL_DEFAULT)
560 struct d3d9_surface *surface;
562 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
564 IUnknown *parent = wined3d_resource_get_parent(resource);
565 IDirect3DBaseTexture9 *texture;
567 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
569 IDirect3DBaseTexture9_Release(texture);
570 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
571 return D3DERR_INVALIDCALL;
574 return D3D_OK;
577 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
579 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
580 return D3DERR_INVALIDCALL;
583 surface = wined3d_resource_get_parent(resource);
584 if (surface->resource.refcount)
586 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
587 return D3DERR_INVALIDCALL;
590 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
593 return D3D_OK;
596 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
597 D3DPRESENT_PARAMETERS *present_parameters)
599 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
600 struct wined3d_swapchain_desc swapchain_desc;
601 HRESULT hr;
603 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
605 wined3d_mutex_lock();
607 if (device->vertex_buffer)
609 wined3d_buffer_decref(device->vertex_buffer);
610 device->vertex_buffer = NULL;
611 device->vertex_buffer_size = 0;
613 if (device->index_buffer)
615 wined3d_buffer_decref(device->index_buffer);
616 device->index_buffer = NULL;
617 device->index_buffer_size = 0;
620 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
621 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
622 NULL, reset_enum_callback, !device->d3d_parent->extended);
623 if (FAILED(hr) && !device->d3d_parent->extended)
624 device->not_reset = TRUE;
625 else
626 device->not_reset = FALSE;
627 wined3d_mutex_unlock();
629 return hr;
632 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
633 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
635 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
636 HRESULT hr;
638 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
639 iface, src_rect, dst_rect, dst_window_override, dirty_region);
641 wined3d_mutex_lock();
642 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
643 dst_window_override, dirty_region, 0);
644 wined3d_mutex_unlock();
646 return hr;
649 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
650 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
653 struct wined3d_surface *wined3d_surface = NULL;
654 struct d3d9_surface *surface_impl;
655 HRESULT hr;
657 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
658 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
660 wined3d_mutex_lock();
661 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
662 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
663 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
665 surface_impl = wined3d_surface_get_parent(wined3d_surface);
666 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
667 IDirect3DSurface9_AddRef(*backbuffer);
669 wined3d_mutex_unlock();
671 return hr;
673 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
674 UINT swapchain, D3DRASTER_STATUS *raster_status)
676 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
677 HRESULT hr;
679 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
681 wined3d_mutex_lock();
682 hr = wined3d_device_get_raster_status(device->wined3d_device,
683 swapchain, (struct wined3d_raster_status *)raster_status);
684 wined3d_mutex_unlock();
686 return hr;
689 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
691 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
692 HRESULT hr;
694 TRACE("iface %p, enable %#x.\n", iface, enable);
696 wined3d_mutex_lock();
697 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
698 wined3d_mutex_unlock();
700 return hr;
703 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
704 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
706 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
708 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
710 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
711 wined3d_mutex_lock();
712 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
713 wined3d_mutex_unlock();
716 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
720 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
722 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
723 wined3d_mutex_lock();
724 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
725 wined3d_mutex_unlock();
728 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
729 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
730 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
733 struct d3d9_texture *object;
734 BOOL set_mem = FALSE;
735 HRESULT hr;
737 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
738 iface, width, height, levels, usage, format, pool, texture, shared_handle);
740 *texture = NULL;
741 if (shared_handle)
743 if (!device->d3d_parent->extended)
745 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
746 return E_NOTIMPL;
749 if (pool == D3DPOOL_SYSTEMMEM)
751 if (levels != 1)
752 return D3DERR_INVALIDCALL;
753 set_mem = TRUE;
755 else
757 if (pool != D3DPOOL_DEFAULT)
759 WARN("Trying to create a shared texture in pool %#x.\n", pool);
760 return D3DERR_INVALIDCALL;
762 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
766 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
767 if (!object)
768 return D3DERR_OUTOFVIDEOMEMORY;
770 hr = texture_init(object, device, width, height, levels, usage, format, pool);
771 if (FAILED(hr))
773 WARN("Failed to initialize texture, hr %#x.\n", hr);
774 HeapFree(GetProcessHeap(), 0, object);
775 return hr;
778 if (set_mem)
780 struct wined3d_resource *resource;
781 struct d3d9_surface *surface;
783 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
784 surface = wined3d_resource_get_parent(resource);
785 wined3d_surface_update_desc(surface->wined3d_surface, width, height,
786 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
787 *shared_handle, 0);
790 TRACE("Created texture %p.\n", object);
791 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
793 return D3D_OK;
796 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
797 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
798 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
800 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
801 struct d3d9_texture *object;
802 HRESULT hr;
804 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
805 iface, width, height, depth, levels);
806 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
807 usage, format, pool, texture, shared_handle);
809 *texture = NULL;
810 if (shared_handle)
812 if (!device->d3d_parent->extended)
814 WARN("Trying to create a shared volume texture on a non-ex device.\n");
815 return E_NOTIMPL;
818 if (pool != D3DPOOL_DEFAULT)
820 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
821 return D3DERR_INVALIDCALL;
823 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
826 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
827 if (!object)
828 return D3DERR_OUTOFVIDEOMEMORY;
830 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
831 if (FAILED(hr))
833 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
834 HeapFree(GetProcessHeap(), 0, object);
835 return hr;
838 TRACE("Created volume texture %p.\n", object);
839 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
841 return D3D_OK;
844 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
845 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
846 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
848 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
849 struct d3d9_texture *object;
850 HRESULT hr;
852 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
853 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
855 *texture = NULL;
856 if (shared_handle)
858 if (!device->d3d_parent->extended)
860 WARN("Trying to create a shared cube texture on a non-ex device.\n");
861 return E_NOTIMPL;
864 if (pool != D3DPOOL_DEFAULT)
866 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
867 return D3DERR_INVALIDCALL;
869 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
872 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
873 if (!object)
874 return D3DERR_OUTOFVIDEOMEMORY;
876 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
877 if (FAILED(hr))
879 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
880 HeapFree(GetProcessHeap(), 0, object);
881 return hr;
884 TRACE("Created cube texture %p.\n", object);
885 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
887 return D3D_OK;
890 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
891 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
892 HANDLE *shared_handle)
894 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
895 struct d3d9_vertexbuffer *object;
896 HRESULT hr;
898 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
899 iface, size, usage, fvf, pool, buffer, shared_handle);
901 if (shared_handle)
903 if (!device->d3d_parent->extended)
905 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
906 return E_NOTIMPL;
909 if (pool != D3DPOOL_DEFAULT)
911 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
912 return D3DERR_NOTAVAILABLE;
914 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
917 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
918 if (!object)
919 return D3DERR_OUTOFVIDEOMEMORY;
921 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
922 if (FAILED(hr))
924 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
925 HeapFree(GetProcessHeap(), 0, object);
926 return hr;
929 TRACE("Created vertex buffer %p.\n", object);
930 *buffer = &object->IDirect3DVertexBuffer9_iface;
932 return D3D_OK;
935 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
936 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
937 HANDLE *shared_handle)
939 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
940 struct d3d9_indexbuffer *object;
941 HRESULT hr;
943 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
944 iface, size, usage, format, pool, buffer, shared_handle);
946 if (shared_handle)
948 if (!device->d3d_parent->extended)
950 WARN("Trying to create a shared index buffer on a non-ex device.\n");
951 return E_NOTIMPL;
954 if (pool != D3DPOOL_DEFAULT)
956 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
957 return D3DERR_NOTAVAILABLE;
959 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
962 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
963 if (!object)
964 return D3DERR_OUTOFVIDEOMEMORY;
966 hr = indexbuffer_init(object, device, size, usage, format, pool);
967 if (FAILED(hr))
969 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
970 HeapFree(GetProcessHeap(), 0, object);
971 return hr;
974 TRACE("Created index buffer %p.\n", object);
975 *buffer = &object->IDirect3DIndexBuffer9_iface;
977 return D3D_OK;
980 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
981 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
982 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
984 struct wined3d_resource *sub_resource;
985 struct wined3d_resource_desc desc;
986 struct d3d9_surface *surface_impl;
987 struct wined3d_texture *texture;
988 HRESULT hr;
990 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
991 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
992 device, width, height, format, flags, surface, usage, pool,
993 multisample_type, multisample_quality);
995 desc.resource_type = WINED3D_RTYPE_TEXTURE;
996 desc.format = wined3dformat_from_d3dformat(format);
997 desc.multisample_type = multisample_type;
998 desc.multisample_quality = multisample_quality;
999 desc.usage = usage & WINED3DUSAGE_MASK;
1000 desc.pool = pool;
1001 desc.width = width;
1002 desc.height = height;
1003 desc.depth = 1;
1004 desc.size = 0;
1006 wined3d_mutex_lock();
1008 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1009 1, flags, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1011 wined3d_mutex_unlock();
1012 WARN("Failed to create texture, hr %#x.\n", hr);
1013 return hr;
1016 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1017 surface_impl = wined3d_resource_get_parent(sub_resource);
1018 surface_impl->forwardReference = NULL;
1019 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1020 *surface = &surface_impl->IDirect3DSurface9_iface;
1021 IDirect3DSurface9_AddRef(*surface);
1022 wined3d_texture_decref(texture);
1024 if (user_mem)
1025 wined3d_surface_update_desc(surface_impl->wined3d_surface, width, height,
1026 desc.format, multisample_type, multisample_quality, user_mem, 0);
1028 wined3d_mutex_unlock();
1030 return D3D_OK;
1033 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1034 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1035 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1037 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1038 DWORD flags = 0;
1040 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1041 "lockable %#x, surface %p, shared_handle %p.\n",
1042 iface, width, height, format, multisample_type, multisample_quality,
1043 lockable, surface, shared_handle);
1045 *surface = NULL;
1046 if (shared_handle)
1048 if (!device->d3d_parent->extended)
1050 WARN("Trying to create a shared render target on a non-ex device.\n");
1051 return E_NOTIMPL;
1054 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1057 if (lockable)
1058 flags |= WINED3D_SURFACE_MAPPABLE;
1060 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1061 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1064 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1065 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1066 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1068 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1069 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1071 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1072 "discard %#x, surface %p, shared_handle %p.\n",
1073 iface, width, height, format, multisample_type, multisample_quality,
1074 discard, surface, shared_handle);
1076 *surface = NULL;
1077 if (shared_handle)
1079 if (!device->d3d_parent->extended)
1081 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1082 return E_NOTIMPL;
1085 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1088 if (discard)
1089 flags |= WINED3D_SURFACE_DISCARD;
1091 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1092 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1096 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1097 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1098 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1100 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1101 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1102 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1103 HRESULT hr;
1105 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1106 iface, src_surface, src_rect, dst_surface, dst_point);
1108 wined3d_mutex_lock();
1109 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1110 dst->wined3d_surface, dst_point);
1111 wined3d_mutex_unlock();
1113 return hr;
1116 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1117 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1119 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1120 struct d3d9_texture *src_impl, *dst_impl;
1121 HRESULT hr;
1123 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1125 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1126 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1128 wined3d_mutex_lock();
1129 hr = wined3d_device_update_texture(device->wined3d_device,
1130 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1131 wined3d_mutex_unlock();
1133 return hr;
1136 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1137 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1139 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1140 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1141 HRESULT hr;
1143 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1145 wined3d_mutex_lock();
1146 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1147 wined3d_mutex_unlock();
1149 return hr;
1152 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1153 UINT swapchain, IDirect3DSurface9 *dst_surface)
1155 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1156 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1157 HRESULT hr;
1159 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1161 wined3d_mutex_lock();
1162 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1163 wined3d_mutex_unlock();
1165 return hr;
1168 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1169 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1171 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1172 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1173 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1174 HRESULT hr = D3DERR_INVALIDCALL;
1175 struct wined3d_resource_desc src_desc, dst_desc;
1176 struct wined3d_resource *wined3d_resource;
1178 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1179 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1181 wined3d_mutex_lock();
1182 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1183 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1185 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1186 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1188 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1190 if (device->in_scene)
1192 WARN("Rejecting depth / stencil blit while in scene.\n");
1193 goto done;
1196 if (src_rect)
1198 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1199 || src_rect->bottom != src_desc.height)
1201 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1202 wine_dbgstr_rect(src_rect));
1203 goto done;
1206 if (dst_rect)
1208 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1209 || dst_rect->bottom != dst_desc.height)
1211 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1212 wine_dbgstr_rect(dst_rect));
1213 goto done;
1216 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1218 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1219 goto done;
1223 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1224 if (hr == WINEDDERR_INVALIDRECT)
1225 hr = D3DERR_INVALIDCALL;
1227 done:
1228 wined3d_mutex_unlock();
1229 return hr;
1232 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1233 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1235 const struct wined3d_color c =
1237 ((color >> 16) & 0xff) / 255.0f,
1238 ((color >> 8) & 0xff) / 255.0f,
1239 (color & 0xff) / 255.0f,
1240 ((color >> 24) & 0xff) / 255.0f,
1242 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1243 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1244 struct wined3d_resource *wined3d_resource;
1245 struct wined3d_resource_desc desc;
1246 HRESULT hr;
1248 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1250 wined3d_mutex_lock();
1252 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1253 wined3d_resource_get_desc(wined3d_resource, &desc);
1255 /* This method is only allowed with surfaces that are render targets, or
1256 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1257 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1259 wined3d_mutex_unlock();
1260 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1261 return D3DERR_INVALIDCALL;
1264 /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1265 hr = wined3d_device_color_fill(device->wined3d_device, surface_impl->wined3d_surface, rect, &c);
1267 wined3d_mutex_unlock();
1269 return hr;
1272 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1273 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1274 HANDLE *shared_handle)
1276 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1277 void *user_mem = NULL;
1279 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1280 iface, width, height, format, pool, surface, shared_handle);
1282 *surface = NULL;
1283 if (pool == D3DPOOL_MANAGED)
1285 WARN("Attempting to create a managed offscreen plain surface.\n");
1286 return D3DERR_INVALIDCALL;
1289 if (shared_handle)
1291 if (!device->d3d_parent->extended)
1293 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1294 return E_NOTIMPL;
1297 if (pool == D3DPOOL_SYSTEMMEM)
1298 user_mem = *shared_handle;
1299 else
1301 if (pool != D3DPOOL_DEFAULT)
1303 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1304 return D3DERR_INVALIDCALL;
1306 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1310 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1311 * regardless of the pool they're created in. Should we set dynamic usage
1312 * here? */
1313 return d3d9_device_create_surface(device, width, height, format,
1314 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1317 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1319 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1320 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1321 HRESULT hr;
1323 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1325 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1327 WARN("Invalid index %u specified.\n", idx);
1328 return D3DERR_INVALIDCALL;
1331 if (!idx && !surface_impl)
1333 WARN("Trying to set render target 0 to NULL.\n");
1334 return D3DERR_INVALIDCALL;
1337 wined3d_mutex_lock();
1338 hr = wined3d_device_set_render_target(device->wined3d_device, idx,
1339 surface_impl ? surface_impl->wined3d_surface : NULL, TRUE);
1340 wined3d_mutex_unlock();
1342 return hr;
1345 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1347 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1348 struct wined3d_surface *wined3d_surface;
1349 struct d3d9_surface *surface_impl;
1350 HRESULT hr = D3D_OK;
1352 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1354 if (!surface)
1355 return D3DERR_INVALIDCALL;
1357 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1359 WARN("Invalid index %u specified.\n", idx);
1360 return D3DERR_INVALIDCALL;
1363 wined3d_mutex_lock();
1364 if ((wined3d_surface = wined3d_device_get_render_target(device->wined3d_device, idx)))
1366 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1367 *surface = &surface_impl->IDirect3DSurface9_iface;
1368 IDirect3DSurface9_AddRef(*surface);
1370 else
1372 hr = WINED3DERR_NOTFOUND;
1373 *surface = NULL;
1375 wined3d_mutex_unlock();
1377 return hr;
1380 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1382 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1383 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1385 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1387 wined3d_mutex_lock();
1388 wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1389 wined3d_mutex_unlock();
1391 return D3D_OK;
1394 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1396 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1397 struct wined3d_surface *wined3d_surface;
1398 struct d3d9_surface *surface_impl;
1399 HRESULT hr = D3D_OK;
1401 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1403 if (!depth_stencil)
1404 return D3DERR_INVALIDCALL;
1406 wined3d_mutex_lock();
1407 if ((wined3d_surface = wined3d_device_get_depth_stencil(device->wined3d_device)))
1409 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1410 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1411 IDirect3DSurface9_AddRef(*depth_stencil);
1413 else
1415 hr = WINED3DERR_NOTFOUND;
1416 *depth_stencil = NULL;
1418 wined3d_mutex_unlock();
1420 return hr;
1423 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1425 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1426 HRESULT hr;
1428 TRACE("iface %p.\n", iface);
1430 wined3d_mutex_lock();
1431 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1432 device->in_scene = TRUE;
1433 wined3d_mutex_unlock();
1435 return hr;
1438 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1440 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1441 HRESULT hr;
1443 TRACE("iface %p.\n", iface);
1445 wined3d_mutex_lock();
1446 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1447 device->in_scene = FALSE;
1448 wined3d_mutex_unlock();
1450 return hr;
1453 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1454 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1456 const struct wined3d_color c =
1458 ((color >> 16) & 0xff) / 255.0f,
1459 ((color >> 8) & 0xff) / 255.0f,
1460 (color & 0xff) / 255.0f,
1461 ((color >> 24) & 0xff) / 255.0f,
1463 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1464 HRESULT hr;
1466 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1467 iface, rect_count, rects, flags, color, z, stencil);
1469 wined3d_mutex_lock();
1470 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1471 wined3d_mutex_unlock();
1473 return hr;
1476 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1477 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1479 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1481 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1483 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1484 wined3d_mutex_lock();
1485 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1486 wined3d_mutex_unlock();
1488 return D3D_OK;
1491 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1492 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1494 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1496 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1498 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1499 wined3d_mutex_lock();
1500 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1501 wined3d_mutex_unlock();
1503 return D3D_OK;
1506 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1507 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1509 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1511 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1513 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1514 wined3d_mutex_lock();
1515 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1516 wined3d_mutex_unlock();
1518 return D3D_OK;
1521 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1523 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1525 TRACE("iface %p, viewport %p.\n", iface, viewport);
1527 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1528 wined3d_mutex_lock();
1529 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1530 wined3d_mutex_unlock();
1532 return D3D_OK;
1535 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1537 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1539 TRACE("iface %p, viewport %p.\n", iface, viewport);
1541 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1542 wined3d_mutex_lock();
1543 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1544 wined3d_mutex_unlock();
1546 return D3D_OK;
1549 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1551 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1553 TRACE("iface %p, material %p.\n", iface, material);
1555 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1556 wined3d_mutex_lock();
1557 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1558 wined3d_mutex_unlock();
1560 return D3D_OK;
1563 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1565 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1567 TRACE("iface %p, material %p.\n", iface, material);
1569 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1570 wined3d_mutex_lock();
1571 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1572 wined3d_mutex_unlock();
1574 return D3D_OK;
1577 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1579 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1580 HRESULT hr;
1582 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1584 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1585 wined3d_mutex_lock();
1586 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1587 wined3d_mutex_unlock();
1589 return hr;
1592 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1594 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1595 HRESULT hr;
1597 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1599 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1600 wined3d_mutex_lock();
1601 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1602 wined3d_mutex_unlock();
1604 return hr;
1607 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1609 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1610 HRESULT hr;
1612 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1614 wined3d_mutex_lock();
1615 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1616 wined3d_mutex_unlock();
1618 return hr;
1621 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1623 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1624 HRESULT hr;
1626 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1628 wined3d_mutex_lock();
1629 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1630 wined3d_mutex_unlock();
1632 return hr;
1635 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1637 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1638 HRESULT hr;
1640 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1642 wined3d_mutex_lock();
1643 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1644 wined3d_mutex_unlock();
1646 return hr;
1649 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1651 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1652 HRESULT hr;
1654 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1656 wined3d_mutex_lock();
1657 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1658 wined3d_mutex_unlock();
1660 return hr;
1663 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1664 D3DRENDERSTATETYPE state, DWORD value)
1666 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1668 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1670 wined3d_mutex_lock();
1671 wined3d_device_set_render_state(device->wined3d_device, state, value);
1672 wined3d_mutex_unlock();
1674 return D3D_OK;
1677 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1678 D3DRENDERSTATETYPE state, DWORD *value)
1680 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1682 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1684 wined3d_mutex_lock();
1685 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1686 wined3d_mutex_unlock();
1688 return D3D_OK;
1691 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1692 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1694 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1695 struct d3d9_stateblock *object;
1696 HRESULT hr;
1698 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1700 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1702 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1703 return D3DERR_INVALIDCALL;
1706 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1707 if (!object)
1708 return E_OUTOFMEMORY;
1710 hr = stateblock_init(object, device, type, NULL);
1711 if (FAILED(hr))
1713 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1714 HeapFree(GetProcessHeap(), 0, object);
1715 return hr;
1718 TRACE("Created stateblock %p.\n", object);
1719 *stateblock = &object->IDirect3DStateBlock9_iface;
1721 return D3D_OK;
1724 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1726 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1727 HRESULT hr;
1729 TRACE("iface %p.\n", iface);
1731 wined3d_mutex_lock();
1732 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1733 wined3d_mutex_unlock();
1735 return hr;
1738 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1740 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1741 struct wined3d_stateblock *wined3d_stateblock;
1742 struct d3d9_stateblock *object;
1743 HRESULT hr;
1745 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1747 wined3d_mutex_lock();
1748 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1749 wined3d_mutex_unlock();
1750 if (FAILED(hr))
1752 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1753 return hr;
1756 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1757 if (!object)
1759 wined3d_mutex_lock();
1760 wined3d_stateblock_decref(wined3d_stateblock);
1761 wined3d_mutex_unlock();
1762 return E_OUTOFMEMORY;
1765 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1766 if (FAILED(hr))
1768 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1769 wined3d_mutex_lock();
1770 wined3d_stateblock_decref(wined3d_stateblock);
1771 wined3d_mutex_unlock();
1772 HeapFree(GetProcessHeap(), 0, object);
1773 return hr;
1776 TRACE("Created stateblock %p.\n", object);
1777 *stateblock = &object->IDirect3DStateBlock9_iface;
1779 return D3D_OK;
1782 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1784 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1785 HRESULT hr;
1787 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1789 wined3d_mutex_lock();
1790 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1791 wined3d_mutex_unlock();
1793 return hr;
1796 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1798 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1799 HRESULT hr;
1801 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1803 wined3d_mutex_lock();
1804 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1805 wined3d_mutex_unlock();
1807 return hr;
1810 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1812 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1813 struct wined3d_texture *wined3d_texture = NULL;
1814 struct d3d9_texture *texture_impl;
1816 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1818 if (!texture)
1819 return D3DERR_INVALIDCALL;
1821 wined3d_mutex_lock();
1822 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1824 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1825 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1826 IDirect3DBaseTexture9_AddRef(*texture);
1828 else
1830 *texture = NULL;
1832 wined3d_mutex_unlock();
1834 return D3D_OK;
1837 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1839 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1840 struct d3d9_texture *texture_impl;
1841 HRESULT hr;
1843 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1845 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1847 wined3d_mutex_lock();
1848 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1849 texture_impl ? texture_impl->wined3d_texture : NULL);
1850 wined3d_mutex_unlock();
1852 return hr;
1855 static const enum wined3d_texture_stage_state tss_lookup[] =
1857 WINED3D_TSS_INVALID, /* 0, unused */
1858 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1859 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1860 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1861 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1862 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1863 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1864 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1865 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1866 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1867 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1868 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1869 WINED3D_TSS_INVALID, /* 12, unused */
1870 WINED3D_TSS_INVALID, /* 13, unused */
1871 WINED3D_TSS_INVALID, /* 14, unused */
1872 WINED3D_TSS_INVALID, /* 15, unused */
1873 WINED3D_TSS_INVALID, /* 16, unused */
1874 WINED3D_TSS_INVALID, /* 17, unused */
1875 WINED3D_TSS_INVALID, /* 18, unused */
1876 WINED3D_TSS_INVALID, /* 19, unused */
1877 WINED3D_TSS_INVALID, /* 20, unused */
1878 WINED3D_TSS_INVALID, /* 21, unused */
1879 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1880 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1881 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1882 WINED3D_TSS_INVALID, /* 25, unused */
1883 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1884 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1885 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1886 WINED3D_TSS_INVALID, /* 29, unused */
1887 WINED3D_TSS_INVALID, /* 30, unused */
1888 WINED3D_TSS_INVALID, /* 31, unused */
1889 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1892 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1893 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1895 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1897 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1899 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1901 WARN("Invalid state %#x passed.\n", state);
1902 return D3D_OK;
1905 wined3d_mutex_lock();
1906 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
1907 wined3d_mutex_unlock();
1909 return D3D_OK;
1912 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1913 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1915 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1917 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
1919 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1921 WARN("Invalid state %#x passed.\n", state);
1922 return D3D_OK;
1925 wined3d_mutex_lock();
1926 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1927 wined3d_mutex_unlock();
1929 return D3D_OK;
1932 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
1933 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
1935 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1937 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
1939 wined3d_mutex_lock();
1940 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
1941 wined3d_mutex_unlock();
1943 return D3D_OK;
1946 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
1947 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
1949 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1951 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
1953 wined3d_mutex_lock();
1954 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
1955 wined3d_mutex_unlock();
1957 return D3D_OK;
1960 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
1962 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1963 HRESULT hr;
1965 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1967 wined3d_mutex_lock();
1968 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1969 wined3d_mutex_unlock();
1971 return hr;
1974 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1975 UINT palette_idx, const PALETTEENTRY *entries)
1977 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1979 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
1980 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
1981 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
1983 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
1984 return D3D_OK;
1987 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1988 UINT palette_idx, PALETTEENTRY *entries)
1990 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1992 return D3DERR_INVALIDCALL;
1995 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
1997 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1999 return D3D_OK;
2002 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2004 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2006 return D3DERR_INVALIDCALL;
2009 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2011 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2013 TRACE("iface %p, rect %p.\n", iface, rect);
2015 wined3d_mutex_lock();
2016 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2017 wined3d_mutex_unlock();
2019 return D3D_OK;
2022 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2024 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2026 TRACE("iface %p, rect %p.\n", iface, rect);
2028 wined3d_mutex_lock();
2029 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2030 wined3d_mutex_unlock();
2032 return D3D_OK;
2035 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2037 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2039 TRACE("iface %p, software %#x.\n", iface, software);
2041 wined3d_mutex_lock();
2042 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2043 wined3d_mutex_unlock();
2045 return D3D_OK;
2048 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2050 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2051 BOOL ret;
2053 TRACE("iface %p.\n", iface);
2055 wined3d_mutex_lock();
2056 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2057 wined3d_mutex_unlock();
2059 return ret;
2062 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2064 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2065 HRESULT hr;
2067 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2069 wined3d_mutex_lock();
2070 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2071 wined3d_mutex_unlock();
2073 return hr;
2076 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2078 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2079 float ret;
2081 TRACE("iface %p.\n", iface);
2083 wined3d_mutex_lock();
2084 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2085 wined3d_mutex_unlock();
2087 return ret;
2090 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2091 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2093 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2094 HRESULT hr;
2096 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2097 iface, primitive_type, start_vertex, primitive_count);
2099 wined3d_mutex_lock();
2100 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2101 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2102 vertex_count_from_primitive_count(primitive_type, primitive_count));
2103 wined3d_mutex_unlock();
2105 return hr;
2108 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2109 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2110 UINT vertex_count, UINT start_idx, UINT primitive_count)
2112 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2113 HRESULT hr;
2115 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2116 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2117 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2118 vertex_count, start_idx, primitive_count);
2120 wined3d_mutex_lock();
2121 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2122 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2123 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2124 vertex_count_from_primitive_count(primitive_type, primitive_count));
2125 wined3d_mutex_unlock();
2127 return hr;
2130 /* The caller is responsible for wined3d locking */
2131 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2133 HRESULT hr;
2135 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2137 UINT size = max(device->vertex_buffer_size * 2, min_size);
2138 struct wined3d_buffer *buffer;
2140 TRACE("Growing vertex buffer to %u bytes\n", size);
2142 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2143 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2144 if (FAILED(hr))
2146 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2147 return hr;
2150 if (device->vertex_buffer)
2151 wined3d_buffer_decref(device->vertex_buffer);
2153 device->vertex_buffer = buffer;
2154 device->vertex_buffer_size = size;
2155 device->vertex_buffer_pos = 0;
2157 return D3D_OK;
2160 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2161 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2163 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2164 HRESULT hr;
2165 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2166 UINT size = vtx_count * stride;
2167 UINT vb_pos, align;
2168 BYTE *buffer_data;
2170 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2171 iface, primitive_type, primitive_count, data, stride);
2173 if (!primitive_count)
2175 WARN("primitive_count is 0, returning D3D_OK\n");
2176 return D3D_OK;
2179 wined3d_mutex_lock();
2181 hr = d3d9_device_prepare_vertex_buffer(device, size);
2182 if (FAILED(hr))
2183 goto done;
2185 vb_pos = device->vertex_buffer_pos;
2186 align = vb_pos % stride;
2187 if (align) align = stride - align;
2188 if (vb_pos + size + align > device->vertex_buffer_size)
2189 vb_pos = 0;
2190 else
2191 vb_pos += align;
2193 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2194 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2195 if (FAILED(hr))
2196 goto done;
2197 memcpy(buffer_data, data, size);
2198 wined3d_buffer_unmap(device->vertex_buffer);
2199 device->vertex_buffer_pos = vb_pos + size;
2201 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2202 if (FAILED(hr))
2203 goto done;
2205 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2206 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2207 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2209 done:
2210 wined3d_mutex_unlock();
2211 return hr;
2214 /* The caller is responsible for wined3d locking */
2215 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2217 HRESULT hr;
2219 if (device->index_buffer_size < min_size || !device->index_buffer)
2221 UINT size = max(device->index_buffer_size * 2, min_size);
2222 struct wined3d_buffer *buffer;
2224 TRACE("Growing index buffer to %u bytes\n", size);
2226 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2227 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2228 if (FAILED(hr))
2230 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2231 return hr;
2234 if (device->index_buffer)
2235 wined3d_buffer_decref(device->index_buffer);
2237 device->index_buffer = buffer;
2238 device->index_buffer_size = size;
2239 device->index_buffer_pos = 0;
2241 return D3D_OK;
2244 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2245 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2246 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2247 const void *vertex_data, UINT vertex_stride)
2249 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2250 HRESULT hr;
2251 BYTE *buffer_data;
2253 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2254 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2255 UINT idx_size = idx_count * idx_fmt_size;
2256 UINT ib_pos;
2258 UINT vtx_size = vertex_count * vertex_stride;
2259 UINT vb_pos, align;
2261 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2262 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2263 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2264 index_data, index_format, vertex_data, vertex_stride);
2266 if (!primitive_count)
2268 WARN("primitive_count is 0, returning D3D_OK\n");
2269 return D3D_OK;
2272 wined3d_mutex_lock();
2274 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2275 if (FAILED(hr))
2276 goto done;
2278 vb_pos = device->vertex_buffer_pos;
2279 align = vb_pos % vertex_stride;
2280 if (align) align = vertex_stride - align;
2281 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2282 vb_pos = 0;
2283 else
2284 vb_pos += align;
2286 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2287 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2288 if (FAILED(hr))
2289 goto done;
2290 memcpy(buffer_data, vertex_data, vtx_size);
2291 wined3d_buffer_unmap(device->vertex_buffer);
2292 device->vertex_buffer_pos = vb_pos + vtx_size;
2294 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2295 if (FAILED(hr))
2296 goto done;
2298 ib_pos = device->index_buffer_pos;
2299 align = ib_pos % idx_fmt_size;
2300 if (align) align = idx_fmt_size - align;
2301 if (ib_pos + idx_size + align > device->index_buffer_size)
2302 ib_pos = 0;
2303 else
2304 ib_pos += align;
2306 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2307 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2308 if (FAILED(hr))
2309 goto done;
2310 memcpy(buffer_data, index_data, idx_size);
2311 wined3d_buffer_unmap(device->index_buffer);
2312 device->index_buffer_pos = ib_pos + idx_size;
2314 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2315 if (FAILED(hr))
2316 goto done;
2318 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2319 wined3dformat_from_d3dformat(index_format));
2320 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2322 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2323 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2325 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2326 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2327 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2329 done:
2330 wined3d_mutex_unlock();
2331 return hr;
2334 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2335 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2336 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2338 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2339 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2340 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2341 HRESULT hr;
2343 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2344 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2346 wined3d_mutex_lock();
2347 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2348 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2349 flags, dst_impl->fvf);
2350 wined3d_mutex_unlock();
2352 return hr;
2355 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2356 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2358 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2359 struct d3d9_vertex_declaration *object;
2360 HRESULT hr;
2362 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2364 if (!declaration)
2366 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2367 return D3DERR_INVALIDCALL;
2370 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2371 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2373 return hr;
2376 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2377 IDirect3DVertexDeclaration9 *declaration)
2379 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2380 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2382 TRACE("iface %p, declaration %p.\n", iface, declaration);
2384 wined3d_mutex_lock();
2385 wined3d_device_set_vertex_declaration(device->wined3d_device,
2386 decl_impl ? decl_impl->wined3d_declaration : NULL);
2387 wined3d_mutex_unlock();
2389 return D3D_OK;
2392 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2393 IDirect3DVertexDeclaration9 **declaration)
2395 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2396 struct wined3d_vertex_declaration *wined3d_declaration;
2397 struct d3d9_vertex_declaration *declaration_impl;
2399 TRACE("iface %p, declaration %p.\n", iface, declaration);
2401 if (!declaration) return D3DERR_INVALIDCALL;
2403 wined3d_mutex_lock();
2404 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2406 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2407 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2408 IDirect3DVertexDeclaration9_AddRef(*declaration);
2410 else
2412 *declaration = NULL;
2414 wined3d_mutex_unlock();
2416 TRACE("Returning %p.\n", *declaration);
2417 return D3D_OK;
2420 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2422 struct wined3d_vertex_declaration *wined3d_declaration;
2423 struct fvf_declaration *fvf_decls = device->fvf_decls;
2424 struct d3d9_vertex_declaration *d3d9_declaration;
2425 D3DVERTEXELEMENT9 *elements;
2426 int p, low, high; /* deliberately signed */
2427 HRESULT hr;
2429 TRACE("Searching for declaration for fvf %08x... ", fvf);
2431 low = 0;
2432 high = device->fvf_decl_count - 1;
2433 while (low <= high)
2435 p = (low + high) >> 1;
2436 TRACE("%d ", p);
2438 if (fvf_decls[p].fvf == fvf)
2440 TRACE("found %p.\n", fvf_decls[p].decl);
2441 return fvf_decls[p].decl;
2444 if (fvf_decls[p].fvf < fvf)
2445 low = p + 1;
2446 else
2447 high = p - 1;
2449 TRACE("not found. Creating and inserting at position %d.\n", low);
2451 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2452 return NULL;
2454 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2455 HeapFree(GetProcessHeap(), 0, elements);
2456 if (FAILED(hr))
2457 return NULL;
2459 if (device->fvf_decl_size == device->fvf_decl_count)
2461 UINT grow = max(device->fvf_decl_size / 2, 8);
2463 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2464 if (!fvf_decls)
2466 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2467 return NULL;
2469 device->fvf_decls = fvf_decls;
2470 device->fvf_decl_size += grow;
2473 d3d9_declaration->fvf = fvf;
2474 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2475 wined3d_vertex_declaration_incref(wined3d_declaration);
2476 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2478 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2479 fvf_decls[low].decl = wined3d_declaration;
2480 fvf_decls[low].fvf = fvf;
2481 ++device->fvf_decl_count;
2483 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2485 return wined3d_declaration;
2488 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2490 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2491 struct wined3d_vertex_declaration *decl;
2493 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2495 if (!fvf)
2497 WARN("%#x is not a valid FVF.\n", fvf);
2498 return D3D_OK;
2501 wined3d_mutex_lock();
2502 if (!(decl = device_get_fvf_declaration(device, fvf)))
2504 wined3d_mutex_unlock();
2505 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2506 return D3DERR_DRIVERINTERNALERROR;
2509 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2510 wined3d_mutex_unlock();
2512 return D3D_OK;
2515 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2517 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2518 struct wined3d_vertex_declaration *wined3d_declaration;
2519 struct d3d9_vertex_declaration *d3d9_declaration;
2521 TRACE("iface %p, fvf %p.\n", iface, fvf);
2523 wined3d_mutex_lock();
2524 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2526 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2527 *fvf = d3d9_declaration->fvf;
2529 else
2531 *fvf = 0;
2533 wined3d_mutex_unlock();
2535 TRACE("Returning FVF %#x.\n", *fvf);
2537 return D3D_OK;
2540 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2541 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2543 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2544 struct d3d9_vertexshader *object;
2545 HRESULT hr;
2547 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2549 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2550 if (!object)
2551 return E_OUTOFMEMORY;
2553 hr = vertexshader_init(object, device, byte_code);
2554 if (FAILED(hr))
2556 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2557 HeapFree(GetProcessHeap(), 0, object);
2558 return hr;
2561 TRACE("Created vertex shader %p.\n", object);
2562 *shader = &object->IDirect3DVertexShader9_iface;
2564 return D3D_OK;
2567 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2570 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2572 TRACE("iface %p, shader %p.\n", iface, shader);
2574 wined3d_mutex_lock();
2575 wined3d_device_set_vertex_shader(device->wined3d_device,
2576 shader_obj ? shader_obj->wined3d_shader : NULL);
2577 wined3d_mutex_unlock();
2579 return D3D_OK;
2582 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2584 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2585 struct d3d9_vertexshader *shader_impl;
2586 struct wined3d_shader *wined3d_shader;
2588 TRACE("iface %p, shader %p.\n", iface, shader);
2590 wined3d_mutex_lock();
2591 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2593 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2594 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2595 IDirect3DVertexShader9_AddRef(*shader);
2597 else
2599 *shader = NULL;
2601 wined3d_mutex_unlock();
2603 TRACE("Returning %p.\n", *shader);
2605 return D3D_OK;
2608 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2609 UINT reg_idx, const float *data, UINT count)
2611 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2612 HRESULT hr;
2614 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2616 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2618 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2619 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2620 return D3DERR_INVALIDCALL;
2623 wined3d_mutex_lock();
2624 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2625 wined3d_mutex_unlock();
2627 return hr;
2630 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2631 UINT reg_idx, float *data, UINT count)
2633 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2634 HRESULT hr;
2636 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2638 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2640 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2641 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2642 return D3DERR_INVALIDCALL;
2645 wined3d_mutex_lock();
2646 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2647 wined3d_mutex_unlock();
2649 return hr;
2652 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2653 UINT reg_idx, const int *data, UINT count)
2655 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2656 HRESULT hr;
2658 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2660 wined3d_mutex_lock();
2661 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2662 wined3d_mutex_unlock();
2664 return hr;
2667 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2668 UINT reg_idx, int *data, UINT count)
2670 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2671 HRESULT hr;
2673 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2675 wined3d_mutex_lock();
2676 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2677 wined3d_mutex_unlock();
2679 return hr;
2682 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2683 UINT reg_idx, const BOOL *data, UINT count)
2685 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2686 HRESULT hr;
2688 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2690 wined3d_mutex_lock();
2691 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2692 wined3d_mutex_unlock();
2694 return hr;
2697 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2698 UINT reg_idx, BOOL *data, UINT count)
2700 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2701 HRESULT hr;
2703 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2705 wined3d_mutex_lock();
2706 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2707 wined3d_mutex_unlock();
2709 return hr;
2712 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2713 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2715 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2716 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2717 HRESULT hr;
2719 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2720 iface, stream_idx, buffer, offset, stride);
2722 wined3d_mutex_lock();
2723 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2724 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2725 wined3d_mutex_unlock();
2727 return hr;
2730 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2731 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2733 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2734 struct d3d9_vertexbuffer *buffer_impl;
2735 struct wined3d_buffer *wined3d_buffer;
2736 HRESULT hr;
2738 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2739 iface, stream_idx, buffer, offset, stride);
2741 if (!buffer)
2742 return D3DERR_INVALIDCALL;
2744 wined3d_mutex_lock();
2745 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2746 if (SUCCEEDED(hr) && wined3d_buffer)
2748 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2749 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2750 IDirect3DVertexBuffer9_AddRef(*buffer);
2751 wined3d_buffer_decref(wined3d_buffer);
2753 else
2755 if (FAILED(hr))
2756 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2757 *buffer = NULL;
2759 wined3d_mutex_unlock();
2761 return hr;
2764 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2766 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2767 HRESULT hr;
2769 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2771 wined3d_mutex_lock();
2772 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2773 wined3d_mutex_unlock();
2775 return hr;
2778 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2780 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2781 HRESULT hr;
2783 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2785 wined3d_mutex_lock();
2786 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2787 wined3d_mutex_unlock();
2789 return hr;
2792 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2794 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2795 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2797 TRACE("iface %p, buffer %p.\n", iface, buffer);
2799 wined3d_mutex_lock();
2800 wined3d_device_set_index_buffer(device->wined3d_device,
2801 ib ? ib->wined3d_buffer : NULL,
2802 ib ? ib->format : WINED3DFMT_UNKNOWN);
2803 wined3d_mutex_unlock();
2805 return D3D_OK;
2808 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2810 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2811 enum wined3d_format_id wined3d_format;
2812 struct wined3d_buffer *wined3d_buffer;
2813 struct d3d9_indexbuffer *buffer_impl;
2815 TRACE("iface %p, buffer %p.\n", iface, buffer);
2817 if (!buffer)
2818 return D3DERR_INVALIDCALL;
2820 wined3d_mutex_lock();
2821 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2823 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2824 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2825 IDirect3DIndexBuffer9_AddRef(*buffer);
2827 else
2829 *buffer = NULL;
2831 wined3d_mutex_unlock();
2833 return D3D_OK;
2836 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2837 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2839 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2840 struct d3d9_pixelshader *object;
2841 HRESULT hr;
2843 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2845 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2846 if (!object)
2848 FIXME("Failed to allocate pixel shader memory.\n");
2849 return E_OUTOFMEMORY;
2852 hr = pixelshader_init(object, device, byte_code);
2853 if (FAILED(hr))
2855 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2856 HeapFree(GetProcessHeap(), 0, object);
2857 return hr;
2860 TRACE("Created pixel shader %p.\n", object);
2861 *shader = &object->IDirect3DPixelShader9_iface;
2863 return D3D_OK;
2866 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2868 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2869 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2871 TRACE("iface %p, shader %p.\n", iface, shader);
2873 wined3d_mutex_lock();
2874 wined3d_device_set_pixel_shader(device->wined3d_device,
2875 shader_obj ? shader_obj->wined3d_shader : NULL);
2876 wined3d_mutex_unlock();
2878 return D3D_OK;
2881 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2883 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2884 struct d3d9_pixelshader *shader_impl;
2885 struct wined3d_shader *wined3d_shader;
2887 TRACE("iface %p, shader %p.\n", iface, shader);
2889 if (!shader) return D3DERR_INVALIDCALL;
2891 wined3d_mutex_lock();
2892 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2894 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2895 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2896 IDirect3DPixelShader9_AddRef(*shader);
2898 else
2900 *shader = NULL;
2902 wined3d_mutex_unlock();
2904 TRACE("Returning %p.\n", *shader);
2906 return D3D_OK;
2909 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2910 UINT reg_idx, const float *data, UINT count)
2912 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2913 HRESULT hr;
2915 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2917 wined3d_mutex_lock();
2918 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2919 wined3d_mutex_unlock();
2921 return hr;
2924 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2925 UINT reg_idx, float *data, UINT count)
2927 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2928 HRESULT hr;
2930 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2932 wined3d_mutex_lock();
2933 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2934 wined3d_mutex_unlock();
2936 return hr;
2939 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2940 UINT reg_idx, const int *data, UINT count)
2942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2943 HRESULT hr;
2945 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2947 wined3d_mutex_lock();
2948 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2949 wined3d_mutex_unlock();
2951 return hr;
2954 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2955 UINT reg_idx, int *data, UINT count)
2957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2958 HRESULT hr;
2960 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2962 wined3d_mutex_lock();
2963 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2964 wined3d_mutex_unlock();
2966 return hr;
2969 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2970 UINT reg_idx, const BOOL *data, UINT count)
2972 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2973 HRESULT hr;
2975 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2977 wined3d_mutex_lock();
2978 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2979 wined3d_mutex_unlock();
2981 return hr;
2984 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2985 UINT reg_idx, BOOL *data, UINT count)
2987 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2988 HRESULT hr;
2990 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2992 wined3d_mutex_lock();
2993 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2994 wined3d_mutex_unlock();
2996 return hr;
2999 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3000 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3002 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3003 iface, handle, segment_count, patch_info);
3004 return D3D_OK;
3007 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3008 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3010 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3011 iface, handle, segment_count, patch_info);
3012 return D3D_OK;
3015 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3017 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3018 return D3DERR_INVALIDCALL;
3021 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3023 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3024 struct d3d9_query *object;
3025 HRESULT hr;
3027 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3029 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3030 if (!object)
3031 return E_OUTOFMEMORY;
3033 hr = query_init(object, device, type);
3034 if (FAILED(hr))
3036 WARN("Failed to initialize query, hr %#x.\n", hr);
3037 HeapFree(GetProcessHeap(), 0, object);
3038 return hr;
3041 TRACE("Created query %p.\n", object);
3042 if (query) *query = &object->IDirect3DQuery9_iface;
3043 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3045 return D3D_OK;
3048 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3049 UINT width, UINT height, float *rows, float *columns)
3051 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3052 iface, width, height, rows, columns);
3054 return E_NOTIMPL;
3057 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3058 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3059 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3061 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3062 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3063 iface, src_surface, dst_surface, src_descs, rect_count,
3064 dst_descs, operation, offset_x, offset_y);
3066 return E_NOTIMPL;
3069 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3070 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3071 const RGNDATA *dirty_region, DWORD flags)
3073 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3074 HRESULT hr;
3076 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3077 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3078 dst_window_override, dirty_region, flags);
3080 wined3d_mutex_lock();
3081 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
3082 dst_window_override, dirty_region, flags);
3083 wined3d_mutex_unlock();
3085 return hr;
3088 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3090 FIXME("iface %p, priority %p stub!\n", iface, priority);
3092 return E_NOTIMPL;
3095 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3097 FIXME("iface %p, priority %d stub!\n", iface, priority);
3099 return E_NOTIMPL;
3102 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3104 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3106 return E_NOTIMPL;
3109 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3110 IDirect3DResource9 **resources, UINT32 resource_count)
3112 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3113 iface, resources, resource_count);
3115 return E_NOTIMPL;
3118 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3120 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3122 return E_NOTIMPL;
3125 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3127 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3129 *max_latency = 2;
3131 return E_NOTIMPL;
3134 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3136 static int i;
3138 TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
3140 if (!i++)
3141 FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
3143 return D3D_OK;
3146 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3147 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3148 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3150 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3151 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3152 iface, width, height, format, multisample_type, multisample_quality,
3153 lockable, surface, shared_handle, usage);
3155 *surface = NULL;
3156 if (shared_handle)
3157 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3159 return E_NOTIMPL;
3162 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3163 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3164 HANDLE *shared_handle, DWORD usage)
3166 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3167 iface, width, height, format, pool, surface, shared_handle, usage);
3169 return E_NOTIMPL;
3172 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3173 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3174 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3176 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3177 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3178 iface, width, height, format, multisample_type, multisample_quality,
3179 discard, surface, shared_handle, usage);
3181 *surface = NULL;
3182 if (shared_handle)
3183 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3185 return E_NOTIMPL;
3188 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3189 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3191 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3192 struct wined3d_swapchain_desc swapchain_desc;
3193 struct wined3d_display_mode wined3d_mode;
3194 HRESULT hr;
3196 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3198 if (mode)
3200 wined3d_mode.width = mode->Width;
3201 wined3d_mode.height = mode->Height;
3202 wined3d_mode.refresh_rate = mode->RefreshRate;
3203 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
3204 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
3207 wined3d_mutex_lock();
3209 if (device->vertex_buffer)
3211 wined3d_buffer_decref(device->vertex_buffer);
3212 device->vertex_buffer = NULL;
3213 device->vertex_buffer_size = 0;
3215 if (device->index_buffer)
3217 wined3d_buffer_decref(device->index_buffer);
3218 device->index_buffer = NULL;
3219 device->index_buffer_size = 0;
3222 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
3223 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
3224 mode ? &wined3d_mode : NULL, reset_enum_callback, FALSE);
3225 wined3d_mutex_unlock();
3227 return hr;
3230 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3231 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3233 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3234 struct wined3d_display_mode wined3d_mode;
3235 HRESULT hr;
3237 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3238 iface, swapchain_idx, mode, rotation);
3240 if (mode->Size != sizeof(*mode))
3241 return D3DERR_INVALIDCALL;
3243 wined3d_mutex_lock();
3244 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3245 (enum wined3d_display_rotation *)rotation);
3246 wined3d_mutex_unlock();
3248 if (SUCCEEDED(hr))
3250 mode->Width = wined3d_mode.width;
3251 mode->Height = wined3d_mode.height;
3252 mode->RefreshRate = wined3d_mode.refresh_rate;
3253 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3254 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3257 return hr;
3260 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3262 /* IUnknown */
3263 d3d9_device_QueryInterface,
3264 d3d9_device_AddRef,
3265 d3d9_device_Release,
3266 /* IDirect3DDevice9 */
3267 d3d9_device_TestCooperativeLevel,
3268 d3d9_device_GetAvailableTextureMem,
3269 d3d9_device_EvictManagedResources,
3270 d3d9_device_GetDirect3D,
3271 d3d9_device_GetDeviceCaps,
3272 d3d9_device_GetDisplayMode,
3273 d3d9_device_GetCreationParameters,
3274 d3d9_device_SetCursorProperties,
3275 d3d9_device_SetCursorPosition,
3276 d3d9_device_ShowCursor,
3277 d3d9_device_CreateAdditionalSwapChain,
3278 d3d9_device_GetSwapChain,
3279 d3d9_device_GetNumberOfSwapChains,
3280 d3d9_device_Reset,
3281 d3d9_device_Present,
3282 d3d9_device_GetBackBuffer,
3283 d3d9_device_GetRasterStatus,
3284 d3d9_device_SetDialogBoxMode,
3285 d3d9_device_SetGammaRamp,
3286 d3d9_device_GetGammaRamp,
3287 d3d9_device_CreateTexture,
3288 d3d9_device_CreateVolumeTexture,
3289 d3d9_device_CreateCubeTexture,
3290 d3d9_device_CreateVertexBuffer,
3291 d3d9_device_CreateIndexBuffer,
3292 d3d9_device_CreateRenderTarget,
3293 d3d9_device_CreateDepthStencilSurface,
3294 d3d9_device_UpdateSurface,
3295 d3d9_device_UpdateTexture,
3296 d3d9_device_GetRenderTargetData,
3297 d3d9_device_GetFrontBufferData,
3298 d3d9_device_StretchRect,
3299 d3d9_device_ColorFill,
3300 d3d9_device_CreateOffscreenPlainSurface,
3301 d3d9_device_SetRenderTarget,
3302 d3d9_device_GetRenderTarget,
3303 d3d9_device_SetDepthStencilSurface,
3304 d3d9_device_GetDepthStencilSurface,
3305 d3d9_device_BeginScene,
3306 d3d9_device_EndScene,
3307 d3d9_device_Clear,
3308 d3d9_device_SetTransform,
3309 d3d9_device_GetTransform,
3310 d3d9_device_MultiplyTransform,
3311 d3d9_device_SetViewport,
3312 d3d9_device_GetViewport,
3313 d3d9_device_SetMaterial,
3314 d3d9_device_GetMaterial,
3315 d3d9_device_SetLight,
3316 d3d9_device_GetLight,
3317 d3d9_device_LightEnable,
3318 d3d9_device_GetLightEnable,
3319 d3d9_device_SetClipPlane,
3320 d3d9_device_GetClipPlane,
3321 d3d9_device_SetRenderState,
3322 d3d9_device_GetRenderState,
3323 d3d9_device_CreateStateBlock,
3324 d3d9_device_BeginStateBlock,
3325 d3d9_device_EndStateBlock,
3326 d3d9_device_SetClipStatus,
3327 d3d9_device_GetClipStatus,
3328 d3d9_device_GetTexture,
3329 d3d9_device_SetTexture,
3330 d3d9_device_GetTextureStageState,
3331 d3d9_device_SetTextureStageState,
3332 d3d9_device_GetSamplerState,
3333 d3d9_device_SetSamplerState,
3334 d3d9_device_ValidateDevice,
3335 d3d9_device_SetPaletteEntries,
3336 d3d9_device_GetPaletteEntries,
3337 d3d9_device_SetCurrentTexturePalette,
3338 d3d9_device_GetCurrentTexturePalette,
3339 d3d9_device_SetScissorRect,
3340 d3d9_device_GetScissorRect,
3341 d3d9_device_SetSoftwareVertexProcessing,
3342 d3d9_device_GetSoftwareVertexProcessing,
3343 d3d9_device_SetNPatchMode,
3344 d3d9_device_GetNPatchMode,
3345 d3d9_device_DrawPrimitive,
3346 d3d9_device_DrawIndexedPrimitive,
3347 d3d9_device_DrawPrimitiveUP,
3348 d3d9_device_DrawIndexedPrimitiveUP,
3349 d3d9_device_ProcessVertices,
3350 d3d9_device_CreateVertexDeclaration,
3351 d3d9_device_SetVertexDeclaration,
3352 d3d9_device_GetVertexDeclaration,
3353 d3d9_device_SetFVF,
3354 d3d9_device_GetFVF,
3355 d3d9_device_CreateVertexShader,
3356 d3d9_device_SetVertexShader,
3357 d3d9_device_GetVertexShader,
3358 d3d9_device_SetVertexShaderConstantF,
3359 d3d9_device_GetVertexShaderConstantF,
3360 d3d9_device_SetVertexShaderConstantI,
3361 d3d9_device_GetVertexShaderConstantI,
3362 d3d9_device_SetVertexShaderConstantB,
3363 d3d9_device_GetVertexShaderConstantB,
3364 d3d9_device_SetStreamSource,
3365 d3d9_device_GetStreamSource,
3366 d3d9_device_SetStreamSourceFreq,
3367 d3d9_device_GetStreamSourceFreq,
3368 d3d9_device_SetIndices,
3369 d3d9_device_GetIndices,
3370 d3d9_device_CreatePixelShader,
3371 d3d9_device_SetPixelShader,
3372 d3d9_device_GetPixelShader,
3373 d3d9_device_SetPixelShaderConstantF,
3374 d3d9_device_GetPixelShaderConstantF,
3375 d3d9_device_SetPixelShaderConstantI,
3376 d3d9_device_GetPixelShaderConstantI,
3377 d3d9_device_SetPixelShaderConstantB,
3378 d3d9_device_GetPixelShaderConstantB,
3379 d3d9_device_DrawRectPatch,
3380 d3d9_device_DrawTriPatch,
3381 d3d9_device_DeletePatch,
3382 d3d9_device_CreateQuery,
3383 /* IDirect3DDevice9Ex */
3384 d3d9_device_SetConvolutionMonoKernel,
3385 d3d9_device_ComposeRects,
3386 d3d9_device_PresentEx,
3387 d3d9_device_GetGPUThreadPriority,
3388 d3d9_device_SetGPUThreadPriority,
3389 d3d9_device_WaitForVBlank,
3390 d3d9_device_CheckResourceResidency,
3391 d3d9_device_SetMaximumFrameLatency,
3392 d3d9_device_GetMaximumFrameLatency,
3393 d3d9_device_CheckDeviceState,
3394 d3d9_device_CreateRenderTargetEx,
3395 d3d9_device_CreateOffscreenPlainSurfaceEx,
3396 d3d9_device_CreateDepthStencilSurfaceEx,
3397 d3d9_device_ResetEx,
3398 d3d9_device_GetDisplayModeEx,
3401 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3403 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3406 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3407 struct wined3d_device *device)
3409 TRACE("device_parent %p, device %p.\n", device_parent, device);
3412 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3414 TRACE("device_parent %p.\n", device_parent);
3417 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3418 void *container_parent, struct wined3d_surface *surface, void **parent,
3419 const struct wined3d_parent_ops **parent_ops)
3421 struct d3d9_device *device = device_from_device_parent(device_parent);
3422 struct d3d9_surface *d3d_surface;
3424 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3425 device_parent, container_parent, surface, parent, parent_ops);
3427 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3429 FIXME("Failed to allocate surface memory.\n");
3430 return D3DERR_OUTOFVIDEOMEMORY;
3433 surface_init(d3d_surface, surface, device, parent_ops);
3434 *parent = d3d_surface;
3435 TRACE("Created surface %p.\n", d3d_surface);
3437 d3d_surface->container = container_parent;
3438 IDirect3DDevice9Ex_Release(d3d_surface->parent_device);
3439 d3d_surface->parent_device = NULL;
3441 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3442 d3d_surface->forwardReference = container_parent;
3444 return D3D_OK;
3447 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3448 void *container_parent, struct wined3d_volume *volume, void **parent,
3449 const struct wined3d_parent_ops **parent_ops)
3451 struct d3d9_volume *d3d_volume;
3453 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3454 device_parent, container_parent, volume, parent, parent_ops);
3456 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3457 return E_OUTOFMEMORY;
3459 volume_init(d3d_volume, volume, parent_ops);
3460 *parent = d3d_volume;
3461 TRACE("Created volume %p.\n", d3d_volume);
3463 d3d_volume->container = container_parent;
3465 IDirect3DVolume9_Release(&d3d_volume->IDirect3DVolume9_iface);
3466 d3d_volume->forwardReference = container_parent;
3468 return D3D_OK;
3471 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3472 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3474 struct d3d9_device *device = device_from_device_parent(device_parent);
3475 struct wined3d_resource_desc texture_desc;
3476 struct d3d9_surface *d3d_surface;
3477 struct wined3d_texture *texture;
3478 HRESULT hr;
3480 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3481 device_parent, container_parent, desc, surface);
3483 if (container_parent == device_parent)
3484 container_parent = &device->IDirect3DDevice9Ex_iface;
3486 texture_desc = *desc;
3487 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3488 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3489 WINED3D_SURFACE_MAPPABLE, container_parent, &d3d9_null_wined3d_parent_ops, &texture)))
3491 WARN("Failed to create texture, hr %#x.\n", hr);
3492 return hr;
3495 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3496 wined3d_surface_incref(*surface);
3497 wined3d_texture_decref(texture);
3499 d3d_surface = wined3d_surface_get_parent(*surface);
3500 d3d_surface->forwardReference = NULL;
3501 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3503 return hr;
3506 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3507 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3509 struct d3d9_device *device = device_from_device_parent(device_parent);
3510 struct d3d9_swapchain *d3d_swapchain;
3511 HRESULT hr;
3513 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3515 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3516 if (FAILED(hr))
3518 WARN("Failed to create swapchain, hr %#x.\n", hr);
3519 *swapchain = NULL;
3520 return hr;
3523 *swapchain = d3d_swapchain->wined3d_swapchain;
3524 wined3d_swapchain_incref(*swapchain);
3525 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3527 return hr;
3530 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3532 device_parent_wined3d_device_created,
3533 device_parent_mode_changed,
3534 device_parent_surface_created,
3535 device_parent_volume_created,
3536 device_parent_create_swapchain_surface,
3537 device_parent_create_swapchain,
3540 static void setup_fpu(void)
3542 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3543 WORD cw;
3544 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3545 cw = (cw & ~0xf3f) | 0x3f;
3546 __asm__ volatile ("fldcw %0" : : "m" (cw));
3547 #elif defined(__i386__) && defined(_MSC_VER)
3548 WORD cw;
3549 __asm fnstcw cw;
3550 cw = (cw & ~0xf3f) | 0x3f;
3551 __asm fldcw cw;
3552 #else
3553 FIXME("FPU setup not implemented for this platform.\n");
3554 #endif
3557 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3558 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3559 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3561 struct wined3d_swapchain_desc *swapchain_desc;
3562 UINT i, count = 1;
3563 HRESULT hr;
3565 if (mode)
3566 FIXME("Ignoring display mode.\n");
3568 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3569 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3570 device->refcount = 1;
3572 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3574 wined3d_mutex_lock();
3575 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3576 &device->device_parent, &device->wined3d_device);
3577 if (FAILED(hr))
3579 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3580 wined3d_mutex_unlock();
3581 return hr;
3584 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3586 WINED3DCAPS caps;
3588 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3589 count = caps.NumberOfAdaptersInGroup;
3592 if (flags & D3DCREATE_MULTITHREADED)
3593 wined3d_device_set_multithreaded(device->wined3d_device);
3595 if (!parameters->Windowed)
3597 if (!focus_window)
3598 focus_window = parameters->hDeviceWindow;
3599 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3601 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3602 wined3d_device_decref(device->wined3d_device);
3603 wined3d_mutex_unlock();
3604 return hr;
3607 for (i = 0; i < count; ++i)
3609 HWND device_window = parameters[i].hDeviceWindow;
3611 if (!device_window) device_window = focus_window;
3612 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3613 parameters[i].BackBufferWidth,
3614 parameters[i].BackBufferHeight);
3618 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3619 if (!swapchain_desc)
3621 ERR("Failed to allocate wined3d parameters.\n");
3622 wined3d_device_decref(device->wined3d_device);
3623 wined3d_mutex_unlock();
3624 return E_OUTOFMEMORY;
3627 for (i = 0; i < count; ++i)
3629 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i]);
3632 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3633 if (FAILED(hr))
3635 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3636 wined3d_device_release_focus_window(device->wined3d_device);
3637 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3638 wined3d_device_decref(device->wined3d_device);
3639 wined3d_mutex_unlock();
3640 return hr;
3643 wined3d_mutex_unlock();
3645 for (i = 0; i < count; ++i)
3647 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3649 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3651 /* Initialize the converted declaration array. This creates a valid pointer
3652 * and when adding decls HeapReAlloc() can be used without further checking. */
3653 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3654 if (!device->fvf_decls)
3656 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3657 wined3d_mutex_lock();
3658 wined3d_device_uninit_3d(device->wined3d_device);
3659 wined3d_device_release_focus_window(device->wined3d_device);
3660 wined3d_device_decref(device->wined3d_device);
3661 wined3d_mutex_unlock();
3662 return E_OUTOFMEMORY;
3665 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3666 device->d3d_parent = parent;
3668 return D3D_OK;