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