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