2 * IWineD3DCubeTexture implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * 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
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29 static const GLenum cube_targets[6] = {
30 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
31 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
32 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
33 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
34 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
35 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
38 /* *******************************************
39 IWineD3DCubeTexture IUnknown parts follow
40 ******************************************* */
41 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
43 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
44 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
45 if (IsEqualGUID(riid, &IID_IUnknown)
46 || IsEqualGUID(riid, &IID_IWineD3DBase)
47 || IsEqualGUID(riid, &IID_IWineD3DResource)
48 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
49 || IsEqualGUID(riid, &IID_IWineD3DTexture)) {
50 IUnknown_AddRef(iface);
58 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
59 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
60 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
61 return InterlockedIncrement(&This->resource.ref);
64 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
65 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
67 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
68 ref = InterlockedDecrement(&This->resource.ref);
70 IWineD3DCubeTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
75 /* ****************************************************
76 IWineD3DCubeTexture IWineD3DResource parts follow
77 **************************************************** */
78 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
79 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
82 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
83 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
86 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
87 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
90 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
91 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
94 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
95 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
98 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
99 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
102 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
103 /* Override the IWineD3DResource Preload method */
105 BOOL setGlTextureDesc = FALSE;
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 if (This->baseTexture.textureName == 0) setGlTextureDesc = TRUE;
115 /* We only have to activate a context for gl when we're not drawing. In most cases PreLoad will be called during draw
116 * and a context was activated at the beginning of drawPrimitive
118 if(!device->isInDraw) {
119 /* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
120 * offscreen render targets into their texture
123 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
125 } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
126 srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
127 srgb_was_toggled = (This->baseTexture.is_srgb != srgb_mode);
128 This->baseTexture.is_srgb = srgb_mode;
130 IWineD3DCubeTexture_BindTexture(iface);
133 /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
134 if (This->baseTexture.dirty) {
135 for (i = 0; i < This->baseTexture.levels; i++) {
136 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
138 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
139 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
142 } else if (srgb_was_toggled) {
143 /* Loop is repeated in the else block with the extra AddDirtyRect line to avoid the alternative of
144 * checking srgb_was_toggled in every iteration, even when the texture is just dirty
146 if (This->baseTexture.srgb_mode_change_count < 20)
147 ++This->baseTexture.srgb_mode_change_count;
149 FIXME("Cubetexture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
151 for (i = 0; i < This->baseTexture.levels; i++) {
152 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
153 IWineD3DSurfaceImpl_AddDirtyRect(This->surfaces[j][i], NULL);
154 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
155 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
159 TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
163 /* No longer dirty */
164 This->baseTexture.dirty = FALSE;
168 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
169 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
172 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
173 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
176 /* ******************************************************
177 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
178 ****************************************************** */
179 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
180 return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
183 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
184 return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
187 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
188 return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
191 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
192 return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
195 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
196 return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
199 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
200 IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
203 /* Internal function, No d3d mapping */
204 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
205 return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
208 /* Internal function, No d3d mapping */
209 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
210 return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
213 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
214 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
215 TRACE("(%p) : relay to BaseTexture\n", This);
216 return IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
219 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnBindTexture(IWineD3DCubeTexture *iface) {
220 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
221 TRACE("(%p) : relay to BaseTexture\n", This);
222 return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
225 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
226 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
227 TRACE("(%p)\n", This);
229 return GL_TEXTURE_CUBE_MAP_ARB;
232 static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface,
233 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
234 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
235 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
237 IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
240 /* Apply non-power2 mappings and texture offsets so long as the texture coords aren't projected or generated */
241 if(This->pow2scalingFactor != 1.0f) {
242 if((textureStates[WINED3DTSS_TEXCOORDINDEX] & 0xFFFF0000) == WINED3DTSS_TCI_PASSTHRU &&
243 (~textureStates[WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED)) {
245 glMatrixMode(GL_TEXTURE);
246 memset(matrix, 0 , sizeof(matrix));
248 matrix[0] = This->pow2scalingFactor;
249 matrix[5] = This->pow2scalingFactor;
250 matrix[10] = This->pow2scalingFactor;
251 #if 0 /* Translation fixup is no longer required (here for reminder) */
252 matrix[12] = -0.25f / (float)This->edgeLength;
253 matrix[13] = -0.75f / (float)This->edgeLength;
254 matrix[14] = -0.25f / (float)This->edgeLength;
256 TRACE("(%p) Setup Matrix:\n", This);
257 TRACE(" %f %f %f %f\n", matrix[0], matrix[1], matrix[2], matrix[3]);
258 TRACE(" %f %f %f %f\n", matrix[4], matrix[5], matrix[6], matrix[7]);
259 TRACE(" %f %f %f %f\n", matrix[8], matrix[9], matrix[10], matrix[11]);
260 TRACE(" %f %f %f %f\n", matrix[12], matrix[13], matrix[14], matrix[15]);
262 glMultMatrixf(matrix);
264 /* I don't expect nonpower 2 textures to be used with generated texture coordinates, but if they are present a fixme. */
265 FIXME("Non-power2 texture being used with generated texture coords\n");
272 /* *******************************************
273 IWineD3DCubeTexture IWineD3DCubeTexture parts follow
274 ******************************************* */
275 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
276 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
278 TRACE("(%p) : Cleaning up\n",This);
279 for (i = 0; i < This->baseTexture.levels; i++) {
280 for (j = 0; j < 6; j++) {
281 if (This->surfaces[j][i] != NULL) {
282 /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
283 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, 0);
284 /* Cleanup the container */
285 IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
286 D3DCB_DestroySurface(This->surfaces[j][i]);
290 IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
291 /* finally delete the object */
292 HeapFree(GetProcessHeap(), 0, This);
295 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
296 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
298 if (Level < This->baseTexture.levels) {
299 TRACE("(%p) level (%d)\n", This, Level);
300 return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
302 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
303 return WINED3DERR_INVALIDCALL;
306 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
307 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
308 HRESULT hr = WINED3DERR_INVALIDCALL;
310 if (Level < This->baseTexture.levels && FaceType >= WINED3DCUBEMAP_FACE_POSITIVE_X && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
311 *ppCubeMapSurface = This->surfaces[FaceType][Level];
312 IWineD3DSurface_AddRef(*ppCubeMapSurface);
316 if (WINED3D_OK == hr) {
317 TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
319 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
325 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
326 HRESULT hr = WINED3DERR_INVALIDCALL;
327 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
329 if (Level < This->baseTexture.levels && FaceType >= WINED3DCUBEMAP_FACE_POSITIVE_X && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
330 hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
333 if (WINED3D_OK == hr) {
334 TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
336 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
342 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
343 HRESULT hr = WINED3DERR_INVALIDCALL;
344 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
346 if (Level < This->baseTexture.levels && FaceType >= WINED3DCUBEMAP_FACE_POSITIVE_X && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
347 hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
350 if (WINED3D_OK == hr) {
351 TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
353 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
358 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
359 HRESULT hr = WINED3DERR_INVALIDCALL;
360 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
361 This->baseTexture.dirty = TRUE;
362 TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
363 if (FaceType >= WINED3DCUBEMAP_FACE_POSITIVE_X && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
364 hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
366 WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
372 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
375 IWineD3DCubeTextureImpl_QueryInterface,
376 IWineD3DCubeTextureImpl_AddRef,
377 IWineD3DCubeTextureImpl_Release,
378 /* IWineD3DResource */
379 IWineD3DCubeTextureImpl_GetParent,
380 IWineD3DCubeTextureImpl_GetDevice,
381 IWineD3DCubeTextureImpl_SetPrivateData,
382 IWineD3DCubeTextureImpl_GetPrivateData,
383 IWineD3DCubeTextureImpl_FreePrivateData,
384 IWineD3DCubeTextureImpl_SetPriority,
385 IWineD3DCubeTextureImpl_GetPriority,
386 IWineD3DCubeTextureImpl_PreLoad,
387 IWineD3DCubeTextureImpl_GetType,
388 /* IWineD3DBaseTexture */
389 IWineD3DCubeTextureImpl_SetLOD,
390 IWineD3DCubeTextureImpl_GetLOD,
391 IWineD3DCubeTextureImpl_GetLevelCount,
392 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
393 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
394 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
395 IWineD3DCubeTextureImpl_SetDirty,
396 IWineD3DCubeTextureImpl_GetDirty,
397 IWineD3DCubeTextureImpl_BindTexture,
398 IWineD3DCubeTextureImpl_UnBindTexture,
399 IWineD3DCubeTextureImpl_GetTextureDimensions,
400 IWineD3DCubeTextureImpl_ApplyStateChanges,
401 /* IWineD3DCubeTexture */
402 IWineD3DCubeTextureImpl_Destroy,
403 IWineD3DCubeTextureImpl_GetLevelDesc,
404 IWineD3DCubeTextureImpl_GetCubeMapSurface,
405 IWineD3DCubeTextureImpl_LockRect,
406 IWineD3DCubeTextureImpl_UnlockRect,
407 IWineD3DCubeTextureImpl_AddDirtyRect