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