d3d9: Don't return a pointer to the implementation in IDirect3DSwapChain9Impl_QueryIn...
[wine/multimedia.git] / dlls / d3d9 / cubetexture.c
blob349f79082e8abffbcf42960786272253358bb0cd
1 /*
2 * IDirect3DCubeTexture9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
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.
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.
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
23 #include "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
29 static inline IDirect3DCubeTexture9Impl *impl_from_IDirect3DCubeTexture9(IDirect3DCubeTexture9 *iface)
31 return CONTAINING_RECORD(iface, IDirect3DCubeTexture9Impl, IDirect3DCubeTexture9_iface);
34 static HRESULT WINAPI IDirect3DCubeTexture9Impl_QueryInterface(IDirect3DCubeTexture9 *iface,
35 REFIID riid, void **ppobj)
37 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
39 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
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;
50 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
51 *ppobj = NULL;
52 return E_NOINTERFACE;
55 static ULONG WINAPI IDirect3DCubeTexture9Impl_AddRef(IDirect3DCubeTexture9 *iface)
57 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
58 ULONG ref = InterlockedIncrement(&This->ref);
60 TRACE("%p increasing refcount to %u.\n", iface, ref);
62 if (ref == 1)
64 IDirect3DDevice9Ex_AddRef(This->parentDevice);
65 wined3d_mutex_lock();
66 wined3d_texture_incref(This->wined3d_texture);
67 wined3d_mutex_unlock();
70 return ref;
73 static ULONG WINAPI IDirect3DCubeTexture9Impl_Release(IDirect3DCubeTexture9 *iface)
75 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
76 ULONG ref = InterlockedDecrement(&This->ref);
78 TRACE("%p decreasing refcount to %u.\n", iface, ref);
80 if (ref == 0) {
81 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
83 TRACE("Releasing child %p.\n", This->wined3d_texture);
85 wined3d_mutex_lock();
86 wined3d_texture_decref(This->wined3d_texture);
87 wined3d_mutex_unlock();
89 /* Release the device last, as it may cause the device to be destroyed. */
90 IDirect3DDevice9Ex_Release(parentDevice);
92 return ref;
95 /* IDirect3DCubeTexture9 IDirect3DResource9 Interface follow: */
96 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetDevice(IDirect3DCubeTexture9 *iface,
97 IDirect3DDevice9 **device)
99 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
101 TRACE("iface %p, device %p.\n", iface, device);
103 *device = (IDirect3DDevice9 *)This->parentDevice;
104 IDirect3DDevice9_AddRef(*device);
106 TRACE("Returning device %p.\n", *device);
108 return D3D_OK;
111 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetPrivateData(IDirect3DCubeTexture9 *iface,
112 REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
114 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
115 struct wined3d_resource *resource;
116 HRESULT hr;
118 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
119 iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
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();
126 return hr;
129 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetPrivateData(IDirect3DCubeTexture9 *iface,
130 REFGUID refguid, void *pData, DWORD *pSizeOfData)
132 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
133 struct wined3d_resource *resource;
134 HRESULT hr;
136 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
137 iface, debugstr_guid(refguid), pData, pSizeOfData);
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();
144 return hr;
147 static HRESULT WINAPI IDirect3DCubeTexture9Impl_FreePrivateData(IDirect3DCubeTexture9 *iface,
148 REFGUID refguid)
150 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
151 struct wined3d_resource *resource;
152 HRESULT hr;
154 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
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();
161 return hr;
164 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetPriority(IDirect3DCubeTexture9 *iface,
165 DWORD PriorityNew)
167 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
168 DWORD ret;
170 TRACE("iface %p, priority %u.\n", iface, PriorityNew);
172 wined3d_mutex_lock();
173 ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
174 wined3d_mutex_unlock();
176 return ret;
179 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetPriority(IDirect3DCubeTexture9 *iface)
181 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
182 DWORD ret;
184 TRACE("iface %p.\n", iface);
186 wined3d_mutex_lock();
187 ret = wined3d_texture_get_priority(This->wined3d_texture);
188 wined3d_mutex_unlock();
190 return ret;
193 static void WINAPI IDirect3DCubeTexture9Impl_PreLoad(IDirect3DCubeTexture9 *iface)
195 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
197 TRACE("iface %p.\n", iface);
199 wined3d_mutex_lock();
200 wined3d_texture_preload(This->wined3d_texture);
201 wined3d_mutex_unlock();
204 static D3DRESOURCETYPE WINAPI IDirect3DCubeTexture9Impl_GetType(IDirect3DCubeTexture9 *iface)
206 TRACE("iface %p.\n", iface);
208 return D3DRTYPE_CUBETEXTURE;
211 /* IDirect3DCubeTexture9 IDirect3DBaseTexture9 Interface follow: */
212 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetLOD(IDirect3DCubeTexture9 *iface, DWORD LODNew)
214 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
215 DWORD ret;
217 TRACE("iface %p, lod %u.\n", iface, LODNew);
219 wined3d_mutex_lock();
220 ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
221 wined3d_mutex_unlock();
223 return ret;
226 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLOD(IDirect3DCubeTexture9 *iface)
228 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
229 DWORD ret;
231 TRACE("iface %p.\n", iface);
233 wined3d_mutex_lock();
234 ret = wined3d_texture_get_lod(This->wined3d_texture);
235 wined3d_mutex_unlock();
237 return ret;
240 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLevelCount(IDirect3DCubeTexture9 *iface)
242 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
243 DWORD ret;
245 TRACE("iface %p.\n", iface);
247 wined3d_mutex_lock();
248 ret = wined3d_texture_get_level_count(This->wined3d_texture);
249 wined3d_mutex_unlock();
251 return ret;
254 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetAutoGenFilterType(IDirect3DCubeTexture9 *iface,
255 D3DTEXTUREFILTERTYPE FilterType)
257 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
258 HRESULT hr;
260 TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
262 wined3d_mutex_lock();
263 hr = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (enum wined3d_texture_filter_type)FilterType);
264 wined3d_mutex_unlock();
266 return hr;
269 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DCubeTexture9Impl_GetAutoGenFilterType(IDirect3DCubeTexture9 *iface)
271 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
272 D3DTEXTUREFILTERTYPE ret;
274 TRACE("iface %p.\n", iface);
276 wined3d_mutex_lock();
277 ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
278 wined3d_mutex_unlock();
280 return ret;
283 static void WINAPI IDirect3DCubeTexture9Impl_GenerateMipSubLevels(IDirect3DCubeTexture9 *iface)
285 IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
287 TRACE("iface %p.\n", iface);
289 wined3d_mutex_lock();
290 wined3d_texture_generate_mipmaps(This->wined3d_texture);
291 wined3d_mutex_unlock();
294 /* IDirect3DCubeTexture9 Interface follow: */
295 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetLevelDesc(IDirect3DCubeTexture9 *iface,
296 UINT level, D3DSURFACE_DESC *desc)
298 IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
299 struct wined3d_resource *sub_resource;
300 HRESULT hr = D3D_OK;
302 TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
304 wined3d_mutex_lock();
305 if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
306 hr = D3DERR_INVALIDCALL;
307 else
309 struct wined3d_resource_desc wined3d_desc;
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 & WINED3DUSAGE_MASK;
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;
321 wined3d_mutex_unlock();
323 return hr;
326 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetCubeMapSurface(IDirect3DCubeTexture9 *iface,
327 D3DCUBEMAP_FACES face, UINT level, IDirect3DSurface9 **surface)
329 IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
330 struct wined3d_resource *sub_resource;
331 UINT sub_resource_idx;
333 TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, face, level, surface);
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)))
339 wined3d_mutex_unlock();
340 return D3DERR_INVALIDCALL;
343 *surface = wined3d_resource_get_parent(sub_resource);
344 IDirect3DSurface9_AddRef(*surface);
345 wined3d_mutex_unlock();
347 return D3D_OK;
350 static HRESULT WINAPI IDirect3DCubeTexture9Impl_LockRect(IDirect3DCubeTexture9 *iface,
351 D3DCUBEMAP_FACES face, UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect,
352 DWORD flags)
354 IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
355 struct wined3d_resource *sub_resource;
356 UINT sub_resource_idx;
357 HRESULT hr;
359 TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
360 iface, face, level, locked_rect, rect, flags);
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();
371 return hr;
374 static HRESULT WINAPI IDirect3DCubeTexture9Impl_UnlockRect(IDirect3DCubeTexture9 *iface,
375 D3DCUBEMAP_FACES face, UINT level)
377 IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
378 struct wined3d_resource *sub_resource;
379 UINT sub_resource_idx;
380 HRESULT hr;
382 TRACE("iface %p, face %#x, level %u.\n", iface, face, level);
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();
392 return hr;
395 static HRESULT WINAPI IDirect3DCubeTexture9Impl_AddDirtyRect(IDirect3DCubeTexture9 *iface,
396 D3DCUBEMAP_FACES face, const RECT *dirty_rect)
398 IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
399 HRESULT hr;
401 TRACE("iface %p, face %#x, dirty_rect %s.\n",
402 iface, face, wine_dbgstr_rect(dirty_rect));
404 wined3d_mutex_lock();
405 if (!dirty_rect)
406 hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, NULL);
407 else
409 struct wined3d_box dirty_region;
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);
419 wined3d_mutex_unlock();
421 return hr;
424 static const IDirect3DCubeTexture9Vtbl Direct3DCubeTexture9_Vtbl =
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
453 static void STDMETHODCALLTYPE cubetexture_wined3d_object_destroyed(void *parent)
455 HeapFree(GetProcessHeap(), 0, parent);
458 static const struct wined3d_parent_ops d3d9_cubetexture_wined3d_parent_ops =
460 cubetexture_wined3d_object_destroyed,
463 HRESULT cubetexture_init(IDirect3DCubeTexture9Impl *texture, IDirect3DDevice9Impl *device,
464 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
466 HRESULT hr;
468 texture->IDirect3DCubeTexture9_iface.lpVtbl = &Direct3DCubeTexture9_Vtbl;
469 texture->ref = 1;
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))
478 WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
479 return hr;
482 texture->parentDevice = &device->IDirect3DDevice9Ex_iface;
483 IDirect3DDevice9Ex_AddRef(texture->parentDevice);
485 return D3D_OK;