wined3d: Get rid of the WINED3DBOX typedef.
[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     struct wined3d_resource *resource;
111     HRESULT hr;
112
113     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
114             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
115
116     wined3d_mutex_lock();
117     resource = wined3d_texture_get_resource(This->wined3d_texture);
118     hr = wined3d_resource_set_private_data(resource, refguid, pData, SizeOfData, Flags);
119     wined3d_mutex_unlock();
120
121     return hr;
122 }
123
124 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetPrivateData(IDirect3DVolumeTexture9 *iface,
125         REFGUID refguid, void *pData, DWORD *pSizeOfData)
126 {
127     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
128     struct wined3d_resource *resource;
129     HRESULT hr;
130
131     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
132             iface, debugstr_guid(refguid), pData, pSizeOfData);
133
134     wined3d_mutex_lock();
135     resource = wined3d_texture_get_resource(This->wined3d_texture);
136     hr = wined3d_resource_get_private_data(resource, refguid, pData, pSizeOfData);
137     wined3d_mutex_unlock();
138
139     return hr;
140 }
141
142 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_FreePrivateData(IDirect3DVolumeTexture9 *iface,
143         REFGUID refguid)
144 {
145     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
146     struct wined3d_resource *resource;
147     HRESULT hr;
148
149     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
150
151     wined3d_mutex_lock();
152     resource = wined3d_texture_get_resource(This->wined3d_texture);
153     hr = wined3d_resource_free_private_data(resource, refguid);
154     wined3d_mutex_unlock();
155
156     return hr;
157 }
158
159 static DWORD WINAPI IDirect3DVolumeTexture9Impl_SetPriority(IDirect3DVolumeTexture9 *iface,
160         DWORD PriorityNew)
161 {
162     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
163     DWORD priority;
164
165     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
166
167     wined3d_mutex_lock();
168     priority = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
169     wined3d_mutex_unlock();
170
171     return priority;
172 }
173
174 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetPriority(IDirect3DVolumeTexture9 *iface)
175 {
176     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
177     DWORD priority;
178
179     TRACE("iface %p.\n", iface);
180
181     wined3d_mutex_lock();
182     priority = wined3d_texture_get_priority(This->wined3d_texture);
183     wined3d_mutex_unlock();
184
185     return priority;
186 }
187
188 static void WINAPI IDirect3DVolumeTexture9Impl_PreLoad(IDirect3DVolumeTexture9 *iface)
189 {
190     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
191
192     TRACE("iface %p.\n", iface);
193
194     wined3d_mutex_lock();
195     wined3d_texture_preload(This->wined3d_texture);
196     wined3d_mutex_unlock();
197 }
198
199 static D3DRESOURCETYPE WINAPI IDirect3DVolumeTexture9Impl_GetType(IDirect3DVolumeTexture9 *iface)
200 {
201     TRACE("iface %p.\n", iface);
202
203     return D3DRTYPE_VOLUMETEXTURE;
204 }
205
206 static DWORD WINAPI IDirect3DVolumeTexture9Impl_SetLOD(IDirect3DVolumeTexture9 *iface, DWORD LODNew)
207 {
208     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
209     DWORD lod;
210
211     TRACE("iface %p, lod %u.\n", iface, LODNew);
212
213     wined3d_mutex_lock();
214     lod = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
215     wined3d_mutex_unlock();
216
217     return lod;
218 }
219
220 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetLOD(IDirect3DVolumeTexture9 *iface)
221 {
222     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
223     DWORD lod;
224
225     TRACE("iface %p.\n", iface);
226
227     wined3d_mutex_lock();
228     lod = wined3d_texture_get_lod(This->wined3d_texture);
229     wined3d_mutex_unlock();
230
231     return lod;
232 }
233
234 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetLevelCount(IDirect3DVolumeTexture9 *iface)
235 {
236     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
237     DWORD level_count;
238
239     TRACE("iface %p.\n", iface);
240
241     wined3d_mutex_lock();
242     level_count = wined3d_texture_get_level_count(This->wined3d_texture);
243     wined3d_mutex_unlock();
244
245     return level_count;
246 }
247
248 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_SetAutoGenFilterType(IDirect3DVolumeTexture9 *iface,
249         D3DTEXTUREFILTERTYPE FilterType)
250 {
251     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
252     HRESULT hr;
253
254     TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
255
256     wined3d_mutex_lock();
257     hr = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (WINED3DTEXTUREFILTERTYPE)FilterType);
258     wined3d_mutex_unlock();
259
260     return hr;
261 }
262
263 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DVolumeTexture9Impl_GetAutoGenFilterType(IDirect3DVolumeTexture9 *iface)
264 {
265     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
266     D3DTEXTUREFILTERTYPE filter_type;
267
268     TRACE("iface %p.\n", iface);
269
270     wined3d_mutex_lock();
271     filter_type = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
272     wined3d_mutex_unlock();
273
274     return filter_type;
275 }
276
277 static void WINAPI IDirect3DVolumeTexture9Impl_GenerateMipSubLevels(IDirect3DVolumeTexture9 *iface)
278 {
279     IDirect3DVolumeTexture9Impl *This = impl_from_IDirect3DVolumeTexture9(iface);
280
281     TRACE("iface %p.\n", iface);
282
283     wined3d_mutex_lock();
284     wined3d_texture_generate_mipmaps(This->wined3d_texture);
285     wined3d_mutex_unlock();
286 }
287
288 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetLevelDesc(IDirect3DVolumeTexture9 *iface,
289         UINT level, D3DVOLUME_DESC *desc)
290 {
291     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
292     struct wined3d_resource *sub_resource;
293     HRESULT hr = D3D_OK;
294
295     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
296
297     wined3d_mutex_lock();
298     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
299         hr = D3DERR_INVALIDCALL;
300     else
301     {
302         struct wined3d_resource_desc wined3d_desc;
303
304         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
305         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
306         desc->Type = wined3d_desc.resource_type;
307         desc->Usage = wined3d_desc.usage;
308         desc->Pool = wined3d_desc.pool;
309         desc->Width = wined3d_desc.width;
310         desc->Height = wined3d_desc.height;
311         desc->Depth = wined3d_desc.depth;
312     }
313     wined3d_mutex_unlock();
314
315     return hr;
316 }
317
318 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetVolumeLevel(IDirect3DVolumeTexture9 *iface,
319         UINT level, IDirect3DVolume9 **volume)
320 {
321     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
322     struct wined3d_resource *sub_resource;
323
324     TRACE("iface %p, level %u, volume %p.\n", iface, level, volume);
325
326     wined3d_mutex_lock();
327     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
328     {
329         wined3d_mutex_unlock();
330         return D3DERR_INVALIDCALL;
331     }
332
333     *volume = wined3d_resource_get_parent(sub_resource);
334     IDirect3DVolume9_AddRef(*volume);
335     wined3d_mutex_unlock();
336
337     return D3D_OK;
338 }
339
340 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_LockBox(IDirect3DVolumeTexture9 *iface,
341         UINT level, D3DLOCKED_BOX *locked_box, const D3DBOX *box, DWORD flags)
342 {
343     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
344     struct wined3d_resource *sub_resource;
345     HRESULT hr;
346
347     TRACE("iface %p, level %u, locked_box %p, box %p, flags %#x.\n",
348             iface, level, locked_box, box, flags);
349
350     wined3d_mutex_lock();
351     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
352         hr = D3DERR_INVALIDCALL;
353     else
354         hr = IDirect3DVolume9_LockBox((IDirect3DVolume9 *)wined3d_resource_get_parent(sub_resource),
355                 locked_box, box, flags);
356     wined3d_mutex_unlock();
357
358     return hr;
359 }
360
361 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_UnlockBox(IDirect3DVolumeTexture9 *iface,
362         UINT level)
363 {
364     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
365     struct wined3d_resource *sub_resource;
366     HRESULT hr;
367
368     TRACE("iface %p, level %u.\n", iface, level);
369
370     wined3d_mutex_lock();
371     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
372         hr = D3DERR_INVALIDCALL;
373     else
374         hr = IDirect3DVolume9_UnlockBox((IDirect3DVolume9 *)wined3d_resource_get_parent(sub_resource));
375     wined3d_mutex_unlock();
376
377     return hr;
378 }
379
380 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_AddDirtyBox(IDirect3DVolumeTexture9 *iface,
381         const D3DBOX *dirty_box)
382 {
383     IDirect3DVolumeTexture9Impl *texture = impl_from_IDirect3DVolumeTexture9(iface);
384     HRESULT hr;
385
386     TRACE("iface %p, dirty_box %p.\n", iface, dirty_box);
387
388     wined3d_mutex_lock();
389     hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, (const struct wined3d_box *)dirty_box);
390     wined3d_mutex_unlock();
391
392     return hr;
393 }
394
395
396 static const IDirect3DVolumeTexture9Vtbl Direct3DVolumeTexture9_Vtbl =
397 {
398     /* IUnknown */
399     IDirect3DVolumeTexture9Impl_QueryInterface,
400     IDirect3DVolumeTexture9Impl_AddRef,
401     IDirect3DVolumeTexture9Impl_Release,
402     /* IDirect3DResource9 */
403     IDirect3DVolumeTexture9Impl_GetDevice,
404     IDirect3DVolumeTexture9Impl_SetPrivateData,
405     IDirect3DVolumeTexture9Impl_GetPrivateData,
406     IDirect3DVolumeTexture9Impl_FreePrivateData,
407     IDirect3DVolumeTexture9Impl_SetPriority,
408     IDirect3DVolumeTexture9Impl_GetPriority,
409     IDirect3DVolumeTexture9Impl_PreLoad,
410     IDirect3DVolumeTexture9Impl_GetType,
411     /* IDirect3DBaseTexture9 */
412     IDirect3DVolumeTexture9Impl_SetLOD,
413     IDirect3DVolumeTexture9Impl_GetLOD,
414     IDirect3DVolumeTexture9Impl_GetLevelCount,
415     IDirect3DVolumeTexture9Impl_SetAutoGenFilterType,
416     IDirect3DVolumeTexture9Impl_GetAutoGenFilterType,
417     IDirect3DVolumeTexture9Impl_GenerateMipSubLevels,
418     /* IDirect3DVolumeTexture9 */
419     IDirect3DVolumeTexture9Impl_GetLevelDesc,
420     IDirect3DVolumeTexture9Impl_GetVolumeLevel,
421     IDirect3DVolumeTexture9Impl_LockBox,
422     IDirect3DVolumeTexture9Impl_UnlockBox,
423     IDirect3DVolumeTexture9Impl_AddDirtyBox
424 };
425
426 static void STDMETHODCALLTYPE volumetexture_wined3d_object_destroyed(void *parent)
427 {
428     HeapFree(GetProcessHeap(), 0, parent);
429 }
430
431 static const struct wined3d_parent_ops d3d9_volumetexture_wined3d_parent_ops =
432 {
433     volumetexture_wined3d_object_destroyed,
434 };
435
436 HRESULT volumetexture_init(IDirect3DVolumeTexture9Impl *texture, IDirect3DDevice9Impl *device,
437         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
438 {
439     HRESULT hr;
440
441     texture->IDirect3DVolumeTexture9_iface.lpVtbl = &Direct3DVolumeTexture9_Vtbl;
442     texture->ref = 1;
443
444     wined3d_mutex_lock();
445     hr = wined3d_texture_create_3d(device->wined3d_device, width, height, depth, levels,
446             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool, texture,
447             &d3d9_volumetexture_wined3d_parent_ops, &texture->wined3d_texture);
448     wined3d_mutex_unlock();
449     if (FAILED(hr))
450     {
451         WARN("Failed to create wined3d volume texture, hr %#x.\n", hr);
452         return hr;
453     }
454
455     texture->parentDevice = &device->IDirect3DDevice9Ex_iface;
456     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
457
458     return D3D_OK;
459 }