wined3d: Add filtering flag to more texture formats.
[wine] / dlls / d3d9 / cubetexture.c
1 /*
2  * IDirect3DCubeTexture9 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
29 static inline IDirect3DCubeTexture9Impl *impl_from_IDirect3DCubeTexture9(IDirect3DCubeTexture9 *iface)
30 {
31     return CONTAINING_RECORD(iface, IDirect3DCubeTexture9Impl, IDirect3DCubeTexture9_iface);
32 }
33
34 static HRESULT WINAPI IDirect3DCubeTexture9Impl_QueryInterface(IDirect3DCubeTexture9 *iface,
35         REFIID riid, void **ppobj)
36 {
37     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
38
39     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
40
41     if (IsEqualGUID(riid, &IID_IUnknown)
42         || IsEqualGUID(riid, &IID_IDirect3DResource9)
43         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
44         || IsEqualGUID(riid, &IID_IDirect3DCubeTexture9)) {
45         IDirect3DCubeTexture9_AddRef(iface);
46         *ppobj = This;
47         return S_OK;
48     }
49
50     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
51     *ppobj = NULL;
52     return E_NOINTERFACE;
53 }
54
55 static ULONG WINAPI IDirect3DCubeTexture9Impl_AddRef(IDirect3DCubeTexture9 *iface)
56 {
57     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
58     ULONG ref = InterlockedIncrement(&This->ref);
59
60     TRACE("%p increasing refcount to %u.\n", iface, ref);
61
62     if (ref == 1)
63     {
64         IDirect3DDevice9Ex_AddRef(This->parentDevice);
65         wined3d_mutex_lock();
66         wined3d_texture_incref(This->wined3d_texture);
67         wined3d_mutex_unlock();
68     }
69
70     return ref;
71 }
72
73 static ULONG WINAPI IDirect3DCubeTexture9Impl_Release(IDirect3DCubeTexture9 *iface)
74 {
75     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
76     ULONG ref = InterlockedDecrement(&This->ref);
77
78     TRACE("%p decreasing refcount to %u.\n", iface, ref);
79
80     if (ref == 0) {
81         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
82
83         TRACE("Releasing child %p.\n", This->wined3d_texture);
84
85         wined3d_mutex_lock();
86         wined3d_texture_decref(This->wined3d_texture);
87         wined3d_mutex_unlock();
88
89         /* Release the device last, as it may cause the device to be destroyed. */
90         IDirect3DDevice9Ex_Release(parentDevice);
91     }
92     return ref;
93 }
94
95 /* IDirect3DCubeTexture9 IDirect3DResource9 Interface follow: */
96 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetDevice(IDirect3DCubeTexture9 *iface,
97         IDirect3DDevice9 **device)
98 {
99     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
100
101     TRACE("iface %p, device %p.\n", iface, device);
102
103     *device = (IDirect3DDevice9 *)This->parentDevice;
104     IDirect3DDevice9_AddRef(*device);
105
106     TRACE("Returning device %p.\n", *device);
107
108     return D3D_OK;
109 }
110
111 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetPrivateData(IDirect3DCubeTexture9 *iface,
112         REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
113 {
114     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
115     struct wined3d_resource *resource;
116     HRESULT hr;
117
118     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
119             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
120
121     wined3d_mutex_lock();
122     resource = wined3d_texture_get_resource(This->wined3d_texture);
123     hr = wined3d_resource_set_private_data(resource, refguid, pData, SizeOfData, Flags);
124     wined3d_mutex_unlock();
125
126     return hr;
127 }
128
129 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetPrivateData(IDirect3DCubeTexture9 *iface,
130         REFGUID refguid, void *pData, DWORD *pSizeOfData)
131 {
132     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
133     struct wined3d_resource *resource;
134     HRESULT hr;
135
136     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
137             iface, debugstr_guid(refguid), pData, pSizeOfData);
138
139     wined3d_mutex_lock();
140     resource = wined3d_texture_get_resource(This->wined3d_texture);
141     hr = wined3d_resource_get_private_data(resource, refguid, pData, pSizeOfData);
142     wined3d_mutex_unlock();
143
144     return hr;
145 }
146
147 static HRESULT WINAPI IDirect3DCubeTexture9Impl_FreePrivateData(IDirect3DCubeTexture9 *iface,
148         REFGUID refguid)
149 {
150     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
151     struct wined3d_resource *resource;
152     HRESULT hr;
153
154     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
155
156     wined3d_mutex_lock();
157     resource = wined3d_texture_get_resource(This->wined3d_texture);
158     hr = wined3d_resource_free_private_data(resource, refguid);
159     wined3d_mutex_unlock();
160
161     return hr;
162 }
163
164 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetPriority(IDirect3DCubeTexture9 *iface,
165         DWORD PriorityNew)
166 {
167     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
168     DWORD ret;
169
170     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
171
172     wined3d_mutex_lock();
173     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
174     wined3d_mutex_unlock();
175
176     return ret;
177 }
178
179 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetPriority(IDirect3DCubeTexture9 *iface)
180 {
181     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
182     DWORD ret;
183
184     TRACE("iface %p.\n", iface);
185
186     wined3d_mutex_lock();
187     ret = wined3d_texture_get_priority(This->wined3d_texture);
188     wined3d_mutex_unlock();
189
190     return ret;
191 }
192
193 static void WINAPI IDirect3DCubeTexture9Impl_PreLoad(IDirect3DCubeTexture9 *iface)
194 {
195     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
196
197     TRACE("iface %p.\n", iface);
198
199     wined3d_mutex_lock();
200     wined3d_texture_preload(This->wined3d_texture);
201     wined3d_mutex_unlock();
202 }
203
204 static D3DRESOURCETYPE WINAPI IDirect3DCubeTexture9Impl_GetType(IDirect3DCubeTexture9 *iface)
205 {
206     TRACE("iface %p.\n", iface);
207
208     return D3DRTYPE_CUBETEXTURE;
209 }
210
211 /* IDirect3DCubeTexture9 IDirect3DBaseTexture9 Interface follow: */
212 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetLOD(IDirect3DCubeTexture9 *iface, DWORD LODNew)
213 {
214     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
215     DWORD ret;
216
217     TRACE("iface %p, lod %u.\n", iface, LODNew);
218
219     wined3d_mutex_lock();
220     ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
221     wined3d_mutex_unlock();
222
223     return ret;
224 }
225
226 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLOD(IDirect3DCubeTexture9 *iface)
227 {
228     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
229     DWORD ret;
230
231     TRACE("iface %p.\n", iface);
232
233     wined3d_mutex_lock();
234     ret = wined3d_texture_get_lod(This->wined3d_texture);
235     wined3d_mutex_unlock();
236
237     return ret;
238 }
239
240 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLevelCount(IDirect3DCubeTexture9 *iface)
241 {
242     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
243     DWORD ret;
244
245     TRACE("iface %p.\n", iface);
246
247     wined3d_mutex_lock();
248     ret = wined3d_texture_get_level_count(This->wined3d_texture);
249     wined3d_mutex_unlock();
250
251     return ret;
252 }
253
254 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetAutoGenFilterType(IDirect3DCubeTexture9 *iface,
255         D3DTEXTUREFILTERTYPE FilterType)
256 {
257     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
258     HRESULT hr;
259
260     TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
261
262     wined3d_mutex_lock();
263     hr = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (WINED3DTEXTUREFILTERTYPE)FilterType);
264     wined3d_mutex_unlock();
265
266     return hr;
267 }
268
269 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DCubeTexture9Impl_GetAutoGenFilterType(IDirect3DCubeTexture9 *iface)
270 {
271     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
272     D3DTEXTUREFILTERTYPE ret;
273
274     TRACE("iface %p.\n", iface);
275
276     wined3d_mutex_lock();
277     ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
278     wined3d_mutex_unlock();
279
280     return ret;
281 }
282
283 static void WINAPI IDirect3DCubeTexture9Impl_GenerateMipSubLevels(IDirect3DCubeTexture9 *iface)
284 {
285     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
286
287     TRACE("iface %p.\n", iface);
288
289     wined3d_mutex_lock();
290     wined3d_texture_generate_mipmaps(This->wined3d_texture);
291     wined3d_mutex_unlock();
292 }
293
294 /* IDirect3DCubeTexture9 Interface follow: */
295 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetLevelDesc(IDirect3DCubeTexture9 *iface,
296         UINT level, D3DSURFACE_DESC *desc)
297 {
298     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
299     struct wined3d_resource *sub_resource;
300     HRESULT hr = D3D_OK;
301
302     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
303
304     wined3d_mutex_lock();
305     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
306         hr = D3DERR_INVALIDCALL;
307     else
308     {
309         struct wined3d_resource_desc wined3d_desc;
310
311         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
312         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
313         desc->Type = wined3d_desc.resource_type;
314         desc->Usage = wined3d_desc.usage;
315         desc->Pool = wined3d_desc.pool;
316         desc->MultiSampleType = wined3d_desc.multisample_type;
317         desc->MultiSampleQuality = wined3d_desc.multisample_quality;
318         desc->Width = wined3d_desc.width;
319         desc->Height = wined3d_desc.height;
320     }
321     wined3d_mutex_unlock();
322
323     return hr;
324 }
325
326 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetCubeMapSurface(IDirect3DCubeTexture9 *iface,
327         D3DCUBEMAP_FACES face, UINT level, IDirect3DSurface9 **surface)
328 {
329     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
330     struct wined3d_resource *sub_resource;
331     UINT sub_resource_idx;
332
333     TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, face, level, surface);
334
335     wined3d_mutex_lock();
336     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
337     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
338     {
339         wined3d_mutex_unlock();
340         return D3DERR_INVALIDCALL;
341     }
342
343     *surface = wined3d_resource_get_parent(sub_resource);
344     IDirect3DSurface9_AddRef(*surface);
345     wined3d_mutex_unlock();
346
347     return D3D_OK;
348 }
349
350 static HRESULT WINAPI IDirect3DCubeTexture9Impl_LockRect(IDirect3DCubeTexture9 *iface,
351         D3DCUBEMAP_FACES face, UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect,
352         DWORD flags)
353 {
354     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
355     struct wined3d_resource *sub_resource;
356     UINT sub_resource_idx;
357     HRESULT hr;
358
359     TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
360             iface, face, level, locked_rect, rect, flags);
361
362     wined3d_mutex_lock();
363     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
364     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
365         hr = D3DERR_INVALIDCALL;
366     else
367         hr = IDirect3DSurface9_LockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource),
368                 locked_rect, rect, flags);
369     wined3d_mutex_unlock();
370
371     return hr;
372 }
373
374 static HRESULT WINAPI IDirect3DCubeTexture9Impl_UnlockRect(IDirect3DCubeTexture9 *iface,
375         D3DCUBEMAP_FACES face, UINT level)
376 {
377     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
378     struct wined3d_resource *sub_resource;
379     UINT sub_resource_idx;
380     HRESULT hr;
381
382     TRACE("iface %p, face %#x, level %u.\n", iface, face, level);
383
384     wined3d_mutex_lock();
385     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
386     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
387         hr = D3DERR_INVALIDCALL;
388     else
389         hr = IDirect3DSurface9_UnlockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource));
390     wined3d_mutex_unlock();
391
392     return hr;
393 }
394
395 static HRESULT  WINAPI IDirect3DCubeTexture9Impl_AddDirtyRect(IDirect3DCubeTexture9 *iface,
396         D3DCUBEMAP_FACES face, const RECT *dirty_rect)
397 {
398     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
399     HRESULT hr;
400
401     TRACE("iface %p, face %#x, dirty_rect %s.\n",
402             iface, face, wine_dbgstr_rect(dirty_rect));
403
404     wined3d_mutex_lock();
405     if (!dirty_rect)
406         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, NULL);
407     else
408     {
409         WINED3DBOX dirty_region;
410
411         dirty_region.Left = dirty_rect->left;
412         dirty_region.Top = dirty_rect->top;
413         dirty_region.Right = dirty_rect->right;
414         dirty_region.Bottom = dirty_rect->bottom;
415         dirty_region.Front = 0;
416         dirty_region.Back = 1;
417         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, &dirty_region);
418     }
419     wined3d_mutex_unlock();
420
421     return hr;
422 }
423
424 static const IDirect3DCubeTexture9Vtbl Direct3DCubeTexture9_Vtbl =
425 {
426     /* IUnknown */
427     IDirect3DCubeTexture9Impl_QueryInterface,
428     IDirect3DCubeTexture9Impl_AddRef,
429     IDirect3DCubeTexture9Impl_Release,
430     /* IDirect3DResource9 */
431     IDirect3DCubeTexture9Impl_GetDevice,
432     IDirect3DCubeTexture9Impl_SetPrivateData,
433     IDirect3DCubeTexture9Impl_GetPrivateData,
434     IDirect3DCubeTexture9Impl_FreePrivateData,
435     IDirect3DCubeTexture9Impl_SetPriority,
436     IDirect3DCubeTexture9Impl_GetPriority,
437     IDirect3DCubeTexture9Impl_PreLoad,
438     IDirect3DCubeTexture9Impl_GetType,
439     /* IDirect3DBaseTexture9 */
440     IDirect3DCubeTexture9Impl_SetLOD,
441     IDirect3DCubeTexture9Impl_GetLOD,
442     IDirect3DCubeTexture9Impl_GetLevelCount,
443     IDirect3DCubeTexture9Impl_SetAutoGenFilterType,
444     IDirect3DCubeTexture9Impl_GetAutoGenFilterType,
445     IDirect3DCubeTexture9Impl_GenerateMipSubLevels,
446     IDirect3DCubeTexture9Impl_GetLevelDesc,
447     IDirect3DCubeTexture9Impl_GetCubeMapSurface,
448     IDirect3DCubeTexture9Impl_LockRect,
449     IDirect3DCubeTexture9Impl_UnlockRect,
450     IDirect3DCubeTexture9Impl_AddDirtyRect
451 };
452
453 static void STDMETHODCALLTYPE cubetexture_wined3d_object_destroyed(void *parent)
454 {
455     HeapFree(GetProcessHeap(), 0, parent);
456 }
457
458 static const struct wined3d_parent_ops d3d9_cubetexture_wined3d_parent_ops =
459 {
460     cubetexture_wined3d_object_destroyed,
461 };
462
463 HRESULT cubetexture_init(IDirect3DCubeTexture9Impl *texture, IDirect3DDevice9Impl *device,
464         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
465 {
466     HRESULT hr;
467
468     texture->IDirect3DCubeTexture9_iface.lpVtbl = &Direct3DCubeTexture9_Vtbl;
469     texture->ref = 1;
470
471     wined3d_mutex_lock();
472     hr = wined3d_texture_create_cube(device->wined3d_device, edge_length,
473             levels, usage, wined3dformat_from_d3dformat(format), pool, texture,
474             &d3d9_cubetexture_wined3d_parent_ops, &texture->wined3d_texture);
475     wined3d_mutex_unlock();
476     if (FAILED(hr))
477     {
478         WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
479         return hr;
480     }
481
482     texture->parentDevice = &device->IDirect3DDevice9Ex_iface;
483     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
484
485     return D3D_OK;
486 }