* Sync up to trunk head (r64939).
[reactos.git] / dll / directx / wine / d3d9 / device.c
1 /*
2 * IDirect3DDevice9 implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "d3d9_private.h"
24
25 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
26
27 static const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
28 {
29 d3d9_null_wined3d_object_destroyed,
30 };
31
32 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
33 {
34 BYTE *c = (BYTE *)&format;
35
36 /* Don't translate FOURCC formats */
37 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
38
39 switch(format)
40 {
41 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
42 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
43 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
44 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
45 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
46 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
47 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
48 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
49 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
50 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
51 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
52 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
53 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
54 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
55 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
56 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
57 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
58 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
59 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
60 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
61 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
62 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
63 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
64 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
65 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
66 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
67 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
68 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
69 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
70 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
71 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
72 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
73 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
74 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
75 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
76 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
77 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
78 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
79 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
80 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
81 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
82 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
83 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
84 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
85 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
86 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
87 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
88 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
89 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
90 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
91 default:
92 FIXME("Unhandled wined3d format %#x.\n", format);
93 return D3DFMT_UNKNOWN;
94 }
95 }
96
97 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
98 {
99 BYTE *c = (BYTE *)&format;
100
101 /* Don't translate FOURCC formats */
102 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
103
104 switch(format)
105 {
106 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
107 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
108 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
109 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
110 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
111 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
112 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
113 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
114 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
115 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
116 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
117 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
118 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
119 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
120 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
121 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
122 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
123 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
124 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
125 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
126 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
127 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
128 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
129 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
130 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
131 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
132 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
133 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
134 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
135 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
136 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
137 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
138 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
139 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
140 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
141 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
142 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
143 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
144 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
145 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
146 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
147 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
148 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
149 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
150 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
151 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
152 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
153 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
154 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
155 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
156 default:
157 FIXME("Unhandled D3DFORMAT %#x\n", format);
158 return WINED3DFMT_UNKNOWN;
159 }
160 }
161
162 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
163 {
164 switch(primitive_type)
165 {
166 case D3DPT_POINTLIST:
167 return primitive_count;
168
169 case D3DPT_LINELIST:
170 return primitive_count * 2;
171
172 case D3DPT_LINESTRIP:
173 return primitive_count + 1;
174
175 case D3DPT_TRIANGLELIST:
176 return primitive_count * 3;
177
178 case D3DPT_TRIANGLESTRIP:
179 case D3DPT_TRIANGLEFAN:
180 return primitive_count + 2;
181
182 default:
183 FIXME("Unhandled primitive type %#x\n", primitive_type);
184 return 0;
185 }
186 }
187
188 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
189 const struct wined3d_swapchain_desc *swapchain_desc)
190 {
191 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
192 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
193 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
194 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
195 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
196 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
197 present_parameters->SwapEffect = swapchain_desc->swap_effect;
198 present_parameters->hDeviceWindow = swapchain_desc->device_window;
199 present_parameters->Windowed = swapchain_desc->windowed;
200 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
201 present_parameters->AutoDepthStencilFormat
202 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
203 present_parameters->Flags = swapchain_desc->flags;
204 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
205 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
206 }
207
208 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
209 const D3DPRESENT_PARAMETERS *present_parameters)
210 {
211 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
212 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
213 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
214 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
215 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
216 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
217 swapchain_desc->swap_effect = present_parameters->SwapEffect;
218 swapchain_desc->device_window = present_parameters->hDeviceWindow;
219 swapchain_desc->windowed = present_parameters->Windowed;
220 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
221 swapchain_desc->auto_depth_stencil_format
222 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
223 swapchain_desc->flags = present_parameters->Flags;
224 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
225 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
226 swapchain_desc->auto_restore_display_mode = TRUE;
227 }
228
229 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
230 {
231 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
232
233 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
234 || IsEqualGUID(riid, &IID_IUnknown))
235 {
236 IDirect3DDevice9Ex_AddRef(iface);
237 *out = iface;
238 return S_OK;
239 }
240
241 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
242 {
243 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
244
245 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
246 * It doesn't matter with which function the device was created. */
247 if (!device->d3d_parent->extended)
248 {
249 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
250 *out = NULL;
251 return E_NOINTERFACE;
252 }
253
254 IDirect3DDevice9Ex_AddRef(iface);
255 *out = iface;
256 return S_OK;
257 }
258
259 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
260
261 *out = NULL;
262 return E_NOINTERFACE;
263 }
264
265 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
266 {
267 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
268 ULONG refcount = InterlockedIncrement(&device->refcount);
269
270 TRACE("%p increasing refcount to %u.\n", iface, refcount);
271
272 return refcount;
273 }
274
275 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
276 {
277 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
278 ULONG refcount;
279
280 if (device->in_destruction)
281 return 0;
282
283 refcount = InterlockedDecrement(&device->refcount);
284
285 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
286
287 if (!refcount)
288 {
289 unsigned i;
290 device->in_destruction = TRUE;
291
292 wined3d_mutex_lock();
293 for (i = 0; i < device->fvf_decl_count; ++i)
294 {
295 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
296 }
297 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
298
299 if (device->vertex_buffer)
300 wined3d_buffer_decref(device->vertex_buffer);
301 if (device->index_buffer)
302 wined3d_buffer_decref(device->index_buffer);
303
304 wined3d_device_uninit_3d(device->wined3d_device);
305 wined3d_device_release_focus_window(device->wined3d_device);
306 wined3d_device_decref(device->wined3d_device);
307 wined3d_mutex_unlock();
308
309 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
310
311 HeapFree(GetProcessHeap(), 0, device);
312 }
313
314 return refcount;
315 }
316
317 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
318 {
319 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
320
321 TRACE("iface %p.\n", iface);
322
323 TRACE("device state: %#x.\n", device->device_state);
324
325 if (device->d3d_parent->extended)
326 return D3D_OK;
327
328 switch (device->device_state)
329 {
330 default:
331 case D3D9_DEVICE_STATE_OK:
332 return D3D_OK;
333 case D3D9_DEVICE_STATE_LOST:
334 return D3DERR_DEVICELOST;
335 case D3D9_DEVICE_STATE_NOT_RESET:
336 return D3DERR_DEVICENOTRESET;
337 }
338 }
339
340 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
341 {
342 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
343 UINT ret;
344
345 TRACE("iface %p.\n", iface);
346
347 wined3d_mutex_lock();
348 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
349 wined3d_mutex_unlock();
350
351 return ret;
352 }
353
354 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
355 {
356 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
357
358 TRACE("iface %p.\n", iface);
359
360 wined3d_mutex_lock();
361 wined3d_device_evict_managed_resources(device->wined3d_device);
362 wined3d_mutex_unlock();
363
364 return D3D_OK;
365 }
366
367 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
368 {
369 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
370
371 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
372
373 if (!d3d9)
374 return D3DERR_INVALIDCALL;
375
376 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
377 }
378
379 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
380 {
381 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
382 WINED3DCAPS *wined3d_caps;
383 HRESULT hr;
384
385 TRACE("iface %p, caps %p.\n", iface, caps);
386
387 if (!caps)
388 return D3DERR_INVALIDCALL;
389
390 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
391 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
392
393 memset(caps, 0, sizeof(*caps));
394
395 wined3d_mutex_lock();
396 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
397 wined3d_mutex_unlock();
398
399 WINECAPSTOD3D9CAPS(caps, wined3d_caps)
400 HeapFree(GetProcessHeap(), 0, wined3d_caps);
401
402 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
403 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
404
405 filter_caps(caps);
406
407 return hr;
408 }
409
410 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
411 {
412 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
413 struct wined3d_display_mode wined3d_mode;
414 HRESULT hr;
415
416 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
417
418 wined3d_mutex_lock();
419 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
420 wined3d_mutex_unlock();
421
422 if (SUCCEEDED(hr))
423 {
424 mode->Width = wined3d_mode.width;
425 mode->Height = wined3d_mode.height;
426 mode->RefreshRate = wined3d_mode.refresh_rate;
427 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
428 }
429
430 return hr;
431 }
432
433 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
434 D3DDEVICE_CREATION_PARAMETERS *parameters)
435 {
436 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
437
438 TRACE("iface %p, parameters %p.\n", iface, parameters);
439
440 wined3d_mutex_lock();
441 wined3d_device_get_creation_parameters(device->wined3d_device,
442 (struct wined3d_device_creation_parameters *)parameters);
443 wined3d_mutex_unlock();
444
445 return D3D_OK;
446 }
447
448 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
449 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
450 {
451 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
452 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
453 HRESULT hr;
454
455 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
456 iface, hotspot_x, hotspot_y, bitmap);
457
458 if (!bitmap)
459 {
460 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
461 return D3DERR_INVALIDCALL;
462 }
463
464 wined3d_mutex_lock();
465 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
466 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
467 wined3d_mutex_unlock();
468
469 return hr;
470 }
471
472 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
473 {
474 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
475
476 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
477
478 wined3d_mutex_lock();
479 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
480 wined3d_mutex_unlock();
481 }
482
483 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
484 {
485 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
486 BOOL ret;
487
488 TRACE("iface %p, show %#x.\n", iface, show);
489
490 wined3d_mutex_lock();
491 ret = wined3d_device_show_cursor(device->wined3d_device, show);
492 wined3d_mutex_unlock();
493
494 return ret;
495 }
496
497 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
498 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
499 {
500 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
501 struct wined3d_swapchain_desc desc;
502 struct d3d9_swapchain *object;
503 HRESULT hr;
504
505 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
506 iface, present_parameters, swapchain);
507
508 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
509 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
510 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
511 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
512
513 return hr;
514 }
515
516 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
517 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
518 {
519 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
520 struct wined3d_swapchain *wined3d_swapchain;
521 struct d3d9_swapchain *swapchain_impl;
522 HRESULT hr;
523
524 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
525
526 wined3d_mutex_lock();
527 if ((wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, swapchain_idx)))
528 {
529 swapchain_impl = wined3d_swapchain_get_parent(wined3d_swapchain);
530 *swapchain = (IDirect3DSwapChain9 *)&swapchain_impl->IDirect3DSwapChain9Ex_iface;
531 IDirect3DSwapChain9Ex_AddRef(*swapchain);
532 hr = D3D_OK;
533 }
534 else
535 {
536 *swapchain = NULL;
537 hr = D3DERR_INVALIDCALL;
538 }
539 wined3d_mutex_unlock();
540
541 return hr;
542 }
543
544 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
545 {
546 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
547 UINT count;
548
549 TRACE("iface %p.\n", iface);
550
551 wined3d_mutex_lock();
552 count = wined3d_device_get_swapchain_count(device->wined3d_device);
553 wined3d_mutex_unlock();
554
555 return count;
556 }
557
558 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
559 {
560 struct wined3d_resource_desc desc;
561
562 wined3d_resource_get_desc(resource, &desc);
563 if (desc.pool == WINED3D_POOL_DEFAULT)
564 {
565 struct d3d9_surface *surface;
566
567 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
568 {
569 IUnknown *parent = wined3d_resource_get_parent(resource);
570 IDirect3DBaseTexture9 *texture;
571
572 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
573 {
574 IDirect3DBaseTexture9_Release(texture);
575 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
576 return D3DERR_INVALIDCALL;
577 }
578
579 return D3D_OK;
580 }
581
582 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
583 {
584 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
585 return D3DERR_INVALIDCALL;
586 }
587
588 surface = wined3d_resource_get_parent(resource);
589 if (surface->resource.refcount)
590 {
591 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
592 return D3DERR_INVALIDCALL;
593 }
594
595 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
596 }
597
598 return D3D_OK;
599 }
600
601 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
602 D3DPRESENT_PARAMETERS *present_parameters)
603 {
604 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
605 struct wined3d_swapchain_desc swapchain_desc;
606 HRESULT hr;
607
608 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
609
610 wined3d_mutex_lock();
611
612 if (device->vertex_buffer)
613 {
614 wined3d_buffer_decref(device->vertex_buffer);
615 device->vertex_buffer = NULL;
616 device->vertex_buffer_size = 0;
617 }
618 if (device->index_buffer)
619 {
620 wined3d_buffer_decref(device->index_buffer);
621 device->index_buffer = NULL;
622 device->index_buffer_size = 0;
623 }
624
625 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
626 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
627 NULL, reset_enum_callback, !device->d3d_parent->extended);
628 if (FAILED(hr) && !device->d3d_parent->extended)
629 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
630 else
631 device->device_state = D3D9_DEVICE_STATE_OK;
632 wined3d_mutex_unlock();
633
634 return hr;
635 }
636
637 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
638 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
639 {
640 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
641 HRESULT hr;
642
643 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
644 iface, src_rect, dst_rect, dst_window_override, dirty_region);
645
646 if (device->device_state != D3D9_DEVICE_STATE_OK)
647 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
648
649 wined3d_mutex_lock();
650 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
651 dst_window_override, dirty_region, 0);
652 wined3d_mutex_unlock();
653
654 return hr;
655 }
656
657 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
658 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
659 {
660 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
661 struct wined3d_surface *wined3d_surface = NULL;
662 struct d3d9_surface *surface_impl;
663 HRESULT hr;
664
665 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
666 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
667
668 wined3d_mutex_lock();
669 hr = wined3d_device_get_back_buffer(device->wined3d_device, swapchain,
670 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
671 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
672 {
673 surface_impl = wined3d_surface_get_parent(wined3d_surface);
674 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
675 IDirect3DSurface9_AddRef(*backbuffer);
676 }
677 wined3d_mutex_unlock();
678
679 return hr;
680 }
681 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
682 UINT swapchain, D3DRASTER_STATUS *raster_status)
683 {
684 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
685 HRESULT hr;
686
687 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
688
689 wined3d_mutex_lock();
690 hr = wined3d_device_get_raster_status(device->wined3d_device,
691 swapchain, (struct wined3d_raster_status *)raster_status);
692 wined3d_mutex_unlock();
693
694 return hr;
695 }
696
697 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
698 {
699 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
700 HRESULT hr;
701
702 TRACE("iface %p, enable %#x.\n", iface, enable);
703
704 wined3d_mutex_lock();
705 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
706 wined3d_mutex_unlock();
707
708 return hr;
709 }
710
711 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
712 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
713 {
714 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
715
716 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
717
718 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
719 wined3d_mutex_lock();
720 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
721 wined3d_mutex_unlock();
722 }
723
724 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
725 {
726 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
727
728 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
729
730 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
731 wined3d_mutex_lock();
732 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
733 wined3d_mutex_unlock();
734 }
735
736 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
737 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
738 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
739 {
740 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
741 struct d3d9_texture *object;
742 BOOL set_mem = FALSE;
743 HRESULT hr;
744
745 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
746 iface, width, height, levels, usage, format, pool, texture, shared_handle);
747
748 *texture = NULL;
749 if (shared_handle)
750 {
751 if (!device->d3d_parent->extended)
752 {
753 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
754 return E_NOTIMPL;
755 }
756
757 if (pool == D3DPOOL_SYSTEMMEM)
758 {
759 if (levels != 1)
760 return D3DERR_INVALIDCALL;
761 set_mem = TRUE;
762 }
763 else
764 {
765 if (pool != D3DPOOL_DEFAULT)
766 {
767 WARN("Trying to create a shared texture in pool %#x.\n", pool);
768 return D3DERR_INVALIDCALL;
769 }
770 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
771 }
772 }
773
774 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
775 if (!object)
776 return D3DERR_OUTOFVIDEOMEMORY;
777
778 hr = texture_init(object, device, width, height, levels, usage, format, pool);
779 if (FAILED(hr))
780 {
781 WARN("Failed to initialize texture, hr %#x.\n", hr);
782 HeapFree(GetProcessHeap(), 0, object);
783 return hr;
784 }
785
786 if (set_mem)
787 {
788 struct wined3d_resource *resource;
789 struct d3d9_surface *surface;
790
791 resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
792 surface = wined3d_resource_get_parent(resource);
793 wined3d_surface_update_desc(surface->wined3d_surface, width, height,
794 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
795 *shared_handle, 0);
796 }
797
798 TRACE("Created texture %p.\n", object);
799 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
800
801 return D3D_OK;
802 }
803
804 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
805 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
806 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
807 {
808 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
809 struct d3d9_texture *object;
810 HRESULT hr;
811
812 TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
813 iface, width, height, depth, levels);
814 TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
815 usage, format, pool, texture, shared_handle);
816
817 *texture = NULL;
818 if (shared_handle)
819 {
820 if (!device->d3d_parent->extended)
821 {
822 WARN("Trying to create a shared volume texture on a non-ex device.\n");
823 return E_NOTIMPL;
824 }
825
826 if (pool != D3DPOOL_DEFAULT)
827 {
828 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
829 return D3DERR_INVALIDCALL;
830 }
831 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
832 }
833
834 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
835 if (!object)
836 return D3DERR_OUTOFVIDEOMEMORY;
837
838 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
839 if (FAILED(hr))
840 {
841 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
842 HeapFree(GetProcessHeap(), 0, object);
843 return hr;
844 }
845
846 TRACE("Created volume texture %p.\n", object);
847 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
848
849 return D3D_OK;
850 }
851
852 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
853 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
854 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
855 {
856 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
857 struct d3d9_texture *object;
858 HRESULT hr;
859
860 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
861 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
862
863 *texture = NULL;
864 if (shared_handle)
865 {
866 if (!device->d3d_parent->extended)
867 {
868 WARN("Trying to create a shared cube texture on a non-ex device.\n");
869 return E_NOTIMPL;
870 }
871
872 if (pool != D3DPOOL_DEFAULT)
873 {
874 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
875 return D3DERR_INVALIDCALL;
876 }
877 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
878 }
879
880 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
881 if (!object)
882 return D3DERR_OUTOFVIDEOMEMORY;
883
884 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
885 if (FAILED(hr))
886 {
887 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
888 HeapFree(GetProcessHeap(), 0, object);
889 return hr;
890 }
891
892 TRACE("Created cube texture %p.\n", object);
893 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
894
895 return D3D_OK;
896 }
897
898 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
899 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
900 HANDLE *shared_handle)
901 {
902 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
903 struct d3d9_vertexbuffer *object;
904 HRESULT hr;
905
906 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
907 iface, size, usage, fvf, pool, buffer, shared_handle);
908
909 if (shared_handle)
910 {
911 if (!device->d3d_parent->extended)
912 {
913 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
914 return E_NOTIMPL;
915 }
916
917 if (pool != D3DPOOL_DEFAULT)
918 {
919 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
920 return D3DERR_NOTAVAILABLE;
921 }
922 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
923 }
924
925 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
926 if (!object)
927 return D3DERR_OUTOFVIDEOMEMORY;
928
929 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
930 if (FAILED(hr))
931 {
932 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
933 HeapFree(GetProcessHeap(), 0, object);
934 return hr;
935 }
936
937 TRACE("Created vertex buffer %p.\n", object);
938 *buffer = &object->IDirect3DVertexBuffer9_iface;
939
940 return D3D_OK;
941 }
942
943 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
944 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
945 HANDLE *shared_handle)
946 {
947 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
948 struct d3d9_indexbuffer *object;
949 HRESULT hr;
950
951 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
952 iface, size, usage, format, pool, buffer, shared_handle);
953
954 if (shared_handle)
955 {
956 if (!device->d3d_parent->extended)
957 {
958 WARN("Trying to create a shared index buffer on a non-ex device.\n");
959 return E_NOTIMPL;
960 }
961
962 if (pool != D3DPOOL_DEFAULT)
963 {
964 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
965 return D3DERR_NOTAVAILABLE;
966 }
967 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
968 }
969
970 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
971 if (!object)
972 return D3DERR_OUTOFVIDEOMEMORY;
973
974 hr = indexbuffer_init(object, device, size, usage, format, pool);
975 if (FAILED(hr))
976 {
977 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
978 HeapFree(GetProcessHeap(), 0, object);
979 return hr;
980 }
981
982 TRACE("Created index buffer %p.\n", object);
983 *buffer = &object->IDirect3DIndexBuffer9_iface;
984
985 return D3D_OK;
986 }
987
988 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
989 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
990 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
991 {
992 struct wined3d_resource *sub_resource;
993 struct wined3d_resource_desc desc;
994 struct d3d9_surface *surface_impl;
995 struct wined3d_texture *texture;
996 HRESULT hr;
997
998 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
999 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1000 device, width, height, format, flags, surface, usage, pool,
1001 multisample_type, multisample_quality);
1002
1003 desc.resource_type = WINED3D_RTYPE_TEXTURE;
1004 desc.format = wined3dformat_from_d3dformat(format);
1005 desc.multisample_type = multisample_type;
1006 desc.multisample_quality = multisample_quality;
1007 desc.usage = usage & WINED3DUSAGE_MASK;
1008 desc.pool = pool;
1009 desc.width = width;
1010 desc.height = height;
1011 desc.depth = 1;
1012 desc.size = 0;
1013
1014 wined3d_mutex_lock();
1015
1016 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1017 1, flags, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1018 {
1019 wined3d_mutex_unlock();
1020 WARN("Failed to create texture, hr %#x.\n", hr);
1021 return hr;
1022 }
1023
1024 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
1025 surface_impl = wined3d_resource_get_parent(sub_resource);
1026 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1027 *surface = &surface_impl->IDirect3DSurface9_iface;
1028 IDirect3DSurface9_AddRef(*surface);
1029 wined3d_texture_decref(texture);
1030
1031 if (user_mem)
1032 wined3d_surface_update_desc(surface_impl->wined3d_surface, width, height,
1033 desc.format, multisample_type, multisample_quality, user_mem, 0);
1034
1035 wined3d_mutex_unlock();
1036
1037 return D3D_OK;
1038 }
1039
1040 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1041 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1042 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1043 {
1044 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1045 DWORD flags = 0;
1046
1047 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1048 "lockable %#x, surface %p, shared_handle %p.\n",
1049 iface, width, height, format, multisample_type, multisample_quality,
1050 lockable, surface, shared_handle);
1051
1052 *surface = NULL;
1053 if (shared_handle)
1054 {
1055 if (!device->d3d_parent->extended)
1056 {
1057 WARN("Trying to create a shared render target on a non-ex device.\n");
1058 return E_NOTIMPL;
1059 }
1060
1061 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1062 }
1063
1064 if (lockable)
1065 flags |= WINED3D_SURFACE_MAPPABLE;
1066
1067 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1068 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1069 }
1070
1071 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1072 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1073 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1074 {
1075 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1076 DWORD flags = WINED3D_SURFACE_MAPPABLE;
1077
1078 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1079 "discard %#x, surface %p, shared_handle %p.\n",
1080 iface, width, height, format, multisample_type, multisample_quality,
1081 discard, surface, shared_handle);
1082
1083 *surface = NULL;
1084 if (shared_handle)
1085 {
1086 if (!device->d3d_parent->extended)
1087 {
1088 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1089 return E_NOTIMPL;
1090 }
1091
1092 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1093 }
1094
1095 if (discard)
1096 flags |= WINED3D_SURFACE_DISCARD;
1097
1098 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1099 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1100 }
1101
1102
1103 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1104 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1105 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1106 {
1107 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1108 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1109 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1110 HRESULT hr;
1111
1112 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1113 iface, src_surface, src_rect, dst_surface, dst_point);
1114
1115 wined3d_mutex_lock();
1116 hr = wined3d_device_update_surface(device->wined3d_device, src->wined3d_surface, src_rect,
1117 dst->wined3d_surface, dst_point);
1118 wined3d_mutex_unlock();
1119
1120 return hr;
1121 }
1122
1123 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1124 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1125 {
1126 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1127 struct d3d9_texture *src_impl, *dst_impl;
1128 HRESULT hr;
1129
1130 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1131
1132 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1133 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1134
1135 wined3d_mutex_lock();
1136 hr = wined3d_device_update_texture(device->wined3d_device,
1137 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1138 wined3d_mutex_unlock();
1139
1140 return hr;
1141 }
1142
1143 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1144 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1145 {
1146 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1147 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1148 HRESULT hr;
1149
1150 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1151
1152 wined3d_mutex_lock();
1153 hr = wined3d_surface_get_render_target_data(dst_impl->wined3d_surface, rt_impl->wined3d_surface);
1154 wined3d_mutex_unlock();
1155
1156 return hr;
1157 }
1158
1159 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1160 UINT swapchain, IDirect3DSurface9 *dst_surface)
1161 {
1162 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1163 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1164 HRESULT hr;
1165
1166 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1167
1168 wined3d_mutex_lock();
1169 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, swapchain, dst_impl->wined3d_surface);
1170 wined3d_mutex_unlock();
1171
1172 return hr;
1173 }
1174
1175 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1176 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1177 {
1178 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1179 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1180 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1181 HRESULT hr = D3DERR_INVALIDCALL;
1182 struct wined3d_resource_desc src_desc, dst_desc;
1183 struct wined3d_resource *wined3d_resource;
1184
1185 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1186 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1187
1188 wined3d_mutex_lock();
1189 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1190 wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1191
1192 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1193 wined3d_resource_get_desc(wined3d_resource, &src_desc);
1194
1195 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1196 {
1197 if (device->in_scene)
1198 {
1199 WARN("Rejecting depth / stencil blit while in scene.\n");
1200 goto done;
1201 }
1202
1203 if (src_rect)
1204 {
1205 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1206 || src_rect->bottom != src_desc.height)
1207 {
1208 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1209 wine_dbgstr_rect(src_rect));
1210 goto done;
1211 }
1212 }
1213 if (dst_rect)
1214 {
1215 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1216 || dst_rect->bottom != dst_desc.height)
1217 {
1218 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1219 wine_dbgstr_rect(dst_rect));
1220 goto done;
1221 }
1222 }
1223 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1224 {
1225 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1226 goto done;
1227 }
1228 }
1229
1230 hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1231 if (hr == WINEDDERR_INVALIDRECT)
1232 hr = D3DERR_INVALIDCALL;
1233
1234 done:
1235 wined3d_mutex_unlock();
1236 return hr;
1237 }
1238
1239 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1240 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1241 {
1242 const struct wined3d_color c =
1243 {
1244 ((color >> 16) & 0xff) / 255.0f,
1245 ((color >> 8) & 0xff) / 255.0f,
1246 (color & 0xff) / 255.0f,
1247 ((color >> 24) & 0xff) / 255.0f,
1248 };
1249 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1250 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1251 struct wined3d_resource *wined3d_resource;
1252 struct wined3d_resource_desc desc;
1253 HRESULT hr;
1254
1255 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1256
1257 wined3d_mutex_lock();
1258
1259 wined3d_resource = wined3d_surface_get_resource(surface_impl->wined3d_surface);
1260 wined3d_resource_get_desc(wined3d_resource, &desc);
1261
1262 /* This method is only allowed with surfaces that are render targets, or
1263 * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1264 if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1265 {
1266 wined3d_mutex_unlock();
1267 WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1268 return D3DERR_INVALIDCALL;
1269 }
1270
1271 if (desc.pool != WINED3D_POOL_DEFAULT && desc.pool != WINED3D_POOL_SYSTEM_MEM)
1272 {
1273 WARN("Color-fill not allowed on surfaces in pool %#x.\n", desc.pool);
1274 return D3DERR_INVALIDCALL;
1275 }
1276
1277 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1278 d3d9_surface_get_rendertarget_view(surface_impl), rect, &c);
1279
1280 wined3d_mutex_unlock();
1281
1282 return hr;
1283 }
1284
1285 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1286 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1287 HANDLE *shared_handle)
1288 {
1289 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1290 void *user_mem = NULL;
1291
1292 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1293 iface, width, height, format, pool, surface, shared_handle);
1294
1295 *surface = NULL;
1296 if (pool == D3DPOOL_MANAGED)
1297 {
1298 WARN("Attempting to create a managed offscreen plain surface.\n");
1299 return D3DERR_INVALIDCALL;
1300 }
1301
1302 if (shared_handle)
1303 {
1304 if (!device->d3d_parent->extended)
1305 {
1306 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1307 return E_NOTIMPL;
1308 }
1309
1310 if (pool == D3DPOOL_SYSTEMMEM)
1311 user_mem = *shared_handle;
1312 else
1313 {
1314 if (pool != D3DPOOL_DEFAULT)
1315 {
1316 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1317 return D3DERR_INVALIDCALL;
1318 }
1319 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1320 }
1321 }
1322
1323 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1324 * regardless of the pool they're created in. Should we set dynamic usage
1325 * here? */
1326 return d3d9_device_create_surface(device, width, height, format,
1327 WINED3D_SURFACE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1328 }
1329
1330 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1331 {
1332 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1333 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1334 HRESULT hr;
1335
1336 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1337
1338 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1339 {
1340 WARN("Invalid index %u specified.\n", idx);
1341 return D3DERR_INVALIDCALL;
1342 }
1343
1344 if (!idx && !surface_impl)
1345 {
1346 WARN("Trying to set render target 0 to NULL.\n");
1347 return D3DERR_INVALIDCALL;
1348 }
1349
1350 wined3d_mutex_lock();
1351 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx,
1352 surface_impl ? d3d9_surface_get_rendertarget_view(surface_impl) : NULL, TRUE);
1353 wined3d_mutex_unlock();
1354
1355 return hr;
1356 }
1357
1358 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1359 {
1360 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1361 struct wined3d_rendertarget_view *wined3d_rtv;
1362 struct d3d9_surface *surface_impl;
1363 HRESULT hr = D3D_OK;
1364
1365 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1366
1367 if (!surface)
1368 return D3DERR_INVALIDCALL;
1369
1370 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1371 {
1372 WARN("Invalid index %u specified.\n", idx);
1373 return D3DERR_INVALIDCALL;
1374 }
1375
1376 wined3d_mutex_lock();
1377 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1378 {
1379 /* We want the sub resource parent here, since the view itself may be
1380 * internal to wined3d and may not have a parent. */
1381 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1382 *surface = &surface_impl->IDirect3DSurface9_iface;
1383 IDirect3DSurface9_AddRef(*surface);
1384 }
1385 else
1386 {
1387 hr = WINED3DERR_NOTFOUND;
1388 *surface = NULL;
1389 }
1390 wined3d_mutex_unlock();
1391
1392 return hr;
1393 }
1394
1395 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1396 {
1397 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1398 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1399
1400 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1401
1402 wined3d_mutex_lock();
1403 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1404 ds_impl ? d3d9_surface_get_rendertarget_view(ds_impl) : NULL);
1405 wined3d_mutex_unlock();
1406
1407 return D3D_OK;
1408 }
1409
1410 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1411 {
1412 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1413 struct wined3d_rendertarget_view *wined3d_dsv;
1414 struct d3d9_surface *surface_impl;
1415 HRESULT hr = D3D_OK;
1416
1417 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1418
1419 if (!depth_stencil)
1420 return D3DERR_INVALIDCALL;
1421
1422 wined3d_mutex_lock();
1423 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1424 {
1425 /* We want the sub resource parent here, since the view itself may be
1426 * internal to wined3d and may not have a parent. */
1427 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1428 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1429 IDirect3DSurface9_AddRef(*depth_stencil);
1430 }
1431 else
1432 {
1433 hr = WINED3DERR_NOTFOUND;
1434 *depth_stencil = NULL;
1435 }
1436 wined3d_mutex_unlock();
1437
1438 return hr;
1439 }
1440
1441 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1442 {
1443 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1444 HRESULT hr;
1445
1446 TRACE("iface %p.\n", iface);
1447
1448 wined3d_mutex_lock();
1449 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1450 device->in_scene = TRUE;
1451 wined3d_mutex_unlock();
1452
1453 return hr;
1454 }
1455
1456 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1457 {
1458 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1459 HRESULT hr;
1460
1461 TRACE("iface %p.\n", iface);
1462
1463 wined3d_mutex_lock();
1464 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1465 device->in_scene = FALSE;
1466 wined3d_mutex_unlock();
1467
1468 return hr;
1469 }
1470
1471 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1472 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1473 {
1474 const struct wined3d_color c =
1475 {
1476 ((color >> 16) & 0xff) / 255.0f,
1477 ((color >> 8) & 0xff) / 255.0f,
1478 (color & 0xff) / 255.0f,
1479 ((color >> 24) & 0xff) / 255.0f,
1480 };
1481 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1482 HRESULT hr;
1483
1484 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1485 iface, rect_count, rects, flags, color, z, stencil);
1486
1487 wined3d_mutex_lock();
1488 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1489 wined3d_mutex_unlock();
1490
1491 return hr;
1492 }
1493
1494 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1495 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1496 {
1497 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1498
1499 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1500
1501 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1502 wined3d_mutex_lock();
1503 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1504 wined3d_mutex_unlock();
1505
1506 return D3D_OK;
1507 }
1508
1509 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1510 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1511 {
1512 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1513
1514 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1515
1516 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1517 wined3d_mutex_lock();
1518 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1519 wined3d_mutex_unlock();
1520
1521 return D3D_OK;
1522 }
1523
1524 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1525 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1526 {
1527 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1528
1529 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1530
1531 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1532 wined3d_mutex_lock();
1533 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1534 wined3d_mutex_unlock();
1535
1536 return D3D_OK;
1537 }
1538
1539 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1540 {
1541 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1542
1543 TRACE("iface %p, viewport %p.\n", iface, viewport);
1544
1545 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1546 wined3d_mutex_lock();
1547 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1548 wined3d_mutex_unlock();
1549
1550 return D3D_OK;
1551 }
1552
1553 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1554 {
1555 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1556
1557 TRACE("iface %p, viewport %p.\n", iface, viewport);
1558
1559 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1560 wined3d_mutex_lock();
1561 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1562 wined3d_mutex_unlock();
1563
1564 return D3D_OK;
1565 }
1566
1567 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1568 {
1569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1570
1571 TRACE("iface %p, material %p.\n", iface, material);
1572
1573 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1574 wined3d_mutex_lock();
1575 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1576 wined3d_mutex_unlock();
1577
1578 return D3D_OK;
1579 }
1580
1581 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1582 {
1583 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1584
1585 TRACE("iface %p, material %p.\n", iface, material);
1586
1587 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1588 wined3d_mutex_lock();
1589 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1590 wined3d_mutex_unlock();
1591
1592 return D3D_OK;
1593 }
1594
1595 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1596 {
1597 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1598 HRESULT hr;
1599
1600 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1601
1602 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1603 wined3d_mutex_lock();
1604 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1605 wined3d_mutex_unlock();
1606
1607 return hr;
1608 }
1609
1610 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1611 {
1612 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1613 HRESULT hr;
1614
1615 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1616
1617 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1618 wined3d_mutex_lock();
1619 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1620 wined3d_mutex_unlock();
1621
1622 return hr;
1623 }
1624
1625 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1626 {
1627 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1628 HRESULT hr;
1629
1630 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1631
1632 wined3d_mutex_lock();
1633 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1634 wined3d_mutex_unlock();
1635
1636 return hr;
1637 }
1638
1639 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1640 {
1641 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1642 HRESULT hr;
1643
1644 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1645
1646 wined3d_mutex_lock();
1647 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1648 wined3d_mutex_unlock();
1649
1650 return hr;
1651 }
1652
1653 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
1654 {
1655 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1656 HRESULT hr;
1657
1658 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1659
1660 wined3d_mutex_lock();
1661 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1662 wined3d_mutex_unlock();
1663
1664 return hr;
1665 }
1666
1667 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
1668 {
1669 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1670 HRESULT hr;
1671
1672 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1673
1674 wined3d_mutex_lock();
1675 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1676 wined3d_mutex_unlock();
1677
1678 return hr;
1679 }
1680
1681 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
1682 D3DRENDERSTATETYPE state, DWORD value)
1683 {
1684 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1685
1686 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1687
1688 wined3d_mutex_lock();
1689 wined3d_device_set_render_state(device->wined3d_device, state, value);
1690 wined3d_mutex_unlock();
1691
1692 return D3D_OK;
1693 }
1694
1695 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
1696 D3DRENDERSTATETYPE state, DWORD *value)
1697 {
1698 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1699
1700 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1701
1702 wined3d_mutex_lock();
1703 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1704 wined3d_mutex_unlock();
1705
1706 return D3D_OK;
1707 }
1708
1709 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
1710 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1711 {
1712 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1713 struct d3d9_stateblock *object;
1714 HRESULT hr;
1715
1716 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1717
1718 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1719 {
1720 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1721 return D3DERR_INVALIDCALL;
1722 }
1723
1724 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1725 if (!object)
1726 return E_OUTOFMEMORY;
1727
1728 hr = stateblock_init(object, device, type, NULL);
1729 if (FAILED(hr))
1730 {
1731 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1732 HeapFree(GetProcessHeap(), 0, object);
1733 return hr;
1734 }
1735
1736 TRACE("Created stateblock %p.\n", object);
1737 *stateblock = &object->IDirect3DStateBlock9_iface;
1738
1739 return D3D_OK;
1740 }
1741
1742 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
1743 {
1744 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1745 HRESULT hr;
1746
1747 TRACE("iface %p.\n", iface);
1748
1749 wined3d_mutex_lock();
1750 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1751 wined3d_mutex_unlock();
1752
1753 return hr;
1754 }
1755
1756 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1757 {
1758 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1759 struct wined3d_stateblock *wined3d_stateblock;
1760 struct d3d9_stateblock *object;
1761 HRESULT hr;
1762
1763 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1764
1765 wined3d_mutex_lock();
1766 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
1767 wined3d_mutex_unlock();
1768 if (FAILED(hr))
1769 {
1770 WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1771 return hr;
1772 }
1773
1774 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1775 if (!object)
1776 {
1777 wined3d_mutex_lock();
1778 wined3d_stateblock_decref(wined3d_stateblock);
1779 wined3d_mutex_unlock();
1780 return E_OUTOFMEMORY;
1781 }
1782
1783 hr = stateblock_init(object, device, 0, wined3d_stateblock);
1784 if (FAILED(hr))
1785 {
1786 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1787 wined3d_mutex_lock();
1788 wined3d_stateblock_decref(wined3d_stateblock);
1789 wined3d_mutex_unlock();
1790 HeapFree(GetProcessHeap(), 0, object);
1791 return hr;
1792 }
1793
1794 TRACE("Created stateblock %p.\n", object);
1795 *stateblock = &object->IDirect3DStateBlock9_iface;
1796
1797 return D3D_OK;
1798 }
1799
1800 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
1801 {
1802 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1803 HRESULT hr;
1804
1805 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1806
1807 wined3d_mutex_lock();
1808 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1809 wined3d_mutex_unlock();
1810
1811 return hr;
1812 }
1813
1814 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
1815 {
1816 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1817 HRESULT hr;
1818
1819 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1820
1821 wined3d_mutex_lock();
1822 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1823 wined3d_mutex_unlock();
1824
1825 return hr;
1826 }
1827
1828 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
1829 {
1830 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1831 struct wined3d_texture *wined3d_texture = NULL;
1832 struct d3d9_texture *texture_impl;
1833
1834 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1835
1836 if (!texture)
1837 return D3DERR_INVALIDCALL;
1838
1839 wined3d_mutex_lock();
1840 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1841 {
1842 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1843 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
1844 IDirect3DBaseTexture9_AddRef(*texture);
1845 }
1846 else
1847 {
1848 *texture = NULL;
1849 }
1850 wined3d_mutex_unlock();
1851
1852 return D3D_OK;
1853 }
1854
1855 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
1856 {
1857 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1858 struct d3d9_texture *texture_impl;
1859 HRESULT hr;
1860
1861 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1862
1863 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
1864
1865 wined3d_mutex_lock();
1866 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1867 texture_impl ? texture_impl->wined3d_texture : NULL);
1868 wined3d_mutex_unlock();
1869
1870 return hr;
1871 }
1872
1873 static const enum wined3d_texture_stage_state tss_lookup[] =
1874 {
1875 WINED3D_TSS_INVALID, /* 0, unused */
1876 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
1877 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
1878 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
1879 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
1880 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
1881 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
1882 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
1883 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
1884 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
1885 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
1886 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
1887 WINED3D_TSS_INVALID, /* 12, unused */
1888 WINED3D_TSS_INVALID, /* 13, unused */
1889 WINED3D_TSS_INVALID, /* 14, unused */
1890 WINED3D_TSS_INVALID, /* 15, unused */
1891 WINED3D_TSS_INVALID, /* 16, unused */
1892 WINED3D_TSS_INVALID, /* 17, unused */
1893 WINED3D_TSS_INVALID, /* 18, unused */
1894 WINED3D_TSS_INVALID, /* 19, unused */
1895 WINED3D_TSS_INVALID, /* 20, unused */
1896 WINED3D_TSS_INVALID, /* 21, unused */
1897 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
1898 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
1899 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1900 WINED3D_TSS_INVALID, /* 25, unused */
1901 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
1902 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
1903 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
1904 WINED3D_TSS_INVALID, /* 29, unused */
1905 WINED3D_TSS_INVALID, /* 30, unused */
1906 WINED3D_TSS_INVALID, /* 31, unused */
1907 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
1908 };
1909
1910 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
1911 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
1912 {
1913 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1914
1915 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
1916
1917 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1918 {
1919 WARN("Invalid state %#x passed.\n", state);
1920 return D3D_OK;
1921 }
1922
1923 wined3d_mutex_lock();
1924 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
1925 wined3d_mutex_unlock();
1926
1927 return D3D_OK;
1928 }
1929
1930 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
1931 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
1932 {
1933 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1934
1935 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
1936
1937 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1938 {
1939 WARN("Invalid state %#x passed.\n", state);
1940 return D3D_OK;
1941 }
1942
1943 wined3d_mutex_lock();
1944 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
1945 wined3d_mutex_unlock();
1946
1947 return D3D_OK;
1948 }
1949
1950 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
1951 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
1952 {
1953 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1954
1955 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
1956
1957 wined3d_mutex_lock();
1958 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
1959 wined3d_mutex_unlock();
1960
1961 return D3D_OK;
1962 }
1963
1964 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
1965 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
1966 {
1967 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1968
1969 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
1970
1971 wined3d_mutex_lock();
1972 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
1973 wined3d_mutex_unlock();
1974
1975 return D3D_OK;
1976 }
1977
1978 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
1979 {
1980 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1981 HRESULT hr;
1982
1983 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1984
1985 wined3d_mutex_lock();
1986 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1987 wined3d_mutex_unlock();
1988
1989 return hr;
1990 }
1991
1992 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1993 UINT palette_idx, const PALETTEENTRY *entries)
1994 {
1995 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1996
1997 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
1998 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
1999 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2000 *
2001 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2002 return D3D_OK;
2003 }
2004
2005 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2006 UINT palette_idx, PALETTEENTRY *entries)
2007 {
2008 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2009
2010 return D3DERR_INVALIDCALL;
2011 }
2012
2013 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2014 {
2015 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2016
2017 return D3D_OK;
2018 }
2019
2020 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2021 {
2022 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2023
2024 return D3DERR_INVALIDCALL;
2025 }
2026
2027 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2028 {
2029 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2030
2031 TRACE("iface %p, rect %p.\n", iface, rect);
2032
2033 wined3d_mutex_lock();
2034 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2035 wined3d_mutex_unlock();
2036
2037 return D3D_OK;
2038 }
2039
2040 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2041 {
2042 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2043
2044 TRACE("iface %p, rect %p.\n", iface, rect);
2045
2046 wined3d_mutex_lock();
2047 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2048 wined3d_mutex_unlock();
2049
2050 return D3D_OK;
2051 }
2052
2053 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2054 {
2055 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2056
2057 TRACE("iface %p, software %#x.\n", iface, software);
2058
2059 wined3d_mutex_lock();
2060 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2061 wined3d_mutex_unlock();
2062
2063 return D3D_OK;
2064 }
2065
2066 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2067 {
2068 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2069 BOOL ret;
2070
2071 TRACE("iface %p.\n", iface);
2072
2073 wined3d_mutex_lock();
2074 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2075 wined3d_mutex_unlock();
2076
2077 return ret;
2078 }
2079
2080 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2081 {
2082 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2083 HRESULT hr;
2084
2085 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2086
2087 wined3d_mutex_lock();
2088 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2089 wined3d_mutex_unlock();
2090
2091 return hr;
2092 }
2093
2094 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2095 {
2096 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2097 float ret;
2098
2099 TRACE("iface %p.\n", iface);
2100
2101 wined3d_mutex_lock();
2102 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2103 wined3d_mutex_unlock();
2104
2105 return ret;
2106 }
2107
2108 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2109 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2110 {
2111 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2112 HRESULT hr;
2113
2114 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2115 iface, primitive_type, start_vertex, primitive_count);
2116
2117 wined3d_mutex_lock();
2118 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2119 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2120 vertex_count_from_primitive_count(primitive_type, primitive_count));
2121 wined3d_mutex_unlock();
2122
2123 return hr;
2124 }
2125
2126 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2127 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2128 UINT vertex_count, UINT start_idx, UINT primitive_count)
2129 {
2130 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2131 HRESULT hr;
2132
2133 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2134 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2135 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2136 vertex_count, start_idx, primitive_count);
2137
2138 wined3d_mutex_lock();
2139 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2140 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2141 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2142 vertex_count_from_primitive_count(primitive_type, primitive_count));
2143 wined3d_mutex_unlock();
2144
2145 return hr;
2146 }
2147
2148 /* The caller is responsible for wined3d locking */
2149 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2150 {
2151 HRESULT hr;
2152
2153 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2154 {
2155 UINT size = max(device->vertex_buffer_size * 2, min_size);
2156 struct wined3d_buffer *buffer;
2157
2158 TRACE("Growing vertex buffer to %u bytes\n", size);
2159
2160 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2161 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2162 if (FAILED(hr))
2163 {
2164 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2165 return hr;
2166 }
2167
2168 if (device->vertex_buffer)
2169 wined3d_buffer_decref(device->vertex_buffer);
2170
2171 device->vertex_buffer = buffer;
2172 device->vertex_buffer_size = size;
2173 device->vertex_buffer_pos = 0;
2174 }
2175 return D3D_OK;
2176 }
2177
2178 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2179 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2180 {
2181 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2182 HRESULT hr;
2183 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2184 UINT size = vtx_count * stride;
2185 UINT vb_pos, align;
2186 BYTE *buffer_data;
2187
2188 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2189 iface, primitive_type, primitive_count, data, stride);
2190
2191 if (!primitive_count)
2192 {
2193 WARN("primitive_count is 0, returning D3D_OK\n");
2194 return D3D_OK;
2195 }
2196
2197 wined3d_mutex_lock();
2198
2199 hr = d3d9_device_prepare_vertex_buffer(device, size);
2200 if (FAILED(hr))
2201 goto done;
2202
2203 vb_pos = device->vertex_buffer_pos;
2204 align = vb_pos % stride;
2205 if (align) align = stride - align;
2206 if (vb_pos + size + align > device->vertex_buffer_size)
2207 vb_pos = 0;
2208 else
2209 vb_pos += align;
2210
2211 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2212 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2213 if (FAILED(hr))
2214 goto done;
2215 memcpy(buffer_data, data, size);
2216 wined3d_buffer_unmap(device->vertex_buffer);
2217 device->vertex_buffer_pos = vb_pos + size;
2218
2219 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2220 if (FAILED(hr))
2221 goto done;
2222
2223 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2224 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2225 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2226
2227 done:
2228 wined3d_mutex_unlock();
2229 return hr;
2230 }
2231
2232 /* The caller is responsible for wined3d locking */
2233 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2234 {
2235 HRESULT hr;
2236
2237 if (device->index_buffer_size < min_size || !device->index_buffer)
2238 {
2239 UINT size = max(device->index_buffer_size * 2, min_size);
2240 struct wined3d_buffer *buffer;
2241
2242 TRACE("Growing index buffer to %u bytes\n", size);
2243
2244 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2245 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2246 if (FAILED(hr))
2247 {
2248 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2249 return hr;
2250 }
2251
2252 if (device->index_buffer)
2253 wined3d_buffer_decref(device->index_buffer);
2254
2255 device->index_buffer = buffer;
2256 device->index_buffer_size = size;
2257 device->index_buffer_pos = 0;
2258 }
2259 return D3D_OK;
2260 }
2261
2262 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2263 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2264 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2265 const void *vertex_data, UINT vertex_stride)
2266 {
2267 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2268 HRESULT hr;
2269 BYTE *buffer_data;
2270
2271 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2272 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2273 UINT idx_size = idx_count * idx_fmt_size;
2274 UINT ib_pos;
2275
2276 UINT vtx_size = vertex_count * vertex_stride;
2277 UINT vb_pos, align;
2278
2279 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2280 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2281 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2282 index_data, index_format, vertex_data, vertex_stride);
2283
2284 if (!primitive_count)
2285 {
2286 WARN("primitive_count is 0, returning D3D_OK\n");
2287 return D3D_OK;
2288 }
2289
2290 wined3d_mutex_lock();
2291
2292 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2293 if (FAILED(hr))
2294 goto done;
2295
2296 vb_pos = device->vertex_buffer_pos;
2297 align = vb_pos % vertex_stride;
2298 if (align) align = vertex_stride - align;
2299 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2300 vb_pos = 0;
2301 else
2302 vb_pos += align;
2303
2304 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2305 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2306 if (FAILED(hr))
2307 goto done;
2308 memcpy(buffer_data, vertex_data, vtx_size);
2309 wined3d_buffer_unmap(device->vertex_buffer);
2310 device->vertex_buffer_pos = vb_pos + vtx_size;
2311
2312 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2313 if (FAILED(hr))
2314 goto done;
2315
2316 ib_pos = device->index_buffer_pos;
2317 align = ib_pos % idx_fmt_size;
2318 if (align) align = idx_fmt_size - align;
2319 if (ib_pos + idx_size + align > device->index_buffer_size)
2320 ib_pos = 0;
2321 else
2322 ib_pos += align;
2323
2324 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2325 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2326 if (FAILED(hr))
2327 goto done;
2328 memcpy(buffer_data, index_data, idx_size);
2329 wined3d_buffer_unmap(device->index_buffer);
2330 device->index_buffer_pos = ib_pos + idx_size;
2331
2332 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2333 if (FAILED(hr))
2334 goto done;
2335
2336 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2337 wined3dformat_from_d3dformat(index_format));
2338 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2339
2340 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2341 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2342
2343 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2344 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2345 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2346
2347 done:
2348 wined3d_mutex_unlock();
2349 return hr;
2350 }
2351
2352 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2353 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2354 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2355 {
2356 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2357 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2358 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2359 HRESULT hr;
2360
2361 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2362 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2363
2364 wined3d_mutex_lock();
2365 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2366 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2367 flags, dst_impl->fvf);
2368 wined3d_mutex_unlock();
2369
2370 return hr;
2371 }
2372
2373 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2374 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2375 {
2376 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2377 struct d3d9_vertex_declaration *object;
2378 HRESULT hr;
2379
2380 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2381
2382 if (!declaration)
2383 {
2384 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2385 return D3DERR_INVALIDCALL;
2386 }
2387
2388 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2389 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2390
2391 return hr;
2392 }
2393
2394 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2395 IDirect3DVertexDeclaration9 *declaration)
2396 {
2397 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2398 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2399
2400 TRACE("iface %p, declaration %p.\n", iface, declaration);
2401
2402 wined3d_mutex_lock();
2403 wined3d_device_set_vertex_declaration(device->wined3d_device,
2404 decl_impl ? decl_impl->wined3d_declaration : NULL);
2405 wined3d_mutex_unlock();
2406
2407 return D3D_OK;
2408 }
2409
2410 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2411 IDirect3DVertexDeclaration9 **declaration)
2412 {
2413 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2414 struct wined3d_vertex_declaration *wined3d_declaration;
2415 struct d3d9_vertex_declaration *declaration_impl;
2416
2417 TRACE("iface %p, declaration %p.\n", iface, declaration);
2418
2419 if (!declaration) return D3DERR_INVALIDCALL;
2420
2421 wined3d_mutex_lock();
2422 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2423 {
2424 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2425 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2426 IDirect3DVertexDeclaration9_AddRef(*declaration);
2427 }
2428 else
2429 {
2430 *declaration = NULL;
2431 }
2432 wined3d_mutex_unlock();
2433
2434 TRACE("Returning %p.\n", *declaration);
2435 return D3D_OK;
2436 }
2437
2438 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2439 {
2440 struct wined3d_vertex_declaration *wined3d_declaration;
2441 struct fvf_declaration *fvf_decls = device->fvf_decls;
2442 struct d3d9_vertex_declaration *d3d9_declaration;
2443 D3DVERTEXELEMENT9 *elements;
2444 int p, low, high; /* deliberately signed */
2445 HRESULT hr;
2446
2447 TRACE("Searching for declaration for fvf %08x... ", fvf);
2448
2449 low = 0;
2450 high = device->fvf_decl_count - 1;
2451 while (low <= high)
2452 {
2453 p = (low + high) >> 1;
2454 TRACE("%d ", p);
2455
2456 if (fvf_decls[p].fvf == fvf)
2457 {
2458 TRACE("found %p.\n", fvf_decls[p].decl);
2459 return fvf_decls[p].decl;
2460 }
2461
2462 if (fvf_decls[p].fvf < fvf)
2463 low = p + 1;
2464 else
2465 high = p - 1;
2466 }
2467 TRACE("not found. Creating and inserting at position %d.\n", low);
2468
2469 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2470 return NULL;
2471
2472 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2473 HeapFree(GetProcessHeap(), 0, elements);
2474 if (FAILED(hr))
2475 return NULL;
2476
2477 if (device->fvf_decl_size == device->fvf_decl_count)
2478 {
2479 UINT grow = max(device->fvf_decl_size / 2, 8);
2480
2481 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2482 if (!fvf_decls)
2483 {
2484 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2485 return NULL;
2486 }
2487 device->fvf_decls = fvf_decls;
2488 device->fvf_decl_size += grow;
2489 }
2490
2491 d3d9_declaration->fvf = fvf;
2492 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2493 wined3d_vertex_declaration_incref(wined3d_declaration);
2494 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2495
2496 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2497 fvf_decls[low].decl = wined3d_declaration;
2498 fvf_decls[low].fvf = fvf;
2499 ++device->fvf_decl_count;
2500
2501 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2502
2503 return wined3d_declaration;
2504 }
2505
2506 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2507 {
2508 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2509 struct wined3d_vertex_declaration *decl;
2510
2511 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2512
2513 if (!fvf)
2514 {
2515 WARN("%#x is not a valid FVF.\n", fvf);
2516 return D3D_OK;
2517 }
2518
2519 wined3d_mutex_lock();
2520 if (!(decl = device_get_fvf_declaration(device, fvf)))
2521 {
2522 wined3d_mutex_unlock();
2523 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2524 return D3DERR_DRIVERINTERNALERROR;
2525 }
2526
2527 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2528 wined3d_mutex_unlock();
2529
2530 return D3D_OK;
2531 }
2532
2533 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2534 {
2535 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2536 struct wined3d_vertex_declaration *wined3d_declaration;
2537 struct d3d9_vertex_declaration *d3d9_declaration;
2538
2539 TRACE("iface %p, fvf %p.\n", iface, fvf);
2540
2541 wined3d_mutex_lock();
2542 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2543 {
2544 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2545 *fvf = d3d9_declaration->fvf;
2546 }
2547 else
2548 {
2549 *fvf = 0;
2550 }
2551 wined3d_mutex_unlock();
2552
2553 TRACE("Returning FVF %#x.\n", *fvf);
2554
2555 return D3D_OK;
2556 }
2557
2558 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2559 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2560 {
2561 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2562 struct d3d9_vertexshader *object;
2563 HRESULT hr;
2564
2565 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2566
2567 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2568 if (!object)
2569 return E_OUTOFMEMORY;
2570
2571 hr = vertexshader_init(object, device, byte_code);
2572 if (FAILED(hr))
2573 {
2574 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2575 HeapFree(GetProcessHeap(), 0, object);
2576 return hr;
2577 }
2578
2579 TRACE("Created vertex shader %p.\n", object);
2580 *shader = &object->IDirect3DVertexShader9_iface;
2581
2582 return D3D_OK;
2583 }
2584
2585 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2586 {
2587 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2588 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2589
2590 TRACE("iface %p, shader %p.\n", iface, shader);
2591
2592 wined3d_mutex_lock();
2593 wined3d_device_set_vertex_shader(device->wined3d_device,
2594 shader_obj ? shader_obj->wined3d_shader : NULL);
2595 wined3d_mutex_unlock();
2596
2597 return D3D_OK;
2598 }
2599
2600 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2601 {
2602 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2603 struct d3d9_vertexshader *shader_impl;
2604 struct wined3d_shader *wined3d_shader;
2605
2606 TRACE("iface %p, shader %p.\n", iface, shader);
2607
2608 wined3d_mutex_lock();
2609 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2610 {
2611 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2612 *shader = &shader_impl->IDirect3DVertexShader9_iface;
2613 IDirect3DVertexShader9_AddRef(*shader);
2614 }
2615 else
2616 {
2617 *shader = NULL;
2618 }
2619 wined3d_mutex_unlock();
2620
2621 TRACE("Returning %p.\n", *shader);
2622
2623 return D3D_OK;
2624 }
2625
2626 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2627 UINT reg_idx, const float *data, UINT count)
2628 {
2629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2630 HRESULT hr;
2631
2632 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2633
2634 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2635 {
2636 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2637 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2638 return D3DERR_INVALIDCALL;
2639 }
2640
2641 wined3d_mutex_lock();
2642 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2643 wined3d_mutex_unlock();
2644
2645 return hr;
2646 }
2647
2648 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2649 UINT reg_idx, float *data, UINT count)
2650 {
2651 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2652 HRESULT hr;
2653
2654 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2655
2656 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2657 {
2658 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2659 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2660 return D3DERR_INVALIDCALL;
2661 }
2662
2663 wined3d_mutex_lock();
2664 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, reg_idx, data, count);
2665 wined3d_mutex_unlock();
2666
2667 return hr;
2668 }
2669
2670 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2671 UINT reg_idx, const int *data, UINT count)
2672 {
2673 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2674 HRESULT hr;
2675
2676 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2677
2678 wined3d_mutex_lock();
2679 hr = wined3d_device_set_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2680 wined3d_mutex_unlock();
2681
2682 return hr;
2683 }
2684
2685 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2686 UINT reg_idx, int *data, UINT count)
2687 {
2688 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2689 HRESULT hr;
2690
2691 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2692
2693 wined3d_mutex_lock();
2694 hr = wined3d_device_get_vs_consts_i(device->wined3d_device, reg_idx, data, count);
2695 wined3d_mutex_unlock();
2696
2697 return hr;
2698 }
2699
2700 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2701 UINT reg_idx, const BOOL *data, UINT count)
2702 {
2703 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2704 HRESULT hr;
2705
2706 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2707
2708 wined3d_mutex_lock();
2709 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2710 wined3d_mutex_unlock();
2711
2712 return hr;
2713 }
2714
2715 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2716 UINT reg_idx, BOOL *data, UINT count)
2717 {
2718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2719 HRESULT hr;
2720
2721 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2722
2723 wined3d_mutex_lock();
2724 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, data, count);
2725 wined3d_mutex_unlock();
2726
2727 return hr;
2728 }
2729
2730 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
2731 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
2732 {
2733 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2734 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
2735 HRESULT hr;
2736
2737 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2738 iface, stream_idx, buffer, offset, stride);
2739
2740 wined3d_mutex_lock();
2741 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2742 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
2743 wined3d_mutex_unlock();
2744
2745 return hr;
2746 }
2747
2748 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
2749 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
2750 {
2751 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2752 struct d3d9_vertexbuffer *buffer_impl;
2753 struct wined3d_buffer *wined3d_buffer;
2754 HRESULT hr;
2755
2756 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2757 iface, stream_idx, buffer, offset, stride);
2758
2759 if (!buffer)
2760 return D3DERR_INVALIDCALL;
2761
2762 wined3d_mutex_lock();
2763 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
2764 if (SUCCEEDED(hr) && wined3d_buffer)
2765 {
2766 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2767 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
2768 IDirect3DVertexBuffer9_AddRef(*buffer);
2769 }
2770 else
2771 {
2772 if (FAILED(hr))
2773 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
2774 *buffer = NULL;
2775 }
2776 wined3d_mutex_unlock();
2777
2778 return hr;
2779 }
2780
2781 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
2782 {
2783 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2784 HRESULT hr;
2785
2786 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
2787
2788 wined3d_mutex_lock();
2789 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
2790 wined3d_mutex_unlock();
2791
2792 return hr;
2793 }
2794
2795 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
2796 {
2797 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2798 HRESULT hr;
2799
2800 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
2801
2802 wined3d_mutex_lock();
2803 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
2804 wined3d_mutex_unlock();
2805
2806 return hr;
2807 }
2808
2809 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
2810 {
2811 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2812 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
2813
2814 TRACE("iface %p, buffer %p.\n", iface, buffer);
2815
2816 wined3d_mutex_lock();
2817 wined3d_device_set_index_buffer(device->wined3d_device,
2818 ib ? ib->wined3d_buffer : NULL,
2819 ib ? ib->format : WINED3DFMT_UNKNOWN);
2820 wined3d_mutex_unlock();
2821
2822 return D3D_OK;
2823 }
2824
2825 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
2826 {
2827 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2828 enum wined3d_format_id wined3d_format;
2829 struct wined3d_buffer *wined3d_buffer;
2830 struct d3d9_indexbuffer *buffer_impl;
2831
2832 TRACE("iface %p, buffer %p.\n", iface, buffer);
2833
2834 if (!buffer)
2835 return D3DERR_INVALIDCALL;
2836
2837 wined3d_mutex_lock();
2838 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2839 {
2840 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2841 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
2842 IDirect3DIndexBuffer9_AddRef(*buffer);
2843 }
2844 else
2845 {
2846 *buffer = NULL;
2847 }
2848 wined3d_mutex_unlock();
2849
2850 return D3D_OK;
2851 }
2852
2853 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
2854 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2855 {
2856 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2857 struct d3d9_pixelshader *object;
2858 HRESULT hr;
2859
2860 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2861
2862 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2863 if (!object)
2864 {
2865 FIXME("Failed to allocate pixel shader memory.\n");
2866 return E_OUTOFMEMORY;
2867 }
2868
2869 hr = pixelshader_init(object, device, byte_code);
2870 if (FAILED(hr))
2871 {
2872 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2873 HeapFree(GetProcessHeap(), 0, object);
2874 return hr;
2875 }
2876
2877 TRACE("Created pixel shader %p.\n", object);
2878 *shader = &object->IDirect3DPixelShader9_iface;
2879
2880 return D3D_OK;
2881 }
2882
2883 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
2884 {
2885 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2886 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2887
2888 TRACE("iface %p, shader %p.\n", iface, shader);
2889
2890 wined3d_mutex_lock();
2891 wined3d_device_set_pixel_shader(device->wined3d_device,
2892 shader_obj ? shader_obj->wined3d_shader : NULL);
2893 wined3d_mutex_unlock();
2894
2895 return D3D_OK;
2896 }
2897
2898 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
2899 {
2900 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2901 struct d3d9_pixelshader *shader_impl;
2902 struct wined3d_shader *wined3d_shader;
2903
2904 TRACE("iface %p, shader %p.\n", iface, shader);
2905
2906 if (!shader) return D3DERR_INVALIDCALL;
2907
2908 wined3d_mutex_lock();
2909 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
2910 {
2911 shader_impl = wined3d_shader_get_parent(wined3d_shader);
2912 *shader = &shader_impl->IDirect3DPixelShader9_iface;
2913 IDirect3DPixelShader9_AddRef(*shader);
2914 }
2915 else
2916 {
2917 *shader = NULL;
2918 }
2919 wined3d_mutex_unlock();
2920
2921 TRACE("Returning %p.\n", *shader);
2922
2923 return D3D_OK;
2924 }
2925
2926 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2927 UINT reg_idx, const float *data, UINT count)
2928 {
2929 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2930 HRESULT hr;
2931
2932 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2933
2934 wined3d_mutex_lock();
2935 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2936 wined3d_mutex_unlock();
2937
2938 return hr;
2939 }
2940
2941 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2942 UINT reg_idx, float *data, UINT count)
2943 {
2944 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2945 HRESULT hr;
2946
2947 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2948
2949 wined3d_mutex_lock();
2950 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, reg_idx, data, count);
2951 wined3d_mutex_unlock();
2952
2953 return hr;
2954 }
2955
2956 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2957 UINT reg_idx, const int *data, UINT count)
2958 {
2959 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2960 HRESULT hr;
2961
2962 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2963
2964 wined3d_mutex_lock();
2965 hr = wined3d_device_set_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2966 wined3d_mutex_unlock();
2967
2968 return hr;
2969 }
2970
2971 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2972 UINT reg_idx, int *data, UINT count)
2973 {
2974 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2975 HRESULT hr;
2976
2977 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2978
2979 wined3d_mutex_lock();
2980 hr = wined3d_device_get_ps_consts_i(device->wined3d_device, reg_idx, data, count);
2981 wined3d_mutex_unlock();
2982
2983 return hr;
2984 }
2985
2986 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2987 UINT reg_idx, const BOOL *data, UINT count)
2988 {
2989 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2990 HRESULT hr;
2991
2992 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2993
2994 wined3d_mutex_lock();
2995 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, data, count);
2996 wined3d_mutex_unlock();
2997
2998 return hr;
2999 }
3000
3001 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3002 UINT reg_idx, BOOL *data, UINT count)
3003 {
3004 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3005 HRESULT hr;
3006
3007 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3008
3009 wined3d_mutex_lock();
3010 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, data, count);
3011 wined3d_mutex_unlock();
3012
3013 return hr;
3014 }
3015
3016 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3017 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3018 {
3019 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3020 iface, handle, segment_count, patch_info);
3021 return D3D_OK;
3022 }
3023
3024 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3025 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3026 {
3027 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3028 iface, handle, segment_count, patch_info);
3029 return D3D_OK;
3030 }
3031
3032 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3033 {
3034 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3035 return D3DERR_INVALIDCALL;
3036 }
3037
3038 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3039 {
3040 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3041 struct d3d9_query *object;
3042 HRESULT hr;
3043
3044 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3045
3046 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3047 if (!object)
3048 return E_OUTOFMEMORY;
3049
3050 hr = query_init(object, device, type);
3051 if (FAILED(hr))
3052 {
3053 WARN("Failed to initialize query, hr %#x.\n", hr);
3054 HeapFree(GetProcessHeap(), 0, object);
3055 return hr;
3056 }
3057
3058 TRACE("Created query %p.\n", object);
3059 if (query) *query = &object->IDirect3DQuery9_iface;
3060 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3061
3062 return D3D_OK;
3063 }
3064
3065 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3066 UINT width, UINT height, float *rows, float *columns)
3067 {
3068 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3069 iface, width, height, rows, columns);
3070
3071 return E_NOTIMPL;
3072 }
3073
3074 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3075 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3076 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3077 {
3078 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
3079 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3080 iface, src_surface, dst_surface, src_descs, rect_count,
3081 dst_descs, operation, offset_x, offset_y);
3082
3083 return E_NOTIMPL;
3084 }
3085
3086 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3087 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3088 const RGNDATA *dirty_region, DWORD flags)
3089 {
3090 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3091 HRESULT hr;
3092
3093 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3094 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3095 dst_window_override, dirty_region, flags);
3096
3097 if (device->device_state != D3D9_DEVICE_STATE_OK)
3098 return S_PRESENT_OCCLUDED;
3099
3100 wined3d_mutex_lock();
3101 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
3102 dst_window_override, dirty_region, flags);
3103 wined3d_mutex_unlock();
3104
3105 return hr;
3106 }
3107
3108 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3109 {
3110 FIXME("iface %p, priority %p stub!\n", iface, priority);
3111
3112 return E_NOTIMPL;
3113 }
3114
3115 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3116 {
3117 FIXME("iface %p, priority %d stub!\n", iface, priority);
3118
3119 return E_NOTIMPL;
3120 }
3121
3122 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3123 {
3124 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3125
3126 return E_NOTIMPL;
3127 }
3128
3129 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3130 IDirect3DResource9 **resources, UINT32 resource_count)
3131 {
3132 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3133 iface, resources, resource_count);
3134
3135 return E_NOTIMPL;
3136 }
3137
3138 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3139 {
3140 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3141
3142 return E_NOTIMPL;
3143 }
3144
3145 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3146 {
3147 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3148
3149 *max_latency = 2;
3150
3151 return E_NOTIMPL;
3152 }
3153
3154 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3155 {
3156 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3157 struct wined3d_swapchain_desc swapchain_desc;
3158 struct wined3d_swapchain *swapchain;
3159
3160 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3161
3162 wined3d_mutex_lock();
3163 swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3164 wined3d_swapchain_get_desc(swapchain, &swapchain_desc);
3165 wined3d_mutex_unlock();
3166
3167 if (swapchain_desc.windowed)
3168 return D3D_OK;
3169
3170 /* FIXME: This is actually supposed to check if any other device is in
3171 * fullscreen mode. */
3172 if (dst_window != swapchain_desc.device_window)
3173 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3174
3175 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3176 }
3177
3178 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3179 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3180 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3181 {
3182 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3183 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3184 iface, width, height, format, multisample_type, multisample_quality,
3185 lockable, surface, shared_handle, usage);
3186
3187 *surface = NULL;
3188 if (shared_handle)
3189 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3190
3191 return E_NOTIMPL;
3192 }
3193
3194 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3195 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3196 HANDLE *shared_handle, DWORD usage)
3197 {
3198 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3199 iface, width, height, format, pool, surface, shared_handle, usage);
3200
3201 return E_NOTIMPL;
3202 }
3203
3204 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3205 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3206 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3207 {
3208 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
3209 "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3210 iface, width, height, format, multisample_type, multisample_quality,
3211 discard, surface, shared_handle, usage);
3212
3213 *surface = NULL;
3214 if (shared_handle)
3215 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3216
3217 return E_NOTIMPL;
3218 }
3219
3220 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3221 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3222 {
3223 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3224 struct wined3d_swapchain_desc swapchain_desc;
3225 struct wined3d_display_mode wined3d_mode;
3226 HRESULT hr;
3227
3228 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3229
3230 if (mode)
3231 {
3232 wined3d_mode.width = mode->Width;
3233 wined3d_mode.height = mode->Height;
3234 wined3d_mode.refresh_rate = mode->RefreshRate;
3235 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
3236 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
3237 }
3238
3239 wined3d_mutex_lock();
3240
3241 if (device->vertex_buffer)
3242 {
3243 wined3d_buffer_decref(device->vertex_buffer);
3244 device->vertex_buffer = NULL;
3245 device->vertex_buffer_size = 0;
3246 }
3247 if (device->index_buffer)
3248 {
3249 wined3d_buffer_decref(device->index_buffer);
3250 device->index_buffer = NULL;
3251 device->index_buffer_size = 0;
3252 }
3253
3254 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
3255 hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
3256 mode ? &wined3d_mode : NULL, reset_enum_callback, FALSE);
3257 wined3d_mutex_unlock();
3258
3259 return hr;
3260 }
3261
3262 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3263 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3264 {
3265 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3266 struct wined3d_display_mode wined3d_mode;
3267 HRESULT hr;
3268
3269 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3270 iface, swapchain_idx, mode, rotation);
3271
3272 if (mode->Size != sizeof(*mode))
3273 return D3DERR_INVALIDCALL;
3274
3275 wined3d_mutex_lock();
3276 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3277 (enum wined3d_display_rotation *)rotation);
3278 wined3d_mutex_unlock();
3279
3280 if (SUCCEEDED(hr))
3281 {
3282 mode->Width = wined3d_mode.width;
3283 mode->Height = wined3d_mode.height;
3284 mode->RefreshRate = wined3d_mode.refresh_rate;
3285 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3286 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3287 }
3288
3289 return hr;
3290 }
3291
3292 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3293 {
3294 /* IUnknown */
3295 d3d9_device_QueryInterface,
3296 d3d9_device_AddRef,
3297 d3d9_device_Release,
3298 /* IDirect3DDevice9 */
3299 d3d9_device_TestCooperativeLevel,
3300 d3d9_device_GetAvailableTextureMem,
3301 d3d9_device_EvictManagedResources,
3302 d3d9_device_GetDirect3D,
3303 d3d9_device_GetDeviceCaps,
3304 d3d9_device_GetDisplayMode,
3305 d3d9_device_GetCreationParameters,
3306 d3d9_device_SetCursorProperties,
3307 d3d9_device_SetCursorPosition,
3308 d3d9_device_ShowCursor,
3309 d3d9_device_CreateAdditionalSwapChain,
3310 d3d9_device_GetSwapChain,
3311 d3d9_device_GetNumberOfSwapChains,
3312 d3d9_device_Reset,
3313 d3d9_device_Present,
3314 d3d9_device_GetBackBuffer,
3315 d3d9_device_GetRasterStatus,
3316 d3d9_device_SetDialogBoxMode,
3317 d3d9_device_SetGammaRamp,
3318 d3d9_device_GetGammaRamp,
3319 d3d9_device_CreateTexture,
3320 d3d9_device_CreateVolumeTexture,
3321 d3d9_device_CreateCubeTexture,
3322 d3d9_device_CreateVertexBuffer,
3323 d3d9_device_CreateIndexBuffer,
3324 d3d9_device_CreateRenderTarget,
3325 d3d9_device_CreateDepthStencilSurface,
3326 d3d9_device_UpdateSurface,
3327 d3d9_device_UpdateTexture,
3328 d3d9_device_GetRenderTargetData,
3329 d3d9_device_GetFrontBufferData,
3330 d3d9_device_StretchRect,
3331 d3d9_device_ColorFill,
3332 d3d9_device_CreateOffscreenPlainSurface,
3333 d3d9_device_SetRenderTarget,
3334 d3d9_device_GetRenderTarget,
3335 d3d9_device_SetDepthStencilSurface,
3336 d3d9_device_GetDepthStencilSurface,
3337 d3d9_device_BeginScene,
3338 d3d9_device_EndScene,
3339 d3d9_device_Clear,
3340 d3d9_device_SetTransform,
3341 d3d9_device_GetTransform,
3342 d3d9_device_MultiplyTransform,
3343 d3d9_device_SetViewport,
3344 d3d9_device_GetViewport,
3345 d3d9_device_SetMaterial,
3346 d3d9_device_GetMaterial,
3347 d3d9_device_SetLight,
3348 d3d9_device_GetLight,
3349 d3d9_device_LightEnable,
3350 d3d9_device_GetLightEnable,
3351 d3d9_device_SetClipPlane,
3352 d3d9_device_GetClipPlane,
3353 d3d9_device_SetRenderState,
3354 d3d9_device_GetRenderState,
3355 d3d9_device_CreateStateBlock,
3356 d3d9_device_BeginStateBlock,
3357 d3d9_device_EndStateBlock,
3358 d3d9_device_SetClipStatus,
3359 d3d9_device_GetClipStatus,
3360 d3d9_device_GetTexture,
3361 d3d9_device_SetTexture,
3362 d3d9_device_GetTextureStageState,
3363 d3d9_device_SetTextureStageState,
3364 d3d9_device_GetSamplerState,
3365 d3d9_device_SetSamplerState,
3366 d3d9_device_ValidateDevice,
3367 d3d9_device_SetPaletteEntries,
3368 d3d9_device_GetPaletteEntries,
3369 d3d9_device_SetCurrentTexturePalette,
3370 d3d9_device_GetCurrentTexturePalette,
3371 d3d9_device_SetScissorRect,
3372 d3d9_device_GetScissorRect,
3373 d3d9_device_SetSoftwareVertexProcessing,
3374 d3d9_device_GetSoftwareVertexProcessing,
3375 d3d9_device_SetNPatchMode,
3376 d3d9_device_GetNPatchMode,
3377 d3d9_device_DrawPrimitive,
3378 d3d9_device_DrawIndexedPrimitive,
3379 d3d9_device_DrawPrimitiveUP,
3380 d3d9_device_DrawIndexedPrimitiveUP,
3381 d3d9_device_ProcessVertices,
3382 d3d9_device_CreateVertexDeclaration,
3383 d3d9_device_SetVertexDeclaration,
3384 d3d9_device_GetVertexDeclaration,
3385 d3d9_device_SetFVF,
3386 d3d9_device_GetFVF,
3387 d3d9_device_CreateVertexShader,
3388 d3d9_device_SetVertexShader,
3389 d3d9_device_GetVertexShader,
3390 d3d9_device_SetVertexShaderConstantF,
3391 d3d9_device_GetVertexShaderConstantF,
3392 d3d9_device_SetVertexShaderConstantI,
3393 d3d9_device_GetVertexShaderConstantI,
3394 d3d9_device_SetVertexShaderConstantB,
3395 d3d9_device_GetVertexShaderConstantB,
3396 d3d9_device_SetStreamSource,
3397 d3d9_device_GetStreamSource,
3398 d3d9_device_SetStreamSourceFreq,
3399 d3d9_device_GetStreamSourceFreq,
3400 d3d9_device_SetIndices,
3401 d3d9_device_GetIndices,
3402 d3d9_device_CreatePixelShader,
3403 d3d9_device_SetPixelShader,
3404 d3d9_device_GetPixelShader,
3405 d3d9_device_SetPixelShaderConstantF,
3406 d3d9_device_GetPixelShaderConstantF,
3407 d3d9_device_SetPixelShaderConstantI,
3408 d3d9_device_GetPixelShaderConstantI,
3409 d3d9_device_SetPixelShaderConstantB,
3410 d3d9_device_GetPixelShaderConstantB,
3411 d3d9_device_DrawRectPatch,
3412 d3d9_device_DrawTriPatch,
3413 d3d9_device_DeletePatch,
3414 d3d9_device_CreateQuery,
3415 /* IDirect3DDevice9Ex */
3416 d3d9_device_SetConvolutionMonoKernel,
3417 d3d9_device_ComposeRects,
3418 d3d9_device_PresentEx,
3419 d3d9_device_GetGPUThreadPriority,
3420 d3d9_device_SetGPUThreadPriority,
3421 d3d9_device_WaitForVBlank,
3422 d3d9_device_CheckResourceResidency,
3423 d3d9_device_SetMaximumFrameLatency,
3424 d3d9_device_GetMaximumFrameLatency,
3425 d3d9_device_CheckDeviceState,
3426 d3d9_device_CreateRenderTargetEx,
3427 d3d9_device_CreateOffscreenPlainSurfaceEx,
3428 d3d9_device_CreateDepthStencilSurfaceEx,
3429 d3d9_device_ResetEx,
3430 d3d9_device_GetDisplayModeEx,
3431 };
3432
3433 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3434 {
3435 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3436 }
3437
3438 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3439 struct wined3d_device *device)
3440 {
3441 TRACE("device_parent %p, device %p.\n", device_parent, device);
3442 }
3443
3444 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3445 {
3446 TRACE("device_parent %p.\n", device_parent);
3447 }
3448
3449 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3450 {
3451 struct d3d9_device *device = device_from_device_parent(device_parent);
3452
3453 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3454
3455 if (!device->d3d_parent)
3456 return;
3457
3458 if (!activate)
3459 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3460 else if (device->d3d_parent->extended)
3461 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3462 else
3463 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3464 }
3465
3466 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3467 void *container_parent, struct wined3d_surface *surface, void **parent,
3468 const struct wined3d_parent_ops **parent_ops)
3469 {
3470 struct d3d9_surface *d3d_surface;
3471
3472 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3473 device_parent, container_parent, surface, parent, parent_ops);
3474
3475 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3476 return E_OUTOFMEMORY;
3477
3478 surface_init(d3d_surface, container_parent, surface, parent_ops);
3479 *parent = d3d_surface;
3480 TRACE("Created surface %p.\n", d3d_surface);
3481
3482 return D3D_OK;
3483 }
3484
3485 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3486 void *container_parent, struct wined3d_volume *volume, void **parent,
3487 const struct wined3d_parent_ops **parent_ops)
3488 {
3489 struct d3d9_volume *d3d_volume;
3490
3491 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3492 device_parent, container_parent, volume, parent, parent_ops);
3493
3494 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3495 return E_OUTOFMEMORY;
3496
3497 volume_init(d3d_volume, container_parent, volume, parent_ops);
3498 *parent = d3d_volume;
3499 TRACE("Created volume %p.\n", d3d_volume);
3500
3501 return D3D_OK;
3502 }
3503
3504 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3505 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3506 {
3507 struct d3d9_device *device = device_from_device_parent(device_parent);
3508 struct wined3d_resource_desc texture_desc;
3509 struct d3d9_surface *d3d_surface;
3510 struct wined3d_texture *texture;
3511 HRESULT hr;
3512
3513 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3514 device_parent, container_parent, desc, surface);
3515
3516 if (container_parent == device_parent)
3517 container_parent = &device->IDirect3DDevice9Ex_iface;
3518
3519 texture_desc = *desc;
3520 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3521 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3522 WINED3D_SURFACE_MAPPABLE, container_parent, &d3d9_null_wined3d_parent_ops, &texture)))
3523 {
3524 WARN("Failed to create texture, hr %#x.\n", hr);
3525 return hr;
3526 }
3527
3528 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3529 wined3d_surface_incref(*surface);
3530 wined3d_texture_decref(texture);
3531
3532 d3d_surface = wined3d_surface_get_parent(*surface);
3533 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3534
3535 return hr;
3536 }
3537
3538 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3539 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3540 {
3541 struct d3d9_device *device = device_from_device_parent(device_parent);
3542 struct d3d9_swapchain *d3d_swapchain;
3543 HRESULT hr;
3544
3545 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3546
3547 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3548 if (FAILED(hr))
3549 {
3550 WARN("Failed to create swapchain, hr %#x.\n", hr);
3551 *swapchain = NULL;
3552 return hr;
3553 }
3554
3555 *swapchain = d3d_swapchain->wined3d_swapchain;
3556 wined3d_swapchain_incref(*swapchain);
3557 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3558
3559 return hr;
3560 }
3561
3562 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3563 {
3564 device_parent_wined3d_device_created,
3565 device_parent_mode_changed,
3566 device_parent_activate,
3567 device_parent_surface_created,
3568 device_parent_volume_created,
3569 device_parent_create_swapchain_surface,
3570 device_parent_create_swapchain,
3571 };
3572
3573 static void setup_fpu(void)
3574 {
3575 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3576 WORD cw;
3577 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3578 cw = (cw & ~0xf3f) | 0x3f;
3579 __asm__ volatile ("fldcw %0" : : "m" (cw));
3580 #elif defined(__i386__) && defined(_MSC_VER)
3581 WORD cw;
3582 __asm fnstcw cw;
3583 cw = (cw & ~0xf3f) | 0x3f;
3584 __asm fldcw cw;
3585 #else
3586 FIXME("FPU setup not implemented for this platform.\n");
3587 #endif
3588 }
3589
3590 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3591 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3592 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3593 {
3594 struct wined3d_swapchain_desc *swapchain_desc;
3595 UINT i, count = 1;
3596 HRESULT hr;
3597
3598 if (mode)
3599 FIXME("Ignoring display mode.\n");
3600
3601 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3602 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3603 device->refcount = 1;
3604
3605 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3606
3607 wined3d_mutex_lock();
3608 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3609 &device->device_parent, &device->wined3d_device);
3610 if (FAILED(hr))
3611 {
3612 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3613 wined3d_mutex_unlock();
3614 return hr;
3615 }
3616
3617 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3618 {
3619 WINED3DCAPS caps;
3620
3621 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3622 count = caps.NumberOfAdaptersInGroup;
3623 }
3624
3625 if (flags & D3DCREATE_MULTITHREADED)
3626 wined3d_device_set_multithreaded(device->wined3d_device);
3627
3628 if (!parameters->Windowed)
3629 {
3630 if (!focus_window)
3631 focus_window = parameters->hDeviceWindow;
3632 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3633 {
3634 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3635 wined3d_device_decref(device->wined3d_device);
3636 wined3d_mutex_unlock();
3637 return hr;
3638 }
3639
3640 for (i = 0; i < count; ++i)
3641 {
3642 HWND device_window = parameters[i].hDeviceWindow;
3643
3644 if (!device_window) device_window = focus_window;
3645 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3646 parameters[i].BackBufferWidth,
3647 parameters[i].BackBufferHeight);
3648 }
3649 }
3650
3651 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3652 if (!swapchain_desc)
3653 {
3654 ERR("Failed to allocate wined3d parameters.\n");
3655 wined3d_device_decref(device->wined3d_device);
3656 wined3d_mutex_unlock();
3657 return E_OUTOFMEMORY;
3658 }
3659
3660 for (i = 0; i < count; ++i)
3661 {
3662 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i]);
3663 }
3664
3665 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3666 if (FAILED(hr))
3667 {
3668 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3669 wined3d_device_release_focus_window(device->wined3d_device);
3670 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3671 wined3d_device_decref(device->wined3d_device);
3672 wined3d_mutex_unlock();
3673 return hr;
3674 }
3675
3676 wined3d_mutex_unlock();
3677
3678 for (i = 0; i < count; ++i)
3679 {
3680 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
3681 }
3682 HeapFree(GetProcessHeap(), 0, swapchain_desc);
3683
3684 /* Initialize the converted declaration array. This creates a valid pointer
3685 * and when adding decls HeapReAlloc() can be used without further checking. */
3686 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
3687 if (!device->fvf_decls)
3688 {
3689 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3690 wined3d_mutex_lock();
3691 wined3d_device_uninit_3d(device->wined3d_device);
3692 wined3d_device_release_focus_window(device->wined3d_device);
3693 wined3d_device_decref(device->wined3d_device);
3694 wined3d_mutex_unlock();
3695 return E_OUTOFMEMORY;
3696 }
3697
3698 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3699 device->d3d_parent = parent;
3700
3701 return D3D_OK;
3702 }