d3d9/tests: Avoid leaking a device in the Reset test.
[wine] / dlls / d3d9 / volumetexture.c
1 /*
2  * IDirect3DVolumeTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 static inline IDirect3DVolumeTexture9Impl *impl_from_IDirect3DVolumeTexture9(IDirect3DVolumeTexture9 *iface)
28 {
29     return CONTAINING_RECORD(iface, IDirect3DVolumeTexture9Impl, IDirect3DVolumeTexture9_iface);
30 }
31
32 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_QueryInterface(IDirect3DVolumeTexture9 *iface,
33         REFIID riid, void **ppobj)
34 {
35     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
36
37     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
38
39     if (IsEqualGUID(riid, &IID_IUnknown)
40     || IsEqualGUID(riid, &IID_IDirect3DResource9)
41     || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
42     || IsEqualGUID(riid, &IID_IDirect3DVolumeTexture9)) {
43         IDirect3DVolumeTexture9_AddRef(iface);
44         *ppobj = This;
45         return S_OK;
46     }
47
48     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
49     *ppobj = NULL;
50     return E_NOINTERFACE;
51 }
52
53 static ULONG WINAPI IDirect3DVolumeTexture9Impl_AddRef(IDirect3DVolumeTexture9 *iface)
54 {
55     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
56     ULONG ref = InterlockedIncrement(&This->ref);
57
58     TRACE("%p increasing refcount to %u.\n", iface, ref);
59
60     if (ref == 1)
61     {
62         IDirect3DDevice9Ex_AddRef(This->parentDevice);
63         wined3d_mutex_lock();
64         wined3d_texture_incref(This->wined3d_texture);
65         wined3d_mutex_unlock();
66     }
67
68     return ref;
69 }
70
71 static ULONG WINAPI IDirect3DVolumeTexture9Impl_Release(IDirect3DVolumeTexture9 *iface)
72 {
73     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
74     ULONG ref = InterlockedDecrement(&This->ref);
75
76     TRACE("%p decreasing refcount to %u.\n", iface, ref);
77
78     if (ref == 0) {
79         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
80
81         wined3d_mutex_lock();
82         wined3d_texture_decref(This->wined3d_texture);
83         wined3d_mutex_unlock();
84
85         /* Release the device last, as it may cause the device to be destroyed. */
86         IDirect3DDevice9Ex_Release(parentDevice);
87     }
88     return ref;
89 }
90
91 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetDevice(IDirect3DVolumeTexture9 *iface,
92         IDirect3DDevice9 **device)
93 {
94     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
95
96     TRACE("iface %p, device %p.\n", iface, device);
97
98     *device = (IDirect3DDevice9 *)This->parentDevice;
99     IDirect3DDevice9_AddRef(*device);
100
101     TRACE("Returning device %p.\n", *device);
102
103     return D3D_OK;
104 }
105
106 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_SetPrivateData(IDirect3DVolumeTexture9 *iface,
107         REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
108 {
109     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
110     HRESULT hr;
111
112     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
113             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
114
115     wined3d_mutex_lock();
116     hr = wined3d_texture_set_private_data(This->wined3d_texture, refguid, pData, SizeOfData, Flags);
117     wined3d_mutex_unlock();
118
119     return hr;
120 }
121
122 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetPrivateData(IDirect3DVolumeTexture9 *iface,
123         REFGUID refguid, void *pData, DWORD *pSizeOfData)
124 {
125     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
126     HRESULT hr;
127
128     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
129             iface, debugstr_guid(refguid), pData, pSizeOfData);
130
131     wined3d_mutex_lock();
132     hr = wined3d_texture_get_private_data(This->wined3d_texture, refguid, pData, pSizeOfData);
133     wined3d_mutex_unlock();
134
135     return hr;
136 }
137
138 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_FreePrivateData(IDirect3DVolumeTexture9 *iface,
139         REFGUID refguid)
140 {
141     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
142     HRESULT hr;
143
144     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
145
146     wined3d_mutex_lock();
147     hr = wined3d_texture_free_private_data(This->wined3d_texture, refguid);
148     wined3d_mutex_unlock();
149
150     return hr;
151 }
152
153 static DWORD WINAPI IDirect3DVolumeTexture9Impl_SetPriority(IDirect3DVolumeTexture9 *iface,
154         DWORD PriorityNew)
155 {
156     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
157     DWORD priority;
158
159     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
160
161     wined3d_mutex_lock();
162     priority = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
163     wined3d_mutex_unlock();
164
165     return priority;
166 }
167
168 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetPriority(IDirect3DVolumeTexture9 *iface)
169 {
170     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
171     DWORD priority;
172
173     TRACE("iface %p.\n", iface);
174
175     wined3d_mutex_lock();
176     priority = wined3d_texture_get_priority(This->wined3d_texture);
177     wined3d_mutex_unlock();
178
179     return priority;
180 }
181
182 static void WINAPI IDirect3DVolumeTexture9Impl_PreLoad(IDirect3DVolumeTexture9 *iface)
183 {
184     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
185
186     TRACE("iface %p.\n", iface);
187
188     wined3d_mutex_lock();
189     wined3d_texture_preload(This->wined3d_texture);
190     wined3d_mutex_unlock();
191 }
192
193 static D3DRESOURCETYPE WINAPI IDirect3DVolumeTexture9Impl_GetType(IDirect3DVolumeTexture9 *iface)
194 {
195     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
196     D3DRESOURCETYPE type;
197
198     TRACE("iface %p.\n", iface);
199
200     wined3d_mutex_lock();
201     type = wined3d_texture_get_type(This->wined3d_texture);
202     wined3d_mutex_unlock();
203
204     return type;
205 }
206
207 static DWORD WINAPI IDirect3DVolumeTexture9Impl_SetLOD(IDirect3DVolumeTexture9 *iface, DWORD LODNew)
208 {
209     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
210     DWORD lod;
211
212     TRACE("iface %p, lod %u.\n", iface, LODNew);
213
214     wined3d_mutex_lock();
215     lod = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
216     wined3d_mutex_unlock();
217
218     return lod;
219 }
220
221 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetLOD(IDirect3DVolumeTexture9 *iface)
222 {
223     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
224     DWORD lod;
225
226     TRACE("iface %p.\n", iface);
227
228     wined3d_mutex_lock();
229     lod = wined3d_texture_get_lod(This->wined3d_texture);
230     wined3d_mutex_unlock();
231
232     return lod;
233 }
234
235 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetLevelCount(IDirect3DVolumeTexture9 *iface)
236 {
237     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
238     DWORD level_count;
239
240     TRACE("iface %p.\n", iface);
241
242     wined3d_mutex_lock();
243     level_count = wined3d_texture_get_level_count(This->wined3d_texture);
244     wined3d_mutex_unlock();
245
246     return level_count;
247 }
248
249 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_SetAutoGenFilterType(IDirect3DVolumeTexture9 *iface,
250         D3DTEXTUREFILTERTYPE FilterType)
251 {
252     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
253     HRESULT hr;
254
255     TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
256
257     wined3d_mutex_lock();
258     hr = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (WINED3DTEXTUREFILTERTYPE)FilterType);
259     wined3d_mutex_unlock();
260
261     return hr;
262 }
263
264 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DVolumeTexture9Impl_GetAutoGenFilterType(IDirect3DVolumeTexture9 *iface)
265 {
266     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
267     D3DTEXTUREFILTERTYPE filter_type;
268
269     TRACE("iface %p.\n", iface);
270
271     wined3d_mutex_lock();
272     filter_type = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
273     wined3d_mutex_unlock();
274
275     return filter_type;
276 }
277
278 static void WINAPI IDirect3DVolumeTexture9Impl_GenerateMipSubLevels(IDirect3DVolumeTexture9 *iface)
279 {
280     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
281
282     TRACE("iface %p.\n", iface);
283
284     wined3d_mutex_lock();
285     wined3d_texture_generate_mipmaps(This->wined3d_texture);
286     wined3d_mutex_unlock();
287 }
288
289 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetLevelDesc(IDirect3DVolumeTexture9 *iface,
290         UINT level, D3DVOLUME_DESC *desc)
291 {
292     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
293     struct wined3d_resource *sub_resource;
294     HRESULT hr = D3D_OK;
295
296     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
297
298     wined3d_mutex_lock();
299     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
300         hr = D3DERR_INVALIDCALL;
301     else
302     {
303         struct wined3d_resource_desc wined3d_desc;
304
305         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
306         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
307         desc->Type = wined3d_desc.resource_type;
308         desc->Usage = wined3d_desc.usage;
309         desc->Pool = wined3d_desc.pool;
310         desc->Width = wined3d_desc.width;
311         desc->Height = wined3d_desc.height;
312         desc->Depth = wined3d_desc.depth;
313     }
314     wined3d_mutex_unlock();
315
316     return hr;
317 }
318
319 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetVolumeLevel(IDirect3DVolumeTexture9 *iface,
320         UINT level, IDirect3DVolume9 **volume)
321 {
322     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
323     struct wined3d_resource *sub_resource;
324
325     TRACE("iface %p, level %u, volume %p.\n", iface, level, volume);
326
327     wined3d_mutex_lock();
328     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
329     {
330         wined3d_mutex_unlock();
331         return D3DERR_INVALIDCALL;
332     }
333
334     *volume = wined3d_resource_get_parent(sub_resource);
335     IDirect3DVolume9_AddRef(*volume);
336     wined3d_mutex_unlock();
337
338     return D3D_OK;
339 }
340
341 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_LockBox(IDirect3DVolumeTexture9 *iface,
342         UINT level, D3DLOCKED_BOX *locked_box, const D3DBOX *box, DWORD flags)
343 {
344     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
345     struct wined3d_resource *sub_resource;
346     HRESULT hr;
347
348     TRACE("iface %p, level %u, locked_box %p, box %p, flags %#x.\n",
349             iface, level, locked_box, box, flags);
350
351     wined3d_mutex_lock();
352     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
353         hr = D3DERR_INVALIDCALL;
354     else
355         hr = IDirect3DVolume9_LockBox((IDirect3DVolume9 *)wined3d_resource_get_parent(sub_resource),
356                 locked_box, box, flags);
357     wined3d_mutex_unlock();
358
359     return hr;
360 }
361
362 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_UnlockBox(IDirect3DVolumeTexture9 *iface,
363         UINT level)
364 {
365     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
366     struct wined3d_resource *sub_resource;
367     HRESULT hr;
368
369     TRACE("iface %p, level %u.\n", iface, level);
370
371     wined3d_mutex_lock();
372     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
373         hr = D3DERR_INVALIDCALL;
374     else
375         hr = IDirect3DVolume9_UnlockBox((IDirect3DVolume9 *)wined3d_resource_get_parent(sub_resource));
376     wined3d_mutex_unlock();
377
378     return hr;
379 }
380
381 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_AddDirtyBox(IDirect3DVolumeTexture9 *iface,
382         const D3DBOX *dirty_box)
383 {
384     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
385     HRESULT hr;
386
387     TRACE("iface %p, dirty_box %p.\n", iface, dirty_box);
388
389     wined3d_mutex_lock();
390     hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, (const WINED3DBOX *)dirty_box);
391     wined3d_mutex_unlock();
392
393     return hr;
394 }
395
396
397 static const IDirect3DVolumeTexture9Vtbl Direct3DVolumeTexture9_Vtbl =
398 {
399     /* IUnknown */
400     IDirect3DVolumeTexture9Impl_QueryInterface,
401     IDirect3DVolumeTexture9Impl_AddRef,
402     IDirect3DVolumeTexture9Impl_Release,
403     /* IDirect3DResource9 */
404     IDirect3DVolumeTexture9Impl_GetDevice,
405     IDirect3DVolumeTexture9Impl_SetPrivateData,
406     IDirect3DVolumeTexture9Impl_GetPrivateData,
407     IDirect3DVolumeTexture9Impl_FreePrivateData,
408     IDirect3DVolumeTexture9Impl_SetPriority,
409     IDirect3DVolumeTexture9Impl_GetPriority,
410     IDirect3DVolumeTexture9Impl_PreLoad,
411     IDirect3DVolumeTexture9Impl_GetType,
412     /* IDirect3DBaseTexture9 */
413     IDirect3DVolumeTexture9Impl_SetLOD,
414     IDirect3DVolumeTexture9Impl_GetLOD,
415     IDirect3DVolumeTexture9Impl_GetLevelCount,
416     IDirect3DVolumeTexture9Impl_SetAutoGenFilterType,
417     IDirect3DVolumeTexture9Impl_GetAutoGenFilterType,
418     IDirect3DVolumeTexture9Impl_GenerateMipSubLevels,
419     /* IDirect3DVolumeTexture9 */
420     IDirect3DVolumeTexture9Impl_GetLevelDesc,
421     IDirect3DVolumeTexture9Impl_GetVolumeLevel,
422     IDirect3DVolumeTexture9Impl_LockBox,
423     IDirect3DVolumeTexture9Impl_UnlockBox,
424     IDirect3DVolumeTexture9Impl_AddDirtyBox
425 };
426
427 static void STDMETHODCALLTYPE volumetexture_wined3d_object_destroyed(void *parent)
428 {
429     HeapFree(GetProcessHeap(), 0, parent);
430 }
431
432 static const struct wined3d_parent_ops d3d9_volumetexture_wined3d_parent_ops =
433 {
434     volumetexture_wined3d_object_destroyed,
435 };
436
437 HRESULT volumetexture_init(IDirect3DVolumeTexture9Impl *texture, IDirect3DDevice9Impl *device,
438         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
439 {
440     HRESULT hr;
441
442     texture->IDirect3DVolumeTexture9_iface.lpVtbl = &Direct3DVolumeTexture9_Vtbl;
443     texture->ref = 1;
444
445     wined3d_mutex_lock();
446     hr = IWineD3DDevice_CreateVolumeTexture(device->WineD3DDevice, width, height, depth, levels,
447             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool, texture,
448             &d3d9_volumetexture_wined3d_parent_ops, &texture->wined3d_texture);
449     wined3d_mutex_unlock();
450     if (FAILED(hr))
451     {
452         WARN("Failed to create wined3d volume texture, hr %#x.\n", hr);
453         return hr;
454     }
455
456     texture->parentDevice = &device->IDirect3DDevice9Ex_iface;
457     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
458
459     return D3D_OK;
460 }