wined3d: Remove the pixelshader fogstart/fogend optimization.
[wine] / dlls / wined3d / cubetexture.c
1 /*
2  * IWineD3DCubeTexture implementation
3  *
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
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29
30 /* *******************************************
31    IWineD3DCubeTexture IUnknown parts follow
32    ******************************************* */
33 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
34 {
35     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
36     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
37     if (IsEqualGUID(riid, &IID_IUnknown)
38         || IsEqualGUID(riid, &IID_IWineD3DBase)
39         || IsEqualGUID(riid, &IID_IWineD3DResource)
40         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
41         || IsEqualGUID(riid, &IID_IWineD3DCubeTexture)) {
42         IUnknown_AddRef(iface);
43         *ppobj = This;
44         return S_OK;
45     }
46     *ppobj = NULL;
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
51     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
52     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
53     return InterlockedIncrement(&This->resource.ref);
54 }
55
56 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
57     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
58     ULONG ref;
59     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
60     ref = InterlockedDecrement(&This->resource.ref);
61     if (ref == 0) {
62         IWineD3DCubeTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
63     }
64     return ref;
65 }
66
67 /* ****************************************************
68    IWineD3DCubeTexture IWineD3DResource parts follow
69    **************************************************** */
70 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
71     return resource_get_device((IWineD3DResource *)iface, ppDevice);
72 }
73
74 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
75     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
76 }
77
78 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
79     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
80 }
81
82 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
83     return resource_free_private_data((IWineD3DResource *)iface, refguid);
84 }
85
86 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
87     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
88 }
89
90 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
91     return resource_get_priority((IWineD3DResource *)iface);
92 }
93
94 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
95     /* Override the IWineD3DResource Preload method */
96     unsigned int i,j;
97     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
98     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
99     BOOL srgb_mode = This->baseTexture.is_srgb;
100     BOOL srgb_was_toggled = FALSE;
101
102     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
103
104     /* We only have to activate a context for gl when we're not drawing. In most cases PreLoad will be called during draw
105      * and a context was activated at the beginning of drawPrimitive
106      */
107     if(!device->isInDraw) {
108         /* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
109          * offscreen render targets into their texture
110          */
111         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
112     } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
113         srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
114         srgb_was_toggled = (This->baseTexture.is_srgb != srgb_mode);
115         This->baseTexture.is_srgb = srgb_mode;
116     }
117
118     if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
119         for (i = 0; i < This->baseTexture.levels; i++) {
120             for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
121                 if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[j][i])) {
122                     TRACE("Reloading surface because the d3d8/9 palette was changed\n");
123                     /* TODO: This is not necessarily needed with hw palettized texture support */
124                     IWineD3DSurface_LoadLocation(This->surfaces[j][i], SFLAG_INSYSMEM, NULL);
125                     /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
126                     IWineD3DSurface_ModifyLocation(This->surfaces[j][i], SFLAG_INTEXTURE, FALSE);
127                 }
128             }
129         }
130     }
131     /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
132     if (This->baseTexture.dirty) {
133         for (i = 0; i < This->baseTexture.levels; i++) {
134             for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
135                 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
136             }
137         }
138     } else if (srgb_was_toggled) {
139         /* Loop is repeated in the else block with the extra AddDirtyRect line to avoid the alternative of
140          * checking srgb_was_toggled in every iteration, even when the texture is just dirty
141          */
142         if (This->baseTexture.srgb_mode_change_count < 20)
143             ++This->baseTexture.srgb_mode_change_count;
144         else
145             FIXME("Cubetexture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
146
147         for (i = 0; i < This->baseTexture.levels; i++) {
148             for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
149                 IWineD3DSurface_AddDirtyRect(This->surfaces[j][i], NULL);
150                 surface_force_reload(This->surfaces[j][i]);
151                 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
152             }
153         }
154     } else {
155         TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
156     }
157
158     /* No longer dirty */
159     This->baseTexture.dirty = FALSE;
160     return ;
161 }
162
163 static void WINAPI IWineD3DCubeTextureImpl_UnLoad(IWineD3DCubeTexture *iface) {
164     unsigned int i, j;
165     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
166     TRACE("(%p)\n", This);
167
168     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
169      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
170      * surface is fine
171      */
172     for (i = 0; i < This->baseTexture.levels; i++) {
173         for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
174             IWineD3DSurface_UnLoad(This->surfaces[j][i]);
175             surface_set_texture_name(This->surfaces[j][i], 0);
176         }
177     }
178
179     basetexture_unload((IWineD3DBaseTexture *)iface);
180 }
181
182 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
183     return resource_get_type((IWineD3DResource *)iface);
184 }
185
186 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
187     return resource_get_parent((IWineD3DResource *)iface, pParent);
188 }
189
190 /* ******************************************************
191    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
192    ****************************************************** */
193 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
194     return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
195 }
196
197 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
198     return basetexture_get_lod((IWineD3DBaseTexture *)iface);
199 }
200
201 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
202     return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
203 }
204
205 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
206   return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
207 }
208
209 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
210   return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
211 }
212
213 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
214     basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
215 }
216
217 /* Internal function, No d3d mapping */
218 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
219     return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
220 }
221
222 /* Internal function, No d3d mapping */
223 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
224     return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
225 }
226
227 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
228     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
229     BOOL set_gl_texture_desc = This->baseTexture.textureName == 0;
230     HRESULT hr;
231
232     TRACE("(%p) : relay to BaseTexture\n", This);
233
234     hr = basetexture_bind((IWineD3DBaseTexture *)iface);
235     if (set_gl_texture_desc && SUCCEEDED(hr)) {
236         UINT i, j;
237         for (i = 0; i < This->baseTexture.levels; ++i) {
238             for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j) {
239                 surface_set_texture_name(This->surfaces[j][i], This->baseTexture.textureName);
240             }
241         }
242     }
243
244     return hr;
245 }
246
247 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
248     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
249     TRACE("(%p)\n", This);
250
251     return GL_TEXTURE_CUBE_MAP_ARB;
252 }
253
254 static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface) {
255     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
256     TRACE("(%p)\n", This);
257
258     return FALSE;
259 }
260
261 static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface, 
262                                                         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], 
263                                                         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
264     TRACE("(%p) : relay to BaseTexture\n", iface);
265     basetexture_apply_state_changes((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
266 }
267
268
269 /* *******************************************
270    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
271    ******************************************* */
272 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
273     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
274     unsigned int i,j;
275     TRACE("(%p) : Cleaning up\n",This);
276     for (i = 0; i < This->baseTexture.levels; i++) {
277         for (j = 0; j < 6; j++) {
278             if (This->surfaces[j][i] != NULL) {
279                 IWineD3DSurface *surface = This->surfaces[j][i];
280                 /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
281                 surface_set_texture_name(surface, 0);
282                 surface_set_texture_target(surface, 0);
283                 /* Cleanup the container */
284                 IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
285                 D3DCB_DestroySurface(This->surfaces[j][i]);
286             }
287         }
288     }
289     basetexture_cleanup((IWineD3DBaseTexture *)iface);
290     /* finally delete the object */
291     HeapFree(GetProcessHeap(), 0, This);
292 }
293
294 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
295     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
296
297     if (Level < This->baseTexture.levels) {
298         TRACE("(%p) level (%d)\n", This, Level);
299         return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
300     }
301     WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
302     return WINED3DERR_INVALIDCALL;
303 }
304
305 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
306     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
307     HRESULT hr = WINED3DERR_INVALIDCALL;
308
309     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
310         *ppCubeMapSurface = This->surfaces[FaceType][Level];
311         IWineD3DSurface_AddRef(*ppCubeMapSurface);
312
313         hr = WINED3D_OK;
314     }
315     if (WINED3D_OK == hr) {
316         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
317     } else {
318         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
319     }
320
321     return hr;
322 }
323
324 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
325     HRESULT hr = WINED3DERR_INVALIDCALL;
326     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
327
328     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
329         hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
330     }
331
332     if (WINED3D_OK == hr) {
333         TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
334     } else {
335         WARN("(%p) level(%d) overflow Levels(%d)  Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
336     }
337
338     return hr;
339 }
340
341 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
342     HRESULT hr = WINED3DERR_INVALIDCALL;
343     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
344
345     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
346         hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
347     }
348
349     if (WINED3D_OK == hr) {
350         TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
351     } else {
352         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
353     }
354     return hr;
355 }
356
357 static HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
358     HRESULT hr = WINED3DERR_INVALIDCALL;
359     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
360     This->baseTexture.dirty = TRUE;
361     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
362     if (FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
363         hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
364     } else {
365         WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
366     }
367     return hr;
368 }
369
370
371 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
372 {
373     /* IUnknown */
374     IWineD3DCubeTextureImpl_QueryInterface,
375     IWineD3DCubeTextureImpl_AddRef,
376     IWineD3DCubeTextureImpl_Release,
377     /* IWineD3DResource */
378     IWineD3DCubeTextureImpl_GetParent,
379     IWineD3DCubeTextureImpl_GetDevice,
380     IWineD3DCubeTextureImpl_SetPrivateData,
381     IWineD3DCubeTextureImpl_GetPrivateData,
382     IWineD3DCubeTextureImpl_FreePrivateData,
383     IWineD3DCubeTextureImpl_SetPriority,
384     IWineD3DCubeTextureImpl_GetPriority,
385     IWineD3DCubeTextureImpl_PreLoad,
386     IWineD3DCubeTextureImpl_UnLoad,
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_GetTextureDimensions,
399     IWineD3DCubeTextureImpl_IsCondNP2,
400     IWineD3DCubeTextureImpl_ApplyStateChanges,
401     /* IWineD3DCubeTexture */
402     IWineD3DCubeTextureImpl_Destroy,
403     IWineD3DCubeTextureImpl_GetLevelDesc,
404     IWineD3DCubeTextureImpl_GetCubeMapSurface,
405     IWineD3DCubeTextureImpl_LockRect,
406     IWineD3DCubeTextureImpl_UnlockRect,
407     IWineD3DCubeTextureImpl_AddDirtyRect
408 };