Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / state_trackers / d3d1x / gd3d11 / d3d11_objects.h
1 /**************************************************************************
2  *
3  * Copyright 2010 Luca Barbieri
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 template<typename Base = ID3D11DeviceChild>
28 struct GalliumD3D11DeviceChild : public GalliumPrivateDataComObject<Base, dual_refcnt_t>
29 {
30         GalliumD3D11Screen* device; // must not be null
31
32
33         // if this is called, the subclass constructor must set device itself
34         GalliumD3D11DeviceChild()
35         : device(0)
36         {}
37
38         GalliumD3D11DeviceChild(GalliumD3D11Screen* p_device)
39         {
40                 // we store the reference count minus one in refcnt
41                 device = p_device;
42                 device->AddRef();
43         }
44
45         /* The purpose of this is to avoid cyclic garbage, since this won't hold
46          * a pointer to the device if it is only held by a pipeline binding in the immediate context
47          *
48          * TODO: we could only manipulate the device refcnt when atomic_refcnt == 0 changes,
49          * but this requires more complex atomic ops
50          */
51         inline ULONG add_ref()
52         {
53                 device->AddRef();
54                 return GalliumPrivateDataComObject<Base, dual_refcnt_t>::add_ref();
55         }
56
57         inline ULONG release()
58         {
59                 device->Release();
60                 return GalliumPrivateDataComObject<Base, dual_refcnt_t>::release();
61         }
62
63         virtual ULONG STDMETHODCALLTYPE AddRef()
64         {
65                 return add_ref();
66         }
67
68         virtual ULONG STDMETHODCALLTYPE Release()
69         {
70                 return release();
71         }
72
73         virtual void STDMETHODCALLTYPE GetDevice(
74                 ID3D11Device **out_device
75          )
76         {
77                 device->AddRef();
78                 *out_device = device;
79         }
80 };
81
82 template<typename Base = ID3D11DeviceChild, typename Object = void>
83 struct GalliumD3D11Object : public GalliumD3D11DeviceChild<Base>
84 {
85         Object* object;
86         GalliumD3D11Object(GalliumD3D11Screen* device, Object* object)
87         : GalliumD3D11DeviceChild<Base>(device), object(object)
88         {}
89
90         virtual ~GalliumD3D11Object();
91 };
92
93 #define IMPLEMENT_OBJECT_DTOR(name, gallium) \
94 template<> \
95 GalliumD3D11Object<ID3D11##name, void>::~GalliumD3D11Object() \
96 { \
97         DX10_ONLY(device->Unbind##name(this)); \
98         device->immediate_pipe->delete_##gallium##_state(device->immediate_pipe, object); \
99 }
100
101 #define IMPLEMENT_VIEW_DTOR(name, gallium) \
102 template<> \
103 GalliumD3D11Object<ID3D11##name, struct pipe_##gallium>::~GalliumD3D11Object() \
104 { \
105         DX10_ONLY(device->Unbind##name(this)); \
106         pipe_##gallium##_reference(&object, 0); \
107 }
108
109 IMPLEMENT_OBJECT_DTOR(InputLayout, vertex_elements)
110 IMPLEMENT_OBJECT_DTOR(DepthStencilState, depth_stencil_alpha)
111 IMPLEMENT_OBJECT_DTOR(RasterizerState, rasterizer)
112 IMPLEMENT_OBJECT_DTOR(SamplerState, sampler)
113 IMPLEMENT_OBJECT_DTOR(BlendState, blend)
114 IMPLEMENT_OBJECT_DTOR(VertexShader, vs)
115 IMPLEMENT_OBJECT_DTOR(PixelShader, fs)
116 IMPLEMENT_OBJECT_DTOR(GeometryShader, gs)
117
118 IMPLEMENT_VIEW_DTOR(ShaderResourceView, sampler_view)
119 IMPLEMENT_VIEW_DTOR(RenderTargetView, surface)
120 IMPLEMENT_VIEW_DTOR(DepthStencilView, surface)
121
122 #if API >= 11
123 // IMPLEMENT_VIEW_DTOR(UnorderedAccessView, surface);
124 // IMPLEMENT_OBJECT_DTOR(HullShader, tcs);
125 // IMPLEMENT_OBJECT_DTOR(DomainShader, tes);
126 // IMPLEMENT_OBJECT_DTOR(ComputeShader, cs);
127 #else
128 IMPLEMENT_OBJECT_DTOR(BlendState1, blend)
129 IMPLEMENT_VIEW_DTOR(ShaderResourceView1, sampler_view)
130 #endif
131
132 template<typename Base, typename Desc, typename Object = void>
133 struct GalliumD3D11DescribedObject : public GalliumD3D11Object<Base, Object>
134 {
135         Desc desc;
136         GalliumD3D11DescribedObject(GalliumD3D11Screen* device, Object* object, const Desc& desc)
137         : GalliumD3D11Object<Base, Object>(device, object), desc(desc)
138         {}
139
140         virtual void STDMETHODCALLTYPE GetDesc(Desc *out_desc)
141         {
142                 memcpy(out_desc, &desc, sizeof(desc));
143         }
144 };
145
146 typedef GalliumD3D11Object<ID3D11InputLayout> GalliumD3D11InputLayout;
147 typedef GalliumD3D11DescribedObject<ID3D11DepthStencilState, D3D11_DEPTH_STENCIL_DESC> GalliumD3D11DepthStencilState;
148 typedef GalliumD3D11DescribedObject<ID3D11RasterizerState, D3D11_RASTERIZER_DESC> GalliumD3D11RasterizerStateBase;
149 typedef GalliumD3D11DescribedObject<ID3D11SamplerState, D3D11_SAMPLER_DESC> GalliumD3D11SamplerState;
150
151 #if API >= 11
152 typedef GalliumD3D11DescribedObject<ID3D11BlendState, D3D11_BLEND_DESC> GalliumD3D11BlendState;
153 #else
154 typedef GalliumD3D10DescribedObject<ID3D10BlendState1, D3D10_BLEND_DESC> GalliumD3D10BlendStateBase;
155
156 struct GalliumD3D10BlendState : public GalliumD3D10BlendStateBase
157 {
158         static D3D10_BLEND_DESC convert_to_d3d10(const D3D10_BLEND_DESC1& desc1)
159         {
160                 D3D10_BLEND_DESC desc;
161                 desc.AlphaToCoverageEnable = desc1.AlphaToCoverageEnable;
162                 desc.SrcBlend = desc1.RenderTarget[0].SrcBlend;
163                 desc.DestBlend = desc1.RenderTarget[0].DestBlend;
164                 desc.BlendOp = desc1.RenderTarget[0].BlendOp;
165                 desc.SrcBlendAlpha = desc1.RenderTarget[0].SrcBlendAlpha;
166                 desc.DestBlendAlpha = desc1.RenderTarget[0].DestBlendAlpha;
167                 desc.BlendOpAlpha = desc1.RenderTarget[0].BlendOpAlpha;
168                 for(unsigned i = 0; i < 8; ++i)
169                 {
170                         desc.BlendEnable[i] = desc1.RenderTarget[i].BlendEnable;
171                         desc.RenderTargetWriteMask[i] = desc1.RenderTarget[i].RenderTargetWriteMask;
172                 }
173                 return desc;
174         }
175
176         D3D10_BLEND_DESC1 desc1;
177
178         GalliumD3D10BlendState(GalliumD3D10Screen* device, void* object, const D3D10_BLEND_DESC& desc)
179         : GalliumD3D10BlendStateBase(device, object, desc)
180         {
181                 memset(&desc1, 0, sizeof(desc1));
182                 desc1.AlphaToCoverageEnable = desc.AlphaToCoverageEnable;
183                 desc1.RenderTarget[0].SrcBlend = desc.SrcBlend;
184                 desc1.RenderTarget[0].DestBlend = desc.DestBlend;
185                 desc1.RenderTarget[0].BlendOp = desc.BlendOp;
186                 desc1.RenderTarget[0].SrcBlendAlpha = desc.SrcBlendAlpha;
187                 desc1.RenderTarget[0].DestBlendAlpha = desc.DestBlendAlpha;
188                 desc1.RenderTarget[0].BlendOpAlpha = desc.BlendOpAlpha;
189                 for(unsigned i = 0; i < 8; ++i)
190                 {
191                         desc1.RenderTarget[i].BlendEnable = desc.BlendEnable[i];
192                         desc1.RenderTarget[i].RenderTargetWriteMask = desc.RenderTargetWriteMask[i];
193                 }
194         }
195
196         GalliumD3D10BlendState(GalliumD3D10Screen* device, void* object, const D3D10_BLEND_DESC1& desc)
197         : GalliumD3D10BlendStateBase(device, object, convert_to_d3d10(desc)), desc1(desc1)
198         {}
199
200         virtual void STDMETHODCALLTYPE GetDesc1(D3D10_BLEND_DESC1 *out_desc)
201         {
202                 memcpy(out_desc, &desc1, sizeof(desc1));
203         }
204 };
205 #endif
206
207 struct GalliumD3D11RasterizerState : public GalliumD3D11RasterizerStateBase
208 {
209         bool depth_clamp;
210
211         GalliumD3D11RasterizerState(GalliumD3D11Screen* device, void* object, const D3D11_RASTERIZER_DESC& desc, bool depth_clamp)
212         : GalliumD3D11RasterizerStateBase(device, object, desc), depth_clamp(depth_clamp)
213         {}
214 };
215
216 template<typename Base = ID3D11DeviceChild>
217 struct GalliumD3D11Shader : public GalliumD3D11Object<Base>
218 {
219         std::vector<int> slot_to_resource;
220         std::vector<int> slot_to_sampler;
221
222         GalliumD3D11Shader(GalliumD3D11Screen* device, void* object)
223         : GalliumD3D11Object<Base>(device, object)
224         {}
225 };
226
227 typedef GalliumD3D11Shader<ID3D11VertexShader> GalliumD3D11VertexShader;
228 typedef GalliumD3D11Shader<ID3D11GeometryShader> GalliumD3D11GeometryShader;
229 typedef GalliumD3D11Shader<ID3D11PixelShader> GalliumD3D11PixelShader;
230
231 #if API >= 11
232 /*
233 typedef GalliumD3D11Shader<ID3D11HullShader> GalliumD3D11HullShader;
234 typedef GalliumD3D11Shader<ID3D11DomainShader> GalliumD3D11DomainShader;
235 typedef GalliumD3D11Shader<ID3D11ComputeShader> GalliumD3D11ComputeShader;
236 */
237 #endif
238
239 template<typename Base = ID3D11Resource>
240 struct GalliumD3D11ResourceBase : public GalliumD3D11DeviceChild<Base>
241 {
242         unsigned eviction_priority;
243
244         virtual void STDMETHODCALLTYPE SetEvictionPriority(
245                 unsigned new_eviction_priority
246         )
247         {
248                 eviction_priority = new_eviction_priority;
249         }
250
251         virtual unsigned STDMETHODCALLTYPE GetEvictionPriority()
252         {
253                 return eviction_priority;
254         }
255 };
256
257 template<typename Real>
258 struct GalliumDXGIResource : public IDXGIResource
259 {
260         virtual HRESULT STDMETHODCALLTYPE SetEvictionPriority(
261                 unsigned new_eviction_priority
262         )
263         {
264                 static_cast<Real*>(this)->eviction_priority = new_eviction_priority;
265                 return S_OK;
266         }
267
268         virtual HRESULT STDMETHODCALLTYPE GetEvictionPriority(unsigned* out_eviction_priority)
269         {
270                 *out_eviction_priority = static_cast<Real*>(this)->eviction_priority;
271                 return S_OK;
272         }
273
274         virtual HRESULT STDMETHODCALLTYPE GetDevice(
275                 REFIID riid,
276                 void **out_parent)
277         {
278                 if(!static_cast<Real*>(this)->device)
279                         return E_NOINTERFACE;
280                 return static_cast<Real*>(this)->device->QueryInterface(riid, out_parent);
281         }
282
283         virtual HRESULT STDMETHODCALLTYPE GetParent(
284                 REFIID riid,
285                 void **out_parent)
286         {
287                 if(!static_cast<Real*>(this)->device)
288                         return E_NOINTERFACE;
289                 return static_cast<Real*>(this)->device->QueryInterface(riid, out_parent);
290         }
291 };
292
293 template<typename T>
294 struct com_traits<GalliumDXGIResource<T> > : public com_traits<IDXGIResource>
295 {};
296
297 template<typename Base = ID3D11Resource>
298 struct GalliumD3D11Resource
299         : public GalliumMultiComObject<
300                 GalliumMultiPrivateDataComObject<
301                         GalliumD3D11ResourceBase<Base>,
302                         GalliumDXGIResource<GalliumD3D11Resource<Base> >
303                 >,
304                 IGalliumResource
305         >
306 {
307         struct pipe_resource* resource;
308         std::unordered_map<unsigned, pipe_transfer*> transfers;
309         float min_lod;
310         DXGI_USAGE dxgi_usage;
311
312         GalliumD3D11Resource(GalliumD3D11Screen* device = 0, struct pipe_resource* resource = 0, unsigned dxgi_usage = 0)
313         : resource(resource), min_lod(0), dxgi_usage(dxgi_usage)
314         {
315                 this->device = device;
316                 if(device)
317                         device->AddRef();
318                 this->eviction_priority = 0;
319         }
320
321         ~GalliumD3D11Resource()
322         {
323                 pipe_resource_reference(&resource, 0);
324         }
325
326         virtual HRESULT STDMETHODCALLTYPE GetUsage(
327                 DXGI_USAGE *out_usage
328          )
329         {
330                 *out_usage = this->dxgi_usage;
331                 return S_OK;
332         }
333
334         virtual HRESULT STDMETHODCALLTYPE GetSharedHandle(HANDLE *out_shared_handle)
335         {
336                 return E_NOTIMPL;
337         }
338
339         virtual struct pipe_resource* STDMETHODCALLTYPE GetGalliumResource()
340         {
341                 return resource;
342         }
343 };
344
345 template<typename Base, typename Desc, D3D11_RESOURCE_DIMENSION Dim>
346 struct GalliumD3D11TypedResource : public GalliumD3D11Resource<Base>
347 {
348         Desc desc;
349         GalliumD3D11TypedResource() {}
350         GalliumD3D11TypedResource(GalliumD3D11Screen* device, struct pipe_resource* resource, const Desc& desc, unsigned dxgi_usage)
351         : GalliumD3D11Resource<Base>(device, resource, dxgi_usage), desc(desc)
352         {}
353         virtual void STDMETHODCALLTYPE GetType(
354                 D3D11_RESOURCE_DIMENSION *out_resource_dimension)
355         {
356                 *out_resource_dimension = Dim;
357         }
358         virtual void STDMETHODCALLTYPE GetDesc(Desc *out_desc)
359         {
360                 memcpy(out_desc, &desc, sizeof(desc));
361         }
362 };
363
364 typedef GalliumD3D11TypedResource<ID3D11Texture1D, D3D11_TEXTURE1D_DESC, D3D11_RESOURCE_DIMENSION_TEXTURE1D> GalliumD3D11Texture1DBase;
365 typedef GalliumD3D11TypedResource<ID3D11Texture2D, D3D11_TEXTURE2D_DESC, D3D11_RESOURCE_DIMENSION_TEXTURE2D> GalliumD3D11Texture2DBase;
366 typedef GalliumD3D11TypedResource<ID3D11Texture3D, D3D11_TEXTURE3D_DESC, D3D11_RESOURCE_DIMENSION_TEXTURE3D> GalliumD3D11Texture3DBase;
367 typedef GalliumD3D11TypedResource<ID3D11Buffer, D3D11_BUFFER_DESC, D3D11_RESOURCE_DIMENSION_BUFFER> GalliumD3D11BufferBase;
368
369 #if API >= 11
370 typedef GalliumD3D11BufferBase GalliumD3D11Buffer;
371 typedef GalliumD3D11Texture1DBase GalliumD3D11Texture1D;
372 typedef GalliumD3D11Texture2DBase GalliumD3D11Texture2D;
373 typedef GalliumD3D11Texture3DBase GalliumD3D11Texture3D;
374 #else
375 struct GalliumD3D10Buffer : public GalliumD3D10BufferBase
376 {
377         GalliumD3D10Buffer(GalliumD3D10Screen* device, struct pipe_resource* resource, const D3D10_BUFFER_DESC& desc, unsigned dxgi_usage)
378         : GalliumD3D10BufferBase(device, resource, desc, dxgi_usage)
379         {}
380
381         ~GalliumD3D10Buffer()
382         {
383                 device->UnbindBuffer(this);
384         }
385
386         virtual HRESULT STDMETHODCALLTYPE Map(
387                 D3D10_MAP map_type,
388                 unsigned map_flags,
389                 void **out_data)
390         {
391                 D3D10_MAPPED_SUBRESOURCE msr;
392                 HRESULT hr = device->Map(this, 0, map_type, map_flags, &msr);
393                 if(!SUCCEEDED(hr))
394                         return hr;
395                 *out_data = msr.pData;
396                 return S_OK;
397         }
398
399         virtual void STDMETHODCALLTYPE Unmap()
400         {
401                 device->Unmap(this, 0);
402         }
403 };
404
405 struct GalliumD3D10Texture1D : public GalliumD3D10Texture1DBase
406 {
407         GalliumD3D10Texture1D(GalliumD3D10Screen* device, struct pipe_resource* resource, const D3D10_TEXTURE1D_DESC& desc, unsigned dxgi_usage)
408         : GalliumD3D10Texture1DBase(device, resource, desc, dxgi_usage)
409         {}
410
411         virtual HRESULT STDMETHODCALLTYPE Map(
412                 unsigned subresource,
413                 D3D10_MAP map_type,
414                 unsigned map_flags,
415                 void **out_data)
416         {
417                 D3D10_MAPPED_SUBRESOURCE msr;
418                 HRESULT hr = device->Map(this, subresource, map_type, map_flags, &msr);
419                 if(!SUCCEEDED(hr))
420                         return hr;
421                 *out_data = msr.pData;
422                 return S_OK;
423         }
424
425         virtual void STDMETHODCALLTYPE Unmap(
426                 unsigned subresource
427         )
428         {
429                 device->Unmap(this, subresource);
430         }
431 };
432
433 struct GalliumD3D10Texture2D : public GalliumD3D10Texture2DBase
434 {
435         GalliumD3D10Texture2D() {}
436         GalliumD3D10Texture2D(GalliumD3D10Screen* device, struct pipe_resource* resource, const D3D10_TEXTURE2D_DESC& desc, unsigned dxgi_usage)
437         : GalliumD3D10Texture2DBase(device, resource, desc, dxgi_usage)
438         {}
439
440         virtual HRESULT STDMETHODCALLTYPE Map(
441                 unsigned subresource,
442                 D3D10_MAP map_type,
443                 unsigned map_flags,
444                 D3D10_MAPPED_TEXTURE2D *out_mapped_subresource)
445         {
446                 D3D10_MAPPED_SUBRESOURCE msr;
447                 HRESULT hr = device->Map(this, subresource, map_type, map_flags, &msr);
448                 if(!SUCCEEDED(hr))
449                         return hr;
450                 out_mapped_subresource->pData = msr.pData;
451                 out_mapped_subresource->RowPitch = msr.RowPitch;
452                 return S_OK;
453         }
454
455         virtual void STDMETHODCALLTYPE Unmap(
456                 unsigned subresource
457         )
458         {
459                 device->Unmap(this, subresource);
460         }
461 };
462
463
464 struct GalliumD3D10Texture3D : public GalliumD3D10Texture3DBase
465 {
466         GalliumD3D10Texture3D(GalliumD3D10Screen* device, struct pipe_resource* resource, const D3D10_TEXTURE3D_DESC& desc, unsigned dxgi_usage)
467         : GalliumD3D10Texture3DBase(device, resource, desc, dxgi_usage)
468         {}
469
470         virtual HRESULT STDMETHODCALLTYPE Map(
471                 unsigned subresource,
472                 D3D10_MAP map_type,
473                 unsigned map_flags,
474                 D3D10_MAPPED_TEXTURE3D *out_mapped_subresource)
475         {
476                 D3D10_MAPPED_SUBRESOURCE msr;
477                 HRESULT hr = device->Map(this, subresource, map_type, map_flags, &msr);
478                 if(!SUCCEEDED(hr))
479                         return hr;
480                 out_mapped_subresource->pData = msr.pData;
481                 out_mapped_subresource->RowPitch = msr.RowPitch;
482                 out_mapped_subresource->DepthPitch = msr.DepthPitch;
483                 return S_OK;
484         }
485
486         virtual void STDMETHODCALLTYPE Unmap(
487                 unsigned subresource
488         )
489         {
490                 device->Unmap(this, subresource);
491         }
492 };
493 #endif
494
495 struct GalliumD3D11Surface : public GalliumMultiPrivateDataComObject<GalliumD3D11Texture2D, IDXGISurface1>
496 {
497         GalliumD3D11Surface(GalliumD3D11Screen* device, struct pipe_resource* resource, const D3D11_TEXTURE2D_DESC& desc, unsigned dxgi_usage)
498         {
499                 this->device = device;
500                 this->device->AddRef();
501                 this->resource = resource;
502                 this->desc = desc;
503                 this->dxgi_usage = dxgi_usage;
504         }
505
506         virtual HRESULT STDMETHODCALLTYPE GetDesc(
507                 DXGI_SURFACE_DESC *out_desc)
508         {
509                 out_desc->Format = this->desc.Format;
510                 out_desc->Width = this->desc.Width;
511                 out_desc->Height = this->desc.Height;
512                 out_desc->SampleDesc = this->desc.SampleDesc;
513                 return S_OK;
514         }
515
516         virtual HRESULT STDMETHODCALLTYPE GetParent(
517                 REFIID riid,
518                 void **out_parent)
519         {
520                 if(!device)
521                         return E_NOINTERFACE;
522                 return device->QueryInterface(riid, out_parent);
523         }
524
525         /* TODO: somehow implement these */
526         virtual HRESULT STDMETHODCALLTYPE GetDC(
527                 BOOL discard,
528                 HDC *out_hdc)
529         {
530                 *out_hdc = 0;
531                 return E_NOTIMPL;
532         }
533
534         virtual HRESULT STDMETHODCALLTYPE ReleaseDC(
535                 RECT *out_dirty_rect)
536         {
537                 return E_NOTIMPL;
538         }
539
540         virtual HRESULT STDMETHODCALLTYPE Map(
541                 DXGI_MAPPED_RECT *out_locked_rect,
542                 unsigned map_flags)
543         {
544                 D3D11_MAP d3d_map;
545                 if(map_flags & DXGI_MAP_DISCARD)
546                         d3d_map = D3D11_MAP_WRITE_DISCARD;
547                 else
548                 {
549                         if(map_flags & DXGI_MAP_READ)
550                         {
551                                 if(map_flags & DXGI_MAP_WRITE)
552                                         d3d_map = D3D11_MAP_READ_WRITE;
553                                 else
554                                         d3d_map = D3D11_MAP_READ;
555                         }
556                         else
557                                 d3d_map = D3D11_MAP_WRITE;
558                 }
559                 D3D11_MAPPED_SUBRESOURCE d3d_mapped;
560                 HRESULT hres = this->device->get_immediate_context()->Map(this, 0, d3d_map, 0, &d3d_mapped);
561                 out_locked_rect->pBits = (uint8_t*)d3d_mapped.pData;
562                 out_locked_rect->Pitch = d3d_mapped.RowPitch;
563                 return hres;
564         }
565
566         virtual HRESULT STDMETHODCALLTYPE Unmap(void)
567         {
568                 this->device->get_immediate_context()->Unmap(this, 0);
569                 return S_OK;
570         }
571
572         virtual HRESULT STDMETHODCALLTYPE GetDevice(
573                 REFIID riid,
574                 void **out_parent)
575         {
576                 if(!device)
577                         return E_NOINTERFACE;
578                 return device->QueryInterface(riid, out_parent);
579         }
580 };
581
582 template<typename Base, typename Desc, typename Object>
583 struct GalliumD3D11View : public GalliumD3D11DescribedObject<Base, Desc, Object>
584 {
585         GalliumD3D11Resource<>* resource;
586         GalliumD3D11View(GalliumD3D11Screen* device, GalliumD3D11Resource<>* resource, Object* object, const Desc& desc)
587         : GalliumD3D11DescribedObject<Base, Desc, Object>(device, object, desc), resource(resource)
588         {
589                 resource->AddRef();
590         }
591
592         ~GalliumD3D11View()
593         {
594                 resource->Release();
595         }
596
597         virtual void STDMETHODCALLTYPE GetResource(ID3D11Resource** out_resource)
598         {
599                 resource->AddRef();
600                 *out_resource = resource;
601         }
602 };
603
604 typedef GalliumD3D11View<ID3D11DepthStencilView, D3D11_DEPTH_STENCIL_VIEW_DESC, struct pipe_surface> GalliumD3D11DepthStencilView;
605 typedef GalliumD3D11View<ID3D11RenderTargetView, D3D11_RENDER_TARGET_VIEW_DESC, struct pipe_surface> GalliumD3D11RenderTargetView;
606
607 #if API >= 11
608 typedef GalliumD3D11View<ID3D11ShaderResourceView, D3D11_SHADER_RESOURCE_VIEW_DESC, struct pipe_sampler_view> GalliumD3D11ShaderResourceView;
609 #else
610 typedef GalliumD3D10View<ID3D10ShaderResourceView1, D3D10_SHADER_RESOURCE_VIEW_DESC1, struct pipe_sampler_view> GalliumD3D10ShaderResourceViewBase;
611
612 struct GalliumD3D10ShaderResourceView : public GalliumD3D10ShaderResourceViewBase
613 {
614         GalliumD3D10ShaderResourceView(GalliumD3D10Screen* device, GalliumD3D10Resource<>* resource, struct pipe_sampler_view* view, const D3D10_SHADER_RESOURCE_VIEW_DESC1& desc)
615         : GalliumD3D10ShaderResourceViewBase(device, resource, view, desc)
616         {}
617
618         virtual void STDMETHODCALLTYPE GetDesc1(D3D10_SHADER_RESOURCE_VIEW_DESC1 *out_desc)
619         {
620                 memcpy(out_desc, &desc, sizeof(*out_desc));
621         }
622
623         virtual void STDMETHODCALLTYPE GetDesc(D3D10_SHADER_RESOURCE_VIEW_DESC *out_desc)
624         {
625                 memcpy(out_desc, &desc, sizeof(*out_desc));
626         }
627 };
628 #endif
629
630 template<typename Base = ID3D11Asynchronous>
631 struct GalliumD3D11Asynchronous : public GalliumD3D11DeviceChild<Base>
632 {
633         struct pipe_query* query;
634         unsigned data_size;
635
636         GalliumD3D11Asynchronous(GalliumD3D11Screen* device, struct pipe_query* query, unsigned data_size)
637         : GalliumD3D11DeviceChild<Base>(device), query(query), data_size(data_size)
638         {}
639
640         ~GalliumD3D11Asynchronous()
641         {
642                 this->device->immediate_pipe->destroy_query(this->device->immediate_pipe, query);
643         }
644
645         virtual unsigned STDMETHODCALLTYPE GetDataSize()
646         {
647                 return data_size;
648         }
649
650 #if API < 11
651         virtual void STDMETHODCALLTYPE Begin()
652         {
653                 this->device->Begin(this);
654         }
655
656         virtual void STDMETHODCALLTYPE End()
657         {
658                 this->device->End(this);
659         }
660
661         virtual HRESULT STDMETHODCALLTYPE GetData(
662                 void * out_data,
663                 unsigned data_size,
664                 unsigned get_data_flags)
665         {
666                 return this->device->GetData(this, out_data, data_size, get_data_flags);
667         }
668 #endif
669 };
670
671 template<typename Base = ID3D11Asynchronous>
672 struct GalliumD3D11QueryOrPredicate : public GalliumD3D11Asynchronous<Base>
673 {
674         D3D11_QUERY_DESC desc;
675         GalliumD3D11QueryOrPredicate(GalliumD3D11Screen* device, struct pipe_query* query, unsigned data_size, const D3D11_QUERY_DESC& desc)
676         : GalliumD3D11Asynchronous<Base>(device, query, data_size), desc(desc)
677         {}
678
679         virtual void STDMETHODCALLTYPE GetDesc(
680                 D3D11_QUERY_DESC *out_desc)
681         {
682                 *out_desc = desc;
683         }
684 };
685
686 struct GalliumD3D11Query : public GalliumD3D11QueryOrPredicate<ID3D11Query>
687 {
688         GalliumD3D11Query(GalliumD3D11Screen* device, struct pipe_query* query, unsigned data_size, const D3D11_QUERY_DESC& desc)
689         : GalliumD3D11QueryOrPredicate<ID3D11Query>(device, query, data_size, desc)
690         {}
691 };
692
693 struct GalliumD3D11Predicate : public GalliumD3D11QueryOrPredicate<ID3D11Predicate>
694 {
695         GalliumD3D11Predicate(GalliumD3D11Screen* device, struct pipe_query* query, unsigned data_size, const D3D11_QUERY_DESC& desc)
696         : GalliumD3D11QueryOrPredicate<ID3D11Predicate>(device, query, data_size, desc)
697         {}
698
699         ~GalliumD3D11Predicate()
700         {
701                 DX10_ONLY(device->UnbindPredicate(this));
702         }
703 };
704
705 struct GalliumD3D11Counter : public GalliumD3D11Asynchronous<ID3D11Counter>
706 {
707         D3D11_COUNTER_DESC desc;
708         GalliumD3D11Counter(GalliumD3D11Screen* device, struct pipe_query* query, unsigned data_size, const D3D11_COUNTER_DESC& desc)
709         : GalliumD3D11Asynchronous<ID3D11Counter>(device, query, data_size), desc(desc)
710         {}
711
712         virtual void STDMETHODCALLTYPE GetDesc(
713                 D3D11_COUNTER_DESC *out_desc)
714         {
715                 *out_desc = desc;
716         }
717 };