wined3d: Fix light state bugs.
[wine] / dlls / d3d9 / texture.c
1 /*
2  * IDirect3DTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 /* IDirect3DTexture9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DTexture9Impl_QueryInterface(LPDIRECT3DTEXTURE9 iface, REFIID riid, LPVOID* ppobj) {
30     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource9)
34         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
35         || IsEqualGUID(riid, &IID_IDirect3DTexture9)) {
36         IUnknown_AddRef(iface);
37         *ppobj = This;
38         return S_OK;
39     }
40
41     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
42     *ppobj = NULL;
43     return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DTexture9Impl_AddRef(LPDIRECT3DTEXTURE9 iface) {
47     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
48     ULONG ref = InterlockedIncrement(&This->ref);
49
50     TRACE("(%p) : AddRef from %ld\n", This, ref - 1);
51
52     return ref;
53 }
54
55 static ULONG WINAPI IDirect3DTexture9Impl_Release(LPDIRECT3DTEXTURE9 iface) {
56     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
57     ULONG ref = InterlockedDecrement(&This->ref);
58
59     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
60
61     if (ref == 0) {
62         IWineD3DTexture_Release(This->wineD3DTexture);
63         IUnknown_Release(This->parentDevice);
64         HeapFree(GetProcessHeap(), 0, This);
65     }
66     return ref;
67 }
68
69 /* IDirect3DTexture9 IDirect3DResource9 Interface follow: */
70 static HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(LPDIRECT3DTEXTURE9 iface, IDirect3DDevice9** ppDevice) {
71     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
72     TRACE("(%p) Relay\n", This);
73     return IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
74 }
75
76 static HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
77     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
78     TRACE("(%p) Relay\n", This);
79     return IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
80 }
81
82 static HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
83     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
84     TRACE("(%p) Relay\n", This);
85     return IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
86 }
87
88 static HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid) {
89     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
90     TRACE("(%p) Relay\n", This);
91     return IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
92 }
93
94 static DWORD WINAPI IDirect3DTexture9Impl_SetPriority(LPDIRECT3DTEXTURE9 iface, DWORD PriorityNew) {
95     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
96     TRACE("(%p) Relay\n", This);
97     return IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
98 }
99
100 static DWORD WINAPI IDirect3DTexture9Impl_GetPriority(LPDIRECT3DTEXTURE9 iface) {
101     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
102     TRACE("(%p) Relay\n", This);
103     return IWineD3DTexture_GetPriority(This->wineD3DTexture);
104 }
105
106 static void WINAPI IDirect3DTexture9Impl_PreLoad(LPDIRECT3DTEXTURE9 iface) {
107     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
108     TRACE("(%p) Relay\n", This);
109     return IWineD3DTexture_PreLoad(This->wineD3DTexture);
110 }
111
112 static D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(LPDIRECT3DTEXTURE9 iface) {
113     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
114     TRACE("(%p) Relay\n", This);
115     return IWineD3DTexture_GetType(This->wineD3DTexture);
116 }
117
118 /* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
119 static DWORD WINAPI IDirect3DTexture9Impl_SetLOD(LPDIRECT3DTEXTURE9 iface, DWORD LODNew) {
120     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
121     TRACE("(%p) Relay\n", This);
122     return IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
123 }
124
125 static DWORD WINAPI IDirect3DTexture9Impl_GetLOD(LPDIRECT3DTEXTURE9 iface) {
126     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
127     TRACE("(%p) Relay\n", This);
128     return IWineD3DTexture_GetLOD(This->wineD3DTexture);
129 }
130
131 static DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(LPDIRECT3DTEXTURE9 iface) {
132     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
133     TRACE("(%p) Relay\n", This);
134     return IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
135 }
136
137 static HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
138     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
139     TRACE("(%p) Relay\n", This);
140     return IWineD3DTexture_SetAutoGenFilterType(This->wineD3DTexture, (WINED3DTEXTUREFILTERTYPE) FilterType);
141 }
142
143 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface) {
144     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
145     TRACE("(%p) Relay\n", This);
146     return (D3DTEXTUREFILTERTYPE) IWineD3DTexture_GetAutoGenFilterType(This->wineD3DTexture);
147 }
148
149 static void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(LPDIRECT3DTEXTURE9 iface) {
150     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
151     TRACE("(%p) Relay\n", This);
152     return IWineD3DTexture_GenerateMipSubLevels(This->wineD3DTexture);
153 }
154
155 /* IDirect3DTexture9 Interface follow: */
156 static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
157     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
158
159     WINED3DSURFACE_DESC    wined3ddesc;
160     UINT                   tmpInt = -1;
161     TRACE("(%p) Relay\n", This);
162
163     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
164     wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
165     wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
166     wined3ddesc.Usage               = &pDesc->Usage;
167     wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
168     wined3ddesc.Size                = &tmpInt; /* required for d3d8 */
169     wined3ddesc.MultiSampleType     = (WINED3DMULTISAMPLE_TYPE *) &pDesc->MultiSampleType;
170     wined3ddesc.MultiSampleQuality  = &pDesc->MultiSampleQuality;
171     wined3ddesc.Width               = &pDesc->Width;
172     wined3ddesc.Height              = &pDesc->Height;
173
174     return IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
175 }
176
177 static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(LPDIRECT3DTEXTURE9 iface, UINT Level, IDirect3DSurface9** ppSurfaceLevel) {
178     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
179     HRESULT hrc = D3D_OK;
180     IWineD3DSurface *mySurface = NULL;
181
182     TRACE("(%p) Relay\n", This);
183     hrc = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
184     if (hrc == D3D_OK && NULL != ppSurfaceLevel) {
185        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppSurfaceLevel);
186        IWineD3DSurface_Release(mySurface);
187     }
188     return hrc;
189 }
190
191 static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
192     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
193     TRACE("(%p) Relay\n", This);
194     return IWineD3DTexture_LockRect(This->wineD3DTexture, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
195 }
196
197 static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(LPDIRECT3DTEXTURE9 iface, UINT Level) {
198     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
199     TRACE("(%p) Relay\n", This);
200     return IWineD3DTexture_UnlockRect(This->wineD3DTexture, Level);
201 }
202
203 static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(LPDIRECT3DTEXTURE9 iface, CONST RECT* pDirtyRect) {
204     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
205     TRACE("(%p) Relay\n", This);
206     return IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
207 }
208
209 static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
210 {
211     /* IUnknown */
212     IDirect3DTexture9Impl_QueryInterface,
213     IDirect3DTexture9Impl_AddRef,
214     IDirect3DTexture9Impl_Release,
215      /* IDirect3DResource9 */
216     IDirect3DTexture9Impl_GetDevice,
217     IDirect3DTexture9Impl_SetPrivateData,
218     IDirect3DTexture9Impl_GetPrivateData,
219     IDirect3DTexture9Impl_FreePrivateData,
220     IDirect3DTexture9Impl_SetPriority,
221     IDirect3DTexture9Impl_GetPriority,
222     IDirect3DTexture9Impl_PreLoad,
223     IDirect3DTexture9Impl_GetType,
224     /* IDirect3dBaseTexture9 */
225     IDirect3DTexture9Impl_SetLOD,
226     IDirect3DTexture9Impl_GetLOD,
227     IDirect3DTexture9Impl_GetLevelCount,
228     IDirect3DTexture9Impl_SetAutoGenFilterType,
229     IDirect3DTexture9Impl_GetAutoGenFilterType,
230     IDirect3DTexture9Impl_GenerateMipSubLevels,
231     /* IDirect3DTexture9 */
232     IDirect3DTexture9Impl_GetLevelDesc,
233     IDirect3DTexture9Impl_GetSurfaceLevel,
234     IDirect3DTexture9Impl_LockRect,
235     IDirect3DTexture9Impl_UnlockRect,
236     IDirect3DTexture9Impl_AddDirtyRect
237 };
238
239
240 /* IDirect3DDevice9 IDirect3DTexture9 Methods follow: */
241 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateTexture(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
242                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, HANDLE* pSharedHandle) {
243     IDirect3DTexture9Impl *object;
244     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
245     HRESULT hrc = D3D_OK;
246     
247     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%ld), Fmt(%u), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
248
249     /* Allocate the storage for the device */
250     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture9Impl));
251
252     if (NULL == object) {
253         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
254         return D3DERR_OUTOFVIDEOMEMORY;
255     }
256
257     object->lpVtbl = &Direct3DTexture9_Vtbl;
258     object->ref = 1;
259     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage,
260                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DTexture, pSharedHandle, (IUnknown *)object, D3D9CB_CreateSurface);
261
262     if (FAILED(hrc)) {
263
264         /* free up object */
265         FIXME("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
266         HeapFree(GetProcessHeap(), 0, object);
267    } else {
268         IUnknown_AddRef(iface);
269         object->parentDevice = iface;
270         *ppTexture= (LPDIRECT3DTEXTURE9) object;
271         TRACE("(%p) Created Texture %p, %p\n", This, object, object->wineD3DTexture);
272    }
273
274    return hrc;
275 }