wined3d: Get rid of the WINED3DPRESENT_PARAMETERS typedef.
[wine/multimedia.git] / dlls / d3d9 / device.c
blobf4686e3554887087fed70f68671eccd424618217
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);
193 IDirect3D9 *d3d;
194 IDirect3D9Impl *d3dimpl;
196 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
198 if (IsEqualGUID(riid, &IID_IUnknown)
199 || IsEqualGUID(riid, &IID_IDirect3DDevice9)) {
200 IDirect3DDevice9Ex_AddRef(iface);
201 *ppobj = This;
202 TRACE("Returning IDirect3DDevice9 interface at %p\n", *ppobj);
203 return S_OK;
204 } else if(IsEqualGUID(riid, &IID_IDirect3DDevice9Ex)) {
205 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
206 * It doesn't matter with which function the device was created.
208 IDirect3DDevice9_GetDirect3D(iface, &d3d);
209 d3dimpl = (IDirect3D9Impl *) d3d;
211 if(d3dimpl->extended) {
212 *ppobj = iface;
213 IDirect3DDevice9Ex_AddRef((IDirect3DDevice9Ex *) *ppobj);
214 IDirect3D9_Release(d3d);
215 TRACE("Returning IDirect3DDevice9Ex interface at %p\n", *ppobj);
216 return S_OK;
217 } else {
218 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE\n");
219 IDirect3D9_Release(d3d);
220 *ppobj = NULL;
221 return E_NOINTERFACE;
225 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
226 *ppobj = NULL;
227 return E_NOINTERFACE;
230 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(IDirect3DDevice9Ex *iface)
232 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
233 ULONG ref = InterlockedIncrement(&This->ref);
235 TRACE("%p increasing refcount to %u.\n", iface, ref);
237 return ref;
240 static ULONG WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Release(IDirect3DDevice9Ex *iface)
242 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
243 ULONG ref;
245 if (This->inDestruction) return 0;
246 ref = InterlockedDecrement(&This->ref);
248 TRACE("%p decreasing refcount to %u.\n", iface, ref);
250 if (ref == 0) {
251 unsigned i;
252 This->inDestruction = TRUE;
254 wined3d_mutex_lock();
255 for(i = 0; i < This->numConvertedDecls; i++) {
256 /* Unless Wine is buggy or the app has a bug the refcount will be 0, because decls hold a reference to the
257 * device
259 IDirect3DVertexDeclaration9Impl_Destroy(This->convertedDecls[i]);
261 HeapFree(GetProcessHeap(), 0, This->convertedDecls);
263 wined3d_device_uninit_3d(This->wined3d_device);
264 wined3d_device_release_focus_window(This->wined3d_device);
265 wined3d_device_decref(This->wined3d_device);
266 wined3d_mutex_unlock();
268 IDirect3D9_Release(This->d3d_parent);
270 HeapFree(GetProcessHeap(), 0, This);
272 return ref;
275 static HRESULT WINAPI IDirect3DDevice9Impl_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
277 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
279 TRACE("iface %p.\n", iface);
281 if (This->notreset)
283 TRACE("D3D9 device is marked not reset.\n");
284 return D3DERR_DEVICENOTRESET;
287 return D3D_OK;
290 static UINT WINAPI IDirect3DDevice9Impl_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
292 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
293 HRESULT hr;
295 TRACE("iface %p.\n", iface);
297 wined3d_mutex_lock();
298 hr = wined3d_device_get_available_texture_mem(This->wined3d_device);
299 wined3d_mutex_unlock();
301 return hr;
304 static HRESULT WINAPI IDirect3DDevice9Impl_EvictManagedResources(IDirect3DDevice9Ex *iface)
306 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
308 TRACE("iface %p.\n", iface);
310 wined3d_mutex_lock();
311 wined3d_device_evict_managed_resources(This->wined3d_device);
312 wined3d_mutex_unlock();
314 return D3D_OK;
317 static HRESULT WINAPI IDirect3DDevice9Impl_GetDirect3D(IDirect3DDevice9Ex *iface,
318 IDirect3D9 **ppD3D9)
320 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
322 TRACE("iface %p, d3d9 %p.\n", iface, ppD3D9);
324 if (NULL == ppD3D9) {
325 return D3DERR_INVALIDCALL;
328 return IDirect3D9Ex_QueryInterface(This->d3d_parent, &IID_IDirect3D9, (void **)ppD3D9);
331 static HRESULT WINAPI IDirect3DDevice9Impl_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *pCaps)
333 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
334 HRESULT hrc = D3D_OK;
335 WINED3DCAPS *pWineCaps;
337 TRACE("iface %p, caps %p.\n", iface, pCaps);
339 if(NULL == pCaps){
340 return D3DERR_INVALIDCALL;
342 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
343 if(pWineCaps == NULL){
344 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
347 memset(pCaps, 0, sizeof(*pCaps));
349 wined3d_mutex_lock();
350 hrc = wined3d_device_get_device_caps(This->wined3d_device, pWineCaps);
351 wined3d_mutex_unlock();
353 WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
354 HeapFree(GetProcessHeap(), 0, pWineCaps);
356 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
357 pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
359 filter_caps(pCaps);
361 TRACE("Returning %p %p\n", This, pCaps);
362 return hrc;
365 static HRESULT WINAPI IDirect3DDevice9Impl_GetDisplayMode(IDirect3DDevice9Ex *iface,
366 UINT iSwapChain, D3DDISPLAYMODE *pMode)
368 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
369 HRESULT hr;
371 TRACE("iface %p, swapchain %u, mode %p.\n", iface, iSwapChain, pMode);
373 wined3d_mutex_lock();
374 hr = wined3d_device_get_display_mode(This->wined3d_device, iSwapChain, (struct wined3d_display_mode *)pMode);
375 wined3d_mutex_unlock();
377 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
379 return hr;
382 static HRESULT WINAPI IDirect3DDevice9Impl_GetCreationParameters(IDirect3DDevice9Ex *iface,
383 D3DDEVICE_CREATION_PARAMETERS *pParameters)
385 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
386 HRESULT hr;
388 TRACE("iface %p, parameters %p.\n", iface, pParameters);
390 wined3d_mutex_lock();
391 hr = wined3d_device_get_creation_parameters(This->wined3d_device,
392 (struct wined3d_device_creation_parameters *)pParameters);
393 wined3d_mutex_unlock();
395 return hr;
398 static HRESULT WINAPI IDirect3DDevice9Impl_SetCursorProperties(IDirect3DDevice9Ex *iface,
399 UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9 *pCursorBitmap)
401 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
402 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pCursorBitmap);
403 HRESULT hr;
405 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
406 iface, XHotSpot, YHotSpot, pCursorBitmap);
408 if (!pCursorBitmap)
410 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
411 return D3DERR_INVALIDCALL;
414 wined3d_mutex_lock();
415 hr = wined3d_device_set_cursor_properties(This->wined3d_device, XHotSpot, YHotSpot, pSurface->wined3d_surface);
416 wined3d_mutex_unlock();
418 return hr;
421 static void WINAPI IDirect3DDevice9Impl_SetCursorPosition(IDirect3DDevice9Ex *iface,
422 int XScreenSpace, int YScreenSpace, DWORD Flags)
424 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
426 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, XScreenSpace, YScreenSpace, Flags);
428 wined3d_mutex_lock();
429 wined3d_device_set_cursor_position(This->wined3d_device, XScreenSpace, YScreenSpace, Flags);
430 wined3d_mutex_unlock();
433 static BOOL WINAPI IDirect3DDevice9Impl_ShowCursor(IDirect3DDevice9Ex *iface, BOOL bShow)
435 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
436 BOOL ret;
438 TRACE("iface %p, show %#x.\n", iface, bShow);
440 wined3d_mutex_lock();
441 ret = wined3d_device_show_cursor(This->wined3d_device, bShow);
442 wined3d_mutex_unlock();
444 return ret;
447 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
448 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
450 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
451 IDirect3DSwapChain9Impl *object;
452 HRESULT hr;
454 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
455 iface, present_parameters, swapchain);
457 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
458 if (!object)
460 ERR("Failed to allocate swapchain memory.\n");
461 return E_OUTOFMEMORY;
464 hr = swapchain_init(object, This, present_parameters);
465 if (FAILED(hr))
467 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
468 HeapFree(GetProcessHeap(), 0, object);
469 return hr;
472 TRACE("Created swapchain %p.\n", object);
473 *swapchain = (IDirect3DSwapChain9 *)object;
475 return D3D_OK;
478 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_GetSwapChain(IDirect3DDevice9Ex *iface,
479 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
481 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
482 struct wined3d_swapchain *wined3d_swapchain = NULL;
483 HRESULT hr;
485 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
487 wined3d_mutex_lock();
488 hr = wined3d_device_get_swapchain(This->wined3d_device, swapchain_idx, &wined3d_swapchain);
489 if (SUCCEEDED(hr) && wined3d_swapchain)
491 *swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
492 IDirect3DSwapChain9_AddRef(*swapchain);
493 wined3d_swapchain_decref(wined3d_swapchain);
495 else
497 *swapchain = NULL;
499 wined3d_mutex_unlock();
501 return hr;
504 static UINT WINAPI IDirect3DDevice9Impl_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
506 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
507 UINT count;
509 TRACE("iface %p.\n", iface);
511 wined3d_mutex_lock();
512 count = wined3d_device_get_swapchain_count(This->wined3d_device);
513 wined3d_mutex_unlock();
515 return count;
518 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
520 struct wined3d_resource_desc desc;
522 wined3d_resource_get_desc(resource, &desc);
523 if (desc.pool == WINED3DPOOL_DEFAULT)
525 IDirect3DSurface9 *surface;
527 if (desc.resource_type != WINED3DRTYPE_SURFACE)
529 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
530 return D3DERR_INVALIDCALL;
533 surface = wined3d_resource_get_parent(resource);
535 IDirect3DSurface9_AddRef(surface);
536 if (IDirect3DSurface9_Release(surface))
538 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
539 return D3DERR_INVALIDCALL;
542 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
545 return D3D_OK;
548 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Reset(IDirect3DDevice9Ex *iface,
549 D3DPRESENT_PARAMETERS *pPresentationParameters)
551 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
552 struct wined3d_swapchain_desc swapchain_desc;
553 HRESULT hr;
555 TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
557 /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
558 * such objects can still be used for rendering after their external d3d9 object has been destroyed.
559 * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
560 * make use of the hidden reference and destroys the objects.
562 * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
563 * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
565 wined3d_mutex_lock();
567 swapchain_desc.backbuffer_width = pPresentationParameters->BackBufferWidth;
568 swapchain_desc.backbuffer_height = pPresentationParameters->BackBufferHeight;
569 swapchain_desc.backbuffer_format = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
570 swapchain_desc.backbuffer_count = pPresentationParameters->BackBufferCount;
571 swapchain_desc.multisample_type = pPresentationParameters->MultiSampleType;
572 swapchain_desc.multisample_quality = pPresentationParameters->MultiSampleQuality;
573 swapchain_desc.swap_effect = pPresentationParameters->SwapEffect;
574 swapchain_desc.device_window = pPresentationParameters->hDeviceWindow;
575 swapchain_desc.windowed = pPresentationParameters->Windowed;
576 swapchain_desc.enable_auto_depth_stencil = pPresentationParameters->EnableAutoDepthStencil;
577 swapchain_desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
578 swapchain_desc.flags = pPresentationParameters->Flags;
579 swapchain_desc.refresh_rate = pPresentationParameters->FullScreen_RefreshRateInHz;
580 swapchain_desc.swap_interval = pPresentationParameters->PresentationInterval;
581 swapchain_desc.auto_restore_display_mode = TRUE;
583 hr = wined3d_device_reset(This->wined3d_device, &swapchain_desc, reset_enum_callback);
584 if (FAILED(hr))
585 This->notreset = TRUE;
586 else
587 This->notreset = FALSE;
589 wined3d_mutex_unlock();
591 return hr;
594 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Present(IDirect3DDevice9Ex *iface,
595 const RECT *pSourceRect, const RECT *pDestRect, HWND hDestWindowOverride,
596 const RGNDATA *pDirtyRegion)
598 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
599 HRESULT hr;
601 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
602 iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
604 wined3d_mutex_lock();
605 hr = wined3d_device_present(This->wined3d_device, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
606 wined3d_mutex_unlock();
608 return hr;
611 static HRESULT WINAPI IDirect3DDevice9Impl_GetBackBuffer(IDirect3DDevice9Ex *iface,
612 UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 **ppBackBuffer)
614 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
615 struct wined3d_surface *wined3d_surface = NULL;
616 HRESULT hr;
618 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
619 iface, iSwapChain, BackBuffer, Type, ppBackBuffer);
621 wined3d_mutex_lock();
622 hr = wined3d_device_get_back_buffer(This->wined3d_device, iSwapChain,
623 BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &wined3d_surface);
624 if (SUCCEEDED(hr) && wined3d_surface && ppBackBuffer)
626 *ppBackBuffer = wined3d_surface_get_parent(wined3d_surface);
627 IDirect3DSurface9_AddRef(*ppBackBuffer);
628 wined3d_surface_decref(wined3d_surface);
630 wined3d_mutex_unlock();
632 return hr;
634 static HRESULT WINAPI IDirect3DDevice9Impl_GetRasterStatus(IDirect3DDevice9Ex *iface,
635 UINT iSwapChain, D3DRASTER_STATUS *pRasterStatus)
637 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
638 HRESULT hr;
640 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, iSwapChain, pRasterStatus);
642 wined3d_mutex_lock();
643 hr = wined3d_device_get_raster_status(This->wined3d_device, iSwapChain, (WINED3DRASTER_STATUS *)pRasterStatus);
644 wined3d_mutex_unlock();
646 return hr;
649 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(IDirect3DDevice9Ex *iface,
650 BOOL bEnableDialogs)
652 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
653 HRESULT hr;
655 TRACE("iface %p, enable %#x.\n", iface, bEnableDialogs);
657 wined3d_mutex_lock();
658 hr = wined3d_device_set_dialog_box_mode(This->wined3d_device, bEnableDialogs);
659 wined3d_mutex_unlock();
661 return hr;
664 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
665 DWORD Flags, const D3DGAMMARAMP *pRamp)
667 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
669 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, iSwapChain, Flags, pRamp);
671 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
672 wined3d_mutex_lock();
673 wined3d_device_set_gamma_ramp(This->wined3d_device, iSwapChain, Flags, (const struct wined3d_gamma_ramp *)pRamp);
674 wined3d_mutex_unlock();
677 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
678 D3DGAMMARAMP *pRamp)
680 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
682 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, iSwapChain, pRamp);
684 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
685 wined3d_mutex_lock();
686 wined3d_device_get_gamma_ramp(This->wined3d_device, iSwapChain, (struct wined3d_gamma_ramp *)pRamp);
687 wined3d_mutex_unlock();
690 static HRESULT WINAPI IDirect3DDevice9Impl_CreateTexture(IDirect3DDevice9Ex *iface,
691 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
692 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
694 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
695 IDirect3DTexture9Impl *object;
696 BOOL set_mem = FALSE;
697 HRESULT hr;
699 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
700 iface, width, height, levels, usage, format, pool, texture, shared_handle);
702 if (shared_handle)
704 if (pool == D3DPOOL_SYSTEMMEM)
706 if (levels != 1)
707 return D3DERR_INVALIDCALL;
708 set_mem = TRUE;
710 else
711 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
714 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
715 if (!object)
717 ERR("Failed to allocate texture memory.\n");
718 return D3DERR_OUTOFVIDEOMEMORY;
721 hr = texture_init(object, This, width, height, levels, usage, format, pool);
722 if (FAILED(hr))
724 WARN("Failed to initialize texture, hr %#x.\n", hr);
725 HeapFree(GetProcessHeap(), 0, object);
726 return hr;
729 if (set_mem)
731 struct wined3d_resource *resource;
732 IDirect3DSurface9Impl *surface;
734 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
735 surface = wined3d_resource_get_parent(resource);
736 wined3d_surface_set_mem(surface->wined3d_surface, *shared_handle);
739 TRACE("Created texture %p.\n", object);
740 *texture = &object->IDirect3DTexture9_iface;
742 return D3D_OK;
745 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
746 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
747 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
749 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
750 IDirect3DVolumeTexture9Impl *object;
751 HRESULT hr;
753 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
754 iface, width, height, depth, levels);
755 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
756 usage, format, pool, texture, shared_handle);
758 if (shared_handle)
759 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
761 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
762 if (!object)
764 ERR("Failed to allocate volume texture memory.\n");
765 return D3DERR_OUTOFVIDEOMEMORY;
768 hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
769 if (FAILED(hr))
771 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
772 HeapFree(GetProcessHeap(), 0, object);
773 return hr;
776 TRACE("Created volume texture %p.\n", object);
777 *texture = &object->IDirect3DVolumeTexture9_iface;
779 return D3D_OK;
782 static HRESULT WINAPI IDirect3DDevice9Impl_CreateCubeTexture(IDirect3DDevice9Ex *iface,
783 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
784 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
786 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
787 IDirect3DCubeTexture9Impl *object;
788 HRESULT hr;
790 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
791 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
793 if (shared_handle)
794 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
796 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
797 if (!object)
799 ERR("Failed to allocate cube texture memory.\n");
800 return D3DERR_OUTOFVIDEOMEMORY;
803 hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
804 if (FAILED(hr))
806 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
807 HeapFree(GetProcessHeap(), 0, object);
808 return hr;
811 TRACE("Created cube texture %p.\n", object);
812 *texture = &object->IDirect3DCubeTexture9_iface;
814 return D3D_OK;
817 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
818 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
819 HANDLE *shared_handle)
821 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
822 IDirect3DVertexBuffer9Impl *object;
823 HRESULT hr;
825 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
826 iface, size, usage, fvf, pool, buffer, shared_handle);
828 if (shared_handle)
829 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
831 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
832 if (!object)
834 ERR("Failed to allocate buffer memory.\n");
835 return D3DERR_OUTOFVIDEOMEMORY;
838 hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
839 if (FAILED(hr))
841 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
842 HeapFree(GetProcessHeap(), 0, object);
843 return hr;
846 TRACE("Created vertex buffer %p.\n", object);
847 *buffer = &object->IDirect3DVertexBuffer9_iface;
849 return D3D_OK;
852 static HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
853 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
854 HANDLE *shared_handle)
856 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
857 IDirect3DIndexBuffer9Impl *object;
858 HRESULT hr;
860 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
861 iface, size, usage, format, pool, buffer, shared_handle);
863 if (shared_handle)
864 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
866 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
867 if (!object)
869 ERR("Failed to allocate buffer memory.\n");
870 return D3DERR_OUTOFVIDEOMEMORY;
873 hr = indexbuffer_init(object, This, size, usage, format, pool);
874 if (FAILED(hr))
876 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
877 HeapFree(GetProcessHeap(), 0, object);
878 return hr;
881 TRACE("Created index buffer %p.\n", object);
882 *buffer = &object->IDirect3DIndexBuffer9_iface;
884 return D3D_OK;
887 static HRESULT IDirect3DDevice9Impl_CreateSurface(IDirect3DDevice9Impl *device, UINT Width,
888 UINT Height, D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level,
889 IDirect3DSurface9 **ppSurface, UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample,
890 DWORD MultisampleQuality)
892 IDirect3DSurface9Impl *object;
893 HRESULT hr;
895 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p.\n"
896 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
897 device, Width, Height, Format, Lockable, Discard, Level, ppSurface, Usage, Pool,
898 MultiSample, MultisampleQuality);
900 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
901 if (!object)
903 FIXME("Failed to allocate surface memory.\n");
904 return D3DERR_OUTOFVIDEOMEMORY;
907 hr = surface_init(object, device, Width, Height, Format, Lockable, Discard, Level, Usage, Pool,
908 MultiSample, MultisampleQuality);
909 if (FAILED(hr))
911 WARN("Failed to initialize surface, hr %#x.\n", hr);
912 HeapFree(GetProcessHeap(), 0, object);
913 return hr;
916 TRACE("Created surface %p.\n", object);
917 *ppSurface = &object->IDirect3DSurface9_iface;
919 return D3D_OK;
922 static HRESULT WINAPI IDirect3DDevice9Impl_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT Width,
923 UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality,
924 BOOL Lockable, IDirect3DSurface9 **ppSurface, HANDLE *shared_handle)
926 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
927 HRESULT hr;
929 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
930 "lockable %#x, surface %p, shared_handle %p.\n",
931 iface, Width, Height, Format, MultiSample, MultisampleQuality,
932 Lockable, ppSurface, shared_handle);
934 if (shared_handle)
935 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
937 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, Lockable,
938 FALSE /* Discard */, 0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,
939 MultiSample, MultisampleQuality);
941 return hr;
944 static HRESULT WINAPI IDirect3DDevice9Impl_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface,
945 UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
946 DWORD MultisampleQuality, BOOL Discard, IDirect3DSurface9 **ppSurface,
947 HANDLE *shared_handle)
949 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
950 HRESULT hr;
952 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
953 "discard %#x, surface %p, shared_handle %p.\n",
954 iface, Width, Height, Format, MultiSample, MultisampleQuality,
955 Discard, ppSurface, shared_handle);
957 if (shared_handle)
958 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
960 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
961 Discard, 0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample,
962 MultisampleQuality);
964 return hr;
968 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateSurface(IDirect3DDevice9Ex *iface,
969 IDirect3DSurface9 *pSourceSurface, const RECT *pSourceRect,
970 IDirect3DSurface9 *pDestinationSurface, const POINT *pDestPoint)
972 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
973 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(pSourceSurface);
974 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(pDestinationSurface);
975 HRESULT hr;
977 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
978 iface, pSourceSurface, pSourceRect, pDestinationSurface, pDestPoint);
980 wined3d_mutex_lock();
981 hr = wined3d_device_update_surface(This->wined3d_device, src->wined3d_surface, pSourceRect,
982 dst->wined3d_surface, pDestPoint);
983 wined3d_mutex_unlock();
985 return hr;
988 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateTexture(IDirect3DDevice9Ex *iface,
989 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
991 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
992 HRESULT hr;
994 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
996 wined3d_mutex_lock();
997 hr = wined3d_device_update_texture(This->wined3d_device,
998 ((IDirect3DBaseTexture9Impl *)src_texture)->wined3d_texture,
999 ((IDirect3DBaseTexture9Impl *)dst_texture)->wined3d_texture);
1000 wined3d_mutex_unlock();
1002 return hr;
1005 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1006 IDirect3DSurface9 *pRenderTarget, IDirect3DSurface9 *pDestSurface)
1008 IDirect3DSurface9Impl *renderTarget = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
1009 IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1010 HRESULT hr;
1012 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, pRenderTarget, pDestSurface);
1014 wined3d_mutex_lock();
1015 hr = wined3d_surface_get_render_target_data(destSurface->wined3d_surface, renderTarget->wined3d_surface);
1016 wined3d_mutex_unlock();
1018 return hr;
1021 static HRESULT WINAPI IDirect3DDevice9Impl_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1022 UINT iSwapChain, IDirect3DSurface9 *pDestSurface)
1024 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1025 IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1026 HRESULT hr;
1028 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, iSwapChain, pDestSurface);
1030 wined3d_mutex_lock();
1031 hr = wined3d_device_get_front_buffer_data(This->wined3d_device, iSwapChain, destSurface->wined3d_surface);
1032 wined3d_mutex_unlock();
1034 return hr;
1037 static HRESULT WINAPI IDirect3DDevice9Impl_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *pSourceSurface,
1038 const RECT *pSourceRect, IDirect3DSurface9 *pDestSurface, const RECT *pDestRect, D3DTEXTUREFILTERTYPE Filter)
1040 IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(pSourceSurface);
1041 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1042 HRESULT hr;
1044 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1045 iface, pSourceSurface, pSourceRect, pDestSurface, pDestRect, Filter);
1047 wined3d_mutex_lock();
1048 hr = wined3d_surface_blt(dst->wined3d_surface, pDestRect, src->wined3d_surface, pSourceRect, 0, NULL, Filter);
1049 if (hr == WINEDDERR_INVALIDRECT)
1050 hr = D3DERR_INVALIDCALL;
1051 wined3d_mutex_unlock();
1053 return hr;
1056 static HRESULT WINAPI IDirect3DDevice9Impl_ColorFill(IDirect3DDevice9Ex *iface,
1057 IDirect3DSurface9 *pSurface, const RECT *pRect, D3DCOLOR color)
1059 const struct wined3d_color c =
1061 ((color >> 16) & 0xff) / 255.0f,
1062 ((color >> 8) & 0xff) / 255.0f,
1063 (color & 0xff) / 255.0f,
1064 ((color >> 24) & 0xff) / 255.0f,
1066 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1067 IDirect3DSurface9Impl *surface = unsafe_impl_from_IDirect3DSurface9(pSurface);
1068 struct wined3d_resource *wined3d_resource;
1069 struct wined3d_resource_desc desc;
1070 HRESULT hr;
1072 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, pSurface, pRect, color);
1074 wined3d_mutex_lock();
1076 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
1077 wined3d_resource_get_desc(wined3d_resource, &desc);
1079 /* This method is only allowed with surfaces that are render targets, or
1080 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1081 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3DPOOL_DEFAULT)
1083 wined3d_mutex_unlock();
1084 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1085 return D3DERR_INVALIDCALL;
1088 /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1089 hr = wined3d_device_color_fill(This->wined3d_device, surface->wined3d_surface, pRect, &c);
1091 wined3d_mutex_unlock();
1093 return hr;
1096 static HRESULT WINAPI IDirect3DDevice9Impl_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1097 UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface,
1098 HANDLE *shared_handle)
1100 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1101 HRESULT hr;
1103 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1104 iface, Width, Height, Format, Pool, ppSurface, shared_handle);
1106 if (shared_handle)
1107 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1109 if (Pool == D3DPOOL_MANAGED)
1111 FIXME("Attempting to create a managed offscreen plain surface\n");
1112 return D3DERR_INVALIDCALL;
1115 'Off-screen plain surfaces are always lockable, regardless of their pool types.'
1116 but then...
1117 D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
1118 Why, their always lockable?
1119 should I change the usage to dynamic?
1121 hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
1122 FALSE /* Discard */, 0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */,
1123 Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1125 return hr;
1128 /* TODO: move to wineD3D */
1129 static HRESULT WINAPI IDirect3DDevice9Impl_SetRenderTarget(IDirect3DDevice9Ex *iface,
1130 DWORD RenderTargetIndex, IDirect3DSurface9 *pRenderTarget)
1132 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1133 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
1134 HRESULT hr;
1136 TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, pRenderTarget);
1138 if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1140 WARN("Invalid index %u specified.\n", RenderTargetIndex);
1141 return D3DERR_INVALIDCALL;
1144 wined3d_mutex_lock();
1145 hr = wined3d_device_set_render_target(This->wined3d_device, RenderTargetIndex,
1146 pSurface ? pSurface->wined3d_surface : NULL, TRUE);
1147 wined3d_mutex_unlock();
1149 return hr;
1152 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTarget(IDirect3DDevice9Ex *iface,
1153 DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget)
1155 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1156 struct wined3d_surface *wined3d_surface;
1157 HRESULT hr;
1159 TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, ppRenderTarget);
1161 if (ppRenderTarget == NULL) {
1162 return D3DERR_INVALIDCALL;
1165 if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1167 WARN("Invalid index %u specified.\n", RenderTargetIndex);
1168 return D3DERR_INVALIDCALL;
1171 wined3d_mutex_lock();
1172 hr = wined3d_device_get_render_target(This->wined3d_device, RenderTargetIndex, &wined3d_surface);
1173 if (SUCCEEDED(hr))
1175 *ppRenderTarget = wined3d_surface_get_parent(wined3d_surface);
1176 IDirect3DSurface9_AddRef(*ppRenderTarget);
1177 wined3d_surface_decref(wined3d_surface);
1179 else
1181 if (hr != WINED3DERR_NOTFOUND)
1182 WARN("Failed to get render target %u, hr %#x.\n", RenderTargetIndex, hr);
1183 *ppRenderTarget = NULL;
1185 wined3d_mutex_unlock();
1187 return hr;
1190 static HRESULT WINAPI IDirect3DDevice9Impl_SetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1191 IDirect3DSurface9 *pZStencilSurface)
1193 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1194 IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pZStencilSurface);
1195 HRESULT hr;
1197 TRACE("iface %p, depth_stencil %p.\n", iface, pZStencilSurface);
1199 wined3d_mutex_lock();
1200 hr = wined3d_device_set_depth_stencil(This->wined3d_device, pSurface ? pSurface->wined3d_surface : NULL);
1201 wined3d_mutex_unlock();
1203 return hr;
1206 static HRESULT WINAPI IDirect3DDevice9Impl_GetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1207 IDirect3DSurface9 **ppZStencilSurface)
1209 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1210 struct wined3d_surface *wined3d_surface;
1211 HRESULT hr;
1213 TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1215 if(ppZStencilSurface == NULL){
1216 return D3DERR_INVALIDCALL;
1219 wined3d_mutex_lock();
1220 hr = wined3d_device_get_depth_stencil(This->wined3d_device, &wined3d_surface);
1221 if (SUCCEEDED(hr))
1223 *ppZStencilSurface = wined3d_surface_get_parent(wined3d_surface);
1224 IDirect3DSurface9_AddRef(*ppZStencilSurface);
1225 wined3d_surface_decref(wined3d_surface);
1227 else
1229 if (hr != WINED3DERR_NOTFOUND)
1230 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1231 *ppZStencilSurface = NULL;
1233 wined3d_mutex_unlock();
1235 return hr;
1238 static HRESULT WINAPI IDirect3DDevice9Impl_BeginScene(IDirect3DDevice9Ex *iface)
1240 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1241 HRESULT hr;
1243 TRACE("iface %p.\n", iface);
1245 wined3d_mutex_lock();
1246 hr = wined3d_device_begin_scene(This->wined3d_device);
1247 wined3d_mutex_unlock();
1249 return hr;
1252 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_EndScene(IDirect3DDevice9Ex *iface)
1254 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1255 HRESULT hr;
1257 TRACE("iface %p.\n", iface);
1259 wined3d_mutex_lock();
1260 hr = wined3d_device_end_scene(This->wined3d_device);
1261 wined3d_mutex_unlock();
1263 return hr;
1266 static HRESULT WINAPI IDirect3DDevice9Impl_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1267 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1269 const struct wined3d_color c =
1271 ((color >> 16) & 0xff) / 255.0f,
1272 ((color >> 8) & 0xff) / 255.0f,
1273 (color & 0xff) / 255.0f,
1274 ((color >> 24) & 0xff) / 255.0f,
1276 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1277 HRESULT hr;
1279 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1280 iface, rect_count, rects, flags, color, z, stencil);
1282 wined3d_mutex_lock();
1283 hr = wined3d_device_clear(This->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1284 wined3d_mutex_unlock();
1286 return hr;
1289 static HRESULT WINAPI IDirect3DDevice9Impl_SetTransform(IDirect3DDevice9Ex *iface,
1290 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *lpMatrix)
1292 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1293 HRESULT hr;
1295 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1297 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1298 wined3d_mutex_lock();
1299 hr = wined3d_device_set_transform(This->wined3d_device, State, (const struct wined3d_matrix *)lpMatrix);
1300 wined3d_mutex_unlock();
1302 return hr;
1305 static HRESULT WINAPI IDirect3DDevice9Impl_GetTransform(IDirect3DDevice9Ex *iface,
1306 D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
1308 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1309 HRESULT hr;
1311 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1313 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1314 wined3d_mutex_lock();
1315 hr = wined3d_device_get_transform(This->wined3d_device, State, (struct wined3d_matrix *)pMatrix);
1316 wined3d_mutex_unlock();
1318 return hr;
1321 static HRESULT WINAPI IDirect3DDevice9Impl_MultiplyTransform(IDirect3DDevice9Ex *iface,
1322 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
1324 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1325 HRESULT hr;
1327 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1329 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1330 wined3d_mutex_lock();
1331 hr = wined3d_device_multiply_transform(This->wined3d_device, State, (const struct wined3d_matrix *)pMatrix);
1332 wined3d_mutex_unlock();
1334 return hr;
1337 static HRESULT WINAPI IDirect3DDevice9Impl_SetViewport(IDirect3DDevice9Ex *iface,
1338 const D3DVIEWPORT9 *pViewport)
1340 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1341 HRESULT hr;
1343 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1345 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1346 wined3d_mutex_lock();
1347 hr = wined3d_device_set_viewport(This->wined3d_device, (const struct wined3d_viewport *)pViewport);
1348 wined3d_mutex_unlock();
1350 return hr;
1353 static HRESULT WINAPI IDirect3DDevice9Impl_GetViewport(IDirect3DDevice9Ex *iface,
1354 D3DVIEWPORT9 *pViewport)
1356 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1357 HRESULT hr;
1359 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1361 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1362 wined3d_mutex_lock();
1363 hr = wined3d_device_get_viewport(This->wined3d_device, (struct wined3d_viewport *)pViewport);
1364 wined3d_mutex_unlock();
1366 return hr;
1369 static HRESULT WINAPI IDirect3DDevice9Impl_SetMaterial(IDirect3DDevice9Ex *iface,
1370 const D3DMATERIAL9 *pMaterial)
1372 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1373 HRESULT hr;
1375 TRACE("iface %p, material %p.\n", iface, pMaterial);
1377 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1378 wined3d_mutex_lock();
1379 hr = wined3d_device_set_material(This->wined3d_device, (const struct wined3d_material *)pMaterial);
1380 wined3d_mutex_unlock();
1382 return hr;
1385 static HRESULT WINAPI IDirect3DDevice9Impl_GetMaterial(IDirect3DDevice9Ex *iface,
1386 D3DMATERIAL9 *pMaterial)
1388 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1389 HRESULT hr;
1391 TRACE("iface %p, material %p.\n", iface, pMaterial);
1393 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1394 wined3d_mutex_lock();
1395 hr = wined3d_device_get_material(This->wined3d_device, (struct wined3d_material *)pMaterial);
1396 wined3d_mutex_unlock();
1398 return hr;
1401 static HRESULT WINAPI IDirect3DDevice9Impl_SetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1402 const D3DLIGHT9 *pLight)
1404 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1405 HRESULT hr;
1407 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1409 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1410 wined3d_mutex_lock();
1411 hr = wined3d_device_set_light(This->wined3d_device, Index, (const struct wined3d_light *)pLight);
1412 wined3d_mutex_unlock();
1414 return hr;
1417 static HRESULT WINAPI IDirect3DDevice9Impl_GetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1418 D3DLIGHT9 *pLight)
1420 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1421 HRESULT hr;
1423 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1425 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1426 wined3d_mutex_lock();
1427 hr = wined3d_device_get_light(This->wined3d_device, Index, (struct wined3d_light *)pLight);
1428 wined3d_mutex_unlock();
1430 return hr;
1433 static HRESULT WINAPI IDirect3DDevice9Impl_LightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1434 BOOL Enable)
1436 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1437 HRESULT hr;
1439 TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1441 wined3d_mutex_lock();
1442 hr = wined3d_device_set_light_enable(This->wined3d_device, Index, Enable);
1443 wined3d_mutex_unlock();
1445 return hr;
1448 static HRESULT WINAPI IDirect3DDevice9Impl_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1449 BOOL *pEnable)
1451 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1452 HRESULT hr;
1454 TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1456 wined3d_mutex_lock();
1457 hr = wined3d_device_get_light_enable(This->wined3d_device, Index, pEnable);
1458 wined3d_mutex_unlock();
1460 return hr;
1463 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1464 const float *pPlane)
1466 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1467 HRESULT hr;
1469 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1471 wined3d_mutex_lock();
1472 hr = wined3d_device_set_clip_plane(This->wined3d_device, Index, pPlane);
1473 wined3d_mutex_unlock();
1475 return hr;
1478 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1479 float *pPlane)
1481 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1482 HRESULT hr;
1484 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1486 wined3d_mutex_lock();
1487 hr = wined3d_device_get_clip_plane(This->wined3d_device, Index, pPlane);
1488 wined3d_mutex_unlock();
1490 return hr;
1493 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetRenderState(IDirect3DDevice9Ex *iface,
1494 D3DRENDERSTATETYPE State, DWORD Value)
1496 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1497 HRESULT hr;
1499 TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1501 wined3d_mutex_lock();
1502 hr = wined3d_device_set_render_state(This->wined3d_device, State, Value);
1503 wined3d_mutex_unlock();
1505 return hr;
1508 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderState(IDirect3DDevice9Ex *iface,
1509 D3DRENDERSTATETYPE State, DWORD *pValue)
1511 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1512 HRESULT hr;
1514 TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1516 wined3d_mutex_lock();
1517 hr = wined3d_device_get_render_state(This->wined3d_device, State, pValue);
1518 wined3d_mutex_unlock();
1520 return hr;
1523 static HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(IDirect3DDevice9Ex *iface,
1524 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1526 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1527 IDirect3DStateBlock9Impl *object;
1528 HRESULT hr;
1530 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1532 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1534 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1535 return D3DERR_INVALIDCALL;
1538 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1539 if (!object)
1541 ERR("Failed to allocate stateblock memory.\n");
1542 return E_OUTOFMEMORY;
1545 hr = stateblock_init(object, This, type, NULL);
1546 if (FAILED(hr))
1548 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1549 HeapFree(GetProcessHeap(), 0, object);
1550 return hr;
1553 TRACE("Created stateblock %p.\n", object);
1554 *stateblock = &object->IDirect3DStateBlock9_iface;
1556 return D3D_OK;
1559 static HRESULT WINAPI IDirect3DDevice9Impl_BeginStateBlock(IDirect3DDevice9Ex *iface)
1561 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1562 HRESULT hr;
1564 TRACE("iface %p.\n", iface);
1566 wined3d_mutex_lock();
1567 hr = wined3d_device_begin_stateblock(This->wined3d_device);
1568 wined3d_mutex_unlock();
1570 return hr;
1573 static HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface,
1574 IDirect3DStateBlock9 **stateblock)
1576 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1577 struct wined3d_stateblock *wined3d_stateblock;
1578 IDirect3DStateBlock9Impl *object;
1579 HRESULT hr;
1581 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1583 wined3d_mutex_lock();
1584 hr = wined3d_device_end_stateblock(This->wined3d_device, &wined3d_stateblock);
1585 wined3d_mutex_unlock();
1586 if (FAILED(hr))
1588 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1589 return hr;
1592 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1593 if (!object)
1595 ERR("Failed to allocate stateblock memory.\n");
1596 wined3d_mutex_lock();
1597 wined3d_stateblock_decref(wined3d_stateblock);
1598 wined3d_mutex_unlock();
1599 return E_OUTOFMEMORY;
1602 hr = stateblock_init(object, This, 0, wined3d_stateblock);
1603 if (FAILED(hr))
1605 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1606 wined3d_mutex_lock();
1607 wined3d_stateblock_decref(wined3d_stateblock);
1608 wined3d_mutex_unlock();
1609 HeapFree(GetProcessHeap(), 0, object);
1610 return hr;
1613 TRACE("Created stateblock %p.\n", object);
1614 *stateblock = &object->IDirect3DStateBlock9_iface;
1616 return D3D_OK;
1619 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipStatus(IDirect3DDevice9Ex *iface,
1620 const D3DCLIPSTATUS9 *pClipStatus)
1622 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1623 HRESULT hr;
1625 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1627 wined3d_mutex_lock();
1628 hr = wined3d_device_set_clip_status(This->wined3d_device, (const struct wined3d_clip_status *)pClipStatus);
1629 wined3d_mutex_unlock();
1631 return hr;
1634 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipStatus(IDirect3DDevice9Ex *iface,
1635 D3DCLIPSTATUS9 *pClipStatus)
1637 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1638 HRESULT hr;
1640 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1642 wined3d_mutex_lock();
1643 hr = wined3d_device_get_clip_status(This->wined3d_device, (struct wined3d_clip_status *)pClipStatus);
1644 wined3d_mutex_unlock();
1646 return hr;
1649 static HRESULT WINAPI IDirect3DDevice9Impl_GetTexture(IDirect3DDevice9Ex *iface, DWORD Stage,
1650 IDirect3DBaseTexture9 **ppTexture)
1652 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1653 struct wined3d_texture *wined3d_texture = NULL;
1654 HRESULT hr;
1656 TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1658 if(ppTexture == NULL){
1659 return D3DERR_INVALIDCALL;
1662 wined3d_mutex_lock();
1663 hr = wined3d_device_get_texture(This->wined3d_device, Stage, &wined3d_texture);
1664 if (SUCCEEDED(hr) && wined3d_texture)
1666 *ppTexture = wined3d_texture_get_parent(wined3d_texture);
1667 IDirect3DBaseTexture9_AddRef(*ppTexture);
1668 wined3d_texture_decref(wined3d_texture);
1670 else
1672 if (FAILED(hr))
1674 WARN("Call to get texture (%u) failed (%p).\n", Stage, wined3d_texture);
1676 *ppTexture = NULL;
1678 wined3d_mutex_unlock();
1680 return hr;
1683 static HRESULT WINAPI IDirect3DDevice9Impl_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage,
1684 IDirect3DBaseTexture9 *texture)
1686 IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
1687 HRESULT hr;
1689 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1691 wined3d_mutex_lock();
1692 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1693 texture ? ((IDirect3DBaseTexture9Impl *)texture)->wined3d_texture : NULL);
1694 wined3d_mutex_unlock();
1696 return hr;
1699 static const WINED3DTEXTURESTAGESTATETYPE tss_lookup[] =
1701 WINED3DTSS_FORCE_DWORD, /* 0, unused */
1702 WINED3DTSS_COLOROP, /* 1, D3DTSS_COLOROP */
1703 WINED3DTSS_COLORARG1, /* 2, D3DTSS_COLORARG1 */
1704 WINED3DTSS_COLORARG2, /* 3, D3DTSS_COLORARG2 */
1705 WINED3DTSS_ALPHAOP, /* 4, D3DTSS_ALPHAOP */
1706 WINED3DTSS_ALPHAARG1, /* 5, D3DTSS_ALPHAARG1 */
1707 WINED3DTSS_ALPHAARG2, /* 6, D3DTSS_ALPHAARG2 */
1708 WINED3DTSS_BUMPENVMAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1709 WINED3DTSS_BUMPENVMAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1710 WINED3DTSS_BUMPENVMAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1711 WINED3DTSS_BUMPENVMAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1712 WINED3DTSS_TEXCOORDINDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1713 WINED3DTSS_FORCE_DWORD, /* 12, unused */
1714 WINED3DTSS_FORCE_DWORD, /* 13, unused */
1715 WINED3DTSS_FORCE_DWORD, /* 14, unused */
1716 WINED3DTSS_FORCE_DWORD, /* 15, unused */
1717 WINED3DTSS_FORCE_DWORD, /* 16, unused */
1718 WINED3DTSS_FORCE_DWORD, /* 17, unused */
1719 WINED3DTSS_FORCE_DWORD, /* 18, unused */
1720 WINED3DTSS_FORCE_DWORD, /* 19, unused */
1721 WINED3DTSS_FORCE_DWORD, /* 20, unused */
1722 WINED3DTSS_FORCE_DWORD, /* 21, unused */
1723 WINED3DTSS_BUMPENVLSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1724 WINED3DTSS_BUMPENVLOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1725 WINED3DTSS_TEXTURETRANSFORMFLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1726 WINED3DTSS_FORCE_DWORD, /* 25, unused */
1727 WINED3DTSS_COLORARG0, /* 26, D3DTSS_COLORARG0 */
1728 WINED3DTSS_ALPHAARG0, /* 27, D3DTSS_ALPHAARG0 */
1729 WINED3DTSS_RESULTARG, /* 28, D3DTSS_RESULTARG */
1730 WINED3DTSS_FORCE_DWORD, /* 29, unused */
1731 WINED3DTSS_FORCE_DWORD, /* 30, unused */
1732 WINED3DTSS_FORCE_DWORD, /* 31, unused */
1733 WINED3DTSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1736 static HRESULT WINAPI IDirect3DDevice9Impl_GetTextureStageState(IDirect3DDevice9Ex *iface,
1737 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1739 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1740 HRESULT hr;
1742 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1744 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1746 WARN("Invalid Type %#x passed.\n", Type);
1747 return D3D_OK;
1750 wined3d_mutex_lock();
1751 hr = wined3d_device_get_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], pValue);
1752 wined3d_mutex_unlock();
1754 return hr;
1757 static HRESULT WINAPI IDirect3DDevice9Impl_SetTextureStageState(IDirect3DDevice9Ex *iface,
1758 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1760 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1761 HRESULT hr;
1763 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1765 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1767 WARN("Invalid Type %#x passed.\n", Type);
1768 return D3D_OK;
1771 wined3d_mutex_lock();
1772 hr = wined3d_device_set_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], Value);
1773 wined3d_mutex_unlock();
1775 return hr;
1778 static HRESULT WINAPI IDirect3DDevice9Impl_GetSamplerState(IDirect3DDevice9Ex *iface, DWORD Sampler,
1779 D3DSAMPLERSTATETYPE Type, DWORD *pValue)
1781 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1782 HRESULT hr;
1784 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, Sampler, Type, pValue);
1786 wined3d_mutex_lock();
1787 hr = wined3d_device_get_sampler_state(This->wined3d_device, Sampler, Type, pValue);
1788 wined3d_mutex_unlock();
1790 return hr;
1793 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetSamplerState(IDirect3DDevice9Ex *iface,
1794 DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value)
1796 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1797 HRESULT hr;
1799 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, Sampler, Type, Value);
1801 wined3d_mutex_lock();
1802 hr = wined3d_device_set_sampler_state(This->wined3d_device, Sampler, Type, Value);
1803 wined3d_mutex_unlock();
1805 return hr;
1808 static HRESULT WINAPI IDirect3DDevice9Impl_ValidateDevice(IDirect3DDevice9Ex *iface,
1809 DWORD *pNumPasses)
1811 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1812 HRESULT hr;
1814 TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1816 wined3d_mutex_lock();
1817 hr = wined3d_device_validate_device(This->wined3d_device, pNumPasses);
1818 wined3d_mutex_unlock();
1820 return hr;
1823 static HRESULT WINAPI IDirect3DDevice9Impl_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1824 UINT PaletteNumber, const PALETTEENTRY *pEntries)
1826 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1828 return D3DERR_INVALIDCALL;
1831 static HRESULT WINAPI IDirect3DDevice9Impl_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1832 UINT PaletteNumber, PALETTEENTRY *pEntries)
1834 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1836 return D3DERR_INVALIDCALL;
1839 static HRESULT WINAPI IDirect3DDevice9Impl_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1840 UINT PaletteNumber)
1842 FIXME("iface %p, palette_idx %u unimplemented.\n", iface, PaletteNumber);
1844 return D3DERR_INVALIDCALL;
1847 static HRESULT WINAPI IDirect3DDevice9Impl_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1848 UINT *PaletteNumber)
1850 FIXME("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1852 return D3DERR_INVALIDCALL;
1855 static HRESULT WINAPI IDirect3DDevice9Impl_SetScissorRect(IDirect3DDevice9Ex *iface,
1856 const RECT *pRect)
1858 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1859 HRESULT hr;
1861 TRACE("iface %p, rect %p.\n", iface, pRect);
1863 wined3d_mutex_lock();
1864 hr = wined3d_device_set_scissor_rect(This->wined3d_device, pRect);
1865 wined3d_mutex_unlock();
1867 return hr;
1870 static HRESULT WINAPI IDirect3DDevice9Impl_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *pRect)
1872 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1873 HRESULT hr;
1875 TRACE("iface %p, rect %p.\n", iface, pRect);
1877 wined3d_mutex_lock();
1878 hr = wined3d_device_get_scissor_rect(This->wined3d_device, pRect);
1879 wined3d_mutex_unlock();
1881 return hr;
1884 static HRESULT WINAPI IDirect3DDevice9Impl_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface,
1885 BOOL bSoftware)
1887 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1888 HRESULT hr;
1890 TRACE("iface %p, software %#x.\n", iface, bSoftware);
1892 wined3d_mutex_lock();
1893 hr = wined3d_device_set_software_vertex_processing(This->wined3d_device, bSoftware);
1894 wined3d_mutex_unlock();
1896 return hr;
1899 static BOOL WINAPI IDirect3DDevice9Impl_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
1901 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1902 BOOL ret;
1904 TRACE("iface %p.\n", iface);
1906 wined3d_mutex_lock();
1907 ret = wined3d_device_get_software_vertex_processing(This->wined3d_device);
1908 wined3d_mutex_unlock();
1910 return ret;
1913 static HRESULT WINAPI IDirect3DDevice9Impl_SetNPatchMode(IDirect3DDevice9Ex *iface, float nSegments)
1915 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1916 HRESULT hr;
1918 TRACE("iface %p, segment_count %.8e.\n", iface, nSegments);
1920 wined3d_mutex_lock();
1921 hr = wined3d_device_set_npatch_mode(This->wined3d_device, nSegments);
1922 wined3d_mutex_unlock();
1924 return hr;
1927 static float WINAPI IDirect3DDevice9Impl_GetNPatchMode(IDirect3DDevice9Ex *iface)
1929 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1930 float ret;
1932 TRACE("iface %p.\n", iface);
1934 wined3d_mutex_lock();
1935 ret = wined3d_device_get_npatch_mode(This->wined3d_device);
1936 wined3d_mutex_unlock();
1938 return ret;
1941 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(IDirect3DDevice9Ex *iface,
1942 D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1944 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1945 HRESULT hr;
1947 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1948 iface, PrimitiveType, StartVertex, PrimitiveCount);
1950 wined3d_mutex_lock();
1951 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1952 hr = wined3d_device_draw_primitive(This->wined3d_device, StartVertex,
1953 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1954 wined3d_mutex_unlock();
1956 return hr;
1959 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
1960 D3DPRIMITIVETYPE PrimitiveType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices,
1961 UINT startIndex, UINT primCount)
1963 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1964 HRESULT hr;
1966 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
1967 "vertex_count %u, start_idx %u, primitive_count %u.\n",
1968 iface, PrimitiveType, BaseVertexIndex, MinVertexIndex,
1969 NumVertices, startIndex, primCount);
1971 wined3d_mutex_lock();
1972 wined3d_device_set_base_vertex_index(This->wined3d_device, BaseVertexIndex);
1973 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1974 hr = wined3d_device_draw_indexed_primitive(This->wined3d_device, startIndex,
1975 vertex_count_from_primitive_count(PrimitiveType, primCount));
1976 wined3d_mutex_unlock();
1978 return hr;
1981 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
1982 D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
1983 UINT VertexStreamZeroStride)
1985 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1986 HRESULT hr;
1988 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1989 iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1991 wined3d_mutex_lock();
1992 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1993 hr = wined3d_device_draw_primitive_up(This->wined3d_device,
1994 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1995 pVertexStreamZeroData, VertexStreamZeroStride);
1996 wined3d_mutex_unlock();
1998 return hr;
2001 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2002 D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
2003 UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
2004 const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
2006 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2007 HRESULT hr;
2009 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
2010 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2011 iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
2012 pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
2014 wined3d_mutex_lock();
2015 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2016 hr = wined3d_device_draw_indexed_primitive_up(This->wined3d_device,
2017 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
2018 wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
2019 wined3d_mutex_unlock();
2021 return hr;
2024 static HRESULT WINAPI IDirect3DDevice9Impl_ProcessVertices(IDirect3DDevice9Ex *iface,
2025 UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9 *pDestBuffer,
2026 IDirect3DVertexDeclaration9 *pVertexDecl, DWORD Flags)
2028 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2029 IDirect3DVertexBuffer9Impl *dest = unsafe_impl_from_IDirect3DVertexBuffer9(pDestBuffer);
2030 IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
2031 HRESULT hr;
2033 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2034 iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags);
2036 wined3d_mutex_lock();
2037 hr = wined3d_device_process_vertices(This->wined3d_device, SrcStartIndex, DestIndex, VertexCount,
2038 dest->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags, dest->fvf);
2039 wined3d_mutex_unlock();
2041 return hr;
2044 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2045 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2047 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2048 IDirect3DVertexDeclaration9Impl *object;
2049 HRESULT hr;
2051 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2053 if (!declaration)
2055 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2056 return D3DERR_INVALIDCALL;
2059 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2060 if (!object)
2062 ERR("Failed to allocate vertex declaration memory.\n");
2063 return E_OUTOFMEMORY;
2066 hr = vertexdeclaration_init(object, This, elements);
2067 if (FAILED(hr))
2069 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2070 HeapFree(GetProcessHeap(), 0, object);
2071 return hr;
2074 TRACE("Created vertex declaration %p.\n", object);
2075 *declaration = (IDirect3DVertexDeclaration9 *)object;
2077 return D3D_OK;
2080 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2081 IDirect3DVertexDeclaration9 *declaration)
2083 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2084 HRESULT hr;
2086 TRACE("iface %p, declaration %p.\n", iface, declaration);
2088 wined3d_mutex_lock();
2089 hr = wined3d_device_set_vertex_declaration(This->wined3d_device,
2090 declaration ? ((IDirect3DVertexDeclaration9Impl *)declaration)->wineD3DVertexDeclaration : NULL);
2091 wined3d_mutex_unlock();
2093 return hr;
2096 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2097 IDirect3DVertexDeclaration9 **declaration)
2099 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2100 struct wined3d_vertex_declaration *wined3d_declaration = NULL;
2101 HRESULT hr;
2103 TRACE("iface %p, declaration %p.\n", iface, declaration);
2105 if (!declaration) return D3DERR_INVALIDCALL;
2107 wined3d_mutex_lock();
2108 hr = wined3d_device_get_vertex_declaration(This->wined3d_device, &wined3d_declaration);
2109 if (SUCCEEDED(hr) && wined3d_declaration)
2111 *declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2112 IDirect3DVertexDeclaration9_AddRef(*declaration);
2113 wined3d_vertex_declaration_decref(wined3d_declaration);
2115 else
2117 *declaration = NULL;
2119 wined3d_mutex_unlock();
2121 TRACE("Returning %p.\n", *declaration);
2122 return hr;
2125 static IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
2126 HRESULT hr;
2127 D3DVERTEXELEMENT9* elements = NULL;
2128 IDirect3DVertexDeclaration9* pDecl = NULL;
2129 int p, low, high; /* deliberately signed */
2130 IDirect3DVertexDeclaration9 **convertedDecls = This->convertedDecls;
2132 TRACE("Searching for declaration for fvf %08x... ", fvf);
2134 low = 0;
2135 high = This->numConvertedDecls - 1;
2136 while(low <= high) {
2137 p = (low + high) >> 1;
2138 TRACE("%d ", p);
2139 if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
2140 TRACE("found %p\n", convertedDecls[p]);
2141 return convertedDecls[p];
2142 } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
2143 low = p + 1;
2144 } else {
2145 high = p - 1;
2148 TRACE("not found. Creating and inserting at position %d.\n", low);
2150 hr = vdecl_convert_fvf(fvf, &elements);
2151 if (hr != S_OK) return NULL;
2153 hr = IDirect3DDevice9Impl_CreateVertexDeclaration(&This->IDirect3DDevice9Ex_iface, elements,
2154 &pDecl);
2155 HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
2156 if (hr != S_OK) return NULL;
2158 if(This->declArraySize == This->numConvertedDecls) {
2159 int grow = max(This->declArraySize / 2, 8);
2160 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2161 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2162 if(!convertedDecls) {
2163 /* This will destroy it */
2164 IDirect3DVertexDeclaration9_Release(pDecl);
2165 return NULL;
2167 This->convertedDecls = convertedDecls;
2168 This->declArraySize += grow;
2171 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
2172 convertedDecls[low] = pDecl;
2173 This->numConvertedDecls++;
2175 /* Will prevent the decl from being destroyed */
2176 ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
2177 IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
2179 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
2180 return pDecl;
2183 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(IDirect3DDevice9Ex *iface, DWORD FVF)
2185 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2186 IDirect3DVertexDeclaration9 *decl;
2187 HRESULT hr;
2189 TRACE("iface %p, fvf %#x.\n", iface, FVF);
2191 if (!FVF)
2193 WARN("%#x is not a valid FVF\n", FVF);
2194 return D3D_OK;
2197 wined3d_mutex_lock();
2198 decl = getConvertedDecl(This, FVF);
2199 wined3d_mutex_unlock();
2201 if (!decl)
2203 /* Any situation when this should happen, except out of memory? */
2204 ERR("Failed to create a converted vertex declaration\n");
2205 return D3DERR_DRIVERINTERNALERROR;
2208 hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, decl);
2209 if (FAILED(hr)) ERR("Failed to set vertex declaration\n");
2211 return hr;
2214 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(IDirect3DDevice9Ex *iface, DWORD *pFVF)
2216 IDirect3DVertexDeclaration9 *decl;
2217 HRESULT hr;
2219 TRACE("iface %p, fvf %p.\n", iface, pFVF);
2221 hr = IDirect3DDevice9_GetVertexDeclaration(iface, &decl);
2222 if (FAILED(hr))
2224 WARN("Failed to get vertex declaration, %#x\n", hr);
2225 *pFVF = 0;
2226 return hr;
2229 if (decl)
2231 *pFVF = ((IDirect3DVertexDeclaration9Impl *)decl)->convFVF;
2232 IDirect3DVertexDeclaration9_Release(decl);
2234 else
2236 *pFVF = 0;
2239 TRACE("Returning FVF %#x\n", *pFVF);
2241 return hr;
2244 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(IDirect3DDevice9Ex *iface,
2245 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2247 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2248 IDirect3DVertexShader9Impl *object;
2249 HRESULT hr;
2251 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2253 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2254 if (!object)
2256 ERR("Failed to allocate vertex shader memory.\n");
2257 return E_OUTOFMEMORY;
2260 hr = vertexshader_init(object, This, byte_code);
2261 if (FAILED(hr))
2263 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2264 HeapFree(GetProcessHeap(), 0, object);
2265 return hr;
2268 TRACE("Created vertex shader %p.\n", object);
2269 *shader = &object->IDirect3DVertexShader9_iface;
2271 return D3D_OK;
2274 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShader(IDirect3DDevice9Ex *iface,
2275 IDirect3DVertexShader9 *shader)
2277 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2278 IDirect3DVertexShader9Impl *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2279 HRESULT hr;
2281 TRACE("iface %p, shader %p.\n", iface, shader);
2283 wined3d_mutex_lock();
2284 hr = wined3d_device_set_vertex_shader(This->wined3d_device,
2285 shader_obj ? shader_obj->wined3d_shader : NULL);
2286 wined3d_mutex_unlock();
2288 return hr;
2291 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShader(IDirect3DDevice9Ex *iface,
2292 IDirect3DVertexShader9 **shader)
2294 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2295 struct wined3d_shader *wined3d_shader;
2297 TRACE("iface %p, shader %p.\n", iface, shader);
2299 wined3d_mutex_lock();
2300 wined3d_shader = wined3d_device_get_vertex_shader(This->wined3d_device);
2301 if (wined3d_shader)
2303 *shader = wined3d_shader_get_parent(wined3d_shader);
2304 IDirect3DVertexShader9_AddRef(*shader);
2305 wined3d_shader_decref(wined3d_shader);
2307 else
2309 *shader = NULL;
2311 wined3d_mutex_unlock();
2313 TRACE("Returning %p.\n", *shader);
2315 return D3D_OK;
2318 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2319 UINT reg_idx, const float *data, UINT count)
2321 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2322 HRESULT hr;
2324 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2326 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2328 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2329 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2330 return D3DERR_INVALIDCALL;
2333 wined3d_mutex_lock();
2334 hr = wined3d_device_set_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2335 wined3d_mutex_unlock();
2337 return hr;
2340 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2341 UINT reg_idx, float *data, UINT count)
2343 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2344 HRESULT hr;
2346 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2348 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2350 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2351 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2352 return D3DERR_INVALIDCALL;
2355 wined3d_mutex_lock();
2356 hr = wined3d_device_get_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2357 wined3d_mutex_unlock();
2359 return hr;
2362 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2363 UINT reg_idx, const int *data, UINT count)
2365 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2366 HRESULT hr;
2368 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2370 wined3d_mutex_lock();
2371 hr = wined3d_device_set_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2372 wined3d_mutex_unlock();
2374 return hr;
2377 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2378 UINT reg_idx, int *data, UINT count)
2380 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2381 HRESULT hr;
2383 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2385 wined3d_mutex_lock();
2386 hr = wined3d_device_get_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2387 wined3d_mutex_unlock();
2389 return hr;
2392 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2393 UINT reg_idx, const BOOL *data, UINT count)
2395 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2396 HRESULT hr;
2398 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2400 wined3d_mutex_lock();
2401 hr = wined3d_device_set_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2402 wined3d_mutex_unlock();
2404 return hr;
2407 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2408 UINT reg_idx, BOOL *data, UINT count)
2410 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2411 HRESULT hr;
2413 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2415 wined3d_mutex_lock();
2416 hr = wined3d_device_get_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2417 wined3d_mutex_unlock();
2419 return hr;
2422 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(IDirect3DDevice9Ex *iface,
2423 UINT StreamNumber, IDirect3DVertexBuffer9 *pStreamData, UINT OffsetInBytes, UINT Stride)
2425 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2426 IDirect3DVertexBuffer9Impl *streamdata = unsafe_impl_from_IDirect3DVertexBuffer9(pStreamData);
2427 HRESULT hr;
2429 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2430 iface, StreamNumber, pStreamData, OffsetInBytes, Stride);
2432 wined3d_mutex_lock();
2433 hr = wined3d_device_set_stream_source(This->wined3d_device, StreamNumber,
2434 streamdata ? streamdata->wineD3DVertexBuffer : NULL,
2435 OffsetInBytes, Stride);
2436 wined3d_mutex_unlock();
2438 return hr;
2441 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(IDirect3DDevice9Ex *iface,
2442 UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT *OffsetInBytes, UINT *pStride)
2444 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2445 struct wined3d_buffer *retStream = NULL;
2446 HRESULT hr;
2448 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2449 iface, StreamNumber, pStream, OffsetInBytes, pStride);
2451 if(pStream == NULL){
2452 return D3DERR_INVALIDCALL;
2455 wined3d_mutex_lock();
2456 hr = wined3d_device_get_stream_source(This->wined3d_device, StreamNumber, &retStream, OffsetInBytes, pStride);
2457 if (SUCCEEDED(hr) && retStream)
2459 *pStream = wined3d_buffer_get_parent(retStream);
2460 IDirect3DVertexBuffer9_AddRef(*pStream);
2461 wined3d_buffer_decref(retStream);
2463 else
2465 if (FAILED(hr))
2467 FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
2469 *pStream = NULL;
2471 wined3d_mutex_unlock();
2473 return hr;
2476 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2477 UINT StreamNumber, UINT Divider)
2479 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2480 HRESULT hr;
2482 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, StreamNumber, Divider);
2484 wined3d_mutex_lock();
2485 hr = wined3d_device_set_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2486 wined3d_mutex_unlock();
2488 return hr;
2491 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2492 UINT StreamNumber, UINT *Divider)
2494 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2495 HRESULT hr;
2497 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, StreamNumber, Divider);
2499 wined3d_mutex_lock();
2500 hr = wined3d_device_get_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2501 wined3d_mutex_unlock();
2503 return hr;
2506 static HRESULT WINAPI IDirect3DDevice9Impl_SetIndices(IDirect3DDevice9Ex *iface,
2507 IDirect3DIndexBuffer9 *pIndexData)
2509 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2510 IDirect3DIndexBuffer9Impl *ib = unsafe_impl_from_IDirect3DIndexBuffer9(pIndexData);
2511 HRESULT hr;
2513 TRACE("iface %p, buffer %p.\n", iface, pIndexData);
2515 wined3d_mutex_lock();
2516 hr = wined3d_device_set_index_buffer(This->wined3d_device,
2517 ib ? ib->wineD3DIndexBuffer : NULL,
2518 ib ? ib->format : WINED3DFMT_UNKNOWN);
2519 wined3d_mutex_unlock();
2521 return hr;
2524 static HRESULT WINAPI IDirect3DDevice9Impl_GetIndices(IDirect3DDevice9Ex *iface,
2525 IDirect3DIndexBuffer9 **ppIndexData)
2527 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2528 struct wined3d_buffer *retIndexData = NULL;
2529 HRESULT hr;
2531 TRACE("iface %p, buffer %p.\n", iface, ppIndexData);
2533 if(ppIndexData == NULL){
2534 return D3DERR_INVALIDCALL;
2537 wined3d_mutex_lock();
2538 hr = wined3d_device_get_index_buffer(This->wined3d_device, &retIndexData);
2539 if (SUCCEEDED(hr) && retIndexData)
2541 *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2542 IDirect3DIndexBuffer9_AddRef(*ppIndexData);
2543 wined3d_buffer_decref(retIndexData);
2545 else
2547 if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2548 *ppIndexData = NULL;
2550 wined3d_mutex_unlock();
2552 return hr;
2555 static HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(IDirect3DDevice9Ex *iface,
2556 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2558 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2559 IDirect3DPixelShader9Impl *object;
2560 HRESULT hr;
2562 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2564 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2565 if (!object)
2567 FIXME("Failed to allocate pixel shader memory.\n");
2568 return E_OUTOFMEMORY;
2571 hr = pixelshader_init(object, This, byte_code);
2572 if (FAILED(hr))
2574 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2575 HeapFree(GetProcessHeap(), 0, object);
2576 return hr;
2579 TRACE("Created pixel shader %p.\n", object);
2580 *shader = &object->IDirect3DPixelShader9_iface;
2582 return D3D_OK;
2585 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShader(IDirect3DDevice9Ex *iface,
2586 IDirect3DPixelShader9 *shader)
2588 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2589 IDirect3DPixelShader9Impl *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2590 HRESULT hr;
2592 TRACE("iface %p, shader %p.\n", iface, shader);
2594 wined3d_mutex_lock();
2595 hr = wined3d_device_set_pixel_shader(This->wined3d_device,
2596 shader_obj ? shader_obj->wined3d_shader : NULL);
2597 wined3d_mutex_unlock();
2599 return hr;
2602 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShader(IDirect3DDevice9Ex *iface,
2603 IDirect3DPixelShader9 **shader)
2605 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2606 struct wined3d_shader *wined3d_shader;
2608 TRACE("iface %p, shader %p.\n", iface, shader);
2610 if (!shader) return D3DERR_INVALIDCALL;
2612 wined3d_mutex_lock();
2613 wined3d_shader = wined3d_device_get_pixel_shader(This->wined3d_device);
2614 if (wined3d_shader)
2616 *shader = wined3d_shader_get_parent(wined3d_shader);
2617 IDirect3DPixelShader9_AddRef(*shader);
2618 wined3d_shader_decref(wined3d_shader);
2620 else
2622 *shader = NULL;
2624 wined3d_mutex_unlock();
2626 TRACE("Returning %p.\n", *shader);
2628 return D3D_OK;
2631 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2632 UINT reg_idx, const float *data, UINT count)
2634 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2635 HRESULT hr;
2637 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2639 wined3d_mutex_lock();
2640 hr = wined3d_device_set_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2641 wined3d_mutex_unlock();
2643 return hr;
2646 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2647 UINT reg_idx, float *data, UINT count)
2649 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2650 HRESULT hr;
2652 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2654 wined3d_mutex_lock();
2655 hr = wined3d_device_get_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2656 wined3d_mutex_unlock();
2658 return hr;
2661 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2662 UINT reg_idx, const int *data, UINT count)
2664 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2665 HRESULT hr;
2667 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2669 wined3d_mutex_lock();
2670 hr = wined3d_device_set_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2671 wined3d_mutex_unlock();
2673 return hr;
2676 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2677 UINT reg_idx, int *data, UINT count)
2679 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2680 HRESULT hr;
2682 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2684 wined3d_mutex_lock();
2685 hr = wined3d_device_get_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2686 wined3d_mutex_unlock();
2688 return hr;
2691 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2692 UINT reg_idx, const BOOL *data, UINT count)
2694 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2695 HRESULT hr;
2697 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2699 wined3d_mutex_lock();
2700 hr = wined3d_device_set_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2701 wined3d_mutex_unlock();
2703 return hr;
2706 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2707 UINT reg_idx, BOOL *data, UINT count)
2709 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2710 HRESULT hr;
2712 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2714 wined3d_mutex_lock();
2715 hr = wined3d_device_get_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2716 wined3d_mutex_unlock();
2718 return hr;
2721 static HRESULT WINAPI IDirect3DDevice9Impl_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2722 const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2724 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2725 HRESULT hr;
2727 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2728 iface, Handle, pNumSegs, pRectPatchInfo);
2730 wined3d_mutex_lock();
2731 hr = wined3d_device_draw_rect_patch(This->wined3d_device, Handle,
2732 pNumSegs, (const struct wined3d_rect_patch_info *)pRectPatchInfo);
2733 wined3d_mutex_unlock();
2735 return hr;
2738 static HRESULT WINAPI IDirect3DDevice9Impl_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2739 const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2741 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2742 HRESULT hr;
2744 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2745 iface, Handle, pNumSegs, pTriPatchInfo);
2747 wined3d_mutex_lock();
2748 hr = wined3d_device_draw_tri_patch(This->wined3d_device, Handle,
2749 pNumSegs, (const struct wined3d_tri_patch_info *)pTriPatchInfo);
2750 wined3d_mutex_unlock();
2752 return hr;
2755 static HRESULT WINAPI IDirect3DDevice9Impl_DeletePatch(IDirect3DDevice9Ex *iface, UINT Handle)
2757 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2758 HRESULT hr;
2760 TRACE("iface %p, handle %#x.\n", iface, Handle);
2762 wined3d_mutex_lock();
2763 hr = wined3d_device_delete_patch(This->wined3d_device, Handle);
2764 wined3d_mutex_unlock();
2766 return hr;
2769 static HRESULT WINAPI IDirect3DDevice9Impl_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type,
2770 IDirect3DQuery9 **query)
2772 IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2773 IDirect3DQuery9Impl *object;
2774 HRESULT hr;
2776 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2778 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2779 if (!object)
2781 ERR("Failed to allocate query memory.\n");
2782 return E_OUTOFMEMORY;
2785 hr = query_init(object, This, type);
2786 if (FAILED(hr))
2788 WARN("Failed to initialize query, hr %#x.\n", hr);
2789 HeapFree(GetProcessHeap(), 0, object);
2790 return hr;
2793 TRACE("Created query %p.\n", object);
2794 if (query) *query = &object->IDirect3DQuery9_iface;
2795 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
2797 return D3D_OK;
2800 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2801 UINT width, UINT height, float *rows, float *columns)
2803 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2804 iface, width, height, rows, columns);
2806 return E_NOTIMPL;
2809 static HRESULT WINAPI IDirect3DDevice9ExImpl_ComposeRects(IDirect3DDevice9Ex *iface,
2810 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2811 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2813 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2814 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2815 iface, src_surface, dst_surface, src_descs, rect_count,
2816 dst_descs, operation, offset_x, offset_y);
2818 return E_NOTIMPL;
2821 static HRESULT WINAPI IDirect3DDevice9ExImpl_PresentEx(IDirect3DDevice9Ex *iface,
2822 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2823 const RGNDATA *dirty_region, DWORD flags)
2825 FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2826 iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2828 return E_NOTIMPL;
2831 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2833 FIXME("iface %p, priority %p stub!\n", iface, priority);
2835 return E_NOTIMPL;
2838 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2840 FIXME("iface %p, priority %d stub!\n", iface, priority);
2842 return E_NOTIMPL;
2845 static HRESULT WINAPI IDirect3DDevice9ExImpl_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2847 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2849 return E_NOTIMPL;
2852 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2853 IDirect3DResource9 **resources, UINT32 resource_count)
2855 FIXME("iface %p, resources %p, resource_count %u stub!\n",
2856 iface, resources, resource_count);
2858 return E_NOTIMPL;
2861 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2863 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2865 return E_NOTIMPL;
2868 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2870 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2872 *max_latency = 2;
2874 return E_NOTIMPL;
2877 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2879 static int i;
2881 TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
2883 if (!i++)
2884 FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2886 return D3D_OK;
2889 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2890 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2891 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2893 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2894 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2895 iface, width, height, format, multisample_type, multisample_quality,
2896 lockable, surface, shared_handle, usage);
2898 if (shared_handle)
2899 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2901 return E_NOTIMPL;
2904 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2905 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2906 HANDLE *shared_handle, DWORD usage)
2908 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2909 iface, width, height, format, pool, surface, shared_handle, usage);
2911 return E_NOTIMPL;
2914 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2915 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2916 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2918 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2919 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2920 iface, width, height, format, multisample_type, multisample_quality,
2921 discard, surface, shared_handle, usage);
2923 if (shared_handle)
2924 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2926 return E_NOTIMPL;
2929 static HRESULT WINAPI IDirect3DDevice9ExImpl_ResetEx(IDirect3DDevice9Ex *iface,
2930 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2932 FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2934 return E_NOTIMPL;
2937 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2938 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2940 FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2942 return E_NOTIMPL;
2945 static const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
2947 /* IUnknown */
2948 IDirect3DDevice9Impl_QueryInterface,
2949 IDirect3DDevice9Impl_AddRef,
2950 IDirect3DDevice9Impl_Release,
2951 /* IDirect3DDevice9 */
2952 IDirect3DDevice9Impl_TestCooperativeLevel,
2953 IDirect3DDevice9Impl_GetAvailableTextureMem,
2954 IDirect3DDevice9Impl_EvictManagedResources,
2955 IDirect3DDevice9Impl_GetDirect3D,
2956 IDirect3DDevice9Impl_GetDeviceCaps,
2957 IDirect3DDevice9Impl_GetDisplayMode,
2958 IDirect3DDevice9Impl_GetCreationParameters,
2959 IDirect3DDevice9Impl_SetCursorProperties,
2960 IDirect3DDevice9Impl_SetCursorPosition,
2961 IDirect3DDevice9Impl_ShowCursor,
2962 IDirect3DDevice9Impl_CreateAdditionalSwapChain,
2963 IDirect3DDevice9Impl_GetSwapChain,
2964 IDirect3DDevice9Impl_GetNumberOfSwapChains,
2965 IDirect3DDevice9Impl_Reset,
2966 IDirect3DDevice9Impl_Present,
2967 IDirect3DDevice9Impl_GetBackBuffer,
2968 IDirect3DDevice9Impl_GetRasterStatus,
2969 IDirect3DDevice9Impl_SetDialogBoxMode,
2970 IDirect3DDevice9Impl_SetGammaRamp,
2971 IDirect3DDevice9Impl_GetGammaRamp,
2972 IDirect3DDevice9Impl_CreateTexture,
2973 IDirect3DDevice9Impl_CreateVolumeTexture,
2974 IDirect3DDevice9Impl_CreateCubeTexture,
2975 IDirect3DDevice9Impl_CreateVertexBuffer,
2976 IDirect3DDevice9Impl_CreateIndexBuffer,
2977 IDirect3DDevice9Impl_CreateRenderTarget,
2978 IDirect3DDevice9Impl_CreateDepthStencilSurface,
2979 IDirect3DDevice9Impl_UpdateSurface,
2980 IDirect3DDevice9Impl_UpdateTexture,
2981 IDirect3DDevice9Impl_GetRenderTargetData,
2982 IDirect3DDevice9Impl_GetFrontBufferData,
2983 IDirect3DDevice9Impl_StretchRect,
2984 IDirect3DDevice9Impl_ColorFill,
2985 IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
2986 IDirect3DDevice9Impl_SetRenderTarget,
2987 IDirect3DDevice9Impl_GetRenderTarget,
2988 IDirect3DDevice9Impl_SetDepthStencilSurface,
2989 IDirect3DDevice9Impl_GetDepthStencilSurface,
2990 IDirect3DDevice9Impl_BeginScene,
2991 IDirect3DDevice9Impl_EndScene,
2992 IDirect3DDevice9Impl_Clear,
2993 IDirect3DDevice9Impl_SetTransform,
2994 IDirect3DDevice9Impl_GetTransform,
2995 IDirect3DDevice9Impl_MultiplyTransform,
2996 IDirect3DDevice9Impl_SetViewport,
2997 IDirect3DDevice9Impl_GetViewport,
2998 IDirect3DDevice9Impl_SetMaterial,
2999 IDirect3DDevice9Impl_GetMaterial,
3000 IDirect3DDevice9Impl_SetLight,
3001 IDirect3DDevice9Impl_GetLight,
3002 IDirect3DDevice9Impl_LightEnable,
3003 IDirect3DDevice9Impl_GetLightEnable,
3004 IDirect3DDevice9Impl_SetClipPlane,
3005 IDirect3DDevice9Impl_GetClipPlane,
3006 IDirect3DDevice9Impl_SetRenderState,
3007 IDirect3DDevice9Impl_GetRenderState,
3008 IDirect3DDevice9Impl_CreateStateBlock,
3009 IDirect3DDevice9Impl_BeginStateBlock,
3010 IDirect3DDevice9Impl_EndStateBlock,
3011 IDirect3DDevice9Impl_SetClipStatus,
3012 IDirect3DDevice9Impl_GetClipStatus,
3013 IDirect3DDevice9Impl_GetTexture,
3014 IDirect3DDevice9Impl_SetTexture,
3015 IDirect3DDevice9Impl_GetTextureStageState,
3016 IDirect3DDevice9Impl_SetTextureStageState,
3017 IDirect3DDevice9Impl_GetSamplerState,
3018 IDirect3DDevice9Impl_SetSamplerState,
3019 IDirect3DDevice9Impl_ValidateDevice,
3020 IDirect3DDevice9Impl_SetPaletteEntries,
3021 IDirect3DDevice9Impl_GetPaletteEntries,
3022 IDirect3DDevice9Impl_SetCurrentTexturePalette,
3023 IDirect3DDevice9Impl_GetCurrentTexturePalette,
3024 IDirect3DDevice9Impl_SetScissorRect,
3025 IDirect3DDevice9Impl_GetScissorRect,
3026 IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
3027 IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
3028 IDirect3DDevice9Impl_SetNPatchMode,
3029 IDirect3DDevice9Impl_GetNPatchMode,
3030 IDirect3DDevice9Impl_DrawPrimitive,
3031 IDirect3DDevice9Impl_DrawIndexedPrimitive,
3032 IDirect3DDevice9Impl_DrawPrimitiveUP,
3033 IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
3034 IDirect3DDevice9Impl_ProcessVertices,
3035 IDirect3DDevice9Impl_CreateVertexDeclaration,
3036 IDirect3DDevice9Impl_SetVertexDeclaration,
3037 IDirect3DDevice9Impl_GetVertexDeclaration,
3038 IDirect3DDevice9Impl_SetFVF,
3039 IDirect3DDevice9Impl_GetFVF,
3040 IDirect3DDevice9Impl_CreateVertexShader,
3041 IDirect3DDevice9Impl_SetVertexShader,
3042 IDirect3DDevice9Impl_GetVertexShader,
3043 IDirect3DDevice9Impl_SetVertexShaderConstantF,
3044 IDirect3DDevice9Impl_GetVertexShaderConstantF,
3045 IDirect3DDevice9Impl_SetVertexShaderConstantI,
3046 IDirect3DDevice9Impl_GetVertexShaderConstantI,
3047 IDirect3DDevice9Impl_SetVertexShaderConstantB,
3048 IDirect3DDevice9Impl_GetVertexShaderConstantB,
3049 IDirect3DDevice9Impl_SetStreamSource,
3050 IDirect3DDevice9Impl_GetStreamSource,
3051 IDirect3DDevice9Impl_SetStreamSourceFreq,
3052 IDirect3DDevice9Impl_GetStreamSourceFreq,
3053 IDirect3DDevice9Impl_SetIndices,
3054 IDirect3DDevice9Impl_GetIndices,
3055 IDirect3DDevice9Impl_CreatePixelShader,
3056 IDirect3DDevice9Impl_SetPixelShader,
3057 IDirect3DDevice9Impl_GetPixelShader,
3058 IDirect3DDevice9Impl_SetPixelShaderConstantF,
3059 IDirect3DDevice9Impl_GetPixelShaderConstantF,
3060 IDirect3DDevice9Impl_SetPixelShaderConstantI,
3061 IDirect3DDevice9Impl_GetPixelShaderConstantI,
3062 IDirect3DDevice9Impl_SetPixelShaderConstantB,
3063 IDirect3DDevice9Impl_GetPixelShaderConstantB,
3064 IDirect3DDevice9Impl_DrawRectPatch,
3065 IDirect3DDevice9Impl_DrawTriPatch,
3066 IDirect3DDevice9Impl_DeletePatch,
3067 IDirect3DDevice9Impl_CreateQuery,
3068 /* IDirect3DDevice9Ex */
3069 IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
3070 IDirect3DDevice9ExImpl_ComposeRects,
3071 IDirect3DDevice9ExImpl_PresentEx,
3072 IDirect3DDevice9ExImpl_GetGPUThreadPriority,
3073 IDirect3DDevice9ExImpl_SetGPUThreadPriority,
3074 IDirect3DDevice9ExImpl_WaitForVBlank,
3075 IDirect3DDevice9ExImpl_CheckResourceResidency,
3076 IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
3077 IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
3078 IDirect3DDevice9ExImpl_CheckDeviceState,
3079 IDirect3DDevice9ExImpl_CreateRenderTargetEx,
3080 IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
3081 IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
3082 IDirect3DDevice9ExImpl_ResetEx,
3083 IDirect3DDevice9ExImpl_GetDisplayModeEx
3086 static inline struct IDirect3DDevice9Impl *device_from_device_parent(struct wined3d_device_parent *device_parent)
3088 return CONTAINING_RECORD(device_parent, struct IDirect3DDevice9Impl, device_parent);
3091 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3092 struct wined3d_device *device)
3094 TRACE("device_parent %p, device %p.\n", device_parent, device);
3097 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3099 TRACE("device_parent %p.\n", device_parent);
3102 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
3103 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
3104 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
3106 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3107 IDirect3DSurface9Impl *d3d_surface;
3108 BOOL lockable = TRUE;
3109 HRESULT hr;
3111 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
3112 "\tpool %#x, level %u, face %u, surface %p.\n",
3113 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
3115 if (pool == WINED3DPOOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
3116 lockable = FALSE;
3118 hr = IDirect3DDevice9Impl_CreateSurface(device, width, height,
3119 d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
3120 (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
3121 if (FAILED(hr))
3123 WARN("Failed to create surface, hr %#x.\n", hr);
3124 return hr;
3127 *surface = d3d_surface->wined3d_surface;
3128 wined3d_surface_incref(*surface);
3130 d3d_surface->container = container_parent;
3131 IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
3132 d3d_surface->parentDevice = NULL;
3134 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3135 d3d_surface->forwardReference = container_parent;
3137 return hr;
3140 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
3141 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
3142 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
3143 struct wined3d_surface **surface)
3145 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3146 IDirect3DSurface9Impl *d3d_surface;
3147 HRESULT hr;
3149 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3150 "\tmultisample_quality %u, lockable %u, surface %p.\n",
3151 device_parent, container_parent, width, height, format, multisample_type,
3152 multisample_quality, lockable, surface);
3154 hr = IDirect3DDevice9Impl_CreateRenderTarget(&device->IDirect3DDevice9Ex_iface, width, height,
3155 d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
3156 (IDirect3DSurface9 **)&d3d_surface, NULL);
3157 if (FAILED(hr))
3159 WARN("Failed to create rendertarget, hr %#x.\n", hr);
3160 return hr;
3163 *surface = d3d_surface->wined3d_surface;
3164 wined3d_surface_incref(*surface);
3166 d3d_surface->container = container_parent;
3167 /* Implicit surfaces are created with an refcount of 0 */
3168 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3170 return hr;
3173 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
3174 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
3175 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
3177 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3178 IDirect3DSurface9Impl *d3d_surface;
3179 HRESULT hr;
3181 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3182 "\tmultisample_quality %u, discard %u, surface %p.\n",
3183 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
3185 hr = IDirect3DDevice9Impl_CreateDepthStencilSurface(&device->IDirect3DDevice9Ex_iface, width,
3186 height, d3dformat_from_wined3dformat(format), multisample_type, multisample_quality,
3187 discard, (IDirect3DSurface9 **)&d3d_surface, NULL);
3188 if (FAILED(hr))
3190 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
3191 return hr;
3194 *surface = d3d_surface->wined3d_surface;
3195 wined3d_surface_incref(*surface);
3196 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice9Ex_iface;
3197 /* Implicit surfaces are created with an refcount of 0 */
3198 IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3200 return hr;
3203 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
3204 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
3205 WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
3207 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3208 IDirect3DVolume9Impl *object;
3209 HRESULT hr;
3211 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
3212 "format %#x, pool %#x, usage %#x, volume %p\n",
3213 device_parent, container_parent, width, height, depth,
3214 format, pool, usage, volume);
3216 /* Allocate the storage for the device */
3217 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3218 if (!object)
3220 FIXME("Allocation of memory failed\n");
3221 *volume = NULL;
3222 return D3DERR_OUTOFVIDEOMEMORY;
3225 hr = volume_init(object, device, width, height, depth, usage, format, pool);
3226 if (FAILED(hr))
3228 WARN("Failed to initialize volume, hr %#x.\n", hr);
3229 HeapFree(GetProcessHeap(), 0, object);
3230 return hr;
3233 *volume = object->wined3d_volume;
3234 wined3d_volume_incref(*volume);
3235 IDirect3DVolume9_Release(&object->IDirect3DVolume9_iface);
3237 object->container = container_parent;
3238 object->forwardReference = container_parent;
3240 TRACE("Created volume %p.\n", object);
3242 return hr;
3245 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3246 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3248 struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3249 D3DPRESENT_PARAMETERS local_parameters;
3250 IDirect3DSwapChain9 *d3d_swapchain;
3251 HRESULT hr;
3253 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3255 /* Copy the presentation parameters */
3256 local_parameters.BackBufferWidth = desc->backbuffer_width;
3257 local_parameters.BackBufferHeight = desc->backbuffer_height;
3258 local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(desc->backbuffer_format);
3259 local_parameters.BackBufferCount = desc->backbuffer_count;
3260 local_parameters.MultiSampleType = desc->multisample_type;
3261 local_parameters.MultiSampleQuality = desc->multisample_quality;
3262 local_parameters.SwapEffect = desc->swap_effect;
3263 local_parameters.hDeviceWindow = desc->device_window;
3264 local_parameters.Windowed = desc->windowed;
3265 local_parameters.EnableAutoDepthStencil = desc->enable_auto_depth_stencil;
3266 local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc->auto_depth_stencil_format);
3267 local_parameters.Flags = desc->flags;
3268 local_parameters.FullScreen_RefreshRateInHz = desc->refresh_rate;
3269 local_parameters.PresentationInterval = desc->swap_interval;
3271 hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain(&device->IDirect3DDevice9Ex_iface,
3272 &local_parameters, &d3d_swapchain);
3273 if (FAILED(hr))
3275 WARN("Failed to create swapchain, hr %#x.\n", hr);
3276 *swapchain = NULL;
3277 return hr;
3280 *swapchain = ((IDirect3DSwapChain9Impl *)d3d_swapchain)->wined3d_swapchain;
3281 wined3d_swapchain_incref(*swapchain);
3282 IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)d3d_swapchain);
3284 /* Copy back the presentation parameters */
3285 desc->backbuffer_width = local_parameters.BackBufferWidth;
3286 desc->backbuffer_height = local_parameters.BackBufferHeight;
3287 desc->backbuffer_format = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3288 desc->backbuffer_count = local_parameters.BackBufferCount;
3289 desc->multisample_type = local_parameters.MultiSampleType;
3290 desc->multisample_quality = local_parameters.MultiSampleQuality;
3291 desc->swap_effect = local_parameters.SwapEffect;
3292 desc->device_window = local_parameters.hDeviceWindow;
3293 desc->windowed = local_parameters.Windowed;
3294 desc->enable_auto_depth_stencil = local_parameters.EnableAutoDepthStencil;
3295 desc->auto_depth_stencil_format = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3296 desc->flags = local_parameters.Flags;
3297 desc->refresh_rate = local_parameters.FullScreen_RefreshRateInHz;
3298 desc->swap_interval = local_parameters.PresentationInterval;
3300 return hr;
3303 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3305 device_parent_wined3d_device_created,
3306 device_parent_mode_changed,
3307 device_parent_create_surface,
3308 device_parent_create_rendertarget,
3309 device_parent_create_depth_stencil,
3310 device_parent_create_volume,
3311 device_parent_create_swapchain,
3314 static void setup_fpu(void)
3316 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3317 WORD cw;
3318 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3319 cw = (cw & ~0xf3f) | 0x3f;
3320 __asm__ volatile ("fldcw %0" : : "m" (cw));
3321 #elif defined(__i386__) && defined(_MSC_VER)
3322 WORD cw;
3323 __asm fnstcw cw;
3324 cw = (cw & ~0xf3f) | 0x3f;
3325 __asm fldcw cw;
3326 #else
3327 FIXME("FPU setup not implemented for this platform.\n");
3328 #endif
3331 HRESULT device_init(IDirect3DDevice9Impl *device, IDirect3D9Impl *parent, struct wined3d *wined3d,
3332 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3333 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3335 struct wined3d_swapchain_desc *swapchain_desc;
3336 UINT i, count = 1;
3337 HRESULT hr;
3339 if (mode)
3340 FIXME("Ignoring display mode.\n");
3342 device->IDirect3DDevice9Ex_iface.lpVtbl = &Direct3DDevice9_Vtbl;
3343 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3344 device->ref = 1;
3346 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3348 wined3d_mutex_lock();
3349 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3350 &device->device_parent, &device->wined3d_device);
3351 if (FAILED(hr))
3353 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3354 wined3d_mutex_unlock();
3355 return hr;
3358 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3360 WINED3DCAPS caps;
3362 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3363 count = caps.NumberOfAdaptersInGroup;
3366 if (flags & D3DCREATE_MULTITHREADED)
3367 wined3d_device_set_multithreaded(device->wined3d_device);
3369 if (!parameters->Windowed)
3371 if (!focus_window)
3372 focus_window = parameters->hDeviceWindow;
3373 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3375 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3376 wined3d_device_decref(device->wined3d_device);
3377 wined3d_mutex_unlock();
3378 return hr;
3381 for (i = 0; i < count; ++i)
3383 HWND device_window = parameters[i].hDeviceWindow;
3385 if (!device_window) device_window = focus_window;
3386 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3387 parameters[i].BackBufferWidth,
3388 parameters[i].BackBufferHeight);
3392 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3393 if (!swapchain_desc)
3395 ERR("Failed to allocate wined3d parameters.\n");
3396 wined3d_device_decref(device->wined3d_device);
3397 wined3d_mutex_unlock();
3398 return E_OUTOFMEMORY;
3401 for (i = 0; i < count; ++i)
3403 swapchain_desc[i].backbuffer_width = parameters[i].BackBufferWidth;
3404 swapchain_desc[i].backbuffer_height = parameters[i].BackBufferHeight;
3405 swapchain_desc[i].backbuffer_format = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
3406 swapchain_desc[i].backbuffer_count = parameters[i].BackBufferCount;
3407 swapchain_desc[i].multisample_type = parameters[i].MultiSampleType;
3408 swapchain_desc[i].multisample_quality = parameters[i].MultiSampleQuality;
3409 swapchain_desc[i].swap_effect = parameters[i].SwapEffect;
3410 swapchain_desc[i].device_window = parameters[i].hDeviceWindow;
3411 swapchain_desc[i].windowed = parameters[i].Windowed;
3412 swapchain_desc[i].enable_auto_depth_stencil = parameters[i].EnableAutoDepthStencil;
3413 swapchain_desc[i].auto_depth_stencil_format =
3414 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
3415 swapchain_desc[i].flags = parameters[i].Flags;
3416 swapchain_desc[i].refresh_rate = parameters[i].FullScreen_RefreshRateInHz;
3417 swapchain_desc[i].swap_interval = parameters[i].PresentationInterval;
3418 swapchain_desc[i].auto_restore_display_mode = TRUE;
3421 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3422 if (FAILED(hr))
3424 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3425 wined3d_device_release_focus_window(device->wined3d_device);
3426 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3427 wined3d_device_decref(device->wined3d_device);
3428 wined3d_mutex_unlock();
3429 return hr;
3432 wined3d_mutex_unlock();
3434 for (i = 0; i < count; ++i)
3436 parameters[i].BackBufferWidth = swapchain_desc[i].backbuffer_width;
3437 parameters[i].BackBufferHeight = swapchain_desc[i].backbuffer_height;
3438 parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc[i].backbuffer_format);
3439 parameters[i].BackBufferCount = swapchain_desc[i].backbuffer_count;
3440 parameters[i].MultiSampleType = swapchain_desc[i].multisample_type;
3441 parameters[i].MultiSampleQuality = swapchain_desc[i].multisample_quality;
3442 parameters[i].SwapEffect = swapchain_desc[i].swap_effect;
3443 parameters[i].hDeviceWindow = swapchain_desc[i].device_window;
3444 parameters[i].Windowed = swapchain_desc[i].windowed;
3445 parameters[i].EnableAutoDepthStencil = swapchain_desc[i].enable_auto_depth_stencil;
3446 parameters[i].AutoDepthStencilFormat =
3447 d3dformat_from_wined3dformat(swapchain_desc[i].auto_depth_stencil_format);
3448 parameters[i].Flags = swapchain_desc[i].flags;
3449 parameters[i].FullScreen_RefreshRateInHz = swapchain_desc[i].refresh_rate;
3450 parameters[i].PresentationInterval = swapchain_desc[i].swap_interval;
3452 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3454 /* Initialize the converted declaration array. This creates a valid pointer
3455 * and when adding decls HeapReAlloc() can be used without further checking. */
3456 device->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
3457 if (!device->convertedDecls)
3459 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3460 wined3d_mutex_lock();
3461 wined3d_device_uninit_3d(device->wined3d_device);
3462 wined3d_device_release_focus_window(device->wined3d_device);
3463 wined3d_device_decref(device->wined3d_device);
3464 wined3d_mutex_unlock();
3465 return E_OUTOFMEMORY;
3468 device->d3d_parent = &parent->IDirect3D9Ex_iface;
3469 IDirect3D9_AddRef(device->d3d_parent);
3471 return D3D_OK;