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