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