d3d8: Get rid of IDirect3DCubeTexture8Impl.
[wine/multimedia.git] / dlls / d3d8 / device.c
blob5cdaef22fcd38bb7abcc317749b07d8696df2e8c
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
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
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
41 BYTE *c = (BYTE *)&format;
43 /* Don't translate FOURCC formats */
44 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
46 switch(format)
48 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
49 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
50 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
51 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
52 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
53 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
54 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
55 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
56 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
57 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
58 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
59 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
60 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
61 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
73 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
74 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
75 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
76 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
77 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
78 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
79 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
80 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
81 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
82 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
83 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
84 default:
85 FIXME("Unhandled wined3d format %#x.\n", format);
86 return D3DFMT_UNKNOWN;
90 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
92 BYTE *c = (BYTE *)&format;
94 /* Don't translate FOURCC formats */
95 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
97 switch(format)
99 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
100 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
101 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
102 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
103 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
104 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
105 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
106 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
107 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
108 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
109 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
110 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
111 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
112 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
113 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
114 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
115 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
116 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
117 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
118 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
119 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
120 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
121 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
122 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
123 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
124 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
125 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
126 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
127 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
128 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
129 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
130 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
131 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
132 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
133 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
134 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
135 default:
136 FIXME("Unhandled D3DFORMAT %#x\n", format);
137 return WINED3DFMT_UNKNOWN;
141 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
143 switch(primitive_type)
145 case D3DPT_POINTLIST:
146 return primitive_count;
148 case D3DPT_LINELIST:
149 return primitive_count * 2;
151 case D3DPT_LINESTRIP:
152 return primitive_count + 1;
154 case D3DPT_TRIANGLELIST:
155 return primitive_count * 3;
157 case D3DPT_TRIANGLESTRIP:
158 case D3DPT_TRIANGLEFAN:
159 return primitive_count + 2;
161 default:
162 FIXME("Unhandled primitive type %#x\n", primitive_type);
163 return 0;
167 /* Handle table functions */
168 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
170 struct d3d8_handle_entry *entry;
172 if (t->free_entries)
174 DWORD index = t->free_entries - t->entries;
175 /* Use a free handle */
176 entry = t->free_entries;
177 if (entry->type != D3D8_HANDLE_FREE)
179 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
180 return D3D8_INVALID_HANDLE;
182 t->free_entries = entry->object;
183 entry->object = object;
184 entry->type = type;
186 return index;
189 if (!(t->entry_count < t->table_size))
191 /* Grow the table */
192 UINT new_size = t->table_size + (t->table_size >> 1);
193 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
194 0, t->entries, new_size * sizeof(*t->entries));
195 if (!new_entries)
197 ERR("Failed to grow the handle table.\n");
198 return D3D8_INVALID_HANDLE;
200 t->entries = new_entries;
201 t->table_size = new_size;
204 entry = &t->entries[t->entry_count];
205 entry->object = object;
206 entry->type = type;
208 return t->entry_count++;
211 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
213 struct d3d8_handle_entry *entry;
214 void *object;
216 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
218 WARN("Invalid handle %u passed.\n", handle);
219 return NULL;
222 entry = &t->entries[handle];
223 if (entry->type != type)
225 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
226 return NULL;
229 object = entry->object;
230 entry->object = t->free_entries;
231 entry->type = D3D8_HANDLE_FREE;
232 t->free_entries = entry;
234 return object;
237 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
239 struct d3d8_handle_entry *entry;
241 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
243 WARN("Invalid handle %u passed.\n", handle);
244 return NULL;
247 entry = &t->entries[handle];
248 if (entry->type != type)
250 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
251 return NULL;
254 return entry->object;
257 static inline IDirect3DDevice8Impl *impl_from_IDirect3DDevice8(IDirect3DDevice8 *iface)
259 return CONTAINING_RECORD(iface, IDirect3DDevice8Impl, IDirect3DDevice8_iface);
262 static HRESULT WINAPI IDirect3DDevice8Impl_QueryInterface(IDirect3DDevice8 *iface, REFIID riid,
263 void **ppobj)
265 TRACE("iface %p, riid %s, object %p.\n",
266 iface, debugstr_guid(riid), ppobj);
268 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
269 || IsEqualGUID(riid, &IID_IUnknown))
271 IUnknown_AddRef(iface);
272 *ppobj = iface;
273 return S_OK;
276 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
278 *ppobj = NULL;
279 return E_NOINTERFACE;
282 static ULONG WINAPI IDirect3DDevice8Impl_AddRef(IDirect3DDevice8 *iface)
284 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
285 ULONG ref = InterlockedIncrement(&This->ref);
287 TRACE("%p increasing refcount to %u.\n", iface, ref);
289 return ref;
292 static ULONG WINAPI IDirect3DDevice8Impl_Release(IDirect3DDevice8 *iface)
294 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
295 ULONG ref;
297 if (This->inDestruction) return 0;
298 ref = InterlockedDecrement(&This->ref);
300 TRACE("%p decreasing refcount to %u.\n", iface, ref);
302 if (ref == 0) {
303 unsigned i;
304 IDirect3D8 *parent = This->d3d_parent;
306 TRACE("Releasing wined3d device %p.\n", This->wined3d_device);
308 wined3d_mutex_lock();
310 This->inDestruction = TRUE;
312 for (i = 0; i < This->numConvertedDecls; ++i)
314 d3d8_vertex_declaration_destroy(This->decls[i].declaration);
316 HeapFree(GetProcessHeap(), 0, This->decls);
318 wined3d_device_uninit_3d(This->wined3d_device);
319 wined3d_device_release_focus_window(This->wined3d_device);
320 wined3d_device_decref(This->wined3d_device);
321 HeapFree(GetProcessHeap(), 0, This->handle_table.entries);
322 HeapFree(GetProcessHeap(), 0, This);
324 wined3d_mutex_unlock();
326 IDirect3D8_Release(parent);
328 return ref;
331 /* IDirect3DDevice Interface follow: */
332 static HRESULT WINAPI IDirect3DDevice8Impl_TestCooperativeLevel(IDirect3DDevice8 *iface)
334 IDirect3DDevice8Impl *device = impl_from_IDirect3DDevice8(iface);
336 TRACE("iface %p.\n", iface);
338 if (device->lost)
340 TRACE("Device is lost.\n");
341 return D3DERR_DEVICENOTRESET;
344 return D3D_OK;
347 static UINT WINAPI IDirect3DDevice8Impl_GetAvailableTextureMem(IDirect3DDevice8 *iface)
349 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
350 HRESULT hr;
352 TRACE("iface %p.\n", iface);
354 wined3d_mutex_lock();
355 hr = wined3d_device_get_available_texture_mem(This->wined3d_device);
356 wined3d_mutex_unlock();
358 return hr;
361 static HRESULT WINAPI IDirect3DDevice8Impl_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface,
362 DWORD Bytes)
364 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
366 TRACE("iface %p, byte_count %u.\n", iface, Bytes);
367 if (Bytes) FIXME("Byte count ignored.\n");
369 wined3d_mutex_lock();
370 wined3d_device_evict_managed_resources(This->wined3d_device);
371 wined3d_mutex_unlock();
373 return D3D_OK;
376 static HRESULT WINAPI IDirect3DDevice8Impl_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **ppD3D8)
378 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
380 TRACE("iface %p, d3d8 %p.\n", iface, ppD3D8);
382 if (NULL == ppD3D8) {
383 return D3DERR_INVALIDCALL;
386 return IDirect3D8_QueryInterface(This->d3d_parent, &IID_IDirect3D8, (void **)ppD3D8);
389 static HRESULT WINAPI IDirect3DDevice8Impl_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *pCaps)
391 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
392 HRESULT hrc = D3D_OK;
393 WINED3DCAPS *pWineCaps;
395 TRACE("iface %p, caps %p.\n", iface, pCaps);
397 if(NULL == pCaps){
398 return D3DERR_INVALIDCALL;
400 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
401 if(pWineCaps == NULL){
402 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
405 wined3d_mutex_lock();
406 hrc = wined3d_device_get_device_caps(This->wined3d_device, pWineCaps);
407 wined3d_mutex_unlock();
409 fixup_caps(pWineCaps);
410 WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
411 HeapFree(GetProcessHeap(), 0, pWineCaps);
413 TRACE("Returning %p %p\n", This, pCaps);
414 return hrc;
417 static HRESULT WINAPI IDirect3DDevice8Impl_GetDisplayMode(IDirect3DDevice8 *iface,
418 D3DDISPLAYMODE *pMode)
420 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
421 HRESULT hr;
423 TRACE("iface %p, mode %p.\n", iface, pMode);
425 wined3d_mutex_lock();
426 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, (struct wined3d_display_mode *)pMode);
427 wined3d_mutex_unlock();
429 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
431 return hr;
434 static HRESULT WINAPI IDirect3DDevice8Impl_GetCreationParameters(IDirect3DDevice8 *iface,
435 D3DDEVICE_CREATION_PARAMETERS *pParameters)
437 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
438 HRESULT hr;
440 TRACE("iface %p, parameters %p.\n", iface, pParameters);
442 wined3d_mutex_lock();
443 hr = wined3d_device_get_creation_parameters(This->wined3d_device,
444 (struct wined3d_device_creation_parameters *)pParameters);
445 wined3d_mutex_unlock();
447 return hr;
450 static HRESULT WINAPI IDirect3DDevice8Impl_SetCursorProperties(IDirect3DDevice8 *iface,
451 UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8 *pCursorBitmap)
453 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
454 IDirect3DSurface8Impl *pSurface = unsafe_impl_from_IDirect3DSurface8(pCursorBitmap);
455 HRESULT hr;
457 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
458 iface, XHotSpot, YHotSpot, pCursorBitmap);
460 if (!pCursorBitmap)
462 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
463 return D3DERR_INVALIDCALL;
466 wined3d_mutex_lock();
467 hr = wined3d_device_set_cursor_properties(This->wined3d_device, XHotSpot, YHotSpot, pSurface->wined3d_surface);
468 wined3d_mutex_unlock();
470 return hr;
473 static void WINAPI IDirect3DDevice8Impl_SetCursorPosition(IDirect3DDevice8 *iface,
474 UINT XScreenSpace, UINT YScreenSpace, DWORD Flags)
476 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
478 TRACE("iface %p, x %u, y %u, flags %#x.\n",
479 iface, XScreenSpace, YScreenSpace, Flags);
481 wined3d_mutex_lock();
482 wined3d_device_set_cursor_position(This->wined3d_device, XScreenSpace, YScreenSpace, Flags);
483 wined3d_mutex_unlock();
486 static BOOL WINAPI IDirect3DDevice8Impl_ShowCursor(IDirect3DDevice8 *iface, BOOL bShow)
488 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
489 BOOL ret;
491 TRACE("iface %p, show %#x.\n", iface, bShow);
493 wined3d_mutex_lock();
494 ret = wined3d_device_show_cursor(This->wined3d_device, bShow);
495 wined3d_mutex_unlock();
497 return ret;
500 static HRESULT WINAPI IDirect3DDevice8Impl_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
501 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
503 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
504 IDirect3DSwapChain8Impl *object;
505 HRESULT hr;
507 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
508 iface, present_parameters, swapchain);
510 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
511 if (!object)
513 ERR("Failed to allocate swapchain memory.\n");
514 return E_OUTOFMEMORY;
517 hr = swapchain_init(object, This, present_parameters);
518 if (FAILED(hr))
520 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
521 HeapFree(GetProcessHeap(), 0, object);
522 return hr;
525 TRACE("Created swapchain %p.\n", object);
526 *swapchain = &object->IDirect3DSwapChain8_iface;
528 return D3D_OK;
531 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
533 struct wined3d_resource_desc desc;
535 wined3d_resource_get_desc(resource, &desc);
536 if (desc.pool == WINED3D_POOL_DEFAULT)
538 IDirect3DSurface8 *surface;
540 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
542 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
543 return D3DERR_DEVICELOST;
546 surface = wined3d_resource_get_parent(resource);
548 IDirect3DSurface8_AddRef(surface);
549 if (IDirect3DSurface8_Release(surface))
551 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
552 return D3DERR_DEVICELOST;
555 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
558 return D3D_OK;
561 static HRESULT WINAPI IDirect3DDevice8Impl_Reset(IDirect3DDevice8 *iface,
562 D3DPRESENT_PARAMETERS *pPresentationParameters)
564 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
565 struct wined3d_swapchain_desc swapchain_desc;
566 HRESULT hr;
568 TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
570 wined3d_mutex_lock();
572 swapchain_desc.backbuffer_width = pPresentationParameters->BackBufferWidth;
573 swapchain_desc.backbuffer_height = pPresentationParameters->BackBufferHeight;
574 swapchain_desc.backbuffer_format = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
575 swapchain_desc.backbuffer_count = pPresentationParameters->BackBufferCount;
576 swapchain_desc.multisample_type = pPresentationParameters->MultiSampleType;
577 swapchain_desc.multisample_quality = 0; /* d3d9 only */
578 swapchain_desc.swap_effect = pPresentationParameters->SwapEffect;
579 swapchain_desc.device_window = pPresentationParameters->hDeviceWindow;
580 swapchain_desc.windowed = pPresentationParameters->Windowed;
581 swapchain_desc.enable_auto_depth_stencil = pPresentationParameters->EnableAutoDepthStencil;
582 swapchain_desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
583 swapchain_desc.flags = pPresentationParameters->Flags;
584 swapchain_desc.refresh_rate = pPresentationParameters->FullScreen_RefreshRateInHz;
585 swapchain_desc.swap_interval = pPresentationParameters->FullScreen_PresentationInterval;
586 swapchain_desc.auto_restore_display_mode = TRUE;
588 hr = wined3d_device_reset(This->wined3d_device, &swapchain_desc, reset_enum_callback);
589 if (SUCCEEDED(hr))
591 hr = wined3d_device_set_render_state(This->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
592 This->lost = FALSE;
594 else
596 This->lost = TRUE;
598 wined3d_mutex_unlock();
600 return hr;
603 static HRESULT WINAPI IDirect3DDevice8Impl_Present(IDirect3DDevice8 *iface, const RECT *pSourceRect,
604 const RECT *pDestRect, HWND hDestWindowOverride, const RGNDATA *pDirtyRegion)
606 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
607 HRESULT hr;
609 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
610 iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
612 wined3d_mutex_lock();
613 hr = wined3d_device_present(This->wined3d_device, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
614 wined3d_mutex_unlock();
616 return hr;
619 static HRESULT WINAPI IDirect3DDevice8Impl_GetBackBuffer(IDirect3DDevice8 *iface,
620 UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8 **ppBackBuffer)
622 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
623 struct wined3d_surface *wined3d_surface = NULL;
624 HRESULT hr;
626 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
627 iface, BackBuffer, Type, ppBackBuffer);
629 wined3d_mutex_lock();
630 hr = wined3d_device_get_back_buffer(This->wined3d_device, 0,
631 BackBuffer, (enum wined3d_backbuffer_type)Type, &wined3d_surface);
632 if (SUCCEEDED(hr) && wined3d_surface && ppBackBuffer)
634 *ppBackBuffer = wined3d_surface_get_parent(wined3d_surface);
635 IDirect3DSurface8_AddRef(*ppBackBuffer);
636 wined3d_surface_decref(wined3d_surface);
638 wined3d_mutex_unlock();
640 return hr;
643 static HRESULT WINAPI IDirect3DDevice8Impl_GetRasterStatus(IDirect3DDevice8 *iface,
644 D3DRASTER_STATUS *pRasterStatus)
646 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
647 HRESULT hr;
649 TRACE("iface %p, raster_status %p.\n", iface, pRasterStatus);
651 wined3d_mutex_lock();
652 hr = wined3d_device_get_raster_status(This->wined3d_device, 0, (struct wined3d_raster_status *)pRasterStatus);
653 wined3d_mutex_unlock();
655 return hr;
658 static void WINAPI IDirect3DDevice8Impl_SetGammaRamp(IDirect3DDevice8 *iface, DWORD Flags,
659 const D3DGAMMARAMP *pRamp)
661 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
663 TRACE("iface %p, flags %#x, ramp %p.\n", iface, Flags, pRamp);
665 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
666 wined3d_mutex_lock();
667 wined3d_device_set_gamma_ramp(This->wined3d_device, 0, Flags, (const struct wined3d_gamma_ramp *)pRamp);
668 wined3d_mutex_unlock();
671 static void WINAPI IDirect3DDevice8Impl_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *pRamp)
673 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
675 TRACE("iface %p, ramp %p.\n", iface, pRamp);
677 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
678 wined3d_mutex_lock();
679 wined3d_device_get_gamma_ramp(This->wined3d_device, 0, (struct wined3d_gamma_ramp *)pRamp);
680 wined3d_mutex_unlock();
683 static HRESULT WINAPI IDirect3DDevice8Impl_CreateTexture(IDirect3DDevice8 *iface,
684 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
685 D3DPOOL pool, IDirect3DTexture8 **texture)
687 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
688 struct d3d8_texture *object;
689 HRESULT hr;
691 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
692 iface, width, height, levels, usage, format, pool, texture);
694 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
695 if (!object)
697 ERR("Failed to allocate texture memory.\n");
698 return D3DERR_OUTOFVIDEOMEMORY;
701 hr = texture_init(object, This, width, height, levels, usage, format, pool);
702 if (FAILED(hr))
704 WARN("Failed to initialize texture, hr %#x.\n", hr);
705 HeapFree(GetProcessHeap(), 0, object);
706 return hr;
709 TRACE("Created texture %p.\n", object);
710 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
712 return D3D_OK;
715 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVolumeTexture(IDirect3DDevice8 *iface,
716 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
717 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
719 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
720 IDirect3DVolumeTexture8Impl *object;
721 HRESULT hr;
723 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
724 iface, width, height, depth, levels, usage, format, pool, texture);
726 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
727 if (!object)
729 ERR("Failed to allocate volume texture memory.\n");
730 return D3DERR_OUTOFVIDEOMEMORY;
733 hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
734 if (FAILED(hr))
736 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
737 HeapFree(GetProcessHeap(), 0, object);
738 return hr;
741 TRACE("Created volume texture %p.\n", object);
742 *texture = &object->IDirect3DVolumeTexture8_iface;
744 return D3D_OK;
747 static HRESULT WINAPI IDirect3DDevice8Impl_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
748 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
750 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
751 struct d3d8_texture *object;
752 HRESULT hr;
754 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
755 iface, edge_length, levels, usage, format, pool, texture);
757 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
758 if (!object)
760 ERR("Failed to allocate cube texture memory.\n");
761 return D3DERR_OUTOFVIDEOMEMORY;
764 hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
765 if (FAILED(hr))
767 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
768 HeapFree(GetProcessHeap(), 0, object);
769 return hr;
772 TRACE("Created cube texture %p.\n", object);
773 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
775 return hr;
778 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
779 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
781 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
782 IDirect3DVertexBuffer8Impl *object;
783 HRESULT hr;
785 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
786 iface, size, usage, fvf, pool, buffer);
788 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
789 if (!object)
791 ERR("Failed to allocate buffer memory.\n");
792 return D3DERR_OUTOFVIDEOMEMORY;
795 hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
796 if (FAILED(hr))
798 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
799 HeapFree(GetProcessHeap(), 0, object);
800 return hr;
803 TRACE("Created vertex buffer %p.\n", object);
804 *buffer = &object->IDirect3DVertexBuffer8_iface;
806 return D3D_OK;
809 static HRESULT WINAPI IDirect3DDevice8Impl_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
810 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
812 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
813 IDirect3DIndexBuffer8Impl *object;
814 HRESULT hr;
816 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
817 iface, size, usage, format, pool, buffer);
819 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
820 if (!object)
822 ERR("Failed to allocate buffer memory.\n");
823 return D3DERR_OUTOFVIDEOMEMORY;
826 hr = indexbuffer_init(object, This, size, usage, format, pool);
827 if (FAILED(hr))
829 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
830 HeapFree(GetProcessHeap(), 0, object);
831 return hr;
834 TRACE("Created index buffer %p.\n", object);
835 *buffer = &object->IDirect3DIndexBuffer8_iface;
837 return D3D_OK;
840 static HRESULT IDirect3DDevice8Impl_CreateSurface(IDirect3DDevice8Impl *device, UINT Width,
841 UINT Height, D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level,
842 IDirect3DSurface8 **ppSurface, UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample,
843 DWORD MultisampleQuality)
845 IDirect3DSurface8Impl *object;
846 HRESULT hr;
848 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p,\n"
849 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
850 device, Width, Height, Format, Lockable, Discard, Level, ppSurface,
851 Usage, Pool, MultiSample, MultisampleQuality);
853 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
854 if (!object)
856 FIXME("Failed to allocate surface memory.\n");
857 return D3DERR_OUTOFVIDEOMEMORY;
860 hr = surface_init(object, device, Width, Height, Format, Lockable, Discard, Level, Usage,
861 Pool, MultiSample, MultisampleQuality);
862 if (FAILED(hr))
864 WARN("Failed to initialize surface, hr %#x.\n", hr);
865 HeapFree(GetProcessHeap(), 0, object);
866 return hr;
869 TRACE("Created surface %p.\n", object);
870 *ppSurface = &object->IDirect3DSurface8_iface;
872 return D3D_OK;
875 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(IDirect3DDevice8 *iface, UINT Width,
876 UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable,
877 IDirect3DSurface8 **ppSurface)
879 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
880 HRESULT hr;
882 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
883 iface, Width, Height, Format, MultiSample, Lockable, ppSurface);
885 hr = IDirect3DDevice8Impl_CreateSurface(This, Width, Height, Format, Lockable,
886 FALSE /* Discard */, 0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,
887 MultiSample, 0);
889 return hr;
892 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
893 UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
894 IDirect3DSurface8 **ppSurface)
896 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
897 HRESULT hr;
899 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
900 iface, Width, Height, Format, MultiSample, ppSurface);
902 /* TODO: Verify that Discard is false */
903 hr = IDirect3DDevice8Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */, FALSE,
904 0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample, 0);
906 return hr;
909 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
910 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(IDirect3DDevice8 *iface, UINT Width,
911 UINT Height, D3DFORMAT Format, IDirect3DSurface8 **ppSurface)
913 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
914 HRESULT hr;
916 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
917 iface, Width, Height, Format, ppSurface);
919 hr = IDirect3DDevice8Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
920 FALSE /* Discard */, 0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */,
921 D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
923 return hr;
926 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(IDirect3DDevice8 *iface,
927 IDirect3DSurface8 *pSourceSurface, const RECT *pSourceRects, UINT cRects,
928 IDirect3DSurface8 *pDestinationSurface, const POINT *pDestPoints)
930 IDirect3DSurface8Impl *Source = unsafe_impl_from_IDirect3DSurface8(pSourceSurface);
931 IDirect3DSurface8Impl *Dest = unsafe_impl_from_IDirect3DSurface8(pDestinationSurface);
932 enum wined3d_format_id srcFormat, destFormat;
933 struct wined3d_resource_desc wined3d_desc;
934 struct wined3d_resource *wined3d_resource;
935 UINT src_w, src_h;
936 HRESULT hr;
938 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
939 iface, pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
941 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
942 * destination texture is in WINED3D_POOL_DEFAULT. */
944 wined3d_mutex_lock();
945 wined3d_resource = wined3d_surface_get_resource(Source->wined3d_surface);
946 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
947 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
949 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n",
950 pSourceSurface);
951 wined3d_mutex_unlock();
952 return D3DERR_INVALIDCALL;
954 srcFormat = wined3d_desc.format;
955 src_w = wined3d_desc.width;
956 src_h = wined3d_desc.height;
958 wined3d_resource = wined3d_surface_get_resource(Dest->wined3d_surface);
959 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
960 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
962 WARN("Dest %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n",
963 pDestinationSurface);
964 wined3d_mutex_unlock();
965 return D3DERR_INVALIDCALL;
967 destFormat = wined3d_desc.format;
969 /* Check that the source and destination formats match */
970 if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat)
972 WARN("Source %p format must match the dest %p format, returning D3DERR_INVALIDCALL.\n",
973 pSourceSurface, pDestinationSurface);
974 wined3d_mutex_unlock();
975 return D3DERR_INVALIDCALL;
977 else if (WINED3DFMT_UNKNOWN == destFormat)
979 TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
980 if (FAILED(hr = wined3d_surface_update_desc(Dest->wined3d_surface, wined3d_desc.width, wined3d_desc.height,
981 srcFormat, wined3d_desc.multisample_type, wined3d_desc.multisample_quality)))
983 WARN("Failed to update surface desc, hr %#x.\n", hr);
984 wined3d_mutex_unlock();
985 return hr;
989 /* Quick if complete copy ... */
990 if (!cRects && !pSourceRects && !pDestPoints)
992 RECT rect = {0, 0, src_w, src_h};
993 wined3d_surface_blt(Dest->wined3d_surface, &rect,
994 Source->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
996 else
998 unsigned int i;
999 /* Copy rect by rect */
1000 if (pSourceRects && pDestPoints)
1002 for (i = 0; i < cRects; ++i)
1004 UINT w = pSourceRects[i].right - pSourceRects[i].left;
1005 UINT h = pSourceRects[i].bottom - pSourceRects[i].top;
1006 RECT dst_rect = {pDestPoints[i].x, pDestPoints[i].y,
1007 pDestPoints[i].x + w, pDestPoints[i].y + h};
1009 wined3d_surface_blt(Dest->wined3d_surface, &dst_rect,
1010 Source->wined3d_surface, &pSourceRects[i], 0, NULL, WINED3D_TEXF_POINT);
1013 else
1015 for (i = 0; i < cRects; ++i)
1017 UINT w = pSourceRects[i].right - pSourceRects[i].left;
1018 UINT h = pSourceRects[i].bottom - pSourceRects[i].top;
1019 RECT dst_rect = {0, 0, w, h};
1021 wined3d_surface_blt(Dest->wined3d_surface, &dst_rect,
1022 Source->wined3d_surface, &pSourceRects[i], 0, NULL, WINED3D_TEXF_POINT);
1026 wined3d_mutex_unlock();
1028 return WINED3D_OK;
1031 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(IDirect3DDevice8 *iface,
1032 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1034 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1035 HRESULT hr;
1037 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1039 wined3d_mutex_lock();
1040 hr = wined3d_device_update_texture(This->wined3d_device,
1041 ((IDirect3DBaseTexture8Impl *)src_texture)->wined3d_texture,
1042 ((IDirect3DBaseTexture8Impl *)dst_texture)->wined3d_texture);
1043 wined3d_mutex_unlock();
1045 return hr;
1048 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(IDirect3DDevice8 *iface,
1049 IDirect3DSurface8 *pDestSurface)
1051 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1052 IDirect3DSurface8Impl *destSurface = unsafe_impl_from_IDirect3DSurface8(pDestSurface);
1053 HRESULT hr;
1055 TRACE("iface %p, dst_surface %p.\n", iface, pDestSurface);
1057 if (pDestSurface == NULL) {
1058 WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
1059 return D3DERR_INVALIDCALL;
1062 wined3d_mutex_lock();
1063 hr = wined3d_device_get_front_buffer_data(This->wined3d_device, 0, destSurface->wined3d_surface);
1064 wined3d_mutex_unlock();
1066 return hr;
1069 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(IDirect3DDevice8 *iface,
1070 IDirect3DSurface8 *pRenderTarget, IDirect3DSurface8 *pNewZStencil)
1072 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1073 IDirect3DSurface8Impl *pSurface = unsafe_impl_from_IDirect3DSurface8(pRenderTarget);
1074 IDirect3DSurface8Impl *pZSurface = unsafe_impl_from_IDirect3DSurface8(pNewZStencil);
1075 struct wined3d_surface *original_ds = NULL;
1076 HRESULT hr;
1078 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, pRenderTarget, pNewZStencil);
1080 wined3d_mutex_lock();
1082 if (pZSurface)
1084 struct wined3d_resource_desc ds_desc, rt_desc;
1085 struct wined3d_resource *wined3d_resource;
1086 struct wined3d_surface *original_rt = NULL;
1088 /* If no render target is passed in check the size against the current RT */
1089 if (!pRenderTarget)
1091 hr = wined3d_device_get_render_target(This->wined3d_device, 0, &original_rt);
1092 if (FAILED(hr) || !original_rt)
1094 wined3d_mutex_unlock();
1095 return hr;
1097 wined3d_resource = wined3d_surface_get_resource(original_rt);
1098 wined3d_surface_decref(original_rt);
1100 else
1101 wined3d_resource = wined3d_surface_get_resource(pSurface->wined3d_surface);
1102 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1104 wined3d_resource = wined3d_surface_get_resource(pZSurface->wined3d_surface);
1105 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1107 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1109 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1110 wined3d_mutex_unlock();
1111 return D3DERR_INVALIDCALL;
1115 hr = wined3d_device_get_depth_stencil(This->wined3d_device, &original_ds);
1116 if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
1118 hr = wined3d_device_set_depth_stencil(This->wined3d_device, pZSurface ? pZSurface->wined3d_surface : NULL);
1119 if (SUCCEEDED(hr) && pRenderTarget)
1121 hr = wined3d_device_set_render_target(This->wined3d_device, 0, pSurface->wined3d_surface, TRUE);
1122 if (FAILED(hr))
1123 wined3d_device_set_depth_stencil(This->wined3d_device, original_ds);
1126 if (original_ds)
1127 wined3d_surface_decref(original_ds);
1129 wined3d_mutex_unlock();
1131 return hr;
1134 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderTarget(IDirect3DDevice8 *iface,
1135 IDirect3DSurface8 **ppRenderTarget)
1137 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1138 struct wined3d_surface *wined3d_surface;
1139 HRESULT hr;
1141 TRACE("iface %p, render_target %p.\n", iface, ppRenderTarget);
1143 if (ppRenderTarget == NULL) {
1144 return D3DERR_INVALIDCALL;
1147 wined3d_mutex_lock();
1148 hr = wined3d_device_get_render_target(This->wined3d_device, 0, &wined3d_surface);
1149 if (SUCCEEDED(hr) && wined3d_surface)
1151 *ppRenderTarget = wined3d_surface_get_parent(wined3d_surface);
1152 IDirect3DSurface8_AddRef(*ppRenderTarget);
1153 wined3d_surface_decref(wined3d_surface);
1155 else
1157 FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
1158 *ppRenderTarget = NULL;
1160 wined3d_mutex_unlock();
1162 return hr;
1165 static HRESULT WINAPI IDirect3DDevice8Impl_GetDepthStencilSurface(IDirect3DDevice8 *iface,
1166 IDirect3DSurface8 **ppZStencilSurface)
1168 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1169 struct wined3d_surface *wined3d_surface;
1170 HRESULT hr;
1172 TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1174 if(ppZStencilSurface == NULL){
1175 return D3DERR_INVALIDCALL;
1178 wined3d_mutex_lock();
1179 hr = wined3d_device_get_depth_stencil(This->wined3d_device, &wined3d_surface);
1180 if (SUCCEEDED(hr))
1182 *ppZStencilSurface = wined3d_surface_get_parent(wined3d_surface);
1183 IDirect3DSurface8_AddRef(*ppZStencilSurface);
1184 wined3d_surface_decref(wined3d_surface);
1186 else
1188 if (hr != WINED3DERR_NOTFOUND)
1189 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1190 *ppZStencilSurface = NULL;
1192 wined3d_mutex_unlock();
1194 return hr;
1197 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(IDirect3DDevice8 *iface)
1199 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1200 HRESULT hr;
1202 TRACE("iface %p.\n", iface);
1204 wined3d_mutex_lock();
1205 hr = wined3d_device_begin_scene(This->wined3d_device);
1206 wined3d_mutex_unlock();
1208 return hr;
1211 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice8Impl_EndScene(IDirect3DDevice8 *iface)
1213 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1214 HRESULT hr;
1216 TRACE("iface %p.\n", iface);
1218 wined3d_mutex_lock();
1219 hr = wined3d_device_end_scene(This->wined3d_device);
1220 wined3d_mutex_unlock();
1222 return hr;
1225 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1226 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1228 const struct wined3d_color c =
1230 ((color >> 16) & 0xff) / 255.0f,
1231 ((color >> 8) & 0xff) / 255.0f,
1232 (color & 0xff) / 255.0f,
1233 ((color >> 24) & 0xff) / 255.0f,
1235 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1236 HRESULT hr;
1238 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1239 iface, rect_count, rects, flags, color, z, stencil);
1241 wined3d_mutex_lock();
1242 hr = wined3d_device_clear(This->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1243 wined3d_mutex_unlock();
1245 return hr;
1248 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(IDirect3DDevice8 *iface,
1249 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *lpMatrix)
1251 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1252 HRESULT hr;
1254 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1256 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1257 wined3d_mutex_lock();
1258 hr = wined3d_device_set_transform(This->wined3d_device, State, (const struct wined3d_matrix *)lpMatrix);
1259 wined3d_mutex_unlock();
1261 return hr;
1264 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(IDirect3DDevice8 *iface,
1265 D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
1267 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1268 HRESULT hr;
1270 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1272 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1273 wined3d_mutex_lock();
1274 hr = wined3d_device_get_transform(This->wined3d_device, State, (struct wined3d_matrix *)pMatrix);
1275 wined3d_mutex_unlock();
1277 return hr;
1280 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(IDirect3DDevice8 *iface,
1281 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
1283 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1284 HRESULT hr;
1286 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1288 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1289 wined3d_mutex_lock();
1290 hr = wined3d_device_multiply_transform(This->wined3d_device, State, (const struct wined3d_matrix *)pMatrix);
1291 wined3d_mutex_unlock();
1293 return hr;
1296 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(IDirect3DDevice8 *iface,
1297 const D3DVIEWPORT8 *pViewport)
1299 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1300 HRESULT hr;
1302 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1304 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1305 wined3d_mutex_lock();
1306 hr = wined3d_device_set_viewport(This->wined3d_device, (const struct wined3d_viewport *)pViewport);
1307 wined3d_mutex_unlock();
1309 return hr;
1312 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(IDirect3DDevice8 *iface,
1313 D3DVIEWPORT8 *pViewport)
1315 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1316 HRESULT hr;
1318 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1320 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1321 wined3d_mutex_lock();
1322 hr = wined3d_device_get_viewport(This->wined3d_device, (struct wined3d_viewport *)pViewport);
1323 wined3d_mutex_unlock();
1325 return hr;
1328 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(IDirect3DDevice8 *iface,
1329 const D3DMATERIAL8 *pMaterial)
1331 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1332 HRESULT hr;
1334 TRACE("iface %p, material %p.\n", iface, pMaterial);
1336 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1337 wined3d_mutex_lock();
1338 hr = wined3d_device_set_material(This->wined3d_device, (const struct wined3d_material *)pMaterial);
1339 wined3d_mutex_unlock();
1341 return hr;
1344 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(IDirect3DDevice8 *iface,
1345 D3DMATERIAL8 *pMaterial)
1347 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1348 HRESULT hr;
1350 TRACE("iface %p, material %p.\n", iface, pMaterial);
1352 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1353 wined3d_mutex_lock();
1354 hr = wined3d_device_get_material(This->wined3d_device, (struct wined3d_material *)pMaterial);
1355 wined3d_mutex_unlock();
1357 return hr;
1360 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(IDirect3DDevice8 *iface, DWORD Index,
1361 const D3DLIGHT8 *pLight)
1363 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1364 HRESULT hr;
1366 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1368 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1369 wined3d_mutex_lock();
1370 hr = wined3d_device_set_light(This->wined3d_device, Index, (const struct wined3d_light *)pLight);
1371 wined3d_mutex_unlock();
1373 return hr;
1376 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(IDirect3DDevice8 *iface, DWORD Index,
1377 D3DLIGHT8 *pLight)
1379 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1380 HRESULT hr;
1382 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1384 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1385 wined3d_mutex_lock();
1386 hr = wined3d_device_get_light(This->wined3d_device, Index, (struct wined3d_light *)pLight);
1387 wined3d_mutex_unlock();
1389 return hr;
1392 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(IDirect3DDevice8 *iface, DWORD Index,
1393 BOOL Enable)
1395 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1396 HRESULT hr;
1398 TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1400 wined3d_mutex_lock();
1401 hr = wined3d_device_set_light_enable(This->wined3d_device, Index, Enable);
1402 wined3d_mutex_unlock();
1404 return hr;
1407 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(IDirect3DDevice8 *iface, DWORD Index,
1408 BOOL *pEnable)
1410 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1411 HRESULT hr;
1413 TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1415 wined3d_mutex_lock();
1416 hr = wined3d_device_get_light_enable(This->wined3d_device, Index, pEnable);
1417 wined3d_mutex_unlock();
1419 return hr;
1422 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(IDirect3DDevice8 *iface, DWORD Index,
1423 const float *pPlane)
1425 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1426 HRESULT hr;
1428 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1430 wined3d_mutex_lock();
1431 hr = wined3d_device_set_clip_plane(This->wined3d_device, Index, pPlane);
1432 wined3d_mutex_unlock();
1434 return hr;
1437 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(IDirect3DDevice8 *iface, DWORD Index,
1438 float *pPlane)
1440 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1441 HRESULT hr;
1443 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1445 wined3d_mutex_lock();
1446 hr = wined3d_device_get_clip_plane(This->wined3d_device, Index, pPlane);
1447 wined3d_mutex_unlock();
1449 return hr;
1452 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(IDirect3DDevice8 *iface,
1453 D3DRENDERSTATETYPE State, DWORD Value)
1455 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1456 HRESULT hr;
1458 TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1460 wined3d_mutex_lock();
1461 switch (State)
1463 case D3DRS_ZBIAS:
1464 hr = wined3d_device_set_render_state(This->wined3d_device, WINED3D_RS_DEPTHBIAS, Value);
1465 break;
1467 default:
1468 hr = wined3d_device_set_render_state(This->wined3d_device, State, Value);
1470 wined3d_mutex_unlock();
1472 return hr;
1475 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(IDirect3DDevice8 *iface,
1476 D3DRENDERSTATETYPE State, DWORD *pValue)
1478 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1479 HRESULT hr;
1481 TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1483 wined3d_mutex_lock();
1484 switch (State)
1486 case D3DRS_ZBIAS:
1487 hr = wined3d_device_get_render_state(This->wined3d_device, WINED3D_RS_DEPTHBIAS, pValue);
1488 break;
1490 default:
1491 hr = wined3d_device_get_render_state(This->wined3d_device, State, pValue);
1493 wined3d_mutex_unlock();
1495 return hr;
1498 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(IDirect3DDevice8 *iface)
1500 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1501 HRESULT hr;
1503 TRACE("iface %p.\n", iface);
1505 wined3d_mutex_lock();
1506 hr = wined3d_device_begin_stateblock(This->wined3d_device);
1507 wined3d_mutex_unlock();
1509 return hr;
1512 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(IDirect3DDevice8 *iface, DWORD *pToken)
1514 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1515 struct wined3d_stateblock *stateblock;
1516 HRESULT hr;
1518 TRACE("iface %p, token %p.\n", iface, pToken);
1520 /* Tell wineD3D to endstateblock before anything else (in case we run out
1521 * of memory later and cause locking problems)
1523 wined3d_mutex_lock();
1524 hr = wined3d_device_end_stateblock(This->wined3d_device, &stateblock);
1525 if (FAILED(hr))
1527 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1528 wined3d_mutex_unlock();
1529 return hr;
1532 *pToken = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1533 wined3d_mutex_unlock();
1535 if (*pToken == D3D8_INVALID_HANDLE)
1537 ERR("Failed to create a handle\n");
1538 wined3d_mutex_lock();
1539 wined3d_stateblock_decref(stateblock);
1540 wined3d_mutex_unlock();
1541 return E_FAIL;
1543 ++*pToken;
1545 TRACE("Returning %#x (%p).\n", *pToken, stateblock);
1547 return hr;
1550 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1552 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1553 struct wined3d_stateblock *stateblock;
1554 HRESULT hr;
1556 TRACE("iface %p, token %#x.\n", iface, Token);
1558 if (!Token) return D3D_OK;
1560 wined3d_mutex_lock();
1561 stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1562 if (!stateblock)
1564 WARN("Invalid handle (%#x) passed.\n", Token);
1565 wined3d_mutex_unlock();
1566 return D3DERR_INVALIDCALL;
1568 hr = wined3d_stateblock_apply(stateblock);
1569 wined3d_mutex_unlock();
1571 return hr;
1574 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1576 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1577 struct wined3d_stateblock *stateblock;
1578 HRESULT hr;
1580 TRACE("iface %p, token %#x.\n", iface, Token);
1582 wined3d_mutex_lock();
1583 stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1584 if (!stateblock)
1586 WARN("Invalid handle (%#x) passed.\n", Token);
1587 wined3d_mutex_unlock();
1588 return D3DERR_INVALIDCALL;
1590 hr = wined3d_stateblock_capture(stateblock);
1591 wined3d_mutex_unlock();
1593 return hr;
1596 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1598 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1599 struct wined3d_stateblock *stateblock;
1601 TRACE("iface %p, token %#x.\n", iface, Token);
1603 wined3d_mutex_lock();
1604 stateblock = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1606 if (!stateblock)
1608 WARN("Invalid handle (%#x) passed.\n", Token);
1609 wined3d_mutex_unlock();
1610 return D3DERR_INVALIDCALL;
1613 if (wined3d_stateblock_decref(stateblock))
1615 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1617 wined3d_mutex_unlock();
1619 return D3D_OK;
1622 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1623 D3DSTATEBLOCKTYPE Type, DWORD *handle)
1625 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1626 struct wined3d_stateblock *stateblock;
1627 HRESULT hr;
1629 TRACE("iface %p, type %#x, handle %p.\n", iface, Type, handle);
1631 if (Type != D3DSBT_ALL
1632 && Type != D3DSBT_PIXELSTATE
1633 && Type != D3DSBT_VERTEXSTATE)
1635 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1636 return D3DERR_INVALIDCALL;
1639 wined3d_mutex_lock();
1640 hr = wined3d_stateblock_create(This->wined3d_device, (enum wined3d_stateblock_type)Type, &stateblock);
1641 if (FAILED(hr))
1643 wined3d_mutex_unlock();
1644 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1645 return hr;
1648 *handle = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1649 wined3d_mutex_unlock();
1651 if (*handle == D3D8_INVALID_HANDLE)
1653 ERR("Failed to allocate a handle.\n");
1654 wined3d_mutex_lock();
1655 wined3d_stateblock_decref(stateblock);
1656 wined3d_mutex_unlock();
1657 return E_FAIL;
1659 ++*handle;
1661 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1663 return hr;
1666 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(IDirect3DDevice8 *iface,
1667 const D3DCLIPSTATUS8 *pClipStatus)
1669 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1670 HRESULT hr;
1672 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1673 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1675 wined3d_mutex_lock();
1676 hr = wined3d_device_set_clip_status(This->wined3d_device, (const struct wined3d_clip_status *)pClipStatus);
1677 wined3d_mutex_unlock();
1679 return hr;
1682 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(IDirect3DDevice8 *iface,
1683 D3DCLIPSTATUS8 *pClipStatus)
1685 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1686 HRESULT hr;
1688 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1690 wined3d_mutex_lock();
1691 hr = wined3d_device_get_clip_status(This->wined3d_device, (struct wined3d_clip_status *)pClipStatus);
1692 wined3d_mutex_unlock();
1694 return hr;
1697 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(IDirect3DDevice8 *iface,
1698 DWORD Stage, IDirect3DBaseTexture8 **ppTexture)
1700 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1701 struct wined3d_texture *wined3d_texture;
1702 HRESULT hr;
1704 TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1706 if(ppTexture == NULL){
1707 return D3DERR_INVALIDCALL;
1710 wined3d_mutex_lock();
1711 hr = wined3d_device_get_texture(This->wined3d_device, Stage, &wined3d_texture);
1712 if (FAILED(hr))
1714 WARN("Failed to get texture for stage %u, hr %#x.\n", Stage, hr);
1715 wined3d_mutex_unlock();
1716 *ppTexture = NULL;
1717 return hr;
1720 if (wined3d_texture)
1722 *ppTexture = wined3d_texture_get_parent(wined3d_texture);
1723 IDirect3DBaseTexture8_AddRef(*ppTexture);
1724 wined3d_texture_decref(wined3d_texture);
1726 else
1728 *ppTexture = NULL;
1730 wined3d_mutex_unlock();
1732 return D3D_OK;
1735 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(IDirect3DDevice8 *iface, DWORD Stage,
1736 IDirect3DBaseTexture8 *pTexture)
1738 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1739 HRESULT hr;
1741 TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1743 wined3d_mutex_lock();
1744 hr = wined3d_device_set_texture(This->wined3d_device, Stage,
1745 pTexture ? ((IDirect3DBaseTexture8Impl *)pTexture)->wined3d_texture : NULL);
1746 wined3d_mutex_unlock();
1748 return hr;
1751 static const struct tss_lookup
1753 BOOL sampler_state;
1754 enum wined3d_texture_stage_state state;
1756 tss_lookup[] =
1758 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1759 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1760 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1761 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1762 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1763 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1764 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1765 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1766 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1767 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1768 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1769 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1770 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1771 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1772 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1773 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1774 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1775 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1776 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1777 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1778 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1779 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1780 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1781 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1782 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1783 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1784 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1785 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1786 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1789 static HRESULT WINAPI IDirect3DDevice8Impl_GetTextureStageState(IDirect3DDevice8 *iface,
1790 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1792 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1793 const struct tss_lookup *l;
1794 HRESULT hr;
1796 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1798 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1800 WARN("Invalid Type %#x passed.\n", Type);
1801 return D3D_OK;
1804 l = &tss_lookup[Type];
1806 wined3d_mutex_lock();
1807 if (l->sampler_state)
1808 hr = wined3d_device_get_sampler_state(This->wined3d_device, Stage, l->state, pValue);
1809 else
1810 hr = wined3d_device_get_texture_stage_state(This->wined3d_device, Stage, l->state, pValue);
1811 wined3d_mutex_unlock();
1813 return hr;
1816 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(IDirect3DDevice8 *iface,
1817 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1819 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1820 const struct tss_lookup *l;
1821 HRESULT hr;
1823 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1825 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1827 WARN("Invalid Type %#x passed.\n", Type);
1828 return D3D_OK;
1831 l = &tss_lookup[Type];
1833 wined3d_mutex_lock();
1834 if (l->sampler_state)
1835 hr = wined3d_device_set_sampler_state(This->wined3d_device, Stage, l->state, Value);
1836 else
1837 hr = wined3d_device_set_texture_stage_state(This->wined3d_device, Stage, l->state, Value);
1838 wined3d_mutex_unlock();
1840 return hr;
1843 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(IDirect3DDevice8 *iface,
1844 DWORD *pNumPasses)
1846 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1847 HRESULT hr;
1849 TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1851 wined3d_mutex_lock();
1852 hr = wined3d_device_validate_device(This->wined3d_device, pNumPasses);
1853 wined3d_mutex_unlock();
1855 return hr;
1858 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(IDirect3DDevice8 *iface,
1859 DWORD info_id, void *info, DWORD info_size)
1861 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1863 return D3D_OK;
1866 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(IDirect3DDevice8 *iface,
1867 UINT PaletteNumber, const PALETTEENTRY *pEntries)
1869 FIXME("iface %p, palette_idx %u, entries %p unimplemented\n", iface, PaletteNumber, pEntries);
1871 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1872 * does not have a d3d8/9-style palette API */
1874 return D3DERR_INVALIDCALL;
1877 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(IDirect3DDevice8 *iface,
1878 UINT PaletteNumber, PALETTEENTRY *pEntries)
1880 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1882 return D3DERR_INVALIDCALL;
1885 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(IDirect3DDevice8 *iface,
1886 UINT PaletteNumber)
1888 FIXME("iface %p, palette_idx %u unimplemented.\n", iface, PaletteNumber);
1890 return D3DERR_INVALIDCALL;
1893 static HRESULT WINAPI IDirect3DDevice8Impl_GetCurrentTexturePalette(IDirect3DDevice8 *iface,
1894 UINT *PaletteNumber)
1896 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, PaletteNumber);
1898 return D3DERR_INVALIDCALL;
1901 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface,
1902 D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1904 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1905 HRESULT hr;
1907 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1908 iface, PrimitiveType, StartVertex, PrimitiveCount);
1910 wined3d_mutex_lock();
1911 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1912 hr = wined3d_device_draw_primitive(This->wined3d_device, StartVertex,
1913 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1914 wined3d_mutex_unlock();
1916 return hr;
1919 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1920 D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertices, UINT startIndex,
1921 UINT primCount)
1923 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1924 HRESULT hr;
1926 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1927 iface, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1929 wined3d_mutex_lock();
1930 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1931 hr = wined3d_device_draw_indexed_primitive(This->wined3d_device, startIndex,
1932 vertex_count_from_primitive_count(PrimitiveType, primCount));
1933 wined3d_mutex_unlock();
1935 return hr;
1938 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1939 D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
1940 UINT VertexStreamZeroStride)
1942 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1943 HRESULT hr;
1945 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1946 iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1948 wined3d_mutex_lock();
1949 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1950 hr = wined3d_device_draw_primitive_up(This->wined3d_device,
1951 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1952 pVertexStreamZeroData, VertexStreamZeroStride);
1953 wined3d_mutex_unlock();
1955 return hr;
1958 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
1959 D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
1960 UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
1961 const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
1963 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1964 HRESULT hr;
1966 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1967 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1968 iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1969 pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1971 wined3d_mutex_lock();
1972 wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1973 hr = wined3d_device_draw_indexed_primitive_up(This->wined3d_device,
1974 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1975 wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1976 wined3d_mutex_unlock();
1978 return hr;
1981 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(IDirect3DDevice8 *iface,
1982 UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer8 *pDestBuffer,
1983 DWORD Flags)
1985 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1986 IDirect3DVertexBuffer8Impl *dest = unsafe_impl_from_IDirect3DVertexBuffer8(pDestBuffer);
1987 HRESULT hr;
1989 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
1990 iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, Flags);
1992 wined3d_mutex_lock();
1993 hr = wined3d_device_process_vertices(This->wined3d_device, SrcStartIndex, DestIndex,
1994 VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1995 wined3d_mutex_unlock();
1997 return hr;
2000 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
2001 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2003 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2004 struct d3d8_vertex_shader *object;
2005 DWORD shader_handle;
2006 DWORD handle;
2007 HRESULT hr;
2009 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2010 iface, declaration, byte_code, shader, usage);
2012 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2013 if (!object)
2015 ERR("Failed to allocate vertex shader memory.\n");
2016 *shader = 0;
2017 return E_OUTOFMEMORY;
2020 wined3d_mutex_lock();
2021 handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
2022 wined3d_mutex_unlock();
2023 if (handle == D3D8_INVALID_HANDLE)
2025 ERR("Failed to allocate vertex shader handle.\n");
2026 HeapFree(GetProcessHeap(), 0, object);
2027 *shader = 0;
2028 return E_OUTOFMEMORY;
2031 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2033 hr = d3d8_vertex_shader_init(object, This, declaration, byte_code, shader_handle, usage);
2034 if (FAILED(hr))
2036 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2037 wined3d_mutex_lock();
2038 d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
2039 wined3d_mutex_unlock();
2040 HeapFree(GetProcessHeap(), 0, object);
2041 *shader = 0;
2042 return hr;
2045 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2046 *shader = shader_handle;
2048 return D3D_OK;
2051 static struct d3d8_vertex_declaration *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
2053 struct d3d8_vertex_declaration *d3d8_declaration;
2054 HRESULT hr;
2055 int p, low, high; /* deliberately signed */
2056 struct FvfToDecl *convertedDecls = This->decls;
2058 TRACE("Searching for declaration for fvf %08x... ", fvf);
2060 low = 0;
2061 high = This->numConvertedDecls - 1;
2062 while(low <= high) {
2063 p = (low + high) >> 1;
2064 TRACE("%d ", p);
2065 if (convertedDecls[p].fvf == fvf)
2067 TRACE("found %p\n", convertedDecls[p].declaration);
2068 return convertedDecls[p].declaration;
2070 else if (convertedDecls[p].fvf < fvf)
2072 low = p + 1;
2073 } else {
2074 high = p - 1;
2077 TRACE("not found. Creating and inserting at position %d.\n", low);
2079 d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
2080 if (!d3d8_declaration)
2082 ERR("Memory allocation failed.\n");
2083 return NULL;
2086 hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, This, fvf);
2087 if (FAILED(hr))
2089 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2090 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2091 return NULL;
2094 if(This->declArraySize == This->numConvertedDecls) {
2095 int grow = This->declArraySize / 2;
2096 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2097 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2098 if (!convertedDecls)
2100 d3d8_vertex_declaration_destroy(d3d8_declaration);
2101 return NULL;
2103 This->decls = convertedDecls;
2104 This->declArraySize += grow;
2107 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
2108 convertedDecls[low].declaration = d3d8_declaration;
2109 convertedDecls[low].fvf = fvf;
2110 This->numConvertedDecls++;
2112 TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
2113 return d3d8_declaration;
2116 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(IDirect3DDevice8 *iface, DWORD pShader)
2118 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2119 struct d3d8_vertex_shader *shader;
2120 HRESULT hr;
2122 TRACE("iface %p, shader %#x.\n", iface, pShader);
2124 if (VS_HIGHESTFIXEDFXF >= pShader) {
2125 TRACE("Setting FVF, %#x\n", pShader);
2127 wined3d_mutex_lock();
2128 wined3d_device_set_vertex_declaration(This->wined3d_device,
2129 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
2130 wined3d_device_set_vertex_shader(This->wined3d_device, NULL);
2131 wined3d_mutex_unlock();
2133 return D3D_OK;
2136 TRACE("Setting shader\n");
2138 wined3d_mutex_lock();
2139 shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2140 if (!shader)
2142 WARN("Invalid handle (%#x) passed.\n", pShader);
2143 wined3d_mutex_unlock();
2145 return D3DERR_INVALIDCALL;
2148 hr = wined3d_device_set_vertex_declaration(This->wined3d_device,
2149 shader->vertex_declaration->wined3d_vertex_declaration);
2150 if (SUCCEEDED(hr))
2151 hr = wined3d_device_set_vertex_shader(This->wined3d_device, shader->wined3d_shader);
2152 wined3d_mutex_unlock();
2154 TRACE("Returning hr %#x\n", hr);
2156 return hr;
2159 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2161 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2162 struct wined3d_vertex_declaration *wined3d_declaration;
2163 struct d3d8_vertex_declaration *d3d8_declaration;
2164 HRESULT hr;
2166 TRACE("iface %p, shader %p.\n", iface, ppShader);
2168 wined3d_mutex_lock();
2169 hr = wined3d_device_get_vertex_declaration(This->wined3d_device, &wined3d_declaration);
2170 if (FAILED(hr))
2172 wined3d_mutex_unlock();
2173 WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
2174 This, hr, This->wined3d_device);
2175 return hr;
2178 if (!wined3d_declaration)
2180 wined3d_mutex_unlock();
2181 *ppShader = 0;
2182 return D3D_OK;
2185 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2186 wined3d_vertex_declaration_decref(wined3d_declaration);
2187 wined3d_mutex_unlock();
2188 *ppShader = d3d8_declaration->shader_handle;
2190 TRACE("(%p) : returning %#x\n", This, *ppShader);
2192 return hr;
2195 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD pShader)
2197 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2198 struct d3d8_vertex_shader *shader;
2199 struct wined3d_shader *cur;
2201 TRACE("iface %p, shader %#x.\n", iface, pShader);
2203 wined3d_mutex_lock();
2204 shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2205 if (!shader)
2207 WARN("Invalid handle (%#x) passed.\n", pShader);
2208 wined3d_mutex_unlock();
2210 return D3DERR_INVALIDCALL;
2213 cur = wined3d_device_get_vertex_shader(This->wined3d_device);
2214 if (cur)
2216 if (cur == shader->wined3d_shader)
2217 IDirect3DDevice8_SetVertexShader(iface, 0);
2218 wined3d_shader_decref(cur);
2221 wined3d_mutex_unlock();
2223 d3d8_vertex_shader_destroy(shader);
2225 return D3D_OK;
2228 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2229 DWORD Register, const void *pConstantData, DWORD ConstantCount)
2231 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2232 HRESULT hr;
2234 TRACE("iface %p, register %u, data %p, count %u.\n",
2235 iface, Register, pConstantData, ConstantCount);
2237 if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2238 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2239 Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2240 return D3DERR_INVALIDCALL;
2243 wined3d_mutex_lock();
2244 hr = wined3d_device_set_vs_consts_f(This->wined3d_device, Register, pConstantData, ConstantCount);
2245 wined3d_mutex_unlock();
2247 return hr;
2250 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2251 DWORD Register, void *pConstantData, DWORD ConstantCount)
2253 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2254 HRESULT hr;
2256 TRACE("iface %p, register %u, data %p, count %u.\n",
2257 iface, Register, pConstantData, ConstantCount);
2259 if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2260 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2261 Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2262 return D3DERR_INVALIDCALL;
2265 wined3d_mutex_lock();
2266 hr = wined3d_device_get_vs_consts_f(This->wined3d_device, Register, pConstantData, ConstantCount);
2267 wined3d_mutex_unlock();
2269 return hr;
2272 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2273 DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2275 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2276 struct d3d8_vertex_declaration *declaration;
2277 struct d3d8_vertex_shader *shader;
2279 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2280 iface, pVertexShader, pData, pSizeOfData);
2282 wined3d_mutex_lock();
2283 shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2284 wined3d_mutex_unlock();
2286 if (!shader)
2288 WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2289 return D3DERR_INVALIDCALL;
2291 declaration = shader->vertex_declaration;
2293 /* If pData is NULL, we just return the required size of the buffer. */
2294 if (!pData) {
2295 *pSizeOfData = declaration->elements_size;
2296 return D3D_OK;
2299 /* MSDN claims that if *pSizeOfData is smaller than the required size
2300 * we should write the required size and return D3DERR_MOREDATA.
2301 * That's not actually true. */
2302 if (*pSizeOfData < declaration->elements_size) {
2303 return D3DERR_INVALIDCALL;
2306 CopyMemory(pData, declaration->elements, declaration->elements_size);
2308 return D3D_OK;
2311 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2312 DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2314 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2315 struct d3d8_vertex_shader *shader = NULL;
2316 HRESULT hr;
2318 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2319 iface, pVertexShader, pData, pSizeOfData);
2321 wined3d_mutex_lock();
2322 shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2323 if (!shader)
2325 WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2326 wined3d_mutex_unlock();
2328 return D3DERR_INVALIDCALL;
2331 if (!shader->wined3d_shader)
2333 wined3d_mutex_unlock();
2334 *pSizeOfData = 0;
2335 return D3D_OK;
2338 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, pData, pSizeOfData);
2339 wined3d_mutex_unlock();
2341 return hr;
2344 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(IDirect3DDevice8 *iface,
2345 IDirect3DIndexBuffer8 *pIndexData, UINT baseVertexIndex)
2347 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2348 IDirect3DIndexBuffer8Impl *ib = unsafe_impl_from_IDirect3DIndexBuffer8(pIndexData);
2349 HRESULT hr;
2351 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, pIndexData, baseVertexIndex);
2353 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2354 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2355 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2356 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2357 * problem)
2359 wined3d_mutex_lock();
2360 wined3d_device_set_base_vertex_index(This->wined3d_device, baseVertexIndex);
2361 hr = wined3d_device_set_index_buffer(This->wined3d_device,
2362 ib ? ib->wineD3DIndexBuffer : NULL,
2363 ib ? ib->format : WINED3DFMT_UNKNOWN);
2364 wined3d_mutex_unlock();
2366 return hr;
2369 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(IDirect3DDevice8 *iface,
2370 IDirect3DIndexBuffer8 **ppIndexData, UINT *pBaseVertexIndex)
2372 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2373 struct wined3d_buffer *retIndexData = NULL;
2374 HRESULT hr;
2376 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, ppIndexData, pBaseVertexIndex);
2378 if(ppIndexData == NULL){
2379 return D3DERR_INVALIDCALL;
2382 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2383 wined3d_mutex_lock();
2384 *pBaseVertexIndex = wined3d_device_get_base_vertex_index(This->wined3d_device);
2385 hr = wined3d_device_get_index_buffer(This->wined3d_device, &retIndexData);
2386 if (SUCCEEDED(hr) && retIndexData)
2388 *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2389 IDirect3DIndexBuffer8_AddRef(*ppIndexData);
2390 wined3d_buffer_decref(retIndexData);
2391 } else {
2392 if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2393 *ppIndexData = NULL;
2395 wined3d_mutex_unlock();
2397 return hr;
2400 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(IDirect3DDevice8 *iface,
2401 const DWORD *byte_code, DWORD *shader)
2403 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2404 struct d3d8_pixel_shader *object;
2405 DWORD shader_handle;
2406 DWORD handle;
2407 HRESULT hr;
2409 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2411 if (!shader)
2413 TRACE("(%p) Invalid call\n", This);
2414 return D3DERR_INVALIDCALL;
2417 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2418 if (!object)
2420 ERR("Failed to allocate pixel shader memmory.\n");
2421 return E_OUTOFMEMORY;
2424 wined3d_mutex_lock();
2425 handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2426 wined3d_mutex_unlock();
2427 if (handle == D3D8_INVALID_HANDLE)
2429 ERR("Failed to allocate pixel shader handle.\n");
2430 HeapFree(GetProcessHeap(), 0, object);
2431 return E_OUTOFMEMORY;
2434 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2436 hr = d3d8_pixel_shader_init(object, This, byte_code, shader_handle);
2437 if (FAILED(hr))
2439 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2440 wined3d_mutex_lock();
2441 d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_PS);
2442 wined3d_mutex_unlock();
2443 HeapFree(GetProcessHeap(), 0, object);
2444 *shader = 0;
2445 return hr;
2448 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2449 *shader = shader_handle;
2451 return D3D_OK;
2454 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2456 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2457 struct d3d8_pixel_shader *shader;
2458 HRESULT hr;
2460 TRACE("iface %p, shader %#x.\n", iface, pShader);
2462 wined3d_mutex_lock();
2464 if (!pShader)
2466 hr = wined3d_device_set_pixel_shader(This->wined3d_device, NULL);
2467 wined3d_mutex_unlock();
2468 return hr;
2471 shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2472 if (!shader)
2474 WARN("Invalid handle (%#x) passed.\n", pShader);
2475 wined3d_mutex_unlock();
2476 return D3DERR_INVALIDCALL;
2479 TRACE("(%p) : Setting shader %p\n", This, shader);
2480 hr = wined3d_device_set_pixel_shader(This->wined3d_device, shader->wined3d_shader);
2481 wined3d_mutex_unlock();
2483 return hr;
2486 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2488 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2489 struct wined3d_shader *object;
2491 TRACE("iface %p, shader %p.\n", iface, ppShader);
2493 if (NULL == ppShader) {
2494 TRACE("(%p) Invalid call\n", This);
2495 return D3DERR_INVALIDCALL;
2498 wined3d_mutex_lock();
2499 object = wined3d_device_get_pixel_shader(This->wined3d_device);
2500 if (object)
2502 struct d3d8_pixel_shader *d3d8_shader;
2503 d3d8_shader = wined3d_shader_get_parent(object);
2504 wined3d_shader_decref(object);
2505 *ppShader = d3d8_shader->handle;
2507 else
2509 *ppShader = 0;
2511 wined3d_mutex_unlock();
2513 TRACE("(%p) : returning %#x\n", This, *ppShader);
2515 return D3D_OK;
2518 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2520 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2521 struct d3d8_pixel_shader *shader;
2522 struct wined3d_shader *cur;
2524 TRACE("iface %p, shader %#x.\n", iface, pShader);
2526 wined3d_mutex_lock();
2528 shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2529 if (!shader)
2531 WARN("Invalid handle (%#x) passed.\n", pShader);
2532 wined3d_mutex_unlock();
2533 return D3DERR_INVALIDCALL;
2536 cur = wined3d_device_get_pixel_shader(This->wined3d_device);
2537 if (cur)
2539 if (cur == shader->wined3d_shader)
2540 IDirect3DDevice8_SetPixelShader(iface, 0);
2541 wined3d_shader_decref(cur);
2544 wined3d_mutex_unlock();
2546 d3d8_pixel_shader_destroy(shader);
2548 return D3D_OK;
2551 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2552 DWORD Register, const void *pConstantData, DWORD ConstantCount)
2554 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2555 HRESULT hr;
2557 TRACE("iface %p, register %u, data %p, count %u.\n",
2558 iface, Register, pConstantData, ConstantCount);
2560 wined3d_mutex_lock();
2561 hr = wined3d_device_set_ps_consts_f(This->wined3d_device, Register, pConstantData, ConstantCount);
2562 wined3d_mutex_unlock();
2564 return hr;
2567 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2568 DWORD Register, void *pConstantData, DWORD ConstantCount)
2570 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2571 HRESULT hr;
2573 TRACE("iface %p, register %u, data %p, count %u.\n",
2574 iface, Register, pConstantData, ConstantCount);
2576 wined3d_mutex_lock();
2577 hr = wined3d_device_get_ps_consts_f(This->wined3d_device, Register, pConstantData, ConstantCount);
2578 wined3d_mutex_unlock();
2580 return hr;
2583 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2584 DWORD pPixelShader, void *pData, DWORD *pSizeOfData)
2586 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2587 struct d3d8_pixel_shader *shader = NULL;
2588 HRESULT hr;
2590 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2591 iface, pPixelShader, pData, pSizeOfData);
2593 wined3d_mutex_lock();
2594 shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2595 if (!shader)
2597 WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2598 wined3d_mutex_unlock();
2600 return D3DERR_INVALIDCALL;
2603 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, pData, pSizeOfData);
2604 wined3d_mutex_unlock();
2606 return hr;
2609 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(IDirect3DDevice8 *iface, UINT Handle,
2610 const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2612 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2613 HRESULT hr;
2615 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2616 iface, Handle, pNumSegs, pRectPatchInfo);
2618 wined3d_mutex_lock();
2619 hr = wined3d_device_draw_rect_patch(This->wined3d_device, Handle,
2620 pNumSegs, (const struct wined3d_rect_patch_info *)pRectPatchInfo);
2621 wined3d_mutex_unlock();
2623 return hr;
2626 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(IDirect3DDevice8 *iface, UINT Handle,
2627 const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2629 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2630 HRESULT hr;
2632 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2633 iface, Handle, pNumSegs, pTriPatchInfo);
2635 wined3d_mutex_lock();
2636 hr = wined3d_device_draw_tri_patch(This->wined3d_device, Handle,
2637 pNumSegs, (const struct wined3d_tri_patch_info *)pTriPatchInfo);
2638 wined3d_mutex_unlock();
2640 return hr;
2643 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(IDirect3DDevice8 *iface, UINT Handle)
2645 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2646 HRESULT hr;
2648 TRACE("iface %p, handle %#x.\n", iface, Handle);
2650 wined3d_mutex_lock();
2651 hr = wined3d_device_delete_patch(This->wined3d_device, Handle);
2652 wined3d_mutex_unlock();
2654 return hr;
2657 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(IDirect3DDevice8 *iface,
2658 UINT StreamNumber, IDirect3DVertexBuffer8 *pStreamData, UINT Stride)
2660 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2661 IDirect3DVertexBuffer8Impl *streamdata = unsafe_impl_from_IDirect3DVertexBuffer8(pStreamData);
2662 HRESULT hr;
2664 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2665 iface, StreamNumber, pStreamData, Stride);
2667 wined3d_mutex_lock();
2668 hr = wined3d_device_set_stream_source(This->wined3d_device, StreamNumber,
2669 streamdata ? streamdata->wineD3DVertexBuffer : NULL, 0/* Offset in bytes */, Stride);
2670 wined3d_mutex_unlock();
2672 return hr;
2675 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(IDirect3DDevice8 *iface,
2676 UINT StreamNumber, IDirect3DVertexBuffer8 **pStream, UINT *pStride)
2678 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2679 struct wined3d_buffer *retStream = NULL;
2680 HRESULT hr;
2682 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2683 iface, StreamNumber, pStream, pStride);
2685 if(pStream == NULL){
2686 return D3DERR_INVALIDCALL;
2689 wined3d_mutex_lock();
2690 hr = wined3d_device_get_stream_source(This->wined3d_device, StreamNumber,
2691 &retStream, 0 /* Offset in bytes */, pStride);
2692 if (SUCCEEDED(hr) && retStream)
2694 *pStream = wined3d_buffer_get_parent(retStream);
2695 IDirect3DVertexBuffer8_AddRef(*pStream);
2696 wined3d_buffer_decref(retStream);
2698 else
2700 if (FAILED(hr)) FIXME("Call to GetStreamSource failed, hr %#x.\n", hr);
2701 *pStream = NULL;
2703 wined3d_mutex_unlock();
2705 return hr;
2708 static const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2710 IDirect3DDevice8Impl_QueryInterface,
2711 IDirect3DDevice8Impl_AddRef,
2712 IDirect3DDevice8Impl_Release,
2713 IDirect3DDevice8Impl_TestCooperativeLevel,
2714 IDirect3DDevice8Impl_GetAvailableTextureMem,
2715 IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2716 IDirect3DDevice8Impl_GetDirect3D,
2717 IDirect3DDevice8Impl_GetDeviceCaps,
2718 IDirect3DDevice8Impl_GetDisplayMode,
2719 IDirect3DDevice8Impl_GetCreationParameters,
2720 IDirect3DDevice8Impl_SetCursorProperties,
2721 IDirect3DDevice8Impl_SetCursorPosition,
2722 IDirect3DDevice8Impl_ShowCursor,
2723 IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2724 IDirect3DDevice8Impl_Reset,
2725 IDirect3DDevice8Impl_Present,
2726 IDirect3DDevice8Impl_GetBackBuffer,
2727 IDirect3DDevice8Impl_GetRasterStatus,
2728 IDirect3DDevice8Impl_SetGammaRamp,
2729 IDirect3DDevice8Impl_GetGammaRamp,
2730 IDirect3DDevice8Impl_CreateTexture,
2731 IDirect3DDevice8Impl_CreateVolumeTexture,
2732 IDirect3DDevice8Impl_CreateCubeTexture,
2733 IDirect3DDevice8Impl_CreateVertexBuffer,
2734 IDirect3DDevice8Impl_CreateIndexBuffer,
2735 IDirect3DDevice8Impl_CreateRenderTarget,
2736 IDirect3DDevice8Impl_CreateDepthStencilSurface,
2737 IDirect3DDevice8Impl_CreateImageSurface,
2738 IDirect3DDevice8Impl_CopyRects,
2739 IDirect3DDevice8Impl_UpdateTexture,
2740 IDirect3DDevice8Impl_GetFrontBuffer,
2741 IDirect3DDevice8Impl_SetRenderTarget,
2742 IDirect3DDevice8Impl_GetRenderTarget,
2743 IDirect3DDevice8Impl_GetDepthStencilSurface,
2744 IDirect3DDevice8Impl_BeginScene,
2745 IDirect3DDevice8Impl_EndScene,
2746 IDirect3DDevice8Impl_Clear,
2747 IDirect3DDevice8Impl_SetTransform,
2748 IDirect3DDevice8Impl_GetTransform,
2749 IDirect3DDevice8Impl_MultiplyTransform,
2750 IDirect3DDevice8Impl_SetViewport,
2751 IDirect3DDevice8Impl_GetViewport,
2752 IDirect3DDevice8Impl_SetMaterial,
2753 IDirect3DDevice8Impl_GetMaterial,
2754 IDirect3DDevice8Impl_SetLight,
2755 IDirect3DDevice8Impl_GetLight,
2756 IDirect3DDevice8Impl_LightEnable,
2757 IDirect3DDevice8Impl_GetLightEnable,
2758 IDirect3DDevice8Impl_SetClipPlane,
2759 IDirect3DDevice8Impl_GetClipPlane,
2760 IDirect3DDevice8Impl_SetRenderState,
2761 IDirect3DDevice8Impl_GetRenderState,
2762 IDirect3DDevice8Impl_BeginStateBlock,
2763 IDirect3DDevice8Impl_EndStateBlock,
2764 IDirect3DDevice8Impl_ApplyStateBlock,
2765 IDirect3DDevice8Impl_CaptureStateBlock,
2766 IDirect3DDevice8Impl_DeleteStateBlock,
2767 IDirect3DDevice8Impl_CreateStateBlock,
2768 IDirect3DDevice8Impl_SetClipStatus,
2769 IDirect3DDevice8Impl_GetClipStatus,
2770 IDirect3DDevice8Impl_GetTexture,
2771 IDirect3DDevice8Impl_SetTexture,
2772 IDirect3DDevice8Impl_GetTextureStageState,
2773 IDirect3DDevice8Impl_SetTextureStageState,
2774 IDirect3DDevice8Impl_ValidateDevice,
2775 IDirect3DDevice8Impl_GetInfo,
2776 IDirect3DDevice8Impl_SetPaletteEntries,
2777 IDirect3DDevice8Impl_GetPaletteEntries,
2778 IDirect3DDevice8Impl_SetCurrentTexturePalette,
2779 IDirect3DDevice8Impl_GetCurrentTexturePalette,
2780 IDirect3DDevice8Impl_DrawPrimitive,
2781 IDirect3DDevice8Impl_DrawIndexedPrimitive,
2782 IDirect3DDevice8Impl_DrawPrimitiveUP,
2783 IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2784 IDirect3DDevice8Impl_ProcessVertices,
2785 IDirect3DDevice8Impl_CreateVertexShader,
2786 IDirect3DDevice8Impl_SetVertexShader,
2787 IDirect3DDevice8Impl_GetVertexShader,
2788 IDirect3DDevice8Impl_DeleteVertexShader,
2789 IDirect3DDevice8Impl_SetVertexShaderConstant,
2790 IDirect3DDevice8Impl_GetVertexShaderConstant,
2791 IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2792 IDirect3DDevice8Impl_GetVertexShaderFunction,
2793 IDirect3DDevice8Impl_SetStreamSource,
2794 IDirect3DDevice8Impl_GetStreamSource,
2795 IDirect3DDevice8Impl_SetIndices,
2796 IDirect3DDevice8Impl_GetIndices,
2797 IDirect3DDevice8Impl_CreatePixelShader,
2798 IDirect3DDevice8Impl_SetPixelShader,
2799 IDirect3DDevice8Impl_GetPixelShader,
2800 IDirect3DDevice8Impl_DeletePixelShader,
2801 IDirect3DDevice8Impl_SetPixelShaderConstant,
2802 IDirect3DDevice8Impl_GetPixelShaderConstant,
2803 IDirect3DDevice8Impl_GetPixelShaderFunction,
2804 IDirect3DDevice8Impl_DrawRectPatch,
2805 IDirect3DDevice8Impl_DrawTriPatch,
2806 IDirect3DDevice8Impl_DeletePatch
2809 static inline IDirect3DDevice8Impl *device_from_device_parent(struct wined3d_device_parent *device_parent)
2811 return CONTAINING_RECORD(device_parent, IDirect3DDevice8Impl, device_parent);
2814 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2815 struct wined3d_device *device)
2817 TRACE("device_parent %p, device %p\n", device_parent, device);
2820 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2822 TRACE("device_parent %p.\n", device_parent);
2825 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
2826 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2827 enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
2829 IDirect3DDevice8Impl *device = device_from_device_parent(device_parent);
2830 IDirect3DSurface8Impl *d3d_surface;
2831 BOOL lockable = TRUE;
2832 HRESULT hr;
2834 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
2835 "\tpool %#x, level %u, face %u, surface %p.\n",
2836 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
2839 if (pool == WINED3D_POOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC))
2840 lockable = FALSE;
2842 hr = IDirect3DDevice8Impl_CreateSurface(device, width, height,
2843 d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2844 (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2845 if (FAILED(hr))
2847 WARN("Failed to create surface, hr %#x.\n", hr);
2848 return hr;
2851 *surface = d3d_surface->wined3d_surface;
2852 wined3d_surface_incref(*surface);
2854 d3d_surface->container = container_parent;
2855 IUnknown_Release(d3d_surface->parentDevice);
2856 d3d_surface->parentDevice = NULL;
2858 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2859 d3d_surface->forwardReference = container_parent;
2861 return hr;
2864 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
2865 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
2866 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, BOOL lockable,
2867 struct wined3d_surface **surface)
2869 IDirect3DDevice8Impl *device = device_from_device_parent(device_parent);
2870 IDirect3DSurface8Impl *d3d_surface;
2871 HRESULT hr;
2873 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2874 "\tmultisample_quality %u, lockable %u, surface %p.\n",
2875 device_parent, container_parent, width, height, format,
2876 multisample_type, multisample_quality, lockable, surface);
2878 hr = IDirect3DDevice8_CreateRenderTarget(&device->IDirect3DDevice8_iface, width, height,
2879 d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2880 if (FAILED(hr))
2882 WARN("Failed to create rendertarget, hr %#x.\n", hr);
2883 return hr;
2886 *surface = d3d_surface->wined3d_surface;
2887 wined3d_surface_incref(*surface);
2889 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice8_iface;
2890 /* Implicit surfaces are created with an refcount of 0 */
2891 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2893 return hr;
2896 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
2897 UINT width, UINT height, enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
2898 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
2900 IDirect3DDevice8Impl *device = device_from_device_parent(device_parent);
2901 IDirect3DSurface8Impl *d3d_surface;
2902 HRESULT hr;
2904 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2905 "\tmultisample_quality %u, discard %u, surface %p.\n",
2906 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
2908 hr = IDirect3DDevice8_CreateDepthStencilSurface(&device->IDirect3DDevice8_iface, width, height,
2909 d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2910 if (FAILED(hr))
2912 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
2913 return hr;
2916 *surface = d3d_surface->wined3d_surface;
2917 wined3d_surface_incref(*surface);
2919 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice8_iface;
2920 /* Implicit surfaces are created with an refcount of 0 */
2921 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2923 return hr;
2926 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
2927 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2928 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
2930 IDirect3DDevice8Impl *device = device_from_device_parent(device_parent);
2931 IDirect3DVolume8Impl *object;
2932 HRESULT hr;
2934 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
2935 "format %#x, pool %#x, usage %#x, volume %p.\n",
2936 device_parent, container_parent, width, height, depth,
2937 format, pool, usage, volume);
2939 /* Allocate the storage for the device */
2940 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2941 if (!object)
2943 FIXME("Allocation of memory failed\n");
2944 *volume = NULL;
2945 return D3DERR_OUTOFVIDEOMEMORY;
2948 hr = volume_init(object, device, width, height, depth, usage, format, pool);
2949 if (FAILED(hr))
2951 WARN("Failed to initialize volume, hr %#x.\n", hr);
2952 HeapFree(GetProcessHeap(), 0, object);
2953 return hr;
2956 *volume = object->wined3d_volume;
2957 wined3d_volume_incref(*volume);
2958 IDirect3DVolume8_Release(&object->IDirect3DVolume8_iface);
2960 object->container = container_parent;
2961 object->forwardReference = container_parent;
2963 TRACE("Created volume %p.\n", object);
2965 return hr;
2968 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
2969 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
2971 IDirect3DDevice8Impl *device = device_from_device_parent(device_parent);
2972 D3DPRESENT_PARAMETERS local_parameters;
2973 IDirect3DSwapChain8 *d3d_swapchain;
2974 HRESULT hr;
2976 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
2978 /* Copy the presentation parameters */
2979 local_parameters.BackBufferWidth = desc->backbuffer_width;
2980 local_parameters.BackBufferHeight = desc->backbuffer_height;
2981 local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(desc->backbuffer_format);
2982 local_parameters.BackBufferCount = desc->backbuffer_count;
2983 local_parameters.MultiSampleType = desc->multisample_type;
2984 local_parameters.SwapEffect = desc->swap_effect;
2985 local_parameters.hDeviceWindow = desc->device_window;
2986 local_parameters.Windowed = desc->windowed;
2987 local_parameters.EnableAutoDepthStencil = desc->enable_auto_depth_stencil;
2988 local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc->auto_depth_stencil_format);
2989 local_parameters.Flags = desc->flags;
2990 local_parameters.FullScreen_RefreshRateInHz = desc->refresh_rate;
2991 local_parameters.FullScreen_PresentationInterval = desc->swap_interval;
2993 hr = IDirect3DDevice8_CreateAdditionalSwapChain(&device->IDirect3DDevice8_iface,
2994 &local_parameters, &d3d_swapchain);
2995 if (FAILED(hr))
2997 WARN("Failed to create swapchain, hr %#x.\n", hr);
2998 *swapchain = NULL;
2999 return hr;
3002 *swapchain = ((IDirect3DSwapChain8Impl *)d3d_swapchain)->wined3d_swapchain;
3003 wined3d_swapchain_incref(*swapchain);
3004 IDirect3DSwapChain8_Release(d3d_swapchain);
3006 /* Copy back the presentation parameters */
3007 desc->backbuffer_width = local_parameters.BackBufferWidth;
3008 desc->backbuffer_height = local_parameters.BackBufferHeight;
3009 desc->backbuffer_format = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3010 desc->backbuffer_count = local_parameters.BackBufferCount;
3011 desc->multisample_type = local_parameters.MultiSampleType;
3012 desc->swap_effect = local_parameters.SwapEffect;
3013 desc->device_window = local_parameters.hDeviceWindow;
3014 desc->windowed = local_parameters.Windowed;
3015 desc->enable_auto_depth_stencil = local_parameters.EnableAutoDepthStencil;
3016 desc->auto_depth_stencil_format = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3017 desc->flags = local_parameters.Flags;
3018 desc->refresh_rate = local_parameters.FullScreen_RefreshRateInHz;
3019 desc->swap_interval = local_parameters.FullScreen_PresentationInterval;
3021 return hr;
3024 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3026 device_parent_wined3d_device_created,
3027 device_parent_mode_changed,
3028 device_parent_create_surface,
3029 device_parent_create_rendertarget,
3030 device_parent_create_depth_stencil,
3031 device_parent_create_volume,
3032 device_parent_create_swapchain,
3035 static void setup_fpu(void)
3037 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3038 WORD cw;
3039 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3040 cw = (cw & ~0xf3f) | 0x3f;
3041 __asm__ volatile ("fldcw %0" : : "m" (cw));
3042 #elif defined(__i386__) && defined(_MSC_VER)
3043 WORD cw;
3044 __asm fnstcw cw;
3045 cw = (cw & ~0xf3f) | 0x3f;
3046 __asm fldcw cw;
3047 #else
3048 FIXME("FPU setup not implemented for this platform.\n");
3049 #endif
3052 HRESULT device_init(IDirect3DDevice8Impl *device, IDirect3D8Impl *parent, struct wined3d *wined3d, UINT adapter,
3053 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3055 struct wined3d_swapchain_desc swapchain_desc;
3056 HRESULT hr;
3058 device->IDirect3DDevice8_iface.lpVtbl = &Direct3DDevice8_Vtbl;
3059 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3060 device->ref = 1;
3061 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3062 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3063 if (!device->handle_table.entries)
3065 ERR("Failed to allocate handle table memory.\n");
3066 return E_OUTOFMEMORY;
3068 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3070 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3072 wined3d_mutex_lock();
3073 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3074 &device->device_parent, &device->wined3d_device);
3075 if (FAILED(hr))
3077 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3078 wined3d_mutex_unlock();
3079 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3080 return hr;
3083 if (!parameters->Windowed)
3085 HWND device_window = parameters->hDeviceWindow;
3087 if (!focus_window)
3088 focus_window = device_window;
3089 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3091 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3092 wined3d_device_decref(device->wined3d_device);
3093 wined3d_mutex_unlock();
3094 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3095 return hr;
3098 if (!device_window)
3099 device_window = focus_window;
3100 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3101 parameters->BackBufferWidth,
3102 parameters->BackBufferHeight);
3105 if (flags & D3DCREATE_MULTITHREADED)
3106 wined3d_device_set_multithreaded(device->wined3d_device);
3108 swapchain_desc.backbuffer_width = parameters->BackBufferWidth;
3109 swapchain_desc.backbuffer_height = parameters->BackBufferHeight;
3110 swapchain_desc.backbuffer_format = wined3dformat_from_d3dformat(parameters->BackBufferFormat);
3111 swapchain_desc.backbuffer_count = parameters->BackBufferCount;
3112 swapchain_desc.multisample_type = parameters->MultiSampleType;
3113 swapchain_desc.multisample_quality = 0; /* d3d9 only */
3114 swapchain_desc.swap_effect = parameters->SwapEffect;
3115 swapchain_desc.device_window = parameters->hDeviceWindow;
3116 swapchain_desc.windowed = parameters->Windowed;
3117 swapchain_desc.enable_auto_depth_stencil = parameters->EnableAutoDepthStencil;
3118 swapchain_desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(parameters->AutoDepthStencilFormat);
3119 swapchain_desc.flags = parameters->Flags;
3120 swapchain_desc.refresh_rate = parameters->FullScreen_RefreshRateInHz;
3121 swapchain_desc.swap_interval = parameters->FullScreen_PresentationInterval;
3122 swapchain_desc.auto_restore_display_mode = TRUE;
3124 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3125 if (FAILED(hr))
3127 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3128 wined3d_device_release_focus_window(device->wined3d_device);
3129 wined3d_device_decref(device->wined3d_device);
3130 wined3d_mutex_unlock();
3131 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3132 return hr;
3135 hr = wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3136 wined3d_mutex_unlock();
3137 if (FAILED(hr))
3139 ERR("Failed to set minimum pointsize, hr %#x.\n", hr);
3140 goto err;
3143 parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
3144 parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
3145 parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
3146 parameters->BackBufferCount = swapchain_desc.backbuffer_count;
3147 parameters->MultiSampleType = swapchain_desc.multisample_type;
3148 parameters->SwapEffect = swapchain_desc.swap_effect;
3149 parameters->hDeviceWindow = swapchain_desc.device_window;
3150 parameters->Windowed = swapchain_desc.windowed;
3151 parameters->EnableAutoDepthStencil = swapchain_desc.enable_auto_depth_stencil;
3152 parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(swapchain_desc.auto_depth_stencil_format);
3153 parameters->Flags = swapchain_desc.flags;
3154 parameters->FullScreen_RefreshRateInHz = swapchain_desc.refresh_rate;
3155 parameters->FullScreen_PresentationInterval = swapchain_desc.swap_interval;
3157 device->declArraySize = 16;
3158 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3159 if (!device->decls)
3161 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3162 hr = E_OUTOFMEMORY;
3163 goto err;
3166 device->d3d_parent = &parent->IDirect3D8_iface;
3167 IDirect3D8_AddRef(device->d3d_parent);
3169 return D3D_OK;
3171 err:
3172 wined3d_mutex_lock();
3173 wined3d_device_uninit_3d(device->wined3d_device);
3174 wined3d_device_release_focus_window(device->wined3d_device);
3175 wined3d_device_decref(device->wined3d_device);
3176 wined3d_mutex_unlock();
3177 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3178 return hr;