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

#include "config.h"
#include "wined3d_private.h"

27
WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28
#define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29 30 31 32

/* *******************************************
   IWineD3DTexture IUnknown parts follow
   ******************************************* */
33
static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
34 35
{
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
36 37
    TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
    if (IsEqualGUID(riid, &IID_IUnknown)
38
        || IsEqualGUID(riid, &IID_IWineD3DBase)
39 40 41 42 43
        || IsEqualGUID(riid, &IID_IWineD3DResource)
        || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
        || IsEqualGUID(riid, &IID_IWineD3DTexture)){
        IUnknown_AddRef(iface);
        *ppobj = This;
44
        return WINED3D_OK;
45
    }
46
    *ppobj = NULL;
47 48 49
    return E_NOINTERFACE;
}

50
static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
51
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
52
    TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
53 54 55
    return InterlockedIncrement(&This->resource.ref);
}

56
static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
57 58
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
    ULONG ref;
59
    TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
60 61
    ref = InterlockedDecrement(&This->resource.ref);
    if (ref == 0) {
62
        IWineD3DTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
63 64 65 66
    }
    return ref;
}

67

68 69 70
/* ****************************************************
   IWineD3DTexture IWineD3DResource parts follow
   **************************************************** */
71
static HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
72
    return resource_get_device((IWineD3DResource *)iface, ppDevice);
73 74
}

75
static HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
76
    return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
77 78
}

79
static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
80
    return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
81 82
}

83
static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
84
    return resource_free_private_data((IWineD3DResource *)iface, refguid);
85 86
}

87
static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
88
    return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
89 90
}

91
static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
92
    return resource_get_priority((IWineD3DResource *)iface);
93 94
}

95
static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
96 97

    /* Override the IWineD3DResource PreLoad method */
98 99
    unsigned int i;
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
100
    IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
101 102
    BOOL srgb_mode = This->baseTexture.is_srgb;
    BOOL srgb_was_toggled = FALSE;
103

104
    TRACE("(%p) : About to load texture\n", This);
105

106 107 108 109 110
    if(!device->isInDraw) {
        /* ActivateContext sets isInDraw to TRUE when loading a pbuffer into a texture, thus no danger of
         * recursive calls
         */
        ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
111 112 113 114
    } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
        srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
        srgb_was_toggled = This->baseTexture.is_srgb != srgb_mode;
        This->baseTexture.is_srgb = srgb_mode;
115 116
    }

117 118 119 120 121 122 123 124 125 126 127
    if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
        for (i = 0; i < This->baseTexture.levels; i++) {
            if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[i])) {
                TRACE("Reloading surface because the d3d8/9 palette was changed\n");
                /* TODO: This is not necessarily needed with hw palettized texture support */
                IWineD3DSurface_LoadLocation(This->surfaces[i], SFLAG_INSYSMEM, NULL);
                /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
                IWineD3DSurface_ModifyLocation(This->surfaces[i], SFLAG_INTEXTURE, FALSE);
            }
        }
    }
128 129
    /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
    if (This->baseTexture.dirty) {
130
        for (i = 0; i < This->baseTexture.levels; i++) {
131
            IWineD3DSurface_LoadTexture(This->surfaces[i], srgb_mode);
132
        }
133 134 135 136 137
    } else if (srgb_was_toggled) {
        if (This->baseTexture.srgb_mode_change_count < 20)
            ++This->baseTexture.srgb_mode_change_count;
        else
            FIXME("Texture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
138

139
        for (i = 0; i < This->baseTexture.levels; i++) {
140
            IWineD3DSurface_AddDirtyRect(This->surfaces[i], NULL);
141
            surface_force_reload(This->surfaces[i]);
142 143
            IWineD3DSurface_LoadTexture(This->surfaces[i], srgb_mode);
        }
144 145
    } else {
        TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
146
    }
147

148 149 150
    /* No longer dirty */
    This->baseTexture.dirty = FALSE;

151 152 153
    return ;
}

154
static void WINAPI IWineD3DTextureImpl_UnLoad(IWineD3DTexture *iface) {
155 156 157 158 159 160 161 162 163 164
    unsigned int i;
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
    TRACE("(%p)\n", This);

    /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
     * surface before, this one will be a NOP and vice versa. Unloading an unloaded
     * surface is fine
     */
    for (i = 0; i < This->baseTexture.levels; i++) {
        IWineD3DSurface_UnLoad(This->surfaces[i]);
165
        surface_set_texture_name(This->surfaces[i], 0);
166 167
    }

168
    basetexture_unload((IWineD3DBaseTexture *)iface);
169 170
}

171
static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
172
    return resource_get_type((IWineD3DResource *)iface);
173 174
}

175
static HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
176
    return resource_get_parent((IWineD3DResource *)iface, pParent);
177 178 179 180 181
}

/* ******************************************************
   IWineD3DTexture IWineD3DBaseTexture parts follow
   ****************************************************** */
182
static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
183
    return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
184 185
}

186
static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
187
    return basetexture_get_lod((IWineD3DBaseTexture *)iface);
188 189
}

190
static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
191
    return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
192 193
}

194
static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
195
  return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
196 197
}

198
static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
199
  return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
200 201
}

202
static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
203
    basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
204 205 206
}

/* Internal function, No d3d mapping */
207
static BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
208
    return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
209 210
}

211
static BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
212
    return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
213 214
}

215
static HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface) {
216
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
217 218 219
    BOOL set_gl_texture_desc = This->baseTexture.textureName == 0;
    HRESULT hr;

220
    TRACE("(%p) : relay to BaseTexture\n", This);
221

222
    hr = basetexture_bind((IWineD3DBaseTexture *)iface);
223 224 225
    if (set_gl_texture_desc && SUCCEEDED(hr)) {
        UINT i;
        for (i = 0; i < This->baseTexture.levels; ++i) {
226
            surface_set_texture_name(This->surfaces[i], This->baseTexture.textureName);
227
        }
228 229
        /* Conditinal non power of two textures use a different clamping default. If we're using the GL_WINE_normalized_texrect
         * partial driver emulation, we're dealing with a GL_TEXTURE_2D texture which has the address mode set to repeat - something
230 231
         * that prevents us from hitting the accelerated codepath. Thus manually set the GL state. The same applies to filtering.
         * Even if the texture has only one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW fallback on macos.
232 233 234 235 236 237
         */
        if(IWineD3DBaseTexture_IsCondNP2(iface)) {
            ENTER_GL();
            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
238 239 240 241 242
            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
243 244 245
            LEAVE_GL();
            This->baseTexture.states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
            This->baseTexture.states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
246 247 248
            This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_POINT;
            This->baseTexture.states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT;
            This->baseTexture.states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_NONE;
249
        }
250 251 252
    }

    return hr;
253 254
}

255
static UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
256
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
257
    TRACE("(%p)\n", This);
258

259
    return This->target;
260 261
}

262 263 264 265 266 267 268
static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
    TRACE("(%p)\n", This);

    return This->cond_np2;
}

269
static void WINAPI IWineD3DTextureImpl_ApplyStateChanges(IWineD3DTexture *iface,
270 271
                                                   const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
                                                   const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
272
    TRACE("(%p) : relay to BaseTexture\n", iface);
273
    basetexture_apply_state_changes((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
274
}
275

276 277 278
/* *******************************************
   IWineD3DTexture IWineD3DTexture parts follow
   ******************************************* */
279 280 281 282 283 284 285 286
static void WINAPI IWineD3DTextureImpl_Destroy(IWineD3DTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
    int i;

    TRACE("(%p) : Cleaning up\n",This);
    for (i = 0; i < This->baseTexture.levels; i++) {
        if (This->surfaces[i] != NULL) {
            /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
287 288
            surface_set_texture_name(This->surfaces[i], 0);
            surface_set_texture_target(This->surfaces[i], 0);
289 290 291 292 293
            IWineD3DSurface_SetContainer(This->surfaces[i], 0);
            D3DCB_DestroySurface(This->surfaces[i]);
        }
    }
    TRACE("(%p) : cleaning up base texture\n", This);
294
    basetexture_cleanup((IWineD3DBaseTexture *)iface);
295 296 297 298
    /* free the object */
    HeapFree(GetProcessHeap(), 0, This);
}

299
static HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
300 301 302 303
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;

    if (Level < This->baseTexture.levels) {
        TRACE("(%p) Level (%d)\n", This, Level);
304
        return IWineD3DSurface_GetDesc(This->surfaces[Level], pDesc);
305
    }
306
    WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
307
    return WINED3DERR_INVALIDCALL;
308 309
}

310
static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
311
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
312
    HRESULT hr = WINED3DERR_INVALIDCALL;
313 314 315

    if (Level < This->baseTexture.levels) {
        *ppSurfaceLevel = This->surfaces[Level];
316
        IWineD3DSurface_AddRef(This->surfaces[Level]);
317
        hr = WINED3D_OK;
318 319
        TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
    }
320
    if (WINED3D_OK != hr) {
321 322 323 324
        WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
        *ppSurfaceLevel = NULL; /* Just to be on the safe side.. */
    }
    return hr;
325 326
}

327
static HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, WINED3DLOCKED_RECT *pLockedRect,
328 329
                                            CONST RECT *pRect, DWORD Flags) {
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
330
    HRESULT hr = WINED3DERR_INVALIDCALL;
331 332

    if (Level < This->baseTexture.levels) {
333
        hr = IWineD3DSurface_LockRect(This->surfaces[Level], pLockedRect, pRect, Flags);
334
    }
335
    if (WINED3D_OK == hr) {
336
        TRACE("(%p) Level (%d) success\n", This, Level);
337
    } else {
338
        WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
339
    }
340

341 342 343
    return hr;
}

344
static HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
345
   IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
346
    HRESULT hr = WINED3DERR_INVALIDCALL;
347 348

    if (Level < This->baseTexture.levels) {
349 350
        hr = IWineD3DSurface_UnlockRect(This->surfaces[Level]);
    }
351
    if ( WINED3D_OK == hr) {
352
        TRACE("(%p) Level (%d) success\n", This, Level);
353
    } else {
354
        WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
355 356 357 358
    }
    return hr;
}

359
static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
360 361
    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
    This->baseTexture.dirty = TRUE;
362
    TRACE("(%p) : dirtyfication of surface Level (0)\n", This);
363
    return IWineD3DSurface_AddDirtyRect(This->surfaces[0], pDirtyRect);
364 365
}

366
const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
367
{
368
    /* IUnknown */
369 370 371
    IWineD3DTextureImpl_QueryInterface,
    IWineD3DTextureImpl_AddRef,
    IWineD3DTextureImpl_Release,
372
    /* IWineD3DResource */
373 374 375 376 377 378 379 380
    IWineD3DTextureImpl_GetParent,
    IWineD3DTextureImpl_GetDevice,
    IWineD3DTextureImpl_SetPrivateData,
    IWineD3DTextureImpl_GetPrivateData,
    IWineD3DTextureImpl_FreePrivateData,
    IWineD3DTextureImpl_SetPriority,
    IWineD3DTextureImpl_GetPriority,
    IWineD3DTextureImpl_PreLoad,
381
    IWineD3DTextureImpl_UnLoad,
382
    IWineD3DTextureImpl_GetType,
383
    /* IWineD3DBaseTexture */
384 385 386 387 388 389 390 391
    IWineD3DTextureImpl_SetLOD,
    IWineD3DTextureImpl_GetLOD,
    IWineD3DTextureImpl_GetLevelCount,
    IWineD3DTextureImpl_SetAutoGenFilterType,
    IWineD3DTextureImpl_GetAutoGenFilterType,
    IWineD3DTextureImpl_GenerateMipSubLevels,
    IWineD3DTextureImpl_SetDirty,
    IWineD3DTextureImpl_GetDirty,
392
    IWineD3DTextureImpl_BindTexture,
393
    IWineD3DTextureImpl_GetTextureDimensions,
394
    IWineD3DTextureImpl_IsCondNP2,
395
    IWineD3DTextureImpl_ApplyStateChanges,
396
    /* IWineD3DTexture */
397
    IWineD3DTextureImpl_Destroy,
398 399 400 401 402 403
    IWineD3DTextureImpl_GetLevelDesc,
    IWineD3DTextureImpl_GetSurfaceLevel,
    IWineD3DTextureImpl_LockRect,
    IWineD3DTextureImpl_UnlockRect,
    IWineD3DTextureImpl_AddDirtyRect
};