texture.c 17 KB
Newer Older
1
/* Direct3D Texture
2
 * Copyright (c) 1998 Lionel ULMER
3
 * Copyright (c) 2006 Stefan DÖSINGER
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This file contains the implementation of interface Direct3DTexture2.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 38
#include "wine/exception.h"

39 40 41
#include "ddraw.h"
#include "d3d.h"

42 43
#include "ddraw_private.h"
#include "wine/debug.h"
44

45
WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46
WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
47

48 49 50 51 52 53 54
/*****************************************************************************
 * IUnknown interfaces. They are thunks to IDirectDrawSurface7
 *****************************************************************************/
static HRESULT WINAPI
Thunk_IDirect3DTextureImpl_2_QueryInterface(IDirect3DTexture2 *iface,
                                            REFIID riid,
                                            void **obj)
55
{
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);
59 60
}

61 62 63 64 65
static HRESULT WINAPI
Thunk_IDirect3DTextureImpl_1_QueryInterface(IDirect3DTexture *iface,
                                            REFIID riid,
                                            void **obj)
{
66
    IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
67
    TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", This, debugstr_guid(riid), obj);
68

69
    return IDirectDrawSurface7_QueryInterface((IDirectDrawSurface7 *)This, riid, obj);
70 71
}

72 73
static ULONG WINAPI
Thunk_IDirect3DTextureImpl_2_AddRef(IDirect3DTexture2 *iface)
74
{
75
    IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
76 77
    TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);

78
    return IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)This);
79 80
}

81 82
static ULONG WINAPI
Thunk_IDirect3DTextureImpl_1_AddRef(IDirect3DTexture *iface)
83
{
84
    IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
85 86
    TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);

87
    return IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)This);
88 89
}

90 91
static ULONG WINAPI
Thunk_IDirect3DTextureImpl_2_Release(IDirect3DTexture2 *iface)
92
{
93
    IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
94
    TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
95

96
    return IDirectDrawSurface7_Release((IDirectDrawSurface7 *)This);
97
}
98 99


100 101 102
static ULONG WINAPI
Thunk_IDirect3DTextureImpl_1_Release(IDirect3DTexture *iface)
{
103
    IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
104
    TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", This);
105

106
    return IDirectDrawSurface7_Release((IDirectDrawSurface7 *)This);
107
}
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/*****************************************************************************
 * IDirect3DTexture interface
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DTexture1::Initialize
 *
 * The sdk says it's not implemented
 *
 * Params:
 *  ?
 *
 * Returns
 *  DDERR_UNSUPPORTED
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DTextureImpl_1_Initialize(IDirect3DTexture *iface,
                                  IDirect3DDevice *Direct3DDevice,
                                  IDirectDrawSurface *DDSurface)
129
{
130
    TRACE("(%p)->(%p,%p) Not implemented\n", iface, Direct3DDevice, DDSurface);
131
    return DDERR_UNSUPPORTED; /* Unchecked */
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*****************************************************************************
 * IDirect3DTexture2::PaletteChanged
 *
 * Informs the texture about a palette change
 *
 * Params:
 *  Start: Start index of the change
 *  Count: The number of changed entries
 *
 * Returns
 *  D3D_OK, because it's a stub
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DTextureImpl_PaletteChanged(IDirect3DTexture2 *iface,
                                         DWORD Start,
                                         DWORD Count)
151
{
152
    IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
153
    FIXME("(%p)->(%08x,%08x): stub!\n", This, Start, Count);
154
    return D3D_OK;
155 156
}

157 158 159 160
static HRESULT WINAPI
Thunk_IDirect3DTextureImpl_1_PaletteChanged(IDirect3DTexture *iface,
                                            DWORD Start,
                                            DWORD Count)
161
{
162
    IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
163
    TRACE("(%p)->(%08x,%08x) thunking to IDirect3DTexture2 interface.\n", This, Start, Count);
164

165
    return IDirect3DTexture2_PaletteChanged((IDirect3DTexture2 *)&This->IDirect3DTexture2_vtbl, Start, Count);
166 167
}

168

169 170 171 172 173 174 175 176 177 178 179 180
/*****************************************************************************
 * IDirect3DTexture::Unload
 *
 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
 *
 *
 * Returns:
 *  DDERR_UNSUPPORTED
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DTextureImpl_1_Unload(IDirect3DTexture *iface)
181
{
182
    IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
183 184
    TRACE("(%p)->(): not implemented!\n", This);
    return DDERR_UNSUPPORTED;
185 186
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/*****************************************************************************
 * IDirect3DTexture2::GetHandle
 *
 * Returns handle for the texture. At the moment, the interface
 * to the IWineD3DTexture is used.
 *
 * Params:
 *  Direct3DDevice2: Device this handle is assigned to
 *  Handle: Address to store the handle at.
 *
 * Returns:
 *  D3D_OK
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DTextureImpl_GetHandle(IDirect3DTexture2 *iface,
                                    IDirect3DDevice2 *Direct3DDevice2,
                                    D3DTEXTUREHANDLE *lpHandle)
205
{
206
    IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
207
    IDirect3DDeviceImpl *d3d = device_from_device2(Direct3DDevice2);
208

209 210
    TRACE("(%p)->(%p,%p)\n", This, d3d, lpHandle);

211
    EnterCriticalSection(&ddraw_cs);
212
    if(!This->Handle)
213
    {
214 215 216 217 218 219
        This->Handle = IDirect3DDeviceImpl_CreateHandle(d3d);
        if(This->Handle)
        {
            d3d->Handles[This->Handle - 1].ptr = This;
            d3d->Handles[This->Handle - 1].type = DDrawHandle_Texture;
        }
220
    }
221
    *lpHandle = This->Handle;
222

223
    TRACE(" returning handle %08x.\n", *lpHandle);
224

225
    LeaveCriticalSection(&ddraw_cs);
226
    return D3D_OK;
227 228
}

229 230 231 232
static HRESULT WINAPI
Thunk_IDirect3DTextureImpl_1_GetHandle(IDirect3DTexture *iface,
                                       LPDIRECT3DDEVICE lpDirect3DDevice,
                                       LPD3DTEXTUREHANDLE lpHandle)
233
{
234
    IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
235
    IDirect3DDeviceImpl *d3d = device_from_device1(lpDirect3DDevice);
236 237 238
    IDirect3DTexture2 *d3d_texture2 = (IDirect3DTexture2 *)&This->IDirect3DTexture2_vtbl;
    IDirect3DDevice2 *d3d_device2 = (IDirect3DDevice2 *)&d3d->IDirect3DDevice2_vtbl;

239
    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DTexture2 interface.\n", This, d3d, lpHandle);
240

241
    return IDirect3DTexture2_GetHandle(d3d_texture2, d3d_device2, lpHandle);
242 243
}

244 245 246 247 248 249 250 251 252 253 254

/*****************************************************************************
 * get_sub_mimaplevel
 *
 * Helper function that returns the next mipmap level
 *
 * tex_ptr: Surface of which to return the next level
 *
 *****************************************************************************/
static IDirectDrawSurfaceImpl *
get_sub_mimaplevel(IDirectDrawSurfaceImpl *tex_ptr)
255
{
256
    /* Now go down the mipmap chain to the next surface */
257
    static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
258 259 260
    LPDIRECTDRAWSURFACE7 next_level;
    IDirectDrawSurfaceImpl *surf_ptr;
    HRESULT hr;
261

262
    hr = IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)tex_ptr, &mipmap_caps, &next_level);
263
    if (FAILED(hr)) return NULL;
264

265
    surf_ptr = (IDirectDrawSurfaceImpl *)next_level;
266
    IDirectDrawSurface7_Release(next_level);
267

268
    return surf_ptr;
269 270
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
/*****************************************************************************
 * IDirect3DTexture2::Load
 *
 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
 *
 * This function isn't relayed to WineD3D because the whole interface is
 * implemented in DDraw only. For speed improvements a implementation which
 * takes OpenGL more into account could be placed into WineD3D.
 *
 * Params:
 *  D3DTexture2: Address of the texture to load
 *
 * Returns:
 *  D3D_OK on success
 *  D3DERR_TEXTURE_LOAD_FAILED.
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DTextureImpl_Load(IDirect3DTexture2 *iface,
                          IDirect3DTexture2 *D3DTexture2)
291
{
292
    IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
293
    IDirectDrawSurfaceImpl *src_ptr = surface_from_texture2(D3DTexture2);
294
    HRESULT ret_value = D3D_OK;
295 296
    if(src_ptr == This)
    {
297
        TRACE("copying surface %p to surface %p, why?\n", src_ptr, This);
298 299
        return ret_value;
    }
300

301
    TRACE("(%p)->(%p)\n", This, src_ptr);
302
    EnterCriticalSection(&ddraw_cs);
303 304 305 306

    if (((src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) != (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)) ||
        (src_ptr->surface_desc.u2.dwMipMapCount != This->surface_desc.u2.dwMipMapCount))
    {
307 308 309
        ERR("Trying to load surfaces with different mip-map counts !\n");
    }

310 311
    while(1)
    {
312 313
        IWineD3DPalette *wine_pal, *wine_pal_src;
        IDirectDrawPalette *pal = NULL, *pal_src = NULL;
314 315 316 317 318 319 320 321 322 323 324 325
        DDSURFACEDESC *src_d, *dst_d;

        TRACE(" copying surface %p to surface %p (mipmap level %d)\n", src_ptr, This, src_ptr->mipmap_level);

        /* Suppress the ALLOCONLOAD flag */
        This->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;

        /* Get the palettes */
        ret_value = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wine_pal);
        if( ret_value != D3D_OK)
        {
            ERR("IWineD3DSurface::GetPalette failed! This is unexpected\n");
326
            LeaveCriticalSection(&ddraw_cs);
327 328 329 330 331 332 333 334
            return D3DERR_TEXTURE_LOAD_FAILED;
        }
        if(wine_pal)
        {
            ret_value = IWineD3DPalette_GetParent(wine_pal, (IUnknown **) &pal);
            if(ret_value != D3D_OK)
            {
                ERR("IWineD3DPalette::GetParent failed! This is unexpected\n");
335
                LeaveCriticalSection(&ddraw_cs);
336 337 338 339 340 341 342 343
                return D3DERR_TEXTURE_LOAD_FAILED;
            }
        }

        ret_value = IWineD3DSurface_GetPalette(src_ptr->WineD3DSurface, &wine_pal_src);
        if( ret_value != D3D_OK)
        {
            ERR("IWineD3DSurface::GetPalette failed! This is unexpected\n");
344
            LeaveCriticalSection(&ddraw_cs);
345 346 347 348 349 350 351 352
            return D3DERR_TEXTURE_LOAD_FAILED;
        }
        if(wine_pal_src)
        {
            ret_value = IWineD3DPalette_GetParent(wine_pal_src, (IUnknown **) &pal_src);
            if(ret_value != D3D_OK)
            {
                ERR("IWineD3DPalette::GetParent failed! This is unexpected\n");
353
                if (pal) IDirectDrawPalette_Release(pal);
354
                LeaveCriticalSection(&ddraw_cs);
355 356 357 358
                return D3DERR_TEXTURE_LOAD_FAILED;
            }
        }

359
        if (pal_src != NULL)
360
        {
361
            PALETTEENTRY palent[256];
362

363 364
            if (pal == NULL)
            {
365
                IDirectDrawPalette_Release(pal_src);
366 367 368
                LeaveCriticalSection(&ddraw_cs);
                return DDERR_NOPALETTEATTACHED;
            }
369 370
            IDirectDrawPalette_GetEntries(pal_src, 0, 0, 256, palent);
            IDirectDrawPalette_SetEntries(pal, 0, 0, 256, palent);
371 372
        }

373 374 375
        if (pal) IDirectDrawPalette_Release(pal);
        if (pal_src) IDirectDrawPalette_Release(pal_src);

376 377 378 379 380 381 382 383
        /* Copy one surface on the other */
        dst_d = (DDSURFACEDESC *)&(This->surface_desc);
        src_d = (DDSURFACEDESC *)&(src_ptr->surface_desc);

        if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight))
        {
            /* Should also check for same pixel format, u1.lPitch, ... */
            ERR("Error in surface sizes\n");
384
            LeaveCriticalSection(&ddraw_cs);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
            return D3DERR_TEXTURE_LOAD_FAILED;
        }
        else
        {
            WINED3DLOCKED_RECT pSrcRect, pDstRect;

            /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
            /* I should put a macro for the calculus of bpp */

            /* Copy also the ColorKeying stuff */
            if (src_d->dwFlags & DDSD_CKSRCBLT)
            {
                dst_d->dwFlags |= DDSD_CKSRCBLT;
                dst_d->ddckCKSrcBlt.dwColorSpaceLowValue = src_d->ddckCKSrcBlt.dwColorSpaceLowValue;
                dst_d->ddckCKSrcBlt.dwColorSpaceHighValue = src_d->ddckCKSrcBlt.dwColorSpaceHighValue;
            }

            /* Copy the main memory texture into the surface that corresponds to the OpenGL
              texture object. */

            ret_value = IWineD3DSurface_LockRect(src_ptr->WineD3DSurface, &pSrcRect, NULL, 0);
            if(ret_value != D3D_OK)
            {
                ERR(" (%p) Locking the source surface failed\n", This);
409
                LeaveCriticalSection(&ddraw_cs);
410 411 412 413 414 415 416 417
                return D3DERR_TEXTURE_LOAD_FAILED;
            }

            ret_value = IWineD3DSurface_LockRect(This->WineD3DSurface, &pDstRect, NULL, 0);
            if(ret_value != D3D_OK)
            {
                ERR(" (%p) Locking the destination surface failed\n", This);
                IWineD3DSurface_UnlockRect(src_ptr->WineD3DSurface);
418
                LeaveCriticalSection(&ddraw_cs);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
                return D3DERR_TEXTURE_LOAD_FAILED;
            }

            if (This->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
                memcpy(pDstRect.pBits, pSrcRect.pBits, src_ptr->surface_desc.u1.dwLinearSize);
            else
                memcpy(pDstRect.pBits, pSrcRect.pBits, pSrcRect.Pitch * src_d->dwHeight);

            IWineD3DSurface_UnlockRect(src_ptr->WineD3DSurface);
            IWineD3DSurface_UnlockRect(This->WineD3DSurface);
        }

        if (src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
        {
            src_ptr = get_sub_mimaplevel(src_ptr);
        }
        else
        {
            src_ptr = NULL;
        }
        if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
        {
            This = get_sub_mimaplevel(This);
        }
        else
        {
            This = NULL;
        }

        if ((src_ptr == NULL) || (This == NULL))
        {
            if (src_ptr != This)
            {
                ERR(" Loading surface with different mipmap structure !!!\n");
            }
            break;
        }
456
    }
457

458
    LeaveCriticalSection(&ddraw_cs);
459
    return ret_value;
460
}
461

462 463 464
static HRESULT WINAPI
Thunk_IDirect3DTextureImpl_1_Load(IDirect3DTexture *iface,
                                  IDirect3DTexture *D3DTexture)
465
{
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);
469

470 471
    return IDirect3DTexture2_Load((IDirect3DTexture2 *)&This->IDirect3DTexture2_vtbl,
            D3DTexture ? (IDirect3DTexture2 *)&surface_from_texture1(D3DTexture)->IDirect3DTexture2_vtbl : NULL);
472 473
}

474 475 476 477
/*****************************************************************************
 * The VTables
 *****************************************************************************/
const IDirect3DTexture2Vtbl IDirect3DTexture2_Vtbl =
478
{
479 480 481 482 483 484
    Thunk_IDirect3DTextureImpl_2_QueryInterface,
    Thunk_IDirect3DTextureImpl_2_AddRef,
    Thunk_IDirect3DTextureImpl_2_Release,
    IDirect3DTextureImpl_GetHandle,
    IDirect3DTextureImpl_PaletteChanged,
    IDirect3DTextureImpl_Load,
485 486
};

487

488
const IDirect3DTextureVtbl IDirect3DTexture1_Vtbl =
489
{
490 491 492 493 494 495 496 497
    Thunk_IDirect3DTextureImpl_1_QueryInterface,
    Thunk_IDirect3DTextureImpl_1_AddRef,
    Thunk_IDirect3DTextureImpl_1_Release,
    IDirect3DTextureImpl_1_Initialize,
    Thunk_IDirect3DTextureImpl_1_GetHandle,
    Thunk_IDirect3DTextureImpl_1_PaletteChanged,
    Thunk_IDirect3DTextureImpl_1_Load,
    IDirect3DTextureImpl_1_Unload,
498
};