d3dx9: Implement volume texture filtering in D3DXFilterTexture.
[wine] / dlls / d3d10core / view.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 #define NONAMELESSUNION
24 #include "d3d10core_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d10core);
27
28 static struct wined3d_resource *wined3d_resource_from_resource(ID3D10Resource *resource)
29 {
30     D3D10_RESOURCE_DIMENSION dimension;
31
32     ID3D10Resource_GetType(resource, &dimension);
33
34     switch(dimension)
35     {
36         case D3D10_RESOURCE_DIMENSION_BUFFER:
37             return wined3d_buffer_get_resource(((struct d3d10_buffer *)resource)->wined3d_buffer);
38
39         case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
40             return wined3d_texture_get_resource(((struct d3d10_texture2d *)resource)->wined3d_texture);
41
42         default:
43             FIXME("Unhandled resource dimension %#x.\n", dimension);
44             return NULL;
45     }
46 }
47
48 static HRESULT set_rtdesc_from_resource(D3D10_RENDER_TARGET_VIEW_DESC *desc, ID3D10Resource *resource)
49 {
50     D3D10_RESOURCE_DIMENSION dimension;
51     HRESULT hr;
52
53     ID3D10Resource_GetType(resource, &dimension);
54
55     switch(dimension)
56     {
57         case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
58         {
59             ID3D10Texture1D *texture;
60             D3D10_TEXTURE1D_DESC texture_desc;
61
62             hr = ID3D10Resource_QueryInterface(resource, &IID_ID3D10Texture1D, (void **)&texture);
63             if (FAILED(hr))
64             {
65                 ERR("Resource of type TEXTURE1D doesn't implement ID3D10Texture1D?\n");
66                 return E_INVALIDARG;
67             }
68
69             ID3D10Texture1D_GetDesc(texture, &texture_desc);
70             ID3D10Texture1D_Release(texture);
71
72             desc->Format = texture_desc.Format;
73             if (texture_desc.ArraySize == 1)
74             {
75                 desc->ViewDimension = D3D10_RTV_DIMENSION_TEXTURE1D;
76                 desc->u.Texture1D.MipSlice = 0;
77             }
78             else
79             {
80                 desc->ViewDimension = D3D10_RTV_DIMENSION_TEXTURE1DARRAY;
81                 desc->u.Texture1DArray.MipSlice = 0;
82                 desc->u.Texture1DArray.FirstArraySlice = 0;
83                 desc->u.Texture1DArray.ArraySize = 1;
84             }
85
86             return S_OK;
87         }
88
89         case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
90         {
91             ID3D10Texture2D *texture;
92             D3D10_TEXTURE2D_DESC texture_desc;
93
94             hr = ID3D10Resource_QueryInterface(resource, &IID_ID3D10Texture2D, (void **)&texture);
95             if (FAILED(hr))
96             {
97                 ERR("Resource of type TEXTURE2D doesn't implement ID3D10Texture2D?\n");
98                 return E_INVALIDARG;
99             }
100
101             ID3D10Texture2D_GetDesc(texture, &texture_desc);
102             ID3D10Texture2D_Release(texture);
103
104             desc->Format = texture_desc.Format;
105             if (texture_desc.ArraySize == 1)
106             {
107                 if (texture_desc.SampleDesc.Count == 1)
108                 {
109                     desc->ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2D;
110                     desc->u.Texture2D.MipSlice = 0;
111                 }
112                 else
113                 {
114                     desc->ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2DMS;
115                 }
116             }
117             else
118             {
119                 if (texture_desc.SampleDesc.Count == 1)
120                 {
121                     desc->ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2DARRAY;
122                     desc->u.Texture2DArray.MipSlice = 0;
123                     desc->u.Texture2DArray.FirstArraySlice = 0;
124                     desc->u.Texture2DArray.ArraySize = 1;
125                 }
126                 else
127                 {
128                     desc->ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2DMSARRAY;
129                     desc->u.Texture2DMSArray.FirstArraySlice = 0;
130                     desc->u.Texture2DMSArray.ArraySize = 1;
131                 }
132             }
133
134             return S_OK;
135         }
136
137         case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
138         {
139             ID3D10Texture3D *texture;
140             D3D10_TEXTURE3D_DESC texture_desc;
141
142             hr = ID3D10Resource_QueryInterface(resource, &IID_ID3D10Texture3D, (void **)&texture);
143             if (FAILED(hr))
144             {
145                 ERR("Resource of type TEXTURE3D doesn't implement ID3D10Texture3D?\n");
146                 return E_INVALIDARG;
147             }
148
149             ID3D10Texture3D_GetDesc(texture, &texture_desc);
150             ID3D10Texture3D_Release(texture);
151
152             desc->Format = texture_desc.Format;
153             desc->ViewDimension = D3D10_RTV_DIMENSION_TEXTURE3D;
154             desc->u.Texture3D.MipSlice = 0;
155             desc->u.Texture3D.FirstWSlice = 0;
156             desc->u.Texture3D.WSize = 1;
157
158             return S_OK;
159         }
160
161         default:
162             FIXME("Unhandled resource dimension %#x.\n", dimension);
163             return E_INVALIDARG;
164     }
165 }
166
167 static inline struct d3d10_depthstencil_view *impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
168 {
169     return CONTAINING_RECORD(iface, struct d3d10_depthstencil_view, ID3D10DepthStencilView_iface);
170 }
171
172 /* IUnknown methods */
173
174 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_QueryInterface(ID3D10DepthStencilView *iface,
175         REFIID riid, void **object)
176 {
177     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
178
179     if (IsEqualGUID(riid, &IID_ID3D10DepthStencilView)
180             || IsEqualGUID(riid, &IID_ID3D10View)
181             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
182             || IsEqualGUID(riid, &IID_IUnknown))
183     {
184         IUnknown_AddRef(iface);
185         *object = iface;
186         return S_OK;
187     }
188
189     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
190
191     *object = NULL;
192     return E_NOINTERFACE;
193 }
194
195 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_AddRef(ID3D10DepthStencilView *iface)
196 {
197     struct d3d10_depthstencil_view *This = impl_from_ID3D10DepthStencilView(iface);
198     ULONG refcount = InterlockedIncrement(&This->refcount);
199
200     TRACE("%p increasing refcount to %u.\n", This, refcount);
201
202     return refcount;
203 }
204
205 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_Release(ID3D10DepthStencilView *iface)
206 {
207     struct d3d10_depthstencil_view *This = impl_from_ID3D10DepthStencilView(iface);
208     ULONG refcount = InterlockedDecrement(&This->refcount);
209
210     TRACE("%p decreasing refcount to %u.\n", This, refcount);
211
212     if (!refcount)
213     {
214         HeapFree(GetProcessHeap(), 0, This);
215     }
216
217     return refcount;
218 }
219
220 /* ID3D10DeviceChild methods */
221
222 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDevice(ID3D10DepthStencilView *iface, ID3D10Device **device)
223 {
224     FIXME("iface %p, device %p stub!\n", iface, device);
225 }
226
227 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_GetPrivateData(ID3D10DepthStencilView *iface,
228         REFGUID guid, UINT *data_size, void *data)
229 {
230     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
231             iface, debugstr_guid(guid), data_size, data);
232
233     return E_NOTIMPL;
234 }
235
236 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateData(ID3D10DepthStencilView *iface,
237         REFGUID guid, UINT data_size, const void *data)
238 {
239     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
240             iface, debugstr_guid(guid), data_size, data);
241
242     return E_NOTIMPL;
243 }
244
245 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateDataInterface(ID3D10DepthStencilView *iface,
246         REFGUID guid, const IUnknown *data)
247 {
248     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
249
250     return E_NOTIMPL;
251 }
252
253 /* ID3D10View methods */
254
255 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetResource(ID3D10DepthStencilView *iface,
256         ID3D10Resource **resource)
257 {
258     FIXME("iface %p, resource %p stub!\n", iface, resource);
259 }
260
261 /* ID3D10DepthStencilView methods */
262
263 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDesc(ID3D10DepthStencilView *iface,
264         D3D10_DEPTH_STENCIL_VIEW_DESC *desc)
265 {
266     FIXME("iface %p, desc %p stub!\n", iface, desc);
267 }
268
269 static const struct ID3D10DepthStencilViewVtbl d3d10_depthstencil_view_vtbl =
270 {
271     /* IUnknown methods */
272     d3d10_depthstencil_view_QueryInterface,
273     d3d10_depthstencil_view_AddRef,
274     d3d10_depthstencil_view_Release,
275     /* ID3D10DeviceChild methods */
276     d3d10_depthstencil_view_GetDevice,
277     d3d10_depthstencil_view_GetPrivateData,
278     d3d10_depthstencil_view_SetPrivateData,
279     d3d10_depthstencil_view_SetPrivateDataInterface,
280     /* ID3D10View methods */
281     d3d10_depthstencil_view_GetResource,
282     /* ID3D10DepthStencilView methods */
283     d3d10_depthstencil_view_GetDesc,
284 };
285
286 HRESULT d3d10_depthstencil_view_init(struct d3d10_depthstencil_view *view)
287 {
288     view->ID3D10DepthStencilView_iface.lpVtbl = &d3d10_depthstencil_view_vtbl;
289     view->refcount = 1;
290
291     return S_OK;
292 }
293
294 static inline struct d3d10_rendertarget_view *impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
295 {
296     return CONTAINING_RECORD(iface, struct d3d10_rendertarget_view, ID3D10RenderTargetView_iface);
297 }
298
299 /* IUnknown methods */
300
301 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_QueryInterface(ID3D10RenderTargetView *iface,
302         REFIID riid, void **object)
303 {
304     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
305
306     if (IsEqualGUID(riid, &IID_ID3D10RenderTargetView)
307             || IsEqualGUID(riid, &IID_ID3D10View)
308             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
309             || IsEqualGUID(riid, &IID_IUnknown))
310     {
311         IUnknown_AddRef(iface);
312         *object = iface;
313         return S_OK;
314     }
315
316     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
317
318     *object = NULL;
319     return E_NOINTERFACE;
320 }
321
322 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_AddRef(ID3D10RenderTargetView *iface)
323 {
324     struct d3d10_rendertarget_view *This = impl_from_ID3D10RenderTargetView(iface);
325     ULONG refcount = InterlockedIncrement(&This->refcount);
326
327     TRACE("%p increasing refcount to %u\n", This, refcount);
328
329     return refcount;
330 }
331
332 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_Release(ID3D10RenderTargetView *iface)
333 {
334     struct d3d10_rendertarget_view *This = impl_from_ID3D10RenderTargetView(iface);
335     ULONG refcount = InterlockedDecrement(&This->refcount);
336
337     TRACE("%p decreasing refcount to %u\n", This, refcount);
338
339     if (!refcount)
340     {
341         wined3d_rendertarget_view_decref(This->wined3d_view);
342         HeapFree(GetProcessHeap(), 0, This);
343     }
344
345     return refcount;
346 }
347
348 /* ID3D10DeviceChild methods */
349
350 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDevice(ID3D10RenderTargetView *iface, ID3D10Device **device)
351 {
352     FIXME("iface %p, device %p stub!\n", iface, device);
353 }
354
355 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_GetPrivateData(ID3D10RenderTargetView *iface,
356         REFGUID guid, UINT *data_size, void *data)
357 {
358     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
359             iface, debugstr_guid(guid), data_size, data);
360
361     return E_NOTIMPL;
362 }
363
364 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateData(ID3D10RenderTargetView *iface,
365         REFGUID guid, UINT data_size, const void *data)
366 {
367     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
368             iface, debugstr_guid(guid), data_size, data);
369
370     return E_NOTIMPL;
371 }
372
373 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateDataInterface(ID3D10RenderTargetView *iface,
374         REFGUID guid, const IUnknown *data)
375 {
376     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
377
378     return E_NOTIMPL;
379 }
380
381 /* ID3D10View methods */
382
383 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetResource(ID3D10RenderTargetView *iface,
384         ID3D10Resource **resource)
385 {
386     struct d3d10_rendertarget_view *This = impl_from_ID3D10RenderTargetView(iface);
387     struct wined3d_resource *wined3d_resource;
388     IUnknown *parent;
389     HRESULT hr;
390
391     TRACE("iface %p, resource %p\n", iface, resource);
392
393     wined3d_resource = wined3d_rendertarget_view_get_resource(This->wined3d_view);
394     if (!wined3d_resource)
395     {
396         ERR("Failed to get wined3d resource.\n");
397         *resource = NULL;
398         return;
399     }
400
401     parent = wined3d_resource_get_parent(wined3d_resource);
402     hr = IUnknown_QueryInterface(parent, &IID_ID3D10Resource, (void **)&resource);
403     if (FAILED(hr))
404     {
405         ERR("Resource parent isn't a d3d10 resource, hr %#x\n", hr);
406         *resource = NULL;
407         return;
408     }
409 }
410
411 /* ID3D10RenderTargetView methods */
412
413 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDesc(ID3D10RenderTargetView *iface,
414         D3D10_RENDER_TARGET_VIEW_DESC *desc)
415 {
416     struct d3d10_rendertarget_view *This = impl_from_ID3D10RenderTargetView(iface);
417
418     TRACE("iface %p, desc %p\n", iface, desc);
419
420     *desc = This->desc;
421 }
422
423 static const struct ID3D10RenderTargetViewVtbl d3d10_rendertarget_view_vtbl =
424 {
425     /* IUnknown methods */
426     d3d10_rendertarget_view_QueryInterface,
427     d3d10_rendertarget_view_AddRef,
428     d3d10_rendertarget_view_Release,
429     /* ID3D10DeviceChild methods */
430     d3d10_rendertarget_view_GetDevice,
431     d3d10_rendertarget_view_GetPrivateData,
432     d3d10_rendertarget_view_SetPrivateData,
433     d3d10_rendertarget_view_SetPrivateDataInterface,
434     /* ID3D10View methods */
435     d3d10_rendertarget_view_GetResource,
436     /* ID3D10RenderTargetView methods */
437     d3d10_rendertarget_view_GetDesc,
438 };
439
440 HRESULT d3d10_rendertarget_view_init(struct d3d10_rendertarget_view *view,
441         ID3D10Resource *resource, const D3D10_RENDER_TARGET_VIEW_DESC *desc)
442 {
443     struct wined3d_resource *wined3d_resource;
444     HRESULT hr;
445
446     view->ID3D10RenderTargetView_iface.lpVtbl = &d3d10_rendertarget_view_vtbl;
447     view->refcount = 1;
448
449     if (!desc)
450     {
451         HRESULT hr = set_rtdesc_from_resource(&view->desc, resource);
452         if (FAILED(hr)) return hr;
453     }
454     else
455     {
456         view->desc = *desc;
457     }
458
459     wined3d_resource = wined3d_resource_from_resource(resource);
460     if (!wined3d_resource)
461     {
462         ERR("Failed to get wined3d resource for d3d10 resource %p.\n", resource);
463         return E_FAIL;
464     }
465
466     hr = wined3d_rendertarget_view_create(wined3d_resource, view, &view->wined3d_view);
467     if (FAILED(hr))
468     {
469         WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
470         return hr;
471     }
472
473     return S_OK;
474 }
475
476 struct d3d10_rendertarget_view *unsafe_impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
477 {
478     if (!iface)
479         return NULL;
480     assert(iface->lpVtbl == &d3d10_rendertarget_view_vtbl);
481
482     return impl_from_ID3D10RenderTargetView(iface);
483 }
484
485 static inline struct d3d10_shader_resource_view *impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView *iface)
486 {
487     return CONTAINING_RECORD(iface, struct d3d10_shader_resource_view, ID3D10ShaderResourceView_iface);
488 }
489
490 /* IUnknown methods */
491
492 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_QueryInterface(ID3D10ShaderResourceView *iface,
493         REFIID riid, void **object)
494 {
495     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
496
497     if (IsEqualGUID(riid, &IID_ID3D10ShaderResourceView)
498             || IsEqualGUID(riid, &IID_ID3D10View)
499             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
500             || IsEqualGUID(riid, &IID_IUnknown))
501     {
502         IUnknown_AddRef(iface);
503         *object = iface;
504         return S_OK;
505     }
506
507     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
508
509     *object = NULL;
510     return E_NOINTERFACE;
511 }
512
513 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_AddRef(ID3D10ShaderResourceView *iface)
514 {
515     struct d3d10_shader_resource_view *This = impl_from_ID3D10ShaderResourceView(iface);
516     ULONG refcount = InterlockedIncrement(&This->refcount);
517
518     TRACE("%p increasing refcount to %u.\n", This, refcount);
519
520     return refcount;
521 }
522
523 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_Release(ID3D10ShaderResourceView *iface)
524 {
525     struct d3d10_shader_resource_view *This = impl_from_ID3D10ShaderResourceView(iface);
526     ULONG refcount = InterlockedDecrement(&This->refcount);
527
528     TRACE("%p decreasing refcount to %u.\n", This, refcount);
529
530     if (!refcount)
531     {
532         HeapFree(GetProcessHeap(), 0, This);
533     }
534
535     return refcount;
536 }
537
538 /* ID3D10DeviceChild methods */
539
540 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDevice(ID3D10ShaderResourceView *iface,
541         ID3D10Device **device)
542 {
543     FIXME("iface %p, device %p stub!\n", iface, device);
544 }
545
546 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_GetPrivateData(ID3D10ShaderResourceView *iface,
547         REFGUID guid, UINT *data_size, void *data)
548 {
549     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
550             iface, debugstr_guid(guid), data_size, data);
551
552     return E_NOTIMPL;
553 }
554
555 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateData(ID3D10ShaderResourceView *iface,
556         REFGUID guid, UINT data_size, const void *data)
557 {
558     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
559             iface, debugstr_guid(guid), data_size, data);
560
561     return E_NOTIMPL;
562 }
563
564 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateDataInterface(ID3D10ShaderResourceView *iface,
565         REFGUID guid, const IUnknown *data)
566 {
567     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
568
569     return E_NOTIMPL;
570 }
571
572 /* ID3D10View methods */
573
574 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetResource(ID3D10ShaderResourceView *iface,
575         ID3D10Resource **resource)
576 {
577     FIXME("iface %p, resource %p stub!\n", iface, resource);
578 }
579
580 /* ID3D10ShaderResourceView methods */
581
582 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc(ID3D10ShaderResourceView *iface,
583         D3D10_SHADER_RESOURCE_VIEW_DESC *desc)
584 {
585     FIXME("iface %p, desc %p stub!\n", iface, desc);
586 }
587
588 static const struct ID3D10ShaderResourceViewVtbl d3d10_shader_resource_view_vtbl =
589 {
590     /* IUnknown methods */
591     d3d10_shader_resource_view_QueryInterface,
592     d3d10_shader_resource_view_AddRef,
593     d3d10_shader_resource_view_Release,
594     /* ID3D10DeviceChild methods */
595     d3d10_shader_resource_view_GetDevice,
596     d3d10_shader_resource_view_GetPrivateData,
597     d3d10_shader_resource_view_SetPrivateData,
598     d3d10_shader_resource_view_SetPrivateDataInterface,
599     /* ID3D10View methods */
600     d3d10_shader_resource_view_GetResource,
601     /* ID3D10ShaderResourceView methods */
602     d3d10_shader_resource_view_GetDesc,
603 };
604
605 HRESULT d3d10_shader_resource_view_init(struct d3d10_shader_resource_view *view)
606 {
607     view->ID3D10ShaderResourceView_iface.lpVtbl = &d3d10_shader_resource_view_vtbl;
608     view->refcount = 1;
609
610     return S_OK;
611 }