wined3d: Add WGL_ARB_pixel_format detection.
[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         IDirect3DTexture9_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 %d\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 %d\n", This, ref);
60
61     if (ref == 0) {
62         EnterCriticalSection(&d3d9_cs);
63         IWineD3DTexture_Destroy(This->wineD3DTexture, D3D9CB_DestroySurface);
64         LeaveCriticalSection(&d3d9_cs);
65         IDirect3DDevice9Ex_Release(This->parentDevice);
66         HeapFree(GetProcessHeap(), 0, This);
67     }
68     return ref;
69 }
70
71 /* IDirect3DTexture9 IDirect3DResource9 Interface follow: */
72 static HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(LPDIRECT3DTEXTURE9 iface, IDirect3DDevice9** ppDevice) {
73     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
74     HRESULT hr;
75     TRACE("(%p) Relay\n", This);
76
77     EnterCriticalSection(&d3d9_cs);
78     hr = IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
79     LeaveCriticalSection(&d3d9_cs);
80     return hr;
81 }
82
83 static HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
84     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
85     HRESULT hr;
86     TRACE("(%p) Relay\n", This);
87
88     EnterCriticalSection(&d3d9_cs);
89     hr = IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
90     LeaveCriticalSection(&d3d9_cs);
91     return hr;
92 }
93
94 static HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
95     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
96     HRESULT hr;
97     TRACE("(%p) Relay\n", This);
98
99     EnterCriticalSection(&d3d9_cs);
100     hr = IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
101     LeaveCriticalSection(&d3d9_cs);
102     return hr;
103 }
104
105 static HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid) {
106     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
107     HRESULT hr;
108     TRACE("(%p) Relay\n", This);
109
110     EnterCriticalSection(&d3d9_cs);
111     hr = IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
112     LeaveCriticalSection(&d3d9_cs);
113     return hr;
114 }
115
116 static DWORD WINAPI IDirect3DTexture9Impl_SetPriority(LPDIRECT3DTEXTURE9 iface, DWORD PriorityNew) {
117     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
118     DWORD ret;
119     TRACE("(%p) Relay\n", This);
120
121     EnterCriticalSection(&d3d9_cs);
122     ret = IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
123     LeaveCriticalSection(&d3d9_cs);
124     return ret;
125 }
126
127 static DWORD WINAPI IDirect3DTexture9Impl_GetPriority(LPDIRECT3DTEXTURE9 iface) {
128     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
129     DWORD ret;
130     TRACE("(%p) Relay\n", This);
131
132     EnterCriticalSection(&d3d9_cs);
133     ret = IWineD3DTexture_GetPriority(This->wineD3DTexture);
134     LeaveCriticalSection(&d3d9_cs);
135     return ret;
136 }
137
138 static void WINAPI IDirect3DTexture9Impl_PreLoad(LPDIRECT3DTEXTURE9 iface) {
139     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
140     TRACE("(%p) Relay\n", This);
141
142     EnterCriticalSection(&d3d9_cs);
143     IWineD3DTexture_PreLoad(This->wineD3DTexture);
144     LeaveCriticalSection(&d3d9_cs);
145 }
146
147 static D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(LPDIRECT3DTEXTURE9 iface) {
148     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
149     HRESULT ret;
150     TRACE("(%p) Relay\n", This);
151
152     EnterCriticalSection(&d3d9_cs);
153     ret = IWineD3DTexture_GetType(This->wineD3DTexture);
154     LeaveCriticalSection(&d3d9_cs);
155     return ret;
156 }
157
158 /* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
159 static DWORD WINAPI IDirect3DTexture9Impl_SetLOD(LPDIRECT3DTEXTURE9 iface, DWORD LODNew) {
160     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
161     DWORD ret;
162     TRACE("(%p) Relay\n", This);
163
164     EnterCriticalSection(&d3d9_cs);
165     ret = IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
166     LeaveCriticalSection(&d3d9_cs);
167     return ret;
168 }
169
170 static DWORD WINAPI IDirect3DTexture9Impl_GetLOD(LPDIRECT3DTEXTURE9 iface) {
171     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
172     DWORD ret;
173     TRACE("(%p) Relay\n", This);
174
175     EnterCriticalSection(&d3d9_cs);
176     ret = IWineD3DTexture_GetLOD(This->wineD3DTexture);
177     LeaveCriticalSection(&d3d9_cs);
178     return ret;
179 }
180
181 static DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(LPDIRECT3DTEXTURE9 iface) {
182     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
183     DWORD ret;
184     TRACE("(%p) Relay\n", This);
185
186     EnterCriticalSection(&d3d9_cs);
187     ret = IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
188     LeaveCriticalSection(&d3d9_cs);
189     return ret;
190 }
191
192 static HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
193     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
194     HRESULT hr;
195     TRACE("(%p) Relay\n", This);
196
197     EnterCriticalSection(&d3d9_cs);
198     hr = IWineD3DTexture_SetAutoGenFilterType(This->wineD3DTexture, (WINED3DTEXTUREFILTERTYPE) FilterType);
199     LeaveCriticalSection(&d3d9_cs);
200     return hr;
201 }
202
203 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface) {
204     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
205     D3DTEXTUREFILTERTYPE ret;
206     TRACE("(%p) Relay\n", This);
207
208     EnterCriticalSection(&d3d9_cs);
209     ret = (D3DTEXTUREFILTERTYPE) IWineD3DTexture_GetAutoGenFilterType(This->wineD3DTexture);
210     LeaveCriticalSection(&d3d9_cs);
211     return ret;
212 }
213
214 static void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(LPDIRECT3DTEXTURE9 iface) {
215     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
216     TRACE("(%p) Relay\n", This);
217
218     EnterCriticalSection(&d3d9_cs);
219     IWineD3DTexture_GenerateMipSubLevels(This->wineD3DTexture);
220     LeaveCriticalSection(&d3d9_cs);
221 }
222
223 /* IDirect3DTexture9 Interface follow: */
224 static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
225     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
226
227     WINED3DSURFACE_DESC    wined3ddesc;
228     UINT                   tmpInt = -1;
229     HRESULT                hr;
230     WINED3DFORMAT format;
231
232     TRACE("(%p) Relay\n", This);
233
234     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
235     wined3ddesc.Format              = &format;
236     wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
237     wined3ddesc.Usage               = &pDesc->Usage;
238     wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
239     wined3ddesc.Size                = &tmpInt; /* required for d3d8 */
240     wined3ddesc.MultiSampleType     = (WINED3DMULTISAMPLE_TYPE *) &pDesc->MultiSampleType;
241     wined3ddesc.MultiSampleQuality  = &pDesc->MultiSampleQuality;
242     wined3ddesc.Width               = &pDesc->Width;
243     wined3ddesc.Height              = &pDesc->Height;
244
245     EnterCriticalSection(&d3d9_cs);
246     hr = IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
247     LeaveCriticalSection(&d3d9_cs);
248
249     if (SUCCEEDED(hr)) pDesc->Format = d3dformat_from_wined3dformat(format);
250
251     return hr;
252 }
253
254 static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(LPDIRECT3DTEXTURE9 iface, UINT Level, IDirect3DSurface9** ppSurfaceLevel) {
255     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
256     HRESULT hrc = D3D_OK;
257     IWineD3DSurface *mySurface = NULL;
258
259     TRACE("(%p) Relay\n", This);
260     EnterCriticalSection(&d3d9_cs);
261     hrc = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
262     if (hrc == D3D_OK && NULL != ppSurfaceLevel) {
263        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppSurfaceLevel);
264        IWineD3DSurface_Release(mySurface);
265     }
266     LeaveCriticalSection(&d3d9_cs);
267     return hrc;
268 }
269
270 static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
271     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
272     HRESULT hr;
273     TRACE("(%p) Relay\n", This);
274
275     EnterCriticalSection(&d3d9_cs);
276     hr = IWineD3DTexture_LockRect(This->wineD3DTexture, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
277     LeaveCriticalSection(&d3d9_cs);
278     return hr;
279 }
280
281 static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(LPDIRECT3DTEXTURE9 iface, UINT Level) {
282     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
283     HRESULT hr;
284     TRACE("(%p) Relay\n", This);
285
286     EnterCriticalSection(&d3d9_cs);
287     hr = IWineD3DTexture_UnlockRect(This->wineD3DTexture, Level);
288     LeaveCriticalSection(&d3d9_cs);
289     return hr;
290 }
291
292 static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(LPDIRECT3DTEXTURE9 iface, CONST RECT* pDirtyRect) {
293     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
294     HRESULT hr;
295     TRACE("(%p) Relay\n", This);
296
297     EnterCriticalSection(&d3d9_cs);
298     hr = IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
299     LeaveCriticalSection(&d3d9_cs);
300     return hr;
301 }
302
303 static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
304 {
305     /* IUnknown */
306     IDirect3DTexture9Impl_QueryInterface,
307     IDirect3DTexture9Impl_AddRef,
308     IDirect3DTexture9Impl_Release,
309      /* IDirect3DResource9 */
310     IDirect3DTexture9Impl_GetDevice,
311     IDirect3DTexture9Impl_SetPrivateData,
312     IDirect3DTexture9Impl_GetPrivateData,
313     IDirect3DTexture9Impl_FreePrivateData,
314     IDirect3DTexture9Impl_SetPriority,
315     IDirect3DTexture9Impl_GetPriority,
316     IDirect3DTexture9Impl_PreLoad,
317     IDirect3DTexture9Impl_GetType,
318     /* IDirect3dBaseTexture9 */
319     IDirect3DTexture9Impl_SetLOD,
320     IDirect3DTexture9Impl_GetLOD,
321     IDirect3DTexture9Impl_GetLevelCount,
322     IDirect3DTexture9Impl_SetAutoGenFilterType,
323     IDirect3DTexture9Impl_GetAutoGenFilterType,
324     IDirect3DTexture9Impl_GenerateMipSubLevels,
325     /* IDirect3DTexture9 */
326     IDirect3DTexture9Impl_GetLevelDesc,
327     IDirect3DTexture9Impl_GetSurfaceLevel,
328     IDirect3DTexture9Impl_LockRect,
329     IDirect3DTexture9Impl_UnlockRect,
330     IDirect3DTexture9Impl_AddDirtyRect
331 };
332
333
334 /* IDirect3DDevice9 IDirect3DTexture9 Methods follow: */
335 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateTexture(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
336                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, HANDLE* pSharedHandle) {
337     IDirect3DTexture9Impl *object;
338     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
339     HRESULT hrc = D3D_OK;
340     
341     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%d), Fmt(%#x), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
342
343     /* Allocate the storage for the device */
344     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture9Impl));
345
346     if (NULL == object) {
347         ERR("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
348         return D3DERR_OUTOFVIDEOMEMORY;
349     }
350
351     object->lpVtbl = &Direct3DTexture9_Vtbl;
352     object->ref = 1;
353     EnterCriticalSection(&d3d9_cs);
354     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage & WINED3DUSAGE_MASK,
355             wined3dformat_from_d3dformat(Format), Pool, &object->wineD3DTexture, pSharedHandle, (IUnknown *)object);
356     LeaveCriticalSection(&d3d9_cs);
357     if (FAILED(hrc)) {
358
359         /* free up object */
360         WARN("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
361         HeapFree(GetProcessHeap(), 0, object);
362    } else {
363         IDirect3DDevice9Ex_AddRef(iface);
364         object->parentDevice = iface;
365         *ppTexture= (LPDIRECT3DTEXTURE9) object;
366         TRACE("(%p) Created Texture %p, %p\n", This, object, object->wineD3DTexture);
367    }
368
369    return hrc;
370 }