Synchronize with trunk r58457.
[reactos.git] / dll / directx / wine / d3d8 / device.c
1 /*
2 * IDirect3DDevice8 implementation
3 *
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <config.h>
23
24 //#include <math.h>
25 //#include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 //#include "windef.h"
30 //#include "winbase.h"
31 //#include "winuser.h"
32 //#include "wingdi.h"
33 //#include "wine/debug.h"
34
35 #include "d3d8_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
38
39 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
40 {
41 BYTE *c = (BYTE *)&format;
42
43 /* Don't translate FOURCC formats */
44 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
45
46 switch(format)
47 {
48 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
49 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
50 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
51 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
52 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
53 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
54 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
55 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
56 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
57 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
58 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
59 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
60 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
61 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
73 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
74 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
75 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
76 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
77 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
78 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
79 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
80 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
81 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
82 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
83 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
84 default:
85 FIXME("Unhandled wined3d format %#x.\n", format);
86 return D3DFMT_UNKNOWN;
87 }
88 }
89
90 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
91 {
92 BYTE *c = (BYTE *)&format;
93
94 /* Don't translate FOURCC formats */
95 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
96
97 switch(format)
98 {
99 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
100 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
101 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
102 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
103 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
104 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
105 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
106 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
107 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
108 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
109 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
110 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
111 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
112 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
113 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
114 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
115 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
116 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
117 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
118 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
119 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
120 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
121 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
122 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
123 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
124 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
125 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
126 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
127 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
128 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
129 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
130 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
131 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
132 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
133 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
134 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
135 default:
136 FIXME("Unhandled D3DFORMAT %#x\n", format);
137 return WINED3DFMT_UNKNOWN;
138 }
139 }
140
141 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
142 {
143 switch(primitive_type)
144 {
145 case D3DPT_POINTLIST:
146 return primitive_count;
147
148 case D3DPT_LINELIST:
149 return primitive_count * 2;
150
151 case D3DPT_LINESTRIP:
152 return primitive_count + 1;
153
154 case D3DPT_TRIANGLELIST:
155 return primitive_count * 3;
156
157 case D3DPT_TRIANGLESTRIP:
158 case D3DPT_TRIANGLEFAN:
159 return primitive_count + 2;
160
161 default:
162 FIXME("Unhandled primitive type %#x\n", primitive_type);
163 return 0;
164 }
165 }
166
167 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
168 const struct wined3d_swapchain_desc *swapchain_desc)
169 {
170 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
171 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
172 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
173 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
174 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
175 present_parameters->SwapEffect = swapchain_desc->swap_effect;
176 present_parameters->hDeviceWindow = swapchain_desc->device_window;
177 present_parameters->Windowed = swapchain_desc->windowed;
178 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
179 present_parameters->AutoDepthStencilFormat
180 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
181 present_parameters->Flags = swapchain_desc->flags;
182 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
183 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
184 }
185
186 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
187 const D3DPRESENT_PARAMETERS *present_parameters)
188 {
189 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
190 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
191 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
192 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
193 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
194 swapchain_desc->multisample_quality = 0; /* d3d9 only */
195 swapchain_desc->swap_effect = present_parameters->SwapEffect;
196 swapchain_desc->device_window = present_parameters->hDeviceWindow;
197 swapchain_desc->windowed = present_parameters->Windowed;
198 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
199 swapchain_desc->auto_depth_stencil_format
200 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
201 swapchain_desc->flags = present_parameters->Flags;
202 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
203 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
204 swapchain_desc->auto_restore_display_mode = TRUE;
205 }
206
207 /* Handle table functions */
208 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
209 {
210 struct d3d8_handle_entry *entry;
211
212 if (t->free_entries)
213 {
214 DWORD index = t->free_entries - t->entries;
215 /* Use a free handle */
216 entry = t->free_entries;
217 if (entry->type != D3D8_HANDLE_FREE)
218 {
219 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
220 return D3D8_INVALID_HANDLE;
221 }
222 t->free_entries = entry->object;
223 entry->object = object;
224 entry->type = type;
225
226 return index;
227 }
228
229 if (!(t->entry_count < t->table_size))
230 {
231 /* Grow the table */
232 UINT new_size = t->table_size + (t->table_size >> 1);
233 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
234 0, t->entries, new_size * sizeof(*t->entries));
235 if (!new_entries)
236 {
237 ERR("Failed to grow the handle table.\n");
238 return D3D8_INVALID_HANDLE;
239 }
240 t->entries = new_entries;
241 t->table_size = new_size;
242 }
243
244 entry = &t->entries[t->entry_count];
245 entry->object = object;
246 entry->type = type;
247
248 return t->entry_count++;
249 }
250
251 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
252 {
253 struct d3d8_handle_entry *entry;
254 void *object;
255
256 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
257 {
258 WARN("Invalid handle %u passed.\n", handle);
259 return NULL;
260 }
261
262 entry = &t->entries[handle];
263 if (entry->type != type)
264 {
265 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
266 return NULL;
267 }
268
269 object = entry->object;
270 entry->object = t->free_entries;
271 entry->type = D3D8_HANDLE_FREE;
272 t->free_entries = entry;
273
274 return object;
275 }
276
277 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
278 {
279 struct d3d8_handle_entry *entry;
280
281 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
282 {
283 WARN("Invalid handle %u passed.\n", handle);
284 return NULL;
285 }
286
287 entry = &t->entries[handle];
288 if (entry->type != type)
289 {
290 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
291 return NULL;
292 }
293
294 return entry->object;
295 }
296
297 static inline struct d3d8_device *impl_from_IDirect3DDevice8(IDirect3DDevice8 *iface)
298 {
299 return CONTAINING_RECORD(iface, struct d3d8_device, IDirect3DDevice8_iface);
300 }
301
302 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
303 {
304 TRACE("iface %p, riid %s, out %p.\n",
305 iface, debugstr_guid(riid), out);
306
307 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
308 || IsEqualGUID(riid, &IID_IUnknown))
309 {
310 IDirect3DDevice8_AddRef(iface);
311 *out = iface;
312 return S_OK;
313 }
314
315 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
316
317 *out = NULL;
318 return E_NOINTERFACE;
319 }
320
321 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
322 {
323 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
324 ULONG ref = InterlockedIncrement(&device->ref);
325
326 TRACE("%p increasing refcount to %u.\n", iface, ref);
327
328 return ref;
329 }
330
331 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
332 {
333 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
334 ULONG ref;
335
336 if (device->inDestruction)
337 return 0;
338
339 ref = InterlockedDecrement(&device->ref);
340
341 TRACE("%p decreasing refcount to %u.\n", iface, ref);
342
343 if (!ref)
344 {
345 IDirect3D8 *parent = device->d3d_parent;
346 unsigned i;
347
348 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
349
350 wined3d_mutex_lock();
351
352 device->inDestruction = TRUE;
353
354 for (i = 0; i < device->numConvertedDecls; ++i)
355 {
356 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
357 }
358 HeapFree(GetProcessHeap(), 0, device->decls);
359
360 if (device->vertex_buffer)
361 wined3d_buffer_decref(device->vertex_buffer);
362 if (device->index_buffer)
363 wined3d_buffer_decref(device->index_buffer);
364
365 wined3d_device_uninit_3d(device->wined3d_device);
366 wined3d_device_release_focus_window(device->wined3d_device);
367 wined3d_device_decref(device->wined3d_device);
368 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
369 HeapFree(GetProcessHeap(), 0, device);
370
371 wined3d_mutex_unlock();
372
373 IDirect3D8_Release(parent);
374 }
375 return ref;
376 }
377
378 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
379 {
380 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
381
382 TRACE("iface %p.\n", iface);
383
384 if (device->lost)
385 {
386 TRACE("Device is lost.\n");
387 return D3DERR_DEVICENOTRESET;
388 }
389
390 return D3D_OK;
391 }
392
393 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
394 {
395 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
396 HRESULT hr;
397
398 TRACE("iface %p.\n", iface);
399
400 wined3d_mutex_lock();
401 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
402 wined3d_mutex_unlock();
403
404 return hr;
405 }
406
407 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
408 {
409 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
410
411 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
412
413 if (byte_count)
414 FIXME("Byte count ignored.\n");
415
416 wined3d_mutex_lock();
417 wined3d_device_evict_managed_resources(device->wined3d_device);
418 wined3d_mutex_unlock();
419
420 return D3D_OK;
421 }
422
423 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
424 {
425 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
426
427 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
428
429 if (!d3d8)
430 return D3DERR_INVALIDCALL;
431
432 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
433 }
434
435 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
436 {
437 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
438 WINED3DCAPS *wined3d_caps;
439 HRESULT hr;
440
441 TRACE("iface %p, caps %p.\n", iface, caps);
442
443 if (!caps)
444 return D3DERR_INVALIDCALL;
445
446 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
447 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
448
449 wined3d_mutex_lock();
450 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
451 wined3d_mutex_unlock();
452
453 fixup_caps(wined3d_caps);
454 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
455 HeapFree(GetProcessHeap(), 0, wined3d_caps);
456
457 return hr;
458 }
459
460 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
461 {
462 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
463 struct wined3d_display_mode wined3d_mode;
464 HRESULT hr;
465
466 TRACE("iface %p, mode %p.\n", iface, mode);
467
468 wined3d_mutex_lock();
469 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
470 wined3d_mutex_unlock();
471
472 if (SUCCEEDED(hr))
473 {
474 mode->Width = wined3d_mode.width;
475 mode->Height = wined3d_mode.height;
476 mode->RefreshRate = wined3d_mode.refresh_rate;
477 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
478 }
479
480 return hr;
481 }
482
483 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
484 D3DDEVICE_CREATION_PARAMETERS *parameters)
485 {
486 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
487
488 TRACE("iface %p, parameters %p.\n", iface, parameters);
489
490 wined3d_mutex_lock();
491 wined3d_device_get_creation_parameters(device->wined3d_device,
492 (struct wined3d_device_creation_parameters *)parameters);
493 wined3d_mutex_unlock();
494
495 return D3D_OK;
496 }
497
498 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
499 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
500 {
501 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
502 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
503 HRESULT hr;
504
505 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
506 iface, hotspot_x, hotspot_y, bitmap);
507
508 if (!bitmap)
509 {
510 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
511 return D3DERR_INVALIDCALL;
512 }
513
514 wined3d_mutex_lock();
515 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
516 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
517 wined3d_mutex_unlock();
518
519 return hr;
520 }
521
522 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
523 {
524 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
525
526 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
527
528 wined3d_mutex_lock();
529 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
530 wined3d_mutex_unlock();
531 }
532
533 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
534 {
535 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
536 BOOL ret;
537
538 TRACE("iface %p, show %#x.\n", iface, show);
539
540 wined3d_mutex_lock();
541 ret = wined3d_device_show_cursor(device->wined3d_device, show);
542 wined3d_mutex_unlock();
543
544 return ret;
545 }
546
547 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
548 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
549 {
550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
551 struct wined3d_swapchain_desc desc;
552 struct d3d8_swapchain *object;
553
554 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
555 iface, present_parameters, swapchain);
556
557 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
558 if (SUCCEEDED(d3d8_swapchain_create(device, &desc, &object)))
559 *swapchain = &object->IDirect3DSwapChain8_iface;
560 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
561
562 return D3D_OK;
563 }
564
565 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
566 {
567 struct wined3d_resource_desc desc;
568
569 wined3d_resource_get_desc(resource, &desc);
570 if (desc.pool == WINED3D_POOL_DEFAULT)
571 {
572 struct d3d8_surface *surface;
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_DEVICELOST;
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_DEVICELOST;
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 d3d8_device_Reset(IDirect3DDevice8 *iface,
594 D3DPRESENT_PARAMETERS *present_parameters)
595 {
596 struct d3d8_device *device = impl_from_IDirect3DDevice8(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 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
619 NULL, reset_enum_callback, TRUE)))
620 {
621 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
622 device->lost = FALSE;
623 }
624 else
625 {
626 device->lost = TRUE;
627 }
628 wined3d_mutex_unlock();
629
630 return hr;
631 }
632
633 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
634 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
635 {
636 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
637 HRESULT hr;
638
639 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
640 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
641
642 wined3d_mutex_lock();
643 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
644 dst_window_override, dirty_region, 0);
645 wined3d_mutex_unlock();
646
647 return hr;
648 }
649
650 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
651 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
652 {
653 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
654 struct wined3d_surface *wined3d_surface = NULL;
655 struct d3d8_surface *surface_impl;
656 HRESULT hr;
657
658 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
659 iface, backbuffer_idx, backbuffer_type, backbuffer);
660
661 wined3d_mutex_lock();
662 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
663 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
664 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
665 {
666 surface_impl = wined3d_surface_get_parent(wined3d_surface);
667 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
668 IDirect3DSurface8_AddRef(*backbuffer);
669 }
670 wined3d_mutex_unlock();
671
672 return hr;
673 }
674
675 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
676 {
677 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
678 HRESULT hr;
679
680 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
681
682 wined3d_mutex_lock();
683 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
684 wined3d_mutex_unlock();
685
686 return hr;
687 }
688
689 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
690 {
691 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
692
693 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
694
695 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
696 wined3d_mutex_lock();
697 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
698 wined3d_mutex_unlock();
699 }
700
701 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
702 {
703 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
704
705 TRACE("iface %p, ramp %p.\n", iface, ramp);
706
707 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
708 wined3d_mutex_lock();
709 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
710 wined3d_mutex_unlock();
711 }
712
713 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
714 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
715 D3DPOOL pool, IDirect3DTexture8 **texture)
716 {
717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
718 struct d3d8_texture *object;
719 HRESULT hr;
720
721 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
722 iface, width, height, levels, usage, format, pool, texture);
723
724 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
725 if (!object)
726 return D3DERR_OUTOFVIDEOMEMORY;
727
728 hr = texture_init(object, device, width, height, levels, usage, format, pool);
729 if (FAILED(hr))
730 {
731 WARN("Failed to initialize texture, hr %#x.\n", hr);
732 HeapFree(GetProcessHeap(), 0, object);
733 return hr;
734 }
735
736 TRACE("Created texture %p.\n", object);
737 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
738
739 return D3D_OK;
740 }
741
742 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
743 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
744 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
745 {
746 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
747 struct d3d8_texture *object;
748 HRESULT hr;
749
750 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
751 iface, width, height, depth, levels, usage, format, pool, texture);
752
753 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
754 if (!object)
755 return D3DERR_OUTOFVIDEOMEMORY;
756
757 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
758 if (FAILED(hr))
759 {
760 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
761 HeapFree(GetProcessHeap(), 0, object);
762 return hr;
763 }
764
765 TRACE("Created volume texture %p.\n", object);
766 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
767
768 return D3D_OK;
769 }
770
771 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
772 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
773 {
774 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
775 struct d3d8_texture *object;
776 HRESULT hr;
777
778 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
779 iface, edge_length, levels, usage, format, pool, texture);
780
781 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
782 if (!object)
783 return D3DERR_OUTOFVIDEOMEMORY;
784
785 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
786 if (FAILED(hr))
787 {
788 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
789 HeapFree(GetProcessHeap(), 0, object);
790 return hr;
791 }
792
793 TRACE("Created cube texture %p.\n", object);
794 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
795
796 return hr;
797 }
798
799 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
800 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
801 {
802 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
803 struct d3d8_vertexbuffer *object;
804 HRESULT hr;
805
806 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
807 iface, size, usage, fvf, pool, buffer);
808
809 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
810 if (!object)
811 return D3DERR_OUTOFVIDEOMEMORY;
812
813 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
814 if (FAILED(hr))
815 {
816 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
817 HeapFree(GetProcessHeap(), 0, object);
818 return hr;
819 }
820
821 TRACE("Created vertex buffer %p.\n", object);
822 *buffer = &object->IDirect3DVertexBuffer8_iface;
823
824 return D3D_OK;
825 }
826
827 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
828 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
829 {
830 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
831 struct d3d8_indexbuffer *object;
832 HRESULT hr;
833
834 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
835 iface, size, usage, format, pool, buffer);
836
837 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
838 if (!object)
839 return D3DERR_OUTOFVIDEOMEMORY;
840
841 hr = indexbuffer_init(object, device, size, usage, format, pool);
842 if (FAILED(hr))
843 {
844 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
845 HeapFree(GetProcessHeap(), 0, object);
846 return hr;
847 }
848
849 TRACE("Created index buffer %p.\n", object);
850 *buffer = &object->IDirect3DIndexBuffer8_iface;
851
852 return D3D_OK;
853 }
854
855 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
856 D3DFORMAT format, BOOL lockable, BOOL discard, IDirect3DSurface8 **surface, UINT usage,
857 D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
858 {
859 struct d3d8_surface *object;
860 HRESULT hr;
861
862 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, surface %p,\n"
863 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
864 device, width, height, format, lockable, discard, surface,
865 usage, pool, multisample_type, multisample_quality);
866
867 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
868 {
869 FIXME("Failed to allocate surface memory.\n");
870 return D3DERR_OUTOFVIDEOMEMORY;
871 }
872
873 if (FAILED(hr = surface_init(object, device, width, height, format, lockable,
874 discard, usage, pool, multisample_type, multisample_quality)))
875 {
876 WARN("Failed to initialize surface, hr %#x.\n", hr);
877 HeapFree(GetProcessHeap(), 0, object);
878 return hr;
879 }
880
881 TRACE("Created surface %p.\n", object);
882 *surface = &object->IDirect3DSurface8_iface;
883
884 return D3D_OK;
885 }
886
887 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
888 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
889 IDirect3DSurface8 **surface)
890 {
891 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
892
893 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
894 iface, width, height, format, multisample_type, lockable, surface);
895
896 return d3d8_device_create_surface(device, width, height, format, lockable, FALSE,
897 surface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
898 }
899
900 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
901 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
902 IDirect3DSurface8 **surface)
903 {
904 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
905
906 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
907 iface, width, height, format, multisample_type, surface);
908
909 /* TODO: Verify that Discard is false */
910 return d3d8_device_create_surface(device, width, height, format, TRUE, FALSE,
911 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
912 }
913
914 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
915 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
916 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
917 {
918 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
919
920 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
921 iface, width, height, format, surface);
922
923 return d3d8_device_create_surface(device, width, height, format, TRUE, FALSE,
924 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
925 }
926
927 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
928 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
929 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
930 {
931 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
932 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
933 enum wined3d_format_id src_format, dst_format;
934 struct wined3d_resource_desc wined3d_desc;
935 struct wined3d_resource *wined3d_resource;
936 UINT src_w, src_h;
937 HRESULT hr;
938
939 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
940 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
941
942 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
943 * destination texture is in WINED3D_POOL_DEFAULT. */
944
945 wined3d_mutex_lock();
946 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
947 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
948 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
949 {
950 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
951 wined3d_mutex_unlock();
952 return D3DERR_INVALIDCALL;
953 }
954 src_format = wined3d_desc.format;
955 src_w = wined3d_desc.width;
956 src_h = wined3d_desc.height;
957
958 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
959 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
960 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
961 {
962 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
963 wined3d_mutex_unlock();
964 return D3DERR_INVALIDCALL;
965 }
966 dst_format = wined3d_desc.format;
967
968 /* Check that the source and destination formats match */
969 if (src_format != dst_format && WINED3DFMT_UNKNOWN != dst_format)
970 {
971 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
972 src_surface, dst_surface);
973 wined3d_mutex_unlock();
974 return D3DERR_INVALIDCALL;
975 }
976 else if (WINED3DFMT_UNKNOWN == dst_format)
977 {
978 TRACE("Converting destination surface from WINED3DFMT_UNKNOWN to the source format.\n");
979 if (FAILED(hr = wined3d_surface_update_desc(dst->wined3d_surface, wined3d_desc.width, wined3d_desc.height,
980 src_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality)))
981 {
982 WARN("Failed to update surface desc, hr %#x.\n", hr);
983 wined3d_mutex_unlock();
984 return hr;
985 }
986 }
987
988 /* Quick if complete copy ... */
989 if (!rect_count && !src_rects && !dst_points)
990 {
991 RECT rect = {0, 0, src_w, src_h};
992 wined3d_surface_blt(dst->wined3d_surface, &rect,
993 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
994 }
995 else
996 {
997 unsigned int i;
998 /* Copy rect by rect */
999 if (src_rects && dst_points)
1000 {
1001 for (i = 0; i < rect_count; ++i)
1002 {
1003 UINT w = src_rects[i].right - src_rects[i].left;
1004 UINT h = src_rects[i].bottom - src_rects[i].top;
1005 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1006 dst_points[i].x + w, dst_points[i].y + h};
1007
1008 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1009 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1010 }
1011 }
1012 else
1013 {
1014 for (i = 0; i < rect_count; ++i)
1015 {
1016 UINT w = src_rects[i].right - src_rects[i].left;
1017 UINT h = src_rects[i].bottom - src_rects[i].top;
1018 RECT dst_rect = {0, 0, w, h};
1019
1020 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1021 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1022 }
1023 }
1024 }
1025 wined3d_mutex_unlock();
1026
1027 return WINED3D_OK;
1028 }
1029
1030 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1031 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1032 {
1033 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1034 struct d3d8_texture *src_impl, *dst_impl;
1035 HRESULT hr;
1036
1037 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1038
1039 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1040 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1041
1042 wined3d_mutex_lock();
1043 hr = wined3d_device_update_texture(device->wined3d_device,
1044 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1045 wined3d_mutex_unlock();
1046
1047 return hr;
1048 }
1049
1050 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1051 {
1052 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1053 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1054 HRESULT hr;
1055
1056 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1057
1058 if (!dst_surface)
1059 {
1060 WARN("Invalid destination surface passed.\n");
1061 return D3DERR_INVALIDCALL;
1062 }
1063
1064 wined3d_mutex_lock();
1065 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1066 wined3d_mutex_unlock();
1067
1068 return hr;
1069 }
1070
1071 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1072 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1073 {
1074 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1075 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1076 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1077 struct wined3d_surface *original_ds = NULL;
1078 HRESULT hr = D3D_OK;
1079
1080 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1081
1082 wined3d_mutex_lock();
1083
1084 if (ds_impl)
1085 {
1086 struct wined3d_resource_desc ds_desc, rt_desc;
1087 struct wined3d_resource *wined3d_resource;
1088 struct wined3d_surface *original_rt = NULL;
1089
1090 /* If no render target is passed in check the size against the current RT */
1091 if (!render_target)
1092 {
1093 if (!(original_rt = wined3d_device_get_render_target(device->wined3d_device, 0)))
1094 {
1095 wined3d_mutex_unlock();
1096 return D3DERR_NOTFOUND;
1097 }
1098 wined3d_resource = wined3d_surface_get_resource(original_rt);
1099 }
1100 else
1101 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1102 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1103
1104 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1105 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1106
1107 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1108 {
1109 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1110 wined3d_mutex_unlock();
1111 return D3DERR_INVALIDCALL;
1112 }
1113 }
1114
1115 original_ds = wined3d_device_get_depth_stencil(device->wined3d_device);
1116 wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1117 if (render_target)
1118 {
1119 hr = wined3d_device_set_render_target(device->wined3d_device, 0, rt_impl->wined3d_surface, TRUE);
1120 if (FAILED(hr))
1121 wined3d_device_set_depth_stencil(device->wined3d_device, original_ds);
1122 }
1123
1124 wined3d_mutex_unlock();
1125
1126 return hr;
1127 }
1128
1129 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1130 {
1131 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1132 struct wined3d_surface *wined3d_surface;
1133 struct d3d8_surface *surface_impl;
1134 HRESULT hr;
1135
1136 TRACE("iface %p, render_target %p.\n", iface, render_target);
1137
1138 if (!render_target)
1139 return D3DERR_INVALIDCALL;
1140
1141 wined3d_mutex_lock();
1142 if ((wined3d_surface = wined3d_device_get_render_target(device->wined3d_device, 0)))
1143 {
1144 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1145 *render_target = &surface_impl->IDirect3DSurface8_iface;
1146 IDirect3DSurface8_AddRef(*render_target);
1147 hr = D3D_OK;
1148 }
1149 else
1150 {
1151 ERR("Failed to get wined3d render target.\n");
1152 *render_target = NULL;
1153 hr = D3DERR_NOTFOUND;
1154 }
1155 wined3d_mutex_unlock();
1156
1157 return hr;
1158 }
1159
1160 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1161 {
1162 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1163 struct wined3d_surface *wined3d_surface;
1164 struct d3d8_surface *surface_impl;
1165 HRESULT hr = D3D_OK;
1166
1167 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1168
1169 if (!depth_stencil)
1170 return D3DERR_INVALIDCALL;
1171
1172 wined3d_mutex_lock();
1173 if ((wined3d_surface = wined3d_device_get_depth_stencil(device->wined3d_device)))
1174 {
1175 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1176 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1177 IDirect3DSurface8_AddRef(*depth_stencil);
1178 }
1179 else
1180 {
1181 hr = WINED3DERR_NOTFOUND;
1182 *depth_stencil = NULL;
1183 }
1184 wined3d_mutex_unlock();
1185
1186 return hr;
1187 }
1188
1189 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1190 {
1191 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1192 HRESULT hr;
1193
1194 TRACE("iface %p.\n", iface);
1195
1196 wined3d_mutex_lock();
1197 hr = wined3d_device_begin_scene(device->wined3d_device);
1198 wined3d_mutex_unlock();
1199
1200 return hr;
1201 }
1202
1203 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1204 {
1205 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1206 HRESULT hr;
1207
1208 TRACE("iface %p.\n", iface);
1209
1210 wined3d_mutex_lock();
1211 hr = wined3d_device_end_scene(device->wined3d_device);
1212 wined3d_mutex_unlock();
1213
1214 return hr;
1215 }
1216
1217 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1218 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1219 {
1220 const struct wined3d_color c =
1221 {
1222 ((color >> 16) & 0xff) / 255.0f,
1223 ((color >> 8) & 0xff) / 255.0f,
1224 (color & 0xff) / 255.0f,
1225 ((color >> 24) & 0xff) / 255.0f,
1226 };
1227 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1228 HRESULT hr;
1229
1230 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1231 iface, rect_count, rects, flags, color, z, stencil);
1232
1233 wined3d_mutex_lock();
1234 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1235 wined3d_mutex_unlock();
1236
1237 return hr;
1238 }
1239
1240 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1241 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1242 {
1243 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1244
1245 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1246
1247 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1248 wined3d_mutex_lock();
1249 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1250 wined3d_mutex_unlock();
1251
1252 return D3D_OK;
1253 }
1254
1255 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1256 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1257 {
1258 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1259
1260 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1261
1262 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1263 wined3d_mutex_lock();
1264 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1265 wined3d_mutex_unlock();
1266
1267 return D3D_OK;
1268 }
1269
1270 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1271 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1272 {
1273 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1274
1275 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1276
1277 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1278 wined3d_mutex_lock();
1279 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1280 wined3d_mutex_unlock();
1281
1282 return D3D_OK;
1283 }
1284
1285 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1286 {
1287 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1288
1289 TRACE("iface %p, viewport %p.\n", iface, viewport);
1290
1291 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1292 wined3d_mutex_lock();
1293 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1294 wined3d_mutex_unlock();
1295
1296 return D3D_OK;
1297 }
1298
1299 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1300 {
1301 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1302
1303 TRACE("iface %p, viewport %p.\n", iface, viewport);
1304
1305 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1306 wined3d_mutex_lock();
1307 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1308 wined3d_mutex_unlock();
1309
1310 return D3D_OK;
1311 }
1312
1313 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1314 {
1315 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1316
1317 TRACE("iface %p, material %p.\n", iface, material);
1318
1319 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1320 wined3d_mutex_lock();
1321 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1322 wined3d_mutex_unlock();
1323
1324 return D3D_OK;
1325 }
1326
1327 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1328 {
1329 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1330
1331 TRACE("iface %p, material %p.\n", iface, material);
1332
1333 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1334 wined3d_mutex_lock();
1335 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1336 wined3d_mutex_unlock();
1337
1338 return D3D_OK;
1339 }
1340
1341 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1342 {
1343 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1344 HRESULT hr;
1345
1346 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1347
1348 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1349 wined3d_mutex_lock();
1350 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1351 wined3d_mutex_unlock();
1352
1353 return hr;
1354 }
1355
1356 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1357 {
1358 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1359 HRESULT hr;
1360
1361 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1362
1363 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1364 wined3d_mutex_lock();
1365 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1366 wined3d_mutex_unlock();
1367
1368 return hr;
1369 }
1370
1371 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1372 {
1373 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1374 HRESULT hr;
1375
1376 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1377
1378 wined3d_mutex_lock();
1379 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1380 wined3d_mutex_unlock();
1381
1382 return hr;
1383 }
1384
1385 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1386 {
1387 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1388 HRESULT hr;
1389
1390 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1391
1392 wined3d_mutex_lock();
1393 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1394 wined3d_mutex_unlock();
1395
1396 return hr;
1397 }
1398
1399 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1400 {
1401 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1402 HRESULT hr;
1403
1404 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1405
1406 wined3d_mutex_lock();
1407 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1408 wined3d_mutex_unlock();
1409
1410 return hr;
1411 }
1412
1413 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1414 {
1415 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1416 HRESULT hr;
1417
1418 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1419
1420 wined3d_mutex_lock();
1421 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1422 wined3d_mutex_unlock();
1423
1424 return hr;
1425 }
1426
1427 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1428 D3DRENDERSTATETYPE state, DWORD value)
1429 {
1430 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1431
1432 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1433
1434 wined3d_mutex_lock();
1435 switch (state)
1436 {
1437 case D3DRS_ZBIAS:
1438 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1439 break;
1440
1441 default:
1442 wined3d_device_set_render_state(device->wined3d_device, state, value);
1443 }
1444 wined3d_mutex_unlock();
1445
1446 return D3D_OK;
1447 }
1448
1449 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1450 D3DRENDERSTATETYPE state, DWORD *value)
1451 {
1452 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1453
1454 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1455
1456 wined3d_mutex_lock();
1457 switch (state)
1458 {
1459 case D3DRS_ZBIAS:
1460 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1461 break;
1462
1463 default:
1464 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1465 }
1466 wined3d_mutex_unlock();
1467
1468 return D3D_OK;
1469 }
1470
1471 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1472 {
1473 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1474 HRESULT hr;
1475
1476 TRACE("iface %p.\n", iface);
1477
1478 wined3d_mutex_lock();
1479 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1480 wined3d_mutex_unlock();
1481
1482 return hr;
1483 }
1484
1485 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1486 {
1487 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1488 struct wined3d_stateblock *stateblock;
1489 HRESULT hr;
1490
1491 TRACE("iface %p, token %p.\n", iface, token);
1492
1493 /* Tell wineD3D to endstateblock before anything else (in case we run out
1494 * of memory later and cause locking problems)
1495 */
1496 wined3d_mutex_lock();
1497 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1498 if (FAILED(hr))
1499 {
1500 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1501 wined3d_mutex_unlock();
1502 return hr;
1503 }
1504
1505 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1506 wined3d_mutex_unlock();
1507
1508 if (*token == D3D8_INVALID_HANDLE)
1509 {
1510 ERR("Failed to create a handle\n");
1511 wined3d_mutex_lock();
1512 wined3d_stateblock_decref(stateblock);
1513 wined3d_mutex_unlock();
1514 return E_FAIL;
1515 }
1516 ++*token;
1517
1518 TRACE("Returning %#x (%p).\n", *token, stateblock);
1519
1520 return hr;
1521 }
1522
1523 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1524 {
1525 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1526 struct wined3d_stateblock *stateblock;
1527
1528 TRACE("iface %p, token %#x.\n", iface, token);
1529
1530 if (!token)
1531 return D3D_OK;
1532
1533 wined3d_mutex_lock();
1534 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1535 if (!stateblock)
1536 {
1537 WARN("Invalid handle (%#x) passed.\n", token);
1538 wined3d_mutex_unlock();
1539 return D3DERR_INVALIDCALL;
1540 }
1541 wined3d_stateblock_apply(stateblock);
1542 wined3d_mutex_unlock();
1543
1544 return D3D_OK;
1545 }
1546
1547 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1548 {
1549 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1550 struct wined3d_stateblock *stateblock;
1551
1552 TRACE("iface %p, token %#x.\n", iface, token);
1553
1554 wined3d_mutex_lock();
1555 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1556 if (!stateblock)
1557 {
1558 WARN("Invalid handle (%#x) passed.\n", token);
1559 wined3d_mutex_unlock();
1560 return D3DERR_INVALIDCALL;
1561 }
1562 wined3d_stateblock_capture(stateblock);
1563 wined3d_mutex_unlock();
1564
1565 return D3D_OK;
1566 }
1567
1568 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1569 {
1570 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1571 struct wined3d_stateblock *stateblock;
1572
1573 TRACE("iface %p, token %#x.\n", iface, token);
1574
1575 wined3d_mutex_lock();
1576 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1577
1578 if (!stateblock)
1579 {
1580 WARN("Invalid handle (%#x) passed.\n", token);
1581 wined3d_mutex_unlock();
1582 return D3DERR_INVALIDCALL;
1583 }
1584
1585 if (wined3d_stateblock_decref(stateblock))
1586 {
1587 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1588 }
1589 wined3d_mutex_unlock();
1590
1591 return D3D_OK;
1592 }
1593
1594 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1595 D3DSTATEBLOCKTYPE type, DWORD *handle)
1596 {
1597 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1598 struct wined3d_stateblock *stateblock;
1599 HRESULT hr;
1600
1601 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1602
1603 if (type != D3DSBT_ALL
1604 && type != D3DSBT_PIXELSTATE
1605 && type != D3DSBT_VERTEXSTATE)
1606 {
1607 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1608 return D3DERR_INVALIDCALL;
1609 }
1610
1611 wined3d_mutex_lock();
1612 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1613 if (FAILED(hr))
1614 {
1615 wined3d_mutex_unlock();
1616 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1617 return hr;
1618 }
1619
1620 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1621 wined3d_mutex_unlock();
1622
1623 if (*handle == D3D8_INVALID_HANDLE)
1624 {
1625 ERR("Failed to allocate a handle.\n");
1626 wined3d_mutex_lock();
1627 wined3d_stateblock_decref(stateblock);
1628 wined3d_mutex_unlock();
1629 return E_FAIL;
1630 }
1631 ++*handle;
1632
1633 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1634
1635 return hr;
1636 }
1637
1638 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1639 {
1640 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1641 HRESULT hr;
1642
1643 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1644 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1645
1646 wined3d_mutex_lock();
1647 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1648 wined3d_mutex_unlock();
1649
1650 return hr;
1651 }
1652
1653 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1654 {
1655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1656 HRESULT hr;
1657
1658 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1659
1660 wined3d_mutex_lock();
1661 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1662 wined3d_mutex_unlock();
1663
1664 return hr;
1665 }
1666
1667 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1668 {
1669 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1670 struct wined3d_texture *wined3d_texture;
1671 struct d3d8_texture *texture_impl;
1672
1673 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1674
1675 if (!texture)
1676 return D3DERR_INVALIDCALL;
1677
1678 wined3d_mutex_lock();
1679 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1680 {
1681 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1682 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1683 IDirect3DBaseTexture8_AddRef(*texture);
1684 }
1685 else
1686 {
1687 *texture = NULL;
1688 }
1689 wined3d_mutex_unlock();
1690
1691 return D3D_OK;
1692 }
1693
1694 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1695 {
1696 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1697 struct d3d8_texture *texture_impl;
1698 HRESULT hr;
1699
1700 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1701
1702 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1703
1704 wined3d_mutex_lock();
1705 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1706 texture_impl ? texture_impl->wined3d_texture : NULL);
1707 wined3d_mutex_unlock();
1708
1709 return hr;
1710 }
1711
1712 static const struct tss_lookup
1713 {
1714 BOOL sampler_state;
1715 enum wined3d_texture_stage_state state;
1716 }
1717 tss_lookup[] =
1718 {
1719 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1720 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1721 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1722 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1723 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1724 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1725 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1726 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1727 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1728 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1729 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1730 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1731 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1732 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1733 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1734 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1735 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1736 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1737 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1738 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1739 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1740 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1741 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1742 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1743 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1744 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1745 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1746 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1747 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1748 };
1749
1750 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1751 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1752 {
1753 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1754 const struct tss_lookup *l;
1755
1756 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1757
1758 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1759 {
1760 WARN("Invalid Type %#x passed.\n", Type);
1761 return D3D_OK;
1762 }
1763
1764 l = &tss_lookup[Type];
1765
1766 wined3d_mutex_lock();
1767 if (l->sampler_state)
1768 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1769 else
1770 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1771 wined3d_mutex_unlock();
1772
1773 return D3D_OK;
1774 }
1775
1776 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1777 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1778 {
1779 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1780 const struct tss_lookup *l;
1781
1782 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1783
1784 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1785 {
1786 WARN("Invalid type %#x passed.\n", type);
1787 return D3D_OK;
1788 }
1789
1790 l = &tss_lookup[type];
1791
1792 wined3d_mutex_lock();
1793 if (l->sampler_state)
1794 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1795 else
1796 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1797 wined3d_mutex_unlock();
1798
1799 return D3D_OK;
1800 }
1801
1802 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1803 {
1804 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1805 HRESULT hr;
1806
1807 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1808
1809 wined3d_mutex_lock();
1810 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1811 wined3d_mutex_unlock();
1812
1813 return hr;
1814 }
1815
1816 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1817 DWORD info_id, void *info, DWORD info_size)
1818 {
1819 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1820
1821 return D3D_OK;
1822 }
1823
1824 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1825 UINT palette_idx, const PALETTEENTRY *entries)
1826 {
1827 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1828
1829 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1830 * does not have a d3d8/9-style palette API */
1831
1832 return D3D_OK;
1833 }
1834
1835 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1836 UINT palette_idx, PALETTEENTRY *entries)
1837 {
1838 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1839
1840 return D3DERR_INVALIDCALL;
1841 }
1842
1843 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1844 {
1845 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1846
1847 return D3D_OK;
1848 }
1849
1850 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1851 {
1852 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1853
1854 return D3DERR_INVALIDCALL;
1855 }
1856
1857 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1858 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1859 {
1860 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1861 HRESULT hr;
1862
1863 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1864 iface, primitive_type, start_vertex, primitive_count);
1865
1866 wined3d_mutex_lock();
1867 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1868 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1869 vertex_count_from_primitive_count(primitive_type, primitive_count));
1870 wined3d_mutex_unlock();
1871
1872 return hr;
1873 }
1874
1875 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1876 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1877 UINT start_idx, UINT primitive_count)
1878 {
1879 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1880 HRESULT hr;
1881
1882 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1883 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1884
1885 wined3d_mutex_lock();
1886 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1887 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1888 vertex_count_from_primitive_count(primitive_type, primitive_count));
1889 wined3d_mutex_unlock();
1890
1891 return hr;
1892 }
1893
1894 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
1895
1896 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
1897 {
1898 d3d8_null_wined3d_object_destroyed,
1899 };
1900
1901 /* The caller is responsible for wined3d locking */
1902 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1903 {
1904 HRESULT hr;
1905
1906 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1907 {
1908 UINT size = max(device->vertex_buffer_size * 2, min_size);
1909 struct wined3d_buffer *buffer;
1910
1911 TRACE("Growing vertex buffer to %u bytes\n", size);
1912
1913 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1914 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1915 if (FAILED(hr))
1916 {
1917 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1918 return hr;
1919 }
1920
1921 if (device->vertex_buffer)
1922 wined3d_buffer_decref(device->vertex_buffer);
1923
1924 device->vertex_buffer = buffer;
1925 device->vertex_buffer_size = size;
1926 device->vertex_buffer_pos = 0;
1927 }
1928 return D3D_OK;
1929 }
1930
1931 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1932 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
1933 UINT stride)
1934 {
1935 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1936 HRESULT hr;
1937 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
1938 UINT size = vtx_count * stride;
1939 UINT vb_pos, align;
1940 BYTE *buffer_data;
1941
1942 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1943 iface, primitive_type, primitive_count, data, stride);
1944
1945 if (!primitive_count)
1946 {
1947 WARN("primitive_count is 0, returning D3D_OK\n");
1948 return D3D_OK;
1949 }
1950
1951 wined3d_mutex_lock();
1952 hr = d3d8_device_prepare_vertex_buffer(device, size);
1953 if (FAILED(hr))
1954 goto done;
1955
1956 vb_pos = device->vertex_buffer_pos;
1957 align = vb_pos % stride;
1958 if (align) align = stride - align;
1959 if (vb_pos + size + align > device->vertex_buffer_size)
1960 vb_pos = 0;
1961 else
1962 vb_pos += align;
1963
1964 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
1965 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
1966 if (FAILED(hr))
1967 goto done;
1968 memcpy(buffer_data, data, size);
1969 wined3d_buffer_unmap(device->vertex_buffer);
1970 device->vertex_buffer_pos = vb_pos + size;
1971
1972 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
1973 if (FAILED(hr))
1974 goto done;
1975
1976 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1977 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
1978 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
1979
1980 done:
1981 wined3d_mutex_unlock();
1982 return hr;
1983 }
1984
1985 /* The caller is responsible for wined3d locking */
1986 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
1987 {
1988 HRESULT hr;
1989
1990 if (device->index_buffer_size < min_size || !device->index_buffer)
1991 {
1992 UINT size = max(device->index_buffer_size * 2, min_size);
1993 struct wined3d_buffer *buffer;
1994
1995 TRACE("Growing index buffer to %u bytes\n", size);
1996
1997 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1998 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1999 if (FAILED(hr))
2000 {
2001 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2002 return hr;
2003 }
2004
2005 if (device->index_buffer)
2006 wined3d_buffer_decref(device->index_buffer);
2007
2008 device->index_buffer = buffer;
2009 device->index_buffer_size = size;
2010 device->index_buffer_pos = 0;
2011 }
2012 return D3D_OK;
2013 }
2014
2015 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2016 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2017 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2018 const void *vertex_data, UINT vertex_stride)
2019 {
2020 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2021 HRESULT hr;
2022 BYTE *buffer_data;
2023
2024 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2025 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2026 UINT idx_size = idx_count * idx_fmt_size;
2027 UINT ib_pos;
2028
2029 UINT vtx_size = vertex_count * vertex_stride;
2030 UINT vb_pos, align;
2031
2032 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2033 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2034 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2035 index_data, index_format, vertex_data, vertex_stride);
2036
2037 if (!primitive_count)
2038 {
2039 WARN("primitive_count is 0, returning D3D_OK\n");
2040 return D3D_OK;
2041 }
2042
2043 wined3d_mutex_lock();
2044
2045 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2046 if (FAILED(hr))
2047 goto done;
2048
2049 vb_pos = device->vertex_buffer_pos;
2050 align = vb_pos % vertex_stride;
2051 if (align) align = vertex_stride - align;
2052 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2053 vb_pos = 0;
2054 else
2055 vb_pos += align;
2056
2057 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2058 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2059 if (FAILED(hr))
2060 goto done;
2061 memcpy(buffer_data, vertex_data, vtx_size);
2062 wined3d_buffer_unmap(device->vertex_buffer);
2063 device->vertex_buffer_pos = vb_pos + vtx_size;
2064
2065 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2066 if (FAILED(hr))
2067 goto done;
2068
2069 ib_pos = device->index_buffer_pos;
2070 align = ib_pos % idx_fmt_size;
2071 if (align) align = idx_fmt_size - align;
2072 if (ib_pos + idx_size + align > device->index_buffer_size)
2073 ib_pos = 0;
2074 else
2075 ib_pos += align;
2076
2077 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2078 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2079 if (FAILED(hr))
2080 goto done;
2081 memcpy(buffer_data, index_data, idx_size);
2082 wined3d_buffer_unmap(device->index_buffer);
2083 device->index_buffer_pos = ib_pos + idx_size;
2084
2085 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2086 if (FAILED(hr))
2087 goto done;
2088
2089 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2090 wined3dformat_from_d3dformat(index_format));
2091 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2092
2093 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2094 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2095
2096 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2097 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2098 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2099
2100 done:
2101 wined3d_mutex_unlock();
2102 return hr;
2103 }
2104
2105 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2106 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2107 {
2108 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2109 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2110 HRESULT hr;
2111
2112 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2113 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2114
2115 wined3d_mutex_lock();
2116 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2117 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2118 wined3d_mutex_unlock();
2119
2120 return hr;
2121 }
2122
2123 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2124 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2125 {
2126 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2127 struct d3d8_vertex_shader *object;
2128 DWORD shader_handle;
2129 DWORD handle;
2130 HRESULT hr;
2131
2132 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2133 iface, declaration, byte_code, shader, usage);
2134
2135 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2136 if (!object)
2137 {
2138 *shader = 0;
2139 return E_OUTOFMEMORY;
2140 }
2141
2142 wined3d_mutex_lock();
2143 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2144 wined3d_mutex_unlock();
2145 if (handle == D3D8_INVALID_HANDLE)
2146 {
2147 ERR("Failed to allocate vertex shader handle.\n");
2148 HeapFree(GetProcessHeap(), 0, object);
2149 *shader = 0;
2150 return E_OUTOFMEMORY;
2151 }
2152
2153 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2154
2155 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2156 if (FAILED(hr))
2157 {
2158 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2159 wined3d_mutex_lock();
2160 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2161 wined3d_mutex_unlock();
2162 HeapFree(GetProcessHeap(), 0, object);
2163 *shader = 0;
2164 return hr;
2165 }
2166
2167 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2168 *shader = shader_handle;
2169
2170 return D3D_OK;
2171 }
2172
2173 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2174 {
2175 struct d3d8_vertex_declaration *d3d8_declaration;
2176 struct FvfToDecl *convertedDecls = device->decls;
2177 int p, low, high; /* deliberately signed */
2178 HRESULT hr;
2179
2180 TRACE("Searching for declaration for fvf %08x... ", fvf);
2181
2182 low = 0;
2183 high = device->numConvertedDecls - 1;
2184 while (low <= high)
2185 {
2186 p = (low + high) >> 1;
2187 TRACE("%d ", p);
2188
2189 if (convertedDecls[p].fvf == fvf)
2190 {
2191 TRACE("found %p\n", convertedDecls[p].declaration);
2192 return convertedDecls[p].declaration;
2193 }
2194
2195 if (convertedDecls[p].fvf < fvf)
2196 low = p + 1;
2197 else
2198 high = p - 1;
2199 }
2200 TRACE("not found. Creating and inserting at position %d.\n", low);
2201
2202 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2203 return NULL;
2204
2205 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2206 {
2207 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2208 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2209 return NULL;
2210 }
2211
2212 if (device->declArraySize == device->numConvertedDecls)
2213 {
2214 UINT grow = device->declArraySize / 2;
2215
2216 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2217 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2218 if (!convertedDecls)
2219 {
2220 d3d8_vertex_declaration_destroy(d3d8_declaration);
2221 return NULL;
2222 }
2223 device->decls = convertedDecls;
2224 device->declArraySize += grow;
2225 }
2226
2227 memmove(convertedDecls + low + 1, convertedDecls + low,
2228 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2229 convertedDecls[low].declaration = d3d8_declaration;
2230 convertedDecls[low].fvf = fvf;
2231 ++device->numConvertedDecls;
2232
2233 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2234
2235 return d3d8_declaration;
2236 }
2237
2238 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2239 {
2240 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2241 struct d3d8_vertex_shader *shader_impl;
2242
2243 TRACE("iface %p, shader %#x.\n", iface, shader);
2244
2245 if (VS_HIGHESTFIXEDFXF >= shader)
2246 {
2247 TRACE("Setting FVF, %#x\n", shader);
2248
2249 wined3d_mutex_lock();
2250 wined3d_device_set_vertex_declaration(device->wined3d_device,
2251 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2252 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2253 wined3d_mutex_unlock();
2254
2255 return D3D_OK;
2256 }
2257
2258 TRACE("Setting shader\n");
2259
2260 wined3d_mutex_lock();
2261 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2262 {
2263 WARN("Invalid handle (%#x) passed.\n", shader);
2264 wined3d_mutex_unlock();
2265
2266 return D3DERR_INVALIDCALL;
2267 }
2268
2269 wined3d_device_set_vertex_declaration(device->wined3d_device,
2270 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2271 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2272 wined3d_mutex_unlock();
2273
2274 return D3D_OK;
2275 }
2276
2277 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2278 {
2279 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2280 struct wined3d_vertex_declaration *wined3d_declaration;
2281 struct d3d8_vertex_declaration *d3d8_declaration;
2282
2283 TRACE("iface %p, shader %p.\n", iface, shader);
2284
2285 wined3d_mutex_lock();
2286 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2287 {
2288 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2289 *shader = d3d8_declaration->shader_handle;
2290 }
2291 else
2292 {
2293 *shader = 0;
2294 }
2295 wined3d_mutex_unlock();
2296
2297 TRACE("Returning %#x.\n", *shader);
2298
2299 return D3D_OK;
2300 }
2301
2302 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2303 {
2304 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2305 struct d3d8_vertex_shader *shader_impl;
2306
2307 TRACE("iface %p, shader %#x.\n", iface, shader);
2308
2309 wined3d_mutex_lock();
2310 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2311 {
2312 WARN("Invalid handle (%#x) passed.\n", shader);
2313 wined3d_mutex_unlock();
2314
2315 return D3DERR_INVALIDCALL;
2316 }
2317
2318 if (shader_impl->wined3d_shader
2319 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2320 IDirect3DDevice8_SetVertexShader(iface, 0);
2321
2322 wined3d_mutex_unlock();
2323
2324 d3d8_vertex_shader_destroy(shader_impl);
2325
2326 return D3D_OK;
2327 }
2328
2329 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2330 DWORD start_register, const void *data, DWORD count)
2331 {
2332 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2333 HRESULT hr;
2334
2335 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2336 iface, start_register, data, count);
2337
2338 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2339 {
2340 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2341 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2342 return D3DERR_INVALIDCALL;
2343 }
2344
2345 wined3d_mutex_lock();
2346 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2347 wined3d_mutex_unlock();
2348
2349 return hr;
2350 }
2351
2352 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2353 DWORD start_register, void *data, DWORD count)
2354 {
2355 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2356 HRESULT hr;
2357
2358 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2359 iface, start_register, data, count);
2360
2361 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2362 {
2363 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2364 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2365 return D3DERR_INVALIDCALL;
2366 }
2367
2368 wined3d_mutex_lock();
2369 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2370 wined3d_mutex_unlock();
2371
2372 return hr;
2373 }
2374
2375 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2376 DWORD shader, void *data, DWORD *data_size)
2377 {
2378 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2379 struct d3d8_vertex_declaration *declaration;
2380 struct d3d8_vertex_shader *shader_impl;
2381
2382 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2383 iface, shader, data, data_size);
2384
2385 wined3d_mutex_lock();
2386 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2387 wined3d_mutex_unlock();
2388
2389 if (!shader_impl)
2390 {
2391 WARN("Invalid handle (%#x) passed.\n", shader);
2392 return D3DERR_INVALIDCALL;
2393 }
2394 declaration = shader_impl->vertex_declaration;
2395
2396 if (!data)
2397 {
2398 *data_size = declaration->elements_size;
2399 return D3D_OK;
2400 }
2401
2402 /* MSDN claims that if *data_size is smaller than the required size
2403 * we should write the required size and return D3DERR_MOREDATA.
2404 * That's not actually true. */
2405 if (*data_size < declaration->elements_size)
2406 return D3DERR_INVALIDCALL;
2407
2408 memcpy(data, declaration->elements, declaration->elements_size);
2409
2410 return D3D_OK;
2411 }
2412
2413 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2414 DWORD shader, void *data, DWORD *data_size)
2415 {
2416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2417 struct d3d8_vertex_shader *shader_impl = NULL;
2418 HRESULT hr;
2419
2420 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2421 iface, shader, data, data_size);
2422
2423 wined3d_mutex_lock();
2424 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2425 {
2426 WARN("Invalid handle (%#x) passed.\n", shader);
2427 wined3d_mutex_unlock();
2428
2429 return D3DERR_INVALIDCALL;
2430 }
2431
2432 if (!shader_impl->wined3d_shader)
2433 {
2434 wined3d_mutex_unlock();
2435 *data_size = 0;
2436 return D3D_OK;
2437 }
2438
2439 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2440 wined3d_mutex_unlock();
2441
2442 return hr;
2443 }
2444
2445 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2446 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2447 {
2448 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2449 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2450
2451 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2452
2453 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2454 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2455 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2456 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2457 * problem)
2458 */
2459 wined3d_mutex_lock();
2460 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2461 wined3d_device_set_index_buffer(device->wined3d_device,
2462 ib ? ib->wined3d_buffer : NULL,
2463 ib ? ib->format : WINED3DFMT_UNKNOWN);
2464 wined3d_mutex_unlock();
2465
2466 return D3D_OK;
2467 }
2468
2469 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2470 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2471 {
2472 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2473 enum wined3d_format_id wined3d_format;
2474 struct wined3d_buffer *wined3d_buffer;
2475 struct d3d8_indexbuffer *buffer_impl;
2476
2477 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2478
2479 if (!buffer)
2480 return D3DERR_INVALIDCALL;
2481
2482 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2483 wined3d_mutex_lock();
2484 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2485 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2486 {
2487 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2488 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2489 IDirect3DIndexBuffer8_AddRef(*buffer);
2490 }
2491 else
2492 {
2493 *buffer = NULL;
2494 }
2495 wined3d_mutex_unlock();
2496
2497 return D3D_OK;
2498 }
2499
2500 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2501 const DWORD *byte_code, DWORD *shader)
2502 {
2503 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2504 struct d3d8_pixel_shader *object;
2505 DWORD shader_handle;
2506 DWORD handle;
2507 HRESULT hr;
2508
2509 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2510
2511 if (!shader)
2512 return D3DERR_INVALIDCALL;
2513
2514 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2515 if (!object)
2516 return E_OUTOFMEMORY;
2517
2518 wined3d_mutex_lock();
2519 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2520 wined3d_mutex_unlock();
2521 if (handle == D3D8_INVALID_HANDLE)
2522 {
2523 ERR("Failed to allocate pixel shader handle.\n");
2524 HeapFree(GetProcessHeap(), 0, object);
2525 return E_OUTOFMEMORY;
2526 }
2527
2528 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2529
2530 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2531 if (FAILED(hr))
2532 {
2533 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2534 wined3d_mutex_lock();
2535 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2536 wined3d_mutex_unlock();
2537 HeapFree(GetProcessHeap(), 0, object);
2538 *shader = 0;
2539 return hr;
2540 }
2541
2542 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2543 *shader = shader_handle;
2544
2545 return D3D_OK;
2546 }
2547
2548 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2549 {
2550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2551 struct d3d8_pixel_shader *shader_impl;
2552
2553 TRACE("iface %p, shader %#x.\n", iface, shader);
2554
2555 wined3d_mutex_lock();
2556
2557 if (!shader)
2558 {
2559 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2560 wined3d_mutex_unlock();
2561 return D3D_OK;
2562 }
2563
2564 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2565 {
2566 WARN("Invalid handle (%#x) passed.\n", shader);
2567 wined3d_mutex_unlock();
2568 return D3DERR_INVALIDCALL;
2569 }
2570
2571 TRACE("Setting shader %p.\n", shader_impl);
2572 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2573 wined3d_mutex_unlock();
2574
2575 return D3D_OK;
2576 }
2577
2578 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2579 {
2580 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2581 struct wined3d_shader *object;
2582
2583 TRACE("iface %p, shader %p.\n", iface, shader);
2584
2585 if (!shader)
2586 return D3DERR_INVALIDCALL;
2587
2588 wined3d_mutex_lock();
2589 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2590 {
2591 struct d3d8_pixel_shader *d3d8_shader;
2592 d3d8_shader = wined3d_shader_get_parent(object);
2593 *shader = d3d8_shader->handle;
2594 }
2595 else
2596 {
2597 *shader = 0;
2598 }
2599 wined3d_mutex_unlock();
2600
2601 TRACE("Returning %#x.\n", *shader);
2602
2603 return D3D_OK;
2604 }
2605
2606 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2607 {
2608 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2609 struct d3d8_pixel_shader *shader_impl;
2610
2611 TRACE("iface %p, shader %#x.\n", iface, shader);
2612
2613 wined3d_mutex_lock();
2614
2615 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2616 {
2617 WARN("Invalid handle (%#x) passed.\n", shader);
2618 wined3d_mutex_unlock();
2619 return D3DERR_INVALIDCALL;
2620 }
2621
2622 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2623 IDirect3DDevice8_SetPixelShader(iface, 0);
2624
2625 wined3d_mutex_unlock();
2626
2627 d3d8_pixel_shader_destroy(shader_impl);
2628
2629 return D3D_OK;
2630 }
2631
2632 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2633 DWORD start_register, const void *data, DWORD count)
2634 {
2635 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2636 HRESULT hr;
2637
2638 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2639 iface, start_register, data, count);
2640
2641 wined3d_mutex_lock();
2642 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2643 wined3d_mutex_unlock();
2644
2645 return hr;
2646 }
2647
2648 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2649 DWORD start_register, void *data, DWORD count)
2650 {
2651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2652 HRESULT hr;
2653
2654 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2655 iface, start_register, data, count);
2656
2657 wined3d_mutex_lock();
2658 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2659 wined3d_mutex_unlock();
2660
2661 return hr;
2662 }
2663
2664 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2665 DWORD shader, void *data, DWORD *data_size)
2666 {
2667 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2668 struct d3d8_pixel_shader *shader_impl = NULL;
2669 HRESULT hr;
2670
2671 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2672 iface, shader, data, data_size);
2673
2674 wined3d_mutex_lock();
2675 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2676 {
2677 WARN("Invalid handle (%#x) passed.\n", shader);
2678 wined3d_mutex_unlock();
2679
2680 return D3DERR_INVALIDCALL;
2681 }
2682
2683 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2684 wined3d_mutex_unlock();
2685
2686 return hr;
2687 }
2688
2689 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2690 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2691 {
2692 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2693 iface, handle, segment_count, patch_info);
2694 return D3D_OK;
2695 }
2696
2697 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2698 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2699 {
2700 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2701 iface, handle, segment_count, patch_info);
2702 return D3D_OK;
2703 }
2704
2705 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2706 {
2707 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2708 return D3DERR_INVALIDCALL;
2709 }
2710
2711 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2712 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2713 {
2714 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2715 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2716 HRESULT hr;
2717
2718 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2719 iface, stream_idx, buffer, stride);
2720
2721 wined3d_mutex_lock();
2722 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2723 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2724 wined3d_mutex_unlock();
2725
2726 return hr;
2727 }
2728
2729 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2730 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2731 {
2732 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2733 struct d3d8_vertexbuffer *buffer_impl;
2734 struct wined3d_buffer *wined3d_buffer = NULL;
2735 HRESULT hr;
2736
2737 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2738 iface, stream_idx, buffer, stride);
2739
2740 if (!buffer)
2741 return D3DERR_INVALIDCALL;
2742
2743 wined3d_mutex_lock();
2744 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2745 if (SUCCEEDED(hr) && wined3d_buffer)
2746 {
2747 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2748 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2749 IDirect3DVertexBuffer8_AddRef(*buffer);
2750 wined3d_buffer_decref(wined3d_buffer);
2751 }
2752 else
2753 {
2754 if (FAILED(hr))
2755 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2756 *buffer = NULL;
2757 }
2758 wined3d_mutex_unlock();
2759
2760 return hr;
2761 }
2762
2763 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2764 {
2765 d3d8_device_QueryInterface,
2766 d3d8_device_AddRef,
2767 d3d8_device_Release,
2768 d3d8_device_TestCooperativeLevel,
2769 d3d8_device_GetAvailableTextureMem,
2770 d3d8_device_ResourceManagerDiscardBytes,
2771 d3d8_device_GetDirect3D,
2772 d3d8_device_GetDeviceCaps,
2773 d3d8_device_GetDisplayMode,
2774 d3d8_device_GetCreationParameters,
2775 d3d8_device_SetCursorProperties,
2776 d3d8_device_SetCursorPosition,
2777 d3d8_device_ShowCursor,
2778 d3d8_device_CreateAdditionalSwapChain,
2779 d3d8_device_Reset,
2780 d3d8_device_Present,
2781 d3d8_device_GetBackBuffer,
2782 d3d8_device_GetRasterStatus,
2783 d3d8_device_SetGammaRamp,
2784 d3d8_device_GetGammaRamp,
2785 d3d8_device_CreateTexture,
2786 d3d8_device_CreateVolumeTexture,
2787 d3d8_device_CreateCubeTexture,
2788 d3d8_device_CreateVertexBuffer,
2789 d3d8_device_CreateIndexBuffer,
2790 d3d8_device_CreateRenderTarget,
2791 d3d8_device_CreateDepthStencilSurface,
2792 d3d8_device_CreateImageSurface,
2793 d3d8_device_CopyRects,
2794 d3d8_device_UpdateTexture,
2795 d3d8_device_GetFrontBuffer,
2796 d3d8_device_SetRenderTarget,
2797 d3d8_device_GetRenderTarget,
2798 d3d8_device_GetDepthStencilSurface,
2799 d3d8_device_BeginScene,
2800 d3d8_device_EndScene,
2801 d3d8_device_Clear,
2802 d3d8_device_SetTransform,
2803 d3d8_device_GetTransform,
2804 d3d8_device_MultiplyTransform,
2805 d3d8_device_SetViewport,
2806 d3d8_device_GetViewport,
2807 d3d8_device_SetMaterial,
2808 d3d8_device_GetMaterial,
2809 d3d8_device_SetLight,
2810 d3d8_device_GetLight,
2811 d3d8_device_LightEnable,
2812 d3d8_device_GetLightEnable,
2813 d3d8_device_SetClipPlane,
2814 d3d8_device_GetClipPlane,
2815 d3d8_device_SetRenderState,
2816 d3d8_device_GetRenderState,
2817 d3d8_device_BeginStateBlock,
2818 d3d8_device_EndStateBlock,
2819 d3d8_device_ApplyStateBlock,
2820 d3d8_device_CaptureStateBlock,
2821 d3d8_device_DeleteStateBlock,
2822 d3d8_device_CreateStateBlock,
2823 d3d8_device_SetClipStatus,
2824 d3d8_device_GetClipStatus,
2825 d3d8_device_GetTexture,
2826 d3d8_device_SetTexture,
2827 d3d8_device_GetTextureStageState,
2828 d3d8_device_SetTextureStageState,
2829 d3d8_device_ValidateDevice,
2830 d3d8_device_GetInfo,
2831 d3d8_device_SetPaletteEntries,
2832 d3d8_device_GetPaletteEntries,
2833 d3d8_device_SetCurrentTexturePalette,
2834 d3d8_device_GetCurrentTexturePalette,
2835 d3d8_device_DrawPrimitive,
2836 d3d8_device_DrawIndexedPrimitive,
2837 d3d8_device_DrawPrimitiveUP,
2838 d3d8_device_DrawIndexedPrimitiveUP,
2839 d3d8_device_ProcessVertices,
2840 d3d8_device_CreateVertexShader,
2841 d3d8_device_SetVertexShader,
2842 d3d8_device_GetVertexShader,
2843 d3d8_device_DeleteVertexShader,
2844 d3d8_device_SetVertexShaderConstant,
2845 d3d8_device_GetVertexShaderConstant,
2846 d3d8_device_GetVertexShaderDeclaration,
2847 d3d8_device_GetVertexShaderFunction,
2848 d3d8_device_SetStreamSource,
2849 d3d8_device_GetStreamSource,
2850 d3d8_device_SetIndices,
2851 d3d8_device_GetIndices,
2852 d3d8_device_CreatePixelShader,
2853 d3d8_device_SetPixelShader,
2854 d3d8_device_GetPixelShader,
2855 d3d8_device_DeletePixelShader,
2856 d3d8_device_SetPixelShaderConstant,
2857 d3d8_device_GetPixelShaderConstant,
2858 d3d8_device_GetPixelShaderFunction,
2859 d3d8_device_DrawRectPatch,
2860 d3d8_device_DrawTriPatch,
2861 d3d8_device_DeletePatch,
2862 };
2863
2864 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2865 {
2866 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2867 }
2868
2869 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2870 struct wined3d_device *device)
2871 {
2872 TRACE("device_parent %p, device %p\n", device_parent, device);
2873 }
2874
2875 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2876 {
2877 TRACE("device_parent %p.\n", device_parent);
2878 }
2879
2880 static HRESULT CDECL device_parent_create_texture_surface(struct wined3d_device_parent *device_parent,
2881 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2882 enum wined3d_pool pool, UINT sub_resource_idx, struct wined3d_surface **surface)
2883 {
2884 struct d3d8_device *device = device_from_device_parent(device_parent);
2885 struct d3d8_surface *d3d_surface;
2886 BOOL lockable = TRUE;
2887 HRESULT hr;
2888
2889 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
2890 "\tpool %#x, sub_resource_idx %u, surface %p.\n",
2891 device_parent, container_parent, width, height, format, usage, pool, sub_resource_idx, surface);
2892
2893
2894 if (pool == WINED3D_POOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC))
2895 lockable = FALSE;
2896
2897 if (FAILED(hr = d3d8_device_create_surface(device, width, height, d3dformat_from_wined3dformat(format),
2898 lockable, FALSE, (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0)))
2899 {
2900 WARN("Failed to create surface, hr %#x.\n", hr);
2901 return hr;
2902 }
2903
2904 *surface = d3d_surface->wined3d_surface;
2905 wined3d_surface_incref(*surface);
2906
2907 d3d_surface->container = container_parent;
2908 IDirect3DDevice8_Release(d3d_surface->parent_device);
2909 d3d_surface->parent_device = NULL;
2910
2911 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2912 d3d_surface->forwardReference = container_parent;
2913
2914 return hr;
2915 }
2916
2917 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
2918 void *container_parent, UINT width, UINT height, enum wined3d_format_id format_id, DWORD usage,
2919 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, struct wined3d_surface **surface)
2920 {
2921 struct d3d8_device *device = device_from_device_parent(device_parent);
2922 struct d3d8_surface *d3d_surface;
2923 HRESULT hr;
2924
2925 TRACE("device_parent %p, container_parent %p, width %u, height %u, format_id %#x, usage %#x,\n"
2926 "\tmultisample_type %#x, multisample_quality %u, surface %p.\n",
2927 device_parent, container_parent, width, height, format_id, usage,
2928 multisample_type, multisample_quality, surface);
2929
2930 if (FAILED(hr = d3d8_device_create_surface(device, width, height, d3dformat_from_wined3dformat(format_id),
2931 TRUE, FALSE, (IDirect3DSurface8 **)&d3d_surface, usage, D3DPOOL_DEFAULT, multisample_type,
2932 multisample_quality)))
2933 {
2934 WARN("Failed to create surface, hr %#x.\n", hr);
2935 return hr;
2936 }
2937
2938 *surface = d3d_surface->wined3d_surface;
2939 wined3d_surface_incref(*surface);
2940
2941 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice8_iface;
2942 /* Implicit surfaces are created with an refcount of 0 */
2943 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2944
2945 return hr;
2946 }
2947
2948 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
2949 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2950 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
2951 {
2952 struct d3d8_device *device = device_from_device_parent(device_parent);
2953 struct d3d8_volume *object;
2954 HRESULT hr;
2955
2956 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
2957 "format %#x, pool %#x, usage %#x, volume %p.\n",
2958 device_parent, container_parent, width, height, depth,
2959 format, pool, usage, volume);
2960
2961 /* Allocate the storage for the device */
2962 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2963 if (!object)
2964 {
2965 FIXME("Allocation of memory failed\n");
2966 *volume = NULL;
2967 return D3DERR_OUTOFVIDEOMEMORY;
2968 }
2969
2970 hr = volume_init(object, device, width, height, depth, usage, format, pool);
2971 if (FAILED(hr))
2972 {
2973 WARN("Failed to initialize volume, hr %#x.\n", hr);
2974 HeapFree(GetProcessHeap(), 0, object);
2975 return hr;
2976 }
2977
2978 *volume = object->wined3d_volume;
2979 wined3d_volume_incref(*volume);
2980 IDirect3DVolume8_Release(&object->IDirect3DVolume8_iface);
2981
2982 object->container = container_parent;
2983 object->forwardReference = container_parent;
2984
2985 TRACE("Created volume %p.\n", object);
2986
2987 return hr;
2988 }
2989
2990 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
2991 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
2992 {
2993 struct d3d8_device *device = device_from_device_parent(device_parent);
2994 struct d3d8_swapchain *d3d_swapchain;
2995 HRESULT hr;
2996
2997 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
2998
2999 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3000 {
3001 WARN("Failed to create swapchain, hr %#x.\n", hr);
3002 *swapchain = NULL;
3003 return hr;
3004 }
3005
3006 *swapchain = d3d_swapchain->wined3d_swapchain;
3007 wined3d_swapchain_incref(*swapchain);
3008 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3009
3010 return hr;
3011 }
3012
3013 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3014 {
3015 device_parent_wined3d_device_created,
3016 device_parent_mode_changed,
3017 device_parent_create_swapchain_surface,
3018 device_parent_create_texture_surface,
3019 device_parent_create_volume,
3020 device_parent_create_swapchain,
3021 };
3022
3023 static void setup_fpu(void)
3024 {
3025 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3026 WORD cw;
3027 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3028 cw = (cw & ~0xf3f) | 0x3f;
3029 __asm__ volatile ("fldcw %0" : : "m" (cw));
3030 #elif defined(__i386__) && defined(_MSC_VER)
3031 WORD cw;
3032 __asm fnstcw cw;
3033 cw = (cw & ~0xf3f) | 0x3f;
3034 __asm fldcw cw;
3035 #else
3036 FIXME("FPU setup not implemented for this platform.\n");
3037 #endif
3038 }
3039
3040 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3041 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3042 {
3043 struct wined3d_swapchain_desc swapchain_desc;
3044 HRESULT hr;
3045
3046 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3047 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3048 device->ref = 1;
3049 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3050 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3051 if (!device->handle_table.entries)
3052 {
3053 ERR("Failed to allocate handle table memory.\n");
3054 return E_OUTOFMEMORY;
3055 }
3056 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3057
3058 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3059
3060 wined3d_mutex_lock();
3061 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3062 &device->device_parent, &device->wined3d_device);
3063 if (FAILED(hr))
3064 {
3065 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3066 wined3d_mutex_unlock();
3067 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3068 return hr;
3069 }
3070
3071 if (!parameters->Windowed)
3072 {
3073 HWND device_window = parameters->hDeviceWindow;
3074
3075 if (!focus_window)
3076 focus_window = device_window;
3077 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3078 {
3079 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3080 wined3d_device_decref(device->wined3d_device);
3081 wined3d_mutex_unlock();
3082 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3083 return hr;
3084 }
3085
3086 if (!device_window)
3087 device_window = focus_window;
3088 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3089 parameters->BackBufferWidth,
3090 parameters->BackBufferHeight);
3091 }
3092
3093 if (flags & D3DCREATE_MULTITHREADED)
3094 wined3d_device_set_multithreaded(device->wined3d_device);
3095
3096 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3097
3098 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3099 if (FAILED(hr))
3100 {
3101 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3102 wined3d_device_release_focus_window(device->wined3d_device);
3103 wined3d_device_decref(device->wined3d_device);
3104 wined3d_mutex_unlock();
3105 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3106 return hr;
3107 }
3108
3109 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3110 wined3d_mutex_unlock();
3111
3112 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3113
3114 device->declArraySize = 16;
3115 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3116 if (!device->decls)
3117 {
3118 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3119 hr = E_OUTOFMEMORY;
3120 goto err;
3121 }
3122
3123 device->d3d_parent = &parent->IDirect3D8_iface;
3124 IDirect3D8_AddRef(device->d3d_parent);
3125
3126 return D3D_OK;
3127
3128 err:
3129 wined3d_mutex_lock();
3130 wined3d_device_uninit_3d(device->wined3d_device);
3131 wined3d_device_release_focus_window(device->wined3d_device);
3132 wined3d_device_decref(device->wined3d_device);
3133 wined3d_mutex_unlock();
3134 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3135 return hr;
3136 }