wined3d: Generalize AddDirtyRect() / AddDirtyBox() to AddDirtyRegion().
[wine] / dlls / d3d8 / volumetexture.c
1 /*
2  * IDirect3DVolumeTexture8 implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 /* IDirect3DVolumeTexture8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_QueryInterface(LPDIRECT3DVOLUMETEXTURE8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
29
30     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33     || IsEqualGUID(riid, &IID_IDirect3DResource8)
34     || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
35     || IsEqualGUID(riid, &IID_IDirect3DVolumeTexture8)) {
36         IUnknown_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 IDirect3DVolumeTexture8Impl_AddRef(LPDIRECT3DVOLUMETEXTURE8 iface) {
47     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
48     ULONG ref = InterlockedIncrement(&This->ref);
49
50     TRACE("%p increasing refcount to %u.\n", iface, ref);
51
52     if (ref == 1)
53     {
54         IDirect3DDevice8_AddRef(This->parentDevice);
55         wined3d_mutex_lock();
56         IWineD3DVolumeTexture_AddRef(This->wineD3DVolumeTexture);
57         wined3d_mutex_unlock();
58     }
59
60     return ref;
61 }
62
63 static ULONG WINAPI IDirect3DVolumeTexture8Impl_Release(LPDIRECT3DVOLUMETEXTURE8 iface) {
64     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
65     ULONG ref = InterlockedDecrement(&This->ref);
66
67     TRACE("%p decreasing refcount to %u.\n", iface, ref);
68
69     if (ref == 0) {
70         IDirect3DDevice8 *parentDevice = This->parentDevice;
71
72         wined3d_mutex_lock();
73         IWineD3DVolumeTexture_Release(This->wineD3DVolumeTexture);
74         wined3d_mutex_unlock();
75
76         /* Release the device last, as it may cause the device to be destroyed. */
77         IDirect3DDevice8_Release(parentDevice);
78     }
79     return ref;
80 }
81
82 /* IDirect3DVolumeTexture8 IDirect3DResource8 Interface follow: */
83 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_GetDevice(IDirect3DVolumeTexture8 *iface, IDirect3DDevice8 **device)
84 {
85     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
86
87     TRACE("iface %p, device %p.\n", iface, device);
88
89     *device = (IDirect3DDevice8 *)This->parentDevice;
90     IDirect3DDevice8_AddRef(*device);
91
92     TRACE("Returning device %p.\n", *device);
93
94     return D3D_OK;
95 }
96
97 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_SetPrivateData(LPDIRECT3DVOLUMETEXTURE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
98     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
99     HRESULT hr;
100
101     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
102             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
103
104     wined3d_mutex_lock();
105     hr = IWineD3DVolumeTexture_SetPrivateData(This->wineD3DVolumeTexture, refguid, pData, SizeOfData, Flags);
106     wined3d_mutex_unlock();
107
108     return hr;
109 }
110
111 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_GetPrivateData(LPDIRECT3DVOLUMETEXTURE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
112     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
113     HRESULT hr;
114
115     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
116             iface, debugstr_guid(refguid), pData, pSizeOfData);
117
118     wined3d_mutex_lock();
119     hr = IWineD3DVolumeTexture_GetPrivateData(This->wineD3DVolumeTexture, refguid, pData, pSizeOfData);
120     wined3d_mutex_unlock();
121
122     return hr;
123 }
124
125 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_FreePrivateData(LPDIRECT3DVOLUMETEXTURE8 iface, REFGUID refguid) {
126     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
127     HRESULT hr;
128
129     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
130
131     wined3d_mutex_lock();
132     hr = IWineD3DVolumeTexture_FreePrivateData(This->wineD3DVolumeTexture, refguid);
133     wined3d_mutex_unlock();
134
135     return hr;
136 }
137
138 static DWORD WINAPI IDirect3DVolumeTexture8Impl_SetPriority(LPDIRECT3DVOLUMETEXTURE8 iface, DWORD PriorityNew) {
139     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
140     DWORD ret;
141
142     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
143
144     wined3d_mutex_lock();
145     ret = IWineD3DVolumeTexture_SetPriority(This->wineD3DVolumeTexture, PriorityNew);
146     wined3d_mutex_unlock();
147
148     return ret;
149 }
150
151 static DWORD WINAPI IDirect3DVolumeTexture8Impl_GetPriority(LPDIRECT3DVOLUMETEXTURE8 iface) {
152     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
153     DWORD ret;
154
155     TRACE("iface %p.\n", iface);
156
157     wined3d_mutex_lock();
158     ret = IWineD3DVolumeTexture_GetPriority(This->wineD3DVolumeTexture);
159     wined3d_mutex_unlock();
160
161     return ret;
162 }
163
164 static void WINAPI IDirect3DVolumeTexture8Impl_PreLoad(LPDIRECT3DVOLUMETEXTURE8 iface) {
165     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
166
167     TRACE("iface %p.\n", iface);
168
169     wined3d_mutex_lock();
170     IWineD3DVolumeTexture_PreLoad(This->wineD3DVolumeTexture);
171     wined3d_mutex_unlock();
172 }
173
174 static D3DRESOURCETYPE WINAPI IDirect3DVolumeTexture8Impl_GetType(LPDIRECT3DVOLUMETEXTURE8 iface) {
175     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
176     D3DRESOURCETYPE type;
177
178     TRACE("iface %p.\n", iface);
179
180     wined3d_mutex_lock();
181     type = IWineD3DVolumeTexture_GetType(This->wineD3DVolumeTexture);
182     wined3d_mutex_unlock();
183
184     return type;
185 }
186
187 /* IDirect3DVolumeTexture8 IDirect3DBaseTexture8 Interface follow: */
188 static DWORD WINAPI IDirect3DVolumeTexture8Impl_SetLOD(LPDIRECT3DVOLUMETEXTURE8 iface, DWORD LODNew) {
189     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
190     DWORD ret;
191
192     TRACE("iface %p, lod %u.\n", iface, LODNew);
193
194     wined3d_mutex_lock();
195     ret = IWineD3DVolumeTexture_SetLOD(This->wineD3DVolumeTexture, LODNew);
196     wined3d_mutex_unlock();
197
198     return ret;
199 }
200
201 static DWORD WINAPI IDirect3DVolumeTexture8Impl_GetLOD(LPDIRECT3DVOLUMETEXTURE8 iface) {
202     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
203     DWORD ret;
204
205     TRACE("iface %p.\n", iface);
206
207     wined3d_mutex_lock();
208     ret = IWineD3DVolumeTexture_GetLOD(This->wineD3DVolumeTexture);
209     wined3d_mutex_unlock();
210
211     return ret;
212 }
213
214 static DWORD WINAPI IDirect3DVolumeTexture8Impl_GetLevelCount(LPDIRECT3DVOLUMETEXTURE8 iface) {
215     IDirect3DVolumeTexture8Impl *This = (IDirect3DVolumeTexture8Impl *)iface;
216     DWORD ret;
217
218     TRACE("iface %p.\n", iface);
219
220     wined3d_mutex_lock();
221     ret = IWineD3DVolumeTexture_GetLevelCount(This->wineD3DVolumeTexture);
222     wined3d_mutex_unlock();
223
224     return ret;
225 }
226
227 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_GetLevelDesc(IDirect3DVolumeTexture8 *iface,
228         UINT level, D3DVOLUME_DESC *desc)
229 {
230     IDirect3DVolumeTexture8Impl *texture = (IDirect3DVolumeTexture8Impl *)iface;
231     struct wined3d_resource *sub_resource;
232     HRESULT hr = D3D_OK;
233
234     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
235
236     wined3d_mutex_lock();
237     if (!(sub_resource = IWineD3DVolumeTexture_GetSubResource(texture->wineD3DVolumeTexture, level)))
238         hr = D3DERR_INVALIDCALL;
239     else
240     {
241         struct wined3d_resource_desc wined3d_desc;
242
243         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
244         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
245         desc->Type = wined3d_desc.resource_type;
246         desc->Usage = wined3d_desc.usage;
247         desc->Pool = wined3d_desc.pool;
248         desc->Size = wined3d_desc.size;
249         desc->Width = wined3d_desc.width;
250         desc->Height = wined3d_desc.height;
251         desc->Depth = wined3d_desc.depth;
252     }
253     wined3d_mutex_unlock();
254
255     return hr;
256 }
257
258 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_GetVolumeLevel(IDirect3DVolumeTexture8 *iface,
259         UINT level, IDirect3DVolume8 **volume)
260 {
261     IDirect3DVolumeTexture8Impl *texture = (IDirect3DVolumeTexture8Impl *)iface;
262     struct wined3d_resource *sub_resource;
263
264     TRACE("iface %p, level %u, volume %p.\n", iface, level, volume);
265
266     wined3d_mutex_lock();
267     if (!(sub_resource = IWineD3DVolumeTexture_GetSubResource(texture->wineD3DVolumeTexture, level)))
268     {
269         wined3d_mutex_unlock();
270         return D3DERR_INVALIDCALL;
271     }
272
273     *volume = wined3d_resource_get_parent(sub_resource);
274     IDirect3DVolume8_AddRef(*volume);
275     wined3d_mutex_unlock();
276
277     return D3D_OK;
278 }
279
280 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_LockBox(IDirect3DVolumeTexture8 *iface,
281         UINT level, D3DLOCKED_BOX *locked_box, const D3DBOX *box, DWORD flags)
282 {
283     IDirect3DVolumeTexture8Impl *texture = (IDirect3DVolumeTexture8Impl *)iface;
284     struct wined3d_resource *sub_resource;
285     HRESULT hr;
286
287     TRACE("iface %p, level %u, locked_box %p, box %p, flags %#x.\n",
288             iface, level, locked_box, box, flags);
289
290     wined3d_mutex_lock();
291     if (!(sub_resource = IWineD3DVolumeTexture_GetSubResource(texture->wineD3DVolumeTexture, level)))
292         hr = D3DERR_INVALIDCALL;
293     else
294         hr = IDirect3DVolume8_LockBox((IDirect3DVolume8 *)wined3d_resource_get_parent(sub_resource),
295                 locked_box, box, flags);
296     wined3d_mutex_unlock();
297
298     return hr;
299 }
300
301 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_UnlockBox(IDirect3DVolumeTexture8 *iface, UINT level)
302 {
303     IDirect3DVolumeTexture8Impl *texture = (IDirect3DVolumeTexture8Impl *)iface;
304     struct wined3d_resource *sub_resource;
305     HRESULT hr;
306
307     TRACE("iface %p, level %u.\n", iface, level);
308
309     wined3d_mutex_lock();
310     if (!(sub_resource = IWineD3DVolumeTexture_GetSubResource(texture->wineD3DVolumeTexture, level)))
311         hr = D3DERR_INVALIDCALL;
312     else
313         hr = IDirect3DVolume8_UnlockBox((IDirect3DVolume8 *)wined3d_resource_get_parent(sub_resource));
314     wined3d_mutex_unlock();
315
316     return hr;
317 }
318
319 static HRESULT WINAPI IDirect3DVolumeTexture8Impl_AddDirtyBox(IDirect3DVolumeTexture8 *iface,
320         const D3DBOX *dirty_box)
321 {
322     IDirect3DVolumeTexture8Impl *texture = (IDirect3DVolumeTexture8Impl *)iface;
323     HRESULT hr;
324
325     TRACE("iface %p, dirty_box %p.\n", iface, dirty_box);
326
327     wined3d_mutex_lock();
328     hr = IWineD3DVolumeTexture_AddDirtyRegion(texture->wineD3DVolumeTexture, 0, (const WINED3DBOX *)dirty_box);
329     wined3d_mutex_unlock();
330
331     return hr;
332 }
333
334 static const IDirect3DVolumeTexture8Vtbl Direct3DVolumeTexture8_Vtbl =
335 {
336     /* IUnknown */
337     IDirect3DVolumeTexture8Impl_QueryInterface,
338     IDirect3DVolumeTexture8Impl_AddRef,
339     IDirect3DVolumeTexture8Impl_Release,
340     /* IDirect3DResource8 */
341     IDirect3DVolumeTexture8Impl_GetDevice,
342     IDirect3DVolumeTexture8Impl_SetPrivateData,
343     IDirect3DVolumeTexture8Impl_GetPrivateData,
344     IDirect3DVolumeTexture8Impl_FreePrivateData,
345     IDirect3DVolumeTexture8Impl_SetPriority,
346     IDirect3DVolumeTexture8Impl_GetPriority,
347     IDirect3DVolumeTexture8Impl_PreLoad,
348     IDirect3DVolumeTexture8Impl_GetType,
349     /* IDirect3DBaseTexture8 */
350     IDirect3DVolumeTexture8Impl_SetLOD,
351     IDirect3DVolumeTexture8Impl_GetLOD,
352     IDirect3DVolumeTexture8Impl_GetLevelCount,
353     /* IDirect3DVolumeTexture8 */
354     IDirect3DVolumeTexture8Impl_GetLevelDesc,
355     IDirect3DVolumeTexture8Impl_GetVolumeLevel,
356     IDirect3DVolumeTexture8Impl_LockBox,
357     IDirect3DVolumeTexture8Impl_UnlockBox,
358     IDirect3DVolumeTexture8Impl_AddDirtyBox
359 };
360
361 static void STDMETHODCALLTYPE volumetexture_wined3d_object_destroyed(void *parent)
362 {
363     HeapFree(GetProcessHeap(), 0, parent);
364 }
365
366 static const struct wined3d_parent_ops d3d8_volumetexture_wined3d_parent_ops =
367 {
368     volumetexture_wined3d_object_destroyed,
369 };
370
371 HRESULT volumetexture_init(IDirect3DVolumeTexture8Impl *texture, IDirect3DDevice8Impl *device,
372         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
373 {
374     HRESULT hr;
375
376     texture->lpVtbl = &Direct3DVolumeTexture8_Vtbl;
377     texture->ref = 1;
378
379     wined3d_mutex_lock();
380     hr = IWineD3DDevice_CreateVolumeTexture(device->WineD3DDevice, width, height, depth, levels,
381             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool, texture,
382             &d3d8_volumetexture_wined3d_parent_ops, &texture->wineD3DVolumeTexture);
383     wined3d_mutex_unlock();
384     if (FAILED(hr))
385     {
386         WARN("Failed to create wined3d volume texture, hr %#x.\n", hr);
387         return hr;
388     }
389
390     texture->parentDevice = &device->IDirect3DDevice8_iface;
391     IDirect3DDevice8_AddRef(texture->parentDevice);
392
393     return D3D_OK;
394 }