d3d9: Just return the resource type in IDirect3DResource8::GetType() implementations.
[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     HRESULT hr;
116
117     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
118             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
119
120     wined3d_mutex_lock();
121     hr = wined3d_texture_set_private_data(This->wined3d_texture, refguid, pData, SizeOfData, Flags);
122     wined3d_mutex_unlock();
123
124     return hr;
125 }
126
127 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetPrivateData(IDirect3DCubeTexture9 *iface,
128         REFGUID refguid, void *pData, DWORD *pSizeOfData)
129 {
130     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
131     HRESULT hr;
132
133     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
134             iface, debugstr_guid(refguid), pData, pSizeOfData);
135
136     wined3d_mutex_lock();
137     hr = wined3d_texture_get_private_data(This->wined3d_texture, refguid, pData, pSizeOfData);
138     wined3d_mutex_unlock();
139
140     return hr;
141 }
142
143 static HRESULT WINAPI IDirect3DCubeTexture9Impl_FreePrivateData(IDirect3DCubeTexture9 *iface,
144         REFGUID refguid)
145 {
146     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
147     HRESULT hr;
148
149     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
150
151     wined3d_mutex_lock();
152     hr = wined3d_texture_free_private_data(This->wined3d_texture, refguid);
153     wined3d_mutex_unlock();
154
155     return hr;
156 }
157
158 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetPriority(IDirect3DCubeTexture9 *iface,
159         DWORD PriorityNew)
160 {
161     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
162     DWORD ret;
163
164     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
165
166     wined3d_mutex_lock();
167     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
168     wined3d_mutex_unlock();
169
170     return ret;
171 }
172
173 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetPriority(IDirect3DCubeTexture9 *iface)
174 {
175     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
176     DWORD ret;
177
178     TRACE("iface %p.\n", iface);
179
180     wined3d_mutex_lock();
181     ret = wined3d_texture_get_priority(This->wined3d_texture);
182     wined3d_mutex_unlock();
183
184     return ret;
185 }
186
187 static void WINAPI IDirect3DCubeTexture9Impl_PreLoad(IDirect3DCubeTexture9 *iface)
188 {
189     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
190
191     TRACE("iface %p.\n", iface);
192
193     wined3d_mutex_lock();
194     wined3d_texture_preload(This->wined3d_texture);
195     wined3d_mutex_unlock();
196 }
197
198 static D3DRESOURCETYPE WINAPI IDirect3DCubeTexture9Impl_GetType(IDirect3DCubeTexture9 *iface)
199 {
200     TRACE("iface %p.\n", iface);
201
202     return D3DRTYPE_CUBETEXTURE;
203 }
204
205 /* IDirect3DCubeTexture9 IDirect3DBaseTexture9 Interface follow: */
206 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetLOD(IDirect3DCubeTexture9 *iface, DWORD LODNew)
207 {
208     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
209     DWORD ret;
210
211     TRACE("iface %p, lod %u.\n", iface, LODNew);
212
213     wined3d_mutex_lock();
214     ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
215     wined3d_mutex_unlock();
216
217     return ret;
218 }
219
220 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLOD(IDirect3DCubeTexture9 *iface)
221 {
222     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
223     DWORD ret;
224
225     TRACE("iface %p.\n", iface);
226
227     wined3d_mutex_lock();
228     ret = wined3d_texture_get_lod(This->wined3d_texture);
229     wined3d_mutex_unlock();
230
231     return ret;
232 }
233
234 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLevelCount(IDirect3DCubeTexture9 *iface)
235 {
236     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
237     DWORD ret;
238
239     TRACE("iface %p.\n", iface);
240
241     wined3d_mutex_lock();
242     ret = wined3d_texture_get_level_count(This->wined3d_texture);
243     wined3d_mutex_unlock();
244
245     return ret;
246 }
247
248 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetAutoGenFilterType(IDirect3DCubeTexture9 *iface,
249         D3DTEXTUREFILTERTYPE FilterType)
250 {
251     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(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 IDirect3DCubeTexture9Impl_GetAutoGenFilterType(IDirect3DCubeTexture9 *iface)
264 {
265     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
266     D3DTEXTUREFILTERTYPE ret;
267
268     TRACE("iface %p.\n", iface);
269
270     wined3d_mutex_lock();
271     ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
272     wined3d_mutex_unlock();
273
274     return ret;
275 }
276
277 static void WINAPI IDirect3DCubeTexture9Impl_GenerateMipSubLevels(IDirect3DCubeTexture9 *iface)
278 {
279     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(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 /* IDirect3DCubeTexture9 Interface follow: */
289 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetLevelDesc(IDirect3DCubeTexture9 *iface,
290         UINT level, D3DSURFACE_DESC *desc)
291 {
292     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(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->MultiSampleType = wined3d_desc.multisample_type;
311         desc->MultiSampleQuality = wined3d_desc.multisample_quality;
312         desc->Width = wined3d_desc.width;
313         desc->Height = wined3d_desc.height;
314     }
315     wined3d_mutex_unlock();
316
317     return hr;
318 }
319
320 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetCubeMapSurface(IDirect3DCubeTexture9 *iface,
321         D3DCUBEMAP_FACES face, UINT level, IDirect3DSurface9 **surface)
322 {
323     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
324     struct wined3d_resource *sub_resource;
325     UINT sub_resource_idx;
326
327     TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, face, level, surface);
328
329     wined3d_mutex_lock();
330     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
331     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
332     {
333         wined3d_mutex_unlock();
334         return D3DERR_INVALIDCALL;
335     }
336
337     *surface = wined3d_resource_get_parent(sub_resource);
338     IDirect3DSurface9_AddRef(*surface);
339     wined3d_mutex_unlock();
340
341     return D3D_OK;
342 }
343
344 static HRESULT WINAPI IDirect3DCubeTexture9Impl_LockRect(IDirect3DCubeTexture9 *iface,
345         D3DCUBEMAP_FACES face, UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect,
346         DWORD flags)
347 {
348     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
349     struct wined3d_resource *sub_resource;
350     UINT sub_resource_idx;
351     HRESULT hr;
352
353     TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
354             iface, face, level, locked_rect, rect, flags);
355
356     wined3d_mutex_lock();
357     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
358     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
359         hr = D3DERR_INVALIDCALL;
360     else
361         hr = IDirect3DSurface9_LockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource),
362                 locked_rect, rect, flags);
363     wined3d_mutex_unlock();
364
365     return hr;
366 }
367
368 static HRESULT WINAPI IDirect3DCubeTexture9Impl_UnlockRect(IDirect3DCubeTexture9 *iface,
369         D3DCUBEMAP_FACES face, UINT level)
370 {
371     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
372     struct wined3d_resource *sub_resource;
373     UINT sub_resource_idx;
374     HRESULT hr;
375
376     TRACE("iface %p, face %#x, level %u.\n", iface, face, level);
377
378     wined3d_mutex_lock();
379     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
380     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
381         hr = D3DERR_INVALIDCALL;
382     else
383         hr = IDirect3DSurface9_UnlockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource));
384     wined3d_mutex_unlock();
385
386     return hr;
387 }
388
389 static HRESULT  WINAPI IDirect3DCubeTexture9Impl_AddDirtyRect(IDirect3DCubeTexture9 *iface,
390         D3DCUBEMAP_FACES face, const RECT *dirty_rect)
391 {
392     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
393     HRESULT hr;
394
395     TRACE("iface %p, face %#x, dirty_rect %s.\n",
396             iface, face, wine_dbgstr_rect(dirty_rect));
397
398     wined3d_mutex_lock();
399     if (!dirty_rect)
400         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, NULL);
401     else
402     {
403         WINED3DBOX dirty_region;
404
405         dirty_region.Left = dirty_rect->left;
406         dirty_region.Top = dirty_rect->top;
407         dirty_region.Right = dirty_rect->right;
408         dirty_region.Bottom = dirty_rect->bottom;
409         dirty_region.Front = 0;
410         dirty_region.Back = 1;
411         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, &dirty_region);
412     }
413     wined3d_mutex_unlock();
414
415     return hr;
416 }
417
418 static const IDirect3DCubeTexture9Vtbl Direct3DCubeTexture9_Vtbl =
419 {
420     /* IUnknown */
421     IDirect3DCubeTexture9Impl_QueryInterface,
422     IDirect3DCubeTexture9Impl_AddRef,
423     IDirect3DCubeTexture9Impl_Release,
424     /* IDirect3DResource9 */
425     IDirect3DCubeTexture9Impl_GetDevice,
426     IDirect3DCubeTexture9Impl_SetPrivateData,
427     IDirect3DCubeTexture9Impl_GetPrivateData,
428     IDirect3DCubeTexture9Impl_FreePrivateData,
429     IDirect3DCubeTexture9Impl_SetPriority,
430     IDirect3DCubeTexture9Impl_GetPriority,
431     IDirect3DCubeTexture9Impl_PreLoad,
432     IDirect3DCubeTexture9Impl_GetType,
433     /* IDirect3DBaseTexture9 */
434     IDirect3DCubeTexture9Impl_SetLOD,
435     IDirect3DCubeTexture9Impl_GetLOD,
436     IDirect3DCubeTexture9Impl_GetLevelCount,
437     IDirect3DCubeTexture9Impl_SetAutoGenFilterType,
438     IDirect3DCubeTexture9Impl_GetAutoGenFilterType,
439     IDirect3DCubeTexture9Impl_GenerateMipSubLevels,
440     IDirect3DCubeTexture9Impl_GetLevelDesc,
441     IDirect3DCubeTexture9Impl_GetCubeMapSurface,
442     IDirect3DCubeTexture9Impl_LockRect,
443     IDirect3DCubeTexture9Impl_UnlockRect,
444     IDirect3DCubeTexture9Impl_AddDirtyRect
445 };
446
447 static void STDMETHODCALLTYPE cubetexture_wined3d_object_destroyed(void *parent)
448 {
449     HeapFree(GetProcessHeap(), 0, parent);
450 }
451
452 static const struct wined3d_parent_ops d3d9_cubetexture_wined3d_parent_ops =
453 {
454     cubetexture_wined3d_object_destroyed,
455 };
456
457 HRESULT cubetexture_init(IDirect3DCubeTexture9Impl *texture, IDirect3DDevice9Impl *device,
458         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
459 {
460     HRESULT hr;
461
462     texture->IDirect3DCubeTexture9_iface.lpVtbl = &Direct3DCubeTexture9_Vtbl;
463     texture->ref = 1;
464
465     wined3d_mutex_lock();
466     hr = wined3d_texture_create_cube(device->wined3d_device, edge_length,
467             levels, usage, wined3dformat_from_d3dformat(format), pool, texture,
468             &d3d9_cubetexture_wined3d_parent_ops, &texture->wined3d_texture);
469     wined3d_mutex_unlock();
470     if (FAILED(hr))
471     {
472         WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
473         return hr;
474     }
475
476     texture->parentDevice = &device->IDirect3DDevice9Ex_iface;
477     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
478
479     return D3D_OK;
480 }