push c6fcfc519a04d046be60ec60e33d075a2146cc03
[wine/hacks.git] / dlls / d3d8 / directx.c
blob72ccb3dd9d98893457398a3ae3712f836afdc522
1 /*
2 * IDirect3D8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "d3d8_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
40 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
43 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
45 if (IsEqualGUID(riid, &IID_IUnknown)
46 || IsEqualGUID(riid, &IID_IDirect3D8)) {
47 IUnknown_AddRef(iface);
48 *ppobj = This;
49 return S_OK;
52 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
53 *ppobj = NULL;
54 return E_NOINTERFACE;
57 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
58 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
59 ULONG ref = InterlockedIncrement(&This->ref);
61 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
63 return ref;
66 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
67 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
68 ULONG ref = InterlockedDecrement(&This->ref);
70 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
72 if (ref == 0) {
73 TRACE("Releasing wined3d %p\n", This->WineD3D);
74 IWineD3D_Release(This->WineD3D);
75 HeapFree(GetProcessHeap(), 0, This);
78 return ref;
81 /* IDirect3D8 Interface follow: */
82 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
83 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
84 HRESULT hr;
85 TRACE("(%p)->(%p)\n", This, pInitializeFunction);
87 EnterCriticalSection(&d3d8_cs);
88 hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
89 LeaveCriticalSection(&d3d8_cs);
90 return hr;
93 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
94 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
95 HRESULT hr;
96 TRACE("(%p)\n", This);
98 EnterCriticalSection(&d3d8_cs);
99 hr = IWineD3D_GetAdapterCount(This->WineD3D);
100 LeaveCriticalSection(&d3d8_cs);
101 return hr;
104 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier (LPDIRECT3D8 iface,
105 UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
106 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
107 WINED3DADAPTER_IDENTIFIER adapter_id;
108 HRESULT hr;
110 TRACE("(%p)->(%d,%08x, %p\n", This, Adapter, Flags, pIdentifier);
111 EnterCriticalSection(&d3d8_cs);
112 /* dx8 and dx9 have different structures to be filled in, with incompatible
113 layouts so pass in pointers to the places to be filled via an internal
114 structure */
115 adapter_id.Driver = pIdentifier->Driver;
116 adapter_id.Description = pIdentifier->Description;
117 adapter_id.DeviceName = NULL; /* d3d9 only */
118 adapter_id.DriverVersion = &pIdentifier->DriverVersion;
119 adapter_id.VendorId = &pIdentifier->VendorId;
120 adapter_id.DeviceId = &pIdentifier->DeviceId;
121 adapter_id.SubSysId = &pIdentifier->SubSysId;
122 adapter_id.Revision = &pIdentifier->Revision;
123 adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
124 adapter_id.WHQLLevel = &pIdentifier->WHQLLevel;
126 hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
127 LeaveCriticalSection(&d3d8_cs);
128 return hr;
131 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
132 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
133 HRESULT hr;
134 TRACE("(%p)->(%d)\n", This, Adapter);
136 EnterCriticalSection(&d3d8_cs);
137 hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
138 LeaveCriticalSection(&d3d8_cs);
139 return hr;
142 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
143 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
144 HRESULT hr;
145 TRACE("(%p)->(%d, %d, %p)\n", This, Adapter, Mode, pMode);
147 EnterCriticalSection(&d3d8_cs);
148 hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
149 LeaveCriticalSection(&d3d8_cs);
150 return hr;
153 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
154 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
155 HRESULT hr;
156 TRACE("(%p)->(%d,%p)\n", This, Adapter, pMode);
158 EnterCriticalSection(&d3d8_cs);
159 hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
160 LeaveCriticalSection(&d3d8_cs);
161 return hr;
164 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface,
165 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
166 D3DFORMAT BackBufferFormat, BOOL Windowed) {
167 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
168 HRESULT hr;
169 TRACE("(%p)->(%d, %d, %d, %d, %s)\n", This, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed ? "true" : "false");
171 EnterCriticalSection(&d3d8_cs);
172 hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
173 BackBufferFormat, Windowed);
174 LeaveCriticalSection(&d3d8_cs);
175 return hr;
178 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface,
179 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
180 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
181 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
182 HRESULT hr;
183 TRACE("(%p)->(%d, %d, %d, %08x, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
185 EnterCriticalSection(&d3d8_cs);
186 hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
187 Usage, RType, CheckFormat, SURFACE_OPENGL);
188 LeaveCriticalSection(&d3d8_cs);
189 return hr;
192 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
193 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
194 BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
195 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
196 HRESULT hr;
197 TRACE("(%p)-<(%d, %d, %d, %s, %d)\n", This, Adapter, DeviceType, SurfaceFormat, Windowed ? "true" : "false", MultiSampleType);
199 EnterCriticalSection(&d3d8_cs);
200 hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
201 Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
202 LeaveCriticalSection(&d3d8_cs);
203 return hr;
206 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface,
207 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
208 D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
209 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
210 HRESULT hr;
211 TRACE("(%p)-<(%d, %d, %d, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
213 EnterCriticalSection(&d3d8_cs);
214 hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
215 RenderTargetFormat, DepthStencilFormat);
216 LeaveCriticalSection(&d3d8_cs);
217 return hr;
220 static HRESULT WINAPI IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
221 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
222 HRESULT hrc = D3D_OK;
223 WINED3DCAPS *pWineCaps;
225 TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
227 if(NULL == pCaps){
228 return D3DERR_INVALIDCALL;
230 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
231 if(pWineCaps == NULL){
232 return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
234 EnterCriticalSection(&d3d8_cs);
235 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
236 LeaveCriticalSection(&d3d8_cs);
237 WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
238 HeapFree(GetProcessHeap(), 0, pWineCaps);
240 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
241 if(pCaps->PixelShaderVersion > D3DPS_VERSION(1,4)){
242 pCaps->PixelShaderVersion = D3DPS_VERSION(1,4);
244 if(pCaps->VertexShaderVersion > D3DVS_VERSION(1,1)){
245 pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
248 TRACE("(%p) returning %p\n", This, pCaps);
249 return hrc;
252 static HMONITOR WINAPI IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
253 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
254 HMONITOR ret;
255 TRACE("(%p)->(%d)\n", This, Adapter);
257 EnterCriticalSection(&d3d8_cs);
258 ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
259 LeaveCriticalSection(&d3d8_cs);
260 return ret;
263 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
264 IDirect3DSurface8Impl* surfaceParent;
265 TRACE("(%p) call back\n", pSurface);
267 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
268 surfaceParent->isImplicit = FALSE;
269 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
270 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
273 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
274 IUnknown* swapChainParent;
275 TRACE("(%p) call back\n", pSwapChain);
277 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
278 IUnknown_Release(swapChainParent);
279 return IUnknown_Release(swapChainParent);
282 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
283 IDirect3DSurface8Impl* surfaceParent;
284 TRACE("(%p) call back\n", pSurface);
286 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
287 surfaceParent->isImplicit = FALSE;
288 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
289 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
292 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
293 DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
294 IDirect3DDevice8** ppReturnedDeviceInterface) {
296 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
297 IDirect3DDevice8Impl *object = NULL;
298 WINED3DPRESENT_PARAMETERS localParameters;
299 HRESULT hr;
300 TRACE("(%p) Relay\n", This);
302 /* Check the validity range of the adapter parameter */
303 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
304 *ppReturnedDeviceInterface = NULL;
305 return D3DERR_INVALIDCALL;
308 /* Allocate the storage for the device object */
309 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
310 if (NULL == object) {
311 FIXME("Allocation of memory failed\n");
312 *ppReturnedDeviceInterface = NULL;
313 return D3DERR_OUTOFVIDEOMEMORY;
316 object->lpVtbl = &Direct3DDevice8_Vtbl;
317 object->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
318 object->ref = 1;
319 object->shader_handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, INITIAL_SHADER_HANDLE_TABLE_SIZE * sizeof(shader_handle));
320 object->shader_handle_table_size = INITIAL_SHADER_HANDLE_TABLE_SIZE;
321 *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
323 /* Allocate an associated WineD3DDevice object */
324 EnterCriticalSection(&d3d8_cs);
325 hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
326 (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
328 if (hr != D3D_OK) {
329 HeapFree(GetProcessHeap(), 0, object);
330 *ppReturnedDeviceInterface = NULL;
331 LeaveCriticalSection(&d3d8_cs);
332 return hr;
335 TRACE("(%p) : Created Device %p\n", This, object);
337 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
338 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
339 localParameters.BackBufferFormat = pPresentationParameters->BackBufferFormat;
340 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
341 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
342 localParameters.MultiSampleQuality = 0; /* d3d9 only */
343 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
344 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
345 localParameters.Windowed = pPresentationParameters->Windowed;
346 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
347 localParameters.AutoDepthStencilFormat = pPresentationParameters->AutoDepthStencilFormat;
348 localParameters.Flags = pPresentationParameters->Flags;
349 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
350 localParameters.PresentationInterval = pPresentationParameters->FullScreen_PresentationInterval;
351 localParameters.AutoRestoreDisplayMode = TRUE;
353 if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
354 IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
357 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
358 LeaveCriticalSection(&d3d8_cs);
360 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
361 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
362 pPresentationParameters->BackBufferFormat = localParameters.BackBufferFormat;
363 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
364 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
365 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
366 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
367 pPresentationParameters->Windowed = localParameters.Windowed;
368 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
369 pPresentationParameters->AutoDepthStencilFormat = localParameters.AutoDepthStencilFormat;
370 pPresentationParameters->Flags = localParameters.Flags;
371 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
372 pPresentationParameters->FullScreen_PresentationInterval = localParameters.PresentationInterval;
374 if (hr != D3D_OK) {
375 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
376 HeapFree(GetProcessHeap(), 0, object);
377 *ppReturnedDeviceInterface = NULL;
380 object->declArraySize = 16;
381 object->decls = HeapAlloc(GetProcessHeap(), 0, object->declArraySize * sizeof(*object->decls));
382 if(!object->decls) {
383 ERR("Out of memory\n");
384 IWineD3DDevice_Release(object->WineD3DDevice);
385 HeapFree(GetProcessHeap(), 0, object);
386 *ppReturnedDeviceInterface = NULL;
387 hr = E_OUTOFMEMORY;
389 return hr;
392 const IDirect3D8Vtbl Direct3D8_Vtbl =
394 /* IUnknown */
395 IDirect3D8Impl_QueryInterface,
396 IDirect3D8Impl_AddRef,
397 IDirect3D8Impl_Release,
398 /* IDirect3D8 */
399 IDirect3D8Impl_RegisterSoftwareDevice,
400 IDirect3D8Impl_GetAdapterCount,
401 IDirect3D8Impl_GetAdapterIdentifier,
402 IDirect3D8Impl_GetAdapterModeCount,
403 IDirect3D8Impl_EnumAdapterModes,
404 IDirect3D8Impl_GetAdapterDisplayMode,
405 IDirect3D8Impl_CheckDeviceType,
406 IDirect3D8Impl_CheckDeviceFormat,
407 IDirect3D8Impl_CheckDeviceMultiSampleType,
408 IDirect3D8Impl_CheckDepthStencilMatch,
409 IDirect3D8Impl_GetDeviceCaps,
410 IDirect3D8Impl_GetAdapterMonitor,
411 IDirect3D8Impl_CreateDevice