Define RTL_HANDLE and RTL_HANDLE_TABLE if not using Wine's winternl.h
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 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 D3D_OK;
39     }
40
41     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
42     return E_NOINTERFACE;
43 }
44
45 ULONG WINAPI IDirect3DTexture9Impl_AddRef(LPDIRECT3DTEXTURE9 iface) {
46     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
47     ULONG ref = InterlockedIncrement(&This->ref);
48
49     TRACE("(%p) : AddRef from %ld\n", This, ref - 1);
50
51     return ref;
52 }
53
54 ULONG WINAPI IDirect3DTexture9Impl_Release(LPDIRECT3DTEXTURE9 iface) {
55     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
56     ULONG ref = InterlockedDecrement(&This->ref);
57
58     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
59
60     if (ref == 0) {
61         IWineD3DTexture_Release(This->wineD3DTexture);
62         HeapFree(GetProcessHeap(), 0, This);
63     }
64     return ref;
65 }
66
67 /* IDirect3DTexture9 IDirect3DResource9 Interface follow: */
68 HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(LPDIRECT3DTEXTURE9 iface, IDirect3DDevice9** ppDevice) {
69     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
70     TRACE("(%p) Relay\n", This);
71     return IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
72 }
73
74 HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
75     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
76     TRACE("(%p) Relay\n", This);
77     return IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
78 }
79
80 HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
81     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
82     TRACE("(%p) Relay\n", This);
83     return IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
84 }
85
86 HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid) {
87     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
88     TRACE("(%p) Relay\n", This);
89     return IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
90 }
91
92 DWORD WINAPI IDirect3DTexture9Impl_SetPriority(LPDIRECT3DTEXTURE9 iface, DWORD PriorityNew) {
93     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
94     TRACE("(%p) Relay\n", This);
95     return IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
96 }
97
98 DWORD WINAPI IDirect3DTexture9Impl_GetPriority(LPDIRECT3DTEXTURE9 iface) {
99     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
100     TRACE("(%p) Relay\n", This);
101     return IWineD3DTexture_GetPriority(This->wineD3DTexture);
102 }
103
104 void WINAPI IDirect3DTexture9Impl_PreLoad(LPDIRECT3DTEXTURE9 iface) {
105     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
106     TRACE("(%p) Relay\n", This);
107     return IWineD3DTexture_PreLoad(This->wineD3DTexture);
108 }
109
110 D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(LPDIRECT3DTEXTURE9 iface) {
111     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
112     TRACE("(%p) Relay\n", This);
113     return IWineD3DTexture_GetType(This->wineD3DTexture);
114 }
115
116 /* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
117 DWORD WINAPI IDirect3DTexture9Impl_SetLOD(LPDIRECT3DTEXTURE9 iface, DWORD LODNew) {
118     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
119     TRACE("(%p) Relay\n", This);
120     return IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
121 }
122
123 DWORD WINAPI IDirect3DTexture9Impl_GetLOD(LPDIRECT3DTEXTURE9 iface) {
124     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
125     TRACE("(%p) Relay\n", This);
126     return IWineD3DTexture_GetLOD(This->wineD3DTexture);
127 }
128
129 DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(LPDIRECT3DTEXTURE9 iface) {
130     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
131     TRACE("(%p) Relay\n", This);
132     return IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
133 }
134
135 HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
136     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
137     TRACE("(%p) Relay\n", This);
138     return IWineD3DTexture_SetAutoGenFilterType(This->wineD3DTexture, FilterType);
139 }
140
141 D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface) {
142     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
143     TRACE("(%p) Relay\n", This);
144     return IWineD3DTexture_GetAutoGenFilterType(This->wineD3DTexture);
145 }
146
147 void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(LPDIRECT3DTEXTURE9 iface) {
148     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
149     TRACE("(%p) Relay\n", This);
150     return IWineD3DTexture_GenerateMipSubLevels(This->wineD3DTexture);
151 }
152
153 /* IDirect3DTexture9 Interface follow: */
154 HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
155     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
156
157     WINED3DSURFACE_DESC    wined3ddesc;
158     UINT                   tmpInt = -1;
159     TRACE("(%p) Relay\n", This);
160
161     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
162     wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
163     wined3ddesc.Type                = &pDesc->Type;
164     wined3ddesc.Usage               = &pDesc->Usage;
165     wined3ddesc.Pool                = &pDesc->Pool;
166     wined3ddesc.Size                = &tmpInt; /* required for d3d8 */
167     wined3ddesc.MultiSampleType     = &pDesc->MultiSampleType;
168     wined3ddesc.MultiSampleQuality  = &pDesc->MultiSampleQuality;
169     wined3ddesc.Width               = &pDesc->Width;
170     wined3ddesc.Height              = &pDesc->Height;
171
172     return IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
173 }
174
175 HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(LPDIRECT3DTEXTURE9 iface, UINT Level, IDirect3DSurface9** ppSurfaceLevel) {
176     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
177     HRESULT hrc = D3D_OK;
178     IWineD3DSurface *mySurface = NULL;
179
180     TRACE("(%p) Relay\n", This);
181     hrc = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
182     if (hrc == D3D_OK && NULL != ppSurfaceLevel) {
183        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppSurfaceLevel);
184        IWineD3DSurface_Release(mySurface);
185     }
186     return hrc;
187 }
188
189 HRESULT WINAPI IDirect3DTexture9Impl_LockRect(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
190     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
191     TRACE("(%p) Relay\n", This);
192     return IWineD3DTexture_LockRect(This->wineD3DTexture, Level, pLockedRect, pRect, Flags);
193 }
194
195 HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(LPDIRECT3DTEXTURE9 iface, UINT Level) {
196     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
197     TRACE("(%p) Relay\n", This);
198     return IWineD3DTexture_UnlockRect(This->wineD3DTexture, Level);
199 }
200
201 HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(LPDIRECT3DTEXTURE9 iface, CONST RECT* pDirtyRect) {
202     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
203     TRACE("(%p) Relay\n", This);
204     return IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
205 }
206
207 const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
208 {
209     IDirect3DTexture9Impl_QueryInterface,
210     IDirect3DTexture9Impl_AddRef,
211     IDirect3DTexture9Impl_Release,
212     IDirect3DTexture9Impl_GetDevice,
213     IDirect3DTexture9Impl_SetPrivateData,
214     IDirect3DTexture9Impl_GetPrivateData,
215     IDirect3DTexture9Impl_FreePrivateData,
216     IDirect3DTexture9Impl_SetPriority,
217     IDirect3DTexture9Impl_GetPriority,
218     IDirect3DTexture9Impl_PreLoad,
219     IDirect3DTexture9Impl_GetType,
220     
221     IDirect3DTexture9Impl_SetLOD,
222     IDirect3DTexture9Impl_GetLOD,
223     IDirect3DTexture9Impl_GetLevelCount,
224     IDirect3DTexture9Impl_SetAutoGenFilterType,
225     IDirect3DTexture9Impl_GetAutoGenFilterType,
226     IDirect3DTexture9Impl_GenerateMipSubLevels,
227     IDirect3DTexture9Impl_GetLevelDesc,
228     IDirect3DTexture9Impl_GetSurfaceLevel,
229     IDirect3DTexture9Impl_LockRect,
230     IDirect3DTexture9Impl_UnlockRect,
231     IDirect3DTexture9Impl_AddDirtyRect
232 };
233
234
235 /* IDirect3DDevice9 IDirect3DTexture9 Methods follow: */
236 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateTexture(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
237                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, HANDLE* pSharedHandle) {
238     IDirect3DTexture9Impl *object;
239     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
240     HRESULT hrc = D3D_OK;
241     
242     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%ld), Fmt(%u), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
243
244     /* Allocate the storage for the device */
245     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture9Impl));
246
247     if (NULL == object) {
248         FIXME("Allocation of memory failed\n");
249         *ppTexture = NULL;
250         return D3DERR_OUTOFVIDEOMEMORY;
251     }
252
253     object->lpVtbl = &Direct3DTexture9_Vtbl;
254     object->ref = 1;
255     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage,
256                                  (WINED3DFORMAT)Format, Pool, &object->wineD3DTexture, pSharedHandle, (IUnknown *)object, D3D9CB_CreateSurface);
257
258     if (FAILED(hrc)) {
259         /* free up object */ 
260         FIXME("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
261         HeapFree(GetProcessHeap(), 0, object);
262         *ppTexture = NULL;
263    } else {
264         *ppTexture= (LPDIRECT3DTEXTURE9) object;
265    }
266
267    TRACE("(%p) Created Texture %p, %p\n",This,object,object->wineD3DTexture);
268    return hrc;
269 }