2 * IWineD3DCubeTexture implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * 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
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
30 static void cubetexture_cleanup(IWineD3DCubeTextureImpl *This, D3DCB_DESTROYSURFACEFN surface_destroy_cb)
34 TRACE("(%p) : Cleaning up.\n", This);
36 for (i = 0; i < This->baseTexture.levels; ++i)
38 for (j = 0; j < 6; ++j)
40 IWineD3DSurface *surface = This->surfaces[j][i];
44 /* Clean out the texture name we gave to the surface so that the
45 * surface doesn't try and release it. */
46 surface_set_texture_name(surface, 0, TRUE);
47 surface_set_texture_name(surface, 0, FALSE);
48 surface_set_texture_target(surface, 0);
49 IWineD3DSurface_SetContainer(surface, NULL);
50 surface_destroy_cb(surface);
54 basetexture_cleanup((IWineD3DBaseTexture *)This);
57 /* *******************************************
58 IWineD3DCubeTexture IUnknown parts follow
59 ******************************************* */
60 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
62 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
63 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
64 if (IsEqualGUID(riid, &IID_IUnknown)
65 || IsEqualGUID(riid, &IID_IWineD3DBase)
66 || IsEqualGUID(riid, &IID_IWineD3DResource)
67 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
68 || IsEqualGUID(riid, &IID_IWineD3DCubeTexture)) {
69 IUnknown_AddRef(iface);
77 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
78 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
79 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
80 return InterlockedIncrement(&This->resource.ref);
83 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
84 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
86 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
87 ref = InterlockedDecrement(&This->resource.ref);
89 IWineD3DCubeTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
94 /* ****************************************************
95 IWineD3DCubeTexture IWineD3DResource parts follow
96 **************************************************** */
97 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
98 return resource_get_device((IWineD3DResource *)iface, ppDevice);
101 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
102 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
105 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
106 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
109 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
110 return resource_free_private_data((IWineD3DResource *)iface, refguid);
113 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
114 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
117 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
118 return resource_get_priority((IWineD3DResource *)iface);
121 void cubetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb) {
122 /* Override the IWineD3DResource Preload method */
124 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
125 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
130 case SRGB_RGB: srgb_mode = FALSE; break;
131 case SRGB_BOTH: cubetexture_internal_preload(iface, SRGB_RGB);
132 case SRGB_SRGB: srgb_mode = TRUE; break;
133 /* DONTKNOW, and shut up the compiler */
134 default: srgb_mode = This->baseTexture.is_srgb; break;
136 dirty = srgb_mode ? &This->baseTexture.srgbDirty : &This->baseTexture.dirty;
138 TRACE("(%p) : About to load texture: dirtified(%d)\n", This, *dirty);
140 /* We only have to activate a context for gl when we're not drawing. In most cases PreLoad will be called during draw
141 * and a context was activated at the beginning of drawPrimitive
143 if(!device->isInDraw) {
144 /* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
145 * offscreen render targets into their texture
147 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
150 if (This->resource.format_desc->format == WINED3DFMT_P8 || This->resource.format_desc->format == WINED3DFMT_A8P8)
152 for (i = 0; i < This->baseTexture.levels; i++) {
153 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
154 if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[j][i])) {
155 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
156 /* TODO: This is not necessarily needed with hw palettized texture support */
157 IWineD3DSurface_LoadLocation(This->surfaces[j][i], SFLAG_INSYSMEM, NULL);
158 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
159 IWineD3DSurface_ModifyLocation(This->surfaces[j][i], SFLAG_INTEXTURE, FALSE);
164 /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
166 for (i = 0; i < This->baseTexture.levels; i++) {
167 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
168 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
172 TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
175 /* No longer dirty */
180 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
181 cubetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
184 static void WINAPI IWineD3DCubeTextureImpl_UnLoad(IWineD3DCubeTexture *iface) {
186 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
187 TRACE("(%p)\n", This);
189 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
190 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
193 for (i = 0; i < This->baseTexture.levels; i++) {
194 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
195 IWineD3DSurface_UnLoad(This->surfaces[j][i]);
196 surface_set_texture_name(This->surfaces[j][i], 0, TRUE);
197 surface_set_texture_name(This->surfaces[j][i], 0, FALSE);
201 basetexture_unload((IWineD3DBaseTexture *)iface);
204 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
205 return resource_get_type((IWineD3DResource *)iface);
208 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
209 return resource_get_parent((IWineD3DResource *)iface, pParent);
212 /* ******************************************************
213 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
214 ****************************************************** */
215 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
216 return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
219 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
220 return basetexture_get_lod((IWineD3DBaseTexture *)iface);
223 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
224 return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
227 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
228 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
231 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
232 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
235 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
236 basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
239 /* Internal function, No d3d mapping */
240 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
241 return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
244 /* Internal function, No d3d mapping */
245 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
246 return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
249 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface, BOOL srgb) {
250 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
251 BOOL set_gl_texture_desc;
254 TRACE("(%p) : relay to BaseTexture\n", This);
256 hr = basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &set_gl_texture_desc);
257 if (set_gl_texture_desc && SUCCEEDED(hr)) {
259 for (i = 0; i < This->baseTexture.levels; ++i) {
260 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j) {
261 if(This->baseTexture.is_srgb) {
262 surface_set_texture_name(This->surfaces[j][i], This->baseTexture.srgbTextureName, TRUE);
264 surface_set_texture_name(This->surfaces[j][i], This->baseTexture.textureName, FALSE);
273 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
274 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
275 TRACE("(%p)\n", This);
277 return GL_TEXTURE_CUBE_MAP_ARB;
280 static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface) {
281 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
282 TRACE("(%p)\n", This);
287 /* *******************************************
288 IWineD3DCubeTexture IWineD3DCubeTexture parts follow
289 ******************************************* */
290 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
291 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
293 cubetexture_cleanup(This, D3DCB_DestroySurface);
294 /* finally delete the object */
295 HeapFree(GetProcessHeap(), 0, This);
298 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
299 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
301 if (Level < This->baseTexture.levels) {
302 TRACE("(%p) level (%d)\n", This, Level);
303 return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
305 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
306 return WINED3DERR_INVALIDCALL;
309 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
310 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
311 HRESULT hr = WINED3DERR_INVALIDCALL;
313 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
314 *ppCubeMapSurface = This->surfaces[FaceType][Level];
315 IWineD3DSurface_AddRef(*ppCubeMapSurface);
319 if (WINED3D_OK == hr) {
320 TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
322 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
328 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
329 HRESULT hr = WINED3DERR_INVALIDCALL;
330 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
332 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
333 hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
336 if (WINED3D_OK == hr) {
337 TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
339 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
345 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
346 HRESULT hr = WINED3DERR_INVALIDCALL;
347 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
349 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
350 hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
353 if (WINED3D_OK == hr) {
354 TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
356 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
361 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
362 HRESULT hr = WINED3DERR_INVALIDCALL;
363 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
364 This->baseTexture.dirty = TRUE;
365 This->baseTexture.srgbDirty = TRUE;
366 TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
367 if (FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
368 surface_add_dirty_rect(This->surfaces[FaceType][0], pDirtyRect);
371 WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
377 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
380 IWineD3DCubeTextureImpl_QueryInterface,
381 IWineD3DCubeTextureImpl_AddRef,
382 IWineD3DCubeTextureImpl_Release,
383 /* IWineD3DResource */
384 IWineD3DCubeTextureImpl_GetParent,
385 IWineD3DCubeTextureImpl_GetDevice,
386 IWineD3DCubeTextureImpl_SetPrivateData,
387 IWineD3DCubeTextureImpl_GetPrivateData,
388 IWineD3DCubeTextureImpl_FreePrivateData,
389 IWineD3DCubeTextureImpl_SetPriority,
390 IWineD3DCubeTextureImpl_GetPriority,
391 IWineD3DCubeTextureImpl_PreLoad,
392 IWineD3DCubeTextureImpl_UnLoad,
393 IWineD3DCubeTextureImpl_GetType,
394 /* IWineD3DBaseTexture */
395 IWineD3DCubeTextureImpl_SetLOD,
396 IWineD3DCubeTextureImpl_GetLOD,
397 IWineD3DCubeTextureImpl_GetLevelCount,
398 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
399 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
400 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
401 IWineD3DCubeTextureImpl_SetDirty,
402 IWineD3DCubeTextureImpl_GetDirty,
403 IWineD3DCubeTextureImpl_BindTexture,
404 IWineD3DCubeTextureImpl_GetTextureDimensions,
405 IWineD3DCubeTextureImpl_IsCondNP2,
406 /* IWineD3DCubeTexture */
407 IWineD3DCubeTextureImpl_Destroy,
408 IWineD3DCubeTextureImpl_GetLevelDesc,
409 IWineD3DCubeTextureImpl_GetCubeMapSurface,
410 IWineD3DCubeTextureImpl_LockRect,
411 IWineD3DCubeTextureImpl_UnlockRect,
412 IWineD3DCubeTextureImpl_AddDirtyRect