Only reapply the texture states necessary when a different texture
[wine] / dlls / d3d8 / texture.c
1 /*
2  * IDirect3DTexture8 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/debug.h"
28
29 #include "d3d8_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32
33 /* IDirect3DTexture8 IUnknown parts follow: */
34 HRESULT WINAPI IDirect3DTexture8Impl_QueryInterface(LPDIRECT3DTEXTURE8 iface, REFIID riid, LPVOID *ppobj)
35 {
36     ICOM_THIS(IDirect3DTexture8Impl,iface);
37     TRACE("(%p) : QueryInterface for %s\n", This, debugstr_guid(riid));
38     if (IsEqualGUID(riid, &IID_IUnknown)
39         || IsEqualGUID(riid, &IID_IDirect3DResource8)
40         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
41         || IsEqualGUID(riid, &IID_IDirect3DTexture8)) {
42         IDirect3DTexture8Impl_AddRef(iface);
43         *ppobj = This;
44         return D3D_OK;
45     }
46
47     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
48     return E_NOINTERFACE;
49 }
50
51 ULONG WINAPI IDirect3DTexture8Impl_AddRef(LPDIRECT3DTEXTURE8 iface) {
52     ICOM_THIS(IDirect3DTexture8Impl,iface);
53     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
54     return ++(This->ref);
55 }
56
57 ULONG WINAPI IDirect3DTexture8Impl_Release(LPDIRECT3DTEXTURE8 iface) {
58     ICOM_THIS(IDirect3DTexture8Impl,iface);
59     ULONG ref = --This->ref;
60     int i;
61
62     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
63     if (ref == 0) {
64         for (i = 0; i < This->levels; i++) {
65             if (This->surfaces[i] != NULL) {
66                 TRACE("(%p) : Releasing surface %p\n", This, This->surfaces[i]);
67                 IDirect3DSurface8Impl_Release((LPDIRECT3DSURFACE8) This->surfaces[i]);
68             }
69         }
70         HeapFree(GetProcessHeap(), 0, This);
71     }
72     return ref;
73 }
74
75 /* IDirect3DTexture8 IDirect3DResource8 Interface follow: */
76 HRESULT  WINAPI        IDirect3DTexture8Impl_GetDevice(LPDIRECT3DTEXTURE8 iface, IDirect3DDevice8** ppDevice) {
77     ICOM_THIS(IDirect3DTexture8Impl,iface);
78     TRACE("(%p) : returning %p\n", This, This->Device);
79     *ppDevice = (LPDIRECT3DDEVICE8) This->Device; 
80     /**
81      * Note  Calling this method will increase the internal reference count 
82      * on the IDirect3DDevice8 interface. 
83      */
84     IDirect3DDevice8Impl_AddRef(*ppDevice);
85     return D3D_OK;
86 }
87 HRESULT  WINAPI        IDirect3DTexture8Impl_SetPrivateData(LPDIRECT3DTEXTURE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
88     ICOM_THIS(IDirect3DTexture8Impl,iface);
89     FIXME("(%p) : stub\n", This);    return D3D_OK;
90 }
91 HRESULT  WINAPI        IDirect3DTexture8Impl_GetPrivateData(LPDIRECT3DTEXTURE8 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
92     ICOM_THIS(IDirect3DTexture8Impl,iface);
93     FIXME("(%p) : stub\n", This);    return D3D_OK;
94 }
95 HRESULT  WINAPI        IDirect3DTexture8Impl_FreePrivateData(LPDIRECT3DTEXTURE8 iface, REFGUID refguid) {
96     ICOM_THIS(IDirect3DTexture8Impl,iface);
97     FIXME("(%p) : stub\n", This);    return D3D_OK;
98 }
99 DWORD    WINAPI        IDirect3DTexture8Impl_SetPriority(LPDIRECT3DTEXTURE8 iface, DWORD PriorityNew) {
100     ICOM_THIS(IDirect3DTexture8Impl,iface);
101     FIXME("(%p) : stub\n", This);
102     return 0;
103 }
104 DWORD    WINAPI        IDirect3DTexture8Impl_GetPriority(LPDIRECT3DTEXTURE8 iface) {
105     ICOM_THIS(IDirect3DTexture8Impl,iface);
106     FIXME("(%p) : stub\n", This);
107     return 0;
108 }
109 void     WINAPI        IDirect3DTexture8Impl_PreLoad(LPDIRECT3DTEXTURE8 iface) {
110     int i;
111     ICOM_THIS(IDirect3DTexture8Impl,iface);
112     TRACE("(%p) : About to load texture\n", This);
113
114     ENTER_GL();
115
116     for (i = 0; i < This->levels; i++) {
117       if (i == 0 && This->surfaces[i]->textureName != 0 && This->Dirty == FALSE) {
118         glBindTexture(GL_TEXTURE_2D, This->surfaces[i]->textureName);
119         checkGLcall("glBindTexture");
120         TRACE("Texture %p (level %d) given name %d\n", This->surfaces[i], i, This->surfaces[i]->textureName);
121         /* No need to walk through all mip-map levels, since already all assigned */
122         i = This->levels;
123
124       } else {
125         if (i == 0) {
126           if (This->surfaces[i]->textureName == 0) {
127             glGenTextures(1, &This->surfaces[i]->textureName);
128             checkGLcall("glGenTextures");
129             TRACE("Texture %p (level %d) given name %d\n", This->surfaces[i], i, This->surfaces[i]->textureName);
130           }
131           
132           glBindTexture(GL_TEXTURE_2D, This->surfaces[i]->textureName);
133           checkGLcall("glBindTexture");
134         }
135         IDirect3DSurface8Impl_LoadTexture((LPDIRECT3DSURFACE8) This->surfaces[i], GL_TEXTURE_2D, i); 
136       }
137     }
138
139     /* No longer dirty */
140     This->Dirty = FALSE;
141
142     /* Always need to reset the number of mipmap levels when rebinding as it is
143        a property of the active texture unit, and another texture may have set it
144        to a different value                                                       */
145     TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->levels - 1);   
146     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->levels - 1);
147     checkGLcall("glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->levels)");
148
149     LEAVE_GL();
150
151     return ;
152 }
153 D3DRESOURCETYPE WINAPI IDirect3DTexture8Impl_GetType(LPDIRECT3DTEXTURE8 iface) {
154     ICOM_THIS(IDirect3DTexture8Impl,iface);
155     TRACE("(%p) : is %d \n", This, This->ResourceType);
156     return This->ResourceType;
157 }
158
159 /* IDirect3DTexture8 IDirect3DBaseTexture8 Interface follow: */
160 DWORD    WINAPI        IDirect3DTexture8Impl_SetLOD(LPDIRECT3DTEXTURE8 iface, DWORD LODNew) {
161     ICOM_THIS(IDirect3DTexture8Impl,iface);
162     FIXME("(%p) : stub\n", This);
163     return 0;
164 }
165 DWORD    WINAPI        IDirect3DTexture8Impl_GetLOD(LPDIRECT3DTEXTURE8 iface) {
166     ICOM_THIS(IDirect3DTexture8Impl,iface);
167     FIXME("(%p) : stub\n", This);
168     return 0;
169 }
170 DWORD    WINAPI        IDirect3DTexture8Impl_GetLevelCount(LPDIRECT3DTEXTURE8 iface) {
171     ICOM_THIS(IDirect3DTexture8Impl,iface);
172     TRACE("(%p) : returning %d\n", This, This->levels);
173     return This->levels;
174 }
175
176 /* IDirect3DTexture8 */
177 HRESULT  WINAPI        IDirect3DTexture8Impl_GetLevelDesc(LPDIRECT3DTEXTURE8 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
178     ICOM_THIS(IDirect3DTexture8Impl,iface);
179
180     if (Level < This->levels) {
181         TRACE("(%p) Level (%d)\n", This, Level);
182         return IDirect3DSurface8Impl_GetDesc((LPDIRECT3DSURFACE8) This->surfaces[Level], pDesc);
183     } else {
184         FIXME("Levels seems too high?!!\n");
185         return D3DERR_INVALIDCALL;
186     }
187     return D3D_OK;
188 }
189 HRESULT  WINAPI        IDirect3DTexture8Impl_GetSurfaceLevel(LPDIRECT3DTEXTURE8 iface, UINT Level, IDirect3DSurface8** ppSurfaceLevel) {
190     ICOM_THIS(IDirect3DTexture8Impl,iface);
191     *ppSurfaceLevel = (LPDIRECT3DSURFACE8)This->surfaces[Level];
192     IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) This->surfaces[Level]);
193     TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
194     return D3D_OK;
195 }
196 HRESULT  WINAPI        IDirect3DTexture8Impl_LockRect(LPDIRECT3DTEXTURE8 iface, UINT Level,D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags) {
197     HRESULT hr;
198     ICOM_THIS(IDirect3DTexture8Impl,iface);
199     TRACE("(%p) Level (%d)\n", This, Level);
200     if (Level < This->levels) {
201         /**
202          * Not dirtified while Surfaces don't notify dirtification
203          * This->Dirty = TRUE;
204          */
205         hr = IDirect3DSurface8Impl_LockRect((LPDIRECT3DSURFACE8) This->surfaces[Level], pLockedRect, pRect, Flags);
206         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
207     } else {
208         FIXME("Levels seems too high?!!\n");
209         return D3DERR_INVALIDCALL;
210     }
211     return hr;
212 }
213 HRESULT  WINAPI        IDirect3DTexture8Impl_UnlockRect(LPDIRECT3DTEXTURE8 iface, UINT Level) {
214     HRESULT hr;
215     ICOM_THIS(IDirect3DTexture8Impl,iface);
216     TRACE("(%p) Level (%d)\n", This, Level);
217     if (Level < This->levels) {
218         hr = IDirect3DSurface8Impl_UnlockRect((LPDIRECT3DSURFACE8) This->surfaces[Level]);
219         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
220     } else {
221         FIXME("Levels seems too high?!!\n");
222         return D3DERR_INVALIDCALL;
223     }
224     return hr;
225 }
226 HRESULT  WINAPI        IDirect3DTexture8Impl_AddDirtyRect(LPDIRECT3DTEXTURE8 iface, CONST RECT* pDirtyRect) {
227     ICOM_THIS(IDirect3DTexture8Impl,iface);
228     This->Dirty = TRUE;
229     TRACE("(%p) : dirtyfication of surface Level (0)\n", This);    
230     return IDirect3DSurface8Impl_AddDirtyRect((LPDIRECT3DSURFACE8) This->surfaces[0], pDirtyRect);
231 }
232
233
234 ICOM_VTABLE(IDirect3DTexture8) Direct3DTexture8_Vtbl =
235 {
236     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
237     IDirect3DTexture8Impl_QueryInterface,
238     IDirect3DTexture8Impl_AddRef,
239     IDirect3DTexture8Impl_Release,
240     IDirect3DTexture8Impl_GetDevice,
241     IDirect3DTexture8Impl_SetPrivateData,
242     IDirect3DTexture8Impl_GetPrivateData,
243     IDirect3DTexture8Impl_FreePrivateData,
244     IDirect3DTexture8Impl_SetPriority,
245     IDirect3DTexture8Impl_GetPriority,
246     IDirect3DTexture8Impl_PreLoad,
247     IDirect3DTexture8Impl_GetType,
248     IDirect3DTexture8Impl_SetLOD,
249     IDirect3DTexture8Impl_GetLOD,
250     IDirect3DTexture8Impl_GetLevelCount,
251     IDirect3DTexture8Impl_GetLevelDesc,
252     IDirect3DTexture8Impl_GetSurfaceLevel,
253     IDirect3DTexture8Impl_LockRect,
254     IDirect3DTexture8Impl_UnlockRect,
255     IDirect3DTexture8Impl_AddDirtyRect
256
257 };