texture.c 13.4 KB
Newer Older
1 2 3
/*
 * IDirect3DTexture9 implementation
 *
4
 * Copyright 2002-2005 Jason Edmeades
5 6
 * Copyright 2002-2005 Raphael Junqueira
 * Copyright 2005 Oliver Stieber
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23 24 25
 */

#include "config.h"
#include "d3d9_private.h"

26
WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 28

/* IDirect3DTexture9 IUnknown parts follow: */
29
static HRESULT WINAPI IDirect3DTexture9Impl_QueryInterface(LPDIRECT3DTEXTURE9 iface, REFIID riid, LPVOID* ppobj) {
30
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
31 32 33 34 35

    if (IsEqualGUID(riid, &IID_IUnknown)
        || IsEqualGUID(riid, &IID_IDirect3DResource9)
        || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
        || IsEqualGUID(riid, &IID_IDirect3DTexture9)) {
36
        IUnknown_AddRef(iface);
37
        *ppobj = This;
H. Verbeet's avatar
H. Verbeet committed
38
        return S_OK;
39 40 41
    }

    WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
H. Verbeet's avatar
H. Verbeet committed
42
    *ppobj = NULL;
43 44 45
    return E_NOINTERFACE;
}

46
static ULONG WINAPI IDirect3DTexture9Impl_AddRef(LPDIRECT3DTEXTURE9 iface) {
47
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
48 49
    ULONG ref = InterlockedIncrement(&This->ref);

50
    TRACE("(%p) : AddRef from %d\n", This, ref - 1);
51 52

    return ref;
53 54
}

55
static ULONG WINAPI IDirect3DTexture9Impl_Release(LPDIRECT3DTEXTURE9 iface) {
56
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
57
    ULONG ref = InterlockedDecrement(&This->ref);
58

59
    TRACE("(%p) : ReleaseRef to %d\n", This, ref);
60

61
    if (ref == 0) {
62
        EnterCriticalSection(&d3d9_cs);
63
        IWineD3DTexture_Destroy(This->wineD3DTexture, D3D9CB_DestroySurface);
64
        LeaveCriticalSection(&d3d9_cs);
65
        IUnknown_Release(This->parentDevice);
66 67 68 69 70 71
        HeapFree(GetProcessHeap(), 0, This);
    }
    return ref;
}

/* IDirect3DTexture9 IDirect3DResource9 Interface follow: */
72
static HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(LPDIRECT3DTEXTURE9 iface, IDirect3DDevice9** ppDevice) {
73
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
74
    HRESULT hr;
75
    TRACE("(%p) Relay\n", This);
76 77 78 79 80

    EnterCriticalSection(&d3d9_cs);
    hr = IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
81 82
}

83
static HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
84
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
85
    HRESULT hr;
86
    TRACE("(%p) Relay\n", This);
87 88 89 90 91

    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
92 93
}

94
static HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
95
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
96
    HRESULT hr;
97
    TRACE("(%p) Relay\n", This);
98 99 100 101 102

    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
103 104
}

105
static HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid) {
106
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
107
    HRESULT hr;
108
    TRACE("(%p) Relay\n", This);
109 110 111 112 113

    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
114 115
}

116
static DWORD WINAPI IDirect3DTexture9Impl_SetPriority(LPDIRECT3DTEXTURE9 iface, DWORD PriorityNew) {
117
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
118
    DWORD ret;
119
    TRACE("(%p) Relay\n", This);
120 121 122 123 124

    EnterCriticalSection(&d3d9_cs);
    ret = IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
    LeaveCriticalSection(&d3d9_cs);
    return ret;
125 126
}

127
static DWORD WINAPI IDirect3DTexture9Impl_GetPriority(LPDIRECT3DTEXTURE9 iface) {
128
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
129
    DWORD ret;
130
    TRACE("(%p) Relay\n", This);
131 132 133 134 135

    EnterCriticalSection(&d3d9_cs);
    ret = IWineD3DTexture_GetPriority(This->wineD3DTexture);
    LeaveCriticalSection(&d3d9_cs);
    return ret;
136 137
}

138
static void WINAPI IDirect3DTexture9Impl_PreLoad(LPDIRECT3DTEXTURE9 iface) {
139
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
140
    TRACE("(%p) Relay\n", This);
141 142

    EnterCriticalSection(&d3d9_cs);
143
    IWineD3DTexture_PreLoad(This->wineD3DTexture);
144
    LeaveCriticalSection(&d3d9_cs);
145 146
}

147
static D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(LPDIRECT3DTEXTURE9 iface) {
148
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
149
    HRESULT ret;
150
    TRACE("(%p) Relay\n", This);
151 152 153 154 155

    EnterCriticalSection(&d3d9_cs);
    ret = IWineD3DTexture_GetType(This->wineD3DTexture);
    LeaveCriticalSection(&d3d9_cs);
    return ret;
156 157 158
}

/* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
159
static DWORD WINAPI IDirect3DTexture9Impl_SetLOD(LPDIRECT3DTEXTURE9 iface, DWORD LODNew) {
160
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
161
    DWORD ret;
162
    TRACE("(%p) Relay\n", This);
163 164 165 166 167

    EnterCriticalSection(&d3d9_cs);
    ret = IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
    LeaveCriticalSection(&d3d9_cs);
    return ret;
168 169
}

170
static DWORD WINAPI IDirect3DTexture9Impl_GetLOD(LPDIRECT3DTEXTURE9 iface) {
171
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
172
    DWORD ret;
173
    TRACE("(%p) Relay\n", This);
174 175 176 177 178

    EnterCriticalSection(&d3d9_cs);
    ret = IWineD3DTexture_GetLOD(This->wineD3DTexture);
    LeaveCriticalSection(&d3d9_cs);
    return ret;
179 180
}

181
static DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(LPDIRECT3DTEXTURE9 iface) {
182
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
183
    DWORD ret;
184
    TRACE("(%p) Relay\n", This);
185 186 187 188 189

    EnterCriticalSection(&d3d9_cs);
    ret = IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
    LeaveCriticalSection(&d3d9_cs);
    return ret;
190 191
}

192
static HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
193
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
194
    HRESULT hr;
195
    TRACE("(%p) Relay\n", This);
196 197 198 199 200

    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_SetAutoGenFilterType(This->wineD3DTexture, (WINED3DTEXTUREFILTERTYPE) FilterType);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
201 202
}

203
static D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface) {
204
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
205
    D3DTEXTUREFILTERTYPE ret;
206
    TRACE("(%p) Relay\n", This);
207 208 209 210 211

    EnterCriticalSection(&d3d9_cs);
    ret = (D3DTEXTUREFILTERTYPE) IWineD3DTexture_GetAutoGenFilterType(This->wineD3DTexture);
    LeaveCriticalSection(&d3d9_cs);
    return ret;
212 213
}

214
static void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(LPDIRECT3DTEXTURE9 iface) {
215
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
216
    TRACE("(%p) Relay\n", This);
217 218

    EnterCriticalSection(&d3d9_cs);
219
    IWineD3DTexture_GenerateMipSubLevels(This->wineD3DTexture);
220
    LeaveCriticalSection(&d3d9_cs);
221 222 223
}

/* IDirect3DTexture9 Interface follow: */
224
static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
225
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
226

227 228
    WINED3DSURFACE_DESC    wined3ddesc;
    UINT                   tmpInt = -1;
229
    HRESULT                hr;
230
    TRACE("(%p) Relay\n", This);
231 232

    /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
233
    wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
234
    wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
235
    wined3ddesc.Usage               = &pDesc->Usage;
236
    wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
237
    wined3ddesc.Size                = &tmpInt; /* required for d3d8 */
238
    wined3ddesc.MultiSampleType     = (WINED3DMULTISAMPLE_TYPE *) &pDesc->MultiSampleType;
239 240 241 242
    wined3ddesc.MultiSampleQuality  = &pDesc->MultiSampleQuality;
    wined3ddesc.Width               = &pDesc->Width;
    wined3ddesc.Height              = &pDesc->Height;

243 244 245 246
    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
247 248
}

249
static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(LPDIRECT3DTEXTURE9 iface, UINT Level, IDirect3DSurface9** ppSurfaceLevel) {
250
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
251 252
    HRESULT hrc = D3D_OK;
    IWineD3DSurface *mySurface = NULL;
253 254

    TRACE("(%p) Relay\n", This);
255
    EnterCriticalSection(&d3d9_cs);
256
    hrc = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
257
    if (hrc == D3D_OK && NULL != ppSurfaceLevel) {
258 259 260
       IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppSurfaceLevel);
       IWineD3DSurface_Release(mySurface);
    }
261
    LeaveCriticalSection(&d3d9_cs);
262
    return hrc;
263 264
}

265
static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
266
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
267
    HRESULT hr;
268
    TRACE("(%p) Relay\n", This);
269 270 271 272 273

    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_LockRect(This->wineD3DTexture, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
274 275
}

276
static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(LPDIRECT3DTEXTURE9 iface, UINT Level) {
277
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
278
    HRESULT hr;
279
    TRACE("(%p) Relay\n", This);
280 281 282 283 284

    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_UnlockRect(This->wineD3DTexture, Level);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
285 286
}

287
static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(LPDIRECT3DTEXTURE9 iface, CONST RECT* pDirtyRect) {
288
    IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
289
    HRESULT hr;
290
    TRACE("(%p) Relay\n", This);
291 292 293 294 295

    EnterCriticalSection(&d3d9_cs);
    hr = IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
    LeaveCriticalSection(&d3d9_cs);
    return hr;
296 297
}

298
static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
299
{
300
    /* IUnknown */
301 302 303
    IDirect3DTexture9Impl_QueryInterface,
    IDirect3DTexture9Impl_AddRef,
    IDirect3DTexture9Impl_Release,
304
     /* IDirect3DResource9 */
305 306 307 308 309 310 311 312
    IDirect3DTexture9Impl_GetDevice,
    IDirect3DTexture9Impl_SetPrivateData,
    IDirect3DTexture9Impl_GetPrivateData,
    IDirect3DTexture9Impl_FreePrivateData,
    IDirect3DTexture9Impl_SetPriority,
    IDirect3DTexture9Impl_GetPriority,
    IDirect3DTexture9Impl_PreLoad,
    IDirect3DTexture9Impl_GetType,
313
    /* IDirect3dBaseTexture9 */
314 315 316 317 318 319
    IDirect3DTexture9Impl_SetLOD,
    IDirect3DTexture9Impl_GetLOD,
    IDirect3DTexture9Impl_GetLevelCount,
    IDirect3DTexture9Impl_SetAutoGenFilterType,
    IDirect3DTexture9Impl_GetAutoGenFilterType,
    IDirect3DTexture9Impl_GenerateMipSubLevels,
320
    /* IDirect3DTexture9 */
321 322 323 324 325 326 327 328 329 330 331
    IDirect3DTexture9Impl_GetLevelDesc,
    IDirect3DTexture9Impl_GetSurfaceLevel,
    IDirect3DTexture9Impl_LockRect,
    IDirect3DTexture9Impl_UnlockRect,
    IDirect3DTexture9Impl_AddDirtyRect
};


/* IDirect3DDevice9 IDirect3DTexture9 Methods follow: */
HRESULT  WINAPI  IDirect3DDevice9Impl_CreateTexture(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
                                                    D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, HANDLE* pSharedHandle) {
332
    IDirect3DTexture9Impl *object;
333
    IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
334
    HRESULT hrc = D3D_OK;
335
    
336
    TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%d), Fmt(%#x), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
337

338 339
    /* Allocate the storage for the device */
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture9Impl));
340

341
    if (NULL == object) {
342
        FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
343 344 345
        return D3DERR_OUTOFVIDEOMEMORY;
    }

346 347
    object->lpVtbl = &Direct3DTexture9_Vtbl;
    object->ref = 1;
348
    EnterCriticalSection(&d3d9_cs);
349
    hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage & WINED3DUSAGE_MASK,
350
                                 (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DTexture, pSharedHandle, (IUnknown *)object, D3D9CB_CreateSurface);
351
    LeaveCriticalSection(&d3d9_cs);
352

353
    if (FAILED(hrc)) {
354 355

        /* free up object */
356 357
        FIXME("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
        HeapFree(GetProcessHeap(), 0, object);
358
   } else {
359 360
        IUnknown_AddRef(iface);
        object->parentDevice = iface;
361
        *ppTexture= (LPDIRECT3DTEXTURE9) object;
362
        TRACE("(%p) Created Texture %p, %p\n", This, object, object->wineD3DTexture);
363 364 365
   }

   return hrc;
366
}