inetcomm: Implement IMimeInternational_CanConvertCodePages.
[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);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29
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
37 };
38
39 /* *******************************************
40    IWineD3DCubeTexture IUnknown parts follow
41    ******************************************* */
42 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
43 {
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);
52         *ppobj = This;
53         return S_OK;
54     }
55     *ppobj = NULL;
56     return E_NOINTERFACE;
57 }
58
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);
63 }
64
65 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
66     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
67     ULONG ref;
68     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
69     ref = InterlockedDecrement(&This->resource.ref);
70     if (ref == 0) {
71         IWineD3DCubeTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
72     }
73     return ref;
74 }
75
76 /* ****************************************************
77    IWineD3DCubeTexture IWineD3DResource parts follow
78    **************************************************** */
79 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
80     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
81 }
82
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);
85 }
86
87 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
88     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
89 }
90
91 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
92     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
93 }
94
95 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
96     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
97 }
98
99 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
100     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
101 }
102
103 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
104     /* Override the IWineD3DResource Preload method */
105     unsigned int i,j;
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;
110
111     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
112
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
115      */
116     if(!device->isInDraw) {
117         /* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
118          * offscreen render targets into their texture
119          */
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;
125     }
126     IWineD3DCubeTexture_BindTexture(iface);
127
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);
137                 }
138             }
139         }
140     }
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);
146             }
147         }
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
151          */
152         if (This->baseTexture.srgb_mode_change_count < 20)
153             ++This->baseTexture.srgb_mode_change_count;
154         else
155             FIXME("Cubetexture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
156
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);
162             }
163         }
164     } else {
165         TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
166     }
167
168     /* No longer dirty */
169     This->baseTexture.dirty = FALSE;
170     return ;
171 }
172
173 static void WINAPI IWineD3DCubeTextureImpl_UnLoad(IWineD3DCubeTexture *iface) {
174     unsigned int i, j;
175     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
176     TRACE("(%p)\n", This);
177
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
180      * surface is fine
181      */
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));
186         }
187     }
188
189     IWineD3DBaseTextureImpl_UnLoad((IWineD3DBaseTexture *) iface);
190 }
191
192 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
193     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
194 }
195
196 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
197     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
198 }
199
200 /* ******************************************************
201    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
202    ****************************************************** */
203 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
204     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
205 }
206
207 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
208     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
209 }
210
211 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
212     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
213 }
214
215 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
216   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
217 }
218
219 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
220   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
221 }
222
223 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
224     IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
225 }
226
227 /* Internal function, No d3d mapping */
228 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
229     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
230 }
231
232 /* Internal function, No d3d mapping */
233 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
234     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
235 }
236
237 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
238     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
239     BOOL set_gl_texture_desc = This->baseTexture.textureName == 0;
240     HRESULT hr;
241
242     TRACE("(%p) : relay to BaseTexture\n", This);
243
244     hr = IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
245     if (set_gl_texture_desc && SUCCEEDED(hr)) {
246         UINT i, j;
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]);
250             }
251         }
252     }
253
254     return hr;
255 }
256
257 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
258     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
259     TRACE("(%p)\n", This);
260
261     return GL_TEXTURE_CUBE_MAP_ARB;
262 }
263
264 static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface) {
265     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
266     TRACE("(%p)\n", This);
267
268     return FALSE;
269 }
270
271 static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface, 
272                                                         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], 
273                                                         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
274     TRACE("(%p) : relay to BaseTexture\n", iface);
275     IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
276 }
277
278
279 /* *******************************************
280    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
281    ******************************************* */
282 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
283     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
284     unsigned int i,j;
285     TRACE("(%p) : Cleaning up\n",This);
286     for (i = 0; i < This->baseTexture.levels; i++) {
287         for (j = 0; j < 6; j++) {
288             if (This->surfaces[j][i] != NULL) {
289                 /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
290                 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, 0);
291                 /* Cleanup the container */
292                 IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
293                 D3DCB_DestroySurface(This->surfaces[j][i]);
294             }
295         }
296     }
297     IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
298     /* finally delete the object */
299     HeapFree(GetProcessHeap(), 0, This);
300 }
301
302 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
303     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
304
305     if (Level < This->baseTexture.levels) {
306         TRACE("(%p) level (%d)\n", This, Level);
307         return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
308     }
309     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
310     return WINED3DERR_INVALIDCALL;
311 }
312
313 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
314     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
315     HRESULT hr = WINED3DERR_INVALIDCALL;
316
317     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
318         *ppCubeMapSurface = This->surfaces[FaceType][Level];
319         IWineD3DSurface_AddRef(*ppCubeMapSurface);
320
321         hr = WINED3D_OK;
322     }
323     if (WINED3D_OK == hr) {
324         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
325     } else {
326         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
327     }
328
329     return hr;
330 }
331
332 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
333     HRESULT hr = WINED3DERR_INVALIDCALL;
334     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
335
336     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
337         hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
338     }
339
340     if (WINED3D_OK == hr) {
341         TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
342     } else {
343         WARN("(%p) level(%d) overflow Levels(%d)  Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
344     }
345
346     return hr;
347 }
348
349 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
350     HRESULT hr = WINED3DERR_INVALIDCALL;
351     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
352
353     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
354         hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
355     }
356
357     if (WINED3D_OK == hr) {
358         TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
359     } else {
360         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
361     }
362     return hr;
363 }
364
365 static HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
366     HRESULT hr = WINED3DERR_INVALIDCALL;
367     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
368     This->baseTexture.dirty = TRUE;
369     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
370     if (FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
371         hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
372     } else {
373         WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
374     }
375     return hr;
376 }
377
378
379 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
380 {
381     /* IUnknown */
382     IWineD3DCubeTextureImpl_QueryInterface,
383     IWineD3DCubeTextureImpl_AddRef,
384     IWineD3DCubeTextureImpl_Release,
385     /* IWineD3DResource */
386     IWineD3DCubeTextureImpl_GetParent,
387     IWineD3DCubeTextureImpl_GetDevice,
388     IWineD3DCubeTextureImpl_SetPrivateData,
389     IWineD3DCubeTextureImpl_GetPrivateData,
390     IWineD3DCubeTextureImpl_FreePrivateData,
391     IWineD3DCubeTextureImpl_SetPriority,
392     IWineD3DCubeTextureImpl_GetPriority,
393     IWineD3DCubeTextureImpl_PreLoad,
394     IWineD3DCubeTextureImpl_UnLoad,
395     IWineD3DCubeTextureImpl_GetType,
396     /* IWineD3DBaseTexture */
397     IWineD3DCubeTextureImpl_SetLOD,
398     IWineD3DCubeTextureImpl_GetLOD,
399     IWineD3DCubeTextureImpl_GetLevelCount,
400     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
401     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
402     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
403     IWineD3DCubeTextureImpl_SetDirty,
404     IWineD3DCubeTextureImpl_GetDirty,
405     IWineD3DCubeTextureImpl_BindTexture,
406     IWineD3DCubeTextureImpl_GetTextureDimensions,
407     IWineD3DCubeTextureImpl_IsCondNP2,
408     IWineD3DCubeTextureImpl_ApplyStateChanges,
409     /* IWineD3DCubeTexture */
410     IWineD3DCubeTextureImpl_Destroy,
411     IWineD3DCubeTextureImpl_GetLevelDesc,
412     IWineD3DCubeTextureImpl_GetCubeMapSurface,
413     IWineD3DCubeTextureImpl_LockRect,
414     IWineD3DCubeTextureImpl_UnlockRect,
415     IWineD3DCubeTextureImpl_AddDirtyRect
416 };