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);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
30 static const GLenum cube_targets[6] = {
31 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
32 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
33 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
34 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
35 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
36 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
39 /* *******************************************
40 IWineD3DCubeTexture IUnknown parts follow
41 ******************************************* */
42 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
44 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
45 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
46 if (IsEqualGUID(riid, &IID_IUnknown)
47 || IsEqualGUID(riid, &IID_IWineD3DBase)
48 || IsEqualGUID(riid, &IID_IWineD3DResource)
49 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
50 || IsEqualGUID(riid, &IID_IWineD3DTexture)) {
51 IUnknown_AddRef(iface);
59 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
60 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
61 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
62 return InterlockedIncrement(&This->resource.ref);
65 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
66 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
68 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
69 ref = InterlockedDecrement(&This->resource.ref);
71 IWineD3DCubeTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
76 /* ****************************************************
77 IWineD3DCubeTexture IWineD3DResource parts follow
78 **************************************************** */
79 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
80 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
83 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
84 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
87 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
88 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
91 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
92 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
95 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
96 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
99 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
100 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
103 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
104 /* Override the IWineD3DResource Preload method */
106 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
107 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
108 BOOL srgb_mode = This->baseTexture.is_srgb;
109 BOOL srgb_was_toggled = FALSE;
111 TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
113 /* We only have to activate a context for gl when we're not drawing. In most cases PreLoad will be called during draw
114 * and a context was activated at the beginning of drawPrimitive
116 if(!device->isInDraw) {
117 /* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
118 * offscreen render targets into their texture
120 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
121 } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
122 srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
123 srgb_was_toggled = (This->baseTexture.is_srgb != srgb_mode);
124 This->baseTexture.is_srgb = srgb_mode;
126 IWineD3DCubeTexture_BindTexture(iface);
128 if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
129 for (i = 0; i < This->baseTexture.levels; i++) {
130 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
131 if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[j][i])) {
132 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
133 /* TODO: This is not necessarily needed with hw palettized texture support */
134 IWineD3DSurface_LoadLocation(This->surfaces[j][i], SFLAG_INSYSMEM, NULL);
135 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
136 IWineD3DSurface_ModifyLocation(This->surfaces[j][i], SFLAG_INTEXTURE, FALSE);
141 /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
142 if (This->baseTexture.dirty) {
143 for (i = 0; i < This->baseTexture.levels; i++) {
144 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
145 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
148 } else if (srgb_was_toggled) {
149 /* Loop is repeated in the else block with the extra AddDirtyRect line to avoid the alternative of
150 * checking srgb_was_toggled in every iteration, even when the texture is just dirty
152 if (This->baseTexture.srgb_mode_change_count < 20)
153 ++This->baseTexture.srgb_mode_change_count;
155 FIXME("Cubetexture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
157 for (i = 0; i < This->baseTexture.levels; i++) {
158 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
159 IWineD3DSurface_AddDirtyRect(This->surfaces[j][i], NULL);
160 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
161 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
165 TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
168 /* No longer dirty */
169 This->baseTexture.dirty = FALSE;
173 static void WINAPI IWineD3DCubeTextureImpl_UnLoad(IWineD3DCubeTexture *iface) {
175 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
176 TRACE("(%p)\n", This);
178 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
179 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
182 for (i = 0; i < This->baseTexture.levels; i++) {
183 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
184 IWineD3DSurface_UnLoad(This->surfaces[j][i]);
185 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, IWineD3DTexture_GetTextureDimensions(iface));
189 IWineD3DBaseTextureImpl_UnLoad((IWineD3DBaseTexture *) iface);
192 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
193 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
196 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
197 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
200 /* ******************************************************
201 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
202 ****************************************************** */
203 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
204 return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
207 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
208 return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
211 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
212 return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
215 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
216 return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
219 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
220 return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
223 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
224 IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
227 /* Internal function, No d3d mapping */
228 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
229 return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
232 /* Internal function, No d3d mapping */
233 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
234 return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
237 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
238 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
239 BOOL set_gl_texture_desc = This->baseTexture.textureName == 0;
242 TRACE("(%p) : relay to BaseTexture\n", This);
244 hr = IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
245 if (set_gl_texture_desc && SUCCEEDED(hr)) {
247 for (i = 0; i < This->baseTexture.levels; ++i) {
248 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j) {
249 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
257 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnBindTexture(IWineD3DCubeTexture *iface) {
258 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
259 TRACE("(%p) : relay to BaseTexture\n", This);
260 return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
263 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
264 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
265 TRACE("(%p)\n", This);
267 return GL_TEXTURE_CUBE_MAP_ARB;
270 static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface,
271 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
272 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
273 TRACE("(%p) : relay to BaseTexture\n", iface);
274 IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
278 /* *******************************************
279 IWineD3DCubeTexture IWineD3DCubeTexture parts follow
280 ******************************************* */
281 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
282 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
284 TRACE("(%p) : Cleaning up\n",This);
285 for (i = 0; i < This->baseTexture.levels; i++) {
286 for (j = 0; j < 6; j++) {
287 if (This->surfaces[j][i] != NULL) {
288 /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
289 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, 0);
290 /* Cleanup the container */
291 IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
292 D3DCB_DestroySurface(This->surfaces[j][i]);
296 IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
297 /* finally delete the object */
298 HeapFree(GetProcessHeap(), 0, This);
301 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
302 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
304 if (Level < This->baseTexture.levels) {
305 TRACE("(%p) level (%d)\n", This, Level);
306 return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
308 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
309 return WINED3DERR_INVALIDCALL;
312 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
313 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
314 HRESULT hr = WINED3DERR_INVALIDCALL;
316 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
317 *ppCubeMapSurface = This->surfaces[FaceType][Level];
318 IWineD3DSurface_AddRef(*ppCubeMapSurface);
322 if (WINED3D_OK == hr) {
323 TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
325 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
331 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
332 HRESULT hr = WINED3DERR_INVALIDCALL;
333 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
335 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
336 hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
339 if (WINED3D_OK == hr) {
340 TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
342 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
348 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
349 HRESULT hr = WINED3DERR_INVALIDCALL;
350 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
352 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
353 hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
356 if (WINED3D_OK == hr) {
357 TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
359 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
364 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
365 HRESULT hr = WINED3DERR_INVALIDCALL;
366 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
367 This->baseTexture.dirty = TRUE;
368 TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
369 if (FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
370 hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
372 WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
378 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
381 IWineD3DCubeTextureImpl_QueryInterface,
382 IWineD3DCubeTextureImpl_AddRef,
383 IWineD3DCubeTextureImpl_Release,
384 /* IWineD3DResource */
385 IWineD3DCubeTextureImpl_GetParent,
386 IWineD3DCubeTextureImpl_GetDevice,
387 IWineD3DCubeTextureImpl_SetPrivateData,
388 IWineD3DCubeTextureImpl_GetPrivateData,
389 IWineD3DCubeTextureImpl_FreePrivateData,
390 IWineD3DCubeTextureImpl_SetPriority,
391 IWineD3DCubeTextureImpl_GetPriority,
392 IWineD3DCubeTextureImpl_PreLoad,
393 IWineD3DCubeTextureImpl_UnLoad,
394 IWineD3DCubeTextureImpl_GetType,
395 /* IWineD3DBaseTexture */
396 IWineD3DCubeTextureImpl_SetLOD,
397 IWineD3DCubeTextureImpl_GetLOD,
398 IWineD3DCubeTextureImpl_GetLevelCount,
399 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
400 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
401 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
402 IWineD3DCubeTextureImpl_SetDirty,
403 IWineD3DCubeTextureImpl_GetDirty,
404 IWineD3DCubeTextureImpl_BindTexture,
405 IWineD3DCubeTextureImpl_UnBindTexture,
406 IWineD3DCubeTextureImpl_GetTextureDimensions,
407 IWineD3DCubeTextureImpl_ApplyStateChanges,
408 /* IWineD3DCubeTexture */
409 IWineD3DCubeTextureImpl_Destroy,
410 IWineD3DCubeTextureImpl_GetLevelDesc,
411 IWineD3DCubeTextureImpl_GetCubeMapSurface,
412 IWineD3DCubeTextureImpl_LockRect,
413 IWineD3DCubeTextureImpl_UnlockRect,
414 IWineD3DCubeTextureImpl_AddDirtyRect