push a6cedc1a067cfdfb1ba44a8f4b98ea78ae3fe78f
[wine/hacks.git] / dlls / ddraw / texture.c
blob5579a6a9887f14a6fcae0f68e9adf2b3e73d334f
1 /* Direct3D Texture
2 * Copyright (c) 1998 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This file contains the implementation of interface Direct3DTexture2.
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"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #define COBJMACROS
31 #define NONAMELESSUNION
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
39 #include "ddraw.h"
40 #include "d3d.h"
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
48 /*****************************************************************************
49 * IUnknown interfaces. They are thunks to IDirectDrawSurface7
50 *****************************************************************************/
51 static HRESULT WINAPI
52 Thunk_IDirect3DTextureImpl_2_QueryInterface(IDirect3DTexture2 *iface,
53 REFIID riid,
54 void **obj)
56 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
57 TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", This, debugstr_guid(riid), obj);
58 return IDirectDrawSurface7_QueryInterface((IDirectDrawSurface7 *)This, riid, obj);
61 static HRESULT WINAPI
62 Thunk_IDirect3DTextureImpl_1_QueryInterface(IDirect3DTexture *iface,
63 REFIID riid,
64 void **obj)
66 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
67 TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", This, debugstr_guid(riid), obj);
69 return IDirectDrawSurface7_QueryInterface((IDirectDrawSurface7 *)This, riid, obj);
72 static ULONG WINAPI
73 Thunk_IDirect3DTextureImpl_2_AddRef(IDirect3DTexture2 *iface)
75 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
76 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
78 return IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)This);
81 static ULONG WINAPI
82 Thunk_IDirect3DTextureImpl_1_AddRef(IDirect3DTexture *iface)
84 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
85 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
87 return IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)This);
90 static ULONG WINAPI
91 Thunk_IDirect3DTextureImpl_2_Release(IDirect3DTexture2 *iface)
93 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
94 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
96 return IDirectDrawSurface7_Release((IDirectDrawSurface7 *)This);
100 static ULONG WINAPI
101 Thunk_IDirect3DTextureImpl_1_Release(IDirect3DTexture *iface)
103 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
104 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
106 return IDirectDrawSurface7_Release((IDirectDrawSurface7 *)This);
109 /*****************************************************************************
110 * IDirect3DTexture interface
111 *****************************************************************************/
113 /*****************************************************************************
114 * IDirect3DTexture1::Initialize
116 * The sdk says it's not implemented
118 * Params:
121 * Returns
122 * DDERR_UNSUPPORTED
124 *****************************************************************************/
125 static HRESULT WINAPI
126 IDirect3DTextureImpl_1_Initialize(IDirect3DTexture *iface,
127 IDirect3DDevice *Direct3DDevice,
128 IDirectDrawSurface *DDSurface)
130 TRACE("(%p)->(%p,%p) Not implemented\n", iface, Direct3DDevice, DDSurface);
131 return DDERR_UNSUPPORTED; /* Unchecked */
134 /*****************************************************************************
135 * IDirect3DTexture2::PaletteChanged
137 * Informs the texture about a palette change
139 * Params:
140 * Start: Start index of the change
141 * Count: The number of changed entries
143 * Returns
144 * D3D_OK, because it's a stub
146 *****************************************************************************/
147 static HRESULT WINAPI
148 IDirect3DTextureImpl_PaletteChanged(IDirect3DTexture2 *iface,
149 DWORD Start,
150 DWORD Count)
152 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
153 FIXME("(%p)->(%08x,%08x): stub!\n", This, Start, Count);
154 return D3D_OK;
157 static HRESULT WINAPI
158 Thunk_IDirect3DTextureImpl_1_PaletteChanged(IDirect3DTexture *iface,
159 DWORD Start,
160 DWORD Count)
162 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
163 TRACE("(%p)->(%08x,%08x) thunking to IDirect3DTexture2 interface.\n", This, Start, Count);
165 return IDirect3DTexture2_PaletteChanged((IDirect3DTexture2 *)&This->IDirect3DTexture2_vtbl, Start, Count);
169 /*****************************************************************************
170 * IDirect3DTexture::Unload
172 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
175 * Returns:
176 * DDERR_UNSUPPORTED
178 *****************************************************************************/
179 static HRESULT WINAPI
180 IDirect3DTextureImpl_1_Unload(IDirect3DTexture *iface)
182 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
183 TRACE("(%p)->(): not implemented!\n", This);
184 return DDERR_UNSUPPORTED;
187 /*****************************************************************************
188 * IDirect3DTexture2::GetHandle
190 * Returns handle for the texture. At the moment, the interface
191 * to the IWineD3DTexture is used.
193 * Params:
194 * Direct3DDevice2: Device this handle is assigned to
195 * Handle: Address to store the handle at.
197 * Returns:
198 * D3D_OK
200 *****************************************************************************/
201 static HRESULT WINAPI
202 IDirect3DTextureImpl_GetHandle(IDirect3DTexture2 *iface,
203 IDirect3DDevice2 *Direct3DDevice2,
204 D3DTEXTUREHANDLE *lpHandle)
206 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
207 IDirect3DDeviceImpl *d3d = device_from_device2(Direct3DDevice2);
209 TRACE("(%p)->(%p,%p)\n", This, d3d, lpHandle);
211 EnterCriticalSection(&ddraw_cs);
212 if(!This->Handle)
214 This->Handle = IDirect3DDeviceImpl_CreateHandle(d3d);
215 if(This->Handle)
217 d3d->Handles[This->Handle - 1].ptr = This;
218 d3d->Handles[This->Handle - 1].type = DDrawHandle_Texture;
221 *lpHandle = This->Handle;
223 TRACE(" returning handle %08x.\n", *lpHandle);
225 LeaveCriticalSection(&ddraw_cs);
226 return D3D_OK;
229 static HRESULT WINAPI
230 Thunk_IDirect3DTextureImpl_1_GetHandle(IDirect3DTexture *iface,
231 LPDIRECT3DDEVICE lpDirect3DDevice,
232 LPD3DTEXTUREHANDLE lpHandle)
234 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
235 IDirect3DDeviceImpl *d3d = device_from_device1(lpDirect3DDevice);
236 IDirect3DTexture2 *d3d_texture2 = (IDirect3DTexture2 *)&This->IDirect3DTexture2_vtbl;
237 IDirect3DDevice2 *d3d_device2 = (IDirect3DDevice2 *)&d3d->IDirect3DDevice2_vtbl;
239 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DTexture2 interface.\n", This, d3d, lpHandle);
241 return IDirect3DTexture2_GetHandle(d3d_texture2, d3d_device2, lpHandle);
245 /*****************************************************************************
246 * get_sub_mimaplevel
248 * Helper function that returns the next mipmap level
250 * tex_ptr: Surface of which to return the next level
252 *****************************************************************************/
253 static IDirectDrawSurfaceImpl *
254 get_sub_mimaplevel(IDirectDrawSurfaceImpl *tex_ptr)
256 /* Now go down the mipmap chain to the next surface */
257 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
258 LPDIRECTDRAWSURFACE7 next_level;
259 IDirectDrawSurfaceImpl *surf_ptr;
260 HRESULT hr;
262 hr = IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)tex_ptr, &mipmap_caps, &next_level);
263 if (FAILED(hr)) return NULL;
265 surf_ptr = (IDirectDrawSurfaceImpl *)next_level;
266 IDirectDrawSurface7_Release(next_level);
268 return surf_ptr;
271 /*****************************************************************************
272 * IDirect3DTexture2::Load
274 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
276 * This function isn't relayed to WineD3D because the whole interface is
277 * implemented in DDraw only. For speed improvements a implementation which
278 * takes OpenGL more into account could be placed into WineD3D.
280 * Params:
281 * D3DTexture2: Address of the texture to load
283 * Returns:
284 * D3D_OK on success
285 * D3DERR_TEXTURE_LOAD_FAILED.
287 *****************************************************************************/
288 static HRESULT WINAPI
289 IDirect3DTextureImpl_Load(IDirect3DTexture2 *iface,
290 IDirect3DTexture2 *D3DTexture2)
292 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
293 IDirectDrawSurfaceImpl *src_ptr = surface_from_texture2(D3DTexture2);
294 HRESULT ret_value = D3D_OK;
295 if(src_ptr == This)
297 TRACE("copying surface %p to surface %p, why?\n", src_ptr, This);
298 return ret_value;
301 TRACE("(%p)->(%p)\n", This, src_ptr);
302 EnterCriticalSection(&ddraw_cs);
304 if (((src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) != (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)) ||
305 (src_ptr->surface_desc.u2.dwMipMapCount != This->surface_desc.u2.dwMipMapCount))
307 ERR("Trying to load surfaces with different mip-map counts !\n");
310 while(1)
312 IWineD3DPalette *wine_pal, *wine_pal_src;
313 IDirectDrawPalette *pal = NULL, *pal_src = NULL;
314 DDSURFACEDESC *src_d, *dst_d;
316 TRACE(" copying surface %p to surface %p (mipmap level %d)\n", src_ptr, This, src_ptr->mipmap_level);
318 /* Suppress the ALLOCONLOAD flag */
319 This->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
321 /* Get the palettes */
322 ret_value = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wine_pal);
323 if( ret_value != D3D_OK)
325 ERR("IWineD3DSurface::GetPalette failed! This is unexpected\n");
326 LeaveCriticalSection(&ddraw_cs);
327 return D3DERR_TEXTURE_LOAD_FAILED;
329 if(wine_pal)
331 ret_value = IWineD3DPalette_GetParent(wine_pal, (IUnknown **) &pal);
332 if(ret_value != D3D_OK)
334 ERR("IWineD3DPalette::GetParent failed! This is unexpected\n");
335 LeaveCriticalSection(&ddraw_cs);
336 return D3DERR_TEXTURE_LOAD_FAILED;
340 ret_value = IWineD3DSurface_GetPalette(src_ptr->WineD3DSurface, &wine_pal_src);
341 if( ret_value != D3D_OK)
343 ERR("IWineD3DSurface::GetPalette failed! This is unexpected\n");
344 LeaveCriticalSection(&ddraw_cs);
345 return D3DERR_TEXTURE_LOAD_FAILED;
347 if(wine_pal_src)
349 ret_value = IWineD3DPalette_GetParent(wine_pal_src, (IUnknown **) &pal_src);
350 if(ret_value != D3D_OK)
352 ERR("IWineD3DPalette::GetParent failed! This is unexpected\n");
353 if (pal) IDirectDrawPalette_Release(pal);
354 LeaveCriticalSection(&ddraw_cs);
355 return D3DERR_TEXTURE_LOAD_FAILED;
359 if (pal_src != NULL)
361 PALETTEENTRY palent[256];
363 if (pal == NULL)
365 IDirectDrawPalette_Release(pal_src);
366 LeaveCriticalSection(&ddraw_cs);
367 return DDERR_NOPALETTEATTACHED;
369 IDirectDrawPalette_GetEntries(pal_src, 0, 0, 256, palent);
370 IDirectDrawPalette_SetEntries(pal, 0, 0, 256, palent);
373 if (pal) IDirectDrawPalette_Release(pal);
374 if (pal_src) IDirectDrawPalette_Release(pal_src);
376 /* Copy one surface on the other */
377 dst_d = (DDSURFACEDESC *)&(This->surface_desc);
378 src_d = (DDSURFACEDESC *)&(src_ptr->surface_desc);
380 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight))
382 /* Should also check for same pixel format, u1.lPitch, ... */
383 ERR("Error in surface sizes\n");
384 LeaveCriticalSection(&ddraw_cs);
385 return D3DERR_TEXTURE_LOAD_FAILED;
387 else
389 WINED3DLOCKED_RECT pSrcRect, pDstRect;
391 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
392 /* I should put a macro for the calculus of bpp */
394 /* Copy also the ColorKeying stuff */
395 if (src_d->dwFlags & DDSD_CKSRCBLT)
397 dst_d->dwFlags |= DDSD_CKSRCBLT;
398 dst_d->ddckCKSrcBlt.dwColorSpaceLowValue = src_d->ddckCKSrcBlt.dwColorSpaceLowValue;
399 dst_d->ddckCKSrcBlt.dwColorSpaceHighValue = src_d->ddckCKSrcBlt.dwColorSpaceHighValue;
402 /* Copy the main memory texture into the surface that corresponds to the OpenGL
403 texture object. */
405 ret_value = IWineD3DSurface_LockRect(src_ptr->WineD3DSurface, &pSrcRect, NULL, 0);
406 if(ret_value != D3D_OK)
408 ERR(" (%p) Locking the source surface failed\n", This);
409 LeaveCriticalSection(&ddraw_cs);
410 return D3DERR_TEXTURE_LOAD_FAILED;
413 ret_value = IWineD3DSurface_LockRect(This->WineD3DSurface, &pDstRect, NULL, 0);
414 if(ret_value != D3D_OK)
416 ERR(" (%p) Locking the destination surface failed\n", This);
417 IWineD3DSurface_UnlockRect(src_ptr->WineD3DSurface);
418 LeaveCriticalSection(&ddraw_cs);
419 return D3DERR_TEXTURE_LOAD_FAILED;
422 if (This->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
423 memcpy(pDstRect.pBits, pSrcRect.pBits, src_ptr->surface_desc.u1.dwLinearSize);
424 else
425 memcpy(pDstRect.pBits, pSrcRect.pBits, pSrcRect.Pitch * src_d->dwHeight);
427 IWineD3DSurface_UnlockRect(src_ptr->WineD3DSurface);
428 IWineD3DSurface_UnlockRect(This->WineD3DSurface);
431 if (src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
433 src_ptr = get_sub_mimaplevel(src_ptr);
435 else
437 src_ptr = NULL;
439 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
441 This = get_sub_mimaplevel(This);
443 else
445 This = NULL;
448 if ((src_ptr == NULL) || (This == NULL))
450 if (src_ptr != This)
452 ERR(" Loading surface with different mipmap structure !!!\n");
454 break;
458 LeaveCriticalSection(&ddraw_cs);
459 return ret_value;
462 static HRESULT WINAPI
463 Thunk_IDirect3DTextureImpl_1_Load(IDirect3DTexture *iface,
464 IDirect3DTexture *D3DTexture)
466 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
467 IDirectDrawSurfaceImpl *Texture = surface_from_texture1(D3DTexture);
468 TRACE("(%p)->(%p) thunking to IDirect3DTexture2 interface.\n", This, Texture);
470 return IDirect3DTexture2_Load((IDirect3DTexture2 *)&This->IDirect3DTexture2_vtbl,
471 D3DTexture ? (IDirect3DTexture2 *)&surface_from_texture1(D3DTexture)->IDirect3DTexture2_vtbl : NULL);
474 /*****************************************************************************
475 * The VTables
476 *****************************************************************************/
477 const IDirect3DTexture2Vtbl IDirect3DTexture2_Vtbl =
479 Thunk_IDirect3DTextureImpl_2_QueryInterface,
480 Thunk_IDirect3DTextureImpl_2_AddRef,
481 Thunk_IDirect3DTextureImpl_2_Release,
482 IDirect3DTextureImpl_GetHandle,
483 IDirect3DTextureImpl_PaletteChanged,
484 IDirect3DTextureImpl_Load,
488 const IDirect3DTextureVtbl IDirect3DTexture1_Vtbl =
490 Thunk_IDirect3DTextureImpl_1_QueryInterface,
491 Thunk_IDirect3DTextureImpl_1_AddRef,
492 Thunk_IDirect3DTextureImpl_1_Release,
493 IDirect3DTextureImpl_1_Initialize,
494 Thunk_IDirect3DTextureImpl_1_GetHandle,
495 Thunk_IDirect3DTextureImpl_1_PaletteChanged,
496 Thunk_IDirect3DTextureImpl_1_Load,
497 IDirect3DTextureImpl_1_Unload,