urlmon/tests: Test for return value directly, without macros.
[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) IWineD3DSurface_AddRef(This->wined3d_surface);
65
66     return refcount;
67 }
68
69 static void STDMETHODCALLTYPE d3d10_texture2d_wined3d_object_released(void *parent)
70 {
71     struct d3d10_texture2d *This = parent;
72
73     if (This->dxgi_surface) IDXGISurface_Release(This->dxgi_surface);
74     HeapFree(GetProcessHeap(), 0, This);
75 }
76
77 static ULONG STDMETHODCALLTYPE d3d10_texture2d_Release(ID3D10Texture2D *iface)
78 {
79     struct d3d10_texture2d *This = (struct d3d10_texture2d *)iface;
80     ULONG refcount = InterlockedDecrement(&This->refcount);
81
82     TRACE("%p decreasing refcount to %u\n", This, refcount);
83
84     if (!refcount)
85     {
86         if (This->wined3d_surface) IWineD3DSurface_Release(This->wined3d_surface);
87         else d3d10_texture2d_wined3d_object_released(This);
88     }
89
90     return refcount;
91 }
92
93 /* ID3D10DeviceChild methods */
94
95 static void STDMETHODCALLTYPE d3d10_texture2d_GetDevice(ID3D10Texture2D *iface, ID3D10Device **device)
96 {
97     FIXME("iface %p, device %p stub!\n", iface, device);
98 }
99
100 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_GetPrivateData(ID3D10Texture2D *iface,
101         REFGUID guid, UINT *data_size, void *data)
102 {
103     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
104             iface, debugstr_guid(guid), data_size, data);
105
106     return E_NOTIMPL;
107 }
108
109 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateData(ID3D10Texture2D *iface,
110         REFGUID guid, UINT data_size, const void *data)
111 {
112     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
113             iface, debugstr_guid(guid), data_size, data);
114
115     return E_NOTIMPL;
116 }
117
118 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateDataInterface(ID3D10Texture2D *iface,
119         REFGUID guid, const IUnknown *data)
120 {
121     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
122
123     return E_NOTIMPL;
124 }
125
126 /* ID3D10Resource methods */
127
128 static void STDMETHODCALLTYPE d3d10_texture2d_GetType(ID3D10Texture2D *iface,
129         D3D10_RESOURCE_DIMENSION *resource_dimension)
130 {
131     TRACE("iface %p, resource_dimension %p\n", iface, resource_dimension);
132
133     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
134 }
135
136 static void STDMETHODCALLTYPE d3d10_texture2d_SetEvictionPriority(ID3D10Texture2D *iface, UINT eviction_priority)
137 {
138     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
139 }
140
141 static UINT STDMETHODCALLTYPE d3d10_texture2d_GetEvictionPriority(ID3D10Texture2D *iface)
142 {
143     FIXME("iface %p stub!\n", iface);
144
145     return 0;
146 }
147
148 /* ID3D10Texture2D methods */
149
150 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_Map(ID3D10Texture2D *iface, UINT sub_resource,
151         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE2D *mapped_texture)
152 {
153     FIXME("iface %p, sub_resource %u, map_type %u, map_flags %#x, mapped_texture %p stub!\n",
154             iface, sub_resource, map_type, map_flags, mapped_texture);
155
156     return E_NOTIMPL;
157 }
158
159 static void STDMETHODCALLTYPE d3d10_texture2d_Unmap(ID3D10Texture2D *iface, UINT sub_resource)
160 {
161     FIXME("iface %p, sub_resource %u stub!\n", iface, sub_resource);
162 }
163
164 static void STDMETHODCALLTYPE d3d10_texture2d_GetDesc(ID3D10Texture2D *iface, D3D10_TEXTURE2D_DESC *desc)
165 {
166     struct d3d10_texture2d *This = (struct d3d10_texture2d *)iface;
167
168     TRACE("iface %p, desc %p\n", iface, desc);
169
170     *desc = This->desc;
171 }
172
173 static const struct ID3D10Texture2DVtbl d3d10_texture2d_vtbl =
174 {
175     /* IUnknown methods */
176     d3d10_texture2d_QueryInterface,
177     d3d10_texture2d_AddRef,
178     d3d10_texture2d_Release,
179     /* ID3D10DeviceChild methods */
180     d3d10_texture2d_GetDevice,
181     d3d10_texture2d_GetPrivateData,
182     d3d10_texture2d_SetPrivateData,
183     d3d10_texture2d_SetPrivateDataInterface,
184     /* ID3D10Resource methods */
185     d3d10_texture2d_GetType,
186     d3d10_texture2d_SetEvictionPriority,
187     d3d10_texture2d_GetEvictionPriority,
188     /* ID3D10Texture2D methods */
189     d3d10_texture2d_Map,
190     d3d10_texture2d_Unmap,
191     d3d10_texture2d_GetDesc,
192 };
193
194 static const struct wined3d_parent_ops d3d10_texture2d_wined3d_parent_ops =
195 {
196     d3d10_texture2d_wined3d_object_released,
197 };
198
199 HRESULT d3d10_texture2d_init(struct d3d10_texture2d *texture, struct d3d10_device *device,
200         const D3D10_TEXTURE2D_DESC *desc)
201 {
202     HRESULT hr;
203
204     texture->vtbl = &d3d10_texture2d_vtbl;
205     texture->refcount = 1;
206     texture->desc = *desc;
207
208     if (desc->MipLevels == 1 && desc->ArraySize == 1)
209     {
210         IWineDXGIDevice *wine_device;
211
212         hr = ID3D10Device_QueryInterface((ID3D10Device *)device, &IID_IWineDXGIDevice, (void **)&wine_device);
213         if (FAILED(hr))
214         {
215             ERR("Device should implement IWineDXGIDevice\n");
216             return E_FAIL;
217         }
218
219         hr = IWineDXGIDevice_create_surface(wine_device, NULL, 0, NULL,
220                 (IUnknown *)texture, (void **)&texture->dxgi_surface);
221         IWineDXGIDevice_Release(wine_device);
222         if (FAILED(hr))
223         {
224             ERR("Failed to create DXGI surface, returning %#x\n", hr);
225             return hr;
226         }
227
228         FIXME("Implement DXGI<->wined3d usage conversion\n");
229
230         hr = IWineD3DDevice_CreateSurface(device->wined3d_device, desc->Width, desc->Height,
231                 wined3dformat_from_dxgi_format(desc->Format), FALSE, FALSE, 0, desc->Usage, WINED3DPOOL_DEFAULT,
232                 desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3DMULTISAMPLE_NONE,
233                 desc->SampleDesc.Quality, SURFACE_OPENGL, texture, &d3d10_texture2d_wined3d_parent_ops,
234                 &texture->wined3d_surface);
235         if (FAILED(hr))
236         {
237             ERR("CreateSurface failed, returning %#x\n", hr);
238             IDXGISurface_Release(texture->dxgi_surface);
239             return hr;
240         }
241     }
242
243     return S_OK;
244 }
245
246 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_QueryInterface(ID3D10Texture3D *iface, REFIID riid, void **object)
247 {
248     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
249
250     if (IsEqualGUID(riid, &IID_ID3D10Texture3D)
251             || IsEqualGUID(riid, &IID_ID3D10Resource)
252             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
253             || IsEqualGUID(riid, &IID_IUnknown))
254     {
255         IUnknown_AddRef(iface);
256         *object = iface;
257         return S_OK;
258     }
259
260     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
261
262     *object = NULL;
263     return E_NOINTERFACE;
264 }
265
266 static ULONG STDMETHODCALLTYPE d3d10_texture3d_AddRef(ID3D10Texture3D *iface)
267 {
268     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
269     ULONG refcount = InterlockedIncrement(&texture->refcount);
270
271     TRACE("%p increasing refcount to %u.\n", texture, refcount);
272
273     if (refcount == 1)
274         wined3d_texture_incref(texture->wined3d_texture);
275
276     return refcount;
277 }
278
279 static void STDMETHODCALLTYPE d3d10_texture3d_wined3d_object_released(void *parent)
280 {
281     HeapFree(GetProcessHeap(), 0, parent);
282 }
283
284 static ULONG STDMETHODCALLTYPE d3d10_texture3d_Release(ID3D10Texture3D *iface)
285 {
286     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
287     ULONG refcount = InterlockedDecrement(&texture->refcount);
288
289     TRACE("%p decreasing refcount to %u.\n", texture, refcount);
290
291     if (!refcount)
292         wined3d_texture_decref(texture->wined3d_texture);
293
294     return refcount;
295 }
296
297 static void STDMETHODCALLTYPE d3d10_texture3d_GetDevice(ID3D10Texture3D *iface, ID3D10Device **device)
298 {
299     FIXME("iface %p, device %p stub!\n", iface, device);
300 }
301
302 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_GetPrivateData(ID3D10Texture3D *iface,
303         REFGUID guid, UINT *data_size, void *data)
304 {
305     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
306             iface, debugstr_guid(guid), data_size, data);
307
308     return E_NOTIMPL;
309 }
310
311 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateData(ID3D10Texture3D *iface,
312         REFGUID guid, UINT data_size, const void *data)
313 {
314     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
315             iface, debugstr_guid(guid), data_size, data);
316
317     return E_NOTIMPL;
318 }
319
320 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateDataInterface(ID3D10Texture3D *iface,
321         REFGUID guid, const IUnknown *data)
322 {
323     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
324
325     return E_NOTIMPL;
326 }
327
328 static void STDMETHODCALLTYPE d3d10_texture3d_GetType(ID3D10Texture3D *iface,
329         D3D10_RESOURCE_DIMENSION *resource_dimension)
330 {
331     TRACE("iface %p, resource_dimension %p.\n", iface, resource_dimension);
332
333     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE3D;
334 }
335
336 static void STDMETHODCALLTYPE d3d10_texture3d_SetEvictionPriority(ID3D10Texture3D *iface, UINT eviction_priority)
337 {
338     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
339 }
340
341 static UINT STDMETHODCALLTYPE d3d10_texture3d_GetEvictionPriority(ID3D10Texture3D *iface)
342 {
343     FIXME("iface %p stub!\n", iface);
344
345     return 0;
346 }
347
348 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_Map(ID3D10Texture3D *iface, UINT sub_resource_idx,
349         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE3D *mapped_texture)
350 {
351     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
352     struct wined3d_resource *sub_resource;
353     WINED3DLOCKED_BOX wined3d_map_desc;
354     HRESULT hr;
355
356     TRACE("iface %p, sub_resource_idx %u, map_type %u, map_flags %#x, mapped_texture %p.\n",
357             iface, sub_resource_idx, map_type, map_flags, mapped_texture);
358
359     if (map_type != D3D10_MAP_READ_WRITE)
360         FIXME("Ignoring map_type %#x.\n", map_type);
361     if (map_flags)
362         FIXME("Ignoring map_flags %#x.\n", map_flags);
363
364     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
365         hr = E_INVALIDARG;
366     else if (SUCCEEDED(hr = IWineD3DVolume_Map(wined3d_volume_from_resource(sub_resource),
367             &wined3d_map_desc, NULL, 0)))
368     {
369         mapped_texture->pData = wined3d_map_desc.pBits;
370         mapped_texture->RowPitch = wined3d_map_desc.RowPitch;
371         mapped_texture->DepthPitch = wined3d_map_desc.SlicePitch;
372     }
373
374     return hr;
375 }
376
377 static void STDMETHODCALLTYPE d3d10_texture3d_Unmap(ID3D10Texture3D *iface, UINT sub_resource_idx)
378 {
379     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
380     struct wined3d_resource *sub_resource;
381
382     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
383
384     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
385         return;
386
387     IWineD3DVolume_Unmap(wined3d_volume_from_resource(sub_resource));
388 }
389
390 static void STDMETHODCALLTYPE d3d10_texture3d_GetDesc(ID3D10Texture3D *iface, D3D10_TEXTURE3D_DESC *desc)
391 {
392     struct d3d10_texture3d *texture = (struct d3d10_texture3d *)iface;
393
394     TRACE("iface %p, desc %p.\n", iface, desc);
395
396     *desc = texture->desc;
397 }
398
399 static const struct ID3D10Texture3DVtbl d3d10_texture3d_vtbl =
400 {
401     /* IUnknown methods */
402     d3d10_texture3d_QueryInterface,
403     d3d10_texture3d_AddRef,
404     d3d10_texture3d_Release,
405     /* ID3D10DeviceChild methods */
406     d3d10_texture3d_GetDevice,
407     d3d10_texture3d_GetPrivateData,
408     d3d10_texture3d_SetPrivateData,
409     d3d10_texture3d_SetPrivateDataInterface,
410     /* ID3D10Resource methods */
411     d3d10_texture3d_GetType,
412     d3d10_texture3d_SetEvictionPriority,
413     d3d10_texture3d_GetEvictionPriority,
414     /* ID3D10Texture3D methods */
415     d3d10_texture3d_Map,
416     d3d10_texture3d_Unmap,
417     d3d10_texture3d_GetDesc,
418 };
419
420 static const struct wined3d_parent_ops d3d10_texture3d_wined3d_parent_ops =
421 {
422     d3d10_texture3d_wined3d_object_released,
423 };
424
425 HRESULT d3d10_texture3d_init(struct d3d10_texture3d *texture, struct d3d10_device *device,
426         const D3D10_TEXTURE3D_DESC *desc)
427 {
428     HRESULT hr;
429
430     texture->vtbl = &d3d10_texture3d_vtbl;
431     texture->refcount = 1;
432     texture->desc = *desc;
433
434     FIXME("Implement DXGI<->wined3d usage conversion.\n");
435
436     hr = IWineD3DDevice_CreateVolumeTexture(device->wined3d_device, desc->Width, desc->Height, desc->Depth,
437             desc->MipLevels, desc->Usage, wined3dformat_from_dxgi_format(desc->Format), WINED3DPOOL_DEFAULT,
438             texture, &d3d10_texture3d_wined3d_parent_ops, &texture->wined3d_texture);
439     if (FAILED(hr))
440     {
441         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
442         return hr;
443     }
444
445     return S_OK;
446 }