VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/d3d8/device.c@ 65381

Last change on this file since 65381 was 63025, checked in by vboxsync, 8 years ago

GA/NT/Graphics: warnings

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette