usp10: Correct lookahead offset in Coverage-based Chaining Context Glyph Substitution.
[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_surface_get_resource(((struct d3d10_texture2d *)resource)->wined3d_surface);
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 /* IUnknown methods */
168
169 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_QueryInterface(ID3D10DepthStencilView *iface,
170         REFIID riid, void **object)
171 {
172     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
173
174     if (IsEqualGUID(riid, &IID_ID3D10DepthStencilView)
175             || IsEqualGUID(riid, &IID_ID3D10View)
176             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
177             || IsEqualGUID(riid, &IID_IUnknown))
178     {
179         IUnknown_AddRef(iface);
180         *object = iface;
181         return S_OK;
182     }
183
184     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
185
186     *object = NULL;
187     return E_NOINTERFACE;
188 }
189
190 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_AddRef(ID3D10DepthStencilView *iface)
191 {
192     struct d3d10_depthstencil_view *This = (struct d3d10_depthstencil_view *)iface;
193     ULONG refcount = InterlockedIncrement(&This->refcount);
194
195     TRACE("%p increasing refcount to %u.\n", This, refcount);
196
197     return refcount;
198 }
199
200 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_Release(ID3D10DepthStencilView *iface)
201 {
202     struct d3d10_depthstencil_view *This = (struct d3d10_depthstencil_view *)iface;
203     ULONG refcount = InterlockedDecrement(&This->refcount);
204
205     TRACE("%p decreasing refcount to %u.\n", This, refcount);
206
207     if (!refcount)
208     {
209         HeapFree(GetProcessHeap(), 0, This);
210     }
211
212     return refcount;
213 }
214
215 /* ID3D10DeviceChild methods */
216
217 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDevice(ID3D10DepthStencilView *iface, ID3D10Device **device)
218 {
219     FIXME("iface %p, device %p stub!\n", iface, device);
220 }
221
222 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_GetPrivateData(ID3D10DepthStencilView *iface,
223         REFGUID guid, UINT *data_size, void *data)
224 {
225     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
226             iface, debugstr_guid(guid), data_size, data);
227
228     return E_NOTIMPL;
229 }
230
231 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateData(ID3D10DepthStencilView *iface,
232         REFGUID guid, UINT data_size, const void *data)
233 {
234     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
235             iface, debugstr_guid(guid), data_size, data);
236
237     return E_NOTIMPL;
238 }
239
240 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateDataInterface(ID3D10DepthStencilView *iface,
241         REFGUID guid, const IUnknown *data)
242 {
243     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
244
245     return E_NOTIMPL;
246 }
247
248 /* ID3D10View methods */
249
250 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetResource(ID3D10DepthStencilView *iface,
251         ID3D10Resource **resource)
252 {
253     FIXME("iface %p, resource %p stub!\n", iface, resource);
254 }
255
256 /* ID3D10DepthStencilView methods */
257
258 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDesc(ID3D10DepthStencilView *iface,
259         D3D10_DEPTH_STENCIL_VIEW_DESC *desc)
260 {
261     FIXME("iface %p, desc %p stub!\n", iface, desc);
262 }
263
264 static const struct ID3D10DepthStencilViewVtbl d3d10_depthstencil_view_vtbl =
265 {
266     /* IUnknown methods */
267     d3d10_depthstencil_view_QueryInterface,
268     d3d10_depthstencil_view_AddRef,
269     d3d10_depthstencil_view_Release,
270     /* ID3D10DeviceChild methods */
271     d3d10_depthstencil_view_GetDevice,
272     d3d10_depthstencil_view_GetPrivateData,
273     d3d10_depthstencil_view_SetPrivateData,
274     d3d10_depthstencil_view_SetPrivateDataInterface,
275     /* ID3D10View methods */
276     d3d10_depthstencil_view_GetResource,
277     /* ID3D10DepthStencilView methods */
278     d3d10_depthstencil_view_GetDesc,
279 };
280
281 HRESULT d3d10_depthstencil_view_init(struct d3d10_depthstencil_view *view)
282 {
283     view->vtbl = &d3d10_depthstencil_view_vtbl;
284     view->refcount = 1;
285
286     return S_OK;
287 }
288
289 /* IUnknown methods */
290
291 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_QueryInterface(ID3D10RenderTargetView *iface,
292         REFIID riid, void **object)
293 {
294     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
295
296     if (IsEqualGUID(riid, &IID_ID3D10RenderTargetView)
297             || IsEqualGUID(riid, &IID_ID3D10View)
298             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
299             || IsEqualGUID(riid, &IID_IUnknown))
300     {
301         IUnknown_AddRef(iface);
302         *object = iface;
303         return S_OK;
304     }
305
306     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
307
308     *object = NULL;
309     return E_NOINTERFACE;
310 }
311
312 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_AddRef(ID3D10RenderTargetView *iface)
313 {
314     struct d3d10_rendertarget_view *This = (struct d3d10_rendertarget_view *)iface;
315     ULONG refcount = InterlockedIncrement(&This->refcount);
316
317     TRACE("%p increasing refcount to %u\n", This, refcount);
318
319     return refcount;
320 }
321
322 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_Release(ID3D10RenderTargetView *iface)
323 {
324     struct d3d10_rendertarget_view *This = (struct d3d10_rendertarget_view *)iface;
325     ULONG refcount = InterlockedDecrement(&This->refcount);
326
327     TRACE("%p decreasing refcount to %u\n", This, refcount);
328
329     if (!refcount)
330     {
331         wined3d_rendertarget_view_decref(This->wined3d_view);
332         HeapFree(GetProcessHeap(), 0, This);
333     }
334
335     return refcount;
336 }
337
338 /* ID3D10DeviceChild methods */
339
340 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDevice(ID3D10RenderTargetView *iface, ID3D10Device **device)
341 {
342     FIXME("iface %p, device %p stub!\n", iface, device);
343 }
344
345 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_GetPrivateData(ID3D10RenderTargetView *iface,
346         REFGUID guid, UINT *data_size, void *data)
347 {
348     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
349             iface, debugstr_guid(guid), data_size, data);
350
351     return E_NOTIMPL;
352 }
353
354 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateData(ID3D10RenderTargetView *iface,
355         REFGUID guid, UINT data_size, const void *data)
356 {
357     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
358             iface, debugstr_guid(guid), data_size, data);
359
360     return E_NOTIMPL;
361 }
362
363 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateDataInterface(ID3D10RenderTargetView *iface,
364         REFGUID guid, const IUnknown *data)
365 {
366     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
367
368     return E_NOTIMPL;
369 }
370
371 /* ID3D10View methods */
372
373 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetResource(ID3D10RenderTargetView *iface,
374         ID3D10Resource **resource)
375 {
376     struct d3d10_rendertarget_view *This = (struct d3d10_rendertarget_view *)iface;
377     struct wined3d_resource *wined3d_resource;
378     IUnknown *parent;
379     HRESULT hr;
380
381     TRACE("iface %p, resource %p\n", iface, resource);
382
383     wined3d_resource = wined3d_rendertarget_view_get_resource(This->wined3d_view);
384     if (!wined3d_resource)
385     {
386         ERR("Failed to get wined3d resource.\n");
387         *resource = NULL;
388         return;
389     }
390
391     parent = wined3d_resource_get_parent(wined3d_resource);
392     hr = IUnknown_QueryInterface(parent, &IID_ID3D10Resource, (void **)&resource);
393     if (FAILED(hr))
394     {
395         ERR("Resource parent isn't a d3d10 resource, hr %#x\n", hr);
396         *resource = NULL;
397         return;
398     }
399 }
400
401 /* ID3D10RenderTargetView methods */
402
403 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDesc(ID3D10RenderTargetView *iface,
404         D3D10_RENDER_TARGET_VIEW_DESC *desc)
405 {
406     struct d3d10_rendertarget_view *This = (struct d3d10_rendertarget_view *)iface;
407
408     TRACE("iface %p, desc %p\n", iface, desc);
409
410     *desc = This->desc;
411 }
412
413 static const struct ID3D10RenderTargetViewVtbl d3d10_rendertarget_view_vtbl =
414 {
415     /* IUnknown methods */
416     d3d10_rendertarget_view_QueryInterface,
417     d3d10_rendertarget_view_AddRef,
418     d3d10_rendertarget_view_Release,
419     /* ID3D10DeviceChild methods */
420     d3d10_rendertarget_view_GetDevice,
421     d3d10_rendertarget_view_GetPrivateData,
422     d3d10_rendertarget_view_SetPrivateData,
423     d3d10_rendertarget_view_SetPrivateDataInterface,
424     /* ID3D10View methods */
425     d3d10_rendertarget_view_GetResource,
426     /* ID3D10RenderTargetView methods */
427     d3d10_rendertarget_view_GetDesc,
428 };
429
430 HRESULT d3d10_rendertarget_view_init(struct d3d10_rendertarget_view *view,
431         ID3D10Resource *resource, const D3D10_RENDER_TARGET_VIEW_DESC *desc)
432 {
433     struct wined3d_resource *wined3d_resource;
434     HRESULT hr;
435
436     view->vtbl = &d3d10_rendertarget_view_vtbl;
437     view->refcount = 1;
438
439     if (!desc)
440     {
441         HRESULT hr = set_rtdesc_from_resource(&view->desc, resource);
442         if (FAILED(hr)) return hr;
443     }
444     else
445     {
446         view->desc = *desc;
447     }
448
449     wined3d_resource = wined3d_resource_from_resource(resource);
450     if (!wined3d_resource)
451     {
452         ERR("Failed to get wined3d resource for d3d10 resource %p.\n", resource);
453         return E_FAIL;
454     }
455
456     hr = wined3d_rendertarget_view_create(wined3d_resource, view, &view->wined3d_view);
457     if (FAILED(hr))
458     {
459         WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
460         return hr;
461     }
462
463     return S_OK;
464 }
465
466 /* IUnknown methods */
467
468 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_QueryInterface(ID3D10ShaderResourceView *iface,
469         REFIID riid, void **object)
470 {
471     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
472
473     if (IsEqualGUID(riid, &IID_ID3D10ShaderResourceView)
474             || IsEqualGUID(riid, &IID_ID3D10View)
475             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
476             || IsEqualGUID(riid, &IID_IUnknown))
477     {
478         IUnknown_AddRef(iface);
479         *object = iface;
480         return S_OK;
481     }
482
483     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
484
485     *object = NULL;
486     return E_NOINTERFACE;
487 }
488
489 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_AddRef(ID3D10ShaderResourceView *iface)
490 {
491     struct d3d10_shader_resource_view *This = (struct d3d10_shader_resource_view *)iface;
492     ULONG refcount = InterlockedIncrement(&This->refcount);
493
494     TRACE("%p increasing refcount to %u.\n", This, refcount);
495
496     return refcount;
497 }
498
499 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_Release(ID3D10ShaderResourceView *iface)
500 {
501     struct d3d10_shader_resource_view *This = (struct d3d10_shader_resource_view *)iface;
502     ULONG refcount = InterlockedDecrement(&This->refcount);
503
504     TRACE("%p decreasing refcount to %u.\n", This, refcount);
505
506     if (!refcount)
507     {
508         HeapFree(GetProcessHeap(), 0, This);
509     }
510
511     return refcount;
512 }
513
514 /* ID3D10DeviceChild methods */
515
516 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDevice(ID3D10ShaderResourceView *iface,
517         ID3D10Device **device)
518 {
519     FIXME("iface %p, device %p stub!\n", iface, device);
520 }
521
522 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_GetPrivateData(ID3D10ShaderResourceView *iface,
523         REFGUID guid, UINT *data_size, void *data)
524 {
525     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
526             iface, debugstr_guid(guid), data_size, data);
527
528     return E_NOTIMPL;
529 }
530
531 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateData(ID3D10ShaderResourceView *iface,
532         REFGUID guid, UINT data_size, const void *data)
533 {
534     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
535             iface, debugstr_guid(guid), data_size, data);
536
537     return E_NOTIMPL;
538 }
539
540 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateDataInterface(ID3D10ShaderResourceView *iface,
541         REFGUID guid, const IUnknown *data)
542 {
543     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
544
545     return E_NOTIMPL;
546 }
547
548 /* ID3D10View methods */
549
550 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetResource(ID3D10ShaderResourceView *iface,
551         ID3D10Resource **resource)
552 {
553     FIXME("iface %p, resource %p stub!\n", iface, resource);
554 }
555
556 /* ID3D10ShaderResourceView methods */
557
558 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc(ID3D10ShaderResourceView *iface,
559         D3D10_SHADER_RESOURCE_VIEW_DESC *desc)
560 {
561     FIXME("iface %p, desc %p stub!\n", iface, desc);
562 }
563
564 static const struct ID3D10ShaderResourceViewVtbl d3d10_shader_resource_view_vtbl =
565 {
566     /* IUnknown methods */
567     d3d10_shader_resource_view_QueryInterface,
568     d3d10_shader_resource_view_AddRef,
569     d3d10_shader_resource_view_Release,
570     /* ID3D10DeviceChild methods */
571     d3d10_shader_resource_view_GetDevice,
572     d3d10_shader_resource_view_GetPrivateData,
573     d3d10_shader_resource_view_SetPrivateData,
574     d3d10_shader_resource_view_SetPrivateDataInterface,
575     /* ID3D10View methods */
576     d3d10_shader_resource_view_GetResource,
577     /* ID3D10ShaderResourceView methods */
578     d3d10_shader_resource_view_GetDesc,
579 };
580
581 HRESULT d3d10_shader_resource_view_init(struct d3d10_shader_resource_view *view)
582 {
583     view->vtbl = &d3d10_shader_resource_view_vtbl;
584     view->refcount = 1;
585
586     return S_OK;
587 }