d3d9: Properly retrieve an interface pointer in IDirect3DDevice9Impl_GetBackBuffer().
[wine/multimedia.git] / dlls / d3d9 / device.c
blob20635a0fa84de016ad1cfc5828d9db46e0b45821
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 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
30 BYTE *c = (BYTE *)&format;
32 /* Don't translate FOURCC formats */
33 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
35 switch(format)
37 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
38 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
39 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
40 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
41 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
42 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
43 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
44 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
45 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
46 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
47 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
48 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
49 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
50 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
51 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
52 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
53 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
54 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
55 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
56 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
57 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
58 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
59 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
60 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
61 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
62 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
63 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
64 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
65 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
66 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
67 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
68 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
69 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
70 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
71 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
72 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
73 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
74 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
75 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
76 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
77 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
78 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
79 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
80 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
81 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
82 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
83 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
84 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
85 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
86 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
87 default:
88 FIXME("Unhandled wined3d format %#x.\n", format);
89 return D3DFMT_UNKNOWN;
93 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
95 BYTE *c = (BYTE *)&format;
97 /* Don't translate FOURCC formats */
98 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
100 switch(format)
102 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
103 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
104 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
105 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
106 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
107 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
108 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
109 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
110 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
111 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
112 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
113 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
114 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
115 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
116 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
117 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
119 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
120 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
131 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
132 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
133 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
134 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
135 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
136 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
137 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
138 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
139 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
140 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
141 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
142 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
143 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
144 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
145 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
146 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
147 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
148 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
149 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
150 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
151 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
152 default:
153 FIXME("Unhandled D3DFORMAT %#x\n", format);
154 return WINED3DFMT_UNKNOWN;
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
160 switch(primitive_type)
162 case D3DPT_POINTLIST:
163 return primitive_count;
165 case D3DPT_LINELIST:
166 return primitive_count * 2;
168 case D3DPT_LINESTRIP:
169 return primitive_count + 1;
171 case D3DPT_TRIANGLELIST:
172 return primitive_count * 3;
174 case D3DPT_TRIANGLESTRIP:
175 case D3DPT_TRIANGLEFAN:
176 return primitive_count + 2;
178 default:
179 FIXME("Unhandled primitive type %#x\n", primitive_type);
180 return 0;
184 static inline IDirect3DDevice9Impl *impl_from_IDirect3DDevice9Ex(IDirect3DDevice9Ex *iface)
186 return CONTAINING_RECORD(iface, IDirect3DDevice9Impl, IDirect3DDevice9Ex_iface);
189 static HRESULT WINAPI IDirect3DDevice9Impl_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
191 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
193 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
194 || IsEqualGUID(riid, &IID_IUnknown))
196 IDirect3DDevice9Ex_AddRef(iface);
197 *out = iface;
198 return S_OK;
201 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
203 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
205 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
206 * It doesn't matter with which function the device was created. */
207 if (!device->d3d_parent->extended)
209 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
210 *out = NULL;
211 return E_NOINTERFACE;
214 IDirect3DDevice9Ex_AddRef(iface);
215 *out = iface;
216 return S_OK;
219 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
221 *out = NULL;
222 return E_NOINTERFACE;
225 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(IDirect3DDevice9Ex *iface)
227 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
228 ULONG ref = InterlockedIncrement(&This->ref);
230 TRACE("%p increasing refcount to %u.\n", iface, ref);
232 return ref;
235 static ULONG WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Release(IDirect3DDevice9Ex *iface)
237 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
238 ULONG ref;
240 if (device->inDestruction)
241 return 0;
243 ref = InterlockedDecrement(&device->ref);
245 TRACE("%p decreasing refcount to %u.\n", iface, ref);
247 if (!ref)
249 unsigned i;
250 device->inDestruction = TRUE;
252 wined3d_mutex_lock();
253 for (i = 0; i < device->fvf_decl_count; ++i)
255 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
257 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
259 wined3d_device_uninit_3d(device->wined3d_device);
260 wined3d_device_release_focus_window(device->wined3d_device);
261 wined3d_device_decref(device->wined3d_device);
262 wined3d_mutex_unlock();
264 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
266 HeapFree(GetProcessHeap(), 0, device);
269 return ref;
272 static HRESULT WINAPI IDirect3DDevice9Impl_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
274 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
276 TRACE("iface %p.\n", iface);
278 if (This->notreset)
280 TRACE("D3D9 device is marked not reset.\n");
281 return D3DERR_DEVICENOTRESET;
284 return D3D_OK;
287 static UINT WINAPI IDirect3DDevice9Impl_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
289 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
290 HRESULT hr;
292 TRACE("iface %p.\n", iface);
294 wined3d_mutex_lock();
295 hr = wined3d_device_get_available_texture_mem(This->wined3d_device);
296 wined3d_mutex_unlock();
298 return hr;
301 static HRESULT WINAPI IDirect3DDevice9Impl_EvictManagedResources(IDirect3DDevice9Ex *iface)
303 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
305 TRACE("iface %p.\n", iface);
307 wined3d_mutex_lock();
308 wined3d_device_evict_managed_resources(This->wined3d_device);
309 wined3d_mutex_unlock();
311 return D3D_OK;
314 static HRESULT WINAPI IDirect3DDevice9Impl_GetDirect3D(IDirect3DDevice9Ex *iface,
315 IDirect3D9 **ppD3D9)
317 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
319 TRACE("iface %p, d3d9 %p.\n", iface, ppD3D9);
321 if (NULL == ppD3D9) {
322 return D3DERR_INVALIDCALL;
325 return IDirect3D9Ex_QueryInterface(&This->d3d_parent->IDirect3D9Ex_iface,
326 &IID_IDirect3D9, (void **)ppD3D9);
329 static HRESULT WINAPI IDirect3DDevice9Impl_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *pCaps)
331 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
332 HRESULT hrc = D3D_OK;
333 WINED3DCAPS *pWineCaps;
335 TRACE("iface %p, caps %p.\n", iface, pCaps);
337 if(NULL == pCaps){
338 return D3DERR_INVALIDCALL;
340 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
341 if(pWineCaps == NULL){
342 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
345 memset(pCaps, 0, sizeof(*pCaps));
347 wined3d_mutex_lock();
348 hrc = wined3d_device_get_device_caps(This->wined3d_device, pWineCaps);
349 wined3d_mutex_unlock();
351 WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
352 HeapFree(GetProcessHeap(), 0, pWineCaps);
354 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
355 pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
357 filter_caps(pCaps);
359 TRACE("Returning %p %p\n", This, pCaps);
360 return hrc;
363 static HRESULT WINAPI IDirect3DDevice9Impl_GetDisplayMode(IDirect3DDevice9Ex *iface,
364 UINT iSwapChain, D3DDISPLAYMODE *pMode)
366 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
367 HRESULT hr;
369 TRACE("iface %p, swapchain %u, mode %p.\n", iface, iSwapChain, pMode);
371 wined3d_mutex_lock();
372 hr = wined3d_device_get_display_mode(This->wined3d_device, iSwapChain, (struct wined3d_display_mode *)pMode);
373 wined3d_mutex_unlock();
375 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
377 return hr;
380 static HRESULT WINAPI IDirect3DDevice9Impl_GetCreationParameters(IDirect3DDevice9Ex *iface,
381 D3DDEVICE_CREATION_PARAMETERS *pParameters)
383 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
384 HRESULT hr;
386 TRACE("iface %p, parameters %p.\n", iface, pParameters);
388 wined3d_mutex_lock();
389 hr = wined3d_device_get_creation_parameters(This->wined3d_device,
390 (struct wined3d_device_creation_parameters *)pParameters);
391 wined3d_mutex_unlock();
393 return hr;
396 static HRESULT WINAPI IDirect3DDevice9Impl_SetCursorProperties(IDirect3DDevice9Ex *iface,
397 UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9 *pCursorBitmap)
399 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
400 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pCursorBitmap);
401 HRESULT hr;
403 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
404 iface, XHotSpot, YHotSpot, pCursorBitmap);
406 if (!pCursorBitmap)
408 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
409 return D3DERR_INVALIDCALL;
412 wined3d_mutex_lock();
413 hr = wined3d_device_set_cursor_properties(This->wined3d_device, XHotSpot, YHotSpot, pSurface->wined3d_surface);
414 wined3d_mutex_unlock();
416 return hr;
419 static void WINAPI IDirect3DDevice9Impl_SetCursorPosition(IDirect3DDevice9Ex *iface,
420 int XScreenSpace, int YScreenSpace, DWORD Flags)
422 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
424 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, XScreenSpace, YScreenSpace, Flags);
426 wined3d_mutex_lock();
427 wined3d_device_set_cursor_position(This->wined3d_device, XScreenSpace, YScreenSpace, Flags);
428 wined3d_mutex_unlock();
431 static BOOL WINAPI IDirect3DDevice9Impl_ShowCursor(IDirect3DDevice9Ex *iface, BOOL bShow)
433 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
434 BOOL ret;
436 TRACE("iface %p, show %#x.\n", iface, bShow);
438 wined3d_mutex_lock();
439 ret = wined3d_device_show_cursor(This->wined3d_device, bShow);
440 wined3d_mutex_unlock();
442 return ret;
445 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
446 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
448 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
449 IDirect3DSwapChain9Impl *object;
450 HRESULT hr;
452 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
453 iface, present_parameters, swapchain);
455 if (SUCCEEDED(hr = d3d9_swapchain_create(device, present_parameters, &object)))
456 *swapchain = &object->IDirect3DSwapChain9_iface;
458 return hr;
461 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_GetSwapChain(IDirect3DDevice9Ex *iface,
462 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
464 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
465 struct wined3d_swapchain *wined3d_swapchain = NULL;
466 IDirect3DSwapChain9Impl *swapchain_impl;
467 HRESULT hr;
469 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
471 wined3d_mutex_lock();
472 hr = wined3d_device_get_swapchain(This->wined3d_device, swapchain_idx, &wined3d_swapchain);
473 if (SUCCEEDED(hr) && wined3d_swapchain)
475 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
476 *swapchain = &swapchain_impl->IDirect3DSwapChain9_iface;
477 IDirect3DSwapChain9_AddRef(*swapchain);
478 wined3d_swapchain_decref(wined3d_swapchain);
480 else
482 *swapchain = NULL;
484 wined3d_mutex_unlock();
486 return hr;
489 static UINT WINAPI IDirect3DDevice9Impl_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
491 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
492 UINT count;
494 TRACE("iface %p.\n", iface);
496 wined3d_mutex_lock();
497 count = wined3d_device_get_swapchain_count(This->wined3d_device);
498 wined3d_mutex_unlock();
500 return count;
503 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
505 struct wined3d_resource_desc desc;
507 wined3d_resource_get_desc(resource, &desc);
508 if (desc.pool == WINED3D_POOL_DEFAULT)
510 IDirect3DSurface9Impl *surface;
512 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
514 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
515 return D3DERR_INVALIDCALL;
518 surface = wined3d_resource_get_parent(resource);
519 if (surface->ref)
521 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
522 return D3DERR_INVALIDCALL;
525 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
528 return D3D_OK;
531 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Reset(IDirect3DDevice9Ex *iface,
532 D3DPRESENT_PARAMETERS *pPresentationParameters)
534 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
535 struct wined3d_swapchain_desc swapchain_desc;
536 HRESULT hr;
538 TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
540 /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
541 * such objects can still be used for rendering after their external d3d9 object has been destroyed.
542 * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
543 * make use of the hidden reference and destroys the objects.
545 * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
546 * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
548 wined3d_mutex_lock();
550 swapchain_desc.backbuffer_width = pPresentationParameters->BackBufferWidth;
551 swapchain_desc.backbuffer_height = pPresentationParameters->BackBufferHeight;
552 swapchain_desc.backbuffer_format = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
553 swapchain_desc.backbuffer_count = pPresentationParameters->BackBufferCount;
554 swapchain_desc.multisample_type = pPresentationParameters->MultiSampleType;
555 swapchain_desc.multisample_quality = pPresentationParameters->MultiSampleQuality;
556 swapchain_desc.swap_effect = pPresentationParameters->SwapEffect;
557 swapchain_desc.device_window = pPresentationParameters->hDeviceWindow;
558 swapchain_desc.windowed = pPresentationParameters->Windowed;
559 swapchain_desc.enable_auto_depth_stencil = pPresentationParameters->EnableAutoDepthStencil;
560 swapchain_desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
561 swapchain_desc.flags = pPresentationParameters->Flags;
562 swapchain_desc.refresh_rate = pPresentationParameters->FullScreen_RefreshRateInHz;
563 swapchain_desc.swap_interval = pPresentationParameters->PresentationInterval;
564 swapchain_desc.auto_restore_display_mode = TRUE;
566 hr = wined3d_device_reset(This->wined3d_device, &swapchain_desc, reset_enum_callback);
567 if (FAILED(hr))
568 This->notreset = TRUE;
569 else
570 This->notreset = FALSE;
572 wined3d_mutex_unlock();
574 return hr;
577 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Present(IDirect3DDevice9Ex *iface,
578 const RECT *pSourceRect, const RECT *pDestRect, HWND hDestWindowOverride,
579 const RGNDATA *pDirtyRegion)
581 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
582 HRESULT hr;
584 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
585 iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
587 wined3d_mutex_lock();
588 hr = wined3d_device_present(This->wined3d_device, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
589 wined3d_mutex_unlock();
591 return hr;
594 static HRESULT WINAPI IDirect3DDevice9Impl_GetBackBuffer(IDirect3DDevice9Ex *iface,
595 UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 **ppBackBuffer)
597 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
598 struct wined3d_surface *wined3d_surface = NULL;
599 IDirect3DSurface9Impl *surface_impl;
600 HRESULT hr;
602 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
603 iface, iSwapChain, BackBuffer, Type, ppBackBuffer);
605 wined3d_mutex_lock();
606 hr = wined3d_device_get_back_buffer(This->wined3d_device, iSwapChain,
607 BackBuffer, (enum wined3d_backbuffer_type)Type, &wined3d_surface);
608 if (SUCCEEDED(hr) && wined3d_surface && ppBackBuffer)
610 surface_impl = wined3d_surface_get_parent(wined3d_surface);
611 *ppBackBuffer = &surface_impl->IDirect3DSurface9_iface;
612 IDirect3DSurface9_AddRef(*ppBackBuffer);
613 wined3d_surface_decref(wined3d_surface);
615 wined3d_mutex_unlock();
617 return hr;
619 static HRESULT WINAPI IDirect3DDevice9Impl_GetRasterStatus(IDirect3DDevice9Ex *iface,
620 UINT iSwapChain, D3DRASTER_STATUS *pRasterStatus)
622 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
623 HRESULT hr;
625 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, iSwapChain, pRasterStatus);
627 wined3d_mutex_lock();
628 hr = wined3d_device_get_raster_status(This->wined3d_device,
629 iSwapChain, (struct wined3d_raster_status *)pRasterStatus);
630 wined3d_mutex_unlock();
632 return hr;
635 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(IDirect3DDevice9Ex *iface,
636 BOOL bEnableDialogs)
638 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
639 HRESULT hr;
641 TRACE("iface %p, enable %#x.\n", iface, bEnableDialogs);
643 wined3d_mutex_lock();
644 hr = wined3d_device_set_dialog_box_mode(This->wined3d_device, bEnableDialogs);
645 wined3d_mutex_unlock();
647 return hr;
650 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
651 DWORD Flags, const D3DGAMMARAMP *pRamp)
653 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
655 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, iSwapChain, Flags, pRamp);
657 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
658 wined3d_mutex_lock();
659 wined3d_device_set_gamma_ramp(This->wined3d_device, iSwapChain, Flags, (const struct wined3d_gamma_ramp *)pRamp);
660 wined3d_mutex_unlock();
663 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
664 D3DGAMMARAMP *pRamp)
666 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
668 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, iSwapChain, pRamp);
670 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
671 wined3d_mutex_lock();
672 wined3d_device_get_gamma_ramp(This->wined3d_device, iSwapChain, (struct wined3d_gamma_ramp *)pRamp);
673 wined3d_mutex_unlock();
676 static HRESULT WINAPI IDirect3DDevice9Impl_CreateTexture(IDirect3DDevice9Ex *iface,
677 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
678 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
680 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
681 struct d3d9_texture *object;
682 BOOL set_mem = FALSE;
683 HRESULT hr;
685 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
686 iface, width, height, levels, usage, format, pool, texture, shared_handle);
688 if (shared_handle)
690 if (pool == D3DPOOL_SYSTEMMEM)
692 if (levels != 1)
693 return D3DERR_INVALIDCALL;
694 set_mem = TRUE;
696 else
697 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
700 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
701 if (!object)
703 ERR("Failed to allocate texture memory.\n");
704 return D3DERR_OUTOFVIDEOMEMORY;
707 hr = texture_init(object, This, width, height, levels, usage, format, pool);
708 if (FAILED(hr))
710 WARN("Failed to initialize texture, hr %#x.\n", hr);
711 HeapFree(GetProcessHeap(), 0, object);
712 return hr;
715 if (set_mem)
717 struct wined3d_resource *resource;
718 IDirect3DSurface9Impl *surface;
720 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
721 surface = wined3d_resource_get_parent(resource);
722 wined3d_surface_set_mem(surface->wined3d_surface, *shared_handle);
725 TRACE("Created texture %p.\n", object);
726 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
728 return D3D_OK;
731 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
732 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
733 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
735 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
736 struct d3d9_texture *object;
737 HRESULT hr;
739 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
740 iface, width, height, depth, levels);
741 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
742 usage, format, pool, texture, shared_handle);
744 if (shared_handle)
745 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
747 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
748 if (!object)
750 ERR("Failed to allocate volume texture memory.\n");
751 return D3DERR_OUTOFVIDEOMEMORY;
754 hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
755 if (FAILED(hr))
757 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
758 HeapFree(GetProcessHeap(), 0, object);
759 return hr;
762 TRACE("Created volume texture %p.\n", object);
763 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
765 return D3D_OK;
768 static HRESULT WINAPI IDirect3DDevice9Impl_CreateCubeTexture(IDirect3DDevice9Ex *iface,
769 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
770 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
772 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
773 struct d3d9_texture *object;
774 HRESULT hr;
776 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
777 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
779 if (shared_handle)
780 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
782 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
783 if (!object)
785 ERR("Failed to allocate cube texture memory.\n");
786 return D3DERR_OUTOFVIDEOMEMORY;
789 hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
790 if (FAILED(hr))
792 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
793 HeapFree(GetProcessHeap(), 0, object);
794 return hr;
797 TRACE("Created cube texture %p.\n", object);
798 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
800 return D3D_OK;
803 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
804 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
805 HANDLE *shared_handle)
807 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
808 IDirect3DVertexBuffer9Impl *object;
809 HRESULT hr;
811 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
812 iface, size, usage, fvf, pool, buffer, shared_handle);
814 if (shared_handle)
815 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
817 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
818 if (!object)
820 ERR("Failed to allocate buffer memory.\n");
821 return D3DERR_OUTOFVIDEOMEMORY;
824 hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
825 if (FAILED(hr))
827 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
828 HeapFree(GetProcessHeap(), 0, object);
829 return hr;
832 TRACE("Created vertex buffer %p.\n", object);
833 *buffer = &object->IDirect3DVertexBuffer9_iface;
835 return D3D_OK;
838 static HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
839 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
840 HANDLE *shared_handle)
842 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
843 IDirect3DIndexBuffer9Impl *object;
844 HRESULT hr;
846 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
847 iface, size, usage, format, pool, buffer, shared_handle);
849 if (shared_handle)
850 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
852 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
853 if (!object)
855 ERR("Failed to allocate buffer memory.\n");
856 return D3DERR_OUTOFVIDEOMEMORY;
859 hr = indexbuffer_init(object, This, size, usage, format, pool);
860 if (FAILED(hr))
862 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
863 HeapFree(GetProcessHeap(), 0, object);
864 return hr;
867 TRACE("Created index buffer %p.\n", object);
868 *buffer = &object->IDirect3DIndexBuffer9_iface;
870 return D3D_OK;
873 static HRESULT IDirect3DDevice9Impl_CreateSurface(IDirect3DDevice9Impl *device, UINT Width,
874 UINT Height, D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level,
875 IDirect3DSurface9 **ppSurface, UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample,
876 DWORD MultisampleQuality)
878 IDirect3DSurface9Impl *object;
879 HRESULT hr;
881 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p.\n"
882 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
883 device, Width, Height, Format, Lockable, Discard, Level, ppSurface, Usage, Pool,
884 MultiSample, MultisampleQuality);
886 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
887 if (!object)
889 FIXME("Failed to allocate surface memory.\n");
890 return D3DERR_OUTOFVIDEOMEMORY;
893 hr = surface_init(object, device, Width, Height, Format, Lockable, Discard, Level, Usage, Pool,
894 MultiSample, MultisampleQuality);
895 if (FAILED(hr))
897 WARN("Failed to initialize surface, hr %#x.\n", hr);
898 HeapFree(GetProcessHeap(), 0, object);
899 return hr;
902 TRACE("Created surface %p.\n", object);
903 *ppSurface = &object->IDirect3DSurface9_iface;
905 return D3D_OK;
908 static HRESULT WINAPI IDirect3DDevice9Impl_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT Width,
909 UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality,
910 BOOL Lockable, IDirect3DSurface9 **ppSurface, HANDLE *shared_handle)
912 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
913 HRESULT hr;
915 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
916 "lockable %#x, surface %p, shared_handle %p.\n",
917 iface, Width, Height, Format, MultiSample, MultisampleQuality,
918 Lockable, ppSurface, shared_handle);
920 if (shared_handle)
921 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
923 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, Lockable,
924 FALSE /* Discard */, 0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,
925 MultiSample, MultisampleQuality);
927 return hr;
930 static HRESULT WINAPI IDirect3DDevice9Impl_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface,
931 UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
932 DWORD MultisampleQuality, BOOL Discard, IDirect3DSurface9 **ppSurface,
933 HANDLE *shared_handle)
935 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
936 HRESULT hr;
938 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
939 "discard %#x, surface %p, shared_handle %p.\n",
940 iface, Width, Height, Format, MultiSample, MultisampleQuality,
941 Discard, ppSurface, shared_handle);
943 if (shared_handle)
944 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
946 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
947 Discard, 0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample,
948 MultisampleQuality);
950 return hr;
954 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateSurface(IDirect3DDevice9Ex *iface,
955 IDirect3DSurface9 *pSourceSurface, const RECT *pSourceRect,
956 IDirect3DSurface9 *pDestinationSurface, const POINT *pDestPoint)
958 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
959 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(pSourceSurface);
960 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(pDestinationSurface);
961 HRESULT hr;
963 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
964 iface, pSourceSurface, pSourceRect, pDestinationSurface, pDestPoint);
966 wined3d_mutex_lock();
967 hr = wined3d_device_update_surface(This->wined3d_device, src->wined3d_surface, pSourceRect,
968 dst->wined3d_surface, pDestPoint);
969 wined3d_mutex_unlock();
971 return hr;
974 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateTexture(IDirect3DDevice9Ex *iface,
975 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
977 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
978 HRESULT hr;
980 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
982 wined3d_mutex_lock();
983 hr = wined3d_device_update_texture(This->wined3d_device,
984 ((IDirect3DBaseTexture9Impl *)src_texture)->wined3d_texture,
985 ((IDirect3DBaseTexture9Impl *)dst_texture)->wined3d_texture);
986 wined3d_mutex_unlock();
988 return hr;
991 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTargetData(IDirect3DDevice9Ex *iface,
992 IDirect3DSurface9 *pRenderTarget, IDirect3DSurface9 *pDestSurface)
994 IDirect3DSurface9Impl *renderTarget = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
995 IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
996 HRESULT hr;
998 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, pRenderTarget, pDestSurface);
1000 wined3d_mutex_lock();
1001 hr = wined3d_surface_get_render_target_data(destSurface->wined3d_surface, renderTarget->wined3d_surface);
1002 wined3d_mutex_unlock();
1004 return hr;
1007 static HRESULT WINAPI IDirect3DDevice9Impl_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1008 UINT iSwapChain, IDirect3DSurface9 *pDestSurface)
1010 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1011 IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1012 HRESULT hr;
1014 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, iSwapChain, pDestSurface);
1016 wined3d_mutex_lock();
1017 hr = wined3d_device_get_front_buffer_data(This->wined3d_device, iSwapChain, destSurface->wined3d_surface);
1018 wined3d_mutex_unlock();
1020 return hr;
1023 static HRESULT WINAPI IDirect3DDevice9Impl_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1024 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1026 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1027 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1028 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1029 HRESULT hr = D3DERR_INVALIDCALL;
1030 struct wined3d_resource_desc src_desc, dst_desc;
1031 struct wined3d_resource *wined3d_resource;
1033 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1034 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1036 wined3d_mutex_lock();
1037 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1038 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1040 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1041 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1043 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1045 if (This->in_scene)
1047 WARN("Rejecting depth / stencil blit while in scene.\n");
1048 goto done;
1051 if (src_rect)
1053 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1054 || src_rect->bottom != src_desc.height)
1056 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1057 wine_dbgstr_rect(src_rect));
1058 goto done;
1061 if (dst_rect)
1063 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1064 || dst_rect->bottom != dst_desc.height)
1066 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1067 wine_dbgstr_rect(dst_rect));
1068 goto done;
1071 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1073 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1074 goto done;
1078 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1079 if (hr == WINEDDERR_INVALIDRECT)
1080 hr = D3DERR_INVALIDCALL;
1082 done:
1083 wined3d_mutex_unlock();
1084 return hr;
1087 static HRESULT WINAPI IDirect3DDevice9Impl_ColorFill(IDirect3DDevice9Ex *iface,
1088 IDirect3DSurface9 *pSurface, const RECT *pRect, D3DCOLOR color)
1090 const struct wined3d_color c =
1092 ((color >> 16) & 0xff) / 255.0f,
1093 ((color >> 8) & 0xff) / 255.0f,
1094 (color & 0xff) / 255.0f,
1095 ((color >> 24) & 0xff) / 255.0f,
1097 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1098 IDirect3DSurface9Impl *surface = unsafe_impl_from_IDirect3DSurface9(pSurface);
1099 struct wined3d_resource *wined3d_resource;
1100 struct wined3d_resource_desc desc;
1101 HRESULT hr;
1103 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, pSurface, pRect, color);
1105 wined3d_mutex_lock();
1107 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
1108 wined3d_resource_get_desc(wined3d_resource, &desc);
1110 /* This method is only allowed with surfaces that are render targets, or
1111 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1112 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1114 wined3d_mutex_unlock();
1115 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1116 return D3DERR_INVALIDCALL;
1119 /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1120 hr = wined3d_device_color_fill(This->wined3d_device, surface->wined3d_surface, pRect, &c);
1122 wined3d_mutex_unlock();
1124 return hr;
1127 static HRESULT WINAPI IDirect3DDevice9Impl_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1128 UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface,
1129 HANDLE *shared_handle)
1131 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1132 HRESULT hr;
1134 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1135 iface, Width, Height, Format, Pool, ppSurface, shared_handle);
1137 if (shared_handle)
1138 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1140 if (Pool == D3DPOOL_MANAGED)
1142 FIXME("Attempting to create a managed offscreen plain surface\n");
1143 return D3DERR_INVALIDCALL;
1146 'Off-screen plain surfaces are always lockable, regardless of their pool types.'
1147 but then...
1148 D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
1149 Why, their always lockable?
1150 should I change the usage to dynamic?
1152 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
1153 FALSE /* Discard */, 0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */,
1154 Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1156 return hr;
1159 /* TODO: move to wineD3D */
1160 static HRESULT WINAPI IDirect3DDevice9Impl_SetRenderTarget(IDirect3DDevice9Ex *iface,
1161 DWORD RenderTargetIndex, IDirect3DSurface9 *pRenderTarget)
1163 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1164 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
1165 HRESULT hr;
1167 TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, pRenderTarget);
1169 if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1171 WARN("Invalid index %u specified.\n", RenderTargetIndex);
1172 return D3DERR_INVALIDCALL;
1175 wined3d_mutex_lock();
1176 hr = wined3d_device_set_render_target(This->wined3d_device, RenderTargetIndex,
1177 pSurface ? pSurface->wined3d_surface : NULL, TRUE);
1178 wined3d_mutex_unlock();
1180 return hr;
1183 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTarget(IDirect3DDevice9Ex *iface,
1184 DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget)
1186 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1187 struct wined3d_surface *wined3d_surface;
1188 HRESULT hr;
1190 TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, ppRenderTarget);
1192 if (ppRenderTarget == NULL) {
1193 return D3DERR_INVALIDCALL;
1196 if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1198 WARN("Invalid index %u specified.\n", RenderTargetIndex);
1199 return D3DERR_INVALIDCALL;
1202 wined3d_mutex_lock();
1203 hr = wined3d_device_get_render_target(This->wined3d_device, RenderTargetIndex, &wined3d_surface);
1204 if (SUCCEEDED(hr))
1206 *ppRenderTarget = wined3d_surface_get_parent(wined3d_surface);
1207 IDirect3DSurface9_AddRef(*ppRenderTarget);
1208 wined3d_surface_decref(wined3d_surface);
1210 else
1212 if (hr != WINED3DERR_NOTFOUND)
1213 WARN("Failed to get render target %u, hr %#x.\n", RenderTargetIndex, hr);
1214 *ppRenderTarget = NULL;
1216 wined3d_mutex_unlock();
1218 return hr;
1221 static HRESULT WINAPI IDirect3DDevice9Impl_SetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1222 IDirect3DSurface9 *pZStencilSurface)
1224 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1225 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pZStencilSurface);
1226 HRESULT hr;
1228 TRACE("iface %p, depth_stencil %p.\n", iface, pZStencilSurface);
1230 wined3d_mutex_lock();
1231 hr = wined3d_device_set_depth_stencil(This->wined3d_device, pSurface ? pSurface->wined3d_surface : NULL);
1232 wined3d_mutex_unlock();
1234 return hr;
1237 static HRESULT WINAPI IDirect3DDevice9Impl_GetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1238 IDirect3DSurface9 **ppZStencilSurface)
1240 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1241 struct wined3d_surface *wined3d_surface;
1242 HRESULT hr;
1244 TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1246 if(ppZStencilSurface == NULL){
1247 return D3DERR_INVALIDCALL;
1250 wined3d_mutex_lock();
1251 hr = wined3d_device_get_depth_stencil(This->wined3d_device, &wined3d_surface);
1252 if (SUCCEEDED(hr))
1254 *ppZStencilSurface = wined3d_surface_get_parent(wined3d_surface);
1255 IDirect3DSurface9_AddRef(*ppZStencilSurface);
1256 wined3d_surface_decref(wined3d_surface);
1258 else
1260 if (hr != WINED3DERR_NOTFOUND)
1261 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1262 *ppZStencilSurface = NULL;
1264 wined3d_mutex_unlock();
1266 return hr;
1269 static HRESULT WINAPI IDirect3DDevice9Impl_BeginScene(IDirect3DDevice9Ex *iface)
1271 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1272 HRESULT hr;
1274 TRACE("iface %p.\n", iface);
1276 wined3d_mutex_lock();
1277 hr = wined3d_device_begin_scene(This->wined3d_device);
1278 if (SUCCEEDED(hr)) This->in_scene = TRUE;
1279 wined3d_mutex_unlock();
1281 return hr;
1284 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_EndScene(IDirect3DDevice9Ex *iface)
1286 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1287 HRESULT hr;
1289 TRACE("iface %p.\n", iface);
1291 wined3d_mutex_lock();
1292 hr = wined3d_device_end_scene(This->wined3d_device);
1293 if (SUCCEEDED(hr)) This->in_scene = FALSE;
1294 wined3d_mutex_unlock();
1296 return hr;
1299 static HRESULT WINAPI IDirect3DDevice9Impl_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1300 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1302 const struct wined3d_color c =
1304 ((color >> 16) & 0xff) / 255.0f,
1305 ((color >> 8) & 0xff) / 255.0f,
1306 (color & 0xff) / 255.0f,
1307 ((color >> 24) & 0xff) / 255.0f,
1309 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1310 HRESULT hr;
1312 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1313 iface, rect_count, rects, flags, color, z, stencil);
1315 wined3d_mutex_lock();
1316 hr = wined3d_device_clear(This->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1317 wined3d_mutex_unlock();
1319 return hr;
1322 static HRESULT WINAPI IDirect3DDevice9Impl_SetTransform(IDirect3DDevice9Ex *iface,
1323 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *lpMatrix)
1325 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1326 HRESULT hr;
1328 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1330 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1331 wined3d_mutex_lock();
1332 hr = wined3d_device_set_transform(This->wined3d_device, State, (const struct wined3d_matrix *)lpMatrix);
1333 wined3d_mutex_unlock();
1335 return hr;
1338 static HRESULT WINAPI IDirect3DDevice9Impl_GetTransform(IDirect3DDevice9Ex *iface,
1339 D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
1341 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1342 HRESULT hr;
1344 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1346 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1347 wined3d_mutex_lock();
1348 hr = wined3d_device_get_transform(This->wined3d_device, State, (struct wined3d_matrix *)pMatrix);
1349 wined3d_mutex_unlock();
1351 return hr;
1354 static HRESULT WINAPI IDirect3DDevice9Impl_MultiplyTransform(IDirect3DDevice9Ex *iface,
1355 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
1357 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1358 HRESULT hr;
1360 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1362 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1363 wined3d_mutex_lock();
1364 hr = wined3d_device_multiply_transform(This->wined3d_device, State, (const struct wined3d_matrix *)pMatrix);
1365 wined3d_mutex_unlock();
1367 return hr;
1370 static HRESULT WINAPI IDirect3DDevice9Impl_SetViewport(IDirect3DDevice9Ex *iface,
1371 const D3DVIEWPORT9 *pViewport)
1373 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1374 HRESULT hr;
1376 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1378 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1379 wined3d_mutex_lock();
1380 hr = wined3d_device_set_viewport(This->wined3d_device, (const struct wined3d_viewport *)pViewport);
1381 wined3d_mutex_unlock();
1383 return hr;
1386 static HRESULT WINAPI IDirect3DDevice9Impl_GetViewport(IDirect3DDevice9Ex *iface,
1387 D3DVIEWPORT9 *pViewport)
1389 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1390 HRESULT hr;
1392 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1394 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1395 wined3d_mutex_lock();
1396 hr = wined3d_device_get_viewport(This->wined3d_device, (struct wined3d_viewport *)pViewport);
1397 wined3d_mutex_unlock();
1399 return hr;
1402 static HRESULT WINAPI IDirect3DDevice9Impl_SetMaterial(IDirect3DDevice9Ex *iface,
1403 const D3DMATERIAL9 *pMaterial)
1405 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1406 HRESULT hr;
1408 TRACE("iface %p, material %p.\n", iface, pMaterial);
1410 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1411 wined3d_mutex_lock();
1412 hr = wined3d_device_set_material(This->wined3d_device, (const struct wined3d_material *)pMaterial);
1413 wined3d_mutex_unlock();
1415 return hr;
1418 static HRESULT WINAPI IDirect3DDevice9Impl_GetMaterial(IDirect3DDevice9Ex *iface,
1419 D3DMATERIAL9 *pMaterial)
1421 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1422 HRESULT hr;
1424 TRACE("iface %p, material %p.\n", iface, pMaterial);
1426 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1427 wined3d_mutex_lock();
1428 hr = wined3d_device_get_material(This->wined3d_device, (struct wined3d_material *)pMaterial);
1429 wined3d_mutex_unlock();
1431 return hr;
1434 static HRESULT WINAPI IDirect3DDevice9Impl_SetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1435 const D3DLIGHT9 *pLight)
1437 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1438 HRESULT hr;
1440 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1442 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1443 wined3d_mutex_lock();
1444 hr = wined3d_device_set_light(This->wined3d_device, Index, (const struct wined3d_light *)pLight);
1445 wined3d_mutex_unlock();
1447 return hr;
1450 static HRESULT WINAPI IDirect3DDevice9Impl_GetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1451 D3DLIGHT9 *pLight)
1453 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1454 HRESULT hr;
1456 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1458 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1459 wined3d_mutex_lock();
1460 hr = wined3d_device_get_light(This->wined3d_device, Index, (struct wined3d_light *)pLight);
1461 wined3d_mutex_unlock();
1463 return hr;
1466 static HRESULT WINAPI IDirect3DDevice9Impl_LightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1467 BOOL Enable)
1469 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1470 HRESULT hr;
1472 TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1474 wined3d_mutex_lock();
1475 hr = wined3d_device_set_light_enable(This->wined3d_device, Index, Enable);
1476 wined3d_mutex_unlock();
1478 return hr;
1481 static HRESULT WINAPI IDirect3DDevice9Impl_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1482 BOOL *pEnable)
1484 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1485 HRESULT hr;
1487 TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1489 wined3d_mutex_lock();
1490 hr = wined3d_device_get_light_enable(This->wined3d_device, Index, pEnable);
1491 wined3d_mutex_unlock();
1493 return hr;
1496 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1497 const float *pPlane)
1499 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1500 HRESULT hr;
1502 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1504 wined3d_mutex_lock();
1505 hr = wined3d_device_set_clip_plane(This->wined3d_device, Index, pPlane);
1506 wined3d_mutex_unlock();
1508 return hr;
1511 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1512 float *pPlane)
1514 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1515 HRESULT hr;
1517 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1519 wined3d_mutex_lock();
1520 hr = wined3d_device_get_clip_plane(This->wined3d_device, Index, pPlane);
1521 wined3d_mutex_unlock();
1523 return hr;
1526 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetRenderState(IDirect3DDevice9Ex *iface,
1527 D3DRENDERSTATETYPE State, DWORD Value)
1529 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1530 HRESULT hr;
1532 TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1534 wined3d_mutex_lock();
1535 hr = wined3d_device_set_render_state(This->wined3d_device, State, Value);
1536 wined3d_mutex_unlock();
1538 return hr;
1541 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderState(IDirect3DDevice9Ex *iface,
1542 D3DRENDERSTATETYPE State, DWORD *pValue)
1544 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1545 HRESULT hr;
1547 TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1549 wined3d_mutex_lock();
1550 hr = wined3d_device_get_render_state(This->wined3d_device, State, pValue);
1551 wined3d_mutex_unlock();
1553 return hr;
1556 static HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(IDirect3DDevice9Ex *iface,
1557 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1559 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1560 IDirect3DStateBlock9Impl *object;
1561 HRESULT hr;
1563 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1565 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1567 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1568 return D3DERR_INVALIDCALL;
1571 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1572 if (!object)
1574 ERR("Failed to allocate stateblock memory.\n");
1575 return E_OUTOFMEMORY;
1578 hr = stateblock_init(object, This, type, NULL);
1579 if (FAILED(hr))
1581 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1582 HeapFree(GetProcessHeap(), 0, object);
1583 return hr;
1586 TRACE("Created stateblock %p.\n", object);
1587 *stateblock = &object->IDirect3DStateBlock9_iface;
1589 return D3D_OK;
1592 static HRESULT WINAPI IDirect3DDevice9Impl_BeginStateBlock(IDirect3DDevice9Ex *iface)
1594 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1595 HRESULT hr;
1597 TRACE("iface %p.\n", iface);
1599 wined3d_mutex_lock();
1600 hr = wined3d_device_begin_stateblock(This->wined3d_device);
1601 wined3d_mutex_unlock();
1603 return hr;
1606 static HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface,
1607 IDirect3DStateBlock9 **stateblock)
1609 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1610 struct wined3d_stateblock *wined3d_stateblock;
1611 IDirect3DStateBlock9Impl *object;
1612 HRESULT hr;
1614 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1616 wined3d_mutex_lock();
1617 hr = wined3d_device_end_stateblock(This->wined3d_device, &wined3d_stateblock);
1618 wined3d_mutex_unlock();
1619 if (FAILED(hr))
1621 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1622 return hr;
1625 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1626 if (!object)
1628 ERR("Failed to allocate stateblock memory.\n");
1629 wined3d_mutex_lock();
1630 wined3d_stateblock_decref(wined3d_stateblock);
1631 wined3d_mutex_unlock();
1632 return E_OUTOFMEMORY;
1635 hr = stateblock_init(object, This, 0, wined3d_stateblock);
1636 if (FAILED(hr))
1638 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1639 wined3d_mutex_lock();
1640 wined3d_stateblock_decref(wined3d_stateblock);
1641 wined3d_mutex_unlock();
1642 HeapFree(GetProcessHeap(), 0, object);
1643 return hr;
1646 TRACE("Created stateblock %p.\n", object);
1647 *stateblock = &object->IDirect3DStateBlock9_iface;
1649 return D3D_OK;
1652 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipStatus(IDirect3DDevice9Ex *iface,
1653 const D3DCLIPSTATUS9 *pClipStatus)
1655 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1656 HRESULT hr;
1658 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1660 wined3d_mutex_lock();
1661 hr = wined3d_device_set_clip_status(This->wined3d_device, (const struct wined3d_clip_status *)pClipStatus);
1662 wined3d_mutex_unlock();
1664 return hr;
1667 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipStatus(IDirect3DDevice9Ex *iface,
1668 D3DCLIPSTATUS9 *pClipStatus)
1670 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1671 HRESULT hr;
1673 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1675 wined3d_mutex_lock();
1676 hr = wined3d_device_get_clip_status(This->wined3d_device, (struct wined3d_clip_status *)pClipStatus);
1677 wined3d_mutex_unlock();
1679 return hr;
1682 static HRESULT WINAPI IDirect3DDevice9Impl_GetTexture(IDirect3DDevice9Ex *iface, DWORD Stage,
1683 IDirect3DBaseTexture9 **ppTexture)
1685 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1686 struct wined3d_texture *wined3d_texture = NULL;
1687 HRESULT hr;
1689 TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1691 if(ppTexture == NULL){
1692 return D3DERR_INVALIDCALL;
1695 wined3d_mutex_lock();
1696 hr = wined3d_device_get_texture(This->wined3d_device, Stage, &wined3d_texture);
1697 if (SUCCEEDED(hr) && wined3d_texture)
1699 *ppTexture = wined3d_texture_get_parent(wined3d_texture);
1700 IDirect3DBaseTexture9_AddRef(*ppTexture);
1701 wined3d_texture_decref(wined3d_texture);
1703 else
1705 if (FAILED(hr))
1707 WARN("Call to get texture (%u) failed (%p).\n", Stage, wined3d_texture);
1709 *ppTexture = NULL;
1711 wined3d_mutex_unlock();
1713 return hr;
1716 static HRESULT WINAPI IDirect3DDevice9Impl_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage,
1717 IDirect3DBaseTexture9 *texture)
1719 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
1720 HRESULT hr;
1722 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1724 wined3d_mutex_lock();
1725 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1726 texture ? ((IDirect3DBaseTexture9Impl *)texture)->wined3d_texture : NULL);
1727 wined3d_mutex_unlock();
1729 return hr;
1732 static const enum wined3d_texture_stage_state tss_lookup[] =
1734 WINED3D_TSS_INVALID, /* 0, unused */
1735 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1736 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1737 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1738 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1739 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1740 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1741 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1742 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1743 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1744 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1745 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1746 WINED3D_TSS_INVALID, /* 12, unused */
1747 WINED3D_TSS_INVALID, /* 13, unused */
1748 WINED3D_TSS_INVALID, /* 14, unused */
1749 WINED3D_TSS_INVALID, /* 15, unused */
1750 WINED3D_TSS_INVALID, /* 16, unused */
1751 WINED3D_TSS_INVALID, /* 17, unused */
1752 WINED3D_TSS_INVALID, /* 18, unused */
1753 WINED3D_TSS_INVALID, /* 19, unused */
1754 WINED3D_TSS_INVALID, /* 20, unused */
1755 WINED3D_TSS_INVALID, /* 21, unused */
1756 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1757 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1758 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1759 WINED3D_TSS_INVALID, /* 25, unused */
1760 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1761 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1762 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1763 WINED3D_TSS_INVALID, /* 29, unused */
1764 WINED3D_TSS_INVALID, /* 30, unused */
1765 WINED3D_TSS_INVALID, /* 31, unused */
1766 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1769 static HRESULT WINAPI IDirect3DDevice9Impl_GetTextureStageState(IDirect3DDevice9Ex *iface,
1770 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1772 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1773 HRESULT hr;
1775 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1777 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1779 WARN("Invalid Type %#x passed.\n", Type);
1780 return D3D_OK;
1783 wined3d_mutex_lock();
1784 hr = wined3d_device_get_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], pValue);
1785 wined3d_mutex_unlock();
1787 return hr;
1790 static HRESULT WINAPI IDirect3DDevice9Impl_SetTextureStageState(IDirect3DDevice9Ex *iface,
1791 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1793 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1794 HRESULT hr;
1796 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1798 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1800 WARN("Invalid Type %#x passed.\n", Type);
1801 return D3D_OK;
1804 wined3d_mutex_lock();
1805 hr = wined3d_device_set_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], Value);
1806 wined3d_mutex_unlock();
1808 return hr;
1811 static HRESULT WINAPI IDirect3DDevice9Impl_GetSamplerState(IDirect3DDevice9Ex *iface, DWORD Sampler,
1812 D3DSAMPLERSTATETYPE Type, DWORD *pValue)
1814 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1815 HRESULT hr;
1817 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, Sampler, Type, pValue);
1819 wined3d_mutex_lock();
1820 hr = wined3d_device_get_sampler_state(This->wined3d_device, Sampler, Type, pValue);
1821 wined3d_mutex_unlock();
1823 return hr;
1826 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetSamplerState(IDirect3DDevice9Ex *iface,
1827 DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value)
1829 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1830 HRESULT hr;
1832 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, Sampler, Type, Value);
1834 wined3d_mutex_lock();
1835 hr = wined3d_device_set_sampler_state(This->wined3d_device, Sampler, Type, Value);
1836 wined3d_mutex_unlock();
1838 return hr;
1841 static HRESULT WINAPI IDirect3DDevice9Impl_ValidateDevice(IDirect3DDevice9Ex *iface,
1842 DWORD *pNumPasses)
1844 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1845 HRESULT hr;
1847 TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1849 wined3d_mutex_lock();
1850 hr = wined3d_device_validate_device(This->wined3d_device, pNumPasses);
1851 wined3d_mutex_unlock();
1853 return hr;
1856 static HRESULT WINAPI IDirect3DDevice9Impl_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1857 UINT PaletteNumber, const PALETTEENTRY *pEntries)
1859 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1861 return D3DERR_INVALIDCALL;
1864 static HRESULT WINAPI IDirect3DDevice9Impl_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1865 UINT PaletteNumber, PALETTEENTRY *pEntries)
1867 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1869 return D3DERR_INVALIDCALL;
1872 static HRESULT WINAPI IDirect3DDevice9Impl_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1873 UINT PaletteNumber)
1875 FIXME("iface %p, palette_idx %u unimplemented.\n", iface, PaletteNumber);
1877 return D3DERR_INVALIDCALL;
1880 static HRESULT WINAPI IDirect3DDevice9Impl_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1881 UINT *PaletteNumber)
1883 FIXME("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1885 return D3DERR_INVALIDCALL;
1888 static HRESULT WINAPI IDirect3DDevice9Impl_SetScissorRect(IDirect3DDevice9Ex *iface,
1889 const RECT *pRect)
1891 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1892 HRESULT hr;
1894 TRACE("iface %p, rect %p.\n", iface, pRect);
1896 wined3d_mutex_lock();
1897 hr = wined3d_device_set_scissor_rect(This->wined3d_device, pRect);
1898 wined3d_mutex_unlock();
1900 return hr;
1903 static HRESULT WINAPI IDirect3DDevice9Impl_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *pRect)
1905 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1906 HRESULT hr;
1908 TRACE("iface %p, rect %p.\n", iface, pRect);
1910 wined3d_mutex_lock();
1911 hr = wined3d_device_get_scissor_rect(This->wined3d_device, pRect);
1912 wined3d_mutex_unlock();
1914 return hr;
1917 static HRESULT WINAPI IDirect3DDevice9Impl_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface,
1918 BOOL bSoftware)
1920 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1921 HRESULT hr;
1923 TRACE("iface %p, software %#x.\n", iface, bSoftware);
1925 wined3d_mutex_lock();
1926 hr = wined3d_device_set_software_vertex_processing(This->wined3d_device, bSoftware);
1927 wined3d_mutex_unlock();
1929 return hr;
1932 static BOOL WINAPI IDirect3DDevice9Impl_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
1934 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1935 BOOL ret;
1937 TRACE("iface %p.\n", iface);
1939 wined3d_mutex_lock();
1940 ret = wined3d_device_get_software_vertex_processing(This->wined3d_device);
1941 wined3d_mutex_unlock();
1943 return ret;
1946 static HRESULT WINAPI IDirect3DDevice9Impl_SetNPatchMode(IDirect3DDevice9Ex *iface, float nSegments)
1948 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1949 HRESULT hr;
1951 TRACE("iface %p, segment_count %.8e.\n", iface, nSegments);
1953 wined3d_mutex_lock();
1954 hr = wined3d_device_set_npatch_mode(This->wined3d_device, nSegments);
1955 wined3d_mutex_unlock();
1957 return hr;
1960 static float WINAPI IDirect3DDevice9Impl_GetNPatchMode(IDirect3DDevice9Ex *iface)
1962 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1963 float ret;
1965 TRACE("iface %p.\n", iface);
1967 wined3d_mutex_lock();
1968 ret = wined3d_device_get_npatch_mode(This->wined3d_device);
1969 wined3d_mutex_unlock();
1971 return ret;
1974 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(IDirect3DDevice9Ex *iface,
1975 D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1977 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1978 HRESULT hr;
1980 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1981 iface, PrimitiveType, StartVertex, PrimitiveCount);
1983 wined3d_mutex_lock();
1984 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1985 hr = wined3d_device_draw_primitive(This->wined3d_device, StartVertex,
1986 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1987 wined3d_mutex_unlock();
1989 return hr;
1992 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
1993 D3DPRIMITIVETYPE PrimitiveType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices,
1994 UINT startIndex, UINT primCount)
1996 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1997 HRESULT hr;
1999 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2000 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2001 iface, PrimitiveType, BaseVertexIndex, MinVertexIndex,
2002 NumVertices, startIndex, primCount);
2004 wined3d_mutex_lock();
2005 wined3d_device_set_base_vertex_index(This->wined3d_device, BaseVertexIndex);
2006 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2007 hr = wined3d_device_draw_indexed_primitive(This->wined3d_device, startIndex,
2008 vertex_count_from_primitive_count(PrimitiveType, primCount));
2009 wined3d_mutex_unlock();
2011 return hr;
2014 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2015 D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
2016 UINT VertexStreamZeroStride)
2018 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2019 HRESULT hr;
2021 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2022 iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
2024 wined3d_mutex_lock();
2025 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2026 hr = wined3d_device_draw_primitive_up(This->wined3d_device,
2027 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
2028 pVertexStreamZeroData, VertexStreamZeroStride);
2029 wined3d_mutex_unlock();
2031 return hr;
2034 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2035 D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
2036 UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
2037 const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
2039 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2040 HRESULT hr;
2042 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
2043 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2044 iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
2045 pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
2047 wined3d_mutex_lock();
2048 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2049 hr = wined3d_device_draw_indexed_primitive_up(This->wined3d_device,
2050 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
2051 wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
2052 wined3d_mutex_unlock();
2054 return hr;
2057 static HRESULT WINAPI IDirect3DDevice9Impl_ProcessVertices(IDirect3DDevice9Ex *iface,
2058 UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9 *pDestBuffer,
2059 IDirect3DVertexDeclaration9 *pVertexDecl, DWORD Flags)
2061 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2062 IDirect3DVertexBuffer9Impl *dest = unsafe_impl_from_IDirect3DVertexBuffer9(pDestBuffer);
2063 IDirect3DVertexDeclaration9Impl *Decl = unsafe_impl_from_IDirect3DVertexDeclaration9(pVertexDecl);
2064 HRESULT hr;
2066 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2067 iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags);
2069 wined3d_mutex_lock();
2070 hr = wined3d_device_process_vertices(This->wined3d_device, SrcStartIndex, DestIndex, VertexCount,
2071 dest->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags, dest->fvf);
2072 wined3d_mutex_unlock();
2074 return hr;
2077 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2078 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2080 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
2081 IDirect3DVertexDeclaration9Impl *object;
2082 HRESULT hr;
2084 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2086 if (!declaration)
2088 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2089 return D3DERR_INVALIDCALL;
2092 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2093 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2095 return hr;
2098 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2099 IDirect3DVertexDeclaration9 *declaration)
2101 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2102 IDirect3DVertexDeclaration9Impl *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2103 HRESULT hr;
2105 TRACE("iface %p, declaration %p.\n", iface, declaration);
2107 wined3d_mutex_lock();
2108 hr = wined3d_device_set_vertex_declaration(This->wined3d_device,
2109 decl_impl ? decl_impl->wineD3DVertexDeclaration : NULL);
2110 wined3d_mutex_unlock();
2112 return hr;
2115 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2116 IDirect3DVertexDeclaration9 **declaration)
2118 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2119 struct wined3d_vertex_declaration *wined3d_declaration = NULL;
2120 HRESULT hr;
2122 TRACE("iface %p, declaration %p.\n", iface, declaration);
2124 if (!declaration) return D3DERR_INVALIDCALL;
2126 wined3d_mutex_lock();
2127 hr = wined3d_device_get_vertex_declaration(This->wined3d_device, &wined3d_declaration);
2128 if (SUCCEEDED(hr) && wined3d_declaration)
2130 *declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2131 IDirect3DVertexDeclaration9_AddRef(*declaration);
2132 wined3d_vertex_declaration_decref(wined3d_declaration);
2134 else
2136 *declaration = NULL;
2138 wined3d_mutex_unlock();
2140 TRACE("Returning %p.\n", *declaration);
2141 return hr;
2144 static struct wined3d_vertex_declaration *device_get_fvf_declaration(IDirect3DDevice9Impl *device, DWORD fvf)
2146 struct wined3d_vertex_declaration *wined3d_declaration;
2147 struct fvf_declaration *fvf_decls = device->fvf_decls;
2148 IDirect3DVertexDeclaration9Impl *d3d9_declaration;
2149 D3DVERTEXELEMENT9 *elements;
2150 int p, low, high; /* deliberately signed */
2151 HRESULT hr;
2153 TRACE("Searching for declaration for fvf %08x... ", fvf);
2155 low = 0;
2156 high = device->fvf_decl_count - 1;
2157 while (low <= high)
2159 p = (low + high) >> 1;
2160 TRACE("%d ", p);
2162 if (fvf_decls[p].fvf == fvf)
2164 TRACE("found %p.\n", fvf_decls[p].decl);
2165 return fvf_decls[p].decl;
2168 if (fvf_decls[p].fvf < fvf)
2169 low = p + 1;
2170 else
2171 high = p - 1;
2173 TRACE("not found. Creating and inserting at position %d.\n", low);
2175 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2176 return NULL;
2178 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2179 HeapFree(GetProcessHeap(), 0, elements);
2180 if (FAILED(hr))
2181 return NULL;
2183 if (device->fvf_decl_size == device->fvf_decl_count)
2185 UINT grow = max(device->fvf_decl_size / 2, 8);
2187 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2188 if (!fvf_decls)
2190 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2191 return NULL;
2193 device->fvf_decls = fvf_decls;
2194 device->fvf_decl_size += grow;
2197 d3d9_declaration->convFVF = fvf;
2198 wined3d_declaration = d3d9_declaration->wineD3DVertexDeclaration;
2199 wined3d_vertex_declaration_incref(wined3d_declaration);
2200 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2202 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2203 fvf_decls[low].decl = wined3d_declaration;
2204 fvf_decls[low].fvf = fvf;
2205 ++device->fvf_decl_count;
2207 TRACE("Returning %p. %u declatations in array.\n", wined3d_declaration, device->fvf_decl_count);
2209 return wined3d_declaration;
2212 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2214 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2215 struct wined3d_vertex_declaration *decl;
2216 HRESULT hr;
2218 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2220 if (!fvf)
2222 WARN("%#x is not a valid FVF.\n", fvf);
2223 return D3D_OK;
2226 wined3d_mutex_lock();
2227 if (!(decl = device_get_fvf_declaration(This, fvf)))
2229 wined3d_mutex_unlock();
2230 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2231 return D3DERR_DRIVERINTERNALERROR;
2234 hr = wined3d_device_set_vertex_declaration(This->wined3d_device, decl);
2235 if (FAILED(hr))
2236 ERR("Failed to set vertex declaration.\n");
2237 wined3d_mutex_unlock();
2239 return hr;
2242 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2244 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
2245 struct wined3d_vertex_declaration *wined3d_declaration;
2246 IDirect3DVertexDeclaration9Impl *d3d9_declaration;
2247 HRESULT hr;
2249 TRACE("iface %p, fvf %p.\n", iface, fvf);
2251 wined3d_mutex_lock();
2252 if (FAILED(hr = wined3d_device_get_vertex_declaration(device->wined3d_device, &wined3d_declaration)))
2254 wined3d_mutex_unlock();
2255 WARN("Failed to get vertex declaration, hr %#x.\n", hr);
2256 *fvf = 0;
2257 return hr;
2260 if (wined3d_declaration)
2262 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2263 *fvf = d3d9_declaration->convFVF;
2264 wined3d_vertex_declaration_decref(wined3d_declaration);
2266 else
2268 *fvf = 0;
2270 wined3d_mutex_unlock();
2272 TRACE("Returning FVF %#x.\n", *fvf);
2274 return hr;
2277 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(IDirect3DDevice9Ex *iface,
2278 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2280 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2281 IDirect3DVertexShader9Impl *object;
2282 HRESULT hr;
2284 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2286 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2287 if (!object)
2289 ERR("Failed to allocate vertex shader memory.\n");
2290 return E_OUTOFMEMORY;
2293 hr = vertexshader_init(object, This, byte_code);
2294 if (FAILED(hr))
2296 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2297 HeapFree(GetProcessHeap(), 0, object);
2298 return hr;
2301 TRACE("Created vertex shader %p.\n", object);
2302 *shader = &object->IDirect3DVertexShader9_iface;
2304 return D3D_OK;
2307 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShader(IDirect3DDevice9Ex *iface,
2308 IDirect3DVertexShader9 *shader)
2310 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2311 IDirect3DVertexShader9Impl *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2312 HRESULT hr;
2314 TRACE("iface %p, shader %p.\n", iface, shader);
2316 wined3d_mutex_lock();
2317 hr = wined3d_device_set_vertex_shader(This->wined3d_device,
2318 shader_obj ? shader_obj->wined3d_shader : NULL);
2319 wined3d_mutex_unlock();
2321 return hr;
2324 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShader(IDirect3DDevice9Ex *iface,
2325 IDirect3DVertexShader9 **shader)
2327 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2328 struct wined3d_shader *wined3d_shader;
2330 TRACE("iface %p, shader %p.\n", iface, shader);
2332 wined3d_mutex_lock();
2333 wined3d_shader = wined3d_device_get_vertex_shader(This->wined3d_device);
2334 if (wined3d_shader)
2336 *shader = wined3d_shader_get_parent(wined3d_shader);
2337 IDirect3DVertexShader9_AddRef(*shader);
2338 wined3d_shader_decref(wined3d_shader);
2340 else
2342 *shader = NULL;
2344 wined3d_mutex_unlock();
2346 TRACE("Returning %p.\n", *shader);
2348 return D3D_OK;
2351 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2352 UINT reg_idx, const float *data, UINT count)
2354 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2355 HRESULT hr;
2357 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2359 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2361 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2362 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2363 return D3DERR_INVALIDCALL;
2366 wined3d_mutex_lock();
2367 hr = wined3d_device_set_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2368 wined3d_mutex_unlock();
2370 return hr;
2373 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2374 UINT reg_idx, float *data, UINT count)
2376 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2377 HRESULT hr;
2379 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2381 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2383 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2384 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2385 return D3DERR_INVALIDCALL;
2388 wined3d_mutex_lock();
2389 hr = wined3d_device_get_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2390 wined3d_mutex_unlock();
2392 return hr;
2395 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2396 UINT reg_idx, const int *data, UINT count)
2398 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2399 HRESULT hr;
2401 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2403 wined3d_mutex_lock();
2404 hr = wined3d_device_set_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2405 wined3d_mutex_unlock();
2407 return hr;
2410 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2411 UINT reg_idx, int *data, UINT count)
2413 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2414 HRESULT hr;
2416 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2418 wined3d_mutex_lock();
2419 hr = wined3d_device_get_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2420 wined3d_mutex_unlock();
2422 return hr;
2425 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2426 UINT reg_idx, const BOOL *data, UINT count)
2428 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2429 HRESULT hr;
2431 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2433 wined3d_mutex_lock();
2434 hr = wined3d_device_set_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2435 wined3d_mutex_unlock();
2437 return hr;
2440 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2441 UINT reg_idx, BOOL *data, UINT count)
2443 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2444 HRESULT hr;
2446 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2448 wined3d_mutex_lock();
2449 hr = wined3d_device_get_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2450 wined3d_mutex_unlock();
2452 return hr;
2455 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(IDirect3DDevice9Ex *iface,
2456 UINT StreamNumber, IDirect3DVertexBuffer9 *pStreamData, UINT OffsetInBytes, UINT Stride)
2458 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2459 IDirect3DVertexBuffer9Impl *streamdata = unsafe_impl_from_IDirect3DVertexBuffer9(pStreamData);
2460 HRESULT hr;
2462 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2463 iface, StreamNumber, pStreamData, OffsetInBytes, Stride);
2465 wined3d_mutex_lock();
2466 hr = wined3d_device_set_stream_source(This->wined3d_device, StreamNumber,
2467 streamdata ? streamdata->wineD3DVertexBuffer : NULL,
2468 OffsetInBytes, Stride);
2469 wined3d_mutex_unlock();
2471 return hr;
2474 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(IDirect3DDevice9Ex *iface,
2475 UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT *OffsetInBytes, UINT *pStride)
2477 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2478 struct wined3d_buffer *retStream = NULL;
2479 HRESULT hr;
2481 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2482 iface, StreamNumber, pStream, OffsetInBytes, pStride);
2484 if(pStream == NULL){
2485 return D3DERR_INVALIDCALL;
2488 wined3d_mutex_lock();
2489 hr = wined3d_device_get_stream_source(This->wined3d_device, StreamNumber, &retStream, OffsetInBytes, pStride);
2490 if (SUCCEEDED(hr) && retStream)
2492 *pStream = wined3d_buffer_get_parent(retStream);
2493 IDirect3DVertexBuffer9_AddRef(*pStream);
2494 wined3d_buffer_decref(retStream);
2496 else
2498 if (FAILED(hr))
2500 FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
2502 *pStream = NULL;
2504 wined3d_mutex_unlock();
2506 return hr;
2509 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2510 UINT StreamNumber, UINT Divider)
2512 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2513 HRESULT hr;
2515 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, StreamNumber, Divider);
2517 wined3d_mutex_lock();
2518 hr = wined3d_device_set_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2519 wined3d_mutex_unlock();
2521 return hr;
2524 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2525 UINT StreamNumber, UINT *Divider)
2527 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2528 HRESULT hr;
2530 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, StreamNumber, Divider);
2532 wined3d_mutex_lock();
2533 hr = wined3d_device_get_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2534 wined3d_mutex_unlock();
2536 return hr;
2539 static HRESULT WINAPI IDirect3DDevice9Impl_SetIndices(IDirect3DDevice9Ex *iface,
2540 IDirect3DIndexBuffer9 *pIndexData)
2542 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2543 IDirect3DIndexBuffer9Impl *ib = unsafe_impl_from_IDirect3DIndexBuffer9(pIndexData);
2544 HRESULT hr;
2546 TRACE("iface %p, buffer %p.\n", iface, pIndexData);
2548 wined3d_mutex_lock();
2549 hr = wined3d_device_set_index_buffer(This->wined3d_device,
2550 ib ? ib->wineD3DIndexBuffer : NULL,
2551 ib ? ib->format : WINED3DFMT_UNKNOWN);
2552 wined3d_mutex_unlock();
2554 return hr;
2557 static HRESULT WINAPI IDirect3DDevice9Impl_GetIndices(IDirect3DDevice9Ex *iface,
2558 IDirect3DIndexBuffer9 **ppIndexData)
2560 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2561 struct wined3d_buffer *retIndexData = NULL;
2562 HRESULT hr;
2564 TRACE("iface %p, buffer %p.\n", iface, ppIndexData);
2566 if(ppIndexData == NULL){
2567 return D3DERR_INVALIDCALL;
2570 wined3d_mutex_lock();
2571 hr = wined3d_device_get_index_buffer(This->wined3d_device, &retIndexData);
2572 if (SUCCEEDED(hr) && retIndexData)
2574 *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2575 IDirect3DIndexBuffer9_AddRef(*ppIndexData);
2576 wined3d_buffer_decref(retIndexData);
2578 else
2580 if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2581 *ppIndexData = NULL;
2583 wined3d_mutex_unlock();
2585 return hr;
2588 static HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(IDirect3DDevice9Ex *iface,
2589 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2591 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2592 IDirect3DPixelShader9Impl *object;
2593 HRESULT hr;
2595 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2597 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2598 if (!object)
2600 FIXME("Failed to allocate pixel shader memory.\n");
2601 return E_OUTOFMEMORY;
2604 hr = pixelshader_init(object, This, byte_code);
2605 if (FAILED(hr))
2607 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2608 HeapFree(GetProcessHeap(), 0, object);
2609 return hr;
2612 TRACE("Created pixel shader %p.\n", object);
2613 *shader = &object->IDirect3DPixelShader9_iface;
2615 return D3D_OK;
2618 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShader(IDirect3DDevice9Ex *iface,
2619 IDirect3DPixelShader9 *shader)
2621 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2622 IDirect3DPixelShader9Impl *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2623 HRESULT hr;
2625 TRACE("iface %p, shader %p.\n", iface, shader);
2627 wined3d_mutex_lock();
2628 hr = wined3d_device_set_pixel_shader(This->wined3d_device,
2629 shader_obj ? shader_obj->wined3d_shader : NULL);
2630 wined3d_mutex_unlock();
2632 return hr;
2635 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShader(IDirect3DDevice9Ex *iface,
2636 IDirect3DPixelShader9 **shader)
2638 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2639 struct wined3d_shader *wined3d_shader;
2641 TRACE("iface %p, shader %p.\n", iface, shader);
2643 if (!shader) return D3DERR_INVALIDCALL;
2645 wined3d_mutex_lock();
2646 wined3d_shader = wined3d_device_get_pixel_shader(This->wined3d_device);
2647 if (wined3d_shader)
2649 *shader = wined3d_shader_get_parent(wined3d_shader);
2650 IDirect3DPixelShader9_AddRef(*shader);
2651 wined3d_shader_decref(wined3d_shader);
2653 else
2655 *shader = NULL;
2657 wined3d_mutex_unlock();
2659 TRACE("Returning %p.\n", *shader);
2661 return D3D_OK;
2664 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2665 UINT reg_idx, const float *data, UINT count)
2667 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2668 HRESULT hr;
2670 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2672 wined3d_mutex_lock();
2673 hr = wined3d_device_set_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2674 wined3d_mutex_unlock();
2676 return hr;
2679 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2680 UINT reg_idx, float *data, UINT count)
2682 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2683 HRESULT hr;
2685 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2687 wined3d_mutex_lock();
2688 hr = wined3d_device_get_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2689 wined3d_mutex_unlock();
2691 return hr;
2694 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2695 UINT reg_idx, const int *data, UINT count)
2697 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2698 HRESULT hr;
2700 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2702 wined3d_mutex_lock();
2703 hr = wined3d_device_set_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2704 wined3d_mutex_unlock();
2706 return hr;
2709 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2710 UINT reg_idx, int *data, UINT count)
2712 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2713 HRESULT hr;
2715 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2717 wined3d_mutex_lock();
2718 hr = wined3d_device_get_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2719 wined3d_mutex_unlock();
2721 return hr;
2724 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2725 UINT reg_idx, const BOOL *data, UINT count)
2727 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2728 HRESULT hr;
2730 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2732 wined3d_mutex_lock();
2733 hr = wined3d_device_set_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2734 wined3d_mutex_unlock();
2736 return hr;
2739 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2740 UINT reg_idx, BOOL *data, UINT count)
2742 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2743 HRESULT hr;
2745 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2747 wined3d_mutex_lock();
2748 hr = wined3d_device_get_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2749 wined3d_mutex_unlock();
2751 return hr;
2754 static HRESULT WINAPI IDirect3DDevice9Impl_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2755 const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2757 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2758 HRESULT hr;
2760 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2761 iface, Handle, pNumSegs, pRectPatchInfo);
2763 wined3d_mutex_lock();
2764 hr = wined3d_device_draw_rect_patch(This->wined3d_device, Handle,
2765 pNumSegs, (const struct wined3d_rect_patch_info *)pRectPatchInfo);
2766 wined3d_mutex_unlock();
2768 return hr;
2771 static HRESULT WINAPI IDirect3DDevice9Impl_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2772 const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2774 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2775 HRESULT hr;
2777 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2778 iface, Handle, pNumSegs, pTriPatchInfo);
2780 wined3d_mutex_lock();
2781 hr = wined3d_device_draw_tri_patch(This->wined3d_device, Handle,
2782 pNumSegs, (const struct wined3d_tri_patch_info *)pTriPatchInfo);
2783 wined3d_mutex_unlock();
2785 return hr;
2788 static HRESULT WINAPI IDirect3DDevice9Impl_DeletePatch(IDirect3DDevice9Ex *iface, UINT Handle)
2790 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2791 HRESULT hr;
2793 TRACE("iface %p, handle %#x.\n", iface, Handle);
2795 wined3d_mutex_lock();
2796 hr = wined3d_device_delete_patch(This->wined3d_device, Handle);
2797 wined3d_mutex_unlock();
2799 return hr;
2802 static HRESULT WINAPI IDirect3DDevice9Impl_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type,
2803 IDirect3DQuery9 **query)
2805 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2806 IDirect3DQuery9Impl *object;
2807 HRESULT hr;
2809 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2811 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2812 if (!object)
2814 ERR("Failed to allocate query memory.\n");
2815 return E_OUTOFMEMORY;
2818 hr = query_init(object, This, type);
2819 if (FAILED(hr))
2821 WARN("Failed to initialize query, hr %#x.\n", hr);
2822 HeapFree(GetProcessHeap(), 0, object);
2823 return hr;
2826 TRACE("Created query %p.\n", object);
2827 if (query) *query = &object->IDirect3DQuery9_iface;
2828 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
2830 return D3D_OK;
2833 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2834 UINT width, UINT height, float *rows, float *columns)
2836 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2837 iface, width, height, rows, columns);
2839 return E_NOTIMPL;
2842 static HRESULT WINAPI IDirect3DDevice9ExImpl_ComposeRects(IDirect3DDevice9Ex *iface,
2843 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2844 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2846 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2847 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2848 iface, src_surface, dst_surface, src_descs, rect_count,
2849 dst_descs, operation, offset_x, offset_y);
2851 return E_NOTIMPL;
2854 static HRESULT WINAPI IDirect3DDevice9ExImpl_PresentEx(IDirect3DDevice9Ex *iface,
2855 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2856 const RGNDATA *dirty_region, DWORD flags)
2858 FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2859 iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2861 return E_NOTIMPL;
2864 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2866 FIXME("iface %p, priority %p stub!\n", iface, priority);
2868 return E_NOTIMPL;
2871 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2873 FIXME("iface %p, priority %d stub!\n", iface, priority);
2875 return E_NOTIMPL;
2878 static HRESULT WINAPI IDirect3DDevice9ExImpl_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2880 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2882 return E_NOTIMPL;
2885 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2886 IDirect3DResource9 **resources, UINT32 resource_count)
2888 FIXME("iface %p, resources %p, resource_count %u stub!\n",
2889 iface, resources, resource_count);
2891 return E_NOTIMPL;
2894 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2896 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2898 return E_NOTIMPL;
2901 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2903 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2905 *max_latency = 2;
2907 return E_NOTIMPL;
2910 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2912 static int i;
2914 TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
2916 if (!i++)
2917 FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2919 return D3D_OK;
2922 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2923 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2924 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2926 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2927 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2928 iface, width, height, format, multisample_type, multisample_quality,
2929 lockable, surface, shared_handle, usage);
2931 if (shared_handle)
2932 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2934 return E_NOTIMPL;
2937 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2938 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2939 HANDLE *shared_handle, DWORD usage)
2941 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2942 iface, width, height, format, pool, surface, shared_handle, usage);
2944 return E_NOTIMPL;
2947 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2948 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2949 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2951 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2952 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2953 iface, width, height, format, multisample_type, multisample_quality,
2954 discard, surface, shared_handle, usage);
2956 if (shared_handle)
2957 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2959 return E_NOTIMPL;
2962 static HRESULT WINAPI IDirect3DDevice9ExImpl_ResetEx(IDirect3DDevice9Ex *iface,
2963 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2965 FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2967 return E_NOTIMPL;
2970 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2971 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2973 FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2975 return E_NOTIMPL;
2978 static const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
2980 /* IUnknown */
2981 IDirect3DDevice9Impl_QueryInterface,
2982 IDirect3DDevice9Impl_AddRef,
2983 IDirect3DDevice9Impl_Release,
2984 /* IDirect3DDevice9 */
2985 IDirect3DDevice9Impl_TestCooperativeLevel,
2986 IDirect3DDevice9Impl_GetAvailableTextureMem,
2987 IDirect3DDevice9Impl_EvictManagedResources,
2988 IDirect3DDevice9Impl_GetDirect3D,
2989 IDirect3DDevice9Impl_GetDeviceCaps,
2990 IDirect3DDevice9Impl_GetDisplayMode,
2991 IDirect3DDevice9Impl_GetCreationParameters,
2992 IDirect3DDevice9Impl_SetCursorProperties,
2993 IDirect3DDevice9Impl_SetCursorPosition,
2994 IDirect3DDevice9Impl_ShowCursor,
2995 IDirect3DDevice9Impl_CreateAdditionalSwapChain,
2996 IDirect3DDevice9Impl_GetSwapChain,
2997 IDirect3DDevice9Impl_GetNumberOfSwapChains,
2998 IDirect3DDevice9Impl_Reset,
2999 IDirect3DDevice9Impl_Present,
3000 IDirect3DDevice9Impl_GetBackBuffer,
3001 IDirect3DDevice9Impl_GetRasterStatus,
3002 IDirect3DDevice9Impl_SetDialogBoxMode,
3003 IDirect3DDevice9Impl_SetGammaRamp,
3004 IDirect3DDevice9Impl_GetGammaRamp,
3005 IDirect3DDevice9Impl_CreateTexture,
3006 IDirect3DDevice9Impl_CreateVolumeTexture,
3007 IDirect3DDevice9Impl_CreateCubeTexture,
3008 IDirect3DDevice9Impl_CreateVertexBuffer,
3009 IDirect3DDevice9Impl_CreateIndexBuffer,
3010 IDirect3DDevice9Impl_CreateRenderTarget,
3011 IDirect3DDevice9Impl_CreateDepthStencilSurface,
3012 IDirect3DDevice9Impl_UpdateSurface,
3013 IDirect3DDevice9Impl_UpdateTexture,
3014 IDirect3DDevice9Impl_GetRenderTargetData,
3015 IDirect3DDevice9Impl_GetFrontBufferData,
3016 IDirect3DDevice9Impl_StretchRect,
3017 IDirect3DDevice9Impl_ColorFill,
3018 IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
3019 IDirect3DDevice9Impl_SetRenderTarget,
3020 IDirect3DDevice9Impl_GetRenderTarget,
3021 IDirect3DDevice9Impl_SetDepthStencilSurface,
3022 IDirect3DDevice9Impl_GetDepthStencilSurface,
3023 IDirect3DDevice9Impl_BeginScene,
3024 IDirect3DDevice9Impl_EndScene,
3025 IDirect3DDevice9Impl_Clear,
3026 IDirect3DDevice9Impl_SetTransform,
3027 IDirect3DDevice9Impl_GetTransform,
3028 IDirect3DDevice9Impl_MultiplyTransform,
3029 IDirect3DDevice9Impl_SetViewport,
3030 IDirect3DDevice9Impl_GetViewport,
3031 IDirect3DDevice9Impl_SetMaterial,
3032 IDirect3DDevice9Impl_GetMaterial,
3033 IDirect3DDevice9Impl_SetLight,
3034 IDirect3DDevice9Impl_GetLight,
3035 IDirect3DDevice9Impl_LightEnable,
3036 IDirect3DDevice9Impl_GetLightEnable,
3037 IDirect3DDevice9Impl_SetClipPlane,
3038 IDirect3DDevice9Impl_GetClipPlane,
3039 IDirect3DDevice9Impl_SetRenderState,
3040 IDirect3DDevice9Impl_GetRenderState,
3041 IDirect3DDevice9Impl_CreateStateBlock,
3042 IDirect3DDevice9Impl_BeginStateBlock,
3043 IDirect3DDevice9Impl_EndStateBlock,
3044 IDirect3DDevice9Impl_SetClipStatus,
3045 IDirect3DDevice9Impl_GetClipStatus,
3046 IDirect3DDevice9Impl_GetTexture,
3047 IDirect3DDevice9Impl_SetTexture,
3048 IDirect3DDevice9Impl_GetTextureStageState,
3049 IDirect3DDevice9Impl_SetTextureStageState,
3050 IDirect3DDevice9Impl_GetSamplerState,
3051 IDirect3DDevice9Impl_SetSamplerState,
3052 IDirect3DDevice9Impl_ValidateDevice,
3053 IDirect3DDevice9Impl_SetPaletteEntries,
3054 IDirect3DDevice9Impl_GetPaletteEntries,
3055 IDirect3DDevice9Impl_SetCurrentTexturePalette,
3056 IDirect3DDevice9Impl_GetCurrentTexturePalette,
3057 IDirect3DDevice9Impl_SetScissorRect,
3058 IDirect3DDevice9Impl_GetScissorRect,
3059 IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
3060 IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
3061 IDirect3DDevice9Impl_SetNPatchMode,
3062 IDirect3DDevice9Impl_GetNPatchMode,
3063 IDirect3DDevice9Impl_DrawPrimitive,
3064 IDirect3DDevice9Impl_DrawIndexedPrimitive,
3065 IDirect3DDevice9Impl_DrawPrimitiveUP,
3066 IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
3067 IDirect3DDevice9Impl_ProcessVertices,
3068 IDirect3DDevice9Impl_CreateVertexDeclaration,
3069 IDirect3DDevice9Impl_SetVertexDeclaration,
3070 IDirect3DDevice9Impl_GetVertexDeclaration,
3071 IDirect3DDevice9Impl_SetFVF,
3072 IDirect3DDevice9Impl_GetFVF,
3073 IDirect3DDevice9Impl_CreateVertexShader,
3074 IDirect3DDevice9Impl_SetVertexShader,
3075 IDirect3DDevice9Impl_GetVertexShader,
3076 IDirect3DDevice9Impl_SetVertexShaderConstantF,
3077 IDirect3DDevice9Impl_GetVertexShaderConstantF,
3078 IDirect3DDevice9Impl_SetVertexShaderConstantI,
3079 IDirect3DDevice9Impl_GetVertexShaderConstantI,
3080 IDirect3DDevice9Impl_SetVertexShaderConstantB,
3081 IDirect3DDevice9Impl_GetVertexShaderConstantB,
3082 IDirect3DDevice9Impl_SetStreamSource,
3083 IDirect3DDevice9Impl_GetStreamSource,
3084 IDirect3DDevice9Impl_SetStreamSourceFreq,
3085 IDirect3DDevice9Impl_GetStreamSourceFreq,
3086 IDirect3DDevice9Impl_SetIndices,
3087 IDirect3DDevice9Impl_GetIndices,
3088 IDirect3DDevice9Impl_CreatePixelShader,
3089 IDirect3DDevice9Impl_SetPixelShader,
3090 IDirect3DDevice9Impl_GetPixelShader,
3091 IDirect3DDevice9Impl_SetPixelShaderConstantF,
3092 IDirect3DDevice9Impl_GetPixelShaderConstantF,
3093 IDirect3DDevice9Impl_SetPixelShaderConstantI,
3094 IDirect3DDevice9Impl_GetPixelShaderConstantI,
3095 IDirect3DDevice9Impl_SetPixelShaderConstantB,
3096 IDirect3DDevice9Impl_GetPixelShaderConstantB,
3097 IDirect3DDevice9Impl_DrawRectPatch,
3098 IDirect3DDevice9Impl_DrawTriPatch,
3099 IDirect3DDevice9Impl_DeletePatch,
3100 IDirect3DDevice9Impl_CreateQuery,
3101 /* IDirect3DDevice9Ex */
3102 IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
3103 IDirect3DDevice9ExImpl_ComposeRects,
3104 IDirect3DDevice9ExImpl_PresentEx,
3105 IDirect3DDevice9ExImpl_GetGPUThreadPriority,
3106 IDirect3DDevice9ExImpl_SetGPUThreadPriority,
3107 IDirect3DDevice9ExImpl_WaitForVBlank,
3108 IDirect3DDevice9ExImpl_CheckResourceResidency,
3109 IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
3110 IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
3111 IDirect3DDevice9ExImpl_CheckDeviceState,
3112 IDirect3DDevice9ExImpl_CreateRenderTargetEx,
3113 IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
3114 IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
3115 IDirect3DDevice9ExImpl_ResetEx,
3116 IDirect3DDevice9ExImpl_GetDisplayModeEx
3119 static inline struct IDirect3DDevice9Impl *device_from_device_parent(struct wined3d_device_parent *device_parent)
3121 return CONTAINING_RECORD(device_parent, struct IDirect3DDevice9Impl, device_parent);
3124 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3125 struct wined3d_device *device)
3127 TRACE("device_parent %p, device %p.\n", device_parent, device);
3130 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3132 TRACE("device_parent %p.\n", device_parent);
3135 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
3136 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
3137 enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
3139 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3140 IDirect3DSurface9Impl *d3d_surface;
3141 BOOL lockable = TRUE;
3142 HRESULT hr;
3144 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
3145 "\tpool %#x, level %u, face %u, surface %p.\n",
3146 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
3148 if (pool == WINED3D_POOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
3149 lockable = FALSE;
3151 hr = IDirect3DDevice9Impl_CreateSurface(device, width, height,
3152 d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
3153 (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
3154 if (FAILED(hr))
3156 WARN("Failed to create surface, hr %#x.\n", hr);
3157 return hr;
3160 *surface = d3d_surface->wined3d_surface;
3161 wined3d_surface_incref(*surface);
3163 d3d_surface->container = container_parent;
3164 IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
3165 d3d_surface->parentDevice = NULL;
3167 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3168 d3d_surface->forwardReference = container_parent;
3170 return hr;
3173 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
3174 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
3175 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, BOOL lockable,
3176 struct wined3d_surface **surface)
3178 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3179 IDirect3DSurface9Impl *d3d_surface;
3180 HRESULT hr;
3182 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3183 "\tmultisample_quality %u, lockable %u, surface %p.\n",
3184 device_parent, container_parent, width, height, format, multisample_type,
3185 multisample_quality, lockable, surface);
3187 hr = IDirect3DDevice9Impl_CreateRenderTarget(&device->IDirect3DDevice9Ex_iface, width, height,
3188 d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
3189 (IDirect3DSurface9 **)&d3d_surface, NULL);
3190 if (FAILED(hr))
3192 WARN("Failed to create rendertarget, hr %#x.\n", hr);
3193 return hr;
3196 *surface = d3d_surface->wined3d_surface;
3197 wined3d_surface_incref(*surface);
3199 d3d_surface->container = container_parent;
3200 /* Implicit surfaces are created with an refcount of 0 */
3201 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3203 return hr;
3206 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
3207 UINT width, UINT height, enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
3208 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
3210 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3211 IDirect3DSurface9Impl *d3d_surface;
3212 HRESULT hr;
3214 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3215 "\tmultisample_quality %u, discard %u, surface %p.\n",
3216 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
3218 hr = IDirect3DDevice9Impl_CreateDepthStencilSurface(&device->IDirect3DDevice9Ex_iface, width,
3219 height, d3dformat_from_wined3dformat(format), multisample_type, multisample_quality,
3220 discard, (IDirect3DSurface9 **)&d3d_surface, NULL);
3221 if (FAILED(hr))
3223 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
3224 return hr;
3227 *surface = d3d_surface->wined3d_surface;
3228 wined3d_surface_incref(*surface);
3229 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice9Ex_iface;
3230 /* Implicit surfaces are created with an refcount of 0 */
3231 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3233 return hr;
3236 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
3237 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
3238 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
3240 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3241 IDirect3DVolume9Impl *object;
3242 HRESULT hr;
3244 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
3245 "format %#x, pool %#x, usage %#x, volume %p\n",
3246 device_parent, container_parent, width, height, depth,
3247 format, pool, usage, volume);
3249 /* Allocate the storage for the device */
3250 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3251 if (!object)
3253 FIXME("Allocation of memory failed\n");
3254 *volume = NULL;
3255 return D3DERR_OUTOFVIDEOMEMORY;
3258 hr = volume_init(object, device, width, height, depth, usage, format, pool);
3259 if (FAILED(hr))
3261 WARN("Failed to initialize volume, hr %#x.\n", hr);
3262 HeapFree(GetProcessHeap(), 0, object);
3263 return hr;
3266 *volume = object->wined3d_volume;
3267 wined3d_volume_incref(*volume);
3268 IDirect3DVolume9_Release(&object->IDirect3DVolume9_iface);
3270 object->container = container_parent;
3271 object->forwardReference = container_parent;
3273 TRACE("Created volume %p.\n", object);
3275 return hr;
3278 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3279 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3281 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3282 D3DPRESENT_PARAMETERS local_parameters;
3283 IDirect3DSwapChain9Impl *d3d_swapchain;
3284 HRESULT hr;
3286 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3288 /* Copy the presentation parameters */
3289 local_parameters.BackBufferWidth = desc->backbuffer_width;
3290 local_parameters.BackBufferHeight = desc->backbuffer_height;
3291 local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(desc->backbuffer_format);
3292 local_parameters.BackBufferCount = desc->backbuffer_count;
3293 local_parameters.MultiSampleType = desc->multisample_type;
3294 local_parameters.MultiSampleQuality = desc->multisample_quality;
3295 local_parameters.SwapEffect = desc->swap_effect;
3296 local_parameters.hDeviceWindow = desc->device_window;
3297 local_parameters.Windowed = desc->windowed;
3298 local_parameters.EnableAutoDepthStencil = desc->enable_auto_depth_stencil;
3299 local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc->auto_depth_stencil_format);
3300 local_parameters.Flags = desc->flags;
3301 local_parameters.FullScreen_RefreshRateInHz = desc->refresh_rate;
3302 local_parameters.PresentationInterval = desc->swap_interval;
3304 hr = d3d9_swapchain_create(device, &local_parameters, &d3d_swapchain);
3305 if (FAILED(hr))
3307 WARN("Failed to create swapchain, hr %#x.\n", hr);
3308 *swapchain = NULL;
3309 return hr;
3312 *swapchain = d3d_swapchain->wined3d_swapchain;
3313 wined3d_swapchain_incref(*swapchain);
3314 IDirect3DSwapChain9_Release(&d3d_swapchain->IDirect3DSwapChain9_iface);
3316 /* Copy back the presentation parameters */
3317 desc->backbuffer_width = local_parameters.BackBufferWidth;
3318 desc->backbuffer_height = local_parameters.BackBufferHeight;
3319 desc->backbuffer_format = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3320 desc->backbuffer_count = local_parameters.BackBufferCount;
3321 desc->multisample_type = local_parameters.MultiSampleType;
3322 desc->multisample_quality = local_parameters.MultiSampleQuality;
3323 desc->swap_effect = local_parameters.SwapEffect;
3324 desc->device_window = local_parameters.hDeviceWindow;
3325 desc->windowed = local_parameters.Windowed;
3326 desc->enable_auto_depth_stencil = local_parameters.EnableAutoDepthStencil;
3327 desc->auto_depth_stencil_format = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3328 desc->flags = local_parameters.Flags;
3329 desc->refresh_rate = local_parameters.FullScreen_RefreshRateInHz;
3330 desc->swap_interval = local_parameters.PresentationInterval;
3332 return hr;
3335 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3337 device_parent_wined3d_device_created,
3338 device_parent_mode_changed,
3339 device_parent_create_surface,
3340 device_parent_create_rendertarget,
3341 device_parent_create_depth_stencil,
3342 device_parent_create_volume,
3343 device_parent_create_swapchain,
3346 static void setup_fpu(void)
3348 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3349 WORD cw;
3350 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3351 cw = (cw & ~0xf3f) | 0x3f;
3352 __asm__ volatile ("fldcw %0" : : "m" (cw));
3353 #elif defined(__i386__) && defined(_MSC_VER)
3354 WORD cw;
3355 __asm fnstcw cw;
3356 cw = (cw & ~0xf3f) | 0x3f;
3357 __asm fldcw cw;
3358 #else
3359 FIXME("FPU setup not implemented for this platform.\n");
3360 #endif
3363 HRESULT device_init(IDirect3DDevice9Impl *device, IDirect3D9Impl *parent, struct wined3d *wined3d,
3364 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3365 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3367 struct wined3d_swapchain_desc *swapchain_desc;
3368 UINT i, count = 1;
3369 HRESULT hr;
3371 if (mode)
3372 FIXME("Ignoring display mode.\n");
3374 device->IDirect3DDevice9Ex_iface.lpVtbl = &Direct3DDevice9_Vtbl;
3375 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3376 device->ref = 1;
3378 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3380 wined3d_mutex_lock();
3381 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3382 &device->device_parent, &device->wined3d_device);
3383 if (FAILED(hr))
3385 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3386 wined3d_mutex_unlock();
3387 return hr;
3390 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3392 WINED3DCAPS caps;
3394 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3395 count = caps.NumberOfAdaptersInGroup;
3398 if (flags & D3DCREATE_MULTITHREADED)
3399 wined3d_device_set_multithreaded(device->wined3d_device);
3401 if (!parameters->Windowed)
3403 if (!focus_window)
3404 focus_window = parameters->hDeviceWindow;
3405 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3407 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3408 wined3d_device_decref(device->wined3d_device);
3409 wined3d_mutex_unlock();
3410 return hr;
3413 for (i = 0; i < count; ++i)
3415 HWND device_window = parameters[i].hDeviceWindow;
3417 if (!device_window) device_window = focus_window;
3418 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3419 parameters[i].BackBufferWidth,
3420 parameters[i].BackBufferHeight);
3424 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3425 if (!swapchain_desc)
3427 ERR("Failed to allocate wined3d parameters.\n");
3428 wined3d_device_decref(device->wined3d_device);
3429 wined3d_mutex_unlock();
3430 return E_OUTOFMEMORY;
3433 for (i = 0; i < count; ++i)
3435 swapchain_desc[i].backbuffer_width = parameters[i].BackBufferWidth;
3436 swapchain_desc[i].backbuffer_height = parameters[i].BackBufferHeight;
3437 swapchain_desc[i].backbuffer_format = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
3438 swapchain_desc[i].backbuffer_count = parameters[i].BackBufferCount;
3439 swapchain_desc[i].multisample_type = parameters[i].MultiSampleType;
3440 swapchain_desc[i].multisample_quality = parameters[i].MultiSampleQuality;
3441 swapchain_desc[i].swap_effect = parameters[i].SwapEffect;
3442 swapchain_desc[i].device_window = parameters[i].hDeviceWindow;
3443 swapchain_desc[i].windowed = parameters[i].Windowed;
3444 swapchain_desc[i].enable_auto_depth_stencil = parameters[i].EnableAutoDepthStencil;
3445 swapchain_desc[i].auto_depth_stencil_format =
3446 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
3447 swapchain_desc[i].flags = parameters[i].Flags;
3448 swapchain_desc[i].refresh_rate = parameters[i].FullScreen_RefreshRateInHz;
3449 swapchain_desc[i].swap_interval = parameters[i].PresentationInterval;
3450 swapchain_desc[i].auto_restore_display_mode = TRUE;
3453 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3454 if (FAILED(hr))
3456 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3457 wined3d_device_release_focus_window(device->wined3d_device);
3458 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3459 wined3d_device_decref(device->wined3d_device);
3460 wined3d_mutex_unlock();
3461 return hr;
3464 wined3d_mutex_unlock();
3466 for (i = 0; i < count; ++i)
3468 parameters[i].BackBufferWidth = swapchain_desc[i].backbuffer_width;
3469 parameters[i].BackBufferHeight = swapchain_desc[i].backbuffer_height;
3470 parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc[i].backbuffer_format);
3471 parameters[i].BackBufferCount = swapchain_desc[i].backbuffer_count;
3472 parameters[i].MultiSampleType = swapchain_desc[i].multisample_type;
3473 parameters[i].MultiSampleQuality = swapchain_desc[i].multisample_quality;
3474 parameters[i].SwapEffect = swapchain_desc[i].swap_effect;
3475 parameters[i].hDeviceWindow = swapchain_desc[i].device_window;
3476 parameters[i].Windowed = swapchain_desc[i].windowed;
3477 parameters[i].EnableAutoDepthStencil = swapchain_desc[i].enable_auto_depth_stencil;
3478 parameters[i].AutoDepthStencilFormat =
3479 d3dformat_from_wined3dformat(swapchain_desc[i].auto_depth_stencil_format);
3480 parameters[i].Flags = swapchain_desc[i].flags;
3481 parameters[i].FullScreen_RefreshRateInHz = swapchain_desc[i].refresh_rate;
3482 parameters[i].PresentationInterval = swapchain_desc[i].swap_interval;
3484 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3486 /* Initialize the converted declaration array. This creates a valid pointer
3487 * and when adding decls HeapReAlloc() can be used without further checking. */
3488 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3489 if (!device->fvf_decls)
3491 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3492 wined3d_mutex_lock();
3493 wined3d_device_uninit_3d(device->wined3d_device);
3494 wined3d_device_release_focus_window(device->wined3d_device);
3495 wined3d_device_decref(device->wined3d_device);
3496 wined3d_mutex_unlock();
3497 return E_OUTOFMEMORY;
3500 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3501 device->d3d_parent = parent;
3503 return D3D_OK;