riched20: Don't apply paragraph formatting until the end of paragraph.
[wine.git] / dlls / ddraw / device.c
blob2474e499cb8fbca9a4bf03e1e09cae6e5cee3689
1 /*
2 * Copyright (c) 1998-2004 Lionel Ulmer
3 * Copyright (c) 2002-2005 Christian Costa
4 * Copyright (c) 2006-2009, 2011-2013 Stefan Dösinger
5 * Copyright (c) 2008 Alexander Dorofeyev
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.
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.
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
21 * IDirect3DDevice implementation, version 1, 2, 3 and 7. Rendering is relayed
22 * to WineD3D, some minimal DirectDraw specific management is handled here.
23 * The Direct3DDevice is NOT the parent of the WineD3DDevice, because d3d
24 * is initialized when DirectDraw creates the primary surface.
25 * Some type management is necessary, because some D3D types changed between
26 * D3D7 and D3D9.
30 #include "config.h"
31 #include "wine/port.h"
33 #include "ddraw_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
36 WINE_DECLARE_DEBUG_CHANNEL(winediag);
38 /* The device ID */
39 const GUID IID_D3DDEVICE_WineD3D = {
40 0xaef72d43,
41 0xb09a,
42 0x4b7b,
43 { 0xb7,0x98,0xc6,0x8a,0x77,0x2d,0x72,0x2a }
46 static inline void set_fpu_control_word(WORD fpucw)
48 #if defined(__i386__) && defined(__GNUC__)
49 __asm__ volatile ("fldcw %0" : : "m" (fpucw));
50 #elif defined(__i386__) && defined(_MSC_VER)
51 __asm fldcw fpucw;
52 #endif
55 static inline WORD d3d_fpu_setup(void)
57 WORD oldcw;
59 #if defined(__i386__) && defined(__GNUC__)
60 __asm__ volatile ("fnstcw %0" : "=m" (oldcw));
61 #elif defined(__i386__) && defined(_MSC_VER)
62 __asm fnstcw oldcw;
63 #else
64 static BOOL warned = FALSE;
65 if(!warned)
67 FIXME("FPUPRESERVE not implemented for this platform / compiler\n");
68 warned = TRUE;
70 return 0;
71 #endif
73 set_fpu_control_word(0x37f);
75 return oldcw;
78 static inline struct d3d_device *impl_from_IUnknown(IUnknown *iface)
80 return CONTAINING_RECORD(iface, struct d3d_device, IUnknown_inner);
83 static HRESULT WINAPI d3d_device_inner_QueryInterface(IUnknown *iface, REFIID riid, void **out)
85 struct d3d_device *device = impl_from_IUnknown(iface);
87 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
89 if (!riid)
91 *out = NULL;
92 return DDERR_INVALIDPARAMS;
95 if (IsEqualGUID(&IID_IUnknown, riid))
97 IDirect3DDevice7_AddRef(&device->IDirect3DDevice7_iface);
98 *out = &device->IDirect3DDevice7_iface;
99 return S_OK;
102 if (device->version == 7)
104 if (IsEqualGUID(&IID_IDirect3DDevice7, riid))
106 IDirect3DDevice7_AddRef(&device->IDirect3DDevice7_iface);
107 *out = &device->IDirect3DDevice7_iface;
108 return S_OK;
111 else
113 if (IsEqualGUID(&IID_IDirect3DDevice3, riid) && device->version == 3)
115 IDirect3DDevice3_AddRef(&device->IDirect3DDevice3_iface);
116 *out = &device->IDirect3DDevice3_iface;
117 return S_OK;
120 if (IsEqualGUID(&IID_IDirect3DDevice2, riid) && device->version >= 2)
122 IDirect3DDevice2_AddRef(&device->IDirect3DDevice2_iface);
123 *out = &device->IDirect3DDevice2_iface;
124 return S_OK;
127 if (IsEqualGUID(&IID_IDirect3DDevice, riid))
129 IDirect3DDevice_AddRef(&device->IDirect3DDevice_iface);
130 *out = &device->IDirect3DDevice_iface;
131 return S_OK;
135 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
137 *out = NULL;
138 return E_NOINTERFACE;
141 static HRESULT WINAPI d3d_device7_QueryInterface(IDirect3DDevice7 *iface, REFIID riid, void **out)
143 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
145 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
147 return IUnknown_QueryInterface(device->outer_unknown, riid, out);
150 static HRESULT WINAPI d3d_device3_QueryInterface(IDirect3DDevice3 *iface, REFIID riid, void **out)
152 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
154 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
156 return IUnknown_QueryInterface(device->outer_unknown, riid, out);
159 static HRESULT WINAPI d3d_device2_QueryInterface(IDirect3DDevice2 *iface, REFIID riid, void **out)
161 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
163 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
165 return IUnknown_QueryInterface(device->outer_unknown, riid, out);
168 static HRESULT WINAPI d3d_device1_QueryInterface(IDirect3DDevice *iface, REFIID riid, void **out)
170 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
172 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
174 return IUnknown_QueryInterface(device->outer_unknown, riid, out);
177 static ULONG WINAPI d3d_device_inner_AddRef(IUnknown *iface)
179 struct d3d_device *device = impl_from_IUnknown(iface);
180 ULONG ref = InterlockedIncrement(&device->ref);
182 TRACE("%p increasing refcount to %u.\n", device, ref);
184 return ref;
187 static ULONG WINAPI d3d_device7_AddRef(IDirect3DDevice7 *iface)
189 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
191 TRACE("iface %p.\n", iface);
193 return IUnknown_AddRef(device->outer_unknown);
196 static ULONG WINAPI d3d_device3_AddRef(IDirect3DDevice3 *iface)
198 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
200 TRACE("iface %p.\n", iface);
202 return IUnknown_AddRef(device->outer_unknown);
205 static ULONG WINAPI d3d_device2_AddRef(IDirect3DDevice2 *iface)
207 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
209 TRACE("iface %p.\n", iface);
211 return IUnknown_AddRef(device->outer_unknown);
214 static ULONG WINAPI d3d_device1_AddRef(IDirect3DDevice *iface)
216 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
218 TRACE("iface %p.\n", iface);
220 return IUnknown_AddRef(device->outer_unknown);
223 static ULONG WINAPI d3d_device_inner_Release(IUnknown *iface)
225 struct d3d_device *This = impl_from_IUnknown(iface);
226 ULONG ref = InterlockedDecrement(&This->ref);
227 IUnknown *rt_iface;
229 TRACE("%p decreasing refcount to %u.\n", This, ref);
231 /* This method doesn't destroy the wined3d device, because it's still in
232 * use for 2D rendering. IDirectDrawSurface7::Release will destroy the
233 * wined3d device when the render target is released. */
234 if (!ref)
236 DWORD i;
237 struct list *vp_entry, *vp_entry2;
239 wined3d_mutex_lock();
241 /* There is no need to unset any resources here, wined3d will take
242 * care of that on uninit_3d(). */
244 if (This->index_buffer)
245 wined3d_buffer_decref(This->index_buffer);
246 if (This->vertex_buffer)
247 wined3d_buffer_decref(This->vertex_buffer);
249 wined3d_device_set_rendertarget_view(This->wined3d_device, 0, NULL, FALSE);
251 /* Release the wined3d device. This won't destroy it. */
252 if (!wined3d_device_decref(This->wined3d_device))
253 ERR("The wined3d device (%p) was destroyed unexpectedly.\n", This->wined3d_device);
255 /* The texture handles should be unset by now, but there might be some bits
256 * missing in our reference counting(needs test). Do a sanity check. */
257 for (i = 0; i < This->handle_table.entry_count; ++i)
259 struct ddraw_handle_entry *entry = &This->handle_table.entries[i];
261 switch (entry->type)
263 case DDRAW_HANDLE_FREE:
264 break;
266 case DDRAW_HANDLE_MATERIAL:
268 struct d3d_material *m = entry->object;
269 FIXME("Material handle %#x (%p) not unset properly.\n", i + 1, m);
270 m->Handle = 0;
271 break;
274 case DDRAW_HANDLE_MATRIX:
276 /* No FIXME here because this might happen because of sloppy applications. */
277 WARN("Leftover matrix handle %#x (%p), deleting.\n", i + 1, entry->object);
278 IDirect3DDevice_DeleteMatrix(&This->IDirect3DDevice_iface, i + 1);
279 break;
282 case DDRAW_HANDLE_STATEBLOCK:
284 /* No FIXME here because this might happen because of sloppy applications. */
285 WARN("Leftover stateblock handle %#x (%p), deleting.\n", i + 1, entry->object);
286 IDirect3DDevice7_DeleteStateBlock(&This->IDirect3DDevice7_iface, i + 1);
287 break;
290 case DDRAW_HANDLE_SURFACE:
292 struct ddraw_surface *surf = entry->object;
293 FIXME("Texture handle %#x (%p) not unset properly.\n", i + 1, surf);
294 surf->Handle = 0;
295 break;
298 default:
299 FIXME("Handle %#x (%p) has unknown type %#x.\n", i + 1, entry->object, entry->type);
300 break;
304 ddraw_handle_table_destroy(&This->handle_table);
306 LIST_FOR_EACH_SAFE(vp_entry, vp_entry2, &This->viewport_list)
308 struct d3d_viewport *vp = LIST_ENTRY(vp_entry, struct d3d_viewport, entry);
309 IDirect3DDevice3_DeleteViewport(&This->IDirect3DDevice3_iface, &vp->IDirect3DViewport3_iface);
312 TRACE("Releasing render target %p.\n", This->rt_iface);
313 rt_iface = This->rt_iface;
314 This->rt_iface = NULL;
315 if (This->version != 1)
316 IUnknown_Release(rt_iface);
317 TRACE("Render target release done.\n");
319 /* Releasing the render target above may have released the last
320 * reference to the ddraw object. */
321 if (This->ddraw)
322 This->ddraw->d3ddevice = NULL;
324 /* Now free the structure */
325 HeapFree(GetProcessHeap(), 0, This);
326 wined3d_mutex_unlock();
329 TRACE("Done\n");
330 return ref;
333 static ULONG WINAPI d3d_device7_Release(IDirect3DDevice7 *iface)
335 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
337 TRACE("iface %p.\n", iface);
339 return IUnknown_Release(device->outer_unknown);
342 static ULONG WINAPI d3d_device3_Release(IDirect3DDevice3 *iface)
344 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
346 TRACE("iface %p.\n", iface);
348 return IUnknown_Release(device->outer_unknown);
351 static ULONG WINAPI d3d_device2_Release(IDirect3DDevice2 *iface)
353 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
355 TRACE("iface %p.\n", iface);
357 return IUnknown_Release(device->outer_unknown);
360 static ULONG WINAPI d3d_device1_Release(IDirect3DDevice *iface)
362 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
364 TRACE("iface %p.\n", iface);
366 return IUnknown_Release(device->outer_unknown);
369 /*****************************************************************************
370 * IDirect3DDevice Methods
371 *****************************************************************************/
373 /*****************************************************************************
374 * IDirect3DDevice::Initialize
376 * Initializes a Direct3DDevice. This implementation is a no-op, as all
377 * initialization is done at create time.
379 * Exists in Version 1
381 * Parameters:
382 * No idea what they mean, as the MSDN page is gone
384 * Returns: DD_OK
386 *****************************************************************************/
387 static HRESULT WINAPI d3d_device1_Initialize(IDirect3DDevice *iface,
388 IDirect3D *d3d, GUID *guid, D3DDEVICEDESC *device_desc)
390 /* It shouldn't be crucial, but print a FIXME, I'm interested if
391 * any game calls it and when. */
392 FIXME("iface %p, d3d %p, guid %s, device_desc %p nop!\n",
393 iface, d3d, debugstr_guid(guid), device_desc);
395 return D3D_OK;
398 static HRESULT d3d_device7_GetCaps(IDirect3DDevice7 *iface, D3DDEVICEDESC7 *device_desc)
400 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
402 TRACE("iface %p, device_desc %p.\n", iface, device_desc);
404 if (!device_desc)
406 WARN("device_desc is NULL, returning DDERR_INVALIDPARAMS.\n");
407 return DDERR_INVALIDPARAMS;
410 /* Call the same function used by IDirect3D, this saves code */
411 return ddraw_get_d3dcaps(device->ddraw, device_desc);
414 static HRESULT WINAPI d3d_device7_GetCaps_FPUSetup(IDirect3DDevice7 *iface, D3DDEVICEDESC7 *desc)
416 return d3d_device7_GetCaps(iface, desc);
419 static HRESULT WINAPI d3d_device7_GetCaps_FPUPreserve(IDirect3DDevice7 *iface, D3DDEVICEDESC7 *desc)
421 HRESULT hr;
422 WORD old_fpucw;
424 old_fpucw = d3d_fpu_setup();
425 hr = d3d_device7_GetCaps(iface, desc);
426 set_fpu_control_word(old_fpucw);
428 return hr;
430 /*****************************************************************************
431 * IDirect3DDevice3::GetCaps
433 * Retrieves the capabilities of the hardware device and the emulation
434 * device. For Wine, hardware and emulation are the same (it's all HW).
436 * This implementation is used for Version 1, 2, and 3. Version 7 has its own
438 * Parameters:
439 * HWDesc: Structure to fill with the HW caps
440 * HelDesc: Structure to fill with the hardware emulation caps
442 * Returns:
443 * D3D_OK on success
444 * D3DERR_* if a problem occurs. See WineD3D
446 *****************************************************************************/
448 /* There are 3 versions of D3DDEVICEDESC. All 3 share the same name because
449 * Microsoft just expanded the existing structure without naming them
450 * D3DDEVICEDESC2 and D3DDEVICEDESC3. Which version is used have depends
451 * on the version of the DirectX SDK. DirectX 6+ and Wine use the latest
452 * one with 252 bytes.
454 * All 3 versions are allowed as parameters and only the specified amount of
455 * bytes is written.
457 * Note that Direct3D7 and earlier are not available in native Win64
458 * ddraw.dll builds, so possible size differences between 32 bit and
459 * 64 bit are a non-issue.
461 static inline BOOL check_d3ddevicedesc_size(DWORD size)
463 if (size == FIELD_OFFSET(D3DDEVICEDESC, dwMinTextureWidth) /* 172 */
464 || size == FIELD_OFFSET(D3DDEVICEDESC, dwMaxTextureRepeat) /* 204 */
465 || size == sizeof(D3DDEVICEDESC) /* 252 */) return TRUE;
466 return FALSE;
469 static HRESULT WINAPI d3d_device3_GetCaps(IDirect3DDevice3 *iface,
470 D3DDEVICEDESC *HWDesc, D3DDEVICEDESC *HelDesc)
472 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
473 D3DDEVICEDESC7 desc7;
474 D3DDEVICEDESC desc1;
475 HRESULT hr;
477 TRACE("iface %p, hw_desc %p, hel_desc %p.\n", iface, HWDesc, HelDesc);
479 if (!HWDesc)
481 WARN("HWDesc is NULL, returning DDERR_INVALIDPARAMS.\n");
482 return DDERR_INVALIDPARAMS;
484 if (!check_d3ddevicedesc_size(HWDesc->dwSize))
486 WARN("HWDesc->dwSize is %u, returning DDERR_INVALIDPARAMS.\n", HWDesc->dwSize);
487 return DDERR_INVALIDPARAMS;
489 if (!HelDesc)
491 WARN("HelDesc is NULL, returning DDERR_INVALIDPARAMS.\n");
492 return DDERR_INVALIDPARAMS;
494 if (!check_d3ddevicedesc_size(HelDesc->dwSize))
496 WARN("HelDesc->dwSize is %u, returning DDERR_INVALIDPARAMS.\n", HelDesc->dwSize);
497 return DDERR_INVALIDPARAMS;
500 if (FAILED(hr = ddraw_get_d3dcaps(device->ddraw, &desc7)))
501 return hr;
503 ddraw_d3dcaps1_from_7(&desc1, &desc7);
504 DD_STRUCT_COPY_BYSIZE(HWDesc, &desc1);
505 DD_STRUCT_COPY_BYSIZE(HelDesc, &desc1);
506 return D3D_OK;
509 static HRESULT WINAPI d3d_device2_GetCaps(IDirect3DDevice2 *iface,
510 D3DDEVICEDESC *hw_desc, D3DDEVICEDESC *hel_desc)
512 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
514 TRACE("iface %p, hw_desc %p, hel_desc %p.\n", iface, hw_desc, hel_desc);
516 return d3d_device3_GetCaps(&device->IDirect3DDevice3_iface, hw_desc, hel_desc);
519 static HRESULT WINAPI d3d_device1_GetCaps(IDirect3DDevice *iface,
520 D3DDEVICEDESC *hw_desc, D3DDEVICEDESC *hel_desc)
522 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
524 TRACE("iface %p, hw_desc %p, hel_desc %p.\n", iface, hw_desc, hel_desc);
526 return d3d_device3_GetCaps(&device->IDirect3DDevice3_iface, hw_desc, hel_desc);
529 /*****************************************************************************
530 * IDirect3DDevice2::SwapTextureHandles
532 * Swaps the texture handles of 2 Texture interfaces. Version 1 and 2
534 * Parameters:
535 * Tex1, Tex2: The 2 Textures to swap
537 * Returns:
538 * D3D_OK
540 *****************************************************************************/
541 static HRESULT WINAPI d3d_device2_SwapTextureHandles(IDirect3DDevice2 *iface,
542 IDirect3DTexture2 *tex1, IDirect3DTexture2 *tex2)
544 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
545 struct ddraw_surface *surf1 = unsafe_impl_from_IDirect3DTexture2(tex1);
546 struct ddraw_surface *surf2 = unsafe_impl_from_IDirect3DTexture2(tex2);
547 DWORD h1, h2;
549 TRACE("iface %p, tex1 %p, tex2 %p.\n", iface, tex1, tex2);
551 wined3d_mutex_lock();
553 h1 = surf1->Handle - 1;
554 h2 = surf2->Handle - 1;
555 device->handle_table.entries[h1].object = surf2;
556 device->handle_table.entries[h2].object = surf1;
557 surf2->Handle = h1 + 1;
558 surf1->Handle = h2 + 1;
560 wined3d_mutex_unlock();
562 return D3D_OK;
565 static HRESULT WINAPI d3d_device1_SwapTextureHandles(IDirect3DDevice *iface,
566 IDirect3DTexture *tex1, IDirect3DTexture *tex2)
568 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
569 struct ddraw_surface *surf1 = unsafe_impl_from_IDirect3DTexture(tex1);
570 struct ddraw_surface *surf2 = unsafe_impl_from_IDirect3DTexture(tex2);
571 IDirect3DTexture2 *t1 = surf1 ? &surf1->IDirect3DTexture2_iface : NULL;
572 IDirect3DTexture2 *t2 = surf2 ? &surf2->IDirect3DTexture2_iface : NULL;
574 TRACE("iface %p, tex1 %p, tex2 %p.\n", iface, tex1, tex2);
576 return d3d_device2_SwapTextureHandles(&device->IDirect3DDevice2_iface, t1, t2);
579 /*****************************************************************************
580 * IDirect3DDevice3::GetStats
582 * This method seems to retrieve some stats from the device.
583 * The MSDN documentation doesn't exist any more, but the D3DSTATS
584 * structure suggests that the amount of drawn primitives and processed
585 * vertices is returned.
587 * Exists in Version 1, 2 and 3
589 * Parameters:
590 * Stats: Pointer to a D3DSTATS structure to be filled
592 * Returns:
593 * D3D_OK on success
594 * DDERR_INVALIDPARAMS if Stats == NULL
596 *****************************************************************************/
597 static HRESULT WINAPI d3d_device3_GetStats(IDirect3DDevice3 *iface, D3DSTATS *Stats)
599 FIXME("iface %p, stats %p stub!\n", iface, Stats);
601 if(!Stats)
602 return DDERR_INVALIDPARAMS;
604 /* Fill the Stats with 0 */
605 Stats->dwTrianglesDrawn = 0;
606 Stats->dwLinesDrawn = 0;
607 Stats->dwPointsDrawn = 0;
608 Stats->dwSpansDrawn = 0;
609 Stats->dwVerticesProcessed = 0;
611 return D3D_OK;
614 static HRESULT WINAPI d3d_device2_GetStats(IDirect3DDevice2 *iface, D3DSTATS *stats)
616 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
618 TRACE("iface %p, stats %p.\n", iface, stats);
620 return d3d_device3_GetStats(&device->IDirect3DDevice3_iface, stats);
623 static HRESULT WINAPI d3d_device1_GetStats(IDirect3DDevice *iface, D3DSTATS *stats)
625 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
627 TRACE("iface %p, stats %p.\n", iface, stats);
629 return d3d_device3_GetStats(&device->IDirect3DDevice3_iface, stats);
632 /*****************************************************************************
633 * IDirect3DDevice::CreateExecuteBuffer
635 * Creates an IDirect3DExecuteBuffer, used for rendering with a
636 * Direct3DDevice.
638 * Version 1 only.
640 * Params:
641 * Desc: Buffer description
642 * ExecuteBuffer: Address to return the Interface pointer at
643 * UnkOuter: Must be NULL. Basically for aggregation, which ddraw doesn't
644 * support
646 * Returns:
647 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
648 * DDERR_OUTOFMEMORY if we ran out of memory
649 * D3D_OK on success
651 *****************************************************************************/
652 static HRESULT WINAPI d3d_device1_CreateExecuteBuffer(IDirect3DDevice *iface,
653 D3DEXECUTEBUFFERDESC *buffer_desc, IDirect3DExecuteBuffer **ExecuteBuffer, IUnknown *outer_unknown)
655 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
656 struct d3d_execute_buffer *object;
657 HRESULT hr;
659 TRACE("iface %p, buffer_desc %p, buffer %p, outer_unknown %p.\n",
660 iface, buffer_desc, ExecuteBuffer, outer_unknown);
662 if (outer_unknown)
663 return CLASS_E_NOAGGREGATION;
665 /* Allocate the new Execute Buffer */
666 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
667 if(!object)
669 ERR("Failed to allocate execute buffer memory.\n");
670 return DDERR_OUTOFMEMORY;
673 hr = d3d_execute_buffer_init(object, device, buffer_desc);
674 if (FAILED(hr))
676 WARN("Failed to initialize execute buffer, hr %#x.\n", hr);
677 HeapFree(GetProcessHeap(), 0, object);
678 return hr;
681 *ExecuteBuffer = &object->IDirect3DExecuteBuffer_iface;
683 TRACE(" Returning IDirect3DExecuteBuffer at %p, implementation is at %p\n", *ExecuteBuffer, object);
685 return D3D_OK;
688 /*****************************************************************************
689 * IDirect3DDevice::Execute
691 * Executes all the stuff in an execute buffer.
693 * Params:
694 * ExecuteBuffer: The buffer to execute
695 * Viewport: The viewport used for rendering
696 * Flags: Some flags
698 * Returns:
699 * DDERR_INVALIDPARAMS if ExecuteBuffer == NULL
700 * D3D_OK on success
702 *****************************************************************************/
703 static HRESULT WINAPI d3d_device1_Execute(IDirect3DDevice *iface,
704 IDirect3DExecuteBuffer *ExecuteBuffer, IDirect3DViewport *viewport, DWORD flags)
706 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
707 struct d3d_execute_buffer *buffer = unsafe_impl_from_IDirect3DExecuteBuffer(ExecuteBuffer);
708 struct d3d_viewport *viewport_impl = unsafe_impl_from_IDirect3DViewport(viewport);
709 HRESULT hr;
711 TRACE("iface %p, buffer %p, viewport %p, flags %#x.\n", iface, ExecuteBuffer, viewport, flags);
713 if(!buffer)
714 return DDERR_INVALIDPARAMS;
716 /* Execute... */
717 wined3d_mutex_lock();
718 hr = d3d_execute_buffer_execute(buffer, device, viewport_impl);
719 wined3d_mutex_unlock();
721 return hr;
724 /*****************************************************************************
725 * IDirect3DDevice3::AddViewport
727 * Add a Direct3DViewport to the device's viewport list. These viewports
728 * are wrapped to IDirect3DDevice7 viewports in viewport.c
730 * Exists in Version 1, 2 and 3. Note that IDirect3DViewport 1, 2 and 3
731 * are the same interfaces.
733 * Params:
734 * Viewport: The viewport to add
736 * Returns:
737 * DDERR_INVALIDPARAMS if Viewport == NULL
738 * D3D_OK on success
740 *****************************************************************************/
741 static HRESULT WINAPI d3d_device3_AddViewport(IDirect3DDevice3 *iface, IDirect3DViewport3 *viewport)
743 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
744 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport3(viewport);
746 TRACE("iface %p, viewport %p.\n", iface, viewport);
748 /* Sanity check */
749 if(!vp)
750 return DDERR_INVALIDPARAMS;
752 wined3d_mutex_lock();
753 IDirect3DViewport3_AddRef(viewport);
754 list_add_head(&device->viewport_list, &vp->entry);
755 /* Viewport must be usable for Clear() after AddViewport, so set active_device here. */
756 vp->active_device = device;
757 wined3d_mutex_unlock();
759 return D3D_OK;
762 static HRESULT WINAPI d3d_device2_AddViewport(IDirect3DDevice2 *iface,
763 IDirect3DViewport2 *viewport)
765 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
766 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport2(viewport);
768 TRACE("iface %p, viewport %p.\n", iface, viewport);
770 return d3d_device3_AddViewport(&device->IDirect3DDevice3_iface, &vp->IDirect3DViewport3_iface);
773 static HRESULT WINAPI d3d_device1_AddViewport(IDirect3DDevice *iface, IDirect3DViewport *viewport)
775 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
776 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport(viewport);
778 TRACE("iface %p, viewport %p.\n", iface, viewport);
780 return d3d_device3_AddViewport(&device->IDirect3DDevice3_iface, &vp->IDirect3DViewport3_iface);
783 /*****************************************************************************
784 * IDirect3DDevice3::DeleteViewport
786 * Deletes a Direct3DViewport from the device's viewport list.
788 * Exists in Version 1, 2 and 3. Note that all Viewport interface versions
789 * are equal.
791 * Params:
792 * Viewport: The viewport to delete
794 * Returns:
795 * D3D_OK on success
796 * DDERR_INVALIDPARAMS if the viewport wasn't found in the list
798 *****************************************************************************/
799 static HRESULT WINAPI d3d_device3_DeleteViewport(IDirect3DDevice3 *iface, IDirect3DViewport3 *viewport)
801 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
802 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport3(viewport);
804 TRACE("iface %p, viewport %p.\n", iface, viewport);
806 if (!vp)
808 WARN("NULL viewport, returning DDERR_INVALIDPARAMS\n");
809 return DDERR_INVALIDPARAMS;
812 wined3d_mutex_lock();
814 if (vp->active_device != device)
816 WARN("Viewport %p active device is %p.\n", vp, vp->active_device);
817 wined3d_mutex_unlock();
818 return DDERR_INVALIDPARAMS;
821 if (device->current_viewport == vp)
823 TRACE("Deleting current viewport, unsetting and releasing\n");
824 IDirect3DViewport3_Release(viewport);
825 device->current_viewport = NULL;
828 vp->active_device = NULL;
829 list_remove(&vp->entry);
831 IDirect3DViewport3_Release(viewport);
833 wined3d_mutex_unlock();
835 return D3D_OK;
838 static HRESULT WINAPI d3d_device2_DeleteViewport(IDirect3DDevice2 *iface, IDirect3DViewport2 *viewport)
840 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
841 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport2(viewport);
843 TRACE("iface %p, viewport %p.\n", iface, viewport);
845 return d3d_device3_DeleteViewport(&device->IDirect3DDevice3_iface,
846 vp ? &vp->IDirect3DViewport3_iface : NULL);
849 static HRESULT WINAPI d3d_device1_DeleteViewport(IDirect3DDevice *iface, IDirect3DViewport *viewport)
851 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
852 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport(viewport);
854 TRACE("iface %p, viewport %p.\n", iface, viewport);
856 return d3d_device3_DeleteViewport(&device->IDirect3DDevice3_iface,
857 vp ? &vp->IDirect3DViewport3_iface : NULL);
860 /*****************************************************************************
861 * IDirect3DDevice3::NextViewport
863 * Returns a viewport from the viewport list, depending on the
864 * passed viewport and the flags.
866 * Exists in Version 1, 2 and 3. Note that all Viewport interface versions
867 * are equal.
869 * Params:
870 * Viewport: Viewport to use for beginning the search
871 * Flags: D3DNEXT_NEXT, D3DNEXT_HEAD or D3DNEXT_TAIL
873 * Returns:
874 * D3D_OK on success
875 * DDERR_INVALIDPARAMS if the flags were wrong, or Viewport was NULL
877 *****************************************************************************/
878 static HRESULT WINAPI d3d_device3_NextViewport(IDirect3DDevice3 *iface,
879 IDirect3DViewport3 *Viewport3, IDirect3DViewport3 **lplpDirect3DViewport3, DWORD flags)
881 struct d3d_device *This = impl_from_IDirect3DDevice3(iface);
882 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport3(Viewport3);
883 struct d3d_viewport *next;
884 struct list *entry;
886 TRACE("iface %p, viewport %p, next %p, flags %#x.\n",
887 iface, Viewport3, lplpDirect3DViewport3, flags);
889 if(!vp)
891 *lplpDirect3DViewport3 = NULL;
892 return DDERR_INVALIDPARAMS;
896 wined3d_mutex_lock();
897 switch (flags)
899 case D3DNEXT_NEXT:
900 entry = list_next(&This->viewport_list, &vp->entry);
901 break;
903 case D3DNEXT_HEAD:
904 entry = list_head(&This->viewport_list);
905 break;
907 case D3DNEXT_TAIL:
908 entry = list_tail(&This->viewport_list);
909 break;
911 default:
912 WARN("Invalid flags %#x.\n", flags);
913 *lplpDirect3DViewport3 = NULL;
914 wined3d_mutex_unlock();
915 return DDERR_INVALIDPARAMS;
918 if (entry)
920 next = LIST_ENTRY(entry, struct d3d_viewport, entry);
921 *lplpDirect3DViewport3 = &next->IDirect3DViewport3_iface;
923 else
924 *lplpDirect3DViewport3 = NULL;
926 wined3d_mutex_unlock();
928 return D3D_OK;
931 static HRESULT WINAPI d3d_device2_NextViewport(IDirect3DDevice2 *iface,
932 IDirect3DViewport2 *viewport, IDirect3DViewport2 **next, DWORD flags)
934 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
935 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport2(viewport);
936 IDirect3DViewport3 *res;
937 HRESULT hr;
939 TRACE("iface %p, viewport %p, next %p, flags %#x.\n",
940 iface, viewport, next, flags);
942 hr = d3d_device3_NextViewport(&device->IDirect3DDevice3_iface,
943 &vp->IDirect3DViewport3_iface, &res, flags);
944 *next = (IDirect3DViewport2 *)res;
945 return hr;
948 static HRESULT WINAPI d3d_device1_NextViewport(IDirect3DDevice *iface,
949 IDirect3DViewport *viewport, IDirect3DViewport **next, DWORD flags)
951 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
952 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport(viewport);
953 IDirect3DViewport3 *res;
954 HRESULT hr;
956 TRACE("iface %p, viewport %p, next %p, flags %#x.\n",
957 iface, viewport, next, flags);
959 hr = d3d_device3_NextViewport(&device->IDirect3DDevice3_iface,
960 &vp->IDirect3DViewport3_iface, &res, flags);
961 *next = (IDirect3DViewport *)res;
962 return hr;
965 /*****************************************************************************
966 * IDirect3DDevice::Pick
968 * Executes an execute buffer without performing rendering. Instead, a
969 * list of primitives that intersect with (x1,y1) of the passed rectangle
970 * is created. IDirect3DDevice::GetPickRecords can be used to retrieve
971 * this list.
973 * Version 1 only
975 * Params:
976 * ExecuteBuffer: Buffer to execute
977 * Viewport: Viewport to use for execution
978 * Flags: None are defined, according to the SDK
979 * Rect: Specifies the coordinates to be picked. Only x1 and y2 are used,
980 * x2 and y2 are ignored.
982 * Returns:
983 * D3D_OK because it's a stub
985 *****************************************************************************/
986 static HRESULT WINAPI d3d_device1_Pick(IDirect3DDevice *iface, IDirect3DExecuteBuffer *buffer,
987 IDirect3DViewport *viewport, DWORD flags, D3DRECT *rect)
989 FIXME("iface %p, buffer %p, viewport %p, flags %#x, rect %s stub!\n",
990 iface, buffer, viewport, flags, wine_dbgstr_rect((RECT *)rect));
992 return D3D_OK;
995 /*****************************************************************************
996 * IDirect3DDevice::GetPickRecords
998 * Retrieves the pick records generated by IDirect3DDevice::GetPickRecords
1000 * Version 1 only
1002 * Params:
1003 * Count: Pointer to a DWORD containing the numbers of pick records to
1004 * retrieve
1005 * D3DPickRec: Address to store the resulting D3DPICKRECORD array.
1007 * Returns:
1008 * D3D_OK, because it's a stub
1010 *****************************************************************************/
1011 static HRESULT WINAPI d3d_device1_GetPickRecords(IDirect3DDevice *iface,
1012 DWORD *count, D3DPICKRECORD *records)
1014 FIXME("iface %p, count %p, records %p stub!\n", iface, count, records);
1016 return D3D_OK;
1019 /*****************************************************************************
1020 * IDirect3DDevice7::EnumTextureformats
1022 * Enumerates the supported texture formats. It has a list of all possible
1023 * formats and calls IWineD3D::CheckDeviceFormat for each format to see if
1024 * WineD3D supports it. If so, then it is passed to the app.
1026 * This is for Version 7 and 3, older versions have a different
1027 * callback function and their own implementation
1029 * Params:
1030 * Callback: Callback to call for each enumerated format
1031 * Arg: Argument to pass to the callback
1033 * Returns:
1034 * D3D_OK on success
1035 * DDERR_INVALIDPARAMS if Callback == NULL
1037 *****************************************************************************/
1038 static HRESULT d3d_device7_EnumTextureFormats(IDirect3DDevice7 *iface,
1039 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
1041 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
1042 struct wined3d_display_mode mode;
1043 HRESULT hr;
1044 unsigned int i;
1046 static const enum wined3d_format_id FormatList[] =
1048 /* 16 bit */
1049 WINED3DFMT_B5G5R5X1_UNORM,
1050 WINED3DFMT_B5G5R5A1_UNORM,
1051 WINED3DFMT_B4G4R4A4_UNORM,
1052 WINED3DFMT_B5G6R5_UNORM,
1053 /* 32 bit */
1054 WINED3DFMT_B8G8R8X8_UNORM,
1055 WINED3DFMT_B8G8R8A8_UNORM,
1056 /* 8 bit */
1057 WINED3DFMT_B2G3R3_UNORM,
1058 WINED3DFMT_P8_UINT,
1059 /* FOURCC codes */
1060 WINED3DFMT_DXT1,
1061 WINED3DFMT_DXT2,
1062 WINED3DFMT_DXT3,
1063 WINED3DFMT_DXT4,
1064 WINED3DFMT_DXT5,
1067 static const enum wined3d_format_id BumpFormatList[] =
1069 WINED3DFMT_R8G8_SNORM,
1070 WINED3DFMT_R5G5_SNORM_L6_UNORM,
1071 WINED3DFMT_R8G8_SNORM_L8X8_UNORM,
1072 WINED3DFMT_R10G11B11_SNORM,
1073 WINED3DFMT_R10G10B10_SNORM_A2_UNORM
1076 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
1078 if (!callback)
1079 return DDERR_INVALIDPARAMS;
1081 wined3d_mutex_lock();
1083 memset(&mode, 0, sizeof(mode));
1084 if (FAILED(hr = wined3d_get_adapter_display_mode(device->ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1086 wined3d_mutex_unlock();
1087 WARN("Cannot get the current adapter format\n");
1088 return hr;
1091 for (i = 0; i < sizeof(FormatList) / sizeof(*FormatList); ++i)
1093 if (wined3d_check_device_format(device->ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1094 mode.format_id, 0, WINED3D_RTYPE_TEXTURE_2D, FormatList[i]) == D3D_OK)
1096 DDPIXELFORMAT pformat;
1098 memset(&pformat, 0, sizeof(pformat));
1099 pformat.dwSize = sizeof(pformat);
1100 ddrawformat_from_wined3dformat(&pformat, FormatList[i]);
1102 TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
1103 hr = callback(&pformat, context);
1104 if(hr != DDENUMRET_OK)
1106 TRACE("Format enumeration cancelled by application\n");
1107 wined3d_mutex_unlock();
1108 return D3D_OK;
1113 for (i = 0; i < sizeof(BumpFormatList) / sizeof(*BumpFormatList); ++i)
1115 if (wined3d_check_device_format(device->ddraw->wined3d, WINED3DADAPTER_DEFAULT,
1116 WINED3D_DEVICE_TYPE_HAL, mode.format_id, WINED3DUSAGE_QUERY_LEGACYBUMPMAP,
1117 WINED3D_RTYPE_TEXTURE_2D, BumpFormatList[i]) == D3D_OK)
1119 DDPIXELFORMAT pformat;
1121 memset(&pformat, 0, sizeof(pformat));
1122 pformat.dwSize = sizeof(pformat);
1123 ddrawformat_from_wined3dformat(&pformat, BumpFormatList[i]);
1125 TRACE("Enumerating WineD3DFormat %d\n", BumpFormatList[i]);
1126 hr = callback(&pformat, context);
1127 if(hr != DDENUMRET_OK)
1129 TRACE("Format enumeration cancelled by application\n");
1130 wined3d_mutex_unlock();
1131 return D3D_OK;
1135 TRACE("End of enumeration\n");
1136 wined3d_mutex_unlock();
1138 return D3D_OK;
1141 static HRESULT WINAPI d3d_device7_EnumTextureFormats_FPUSetup(IDirect3DDevice7 *iface,
1142 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
1144 return d3d_device7_EnumTextureFormats(iface, callback, context);
1147 static HRESULT WINAPI d3d_device7_EnumTextureFormats_FPUPreserve(IDirect3DDevice7 *iface,
1148 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
1150 HRESULT hr;
1151 WORD old_fpucw;
1153 old_fpucw = d3d_fpu_setup();
1154 hr = d3d_device7_EnumTextureFormats(iface, callback, context);
1155 set_fpu_control_word(old_fpucw);
1157 return hr;
1160 static HRESULT WINAPI d3d_device3_EnumTextureFormats(IDirect3DDevice3 *iface,
1161 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
1163 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
1165 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
1167 return IDirect3DDevice7_EnumTextureFormats(&device->IDirect3DDevice7_iface, callback, context);
1170 /*****************************************************************************
1171 * IDirect3DDevice2::EnumTextureformats
1173 * EnumTextureFormats for Version 1 and 2, see
1174 * IDirect3DDevice7::EnumTexureFormats for a more detailed description.
1176 * This version has a different callback and does not enumerate FourCC
1177 * formats
1179 *****************************************************************************/
1180 static HRESULT WINAPI d3d_device2_EnumTextureFormats(IDirect3DDevice2 *iface,
1181 LPD3DENUMTEXTUREFORMATSCALLBACK callback, void *context)
1183 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
1184 struct wined3d_display_mode mode;
1185 HRESULT hr;
1186 unsigned int i;
1188 static const enum wined3d_format_id FormatList[] =
1190 /* 16 bit */
1191 WINED3DFMT_B5G5R5X1_UNORM,
1192 WINED3DFMT_B5G5R5A1_UNORM,
1193 WINED3DFMT_B4G4R4A4_UNORM,
1194 WINED3DFMT_B5G6R5_UNORM,
1195 /* 32 bit */
1196 WINED3DFMT_B8G8R8X8_UNORM,
1197 WINED3DFMT_B8G8R8A8_UNORM,
1198 /* 8 bit */
1199 WINED3DFMT_B2G3R3_UNORM,
1200 WINED3DFMT_P8_UINT,
1201 /* FOURCC codes - Not in this version*/
1204 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
1206 if (!callback)
1207 return DDERR_INVALIDPARAMS;
1209 wined3d_mutex_lock();
1211 memset(&mode, 0, sizeof(mode));
1212 if (FAILED(hr = wined3d_get_adapter_display_mode(device->ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1214 wined3d_mutex_unlock();
1215 WARN("Cannot get the current adapter format\n");
1216 return hr;
1219 for (i = 0; i < sizeof(FormatList) / sizeof(*FormatList); ++i)
1221 if (wined3d_check_device_format(device->ddraw->wined3d, 0, WINED3D_DEVICE_TYPE_HAL,
1222 mode.format_id, 0, WINED3D_RTYPE_TEXTURE_2D, FormatList[i]) == D3D_OK)
1224 DDSURFACEDESC sdesc;
1226 memset(&sdesc, 0, sizeof(sdesc));
1227 sdesc.dwSize = sizeof(sdesc);
1228 sdesc.dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
1229 sdesc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1230 sdesc.ddpfPixelFormat.dwSize = sizeof(sdesc.ddpfPixelFormat);
1231 ddrawformat_from_wined3dformat(&sdesc.ddpfPixelFormat, FormatList[i]);
1233 TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
1234 hr = callback(&sdesc, context);
1235 if(hr != DDENUMRET_OK)
1237 TRACE("Format enumeration cancelled by application\n");
1238 wined3d_mutex_unlock();
1239 return D3D_OK;
1243 TRACE("End of enumeration\n");
1244 wined3d_mutex_unlock();
1246 return D3D_OK;
1249 static HRESULT WINAPI d3d_device1_EnumTextureFormats(IDirect3DDevice *iface,
1250 LPD3DENUMTEXTUREFORMATSCALLBACK callback, void *context)
1252 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
1254 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
1256 return d3d_device2_EnumTextureFormats(&device->IDirect3DDevice2_iface, callback, context);
1259 /*****************************************************************************
1260 * IDirect3DDevice::CreateMatrix
1262 * Creates a matrix handle. A handle is created and memory for a D3DMATRIX is
1263 * allocated for the handle.
1265 * Version 1 only
1267 * Params
1268 * D3DMatHandle: Address to return the handle at
1270 * Returns:
1271 * D3D_OK on success
1272 * DDERR_INVALIDPARAMS if D3DMatHandle = NULL
1274 *****************************************************************************/
1275 static HRESULT WINAPI d3d_device1_CreateMatrix(IDirect3DDevice *iface, D3DMATRIXHANDLE *D3DMatHandle)
1277 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
1278 D3DMATRIX *Matrix;
1279 DWORD h;
1281 TRACE("iface %p, matrix_handle %p.\n", iface, D3DMatHandle);
1283 if(!D3DMatHandle)
1284 return DDERR_INVALIDPARAMS;
1286 Matrix = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DMATRIX));
1287 if(!Matrix)
1289 ERR("Out of memory when allocating a D3DMATRIX\n");
1290 return DDERR_OUTOFMEMORY;
1293 wined3d_mutex_lock();
1295 h = ddraw_allocate_handle(&device->handle_table, Matrix, DDRAW_HANDLE_MATRIX);
1296 if (h == DDRAW_INVALID_HANDLE)
1298 ERR("Failed to allocate a matrix handle.\n");
1299 HeapFree(GetProcessHeap(), 0, Matrix);
1300 wined3d_mutex_unlock();
1301 return DDERR_OUTOFMEMORY;
1304 *D3DMatHandle = h + 1;
1306 TRACE(" returning matrix handle %d\n", *D3DMatHandle);
1308 wined3d_mutex_unlock();
1310 return D3D_OK;
1313 /*****************************************************************************
1314 * IDirect3DDevice::SetMatrix
1316 * Sets a matrix for a matrix handle. The matrix is copied into the memory
1317 * allocated for the handle
1319 * Version 1 only
1321 * Params:
1322 * D3DMatHandle: Handle to set the matrix to
1323 * D3DMatrix: Matrix to set
1325 * Returns:
1326 * D3D_OK on success
1327 * DDERR_INVALIDPARAMS if the handle of the matrix is invalid or the matrix
1328 * to set is NULL
1330 *****************************************************************************/
1331 static HRESULT WINAPI d3d_device1_SetMatrix(IDirect3DDevice *iface,
1332 D3DMATRIXHANDLE D3DMatHandle, D3DMATRIX *D3DMatrix)
1334 struct d3d_device *This = impl_from_IDirect3DDevice(iface);
1335 D3DMATRIX *m;
1337 TRACE("iface %p, matrix_handle %#x, matrix %p.\n", iface, D3DMatHandle, D3DMatrix);
1339 if (!D3DMatrix) return DDERR_INVALIDPARAMS;
1341 wined3d_mutex_lock();
1343 m = ddraw_get_object(&This->handle_table, D3DMatHandle - 1, DDRAW_HANDLE_MATRIX);
1344 if (!m)
1346 WARN("Invalid matrix handle.\n");
1347 wined3d_mutex_unlock();
1348 return DDERR_INVALIDPARAMS;
1351 if (TRACE_ON(ddraw))
1352 dump_D3DMATRIX(D3DMatrix);
1354 *m = *D3DMatrix;
1356 if (D3DMatHandle == This->world)
1357 wined3d_device_set_transform(This->wined3d_device,
1358 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)D3DMatrix);
1360 if (D3DMatHandle == This->view)
1361 wined3d_device_set_transform(This->wined3d_device,
1362 WINED3D_TS_VIEW, (struct wined3d_matrix *)D3DMatrix);
1364 if (D3DMatHandle == This->proj)
1365 wined3d_device_set_transform(This->wined3d_device,
1366 WINED3D_TS_PROJECTION, (struct wined3d_matrix *)D3DMatrix);
1368 wined3d_mutex_unlock();
1370 return D3D_OK;
1373 /*****************************************************************************
1374 * IDirect3DDevice::GetMatrix
1376 * Returns the content of a D3DMATRIX handle
1378 * Version 1 only
1380 * Params:
1381 * D3DMatHandle: Matrix handle to read the content from
1382 * D3DMatrix: Address to store the content at
1384 * Returns:
1385 * D3D_OK on success
1386 * DDERR_INVALIDPARAMS if D3DMatHandle is invalid or D3DMatrix is NULL
1388 *****************************************************************************/
1389 static HRESULT WINAPI d3d_device1_GetMatrix(IDirect3DDevice *iface,
1390 D3DMATRIXHANDLE D3DMatHandle, D3DMATRIX *D3DMatrix)
1392 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
1393 D3DMATRIX *m;
1395 TRACE("iface %p, matrix_handle %#x, matrix %p.\n", iface, D3DMatHandle, D3DMatrix);
1397 if (!D3DMatrix) return DDERR_INVALIDPARAMS;
1399 wined3d_mutex_lock();
1401 m = ddraw_get_object(&device->handle_table, D3DMatHandle - 1, DDRAW_HANDLE_MATRIX);
1402 if (!m)
1404 WARN("Invalid matrix handle.\n");
1405 wined3d_mutex_unlock();
1406 return DDERR_INVALIDPARAMS;
1409 *D3DMatrix = *m;
1411 wined3d_mutex_unlock();
1413 return D3D_OK;
1416 /*****************************************************************************
1417 * IDirect3DDevice::DeleteMatrix
1419 * Destroys a Matrix handle. Frees the memory and unsets the handle data
1421 * Version 1 only
1423 * Params:
1424 * D3DMatHandle: Handle to destroy
1426 * Returns:
1427 * D3D_OK on success
1428 * DDERR_INVALIDPARAMS if D3DMatHandle is invalid
1430 *****************************************************************************/
1431 static HRESULT WINAPI d3d_device1_DeleteMatrix(IDirect3DDevice *iface, D3DMATRIXHANDLE D3DMatHandle)
1433 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
1434 D3DMATRIX *m;
1436 TRACE("iface %p, matrix_handle %#x.\n", iface, D3DMatHandle);
1438 wined3d_mutex_lock();
1440 m = ddraw_free_handle(&device->handle_table, D3DMatHandle - 1, DDRAW_HANDLE_MATRIX);
1441 if (!m)
1443 WARN("Invalid matrix handle.\n");
1444 wined3d_mutex_unlock();
1445 return DDERR_INVALIDPARAMS;
1448 wined3d_mutex_unlock();
1450 HeapFree(GetProcessHeap(), 0, m);
1452 return D3D_OK;
1455 /*****************************************************************************
1456 * IDirect3DDevice7::BeginScene
1458 * This method must be called before any rendering is performed.
1459 * IDirect3DDevice::EndScene has to be called after the scene is complete
1461 * Version 1, 2, 3 and 7
1463 * Returns:
1464 * D3D_OK on success, for details see IWineD3DDevice::BeginScene
1465 * D3DERR_SCENE_IN_SCENE if WineD3D returns an error(Only in case of an already
1466 * started scene).
1468 *****************************************************************************/
1469 static HRESULT d3d_device7_BeginScene(IDirect3DDevice7 *iface)
1471 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
1472 HRESULT hr;
1474 TRACE("iface %p.\n", iface);
1476 wined3d_mutex_lock();
1477 hr = wined3d_device_begin_scene(device->wined3d_device);
1478 wined3d_mutex_unlock();
1480 if(hr == WINED3D_OK) return D3D_OK;
1481 else return D3DERR_SCENE_IN_SCENE; /* TODO: Other possible causes of failure */
1484 static HRESULT WINAPI d3d_device7_BeginScene_FPUSetup(IDirect3DDevice7 *iface)
1486 return d3d_device7_BeginScene(iface);
1489 static HRESULT WINAPI d3d_device7_BeginScene_FPUPreserve(IDirect3DDevice7 *iface)
1491 HRESULT hr;
1492 WORD old_fpucw;
1494 old_fpucw = d3d_fpu_setup();
1495 hr = d3d_device7_BeginScene(iface);
1496 set_fpu_control_word(old_fpucw);
1498 return hr;
1501 static HRESULT WINAPI d3d_device3_BeginScene(IDirect3DDevice3 *iface)
1503 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
1505 TRACE("iface %p.\n", iface);
1507 return IDirect3DDevice7_BeginScene(&device->IDirect3DDevice7_iface);
1510 static HRESULT WINAPI d3d_device2_BeginScene(IDirect3DDevice2 *iface)
1512 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
1514 TRACE("iface %p.\n", iface);
1516 return IDirect3DDevice7_BeginScene(&device->IDirect3DDevice7_iface);
1519 static HRESULT WINAPI d3d_device1_BeginScene(IDirect3DDevice *iface)
1521 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
1523 TRACE("iface %p.\n", iface);
1525 return IDirect3DDevice7_BeginScene(&device->IDirect3DDevice7_iface);
1528 /*****************************************************************************
1529 * IDirect3DDevice7::EndScene
1531 * Ends a scene that has been begun with IDirect3DDevice7::BeginScene.
1532 * This method must be called after rendering is finished.
1534 * Version 1, 2, 3 and 7
1536 * Returns:
1537 * D3D_OK on success, for details see IWineD3DDevice::EndScene
1538 * D3DERR_SCENE_NOT_IN_SCENE is returned if WineD3D returns an error. It does
1539 * that only if the scene was already ended.
1541 *****************************************************************************/
1542 static HRESULT d3d_device7_EndScene(IDirect3DDevice7 *iface)
1544 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
1545 HRESULT hr;
1547 TRACE("iface %p.\n", iface);
1549 wined3d_mutex_lock();
1550 hr = wined3d_device_end_scene(device->wined3d_device);
1551 wined3d_mutex_unlock();
1553 if(hr == WINED3D_OK) return D3D_OK;
1554 else return D3DERR_SCENE_NOT_IN_SCENE;
1557 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d_device7_EndScene_FPUSetup(IDirect3DDevice7 *iface)
1559 return d3d_device7_EndScene(iface);
1562 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d_device7_EndScene_FPUPreserve(IDirect3DDevice7 *iface)
1564 HRESULT hr;
1565 WORD old_fpucw;
1567 old_fpucw = d3d_fpu_setup();
1568 hr = d3d_device7_EndScene(iface);
1569 set_fpu_control_word(old_fpucw);
1571 return hr;
1574 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d_device3_EndScene(IDirect3DDevice3 *iface)
1576 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
1578 TRACE("iface %p.\n", iface);
1580 return IDirect3DDevice7_EndScene(&device->IDirect3DDevice7_iface);
1583 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d_device2_EndScene(IDirect3DDevice2 *iface)
1585 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
1587 TRACE("iface %p.\n", iface);
1589 return IDirect3DDevice7_EndScene(&device->IDirect3DDevice7_iface);
1592 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d_device1_EndScene(IDirect3DDevice *iface)
1594 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
1596 TRACE("iface %p.\n", iface);
1598 return IDirect3DDevice7_EndScene(&device->IDirect3DDevice7_iface);
1601 /*****************************************************************************
1602 * IDirect3DDevice7::GetDirect3D
1604 * Returns the IDirect3D(= interface to the DirectDraw object) used to create
1605 * this device.
1607 * Params:
1608 * Direct3D7: Address to store the interface pointer at
1610 * Returns:
1611 * D3D_OK on success
1612 * DDERR_INVALIDPARAMS if Direct3D7 == NULL
1614 *****************************************************************************/
1615 static HRESULT WINAPI d3d_device7_GetDirect3D(IDirect3DDevice7 *iface, IDirect3D7 **d3d)
1617 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
1619 TRACE("iface %p, d3d %p.\n", iface, d3d);
1621 if (!d3d)
1622 return DDERR_INVALIDPARAMS;
1624 *d3d = &device->ddraw->IDirect3D7_iface;
1625 IDirect3D7_AddRef(*d3d);
1627 TRACE("Returning interface %p.\n", *d3d);
1628 return D3D_OK;
1631 static HRESULT WINAPI d3d_device3_GetDirect3D(IDirect3DDevice3 *iface, IDirect3D3 **d3d)
1633 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
1635 TRACE("iface %p, d3d %p.\n", iface, d3d);
1637 if (!d3d)
1638 return DDERR_INVALIDPARAMS;
1640 *d3d = &device->ddraw->IDirect3D3_iface;
1641 IDirect3D3_AddRef(*d3d);
1643 TRACE("Returning interface %p.\n", *d3d);
1644 return D3D_OK;
1647 static HRESULT WINAPI d3d_device2_GetDirect3D(IDirect3DDevice2 *iface, IDirect3D2 **d3d)
1649 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
1651 TRACE("iface %p, d3d %p.\n", iface, d3d);
1653 if (!d3d)
1654 return DDERR_INVALIDPARAMS;
1656 *d3d = &device->ddraw->IDirect3D2_iface;
1657 IDirect3D2_AddRef(*d3d);
1659 TRACE("Returning interface %p.\n", *d3d);
1660 return D3D_OK;
1663 static HRESULT WINAPI d3d_device1_GetDirect3D(IDirect3DDevice *iface, IDirect3D **d3d)
1665 struct d3d_device *device = impl_from_IDirect3DDevice(iface);
1667 TRACE("iface %p, d3d %p.\n", iface, d3d);
1669 if (!d3d)
1670 return DDERR_INVALIDPARAMS;
1672 *d3d = &device->ddraw->IDirect3D_iface;
1673 IDirect3D_AddRef(*d3d);
1675 TRACE("Returning interface %p.\n", *d3d);
1676 return D3D_OK;
1679 /*****************************************************************************
1680 * IDirect3DDevice3::SetCurrentViewport
1682 * Sets a Direct3DViewport as the current viewport.
1683 * For the thunks note that all viewport interface versions are equal
1685 * Params:
1686 * Direct3DViewport3: The viewport to set
1688 * Version 2 and 3
1690 * Returns:
1691 * D3D_OK on success
1692 * (Is a NULL viewport valid?)
1694 *****************************************************************************/
1695 static HRESULT WINAPI d3d_device3_SetCurrentViewport(IDirect3DDevice3 *iface, IDirect3DViewport3 *Direct3DViewport3)
1697 struct d3d_device *This = impl_from_IDirect3DDevice3(iface);
1698 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport3(Direct3DViewport3);
1700 TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport3);
1702 if (!vp)
1704 WARN("Direct3DViewport3 is NULL, returning DDERR_INVALIDPARAMS\n");
1705 return DDERR_INVALIDPARAMS;
1708 wined3d_mutex_lock();
1709 /* Do nothing if the specified viewport is the same as the current one */
1710 if (This->current_viewport == vp)
1712 wined3d_mutex_unlock();
1713 return D3D_OK;
1716 if (vp->active_device != This)
1718 WARN("Viewport %p active device is %p.\n", vp, vp->active_device);
1719 wined3d_mutex_unlock();
1720 return DDERR_INVALIDPARAMS;
1723 /* Release previous viewport and AddRef the new one */
1724 if (This->current_viewport)
1726 TRACE("ViewportImpl is at %p, interface is at %p\n", This->current_viewport,
1727 &This->current_viewport->IDirect3DViewport3_iface);
1728 IDirect3DViewport3_Release(&This->current_viewport->IDirect3DViewport3_iface);
1730 IDirect3DViewport3_AddRef(Direct3DViewport3);
1732 /* Set this viewport as the current viewport */
1733 This->current_viewport = vp;
1735 /* Activate this viewport */
1736 viewport_activate(This->current_viewport, FALSE);
1738 wined3d_mutex_unlock();
1740 return D3D_OK;
1743 static HRESULT WINAPI d3d_device2_SetCurrentViewport(IDirect3DDevice2 *iface, IDirect3DViewport2 *viewport)
1745 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
1746 struct d3d_viewport *vp = unsafe_impl_from_IDirect3DViewport2(viewport);
1748 TRACE("iface %p, viewport %p.\n", iface, viewport);
1750 return d3d_device3_SetCurrentViewport(&device->IDirect3DDevice3_iface,
1751 vp ? &vp->IDirect3DViewport3_iface : NULL);
1754 /*****************************************************************************
1755 * IDirect3DDevice3::GetCurrentViewport
1757 * Returns the currently active viewport.
1759 * Version 2 and 3
1761 * Params:
1762 * Direct3DViewport3: Address to return the interface pointer at
1764 * Returns:
1765 * D3D_OK on success
1766 * DDERR_INVALIDPARAMS if Direct3DViewport == NULL
1768 *****************************************************************************/
1769 static HRESULT WINAPI d3d_device3_GetCurrentViewport(IDirect3DDevice3 *iface, IDirect3DViewport3 **viewport)
1771 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
1773 TRACE("iface %p, viewport %p.\n", iface, viewport);
1775 wined3d_mutex_lock();
1776 if (!device->current_viewport)
1778 wined3d_mutex_unlock();
1779 WARN("No current viewport, returning D3DERR_NOCURRENTVIEWPORT\n");
1780 return D3DERR_NOCURRENTVIEWPORT;
1783 *viewport = &device->current_viewport->IDirect3DViewport3_iface;
1784 IDirect3DViewport3_AddRef(*viewport);
1786 TRACE("Returning interface %p.\n", *viewport);
1787 wined3d_mutex_unlock();
1788 return D3D_OK;
1791 static HRESULT WINAPI d3d_device2_GetCurrentViewport(IDirect3DDevice2 *iface, IDirect3DViewport2 **viewport)
1793 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
1795 TRACE("iface %p, viewport %p.\n", iface, viewport);
1797 return d3d_device3_GetCurrentViewport(&device->IDirect3DDevice3_iface,
1798 (IDirect3DViewport3 **)viewport);
1801 static BOOL validate_surface_palette(struct ddraw_surface *surface)
1803 return !format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat)
1804 || surface->palette;
1807 static HRESULT d3d_device_set_render_target(struct d3d_device *device,
1808 struct ddraw_surface *target, IUnknown *rt_iface)
1810 HRESULT hr;
1812 if (device->rt_iface == rt_iface)
1814 TRACE("No-op SetRenderTarget operation, not doing anything\n");
1815 return D3D_OK;
1817 if (!target)
1819 WARN("Trying to set render target to NULL.\n");
1820 return DDERR_INVALIDPARAMS;
1823 if (FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device,
1824 0, ddraw_surface_get_rendertarget_view(target), FALSE)))
1825 return hr;
1827 IUnknown_AddRef(rt_iface);
1828 IUnknown_Release(device->rt_iface);
1829 device->rt_iface = rt_iface;
1830 d3d_device_update_depth_stencil(device);
1832 return D3D_OK;
1835 static HRESULT d3d_device7_SetRenderTarget(IDirect3DDevice7 *iface,
1836 IDirectDrawSurface7 *target, DWORD flags)
1838 struct ddraw_surface *target_impl = unsafe_impl_from_IDirectDrawSurface7(target);
1839 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
1840 HRESULT hr;
1842 TRACE("iface %p, target %p, flags %#x.\n", iface, target, flags);
1844 wined3d_mutex_lock();
1846 if (!validate_surface_palette(target_impl))
1848 WARN("Surface %p has an indexed pixel format, but no palette.\n", target_impl);
1849 wined3d_mutex_unlock();
1850 return DDERR_INVALIDCAPS;
1853 if (!(target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE))
1855 WARN("Surface %p is not a render target.\n", target_impl);
1856 wined3d_mutex_unlock();
1857 return DDERR_INVALIDCAPS;
1860 if (!(target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
1862 WARN("Surface %p is not in video memory.\n", target_impl);
1863 wined3d_mutex_unlock();
1864 return DDERR_INVALIDPARAMS;
1867 if (target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1869 WARN("Surface %p is a depth buffer.\n", target_impl);
1870 IDirectDrawSurface7_AddRef(target);
1871 IUnknown_Release(device->rt_iface);
1872 device->rt_iface = (IUnknown *)target;
1873 wined3d_mutex_unlock();
1874 return DDERR_INVALIDPIXELFORMAT;
1877 hr = d3d_device_set_render_target(device, target_impl, (IUnknown *)target);
1878 wined3d_mutex_unlock();
1879 return hr;
1882 static HRESULT WINAPI d3d_device7_SetRenderTarget_FPUSetup(IDirect3DDevice7 *iface,
1883 IDirectDrawSurface7 *NewTarget, DWORD flags)
1885 return d3d_device7_SetRenderTarget(iface, NewTarget, flags);
1888 static HRESULT WINAPI d3d_device7_SetRenderTarget_FPUPreserve(IDirect3DDevice7 *iface,
1889 IDirectDrawSurface7 *NewTarget, DWORD flags)
1891 HRESULT hr;
1892 WORD old_fpucw;
1894 old_fpucw = d3d_fpu_setup();
1895 hr = d3d_device7_SetRenderTarget(iface, NewTarget, flags);
1896 set_fpu_control_word(old_fpucw);
1898 return hr;
1901 static HRESULT WINAPI d3d_device3_SetRenderTarget(IDirect3DDevice3 *iface,
1902 IDirectDrawSurface4 *target, DWORD flags)
1904 struct ddraw_surface *target_impl = unsafe_impl_from_IDirectDrawSurface4(target);
1905 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
1906 HRESULT hr;
1908 TRACE("iface %p, target %p, flags %#x.\n", iface, target, flags);
1910 wined3d_mutex_lock();
1912 if (!validate_surface_palette(target_impl))
1914 WARN("Surface %p has an indexed pixel format, but no palette.\n", target_impl);
1915 wined3d_mutex_unlock();
1916 return DDERR_INVALIDCAPS;
1919 if (!(target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE))
1921 WARN("Surface %p is not a render target.\n", target_impl);
1922 wined3d_mutex_unlock();
1923 return DDERR_INVALIDCAPS;
1926 if (target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1928 WARN("Surface %p is a depth buffer.\n", target_impl);
1929 IDirectDrawSurface4_AddRef(target);
1930 IUnknown_Release(device->rt_iface);
1931 device->rt_iface = (IUnknown *)target;
1932 wined3d_mutex_unlock();
1933 return DDERR_INVALIDPIXELFORMAT;
1936 if (!(target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
1938 WARN("Surface %p is not in video memory.\n", target_impl);
1939 IDirectDrawSurface4_AddRef(target);
1940 IUnknown_Release(device->rt_iface);
1941 device->rt_iface = (IUnknown *)target;
1942 wined3d_mutex_unlock();
1943 return D3D_OK;
1946 hr = d3d_device_set_render_target(device, target_impl, (IUnknown *)target);
1947 wined3d_mutex_unlock();
1948 return hr;
1951 static HRESULT WINAPI d3d_device2_SetRenderTarget(IDirect3DDevice2 *iface,
1952 IDirectDrawSurface *target, DWORD flags)
1954 struct ddraw_surface *target_impl = unsafe_impl_from_IDirectDrawSurface(target);
1955 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
1956 HRESULT hr;
1958 TRACE("iface %p, target %p, flags %#x.\n", iface, target, flags);
1960 wined3d_mutex_lock();
1962 if (!validate_surface_palette(target_impl))
1964 WARN("Surface %p has an indexed pixel format, but no palette.\n", target_impl);
1965 wined3d_mutex_unlock();
1966 return DDERR_INVALIDCAPS;
1969 if (!(target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE))
1971 WARN("Surface %p is not a render target.\n", target_impl);
1972 wined3d_mutex_unlock();
1973 return DDERR_INVALIDCAPS;
1976 if (target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1978 WARN("Surface %p is a depth buffer.\n", target_impl);
1979 IUnknown_Release(device->rt_iface);
1980 device->rt_iface = (IUnknown *)target;
1981 wined3d_mutex_unlock();
1982 return DDERR_INVALIDPIXELFORMAT;
1985 if (!(target_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
1987 WARN("Surface %p is not in video memory.\n", target_impl);
1988 IDirectDrawSurface_AddRef(target);
1989 IUnknown_Release(device->rt_iface);
1990 device->rt_iface = (IUnknown *)target;
1991 wined3d_mutex_unlock();
1992 return D3D_OK;
1995 hr = d3d_device_set_render_target(device, target_impl, (IUnknown *)target);
1996 wined3d_mutex_unlock();
1997 return hr;
2000 /*****************************************************************************
2001 * IDirect3DDevice7::GetRenderTarget
2003 * Returns the current render target.
2004 * This is handled locally, because the WineD3D render target's parent
2005 * is an IParent
2007 * Version 2, 3 and 7
2009 * Params:
2010 * RenderTarget: Address to store the surface interface pointer
2012 * Returns:
2013 * D3D_OK on success
2014 * DDERR_INVALIDPARAMS if RenderTarget == NULL
2016 *****************************************************************************/
2017 static HRESULT WINAPI d3d_device7_GetRenderTarget(IDirect3DDevice7 *iface, IDirectDrawSurface7 **RenderTarget)
2019 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
2020 HRESULT hr;
2022 TRACE("iface %p, target %p.\n", iface, RenderTarget);
2024 if(!RenderTarget)
2025 return DDERR_INVALIDPARAMS;
2027 wined3d_mutex_lock();
2028 hr = IUnknown_QueryInterface(device->rt_iface, &IID_IDirectDrawSurface7, (void **)RenderTarget);
2029 wined3d_mutex_unlock();
2031 return hr;
2034 static HRESULT WINAPI d3d_device3_GetRenderTarget(IDirect3DDevice3 *iface, IDirectDrawSurface4 **RenderTarget)
2036 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
2037 IDirectDrawSurface7 *RenderTarget7;
2038 struct ddraw_surface *RenderTargetImpl;
2039 HRESULT hr;
2041 TRACE("iface %p, target %p.\n", iface, RenderTarget);
2043 if(!RenderTarget)
2044 return DDERR_INVALIDPARAMS;
2046 hr = d3d_device7_GetRenderTarget(&device->IDirect3DDevice7_iface, &RenderTarget7);
2047 if(hr != D3D_OK) return hr;
2048 RenderTargetImpl = impl_from_IDirectDrawSurface7(RenderTarget7);
2049 *RenderTarget = &RenderTargetImpl->IDirectDrawSurface4_iface;
2050 IDirectDrawSurface4_AddRef(*RenderTarget);
2051 IDirectDrawSurface7_Release(RenderTarget7);
2052 return D3D_OK;
2055 static HRESULT WINAPI d3d_device2_GetRenderTarget(IDirect3DDevice2 *iface, IDirectDrawSurface **RenderTarget)
2057 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
2058 IDirectDrawSurface7 *RenderTarget7;
2059 struct ddraw_surface *RenderTargetImpl;
2060 HRESULT hr;
2062 TRACE("iface %p, target %p.\n", iface, RenderTarget);
2064 if(!RenderTarget)
2065 return DDERR_INVALIDPARAMS;
2067 hr = d3d_device7_GetRenderTarget(&device->IDirect3DDevice7_iface, &RenderTarget7);
2068 if(hr != D3D_OK) return hr;
2069 RenderTargetImpl = impl_from_IDirectDrawSurface7(RenderTarget7);
2070 *RenderTarget = &RenderTargetImpl->IDirectDrawSurface_iface;
2071 IDirectDrawSurface_AddRef(*RenderTarget);
2072 IDirectDrawSurface7_Release(RenderTarget7);
2073 return D3D_OK;
2076 /*****************************************************************************
2077 * IDirect3DDevice3::Begin
2079 * Begins a description block of vertices. This is similar to glBegin()
2080 * and glEnd(). After a call to IDirect3DDevice3::End, the vertices
2081 * described with IDirect3DDevice::Vertex are drawn.
2083 * Version 2 and 3
2085 * Params:
2086 * PrimitiveType: The type of primitives to draw
2087 * VertexTypeDesc: A flexible vertex format description of the vertices
2088 * Flags: Some flags..
2090 * Returns:
2091 * D3D_OK on success
2093 *****************************************************************************/
2094 static HRESULT WINAPI d3d_device3_Begin(IDirect3DDevice3 *iface,
2095 D3DPRIMITIVETYPE primitive_type, DWORD fvf, DWORD flags)
2097 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
2099 TRACE("iface %p, primitive_type %#x, fvf %#x, flags %#x.\n",
2100 iface, primitive_type, fvf, flags);
2102 wined3d_mutex_lock();
2103 device->primitive_type = primitive_type;
2104 device->vertex_type = fvf;
2105 device->render_flags = flags;
2106 device->vertex_size = get_flexible_vertex_size(device->vertex_type);
2107 device->nb_vertices = 0;
2108 wined3d_mutex_unlock();
2110 return D3D_OK;
2113 static HRESULT WINAPI d3d_device2_Begin(IDirect3DDevice2 *iface,
2114 D3DPRIMITIVETYPE primitive_type, D3DVERTEXTYPE vertex_type, DWORD flags)
2116 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
2117 DWORD fvf;
2119 TRACE("iface %p, primitive_type %#x, vertex_type %#x, flags %#x.\n",
2120 iface, primitive_type, vertex_type, flags);
2122 switch (vertex_type)
2124 case D3DVT_VERTEX: fvf = D3DFVF_VERTEX; break;
2125 case D3DVT_LVERTEX: fvf = D3DFVF_LVERTEX; break;
2126 case D3DVT_TLVERTEX: fvf = D3DFVF_TLVERTEX; break;
2127 default:
2128 ERR("Unexpected vertex type %#x.\n", vertex_type);
2129 return DDERR_INVALIDPARAMS; /* Should never happen */
2132 return d3d_device3_Begin(&device->IDirect3DDevice3_iface, primitive_type, fvf, flags);
2135 /*****************************************************************************
2136 * IDirect3DDevice3::BeginIndexed
2138 * Draws primitives based on vertices in a vertex array which are specified
2139 * by indices.
2141 * Version 2 and 3
2143 * Params:
2144 * PrimitiveType: Primitive type to draw
2145 * VertexType: A FVF description of the vertex format
2146 * Vertices: pointer to an array containing the vertices
2147 * NumVertices: The number of vertices in the vertex array
2148 * Flags: Some flags ...
2150 * Returns:
2151 * D3D_OK, because it's a stub
2153 *****************************************************************************/
2154 static HRESULT WINAPI d3d_device3_BeginIndexed(IDirect3DDevice3 *iface,
2155 D3DPRIMITIVETYPE primitive_type, DWORD fvf,
2156 void *vertices, DWORD vertex_count, DWORD flags)
2158 FIXME("iface %p, primitive_type %#x, fvf %#x, vertices %p, vertex_count %u, flags %#x stub!\n",
2159 iface, primitive_type, fvf, vertices, vertex_count, flags);
2161 return D3D_OK;
2165 static HRESULT WINAPI d3d_device2_BeginIndexed(IDirect3DDevice2 *iface,
2166 D3DPRIMITIVETYPE primitive_type, D3DVERTEXTYPE vertex_type,
2167 void *vertices, DWORD vertex_count, DWORD flags)
2169 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
2170 DWORD fvf;
2172 TRACE("iface %p, primitive_type %#x, vertex_type %#x, vertices %p, vertex_count %u, flags %#x stub!\n",
2173 iface, primitive_type, vertex_type, vertices, vertex_count, flags);
2175 switch (vertex_type)
2177 case D3DVT_VERTEX: fvf = D3DFVF_VERTEX; break;
2178 case D3DVT_LVERTEX: fvf = D3DFVF_LVERTEX; break;
2179 case D3DVT_TLVERTEX: fvf = D3DFVF_TLVERTEX; break;
2180 default:
2181 ERR("Unexpected vertex type %#x.\n", vertex_type);
2182 return DDERR_INVALIDPARAMS; /* Should never happen */
2185 return d3d_device3_BeginIndexed(&device->IDirect3DDevice3_iface,
2186 primitive_type, fvf, vertices, vertex_count, flags);
2189 /*****************************************************************************
2190 * IDirect3DDevice3::Vertex
2192 * Draws a vertex as described by IDirect3DDevice3::Begin. It places all
2193 * drawn vertices in a vertex buffer. If the buffer is too small, its
2194 * size is increased.
2196 * Version 2 and 3
2198 * Params:
2199 * Vertex: Pointer to the vertex
2201 * Returns:
2202 * D3D_OK, on success
2203 * DDERR_INVALIDPARAMS if Vertex is NULL
2205 *****************************************************************************/
2206 static HRESULT WINAPI d3d_device3_Vertex(IDirect3DDevice3 *iface, void *vertex)
2208 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
2210 TRACE("iface %p, vertex %p.\n", iface, vertex);
2212 if (!vertex)
2213 return DDERR_INVALIDPARAMS;
2215 wined3d_mutex_lock();
2216 if ((device->nb_vertices + 1) * device->vertex_size > device->buffer_size)
2218 BYTE *old_buffer;
2220 device->buffer_size = device->buffer_size ? device->buffer_size * 2 : device->vertex_size * 3;
2221 old_buffer = device->sysmem_vertex_buffer;
2222 device->sysmem_vertex_buffer = HeapAlloc(GetProcessHeap(), 0, device->buffer_size);
2223 if (old_buffer)
2225 memcpy(device->sysmem_vertex_buffer, old_buffer, device->nb_vertices * device->vertex_size);
2226 HeapFree(GetProcessHeap(), 0, old_buffer);
2230 memcpy(device->sysmem_vertex_buffer + device->nb_vertices++ * device->vertex_size, vertex, device->vertex_size);
2231 wined3d_mutex_unlock();
2233 return D3D_OK;
2236 static HRESULT WINAPI d3d_device2_Vertex(IDirect3DDevice2 *iface, void *vertex)
2238 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
2240 TRACE("iface %p, vertex %p.\n", iface, vertex);
2242 return d3d_device3_Vertex(&device->IDirect3DDevice3_iface, vertex);
2245 /*****************************************************************************
2246 * IDirect3DDevice3::Index
2248 * Specifies an index to a vertex to be drawn. The vertex array has to
2249 * be specified with BeginIndexed first.
2251 * Parameters:
2252 * VertexIndex: The index of the vertex to draw
2254 * Returns:
2255 * D3D_OK because it's a stub
2257 *****************************************************************************/
2258 static HRESULT WINAPI d3d_device3_Index(IDirect3DDevice3 *iface, WORD index)
2260 FIXME("iface %p, index %#x stub!\n", iface, index);
2262 return D3D_OK;
2265 static HRESULT WINAPI d3d_device2_Index(IDirect3DDevice2 *iface, WORD index)
2267 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
2269 TRACE("iface %p, index %#x.\n", iface, index);
2271 return d3d_device3_Index(&device->IDirect3DDevice3_iface, index);
2274 /*****************************************************************************
2275 * IDirect3DDevice7::GetRenderState
2277 * Returns the value of a render state. The possible render states are
2278 * defined in include/d3dtypes.h
2280 * Version 2, 3 and 7
2282 * Params:
2283 * RenderStateType: Render state to return the current setting of
2284 * Value: Address to store the value at
2286 * Returns:
2287 * D3D_OK on success, for details see IWineD3DDevice::GetRenderState
2288 * DDERR_INVALIDPARAMS if Value == NULL
2290 *****************************************************************************/
2291 static HRESULT d3d_device7_GetRenderState(IDirect3DDevice7 *iface,
2292 D3DRENDERSTATETYPE state, DWORD *value)
2294 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
2295 HRESULT hr = D3D_OK;
2297 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2299 if (!value)
2300 return DDERR_INVALIDPARAMS;
2302 wined3d_mutex_lock();
2303 switch (state)
2305 case D3DRENDERSTATE_TEXTUREMAG:
2307 enum wined3d_texture_filter_type tex_mag;
2309 tex_mag = wined3d_device_get_sampler_state(device->wined3d_device, 0, WINED3D_SAMP_MAG_FILTER);
2310 switch (tex_mag)
2312 case WINED3D_TEXF_POINT:
2313 *value = D3DFILTER_NEAREST;
2314 break;
2315 case WINED3D_TEXF_LINEAR:
2316 *value = D3DFILTER_LINEAR;
2317 break;
2318 default:
2319 ERR("Unhandled texture mag %d !\n",tex_mag);
2320 *value = 0;
2322 break;
2325 case D3DRENDERSTATE_TEXTUREMIN:
2327 enum wined3d_texture_filter_type tex_min;
2328 enum wined3d_texture_filter_type tex_mip;
2330 tex_min = wined3d_device_get_sampler_state(device->wined3d_device, 0, WINED3D_SAMP_MIN_FILTER);
2331 tex_mip = wined3d_device_get_sampler_state(device->wined3d_device, 0, WINED3D_SAMP_MIP_FILTER);
2332 switch (tex_min)
2334 case WINED3D_TEXF_POINT:
2335 switch (tex_mip)
2337 case WINED3D_TEXF_NONE:
2338 *value = D3DFILTER_NEAREST;
2339 break;
2340 case WINED3D_TEXF_POINT:
2341 *value = D3DFILTER_MIPNEAREST;
2342 break;
2343 case WINED3D_TEXF_LINEAR:
2344 *value = D3DFILTER_LINEARMIPNEAREST;
2345 break;
2346 default:
2347 ERR("Unhandled mip filter %#x.\n", tex_mip);
2348 *value = D3DFILTER_NEAREST;
2349 break;
2351 break;
2352 case WINED3D_TEXF_LINEAR:
2353 switch (tex_mip)
2355 case WINED3D_TEXF_NONE:
2356 *value = D3DFILTER_LINEAR;
2357 break;
2358 case WINED3D_TEXF_POINT:
2359 *value = D3DFILTER_MIPLINEAR;
2360 break;
2361 case WINED3D_TEXF_LINEAR:
2362 *value = D3DFILTER_LINEARMIPLINEAR;
2363 break;
2364 default:
2365 ERR("Unhandled mip filter %#x.\n", tex_mip);
2366 *value = D3DFILTER_LINEAR;
2367 break;
2369 break;
2370 default:
2371 ERR("Unhandled texture min filter %#x.\n",tex_min);
2372 *value = D3DFILTER_NEAREST;
2373 break;
2375 break;
2378 case D3DRENDERSTATE_TEXTUREADDRESS:
2379 case D3DRENDERSTATE_TEXTUREADDRESSU:
2380 *value = wined3d_device_get_sampler_state(device->wined3d_device, 0, WINED3D_SAMP_ADDRESS_U);
2381 break;
2382 case D3DRENDERSTATE_TEXTUREADDRESSV:
2383 *value = wined3d_device_get_sampler_state(device->wined3d_device, 0, WINED3D_SAMP_ADDRESS_V);
2384 break;
2386 case D3DRENDERSTATE_BORDERCOLOR:
2387 FIXME("Unhandled render state D3DRENDERSTATE_BORDERCOLOR.\n");
2388 hr = E_NOTIMPL;
2389 break;
2391 case D3DRENDERSTATE_TEXTUREHANDLE:
2392 case D3DRENDERSTATE_TEXTUREMAPBLEND:
2393 WARN("Render state %#x is invalid in d3d7.\n", state);
2394 hr = DDERR_INVALIDPARAMS;
2395 break;
2397 case D3DRENDERSTATE_ZBIAS:
2398 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
2399 break;
2401 default:
2402 if (state >= D3DRENDERSTATE_STIPPLEPATTERN00
2403 && state <= D3DRENDERSTATE_STIPPLEPATTERN31)
2405 FIXME("Unhandled stipple pattern render state (%#x).\n", state);
2406 hr = E_NOTIMPL;
2407 break;
2409 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2411 wined3d_mutex_unlock();
2413 return hr;
2416 static HRESULT WINAPI d3d_device7_GetRenderState_FPUSetup(IDirect3DDevice7 *iface,
2417 D3DRENDERSTATETYPE state, DWORD *value)
2419 return d3d_device7_GetRenderState(iface, state, value);
2422 static HRESULT WINAPI d3d_device7_GetRenderState_FPUPreserve(IDirect3DDevice7 *iface,
2423 D3DRENDERSTATETYPE state, DWORD *value)
2425 HRESULT hr;
2426 WORD old_fpucw;
2428 old_fpucw = d3d_fpu_setup();
2429 hr = d3d_device7_GetRenderState(iface, state, value);
2430 set_fpu_control_word(old_fpucw);
2432 return hr;
2435 static HRESULT WINAPI d3d_device3_GetRenderState(IDirect3DDevice3 *iface,
2436 D3DRENDERSTATETYPE state, DWORD *value)
2438 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
2440 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2442 switch (state)
2444 case D3DRENDERSTATE_TEXTUREHANDLE:
2446 /* This state is wrapped to SetTexture in SetRenderState, so
2447 * it has to be wrapped to GetTexture here. */
2448 struct wined3d_texture *tex = NULL;
2449 *value = 0;
2451 wined3d_mutex_lock();
2452 if ((tex = wined3d_device_get_texture(device->wined3d_device, 0)))
2454 /* The parent of the texture is the IDirectDrawSurface7
2455 * interface of the ddraw surface. */
2456 struct ddraw_texture *parent = wined3d_texture_get_parent(tex);
2457 if (parent)
2458 *value = parent->root->Handle;
2460 wined3d_mutex_unlock();
2462 return D3D_OK;
2465 case D3DRENDERSTATE_TEXTUREMAPBLEND:
2467 /* D3DRENDERSTATE_TEXTUREMAPBLEND is mapped to texture state stages in SetRenderState; reverse
2468 the mapping to get the value. */
2469 DWORD colorop, colorarg1, colorarg2;
2470 DWORD alphaop, alphaarg1, alphaarg2;
2472 wined3d_mutex_lock();
2474 device->legacyTextureBlending = TRUE;
2476 colorop = wined3d_device_get_texture_stage_state(device->wined3d_device, 0, WINED3D_TSS_COLOR_OP);
2477 colorarg1 = wined3d_device_get_texture_stage_state(device->wined3d_device, 0, WINED3D_TSS_COLOR_ARG1);
2478 colorarg2 = wined3d_device_get_texture_stage_state(device->wined3d_device, 0, WINED3D_TSS_COLOR_ARG2);
2479 alphaop = wined3d_device_get_texture_stage_state(device->wined3d_device, 0, WINED3D_TSS_ALPHA_OP);
2480 alphaarg1 = wined3d_device_get_texture_stage_state(device->wined3d_device, 0, WINED3D_TSS_ALPHA_ARG1);
2481 alphaarg2 = wined3d_device_get_texture_stage_state(device->wined3d_device, 0, WINED3D_TSS_ALPHA_ARG2);
2483 if (colorop == WINED3D_TOP_SELECT_ARG1 && colorarg1 == WINED3DTA_TEXTURE
2484 && alphaop == WINED3D_TOP_SELECT_ARG1 && alphaarg1 == WINED3DTA_TEXTURE)
2485 *value = D3DTBLEND_DECAL;
2486 else if (colorop == WINED3D_TOP_SELECT_ARG1 && colorarg1 == WINED3DTA_TEXTURE
2487 && alphaop == WINED3D_TOP_MODULATE
2488 && alphaarg1 == WINED3DTA_TEXTURE && alphaarg2 == WINED3DTA_CURRENT)
2489 *value = D3DTBLEND_DECALALPHA;
2490 else if (colorop == WINED3D_TOP_MODULATE
2491 && colorarg1 == WINED3DTA_TEXTURE && colorarg2 == WINED3DTA_CURRENT
2492 && alphaop == WINED3D_TOP_MODULATE
2493 && alphaarg1 == WINED3DTA_TEXTURE && alphaarg2 == WINED3DTA_CURRENT)
2494 *value = D3DTBLEND_MODULATEALPHA;
2495 else
2497 struct wined3d_texture *tex = NULL;
2498 BOOL tex_alpha = FALSE;
2499 DDPIXELFORMAT ddfmt;
2501 if ((tex = wined3d_device_get_texture(device->wined3d_device, 0)))
2503 struct wined3d_resource_desc desc;
2505 wined3d_resource_get_desc(wined3d_texture_get_resource(tex), &desc);
2506 ddfmt.dwSize = sizeof(ddfmt);
2507 ddrawformat_from_wined3dformat(&ddfmt, desc.format);
2508 if (ddfmt.u5.dwRGBAlphaBitMask)
2509 tex_alpha = TRUE;
2512 if (!(colorop == WINED3D_TOP_MODULATE
2513 && colorarg1 == WINED3DTA_TEXTURE && colorarg2 == WINED3DTA_CURRENT
2514 && alphaop == (tex_alpha ? WINED3D_TOP_SELECT_ARG1 : WINED3D_TOP_SELECT_ARG2)
2515 && alphaarg1 == WINED3DTA_TEXTURE && alphaarg2 == WINED3DTA_CURRENT))
2516 ERR("Unexpected texture stage state setup, returning D3DTBLEND_MODULATE - likely erroneous.\n");
2518 *value = D3DTBLEND_MODULATE;
2521 wined3d_mutex_unlock();
2523 return D3D_OK;
2526 case D3DRENDERSTATE_LIGHTING:
2527 case D3DRENDERSTATE_NORMALIZENORMALS:
2528 case D3DRENDERSTATE_LOCALVIEWER:
2529 *value = 0xffffffff;
2530 return D3D_OK;
2532 default:
2533 return IDirect3DDevice7_GetRenderState(&device->IDirect3DDevice7_iface, state, value);
2537 static HRESULT WINAPI d3d_device2_GetRenderState(IDirect3DDevice2 *iface,
2538 D3DRENDERSTATETYPE state, DWORD *value)
2540 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
2542 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2544 return IDirect3DDevice3_GetRenderState(&device->IDirect3DDevice3_iface, state, value);
2547 /*****************************************************************************
2548 * IDirect3DDevice7::SetRenderState
2550 * Sets a render state. The possible render states are defined in
2551 * include/d3dtypes.h
2553 * Version 2, 3 and 7
2555 * Params:
2556 * RenderStateType: State to set
2557 * Value: Value to assign to that state
2559 * Returns:
2560 * D3D_OK on success,
2561 * for details see IWineD3DDevice::SetRenderState
2563 *****************************************************************************/
2564 static HRESULT d3d_device7_SetRenderState(IDirect3DDevice7 *iface,
2565 D3DRENDERSTATETYPE state, DWORD value)
2567 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
2568 HRESULT hr = D3D_OK;
2570 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2572 wined3d_mutex_lock();
2573 /* Some render states need special care */
2574 switch (state)
2577 * The ddraw texture filter mapping works like this:
2578 * D3DFILTER_NEAREST Point min/mag, no mip
2579 * D3DFILTER_MIPNEAREST Point min/mag, point mip
2580 * D3DFILTER_LINEARMIPNEAREST: Point min/mag, linear mip
2582 * D3DFILTER_LINEAR Linear min/mag, no mip
2583 * D3DFILTER_MIPLINEAR Linear min/mag, point mip
2584 * D3DFILTER_LINEARMIPLINEAR Linear min/mag, linear mip
2586 * This is the opposite of the GL naming convention,
2587 * D3DFILTER_LINEARMIPNEAREST corresponds to GL_NEAREST_MIPMAP_LINEAR.
2589 case D3DRENDERSTATE_TEXTUREMAG:
2591 enum wined3d_texture_filter_type tex_mag;
2593 switch (value)
2595 case D3DFILTER_NEAREST:
2596 case D3DFILTER_MIPNEAREST:
2597 case D3DFILTER_LINEARMIPNEAREST:
2598 tex_mag = WINED3D_TEXF_POINT;
2599 break;
2600 case D3DFILTER_LINEAR:
2601 case D3DFILTER_MIPLINEAR:
2602 case D3DFILTER_LINEARMIPLINEAR:
2603 tex_mag = WINED3D_TEXF_LINEAR;
2604 break;
2605 default:
2606 tex_mag = WINED3D_TEXF_POINT;
2607 FIXME("Unhandled texture mag %#x.\n", value);
2608 break;
2611 wined3d_device_set_sampler_state(device->wined3d_device, 0, WINED3D_SAMP_MAG_FILTER, tex_mag);
2612 break;
2615 case D3DRENDERSTATE_TEXTUREMIN:
2617 enum wined3d_texture_filter_type tex_min;
2618 enum wined3d_texture_filter_type tex_mip;
2620 switch (value)
2622 case D3DFILTER_NEAREST:
2623 tex_min = WINED3D_TEXF_POINT;
2624 tex_mip = WINED3D_TEXF_NONE;
2625 break;
2626 case D3DFILTER_LINEAR:
2627 tex_min = WINED3D_TEXF_LINEAR;
2628 tex_mip = WINED3D_TEXF_NONE;
2629 break;
2630 case D3DFILTER_MIPNEAREST:
2631 tex_min = WINED3D_TEXF_POINT;
2632 tex_mip = WINED3D_TEXF_POINT;
2633 break;
2634 case D3DFILTER_MIPLINEAR:
2635 tex_min = WINED3D_TEXF_LINEAR;
2636 tex_mip = WINED3D_TEXF_POINT;
2637 break;
2638 case D3DFILTER_LINEARMIPNEAREST:
2639 tex_min = WINED3D_TEXF_POINT;
2640 tex_mip = WINED3D_TEXF_LINEAR;
2641 break;
2642 case D3DFILTER_LINEARMIPLINEAR:
2643 tex_min = WINED3D_TEXF_LINEAR;
2644 tex_mip = WINED3D_TEXF_LINEAR;
2645 break;
2647 default:
2648 FIXME("Unhandled texture min %#x.\n",value);
2649 tex_min = WINED3D_TEXF_POINT;
2650 tex_mip = WINED3D_TEXF_NONE;
2651 break;
2654 wined3d_device_set_sampler_state(device->wined3d_device,
2655 0, WINED3D_SAMP_MIP_FILTER, tex_mip);
2656 wined3d_device_set_sampler_state(device->wined3d_device,
2657 0, WINED3D_SAMP_MIN_FILTER, tex_min);
2658 break;
2661 case D3DRENDERSTATE_TEXTUREADDRESS:
2662 wined3d_device_set_sampler_state(device->wined3d_device,
2663 0, WINED3D_SAMP_ADDRESS_V, value);
2664 /* Drop through */
2665 case D3DRENDERSTATE_TEXTUREADDRESSU:
2666 wined3d_device_set_sampler_state(device->wined3d_device,
2667 0, WINED3D_SAMP_ADDRESS_U, value);
2668 break;
2669 case D3DRENDERSTATE_TEXTUREADDRESSV:
2670 wined3d_device_set_sampler_state(device->wined3d_device,
2671 0, WINED3D_SAMP_ADDRESS_V, value);
2672 break;
2674 case D3DRENDERSTATE_BORDERCOLOR:
2675 /* This should probably just forward to the corresponding sampler
2676 * state. Needs tests. */
2677 FIXME("Unhandled render state D3DRENDERSTATE_BORDERCOLOR.\n");
2678 hr = E_NOTIMPL;
2679 break;
2681 case D3DRENDERSTATE_TEXTUREHANDLE:
2682 case D3DRENDERSTATE_TEXTUREMAPBLEND:
2683 WARN("Render state %#x is invalid in d3d7.\n", state);
2684 hr = DDERR_INVALIDPARAMS;
2685 break;
2687 case D3DRENDERSTATE_ZBIAS:
2688 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
2689 break;
2691 default:
2692 if (state >= D3DRENDERSTATE_STIPPLEPATTERN00
2693 && state <= D3DRENDERSTATE_STIPPLEPATTERN31)
2695 FIXME("Unhandled stipple pattern render state (%#x).\n", state);
2696 hr = E_NOTIMPL;
2697 break;
2700 wined3d_device_set_render_state(device->wined3d_device, state, value);
2701 break;
2703 wined3d_mutex_unlock();
2705 return hr;
2708 static HRESULT WINAPI d3d_device7_SetRenderState_FPUSetup(IDirect3DDevice7 *iface,
2709 D3DRENDERSTATETYPE state, DWORD value)
2711 return d3d_device7_SetRenderState(iface, state, value);
2714 static HRESULT WINAPI d3d_device7_SetRenderState_FPUPreserve(IDirect3DDevice7 *iface,
2715 D3DRENDERSTATETYPE state, DWORD value)
2717 HRESULT hr;
2718 WORD old_fpucw;
2720 old_fpucw = d3d_fpu_setup();
2721 hr = d3d_device7_SetRenderState(iface, state, value);
2722 set_fpu_control_word(old_fpucw);
2724 return hr;
2727 static HRESULT WINAPI d3d_device3_SetRenderState(IDirect3DDevice3 *iface,
2728 D3DRENDERSTATETYPE state, DWORD value)
2730 /* Note about D3DRENDERSTATE_TEXTUREMAPBLEND implementation: most of values
2731 for this state can be directly mapped to texture stage colorop and alphaop, but
2732 D3DTBLEND_MODULATE is tricky: it uses alpha from texture when available and alpha
2733 from diffuse otherwise. So changing the texture must be monitored in SetTexture to modify
2734 alphaarg when needed.
2736 Aliens vs Predator 1 depends on accurate D3DTBLEND_MODULATE emulation
2738 Legacy texture blending (TEXTUREMAPBLEND) and texture stage states: directx6 docs state that
2739 TEXTUREMAPBLEND is deprecated, yet can still be used. Games must not use both or results
2740 are undefined. D3DTBLEND_MODULATE mode in particular is dependent on texture pixel format and
2741 requires fixup of stage 0 texture states when texture changes, but this fixup can interfere
2742 with games not using this deprecated state. So a flag 'legacyTextureBlending' has to be kept
2743 in device - TRUE if the app is using TEXTUREMAPBLEND.
2745 Tests show that setting TEXTUREMAPBLEND on native doesn't seem to change values returned by
2746 GetTextureStageState and vice versa. Not so on Wine, but it is 'undefined' anyway so, probably, ok,
2747 unless some broken game will be found that cares. */
2749 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
2750 HRESULT hr;
2752 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2754 if (state >= D3DSTATE_OVERRIDE_BIAS)
2756 WARN("Unhandled state %#x.\n", state);
2757 return DDERR_INVALIDPARAMS;
2760 wined3d_mutex_lock();
2762 switch (state)
2764 case D3DRENDERSTATE_TEXTUREHANDLE:
2766 struct ddraw_surface *surf;
2768 if (value == 0)
2770 hr = wined3d_device_set_texture(device->wined3d_device, 0, NULL);
2771 break;
2774 surf = ddraw_get_object(&device->handle_table, value - 1, DDRAW_HANDLE_SURFACE);
2775 if (!surf)
2777 WARN("Invalid texture handle.\n");
2778 hr = DDERR_INVALIDPARAMS;
2779 break;
2782 hr = IDirect3DDevice3_SetTexture(iface, 0, &surf->IDirect3DTexture2_iface);
2783 break;
2786 case D3DRENDERSTATE_TEXTUREMAPBLEND:
2788 device->legacyTextureBlending = TRUE;
2790 switch (value)
2792 case D3DTBLEND_MODULATE:
2794 struct wined3d_texture *tex = NULL;
2795 BOOL tex_alpha = FALSE;
2796 DDPIXELFORMAT ddfmt;
2798 if ((tex = wined3d_device_get_texture(device->wined3d_device, 0)))
2800 struct wined3d_resource_desc desc;
2802 wined3d_resource_get_desc(wined3d_texture_get_resource(tex), &desc);
2803 ddfmt.dwSize = sizeof(ddfmt);
2804 ddrawformat_from_wined3dformat(&ddfmt, desc.format);
2805 if (ddfmt.u5.dwRGBAlphaBitMask)
2806 tex_alpha = TRUE;
2809 if (tex_alpha)
2810 wined3d_device_set_texture_stage_state(device->wined3d_device,
2811 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_SELECT_ARG1);
2812 else
2813 wined3d_device_set_texture_stage_state(device->wined3d_device,
2814 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_SELECT_ARG2);
2815 wined3d_device_set_texture_stage_state(device->wined3d_device,
2816 0, WINED3D_TSS_ALPHA_ARG1, WINED3DTA_TEXTURE);
2817 wined3d_device_set_texture_stage_state(device->wined3d_device,
2818 0, WINED3D_TSS_ALPHA_ARG2, WINED3DTA_CURRENT);
2819 wined3d_device_set_texture_stage_state(device->wined3d_device,
2820 0, WINED3D_TSS_COLOR_ARG1, WINED3DTA_TEXTURE);
2821 wined3d_device_set_texture_stage_state(device->wined3d_device,
2822 0, WINED3D_TSS_COLOR_ARG2, WINED3DTA_CURRENT);
2823 wined3d_device_set_texture_stage_state(device->wined3d_device,
2824 0, WINED3D_TSS_COLOR_OP, WINED3D_TOP_MODULATE);
2825 break;
2828 case D3DTBLEND_ADD:
2829 wined3d_device_set_texture_stage_state(device->wined3d_device,
2830 0, WINED3D_TSS_COLOR_OP, WINED3D_TOP_ADD);
2831 wined3d_device_set_texture_stage_state(device->wined3d_device,
2832 0, WINED3D_TSS_COLOR_ARG1, WINED3DTA_TEXTURE);
2833 wined3d_device_set_texture_stage_state(device->wined3d_device,
2834 0, WINED3D_TSS_COLOR_ARG2, WINED3DTA_CURRENT);
2835 wined3d_device_set_texture_stage_state(device->wined3d_device,
2836 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_SELECT_ARG2);
2837 wined3d_device_set_texture_stage_state(device->wined3d_device,
2838 0, WINED3D_TSS_ALPHA_ARG2, WINED3DTA_CURRENT);
2839 break;
2841 case D3DTBLEND_MODULATEALPHA:
2842 wined3d_device_set_texture_stage_state(device->wined3d_device,
2843 0, WINED3D_TSS_COLOR_ARG1, WINED3DTA_TEXTURE);
2844 wined3d_device_set_texture_stage_state(device->wined3d_device,
2845 0, WINED3D_TSS_ALPHA_ARG1, WINED3DTA_TEXTURE);
2846 wined3d_device_set_texture_stage_state(device->wined3d_device,
2847 0, WINED3D_TSS_COLOR_ARG2, WINED3DTA_CURRENT);
2848 wined3d_device_set_texture_stage_state(device->wined3d_device,
2849 0, WINED3D_TSS_ALPHA_ARG2, WINED3DTA_CURRENT);
2850 wined3d_device_set_texture_stage_state(device->wined3d_device,
2851 0, WINED3D_TSS_COLOR_OP, WINED3D_TOP_MODULATE);
2852 wined3d_device_set_texture_stage_state(device->wined3d_device,
2853 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_MODULATE);
2854 break;
2856 case D3DTBLEND_COPY:
2857 case D3DTBLEND_DECAL:
2858 wined3d_device_set_texture_stage_state(device->wined3d_device,
2859 0, WINED3D_TSS_COLOR_ARG1, WINED3DTA_TEXTURE);
2860 wined3d_device_set_texture_stage_state(device->wined3d_device,
2861 0, WINED3D_TSS_ALPHA_ARG1, WINED3DTA_TEXTURE);
2862 wined3d_device_set_texture_stage_state(device->wined3d_device,
2863 0, WINED3D_TSS_COLOR_OP, WINED3D_TOP_SELECT_ARG1);
2864 wined3d_device_set_texture_stage_state(device->wined3d_device,
2865 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_SELECT_ARG1);
2866 break;
2868 case D3DTBLEND_DECALALPHA:
2869 wined3d_device_set_texture_stage_state(device->wined3d_device,
2870 0, WINED3D_TSS_COLOR_OP, WINED3D_TOP_BLEND_TEXTURE_ALPHA);
2871 wined3d_device_set_texture_stage_state(device->wined3d_device,
2872 0, WINED3D_TSS_COLOR_ARG1, WINED3DTA_TEXTURE);
2873 wined3d_device_set_texture_stage_state(device->wined3d_device,
2874 0, WINED3D_TSS_COLOR_ARG2, WINED3DTA_CURRENT);
2875 wined3d_device_set_texture_stage_state(device->wined3d_device,
2876 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_SELECT_ARG2);
2877 wined3d_device_set_texture_stage_state(device->wined3d_device,
2878 0, WINED3D_TSS_ALPHA_ARG2, WINED3DTA_CURRENT);
2879 break;
2881 default:
2882 FIXME("Unhandled texture environment %#x.\n", value);
2885 hr = D3D_OK;
2886 break;
2889 case D3DRENDERSTATE_LIGHTING:
2890 case D3DRENDERSTATE_NORMALIZENORMALS:
2891 case D3DRENDERSTATE_LOCALVIEWER:
2892 hr = D3D_OK;
2893 break;
2895 default:
2896 hr = IDirect3DDevice7_SetRenderState(&device->IDirect3DDevice7_iface, state, value);
2897 break;
2899 wined3d_mutex_unlock();
2901 return hr;
2904 static HRESULT WINAPI d3d_device2_SetRenderState(IDirect3DDevice2 *iface,
2905 D3DRENDERSTATETYPE state, DWORD value)
2907 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
2909 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2911 return IDirect3DDevice3_SetRenderState(&device->IDirect3DDevice3_iface, state, value);
2914 /*****************************************************************************
2915 * Direct3DDevice3::SetLightState
2917 * Sets a light state for Direct3DDevice3 and Direct3DDevice2. The
2918 * light states are forwarded to Direct3DDevice7 render states
2920 * Version 2 and 3
2922 * Params:
2923 * LightStateType: The light state to change
2924 * Value: The value to assign to that light state
2926 * Returns:
2927 * D3D_OK on success
2928 * DDERR_INVALIDPARAMS if the parameters were incorrect
2929 * Also check IDirect3DDevice7::SetRenderState
2931 *****************************************************************************/
2932 static HRESULT WINAPI d3d_device3_SetLightState(IDirect3DDevice3 *iface,
2933 D3DLIGHTSTATETYPE state, DWORD value)
2935 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
2936 HRESULT hr;
2938 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2940 if (!state || (state > D3DLIGHTSTATE_COLORVERTEX))
2942 TRACE("Unexpected Light State Type\n");
2943 return DDERR_INVALIDPARAMS;
2946 wined3d_mutex_lock();
2947 if (state == D3DLIGHTSTATE_MATERIAL)
2949 if (value)
2951 struct d3d_material *m;
2953 if (!(m = ddraw_get_object(&device->handle_table, value - 1, DDRAW_HANDLE_MATERIAL)))
2955 WARN("Invalid material handle.\n");
2956 wined3d_mutex_unlock();
2957 return DDERR_INVALIDPARAMS;
2960 material_activate(m);
2963 device->material = value;
2965 else if (state == D3DLIGHTSTATE_COLORMODEL)
2967 switch (value)
2969 case D3DCOLOR_MONO:
2970 ERR("DDCOLOR_MONO should not happen!\n");
2971 break;
2972 case D3DCOLOR_RGB:
2973 /* We are already in this mode */
2974 TRACE("Setting color model to RGB (no-op).\n");
2975 break;
2976 default:
2977 ERR("Unknown color model!\n");
2978 wined3d_mutex_unlock();
2979 return DDERR_INVALIDPARAMS;
2982 else
2984 D3DRENDERSTATETYPE rs;
2985 switch (state)
2987 case D3DLIGHTSTATE_AMBIENT: /* 2 */
2988 rs = D3DRENDERSTATE_AMBIENT;
2989 break;
2990 case D3DLIGHTSTATE_FOGMODE: /* 4 */
2991 rs = D3DRENDERSTATE_FOGVERTEXMODE;
2992 break;
2993 case D3DLIGHTSTATE_FOGSTART: /* 5 */
2994 rs = D3DRENDERSTATE_FOGSTART;
2995 break;
2996 case D3DLIGHTSTATE_FOGEND: /* 6 */
2997 rs = D3DRENDERSTATE_FOGEND;
2998 break;
2999 case D3DLIGHTSTATE_FOGDENSITY: /* 7 */
3000 rs = D3DRENDERSTATE_FOGDENSITY;
3001 break;
3002 case D3DLIGHTSTATE_COLORVERTEX: /* 8 */
3003 rs = D3DRENDERSTATE_COLORVERTEX;
3004 break;
3005 default:
3006 FIXME("Unhandled D3DLIGHTSTATETYPE %#x.\n", state);
3007 wined3d_mutex_unlock();
3008 return DDERR_INVALIDPARAMS;
3011 hr = IDirect3DDevice7_SetRenderState(&device->IDirect3DDevice7_iface, rs, value);
3012 wined3d_mutex_unlock();
3013 return hr;
3015 wined3d_mutex_unlock();
3017 return D3D_OK;
3020 static HRESULT WINAPI d3d_device2_SetLightState(IDirect3DDevice2 *iface,
3021 D3DLIGHTSTATETYPE state, DWORD value)
3023 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3025 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
3027 return d3d_device3_SetLightState(&device->IDirect3DDevice3_iface, state, value);
3030 /*****************************************************************************
3031 * IDirect3DDevice3::GetLightState
3033 * Returns the current setting of a light state. The state is read from
3034 * the Direct3DDevice7 render state.
3036 * Version 2 and 3
3038 * Params:
3039 * LightStateType: The light state to return
3040 * Value: The address to store the light state setting at
3042 * Returns:
3043 * D3D_OK on success
3044 * DDDERR_INVALIDPARAMS if the parameters were incorrect
3045 * Also see IDirect3DDevice7::GetRenderState
3047 *****************************************************************************/
3048 static HRESULT WINAPI d3d_device3_GetLightState(IDirect3DDevice3 *iface,
3049 D3DLIGHTSTATETYPE state, DWORD *value)
3051 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3052 HRESULT hr;
3054 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
3056 if (!state || (state > D3DLIGHTSTATE_COLORVERTEX))
3058 TRACE("Unexpected Light State Type\n");
3059 return DDERR_INVALIDPARAMS;
3062 if (!value)
3063 return DDERR_INVALIDPARAMS;
3065 wined3d_mutex_lock();
3066 if (state == D3DLIGHTSTATE_MATERIAL)
3068 *value = device->material;
3070 else if (state == D3DLIGHTSTATE_COLORMODEL)
3072 *value = D3DCOLOR_RGB;
3074 else
3076 D3DRENDERSTATETYPE rs;
3077 switch (state)
3079 case D3DLIGHTSTATE_AMBIENT: /* 2 */
3080 rs = D3DRENDERSTATE_AMBIENT;
3081 break;
3082 case D3DLIGHTSTATE_FOGMODE: /* 4 */
3083 rs = D3DRENDERSTATE_FOGVERTEXMODE;
3084 break;
3085 case D3DLIGHTSTATE_FOGSTART: /* 5 */
3086 rs = D3DRENDERSTATE_FOGSTART;
3087 break;
3088 case D3DLIGHTSTATE_FOGEND: /* 6 */
3089 rs = D3DRENDERSTATE_FOGEND;
3090 break;
3091 case D3DLIGHTSTATE_FOGDENSITY: /* 7 */
3092 rs = D3DRENDERSTATE_FOGDENSITY;
3093 break;
3094 case D3DLIGHTSTATE_COLORVERTEX: /* 8 */
3095 rs = D3DRENDERSTATE_COLORVERTEX;
3096 break;
3097 default:
3098 FIXME("Unhandled D3DLIGHTSTATETYPE %#x.\n", state);
3099 wined3d_mutex_unlock();
3100 return DDERR_INVALIDPARAMS;
3103 hr = IDirect3DDevice7_GetRenderState(&device->IDirect3DDevice7_iface, rs, value);
3104 wined3d_mutex_unlock();
3105 return hr;
3107 wined3d_mutex_unlock();
3109 return D3D_OK;
3112 static HRESULT WINAPI d3d_device2_GetLightState(IDirect3DDevice2 *iface,
3113 D3DLIGHTSTATETYPE state, DWORD *value)
3115 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3117 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
3119 return d3d_device3_GetLightState(&device->IDirect3DDevice3_iface, state, value);
3122 /*****************************************************************************
3123 * IDirect3DDevice7::SetTransform
3125 * Assigns a D3DMATRIX to a transform type. The transform types are defined
3126 * in include/d3dtypes.h.
3127 * The D3DTRANSFORMSTATE_WORLD (=1) is translated to D3DTS_WORLDMATRIX(0)
3128 * (=255) for wined3d, because the 1 transform state was removed in d3d8
3129 * and WineD3D already understands the replacement D3DTS_WORLDMATRIX(0)
3131 * Version 2, 3 and 7
3133 * Params:
3134 * TransformStateType: transform state to set
3135 * Matrix: Matrix to assign to the state
3137 * Returns:
3138 * D3D_OK on success
3139 * DDERR_INVALIDPARAMS if Matrix == NULL
3140 * For details see IWineD3DDevice::SetTransform
3142 *****************************************************************************/
3143 static HRESULT d3d_device7_SetTransform(IDirect3DDevice7 *iface,
3144 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3146 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
3147 enum wined3d_transform_state wined3d_state;
3149 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3151 switch (state)
3153 case D3DTRANSFORMSTATE_WORLD:
3154 wined3d_state = WINED3D_TS_WORLD_MATRIX(0);
3155 break;
3156 case D3DTRANSFORMSTATE_WORLD1:
3157 wined3d_state = WINED3D_TS_WORLD_MATRIX(1);
3158 break;
3159 case D3DTRANSFORMSTATE_WORLD2:
3160 wined3d_state = WINED3D_TS_WORLD_MATRIX(2);
3161 break;
3162 case D3DTRANSFORMSTATE_WORLD3:
3163 wined3d_state = WINED3D_TS_WORLD_MATRIX(3);
3164 break;
3165 default:
3166 wined3d_state = state;
3169 if (!matrix)
3170 return DDERR_INVALIDPARAMS;
3172 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
3173 wined3d_mutex_lock();
3174 wined3d_device_set_transform(device->wined3d_device, wined3d_state, (struct wined3d_matrix *)matrix);
3175 wined3d_mutex_unlock();
3177 return D3D_OK;
3180 static HRESULT WINAPI d3d_device7_SetTransform_FPUSetup(IDirect3DDevice7 *iface,
3181 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3183 return d3d_device7_SetTransform(iface, state, matrix);
3186 static HRESULT WINAPI d3d_device7_SetTransform_FPUPreserve(IDirect3DDevice7 *iface,
3187 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3189 HRESULT hr;
3190 WORD old_fpucw;
3192 old_fpucw = d3d_fpu_setup();
3193 hr = d3d_device7_SetTransform(iface, state, matrix);
3194 set_fpu_control_word(old_fpucw);
3196 return hr;
3199 static HRESULT WINAPI d3d_device3_SetTransform(IDirect3DDevice3 *iface,
3200 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3202 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3204 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3206 if (!matrix)
3207 return DDERR_INVALIDPARAMS;
3209 if (state == D3DTRANSFORMSTATE_PROJECTION)
3211 D3DMATRIX projection;
3213 wined3d_mutex_lock();
3214 multiply_matrix(&projection, &device->legacy_clipspace, matrix);
3215 wined3d_device_set_transform(device->wined3d_device,
3216 WINED3D_TS_PROJECTION, (struct wined3d_matrix *)&projection);
3217 device->legacy_projection = *matrix;
3218 wined3d_mutex_unlock();
3220 return D3D_OK;
3223 return IDirect3DDevice7_SetTransform(&device->IDirect3DDevice7_iface, state, matrix);
3226 static HRESULT WINAPI d3d_device2_SetTransform(IDirect3DDevice2 *iface,
3227 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3229 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3231 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3233 return IDirect3DDevice7_SetTransform(&device->IDirect3DDevice7_iface, state, matrix);
3236 /*****************************************************************************
3237 * IDirect3DDevice7::GetTransform
3239 * Returns the matrix assigned to a transform state
3240 * D3DTRANSFORMSTATE_WORLD is translated to D3DTS_WORLDMATRIX(0), see
3241 * SetTransform
3243 * Params:
3244 * TransformStateType: State to read the matrix from
3245 * Matrix: Address to store the matrix at
3247 * Returns:
3248 * D3D_OK on success
3249 * DDERR_INVALIDPARAMS if Matrix == NULL
3250 * For details, see IWineD3DDevice::GetTransform
3252 *****************************************************************************/
3253 static HRESULT d3d_device7_GetTransform(IDirect3DDevice7 *iface,
3254 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3256 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
3257 enum wined3d_transform_state wined3d_state;
3259 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3261 switch (state)
3263 case D3DTRANSFORMSTATE_WORLD:
3264 wined3d_state = WINED3D_TS_WORLD_MATRIX(0);
3265 break;
3266 case D3DTRANSFORMSTATE_WORLD1:
3267 wined3d_state = WINED3D_TS_WORLD_MATRIX(1);
3268 break;
3269 case D3DTRANSFORMSTATE_WORLD2:
3270 wined3d_state = WINED3D_TS_WORLD_MATRIX(2);
3271 break;
3272 case D3DTRANSFORMSTATE_WORLD3:
3273 wined3d_state = WINED3D_TS_WORLD_MATRIX(3);
3274 break;
3275 default:
3276 wined3d_state = state;
3279 if (!matrix)
3280 return DDERR_INVALIDPARAMS;
3282 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
3283 wined3d_mutex_lock();
3284 wined3d_device_get_transform(device->wined3d_device, wined3d_state, (struct wined3d_matrix *)matrix);
3285 wined3d_mutex_unlock();
3287 return D3D_OK;
3290 static HRESULT WINAPI d3d_device7_GetTransform_FPUSetup(IDirect3DDevice7 *iface,
3291 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3293 return d3d_device7_GetTransform(iface, state, matrix);
3296 static HRESULT WINAPI d3d_device7_GetTransform_FPUPreserve(IDirect3DDevice7 *iface,
3297 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3299 HRESULT hr;
3300 WORD old_fpucw;
3302 old_fpucw = d3d_fpu_setup();
3303 hr = d3d_device7_GetTransform(iface, state, matrix);
3304 set_fpu_control_word(old_fpucw);
3306 return hr;
3309 static HRESULT WINAPI d3d_device3_GetTransform(IDirect3DDevice3 *iface,
3310 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3312 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3314 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3316 if (!matrix)
3317 return DDERR_INVALIDPARAMS;
3319 if (state == D3DTRANSFORMSTATE_PROJECTION)
3321 wined3d_mutex_lock();
3322 *matrix = device->legacy_projection;
3323 wined3d_mutex_unlock();
3324 return DD_OK;
3327 return IDirect3DDevice7_GetTransform(&device->IDirect3DDevice7_iface, state, matrix);
3330 static HRESULT WINAPI d3d_device2_GetTransform(IDirect3DDevice2 *iface,
3331 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3333 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3335 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3337 return IDirect3DDevice7_GetTransform(&device->IDirect3DDevice7_iface, state, matrix);
3340 /*****************************************************************************
3341 * IDirect3DDevice7::MultiplyTransform
3343 * Multiplies the already-set transform matrix of a transform state
3344 * with another matrix. For the world matrix, see SetTransform
3346 * Version 2, 3 and 7
3348 * Params:
3349 * TransformStateType: Transform state to multiply
3350 * D3DMatrix Matrix to multiply with.
3352 * Returns
3353 * D3D_OK on success
3354 * DDERR_INVALIDPARAMS if D3DMatrix is NULL
3355 * For details, see IWineD3DDevice::MultiplyTransform
3357 *****************************************************************************/
3358 static HRESULT d3d_device7_MultiplyTransform(IDirect3DDevice7 *iface,
3359 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3361 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
3362 enum wined3d_transform_state wined3d_state;
3364 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3366 switch (state)
3368 case D3DTRANSFORMSTATE_WORLD:
3369 wined3d_state = WINED3D_TS_WORLD_MATRIX(0);
3370 break;
3371 case D3DTRANSFORMSTATE_WORLD1:
3372 wined3d_state = WINED3D_TS_WORLD_MATRIX(1);
3373 break;
3374 case D3DTRANSFORMSTATE_WORLD2:
3375 wined3d_state = WINED3D_TS_WORLD_MATRIX(2);
3376 break;
3377 case D3DTRANSFORMSTATE_WORLD3:
3378 wined3d_state = WINED3D_TS_WORLD_MATRIX(3);
3379 break;
3380 default:
3381 wined3d_state = state;
3384 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
3385 wined3d_mutex_lock();
3386 wined3d_device_multiply_transform(device->wined3d_device,
3387 wined3d_state, (struct wined3d_matrix *)matrix);
3388 wined3d_mutex_unlock();
3390 return D3D_OK;
3393 static HRESULT WINAPI d3d_device7_MultiplyTransform_FPUSetup(IDirect3DDevice7 *iface,
3394 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3396 return d3d_device7_MultiplyTransform(iface, state, matrix);
3399 static HRESULT WINAPI d3d_device7_MultiplyTransform_FPUPreserve(IDirect3DDevice7 *iface,
3400 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3402 HRESULT hr;
3403 WORD old_fpucw;
3405 old_fpucw = d3d_fpu_setup();
3406 hr = d3d_device7_MultiplyTransform(iface, state, matrix);
3407 set_fpu_control_word(old_fpucw);
3409 return hr;
3412 static HRESULT WINAPI d3d_device3_MultiplyTransform(IDirect3DDevice3 *iface,
3413 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3415 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3417 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3419 if (state == D3DTRANSFORMSTATE_PROJECTION)
3421 D3DMATRIX projection, tmp;
3423 wined3d_mutex_lock();
3424 multiply_matrix(&tmp, &device->legacy_projection, matrix);
3425 multiply_matrix(&projection, &device->legacy_clipspace, &tmp);
3426 wined3d_device_set_transform(device->wined3d_device,
3427 WINED3D_TS_PROJECTION, (struct wined3d_matrix *)&projection);
3428 device->legacy_projection = tmp;
3429 wined3d_mutex_unlock();
3431 return D3D_OK;
3434 return IDirect3DDevice7_MultiplyTransform(&device->IDirect3DDevice7_iface, state, matrix);
3437 static HRESULT WINAPI d3d_device2_MultiplyTransform(IDirect3DDevice2 *iface,
3438 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
3440 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3442 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
3444 return IDirect3DDevice7_MultiplyTransform(&device->IDirect3DDevice7_iface, state, matrix);
3447 /*****************************************************************************
3448 * IDirect3DDevice7::DrawPrimitive
3450 * Draws primitives based on vertices in an application-provided pointer
3452 * Version 2, 3 and 7. The IDirect3DDevice2 thunk converts the fixed vertex type into
3453 * an FVF format for D3D7
3455 * Params:
3456 * PrimitiveType: The type of the primitives to draw
3457 * Vertex type: Flexible vertex format vertex description
3458 * Vertices: Pointer to the vertex array
3459 * VertexCount: The number of vertices to draw
3460 * Flags: As usual a few flags
3462 * Returns:
3463 * D3D_OK on success
3464 * DDERR_INVALIDPARAMS if Vertices is NULL
3465 * For details, see IWineD3DDevice::DrawPrimitiveUP
3467 *****************************************************************************/
3469 /* The caller is responsible for wined3d locking */
3470 static HRESULT d3d_device_prepare_vertex_buffer(struct d3d_device *device, UINT min_size)
3472 HRESULT hr;
3474 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
3476 UINT size = max(device->vertex_buffer_size * 2, min_size);
3477 struct wined3d_buffer *buffer;
3479 TRACE("Growing vertex buffer to %u bytes\n", size);
3481 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
3482 WINED3D_POOL_DEFAULT, NULL, &ddraw_null_wined3d_parent_ops, &buffer);
3483 if (FAILED(hr))
3485 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
3486 return hr;
3489 if (device->vertex_buffer)
3490 wined3d_buffer_decref(device->vertex_buffer);
3492 device->vertex_buffer = buffer;
3493 device->vertex_buffer_size = size;
3494 device->vertex_buffer_pos = 0;
3496 return D3D_OK;
3499 static HRESULT d3d_device7_DrawPrimitive(IDirect3DDevice7 *iface,
3500 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices,
3501 DWORD vertex_count, DWORD flags)
3503 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
3504 UINT stride, vb_pos, size, align;
3505 HRESULT hr;
3506 BYTE *data;
3508 TRACE("iface %p, primitive_type %#x, fvf %#x, vertices %p, vertex_count %u, flags %#x.\n",
3509 iface, primitive_type, fvf, vertices, vertex_count, flags);
3511 if (!vertices)
3512 return DDERR_INVALIDPARAMS;
3514 /* Get the stride */
3515 stride = get_flexible_vertex_size(fvf);
3516 size = vertex_count * stride;
3518 wined3d_mutex_lock();
3519 hr = d3d_device_prepare_vertex_buffer(device, size);
3520 if (FAILED(hr))
3521 goto done;
3523 vb_pos = device->vertex_buffer_pos;
3524 align = vb_pos % stride;
3525 if (align) align = stride - align;
3526 if (vb_pos + size + align > device->vertex_buffer_size)
3527 vb_pos = 0;
3528 else
3529 vb_pos += align;
3531 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &data,
3532 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
3533 if (FAILED(hr))
3534 goto done;
3535 memcpy(data, vertices, size);
3536 wined3d_buffer_unmap(device->vertex_buffer);
3537 device->vertex_buffer_pos = vb_pos + size;
3539 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
3540 if (FAILED(hr))
3541 goto done;
3543 wined3d_device_set_vertex_declaration(device->wined3d_device, ddraw_find_decl(device->ddraw, fvf));
3544 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
3545 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vertex_count);
3547 done:
3548 wined3d_mutex_unlock();
3549 return hr;
3552 static HRESULT WINAPI d3d_device7_DrawPrimitive_FPUSetup(IDirect3DDevice7 *iface,
3553 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices,
3554 DWORD vertex_count, DWORD flags)
3556 return d3d_device7_DrawPrimitive(iface, primitive_type, fvf, vertices, vertex_count, flags);
3559 static HRESULT WINAPI d3d_device7_DrawPrimitive_FPUPreserve(IDirect3DDevice7 *iface,
3560 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices,
3561 DWORD vertex_count, DWORD flags)
3563 HRESULT hr;
3564 WORD old_fpucw;
3566 old_fpucw = d3d_fpu_setup();
3567 hr = d3d_device7_DrawPrimitive(iface, primitive_type, fvf, vertices, vertex_count, flags);
3568 set_fpu_control_word(old_fpucw);
3570 return hr;
3573 static void setup_lighting(const struct d3d_device *device, DWORD fvf, DWORD flags)
3575 BOOL enable = TRUE;
3577 /* Ignore the D3DFVF_XYZRHW case here, wined3d takes care of that */
3578 if (!device->material || !(fvf & D3DFVF_NORMAL) || (flags & D3DDP_DONOTLIGHT))
3579 enable = FALSE;
3581 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_LIGHTING, enable);
3585 static HRESULT WINAPI d3d_device3_DrawPrimitive(IDirect3DDevice3 *iface,
3586 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices, DWORD vertex_count,
3587 DWORD flags)
3589 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3591 TRACE("iface %p, primitive_type %#x, fvf %#x, vertices %p, vertex_count %u, flags %#x.\n",
3592 iface, primitive_type, fvf, vertices, vertex_count, flags);
3594 setup_lighting(device, fvf, flags);
3596 return IDirect3DDevice7_DrawPrimitive(&device->IDirect3DDevice7_iface,
3597 primitive_type, fvf, vertices, vertex_count, flags);
3600 static HRESULT WINAPI d3d_device2_DrawPrimitive(IDirect3DDevice2 *iface,
3601 D3DPRIMITIVETYPE primitive_type, D3DVERTEXTYPE vertex_type, void *vertices,
3602 DWORD vertex_count, DWORD flags)
3604 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3605 DWORD fvf;
3607 TRACE("iface %p, primitive_type %#x, vertex_type %#x, vertices %p, vertex_count %u, flags %#x.\n",
3608 iface, primitive_type, vertex_type, vertices, vertex_count, flags);
3610 switch (vertex_type)
3612 case D3DVT_VERTEX: fvf = D3DFVF_VERTEX; break;
3613 case D3DVT_LVERTEX: fvf = D3DFVF_LVERTEX; break;
3614 case D3DVT_TLVERTEX: fvf = D3DFVF_TLVERTEX; break;
3615 default:
3616 FIXME("Unhandled vertex type %#x.\n", vertex_type);
3617 return DDERR_INVALIDPARAMS; /* Should never happen */
3620 return d3d_device3_DrawPrimitive(&device->IDirect3DDevice3_iface,
3621 primitive_type, fvf, vertices, vertex_count, flags);
3624 /*****************************************************************************
3625 * IDirect3DDevice7::DrawIndexedPrimitive
3627 * Draws vertices from an application-provided pointer, based on the index
3628 * numbers in a WORD array.
3630 * Version 2, 3 and 7. The version 7 thunk translates the vertex type into
3631 * an FVF format for D3D7
3633 * Params:
3634 * PrimitiveType: The primitive type to draw
3635 * VertexType: The FVF vertex description
3636 * Vertices: Pointer to the vertex array
3637 * VertexCount: ?
3638 * Indices: Pointer to the index array
3639 * IndexCount: Number of indices = Number of vertices to draw
3640 * Flags: As usual, some flags
3642 * Returns:
3643 * D3D_OK on success
3644 * DDERR_INVALIDPARAMS if Vertices or Indices is NULL
3645 * For details, see IWineD3DDevice::DrawIndexedPrimitiveUP
3647 *****************************************************************************/
3648 /* The caller is responsible for wined3d locking */
3649 static HRESULT d3d_device_prepare_index_buffer(struct d3d_device *device, UINT min_size)
3651 HRESULT hr;
3653 if (device->index_buffer_size < min_size || !device->index_buffer)
3655 UINT size = max(device->index_buffer_size * 2, min_size);
3656 struct wined3d_buffer *buffer;
3658 TRACE("Growing index buffer to %u bytes\n", size);
3660 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
3661 WINED3D_POOL_DEFAULT, NULL, &ddraw_null_wined3d_parent_ops, &buffer);
3662 if (FAILED(hr))
3664 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
3665 return hr;
3668 if (device->index_buffer)
3669 wined3d_buffer_decref(device->index_buffer);
3670 device->index_buffer = buffer;
3671 device->index_buffer_size = size;
3672 device->index_buffer_pos = 0;
3674 return D3D_OK;
3677 static HRESULT d3d_device7_DrawIndexedPrimitive(IDirect3DDevice7 *iface,
3678 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices, DWORD vertex_count,
3679 WORD *indices, DWORD index_count, DWORD flags)
3681 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
3682 HRESULT hr;
3683 UINT stride = get_flexible_vertex_size(fvf);
3684 UINT vtx_size = stride * vertex_count, idx_size = index_count * sizeof(*indices);
3685 UINT vb_pos, ib_pos, align;
3686 BYTE *data;
3688 TRACE("iface %p, primitive_type %#x, fvf %#x, vertices %p, vertex_count %u, "
3689 "indices %p, index_count %u, flags %#x.\n",
3690 iface, primitive_type, fvf, vertices, vertex_count, indices, index_count, flags);
3692 /* Set the D3DDevice's FVF */
3693 wined3d_mutex_lock();
3695 hr = d3d_device_prepare_vertex_buffer(device, vtx_size);
3696 if (FAILED(hr))
3697 goto done;
3699 vb_pos = device->vertex_buffer_pos;
3700 align = vb_pos % stride;
3701 if (align) align = stride - align;
3702 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
3703 vb_pos = 0;
3704 else
3705 vb_pos += align;
3707 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &data,
3708 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
3709 if (FAILED(hr))
3710 goto done;
3711 memcpy(data, vertices, vtx_size);
3712 wined3d_buffer_unmap(device->vertex_buffer);
3713 device->vertex_buffer_pos = vb_pos + vtx_size;
3715 hr = d3d_device_prepare_index_buffer(device, idx_size);
3716 if (FAILED(hr))
3717 goto done;
3718 ib_pos = device->index_buffer_pos;
3719 if (device->index_buffer_size - idx_size < ib_pos)
3720 ib_pos = 0;
3722 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &data,
3723 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
3724 if (FAILED(hr))
3725 goto done;
3726 memcpy(data, indices, idx_size);
3727 wined3d_buffer_unmap(device->index_buffer);
3728 device->index_buffer_pos = ib_pos + idx_size;
3730 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
3731 if (FAILED(hr))
3732 goto done;
3733 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer, WINED3DFMT_R16_UINT);
3735 wined3d_device_set_vertex_declaration(device->wined3d_device, ddraw_find_decl(device->ddraw, fvf));
3736 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
3737 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / stride);
3738 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / sizeof(*indices), index_count);
3740 done:
3741 wined3d_mutex_unlock();
3742 return hr;
3745 static HRESULT WINAPI d3d_device7_DrawIndexedPrimitive_FPUSetup(IDirect3DDevice7 *iface,
3746 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices, DWORD vertex_count,
3747 WORD *indices, DWORD index_count, DWORD flags)
3749 return d3d_device7_DrawIndexedPrimitive(iface, primitive_type, fvf,
3750 vertices, vertex_count, indices, index_count, flags);
3753 static HRESULT WINAPI d3d_device7_DrawIndexedPrimitive_FPUPreserve(IDirect3DDevice7 *iface,
3754 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices, DWORD vertex_count,
3755 WORD *indices, DWORD index_count, DWORD flags)
3757 HRESULT hr;
3758 WORD old_fpucw;
3760 old_fpucw = d3d_fpu_setup();
3761 hr = d3d_device7_DrawIndexedPrimitive(iface, primitive_type, fvf,
3762 vertices, vertex_count, indices, index_count, flags);
3763 set_fpu_control_word(old_fpucw);
3765 return hr;
3768 static HRESULT WINAPI d3d_device3_DrawIndexedPrimitive(IDirect3DDevice3 *iface,
3769 D3DPRIMITIVETYPE primitive_type, DWORD fvf, void *vertices, DWORD vertex_count,
3770 WORD *indices, DWORD index_count, DWORD flags)
3772 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3774 TRACE("iface %p, primitive_type %#x, fvf %#x, vertices %p, vertex_count %u, "
3775 "indices %p, index_count %u, flags %#x.\n",
3776 iface, primitive_type, fvf, vertices, vertex_count, indices, index_count, flags);
3778 setup_lighting(device, fvf, flags);
3780 return IDirect3DDevice7_DrawIndexedPrimitive(&device->IDirect3DDevice7_iface,
3781 primitive_type, fvf, vertices, vertex_count, indices, index_count, flags);
3784 static HRESULT WINAPI d3d_device2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
3785 D3DPRIMITIVETYPE primitive_type, D3DVERTEXTYPE vertex_type, void *vertices,
3786 DWORD vertex_count, WORD *indices, DWORD index_count, DWORD flags)
3788 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3789 DWORD fvf;
3791 TRACE("iface %p, primitive_type %#x, vertex_type %#x, vertices %p, vertex_count %u, "
3792 "indices %p, index_count %u, flags %#x.\n",
3793 iface, primitive_type, vertex_type, vertices, vertex_count, indices, index_count, flags);
3795 switch (vertex_type)
3797 case D3DVT_VERTEX: fvf = D3DFVF_VERTEX; break;
3798 case D3DVT_LVERTEX: fvf = D3DFVF_LVERTEX; break;
3799 case D3DVT_TLVERTEX: fvf = D3DFVF_TLVERTEX; break;
3800 default:
3801 ERR("Unhandled vertex type %#x.\n", vertex_type);
3802 return DDERR_INVALIDPARAMS; /* Should never happen */
3805 return d3d_device3_DrawIndexedPrimitive(&device->IDirect3DDevice3_iface,
3806 primitive_type, fvf, vertices, vertex_count, indices, index_count, flags);
3809 /*****************************************************************************
3810 * IDirect3DDevice3::End
3812 * Ends a draw begun with IDirect3DDevice3::Begin or
3813 * IDirect3DDevice::BeginIndexed. The vertices specified with
3814 * IDirect3DDevice::Vertex or IDirect3DDevice::Index are drawn using
3815 * the IDirect3DDevice3::DrawPrimitive method. So far only
3816 * non-indexed mode is supported
3818 * Version 2 and 3
3820 * Params:
3821 * Flags: Some flags, as usual. Don't know which are defined
3823 * Returns:
3824 * The return value of IDirect3DDevice3::DrawPrimitive
3826 *****************************************************************************/
3827 static HRESULT WINAPI d3d_device3_End(IDirect3DDevice3 *iface, DWORD flags)
3829 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3831 TRACE("iface %p, flags %#x.\n", iface, flags);
3833 return d3d_device3_DrawPrimitive(&device->IDirect3DDevice3_iface, device->primitive_type,
3834 device->vertex_type, device->sysmem_vertex_buffer, device->nb_vertices, device->render_flags);
3837 static HRESULT WINAPI d3d_device2_End(IDirect3DDevice2 *iface, DWORD flags)
3839 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3841 TRACE("iface %p, flags %#x.\n", iface, flags);
3843 return d3d_device3_End(&device->IDirect3DDevice3_iface, flags);
3846 /*****************************************************************************
3847 * IDirect3DDevice7::SetClipStatus
3849 * Sets the clip status. This defines things as clipping conditions and
3850 * the extents of the clipping region.
3852 * Version 2, 3 and 7
3854 * Params:
3855 * ClipStatus:
3857 * Returns:
3858 * D3D_OK because it's a stub
3859 * (DDERR_INVALIDPARAMS if ClipStatus == NULL)
3861 *****************************************************************************/
3862 static HRESULT WINAPI d3d_device7_SetClipStatus(IDirect3DDevice7 *iface, D3DCLIPSTATUS *clip_status)
3864 FIXME("iface %p, clip_status %p stub!\n", iface, clip_status);
3866 /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them
3867 * Perhaps this needs a new data type and an additional IWineD3DDevice method
3869 /* return IWineD3DDevice_SetClipStatus(This->wineD3DDevice, ClipStatus);*/
3870 return D3D_OK;
3873 static HRESULT WINAPI d3d_device3_SetClipStatus(IDirect3DDevice3 *iface, D3DCLIPSTATUS *clip_status)
3875 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3877 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
3879 return IDirect3DDevice7_SetClipStatus(&device->IDirect3DDevice7_iface, clip_status);
3882 static HRESULT WINAPI d3d_device2_SetClipStatus(IDirect3DDevice2 *iface, D3DCLIPSTATUS *clip_status)
3884 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3886 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
3888 return IDirect3DDevice7_SetClipStatus(&device->IDirect3DDevice7_iface, clip_status);
3891 /*****************************************************************************
3892 * IDirect3DDevice7::GetClipStatus
3894 * Returns the clip status
3896 * Params:
3897 * ClipStatus: Address to write the clip status to
3899 * Returns:
3900 * D3D_OK because it's a stub
3902 *****************************************************************************/
3903 static HRESULT WINAPI d3d_device7_GetClipStatus(IDirect3DDevice7 *iface, D3DCLIPSTATUS *clip_status)
3905 FIXME("iface %p, clip_status %p stub!\n", iface, clip_status);
3907 /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them */
3908 /* return IWineD3DDevice_GetClipStatus(This->wineD3DDevice, ClipStatus);*/
3909 return D3D_OK;
3912 static HRESULT WINAPI d3d_device3_GetClipStatus(IDirect3DDevice3 *iface, D3DCLIPSTATUS *clip_status)
3914 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
3916 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
3918 return IDirect3DDevice7_GetClipStatus(&device->IDirect3DDevice7_iface, clip_status);
3921 static HRESULT WINAPI d3d_device2_GetClipStatus(IDirect3DDevice2 *iface, D3DCLIPSTATUS *clip_status)
3923 struct d3d_device *device = impl_from_IDirect3DDevice2(iface);
3925 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
3927 return IDirect3DDevice7_GetClipStatus(&device->IDirect3DDevice7_iface, clip_status);
3930 /*****************************************************************************
3931 * IDirect3DDevice::DrawPrimitiveStrided
3933 * Draws vertices described by a D3DDRAWPRIMITIVESTRIDEDDATA structure.
3935 * Version 3 and 7
3937 * Params:
3938 * PrimitiveType: The primitive type to draw
3939 * VertexType: The FVF description of the vertices to draw (for the stride??)
3940 * D3DDrawPrimStrideData: A D3DDRAWPRIMITIVESTRIDEDDATA structure describing
3941 * the vertex data locations
3942 * VertexCount: The number of vertices to draw
3943 * Flags: Some flags
3945 * Returns:
3946 * D3D_OK, because it's a stub
3947 * (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3948 * (For details, see IWineD3DDevice::DrawPrimitiveStrided)
3950 *****************************************************************************/
3951 static void pack_strided_data(BYTE *dst, DWORD count, const D3DDRAWPRIMITIVESTRIDEDDATA *src, DWORD fvf)
3953 DWORD i, tex, offset;
3955 for (i = 0; i < count; i++)
3957 /* The contents of the strided data are determined by the fvf,
3958 * not by the members set in src. So it's valid
3959 * to have diffuse.lpvData set to 0xdeadbeef if the diffuse flag is
3960 * not set in the fvf. */
3961 if (fvf & D3DFVF_POSITION_MASK)
3963 offset = i * src->position.dwStride;
3964 if (fvf & D3DFVF_XYZRHW)
3966 memcpy(dst, ((BYTE *)src->position.lpvData) + offset, 4 * sizeof(float));
3967 dst += 4 * sizeof(float);
3969 else
3971 memcpy(dst, ((BYTE *)src->position.lpvData) + offset, 3 * sizeof(float));
3972 dst += 3 * sizeof(float);
3976 if (fvf & D3DFVF_NORMAL)
3978 offset = i * src->normal.dwStride;
3979 memcpy(dst, ((BYTE *)src->normal.lpvData) + offset, 3 * sizeof(float));
3980 dst += 3 * sizeof(float);
3983 if (fvf & D3DFVF_DIFFUSE)
3985 offset = i * src->diffuse.dwStride;
3986 memcpy(dst, ((BYTE *)src->diffuse.lpvData) + offset, sizeof(DWORD));
3987 dst += sizeof(DWORD);
3990 if (fvf & D3DFVF_SPECULAR)
3992 offset = i * src->specular.dwStride;
3993 memcpy(dst, ((BYTE *)src->specular.lpvData) + offset, sizeof(DWORD));
3994 dst += sizeof(DWORD);
3997 for (tex = 0; tex < GET_TEXCOUNT_FROM_FVF(fvf); ++tex)
3999 DWORD attrib_count = GET_TEXCOORD_SIZE_FROM_FVF(fvf, tex);
4000 offset = i * src->textureCoords[tex].dwStride;
4001 memcpy(dst, ((BYTE *)src->textureCoords[tex].lpvData) + offset, attrib_count * sizeof(float));
4002 dst += attrib_count * sizeof(float);
4007 static HRESULT d3d_device7_DrawPrimitiveStrided(IDirect3DDevice7 *iface, D3DPRIMITIVETYPE PrimitiveType,
4008 DWORD VertexType, D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount, DWORD Flags)
4010 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
4011 HRESULT hr;
4012 UINT dst_stride = get_flexible_vertex_size(VertexType);
4013 UINT dst_size = dst_stride * VertexCount;
4014 UINT vb_pos, align;
4015 BYTE *dst_data;
4017 TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, flags %#x.\n",
4018 iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
4020 wined3d_mutex_lock();
4021 hr = d3d_device_prepare_vertex_buffer(device, dst_size);
4022 if (FAILED(hr))
4023 goto done;
4025 vb_pos = device->vertex_buffer_pos;
4026 align = vb_pos % dst_stride;
4027 if (align) align = dst_stride - align;
4028 if (vb_pos + dst_size + align > device->vertex_buffer_size)
4029 vb_pos = 0;
4030 else
4031 vb_pos += align;
4033 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, dst_size, &dst_data,
4034 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
4035 if (FAILED(hr))
4036 goto done;
4037 pack_strided_data(dst_data, VertexCount, D3DDrawPrimStrideData, VertexType);
4038 wined3d_buffer_unmap(device->vertex_buffer);
4039 device->vertex_buffer_pos = vb_pos + dst_size;
4041 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, dst_stride);
4042 if (FAILED(hr))
4043 goto done;
4044 wined3d_device_set_vertex_declaration(device->wined3d_device, ddraw_find_decl(device->ddraw, VertexType));
4046 wined3d_device_set_primitive_type(device->wined3d_device, PrimitiveType);
4047 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / dst_stride, VertexCount);
4049 done:
4050 wined3d_mutex_unlock();
4051 return hr;
4054 static HRESULT WINAPI d3d_device7_DrawPrimitiveStrided_FPUSetup(IDirect3DDevice7 *iface,
4055 D3DPRIMITIVETYPE PrimitiveType, DWORD VertexType,
4056 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount, DWORD Flags)
4058 return d3d_device7_DrawPrimitiveStrided(iface, PrimitiveType,
4059 VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
4062 static HRESULT WINAPI d3d_device7_DrawPrimitiveStrided_FPUPreserve(IDirect3DDevice7 *iface,
4063 D3DPRIMITIVETYPE PrimitiveType, DWORD VertexType,
4064 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount, DWORD Flags)
4066 HRESULT hr;
4067 WORD old_fpucw;
4069 old_fpucw = d3d_fpu_setup();
4070 hr = d3d_device7_DrawPrimitiveStrided(iface, PrimitiveType,
4071 VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
4072 set_fpu_control_word(old_fpucw);
4074 return hr;
4077 static HRESULT WINAPI d3d_device3_DrawPrimitiveStrided(IDirect3DDevice3 *iface,
4078 D3DPRIMITIVETYPE PrimitiveType, DWORD VertexType,
4079 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount, DWORD Flags)
4081 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4083 TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, flags %#x.\n",
4084 iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
4086 setup_lighting(device, VertexType, Flags);
4088 return IDirect3DDevice7_DrawPrimitiveStrided(&device->IDirect3DDevice7_iface,
4089 PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
4092 /*****************************************************************************
4093 * IDirect3DDevice7::DrawIndexedPrimitiveStrided
4095 * Draws primitives specified by strided data locations based on indices
4097 * Version 3 and 7
4099 * Params:
4100 * PrimitiveType:
4102 * Returns:
4103 * D3D_OK, because it's a stub
4104 * (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
4105 * (DDERR_INVALIDPARAMS if Indices is NULL)
4106 * (For more details, see IWineD3DDevice::DrawIndexedPrimitiveStrided)
4108 *****************************************************************************/
4109 static HRESULT d3d_device7_DrawIndexedPrimitiveStrided(IDirect3DDevice7 *iface,
4110 D3DPRIMITIVETYPE PrimitiveType, DWORD VertexType,
4111 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount,
4112 WORD *Indices, DWORD IndexCount, DWORD Flags)
4114 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
4115 HRESULT hr;
4116 UINT vtx_dst_stride = get_flexible_vertex_size(VertexType);
4117 UINT vtx_dst_size = VertexCount * vtx_dst_stride;
4118 UINT vb_pos, align;
4119 UINT idx_size = IndexCount * sizeof(WORD);
4120 UINT ib_pos;
4121 BYTE *dst_data;
4123 TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
4124 iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
4126 wined3d_mutex_lock();
4128 hr = d3d_device_prepare_vertex_buffer(device, vtx_dst_size);
4129 if (FAILED(hr))
4130 goto done;
4132 vb_pos = device->vertex_buffer_pos;
4133 align = vb_pos % vtx_dst_stride;
4134 if (align) align = vtx_dst_stride - align;
4135 if (vb_pos + vtx_dst_size + align > device->vertex_buffer_size)
4136 vb_pos = 0;
4137 else
4138 vb_pos += align;
4140 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_dst_size, &dst_data,
4141 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
4142 if (FAILED(hr))
4143 goto done;
4144 pack_strided_data(dst_data, VertexCount, D3DDrawPrimStrideData, VertexType);
4145 wined3d_buffer_unmap(device->vertex_buffer);
4146 device->vertex_buffer_pos = vb_pos + vtx_dst_size;
4148 hr = d3d_device_prepare_index_buffer(device, idx_size);
4149 if (FAILED(hr))
4150 goto done;
4151 ib_pos = device->index_buffer_pos;
4152 if (device->index_buffer_size - idx_size < ib_pos)
4153 ib_pos = 0;
4155 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &dst_data,
4156 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
4157 if (FAILED(hr))
4158 goto done;
4159 memcpy(dst_data, Indices, idx_size);
4160 wined3d_buffer_unmap(device->index_buffer);
4161 device->index_buffer_pos = ib_pos + idx_size;
4163 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vtx_dst_stride);
4164 if (FAILED(hr))
4165 goto done;
4166 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer, WINED3DFMT_R16_UINT);
4167 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vtx_dst_stride);
4169 wined3d_device_set_vertex_declaration(device->wined3d_device, ddraw_find_decl(device->ddraw, VertexType));
4170 wined3d_device_set_primitive_type(device->wined3d_device, PrimitiveType);
4171 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / sizeof(WORD), IndexCount);
4173 done:
4174 wined3d_mutex_unlock();
4175 return hr;
4178 static HRESULT WINAPI d3d_device7_DrawIndexedPrimitiveStrided_FPUSetup(IDirect3DDevice7 *iface,
4179 D3DPRIMITIVETYPE PrimitiveType, DWORD VertexType,
4180 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount,
4181 WORD *Indices, DWORD IndexCount, DWORD Flags)
4183 return d3d_device7_DrawIndexedPrimitiveStrided(iface, PrimitiveType, VertexType,
4184 D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
4187 static HRESULT WINAPI d3d_device7_DrawIndexedPrimitiveStrided_FPUPreserve(IDirect3DDevice7 *iface,
4188 D3DPRIMITIVETYPE PrimitiveType, DWORD VertexType,
4189 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount,
4190 WORD *Indices, DWORD IndexCount, DWORD Flags)
4192 HRESULT hr;
4193 WORD old_fpucw;
4195 old_fpucw = d3d_fpu_setup();
4196 hr = d3d_device7_DrawIndexedPrimitiveStrided(iface, PrimitiveType, VertexType,
4197 D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
4198 set_fpu_control_word(old_fpucw);
4200 return hr;
4203 static HRESULT WINAPI d3d_device3_DrawIndexedPrimitiveStrided(IDirect3DDevice3 *iface,
4204 D3DPRIMITIVETYPE PrimitiveType, DWORD VertexType,
4205 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData, DWORD VertexCount, WORD *Indices,
4206 DWORD IndexCount, DWORD Flags)
4208 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4210 TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
4211 iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
4213 setup_lighting(device, VertexType, Flags);
4215 return IDirect3DDevice7_DrawIndexedPrimitiveStrided(&device->IDirect3DDevice7_iface,
4216 PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
4219 /*****************************************************************************
4220 * IDirect3DDevice7::DrawPrimitiveVB
4222 * Draws primitives from a vertex buffer to the screen.
4224 * Version 3 and 7
4226 * Params:
4227 * PrimitiveType: Type of primitive to be rendered.
4228 * D3DVertexBuf: Source Vertex Buffer
4229 * StartVertex: Index of the first vertex from the buffer to be rendered
4230 * NumVertices: Number of vertices to be rendered
4231 * Flags: Can be D3DDP_WAIT to wait until rendering has finished
4233 * Return values
4234 * D3D_OK on success
4235 * DDERR_INVALIDPARAMS if D3DVertexBuf is NULL
4237 *****************************************************************************/
4238 static HRESULT d3d_device7_DrawPrimitiveVB(IDirect3DDevice7 *iface, D3DPRIMITIVETYPE PrimitiveType,
4239 IDirect3DVertexBuffer7 *D3DVertexBuf, DWORD StartVertex, DWORD NumVertices, DWORD Flags)
4241 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
4242 struct d3d_vertex_buffer *vb = unsafe_impl_from_IDirect3DVertexBuffer7(D3DVertexBuf);
4243 HRESULT hr;
4244 DWORD stride;
4246 TRACE("iface %p, primitive_type %#x, vb %p, start_vertex %u, vertex_count %u, flags %#x.\n",
4247 iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
4249 /* Sanity checks */
4250 if (!vb)
4252 WARN("No Vertex buffer specified.\n");
4253 return DDERR_INVALIDPARAMS;
4255 stride = get_flexible_vertex_size(vb->fvf);
4257 wined3d_mutex_lock();
4258 wined3d_device_set_vertex_declaration(device->wined3d_device, vb->wineD3DVertexDeclaration);
4259 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, vb->wineD3DVertexBuffer, 0, stride);
4260 if (FAILED(hr))
4262 WARN("Failed to set stream source, hr %#x.\n", hr);
4263 wined3d_mutex_unlock();
4264 return hr;
4267 /* Now draw the primitives */
4268 wined3d_device_set_primitive_type(device->wined3d_device, PrimitiveType);
4269 hr = wined3d_device_draw_primitive(device->wined3d_device, StartVertex, NumVertices);
4271 wined3d_mutex_unlock();
4273 return hr;
4276 static HRESULT WINAPI d3d_device7_DrawPrimitiveVB_FPUSetup(IDirect3DDevice7 *iface, D3DPRIMITIVETYPE PrimitiveType,
4277 IDirect3DVertexBuffer7 *D3DVertexBuf, DWORD StartVertex, DWORD NumVertices, DWORD Flags)
4279 return d3d_device7_DrawPrimitiveVB(iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
4282 static HRESULT WINAPI d3d_device7_DrawPrimitiveVB_FPUPreserve(IDirect3DDevice7 *iface, D3DPRIMITIVETYPE PrimitiveType,
4283 IDirect3DVertexBuffer7 *D3DVertexBuf, DWORD StartVertex, DWORD NumVertices, DWORD Flags)
4285 HRESULT hr;
4286 WORD old_fpucw;
4288 old_fpucw = d3d_fpu_setup();
4289 hr = d3d_device7_DrawPrimitiveVB(iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
4290 set_fpu_control_word(old_fpucw);
4292 return hr;
4295 static HRESULT WINAPI d3d_device3_DrawPrimitiveVB(IDirect3DDevice3 *iface, D3DPRIMITIVETYPE PrimitiveType,
4296 IDirect3DVertexBuffer *D3DVertexBuf, DWORD StartVertex, DWORD NumVertices, DWORD Flags)
4298 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4299 struct d3d_vertex_buffer *vb = unsafe_impl_from_IDirect3DVertexBuffer(D3DVertexBuf);
4301 TRACE("iface %p, primitive_type %#x, vb %p, start_vertex %u, vertex_count %u, flags %#x.\n",
4302 iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
4304 setup_lighting(device, vb->fvf, Flags);
4306 return IDirect3DDevice7_DrawPrimitiveVB(&device->IDirect3DDevice7_iface,
4307 PrimitiveType, &vb->IDirect3DVertexBuffer7_iface, StartVertex, NumVertices, Flags);
4310 /*****************************************************************************
4311 * IDirect3DDevice7::DrawIndexedPrimitiveVB
4313 * Draws primitives from a vertex buffer to the screen
4315 * Params:
4316 * PrimitiveType: Type of primitive to be rendered.
4317 * D3DVertexBuf: Source Vertex Buffer
4318 * StartVertex: Index of the first vertex from the buffer to be rendered
4319 * NumVertices: Number of vertices to be rendered
4320 * Indices: Array of DWORDs used to index into the Vertices
4321 * IndexCount: Number of indices in Indices
4322 * Flags: Can be D3DDP_WAIT to wait until rendering has finished
4324 * Return values
4326 *****************************************************************************/
4327 static HRESULT d3d_device7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
4328 D3DPRIMITIVETYPE PrimitiveType, IDirect3DVertexBuffer7 *D3DVertexBuf,
4329 DWORD StartVertex, DWORD NumVertices, WORD *Indices, DWORD IndexCount, DWORD Flags)
4331 struct d3d_device *This = impl_from_IDirect3DDevice7(iface);
4332 struct d3d_vertex_buffer *vb = unsafe_impl_from_IDirect3DVertexBuffer7(D3DVertexBuf);
4333 DWORD stride = get_flexible_vertex_size(vb->fvf);
4334 WORD *LockedIndices;
4335 HRESULT hr;
4336 UINT ib_pos;
4338 TRACE("iface %p, primitive_type %#x, vb %p, start_vertex %u, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
4339 iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Indices, IndexCount, Flags);
4341 /* Steps:
4342 * 1) Upload the Indices to the index buffer
4343 * 2) Set the index source
4344 * 3) Set the Vertex Buffer as the Stream source
4345 * 4) Call IWineD3DDevice::DrawIndexedPrimitive
4348 wined3d_mutex_lock();
4350 wined3d_device_set_vertex_declaration(This->wined3d_device, vb->wineD3DVertexDeclaration);
4352 hr = d3d_device_prepare_index_buffer(This, IndexCount * sizeof(WORD));
4353 if (FAILED(hr))
4355 wined3d_mutex_unlock();
4356 return hr;
4358 ib_pos = This->index_buffer_pos;
4360 if (This->index_buffer_size - IndexCount * sizeof(WORD) < ib_pos)
4361 ib_pos = 0;
4363 /* Copy the index stream into the index buffer. A new IWineD3DDevice
4364 * method could be created which takes an user pointer containing the
4365 * indices or a SetData-Method for the index buffer, which overrides the
4366 * index buffer data with our pointer. */
4367 hr = wined3d_buffer_map(This->index_buffer, ib_pos, IndexCount * sizeof(WORD),
4368 (BYTE **)&LockedIndices, ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
4369 if (FAILED(hr))
4371 ERR("Failed to map buffer, hr %#x.\n", hr);
4372 wined3d_mutex_unlock();
4373 return hr;
4375 memcpy(LockedIndices, Indices, IndexCount * sizeof(WORD));
4376 wined3d_buffer_unmap(This->index_buffer);
4377 This->index_buffer_pos = ib_pos + IndexCount * sizeof(WORD);
4379 /* Set the index stream */
4380 wined3d_device_set_base_vertex_index(This->wined3d_device, StartVertex);
4381 wined3d_device_set_index_buffer(This->wined3d_device, This->index_buffer, WINED3DFMT_R16_UINT);
4383 /* Set the vertex stream source */
4384 hr = wined3d_device_set_stream_source(This->wined3d_device, 0, vb->wineD3DVertexBuffer, 0, stride);
4385 if (FAILED(hr))
4387 ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
4388 wined3d_mutex_unlock();
4389 return hr;
4393 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
4394 hr = wined3d_device_draw_indexed_primitive(This->wined3d_device, ib_pos / sizeof(WORD), IndexCount);
4396 wined3d_mutex_unlock();
4398 return hr;
4401 static HRESULT WINAPI d3d_device7_DrawIndexedPrimitiveVB_FPUSetup(IDirect3DDevice7 *iface,
4402 D3DPRIMITIVETYPE PrimitiveType, IDirect3DVertexBuffer7 *D3DVertexBuf,
4403 DWORD StartVertex, DWORD NumVertices, WORD *Indices, DWORD IndexCount, DWORD Flags)
4405 return d3d_device7_DrawIndexedPrimitiveVB(iface, PrimitiveType,
4406 D3DVertexBuf, StartVertex, NumVertices, Indices, IndexCount, Flags);
4409 static HRESULT WINAPI d3d_device7_DrawIndexedPrimitiveVB_FPUPreserve(IDirect3DDevice7 *iface,
4410 D3DPRIMITIVETYPE PrimitiveType, IDirect3DVertexBuffer7 *D3DVertexBuf,
4411 DWORD StartVertex, DWORD NumVertices, WORD *Indices, DWORD IndexCount, DWORD Flags)
4413 HRESULT hr;
4414 WORD old_fpucw;
4416 old_fpucw = d3d_fpu_setup();
4417 hr = d3d_device7_DrawIndexedPrimitiveVB(iface, PrimitiveType,
4418 D3DVertexBuf, StartVertex, NumVertices, Indices, IndexCount, Flags);
4419 set_fpu_control_word(old_fpucw);
4421 return hr;
4424 static HRESULT WINAPI d3d_device3_DrawIndexedPrimitiveVB(IDirect3DDevice3 *iface,
4425 D3DPRIMITIVETYPE PrimitiveType, IDirect3DVertexBuffer *D3DVertexBuf, WORD *Indices,
4426 DWORD IndexCount, DWORD Flags)
4428 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4429 struct d3d_vertex_buffer *vb = unsafe_impl_from_IDirect3DVertexBuffer(D3DVertexBuf);
4431 TRACE("iface %p, primitive_type %#x, vb %p, indices %p, index_count %u, flags %#x.\n",
4432 iface, PrimitiveType, D3DVertexBuf, Indices, IndexCount, Flags);
4434 setup_lighting(device, vb->fvf, Flags);
4436 return IDirect3DDevice7_DrawIndexedPrimitiveVB(&device->IDirect3DDevice7_iface, PrimitiveType,
4437 &vb->IDirect3DVertexBuffer7_iface, 0, IndexCount, Indices, IndexCount, Flags);
4440 /*****************************************************************************
4441 * IDirect3DDevice7::ComputeSphereVisibility
4443 * Calculates the visibility of spheres in the current viewport. The spheres
4444 * are passed in the Centers and Radii arrays, the results are passed back
4445 * in the ReturnValues array. Return values are either completely visible,
4446 * partially visible or completely invisible.
4447 * The return value consists of a combination of D3DCLIP_* flags, or is
4448 * 0 if the sphere is completely visible (according to the SDK, not checked)
4450 * Version 3 and 7
4452 * Params:
4453 * Centers: Array containing the sphere centers
4454 * Radii: Array containing the sphere radii
4455 * NumSpheres: The number of centers and radii in the arrays
4456 * Flags: Some flags
4457 * ReturnValues: Array to write the results to
4459 * Returns:
4460 * D3D_OK
4461 * (DDERR_INVALIDPARAMS if Centers, Radii or ReturnValues are NULL)
4462 * (D3DERR_INVALIDMATRIX if the combined world, view and proj matrix
4463 * is singular)
4465 *****************************************************************************/
4467 static DWORD in_plane(UINT plane, D3DVECTOR normal, D3DVALUE origin_plane, D3DVECTOR center, D3DVALUE radius)
4469 float distance, norm;
4471 norm = sqrtf(normal.u1.x * normal.u1.x + normal.u2.y * normal.u2.y + normal.u3.z * normal.u3.z);
4472 distance = ( origin_plane + normal.u1.x * center.u1.x + normal.u2.y * center.u2.y + normal.u3.z * center.u3.z ) / norm;
4474 if ( fabs( distance ) < radius ) return D3DSTATUS_CLIPUNIONLEFT << plane;
4475 if ( distance < -radius ) return (D3DSTATUS_CLIPUNIONLEFT | D3DSTATUS_CLIPINTERSECTIONLEFT) << plane;
4476 return 0;
4479 static HRESULT WINAPI d3d_device7_ComputeSphereVisibility(IDirect3DDevice7 *iface,
4480 D3DVECTOR *centers, D3DVALUE *radii, DWORD sphere_count, DWORD flags, DWORD *return_values)
4482 D3DMATRIX m, temp;
4483 D3DVALUE origin_plane[6];
4484 D3DVECTOR vec[6];
4485 HRESULT hr;
4486 UINT i, j;
4488 TRACE("iface %p, centers %p, radii %p, sphere_count %u, flags %#x, return_values %p.\n",
4489 iface, centers, radii, sphere_count, flags, return_values);
4491 hr = d3d_device7_GetTransform(iface, D3DTRANSFORMSTATE_WORLD, &m);
4492 if ( hr != DD_OK ) return DDERR_INVALIDPARAMS;
4493 hr = d3d_device7_GetTransform(iface, D3DTRANSFORMSTATE_VIEW, &temp);
4494 if ( hr != DD_OK ) return DDERR_INVALIDPARAMS;
4495 multiply_matrix(&m, &temp, &m);
4497 hr = d3d_device7_GetTransform(iface, D3DTRANSFORMSTATE_PROJECTION, &temp);
4498 if ( hr != DD_OK ) return DDERR_INVALIDPARAMS;
4499 multiply_matrix(&m, &temp, &m);
4501 /* Left plane */
4502 vec[0].u1.x = m._14 + m._11;
4503 vec[0].u2.y = m._24 + m._21;
4504 vec[0].u3.z = m._34 + m._31;
4505 origin_plane[0] = m._44 + m._41;
4507 /* Right plane */
4508 vec[1].u1.x = m._14 - m._11;
4509 vec[1].u2.y = m._24 - m._21;
4510 vec[1].u3.z = m._34 - m._31;
4511 origin_plane[1] = m._44 - m._41;
4513 /* Top plane */
4514 vec[2].u1.x = m._14 - m._12;
4515 vec[2].u2.y = m._24 - m._22;
4516 vec[2].u3.z = m._34 - m._32;
4517 origin_plane[2] = m._44 - m._42;
4519 /* Bottom plane */
4520 vec[3].u1.x = m._14 + m._12;
4521 vec[3].u2.y = m._24 + m._22;
4522 vec[3].u3.z = m._34 + m._32;
4523 origin_plane[3] = m._44 + m._42;
4525 /* Front plane */
4526 vec[4].u1.x = m._13;
4527 vec[4].u2.y = m._23;
4528 vec[4].u3.z = m._33;
4529 origin_plane[4] = m._43;
4531 /* Back plane*/
4532 vec[5].u1.x = m._14 - m._13;
4533 vec[5].u2.y = m._24 - m._23;
4534 vec[5].u3.z = m._34 - m._33;
4535 origin_plane[5] = m._44 - m._43;
4537 for (i = 0; i < sphere_count; ++i)
4539 return_values[i] = 0;
4540 for (j = 0; j < 6; ++j)
4541 return_values[i] |= in_plane(j, vec[j], origin_plane[j], centers[i], radii[i]);
4544 return D3D_OK;
4547 static HRESULT WINAPI d3d_device3_ComputeSphereVisibility(IDirect3DDevice3 *iface,
4548 D3DVECTOR *centers, D3DVALUE *radii, DWORD sphere_count, DWORD flags, DWORD *return_values)
4550 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4552 TRACE("iface %p, centers %p, radii %p, sphere_count %u, flags %#x, return_values %p.\n",
4553 iface, centers, radii, sphere_count, flags, return_values);
4555 return IDirect3DDevice7_ComputeSphereVisibility(&device->IDirect3DDevice7_iface,
4556 centers, radii, sphere_count, flags, return_values);
4559 /*****************************************************************************
4560 * IDirect3DDevice7::GetTexture
4562 * Returns the texture interface handle assigned to a texture stage.
4563 * The returned texture is AddRefed. This is taken from old ddraw,
4564 * not checked in Windows.
4566 * Version 3 and 7
4568 * Params:
4569 * Stage: Texture stage to read the texture from
4570 * Texture: Address to store the interface pointer at
4572 * Returns:
4573 * D3D_OK on success
4574 * DDERR_INVALIDPARAMS if Texture is NULL
4575 * For details, see IWineD3DDevice::GetTexture
4577 *****************************************************************************/
4578 static HRESULT d3d_device7_GetTexture(IDirect3DDevice7 *iface,
4579 DWORD stage, IDirectDrawSurface7 **texture)
4581 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
4582 struct wined3d_texture *wined3d_texture;
4583 struct ddraw_texture *ddraw_texture;
4585 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
4587 if (!texture)
4588 return DDERR_INVALIDPARAMS;
4590 wined3d_mutex_lock();
4591 if (!(wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
4593 *texture = NULL;
4594 wined3d_mutex_unlock();
4595 return D3D_OK;
4598 ddraw_texture = wined3d_texture_get_parent(wined3d_texture);
4599 *texture = &ddraw_texture->root->IDirectDrawSurface7_iface;
4600 IDirectDrawSurface7_AddRef(*texture);
4601 wined3d_mutex_unlock();
4603 return D3D_OK;
4606 static HRESULT WINAPI d3d_device7_GetTexture_FPUSetup(IDirect3DDevice7 *iface,
4607 DWORD stage, IDirectDrawSurface7 **Texture)
4609 return d3d_device7_GetTexture(iface, stage, Texture);
4612 static HRESULT WINAPI d3d_device7_GetTexture_FPUPreserve(IDirect3DDevice7 *iface,
4613 DWORD stage, IDirectDrawSurface7 **Texture)
4615 HRESULT hr;
4616 WORD old_fpucw;
4618 old_fpucw = d3d_fpu_setup();
4619 hr = d3d_device7_GetTexture(iface, stage, Texture);
4620 set_fpu_control_word(old_fpucw);
4622 return hr;
4625 static HRESULT WINAPI d3d_device3_GetTexture(IDirect3DDevice3 *iface, DWORD stage, IDirect3DTexture2 **Texture2)
4627 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4628 struct ddraw_surface *ret_val_impl;
4629 HRESULT ret;
4630 IDirectDrawSurface7 *ret_val;
4632 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, Texture2);
4634 ret = IDirect3DDevice7_GetTexture(&device->IDirect3DDevice7_iface, stage, &ret_val);
4636 ret_val_impl = unsafe_impl_from_IDirectDrawSurface7(ret_val);
4637 *Texture2 = ret_val_impl ? &ret_val_impl->IDirect3DTexture2_iface : NULL;
4639 TRACE("Returning texture %p.\n", *Texture2);
4641 return ret;
4644 /*****************************************************************************
4645 * IDirect3DDevice7::SetTexture
4647 * Assigns a texture to a texture stage. Is the texture AddRef-ed?
4649 * Version 3 and 7
4651 * Params:
4652 * Stage: The stage to assign the texture to
4653 * Texture: Interface pointer to the texture surface
4655 * Returns
4656 * D3D_OK on success
4657 * For details, see IWineD3DDevice::SetTexture
4659 *****************************************************************************/
4660 static HRESULT d3d_device7_SetTexture(IDirect3DDevice7 *iface,
4661 DWORD stage, IDirectDrawSurface7 *texture)
4663 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
4664 struct ddraw_surface *surf = unsafe_impl_from_IDirectDrawSurface7(texture);
4665 struct wined3d_texture *wined3d_texture = NULL;
4666 HRESULT hr;
4668 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
4670 if (surf && (surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
4671 wined3d_texture = surf->wined3d_texture;
4673 wined3d_mutex_lock();
4674 hr = wined3d_device_set_texture(device->wined3d_device, stage, wined3d_texture);
4675 wined3d_mutex_unlock();
4677 return hr;
4680 static HRESULT WINAPI d3d_device7_SetTexture_FPUSetup(IDirect3DDevice7 *iface,
4681 DWORD stage, IDirectDrawSurface7 *texture)
4683 return d3d_device7_SetTexture(iface, stage, texture);
4686 static HRESULT WINAPI d3d_device7_SetTexture_FPUPreserve(IDirect3DDevice7 *iface,
4687 DWORD stage, IDirectDrawSurface7 *texture)
4689 HRESULT hr;
4690 WORD old_fpucw;
4692 old_fpucw = d3d_fpu_setup();
4693 hr = d3d_device7_SetTexture(iface, stage, texture);
4694 set_fpu_control_word(old_fpucw);
4696 return hr;
4699 static HRESULT WINAPI d3d_device3_SetTexture(IDirect3DDevice3 *iface,
4700 DWORD stage, IDirect3DTexture2 *texture)
4702 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4703 struct ddraw_surface *tex = unsafe_impl_from_IDirect3DTexture2(texture);
4704 DWORD texmapblend;
4705 HRESULT hr;
4707 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
4709 wined3d_mutex_lock();
4711 if (device->legacyTextureBlending)
4712 IDirect3DDevice3_GetRenderState(iface, D3DRENDERSTATE_TEXTUREMAPBLEND, &texmapblend);
4714 hr = IDirect3DDevice7_SetTexture(&device->IDirect3DDevice7_iface, stage, &tex->IDirectDrawSurface7_iface);
4716 if (device->legacyTextureBlending && texmapblend == D3DTBLEND_MODULATE)
4718 /* This fixup is required by the way D3DTBLEND_MODULATE maps to texture stage states.
4719 See d3d_device3_SetRenderState() for details. */
4720 struct wined3d_texture *tex = NULL;
4721 BOOL tex_alpha = FALSE;
4722 DDPIXELFORMAT ddfmt;
4724 if ((tex = wined3d_device_get_texture(device->wined3d_device, 0)))
4726 struct wined3d_resource_desc desc;
4728 wined3d_resource_get_desc(wined3d_texture_get_resource(tex), &desc);
4729 ddfmt.dwSize = sizeof(ddfmt);
4730 ddrawformat_from_wined3dformat(&ddfmt, desc.format);
4731 if (ddfmt.u5.dwRGBAlphaBitMask)
4732 tex_alpha = TRUE;
4735 /* Args 1 and 2 are already set to WINED3DTA_TEXTURE/WINED3DTA_CURRENT in case of D3DTBLEND_MODULATE */
4736 if (tex_alpha)
4737 wined3d_device_set_texture_stage_state(device->wined3d_device,
4738 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_SELECT_ARG1);
4739 else
4740 wined3d_device_set_texture_stage_state(device->wined3d_device,
4741 0, WINED3D_TSS_ALPHA_OP, WINED3D_TOP_SELECT_ARG2);
4744 wined3d_mutex_unlock();
4746 return hr;
4749 static const struct tss_lookup
4751 BOOL sampler_state;
4752 enum wined3d_texture_stage_state state;
4754 tss_lookup[] =
4756 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
4757 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
4758 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
4759 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
4760 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
4761 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
4762 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
4763 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
4764 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
4765 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
4766 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
4767 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
4768 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 12, D3DTSS_ADDRESS */
4769 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
4770 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
4771 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
4772 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
4773 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
4774 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
4775 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
4776 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
4777 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
4778 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
4779 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
4780 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
4783 /*****************************************************************************
4784 * IDirect3DDevice7::GetTextureStageState
4786 * Retrieves a state from a texture stage.
4788 * Version 3 and 7
4790 * Params:
4791 * Stage: The stage to retrieve the state from
4792 * TexStageStateType: The state type to retrieve
4793 * State: Address to store the state's value at
4795 * Returns:
4796 * D3D_OK on success
4797 * DDERR_INVALIDPARAMS if State is NULL
4798 * For details, see IWineD3DDevice::GetTextureStageState
4800 *****************************************************************************/
4801 static HRESULT d3d_device7_GetTextureStageState(IDirect3DDevice7 *iface,
4802 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
4804 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
4805 const struct tss_lookup *l;
4807 TRACE("iface %p, stage %u, state %#x, value %p.\n",
4808 iface, stage, state, value);
4810 if (!value)
4811 return DDERR_INVALIDPARAMS;
4813 if (state > D3DTSS_TEXTURETRANSFORMFLAGS)
4815 WARN("Invalid state %#x passed.\n", state);
4816 return DD_OK;
4819 l = &tss_lookup[state];
4821 wined3d_mutex_lock();
4823 if (l->sampler_state)
4825 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
4827 switch (state)
4829 /* Mipfilter is a sampler state with different values */
4830 case D3DTSS_MIPFILTER:
4832 switch (*value)
4834 case WINED3D_TEXF_NONE:
4835 *value = D3DTFP_NONE;
4836 break;
4837 case WINED3D_TEXF_POINT:
4838 *value = D3DTFP_POINT;
4839 break;
4840 case WINED3D_TEXF_LINEAR:
4841 *value = D3DTFP_LINEAR;
4842 break;
4843 default:
4844 ERR("Unexpected mipfilter value %#x.\n", *value);
4845 *value = D3DTFP_NONE;
4846 break;
4848 break;
4851 /* Magfilter has slightly different values */
4852 case D3DTSS_MAGFILTER:
4854 switch (*value)
4856 case WINED3D_TEXF_POINT:
4857 *value = D3DTFG_POINT;
4858 break;
4859 case WINED3D_TEXF_LINEAR:
4860 *value = D3DTFG_LINEAR;
4861 break;
4862 case WINED3D_TEXF_ANISOTROPIC:
4863 *value = D3DTFG_ANISOTROPIC;
4864 break;
4865 case WINED3D_TEXF_FLAT_CUBIC:
4866 *value = D3DTFG_FLATCUBIC;
4867 break;
4868 case WINED3D_TEXF_GAUSSIAN_CUBIC:
4869 *value = D3DTFG_GAUSSIANCUBIC;
4870 break;
4871 default:
4872 ERR("Unexpected wined3d mag filter value %#x.\n", *value);
4873 *value = D3DTFG_POINT;
4874 break;
4876 break;
4879 default:
4880 break;
4883 else
4885 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
4888 wined3d_mutex_unlock();
4890 return D3D_OK;
4893 static HRESULT WINAPI d3d_device7_GetTextureStageState_FPUSetup(IDirect3DDevice7 *iface,
4894 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
4896 return d3d_device7_GetTextureStageState(iface, stage, state, value);
4899 static HRESULT WINAPI d3d_device7_GetTextureStageState_FPUPreserve(IDirect3DDevice7 *iface,
4900 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
4902 HRESULT hr;
4903 WORD old_fpucw;
4905 old_fpucw = d3d_fpu_setup();
4906 hr = d3d_device7_GetTextureStageState(iface, stage, state, value);
4907 set_fpu_control_word(old_fpucw);
4909 return hr;
4912 static HRESULT WINAPI d3d_device3_GetTextureStageState(IDirect3DDevice3 *iface,
4913 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
4915 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
4917 TRACE("iface %p, stage %u, state %#x, value %p.\n",
4918 iface, stage, state, value);
4920 return IDirect3DDevice7_GetTextureStageState(&device->IDirect3DDevice7_iface, stage, state, value);
4923 /*****************************************************************************
4924 * IDirect3DDevice7::SetTextureStageState
4926 * Sets a texture stage state. Some stage types need to be handled specially,
4927 * because they do not exist in WineD3D and were moved to another place
4929 * Version 3 and 7
4931 * Params:
4932 * Stage: The stage to modify
4933 * TexStageStateType: The state to change
4934 * State: The new value for the state
4936 * Returns:
4937 * D3D_OK on success
4938 * For details, see IWineD3DDevice::SetTextureStageState
4940 *****************************************************************************/
4941 static HRESULT d3d_device7_SetTextureStageState(IDirect3DDevice7 *iface,
4942 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
4944 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
4945 const struct tss_lookup *l;
4947 TRACE("iface %p, stage %u, state %#x, value %#x.\n",
4948 iface, stage, state, value);
4950 if (state > D3DTSS_TEXTURETRANSFORMFLAGS)
4952 WARN("Invalid state %#x passed.\n", state);
4953 return DD_OK;
4956 l = &tss_lookup[state];
4958 wined3d_mutex_lock();
4960 if (l->sampler_state)
4962 switch (state)
4964 /* Mipfilter is a sampler state with different values */
4965 case D3DTSS_MIPFILTER:
4967 switch (value)
4969 case D3DTFP_NONE:
4970 value = WINED3D_TEXF_NONE;
4971 break;
4972 case D3DTFP_POINT:
4973 value = WINED3D_TEXF_POINT;
4974 break;
4975 case 0: /* Unchecked */
4976 case D3DTFP_LINEAR:
4977 value = WINED3D_TEXF_LINEAR;
4978 break;
4979 default:
4980 ERR("Unexpected mipfilter value %#x.\n", value);
4981 value = WINED3D_TEXF_NONE;
4982 break;
4984 break;
4987 /* Magfilter has slightly different values */
4988 case D3DTSS_MAGFILTER:
4990 switch (value)
4992 case D3DTFG_POINT:
4993 value = WINED3D_TEXF_POINT;
4994 break;
4995 case D3DTFG_LINEAR:
4996 value = WINED3D_TEXF_LINEAR;
4997 break;
4998 case D3DTFG_FLATCUBIC:
4999 value = WINED3D_TEXF_FLAT_CUBIC;
5000 break;
5001 case D3DTFG_GAUSSIANCUBIC:
5002 value = WINED3D_TEXF_GAUSSIAN_CUBIC;
5003 break;
5004 case D3DTFG_ANISOTROPIC:
5005 value = WINED3D_TEXF_ANISOTROPIC;
5006 break;
5007 default:
5008 ERR("Unexpected d3d7 mag filter value %#x.\n", value);
5009 value = WINED3D_TEXF_POINT;
5010 break;
5012 break;
5015 case D3DTSS_ADDRESS:
5016 wined3d_device_set_sampler_state(device->wined3d_device, stage, WINED3D_SAMP_ADDRESS_V, value);
5017 break;
5019 default:
5020 break;
5023 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
5025 else
5027 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
5030 wined3d_mutex_unlock();
5032 return D3D_OK;
5035 static HRESULT WINAPI d3d_device7_SetTextureStageState_FPUSetup(IDirect3DDevice7 *iface,
5036 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
5038 return d3d_device7_SetTextureStageState(iface, stage, state, value);
5041 static HRESULT WINAPI d3d_device7_SetTextureStageState_FPUPreserve(IDirect3DDevice7 *iface,
5042 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
5044 HRESULT hr;
5045 WORD old_fpucw;
5047 old_fpucw = d3d_fpu_setup();
5048 hr = d3d_device7_SetTextureStageState(iface, stage, state, value);
5049 set_fpu_control_word(old_fpucw);
5051 return hr;
5054 static HRESULT WINAPI d3d_device3_SetTextureStageState(IDirect3DDevice3 *iface,
5055 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
5057 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
5059 TRACE("iface %p, stage %u, state %#x, value %#x.\n",
5060 iface, stage, state, value);
5062 return IDirect3DDevice7_SetTextureStageState(&device->IDirect3DDevice7_iface, stage, state, value);
5065 /*****************************************************************************
5066 * IDirect3DDevice7::ValidateDevice
5068 * SDK: "Reports the device's ability to render the currently set
5069 * texture-blending operations in a single pass". Whatever that means
5070 * exactly...
5072 * Version 3 and 7
5074 * Params:
5075 * NumPasses: Address to write the number of necessary passes for the
5076 * desired effect to.
5078 * Returns:
5079 * D3D_OK on success
5080 * See IWineD3DDevice::ValidateDevice for more details
5082 *****************************************************************************/
5083 static HRESULT d3d_device7_ValidateDevice(IDirect3DDevice7 *iface, DWORD *pass_count)
5085 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5086 HRESULT hr;
5088 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
5090 wined3d_mutex_lock();
5091 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
5092 wined3d_mutex_unlock();
5094 return hr;
5097 static HRESULT WINAPI d3d_device7_ValidateDevice_FPUSetup(IDirect3DDevice7 *iface, DWORD *pass_count)
5099 return d3d_device7_ValidateDevice(iface, pass_count);
5102 static HRESULT WINAPI d3d_device7_ValidateDevice_FPUPreserve(IDirect3DDevice7 *iface, DWORD *pass_count)
5104 HRESULT hr;
5105 WORD old_fpucw;
5107 old_fpucw = d3d_fpu_setup();
5108 hr = d3d_device7_ValidateDevice(iface, pass_count);
5109 set_fpu_control_word(old_fpucw);
5111 return hr;
5114 static HRESULT WINAPI d3d_device3_ValidateDevice(IDirect3DDevice3 *iface, DWORD *pass_count)
5116 struct d3d_device *device = impl_from_IDirect3DDevice3(iface);
5118 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
5120 return IDirect3DDevice7_ValidateDevice(&device->IDirect3DDevice7_iface, pass_count);
5123 /*****************************************************************************
5124 * IDirect3DDevice7::Clear
5126 * Fills the render target, the z buffer and the stencil buffer with a
5127 * clear color / value
5129 * Version 7 only
5131 * Params:
5132 * Count: Number of rectangles in Rects must be 0 if Rects is NULL
5133 * Rects: Rectangles to clear. If NULL, the whole surface is cleared
5134 * Flags: Some flags, as usual
5135 * Color: Clear color for the render target
5136 * Z: Clear value for the Z buffer
5137 * Stencil: Clear value to store in each stencil buffer entry
5139 * Returns:
5140 * D3D_OK on success
5141 * For details, see IWineD3DDevice::Clear
5143 *****************************************************************************/
5144 static HRESULT d3d_device7_Clear(IDirect3DDevice7 *iface, DWORD count,
5145 D3DRECT *rects, DWORD flags, D3DCOLOR color, D3DVALUE z, DWORD stencil)
5147 const struct wined3d_color c =
5149 ((color >> 16) & 0xff) / 255.0f,
5150 ((color >> 8) & 0xff) / 255.0f,
5151 (color & 0xff) / 255.0f,
5152 ((color >> 24) & 0xff) / 255.0f,
5154 struct d3d_device *This = impl_from_IDirect3DDevice7(iface);
5155 HRESULT hr;
5157 TRACE("iface %p, count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %#x.\n",
5158 iface, count, rects, flags, color, z, stencil);
5160 wined3d_mutex_lock();
5161 hr = wined3d_device_clear(This->wined3d_device, count, (RECT *)rects, flags, &c, z, stencil);
5162 wined3d_mutex_unlock();
5164 return hr;
5167 static HRESULT WINAPI d3d_device7_Clear_FPUSetup(IDirect3DDevice7 *iface, DWORD count,
5168 D3DRECT *rects, DWORD flags, D3DCOLOR color, D3DVALUE z, DWORD stencil)
5170 return d3d_device7_Clear(iface, count, rects, flags, color, z, stencil);
5173 static HRESULT WINAPI d3d_device7_Clear_FPUPreserve(IDirect3DDevice7 *iface, DWORD count,
5174 D3DRECT *rects, DWORD flags, D3DCOLOR color, D3DVALUE z, DWORD stencil)
5176 HRESULT hr;
5177 WORD old_fpucw;
5179 old_fpucw = d3d_fpu_setup();
5180 hr = d3d_device7_Clear(iface, count, rects, flags, color, z, stencil);
5181 set_fpu_control_word(old_fpucw);
5183 return hr;
5186 /*****************************************************************************
5187 * IDirect3DDevice7::SetViewport
5189 * Sets the current viewport.
5191 * Version 7 only, but IDirect3DViewport uses this call for older
5192 * versions
5194 * Params:
5195 * Data: The new viewport to set
5197 * Returns:
5198 * D3D_OK on success
5199 * DDERR_INVALIDPARAMS if Data is NULL
5200 * For more details, see IWineDDDevice::SetViewport
5202 *****************************************************************************/
5203 static HRESULT d3d_device7_SetViewport(IDirect3DDevice7 *iface, D3DVIEWPORT7 *viewport)
5205 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5207 TRACE("iface %p, viewport %p.\n", iface, viewport);
5209 if (!viewport)
5210 return DDERR_INVALIDPARAMS;
5212 /* Note: D3DVIEWPORT7 is compatible with struct wined3d_viewport. */
5213 wined3d_mutex_lock();
5214 wined3d_device_set_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
5215 wined3d_mutex_unlock();
5217 return D3D_OK;
5220 static HRESULT WINAPI d3d_device7_SetViewport_FPUSetup(IDirect3DDevice7 *iface, D3DVIEWPORT7 *viewport)
5222 return d3d_device7_SetViewport(iface, viewport);
5225 static HRESULT WINAPI d3d_device7_SetViewport_FPUPreserve(IDirect3DDevice7 *iface, D3DVIEWPORT7 *viewport)
5227 HRESULT hr;
5228 WORD old_fpucw;
5230 old_fpucw = d3d_fpu_setup();
5231 hr = d3d_device7_SetViewport(iface, viewport);
5232 set_fpu_control_word(old_fpucw);
5234 return hr;
5237 /*****************************************************************************
5238 * IDirect3DDevice::GetViewport
5240 * Returns the current viewport
5242 * Version 7
5244 * Params:
5245 * Data: D3D7Viewport structure to write the viewport information to
5247 * Returns:
5248 * D3D_OK on success
5249 * DDERR_INVALIDPARAMS if Data is NULL
5250 * For more details, see IWineD3DDevice::GetViewport
5252 *****************************************************************************/
5253 static HRESULT d3d_device7_GetViewport(IDirect3DDevice7 *iface, D3DVIEWPORT7 *viewport)
5255 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5257 TRACE("iface %p, viewport %p.\n", iface, viewport);
5259 if (!viewport)
5260 return DDERR_INVALIDPARAMS;
5262 /* Note: D3DVIEWPORT7 is compatible with struct wined3d_viewport. */
5263 wined3d_mutex_lock();
5264 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
5265 wined3d_mutex_unlock();
5267 return D3D_OK;
5270 static HRESULT WINAPI d3d_device7_GetViewport_FPUSetup(IDirect3DDevice7 *iface, D3DVIEWPORT7 *viewport)
5272 return d3d_device7_GetViewport(iface, viewport);
5275 static HRESULT WINAPI d3d_device7_GetViewport_FPUPreserve(IDirect3DDevice7 *iface, D3DVIEWPORT7 *viewport)
5277 HRESULT hr;
5278 WORD old_fpucw;
5280 old_fpucw = d3d_fpu_setup();
5281 hr = d3d_device7_GetViewport(iface, viewport);
5282 set_fpu_control_word(old_fpucw);
5284 return hr;
5287 /*****************************************************************************
5288 * IDirect3DDevice7::SetMaterial
5290 * Sets the Material
5292 * Version 7
5294 * Params:
5295 * Mat: The material to set
5297 * Returns:
5298 * D3D_OK on success
5299 * DDERR_INVALIDPARAMS if Mat is NULL.
5300 * For more details, see IWineD3DDevice::SetMaterial
5302 *****************************************************************************/
5303 static HRESULT d3d_device7_SetMaterial(IDirect3DDevice7 *iface, D3DMATERIAL7 *material)
5305 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5307 TRACE("iface %p, material %p.\n", iface, material);
5309 if (!material)
5310 return DDERR_INVALIDPARAMS;
5312 wined3d_mutex_lock();
5313 /* Note: D3DMATERIAL7 is compatible with struct wined3d_material. */
5314 wined3d_device_set_material(device->wined3d_device, (struct wined3d_material *)material);
5315 wined3d_mutex_unlock();
5317 return D3D_OK;
5320 static HRESULT WINAPI d3d_device7_SetMaterial_FPUSetup(IDirect3DDevice7 *iface, D3DMATERIAL7 *material)
5322 return d3d_device7_SetMaterial(iface, material);
5325 static HRESULT WINAPI d3d_device7_SetMaterial_FPUPreserve(IDirect3DDevice7 *iface, D3DMATERIAL7 *material)
5327 HRESULT hr;
5328 WORD old_fpucw;
5330 old_fpucw = d3d_fpu_setup();
5331 hr = d3d_device7_SetMaterial(iface, material);
5332 set_fpu_control_word(old_fpucw);
5334 return hr;
5337 /*****************************************************************************
5338 * IDirect3DDevice7::GetMaterial
5340 * Returns the current material
5342 * Version 7
5344 * Params:
5345 * Mat: D3DMATERIAL7 structure to write the material parameters to
5347 * Returns:
5348 * D3D_OK on success
5349 * DDERR_INVALIDPARAMS if Mat is NULL
5350 * For more details, see IWineD3DDevice::GetMaterial
5352 *****************************************************************************/
5353 static HRESULT d3d_device7_GetMaterial(IDirect3DDevice7 *iface, D3DMATERIAL7 *material)
5355 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5357 TRACE("iface %p, material %p.\n", iface, material);
5359 wined3d_mutex_lock();
5360 /* Note: D3DMATERIAL7 is compatible with struct wined3d_material. */
5361 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
5362 wined3d_mutex_unlock();
5364 return D3D_OK;
5367 static HRESULT WINAPI d3d_device7_GetMaterial_FPUSetup(IDirect3DDevice7 *iface, D3DMATERIAL7 *material)
5369 return d3d_device7_GetMaterial(iface, material);
5372 static HRESULT WINAPI d3d_device7_GetMaterial_FPUPreserve(IDirect3DDevice7 *iface, D3DMATERIAL7 *material)
5374 HRESULT hr;
5375 WORD old_fpucw;
5377 old_fpucw = d3d_fpu_setup();
5378 hr = d3d_device7_GetMaterial(iface, material);
5379 set_fpu_control_word(old_fpucw);
5381 return hr;
5384 /*****************************************************************************
5385 * IDirect3DDevice7::SetLight
5387 * Assigns a light to a light index, but doesn't activate it yet.
5389 * Version 7, IDirect3DLight uses this method for older versions
5391 * Params:
5392 * LightIndex: The index of the new light
5393 * Light: A D3DLIGHT7 structure describing the light
5395 * Returns:
5396 * D3D_OK on success
5397 * For more details, see IWineD3DDevice::SetLight
5399 *****************************************************************************/
5400 static HRESULT d3d_device7_SetLight(IDirect3DDevice7 *iface, DWORD light_idx, D3DLIGHT7 *light)
5402 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5403 HRESULT hr;
5405 TRACE("iface %p, light_idx %u, light %p.\n", iface, light_idx, light);
5407 wined3d_mutex_lock();
5408 /* Note: D3DLIGHT7 is compatible with struct wined3d_light. */
5409 hr = wined3d_device_set_light(device->wined3d_device, light_idx, (struct wined3d_light *)light);
5410 wined3d_mutex_unlock();
5412 return hr_ddraw_from_wined3d(hr);
5415 static HRESULT WINAPI d3d_device7_SetLight_FPUSetup(IDirect3DDevice7 *iface, DWORD light_idx, D3DLIGHT7 *light)
5417 return d3d_device7_SetLight(iface, light_idx, light);
5420 static HRESULT WINAPI d3d_device7_SetLight_FPUPreserve(IDirect3DDevice7 *iface, DWORD light_idx, D3DLIGHT7 *light)
5422 HRESULT hr;
5423 WORD old_fpucw;
5425 old_fpucw = d3d_fpu_setup();
5426 hr = d3d_device7_SetLight(iface, light_idx, light);
5427 set_fpu_control_word(old_fpucw);
5429 return hr;
5432 /*****************************************************************************
5433 * IDirect3DDevice7::GetLight
5435 * Returns the light assigned to a light index
5437 * Params:
5438 * Light: Structure to write the light information to
5440 * Returns:
5441 * D3D_OK on success
5442 * DDERR_INVALIDPARAMS if Light is NULL
5443 * For details, see IWineD3DDevice::GetLight
5445 *****************************************************************************/
5446 static HRESULT d3d_device7_GetLight(IDirect3DDevice7 *iface, DWORD light_idx, D3DLIGHT7 *light)
5448 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5449 HRESULT rc;
5451 TRACE("iface %p, light_idx %u, light %p.\n", iface, light_idx, light);
5453 wined3d_mutex_lock();
5454 /* Note: D3DLIGHT7 is compatible with struct wined3d_light. */
5455 rc = wined3d_device_get_light(device->wined3d_device, light_idx, (struct wined3d_light *)light);
5456 wined3d_mutex_unlock();
5458 /* Translate the result. WineD3D returns other values than D3D7 */
5459 return hr_ddraw_from_wined3d(rc);
5462 static HRESULT WINAPI d3d_device7_GetLight_FPUSetup(IDirect3DDevice7 *iface, DWORD light_idx, D3DLIGHT7 *light)
5464 return d3d_device7_GetLight(iface, light_idx, light);
5467 static HRESULT WINAPI d3d_device7_GetLight_FPUPreserve(IDirect3DDevice7 *iface, DWORD light_idx, D3DLIGHT7 *light)
5469 HRESULT hr;
5470 WORD old_fpucw;
5472 old_fpucw = d3d_fpu_setup();
5473 hr = d3d_device7_GetLight(iface, light_idx, light);
5474 set_fpu_control_word(old_fpucw);
5476 return hr;
5479 /*****************************************************************************
5480 * IDirect3DDevice7::BeginStateBlock
5482 * Begins recording to a stateblock
5484 * Version 7
5486 * Returns:
5487 * D3D_OK on success
5488 * For details see IWineD3DDevice::BeginStateBlock
5490 *****************************************************************************/
5491 static HRESULT d3d_device7_BeginStateBlock(IDirect3DDevice7 *iface)
5493 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5494 HRESULT hr;
5496 TRACE("iface %p.\n", iface);
5498 wined3d_mutex_lock();
5499 hr = wined3d_device_begin_stateblock(device->wined3d_device);
5500 wined3d_mutex_unlock();
5502 return hr_ddraw_from_wined3d(hr);
5505 static HRESULT WINAPI d3d_device7_BeginStateBlock_FPUSetup(IDirect3DDevice7 *iface)
5507 return d3d_device7_BeginStateBlock(iface);
5510 static HRESULT WINAPI d3d_device7_BeginStateBlock_FPUPreserve(IDirect3DDevice7 *iface)
5512 HRESULT hr;
5513 WORD old_fpucw;
5515 old_fpucw = d3d_fpu_setup();
5516 hr = d3d_device7_BeginStateBlock(iface);
5517 set_fpu_control_word(old_fpucw);
5519 return hr;
5522 /*****************************************************************************
5523 * IDirect3DDevice7::EndStateBlock
5525 * Stops recording to a state block and returns the created stateblock
5526 * handle.
5528 * Version 7
5530 * Params:
5531 * BlockHandle: Address to store the stateblock's handle to
5533 * Returns:
5534 * D3D_OK on success
5535 * DDERR_INVALIDPARAMS if BlockHandle is NULL
5536 * See IWineD3DDevice::EndStateBlock for more details
5538 *****************************************************************************/
5539 static HRESULT d3d_device7_EndStateBlock(IDirect3DDevice7 *iface, DWORD *stateblock)
5541 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5542 struct wined3d_stateblock *wined3d_sb;
5543 HRESULT hr;
5544 DWORD h;
5546 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
5548 if (!stateblock)
5549 return DDERR_INVALIDPARAMS;
5551 wined3d_mutex_lock();
5553 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_sb);
5554 if (FAILED(hr))
5556 WARN("Failed to end stateblock, hr %#x.\n", hr);
5557 wined3d_mutex_unlock();
5558 *stateblock = 0;
5559 return hr_ddraw_from_wined3d(hr);
5562 h = ddraw_allocate_handle(&device->handle_table, wined3d_sb, DDRAW_HANDLE_STATEBLOCK);
5563 if (h == DDRAW_INVALID_HANDLE)
5565 ERR("Failed to allocate a stateblock handle.\n");
5566 wined3d_stateblock_decref(wined3d_sb);
5567 wined3d_mutex_unlock();
5568 *stateblock = 0;
5569 return DDERR_OUTOFMEMORY;
5572 wined3d_mutex_unlock();
5573 *stateblock = h + 1;
5575 return hr_ddraw_from_wined3d(hr);
5578 static HRESULT WINAPI d3d_device7_EndStateBlock_FPUSetup(IDirect3DDevice7 *iface, DWORD *stateblock)
5580 return d3d_device7_EndStateBlock(iface, stateblock);
5583 static HRESULT WINAPI d3d_device7_EndStateBlock_FPUPreserve(IDirect3DDevice7 *iface, DWORD *stateblock)
5585 HRESULT hr;
5586 WORD old_fpucw;
5588 old_fpucw = d3d_fpu_setup();
5589 hr = d3d_device7_EndStateBlock(iface, stateblock);
5590 set_fpu_control_word(old_fpucw);
5592 return hr;
5595 /*****************************************************************************
5596 * IDirect3DDevice7::PreLoad
5598 * Allows the app to signal that a texture will be used soon, to allow
5599 * the Direct3DDevice to load it to the video card in the meantime.
5601 * Version 7
5603 * Params:
5604 * Texture: The texture to preload
5606 * Returns:
5607 * D3D_OK on success
5608 * DDERR_INVALIDPARAMS if Texture is NULL
5609 * See IWineD3DSurface::PreLoad for details
5611 *****************************************************************************/
5612 static HRESULT d3d_device7_PreLoad(IDirect3DDevice7 *iface, IDirectDrawSurface7 *texture)
5614 struct ddraw_surface *surface = unsafe_impl_from_IDirectDrawSurface7(texture);
5616 TRACE("iface %p, texture %p.\n", iface, texture);
5618 if (!texture)
5619 return DDERR_INVALIDPARAMS;
5621 wined3d_mutex_lock();
5622 wined3d_texture_preload(surface->wined3d_texture);
5623 wined3d_mutex_unlock();
5625 return D3D_OK;
5628 static HRESULT WINAPI d3d_device7_PreLoad_FPUSetup(IDirect3DDevice7 *iface, IDirectDrawSurface7 *texture)
5630 return d3d_device7_PreLoad(iface, texture);
5633 static HRESULT WINAPI d3d_device7_PreLoad_FPUPreserve(IDirect3DDevice7 *iface, IDirectDrawSurface7 *texture)
5635 HRESULT hr;
5636 WORD old_fpucw;
5638 old_fpucw = d3d_fpu_setup();
5639 hr = d3d_device7_PreLoad(iface, texture);
5640 set_fpu_control_word(old_fpucw);
5642 return hr;
5645 /*****************************************************************************
5646 * IDirect3DDevice7::ApplyStateBlock
5648 * Activates the state stored in a state block handle.
5650 * Params:
5651 * BlockHandle: The stateblock handle to activate
5653 * Returns:
5654 * D3D_OK on success
5655 * D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
5657 *****************************************************************************/
5658 static HRESULT d3d_device7_ApplyStateBlock(IDirect3DDevice7 *iface, DWORD stateblock)
5660 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5661 struct wined3d_stateblock *wined3d_sb;
5663 TRACE("iface %p, stateblock %#x.\n", iface, stateblock);
5665 wined3d_mutex_lock();
5666 wined3d_sb = ddraw_get_object(&device->handle_table, stateblock - 1, DDRAW_HANDLE_STATEBLOCK);
5667 if (!wined3d_sb)
5669 WARN("Invalid stateblock handle.\n");
5670 wined3d_mutex_unlock();
5671 return D3DERR_INVALIDSTATEBLOCK;
5674 wined3d_stateblock_apply(wined3d_sb);
5675 wined3d_mutex_unlock();
5677 return D3D_OK;
5680 static HRESULT WINAPI d3d_device7_ApplyStateBlock_FPUSetup(IDirect3DDevice7 *iface, DWORD stateblock)
5682 return d3d_device7_ApplyStateBlock(iface, stateblock);
5685 static HRESULT WINAPI d3d_device7_ApplyStateBlock_FPUPreserve(IDirect3DDevice7 *iface, DWORD stateblock)
5687 HRESULT hr;
5688 WORD old_fpucw;
5690 old_fpucw = d3d_fpu_setup();
5691 hr = d3d_device7_ApplyStateBlock(iface, stateblock);
5692 set_fpu_control_word(old_fpucw);
5694 return hr;
5697 /*****************************************************************************
5698 * IDirect3DDevice7::CaptureStateBlock
5700 * Updates a stateblock's values to the values currently set for the device
5702 * Version 7
5704 * Params:
5705 * BlockHandle: Stateblock to update
5707 * Returns:
5708 * D3D_OK on success
5709 * D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
5710 * See IWineD3DDevice::CaptureStateBlock for more details
5712 *****************************************************************************/
5713 static HRESULT d3d_device7_CaptureStateBlock(IDirect3DDevice7 *iface, DWORD stateblock)
5715 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5716 struct wined3d_stateblock *wined3d_sb;
5718 TRACE("iface %p, stateblock %#x.\n", iface, stateblock);
5720 wined3d_mutex_lock();
5721 wined3d_sb = ddraw_get_object(&device->handle_table, stateblock - 1, DDRAW_HANDLE_STATEBLOCK);
5722 if (!wined3d_sb)
5724 WARN("Invalid stateblock handle.\n");
5725 wined3d_mutex_unlock();
5726 return D3DERR_INVALIDSTATEBLOCK;
5729 wined3d_stateblock_capture(wined3d_sb);
5730 wined3d_mutex_unlock();
5732 return D3D_OK;
5735 static HRESULT WINAPI d3d_device7_CaptureStateBlock_FPUSetup(IDirect3DDevice7 *iface, DWORD stateblock)
5737 return d3d_device7_CaptureStateBlock(iface, stateblock);
5740 static HRESULT WINAPI d3d_device7_CaptureStateBlock_FPUPreserve(IDirect3DDevice7 *iface, DWORD stateblock)
5742 HRESULT hr;
5743 WORD old_fpucw;
5745 old_fpucw = d3d_fpu_setup();
5746 hr = d3d_device7_CaptureStateBlock(iface, stateblock);
5747 set_fpu_control_word(old_fpucw);
5749 return hr;
5752 /*****************************************************************************
5753 * IDirect3DDevice7::DeleteStateBlock
5755 * Deletes a stateblock handle. This means releasing the WineD3DStateBlock
5757 * Version 7
5759 * Params:
5760 * BlockHandle: Stateblock handle to delete
5762 * Returns:
5763 * D3D_OK on success
5764 * D3DERR_INVALIDSTATEBLOCK if BlockHandle is 0
5766 *****************************************************************************/
5767 static HRESULT d3d_device7_DeleteStateBlock(IDirect3DDevice7 *iface, DWORD stateblock)
5769 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5770 struct wined3d_stateblock *wined3d_sb;
5771 ULONG ref;
5773 TRACE("iface %p, stateblock %#x.\n", iface, stateblock);
5775 wined3d_mutex_lock();
5777 wined3d_sb = ddraw_free_handle(&device->handle_table, stateblock - 1, DDRAW_HANDLE_STATEBLOCK);
5778 if (!wined3d_sb)
5780 WARN("Invalid stateblock handle.\n");
5781 wined3d_mutex_unlock();
5782 return D3DERR_INVALIDSTATEBLOCK;
5785 if ((ref = wined3d_stateblock_decref(wined3d_sb)))
5787 ERR("Something is still holding stateblock %p (refcount %u).\n", wined3d_sb, ref);
5790 wined3d_mutex_unlock();
5792 return D3D_OK;
5795 static HRESULT WINAPI d3d_device7_DeleteStateBlock_FPUSetup(IDirect3DDevice7 *iface, DWORD stateblock)
5797 return d3d_device7_DeleteStateBlock(iface, stateblock);
5800 static HRESULT WINAPI d3d_device7_DeleteStateBlock_FPUPreserve(IDirect3DDevice7 *iface, DWORD stateblock)
5802 HRESULT hr;
5803 WORD old_fpucw;
5805 old_fpucw = d3d_fpu_setup();
5806 hr = d3d_device7_DeleteStateBlock(iface, stateblock);
5807 set_fpu_control_word(old_fpucw);
5809 return hr;
5812 /*****************************************************************************
5813 * IDirect3DDevice7::CreateStateBlock
5815 * Creates a new state block handle.
5817 * Version 7
5819 * Params:
5820 * Type: The state block type
5821 * BlockHandle: Address to write the created handle to
5823 * Returns:
5824 * D3D_OK on success
5825 * DDERR_INVALIDPARAMS if BlockHandle is NULL
5827 *****************************************************************************/
5828 static HRESULT d3d_device7_CreateStateBlock(IDirect3DDevice7 *iface,
5829 D3DSTATEBLOCKTYPE type, DWORD *stateblock)
5831 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
5832 struct wined3d_stateblock *wined3d_sb;
5833 HRESULT hr;
5834 DWORD h;
5836 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
5838 if (!stateblock)
5839 return DDERR_INVALIDPARAMS;
5841 if (type != D3DSBT_ALL
5842 && type != D3DSBT_PIXELSTATE
5843 && type != D3DSBT_VERTEXSTATE)
5845 WARN("Unexpected stateblock type, returning DDERR_INVALIDPARAMS\n");
5846 return DDERR_INVALIDPARAMS;
5849 wined3d_mutex_lock();
5851 /* The D3DSTATEBLOCKTYPE enum is fine here. */
5852 hr = wined3d_stateblock_create(device->wined3d_device, type, &wined3d_sb);
5853 if (FAILED(hr))
5855 WARN("Failed to create stateblock, hr %#x.\n", hr);
5856 wined3d_mutex_unlock();
5857 return hr_ddraw_from_wined3d(hr);
5860 h = ddraw_allocate_handle(&device->handle_table, wined3d_sb, DDRAW_HANDLE_STATEBLOCK);
5861 if (h == DDRAW_INVALID_HANDLE)
5863 ERR("Failed to allocate stateblock handle.\n");
5864 wined3d_stateblock_decref(wined3d_sb);
5865 wined3d_mutex_unlock();
5866 return DDERR_OUTOFMEMORY;
5869 *stateblock = h + 1;
5870 wined3d_mutex_unlock();
5872 return hr_ddraw_from_wined3d(hr);
5875 static HRESULT WINAPI d3d_device7_CreateStateBlock_FPUSetup(IDirect3DDevice7 *iface,
5876 D3DSTATEBLOCKTYPE type, DWORD *stateblock)
5878 return d3d_device7_CreateStateBlock(iface, type, stateblock);
5881 static HRESULT WINAPI d3d_device7_CreateStateBlock_FPUPreserve(IDirect3DDevice7 *iface,
5882 D3DSTATEBLOCKTYPE type, DWORD *stateblock)
5884 HRESULT hr;
5885 WORD old_fpucw;
5887 old_fpucw = d3d_fpu_setup();
5888 hr = d3d_device7_CreateStateBlock(iface, type, stateblock);
5889 set_fpu_control_word(old_fpucw);
5891 return hr;
5894 static BOOL is_mip_level_subset(struct ddraw_surface *dest, struct ddraw_surface *src)
5896 struct ddraw_surface *src_level, *dest_level;
5897 IDirectDrawSurface7 *temp;
5898 DDSURFACEDESC2 ddsd;
5899 BOOL levelFound; /* at least one suitable sublevel in dest found */
5901 /* To satisfy "destination is mip level subset of source" criteria (regular texture counts as 1 level),
5902 * 1) there must be at least one mip level in destination that matched dimensions of some mip level in source and
5903 * 2) there must be no destination levels that don't match any levels in source. Otherwise it's INVALIDPARAMS.
5905 levelFound = FALSE;
5907 src_level = src;
5908 dest_level = dest;
5910 for (;src_level && dest_level;)
5912 if (src_level->surface_desc.dwWidth == dest_level->surface_desc.dwWidth &&
5913 src_level->surface_desc.dwHeight == dest_level->surface_desc.dwHeight)
5915 levelFound = TRUE;
5917 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
5918 ddsd.ddsCaps.dwCaps2 = DDSCAPS2_MIPMAPSUBLEVEL;
5919 IDirectDrawSurface7_GetAttachedSurface(&dest_level->IDirectDrawSurface7_iface, &ddsd.ddsCaps, &temp);
5921 if (dest_level != dest) IDirectDrawSurface7_Release(&dest_level->IDirectDrawSurface7_iface);
5923 dest_level = unsafe_impl_from_IDirectDrawSurface7(temp);
5926 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
5927 ddsd.ddsCaps.dwCaps2 = DDSCAPS2_MIPMAPSUBLEVEL;
5928 IDirectDrawSurface7_GetAttachedSurface(&src_level->IDirectDrawSurface7_iface, &ddsd.ddsCaps, &temp);
5930 if (src_level != src) IDirectDrawSurface7_Release(&src_level->IDirectDrawSurface7_iface);
5932 src_level = unsafe_impl_from_IDirectDrawSurface7(temp);
5935 if (src_level && src_level != src) IDirectDrawSurface7_Release(&src_level->IDirectDrawSurface7_iface);
5936 if (dest_level && dest_level != dest) IDirectDrawSurface7_Release(&dest_level->IDirectDrawSurface7_iface);
5938 return !dest_level && levelFound;
5941 static void copy_mipmap_chain(struct d3d_device *device, struct ddraw_surface *dst,
5942 struct ddraw_surface *src, const POINT *DestPoint, const RECT *SrcRect)
5944 struct ddraw_surface *dst_level, *src_level;
5945 IDirectDrawSurface7 *temp;
5946 DDSURFACEDESC2 ddsd;
5947 POINT point;
5948 RECT src_rect;
5949 HRESULT hr;
5950 IDirectDrawPalette *pal = NULL, *pal_src = NULL;
5951 DWORD ckeyflag;
5952 DDCOLORKEY ddckey;
5954 /* Copy palette, if possible. */
5955 IDirectDrawSurface7_GetPalette(&src->IDirectDrawSurface7_iface, &pal_src);
5956 IDirectDrawSurface7_GetPalette(&dst->IDirectDrawSurface7_iface, &pal);
5958 if (pal_src != NULL && pal != NULL)
5960 PALETTEENTRY palent[256];
5962 IDirectDrawPalette_GetEntries(pal_src, 0, 0, 256, palent);
5963 IDirectDrawPalette_SetEntries(pal, 0, 0, 256, palent);
5966 if (pal) IDirectDrawPalette_Release(pal);
5967 if (pal_src) IDirectDrawPalette_Release(pal_src);
5969 /* Copy colorkeys, if present. */
5970 for (ckeyflag = DDCKEY_DESTBLT; ckeyflag <= DDCKEY_SRCOVERLAY; ckeyflag <<= 1)
5972 hr = IDirectDrawSurface7_GetColorKey(&src->IDirectDrawSurface7_iface, ckeyflag, &ddckey);
5974 if (SUCCEEDED(hr))
5976 IDirectDrawSurface7_SetColorKey(&dst->IDirectDrawSurface7_iface, ckeyflag, &ddckey);
5980 src_level = src;
5981 dst_level = dst;
5983 point = *DestPoint;
5984 src_rect = *SrcRect;
5986 for (;src_level && dst_level;)
5988 if (src_level->surface_desc.dwWidth == dst_level->surface_desc.dwWidth
5989 && src_level->surface_desc.dwHeight == dst_level->surface_desc.dwHeight)
5991 UINT src_w = src_rect.right - src_rect.left;
5992 UINT src_h = src_rect.bottom - src_rect.top;
5993 RECT dst_rect = {point.x, point.y, point.x + src_w, point.y + src_h};
5995 if (FAILED(hr = wined3d_texture_blt(dst_level->wined3d_texture, dst_level->sub_resource_idx, &dst_rect,
5996 src_level->wined3d_texture, src_level->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
5997 ERR("Blit failed, hr %#x.\n", hr);
5999 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6000 ddsd.ddsCaps.dwCaps2 = DDSCAPS2_MIPMAPSUBLEVEL;
6001 IDirectDrawSurface7_GetAttachedSurface(&dst_level->IDirectDrawSurface7_iface, &ddsd.ddsCaps, &temp);
6003 if (dst_level != dst)
6004 IDirectDrawSurface7_Release(&dst_level->IDirectDrawSurface7_iface);
6006 dst_level = unsafe_impl_from_IDirectDrawSurface7(temp);
6009 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6010 ddsd.ddsCaps.dwCaps2 = DDSCAPS2_MIPMAPSUBLEVEL;
6011 IDirectDrawSurface7_GetAttachedSurface(&src_level->IDirectDrawSurface7_iface, &ddsd.ddsCaps, &temp);
6013 if (src_level != src) IDirectDrawSurface7_Release(&src_level->IDirectDrawSurface7_iface);
6015 src_level = unsafe_impl_from_IDirectDrawSurface7(temp);
6017 point.x /= 2;
6018 point.y /= 2;
6020 src_rect.top /= 2;
6021 src_rect.left /= 2;
6022 src_rect.right = (src_rect.right + 1) / 2;
6023 src_rect.bottom = (src_rect.bottom + 1) / 2;
6026 if (src_level && src_level != src)
6027 IDirectDrawSurface7_Release(&src_level->IDirectDrawSurface7_iface);
6028 if (dst_level && dst_level != dst)
6029 IDirectDrawSurface7_Release(&dst_level->IDirectDrawSurface7_iface);
6032 /*****************************************************************************
6033 * IDirect3DDevice7::Load
6035 * Loads a rectangular area from the source into the destination texture.
6036 * It can also copy the source to the faces of a cubic environment map
6038 * Version 7
6040 * Params:
6041 * DestTex: Destination texture
6042 * DestPoint: Point in the destination where the source image should be
6043 * written to
6044 * SrcTex: Source texture
6045 * SrcRect: Source rectangle
6046 * Flags: Cubemap faces to load (DDSCAPS2_CUBEMAP_ALLFACES, DDSCAPS2_CUBEMAP_POSITIVEX,
6047 * DDSCAPS2_CUBEMAP_NEGATIVEX, DDSCAPS2_CUBEMAP_POSITIVEY, DDSCAPS2_CUBEMAP_NEGATIVEY,
6048 * DDSCAPS2_CUBEMAP_POSITIVEZ, DDSCAPS2_CUBEMAP_NEGATIVEZ)
6050 * Returns:
6051 * D3D_OK on success
6052 * DDERR_INVALIDPARAMS if dst_texture or src_texture is NULL, broken coordinates or anything unexpected.
6055 *****************************************************************************/
6056 static HRESULT d3d_device7_Load(IDirect3DDevice7 *iface, IDirectDrawSurface7 *dst_texture, POINT *dst_pos,
6057 IDirectDrawSurface7 *src_texture, RECT *src_rect, DWORD flags)
6059 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
6060 struct ddraw_surface *dest = unsafe_impl_from_IDirectDrawSurface7(dst_texture);
6061 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(src_texture);
6062 POINT destpoint;
6063 RECT srcrect;
6065 TRACE("iface %p, dst_texture %p, dst_pos %s, src_texture %p, src_rect %s, flags %#x.\n",
6066 iface, dst_texture, wine_dbgstr_point(dst_pos), src_texture, wine_dbgstr_rect(src_rect), flags);
6068 if( (!src) || (!dest) )
6069 return DDERR_INVALIDPARAMS;
6071 wined3d_mutex_lock();
6073 if (!src_rect)
6075 srcrect.left = srcrect.top = 0;
6076 srcrect.right = src->surface_desc.dwWidth;
6077 srcrect.bottom = src->surface_desc.dwHeight;
6079 else
6080 srcrect = *src_rect;
6082 if (!dst_pos)
6083 destpoint.x = destpoint.y = 0;
6084 else
6085 destpoint = *dst_pos;
6087 /* Check bad dimensions. dst_pos is validated against src, not dest, because
6088 * destination can be a subset of mip levels, in which case actual coordinates used
6089 * for it may be divided. If any dimension of dest is larger than source, it can't be
6090 * mip level subset, so an error can be returned early.
6092 if (srcrect.left >= srcrect.right || srcrect.top >= srcrect.bottom ||
6093 srcrect.right > src->surface_desc.dwWidth ||
6094 srcrect.bottom > src->surface_desc.dwHeight ||
6095 destpoint.x + srcrect.right - srcrect.left > src->surface_desc.dwWidth ||
6096 destpoint.y + srcrect.bottom - srcrect.top > src->surface_desc.dwHeight ||
6097 dest->surface_desc.dwWidth > src->surface_desc.dwWidth ||
6098 dest->surface_desc.dwHeight > src->surface_desc.dwHeight)
6100 wined3d_mutex_unlock();
6101 return DDERR_INVALIDPARAMS;
6104 /* Must be top level surfaces. */
6105 if (src->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL ||
6106 dest->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
6108 wined3d_mutex_unlock();
6109 return DDERR_INVALIDPARAMS;
6112 if (src->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6114 struct ddraw_surface *src_face, *dest_face;
6115 DWORD src_face_flag, dest_face_flag;
6116 IDirectDrawSurface7 *temp;
6117 DDSURFACEDESC2 ddsd;
6118 int i;
6120 if (!(dest->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
6122 wined3d_mutex_unlock();
6123 return DDERR_INVALIDPARAMS;
6126 /* Iterate through cube faces 2 times. First time is just to check INVALIDPARAMS conditions, second
6127 * time it's actual surface loading. */
6128 for (i = 0; i < 2; i++)
6130 dest_face = dest;
6131 src_face = src;
6133 for (;dest_face && src_face;)
6135 src_face_flag = src_face->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES;
6136 dest_face_flag = dest_face->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES;
6138 if (src_face_flag == dest_face_flag)
6140 if (i == 0)
6142 /* Destination mip levels must be subset of source mip levels. */
6143 if (!is_mip_level_subset(dest_face, src_face))
6145 wined3d_mutex_unlock();
6146 return DDERR_INVALIDPARAMS;
6149 else if (flags & dest_face_flag)
6151 copy_mipmap_chain(device, dest_face, src_face, &destpoint, &srcrect);
6154 if (src_face_flag < DDSCAPS2_CUBEMAP_NEGATIVEZ)
6156 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6157 ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | (src_face_flag << 1);
6158 IDirectDrawSurface7_GetAttachedSurface(&src->IDirectDrawSurface7_iface, &ddsd.ddsCaps, &temp);
6160 if (src_face != src) IDirectDrawSurface7_Release(&src_face->IDirectDrawSurface7_iface);
6162 src_face = unsafe_impl_from_IDirectDrawSurface7(temp);
6164 else
6166 if (src_face != src) IDirectDrawSurface7_Release(&src_face->IDirectDrawSurface7_iface);
6168 src_face = NULL;
6172 if (dest_face_flag < DDSCAPS2_CUBEMAP_NEGATIVEZ)
6174 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6175 ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | (dest_face_flag << 1);
6176 IDirectDrawSurface7_GetAttachedSurface(&dest->IDirectDrawSurface7_iface, &ddsd.ddsCaps, &temp);
6178 if (dest_face != dest) IDirectDrawSurface7_Release(&dest_face->IDirectDrawSurface7_iface);
6180 dest_face = unsafe_impl_from_IDirectDrawSurface7(temp);
6182 else
6184 if (dest_face != dest) IDirectDrawSurface7_Release(&dest_face->IDirectDrawSurface7_iface);
6186 dest_face = NULL;
6190 if (i == 0)
6192 /* Native returns error if src faces are not subset of dest faces. */
6193 if (src_face)
6195 wined3d_mutex_unlock();
6196 return DDERR_INVALIDPARAMS;
6201 wined3d_mutex_unlock();
6202 return D3D_OK;
6204 else if (dest->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6206 wined3d_mutex_unlock();
6207 return DDERR_INVALIDPARAMS;
6210 /* Handle non cube map textures. */
6212 /* Destination mip levels must be subset of source mip levels. */
6213 if (!is_mip_level_subset(dest, src))
6215 wined3d_mutex_unlock();
6216 return DDERR_INVALIDPARAMS;
6219 copy_mipmap_chain(device, dest, src, &destpoint, &srcrect);
6221 wined3d_mutex_unlock();
6223 return D3D_OK;
6226 static HRESULT WINAPI d3d_device7_Load_FPUSetup(IDirect3DDevice7 *iface, IDirectDrawSurface7 *dst_texture,
6227 POINT *dst_pos, IDirectDrawSurface7 *src_texture, RECT *src_rect, DWORD flags)
6229 return d3d_device7_Load(iface, dst_texture, dst_pos, src_texture, src_rect, flags);
6232 static HRESULT WINAPI d3d_device7_Load_FPUPreserve(IDirect3DDevice7 *iface, IDirectDrawSurface7 *dst_texture,
6233 POINT *dst_pos, IDirectDrawSurface7 *src_texture, RECT *src_rect, DWORD flags)
6235 HRESULT hr;
6236 WORD old_fpucw;
6238 old_fpucw = d3d_fpu_setup();
6239 hr = d3d_device7_Load(iface, dst_texture, dst_pos, src_texture, src_rect, flags);
6240 set_fpu_control_word(old_fpucw);
6242 return hr;
6245 /*****************************************************************************
6246 * IDirect3DDevice7::LightEnable
6248 * Enables or disables a light
6250 * Version 7, IDirect3DLight uses this method too.
6252 * Params:
6253 * LightIndex: The index of the light to enable / disable
6254 * Enable: Enable or disable the light
6256 * Returns:
6257 * D3D_OK on success
6258 * For more details, see IWineD3DDevice::SetLightEnable
6260 *****************************************************************************/
6261 static HRESULT d3d_device7_LightEnable(IDirect3DDevice7 *iface, DWORD light_idx, BOOL enabled)
6263 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
6264 HRESULT hr;
6266 TRACE("iface %p, light_idx %u, enabled %#x.\n", iface, light_idx, enabled);
6268 wined3d_mutex_lock();
6269 hr = wined3d_device_set_light_enable(device->wined3d_device, light_idx, enabled);
6270 wined3d_mutex_unlock();
6272 return hr_ddraw_from_wined3d(hr);
6275 static HRESULT WINAPI d3d_device7_LightEnable_FPUSetup(IDirect3DDevice7 *iface, DWORD light_idx, BOOL enabled)
6277 return d3d_device7_LightEnable(iface, light_idx, enabled);
6280 static HRESULT WINAPI d3d_device7_LightEnable_FPUPreserve(IDirect3DDevice7 *iface, DWORD light_idx, BOOL enabled)
6282 HRESULT hr;
6283 WORD old_fpucw;
6285 old_fpucw = d3d_fpu_setup();
6286 hr = d3d_device7_LightEnable(iface, light_idx, enabled);
6287 set_fpu_control_word(old_fpucw);
6289 return hr;
6292 /*****************************************************************************
6293 * IDirect3DDevice7::GetLightEnable
6295 * Retrieves if the light with the given index is enabled or not
6297 * Version 7
6299 * Params:
6300 * LightIndex: Index of desired light
6301 * Enable: Pointer to a BOOL which contains the result
6303 * Returns:
6304 * D3D_OK on success
6305 * DDERR_INVALIDPARAMS if Enable is NULL
6306 * See IWineD3DDevice::GetLightEnable for more details
6308 *****************************************************************************/
6309 static HRESULT d3d_device7_GetLightEnable(IDirect3DDevice7 *iface, DWORD light_idx, BOOL *enabled)
6311 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
6312 HRESULT hr;
6314 TRACE("iface %p, light_idx %u, enabled %p.\n", iface, light_idx, enabled);
6316 if (!enabled)
6317 return DDERR_INVALIDPARAMS;
6319 wined3d_mutex_lock();
6320 hr = wined3d_device_get_light_enable(device->wined3d_device, light_idx, enabled);
6321 wined3d_mutex_unlock();
6323 return hr_ddraw_from_wined3d(hr);
6326 static HRESULT WINAPI d3d_device7_GetLightEnable_FPUSetup(IDirect3DDevice7 *iface, DWORD light_idx, BOOL *enabled)
6328 return d3d_device7_GetLightEnable(iface, light_idx, enabled);
6331 static HRESULT WINAPI d3d_device7_GetLightEnable_FPUPreserve(IDirect3DDevice7 *iface, DWORD light_idx, BOOL *enabled)
6333 HRESULT hr;
6334 WORD old_fpucw;
6336 old_fpucw = d3d_fpu_setup();
6337 hr = d3d_device7_GetLightEnable(iface, light_idx, enabled);
6338 set_fpu_control_word(old_fpucw);
6340 return hr;
6343 /*****************************************************************************
6344 * IDirect3DDevice7::SetClipPlane
6346 * Sets custom clipping plane
6348 * Version 7
6350 * Params:
6351 * Index: The index of the clipping plane
6352 * PlaneEquation: An equation defining the clipping plane
6354 * Returns:
6355 * D3D_OK on success
6356 * DDERR_INVALIDPARAMS if PlaneEquation is NULL
6357 * See IWineD3DDevice::SetClipPlane for more details
6359 *****************************************************************************/
6360 static HRESULT d3d_device7_SetClipPlane(IDirect3DDevice7 *iface, DWORD idx, D3DVALUE *plane)
6362 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
6363 HRESULT hr;
6365 TRACE("iface %p, idx %u, plane %p.\n", iface, idx, plane);
6367 if (!plane)
6368 return DDERR_INVALIDPARAMS;
6370 wined3d_mutex_lock();
6371 hr = wined3d_device_set_clip_plane(device->wined3d_device, idx, (struct wined3d_vec4 *)plane);
6372 wined3d_mutex_unlock();
6374 return hr;
6377 static HRESULT WINAPI d3d_device7_SetClipPlane_FPUSetup(IDirect3DDevice7 *iface, DWORD idx, D3DVALUE *plane)
6379 return d3d_device7_SetClipPlane(iface, idx, plane);
6382 static HRESULT WINAPI d3d_device7_SetClipPlane_FPUPreserve(IDirect3DDevice7 *iface, DWORD idx, D3DVALUE *plane)
6384 HRESULT hr;
6385 WORD old_fpucw;
6387 old_fpucw = d3d_fpu_setup();
6388 hr = d3d_device7_SetClipPlane(iface, idx, plane);
6389 set_fpu_control_word(old_fpucw);
6391 return hr;
6394 /*****************************************************************************
6395 * IDirect3DDevice7::GetClipPlane
6397 * Returns the clipping plane with a specific index
6399 * Params:
6400 * Index: The index of the desired plane
6401 * PlaneEquation: Address to store the plane equation to
6403 * Returns:
6404 * D3D_OK on success
6405 * DDERR_INVALIDPARAMS if PlaneEquation is NULL
6406 * See IWineD3DDevice::GetClipPlane for more details
6408 *****************************************************************************/
6409 static HRESULT d3d_device7_GetClipPlane(IDirect3DDevice7 *iface, DWORD idx, D3DVALUE *plane)
6411 struct d3d_device *device = impl_from_IDirect3DDevice7(iface);
6412 HRESULT hr;
6414 TRACE("iface %p, idx %u, plane %p.\n", iface, idx, plane);
6416 if (!plane)
6417 return DDERR_INVALIDPARAMS;
6419 wined3d_mutex_lock();
6420 hr = wined3d_device_get_clip_plane(device->wined3d_device, idx, (struct wined3d_vec4 *)plane);
6421 wined3d_mutex_unlock();
6423 return hr;
6426 static HRESULT WINAPI d3d_device7_GetClipPlane_FPUSetup(IDirect3DDevice7 *iface, DWORD idx, D3DVALUE *plane)
6428 return d3d_device7_GetClipPlane(iface, idx, plane);
6431 static HRESULT WINAPI d3d_device7_GetClipPlane_FPUPreserve(IDirect3DDevice7 *iface, DWORD idx, D3DVALUE *plane)
6433 HRESULT hr;
6434 WORD old_fpucw;
6436 old_fpucw = d3d_fpu_setup();
6437 hr = d3d_device7_GetClipPlane(iface, idx, plane);
6438 set_fpu_control_word(old_fpucw);
6440 return hr;
6443 /*****************************************************************************
6444 * IDirect3DDevice7::GetInfo
6446 * Retrieves some information about the device. The DirectX sdk says that
6447 * this version returns S_FALSE for all retail builds of DirectX, that's what
6448 * this implementation does.
6450 * Params:
6451 * DevInfoID: Information type requested
6452 * DevInfoStruct: Pointer to a structure to store the info to
6453 * Size: Size of the structure
6455 * Returns:
6456 * S_FALSE, because it's a non-debug driver
6458 *****************************************************************************/
6459 static HRESULT WINAPI d3d_device7_GetInfo(IDirect3DDevice7 *iface, DWORD info_id, void *info, DWORD info_size)
6461 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n",
6462 iface, info_id, info, info_size);
6464 if (TRACE_ON(ddraw))
6466 TRACE(" info requested : ");
6467 switch (info_id)
6469 case D3DDEVINFOID_TEXTUREMANAGER: TRACE("D3DDEVINFOID_TEXTUREMANAGER\n"); break;
6470 case D3DDEVINFOID_D3DTEXTUREMANAGER: TRACE("D3DDEVINFOID_D3DTEXTUREMANAGER\n"); break;
6471 case D3DDEVINFOID_TEXTURING: TRACE("D3DDEVINFOID_TEXTURING\n"); break;
6472 default: ERR(" invalid flag !!!\n"); return DDERR_INVALIDPARAMS;
6476 return S_FALSE; /* According to MSDN, this is valid for a non-debug driver */
6479 /* For performance optimization, devices created in FPUSETUP and FPUPRESERVE modes
6480 * have separate vtables. Simple functions where this doesn't matter like GetDirect3D
6481 * are not duplicated.
6483 * Device created with DDSCL_FPUSETUP (d3d7 default) - device methods assume that FPU
6484 * has already been setup for optimal d3d operation.
6486 * Device created with DDSCL_FPUPRESERVE - resets and restores FPU mode when necessary in
6487 * d3d calls (FPU may be in a mode non-suitable for d3d when the app calls d3d). Required
6488 * by Sacrifice (game). */
6489 static const struct IDirect3DDevice7Vtbl d3d_device7_fpu_setup_vtbl =
6491 /*** IUnknown Methods ***/
6492 d3d_device7_QueryInterface,
6493 d3d_device7_AddRef,
6494 d3d_device7_Release,
6495 /*** IDirect3DDevice7 ***/
6496 d3d_device7_GetCaps_FPUSetup,
6497 d3d_device7_EnumTextureFormats_FPUSetup,
6498 d3d_device7_BeginScene_FPUSetup,
6499 d3d_device7_EndScene_FPUSetup,
6500 d3d_device7_GetDirect3D,
6501 d3d_device7_SetRenderTarget_FPUSetup,
6502 d3d_device7_GetRenderTarget,
6503 d3d_device7_Clear_FPUSetup,
6504 d3d_device7_SetTransform_FPUSetup,
6505 d3d_device7_GetTransform_FPUSetup,
6506 d3d_device7_SetViewport_FPUSetup,
6507 d3d_device7_MultiplyTransform_FPUSetup,
6508 d3d_device7_GetViewport_FPUSetup,
6509 d3d_device7_SetMaterial_FPUSetup,
6510 d3d_device7_GetMaterial_FPUSetup,
6511 d3d_device7_SetLight_FPUSetup,
6512 d3d_device7_GetLight_FPUSetup,
6513 d3d_device7_SetRenderState_FPUSetup,
6514 d3d_device7_GetRenderState_FPUSetup,
6515 d3d_device7_BeginStateBlock_FPUSetup,
6516 d3d_device7_EndStateBlock_FPUSetup,
6517 d3d_device7_PreLoad_FPUSetup,
6518 d3d_device7_DrawPrimitive_FPUSetup,
6519 d3d_device7_DrawIndexedPrimitive_FPUSetup,
6520 d3d_device7_SetClipStatus,
6521 d3d_device7_GetClipStatus,
6522 d3d_device7_DrawPrimitiveStrided_FPUSetup,
6523 d3d_device7_DrawIndexedPrimitiveStrided_FPUSetup,
6524 d3d_device7_DrawPrimitiveVB_FPUSetup,
6525 d3d_device7_DrawIndexedPrimitiveVB_FPUSetup,
6526 d3d_device7_ComputeSphereVisibility,
6527 d3d_device7_GetTexture_FPUSetup,
6528 d3d_device7_SetTexture_FPUSetup,
6529 d3d_device7_GetTextureStageState_FPUSetup,
6530 d3d_device7_SetTextureStageState_FPUSetup,
6531 d3d_device7_ValidateDevice_FPUSetup,
6532 d3d_device7_ApplyStateBlock_FPUSetup,
6533 d3d_device7_CaptureStateBlock_FPUSetup,
6534 d3d_device7_DeleteStateBlock_FPUSetup,
6535 d3d_device7_CreateStateBlock_FPUSetup,
6536 d3d_device7_Load_FPUSetup,
6537 d3d_device7_LightEnable_FPUSetup,
6538 d3d_device7_GetLightEnable_FPUSetup,
6539 d3d_device7_SetClipPlane_FPUSetup,
6540 d3d_device7_GetClipPlane_FPUSetup,
6541 d3d_device7_GetInfo
6544 static const struct IDirect3DDevice7Vtbl d3d_device7_fpu_preserve_vtbl =
6546 /*** IUnknown Methods ***/
6547 d3d_device7_QueryInterface,
6548 d3d_device7_AddRef,
6549 d3d_device7_Release,
6550 /*** IDirect3DDevice7 ***/
6551 d3d_device7_GetCaps_FPUPreserve,
6552 d3d_device7_EnumTextureFormats_FPUPreserve,
6553 d3d_device7_BeginScene_FPUPreserve,
6554 d3d_device7_EndScene_FPUPreserve,
6555 d3d_device7_GetDirect3D,
6556 d3d_device7_SetRenderTarget_FPUPreserve,
6557 d3d_device7_GetRenderTarget,
6558 d3d_device7_Clear_FPUPreserve,
6559 d3d_device7_SetTransform_FPUPreserve,
6560 d3d_device7_GetTransform_FPUPreserve,
6561 d3d_device7_SetViewport_FPUPreserve,
6562 d3d_device7_MultiplyTransform_FPUPreserve,
6563 d3d_device7_GetViewport_FPUPreserve,
6564 d3d_device7_SetMaterial_FPUPreserve,
6565 d3d_device7_GetMaterial_FPUPreserve,
6566 d3d_device7_SetLight_FPUPreserve,
6567 d3d_device7_GetLight_FPUPreserve,
6568 d3d_device7_SetRenderState_FPUPreserve,
6569 d3d_device7_GetRenderState_FPUPreserve,
6570 d3d_device7_BeginStateBlock_FPUPreserve,
6571 d3d_device7_EndStateBlock_FPUPreserve,
6572 d3d_device7_PreLoad_FPUPreserve,
6573 d3d_device7_DrawPrimitive_FPUPreserve,
6574 d3d_device7_DrawIndexedPrimitive_FPUPreserve,
6575 d3d_device7_SetClipStatus,
6576 d3d_device7_GetClipStatus,
6577 d3d_device7_DrawPrimitiveStrided_FPUPreserve,
6578 d3d_device7_DrawIndexedPrimitiveStrided_FPUPreserve,
6579 d3d_device7_DrawPrimitiveVB_FPUPreserve,
6580 d3d_device7_DrawIndexedPrimitiveVB_FPUPreserve,
6581 d3d_device7_ComputeSphereVisibility,
6582 d3d_device7_GetTexture_FPUPreserve,
6583 d3d_device7_SetTexture_FPUPreserve,
6584 d3d_device7_GetTextureStageState_FPUPreserve,
6585 d3d_device7_SetTextureStageState_FPUPreserve,
6586 d3d_device7_ValidateDevice_FPUPreserve,
6587 d3d_device7_ApplyStateBlock_FPUPreserve,
6588 d3d_device7_CaptureStateBlock_FPUPreserve,
6589 d3d_device7_DeleteStateBlock_FPUPreserve,
6590 d3d_device7_CreateStateBlock_FPUPreserve,
6591 d3d_device7_Load_FPUPreserve,
6592 d3d_device7_LightEnable_FPUPreserve,
6593 d3d_device7_GetLightEnable_FPUPreserve,
6594 d3d_device7_SetClipPlane_FPUPreserve,
6595 d3d_device7_GetClipPlane_FPUPreserve,
6596 d3d_device7_GetInfo
6599 static const struct IDirect3DDevice3Vtbl d3d_device3_vtbl =
6601 /*** IUnknown Methods ***/
6602 d3d_device3_QueryInterface,
6603 d3d_device3_AddRef,
6604 d3d_device3_Release,
6605 /*** IDirect3DDevice3 ***/
6606 d3d_device3_GetCaps,
6607 d3d_device3_GetStats,
6608 d3d_device3_AddViewport,
6609 d3d_device3_DeleteViewport,
6610 d3d_device3_NextViewport,
6611 d3d_device3_EnumTextureFormats,
6612 d3d_device3_BeginScene,
6613 d3d_device3_EndScene,
6614 d3d_device3_GetDirect3D,
6615 d3d_device3_SetCurrentViewport,
6616 d3d_device3_GetCurrentViewport,
6617 d3d_device3_SetRenderTarget,
6618 d3d_device3_GetRenderTarget,
6619 d3d_device3_Begin,
6620 d3d_device3_BeginIndexed,
6621 d3d_device3_Vertex,
6622 d3d_device3_Index,
6623 d3d_device3_End,
6624 d3d_device3_GetRenderState,
6625 d3d_device3_SetRenderState,
6626 d3d_device3_GetLightState,
6627 d3d_device3_SetLightState,
6628 d3d_device3_SetTransform,
6629 d3d_device3_GetTransform,
6630 d3d_device3_MultiplyTransform,
6631 d3d_device3_DrawPrimitive,
6632 d3d_device3_DrawIndexedPrimitive,
6633 d3d_device3_SetClipStatus,
6634 d3d_device3_GetClipStatus,
6635 d3d_device3_DrawPrimitiveStrided,
6636 d3d_device3_DrawIndexedPrimitiveStrided,
6637 d3d_device3_DrawPrimitiveVB,
6638 d3d_device3_DrawIndexedPrimitiveVB,
6639 d3d_device3_ComputeSphereVisibility,
6640 d3d_device3_GetTexture,
6641 d3d_device3_SetTexture,
6642 d3d_device3_GetTextureStageState,
6643 d3d_device3_SetTextureStageState,
6644 d3d_device3_ValidateDevice
6647 static const struct IDirect3DDevice2Vtbl d3d_device2_vtbl =
6649 /*** IUnknown Methods ***/
6650 d3d_device2_QueryInterface,
6651 d3d_device2_AddRef,
6652 d3d_device2_Release,
6653 /*** IDirect3DDevice2 ***/
6654 d3d_device2_GetCaps,
6655 d3d_device2_SwapTextureHandles,
6656 d3d_device2_GetStats,
6657 d3d_device2_AddViewport,
6658 d3d_device2_DeleteViewport,
6659 d3d_device2_NextViewport,
6660 d3d_device2_EnumTextureFormats,
6661 d3d_device2_BeginScene,
6662 d3d_device2_EndScene,
6663 d3d_device2_GetDirect3D,
6664 d3d_device2_SetCurrentViewport,
6665 d3d_device2_GetCurrentViewport,
6666 d3d_device2_SetRenderTarget,
6667 d3d_device2_GetRenderTarget,
6668 d3d_device2_Begin,
6669 d3d_device2_BeginIndexed,
6670 d3d_device2_Vertex,
6671 d3d_device2_Index,
6672 d3d_device2_End,
6673 d3d_device2_GetRenderState,
6674 d3d_device2_SetRenderState,
6675 d3d_device2_GetLightState,
6676 d3d_device2_SetLightState,
6677 d3d_device2_SetTransform,
6678 d3d_device2_GetTransform,
6679 d3d_device2_MultiplyTransform,
6680 d3d_device2_DrawPrimitive,
6681 d3d_device2_DrawIndexedPrimitive,
6682 d3d_device2_SetClipStatus,
6683 d3d_device2_GetClipStatus
6686 static const struct IDirect3DDeviceVtbl d3d_device1_vtbl =
6688 /*** IUnknown Methods ***/
6689 d3d_device1_QueryInterface,
6690 d3d_device1_AddRef,
6691 d3d_device1_Release,
6692 /*** IDirect3DDevice1 ***/
6693 d3d_device1_Initialize,
6694 d3d_device1_GetCaps,
6695 d3d_device1_SwapTextureHandles,
6696 d3d_device1_CreateExecuteBuffer,
6697 d3d_device1_GetStats,
6698 d3d_device1_Execute,
6699 d3d_device1_AddViewport,
6700 d3d_device1_DeleteViewport,
6701 d3d_device1_NextViewport,
6702 d3d_device1_Pick,
6703 d3d_device1_GetPickRecords,
6704 d3d_device1_EnumTextureFormats,
6705 d3d_device1_CreateMatrix,
6706 d3d_device1_SetMatrix,
6707 d3d_device1_GetMatrix,
6708 d3d_device1_DeleteMatrix,
6709 d3d_device1_BeginScene,
6710 d3d_device1_EndScene,
6711 d3d_device1_GetDirect3D
6714 static const struct IUnknownVtbl d3d_device_inner_vtbl =
6716 d3d_device_inner_QueryInterface,
6717 d3d_device_inner_AddRef,
6718 d3d_device_inner_Release,
6721 struct d3d_device *unsafe_impl_from_IDirect3DDevice7(IDirect3DDevice7 *iface)
6723 if (!iface) return NULL;
6724 assert((iface->lpVtbl == &d3d_device7_fpu_preserve_vtbl) || (iface->lpVtbl == &d3d_device7_fpu_setup_vtbl));
6725 return CONTAINING_RECORD(iface, struct d3d_device, IDirect3DDevice7_iface);
6728 struct d3d_device *unsafe_impl_from_IDirect3DDevice3(IDirect3DDevice3 *iface)
6730 if (!iface) return NULL;
6731 assert(iface->lpVtbl == &d3d_device3_vtbl);
6732 return CONTAINING_RECORD(iface, struct d3d_device, IDirect3DDevice3_iface);
6735 struct d3d_device *unsafe_impl_from_IDirect3DDevice2(IDirect3DDevice2 *iface)
6737 if (!iface) return NULL;
6738 assert(iface->lpVtbl == &d3d_device2_vtbl);
6739 return CONTAINING_RECORD(iface, struct d3d_device, IDirect3DDevice2_iface);
6742 struct d3d_device *unsafe_impl_from_IDirect3DDevice(IDirect3DDevice *iface)
6744 if (!iface) return NULL;
6745 assert(iface->lpVtbl == &d3d_device1_vtbl);
6746 return CONTAINING_RECORD(iface, struct d3d_device, IDirect3DDevice_iface);
6749 enum wined3d_depth_buffer_type d3d_device_update_depth_stencil(struct d3d_device *device)
6751 IDirectDrawSurface7 *depthStencil = NULL;
6752 IDirectDrawSurface7 *render_target;
6753 static DDSCAPS2 depthcaps = { DDSCAPS_ZBUFFER, 0, 0, {0} };
6754 struct ddraw_surface *dsi;
6756 if (device->rt_iface && SUCCEEDED(IUnknown_QueryInterface(device->rt_iface,
6757 &IID_IDirectDrawSurface7, (void **)&render_target)))
6759 IDirectDrawSurface7_GetAttachedSurface(render_target, &depthcaps, &depthStencil);
6760 IDirectDrawSurface7_Release(render_target);
6762 if (!depthStencil)
6764 TRACE("Setting wined3d depth stencil to NULL\n");
6765 wined3d_device_set_depth_stencil_view(device->wined3d_device, NULL);
6766 return WINED3D_ZB_FALSE;
6769 dsi = impl_from_IDirectDrawSurface7(depthStencil);
6770 wined3d_device_set_depth_stencil_view(device->wined3d_device,
6771 ddraw_surface_get_rendertarget_view(dsi));
6773 IDirectDrawSurface7_Release(depthStencil);
6774 return WINED3D_ZB_TRUE;
6777 static HRESULT d3d_device_init(struct d3d_device *device, struct ddraw *ddraw,
6778 struct ddraw_surface *target, IUnknown *rt_iface, UINT version, IUnknown *outer_unknown)
6780 static const D3DMATRIX ident =
6782 1.0f, 0.0f, 0.0f, 0.0f,
6783 0.0f, 1.0f, 0.0f, 0.0f,
6784 0.0f, 0.0f, 1.0f, 0.0f,
6785 0.0f, 0.0f, 0.0f, 1.0f,
6787 HRESULT hr;
6789 if (ddraw->cooperative_level & DDSCL_FPUPRESERVE)
6790 device->IDirect3DDevice7_iface.lpVtbl = &d3d_device7_fpu_preserve_vtbl;
6791 else
6792 device->IDirect3DDevice7_iface.lpVtbl = &d3d_device7_fpu_setup_vtbl;
6794 device->IDirect3DDevice3_iface.lpVtbl = &d3d_device3_vtbl;
6795 device->IDirect3DDevice2_iface.lpVtbl = &d3d_device2_vtbl;
6796 device->IDirect3DDevice_iface.lpVtbl = &d3d_device1_vtbl;
6797 device->IUnknown_inner.lpVtbl = &d3d_device_inner_vtbl;
6798 device->ref = 1;
6799 device->version = version;
6801 if (outer_unknown)
6802 device->outer_unknown = outer_unknown;
6803 else
6804 device->outer_unknown = &device->IUnknown_inner;
6806 device->ddraw = ddraw;
6807 list_init(&device->viewport_list);
6809 if (!ddraw_handle_table_init(&device->handle_table, 64))
6811 ERR("Failed to initialize handle table.\n");
6812 return DDERR_OUTOFMEMORY;
6815 device->legacyTextureBlending = FALSE;
6816 device->legacy_projection = ident;
6817 device->legacy_clipspace = ident;
6819 /* This is for convenience. */
6820 device->wined3d_device = ddraw->wined3d_device;
6821 wined3d_device_incref(ddraw->wined3d_device);
6823 /* Render to the back buffer */
6824 if (FAILED(hr = wined3d_device_set_rendertarget_view(ddraw->wined3d_device,
6825 0, ddraw_surface_get_rendertarget_view(target), TRUE)))
6827 ERR("Failed to set render target, hr %#x.\n", hr);
6828 ddraw_handle_table_destroy(&device->handle_table);
6829 return hr;
6832 device->rt_iface = rt_iface;
6833 if (version != 1)
6834 IUnknown_AddRef(device->rt_iface);
6836 ddraw->d3ddevice = device;
6838 wined3d_device_set_render_state(ddraw->wined3d_device, WINED3D_RS_ZENABLE,
6839 d3d_device_update_depth_stencil(device));
6840 if (version == 1) /* Color keying is initially enabled for version 1 devices. */
6841 wined3d_device_set_render_state(ddraw->wined3d_device, WINED3D_RS_COLORKEYENABLE, TRUE);
6842 else if (version == 2)
6843 wined3d_device_set_render_state(ddraw->wined3d_device, WINED3D_RS_SPECULARENABLE, TRUE);
6844 if (version < 7)
6845 wined3d_device_set_render_state(ddraw->wined3d_device, WINED3D_RS_NORMALIZENORMALS, TRUE);
6847 return D3D_OK;
6850 HRESULT d3d_device_create(struct ddraw *ddraw, struct ddraw_surface *target, IUnknown *rt_iface,
6851 UINT version, struct d3d_device **device, IUnknown *outer_unknown)
6853 struct d3d_device *object;
6854 HRESULT hr;
6856 TRACE("ddraw %p, target %p, version %u, device %p, outer_unknown %p.\n",
6857 ddraw, target, version, device, outer_unknown);
6859 if (!(target->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6860 || (target->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
6862 WARN("Surface %p is not a render target.\n", target);
6863 return DDERR_INVALIDCAPS;
6866 if (!validate_surface_palette(target))
6868 WARN("Surface %p has an indexed pixel format, but no palette.\n", target);
6869 return DDERR_NOPALETTEATTACHED;
6872 if (!(target->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
6874 WARN("Surface %p is not in video memory.\n", target);
6875 return D3DERR_SURFACENOTINVIDMEM;
6878 if (ddraw->flags & DDRAW_NO3D)
6880 ERR_(winediag)("The application wants to create a Direct3D device, "
6881 "but the current DirectDrawRenderer does not support this.\n");
6883 return DDERR_NO3D;
6886 if (ddraw->d3ddevice)
6888 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
6889 return DDERR_INVALIDPARAMS;
6892 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
6893 if (!object)
6895 ERR("Failed to allocate device memory.\n");
6896 return DDERR_OUTOFMEMORY;
6899 if (FAILED(hr = d3d_device_init(object, ddraw, target, rt_iface, version, outer_unknown)))
6901 WARN("Failed to initialize device, hr %#x.\n", hr);
6902 HeapFree(GetProcessHeap(), 0, object);
6903 return hr;
6906 TRACE("Created device %p.\n", object);
6907 *device = object;
6909 return D3D_OK;