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