mmdevapi/tests: Add tests for volume control interfaces.
[wine] / dlls / d3d10core / texture.c
1 /*
2  * Copyright 2009 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include "d3d10core_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d10core);
26
27 /* IUnknown methods */
28
29 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_QueryInterface(ID3D10Texture2D *iface, REFIID riid, void **object)
30 {
31     struct d3d10_texture2d *This = (struct d3d10_texture2d *)iface;
32
33     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
34
35     if (IsEqualGUID(riid, &IID_ID3D10Texture2D)
36             || IsEqualGUID(riid, &IID_ID3D10Resource)
37             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
38             || IsEqualGUID(riid, &IID_IUnknown))
39     {
40         IUnknown_AddRef(iface);
41         *object = iface;
42         return S_OK;
43     }
44
45     if (This->dxgi_surface)
46     {
47         TRACE("Forwarding to dxgi surface\n");
48         return IDXGISurface_QueryInterface(This->dxgi_surface, riid, object);
49     }
50
51     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
52
53     *object = NULL;
54     return E_NOINTERFACE;
55 }
56
57 static ULONG STDMETHODCALLTYPE d3d10_texture2d_AddRef(ID3D10Texture2D *iface)
58 {
59     struct d3d10_texture2d *This = (struct d3d10_texture2d *)iface;
60     ULONG refcount = InterlockedIncrement(&This->refcount);
61
62     TRACE("%p increasing refcount to %u\n", This, refcount);
63
64     if (refcount == 1 && This->wined3d_surface)
65         wined3d_surface_incref(This->wined3d_surface);
66
67     return refcount;
68 }
69
70 static void STDMETHODCALLTYPE d3d10_texture2d_wined3d_object_released(void *parent)
71 {
72     struct d3d10_texture2d *This = parent;
73
74     if (This->dxgi_surface) IDXGISurface_Release(This->dxgi_surface);
75     HeapFree(GetProcessHeap(), 0, This);
76 }
77
78 static ULONG STDMETHODCALLTYPE d3d10_texture2d_Release(ID3D10Texture2D *iface)
79 {
80     struct d3d10_texture2d *This = (struct d3d10_texture2d *)iface;
81     ULONG refcount = InterlockedDecrement(&This->refcount);
82
83     TRACE("%p decreasing refcount to %u\n", This, refcount);
84
85     if (!refcount)
86     {
87         if (This->wined3d_surface)
88             wined3d_surface_decref(This->wined3d_surface);
89         else
90             d3d10_texture2d_wined3d_object_released(This);
91     }
92
93     return refcount;
94 }
95
96 /* ID3D10DeviceChild methods */
97
98 static void STDMETHODCALLTYPE d3d10_texture2d_GetDevice(ID3D10Texture2D *iface, ID3D10Device **device)
99 {
100     FIXME("iface %p, device %p stub!\n", iface, device);
101 }
102
103 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_GetPrivateData(ID3D10Texture2D *iface,
104         REFGUID guid, UINT *data_size, void *data)
105 {
106     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
107             iface, debugstr_guid(guid), data_size, data);
108
109     return E_NOTIMPL;
110 }
111
112 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateData(ID3D10Texture2D *iface,
113         REFGUID guid, UINT data_size, const void *data)
114 {
115     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
116             iface, debugstr_guid(guid), data_size, data);
117
118     return E_NOTIMPL;
119 }
120
121 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateDataInterface(ID3D10Texture2D *iface,
122         REFGUID guid, const IUnknown *data)
123 {
124     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
125
126     return E_NOTIMPL;
127 }
128
129 /* ID3D10Resource methods */
130
131 static void STDMETHODCALLTYPE d3d10_texture2d_GetType(ID3D10Texture2D *iface,
132         D3D10_RESOURCE_DIMENSION *resource_dimension)
133 {
134     TRACE("iface %p, resource_dimension %p\n", iface, resource_dimension);
135
136     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
137 }
138
139 static void STDMETHODCALLTYPE d3d10_texture2d_SetEvictionPriority(ID3D10Texture2D *iface, UINT eviction_priority)
140 {
141     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
142 }
143
144 static UINT STDMETHODCALLTYPE d3d10_texture2d_GetEvictionPriority(ID3D10Texture2D *iface)
145 {
146     FIXME("iface %p stub!\n", iface);
147
148     return 0;
149 }
150
151 /* ID3D10Texture2D methods */
152
153 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_Map(ID3D10Texture2D *iface, UINT sub_resource,
154         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE2D *mapped_texture)
155 {
156     FIXME("iface %p, sub_resource %u, map_type %u, map_flags %#x, mapped_texture %p stub!\n",
157             iface, sub_resource, map_type, map_flags, mapped_texture);
158
159     return E_NOTIMPL;
160 }
161
162 static void STDMETHODCALLTYPE d3d10_texture2d_Unmap(ID3D10Texture2D *iface, UINT sub_resource)
163 {
164     FIXME("iface %p, sub_resource %u stub!\n", iface, sub_resource);
165 }
166
167 static void STDMETHODCALLTYPE d3d10_texture2d_GetDesc(ID3D10Texture2D *iface, D3D10_TEXTURE2D_DESC *desc)
168 {
169     struct d3d10_texture2d *This = (struct d3d10_texture2d *)iface;
170
171     TRACE("iface %p, desc %p\n", iface, desc);
172
173     *desc = This->desc;
174 }
175
176 static const struct ID3D10Texture2DVtbl d3d10_texture2d_vtbl =
177 {
178     /* IUnknown methods */
179     d3d10_texture2d_QueryInterface,
180     d3d10_texture2d_AddRef,
181     d3d10_texture2d_Release,
182     /* ID3D10DeviceChild methods */
183     d3d10_texture2d_GetDevice,
184     d3d10_texture2d_GetPrivateData,
185     d3d10_texture2d_SetPrivateData,
186     d3d10_texture2d_SetPrivateDataInterface,
187     /* ID3D10Resource methods */
188     d3d10_texture2d_GetType,
189     d3d10_texture2d_SetEvictionPriority,
190     d3d10_texture2d_GetEvictionPriority,
191     /* ID3D10Texture2D methods */
192     d3d10_texture2d_Map,
193     d3d10_texture2d_Unmap,
194     d3d10_texture2d_GetDesc,
195 };
196
197 static const struct wined3d_parent_ops d3d10_texture2d_wined3d_parent_ops =
198 {
199     d3d10_texture2d_wined3d_object_released,
200 };
201
202 HRESULT d3d10_texture2d_init(struct d3d10_texture2d *texture, struct d3d10_device *device,
203         const D3D10_TEXTURE2D_DESC *desc)
204 {
205     HRESULT hr;
206
207     texture->vtbl = &d3d10_texture2d_vtbl;
208     texture->refcount = 1;
209     texture->desc = *desc;
210
211     if (desc->MipLevels == 1 && desc->ArraySize == 1)
212     {
213         IWineDXGIDevice *wine_device;
214
215         hr = ID3D10Device_QueryInterface((ID3D10Device *)device, &IID_IWineDXGIDevice, (void **)&wine_device);
216         if (FAILED(hr))
217         {
218             ERR("Device should implement IWineDXGIDevice\n");
219             return E_FAIL;
220         }
221
222         hr = IWineDXGIDevice_create_surface(wine_device, NULL, 0, NULL,
223                 (IUnknown *)texture, (void **)&texture->dxgi_surface);
224         IWineDXGIDevice_Release(wine_device);
225         if (FAILED(hr))
226         {
227             ERR("Failed to create DXGI surface, returning %#x\n", hr);
228             return hr;
229         }
230
231         FIXME("Implement DXGI<->wined3d usage conversion\n");
232
233         hr = IWineD3DDevice_CreateSurface(device->wined3d_device, desc->Width, desc->Height,
234                 wined3dformat_from_dxgi_format(desc->Format), FALSE, FALSE, 0, desc->Usage, WINED3DPOOL_DEFAULT,
235                 desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3DMULTISAMPLE_NONE,
236                 desc->SampleDesc.Quality, SURFACE_OPENGL, texture, &d3d10_texture2d_wined3d_parent_ops,
237                 &texture->wined3d_surface);
238         if (FAILED(hr))
239         {
240             ERR("CreateSurface failed, returning %#x\n", hr);
241             IDXGISurface_Release(texture->dxgi_surface);
242             return hr;
243         }
244     }
245
246     return S_OK;
247 }
248
249 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_QueryInterface(ID3D10Texture3D *iface, REFIID riid, void **object)
250 {
251     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
252
253     if (IsEqualGUID(riid, &IID_ID3D10Texture3D)
254             || IsEqualGUID(riid, &IID_ID3D10Resource)
255             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
256             || IsEqualGUID(riid, &IID_IUnknown))
257     {
258         IUnknown_AddRef(iface);
259         *object = iface;
260         return S_OK;
261     }
262
263     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
264
265     *object = NULL;
266     return E_NOINTERFACE;
267 }
268
269 static ULONG STDMETHODCALLTYPE d3d10_texture3d_AddRef(ID3D10Texture3D *iface)
270 {
271     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
272     ULONG refcount = InterlockedIncrement(&texture->refcount);
273
274     TRACE("%p increasing refcount to %u.\n", texture, refcount);
275
276     if (refcount == 1)
277         wined3d_texture_incref(texture->wined3d_texture);
278
279     return refcount;
280 }
281
282 static void STDMETHODCALLTYPE d3d10_texture3d_wined3d_object_released(void *parent)
283 {
284     HeapFree(GetProcessHeap(), 0, parent);
285 }
286
287 static ULONG STDMETHODCALLTYPE d3d10_texture3d_Release(ID3D10Texture3D *iface)
288 {
289     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
290     ULONG refcount = InterlockedDecrement(&texture->refcount);
291
292     TRACE("%p decreasing refcount to %u.\n", texture, refcount);
293
294     if (!refcount)
295         wined3d_texture_decref(texture->wined3d_texture);
296
297     return refcount;
298 }
299
300 static void STDMETHODCALLTYPE d3d10_texture3d_GetDevice(ID3D10Texture3D *iface, ID3D10Device **device)
301 {
302     FIXME("iface %p, device %p stub!\n", iface, device);
303 }
304
305 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_GetPrivateData(ID3D10Texture3D *iface,
306         REFGUID guid, UINT *data_size, void *data)
307 {
308     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
309             iface, debugstr_guid(guid), data_size, data);
310
311     return E_NOTIMPL;
312 }
313
314 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateData(ID3D10Texture3D *iface,
315         REFGUID guid, UINT data_size, const void *data)
316 {
317     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
318             iface, debugstr_guid(guid), data_size, data);
319
320     return E_NOTIMPL;
321 }
322
323 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateDataInterface(ID3D10Texture3D *iface,
324         REFGUID guid, const IUnknown *data)
325 {
326     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
327
328     return E_NOTIMPL;
329 }
330
331 static void STDMETHODCALLTYPE d3d10_texture3d_GetType(ID3D10Texture3D *iface,
332         D3D10_RESOURCE_DIMENSION *resource_dimension)
333 {
334     TRACE("iface %p, resource_dimension %p.\n", iface, resource_dimension);
335
336     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE3D;
337 }
338
339 static void STDMETHODCALLTYPE d3d10_texture3d_SetEvictionPriority(ID3D10Texture3D *iface, UINT eviction_priority)
340 {
341     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
342 }
343
344 static UINT STDMETHODCALLTYPE d3d10_texture3d_GetEvictionPriority(ID3D10Texture3D *iface)
345 {
346     FIXME("iface %p stub!\n", iface);
347
348     return 0;
349 }
350
351 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_Map(ID3D10Texture3D *iface, UINT sub_resource_idx,
352         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE3D *mapped_texture)
353 {
354     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
355     struct wined3d_resource *sub_resource;
356     WINED3DLOCKED_BOX wined3d_map_desc;
357     HRESULT hr;
358
359     TRACE("iface %p, sub_resource_idx %u, map_type %u, map_flags %#x, mapped_texture %p.\n",
360             iface, sub_resource_idx, map_type, map_flags, mapped_texture);
361
362     if (map_type != D3D10_MAP_READ_WRITE)
363         FIXME("Ignoring map_type %#x.\n", map_type);
364     if (map_flags)
365         FIXME("Ignoring map_flags %#x.\n", map_flags);
366
367     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
368         hr = E_INVALIDARG;
369     else if (SUCCEEDED(hr = wined3d_volume_map(wined3d_volume_from_resource(sub_resource),
370             &wined3d_map_desc, NULL, 0)))
371     {
372         mapped_texture->pData = wined3d_map_desc.pBits;
373         mapped_texture->RowPitch = wined3d_map_desc.RowPitch;
374         mapped_texture->DepthPitch = wined3d_map_desc.SlicePitch;
375     }
376
377     return hr;
378 }
379
380 static void STDMETHODCALLTYPE d3d10_texture3d_Unmap(ID3D10Texture3D *iface, UINT sub_resource_idx)
381 {
382     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
383     struct wined3d_resource *sub_resource;
384
385     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
386
387     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
388         return;
389
390     wined3d_volume_unmap(wined3d_volume_from_resource(sub_resource));
391 }
392
393 static void STDMETHODCALLTYPE d3d10_texture3d_GetDesc(ID3D10Texture3D *iface, D3D10_TEXTURE3D_DESC *desc)
394 {
395     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
396
397     TRACE("iface %p, desc %p.\n", iface, desc);
398
399     *desc = texture->desc;
400 }
401
402 static const struct ID3D10Texture3DVtbl d3d10_texture3d_vtbl =
403 {
404     /* IUnknown methods */
405     d3d10_texture3d_QueryInterface,
406     d3d10_texture3d_AddRef,
407     d3d10_texture3d_Release,
408     /* ID3D10DeviceChild methods */
409     d3d10_texture3d_GetDevice,
410     d3d10_texture3d_GetPrivateData,
411     d3d10_texture3d_SetPrivateData,
412     d3d10_texture3d_SetPrivateDataInterface,
413     /* ID3D10Resource methods */
414     d3d10_texture3d_GetType,
415     d3d10_texture3d_SetEvictionPriority,
416     d3d10_texture3d_GetEvictionPriority,
417     /* ID3D10Texture3D methods */
418     d3d10_texture3d_Map,
419     d3d10_texture3d_Unmap,
420     d3d10_texture3d_GetDesc,
421 };
422
423 static const struct wined3d_parent_ops d3d10_texture3d_wined3d_parent_ops =
424 {
425     d3d10_texture3d_wined3d_object_released,
426 };
427
428 HRESULT d3d10_texture3d_init(struct d3d10_texture3d *texture, struct d3d10_device *device,
429         const D3D10_TEXTURE3D_DESC *desc)
430 {
431     HRESULT hr;
432
433     texture->vtbl = &d3d10_texture3d_vtbl;
434     texture->refcount = 1;
435     texture->desc = *desc;
436
437     FIXME("Implement DXGI<->wined3d usage conversion.\n");
438
439     hr = IWineD3DDevice_CreateVolumeTexture(device->wined3d_device, desc->Width, desc->Height, desc->Depth,
440             desc->MipLevels, desc->Usage, wined3dformat_from_dxgi_format(desc->Format), WINED3DPOOL_DEFAULT,
441             texture, &d3d10_texture3d_wined3d_parent_ops, &texture->wined3d_texture);
442     if (FAILED(hr))
443     {
444         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
445         return hr;
446     }
447
448     return S_OK;
449 }