d3d9: Don't return a pointer to the implementation in IDirect3DSwapChain9Impl_QueryIn...
[wine/multimedia.git] / dlls / d3d9 / device.c
blobab96d79009c393bdce09af2b80996f41d5ae05df
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,
190 void **ppobj)
192 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
194 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
196 if (IsEqualGUID(riid, &IID_IUnknown)
197 || IsEqualGUID(riid, &IID_IDirect3DDevice9)) {
198 IDirect3DDevice9Ex_AddRef(iface);
199 *ppobj = This;
200 TRACE("Returning IDirect3DDevice9 interface at %p\n", *ppobj);
201 return S_OK;
202 } else if(IsEqualGUID(riid, &IID_IDirect3DDevice9Ex)) {
203 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
204 * It doesn't matter with which function the device was created. */
206 if (This->d3d_parent->extended)
208 *ppobj = iface;
209 IDirect3DDevice9Ex_AddRef((IDirect3DDevice9Ex *) *ppobj);
210 TRACE("Returning IDirect3DDevice9Ex interface at %p\n", *ppobj);
211 return S_OK;
212 } else {
213 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE\n");
214 *ppobj = NULL;
215 return E_NOINTERFACE;
219 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
220 *ppobj = NULL;
221 return E_NOINTERFACE;
224 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(IDirect3DDevice9Ex *iface)
226 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
227 ULONG ref = InterlockedIncrement(&This->ref);
229 TRACE("%p increasing refcount to %u.\n", iface, ref);
231 return ref;
234 static ULONG WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Release(IDirect3DDevice9Ex *iface)
236 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
237 ULONG ref;
239 if (This->inDestruction) return 0;
240 ref = InterlockedDecrement(&This->ref);
242 TRACE("%p decreasing refcount to %u.\n", iface, ref);
244 if (ref == 0) {
245 unsigned i;
246 This->inDestruction = TRUE;
248 wined3d_mutex_lock();
249 for(i = 0; i < This->numConvertedDecls; i++) {
250 /* Unless Wine is buggy or the app has a bug the refcount will be 0, because decls hold a reference to the
251 * device
253 IDirect3DVertexDeclaration9Impl_Destroy(This->convertedDecls[i]);
255 HeapFree(GetProcessHeap(), 0, This->convertedDecls);
257 wined3d_device_uninit_3d(This->wined3d_device);
258 wined3d_device_release_focus_window(This->wined3d_device);
259 wined3d_device_decref(This->wined3d_device);
260 wined3d_mutex_unlock();
262 IDirect3D9Ex_Release(&This->d3d_parent->IDirect3D9Ex_iface);
264 HeapFree(GetProcessHeap(), 0, This);
266 return ref;
269 static HRESULT WINAPI IDirect3DDevice9Impl_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
271 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
273 TRACE("iface %p.\n", iface);
275 if (This->notreset)
277 TRACE("D3D9 device is marked not reset.\n");
278 return D3DERR_DEVICENOTRESET;
281 return D3D_OK;
284 static UINT WINAPI IDirect3DDevice9Impl_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
286 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
287 HRESULT hr;
289 TRACE("iface %p.\n", iface);
291 wined3d_mutex_lock();
292 hr = wined3d_device_get_available_texture_mem(This->wined3d_device);
293 wined3d_mutex_unlock();
295 return hr;
298 static HRESULT WINAPI IDirect3DDevice9Impl_EvictManagedResources(IDirect3DDevice9Ex *iface)
300 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
302 TRACE("iface %p.\n", iface);
304 wined3d_mutex_lock();
305 wined3d_device_evict_managed_resources(This->wined3d_device);
306 wined3d_mutex_unlock();
308 return D3D_OK;
311 static HRESULT WINAPI IDirect3DDevice9Impl_GetDirect3D(IDirect3DDevice9Ex *iface,
312 IDirect3D9 **ppD3D9)
314 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
316 TRACE("iface %p, d3d9 %p.\n", iface, ppD3D9);
318 if (NULL == ppD3D9) {
319 return D3DERR_INVALIDCALL;
322 return IDirect3D9Ex_QueryInterface(&This->d3d_parent->IDirect3D9Ex_iface,
323 &IID_IDirect3D9, (void **)ppD3D9);
326 static HRESULT WINAPI IDirect3DDevice9Impl_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *pCaps)
328 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
329 HRESULT hrc = D3D_OK;
330 WINED3DCAPS *pWineCaps;
332 TRACE("iface %p, caps %p.\n", iface, pCaps);
334 if(NULL == pCaps){
335 return D3DERR_INVALIDCALL;
337 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
338 if(pWineCaps == NULL){
339 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
342 memset(pCaps, 0, sizeof(*pCaps));
344 wined3d_mutex_lock();
345 hrc = wined3d_device_get_device_caps(This->wined3d_device, pWineCaps);
346 wined3d_mutex_unlock();
348 WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
349 HeapFree(GetProcessHeap(), 0, pWineCaps);
351 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
352 pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
354 filter_caps(pCaps);
356 TRACE("Returning %p %p\n", This, pCaps);
357 return hrc;
360 static HRESULT WINAPI IDirect3DDevice9Impl_GetDisplayMode(IDirect3DDevice9Ex *iface,
361 UINT iSwapChain, D3DDISPLAYMODE *pMode)
363 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
364 HRESULT hr;
366 TRACE("iface %p, swapchain %u, mode %p.\n", iface, iSwapChain, pMode);
368 wined3d_mutex_lock();
369 hr = wined3d_device_get_display_mode(This->wined3d_device, iSwapChain, (struct wined3d_display_mode *)pMode);
370 wined3d_mutex_unlock();
372 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
374 return hr;
377 static HRESULT WINAPI IDirect3DDevice9Impl_GetCreationParameters(IDirect3DDevice9Ex *iface,
378 D3DDEVICE_CREATION_PARAMETERS *pParameters)
380 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
381 HRESULT hr;
383 TRACE("iface %p, parameters %p.\n", iface, pParameters);
385 wined3d_mutex_lock();
386 hr = wined3d_device_get_creation_parameters(This->wined3d_device,
387 (struct wined3d_device_creation_parameters *)pParameters);
388 wined3d_mutex_unlock();
390 return hr;
393 static HRESULT WINAPI IDirect3DDevice9Impl_SetCursorProperties(IDirect3DDevice9Ex *iface,
394 UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9 *pCursorBitmap)
396 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
397 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pCursorBitmap);
398 HRESULT hr;
400 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
401 iface, XHotSpot, YHotSpot, pCursorBitmap);
403 if (!pCursorBitmap)
405 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
406 return D3DERR_INVALIDCALL;
409 wined3d_mutex_lock();
410 hr = wined3d_device_set_cursor_properties(This->wined3d_device, XHotSpot, YHotSpot, pSurface->wined3d_surface);
411 wined3d_mutex_unlock();
413 return hr;
416 static void WINAPI IDirect3DDevice9Impl_SetCursorPosition(IDirect3DDevice9Ex *iface,
417 int XScreenSpace, int YScreenSpace, DWORD Flags)
419 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
421 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, XScreenSpace, YScreenSpace, Flags);
423 wined3d_mutex_lock();
424 wined3d_device_set_cursor_position(This->wined3d_device, XScreenSpace, YScreenSpace, Flags);
425 wined3d_mutex_unlock();
428 static BOOL WINAPI IDirect3DDevice9Impl_ShowCursor(IDirect3DDevice9Ex *iface, BOOL bShow)
430 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
431 BOOL ret;
433 TRACE("iface %p, show %#x.\n", iface, bShow);
435 wined3d_mutex_lock();
436 ret = wined3d_device_show_cursor(This->wined3d_device, bShow);
437 wined3d_mutex_unlock();
439 return ret;
442 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
443 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
445 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
446 IDirect3DSwapChain9Impl *object;
447 HRESULT hr;
449 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
450 iface, present_parameters, swapchain);
452 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
453 if (!object)
455 ERR("Failed to allocate swapchain memory.\n");
456 return E_OUTOFMEMORY;
459 hr = swapchain_init(object, This, present_parameters);
460 if (FAILED(hr))
462 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
463 HeapFree(GetProcessHeap(), 0, object);
464 return hr;
467 TRACE("Created swapchain %p.\n", object);
468 *swapchain = (IDirect3DSwapChain9 *)object;
470 return D3D_OK;
473 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_GetSwapChain(IDirect3DDevice9Ex *iface,
474 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
476 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
477 struct wined3d_swapchain *wined3d_swapchain = NULL;
478 HRESULT hr;
480 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
482 wined3d_mutex_lock();
483 hr = wined3d_device_get_swapchain(This->wined3d_device, swapchain_idx, &wined3d_swapchain);
484 if (SUCCEEDED(hr) && wined3d_swapchain)
486 *swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
487 IDirect3DSwapChain9_AddRef(*swapchain);
488 wined3d_swapchain_decref(wined3d_swapchain);
490 else
492 *swapchain = NULL;
494 wined3d_mutex_unlock();
496 return hr;
499 static UINT WINAPI IDirect3DDevice9Impl_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
501 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
502 UINT count;
504 TRACE("iface %p.\n", iface);
506 wined3d_mutex_lock();
507 count = wined3d_device_get_swapchain_count(This->wined3d_device);
508 wined3d_mutex_unlock();
510 return count;
513 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
515 struct wined3d_resource_desc desc;
517 wined3d_resource_get_desc(resource, &desc);
518 if (desc.pool == WINED3D_POOL_DEFAULT)
520 IDirect3DSurface9 *surface;
522 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
524 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
525 return D3DERR_INVALIDCALL;
528 surface = wined3d_resource_get_parent(resource);
530 IDirect3DSurface9_AddRef(surface);
531 if (IDirect3DSurface9_Release(surface))
533 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
534 return D3DERR_INVALIDCALL;
537 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
540 return D3D_OK;
543 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Reset(IDirect3DDevice9Ex *iface,
544 D3DPRESENT_PARAMETERS *pPresentationParameters)
546 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
547 struct wined3d_swapchain_desc swapchain_desc;
548 HRESULT hr;
550 TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
552 /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
553 * such objects can still be used for rendering after their external d3d9 object has been destroyed.
554 * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
555 * make use of the hidden reference and destroys the objects.
557 * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
558 * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
560 wined3d_mutex_lock();
562 swapchain_desc.backbuffer_width = pPresentationParameters->BackBufferWidth;
563 swapchain_desc.backbuffer_height = pPresentationParameters->BackBufferHeight;
564 swapchain_desc.backbuffer_format = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
565 swapchain_desc.backbuffer_count = pPresentationParameters->BackBufferCount;
566 swapchain_desc.multisample_type = pPresentationParameters->MultiSampleType;
567 swapchain_desc.multisample_quality = pPresentationParameters->MultiSampleQuality;
568 swapchain_desc.swap_effect = pPresentationParameters->SwapEffect;
569 swapchain_desc.device_window = pPresentationParameters->hDeviceWindow;
570 swapchain_desc.windowed = pPresentationParameters->Windowed;
571 swapchain_desc.enable_auto_depth_stencil = pPresentationParameters->EnableAutoDepthStencil;
572 swapchain_desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
573 swapchain_desc.flags = pPresentationParameters->Flags;
574 swapchain_desc.refresh_rate = pPresentationParameters->FullScreen_RefreshRateInHz;
575 swapchain_desc.swap_interval = pPresentationParameters->PresentationInterval;
576 swapchain_desc.auto_restore_display_mode = TRUE;
578 hr = wined3d_device_reset(This->wined3d_device, &swapchain_desc, reset_enum_callback);
579 if (FAILED(hr))
580 This->notreset = TRUE;
581 else
582 This->notreset = FALSE;
584 wined3d_mutex_unlock();
586 return hr;
589 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Present(IDirect3DDevice9Ex *iface,
590 const RECT *pSourceRect, const RECT *pDestRect, HWND hDestWindowOverride,
591 const RGNDATA *pDirtyRegion)
593 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
594 HRESULT hr;
596 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
597 iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
599 wined3d_mutex_lock();
600 hr = wined3d_device_present(This->wined3d_device, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
601 wined3d_mutex_unlock();
603 return hr;
606 static HRESULT WINAPI IDirect3DDevice9Impl_GetBackBuffer(IDirect3DDevice9Ex *iface,
607 UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 **ppBackBuffer)
609 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
610 struct wined3d_surface *wined3d_surface = NULL;
611 HRESULT hr;
613 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
614 iface, iSwapChain, BackBuffer, Type, ppBackBuffer);
616 wined3d_mutex_lock();
617 hr = wined3d_device_get_back_buffer(This->wined3d_device, iSwapChain,
618 BackBuffer, (enum wined3d_backbuffer_type)Type, &wined3d_surface);
619 if (SUCCEEDED(hr) && wined3d_surface && ppBackBuffer)
621 *ppBackBuffer = wined3d_surface_get_parent(wined3d_surface);
622 IDirect3DSurface9_AddRef(*ppBackBuffer);
623 wined3d_surface_decref(wined3d_surface);
625 wined3d_mutex_unlock();
627 return hr;
629 static HRESULT WINAPI IDirect3DDevice9Impl_GetRasterStatus(IDirect3DDevice9Ex *iface,
630 UINT iSwapChain, D3DRASTER_STATUS *pRasterStatus)
632 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
633 HRESULT hr;
635 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, iSwapChain, pRasterStatus);
637 wined3d_mutex_lock();
638 hr = wined3d_device_get_raster_status(This->wined3d_device,
639 iSwapChain, (struct wined3d_raster_status *)pRasterStatus);
640 wined3d_mutex_unlock();
642 return hr;
645 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(IDirect3DDevice9Ex *iface,
646 BOOL bEnableDialogs)
648 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
649 HRESULT hr;
651 TRACE("iface %p, enable %#x.\n", iface, bEnableDialogs);
653 wined3d_mutex_lock();
654 hr = wined3d_device_set_dialog_box_mode(This->wined3d_device, bEnableDialogs);
655 wined3d_mutex_unlock();
657 return hr;
660 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
661 DWORD Flags, const D3DGAMMARAMP *pRamp)
663 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
665 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, iSwapChain, Flags, pRamp);
667 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
668 wined3d_mutex_lock();
669 wined3d_device_set_gamma_ramp(This->wined3d_device, iSwapChain, Flags, (const struct wined3d_gamma_ramp *)pRamp);
670 wined3d_mutex_unlock();
673 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
674 D3DGAMMARAMP *pRamp)
676 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
678 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, iSwapChain, pRamp);
680 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
681 wined3d_mutex_lock();
682 wined3d_device_get_gamma_ramp(This->wined3d_device, iSwapChain, (struct wined3d_gamma_ramp *)pRamp);
683 wined3d_mutex_unlock();
686 static HRESULT WINAPI IDirect3DDevice9Impl_CreateTexture(IDirect3DDevice9Ex *iface,
687 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
688 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
690 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
691 IDirect3DTexture9Impl *object;
692 BOOL set_mem = FALSE;
693 HRESULT hr;
695 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
696 iface, width, height, levels, usage, format, pool, texture, shared_handle);
698 if (shared_handle)
700 if (pool == D3DPOOL_SYSTEMMEM)
702 if (levels != 1)
703 return D3DERR_INVALIDCALL;
704 set_mem = TRUE;
706 else
707 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
710 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
711 if (!object)
713 ERR("Failed to allocate texture memory.\n");
714 return D3DERR_OUTOFVIDEOMEMORY;
717 hr = texture_init(object, This, width, height, levels, usage, format, pool);
718 if (FAILED(hr))
720 WARN("Failed to initialize texture, hr %#x.\n", hr);
721 HeapFree(GetProcessHeap(), 0, object);
722 return hr;
725 if (set_mem)
727 struct wined3d_resource *resource;
728 IDirect3DSurface9Impl *surface;
730 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
731 surface = wined3d_resource_get_parent(resource);
732 wined3d_surface_set_mem(surface->wined3d_surface, *shared_handle);
735 TRACE("Created texture %p.\n", object);
736 *texture = &object->IDirect3DTexture9_iface;
738 return D3D_OK;
741 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
742 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
743 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
745 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
746 IDirect3DVolumeTexture9Impl *object;
747 HRESULT hr;
749 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
750 iface, width, height, depth, levels);
751 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
752 usage, format, pool, texture, shared_handle);
754 if (shared_handle)
755 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
757 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
758 if (!object)
760 ERR("Failed to allocate volume texture memory.\n");
761 return D3DERR_OUTOFVIDEOMEMORY;
764 hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
765 if (FAILED(hr))
767 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
768 HeapFree(GetProcessHeap(), 0, object);
769 return hr;
772 TRACE("Created volume texture %p.\n", object);
773 *texture = &object->IDirect3DVolumeTexture9_iface;
775 return D3D_OK;
778 static HRESULT WINAPI IDirect3DDevice9Impl_CreateCubeTexture(IDirect3DDevice9Ex *iface,
779 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
780 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
782 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
783 IDirect3DCubeTexture9Impl *object;
784 HRESULT hr;
786 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
787 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
789 if (shared_handle)
790 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
792 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
793 if (!object)
795 ERR("Failed to allocate cube texture memory.\n");
796 return D3DERR_OUTOFVIDEOMEMORY;
799 hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
800 if (FAILED(hr))
802 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
803 HeapFree(GetProcessHeap(), 0, object);
804 return hr;
807 TRACE("Created cube texture %p.\n", object);
808 *texture = &object->IDirect3DCubeTexture9_iface;
810 return D3D_OK;
813 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
814 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
815 HANDLE *shared_handle)
817 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
818 IDirect3DVertexBuffer9Impl *object;
819 HRESULT hr;
821 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
822 iface, size, usage, fvf, pool, buffer, shared_handle);
824 if (shared_handle)
825 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
827 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
828 if (!object)
830 ERR("Failed to allocate buffer memory.\n");
831 return D3DERR_OUTOFVIDEOMEMORY;
834 hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
835 if (FAILED(hr))
837 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
838 HeapFree(GetProcessHeap(), 0, object);
839 return hr;
842 TRACE("Created vertex buffer %p.\n", object);
843 *buffer = &object->IDirect3DVertexBuffer9_iface;
845 return D3D_OK;
848 static HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
849 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
850 HANDLE *shared_handle)
852 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
853 IDirect3DIndexBuffer9Impl *object;
854 HRESULT hr;
856 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
857 iface, size, usage, format, pool, buffer, shared_handle);
859 if (shared_handle)
860 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
862 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
863 if (!object)
865 ERR("Failed to allocate buffer memory.\n");
866 return D3DERR_OUTOFVIDEOMEMORY;
869 hr = indexbuffer_init(object, This, size, usage, format, pool);
870 if (FAILED(hr))
872 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
873 HeapFree(GetProcessHeap(), 0, object);
874 return hr;
877 TRACE("Created index buffer %p.\n", object);
878 *buffer = &object->IDirect3DIndexBuffer9_iface;
880 return D3D_OK;
883 static HRESULT IDirect3DDevice9Impl_CreateSurface(IDirect3DDevice9Impl *device, UINT Width,
884 UINT Height, D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level,
885 IDirect3DSurface9 **ppSurface, UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample,
886 DWORD MultisampleQuality)
888 IDirect3DSurface9Impl *object;
889 HRESULT hr;
891 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p.\n"
892 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
893 device, Width, Height, Format, Lockable, Discard, Level, ppSurface, Usage, Pool,
894 MultiSample, MultisampleQuality);
896 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
897 if (!object)
899 FIXME("Failed to allocate surface memory.\n");
900 return D3DERR_OUTOFVIDEOMEMORY;
903 hr = surface_init(object, device, Width, Height, Format, Lockable, Discard, Level, Usage, Pool,
904 MultiSample, MultisampleQuality);
905 if (FAILED(hr))
907 WARN("Failed to initialize surface, hr %#x.\n", hr);
908 HeapFree(GetProcessHeap(), 0, object);
909 return hr;
912 TRACE("Created surface %p.\n", object);
913 *ppSurface = &object->IDirect3DSurface9_iface;
915 return D3D_OK;
918 static HRESULT WINAPI IDirect3DDevice9Impl_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT Width,
919 UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality,
920 BOOL Lockable, IDirect3DSurface9 **ppSurface, HANDLE *shared_handle)
922 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
923 HRESULT hr;
925 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
926 "lockable %#x, surface %p, shared_handle %p.\n",
927 iface, Width, Height, Format, MultiSample, MultisampleQuality,
928 Lockable, ppSurface, shared_handle);
930 if (shared_handle)
931 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
933 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, Lockable,
934 FALSE /* Discard */, 0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,
935 MultiSample, MultisampleQuality);
937 return hr;
940 static HRESULT WINAPI IDirect3DDevice9Impl_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface,
941 UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
942 DWORD MultisampleQuality, BOOL Discard, IDirect3DSurface9 **ppSurface,
943 HANDLE *shared_handle)
945 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
946 HRESULT hr;
948 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
949 "discard %#x, surface %p, shared_handle %p.\n",
950 iface, Width, Height, Format, MultiSample, MultisampleQuality,
951 Discard, ppSurface, shared_handle);
953 if (shared_handle)
954 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
956 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
957 Discard, 0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample,
958 MultisampleQuality);
960 return hr;
964 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateSurface(IDirect3DDevice9Ex *iface,
965 IDirect3DSurface9 *pSourceSurface, const RECT *pSourceRect,
966 IDirect3DSurface9 *pDestinationSurface, const POINT *pDestPoint)
968 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
969 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(pSourceSurface);
970 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(pDestinationSurface);
971 HRESULT hr;
973 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
974 iface, pSourceSurface, pSourceRect, pDestinationSurface, pDestPoint);
976 wined3d_mutex_lock();
977 hr = wined3d_device_update_surface(This->wined3d_device, src->wined3d_surface, pSourceRect,
978 dst->wined3d_surface, pDestPoint);
979 wined3d_mutex_unlock();
981 return hr;
984 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateTexture(IDirect3DDevice9Ex *iface,
985 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
987 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
988 HRESULT hr;
990 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
992 wined3d_mutex_lock();
993 hr = wined3d_device_update_texture(This->wined3d_device,
994 ((IDirect3DBaseTexture9Impl *)src_texture)->wined3d_texture,
995 ((IDirect3DBaseTexture9Impl *)dst_texture)->wined3d_texture);
996 wined3d_mutex_unlock();
998 return hr;
1001 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1002 IDirect3DSurface9 *pRenderTarget, IDirect3DSurface9 *pDestSurface)
1004 IDirect3DSurface9Impl *renderTarget = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
1005 IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1006 HRESULT hr;
1008 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, pRenderTarget, pDestSurface);
1010 wined3d_mutex_lock();
1011 hr = wined3d_surface_get_render_target_data(destSurface->wined3d_surface, renderTarget->wined3d_surface);
1012 wined3d_mutex_unlock();
1014 return hr;
1017 static HRESULT WINAPI IDirect3DDevice9Impl_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1018 UINT iSwapChain, IDirect3DSurface9 *pDestSurface)
1020 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1021 IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1022 HRESULT hr;
1024 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, iSwapChain, pDestSurface);
1026 wined3d_mutex_lock();
1027 hr = wined3d_device_get_front_buffer_data(This->wined3d_device, iSwapChain, destSurface->wined3d_surface);
1028 wined3d_mutex_unlock();
1030 return hr;
1033 static HRESULT WINAPI IDirect3DDevice9Impl_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1034 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1036 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1037 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1038 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1039 HRESULT hr = D3DERR_INVALIDCALL;
1040 struct wined3d_resource_desc src_desc, dst_desc;
1041 struct wined3d_resource *wined3d_resource;
1043 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1044 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1046 wined3d_mutex_lock();
1047 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1048 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1050 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1051 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1053 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1055 if (This->in_scene)
1057 WARN("Rejecting depth / stencil blit while in scene.\n");
1058 goto done;
1061 if (src_rect)
1063 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1064 || src_rect->bottom != src_desc.height)
1066 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1067 wine_dbgstr_rect(src_rect));
1068 goto done;
1071 if (dst_rect)
1073 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1074 || dst_rect->bottom != dst_desc.height)
1076 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1077 wine_dbgstr_rect(dst_rect));
1078 goto done;
1081 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1083 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1084 goto done;
1088 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1089 if (hr == WINEDDERR_INVALIDRECT)
1090 hr = D3DERR_INVALIDCALL;
1092 done:
1093 wined3d_mutex_unlock();
1094 return hr;
1097 static HRESULT WINAPI IDirect3DDevice9Impl_ColorFill(IDirect3DDevice9Ex *iface,
1098 IDirect3DSurface9 *pSurface, const RECT *pRect, D3DCOLOR color)
1100 const struct wined3d_color c =
1102 ((color >> 16) & 0xff) / 255.0f,
1103 ((color >> 8) & 0xff) / 255.0f,
1104 (color & 0xff) / 255.0f,
1105 ((color >> 24) & 0xff) / 255.0f,
1107 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1108 IDirect3DSurface9Impl *surface = unsafe_impl_from_IDirect3DSurface9(pSurface);
1109 struct wined3d_resource *wined3d_resource;
1110 struct wined3d_resource_desc desc;
1111 HRESULT hr;
1113 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, pSurface, pRect, color);
1115 wined3d_mutex_lock();
1117 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
1118 wined3d_resource_get_desc(wined3d_resource, &desc);
1120 /* This method is only allowed with surfaces that are render targets, or
1121 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1122 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1124 wined3d_mutex_unlock();
1125 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1126 return D3DERR_INVALIDCALL;
1129 /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1130 hr = wined3d_device_color_fill(This->wined3d_device, surface->wined3d_surface, pRect, &c);
1132 wined3d_mutex_unlock();
1134 return hr;
1137 static HRESULT WINAPI IDirect3DDevice9Impl_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1138 UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface,
1139 HANDLE *shared_handle)
1141 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1142 HRESULT hr;
1144 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1145 iface, Width, Height, Format, Pool, ppSurface, shared_handle);
1147 if (shared_handle)
1148 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1150 if (Pool == D3DPOOL_MANAGED)
1152 FIXME("Attempting to create a managed offscreen plain surface\n");
1153 return D3DERR_INVALIDCALL;
1156 'Off-screen plain surfaces are always lockable, regardless of their pool types.'
1157 but then...
1158 D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
1159 Why, their always lockable?
1160 should I change the usage to dynamic?
1162 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
1163 FALSE /* Discard */, 0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */,
1164 Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1166 return hr;
1169 /* TODO: move to wineD3D */
1170 static HRESULT WINAPI IDirect3DDevice9Impl_SetRenderTarget(IDirect3DDevice9Ex *iface,
1171 DWORD RenderTargetIndex, IDirect3DSurface9 *pRenderTarget)
1173 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1174 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
1175 HRESULT hr;
1177 TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, pRenderTarget);
1179 if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1181 WARN("Invalid index %u specified.\n", RenderTargetIndex);
1182 return D3DERR_INVALIDCALL;
1185 wined3d_mutex_lock();
1186 hr = wined3d_device_set_render_target(This->wined3d_device, RenderTargetIndex,
1187 pSurface ? pSurface->wined3d_surface : NULL, TRUE);
1188 wined3d_mutex_unlock();
1190 return hr;
1193 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTarget(IDirect3DDevice9Ex *iface,
1194 DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget)
1196 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1197 struct wined3d_surface *wined3d_surface;
1198 HRESULT hr;
1200 TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, ppRenderTarget);
1202 if (ppRenderTarget == NULL) {
1203 return D3DERR_INVALIDCALL;
1206 if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1208 WARN("Invalid index %u specified.\n", RenderTargetIndex);
1209 return D3DERR_INVALIDCALL;
1212 wined3d_mutex_lock();
1213 hr = wined3d_device_get_render_target(This->wined3d_device, RenderTargetIndex, &wined3d_surface);
1214 if (SUCCEEDED(hr))
1216 *ppRenderTarget = wined3d_surface_get_parent(wined3d_surface);
1217 IDirect3DSurface9_AddRef(*ppRenderTarget);
1218 wined3d_surface_decref(wined3d_surface);
1220 else
1222 if (hr != WINED3DERR_NOTFOUND)
1223 WARN("Failed to get render target %u, hr %#x.\n", RenderTargetIndex, hr);
1224 *ppRenderTarget = NULL;
1226 wined3d_mutex_unlock();
1228 return hr;
1231 static HRESULT WINAPI IDirect3DDevice9Impl_SetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1232 IDirect3DSurface9 *pZStencilSurface)
1234 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1235 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pZStencilSurface);
1236 HRESULT hr;
1238 TRACE("iface %p, depth_stencil %p.\n", iface, pZStencilSurface);
1240 wined3d_mutex_lock();
1241 hr = wined3d_device_set_depth_stencil(This->wined3d_device, pSurface ? pSurface->wined3d_surface : NULL);
1242 wined3d_mutex_unlock();
1244 return hr;
1247 static HRESULT WINAPI IDirect3DDevice9Impl_GetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1248 IDirect3DSurface9 **ppZStencilSurface)
1250 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1251 struct wined3d_surface *wined3d_surface;
1252 HRESULT hr;
1254 TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1256 if(ppZStencilSurface == NULL){
1257 return D3DERR_INVALIDCALL;
1260 wined3d_mutex_lock();
1261 hr = wined3d_device_get_depth_stencil(This->wined3d_device, &wined3d_surface);
1262 if (SUCCEEDED(hr))
1264 *ppZStencilSurface = wined3d_surface_get_parent(wined3d_surface);
1265 IDirect3DSurface9_AddRef(*ppZStencilSurface);
1266 wined3d_surface_decref(wined3d_surface);
1268 else
1270 if (hr != WINED3DERR_NOTFOUND)
1271 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1272 *ppZStencilSurface = NULL;
1274 wined3d_mutex_unlock();
1276 return hr;
1279 static HRESULT WINAPI IDirect3DDevice9Impl_BeginScene(IDirect3DDevice9Ex *iface)
1281 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1282 HRESULT hr;
1284 TRACE("iface %p.\n", iface);
1286 wined3d_mutex_lock();
1287 hr = wined3d_device_begin_scene(This->wined3d_device);
1288 if (SUCCEEDED(hr)) This->in_scene = TRUE;
1289 wined3d_mutex_unlock();
1291 return hr;
1294 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_EndScene(IDirect3DDevice9Ex *iface)
1296 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1297 HRESULT hr;
1299 TRACE("iface %p.\n", iface);
1301 wined3d_mutex_lock();
1302 hr = wined3d_device_end_scene(This->wined3d_device);
1303 if (SUCCEEDED(hr)) This->in_scene = FALSE;
1304 wined3d_mutex_unlock();
1306 return hr;
1309 static HRESULT WINAPI IDirect3DDevice9Impl_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1310 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1312 const struct wined3d_color c =
1314 ((color >> 16) & 0xff) / 255.0f,
1315 ((color >> 8) & 0xff) / 255.0f,
1316 (color & 0xff) / 255.0f,
1317 ((color >> 24) & 0xff) / 255.0f,
1319 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1320 HRESULT hr;
1322 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1323 iface, rect_count, rects, flags, color, z, stencil);
1325 wined3d_mutex_lock();
1326 hr = wined3d_device_clear(This->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1327 wined3d_mutex_unlock();
1329 return hr;
1332 static HRESULT WINAPI IDirect3DDevice9Impl_SetTransform(IDirect3DDevice9Ex *iface,
1333 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *lpMatrix)
1335 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1336 HRESULT hr;
1338 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1340 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1341 wined3d_mutex_lock();
1342 hr = wined3d_device_set_transform(This->wined3d_device, State, (const struct wined3d_matrix *)lpMatrix);
1343 wined3d_mutex_unlock();
1345 return hr;
1348 static HRESULT WINAPI IDirect3DDevice9Impl_GetTransform(IDirect3DDevice9Ex *iface,
1349 D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
1351 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1352 HRESULT hr;
1354 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1356 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1357 wined3d_mutex_lock();
1358 hr = wined3d_device_get_transform(This->wined3d_device, State, (struct wined3d_matrix *)pMatrix);
1359 wined3d_mutex_unlock();
1361 return hr;
1364 static HRESULT WINAPI IDirect3DDevice9Impl_MultiplyTransform(IDirect3DDevice9Ex *iface,
1365 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
1367 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1368 HRESULT hr;
1370 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1372 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1373 wined3d_mutex_lock();
1374 hr = wined3d_device_multiply_transform(This->wined3d_device, State, (const struct wined3d_matrix *)pMatrix);
1375 wined3d_mutex_unlock();
1377 return hr;
1380 static HRESULT WINAPI IDirect3DDevice9Impl_SetViewport(IDirect3DDevice9Ex *iface,
1381 const D3DVIEWPORT9 *pViewport)
1383 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1384 HRESULT hr;
1386 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1388 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1389 wined3d_mutex_lock();
1390 hr = wined3d_device_set_viewport(This->wined3d_device, (const struct wined3d_viewport *)pViewport);
1391 wined3d_mutex_unlock();
1393 return hr;
1396 static HRESULT WINAPI IDirect3DDevice9Impl_GetViewport(IDirect3DDevice9Ex *iface,
1397 D3DVIEWPORT9 *pViewport)
1399 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1400 HRESULT hr;
1402 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1404 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1405 wined3d_mutex_lock();
1406 hr = wined3d_device_get_viewport(This->wined3d_device, (struct wined3d_viewport *)pViewport);
1407 wined3d_mutex_unlock();
1409 return hr;
1412 static HRESULT WINAPI IDirect3DDevice9Impl_SetMaterial(IDirect3DDevice9Ex *iface,
1413 const D3DMATERIAL9 *pMaterial)
1415 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1416 HRESULT hr;
1418 TRACE("iface %p, material %p.\n", iface, pMaterial);
1420 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1421 wined3d_mutex_lock();
1422 hr = wined3d_device_set_material(This->wined3d_device, (const struct wined3d_material *)pMaterial);
1423 wined3d_mutex_unlock();
1425 return hr;
1428 static HRESULT WINAPI IDirect3DDevice9Impl_GetMaterial(IDirect3DDevice9Ex *iface,
1429 D3DMATERIAL9 *pMaterial)
1431 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1432 HRESULT hr;
1434 TRACE("iface %p, material %p.\n", iface, pMaterial);
1436 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1437 wined3d_mutex_lock();
1438 hr = wined3d_device_get_material(This->wined3d_device, (struct wined3d_material *)pMaterial);
1439 wined3d_mutex_unlock();
1441 return hr;
1444 static HRESULT WINAPI IDirect3DDevice9Impl_SetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1445 const D3DLIGHT9 *pLight)
1447 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1448 HRESULT hr;
1450 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1452 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1453 wined3d_mutex_lock();
1454 hr = wined3d_device_set_light(This->wined3d_device, Index, (const struct wined3d_light *)pLight);
1455 wined3d_mutex_unlock();
1457 return hr;
1460 static HRESULT WINAPI IDirect3DDevice9Impl_GetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1461 D3DLIGHT9 *pLight)
1463 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1464 HRESULT hr;
1466 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1468 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1469 wined3d_mutex_lock();
1470 hr = wined3d_device_get_light(This->wined3d_device, Index, (struct wined3d_light *)pLight);
1471 wined3d_mutex_unlock();
1473 return hr;
1476 static HRESULT WINAPI IDirect3DDevice9Impl_LightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1477 BOOL Enable)
1479 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1480 HRESULT hr;
1482 TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1484 wined3d_mutex_lock();
1485 hr = wined3d_device_set_light_enable(This->wined3d_device, Index, Enable);
1486 wined3d_mutex_unlock();
1488 return hr;
1491 static HRESULT WINAPI IDirect3DDevice9Impl_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1492 BOOL *pEnable)
1494 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1495 HRESULT hr;
1497 TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1499 wined3d_mutex_lock();
1500 hr = wined3d_device_get_light_enable(This->wined3d_device, Index, pEnable);
1501 wined3d_mutex_unlock();
1503 return hr;
1506 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1507 const float *pPlane)
1509 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1510 HRESULT hr;
1512 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1514 wined3d_mutex_lock();
1515 hr = wined3d_device_set_clip_plane(This->wined3d_device, Index, pPlane);
1516 wined3d_mutex_unlock();
1518 return hr;
1521 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1522 float *pPlane)
1524 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1525 HRESULT hr;
1527 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1529 wined3d_mutex_lock();
1530 hr = wined3d_device_get_clip_plane(This->wined3d_device, Index, pPlane);
1531 wined3d_mutex_unlock();
1533 return hr;
1536 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetRenderState(IDirect3DDevice9Ex *iface,
1537 D3DRENDERSTATETYPE State, DWORD Value)
1539 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1540 HRESULT hr;
1542 TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1544 wined3d_mutex_lock();
1545 hr = wined3d_device_set_render_state(This->wined3d_device, State, Value);
1546 wined3d_mutex_unlock();
1548 return hr;
1551 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderState(IDirect3DDevice9Ex *iface,
1552 D3DRENDERSTATETYPE State, DWORD *pValue)
1554 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1555 HRESULT hr;
1557 TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1559 wined3d_mutex_lock();
1560 hr = wined3d_device_get_render_state(This->wined3d_device, State, pValue);
1561 wined3d_mutex_unlock();
1563 return hr;
1566 static HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(IDirect3DDevice9Ex *iface,
1567 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1569 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1570 IDirect3DStateBlock9Impl *object;
1571 HRESULT hr;
1573 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1575 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1577 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1578 return D3DERR_INVALIDCALL;
1581 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1582 if (!object)
1584 ERR("Failed to allocate stateblock memory.\n");
1585 return E_OUTOFMEMORY;
1588 hr = stateblock_init(object, This, type, NULL);
1589 if (FAILED(hr))
1591 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1592 HeapFree(GetProcessHeap(), 0, object);
1593 return hr;
1596 TRACE("Created stateblock %p.\n", object);
1597 *stateblock = &object->IDirect3DStateBlock9_iface;
1599 return D3D_OK;
1602 static HRESULT WINAPI IDirect3DDevice9Impl_BeginStateBlock(IDirect3DDevice9Ex *iface)
1604 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1605 HRESULT hr;
1607 TRACE("iface %p.\n", iface);
1609 wined3d_mutex_lock();
1610 hr = wined3d_device_begin_stateblock(This->wined3d_device);
1611 wined3d_mutex_unlock();
1613 return hr;
1616 static HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface,
1617 IDirect3DStateBlock9 **stateblock)
1619 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1620 struct wined3d_stateblock *wined3d_stateblock;
1621 IDirect3DStateBlock9Impl *object;
1622 HRESULT hr;
1624 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1626 wined3d_mutex_lock();
1627 hr = wined3d_device_end_stateblock(This->wined3d_device, &wined3d_stateblock);
1628 wined3d_mutex_unlock();
1629 if (FAILED(hr))
1631 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1632 return hr;
1635 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1636 if (!object)
1638 ERR("Failed to allocate stateblock memory.\n");
1639 wined3d_mutex_lock();
1640 wined3d_stateblock_decref(wined3d_stateblock);
1641 wined3d_mutex_unlock();
1642 return E_OUTOFMEMORY;
1645 hr = stateblock_init(object, This, 0, wined3d_stateblock);
1646 if (FAILED(hr))
1648 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1649 wined3d_mutex_lock();
1650 wined3d_stateblock_decref(wined3d_stateblock);
1651 wined3d_mutex_unlock();
1652 HeapFree(GetProcessHeap(), 0, object);
1653 return hr;
1656 TRACE("Created stateblock %p.\n", object);
1657 *stateblock = &object->IDirect3DStateBlock9_iface;
1659 return D3D_OK;
1662 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipStatus(IDirect3DDevice9Ex *iface,
1663 const D3DCLIPSTATUS9 *pClipStatus)
1665 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1666 HRESULT hr;
1668 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1670 wined3d_mutex_lock();
1671 hr = wined3d_device_set_clip_status(This->wined3d_device, (const struct wined3d_clip_status *)pClipStatus);
1672 wined3d_mutex_unlock();
1674 return hr;
1677 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipStatus(IDirect3DDevice9Ex *iface,
1678 D3DCLIPSTATUS9 *pClipStatus)
1680 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1681 HRESULT hr;
1683 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1685 wined3d_mutex_lock();
1686 hr = wined3d_device_get_clip_status(This->wined3d_device, (struct wined3d_clip_status *)pClipStatus);
1687 wined3d_mutex_unlock();
1689 return hr;
1692 static HRESULT WINAPI IDirect3DDevice9Impl_GetTexture(IDirect3DDevice9Ex *iface, DWORD Stage,
1693 IDirect3DBaseTexture9 **ppTexture)
1695 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1696 struct wined3d_texture *wined3d_texture = NULL;
1697 HRESULT hr;
1699 TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1701 if(ppTexture == NULL){
1702 return D3DERR_INVALIDCALL;
1705 wined3d_mutex_lock();
1706 hr = wined3d_device_get_texture(This->wined3d_device, Stage, &wined3d_texture);
1707 if (SUCCEEDED(hr) && wined3d_texture)
1709 *ppTexture = wined3d_texture_get_parent(wined3d_texture);
1710 IDirect3DBaseTexture9_AddRef(*ppTexture);
1711 wined3d_texture_decref(wined3d_texture);
1713 else
1715 if (FAILED(hr))
1717 WARN("Call to get texture (%u) failed (%p).\n", Stage, wined3d_texture);
1719 *ppTexture = NULL;
1721 wined3d_mutex_unlock();
1723 return hr;
1726 static HRESULT WINAPI IDirect3DDevice9Impl_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage,
1727 IDirect3DBaseTexture9 *texture)
1729 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
1730 HRESULT hr;
1732 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1734 wined3d_mutex_lock();
1735 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1736 texture ? ((IDirect3DBaseTexture9Impl *)texture)->wined3d_texture : NULL);
1737 wined3d_mutex_unlock();
1739 return hr;
1742 static const enum wined3d_texture_stage_state tss_lookup[] =
1744 WINED3D_TSS_INVALID, /* 0, unused */
1745 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1746 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1747 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1748 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1749 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1750 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1751 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1752 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1753 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1754 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1755 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1756 WINED3D_TSS_INVALID, /* 12, unused */
1757 WINED3D_TSS_INVALID, /* 13, unused */
1758 WINED3D_TSS_INVALID, /* 14, unused */
1759 WINED3D_TSS_INVALID, /* 15, unused */
1760 WINED3D_TSS_INVALID, /* 16, unused */
1761 WINED3D_TSS_INVALID, /* 17, unused */
1762 WINED3D_TSS_INVALID, /* 18, unused */
1763 WINED3D_TSS_INVALID, /* 19, unused */
1764 WINED3D_TSS_INVALID, /* 20, unused */
1765 WINED3D_TSS_INVALID, /* 21, unused */
1766 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1767 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1768 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1769 WINED3D_TSS_INVALID, /* 25, unused */
1770 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1771 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1772 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1773 WINED3D_TSS_INVALID, /* 29, unused */
1774 WINED3D_TSS_INVALID, /* 30, unused */
1775 WINED3D_TSS_INVALID, /* 31, unused */
1776 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1779 static HRESULT WINAPI IDirect3DDevice9Impl_GetTextureStageState(IDirect3DDevice9Ex *iface,
1780 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1782 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1783 HRESULT hr;
1785 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1787 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1789 WARN("Invalid Type %#x passed.\n", Type);
1790 return D3D_OK;
1793 wined3d_mutex_lock();
1794 hr = wined3d_device_get_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], pValue);
1795 wined3d_mutex_unlock();
1797 return hr;
1800 static HRESULT WINAPI IDirect3DDevice9Impl_SetTextureStageState(IDirect3DDevice9Ex *iface,
1801 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1803 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1804 HRESULT hr;
1806 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1808 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1810 WARN("Invalid Type %#x passed.\n", Type);
1811 return D3D_OK;
1814 wined3d_mutex_lock();
1815 hr = wined3d_device_set_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], Value);
1816 wined3d_mutex_unlock();
1818 return hr;
1821 static HRESULT WINAPI IDirect3DDevice9Impl_GetSamplerState(IDirect3DDevice9Ex *iface, DWORD Sampler,
1822 D3DSAMPLERSTATETYPE Type, DWORD *pValue)
1824 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1825 HRESULT hr;
1827 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, Sampler, Type, pValue);
1829 wined3d_mutex_lock();
1830 hr = wined3d_device_get_sampler_state(This->wined3d_device, Sampler, Type, pValue);
1831 wined3d_mutex_unlock();
1833 return hr;
1836 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetSamplerState(IDirect3DDevice9Ex *iface,
1837 DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value)
1839 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1840 HRESULT hr;
1842 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, Sampler, Type, Value);
1844 wined3d_mutex_lock();
1845 hr = wined3d_device_set_sampler_state(This->wined3d_device, Sampler, Type, Value);
1846 wined3d_mutex_unlock();
1848 return hr;
1851 static HRESULT WINAPI IDirect3DDevice9Impl_ValidateDevice(IDirect3DDevice9Ex *iface,
1852 DWORD *pNumPasses)
1854 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1855 HRESULT hr;
1857 TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1859 wined3d_mutex_lock();
1860 hr = wined3d_device_validate_device(This->wined3d_device, pNumPasses);
1861 wined3d_mutex_unlock();
1863 return hr;
1866 static HRESULT WINAPI IDirect3DDevice9Impl_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1867 UINT PaletteNumber, const PALETTEENTRY *pEntries)
1869 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1871 return D3DERR_INVALIDCALL;
1874 static HRESULT WINAPI IDirect3DDevice9Impl_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1875 UINT PaletteNumber, PALETTEENTRY *pEntries)
1877 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1879 return D3DERR_INVALIDCALL;
1882 static HRESULT WINAPI IDirect3DDevice9Impl_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1883 UINT PaletteNumber)
1885 FIXME("iface %p, palette_idx %u unimplemented.\n", iface, PaletteNumber);
1887 return D3DERR_INVALIDCALL;
1890 static HRESULT WINAPI IDirect3DDevice9Impl_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1891 UINT *PaletteNumber)
1893 FIXME("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1895 return D3DERR_INVALIDCALL;
1898 static HRESULT WINAPI IDirect3DDevice9Impl_SetScissorRect(IDirect3DDevice9Ex *iface,
1899 const RECT *pRect)
1901 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1902 HRESULT hr;
1904 TRACE("iface %p, rect %p.\n", iface, pRect);
1906 wined3d_mutex_lock();
1907 hr = wined3d_device_set_scissor_rect(This->wined3d_device, pRect);
1908 wined3d_mutex_unlock();
1910 return hr;
1913 static HRESULT WINAPI IDirect3DDevice9Impl_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *pRect)
1915 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1916 HRESULT hr;
1918 TRACE("iface %p, rect %p.\n", iface, pRect);
1920 wined3d_mutex_lock();
1921 hr = wined3d_device_get_scissor_rect(This->wined3d_device, pRect);
1922 wined3d_mutex_unlock();
1924 return hr;
1927 static HRESULT WINAPI IDirect3DDevice9Impl_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface,
1928 BOOL bSoftware)
1930 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1931 HRESULT hr;
1933 TRACE("iface %p, software %#x.\n", iface, bSoftware);
1935 wined3d_mutex_lock();
1936 hr = wined3d_device_set_software_vertex_processing(This->wined3d_device, bSoftware);
1937 wined3d_mutex_unlock();
1939 return hr;
1942 static BOOL WINAPI IDirect3DDevice9Impl_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
1944 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1945 BOOL ret;
1947 TRACE("iface %p.\n", iface);
1949 wined3d_mutex_lock();
1950 ret = wined3d_device_get_software_vertex_processing(This->wined3d_device);
1951 wined3d_mutex_unlock();
1953 return ret;
1956 static HRESULT WINAPI IDirect3DDevice9Impl_SetNPatchMode(IDirect3DDevice9Ex *iface, float nSegments)
1958 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1959 HRESULT hr;
1961 TRACE("iface %p, segment_count %.8e.\n", iface, nSegments);
1963 wined3d_mutex_lock();
1964 hr = wined3d_device_set_npatch_mode(This->wined3d_device, nSegments);
1965 wined3d_mutex_unlock();
1967 return hr;
1970 static float WINAPI IDirect3DDevice9Impl_GetNPatchMode(IDirect3DDevice9Ex *iface)
1972 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1973 float ret;
1975 TRACE("iface %p.\n", iface);
1977 wined3d_mutex_lock();
1978 ret = wined3d_device_get_npatch_mode(This->wined3d_device);
1979 wined3d_mutex_unlock();
1981 return ret;
1984 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(IDirect3DDevice9Ex *iface,
1985 D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1987 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1988 HRESULT hr;
1990 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1991 iface, PrimitiveType, StartVertex, PrimitiveCount);
1993 wined3d_mutex_lock();
1994 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1995 hr = wined3d_device_draw_primitive(This->wined3d_device, StartVertex,
1996 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1997 wined3d_mutex_unlock();
1999 return hr;
2002 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2003 D3DPRIMITIVETYPE PrimitiveType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices,
2004 UINT startIndex, UINT primCount)
2006 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2007 HRESULT hr;
2009 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2010 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2011 iface, PrimitiveType, BaseVertexIndex, MinVertexIndex,
2012 NumVertices, startIndex, primCount);
2014 wined3d_mutex_lock();
2015 wined3d_device_set_base_vertex_index(This->wined3d_device, BaseVertexIndex);
2016 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2017 hr = wined3d_device_draw_indexed_primitive(This->wined3d_device, startIndex,
2018 vertex_count_from_primitive_count(PrimitiveType, primCount));
2019 wined3d_mutex_unlock();
2021 return hr;
2024 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2025 D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
2026 UINT VertexStreamZeroStride)
2028 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2029 HRESULT hr;
2031 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2032 iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
2034 wined3d_mutex_lock();
2035 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2036 hr = wined3d_device_draw_primitive_up(This->wined3d_device,
2037 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
2038 pVertexStreamZeroData, VertexStreamZeroStride);
2039 wined3d_mutex_unlock();
2041 return hr;
2044 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2045 D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
2046 UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
2047 const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
2049 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2050 HRESULT hr;
2052 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
2053 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2054 iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
2055 pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
2057 wined3d_mutex_lock();
2058 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2059 hr = wined3d_device_draw_indexed_primitive_up(This->wined3d_device,
2060 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
2061 wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
2062 wined3d_mutex_unlock();
2064 return hr;
2067 static HRESULT WINAPI IDirect3DDevice9Impl_ProcessVertices(IDirect3DDevice9Ex *iface,
2068 UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9 *pDestBuffer,
2069 IDirect3DVertexDeclaration9 *pVertexDecl, DWORD Flags)
2071 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2072 IDirect3DVertexBuffer9Impl *dest = unsafe_impl_from_IDirect3DVertexBuffer9(pDestBuffer);
2073 IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
2074 HRESULT hr;
2076 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2077 iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags);
2079 wined3d_mutex_lock();
2080 hr = wined3d_device_process_vertices(This->wined3d_device, SrcStartIndex, DestIndex, VertexCount,
2081 dest->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags, dest->fvf);
2082 wined3d_mutex_unlock();
2084 return hr;
2087 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2088 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2090 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2091 IDirect3DVertexDeclaration9Impl *object;
2092 HRESULT hr;
2094 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2096 if (!declaration)
2098 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2099 return D3DERR_INVALIDCALL;
2102 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2103 if (!object)
2105 ERR("Failed to allocate vertex declaration memory.\n");
2106 return E_OUTOFMEMORY;
2109 hr = vertexdeclaration_init(object, This, elements);
2110 if (FAILED(hr))
2112 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2113 HeapFree(GetProcessHeap(), 0, object);
2114 return hr;
2117 TRACE("Created vertex declaration %p.\n", object);
2118 *declaration = (IDirect3DVertexDeclaration9 *)object;
2120 return D3D_OK;
2123 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2124 IDirect3DVertexDeclaration9 *declaration)
2126 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2127 HRESULT hr;
2129 TRACE("iface %p, declaration %p.\n", iface, declaration);
2131 wined3d_mutex_lock();
2132 hr = wined3d_device_set_vertex_declaration(This->wined3d_device,
2133 declaration ? ((IDirect3DVertexDeclaration9Impl *)declaration)->wineD3DVertexDeclaration : NULL);
2134 wined3d_mutex_unlock();
2136 return hr;
2139 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2140 IDirect3DVertexDeclaration9 **declaration)
2142 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2143 struct wined3d_vertex_declaration *wined3d_declaration = NULL;
2144 HRESULT hr;
2146 TRACE("iface %p, declaration %p.\n", iface, declaration);
2148 if (!declaration) return D3DERR_INVALIDCALL;
2150 wined3d_mutex_lock();
2151 hr = wined3d_device_get_vertex_declaration(This->wined3d_device, &wined3d_declaration);
2152 if (SUCCEEDED(hr) && wined3d_declaration)
2154 *declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2155 IDirect3DVertexDeclaration9_AddRef(*declaration);
2156 wined3d_vertex_declaration_decref(wined3d_declaration);
2158 else
2160 *declaration = NULL;
2162 wined3d_mutex_unlock();
2164 TRACE("Returning %p.\n", *declaration);
2165 return hr;
2168 static IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
2169 HRESULT hr;
2170 D3DVERTEXELEMENT9* elements = NULL;
2171 IDirect3DVertexDeclaration9* pDecl = NULL;
2172 int p, low, high; /* deliberately signed */
2173 IDirect3DVertexDeclaration9 **convertedDecls = This->convertedDecls;
2175 TRACE("Searching for declaration for fvf %08x... ", fvf);
2177 low = 0;
2178 high = This->numConvertedDecls - 1;
2179 while(low <= high) {
2180 p = (low + high) >> 1;
2181 TRACE("%d ", p);
2182 if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
2183 TRACE("found %p\n", convertedDecls[p]);
2184 return convertedDecls[p];
2185 } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
2186 low = p + 1;
2187 } else {
2188 high = p - 1;
2191 TRACE("not found. Creating and inserting at position %d.\n", low);
2193 hr = vdecl_convert_fvf(fvf, &elements);
2194 if (hr != S_OK) return NULL;
2196 hr = IDirect3DDevice9Impl_CreateVertexDeclaration(&This->IDirect3DDevice9Ex_iface, elements,
2197 &pDecl);
2198 HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
2199 if (hr != S_OK) return NULL;
2201 if(This->declArraySize == This->numConvertedDecls) {
2202 int grow = max(This->declArraySize / 2, 8);
2203 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2204 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2205 if(!convertedDecls) {
2206 /* This will destroy it */
2207 IDirect3DVertexDeclaration9_Release(pDecl);
2208 return NULL;
2210 This->convertedDecls = convertedDecls;
2211 This->declArraySize += grow;
2214 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
2215 convertedDecls[low] = pDecl;
2216 This->numConvertedDecls++;
2218 /* Will prevent the decl from being destroyed */
2219 ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
2220 IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
2222 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
2223 return pDecl;
2226 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(IDirect3DDevice9Ex *iface, DWORD FVF)
2228 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2229 IDirect3DVertexDeclaration9 *decl;
2230 HRESULT hr;
2232 TRACE("iface %p, fvf %#x.\n", iface, FVF);
2234 if (!FVF)
2236 WARN("%#x is not a valid FVF\n", FVF);
2237 return D3D_OK;
2240 wined3d_mutex_lock();
2241 decl = getConvertedDecl(This, FVF);
2242 wined3d_mutex_unlock();
2244 if (!decl)
2246 /* Any situation when this should happen, except out of memory? */
2247 ERR("Failed to create a converted vertex declaration\n");
2248 return D3DERR_DRIVERINTERNALERROR;
2251 hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, decl);
2252 if (FAILED(hr)) ERR("Failed to set vertex declaration\n");
2254 return hr;
2257 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(IDirect3DDevice9Ex *iface, DWORD *pFVF)
2259 IDirect3DVertexDeclaration9 *decl;
2260 HRESULT hr;
2262 TRACE("iface %p, fvf %p.\n", iface, pFVF);
2264 hr = IDirect3DDevice9_GetVertexDeclaration(iface, &decl);
2265 if (FAILED(hr))
2267 WARN("Failed to get vertex declaration, %#x\n", hr);
2268 *pFVF = 0;
2269 return hr;
2272 if (decl)
2274 *pFVF = ((IDirect3DVertexDeclaration9Impl *)decl)->convFVF;
2275 IDirect3DVertexDeclaration9_Release(decl);
2277 else
2279 *pFVF = 0;
2282 TRACE("Returning FVF %#x\n", *pFVF);
2284 return hr;
2287 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(IDirect3DDevice9Ex *iface,
2288 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2290 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2291 IDirect3DVertexShader9Impl *object;
2292 HRESULT hr;
2294 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2296 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2297 if (!object)
2299 ERR("Failed to allocate vertex shader memory.\n");
2300 return E_OUTOFMEMORY;
2303 hr = vertexshader_init(object, This, byte_code);
2304 if (FAILED(hr))
2306 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2307 HeapFree(GetProcessHeap(), 0, object);
2308 return hr;
2311 TRACE("Created vertex shader %p.\n", object);
2312 *shader = &object->IDirect3DVertexShader9_iface;
2314 return D3D_OK;
2317 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShader(IDirect3DDevice9Ex *iface,
2318 IDirect3DVertexShader9 *shader)
2320 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2321 IDirect3DVertexShader9Impl *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2322 HRESULT hr;
2324 TRACE("iface %p, shader %p.\n", iface, shader);
2326 wined3d_mutex_lock();
2327 hr = wined3d_device_set_vertex_shader(This->wined3d_device,
2328 shader_obj ? shader_obj->wined3d_shader : NULL);
2329 wined3d_mutex_unlock();
2331 return hr;
2334 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShader(IDirect3DDevice9Ex *iface,
2335 IDirect3DVertexShader9 **shader)
2337 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2338 struct wined3d_shader *wined3d_shader;
2340 TRACE("iface %p, shader %p.\n", iface, shader);
2342 wined3d_mutex_lock();
2343 wined3d_shader = wined3d_device_get_vertex_shader(This->wined3d_device);
2344 if (wined3d_shader)
2346 *shader = wined3d_shader_get_parent(wined3d_shader);
2347 IDirect3DVertexShader9_AddRef(*shader);
2348 wined3d_shader_decref(wined3d_shader);
2350 else
2352 *shader = NULL;
2354 wined3d_mutex_unlock();
2356 TRACE("Returning %p.\n", *shader);
2358 return D3D_OK;
2361 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2362 UINT reg_idx, const float *data, UINT count)
2364 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2365 HRESULT hr;
2367 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2369 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2371 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2372 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2373 return D3DERR_INVALIDCALL;
2376 wined3d_mutex_lock();
2377 hr = wined3d_device_set_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2378 wined3d_mutex_unlock();
2380 return hr;
2383 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2384 UINT reg_idx, float *data, UINT count)
2386 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2387 HRESULT hr;
2389 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2391 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2393 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2394 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2395 return D3DERR_INVALIDCALL;
2398 wined3d_mutex_lock();
2399 hr = wined3d_device_get_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2400 wined3d_mutex_unlock();
2402 return hr;
2405 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2406 UINT reg_idx, const int *data, UINT count)
2408 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2409 HRESULT hr;
2411 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2413 wined3d_mutex_lock();
2414 hr = wined3d_device_set_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2415 wined3d_mutex_unlock();
2417 return hr;
2420 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2421 UINT reg_idx, int *data, UINT count)
2423 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2424 HRESULT hr;
2426 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2428 wined3d_mutex_lock();
2429 hr = wined3d_device_get_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2430 wined3d_mutex_unlock();
2432 return hr;
2435 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2436 UINT reg_idx, const BOOL *data, UINT count)
2438 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2439 HRESULT hr;
2441 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2443 wined3d_mutex_lock();
2444 hr = wined3d_device_set_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2445 wined3d_mutex_unlock();
2447 return hr;
2450 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2451 UINT reg_idx, BOOL *data, UINT count)
2453 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2454 HRESULT hr;
2456 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2458 wined3d_mutex_lock();
2459 hr = wined3d_device_get_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2460 wined3d_mutex_unlock();
2462 return hr;
2465 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(IDirect3DDevice9Ex *iface,
2466 UINT StreamNumber, IDirect3DVertexBuffer9 *pStreamData, UINT OffsetInBytes, UINT Stride)
2468 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2469 IDirect3DVertexBuffer9Impl *streamdata = unsafe_impl_from_IDirect3DVertexBuffer9(pStreamData);
2470 HRESULT hr;
2472 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2473 iface, StreamNumber, pStreamData, OffsetInBytes, Stride);
2475 wined3d_mutex_lock();
2476 hr = wined3d_device_set_stream_source(This->wined3d_device, StreamNumber,
2477 streamdata ? streamdata->wineD3DVertexBuffer : NULL,
2478 OffsetInBytes, Stride);
2479 wined3d_mutex_unlock();
2481 return hr;
2484 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(IDirect3DDevice9Ex *iface,
2485 UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT *OffsetInBytes, UINT *pStride)
2487 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2488 struct wined3d_buffer *retStream = NULL;
2489 HRESULT hr;
2491 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2492 iface, StreamNumber, pStream, OffsetInBytes, pStride);
2494 if(pStream == NULL){
2495 return D3DERR_INVALIDCALL;
2498 wined3d_mutex_lock();
2499 hr = wined3d_device_get_stream_source(This->wined3d_device, StreamNumber, &retStream, OffsetInBytes, pStride);
2500 if (SUCCEEDED(hr) && retStream)
2502 *pStream = wined3d_buffer_get_parent(retStream);
2503 IDirect3DVertexBuffer9_AddRef(*pStream);
2504 wined3d_buffer_decref(retStream);
2506 else
2508 if (FAILED(hr))
2510 FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
2512 *pStream = NULL;
2514 wined3d_mutex_unlock();
2516 return hr;
2519 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2520 UINT StreamNumber, UINT Divider)
2522 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2523 HRESULT hr;
2525 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, StreamNumber, Divider);
2527 wined3d_mutex_lock();
2528 hr = wined3d_device_set_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2529 wined3d_mutex_unlock();
2531 return hr;
2534 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2535 UINT StreamNumber, UINT *Divider)
2537 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2538 HRESULT hr;
2540 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, StreamNumber, Divider);
2542 wined3d_mutex_lock();
2543 hr = wined3d_device_get_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2544 wined3d_mutex_unlock();
2546 return hr;
2549 static HRESULT WINAPI IDirect3DDevice9Impl_SetIndices(IDirect3DDevice9Ex *iface,
2550 IDirect3DIndexBuffer9 *pIndexData)
2552 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2553 IDirect3DIndexBuffer9Impl *ib = unsafe_impl_from_IDirect3DIndexBuffer9(pIndexData);
2554 HRESULT hr;
2556 TRACE("iface %p, buffer %p.\n", iface, pIndexData);
2558 wined3d_mutex_lock();
2559 hr = wined3d_device_set_index_buffer(This->wined3d_device,
2560 ib ? ib->wineD3DIndexBuffer : NULL,
2561 ib ? ib->format : WINED3DFMT_UNKNOWN);
2562 wined3d_mutex_unlock();
2564 return hr;
2567 static HRESULT WINAPI IDirect3DDevice9Impl_GetIndices(IDirect3DDevice9Ex *iface,
2568 IDirect3DIndexBuffer9 **ppIndexData)
2570 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2571 struct wined3d_buffer *retIndexData = NULL;
2572 HRESULT hr;
2574 TRACE("iface %p, buffer %p.\n", iface, ppIndexData);
2576 if(ppIndexData == NULL){
2577 return D3DERR_INVALIDCALL;
2580 wined3d_mutex_lock();
2581 hr = wined3d_device_get_index_buffer(This->wined3d_device, &retIndexData);
2582 if (SUCCEEDED(hr) && retIndexData)
2584 *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2585 IDirect3DIndexBuffer9_AddRef(*ppIndexData);
2586 wined3d_buffer_decref(retIndexData);
2588 else
2590 if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2591 *ppIndexData = NULL;
2593 wined3d_mutex_unlock();
2595 return hr;
2598 static HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(IDirect3DDevice9Ex *iface,
2599 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2601 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2602 IDirect3DPixelShader9Impl *object;
2603 HRESULT hr;
2605 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2607 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2608 if (!object)
2610 FIXME("Failed to allocate pixel shader memory.\n");
2611 return E_OUTOFMEMORY;
2614 hr = pixelshader_init(object, This, byte_code);
2615 if (FAILED(hr))
2617 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2618 HeapFree(GetProcessHeap(), 0, object);
2619 return hr;
2622 TRACE("Created pixel shader %p.\n", object);
2623 *shader = &object->IDirect3DPixelShader9_iface;
2625 return D3D_OK;
2628 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShader(IDirect3DDevice9Ex *iface,
2629 IDirect3DPixelShader9 *shader)
2631 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2632 IDirect3DPixelShader9Impl *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2633 HRESULT hr;
2635 TRACE("iface %p, shader %p.\n", iface, shader);
2637 wined3d_mutex_lock();
2638 hr = wined3d_device_set_pixel_shader(This->wined3d_device,
2639 shader_obj ? shader_obj->wined3d_shader : NULL);
2640 wined3d_mutex_unlock();
2642 return hr;
2645 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShader(IDirect3DDevice9Ex *iface,
2646 IDirect3DPixelShader9 **shader)
2648 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2649 struct wined3d_shader *wined3d_shader;
2651 TRACE("iface %p, shader %p.\n", iface, shader);
2653 if (!shader) return D3DERR_INVALIDCALL;
2655 wined3d_mutex_lock();
2656 wined3d_shader = wined3d_device_get_pixel_shader(This->wined3d_device);
2657 if (wined3d_shader)
2659 *shader = wined3d_shader_get_parent(wined3d_shader);
2660 IDirect3DPixelShader9_AddRef(*shader);
2661 wined3d_shader_decref(wined3d_shader);
2663 else
2665 *shader = NULL;
2667 wined3d_mutex_unlock();
2669 TRACE("Returning %p.\n", *shader);
2671 return D3D_OK;
2674 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2675 UINT reg_idx, const float *data, UINT count)
2677 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2678 HRESULT hr;
2680 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2682 wined3d_mutex_lock();
2683 hr = wined3d_device_set_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2684 wined3d_mutex_unlock();
2686 return hr;
2689 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2690 UINT reg_idx, float *data, UINT count)
2692 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2693 HRESULT hr;
2695 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2697 wined3d_mutex_lock();
2698 hr = wined3d_device_get_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2699 wined3d_mutex_unlock();
2701 return hr;
2704 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2705 UINT reg_idx, const int *data, UINT count)
2707 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2708 HRESULT hr;
2710 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2712 wined3d_mutex_lock();
2713 hr = wined3d_device_set_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2714 wined3d_mutex_unlock();
2716 return hr;
2719 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2720 UINT reg_idx, int *data, UINT count)
2722 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2723 HRESULT hr;
2725 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2727 wined3d_mutex_lock();
2728 hr = wined3d_device_get_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2729 wined3d_mutex_unlock();
2731 return hr;
2734 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2735 UINT reg_idx, const BOOL *data, UINT count)
2737 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2738 HRESULT hr;
2740 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2742 wined3d_mutex_lock();
2743 hr = wined3d_device_set_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2744 wined3d_mutex_unlock();
2746 return hr;
2749 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2750 UINT reg_idx, BOOL *data, UINT count)
2752 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2753 HRESULT hr;
2755 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2757 wined3d_mutex_lock();
2758 hr = wined3d_device_get_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2759 wined3d_mutex_unlock();
2761 return hr;
2764 static HRESULT WINAPI IDirect3DDevice9Impl_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2765 const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2767 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2768 HRESULT hr;
2770 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2771 iface, Handle, pNumSegs, pRectPatchInfo);
2773 wined3d_mutex_lock();
2774 hr = wined3d_device_draw_rect_patch(This->wined3d_device, Handle,
2775 pNumSegs, (const struct wined3d_rect_patch_info *)pRectPatchInfo);
2776 wined3d_mutex_unlock();
2778 return hr;
2781 static HRESULT WINAPI IDirect3DDevice9Impl_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2782 const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2784 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2785 HRESULT hr;
2787 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2788 iface, Handle, pNumSegs, pTriPatchInfo);
2790 wined3d_mutex_lock();
2791 hr = wined3d_device_draw_tri_patch(This->wined3d_device, Handle,
2792 pNumSegs, (const struct wined3d_tri_patch_info *)pTriPatchInfo);
2793 wined3d_mutex_unlock();
2795 return hr;
2798 static HRESULT WINAPI IDirect3DDevice9Impl_DeletePatch(IDirect3DDevice9Ex *iface, UINT Handle)
2800 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2801 HRESULT hr;
2803 TRACE("iface %p, handle %#x.\n", iface, Handle);
2805 wined3d_mutex_lock();
2806 hr = wined3d_device_delete_patch(This->wined3d_device, Handle);
2807 wined3d_mutex_unlock();
2809 return hr;
2812 static HRESULT WINAPI IDirect3DDevice9Impl_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type,
2813 IDirect3DQuery9 **query)
2815 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2816 IDirect3DQuery9Impl *object;
2817 HRESULT hr;
2819 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2821 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2822 if (!object)
2824 ERR("Failed to allocate query memory.\n");
2825 return E_OUTOFMEMORY;
2828 hr = query_init(object, This, type);
2829 if (FAILED(hr))
2831 WARN("Failed to initialize query, hr %#x.\n", hr);
2832 HeapFree(GetProcessHeap(), 0, object);
2833 return hr;
2836 TRACE("Created query %p.\n", object);
2837 if (query) *query = &object->IDirect3DQuery9_iface;
2838 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
2840 return D3D_OK;
2843 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2844 UINT width, UINT height, float *rows, float *columns)
2846 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2847 iface, width, height, rows, columns);
2849 return E_NOTIMPL;
2852 static HRESULT WINAPI IDirect3DDevice9ExImpl_ComposeRects(IDirect3DDevice9Ex *iface,
2853 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2854 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2856 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2857 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2858 iface, src_surface, dst_surface, src_descs, rect_count,
2859 dst_descs, operation, offset_x, offset_y);
2861 return E_NOTIMPL;
2864 static HRESULT WINAPI IDirect3DDevice9ExImpl_PresentEx(IDirect3DDevice9Ex *iface,
2865 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2866 const RGNDATA *dirty_region, DWORD flags)
2868 FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2869 iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2871 return E_NOTIMPL;
2874 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2876 FIXME("iface %p, priority %p stub!\n", iface, priority);
2878 return E_NOTIMPL;
2881 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2883 FIXME("iface %p, priority %d stub!\n", iface, priority);
2885 return E_NOTIMPL;
2888 static HRESULT WINAPI IDirect3DDevice9ExImpl_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2890 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2892 return E_NOTIMPL;
2895 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2896 IDirect3DResource9 **resources, UINT32 resource_count)
2898 FIXME("iface %p, resources %p, resource_count %u stub!\n",
2899 iface, resources, resource_count);
2901 return E_NOTIMPL;
2904 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2906 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2908 return E_NOTIMPL;
2911 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2913 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2915 *max_latency = 2;
2917 return E_NOTIMPL;
2920 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2922 static int i;
2924 TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
2926 if (!i++)
2927 FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2929 return D3D_OK;
2932 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2933 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2934 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2936 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2937 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2938 iface, width, height, format, multisample_type, multisample_quality,
2939 lockable, surface, shared_handle, usage);
2941 if (shared_handle)
2942 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2944 return E_NOTIMPL;
2947 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2948 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2949 HANDLE *shared_handle, DWORD usage)
2951 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2952 iface, width, height, format, pool, surface, shared_handle, usage);
2954 return E_NOTIMPL;
2957 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2958 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2959 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2961 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2962 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2963 iface, width, height, format, multisample_type, multisample_quality,
2964 discard, surface, shared_handle, usage);
2966 if (shared_handle)
2967 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2969 return E_NOTIMPL;
2972 static HRESULT WINAPI IDirect3DDevice9ExImpl_ResetEx(IDirect3DDevice9Ex *iface,
2973 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2975 FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2977 return E_NOTIMPL;
2980 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2981 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2983 FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2985 return E_NOTIMPL;
2988 static const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
2990 /* IUnknown */
2991 IDirect3DDevice9Impl_QueryInterface,
2992 IDirect3DDevice9Impl_AddRef,
2993 IDirect3DDevice9Impl_Release,
2994 /* IDirect3DDevice9 */
2995 IDirect3DDevice9Impl_TestCooperativeLevel,
2996 IDirect3DDevice9Impl_GetAvailableTextureMem,
2997 IDirect3DDevice9Impl_EvictManagedResources,
2998 IDirect3DDevice9Impl_GetDirect3D,
2999 IDirect3DDevice9Impl_GetDeviceCaps,
3000 IDirect3DDevice9Impl_GetDisplayMode,
3001 IDirect3DDevice9Impl_GetCreationParameters,
3002 IDirect3DDevice9Impl_SetCursorProperties,
3003 IDirect3DDevice9Impl_SetCursorPosition,
3004 IDirect3DDevice9Impl_ShowCursor,
3005 IDirect3DDevice9Impl_CreateAdditionalSwapChain,
3006 IDirect3DDevice9Impl_GetSwapChain,
3007 IDirect3DDevice9Impl_GetNumberOfSwapChains,
3008 IDirect3DDevice9Impl_Reset,
3009 IDirect3DDevice9Impl_Present,
3010 IDirect3DDevice9Impl_GetBackBuffer,
3011 IDirect3DDevice9Impl_GetRasterStatus,
3012 IDirect3DDevice9Impl_SetDialogBoxMode,
3013 IDirect3DDevice9Impl_SetGammaRamp,
3014 IDirect3DDevice9Impl_GetGammaRamp,
3015 IDirect3DDevice9Impl_CreateTexture,
3016 IDirect3DDevice9Impl_CreateVolumeTexture,
3017 IDirect3DDevice9Impl_CreateCubeTexture,
3018 IDirect3DDevice9Impl_CreateVertexBuffer,
3019 IDirect3DDevice9Impl_CreateIndexBuffer,
3020 IDirect3DDevice9Impl_CreateRenderTarget,
3021 IDirect3DDevice9Impl_CreateDepthStencilSurface,
3022 IDirect3DDevice9Impl_UpdateSurface,
3023 IDirect3DDevice9Impl_UpdateTexture,
3024 IDirect3DDevice9Impl_GetRenderTargetData,
3025 IDirect3DDevice9Impl_GetFrontBufferData,
3026 IDirect3DDevice9Impl_StretchRect,
3027 IDirect3DDevice9Impl_ColorFill,
3028 IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
3029 IDirect3DDevice9Impl_SetRenderTarget,
3030 IDirect3DDevice9Impl_GetRenderTarget,
3031 IDirect3DDevice9Impl_SetDepthStencilSurface,
3032 IDirect3DDevice9Impl_GetDepthStencilSurface,
3033 IDirect3DDevice9Impl_BeginScene,
3034 IDirect3DDevice9Impl_EndScene,
3035 IDirect3DDevice9Impl_Clear,
3036 IDirect3DDevice9Impl_SetTransform,
3037 IDirect3DDevice9Impl_GetTransform,
3038 IDirect3DDevice9Impl_MultiplyTransform,
3039 IDirect3DDevice9Impl_SetViewport,
3040 IDirect3DDevice9Impl_GetViewport,
3041 IDirect3DDevice9Impl_SetMaterial,
3042 IDirect3DDevice9Impl_GetMaterial,
3043 IDirect3DDevice9Impl_SetLight,
3044 IDirect3DDevice9Impl_GetLight,
3045 IDirect3DDevice9Impl_LightEnable,
3046 IDirect3DDevice9Impl_GetLightEnable,
3047 IDirect3DDevice9Impl_SetClipPlane,
3048 IDirect3DDevice9Impl_GetClipPlane,
3049 IDirect3DDevice9Impl_SetRenderState,
3050 IDirect3DDevice9Impl_GetRenderState,
3051 IDirect3DDevice9Impl_CreateStateBlock,
3052 IDirect3DDevice9Impl_BeginStateBlock,
3053 IDirect3DDevice9Impl_EndStateBlock,
3054 IDirect3DDevice9Impl_SetClipStatus,
3055 IDirect3DDevice9Impl_GetClipStatus,
3056 IDirect3DDevice9Impl_GetTexture,
3057 IDirect3DDevice9Impl_SetTexture,
3058 IDirect3DDevice9Impl_GetTextureStageState,
3059 IDirect3DDevice9Impl_SetTextureStageState,
3060 IDirect3DDevice9Impl_GetSamplerState,
3061 IDirect3DDevice9Impl_SetSamplerState,
3062 IDirect3DDevice9Impl_ValidateDevice,
3063 IDirect3DDevice9Impl_SetPaletteEntries,
3064 IDirect3DDevice9Impl_GetPaletteEntries,
3065 IDirect3DDevice9Impl_SetCurrentTexturePalette,
3066 IDirect3DDevice9Impl_GetCurrentTexturePalette,
3067 IDirect3DDevice9Impl_SetScissorRect,
3068 IDirect3DDevice9Impl_GetScissorRect,
3069 IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
3070 IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
3071 IDirect3DDevice9Impl_SetNPatchMode,
3072 IDirect3DDevice9Impl_GetNPatchMode,
3073 IDirect3DDevice9Impl_DrawPrimitive,
3074 IDirect3DDevice9Impl_DrawIndexedPrimitive,
3075 IDirect3DDevice9Impl_DrawPrimitiveUP,
3076 IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
3077 IDirect3DDevice9Impl_ProcessVertices,
3078 IDirect3DDevice9Impl_CreateVertexDeclaration,
3079 IDirect3DDevice9Impl_SetVertexDeclaration,
3080 IDirect3DDevice9Impl_GetVertexDeclaration,
3081 IDirect3DDevice9Impl_SetFVF,
3082 IDirect3DDevice9Impl_GetFVF,
3083 IDirect3DDevice9Impl_CreateVertexShader,
3084 IDirect3DDevice9Impl_SetVertexShader,
3085 IDirect3DDevice9Impl_GetVertexShader,
3086 IDirect3DDevice9Impl_SetVertexShaderConstantF,
3087 IDirect3DDevice9Impl_GetVertexShaderConstantF,
3088 IDirect3DDevice9Impl_SetVertexShaderConstantI,
3089 IDirect3DDevice9Impl_GetVertexShaderConstantI,
3090 IDirect3DDevice9Impl_SetVertexShaderConstantB,
3091 IDirect3DDevice9Impl_GetVertexShaderConstantB,
3092 IDirect3DDevice9Impl_SetStreamSource,
3093 IDirect3DDevice9Impl_GetStreamSource,
3094 IDirect3DDevice9Impl_SetStreamSourceFreq,
3095 IDirect3DDevice9Impl_GetStreamSourceFreq,
3096 IDirect3DDevice9Impl_SetIndices,
3097 IDirect3DDevice9Impl_GetIndices,
3098 IDirect3DDevice9Impl_CreatePixelShader,
3099 IDirect3DDevice9Impl_SetPixelShader,
3100 IDirect3DDevice9Impl_GetPixelShader,
3101 IDirect3DDevice9Impl_SetPixelShaderConstantF,
3102 IDirect3DDevice9Impl_GetPixelShaderConstantF,
3103 IDirect3DDevice9Impl_SetPixelShaderConstantI,
3104 IDirect3DDevice9Impl_GetPixelShaderConstantI,
3105 IDirect3DDevice9Impl_SetPixelShaderConstantB,
3106 IDirect3DDevice9Impl_GetPixelShaderConstantB,
3107 IDirect3DDevice9Impl_DrawRectPatch,
3108 IDirect3DDevice9Impl_DrawTriPatch,
3109 IDirect3DDevice9Impl_DeletePatch,
3110 IDirect3DDevice9Impl_CreateQuery,
3111 /* IDirect3DDevice9Ex */
3112 IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
3113 IDirect3DDevice9ExImpl_ComposeRects,
3114 IDirect3DDevice9ExImpl_PresentEx,
3115 IDirect3DDevice9ExImpl_GetGPUThreadPriority,
3116 IDirect3DDevice9ExImpl_SetGPUThreadPriority,
3117 IDirect3DDevice9ExImpl_WaitForVBlank,
3118 IDirect3DDevice9ExImpl_CheckResourceResidency,
3119 IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
3120 IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
3121 IDirect3DDevice9ExImpl_CheckDeviceState,
3122 IDirect3DDevice9ExImpl_CreateRenderTargetEx,
3123 IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
3124 IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
3125 IDirect3DDevice9ExImpl_ResetEx,
3126 IDirect3DDevice9ExImpl_GetDisplayModeEx
3129 static inline struct IDirect3DDevice9Impl *device_from_device_parent(struct wined3d_device_parent *device_parent)
3131 return CONTAINING_RECORD(device_parent, struct IDirect3DDevice9Impl, device_parent);
3134 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3135 struct wined3d_device *device)
3137 TRACE("device_parent %p, device %p.\n", device_parent, device);
3140 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3142 TRACE("device_parent %p.\n", device_parent);
3145 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
3146 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
3147 enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
3149 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3150 IDirect3DSurface9Impl *d3d_surface;
3151 BOOL lockable = TRUE;
3152 HRESULT hr;
3154 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
3155 "\tpool %#x, level %u, face %u, surface %p.\n",
3156 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
3158 if (pool == WINED3D_POOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
3159 lockable = FALSE;
3161 hr = IDirect3DDevice9Impl_CreateSurface(device, width, height,
3162 d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
3163 (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
3164 if (FAILED(hr))
3166 WARN("Failed to create surface, hr %#x.\n", hr);
3167 return hr;
3170 *surface = d3d_surface->wined3d_surface;
3171 wined3d_surface_incref(*surface);
3173 d3d_surface->container = container_parent;
3174 IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
3175 d3d_surface->parentDevice = NULL;
3177 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3178 d3d_surface->forwardReference = container_parent;
3180 return hr;
3183 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
3184 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
3185 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, BOOL lockable,
3186 struct wined3d_surface **surface)
3188 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3189 IDirect3DSurface9Impl *d3d_surface;
3190 HRESULT hr;
3192 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3193 "\tmultisample_quality %u, lockable %u, surface %p.\n",
3194 device_parent, container_parent, width, height, format, multisample_type,
3195 multisample_quality, lockable, surface);
3197 hr = IDirect3DDevice9Impl_CreateRenderTarget(&device->IDirect3DDevice9Ex_iface, width, height,
3198 d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
3199 (IDirect3DSurface9 **)&d3d_surface, NULL);
3200 if (FAILED(hr))
3202 WARN("Failed to create rendertarget, hr %#x.\n", hr);
3203 return hr;
3206 *surface = d3d_surface->wined3d_surface;
3207 wined3d_surface_incref(*surface);
3209 d3d_surface->container = container_parent;
3210 /* Implicit surfaces are created with an refcount of 0 */
3211 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3213 return hr;
3216 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
3217 UINT width, UINT height, enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
3218 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
3220 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3221 IDirect3DSurface9Impl *d3d_surface;
3222 HRESULT hr;
3224 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3225 "\tmultisample_quality %u, discard %u, surface %p.\n",
3226 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
3228 hr = IDirect3DDevice9Impl_CreateDepthStencilSurface(&device->IDirect3DDevice9Ex_iface, width,
3229 height, d3dformat_from_wined3dformat(format), multisample_type, multisample_quality,
3230 discard, (IDirect3DSurface9 **)&d3d_surface, NULL);
3231 if (FAILED(hr))
3233 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
3234 return hr;
3237 *surface = d3d_surface->wined3d_surface;
3238 wined3d_surface_incref(*surface);
3239 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice9Ex_iface;
3240 /* Implicit surfaces are created with an refcount of 0 */
3241 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3243 return hr;
3246 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
3247 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
3248 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
3250 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3251 IDirect3DVolume9Impl *object;
3252 HRESULT hr;
3254 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
3255 "format %#x, pool %#x, usage %#x, volume %p\n",
3256 device_parent, container_parent, width, height, depth,
3257 format, pool, usage, volume);
3259 /* Allocate the storage for the device */
3260 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3261 if (!object)
3263 FIXME("Allocation of memory failed\n");
3264 *volume = NULL;
3265 return D3DERR_OUTOFVIDEOMEMORY;
3268 hr = volume_init(object, device, width, height, depth, usage, format, pool);
3269 if (FAILED(hr))
3271 WARN("Failed to initialize volume, hr %#x.\n", hr);
3272 HeapFree(GetProcessHeap(), 0, object);
3273 return hr;
3276 *volume = object->wined3d_volume;
3277 wined3d_volume_incref(*volume);
3278 IDirect3DVolume9_Release(&object->IDirect3DVolume9_iface);
3280 object->container = container_parent;
3281 object->forwardReference = container_parent;
3283 TRACE("Created volume %p.\n", object);
3285 return hr;
3288 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3289 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3291 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3292 D3DPRESENT_PARAMETERS local_parameters;
3293 IDirect3DSwapChain9 *d3d_swapchain;
3294 HRESULT hr;
3296 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3298 /* Copy the presentation parameters */
3299 local_parameters.BackBufferWidth = desc->backbuffer_width;
3300 local_parameters.BackBufferHeight = desc->backbuffer_height;
3301 local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(desc->backbuffer_format);
3302 local_parameters.BackBufferCount = desc->backbuffer_count;
3303 local_parameters.MultiSampleType = desc->multisample_type;
3304 local_parameters.MultiSampleQuality = desc->multisample_quality;
3305 local_parameters.SwapEffect = desc->swap_effect;
3306 local_parameters.hDeviceWindow = desc->device_window;
3307 local_parameters.Windowed = desc->windowed;
3308 local_parameters.EnableAutoDepthStencil = desc->enable_auto_depth_stencil;
3309 local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc->auto_depth_stencil_format);
3310 local_parameters.Flags = desc->flags;
3311 local_parameters.FullScreen_RefreshRateInHz = desc->refresh_rate;
3312 local_parameters.PresentationInterval = desc->swap_interval;
3314 hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain(&device->IDirect3DDevice9Ex_iface,
3315 &local_parameters, &d3d_swapchain);
3316 if (FAILED(hr))
3318 WARN("Failed to create swapchain, hr %#x.\n", hr);
3319 *swapchain = NULL;
3320 return hr;
3323 *swapchain = ((IDirect3DSwapChain9Impl *)d3d_swapchain)->wined3d_swapchain;
3324 wined3d_swapchain_incref(*swapchain);
3325 IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)d3d_swapchain);
3327 /* Copy back the presentation parameters */
3328 desc->backbuffer_width = local_parameters.BackBufferWidth;
3329 desc->backbuffer_height = local_parameters.BackBufferHeight;
3330 desc->backbuffer_format = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3331 desc->backbuffer_count = local_parameters.BackBufferCount;
3332 desc->multisample_type = local_parameters.MultiSampleType;
3333 desc->multisample_quality = local_parameters.MultiSampleQuality;
3334 desc->swap_effect = local_parameters.SwapEffect;
3335 desc->device_window = local_parameters.hDeviceWindow;
3336 desc->windowed = local_parameters.Windowed;
3337 desc->enable_auto_depth_stencil = local_parameters.EnableAutoDepthStencil;
3338 desc->auto_depth_stencil_format = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3339 desc->flags = local_parameters.Flags;
3340 desc->refresh_rate = local_parameters.FullScreen_RefreshRateInHz;
3341 desc->swap_interval = local_parameters.PresentationInterval;
3343 return hr;
3346 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3348 device_parent_wined3d_device_created,
3349 device_parent_mode_changed,
3350 device_parent_create_surface,
3351 device_parent_create_rendertarget,
3352 device_parent_create_depth_stencil,
3353 device_parent_create_volume,
3354 device_parent_create_swapchain,
3357 static void setup_fpu(void)
3359 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3360 WORD cw;
3361 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3362 cw = (cw & ~0xf3f) | 0x3f;
3363 __asm__ volatile ("fldcw %0" : : "m" (cw));
3364 #elif defined(__i386__) && defined(_MSC_VER)
3365 WORD cw;
3366 __asm fnstcw cw;
3367 cw = (cw & ~0xf3f) | 0x3f;
3368 __asm fldcw cw;
3369 #else
3370 FIXME("FPU setup not implemented for this platform.\n");
3371 #endif
3374 HRESULT device_init(IDirect3DDevice9Impl *device, IDirect3D9Impl *parent, struct wined3d *wined3d,
3375 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3376 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3378 struct wined3d_swapchain_desc *swapchain_desc;
3379 UINT i, count = 1;
3380 HRESULT hr;
3382 if (mode)
3383 FIXME("Ignoring display mode.\n");
3385 device->IDirect3DDevice9Ex_iface.lpVtbl = &Direct3DDevice9_Vtbl;
3386 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3387 device->ref = 1;
3389 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3391 wined3d_mutex_lock();
3392 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3393 &device->device_parent, &device->wined3d_device);
3394 if (FAILED(hr))
3396 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3397 wined3d_mutex_unlock();
3398 return hr;
3401 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3403 WINED3DCAPS caps;
3405 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3406 count = caps.NumberOfAdaptersInGroup;
3409 if (flags & D3DCREATE_MULTITHREADED)
3410 wined3d_device_set_multithreaded(device->wined3d_device);
3412 if (!parameters->Windowed)
3414 if (!focus_window)
3415 focus_window = parameters->hDeviceWindow;
3416 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3418 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3419 wined3d_device_decref(device->wined3d_device);
3420 wined3d_mutex_unlock();
3421 return hr;
3424 for (i = 0; i < count; ++i)
3426 HWND device_window = parameters[i].hDeviceWindow;
3428 if (!device_window) device_window = focus_window;
3429 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3430 parameters[i].BackBufferWidth,
3431 parameters[i].BackBufferHeight);
3435 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3436 if (!swapchain_desc)
3438 ERR("Failed to allocate wined3d parameters.\n");
3439 wined3d_device_decref(device->wined3d_device);
3440 wined3d_mutex_unlock();
3441 return E_OUTOFMEMORY;
3444 for (i = 0; i < count; ++i)
3446 swapchain_desc[i].backbuffer_width = parameters[i].BackBufferWidth;
3447 swapchain_desc[i].backbuffer_height = parameters[i].BackBufferHeight;
3448 swapchain_desc[i].backbuffer_format = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
3449 swapchain_desc[i].backbuffer_count = parameters[i].BackBufferCount;
3450 swapchain_desc[i].multisample_type = parameters[i].MultiSampleType;
3451 swapchain_desc[i].multisample_quality = parameters[i].MultiSampleQuality;
3452 swapchain_desc[i].swap_effect = parameters[i].SwapEffect;
3453 swapchain_desc[i].device_window = parameters[i].hDeviceWindow;
3454 swapchain_desc[i].windowed = parameters[i].Windowed;
3455 swapchain_desc[i].enable_auto_depth_stencil = parameters[i].EnableAutoDepthStencil;
3456 swapchain_desc[i].auto_depth_stencil_format =
3457 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
3458 swapchain_desc[i].flags = parameters[i].Flags;
3459 swapchain_desc[i].refresh_rate = parameters[i].FullScreen_RefreshRateInHz;
3460 swapchain_desc[i].swap_interval = parameters[i].PresentationInterval;
3461 swapchain_desc[i].auto_restore_display_mode = TRUE;
3464 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3465 if (FAILED(hr))
3467 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3468 wined3d_device_release_focus_window(device->wined3d_device);
3469 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3470 wined3d_device_decref(device->wined3d_device);
3471 wined3d_mutex_unlock();
3472 return hr;
3475 wined3d_mutex_unlock();
3477 for (i = 0; i < count; ++i)
3479 parameters[i].BackBufferWidth = swapchain_desc[i].backbuffer_width;
3480 parameters[i].BackBufferHeight = swapchain_desc[i].backbuffer_height;
3481 parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc[i].backbuffer_format);
3482 parameters[i].BackBufferCount = swapchain_desc[i].backbuffer_count;
3483 parameters[i].MultiSampleType = swapchain_desc[i].multisample_type;
3484 parameters[i].MultiSampleQuality = swapchain_desc[i].multisample_quality;
3485 parameters[i].SwapEffect = swapchain_desc[i].swap_effect;
3486 parameters[i].hDeviceWindow = swapchain_desc[i].device_window;
3487 parameters[i].Windowed = swapchain_desc[i].windowed;
3488 parameters[i].EnableAutoDepthStencil = swapchain_desc[i].enable_auto_depth_stencil;
3489 parameters[i].AutoDepthStencilFormat =
3490 d3dformat_from_wined3dformat(swapchain_desc[i].auto_depth_stencil_format);
3491 parameters[i].Flags = swapchain_desc[i].flags;
3492 parameters[i].FullScreen_RefreshRateInHz = swapchain_desc[i].refresh_rate;
3493 parameters[i].PresentationInterval = swapchain_desc[i].swap_interval;
3495 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3497 /* Initialize the converted declaration array. This creates a valid pointer
3498 * and when adding decls HeapReAlloc() can be used without further checking. */
3499 device->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
3500 if (!device->convertedDecls)
3502 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3503 wined3d_mutex_lock();
3504 wined3d_device_uninit_3d(device->wined3d_device);
3505 wined3d_device_release_focus_window(device->wined3d_device);
3506 wined3d_device_decref(device->wined3d_device);
3507 wined3d_mutex_unlock();
3508 return E_OUTOFMEMORY;
3511 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3512 device->d3d_parent = parent;
3514 return D3D_OK;