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