Accept (shorter) date format in Win NT and Win 95.
[wine] / dlls / wined3d / texture.c
1 /*
2  * IDirect3DTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wined3d_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
26 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
27
28 /* *******************************************
29    IWineD3DTexture IUnknown parts follow
30    ******************************************* */
31 HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
32 {
33     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
34     WARN("(%p)->(%s,%p) should not be called\n",This,debugstr_guid(riid),ppobj);
35     return E_NOINTERFACE;
36 }
37
38 ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
39     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
40     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
41     IUnknown_AddRef(This->resource.parent);
42     return InterlockedIncrement(&This->resource.ref);
43 }
44
45 ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
46     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
47     ULONG ref;
48     TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
49     ref = InterlockedDecrement(&This->resource.ref);
50     if (ref == 0) {
51         int i;
52         for (i = 0; i < This->baseTexture.levels; i++) {
53             if (This->surfaces[i] != NULL) {
54                 TRACE("(%p) : Releasing surface %p\n", This, This->surfaces[i]);
55                 IWineD3DSurface_Release((IWineD3DSurface *) This->surfaces[i]);
56             }
57         }
58         IWineD3DDevice_Release((IWineD3DDevice *)This->resource.wineD3DDevice);
59         HeapFree(GetProcessHeap(), 0, This);
60     } else {
61         IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
62     }
63     return ref;
64 }
65
66 /* ****************************************************
67    IWineD3DTexture IWineD3DResource parts follow
68    **************************************************** */
69 HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
70     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
71 }
72
73 HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
74     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
75 }
76
77 HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
78     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
79 }
80
81 HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
82     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
83 }
84
85 DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
86     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
87 }
88
89 DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
90     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
91 }
92
93 void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
94     /* Override the IWineD3DResource Preload method */
95     unsigned int i;
96     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
97     
98     TRACE("(%p) : About to load texture\n", This);
99
100     ENTER_GL();
101
102     for (i = 0; i < This->baseTexture.levels; i++) {
103       if (i == 0 && This->surfaces[i]->textureName != 0 && This->baseTexture.dirty == FALSE) {
104         glBindTexture(GL_TEXTURE_2D, This->surfaces[i]->textureName);
105         checkGLcall("glBindTexture");
106         TRACE("Texture %p (level %d) given name %d\n", This->surfaces[i], i, This->surfaces[i]->textureName);
107         /* No need to walk through all mip-map levels, since already all assigned */
108         i = This->baseTexture.levels;
109
110       } else {
111         if (i == 0) {
112           if (This->surfaces[i]->textureName == 0) {
113             glGenTextures(1, &This->surfaces[i]->textureName);
114             checkGLcall("glGenTextures");
115             TRACE("Texture %p (level %d) given name %d\n", This->surfaces[i], i, This->surfaces[i]->textureName);
116           }
117           
118           glBindTexture(GL_TEXTURE_2D, This->surfaces[i]->textureName);
119           checkGLcall("glBindTexture");
120         }
121         IWineD3DSurface_LoadTexture((IWineD3DSurface *) This->surfaces[i], GL_TEXTURE_2D, i); 
122       }
123     }
124
125     /* No longer dirty */
126     This->baseTexture.dirty = FALSE;
127
128     /* Always need to reset the number of mipmap levels when rebinding as it is
129        a property of the active texture unit, and another texture may have set it
130        to a different value                                                       */
131     TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);   
132     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
133     checkGLcall("glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->levels)");
134
135     LEAVE_GL();
136
137     return ;
138 }
139
140 D3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
141     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
142 }
143
144 HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
145     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
146 }
147
148 /* ******************************************************
149    IWineD3DTexture IWineD3DBaseTexture parts follow
150    ****************************************************** */
151 DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
152     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
153 }
154
155 DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
156     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
157 }
158
159 DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
160     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
161 }
162
163 HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
164   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
165 }
166
167 D3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
168   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
169 }
170
171 void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
172   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
173 }
174
175 /* Internal function, No d3d mapping */
176 BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
177     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, TRUE);
178 }
179
180 BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
181     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
182 }
183
184 /* *******************************************
185    IWineD3DTexture IWineD3DTexture parts follow
186    ******************************************* */
187 HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
188     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
189
190     if (Level < This->baseTexture.levels) {
191         TRACE("(%p) Level (%d)\n", This, Level);
192         return IWineD3DSurface_GetDesc((IWineD3DSurface *) This->surfaces[Level], pDesc);
193     }
194     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
195     return D3DERR_INVALIDCALL;
196 }
197
198 HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
199     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
200     *ppSurfaceLevel = (IWineD3DSurface *) This->surfaces[Level];
201     IWineD3DSurface_AddRef((IWineD3DSurface *) This->surfaces[Level]);
202     TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
203     return D3D_OK;
204 }
205
206 HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, D3DLOCKED_RECT *pLockedRect, 
207                                             CONST RECT *pRect, DWORD Flags) {
208     HRESULT hr;
209     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
210
211     if (Level < This->baseTexture.levels) {
212
213         hr = IWineD3DSurface_LockRect((IWineD3DSurface *) This->surfaces[Level], pLockedRect, pRect, Flags);
214         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
215     } else {
216         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
217         return D3DERR_INVALIDCALL;
218     }
219     return hr;
220 }
221
222 HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
223     HRESULT hr;
224     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
225
226     if (Level < This->baseTexture.levels) {
227         hr = IWineD3DSurface_UnlockRect((IWineD3DSurface *) This->surfaces[Level]);
228         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
229     } else {
230         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
231         return D3DERR_INVALIDCALL;
232     }
233     return hr;
234 }
235
236 HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
237     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
238     This->baseTexture.dirty = TRUE;
239     TRACE("(%p) : dirtyfication of surface Level (0)\n", This);    
240     return IWineD3DSurface_AddDirtyRect((IWineD3DSurface *)This->surfaces[0], pDirtyRect);
241 }
242
243 IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
244 {
245     IWineD3DTextureImpl_QueryInterface,
246     IWineD3DTextureImpl_AddRef,
247     IWineD3DTextureImpl_Release,
248     IWineD3DTextureImpl_GetParent,
249     IWineD3DTextureImpl_GetDevice,
250     IWineD3DTextureImpl_SetPrivateData,
251     IWineD3DTextureImpl_GetPrivateData,
252     IWineD3DTextureImpl_FreePrivateData,
253     IWineD3DTextureImpl_SetPriority,
254     IWineD3DTextureImpl_GetPriority,
255     IWineD3DTextureImpl_PreLoad,
256     IWineD3DTextureImpl_GetType,
257     IWineD3DTextureImpl_SetLOD,
258     IWineD3DTextureImpl_GetLOD,
259     IWineD3DTextureImpl_GetLevelCount,
260     IWineD3DTextureImpl_SetAutoGenFilterType,
261     IWineD3DTextureImpl_GetAutoGenFilterType,
262     IWineD3DTextureImpl_GenerateMipSubLevels,
263     IWineD3DTextureImpl_SetDirty,
264     IWineD3DTextureImpl_GetDirty,
265     IWineD3DTextureImpl_GetLevelDesc,
266     IWineD3DTextureImpl_GetSurfaceLevel,
267     IWineD3DTextureImpl_LockRect,
268     IWineD3DTextureImpl_UnlockRect,
269     IWineD3DTextureImpl_AddDirtyRect
270 };