msi: Append a slash to network and url sources.
[wine] / dlls / ddraw / texture.c
1 /* Direct3D Texture
2  * Copyright (c) 1998 Lionel ULMER
3  * Copyright (c) 2006 Stefan DÖSINGER
4  *
5  * This file contains the implementation of interface Direct3DTexture2.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #define COBJMACROS
31 #define NONAMELESSUNION
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
38
39 #include "ddraw.h"
40 #include "d3d.h"
41
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
47
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)
55 {
56     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
57     TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", This, debugstr_guid(riid), obj);
58     return IDirectDrawSurface7_QueryInterface(ICOM_INTERFACE(This, IDirectDrawSurface7),
59                                               riid,
60                                               obj);
61 }
62
63 static HRESULT WINAPI
64 Thunk_IDirect3DTextureImpl_1_QueryInterface(IDirect3DTexture *iface,
65                                             REFIID riid,
66                                             void **obj)
67 {
68     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
69     TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", This, debugstr_guid(riid), obj);
70
71     return IDirectDrawSurface7_QueryInterface(ICOM_INTERFACE(This, IDirectDrawSurface7),
72                                               riid,
73                                               obj);
74 }
75
76 static ULONG WINAPI
77 Thunk_IDirect3DTextureImpl_2_AddRef(IDirect3DTexture2 *iface)
78 {
79     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
80     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
81
82     return IDirectDrawSurface7_AddRef(ICOM_INTERFACE(This, IDirectDrawSurface7));
83 }
84
85 static ULONG WINAPI
86 Thunk_IDirect3DTextureImpl_1_AddRef(IDirect3DTexture *iface)
87 {
88     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
89     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
90
91     return IDirectDrawSurface7_AddRef(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface));
92 }
93
94 static ULONG WINAPI
95 Thunk_IDirect3DTextureImpl_2_Release(IDirect3DTexture2 *iface)
96 {
97     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
98     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
99
100     return IDirectDrawSurface7_Release(ICOM_INTERFACE(This, IDirectDrawSurface7));
101 }
102
103
104 static ULONG WINAPI
105 Thunk_IDirect3DTextureImpl_1_Release(IDirect3DTexture *iface)
106 {
107     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
108     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
109
110     return IDirectDrawSurface7_Release(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface));
111 }
112
113 /*****************************************************************************
114  * IDirect3DTexture interface
115  *****************************************************************************/
116
117 /*****************************************************************************
118  * IDirect3DTexture1::Initialize
119  *
120  * The sdk says it's not implemented
121  *
122  * Params:
123  *  ?
124  *
125  * Returns
126  *  DDERR_UNSUPPORTED
127  *
128  *****************************************************************************/
129 static HRESULT WINAPI
130 IDirect3DTextureImpl_1_Initialize(IDirect3DTexture *iface,
131                                   IDirect3DDevice *Direct3DDevice,
132                                   IDirectDrawSurface *DDSurface)
133 {
134     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
135     IDirect3DDeviceImpl *d3d = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice, Direct3DDevice);
136     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface3, DDSurface);
137     TRACE("(%p)->(%p,%p) Not implemented\n", This, d3d, surf);
138     return DDERR_UNSUPPORTED; /* Unchecked */
139 }
140
141 /*****************************************************************************
142  * IDirect3DTexture2::PaletteChanged
143  *
144  * Informs the texture about a palette change
145  *
146  * Params:
147  *  Start: Start index of the change
148  *  Count: The number of changed entries
149  *
150  * Returns
151  *  D3D_OK, because it's a stub
152  *
153  *****************************************************************************/
154 static HRESULT WINAPI
155 IDirect3DTextureImpl_PaletteChanged(IDirect3DTexture2 *iface,
156                                          DWORD Start,
157                                          DWORD Count)
158 {
159     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
160     FIXME("(%p)->(%08x,%08x): stub!\n", This, Start, Count);
161     return D3D_OK;
162 }
163
164 static HRESULT WINAPI
165 Thunk_IDirect3DTextureImpl_1_PaletteChanged(IDirect3DTexture *iface,
166                                             DWORD Start,
167                                             DWORD Count)
168 {
169     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
170     TRACE("(%p)->(%08x,%08x) thunking to IDirect3DTexture2 interface.\n", This, Start, Count);
171
172     return IDirect3DTexture2_PaletteChanged(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
173                                             Start,
174                                             Count);
175 }
176
177
178 /*****************************************************************************
179  * IDirect3DTexture::Unload
180  *
181  * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
182  *
183  *
184  * Returns:
185  *  DDERR_UNSUPPORTED
186  *
187  *****************************************************************************/
188 static HRESULT WINAPI
189 IDirect3DTextureImpl_1_Unload(IDirect3DTexture *iface)
190 {
191     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
192     TRACE("(%p)->(): not implemented!\n", This);
193     return DDERR_UNSUPPORTED;
194 }
195
196 /*****************************************************************************
197  * IDirect3DTexture2::GetHandle
198  *
199  * Returns handle for the texture. At the moment, the interface
200  * to the IWineD3DTexture is used.
201  *
202  * Params:
203  *  Direct3DDevice2: Device this handle is assigned to
204  *  Handle: Address to store the handle at.
205  *
206  * Returns:
207  *  D3D_OK
208  *
209  *****************************************************************************/
210 static HRESULT WINAPI
211 IDirect3DTextureImpl_GetHandle(IDirect3DTexture2 *iface,
212                                     IDirect3DDevice2 *Direct3DDevice2,
213                                     D3DTEXTUREHANDLE *lpHandle)
214 {
215     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
216     IDirect3DDeviceImpl *d3d = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice2, Direct3DDevice2);
217
218     TRACE("(%p)->(%p,%p)\n", This, d3d, lpHandle);
219
220     EnterCriticalSection(&ddraw_cs);
221     if(!This->Handle)
222     {
223         This->Handle = IDirect3DDeviceImpl_CreateHandle(d3d);
224         if(This->Handle)
225         {
226             d3d->Handles[This->Handle - 1].ptr = This;
227             d3d->Handles[This->Handle - 1].type = DDrawHandle_Texture;
228         }
229     }
230     *lpHandle = This->Handle;
231
232     TRACE(" returning handle %08x.\n", *lpHandle);
233
234     LeaveCriticalSection(&ddraw_cs);
235     return D3D_OK;
236 }
237
238 static HRESULT WINAPI
239 Thunk_IDirect3DTextureImpl_1_GetHandle(IDirect3DTexture *iface,
240                                        LPDIRECT3DDEVICE lpDirect3DDevice,
241                                        LPD3DTEXTUREHANDLE lpHandle)
242 {
243     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
244     IDirect3DDeviceImpl *d3d = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice, lpDirect3DDevice);
245     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DTexture2 interface.\n", This, d3d, lpHandle);
246
247     return IDirect3DTexture2_GetHandle(ICOM_INTERFACE(This, IDirect3DTexture2),
248                                        ICOM_INTERFACE(d3d, IDirect3DDevice2),
249                                        lpHandle);
250 }
251
252
253 /*****************************************************************************
254  * get_sub_mimaplevel
255  *
256  * Helper function that returns the next mipmap level
257  *
258  * tex_ptr: Surface of which to return the next level
259  *
260  *****************************************************************************/
261 static IDirectDrawSurfaceImpl *
262 get_sub_mimaplevel(IDirectDrawSurfaceImpl *tex_ptr)
263 {
264     /* Now go down the mipmap chain to the next surface */
265     static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
266     LPDIRECTDRAWSURFACE7 next_level;
267     IDirectDrawSurfaceImpl *surf_ptr;
268     HRESULT hr;
269
270     hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(tex_ptr, IDirectDrawSurface7),
271                                                 &mipmap_caps, &next_level);
272     if (FAILED(hr)) return NULL;
273
274     surf_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, next_level);
275     IDirectDrawSurface7_Release(next_level);
276
277     return surf_ptr;
278 }
279
280 /*****************************************************************************
281  * IDirect3DTexture2::Load
282  *
283  * Loads a texture created with the DDSCAPS_ALLOCONLOAD
284  *
285  * This function isn't relayed to WineD3D because the whole interface is
286  * implemented in DDraw only. For speed improvements a implementation which
287  * takes OpenGL more into account could be placed into WineD3D.
288  *
289  * Params:
290  *  D3DTexture2: Address of the texture to load
291  *
292  * Returns:
293  *  D3D_OK on success
294  *  D3DERR_TEXTURE_LOAD_FAILED.
295  *
296  *****************************************************************************/
297 static HRESULT WINAPI
298 IDirect3DTextureImpl_Load(IDirect3DTexture2 *iface,
299                           IDirect3DTexture2 *D3DTexture2)
300 {
301     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
302     IDirectDrawSurfaceImpl *src_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, D3DTexture2);
303     IWineD3DPalette *wine_pal, *wine_pal_src;
304     IDirectDrawPalette *pal = NULL, *pal_src = NULL;
305     IDirectDrawPaletteImpl *pal_impl, *pal_impl_src;
306     HRESULT ret_value = D3D_OK;
307
308     TRACE("(%p)->(%p)\n", This, src_ptr);
309     EnterCriticalSection(&ddraw_cs);
310
311     if (((src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) != (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)) ||
312         (src_ptr->surface_desc.u2.dwMipMapCount != This->surface_desc.u2.dwMipMapCount))
313     {
314         ERR("Trying to load surfaces with different mip-map counts !\n");
315     }
316
317     while(1)
318     {
319         DDSURFACEDESC *src_d, *dst_d;
320
321         TRACE(" copying surface %p to surface %p (mipmap level %d)\n", src_ptr, This, src_ptr->mipmap_level);
322
323         if ( This->surface_desc.ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD )
324             /* If the surface is not allocated and its location is not yet specified,
325               force it to video memory */ 
326             if ( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_SYSTEMMEMORY|DDSCAPS_VIDEOMEMORY)) )
327                 This->surface_desc.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
328
329         /* Suppress the ALLOCONLOAD flag */
330         This->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
331
332         /* Get the palettes */
333         ret_value = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wine_pal);
334         if( ret_value != D3D_OK)
335         {
336             ERR("IWineD3DSurface::GetPalette failed! This is unexpected\n");
337             LeaveCriticalSection(&ddraw_cs);
338             return D3DERR_TEXTURE_LOAD_FAILED;
339         }
340         if(wine_pal)
341         {
342             ret_value = IWineD3DPalette_GetParent(wine_pal, (IUnknown **) &pal);
343             if(ret_value != D3D_OK)
344             {
345                 ERR("IWineD3DPalette::GetParent failed! This is unexpected\n");
346                 LeaveCriticalSection(&ddraw_cs);
347                 return D3DERR_TEXTURE_LOAD_FAILED;
348             }
349             pal_impl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, pal);
350         }
351         else
352         {
353           pal_impl = NULL;
354         }
355
356         ret_value = IWineD3DSurface_GetPalette(src_ptr->WineD3DSurface, &wine_pal_src);
357         if( ret_value != D3D_OK)
358         {
359             ERR("IWineD3DSurface::GetPalette failed! This is unexpected\n");
360             LeaveCriticalSection(&ddraw_cs);
361             return D3DERR_TEXTURE_LOAD_FAILED;
362         }
363         if(wine_pal_src)
364         {
365             ret_value = IWineD3DPalette_GetParent(wine_pal_src, (IUnknown **) &pal_src);
366             if(ret_value != D3D_OK)
367             {
368                 ERR("IWineD3DPalette::GetParent failed! This is unexpected\n");
369                 LeaveCriticalSection(&ddraw_cs);
370                 return D3DERR_TEXTURE_LOAD_FAILED;
371             }
372             pal_impl_src = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, pal_src);
373         }
374         else
375         {
376             pal_impl_src = NULL;
377         }
378
379         /* After seeing some logs, not sure at all about this... */
380         if (pal_impl == NULL)
381         {
382             IWineD3DSurface_SetPalette(This->WineD3DSurface, wine_pal);
383             if (pal_impl_src != NULL) IDirectDrawPalette_AddRef(ICOM_INTERFACE(pal_impl_src, IDirectDrawPalette));
384         }
385         else
386         {
387             if (pal_impl_src != NULL)
388             {
389                 PALETTEENTRY palent[256];
390                 IDirectDrawPalette_GetEntries(ICOM_INTERFACE(pal_impl_src, IDirectDrawPalette),
391                                               0, 0, 256, palent);
392                 IDirectDrawPalette_SetEntries(ICOM_INTERFACE(pal_impl, IDirectDrawPalette),
393                                               0, 0, 256, palent);
394             }
395         }
396
397         /* Copy one surface on the other */
398         dst_d = (DDSURFACEDESC *)&(This->surface_desc);
399         src_d = (DDSURFACEDESC *)&(src_ptr->surface_desc);
400
401         if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight))
402         {
403             /* Should also check for same pixel format, u1.lPitch, ... */
404             ERR("Error in surface sizes\n");
405             LeaveCriticalSection(&ddraw_cs);
406             return D3DERR_TEXTURE_LOAD_FAILED;
407         }
408         else
409         {
410             WINED3DLOCKED_RECT pSrcRect, pDstRect;
411
412             /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
413             /* I should put a macro for the calculus of bpp */
414
415             /* Copy also the ColorKeying stuff */
416             if (src_d->dwFlags & DDSD_CKSRCBLT)
417             {
418                 dst_d->dwFlags |= DDSD_CKSRCBLT;
419                 dst_d->ddckCKSrcBlt.dwColorSpaceLowValue = src_d->ddckCKSrcBlt.dwColorSpaceLowValue;
420                 dst_d->ddckCKSrcBlt.dwColorSpaceHighValue = src_d->ddckCKSrcBlt.dwColorSpaceHighValue;
421             }
422
423             /* Copy the main memory texture into the surface that corresponds to the OpenGL
424               texture object. */
425
426             ret_value = IWineD3DSurface_LockRect(src_ptr->WineD3DSurface, &pSrcRect, NULL, 0);
427             if(ret_value != D3D_OK)
428             {
429                 ERR(" (%p) Locking the source surface failed\n", This);
430                 LeaveCriticalSection(&ddraw_cs);
431                 return D3DERR_TEXTURE_LOAD_FAILED;
432             }
433
434             ret_value = IWineD3DSurface_LockRect(This->WineD3DSurface, &pDstRect, NULL, 0);
435             if(ret_value != D3D_OK)
436             {
437                 ERR(" (%p) Locking the destination surface failed\n", This);
438                 IWineD3DSurface_UnlockRect(src_ptr->WineD3DSurface);
439                 LeaveCriticalSection(&ddraw_cs);
440                 return D3DERR_TEXTURE_LOAD_FAILED;
441             }
442
443             if (This->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
444                 memcpy(pDstRect.pBits, pSrcRect.pBits, src_ptr->surface_desc.u1.dwLinearSize);
445             else
446                 memcpy(pDstRect.pBits, pSrcRect.pBits, pSrcRect.Pitch * src_d->dwHeight);
447
448             IWineD3DSurface_UnlockRect(src_ptr->WineD3DSurface);
449             IWineD3DSurface_UnlockRect(This->WineD3DSurface);
450         }
451
452         if (src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
453         {
454             src_ptr = get_sub_mimaplevel(src_ptr);
455         }
456         else
457         {
458             src_ptr = NULL;
459         }
460         if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
461         {
462             This = get_sub_mimaplevel(This);
463         }
464         else
465         {
466             This = NULL;
467         }
468
469         if ((src_ptr == NULL) || (This == NULL))
470         {
471             if (src_ptr != This)
472             {
473                 ERR(" Loading surface with different mipmap structure !!!\n");
474             }
475             break;
476         }
477     }
478
479     LeaveCriticalSection(&ddraw_cs);
480     return ret_value;
481 }
482
483 static HRESULT WINAPI
484 Thunk_IDirect3DTextureImpl_1_Load(IDirect3DTexture *iface,
485                                   IDirect3DTexture *D3DTexture)
486 {
487     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
488     IDirectDrawSurfaceImpl *Texture = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture, D3DTexture);
489     TRACE("(%p)->(%p) thunking to IDirect3DTexture2 interface.\n", This, Texture);
490
491     return IDirect3DTexture2_Load(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
492                                   COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, D3DTexture));
493 }
494
495 /*****************************************************************************
496  * The VTables
497  *****************************************************************************/
498 const IDirect3DTexture2Vtbl IDirect3DTexture2_Vtbl =
499 {
500     Thunk_IDirect3DTextureImpl_2_QueryInterface,
501     Thunk_IDirect3DTextureImpl_2_AddRef,
502     Thunk_IDirect3DTextureImpl_2_Release,
503     IDirect3DTextureImpl_GetHandle,
504     IDirect3DTextureImpl_PaletteChanged,
505     IDirect3DTextureImpl_Load,
506 };
507
508
509 const IDirect3DTextureVtbl IDirect3DTexture1_Vtbl =
510 {
511     Thunk_IDirect3DTextureImpl_1_QueryInterface,
512     Thunk_IDirect3DTextureImpl_1_AddRef,
513     Thunk_IDirect3DTextureImpl_1_Release,
514     IDirect3DTextureImpl_1_Initialize,
515     Thunk_IDirect3DTextureImpl_1_GetHandle,
516     Thunk_IDirect3DTextureImpl_1_PaletteChanged,
517     Thunk_IDirect3DTextureImpl_1_Load,
518     IDirect3DTextureImpl_1_Unload,
519 };