d3d10core: Implement d3d10_texture2d_Unmap().
[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 static inline struct d3d10_texture2d *impl_from_ID3D10Texture2D(ID3D10Texture2D *iface)
28 {
29     return CONTAINING_RECORD(iface, struct d3d10_texture2d, ID3D10Texture2D_iface);
30 }
31
32 /* IUnknown methods */
33
34 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_QueryInterface(ID3D10Texture2D *iface, REFIID riid, void **object)
35 {
36     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
37
38     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
39
40     if (IsEqualGUID(riid, &IID_ID3D10Texture2D)
41             || IsEqualGUID(riid, &IID_ID3D10Resource)
42             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
43             || IsEqualGUID(riid, &IID_IUnknown))
44     {
45         IUnknown_AddRef(iface);
46         *object = iface;
47         return S_OK;
48     }
49
50     if (This->dxgi_surface)
51     {
52         TRACE("Forwarding to dxgi surface\n");
53         return IDXGISurface_QueryInterface(This->dxgi_surface, riid, object);
54     }
55
56     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
57
58     *object = NULL;
59     return E_NOINTERFACE;
60 }
61
62 static ULONG STDMETHODCALLTYPE d3d10_texture2d_AddRef(ID3D10Texture2D *iface)
63 {
64     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
65     ULONG refcount = InterlockedIncrement(&This->refcount);
66
67     TRACE("%p increasing refcount to %u\n", This, refcount);
68
69     if (refcount == 1)
70         wined3d_texture_incref(This->wined3d_texture);
71
72     return refcount;
73 }
74
75 static void STDMETHODCALLTYPE d3d10_texture2d_wined3d_object_released(void *parent)
76 {
77     struct d3d10_texture2d *This = parent;
78
79     if (This->dxgi_surface) IDXGISurface_Release(This->dxgi_surface);
80     HeapFree(GetProcessHeap(), 0, This);
81 }
82
83 static ULONG STDMETHODCALLTYPE d3d10_texture2d_Release(ID3D10Texture2D *iface)
84 {
85     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
86     ULONG refcount = InterlockedDecrement(&This->refcount);
87
88     TRACE("%p decreasing refcount to %u\n", This, refcount);
89
90     if (!refcount)
91         wined3d_texture_decref(This->wined3d_texture);
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_idx)
163 {
164     struct d3d10_texture2d *texture = impl_from_ID3D10Texture2D(iface);
165     struct wined3d_resource *sub_resource;
166
167     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
168
169     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
170         return;
171
172     wined3d_surface_unmap(wined3d_surface_from_resource(sub_resource));
173 }
174
175 static void STDMETHODCALLTYPE d3d10_texture2d_GetDesc(ID3D10Texture2D *iface, D3D10_TEXTURE2D_DESC *desc)
176 {
177     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
178
179     TRACE("iface %p, desc %p\n", iface, desc);
180
181     *desc = This->desc;
182 }
183
184 static const struct ID3D10Texture2DVtbl d3d10_texture2d_vtbl =
185 {
186     /* IUnknown methods */
187     d3d10_texture2d_QueryInterface,
188     d3d10_texture2d_AddRef,
189     d3d10_texture2d_Release,
190     /* ID3D10DeviceChild methods */
191     d3d10_texture2d_GetDevice,
192     d3d10_texture2d_GetPrivateData,
193     d3d10_texture2d_SetPrivateData,
194     d3d10_texture2d_SetPrivateDataInterface,
195     /* ID3D10Resource methods */
196     d3d10_texture2d_GetType,
197     d3d10_texture2d_SetEvictionPriority,
198     d3d10_texture2d_GetEvictionPriority,
199     /* ID3D10Texture2D methods */
200     d3d10_texture2d_Map,
201     d3d10_texture2d_Unmap,
202     d3d10_texture2d_GetDesc,
203 };
204
205 static const struct wined3d_parent_ops d3d10_texture2d_wined3d_parent_ops =
206 {
207     d3d10_texture2d_wined3d_object_released,
208 };
209
210 HRESULT d3d10_texture2d_init(struct d3d10_texture2d *texture, struct d3d10_device *device,
211         const D3D10_TEXTURE2D_DESC *desc)
212 {
213     HRESULT hr;
214
215     texture->ID3D10Texture2D_iface.lpVtbl = &d3d10_texture2d_vtbl;
216     texture->refcount = 1;
217     texture->desc = *desc;
218
219     if (desc->MipLevels == 1 && desc->ArraySize == 1)
220     {
221         IWineDXGIDevice *wine_device;
222
223         hr = ID3D10Device_QueryInterface(&device->ID3D10Device_iface, &IID_IWineDXGIDevice,
224                 (void **)&wine_device);
225         if (FAILED(hr))
226         {
227             ERR("Device should implement IWineDXGIDevice\n");
228             return E_FAIL;
229         }
230
231         hr = IWineDXGIDevice_create_surface(wine_device, NULL, 0, NULL,
232                 (IUnknown *)&texture->ID3D10Texture2D_iface, (void **)&texture->dxgi_surface);
233         IWineDXGIDevice_Release(wine_device);
234         if (FAILED(hr))
235         {
236             ERR("Failed to create DXGI surface, returning %#x\n", hr);
237             return hr;
238         }
239     }
240
241     FIXME("Implement DXGI<->wined3d usage conversion\n");
242     if (desc->ArraySize != 1)
243         FIXME("Array textures not implemented.\n");
244     if (desc->SampleDesc.Count > 1)
245         FIXME("Multisampled textures not implemented.\n");
246
247     hr = wined3d_texture_create_2d(device->wined3d_device, desc->Width, desc->Height,
248             desc->MipLevels, desc->Usage, wined3dformat_from_dxgi_format(desc->Format), WINED3D_POOL_DEFAULT,
249             texture, &d3d10_texture2d_wined3d_parent_ops, &texture->wined3d_texture);
250     if (FAILED(hr))
251     {
252         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
253         if (texture->dxgi_surface)
254             IDXGISurface_Release(texture->dxgi_surface);
255         return hr;
256     }
257
258     return S_OK;
259 }
260
261 static inline struct d3d10_texture3d *impl_from_ID3D10Texture3D(ID3D10Texture3D *iface)
262 {
263     return CONTAINING_RECORD(iface, struct d3d10_texture3d, ID3D10Texture3D_iface);
264 }
265
266 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_QueryInterface(ID3D10Texture3D *iface, REFIID riid, void **object)
267 {
268     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
269
270     if (IsEqualGUID(riid, &IID_ID3D10Texture3D)
271             || IsEqualGUID(riid, &IID_ID3D10Resource)
272             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
273             || IsEqualGUID(riid, &IID_IUnknown))
274     {
275         IUnknown_AddRef(iface);
276         *object = iface;
277         return S_OK;
278     }
279
280     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
281
282     *object = NULL;
283     return E_NOINTERFACE;
284 }
285
286 static ULONG STDMETHODCALLTYPE d3d10_texture3d_AddRef(ID3D10Texture3D *iface)
287 {
288     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
289     ULONG refcount = InterlockedIncrement(&texture->refcount);
290
291     TRACE("%p increasing refcount to %u.\n", texture, refcount);
292
293     if (refcount == 1)
294         wined3d_texture_incref(texture->wined3d_texture);
295
296     return refcount;
297 }
298
299 static void STDMETHODCALLTYPE d3d10_texture3d_wined3d_object_released(void *parent)
300 {
301     HeapFree(GetProcessHeap(), 0, parent);
302 }
303
304 static ULONG STDMETHODCALLTYPE d3d10_texture3d_Release(ID3D10Texture3D *iface)
305 {
306     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
307     ULONG refcount = InterlockedDecrement(&texture->refcount);
308
309     TRACE("%p decreasing refcount to %u.\n", texture, refcount);
310
311     if (!refcount)
312         wined3d_texture_decref(texture->wined3d_texture);
313
314     return refcount;
315 }
316
317 static void STDMETHODCALLTYPE d3d10_texture3d_GetDevice(ID3D10Texture3D *iface, ID3D10Device **device)
318 {
319     FIXME("iface %p, device %p stub!\n", iface, device);
320 }
321
322 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_GetPrivateData(ID3D10Texture3D *iface,
323         REFGUID guid, UINT *data_size, void *data)
324 {
325     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
326             iface, debugstr_guid(guid), data_size, data);
327
328     return E_NOTIMPL;
329 }
330
331 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateData(ID3D10Texture3D *iface,
332         REFGUID guid, UINT data_size, const void *data)
333 {
334     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
335             iface, debugstr_guid(guid), data_size, data);
336
337     return E_NOTIMPL;
338 }
339
340 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateDataInterface(ID3D10Texture3D *iface,
341         REFGUID guid, const IUnknown *data)
342 {
343     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
344
345     return E_NOTIMPL;
346 }
347
348 static void STDMETHODCALLTYPE d3d10_texture3d_GetType(ID3D10Texture3D *iface,
349         D3D10_RESOURCE_DIMENSION *resource_dimension)
350 {
351     TRACE("iface %p, resource_dimension %p.\n", iface, resource_dimension);
352
353     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE3D;
354 }
355
356 static void STDMETHODCALLTYPE d3d10_texture3d_SetEvictionPriority(ID3D10Texture3D *iface, UINT eviction_priority)
357 {
358     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
359 }
360
361 static UINT STDMETHODCALLTYPE d3d10_texture3d_GetEvictionPriority(ID3D10Texture3D *iface)
362 {
363     FIXME("iface %p stub!\n", iface);
364
365     return 0;
366 }
367
368 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_Map(ID3D10Texture3D *iface, UINT sub_resource_idx,
369         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE3D *mapped_texture)
370 {
371     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
372     struct wined3d_map_desc wined3d_map_desc;
373     struct wined3d_resource *sub_resource;
374     HRESULT hr;
375
376     TRACE("iface %p, sub_resource_idx %u, map_type %u, map_flags %#x, mapped_texture %p.\n",
377             iface, sub_resource_idx, map_type, map_flags, mapped_texture);
378
379     if (map_type != D3D10_MAP_READ_WRITE)
380         FIXME("Ignoring map_type %#x.\n", map_type);
381     if (map_flags)
382         FIXME("Ignoring map_flags %#x.\n", map_flags);
383
384     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
385         hr = E_INVALIDARG;
386     else if (SUCCEEDED(hr = wined3d_volume_map(wined3d_volume_from_resource(sub_resource),
387             &wined3d_map_desc, NULL, 0)))
388     {
389         mapped_texture->pData = wined3d_map_desc.data;
390         mapped_texture->RowPitch = wined3d_map_desc.row_pitch;
391         mapped_texture->DepthPitch = wined3d_map_desc.slice_pitch;
392     }
393
394     return hr;
395 }
396
397 static void STDMETHODCALLTYPE d3d10_texture3d_Unmap(ID3D10Texture3D *iface, UINT sub_resource_idx)
398 {
399     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
400     struct wined3d_resource *sub_resource;
401
402     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
403
404     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
405         return;
406
407     wined3d_volume_unmap(wined3d_volume_from_resource(sub_resource));
408 }
409
410 static void STDMETHODCALLTYPE d3d10_texture3d_GetDesc(ID3D10Texture3D *iface, D3D10_TEXTURE3D_DESC *desc)
411 {
412     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
413
414     TRACE("iface %p, desc %p.\n", iface, desc);
415
416     *desc = texture->desc;
417 }
418
419 static const struct ID3D10Texture3DVtbl d3d10_texture3d_vtbl =
420 {
421     /* IUnknown methods */
422     d3d10_texture3d_QueryInterface,
423     d3d10_texture3d_AddRef,
424     d3d10_texture3d_Release,
425     /* ID3D10DeviceChild methods */
426     d3d10_texture3d_GetDevice,
427     d3d10_texture3d_GetPrivateData,
428     d3d10_texture3d_SetPrivateData,
429     d3d10_texture3d_SetPrivateDataInterface,
430     /* ID3D10Resource methods */
431     d3d10_texture3d_GetType,
432     d3d10_texture3d_SetEvictionPriority,
433     d3d10_texture3d_GetEvictionPriority,
434     /* ID3D10Texture3D methods */
435     d3d10_texture3d_Map,
436     d3d10_texture3d_Unmap,
437     d3d10_texture3d_GetDesc,
438 };
439
440 static const struct wined3d_parent_ops d3d10_texture3d_wined3d_parent_ops =
441 {
442     d3d10_texture3d_wined3d_object_released,
443 };
444
445 HRESULT d3d10_texture3d_init(struct d3d10_texture3d *texture, struct d3d10_device *device,
446         const D3D10_TEXTURE3D_DESC *desc)
447 {
448     HRESULT hr;
449
450     texture->ID3D10Texture3D_iface.lpVtbl = &d3d10_texture3d_vtbl;
451     texture->refcount = 1;
452     texture->desc = *desc;
453
454     FIXME("Implement DXGI<->wined3d usage conversion.\n");
455
456     hr = wined3d_texture_create_3d(device->wined3d_device, desc->Width, desc->Height, desc->Depth,
457             desc->MipLevels, desc->Usage, wined3dformat_from_dxgi_format(desc->Format), WINED3D_POOL_DEFAULT,
458             texture, &d3d10_texture3d_wined3d_parent_ops, &texture->wined3d_texture);
459     if (FAILED(hr))
460     {
461         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
462         return hr;
463     }
464
465     return S_OK;
466 }