mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / d3d11 / view.c
blob734c9619e5bfce13030ff4bd7367dee1e46e1fee
1 /*
2 * Copyright 2009 Henri Verbeet for CodeWeavers
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.
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.
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
20 #define NONAMELESSUNION
21 #define WINE_NO_NAMELESS_EXTENSION
22 #include "d3d11_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
26 static HRESULT get_resource_properties(ID3D11Resource *resource, D3D11_RESOURCE_DIMENSION *dimension,
27 DXGI_FORMAT *format, unsigned int *miplevel_count, unsigned int *layer_count)
29 ID3D11Resource_GetType(resource, dimension);
30 switch (*dimension)
32 case D3D11_RESOURCE_DIMENSION_BUFFER:
33 return S_OK;
35 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
37 const struct d3d_texture1d *texture;
39 if (!(texture = unsafe_impl_from_ID3D11Texture1D((ID3D11Texture1D *)resource)))
41 ERR("Cannot get implementation from ID3D11Texture1D.\n");
42 return E_FAIL;
45 *format = texture->desc.Format;
46 if (miplevel_count)
47 *miplevel_count = texture->desc.MipLevels;
48 *layer_count = texture->desc.ArraySize;
49 break;
52 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
54 const struct d3d_texture2d *texture;
56 if (!(texture = unsafe_impl_from_ID3D11Texture2D((ID3D11Texture2D *)resource)))
58 ERR("Cannot get implementation from ID3D11Texture2D.\n");
59 return E_FAIL;
62 *format = texture->desc.Format;
63 if (miplevel_count)
64 *miplevel_count = texture->desc.MipLevels;
65 *layer_count = texture->desc.ArraySize;
66 break;
69 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
71 const struct d3d_texture3d *texture;
73 if (!(texture = unsafe_impl_from_ID3D11Texture3D((ID3D11Texture3D *)resource)))
75 ERR("Cannot get implementation from ID3D11Texture3D.\n");
76 return E_FAIL;
79 *format = texture->desc.Format;
80 if (miplevel_count)
81 *miplevel_count = texture->desc.MipLevels;
82 *layer_count = texture->desc.Depth;
83 break;
86 default:
87 WARN("Invalid resource dimension %#x.\n", *dimension);
88 return E_INVALIDARG;
91 return S_OK;
94 static HRESULT set_dsv_desc_from_resource(D3D11_DEPTH_STENCIL_VIEW_DESC *desc, ID3D11Resource *resource)
96 D3D11_RESOURCE_DIMENSION dimension;
98 ID3D11Resource_GetType(resource, &dimension);
100 desc->Flags = 0;
102 switch (dimension)
104 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
106 D3D11_TEXTURE1D_DESC texture_desc;
107 ID3D11Texture1D *texture;
109 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture)))
111 ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D.\n");
112 return E_INVALIDARG;
115 ID3D11Texture1D_GetDesc(texture, &texture_desc);
116 ID3D11Texture1D_Release(texture);
118 desc->Format = texture_desc.Format;
119 if (texture_desc.ArraySize == 1)
121 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1D;
122 desc->u.Texture1D.MipSlice = 0;
124 else
126 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1DARRAY;
127 desc->u.Texture1DArray.MipSlice = 0;
128 desc->u.Texture1DArray.FirstArraySlice = 0;
129 desc->u.Texture1DArray.ArraySize = texture_desc.ArraySize;
132 return S_OK;
135 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
137 D3D11_TEXTURE2D_DESC texture_desc;
138 ID3D11Texture2D *texture;
140 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
142 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
143 return E_INVALIDARG;
146 ID3D11Texture2D_GetDesc(texture, &texture_desc);
147 ID3D11Texture2D_Release(texture);
149 desc->Format = texture_desc.Format;
150 if (texture_desc.ArraySize == 1)
152 if (texture_desc.SampleDesc.Count == 1)
154 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
155 desc->u.Texture2D.MipSlice = 0;
157 else
159 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
162 else
164 if (texture_desc.SampleDesc.Count == 1)
166 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
167 desc->u.Texture2DArray.MipSlice = 0;
168 desc->u.Texture2DArray.FirstArraySlice = 0;
169 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
171 else
173 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY;
174 desc->u.Texture2DMSArray.FirstArraySlice = 0;
175 desc->u.Texture2DMSArray.ArraySize = texture_desc.ArraySize;
179 return S_OK;
182 case D3D11_RESOURCE_DIMENSION_BUFFER:
183 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
184 default:
185 WARN("Invalid resource dimension %#x.\n", dimension);
186 return E_INVALIDARG;
190 static HRESULT normalize_dsv_desc(D3D11_DEPTH_STENCIL_VIEW_DESC *desc, ID3D11Resource *resource)
192 D3D11_RESOURCE_DIMENSION dimension;
193 unsigned int layer_count;
194 DXGI_FORMAT format;
195 HRESULT hr;
197 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, NULL, &layer_count)))
198 return hr;
199 switch (dimension)
201 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
202 if (desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE1D
203 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE1DARRAY)
205 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
206 return E_INVALIDARG;
208 break;
210 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
211 if (desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2D
212 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2DARRAY
213 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2DMS
214 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY)
216 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
217 return E_INVALIDARG;
219 break;
221 case D3D11_RESOURCE_DIMENSION_BUFFER:
222 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
223 default:
224 WARN("Invalid resource dimension %#x.\n", dimension);
225 return E_INVALIDARG;
228 if (desc->Format == DXGI_FORMAT_UNKNOWN)
229 desc->Format = format;
231 switch (desc->ViewDimension)
233 case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
234 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < layer_count)
235 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
236 break;
238 case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
239 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
240 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
241 break;
243 case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
244 if (desc->u.Texture2DMSArray.ArraySize == ~0u && desc->u.Texture2DMSArray.FirstArraySlice < layer_count)
245 desc->u.Texture2DMSArray.ArraySize = layer_count - desc->u.Texture2DMSArray.FirstArraySlice;
246 break;
248 default:
249 break;
252 return S_OK;
255 static HRESULT set_rtv_desc_from_resource(D3D11_RENDER_TARGET_VIEW_DESC *desc, ID3D11Resource *resource)
257 D3D11_RESOURCE_DIMENSION dimension;
258 HRESULT hr;
260 ID3D11Resource_GetType(resource, &dimension);
262 switch (dimension)
264 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
266 ID3D11Texture1D *texture;
267 D3D11_TEXTURE1D_DESC texture_desc;
269 hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture);
270 if (FAILED(hr))
272 ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D?\n");
273 return E_INVALIDARG;
276 ID3D11Texture1D_GetDesc(texture, &texture_desc);
277 ID3D11Texture1D_Release(texture);
279 desc->Format = texture_desc.Format;
280 if (texture_desc.ArraySize == 1)
282 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1D;
283 desc->u.Texture1D.MipSlice = 0;
285 else
287 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1DARRAY;
288 desc->u.Texture1DArray.MipSlice = 0;
289 desc->u.Texture1DArray.FirstArraySlice = 0;
290 desc->u.Texture1DArray.ArraySize = texture_desc.ArraySize;
293 return S_OK;
296 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
298 ID3D11Texture2D *texture;
299 D3D11_TEXTURE2D_DESC texture_desc;
301 hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture);
302 if (FAILED(hr))
304 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D?\n");
305 return E_INVALIDARG;
308 ID3D11Texture2D_GetDesc(texture, &texture_desc);
309 ID3D11Texture2D_Release(texture);
311 desc->Format = texture_desc.Format;
312 if (texture_desc.ArraySize == 1)
314 if (texture_desc.SampleDesc.Count == 1)
316 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
317 desc->u.Texture2D.MipSlice = 0;
319 else
321 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
324 else
326 if (texture_desc.SampleDesc.Count == 1)
328 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
329 desc->u.Texture2DArray.MipSlice = 0;
330 desc->u.Texture2DArray.FirstArraySlice = 0;
331 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
333 else
335 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY;
336 desc->u.Texture2DMSArray.FirstArraySlice = 0;
337 desc->u.Texture2DMSArray.ArraySize = texture_desc.ArraySize;
341 return S_OK;
344 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
346 ID3D11Texture3D *texture;
347 D3D11_TEXTURE3D_DESC texture_desc;
349 hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture);
350 if (FAILED(hr))
352 ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D?\n");
353 return E_INVALIDARG;
356 ID3D11Texture3D_GetDesc(texture, &texture_desc);
357 ID3D11Texture3D_Release(texture);
359 desc->Format = texture_desc.Format;
360 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
361 desc->u.Texture3D.MipSlice = 0;
362 desc->u.Texture3D.FirstWSlice = 0;
363 desc->u.Texture3D.WSize = texture_desc.Depth;
365 return S_OK;
368 default:
369 FIXME("Unhandled resource dimension %#x.\n", dimension);
370 return E_INVALIDARG;
374 static HRESULT normalize_rtv_desc(D3D11_RENDER_TARGET_VIEW_DESC *desc, ID3D11Resource *resource)
376 D3D11_RESOURCE_DIMENSION dimension;
377 unsigned int layer_count;
378 DXGI_FORMAT format;
379 HRESULT hr;
381 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, NULL, &layer_count)))
382 return hr;
383 switch (dimension)
385 case D3D11_RESOURCE_DIMENSION_BUFFER:
386 if (desc->ViewDimension != D3D11_RTV_DIMENSION_BUFFER)
388 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
389 return E_INVALIDARG;
391 return S_OK;
393 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
394 if (desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE1D
395 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE1DARRAY)
397 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
398 return E_INVALIDARG;
400 break;
402 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
403 if (desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2D
404 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2DARRAY
405 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2DMS
406 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY)
408 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
409 return E_INVALIDARG;
411 break;
413 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
414 if (desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE3D)
416 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
417 return E_INVALIDARG;
419 break;
421 default:
422 WARN("Invalid resource dimension %#x.\n", dimension);
423 return E_INVALIDARG;
426 if (desc->Format == DXGI_FORMAT_UNKNOWN)
427 desc->Format = format;
429 switch (desc->ViewDimension)
431 case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
432 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < layer_count)
433 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
434 break;
436 case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
437 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
438 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
439 break;
441 case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
442 if (desc->u.Texture2DMSArray.ArraySize == ~0u && desc->u.Texture2DMSArray.FirstArraySlice < layer_count)
443 desc->u.Texture2DMSArray.ArraySize = layer_count - desc->u.Texture2DMSArray.FirstArraySlice;
444 break;
446 case D3D11_RTV_DIMENSION_TEXTURE3D:
447 layer_count = max(1, layer_count >> desc->u.Texture3D.MipSlice);
448 if (desc->u.Texture3D.WSize == ~0u && desc->u.Texture3D.FirstWSlice < layer_count)
449 desc->u.Texture3D.WSize = layer_count - desc->u.Texture3D.FirstWSlice;
450 break;
452 default:
453 break;
456 return S_OK;
459 static HRESULT set_srv_desc_from_resource(D3D11_SHADER_RESOURCE_VIEW_DESC *desc, ID3D11Resource *resource)
461 D3D11_RESOURCE_DIMENSION dimension;
463 ID3D11Resource_GetType(resource, &dimension);
465 switch (dimension)
467 case D3D11_RESOURCE_DIMENSION_BUFFER:
469 D3D11_BUFFER_DESC buffer_desc;
470 ID3D11Buffer *buffer;
472 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Buffer, (void **)&buffer)))
474 ERR("Resource of type BUFFER doesn't implement ID3D11Buffer.\n");
475 return E_INVALIDARG;
478 ID3D11Buffer_GetDesc(buffer, &buffer_desc);
479 ID3D11Buffer_Release(buffer);
481 if (buffer_desc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
483 desc->Format = DXGI_FORMAT_UNKNOWN;
484 desc->ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
485 desc->u.Buffer.u1.FirstElement = 0;
486 desc->u.Buffer.u2.NumElements = buffer_desc.ByteWidth / buffer_desc.StructureByteStride;
487 return S_OK;
490 return E_INVALIDARG;
493 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
495 D3D11_TEXTURE1D_DESC texture_desc;
496 ID3D11Texture1D *texture;
498 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture)))
500 ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D.\n");
501 return E_INVALIDARG;
504 ID3D11Texture1D_GetDesc(texture, &texture_desc);
505 ID3D11Texture1D_Release(texture);
507 desc->Format = texture_desc.Format;
508 if (texture_desc.ArraySize == 1)
510 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
511 desc->u.Texture1D.MostDetailedMip = 0;
512 desc->u.Texture1D.MipLevels = texture_desc.MipLevels;
514 else
516 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY;
517 desc->u.Texture1DArray.MostDetailedMip = 0;
518 desc->u.Texture1DArray.MipLevels = texture_desc.MipLevels;
519 desc->u.Texture1DArray.FirstArraySlice = 0;
520 desc->u.Texture1DArray.ArraySize = texture_desc.ArraySize;
523 return S_OK;
526 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
528 D3D11_TEXTURE2D_DESC texture_desc;
529 ID3D11Texture2D *texture;
531 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
533 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
534 return E_INVALIDARG;
537 ID3D11Texture2D_GetDesc(texture, &texture_desc);
538 ID3D11Texture2D_Release(texture);
540 desc->Format = texture_desc.Format;
541 if (texture_desc.MiscFlags & D3D11_RESOURCE_MISC_TEXTURECUBE)
543 if (texture_desc.ArraySize >= 12)
545 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY;
546 desc->u.TextureCubeArray.MostDetailedMip = 0;
547 desc->u.TextureCubeArray.MipLevels = texture_desc.MipLevels;
548 desc->u.TextureCubeArray.First2DArrayFace = 0;
549 desc->u.TextureCubeArray.NumCubes = texture_desc.ArraySize / 6;
551 else
553 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
554 desc->u.TextureCube.MostDetailedMip = 0;
555 desc->u.TextureCube.MipLevels = texture_desc.MipLevels;
558 else if (texture_desc.ArraySize == 1)
560 if (texture_desc.SampleDesc.Count == 1)
562 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
563 desc->u.Texture2D.MostDetailedMip = 0;
564 desc->u.Texture2D.MipLevels = texture_desc.MipLevels;
566 else
568 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
571 else
573 if (texture_desc.SampleDesc.Count == 1)
575 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
576 desc->u.Texture2DArray.MostDetailedMip = 0;
577 desc->u.Texture2DArray.MipLevels = texture_desc.MipLevels;
578 desc->u.Texture2DArray.FirstArraySlice = 0;
579 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
581 else
583 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY;
584 desc->u.Texture2DMSArray.FirstArraySlice = 0;
585 desc->u.Texture2DMSArray.ArraySize = texture_desc.ArraySize;
589 return S_OK;
592 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
594 D3D11_TEXTURE3D_DESC texture_desc;
595 ID3D11Texture3D *texture;
597 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture)))
599 ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D.\n");
600 return E_INVALIDARG;
603 ID3D11Texture3D_GetDesc(texture, &texture_desc);
604 ID3D11Texture3D_Release(texture);
606 desc->Format = texture_desc.Format;
607 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
608 desc->u.Texture3D.MostDetailedMip = 0;
609 desc->u.Texture3D.MipLevels = texture_desc.MipLevels;
611 return S_OK;
614 default:
615 WARN("Invalid resource dimension %#x.\n", dimension);
616 return E_INVALIDARG;
620 static HRESULT normalize_srv_desc(D3D11_SHADER_RESOURCE_VIEW_DESC *desc, ID3D11Resource *resource)
622 unsigned int miplevel_count, layer_count;
623 D3D11_RESOURCE_DIMENSION dimension;
624 DXGI_FORMAT format;
625 HRESULT hr;
627 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, &miplevel_count, &layer_count)))
628 return hr;
629 switch (dimension)
631 case D3D11_RESOURCE_DIMENSION_BUFFER:
632 if (desc->ViewDimension != D3D11_SRV_DIMENSION_BUFFER
633 && desc->ViewDimension != D3D11_SRV_DIMENSION_BUFFEREX)
635 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
636 return E_INVALIDARG;
638 if (!desc->u.Buffer.u2.NumElements)
640 WARN("Zero sized buffer view.\n");
641 return E_INVALIDARG;
643 return S_OK;
645 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
646 if (desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE1D
647 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE1DARRAY)
649 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
650 return E_INVALIDARG;
652 break;
654 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
655 if (desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2D
656 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2DARRAY
657 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2DMS
658 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY
659 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURECUBE
660 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURECUBEARRAY)
662 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
663 return E_INVALIDARG;
665 break;
667 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
668 if (desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE3D)
670 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
671 return E_INVALIDARG;
673 break;
675 default:
676 WARN("Invalid resource dimension %#x.\n", dimension);
677 return E_INVALIDARG;
680 if (desc->Format == DXGI_FORMAT_UNKNOWN)
681 desc->Format = format;
683 switch (desc->ViewDimension)
685 case D3D11_SRV_DIMENSION_TEXTURE1D:
686 if (desc->u.Texture1D.MipLevels == ~0u && desc->u.Texture1D.MostDetailedMip < miplevel_count)
687 desc->u.Texture1D.MipLevels = miplevel_count - desc->u.Texture1D.MostDetailedMip;
688 break;
690 case D3D11_SRV_DIMENSION_TEXTURE1DARRAY:
691 if (desc->u.Texture1DArray.MipLevels == ~0u && desc->u.Texture1DArray.MostDetailedMip < miplevel_count)
692 desc->u.Texture1DArray.MipLevels = miplevel_count - desc->u.Texture1DArray.MostDetailedMip;
693 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < miplevel_count)
694 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
695 break;
697 case D3D11_SRV_DIMENSION_TEXTURE2D:
698 if (desc->u.Texture2D.MipLevels == ~0u && desc->u.Texture2D.MostDetailedMip < miplevel_count)
699 desc->u.Texture2D.MipLevels = miplevel_count - desc->u.Texture2D.MostDetailedMip;
700 break;
702 case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
703 if (desc->u.Texture2DArray.MipLevels == ~0u && desc->u.Texture2DArray.MostDetailedMip < miplevel_count)
704 desc->u.Texture2DArray.MipLevels = miplevel_count - desc->u.Texture2DArray.MostDetailedMip;
705 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
706 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
707 break;
709 case D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY:
710 if (desc->u.Texture2DMSArray.ArraySize == ~0u && desc->u.Texture2DMSArray.FirstArraySlice < layer_count)
711 desc->u.Texture2DMSArray.ArraySize = layer_count - desc->u.Texture2DMSArray.FirstArraySlice;
712 break;
714 case D3D11_SRV_DIMENSION_TEXTURE3D:
715 if (desc->u.Texture3D.MipLevels == ~0u && desc->u.Texture3D.MostDetailedMip < miplevel_count)
716 desc->u.Texture3D.MipLevels = miplevel_count - desc->u.Texture3D.MostDetailedMip;
717 break;
719 case D3D11_SRV_DIMENSION_TEXTURECUBE:
720 if (desc->u.TextureCube.MipLevels == ~0u && desc->u.TextureCube.MostDetailedMip < miplevel_count)
721 desc->u.TextureCube.MipLevels = miplevel_count - desc->u.TextureCube.MostDetailedMip;
722 break;
724 case D3D11_SRV_DIMENSION_TEXTURECUBEARRAY:
725 if (desc->u.TextureCubeArray.MipLevels == ~0u && desc->u.TextureCubeArray.MostDetailedMip < miplevel_count)
726 desc->u.TextureCubeArray.MipLevels = miplevel_count - desc->u.TextureCubeArray.MostDetailedMip;
727 if (desc->u.TextureCubeArray.NumCubes == ~0u && desc->u.TextureCubeArray.First2DArrayFace < layer_count)
728 desc->u.TextureCubeArray.NumCubes = (layer_count - desc->u.TextureCubeArray.First2DArrayFace) / 6;
729 break;
731 default:
732 break;
735 return S_OK;
738 static HRESULT set_uav_desc_from_resource(D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, ID3D11Resource *resource)
740 D3D11_RESOURCE_DIMENSION dimension;
742 ID3D11Resource_GetType(resource, &dimension);
744 switch (dimension)
746 case D3D11_RESOURCE_DIMENSION_BUFFER:
748 D3D11_BUFFER_DESC buffer_desc;
749 ID3D11Buffer *buffer;
751 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Buffer, (void **)&buffer)))
753 ERR("Resource of type BUFFER doesn't implement ID3D11Buffer.\n");
754 return E_INVALIDARG;
757 ID3D11Buffer_GetDesc(buffer, &buffer_desc);
758 ID3D11Buffer_Release(buffer);
760 if (buffer_desc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
762 desc->Format = DXGI_FORMAT_UNKNOWN;
763 desc->ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
764 desc->u.Buffer.FirstElement = 0;
765 desc->u.Buffer.NumElements = buffer_desc.ByteWidth / buffer_desc.StructureByteStride;
766 desc->u.Buffer.Flags = 0;
767 return S_OK;
770 return E_INVALIDARG;
773 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
775 D3D11_TEXTURE2D_DESC texture_desc;
776 ID3D11Texture2D *texture;
778 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
780 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
781 return E_INVALIDARG;
784 ID3D11Texture2D_GetDesc(texture, &texture_desc);
785 ID3D11Texture2D_Release(texture);
787 if (texture_desc.SampleDesc.Count != 1)
789 WARN("Trying to create view for multisample texture.\n");
790 return E_INVALIDARG;
793 desc->Format = texture_desc.Format;
794 if (texture_desc.ArraySize == 1)
796 desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
797 desc->u.Texture2D.MipSlice = 0;
799 else
801 desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2DARRAY;
802 desc->u.Texture2DArray.MipSlice = 0;
803 desc->u.Texture2DArray.FirstArraySlice = 0;
804 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
807 return S_OK;
810 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
812 D3D11_TEXTURE3D_DESC texture_desc;
813 ID3D11Texture3D *texture;
815 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture)))
817 ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D.\n");
818 return E_INVALIDARG;
821 ID3D11Texture3D_GetDesc(texture, &texture_desc);
822 ID3D11Texture3D_Release(texture);
824 desc->Format = texture_desc.Format;
825 desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE3D;
826 desc->u.Texture3D.MipSlice = 0;
827 desc->u.Texture3D.FirstWSlice = 0;
828 desc->u.Texture3D.WSize = texture_desc.Depth;
830 return S_OK;
833 default:
834 FIXME("Unhandled resource dimension %#x.\n", dimension);
835 return E_INVALIDARG;
839 static HRESULT normalize_uav_desc(D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, ID3D11Resource *resource)
841 D3D11_RESOURCE_DIMENSION dimension;
842 unsigned int layer_count;
843 DXGI_FORMAT format;
844 HRESULT hr;
846 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, NULL, &layer_count)))
847 return hr;
848 switch (dimension)
850 case D3D11_RESOURCE_DIMENSION_BUFFER:
851 if (desc->ViewDimension != D3D11_UAV_DIMENSION_BUFFER)
853 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
854 return E_INVALIDARG;
856 return S_OK;
858 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
859 if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE1D
860 && desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE1DARRAY)
862 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
863 return E_INVALIDARG;
865 break;
867 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
868 if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE2D
869 && desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE2DARRAY)
871 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
872 return E_INVALIDARG;
874 break;
876 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
877 if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE3D)
879 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
880 return E_INVALIDARG;
882 break;
884 default:
885 WARN("Invalid resource dimension %#x.\n", dimension);
886 return E_INVALIDARG;
889 if (desc->Format == DXGI_FORMAT_UNKNOWN)
890 desc->Format = format;
892 switch (desc->ViewDimension)
894 case D3D11_UAV_DIMENSION_TEXTURE1DARRAY:
895 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < layer_count)
896 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
897 break;
899 case D3D11_UAV_DIMENSION_TEXTURE2DARRAY:
900 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
901 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
902 break;
904 case D3D11_UAV_DIMENSION_TEXTURE3D:
905 layer_count = max(1, layer_count >> desc->u.Texture3D.MipSlice);
906 if (desc->u.Texture3D.WSize == ~0u && desc->u.Texture3D.FirstWSlice < layer_count)
907 desc->u.Texture3D.WSize = layer_count - desc->u.Texture3D.FirstWSlice;
908 break;
910 default:
911 break;
914 return S_OK;
917 /* ID3D11DepthStencilView methods */
919 static inline struct d3d_depthstencil_view *impl_from_ID3D11DepthStencilView(ID3D11DepthStencilView *iface)
921 return CONTAINING_RECORD(iface, struct d3d_depthstencil_view, ID3D11DepthStencilView_iface);
924 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_QueryInterface(ID3D11DepthStencilView *iface,
925 REFIID riid, void **object)
927 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
929 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
931 if (IsEqualGUID(riid, &IID_ID3D11DepthStencilView)
932 || IsEqualGUID(riid, &IID_ID3D11View)
933 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
934 || IsEqualGUID(riid, &IID_IUnknown))
936 ID3D11DepthStencilView_AddRef(iface);
937 *object = iface;
938 return S_OK;
941 if (IsEqualGUID(riid, &IID_ID3D10DepthStencilView)
942 || IsEqualGUID(riid, &IID_ID3D10View)
943 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
945 ID3D10DepthStencilView_AddRef(&view->ID3D10DepthStencilView_iface);
946 *object = &view->ID3D10DepthStencilView_iface;
947 return S_OK;
950 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
952 *object = NULL;
953 return E_NOINTERFACE;
956 static ULONG STDMETHODCALLTYPE d3d11_depthstencil_view_AddRef(ID3D11DepthStencilView *iface)
958 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
959 ULONG refcount = InterlockedIncrement(&view->refcount);
961 TRACE("%p increasing refcount to %u.\n", view, refcount);
963 if (refcount == 1)
965 ID3D11Device2_AddRef(view->device);
966 wined3d_mutex_lock();
967 wined3d_rendertarget_view_incref(view->wined3d_view);
968 wined3d_mutex_unlock();
971 return refcount;
974 static ULONG STDMETHODCALLTYPE d3d11_depthstencil_view_Release(ID3D11DepthStencilView *iface)
976 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
977 ULONG refcount = InterlockedDecrement(&view->refcount);
979 TRACE("%p decreasing refcount to %u.\n", view, refcount);
981 if (!refcount)
983 ID3D11Device2 *device = view->device;
985 wined3d_mutex_lock();
986 wined3d_rendertarget_view_decref(view->wined3d_view);
987 wined3d_mutex_unlock();
989 ID3D11Device2_Release(device);
992 return refcount;
995 static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetDevice(ID3D11DepthStencilView *iface,
996 ID3D11Device **device)
998 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1000 TRACE("iface %p, device %p.\n", iface, device);
1002 *device = (ID3D11Device *)view->device;
1003 ID3D11Device_AddRef(*device);
1006 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_GetPrivateData(ID3D11DepthStencilView *iface,
1007 REFGUID guid, UINT *data_size, void *data)
1009 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1011 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1013 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1016 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_SetPrivateData(ID3D11DepthStencilView *iface,
1017 REFGUID guid, UINT data_size, const void *data)
1019 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1021 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1023 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1026 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_SetPrivateDataInterface(ID3D11DepthStencilView *iface,
1027 REFGUID guid, const IUnknown *data)
1029 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1031 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1033 return d3d_set_private_data_interface(&view->private_store, guid, data);
1036 static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetResource(ID3D11DepthStencilView *iface,
1037 ID3D11Resource **resource)
1039 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1041 TRACE("iface %p, resource %p.\n", iface, resource);
1043 *resource = view->resource;
1044 ID3D11Resource_AddRef(*resource);
1047 static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetDesc(ID3D11DepthStencilView *iface,
1048 D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
1050 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1052 TRACE("iface %p, desc %p.\n", iface, desc);
1054 *desc = view->desc;
1057 static const struct ID3D11DepthStencilViewVtbl d3d11_depthstencil_view_vtbl =
1059 /* IUnknown methods */
1060 d3d11_depthstencil_view_QueryInterface,
1061 d3d11_depthstencil_view_AddRef,
1062 d3d11_depthstencil_view_Release,
1063 /* ID3D11DeviceChild methods */
1064 d3d11_depthstencil_view_GetDevice,
1065 d3d11_depthstencil_view_GetPrivateData,
1066 d3d11_depthstencil_view_SetPrivateData,
1067 d3d11_depthstencil_view_SetPrivateDataInterface,
1068 /* ID3D11View methods */
1069 d3d11_depthstencil_view_GetResource,
1070 /* ID3D11DepthStencilView methods */
1071 d3d11_depthstencil_view_GetDesc,
1074 /* ID3D10DepthStencilView methods */
1076 static inline struct d3d_depthstencil_view *impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
1078 return CONTAINING_RECORD(iface, struct d3d_depthstencil_view, ID3D10DepthStencilView_iface);
1081 /* IUnknown methods */
1083 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_QueryInterface(ID3D10DepthStencilView *iface,
1084 REFIID riid, void **object)
1086 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1088 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1090 return d3d11_depthstencil_view_QueryInterface(&view->ID3D11DepthStencilView_iface, riid, object);
1093 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_AddRef(ID3D10DepthStencilView *iface)
1095 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1097 TRACE("iface %p.\n", iface);
1099 return d3d11_depthstencil_view_AddRef(&view->ID3D11DepthStencilView_iface);
1102 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_Release(ID3D10DepthStencilView *iface)
1104 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1106 TRACE("iface %p.\n", iface);
1108 return d3d11_depthstencil_view_Release(&view->ID3D11DepthStencilView_iface);
1111 /* ID3D10DeviceChild methods */
1113 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDevice(ID3D10DepthStencilView *iface, ID3D10Device **device)
1115 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1117 TRACE("iface %p, device %p.\n", iface, device);
1119 ID3D11Device2_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
1122 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_GetPrivateData(ID3D10DepthStencilView *iface,
1123 REFGUID guid, UINT *data_size, void *data)
1125 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1127 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
1128 iface, debugstr_guid(guid), data_size, data);
1130 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1133 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateData(ID3D10DepthStencilView *iface,
1134 REFGUID guid, UINT data_size, const void *data)
1136 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1138 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
1139 iface, debugstr_guid(guid), data_size, data);
1141 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1144 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateDataInterface(ID3D10DepthStencilView *iface,
1145 REFGUID guid, const IUnknown *data)
1147 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1149 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1151 return d3d_set_private_data_interface(&view->private_store, guid, data);
1154 /* ID3D10View methods */
1156 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetResource(ID3D10DepthStencilView *iface,
1157 ID3D10Resource **resource)
1159 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1161 TRACE("iface %p, resource %p.\n", iface, resource);
1163 ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1166 /* ID3D10DepthStencilView methods */
1168 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDesc(ID3D10DepthStencilView *iface,
1169 D3D10_DEPTH_STENCIL_VIEW_DESC *desc)
1171 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1172 const D3D11_DEPTH_STENCIL_VIEW_DESC *d3d11_desc = &view->desc;
1174 TRACE("iface %p, desc %p.\n", iface, desc);
1176 desc->Format = d3d11_desc->Format;
1177 desc->ViewDimension = (D3D10_DSV_DIMENSION)d3d11_desc->ViewDimension;
1178 memcpy(&desc->u, &d3d11_desc->u, sizeof(desc->u));
1181 static const struct ID3D10DepthStencilViewVtbl d3d10_depthstencil_view_vtbl =
1183 /* IUnknown methods */
1184 d3d10_depthstencil_view_QueryInterface,
1185 d3d10_depthstencil_view_AddRef,
1186 d3d10_depthstencil_view_Release,
1187 /* ID3D10DeviceChild methods */
1188 d3d10_depthstencil_view_GetDevice,
1189 d3d10_depthstencil_view_GetPrivateData,
1190 d3d10_depthstencil_view_SetPrivateData,
1191 d3d10_depthstencil_view_SetPrivateDataInterface,
1192 /* ID3D10View methods */
1193 d3d10_depthstencil_view_GetResource,
1194 /* ID3D10DepthStencilView methods */
1195 d3d10_depthstencil_view_GetDesc,
1198 static void STDMETHODCALLTYPE d3d_depth_stencil_view_wined3d_object_destroyed(void *parent)
1200 struct d3d_depthstencil_view *view = parent;
1202 wined3d_private_store_cleanup(&view->private_store);
1203 heap_free(parent);
1206 static const struct wined3d_parent_ops d3d_depth_stencil_view_wined3d_parent_ops =
1208 d3d_depth_stencil_view_wined3d_object_destroyed,
1211 static void wined3d_depth_stencil_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
1212 const D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
1214 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
1216 wined3d_desc->flags = 0;
1217 if (desc->Flags & D3D11_DSV_READ_ONLY_DEPTH)
1218 wined3d_desc->flags |= WINED3D_VIEW_READ_ONLY_DEPTH;
1219 if (desc->Flags & D3D11_DSV_READ_ONLY_STENCIL)
1220 wined3d_desc->flags |= WINED3D_VIEW_READ_ONLY_STENCIL;
1222 wined3d_desc->u.texture.level_count = 1;
1223 switch (desc->ViewDimension)
1225 case D3D11_DSV_DIMENSION_TEXTURE1D:
1226 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
1227 wined3d_desc->u.texture.layer_idx = 0;
1228 wined3d_desc->u.texture.layer_count = 1;
1229 break;
1231 case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
1232 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1233 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
1234 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
1235 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
1236 break;
1238 case D3D11_DSV_DIMENSION_TEXTURE2D:
1239 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
1240 wined3d_desc->u.texture.layer_idx = 0;
1241 wined3d_desc->u.texture.layer_count = 1;
1242 break;
1244 case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
1245 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1246 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
1247 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
1248 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
1249 break;
1251 case D3D11_DSV_DIMENSION_TEXTURE2DMS:
1252 wined3d_desc->u.texture.level_idx = 0;
1253 wined3d_desc->u.texture.layer_idx = 0;
1254 wined3d_desc->u.texture.layer_count = 1;
1255 break;
1257 case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
1258 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1259 wined3d_desc->u.texture.level_idx = 0;
1260 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
1261 wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
1262 break;
1264 default:
1265 FIXME("Unhandled view dimension %#x.\n", desc->ViewDimension);
1266 wined3d_desc->u.texture.level_idx = 0;
1267 wined3d_desc->u.texture.layer_idx = 0;
1268 wined3d_desc->u.texture.layer_count = 1;
1269 break;
1273 static HRESULT d3d_depthstencil_view_init(struct d3d_depthstencil_view *view, struct d3d_device *device,
1274 ID3D11Resource *resource, const D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
1276 struct wined3d_resource *wined3d_resource;
1277 struct wined3d_view_desc wined3d_desc;
1278 HRESULT hr;
1280 view->ID3D11DepthStencilView_iface.lpVtbl = &d3d11_depthstencil_view_vtbl;
1281 view->ID3D10DepthStencilView_iface.lpVtbl = &d3d10_depthstencil_view_vtbl;
1282 view->refcount = 1;
1284 if (!desc)
1286 hr = set_dsv_desc_from_resource(&view->desc, resource);
1288 else
1290 view->desc = *desc;
1291 hr = normalize_dsv_desc(&view->desc, resource);
1293 if (FAILED(hr))
1294 return hr;
1296 wined3d_mutex_lock();
1297 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1299 wined3d_mutex_unlock();
1300 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
1301 return E_FAIL;
1304 wined3d_depth_stencil_view_desc_from_d3d11(&wined3d_desc, &view->desc);
1305 if (FAILED(hr = wined3d_rendertarget_view_create(&wined3d_desc, wined3d_resource,
1306 view, &d3d_depth_stencil_view_wined3d_parent_ops, &view->wined3d_view)))
1308 wined3d_mutex_unlock();
1309 WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
1310 return hr;
1313 wined3d_private_store_init(&view->private_store);
1314 wined3d_mutex_unlock();
1315 view->resource = resource;
1316 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
1318 return S_OK;
1321 HRESULT d3d_depthstencil_view_create(struct d3d_device *device, ID3D11Resource *resource,
1322 const D3D11_DEPTH_STENCIL_VIEW_DESC *desc, struct d3d_depthstencil_view **view)
1324 struct d3d_depthstencil_view *object;
1325 HRESULT hr;
1327 if (!(object = heap_alloc_zero(sizeof(*object))))
1328 return E_OUTOFMEMORY;
1330 if (FAILED(hr = d3d_depthstencil_view_init(object, device, resource, desc)))
1332 WARN("Failed to initialize depthstencil view, hr %#x.\n", hr);
1333 heap_free(object);
1334 return hr;
1337 TRACE("Created depthstencil view %p.\n", object);
1338 *view = object;
1340 return S_OK;
1343 struct d3d_depthstencil_view *unsafe_impl_from_ID3D11DepthStencilView(ID3D11DepthStencilView *iface)
1345 if (!iface)
1346 return NULL;
1347 assert(iface->lpVtbl == &d3d11_depthstencil_view_vtbl);
1349 return impl_from_ID3D11DepthStencilView(iface);
1352 struct d3d_depthstencil_view *unsafe_impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
1354 if (!iface)
1355 return NULL;
1356 assert(iface->lpVtbl == &d3d10_depthstencil_view_vtbl);
1358 return impl_from_ID3D10DepthStencilView(iface);
1361 /* ID3D11RenderTargetView methods */
1363 static inline struct d3d_rendertarget_view *impl_from_ID3D11RenderTargetView(ID3D11RenderTargetView *iface)
1365 return CONTAINING_RECORD(iface, struct d3d_rendertarget_view, ID3D11RenderTargetView_iface);
1368 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_QueryInterface(ID3D11RenderTargetView *iface,
1369 REFIID riid, void **object)
1371 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1373 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1375 if (IsEqualGUID(riid, &IID_ID3D11RenderTargetView)
1376 || IsEqualGUID(riid, &IID_ID3D11View)
1377 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
1378 || IsEqualGUID(riid, &IID_IUnknown))
1380 ID3D11RenderTargetView_AddRef(iface);
1381 *object = iface;
1382 return S_OK;
1385 if (IsEqualGUID(riid, &IID_ID3D10RenderTargetView)
1386 || IsEqualGUID(riid, &IID_ID3D10View)
1387 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
1389 ID3D10RenderTargetView_AddRef(&view->ID3D10RenderTargetView_iface);
1390 *object = &view->ID3D10RenderTargetView_iface;
1391 return S_OK;
1394 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1396 *object = NULL;
1397 return E_NOINTERFACE;
1400 static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_AddRef(ID3D11RenderTargetView *iface)
1402 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1403 ULONG refcount = InterlockedIncrement(&view->refcount);
1405 TRACE("%p increasing refcount to %u.\n", view, refcount);
1407 if (refcount == 1)
1409 ID3D11Device2_AddRef(view->device);
1410 wined3d_mutex_lock();
1411 wined3d_rendertarget_view_incref(view->wined3d_view);
1412 wined3d_mutex_unlock();
1415 return refcount;
1418 static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_Release(ID3D11RenderTargetView *iface)
1420 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1421 ULONG refcount = InterlockedDecrement(&view->refcount);
1423 TRACE("%p decreasing refcount to %u.\n", view, refcount);
1425 if (!refcount)
1427 ID3D11Device2 *device = view->device;
1429 wined3d_mutex_lock();
1430 wined3d_rendertarget_view_decref(view->wined3d_view);
1431 wined3d_mutex_unlock();
1433 ID3D11Device2_Release(device);
1436 return refcount;
1439 static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDevice(ID3D11RenderTargetView *iface,
1440 ID3D11Device **device)
1442 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1444 TRACE("iface %p, device %p.\n", iface, device);
1446 *device = (ID3D11Device *)view->device;
1447 ID3D11Device_AddRef(*device);
1450 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_GetPrivateData(ID3D11RenderTargetView *iface,
1451 REFGUID guid, UINT *data_size, void *data)
1453 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1455 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1457 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1460 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_SetPrivateData(ID3D11RenderTargetView *iface,
1461 REFGUID guid, UINT data_size, const void *data)
1463 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1465 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1467 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1470 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_SetPrivateDataInterface(ID3D11RenderTargetView *iface,
1471 REFGUID guid, const IUnknown *data)
1473 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1475 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1477 return d3d_set_private_data_interface(&view->private_store, guid, data);
1480 static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetResource(ID3D11RenderTargetView *iface,
1481 ID3D11Resource **resource)
1483 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1485 TRACE("iface %p, resource %p.\n", iface, resource);
1487 *resource = view->resource;
1488 ID3D11Resource_AddRef(*resource);
1491 static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDesc(ID3D11RenderTargetView *iface,
1492 D3D11_RENDER_TARGET_VIEW_DESC *desc)
1494 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1496 TRACE("iface %p, desc %p.\n", iface, desc);
1498 *desc = view->desc;
1501 static const struct ID3D11RenderTargetViewVtbl d3d11_rendertarget_view_vtbl =
1503 /* IUnknown methods */
1504 d3d11_rendertarget_view_QueryInterface,
1505 d3d11_rendertarget_view_AddRef,
1506 d3d11_rendertarget_view_Release,
1507 /* ID3D11DeviceChild methods */
1508 d3d11_rendertarget_view_GetDevice,
1509 d3d11_rendertarget_view_GetPrivateData,
1510 d3d11_rendertarget_view_SetPrivateData,
1511 d3d11_rendertarget_view_SetPrivateDataInterface,
1512 /* ID3D11View methods */
1513 d3d11_rendertarget_view_GetResource,
1514 /* ID3D11RenderTargetView methods */
1515 d3d11_rendertarget_view_GetDesc,
1518 /* ID3D10RenderTargetView methods */
1520 static inline struct d3d_rendertarget_view *impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
1522 return CONTAINING_RECORD(iface, struct d3d_rendertarget_view, ID3D10RenderTargetView_iface);
1525 /* IUnknown methods */
1527 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_QueryInterface(ID3D10RenderTargetView *iface,
1528 REFIID riid, void **object)
1530 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1532 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1534 return d3d11_rendertarget_view_QueryInterface(&view->ID3D11RenderTargetView_iface, riid, object);
1537 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_AddRef(ID3D10RenderTargetView *iface)
1539 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1541 TRACE("iface %p.\n", iface);
1543 return d3d11_rendertarget_view_AddRef(&view->ID3D11RenderTargetView_iface);
1546 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_Release(ID3D10RenderTargetView *iface)
1548 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1550 TRACE("iface %p.\n", iface);
1552 return d3d11_rendertarget_view_Release(&view->ID3D11RenderTargetView_iface);
1555 /* ID3D10DeviceChild methods */
1557 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDevice(ID3D10RenderTargetView *iface, ID3D10Device **device)
1559 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1561 TRACE("iface %p, device %p.\n", iface, device);
1563 ID3D11Device2_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
1566 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_GetPrivateData(ID3D10RenderTargetView *iface,
1567 REFGUID guid, UINT *data_size, void *data)
1569 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1571 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
1572 iface, debugstr_guid(guid), data_size, data);
1574 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1577 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateData(ID3D10RenderTargetView *iface,
1578 REFGUID guid, UINT data_size, const void *data)
1580 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1582 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
1583 iface, debugstr_guid(guid), data_size, data);
1585 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1588 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateDataInterface(ID3D10RenderTargetView *iface,
1589 REFGUID guid, const IUnknown *data)
1591 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1593 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1595 return d3d_set_private_data_interface(&view->private_store, guid, data);
1598 /* ID3D10View methods */
1600 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetResource(ID3D10RenderTargetView *iface,
1601 ID3D10Resource **resource)
1603 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1605 TRACE("iface %p, resource %p\n", iface, resource);
1607 ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1610 /* ID3D10RenderTargetView methods */
1612 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDesc(ID3D10RenderTargetView *iface,
1613 D3D10_RENDER_TARGET_VIEW_DESC *desc)
1615 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1617 TRACE("iface %p, desc %p\n", iface, desc);
1619 memcpy(desc, &view->desc, sizeof(*desc));
1622 static const struct ID3D10RenderTargetViewVtbl d3d10_rendertarget_view_vtbl =
1624 /* IUnknown methods */
1625 d3d10_rendertarget_view_QueryInterface,
1626 d3d10_rendertarget_view_AddRef,
1627 d3d10_rendertarget_view_Release,
1628 /* ID3D10DeviceChild methods */
1629 d3d10_rendertarget_view_GetDevice,
1630 d3d10_rendertarget_view_GetPrivateData,
1631 d3d10_rendertarget_view_SetPrivateData,
1632 d3d10_rendertarget_view_SetPrivateDataInterface,
1633 /* ID3D10View methods */
1634 d3d10_rendertarget_view_GetResource,
1635 /* ID3D10RenderTargetView methods */
1636 d3d10_rendertarget_view_GetDesc,
1639 static void STDMETHODCALLTYPE d3d_render_target_view_wined3d_object_destroyed(void *parent)
1641 struct d3d_rendertarget_view *view = parent;
1643 wined3d_private_store_cleanup(&view->private_store);
1644 heap_free(parent);
1647 static const struct wined3d_parent_ops d3d_render_target_view_wined3d_parent_ops =
1649 d3d_render_target_view_wined3d_object_destroyed,
1652 static void wined3d_rendertarget_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
1653 const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1655 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
1657 wined3d_desc->flags = 0;
1658 wined3d_desc->u.texture.level_count = 1;
1659 switch (desc->ViewDimension)
1661 case D3D11_RTV_DIMENSION_BUFFER:
1662 wined3d_desc->u.buffer.start_idx = desc->u.Buffer.u1.FirstElement;
1663 wined3d_desc->u.buffer.count = desc->u.Buffer.u2.NumElements;
1664 break;
1666 case D3D11_RTV_DIMENSION_TEXTURE1D:
1667 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
1668 wined3d_desc->u.texture.layer_idx = 0;
1669 wined3d_desc->u.texture.layer_count = 1;
1670 break;
1672 case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
1673 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1674 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
1675 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
1676 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
1677 break;
1679 case D3D11_RTV_DIMENSION_TEXTURE2D:
1680 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
1681 wined3d_desc->u.texture.layer_idx = 0;
1682 wined3d_desc->u.texture.layer_count = 1;
1683 break;
1685 case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
1686 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1687 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
1688 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
1689 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
1690 break;
1692 case D3D11_RTV_DIMENSION_TEXTURE2DMS:
1693 wined3d_desc->u.texture.level_idx = 0;
1694 wined3d_desc->u.texture.layer_idx = 0;
1695 wined3d_desc->u.texture.layer_count = 1;
1696 break;
1698 case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
1699 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1700 wined3d_desc->u.texture.level_idx = 0;
1701 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
1702 wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
1703 break;
1705 case D3D11_RTV_DIMENSION_TEXTURE3D:
1706 wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MipSlice;
1707 wined3d_desc->u.texture.layer_idx = desc->u.Texture3D.FirstWSlice;
1708 wined3d_desc->u.texture.layer_count = desc->u.Texture3D.WSize;
1709 break;
1711 default:
1712 FIXME("Unhandled view dimension %#x.\n", desc->ViewDimension);
1713 wined3d_desc->u.texture.level_idx = 0;
1714 wined3d_desc->u.texture.layer_idx = 0;
1715 wined3d_desc->u.texture.layer_count = 1;
1716 break;
1720 static HRESULT d3d_rendertarget_view_init(struct d3d_rendertarget_view *view, struct d3d_device *device,
1721 ID3D11Resource *resource, const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1723 struct wined3d_resource *wined3d_resource;
1724 struct wined3d_view_desc wined3d_desc;
1725 HRESULT hr;
1727 view->ID3D11RenderTargetView_iface.lpVtbl = &d3d11_rendertarget_view_vtbl;
1728 view->ID3D10RenderTargetView_iface.lpVtbl = &d3d10_rendertarget_view_vtbl;
1729 view->refcount = 1;
1731 if (!desc)
1733 hr = set_rtv_desc_from_resource(&view->desc, resource);
1735 else
1737 view->desc = *desc;
1738 hr = normalize_rtv_desc(&view->desc, resource);
1740 if (FAILED(hr))
1741 return hr;
1743 wined3d_mutex_lock();
1744 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1746 wined3d_mutex_unlock();
1747 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
1748 return E_FAIL;
1751 wined3d_rendertarget_view_desc_from_d3d11(&wined3d_desc, &view->desc);
1752 if (FAILED(hr = wined3d_rendertarget_view_create(&wined3d_desc, wined3d_resource,
1753 view, &d3d_render_target_view_wined3d_parent_ops, &view->wined3d_view)))
1755 wined3d_mutex_unlock();
1756 WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
1757 return hr;
1760 wined3d_private_store_init(&view->private_store);
1761 wined3d_mutex_unlock();
1762 view->resource = resource;
1763 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
1765 return S_OK;
1768 HRESULT d3d_rendertarget_view_create(struct d3d_device *device, ID3D11Resource *resource,
1769 const D3D11_RENDER_TARGET_VIEW_DESC *desc, struct d3d_rendertarget_view **view)
1771 struct d3d_rendertarget_view *object;
1772 HRESULT hr;
1774 if (!(object = heap_alloc_zero(sizeof(*object))))
1775 return E_OUTOFMEMORY;
1777 if (FAILED(hr = d3d_rendertarget_view_init(object, device, resource, desc)))
1779 WARN("Failed to initialize rendertarget view, hr %#x.\n", hr);
1780 heap_free(object);
1781 return hr;
1784 TRACE("Created rendertarget view %p.\n", object);
1785 *view = object;
1787 return S_OK;
1790 struct d3d_rendertarget_view *unsafe_impl_from_ID3D11RenderTargetView(ID3D11RenderTargetView *iface)
1792 if (!iface)
1793 return NULL;
1794 assert(iface->lpVtbl == &d3d11_rendertarget_view_vtbl);
1796 return impl_from_ID3D11RenderTargetView(iface);
1799 struct d3d_rendertarget_view *unsafe_impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
1801 if (!iface)
1802 return NULL;
1803 assert(iface->lpVtbl == &d3d10_rendertarget_view_vtbl);
1805 return impl_from_ID3D10RenderTargetView(iface);
1808 /* ID3D11ShaderResourceView methods */
1810 static inline struct d3d_shader_resource_view *impl_from_ID3D11ShaderResourceView(ID3D11ShaderResourceView *iface)
1812 return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D11ShaderResourceView_iface);
1815 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_QueryInterface(ID3D11ShaderResourceView *iface,
1816 REFIID riid, void **object)
1818 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1820 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1822 if (IsEqualGUID(riid, &IID_ID3D11ShaderResourceView)
1823 || IsEqualGUID(riid, &IID_ID3D11View)
1824 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
1825 || IsEqualGUID(riid, &IID_IUnknown))
1827 ID3D11ShaderResourceView_AddRef(iface);
1828 *object = iface;
1829 return S_OK;
1832 if (IsEqualGUID(riid, &IID_ID3D10ShaderResourceView1)
1833 || IsEqualGUID(riid, &IID_ID3D10ShaderResourceView)
1834 || IsEqualGUID(riid, &IID_ID3D10View)
1835 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
1837 ID3D10ShaderResourceView1_AddRef(&view->ID3D10ShaderResourceView1_iface);
1838 *object = &view->ID3D10ShaderResourceView1_iface;
1839 return S_OK;
1842 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1844 *object = NULL;
1845 return E_NOINTERFACE;
1848 static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_AddRef(ID3D11ShaderResourceView *iface)
1850 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1851 ULONG refcount = InterlockedIncrement(&view->refcount);
1853 TRACE("%p increasing refcount to %u.\n", view, refcount);
1855 if (refcount == 1)
1857 ID3D11Device2_AddRef(view->device);
1858 wined3d_mutex_lock();
1859 wined3d_shader_resource_view_incref(view->wined3d_view);
1860 wined3d_mutex_unlock();
1863 return refcount;
1866 static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_Release(ID3D11ShaderResourceView *iface)
1868 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1869 ULONG refcount = InterlockedDecrement(&view->refcount);
1871 TRACE("%p decreasing refcount to %u.\n", view, refcount);
1873 if (!refcount)
1875 ID3D11Device2 *device = view->device;
1877 wined3d_mutex_lock();
1878 wined3d_shader_resource_view_decref(view->wined3d_view);
1879 wined3d_mutex_unlock();
1881 ID3D11Device2_Release(device);
1884 return refcount;
1887 static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDevice(ID3D11ShaderResourceView *iface,
1888 ID3D11Device **device)
1890 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1892 TRACE("iface %p, device %p.\n", iface, device);
1894 *device = (ID3D11Device *)view->device;
1895 ID3D11Device_AddRef(*device);
1898 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_GetPrivateData(ID3D11ShaderResourceView *iface,
1899 REFGUID guid, UINT *data_size, void *data)
1901 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1903 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1905 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1908 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_SetPrivateData(ID3D11ShaderResourceView *iface,
1909 REFGUID guid, UINT data_size, const void *data)
1911 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1913 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1915 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1918 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_SetPrivateDataInterface(ID3D11ShaderResourceView *iface,
1919 REFGUID guid, const IUnknown *data)
1921 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1923 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1925 return d3d_set_private_data_interface(&view->private_store, guid, data);
1928 static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetResource(ID3D11ShaderResourceView *iface,
1929 ID3D11Resource **resource)
1931 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1933 TRACE("iface %p, resource %p.\n", iface, resource);
1935 *resource = view->resource;
1936 ID3D11Resource_AddRef(*resource);
1939 static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDesc(ID3D11ShaderResourceView *iface,
1940 D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
1942 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1944 TRACE("iface %p, desc %p.\n", iface, desc);
1946 *desc = view->desc;
1949 static const struct ID3D11ShaderResourceViewVtbl d3d11_shader_resource_view_vtbl =
1951 /* IUnknown methods */
1952 d3d11_shader_resource_view_QueryInterface,
1953 d3d11_shader_resource_view_AddRef,
1954 d3d11_shader_resource_view_Release,
1955 /* ID3D11DeviceChild methods */
1956 d3d11_shader_resource_view_GetDevice,
1957 d3d11_shader_resource_view_GetPrivateData,
1958 d3d11_shader_resource_view_SetPrivateData,
1959 d3d11_shader_resource_view_SetPrivateDataInterface,
1960 /* ID3D11View methods */
1961 d3d11_shader_resource_view_GetResource,
1962 /* ID3D11ShaderResourceView methods */
1963 d3d11_shader_resource_view_GetDesc,
1966 /* ID3D10ShaderResourceView methods */
1968 static inline struct d3d_shader_resource_view *impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView1 *iface)
1970 return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
1973 /* IUnknown methods */
1975 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_QueryInterface(ID3D10ShaderResourceView1 *iface,
1976 REFIID riid, void **object)
1978 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1980 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1982 return d3d11_shader_resource_view_QueryInterface(&view->ID3D11ShaderResourceView_iface, riid, object);
1985 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_AddRef(ID3D10ShaderResourceView1 *iface)
1987 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1989 TRACE("iface %p.\n", iface);
1991 return d3d11_shader_resource_view_AddRef(&view->ID3D11ShaderResourceView_iface);
1994 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_Release(ID3D10ShaderResourceView1 *iface)
1996 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1998 TRACE("iface %p.\n", iface);
2000 return d3d11_shader_resource_view_Release(&view->ID3D11ShaderResourceView_iface);
2003 /* ID3D10DeviceChild methods */
2005 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDevice(ID3D10ShaderResourceView1 *iface,
2006 ID3D10Device **device)
2008 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2010 TRACE("iface %p, device %p.\n", iface, device);
2012 ID3D11Device2_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
2015 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_GetPrivateData(ID3D10ShaderResourceView1 *iface,
2016 REFGUID guid, UINT *data_size, void *data)
2018 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2020 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
2021 iface, debugstr_guid(guid), data_size, data);
2023 return d3d_get_private_data(&view->private_store, guid, data_size, data);
2026 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateData(ID3D10ShaderResourceView1 *iface,
2027 REFGUID guid, UINT data_size, const void *data)
2029 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2031 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
2032 iface, debugstr_guid(guid), data_size, data);
2034 return d3d_set_private_data(&view->private_store, guid, data_size, data);
2037 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateDataInterface(ID3D10ShaderResourceView1 *iface,
2038 REFGUID guid, const IUnknown *data)
2040 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2042 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
2044 return d3d_set_private_data_interface(&view->private_store, guid, data);
2047 /* ID3D10View methods */
2049 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetResource(ID3D10ShaderResourceView1 *iface,
2050 ID3D10Resource **resource)
2052 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2054 TRACE("iface %p, resource %p.\n", iface, resource);
2056 ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
2059 /* ID3D10ShaderResourceView methods */
2061 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc(ID3D10ShaderResourceView1 *iface,
2062 D3D10_SHADER_RESOURCE_VIEW_DESC *desc)
2064 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2066 TRACE("iface %p, desc %p.\n", iface, desc);
2068 memcpy(desc, &view->desc, sizeof(*desc));
2071 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc1(ID3D10ShaderResourceView1 *iface,
2072 D3D10_SHADER_RESOURCE_VIEW_DESC1 *desc)
2074 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2076 TRACE("iface %p, desc %p.\n", iface, desc);
2078 memcpy(desc, &view->desc, sizeof(*desc));
2081 static const struct ID3D10ShaderResourceView1Vtbl d3d10_shader_resource_view_vtbl =
2083 /* IUnknown methods */
2084 d3d10_shader_resource_view_QueryInterface,
2085 d3d10_shader_resource_view_AddRef,
2086 d3d10_shader_resource_view_Release,
2087 /* ID3D10DeviceChild methods */
2088 d3d10_shader_resource_view_GetDevice,
2089 d3d10_shader_resource_view_GetPrivateData,
2090 d3d10_shader_resource_view_SetPrivateData,
2091 d3d10_shader_resource_view_SetPrivateDataInterface,
2092 /* ID3D10View methods */
2093 d3d10_shader_resource_view_GetResource,
2094 /* ID3D10ShaderResourceView methods */
2095 d3d10_shader_resource_view_GetDesc,
2096 /* ID3D10ShaderResourceView1 methods */
2097 d3d10_shader_resource_view_GetDesc1,
2100 static void STDMETHODCALLTYPE d3d_shader_resource_view_wined3d_object_destroyed(void *parent)
2102 struct d3d_shader_resource_view *view = parent;
2104 wined3d_private_store_cleanup(&view->private_store);
2105 heap_free(parent);
2108 static const struct wined3d_parent_ops d3d_shader_resource_view_wined3d_parent_ops =
2110 d3d_shader_resource_view_wined3d_object_destroyed,
2113 static unsigned int wined3d_view_flags_from_d3d11_bufferex_flags(unsigned int d3d11_flags)
2115 unsigned int wined3d_flags = d3d11_flags & WINED3D_VIEW_BUFFER_RAW;
2117 if (d3d11_flags != wined3d_flags)
2118 FIXME("Unhandled flags %#x.\n", d3d11_flags & ~wined3d_flags);
2120 return wined3d_flags;
2123 static HRESULT wined3d_shader_resource_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
2124 const D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
2126 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
2127 wined3d_desc->flags = 0;
2129 switch (desc->ViewDimension)
2131 case D3D11_SRV_DIMENSION_BUFFER:
2132 wined3d_desc->u.buffer.start_idx = desc->u.Buffer.u1.FirstElement;
2133 wined3d_desc->u.buffer.count = desc->u.Buffer.u2.NumElements;
2134 break;
2136 case D3D11_SRV_DIMENSION_TEXTURE1D:
2137 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MostDetailedMip;
2138 wined3d_desc->u.texture.level_count = desc->u.Texture1D.MipLevels;
2139 wined3d_desc->u.texture.layer_idx = 0;
2140 wined3d_desc->u.texture.layer_count = 1;
2141 break;
2143 case D3D11_SRV_DIMENSION_TEXTURE1DARRAY:
2144 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2145 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MostDetailedMip;
2146 wined3d_desc->u.texture.level_count = desc->u.Texture1DArray.MipLevels;
2147 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
2148 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
2149 break;
2151 case D3D11_SRV_DIMENSION_TEXTURE2D:
2152 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MostDetailedMip;
2153 wined3d_desc->u.texture.level_count = desc->u.Texture2D.MipLevels;
2154 wined3d_desc->u.texture.layer_idx = 0;
2155 wined3d_desc->u.texture.layer_count = 1;
2156 break;
2158 case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
2159 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2160 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MostDetailedMip;
2161 wined3d_desc->u.texture.level_count = desc->u.Texture2DArray.MipLevels;
2162 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
2163 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
2164 break;
2166 case D3D11_SRV_DIMENSION_TEXTURE2DMS:
2167 wined3d_desc->u.texture.level_idx = 0;
2168 wined3d_desc->u.texture.level_count = 1;
2169 wined3d_desc->u.texture.layer_idx = 0;
2170 wined3d_desc->u.texture.layer_count = 1;
2171 break;
2173 case D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY:
2174 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2175 wined3d_desc->u.texture.level_idx = 0;
2176 wined3d_desc->u.texture.level_count = 1;
2177 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
2178 wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
2179 break;
2181 case D3D11_SRV_DIMENSION_TEXTURE3D:
2182 wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MostDetailedMip;
2183 wined3d_desc->u.texture.level_count = desc->u.Texture3D.MipLevels;
2184 wined3d_desc->u.texture.layer_idx = 0;
2185 wined3d_desc->u.texture.layer_count = 1;
2186 break;
2188 case D3D11_SRV_DIMENSION_TEXTURECUBE:
2189 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_CUBE;
2190 wined3d_desc->u.texture.level_idx = desc->u.TextureCube.MostDetailedMip;
2191 wined3d_desc->u.texture.level_count = desc->u.TextureCube.MipLevels;
2192 wined3d_desc->u.texture.layer_idx = 0;
2193 wined3d_desc->u.texture.layer_count = 6;
2194 break;
2196 case D3D11_SRV_DIMENSION_TEXTURECUBEARRAY:
2197 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY;
2198 wined3d_desc->u.texture.level_idx = desc->u.TextureCubeArray.MostDetailedMip;
2199 wined3d_desc->u.texture.level_count = desc->u.TextureCubeArray.MipLevels;
2200 wined3d_desc->u.texture.layer_idx = desc->u.TextureCubeArray.First2DArrayFace;
2201 wined3d_desc->u.texture.layer_count = 6 * desc->u.TextureCubeArray.NumCubes;
2202 break;
2204 case D3D11_SRV_DIMENSION_BUFFEREX:
2205 wined3d_desc->flags = wined3d_view_flags_from_d3d11_bufferex_flags(desc->u.BufferEx.Flags);
2206 wined3d_desc->u.buffer.start_idx = desc->u.BufferEx.FirstElement;
2207 wined3d_desc->u.buffer.count = desc->u.BufferEx.NumElements;
2208 break;
2210 default:
2211 WARN("Unrecognized view dimension %#x.\n", desc->ViewDimension);
2212 return E_FAIL;
2215 return S_OK;
2218 static HRESULT d3d_shader_resource_view_init(struct d3d_shader_resource_view *view, struct d3d_device *device,
2219 ID3D11Resource *resource, const D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
2221 struct wined3d_resource *wined3d_resource;
2222 struct wined3d_view_desc wined3d_desc;
2223 HRESULT hr;
2225 view->ID3D11ShaderResourceView_iface.lpVtbl = &d3d11_shader_resource_view_vtbl;
2226 view->ID3D10ShaderResourceView1_iface.lpVtbl = &d3d10_shader_resource_view_vtbl;
2227 view->refcount = 1;
2229 if (!desc)
2231 hr = set_srv_desc_from_resource(&view->desc, resource);
2233 else
2235 view->desc = *desc;
2236 hr = normalize_srv_desc(&view->desc, resource);
2238 if (FAILED(hr))
2239 return hr;
2241 if (FAILED(hr = wined3d_shader_resource_view_desc_from_d3d11(&wined3d_desc, &view->desc)))
2242 return hr;
2244 wined3d_mutex_lock();
2245 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
2247 wined3d_mutex_unlock();
2248 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
2249 return E_FAIL;
2252 if (FAILED(hr = wined3d_shader_resource_view_create(&wined3d_desc, wined3d_resource,
2253 view, &d3d_shader_resource_view_wined3d_parent_ops, &view->wined3d_view)))
2255 wined3d_mutex_unlock();
2256 WARN("Failed to create wined3d shader resource view, hr %#x.\n", hr);
2257 return hr;
2260 wined3d_private_store_init(&view->private_store);
2261 wined3d_mutex_unlock();
2262 view->resource = resource;
2263 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
2265 return S_OK;
2268 HRESULT d3d_shader_resource_view_create(struct d3d_device *device, ID3D11Resource *resource,
2269 const D3D11_SHADER_RESOURCE_VIEW_DESC *desc, struct d3d_shader_resource_view **view)
2271 struct d3d_shader_resource_view *object;
2272 HRESULT hr;
2274 if (!(object = heap_alloc_zero(sizeof(*object))))
2275 return E_OUTOFMEMORY;
2277 if (FAILED(hr = d3d_shader_resource_view_init(object, device, resource, desc)))
2279 WARN("Failed to initialize shader resource view, hr %#x.\n", hr);
2280 heap_free(object);
2281 return hr;
2284 TRACE("Created shader resource view %p.\n", object);
2285 *view = object;
2287 return S_OK;
2290 struct d3d_shader_resource_view *unsafe_impl_from_ID3D11ShaderResourceView(ID3D11ShaderResourceView *iface)
2292 if (!iface)
2293 return NULL;
2294 assert(iface->lpVtbl == &d3d11_shader_resource_view_vtbl);
2295 return impl_from_ID3D11ShaderResourceView(iface);
2298 struct d3d_shader_resource_view *unsafe_impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView *iface)
2300 if (!iface)
2301 return NULL;
2302 assert(iface->lpVtbl == (ID3D10ShaderResourceViewVtbl *)&d3d10_shader_resource_view_vtbl);
2303 return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
2306 /* ID3D11UnorderedAccessView methods */
2308 static inline struct d3d11_unordered_access_view *impl_from_ID3D11UnorderedAccessView(ID3D11UnorderedAccessView *iface)
2310 return CONTAINING_RECORD(iface, struct d3d11_unordered_access_view, ID3D11UnorderedAccessView_iface);
2313 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_QueryInterface(ID3D11UnorderedAccessView *iface,
2314 REFIID riid, void **object)
2316 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
2318 if (IsEqualGUID(riid, &IID_ID3D11UnorderedAccessView)
2319 || IsEqualGUID(riid, &IID_ID3D11View)
2320 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
2321 || IsEqualGUID(riid, &IID_IUnknown))
2323 ID3D11UnorderedAccessView_AddRef(*object = iface);
2324 return S_OK;
2327 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
2329 *object = NULL;
2330 return E_NOINTERFACE;
2333 static ULONG STDMETHODCALLTYPE d3d11_unordered_access_view_AddRef(ID3D11UnorderedAccessView *iface)
2335 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2336 ULONG refcount = InterlockedIncrement(&view->refcount);
2338 TRACE("%p increasing refcount to %u.\n", view, refcount);
2340 if (refcount == 1)
2342 ID3D11Device2_AddRef(view->device);
2343 wined3d_mutex_lock();
2344 wined3d_unordered_access_view_incref(view->wined3d_view);
2345 wined3d_mutex_unlock();
2348 return refcount;
2351 static ULONG STDMETHODCALLTYPE d3d11_unordered_access_view_Release(ID3D11UnorderedAccessView *iface)
2353 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2354 ULONG refcount = InterlockedDecrement(&view->refcount);
2356 TRACE("%p decreasing refcount to %u.\n", view, refcount);
2358 if (!refcount)
2360 ID3D11Device2 *device = view->device;
2362 wined3d_mutex_lock();
2363 wined3d_unordered_access_view_decref(view->wined3d_view);
2364 wined3d_mutex_unlock();
2366 ID3D11Device2_Release(device);
2369 return refcount;
2372 static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetDevice(ID3D11UnorderedAccessView *iface,
2373 ID3D11Device **device)
2375 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2377 TRACE("iface %p, device %p.\n", iface, device);
2379 ID3D11Device_AddRef(*device = (ID3D11Device *)view->device);
2382 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_GetPrivateData(ID3D11UnorderedAccessView *iface,
2383 REFGUID guid, UINT *data_size, void *data)
2385 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2387 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
2389 return d3d_get_private_data(&view->private_store, guid, data_size, data);
2392 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_SetPrivateData(ID3D11UnorderedAccessView *iface,
2393 REFGUID guid, UINT data_size, const void *data)
2395 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2397 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
2399 return d3d_set_private_data(&view->private_store, guid, data_size, data);
2402 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_SetPrivateDataInterface(ID3D11UnorderedAccessView *iface,
2403 REFGUID guid, const IUnknown *data)
2405 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2407 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
2409 return d3d_set_private_data_interface(&view->private_store, guid, data);
2412 static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetResource(ID3D11UnorderedAccessView *iface,
2413 ID3D11Resource **resource)
2415 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2417 TRACE("iface %p, resource %p.\n", iface, resource);
2419 ID3D11Resource_AddRef(*resource = view->resource);
2422 static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetDesc(ID3D11UnorderedAccessView *iface,
2423 D3D11_UNORDERED_ACCESS_VIEW_DESC *desc)
2425 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2427 TRACE("iface %p, desc %p.\n", iface, desc);
2429 *desc = view->desc;
2432 static const struct ID3D11UnorderedAccessViewVtbl d3d11_unordered_access_view_vtbl =
2434 /* IUnknown methods */
2435 d3d11_unordered_access_view_QueryInterface,
2436 d3d11_unordered_access_view_AddRef,
2437 d3d11_unordered_access_view_Release,
2438 /* ID3D11DeviceChild methods */
2439 d3d11_unordered_access_view_GetDevice,
2440 d3d11_unordered_access_view_GetPrivateData,
2441 d3d11_unordered_access_view_SetPrivateData,
2442 d3d11_unordered_access_view_SetPrivateDataInterface,
2443 /* ID3D11View methods */
2444 d3d11_unordered_access_view_GetResource,
2445 /* ID3D11UnorderedAccessView methods */
2446 d3d11_unordered_access_view_GetDesc,
2449 static void STDMETHODCALLTYPE d3d11_unordered_access_view_wined3d_object_destroyed(void *parent)
2451 struct d3d11_unordered_access_view *view = parent;
2453 wined3d_private_store_cleanup(&view->private_store);
2454 heap_free(parent);
2457 static const struct wined3d_parent_ops d3d11_unordered_access_view_wined3d_parent_ops =
2459 d3d11_unordered_access_view_wined3d_object_destroyed,
2462 static unsigned int wined3d_view_flags_from_d3d11_buffer_uav_flags(unsigned int d3d11_flags)
2464 unsigned int wined3d_flags = d3d11_flags & (WINED3D_VIEW_BUFFER_RAW
2465 | WINED3D_VIEW_BUFFER_APPEND | WINED3D_VIEW_BUFFER_COUNTER);
2467 if (d3d11_flags != wined3d_flags)
2468 FIXME("Unhandled flags %#x.\n", d3d11_flags & ~wined3d_flags);
2470 return wined3d_flags;
2473 static HRESULT wined3d_unordered_access_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
2474 const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc)
2476 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
2478 wined3d_desc->flags = 0;
2479 wined3d_desc->u.texture.level_count = 1;
2480 switch (desc->ViewDimension)
2482 case D3D11_UAV_DIMENSION_BUFFER:
2483 wined3d_desc->flags = wined3d_view_flags_from_d3d11_buffer_uav_flags(desc->u.Buffer.Flags);
2484 wined3d_desc->u.buffer.start_idx = desc->u.Buffer.FirstElement;
2485 wined3d_desc->u.buffer.count = desc->u.Buffer.NumElements;
2486 break;
2488 case D3D11_UAV_DIMENSION_TEXTURE1D:
2489 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
2490 wined3d_desc->u.texture.layer_idx = 0;
2491 wined3d_desc->u.texture.layer_count = 1;
2492 break;
2494 case D3D11_UAV_DIMENSION_TEXTURE1DARRAY:
2495 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2496 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
2497 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
2498 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
2499 break;
2501 case D3D11_UAV_DIMENSION_TEXTURE2D:
2502 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
2503 wined3d_desc->u.texture.layer_idx = 0;
2504 wined3d_desc->u.texture.layer_count = 1;
2505 break;
2507 case D3D11_UAV_DIMENSION_TEXTURE2DARRAY:
2508 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2509 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
2510 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
2511 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
2512 break;
2514 case D3D11_UAV_DIMENSION_TEXTURE3D:
2515 wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MipSlice;
2516 wined3d_desc->u.texture.layer_idx = desc->u.Texture3D.FirstWSlice;
2517 wined3d_desc->u.texture.layer_count = desc->u.Texture3D.WSize;
2518 break;
2520 default:
2521 WARN("Unrecognized view dimension %#x.\n", desc->ViewDimension);
2522 return E_FAIL;
2525 return S_OK;
2528 static HRESULT d3d11_unordered_access_view_init(struct d3d11_unordered_access_view *view,
2529 struct d3d_device *device, ID3D11Resource *resource, const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc)
2531 struct wined3d_resource *wined3d_resource;
2532 struct wined3d_view_desc wined3d_desc;
2533 HRESULT hr;
2535 view->ID3D11UnorderedAccessView_iface.lpVtbl = &d3d11_unordered_access_view_vtbl;
2536 view->refcount = 1;
2538 if (!desc)
2540 hr = set_uav_desc_from_resource(&view->desc, resource);
2542 else
2544 view->desc = *desc;
2545 hr = normalize_uav_desc(&view->desc, resource);
2547 if (FAILED(hr))
2548 return hr;
2550 if (FAILED(hr = wined3d_unordered_access_view_desc_from_d3d11(&wined3d_desc, &view->desc)))
2551 return hr;
2553 wined3d_mutex_lock();
2554 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
2556 wined3d_mutex_unlock();
2557 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
2558 return E_FAIL;
2561 if (FAILED(hr = wined3d_unordered_access_view_create(&wined3d_desc, wined3d_resource,
2562 view, &d3d11_unordered_access_view_wined3d_parent_ops, &view->wined3d_view)))
2564 wined3d_mutex_unlock();
2565 WARN("Failed to create wined3d unordered access view, hr %#x.\n", hr);
2566 return hr;
2569 wined3d_private_store_init(&view->private_store);
2570 wined3d_mutex_unlock();
2571 view->resource = resource;
2572 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
2574 return S_OK;
2577 HRESULT d3d11_unordered_access_view_create(struct d3d_device *device, ID3D11Resource *resource,
2578 const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, struct d3d11_unordered_access_view **view)
2580 struct d3d11_unordered_access_view *object;
2581 HRESULT hr;
2583 if (!(object = heap_alloc_zero(sizeof(*object))))
2584 return E_OUTOFMEMORY;
2586 if (FAILED(hr = d3d11_unordered_access_view_init(object, device, resource, desc)))
2588 WARN("Failed to initialize unordered access view, hr %#x.\n", hr);
2589 heap_free(object);
2590 return hr;
2593 TRACE("Created unordered access view %p.\n", object);
2594 *view = object;
2596 return S_OK;
2599 struct d3d11_unordered_access_view *unsafe_impl_from_ID3D11UnorderedAccessView(ID3D11UnorderedAccessView *iface)
2601 if (!iface)
2602 return NULL;
2603 assert(iface->lpVtbl == &d3d11_unordered_access_view_vtbl);
2605 return impl_from_ID3D11UnorderedAccessView(iface);