ntdll: Allow NtQueryVolumeInformationFile to make async volume information queries.
[wine/zf.git] / dlls / d3d11 / view.c
blob4393d6540e3cc98f25e19aed7f9e71bfa62bc02c
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 #include "d3d11_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
25 static HRESULT get_resource_properties(ID3D11Resource *resource, D3D11_RESOURCE_DIMENSION *dimension,
26 DXGI_FORMAT *format, unsigned int *miplevel_count, unsigned int *layer_count)
28 ID3D11Resource_GetType(resource, dimension);
29 switch (*dimension)
31 case D3D11_RESOURCE_DIMENSION_BUFFER:
32 return S_OK;
34 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
36 const struct d3d_texture1d *texture;
38 if (!(texture = unsafe_impl_from_ID3D11Texture1D((ID3D11Texture1D *)resource)))
40 ERR("Cannot get implementation from ID3D11Texture1D.\n");
41 return E_FAIL;
44 *format = texture->desc.Format;
45 if (miplevel_count)
46 *miplevel_count = texture->desc.MipLevels;
47 *layer_count = texture->desc.ArraySize;
48 break;
51 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
53 const struct d3d_texture2d *texture;
55 if (!(texture = unsafe_impl_from_ID3D11Texture2D((ID3D11Texture2D *)resource)))
57 ERR("Cannot get implementation from ID3D11Texture2D.\n");
58 return E_FAIL;
61 *format = texture->desc.Format;
62 if (miplevel_count)
63 *miplevel_count = texture->desc.MipLevels;
64 *layer_count = texture->desc.ArraySize;
65 break;
68 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
70 const struct d3d_texture3d *texture;
72 if (!(texture = unsafe_impl_from_ID3D11Texture3D((ID3D11Texture3D *)resource)))
74 ERR("Cannot get implementation from ID3D11Texture3D.\n");
75 return E_FAIL;
78 *format = texture->desc.Format;
79 if (miplevel_count)
80 *miplevel_count = texture->desc.MipLevels;
81 *layer_count = texture->desc.Depth;
82 break;
85 default:
86 WARN("Invalid resource dimension %#x.\n", *dimension);
87 return E_INVALIDARG;
90 return S_OK;
93 static HRESULT set_dsv_desc_from_resource(D3D11_DEPTH_STENCIL_VIEW_DESC *desc, ID3D11Resource *resource)
95 D3D11_RESOURCE_DIMENSION dimension;
97 ID3D11Resource_GetType(resource, &dimension);
99 desc->Flags = 0;
101 switch (dimension)
103 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
105 D3D11_TEXTURE1D_DESC texture_desc;
106 ID3D11Texture1D *texture;
108 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture)))
110 ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D.\n");
111 return E_INVALIDARG;
114 ID3D11Texture1D_GetDesc(texture, &texture_desc);
115 ID3D11Texture1D_Release(texture);
117 desc->Format = texture_desc.Format;
118 if (texture_desc.ArraySize == 1)
120 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1D;
121 desc->u.Texture1D.MipSlice = 0;
123 else
125 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1DARRAY;
126 desc->u.Texture1DArray.MipSlice = 0;
127 desc->u.Texture1DArray.FirstArraySlice = 0;
128 desc->u.Texture1DArray.ArraySize = texture_desc.ArraySize;
131 return S_OK;
134 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
136 D3D11_TEXTURE2D_DESC texture_desc;
137 ID3D11Texture2D *texture;
139 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
141 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
142 return E_INVALIDARG;
145 ID3D11Texture2D_GetDesc(texture, &texture_desc);
146 ID3D11Texture2D_Release(texture);
148 desc->Format = texture_desc.Format;
149 if (texture_desc.ArraySize == 1)
151 if (texture_desc.SampleDesc.Count == 1)
153 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
154 desc->u.Texture2D.MipSlice = 0;
156 else
158 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
161 else
163 if (texture_desc.SampleDesc.Count == 1)
165 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
166 desc->u.Texture2DArray.MipSlice = 0;
167 desc->u.Texture2DArray.FirstArraySlice = 0;
168 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
170 else
172 desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY;
173 desc->u.Texture2DMSArray.FirstArraySlice = 0;
174 desc->u.Texture2DMSArray.ArraySize = texture_desc.ArraySize;
178 return S_OK;
181 case D3D11_RESOURCE_DIMENSION_BUFFER:
182 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
183 default:
184 WARN("Invalid resource dimension %#x.\n", dimension);
185 return E_INVALIDARG;
189 static HRESULT normalize_dsv_desc(D3D11_DEPTH_STENCIL_VIEW_DESC *desc, ID3D11Resource *resource)
191 D3D11_RESOURCE_DIMENSION dimension;
192 unsigned int layer_count;
193 DXGI_FORMAT format;
194 HRESULT hr;
196 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, NULL, &layer_count)))
197 return hr;
198 switch (dimension)
200 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
201 if (desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE1D
202 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE1DARRAY)
204 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
205 return E_INVALIDARG;
207 break;
209 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
210 if (desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2D
211 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2DARRAY
212 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2DMS
213 && desc->ViewDimension != D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY)
215 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
216 return E_INVALIDARG;
218 break;
220 case D3D11_RESOURCE_DIMENSION_BUFFER:
221 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
222 default:
223 WARN("Invalid resource dimension %#x.\n", dimension);
224 return E_INVALIDARG;
227 if (desc->Format == DXGI_FORMAT_UNKNOWN)
228 desc->Format = format;
230 switch (desc->ViewDimension)
232 case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
233 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < layer_count)
234 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
235 break;
237 case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
238 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
239 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
240 break;
242 case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
243 if (desc->u.Texture2DMSArray.ArraySize == ~0u && desc->u.Texture2DMSArray.FirstArraySlice < layer_count)
244 desc->u.Texture2DMSArray.ArraySize = layer_count - desc->u.Texture2DMSArray.FirstArraySlice;
245 break;
247 default:
248 break;
251 return S_OK;
254 static HRESULT set_rtv_desc_from_resource(D3D11_RENDER_TARGET_VIEW_DESC *desc, ID3D11Resource *resource)
256 D3D11_RESOURCE_DIMENSION dimension;
257 HRESULT hr;
259 ID3D11Resource_GetType(resource, &dimension);
261 switch (dimension)
263 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
265 ID3D11Texture1D *texture;
266 D3D11_TEXTURE1D_DESC texture_desc;
268 hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture);
269 if (FAILED(hr))
271 ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D?\n");
272 return E_INVALIDARG;
275 ID3D11Texture1D_GetDesc(texture, &texture_desc);
276 ID3D11Texture1D_Release(texture);
278 desc->Format = texture_desc.Format;
279 if (texture_desc.ArraySize == 1)
281 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1D;
282 desc->u.Texture1D.MipSlice = 0;
284 else
286 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1DARRAY;
287 desc->u.Texture1DArray.MipSlice = 0;
288 desc->u.Texture1DArray.FirstArraySlice = 0;
289 desc->u.Texture1DArray.ArraySize = texture_desc.ArraySize;
292 return S_OK;
295 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
297 ID3D11Texture2D *texture;
298 D3D11_TEXTURE2D_DESC texture_desc;
300 hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture);
301 if (FAILED(hr))
303 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D?\n");
304 return E_INVALIDARG;
307 ID3D11Texture2D_GetDesc(texture, &texture_desc);
308 ID3D11Texture2D_Release(texture);
310 desc->Format = texture_desc.Format;
311 if (texture_desc.ArraySize == 1)
313 if (texture_desc.SampleDesc.Count == 1)
315 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
316 desc->u.Texture2D.MipSlice = 0;
318 else
320 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
323 else
325 if (texture_desc.SampleDesc.Count == 1)
327 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
328 desc->u.Texture2DArray.MipSlice = 0;
329 desc->u.Texture2DArray.FirstArraySlice = 0;
330 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
332 else
334 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY;
335 desc->u.Texture2DMSArray.FirstArraySlice = 0;
336 desc->u.Texture2DMSArray.ArraySize = texture_desc.ArraySize;
340 return S_OK;
343 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
345 ID3D11Texture3D *texture;
346 D3D11_TEXTURE3D_DESC texture_desc;
348 hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture);
349 if (FAILED(hr))
351 ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D?\n");
352 return E_INVALIDARG;
355 ID3D11Texture3D_GetDesc(texture, &texture_desc);
356 ID3D11Texture3D_Release(texture);
358 desc->Format = texture_desc.Format;
359 desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
360 desc->u.Texture3D.MipSlice = 0;
361 desc->u.Texture3D.FirstWSlice = 0;
362 desc->u.Texture3D.WSize = texture_desc.Depth;
364 return S_OK;
367 default:
368 FIXME("Unhandled resource dimension %#x.\n", dimension);
369 return E_INVALIDARG;
373 static HRESULT normalize_rtv_desc(D3D11_RENDER_TARGET_VIEW_DESC *desc, ID3D11Resource *resource)
375 D3D11_RESOURCE_DIMENSION dimension;
376 unsigned int layer_count;
377 DXGI_FORMAT format;
378 HRESULT hr;
380 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, NULL, &layer_count)))
381 return hr;
382 switch (dimension)
384 case D3D11_RESOURCE_DIMENSION_BUFFER:
385 if (desc->ViewDimension != D3D11_RTV_DIMENSION_BUFFER)
387 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
388 return E_INVALIDARG;
390 return S_OK;
392 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
393 if (desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE1D
394 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE1DARRAY)
396 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
397 return E_INVALIDARG;
399 break;
401 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
402 if (desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2D
403 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2DARRAY
404 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2DMS
405 && desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY)
407 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
408 return E_INVALIDARG;
410 break;
412 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
413 if (desc->ViewDimension != D3D11_RTV_DIMENSION_TEXTURE3D)
415 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
416 return E_INVALIDARG;
418 break;
420 default:
421 WARN("Invalid resource dimension %#x.\n", dimension);
422 return E_INVALIDARG;
425 if (desc->Format == DXGI_FORMAT_UNKNOWN)
426 desc->Format = format;
428 switch (desc->ViewDimension)
430 case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
431 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < layer_count)
432 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
433 break;
435 case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
436 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
437 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
438 break;
440 case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
441 if (desc->u.Texture2DMSArray.ArraySize == ~0u && desc->u.Texture2DMSArray.FirstArraySlice < layer_count)
442 desc->u.Texture2DMSArray.ArraySize = layer_count - desc->u.Texture2DMSArray.FirstArraySlice;
443 break;
445 case D3D11_RTV_DIMENSION_TEXTURE3D:
446 layer_count = max(1, layer_count >> desc->u.Texture3D.MipSlice);
447 if (desc->u.Texture3D.WSize == ~0u && desc->u.Texture3D.FirstWSlice < layer_count)
448 desc->u.Texture3D.WSize = layer_count - desc->u.Texture3D.FirstWSlice;
449 break;
451 default:
452 break;
455 return S_OK;
458 static HRESULT set_srv_desc_from_resource(D3D11_SHADER_RESOURCE_VIEW_DESC *desc, ID3D11Resource *resource)
460 D3D11_RESOURCE_DIMENSION dimension;
462 ID3D11Resource_GetType(resource, &dimension);
464 switch (dimension)
466 case D3D11_RESOURCE_DIMENSION_BUFFER:
468 D3D11_BUFFER_DESC buffer_desc;
469 ID3D11Buffer *buffer;
471 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Buffer, (void **)&buffer)))
473 ERR("Resource of type BUFFER doesn't implement ID3D11Buffer.\n");
474 return E_INVALIDARG;
477 ID3D11Buffer_GetDesc(buffer, &buffer_desc);
478 ID3D11Buffer_Release(buffer);
480 if (buffer_desc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
482 desc->Format = DXGI_FORMAT_UNKNOWN;
483 desc->ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
484 desc->u.Buffer.u1.FirstElement = 0;
485 desc->u.Buffer.u2.NumElements = buffer_desc.ByteWidth / buffer_desc.StructureByteStride;
486 return S_OK;
489 return E_INVALIDARG;
492 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
494 D3D11_TEXTURE1D_DESC texture_desc;
495 ID3D11Texture1D *texture;
497 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture)))
499 ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D.\n");
500 return E_INVALIDARG;
503 ID3D11Texture1D_GetDesc(texture, &texture_desc);
504 ID3D11Texture1D_Release(texture);
506 desc->Format = texture_desc.Format;
507 if (texture_desc.ArraySize == 1)
509 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
510 desc->u.Texture1D.MostDetailedMip = 0;
511 desc->u.Texture1D.MipLevels = texture_desc.MipLevels;
513 else
515 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY;
516 desc->u.Texture1DArray.MostDetailedMip = 0;
517 desc->u.Texture1DArray.MipLevels = texture_desc.MipLevels;
518 desc->u.Texture1DArray.FirstArraySlice = 0;
519 desc->u.Texture1DArray.ArraySize = texture_desc.ArraySize;
522 return S_OK;
525 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
527 D3D11_TEXTURE2D_DESC texture_desc;
528 ID3D11Texture2D *texture;
530 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
532 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
533 return E_INVALIDARG;
536 ID3D11Texture2D_GetDesc(texture, &texture_desc);
537 ID3D11Texture2D_Release(texture);
539 desc->Format = texture_desc.Format;
540 if (texture_desc.MiscFlags & D3D11_RESOURCE_MISC_TEXTURECUBE)
542 if (texture_desc.ArraySize >= 12)
544 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY;
545 desc->u.TextureCubeArray.MostDetailedMip = 0;
546 desc->u.TextureCubeArray.MipLevels = texture_desc.MipLevels;
547 desc->u.TextureCubeArray.First2DArrayFace = 0;
548 desc->u.TextureCubeArray.NumCubes = texture_desc.ArraySize / 6;
550 else
552 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
553 desc->u.TextureCube.MostDetailedMip = 0;
554 desc->u.TextureCube.MipLevels = texture_desc.MipLevels;
557 else if (texture_desc.ArraySize == 1)
559 if (texture_desc.SampleDesc.Count == 1)
561 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
562 desc->u.Texture2D.MostDetailedMip = 0;
563 desc->u.Texture2D.MipLevels = texture_desc.MipLevels;
565 else
567 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
570 else
572 if (texture_desc.SampleDesc.Count == 1)
574 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
575 desc->u.Texture2DArray.MostDetailedMip = 0;
576 desc->u.Texture2DArray.MipLevels = texture_desc.MipLevels;
577 desc->u.Texture2DArray.FirstArraySlice = 0;
578 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
580 else
582 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY;
583 desc->u.Texture2DMSArray.FirstArraySlice = 0;
584 desc->u.Texture2DMSArray.ArraySize = texture_desc.ArraySize;
588 return S_OK;
591 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
593 D3D11_TEXTURE3D_DESC texture_desc;
594 ID3D11Texture3D *texture;
596 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture)))
598 ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D.\n");
599 return E_INVALIDARG;
602 ID3D11Texture3D_GetDesc(texture, &texture_desc);
603 ID3D11Texture3D_Release(texture);
605 desc->Format = texture_desc.Format;
606 desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
607 desc->u.Texture3D.MostDetailedMip = 0;
608 desc->u.Texture3D.MipLevels = texture_desc.MipLevels;
610 return S_OK;
613 default:
614 WARN("Invalid resource dimension %#x.\n", dimension);
615 return E_INVALIDARG;
619 static HRESULT normalize_srv_desc(D3D11_SHADER_RESOURCE_VIEW_DESC *desc, ID3D11Resource *resource)
621 unsigned int miplevel_count, layer_count;
622 D3D11_RESOURCE_DIMENSION dimension;
623 DXGI_FORMAT format;
624 HRESULT hr;
626 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, &miplevel_count, &layer_count)))
627 return hr;
628 switch (dimension)
630 case D3D11_RESOURCE_DIMENSION_BUFFER:
631 if (desc->ViewDimension != D3D11_SRV_DIMENSION_BUFFER
632 && desc->ViewDimension != D3D11_SRV_DIMENSION_BUFFEREX)
634 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
635 return E_INVALIDARG;
637 if (!desc->u.Buffer.u2.NumElements)
639 WARN("Zero sized buffer view.\n");
640 return E_INVALIDARG;
642 return S_OK;
644 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
645 if (desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE1D
646 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE1DARRAY)
648 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
649 return E_INVALIDARG;
651 break;
653 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
654 if (desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2D
655 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2DARRAY
656 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2DMS
657 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY
658 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURECUBE
659 && desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURECUBEARRAY)
661 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
662 return E_INVALIDARG;
664 break;
666 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
667 if (desc->ViewDimension != D3D11_SRV_DIMENSION_TEXTURE3D)
669 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
670 return E_INVALIDARG;
672 break;
674 default:
675 WARN("Invalid resource dimension %#x.\n", dimension);
676 return E_INVALIDARG;
679 if (desc->Format == DXGI_FORMAT_UNKNOWN)
680 desc->Format = format;
682 switch (desc->ViewDimension)
684 case D3D11_SRV_DIMENSION_TEXTURE1D:
685 if (desc->u.Texture1D.MipLevels == ~0u && desc->u.Texture1D.MostDetailedMip < miplevel_count)
686 desc->u.Texture1D.MipLevels = miplevel_count - desc->u.Texture1D.MostDetailedMip;
687 break;
689 case D3D11_SRV_DIMENSION_TEXTURE1DARRAY:
690 if (desc->u.Texture1DArray.MipLevels == ~0u && desc->u.Texture1DArray.MostDetailedMip < miplevel_count)
691 desc->u.Texture1DArray.MipLevels = miplevel_count - desc->u.Texture1DArray.MostDetailedMip;
692 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < miplevel_count)
693 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
694 break;
696 case D3D11_SRV_DIMENSION_TEXTURE2D:
697 if (desc->u.Texture2D.MipLevels == ~0u && desc->u.Texture2D.MostDetailedMip < miplevel_count)
698 desc->u.Texture2D.MipLevels = miplevel_count - desc->u.Texture2D.MostDetailedMip;
699 break;
701 case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
702 if (desc->u.Texture2DArray.MipLevels == ~0u && desc->u.Texture2DArray.MostDetailedMip < miplevel_count)
703 desc->u.Texture2DArray.MipLevels = miplevel_count - desc->u.Texture2DArray.MostDetailedMip;
704 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
705 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
706 break;
708 case D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY:
709 if (desc->u.Texture2DMSArray.ArraySize == ~0u && desc->u.Texture2DMSArray.FirstArraySlice < layer_count)
710 desc->u.Texture2DMSArray.ArraySize = layer_count - desc->u.Texture2DMSArray.FirstArraySlice;
711 break;
713 case D3D11_SRV_DIMENSION_TEXTURE3D:
714 if (desc->u.Texture3D.MipLevels == ~0u && desc->u.Texture3D.MostDetailedMip < miplevel_count)
715 desc->u.Texture3D.MipLevels = miplevel_count - desc->u.Texture3D.MostDetailedMip;
716 break;
718 case D3D11_SRV_DIMENSION_TEXTURECUBE:
719 if (desc->u.TextureCube.MipLevels == ~0u && desc->u.TextureCube.MostDetailedMip < miplevel_count)
720 desc->u.TextureCube.MipLevels = miplevel_count - desc->u.TextureCube.MostDetailedMip;
721 break;
723 case D3D11_SRV_DIMENSION_TEXTURECUBEARRAY:
724 if (desc->u.TextureCubeArray.MipLevels == ~0u && desc->u.TextureCubeArray.MostDetailedMip < miplevel_count)
725 desc->u.TextureCubeArray.MipLevels = miplevel_count - desc->u.TextureCubeArray.MostDetailedMip;
726 if (desc->u.TextureCubeArray.NumCubes == ~0u && desc->u.TextureCubeArray.First2DArrayFace < layer_count)
727 desc->u.TextureCubeArray.NumCubes = (layer_count - desc->u.TextureCubeArray.First2DArrayFace) / 6;
728 break;
730 default:
731 break;
734 return S_OK;
737 static HRESULT set_uav_desc_from_resource(D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, ID3D11Resource *resource)
739 D3D11_RESOURCE_DIMENSION dimension;
741 ID3D11Resource_GetType(resource, &dimension);
743 switch (dimension)
745 case D3D11_RESOURCE_DIMENSION_BUFFER:
747 D3D11_BUFFER_DESC buffer_desc;
748 ID3D11Buffer *buffer;
750 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Buffer, (void **)&buffer)))
752 ERR("Resource of type BUFFER doesn't implement ID3D11Buffer.\n");
753 return E_INVALIDARG;
756 ID3D11Buffer_GetDesc(buffer, &buffer_desc);
757 ID3D11Buffer_Release(buffer);
759 if (buffer_desc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
761 desc->Format = DXGI_FORMAT_UNKNOWN;
762 desc->ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
763 desc->u.Buffer.FirstElement = 0;
764 desc->u.Buffer.NumElements = buffer_desc.ByteWidth / buffer_desc.StructureByteStride;
765 desc->u.Buffer.Flags = 0;
766 return S_OK;
769 return E_INVALIDARG;
772 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
774 D3D11_TEXTURE2D_DESC texture_desc;
775 ID3D11Texture2D *texture;
777 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
779 ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
780 return E_INVALIDARG;
783 ID3D11Texture2D_GetDesc(texture, &texture_desc);
784 ID3D11Texture2D_Release(texture);
786 if (texture_desc.SampleDesc.Count != 1)
788 WARN("Trying to create view for multisample texture.\n");
789 return E_INVALIDARG;
792 desc->Format = texture_desc.Format;
793 if (texture_desc.ArraySize == 1)
795 desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
796 desc->u.Texture2D.MipSlice = 0;
798 else
800 desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2DARRAY;
801 desc->u.Texture2DArray.MipSlice = 0;
802 desc->u.Texture2DArray.FirstArraySlice = 0;
803 desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
806 return S_OK;
809 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
811 D3D11_TEXTURE3D_DESC texture_desc;
812 ID3D11Texture3D *texture;
814 if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture)))
816 ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D.\n");
817 return E_INVALIDARG;
820 ID3D11Texture3D_GetDesc(texture, &texture_desc);
821 ID3D11Texture3D_Release(texture);
823 desc->Format = texture_desc.Format;
824 desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE3D;
825 desc->u.Texture3D.MipSlice = 0;
826 desc->u.Texture3D.FirstWSlice = 0;
827 desc->u.Texture3D.WSize = texture_desc.Depth;
829 return S_OK;
832 default:
833 FIXME("Unhandled resource dimension %#x.\n", dimension);
834 return E_INVALIDARG;
838 static HRESULT normalize_uav_desc(D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, ID3D11Resource *resource)
840 D3D11_RESOURCE_DIMENSION dimension;
841 unsigned int layer_count;
842 DXGI_FORMAT format;
843 HRESULT hr;
845 if (FAILED(hr = get_resource_properties(resource, &dimension, &format, NULL, &layer_count)))
846 return hr;
847 switch (dimension)
849 case D3D11_RESOURCE_DIMENSION_BUFFER:
850 if (desc->ViewDimension != D3D11_UAV_DIMENSION_BUFFER)
852 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
853 return E_INVALIDARG;
855 return S_OK;
857 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
858 if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE1D
859 && desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE1DARRAY)
861 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
862 return E_INVALIDARG;
864 break;
866 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
867 if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE2D
868 && desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE2DARRAY)
870 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
871 return E_INVALIDARG;
873 break;
875 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
876 if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE3D)
878 WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension);
879 return E_INVALIDARG;
881 break;
883 default:
884 WARN("Invalid resource dimension %#x.\n", dimension);
885 return E_INVALIDARG;
888 if (desc->Format == DXGI_FORMAT_UNKNOWN)
889 desc->Format = format;
891 switch (desc->ViewDimension)
893 case D3D11_UAV_DIMENSION_TEXTURE1DARRAY:
894 if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < layer_count)
895 desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.FirstArraySlice;
896 break;
898 case D3D11_UAV_DIMENSION_TEXTURE2DARRAY:
899 if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count)
900 desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice;
901 break;
903 case D3D11_UAV_DIMENSION_TEXTURE3D:
904 layer_count = max(1, layer_count >> desc->u.Texture3D.MipSlice);
905 if (desc->u.Texture3D.WSize == ~0u && desc->u.Texture3D.FirstWSlice < layer_count)
906 desc->u.Texture3D.WSize = layer_count - desc->u.Texture3D.FirstWSlice;
907 break;
909 default:
910 break;
913 return S_OK;
916 /* ID3D11DepthStencilView methods */
918 static inline struct d3d_depthstencil_view *impl_from_ID3D11DepthStencilView(ID3D11DepthStencilView *iface)
920 return CONTAINING_RECORD(iface, struct d3d_depthstencil_view, ID3D11DepthStencilView_iface);
923 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_QueryInterface(ID3D11DepthStencilView *iface,
924 REFIID riid, void **object)
926 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
928 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
930 if (IsEqualGUID(riid, &IID_ID3D11DepthStencilView)
931 || IsEqualGUID(riid, &IID_ID3D11View)
932 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
933 || IsEqualGUID(riid, &IID_IUnknown))
935 ID3D11DepthStencilView_AddRef(iface);
936 *object = iface;
937 return S_OK;
940 if (IsEqualGUID(riid, &IID_ID3D10DepthStencilView)
941 || IsEqualGUID(riid, &IID_ID3D10View)
942 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
944 ID3D10DepthStencilView_AddRef(&view->ID3D10DepthStencilView_iface);
945 *object = &view->ID3D10DepthStencilView_iface;
946 return S_OK;
949 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
951 *object = NULL;
952 return E_NOINTERFACE;
955 static ULONG STDMETHODCALLTYPE d3d11_depthstencil_view_AddRef(ID3D11DepthStencilView *iface)
957 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
958 ULONG refcount = InterlockedIncrement(&view->refcount);
960 TRACE("%p increasing refcount to %u.\n", view, refcount);
962 if (refcount == 1)
964 ID3D11Device2_AddRef(view->device);
965 wined3d_mutex_lock();
966 wined3d_rendertarget_view_incref(view->wined3d_view);
967 wined3d_mutex_unlock();
970 return refcount;
973 static ULONG STDMETHODCALLTYPE d3d11_depthstencil_view_Release(ID3D11DepthStencilView *iface)
975 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
976 ULONG refcount = InterlockedDecrement(&view->refcount);
978 TRACE("%p decreasing refcount to %u.\n", view, refcount);
980 if (!refcount)
982 ID3D11Device2 *device = view->device;
984 wined3d_mutex_lock();
985 wined3d_rendertarget_view_decref(view->wined3d_view);
986 wined3d_mutex_unlock();
988 ID3D11Device2_Release(device);
991 return refcount;
994 static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetDevice(ID3D11DepthStencilView *iface,
995 ID3D11Device **device)
997 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
999 TRACE("iface %p, device %p.\n", iface, device);
1001 *device = (ID3D11Device *)view->device;
1002 ID3D11Device_AddRef(*device);
1005 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_GetPrivateData(ID3D11DepthStencilView *iface,
1006 REFGUID guid, UINT *data_size, void *data)
1008 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1010 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1012 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1015 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_SetPrivateData(ID3D11DepthStencilView *iface,
1016 REFGUID guid, UINT data_size, const void *data)
1018 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1020 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1022 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1025 static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_SetPrivateDataInterface(ID3D11DepthStencilView *iface,
1026 REFGUID guid, const IUnknown *data)
1028 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1030 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1032 return d3d_set_private_data_interface(&view->private_store, guid, data);
1035 static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetResource(ID3D11DepthStencilView *iface,
1036 ID3D11Resource **resource)
1038 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1040 TRACE("iface %p, resource %p.\n", iface, resource);
1042 *resource = view->resource;
1043 ID3D11Resource_AddRef(*resource);
1046 static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetDesc(ID3D11DepthStencilView *iface,
1047 D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
1049 struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
1051 TRACE("iface %p, desc %p.\n", iface, desc);
1053 *desc = view->desc;
1056 static const struct ID3D11DepthStencilViewVtbl d3d11_depthstencil_view_vtbl =
1058 /* IUnknown methods */
1059 d3d11_depthstencil_view_QueryInterface,
1060 d3d11_depthstencil_view_AddRef,
1061 d3d11_depthstencil_view_Release,
1062 /* ID3D11DeviceChild methods */
1063 d3d11_depthstencil_view_GetDevice,
1064 d3d11_depthstencil_view_GetPrivateData,
1065 d3d11_depthstencil_view_SetPrivateData,
1066 d3d11_depthstencil_view_SetPrivateDataInterface,
1067 /* ID3D11View methods */
1068 d3d11_depthstencil_view_GetResource,
1069 /* ID3D11DepthStencilView methods */
1070 d3d11_depthstencil_view_GetDesc,
1073 /* ID3D10DepthStencilView methods */
1075 static inline struct d3d_depthstencil_view *impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
1077 return CONTAINING_RECORD(iface, struct d3d_depthstencil_view, ID3D10DepthStencilView_iface);
1080 /* IUnknown methods */
1082 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_QueryInterface(ID3D10DepthStencilView *iface,
1083 REFIID riid, void **object)
1085 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1087 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1089 return d3d11_depthstencil_view_QueryInterface(&view->ID3D11DepthStencilView_iface, riid, object);
1092 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_AddRef(ID3D10DepthStencilView *iface)
1094 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1096 TRACE("iface %p.\n", iface);
1098 return d3d11_depthstencil_view_AddRef(&view->ID3D11DepthStencilView_iface);
1101 static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_Release(ID3D10DepthStencilView *iface)
1103 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1105 TRACE("iface %p.\n", iface);
1107 return d3d11_depthstencil_view_Release(&view->ID3D11DepthStencilView_iface);
1110 /* ID3D10DeviceChild methods */
1112 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDevice(ID3D10DepthStencilView *iface, ID3D10Device **device)
1114 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1116 TRACE("iface %p, device %p.\n", iface, device);
1118 ID3D11Device2_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
1121 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_GetPrivateData(ID3D10DepthStencilView *iface,
1122 REFGUID guid, UINT *data_size, void *data)
1124 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1126 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
1127 iface, debugstr_guid(guid), data_size, data);
1129 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1132 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateData(ID3D10DepthStencilView *iface,
1133 REFGUID guid, UINT data_size, const void *data)
1135 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1137 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
1138 iface, debugstr_guid(guid), data_size, data);
1140 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1143 static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateDataInterface(ID3D10DepthStencilView *iface,
1144 REFGUID guid, const IUnknown *data)
1146 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1148 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1150 return d3d_set_private_data_interface(&view->private_store, guid, data);
1153 /* ID3D10View methods */
1155 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetResource(ID3D10DepthStencilView *iface,
1156 ID3D10Resource **resource)
1158 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1160 TRACE("iface %p, resource %p.\n", iface, resource);
1162 ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1165 /* ID3D10DepthStencilView methods */
1167 static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDesc(ID3D10DepthStencilView *iface,
1168 D3D10_DEPTH_STENCIL_VIEW_DESC *desc)
1170 struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
1171 const D3D11_DEPTH_STENCIL_VIEW_DESC *d3d11_desc = &view->desc;
1173 TRACE("iface %p, desc %p.\n", iface, desc);
1175 desc->Format = d3d11_desc->Format;
1176 desc->ViewDimension = (D3D10_DSV_DIMENSION)d3d11_desc->ViewDimension;
1177 memcpy(&desc->u, &d3d11_desc->u, sizeof(desc->u));
1180 static const struct ID3D10DepthStencilViewVtbl d3d10_depthstencil_view_vtbl =
1182 /* IUnknown methods */
1183 d3d10_depthstencil_view_QueryInterface,
1184 d3d10_depthstencil_view_AddRef,
1185 d3d10_depthstencil_view_Release,
1186 /* ID3D10DeviceChild methods */
1187 d3d10_depthstencil_view_GetDevice,
1188 d3d10_depthstencil_view_GetPrivateData,
1189 d3d10_depthstencil_view_SetPrivateData,
1190 d3d10_depthstencil_view_SetPrivateDataInterface,
1191 /* ID3D10View methods */
1192 d3d10_depthstencil_view_GetResource,
1193 /* ID3D10DepthStencilView methods */
1194 d3d10_depthstencil_view_GetDesc,
1197 static void STDMETHODCALLTYPE d3d_depth_stencil_view_wined3d_object_destroyed(void *parent)
1199 struct d3d_depthstencil_view *view = parent;
1201 wined3d_private_store_cleanup(&view->private_store);
1202 heap_free(parent);
1205 static const struct wined3d_parent_ops d3d_depth_stencil_view_wined3d_parent_ops =
1207 d3d_depth_stencil_view_wined3d_object_destroyed,
1210 static void wined3d_depth_stencil_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
1211 const D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
1213 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
1215 wined3d_desc->flags = 0;
1216 if (desc->Flags & D3D11_DSV_READ_ONLY_DEPTH)
1217 wined3d_desc->flags |= WINED3D_VIEW_READ_ONLY_DEPTH;
1218 if (desc->Flags & D3D11_DSV_READ_ONLY_STENCIL)
1219 wined3d_desc->flags |= WINED3D_VIEW_READ_ONLY_STENCIL;
1221 wined3d_desc->u.texture.level_count = 1;
1222 switch (desc->ViewDimension)
1224 case D3D11_DSV_DIMENSION_TEXTURE1D:
1225 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
1226 wined3d_desc->u.texture.layer_idx = 0;
1227 wined3d_desc->u.texture.layer_count = 1;
1228 break;
1230 case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
1231 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1232 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
1233 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
1234 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
1235 break;
1237 case D3D11_DSV_DIMENSION_TEXTURE2D:
1238 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
1239 wined3d_desc->u.texture.layer_idx = 0;
1240 wined3d_desc->u.texture.layer_count = 1;
1241 break;
1243 case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
1244 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1245 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
1246 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
1247 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
1248 break;
1250 case D3D11_DSV_DIMENSION_TEXTURE2DMS:
1251 wined3d_desc->u.texture.level_idx = 0;
1252 wined3d_desc->u.texture.layer_idx = 0;
1253 wined3d_desc->u.texture.layer_count = 1;
1254 break;
1256 case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
1257 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1258 wined3d_desc->u.texture.level_idx = 0;
1259 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
1260 wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
1261 break;
1263 default:
1264 FIXME("Unhandled view dimension %#x.\n", desc->ViewDimension);
1265 wined3d_desc->u.texture.level_idx = 0;
1266 wined3d_desc->u.texture.layer_idx = 0;
1267 wined3d_desc->u.texture.layer_count = 1;
1268 break;
1272 static HRESULT d3d_depthstencil_view_init(struct d3d_depthstencil_view *view, struct d3d_device *device,
1273 ID3D11Resource *resource, const D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
1275 struct wined3d_resource *wined3d_resource;
1276 struct wined3d_view_desc wined3d_desc;
1277 HRESULT hr;
1279 view->ID3D11DepthStencilView_iface.lpVtbl = &d3d11_depthstencil_view_vtbl;
1280 view->ID3D10DepthStencilView_iface.lpVtbl = &d3d10_depthstencil_view_vtbl;
1281 view->refcount = 1;
1283 if (!desc)
1285 hr = set_dsv_desc_from_resource(&view->desc, resource);
1287 else
1289 view->desc = *desc;
1290 hr = normalize_dsv_desc(&view->desc, resource);
1292 if (FAILED(hr))
1293 return hr;
1295 wined3d_mutex_lock();
1296 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1298 wined3d_mutex_unlock();
1299 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
1300 return E_FAIL;
1303 wined3d_depth_stencil_view_desc_from_d3d11(&wined3d_desc, &view->desc);
1304 if (FAILED(hr = wined3d_rendertarget_view_create(&wined3d_desc, wined3d_resource,
1305 view, &d3d_depth_stencil_view_wined3d_parent_ops, &view->wined3d_view)))
1307 wined3d_mutex_unlock();
1308 WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
1309 return hr;
1312 wined3d_private_store_init(&view->private_store);
1313 wined3d_mutex_unlock();
1314 view->resource = resource;
1315 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
1317 return S_OK;
1320 HRESULT d3d_depthstencil_view_create(struct d3d_device *device, ID3D11Resource *resource,
1321 const D3D11_DEPTH_STENCIL_VIEW_DESC *desc, struct d3d_depthstencil_view **view)
1323 struct d3d_depthstencil_view *object;
1324 HRESULT hr;
1326 if (!(object = heap_alloc_zero(sizeof(*object))))
1327 return E_OUTOFMEMORY;
1329 if (FAILED(hr = d3d_depthstencil_view_init(object, device, resource, desc)))
1331 WARN("Failed to initialize depthstencil view, hr %#x.\n", hr);
1332 heap_free(object);
1333 return hr;
1336 TRACE("Created depthstencil view %p.\n", object);
1337 *view = object;
1339 return S_OK;
1342 struct d3d_depthstencil_view *unsafe_impl_from_ID3D11DepthStencilView(ID3D11DepthStencilView *iface)
1344 if (!iface)
1345 return NULL;
1346 assert(iface->lpVtbl == &d3d11_depthstencil_view_vtbl);
1348 return impl_from_ID3D11DepthStencilView(iface);
1351 struct d3d_depthstencil_view *unsafe_impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
1353 if (!iface)
1354 return NULL;
1355 assert(iface->lpVtbl == &d3d10_depthstencil_view_vtbl);
1357 return impl_from_ID3D10DepthStencilView(iface);
1360 /* ID3D11RenderTargetView methods */
1362 static inline struct d3d_rendertarget_view *impl_from_ID3D11RenderTargetView(ID3D11RenderTargetView *iface)
1364 return CONTAINING_RECORD(iface, struct d3d_rendertarget_view, ID3D11RenderTargetView_iface);
1367 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_QueryInterface(ID3D11RenderTargetView *iface,
1368 REFIID riid, void **object)
1370 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1372 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1374 if (IsEqualGUID(riid, &IID_ID3D11RenderTargetView)
1375 || IsEqualGUID(riid, &IID_ID3D11View)
1376 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
1377 || IsEqualGUID(riid, &IID_IUnknown))
1379 ID3D11RenderTargetView_AddRef(iface);
1380 *object = iface;
1381 return S_OK;
1384 if (IsEqualGUID(riid, &IID_ID3D10RenderTargetView)
1385 || IsEqualGUID(riid, &IID_ID3D10View)
1386 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
1388 ID3D10RenderTargetView_AddRef(&view->ID3D10RenderTargetView_iface);
1389 *object = &view->ID3D10RenderTargetView_iface;
1390 return S_OK;
1393 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1395 *object = NULL;
1396 return E_NOINTERFACE;
1399 static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_AddRef(ID3D11RenderTargetView *iface)
1401 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1402 ULONG refcount = InterlockedIncrement(&view->refcount);
1404 TRACE("%p increasing refcount to %u.\n", view, refcount);
1406 if (refcount == 1)
1408 ID3D11Device2_AddRef(view->device);
1409 wined3d_mutex_lock();
1410 wined3d_rendertarget_view_incref(view->wined3d_view);
1411 wined3d_mutex_unlock();
1414 return refcount;
1417 static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_Release(ID3D11RenderTargetView *iface)
1419 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1420 ULONG refcount = InterlockedDecrement(&view->refcount);
1422 TRACE("%p decreasing refcount to %u.\n", view, refcount);
1424 if (!refcount)
1426 ID3D11Device2 *device = view->device;
1428 wined3d_mutex_lock();
1429 wined3d_rendertarget_view_decref(view->wined3d_view);
1430 wined3d_mutex_unlock();
1432 ID3D11Device2_Release(device);
1435 return refcount;
1438 static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDevice(ID3D11RenderTargetView *iface,
1439 ID3D11Device **device)
1441 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1443 TRACE("iface %p, device %p.\n", iface, device);
1445 *device = (ID3D11Device *)view->device;
1446 ID3D11Device_AddRef(*device);
1449 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_GetPrivateData(ID3D11RenderTargetView *iface,
1450 REFGUID guid, UINT *data_size, void *data)
1452 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1454 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1456 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1459 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_SetPrivateData(ID3D11RenderTargetView *iface,
1460 REFGUID guid, UINT data_size, const void *data)
1462 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1464 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1466 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1469 static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_SetPrivateDataInterface(ID3D11RenderTargetView *iface,
1470 REFGUID guid, const IUnknown *data)
1472 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1474 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1476 return d3d_set_private_data_interface(&view->private_store, guid, data);
1479 static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetResource(ID3D11RenderTargetView *iface,
1480 ID3D11Resource **resource)
1482 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1484 TRACE("iface %p, resource %p.\n", iface, resource);
1486 *resource = view->resource;
1487 ID3D11Resource_AddRef(*resource);
1490 static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDesc(ID3D11RenderTargetView *iface,
1491 D3D11_RENDER_TARGET_VIEW_DESC *desc)
1493 struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
1495 TRACE("iface %p, desc %p.\n", iface, desc);
1497 *desc = view->desc;
1500 static const struct ID3D11RenderTargetViewVtbl d3d11_rendertarget_view_vtbl =
1502 /* IUnknown methods */
1503 d3d11_rendertarget_view_QueryInterface,
1504 d3d11_rendertarget_view_AddRef,
1505 d3d11_rendertarget_view_Release,
1506 /* ID3D11DeviceChild methods */
1507 d3d11_rendertarget_view_GetDevice,
1508 d3d11_rendertarget_view_GetPrivateData,
1509 d3d11_rendertarget_view_SetPrivateData,
1510 d3d11_rendertarget_view_SetPrivateDataInterface,
1511 /* ID3D11View methods */
1512 d3d11_rendertarget_view_GetResource,
1513 /* ID3D11RenderTargetView methods */
1514 d3d11_rendertarget_view_GetDesc,
1517 /* ID3D10RenderTargetView methods */
1519 static inline struct d3d_rendertarget_view *impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
1521 return CONTAINING_RECORD(iface, struct d3d_rendertarget_view, ID3D10RenderTargetView_iface);
1524 /* IUnknown methods */
1526 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_QueryInterface(ID3D10RenderTargetView *iface,
1527 REFIID riid, void **object)
1529 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1531 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1533 return d3d11_rendertarget_view_QueryInterface(&view->ID3D11RenderTargetView_iface, riid, object);
1536 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_AddRef(ID3D10RenderTargetView *iface)
1538 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1540 TRACE("iface %p.\n", iface);
1542 return d3d11_rendertarget_view_AddRef(&view->ID3D11RenderTargetView_iface);
1545 static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_Release(ID3D10RenderTargetView *iface)
1547 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1549 TRACE("iface %p.\n", iface);
1551 return d3d11_rendertarget_view_Release(&view->ID3D11RenderTargetView_iface);
1554 /* ID3D10DeviceChild methods */
1556 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDevice(ID3D10RenderTargetView *iface, ID3D10Device **device)
1558 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1560 TRACE("iface %p, device %p.\n", iface, device);
1562 ID3D11Device2_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
1565 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_GetPrivateData(ID3D10RenderTargetView *iface,
1566 REFGUID guid, UINT *data_size, void *data)
1568 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1570 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
1571 iface, debugstr_guid(guid), data_size, data);
1573 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1576 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateData(ID3D10RenderTargetView *iface,
1577 REFGUID guid, UINT data_size, const void *data)
1579 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1581 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
1582 iface, debugstr_guid(guid), data_size, data);
1584 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1587 static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateDataInterface(ID3D10RenderTargetView *iface,
1588 REFGUID guid, const IUnknown *data)
1590 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1592 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1594 return d3d_set_private_data_interface(&view->private_store, guid, data);
1597 /* ID3D10View methods */
1599 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetResource(ID3D10RenderTargetView *iface,
1600 ID3D10Resource **resource)
1602 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1604 TRACE("iface %p, resource %p\n", iface, resource);
1606 ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1609 /* ID3D10RenderTargetView methods */
1611 static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDesc(ID3D10RenderTargetView *iface,
1612 D3D10_RENDER_TARGET_VIEW_DESC *desc)
1614 struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1616 TRACE("iface %p, desc %p\n", iface, desc);
1618 memcpy(desc, &view->desc, sizeof(*desc));
1621 static const struct ID3D10RenderTargetViewVtbl d3d10_rendertarget_view_vtbl =
1623 /* IUnknown methods */
1624 d3d10_rendertarget_view_QueryInterface,
1625 d3d10_rendertarget_view_AddRef,
1626 d3d10_rendertarget_view_Release,
1627 /* ID3D10DeviceChild methods */
1628 d3d10_rendertarget_view_GetDevice,
1629 d3d10_rendertarget_view_GetPrivateData,
1630 d3d10_rendertarget_view_SetPrivateData,
1631 d3d10_rendertarget_view_SetPrivateDataInterface,
1632 /* ID3D10View methods */
1633 d3d10_rendertarget_view_GetResource,
1634 /* ID3D10RenderTargetView methods */
1635 d3d10_rendertarget_view_GetDesc,
1638 static void STDMETHODCALLTYPE d3d_render_target_view_wined3d_object_destroyed(void *parent)
1640 struct d3d_rendertarget_view *view = parent;
1642 wined3d_private_store_cleanup(&view->private_store);
1643 heap_free(parent);
1646 static const struct wined3d_parent_ops d3d_render_target_view_wined3d_parent_ops =
1648 d3d_render_target_view_wined3d_object_destroyed,
1651 static void wined3d_rendertarget_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
1652 const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1654 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
1656 wined3d_desc->flags = 0;
1657 wined3d_desc->u.texture.level_count = 1;
1658 switch (desc->ViewDimension)
1660 case D3D11_RTV_DIMENSION_BUFFER:
1661 wined3d_desc->u.buffer.start_idx = desc->u.Buffer.u1.FirstElement;
1662 wined3d_desc->u.buffer.count = desc->u.Buffer.u2.NumElements;
1663 break;
1665 case D3D11_RTV_DIMENSION_TEXTURE1D:
1666 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
1667 wined3d_desc->u.texture.layer_idx = 0;
1668 wined3d_desc->u.texture.layer_count = 1;
1669 break;
1671 case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
1672 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1673 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
1674 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
1675 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
1676 break;
1678 case D3D11_RTV_DIMENSION_TEXTURE2D:
1679 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
1680 wined3d_desc->u.texture.layer_idx = 0;
1681 wined3d_desc->u.texture.layer_count = 1;
1682 break;
1684 case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
1685 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1686 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
1687 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
1688 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
1689 break;
1691 case D3D11_RTV_DIMENSION_TEXTURE2DMS:
1692 wined3d_desc->u.texture.level_idx = 0;
1693 wined3d_desc->u.texture.layer_idx = 0;
1694 wined3d_desc->u.texture.layer_count = 1;
1695 break;
1697 case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
1698 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
1699 wined3d_desc->u.texture.level_idx = 0;
1700 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
1701 wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
1702 break;
1704 case D3D11_RTV_DIMENSION_TEXTURE3D:
1705 wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MipSlice;
1706 wined3d_desc->u.texture.layer_idx = desc->u.Texture3D.FirstWSlice;
1707 wined3d_desc->u.texture.layer_count = desc->u.Texture3D.WSize;
1708 break;
1710 default:
1711 FIXME("Unhandled view dimension %#x.\n", desc->ViewDimension);
1712 wined3d_desc->u.texture.level_idx = 0;
1713 wined3d_desc->u.texture.layer_idx = 0;
1714 wined3d_desc->u.texture.layer_count = 1;
1715 break;
1719 static HRESULT d3d_rendertarget_view_init(struct d3d_rendertarget_view *view, struct d3d_device *device,
1720 ID3D11Resource *resource, const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1722 struct wined3d_resource *wined3d_resource;
1723 struct wined3d_view_desc wined3d_desc;
1724 HRESULT hr;
1726 view->ID3D11RenderTargetView_iface.lpVtbl = &d3d11_rendertarget_view_vtbl;
1727 view->ID3D10RenderTargetView_iface.lpVtbl = &d3d10_rendertarget_view_vtbl;
1728 view->refcount = 1;
1730 if (!desc)
1732 hr = set_rtv_desc_from_resource(&view->desc, resource);
1734 else
1736 view->desc = *desc;
1737 hr = normalize_rtv_desc(&view->desc, resource);
1739 if (FAILED(hr))
1740 return hr;
1742 wined3d_mutex_lock();
1743 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1745 wined3d_mutex_unlock();
1746 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
1747 return E_FAIL;
1750 wined3d_rendertarget_view_desc_from_d3d11(&wined3d_desc, &view->desc);
1751 if (FAILED(hr = wined3d_rendertarget_view_create(&wined3d_desc, wined3d_resource,
1752 view, &d3d_render_target_view_wined3d_parent_ops, &view->wined3d_view)))
1754 wined3d_mutex_unlock();
1755 WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
1756 return hr;
1759 wined3d_private_store_init(&view->private_store);
1760 wined3d_mutex_unlock();
1761 view->resource = resource;
1762 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
1764 return S_OK;
1767 HRESULT d3d_rendertarget_view_create(struct d3d_device *device, ID3D11Resource *resource,
1768 const D3D11_RENDER_TARGET_VIEW_DESC *desc, struct d3d_rendertarget_view **view)
1770 struct d3d_rendertarget_view *object;
1771 HRESULT hr;
1773 if (!(object = heap_alloc_zero(sizeof(*object))))
1774 return E_OUTOFMEMORY;
1776 if (FAILED(hr = d3d_rendertarget_view_init(object, device, resource, desc)))
1778 WARN("Failed to initialize rendertarget view, hr %#x.\n", hr);
1779 heap_free(object);
1780 return hr;
1783 TRACE("Created rendertarget view %p.\n", object);
1784 *view = object;
1786 return S_OK;
1789 struct d3d_rendertarget_view *unsafe_impl_from_ID3D11RenderTargetView(ID3D11RenderTargetView *iface)
1791 if (!iface)
1792 return NULL;
1793 assert(iface->lpVtbl == &d3d11_rendertarget_view_vtbl);
1795 return impl_from_ID3D11RenderTargetView(iface);
1798 struct d3d_rendertarget_view *unsafe_impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
1800 if (!iface)
1801 return NULL;
1802 assert(iface->lpVtbl == &d3d10_rendertarget_view_vtbl);
1804 return impl_from_ID3D10RenderTargetView(iface);
1807 /* ID3D11ShaderResourceView methods */
1809 static inline struct d3d_shader_resource_view *impl_from_ID3D11ShaderResourceView(ID3D11ShaderResourceView *iface)
1811 return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D11ShaderResourceView_iface);
1814 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_QueryInterface(ID3D11ShaderResourceView *iface,
1815 REFIID riid, void **object)
1817 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1819 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1821 if (IsEqualGUID(riid, &IID_ID3D11ShaderResourceView)
1822 || IsEqualGUID(riid, &IID_ID3D11View)
1823 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
1824 || IsEqualGUID(riid, &IID_IUnknown))
1826 ID3D11ShaderResourceView_AddRef(iface);
1827 *object = iface;
1828 return S_OK;
1831 if (IsEqualGUID(riid, &IID_ID3D10ShaderResourceView1)
1832 || IsEqualGUID(riid, &IID_ID3D10ShaderResourceView)
1833 || IsEqualGUID(riid, &IID_ID3D10View)
1834 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
1836 ID3D10ShaderResourceView1_AddRef(&view->ID3D10ShaderResourceView1_iface);
1837 *object = &view->ID3D10ShaderResourceView1_iface;
1838 return S_OK;
1841 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1843 *object = NULL;
1844 return E_NOINTERFACE;
1847 static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_AddRef(ID3D11ShaderResourceView *iface)
1849 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1850 ULONG refcount = InterlockedIncrement(&view->refcount);
1852 TRACE("%p increasing refcount to %u.\n", view, refcount);
1854 if (refcount == 1)
1856 ID3D11Device2_AddRef(view->device);
1857 wined3d_mutex_lock();
1858 wined3d_shader_resource_view_incref(view->wined3d_view);
1859 wined3d_mutex_unlock();
1862 return refcount;
1865 static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_Release(ID3D11ShaderResourceView *iface)
1867 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1868 ULONG refcount = InterlockedDecrement(&view->refcount);
1870 TRACE("%p decreasing refcount to %u.\n", view, refcount);
1872 if (!refcount)
1874 ID3D11Device2 *device = view->device;
1876 wined3d_mutex_lock();
1877 wined3d_shader_resource_view_decref(view->wined3d_view);
1878 wined3d_mutex_unlock();
1880 ID3D11Device2_Release(device);
1883 return refcount;
1886 static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDevice(ID3D11ShaderResourceView *iface,
1887 ID3D11Device **device)
1889 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1891 TRACE("iface %p, device %p.\n", iface, device);
1893 *device = (ID3D11Device *)view->device;
1894 ID3D11Device_AddRef(*device);
1897 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_GetPrivateData(ID3D11ShaderResourceView *iface,
1898 REFGUID guid, UINT *data_size, void *data)
1900 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1902 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1904 return d3d_get_private_data(&view->private_store, guid, data_size, data);
1907 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_SetPrivateData(ID3D11ShaderResourceView *iface,
1908 REFGUID guid, UINT data_size, const void *data)
1910 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1912 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
1914 return d3d_set_private_data(&view->private_store, guid, data_size, data);
1917 static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_SetPrivateDataInterface(ID3D11ShaderResourceView *iface,
1918 REFGUID guid, const IUnknown *data)
1920 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1922 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
1924 return d3d_set_private_data_interface(&view->private_store, guid, data);
1927 static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetResource(ID3D11ShaderResourceView *iface,
1928 ID3D11Resource **resource)
1930 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1932 TRACE("iface %p, resource %p.\n", iface, resource);
1934 *resource = view->resource;
1935 ID3D11Resource_AddRef(*resource);
1938 static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDesc(ID3D11ShaderResourceView *iface,
1939 D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
1941 struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
1943 TRACE("iface %p, desc %p.\n", iface, desc);
1945 *desc = view->desc;
1948 static const struct ID3D11ShaderResourceViewVtbl d3d11_shader_resource_view_vtbl =
1950 /* IUnknown methods */
1951 d3d11_shader_resource_view_QueryInterface,
1952 d3d11_shader_resource_view_AddRef,
1953 d3d11_shader_resource_view_Release,
1954 /* ID3D11DeviceChild methods */
1955 d3d11_shader_resource_view_GetDevice,
1956 d3d11_shader_resource_view_GetPrivateData,
1957 d3d11_shader_resource_view_SetPrivateData,
1958 d3d11_shader_resource_view_SetPrivateDataInterface,
1959 /* ID3D11View methods */
1960 d3d11_shader_resource_view_GetResource,
1961 /* ID3D11ShaderResourceView methods */
1962 d3d11_shader_resource_view_GetDesc,
1965 /* ID3D10ShaderResourceView methods */
1967 static inline struct d3d_shader_resource_view *impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView1 *iface)
1969 return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
1972 /* IUnknown methods */
1974 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_QueryInterface(ID3D10ShaderResourceView1 *iface,
1975 REFIID riid, void **object)
1977 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1979 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
1981 return d3d11_shader_resource_view_QueryInterface(&view->ID3D11ShaderResourceView_iface, riid, object);
1984 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_AddRef(ID3D10ShaderResourceView1 *iface)
1986 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1988 TRACE("iface %p.\n", iface);
1990 return d3d11_shader_resource_view_AddRef(&view->ID3D11ShaderResourceView_iface);
1993 static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_Release(ID3D10ShaderResourceView1 *iface)
1995 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1997 TRACE("iface %p.\n", iface);
1999 return d3d11_shader_resource_view_Release(&view->ID3D11ShaderResourceView_iface);
2002 /* ID3D10DeviceChild methods */
2004 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDevice(ID3D10ShaderResourceView1 *iface,
2005 ID3D10Device **device)
2007 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2009 TRACE("iface %p, device %p.\n", iface, device);
2011 ID3D11Device2_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
2014 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_GetPrivateData(ID3D10ShaderResourceView1 *iface,
2015 REFGUID guid, UINT *data_size, void *data)
2017 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2019 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
2020 iface, debugstr_guid(guid), data_size, data);
2022 return d3d_get_private_data(&view->private_store, guid, data_size, data);
2025 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateData(ID3D10ShaderResourceView1 *iface,
2026 REFGUID guid, UINT data_size, const void *data)
2028 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2030 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
2031 iface, debugstr_guid(guid), data_size, data);
2033 return d3d_set_private_data(&view->private_store, guid, data_size, data);
2036 static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateDataInterface(ID3D10ShaderResourceView1 *iface,
2037 REFGUID guid, const IUnknown *data)
2039 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2041 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
2043 return d3d_set_private_data_interface(&view->private_store, guid, data);
2046 /* ID3D10View methods */
2048 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetResource(ID3D10ShaderResourceView1 *iface,
2049 ID3D10Resource **resource)
2051 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2053 TRACE("iface %p, resource %p.\n", iface, resource);
2055 ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
2058 /* ID3D10ShaderResourceView methods */
2060 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc(ID3D10ShaderResourceView1 *iface,
2061 D3D10_SHADER_RESOURCE_VIEW_DESC *desc)
2063 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2065 TRACE("iface %p, desc %p.\n", iface, desc);
2067 memcpy(desc, &view->desc, sizeof(*desc));
2070 static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc1(ID3D10ShaderResourceView1 *iface,
2071 D3D10_SHADER_RESOURCE_VIEW_DESC1 *desc)
2073 struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
2075 TRACE("iface %p, desc %p.\n", iface, desc);
2077 memcpy(desc, &view->desc, sizeof(*desc));
2080 static const struct ID3D10ShaderResourceView1Vtbl d3d10_shader_resource_view_vtbl =
2082 /* IUnknown methods */
2083 d3d10_shader_resource_view_QueryInterface,
2084 d3d10_shader_resource_view_AddRef,
2085 d3d10_shader_resource_view_Release,
2086 /* ID3D10DeviceChild methods */
2087 d3d10_shader_resource_view_GetDevice,
2088 d3d10_shader_resource_view_GetPrivateData,
2089 d3d10_shader_resource_view_SetPrivateData,
2090 d3d10_shader_resource_view_SetPrivateDataInterface,
2091 /* ID3D10View methods */
2092 d3d10_shader_resource_view_GetResource,
2093 /* ID3D10ShaderResourceView methods */
2094 d3d10_shader_resource_view_GetDesc,
2095 /* ID3D10ShaderResourceView1 methods */
2096 d3d10_shader_resource_view_GetDesc1,
2099 static void STDMETHODCALLTYPE d3d_shader_resource_view_wined3d_object_destroyed(void *parent)
2101 struct d3d_shader_resource_view *view = parent;
2103 wined3d_private_store_cleanup(&view->private_store);
2104 heap_free(parent);
2107 static const struct wined3d_parent_ops d3d_shader_resource_view_wined3d_parent_ops =
2109 d3d_shader_resource_view_wined3d_object_destroyed,
2112 static unsigned int wined3d_view_flags_from_d3d11_bufferex_flags(unsigned int d3d11_flags)
2114 unsigned int wined3d_flags = d3d11_flags & WINED3D_VIEW_BUFFER_RAW;
2116 if (d3d11_flags != wined3d_flags)
2117 FIXME("Unhandled flags %#x.\n", d3d11_flags & ~wined3d_flags);
2119 return wined3d_flags;
2122 static HRESULT wined3d_shader_resource_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
2123 const D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
2125 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
2126 wined3d_desc->flags = 0;
2128 switch (desc->ViewDimension)
2130 case D3D11_SRV_DIMENSION_BUFFER:
2131 wined3d_desc->u.buffer.start_idx = desc->u.Buffer.u1.FirstElement;
2132 wined3d_desc->u.buffer.count = desc->u.Buffer.u2.NumElements;
2133 break;
2135 case D3D11_SRV_DIMENSION_TEXTURE1D:
2136 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MostDetailedMip;
2137 wined3d_desc->u.texture.level_count = desc->u.Texture1D.MipLevels;
2138 wined3d_desc->u.texture.layer_idx = 0;
2139 wined3d_desc->u.texture.layer_count = 1;
2140 break;
2142 case D3D11_SRV_DIMENSION_TEXTURE1DARRAY:
2143 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2144 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MostDetailedMip;
2145 wined3d_desc->u.texture.level_count = desc->u.Texture1DArray.MipLevels;
2146 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
2147 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
2148 break;
2150 case D3D11_SRV_DIMENSION_TEXTURE2D:
2151 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MostDetailedMip;
2152 wined3d_desc->u.texture.level_count = desc->u.Texture2D.MipLevels;
2153 wined3d_desc->u.texture.layer_idx = 0;
2154 wined3d_desc->u.texture.layer_count = 1;
2155 break;
2157 case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
2158 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2159 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MostDetailedMip;
2160 wined3d_desc->u.texture.level_count = desc->u.Texture2DArray.MipLevels;
2161 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
2162 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
2163 break;
2165 case D3D11_SRV_DIMENSION_TEXTURE2DMS:
2166 wined3d_desc->u.texture.level_idx = 0;
2167 wined3d_desc->u.texture.level_count = 1;
2168 wined3d_desc->u.texture.layer_idx = 0;
2169 wined3d_desc->u.texture.layer_count = 1;
2170 break;
2172 case D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY:
2173 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2174 wined3d_desc->u.texture.level_idx = 0;
2175 wined3d_desc->u.texture.level_count = 1;
2176 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
2177 wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
2178 break;
2180 case D3D11_SRV_DIMENSION_TEXTURE3D:
2181 wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MostDetailedMip;
2182 wined3d_desc->u.texture.level_count = desc->u.Texture3D.MipLevels;
2183 wined3d_desc->u.texture.layer_idx = 0;
2184 wined3d_desc->u.texture.layer_count = 1;
2185 break;
2187 case D3D11_SRV_DIMENSION_TEXTURECUBE:
2188 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_CUBE;
2189 wined3d_desc->u.texture.level_idx = desc->u.TextureCube.MostDetailedMip;
2190 wined3d_desc->u.texture.level_count = desc->u.TextureCube.MipLevels;
2191 wined3d_desc->u.texture.layer_idx = 0;
2192 wined3d_desc->u.texture.layer_count = 6;
2193 break;
2195 case D3D11_SRV_DIMENSION_TEXTURECUBEARRAY:
2196 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY;
2197 wined3d_desc->u.texture.level_idx = desc->u.TextureCubeArray.MostDetailedMip;
2198 wined3d_desc->u.texture.level_count = desc->u.TextureCubeArray.MipLevels;
2199 wined3d_desc->u.texture.layer_idx = desc->u.TextureCubeArray.First2DArrayFace;
2200 wined3d_desc->u.texture.layer_count = 6 * desc->u.TextureCubeArray.NumCubes;
2201 break;
2203 case D3D11_SRV_DIMENSION_BUFFEREX:
2204 wined3d_desc->flags = wined3d_view_flags_from_d3d11_bufferex_flags(desc->u.BufferEx.Flags);
2205 wined3d_desc->u.buffer.start_idx = desc->u.BufferEx.FirstElement;
2206 wined3d_desc->u.buffer.count = desc->u.BufferEx.NumElements;
2207 break;
2209 default:
2210 WARN("Unrecognized view dimension %#x.\n", desc->ViewDimension);
2211 return E_FAIL;
2214 return S_OK;
2217 static HRESULT d3d_shader_resource_view_init(struct d3d_shader_resource_view *view, struct d3d_device *device,
2218 ID3D11Resource *resource, const D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
2220 struct wined3d_resource *wined3d_resource;
2221 struct wined3d_view_desc wined3d_desc;
2222 HRESULT hr;
2224 view->ID3D11ShaderResourceView_iface.lpVtbl = &d3d11_shader_resource_view_vtbl;
2225 view->ID3D10ShaderResourceView1_iface.lpVtbl = &d3d10_shader_resource_view_vtbl;
2226 view->refcount = 1;
2228 if (!desc)
2230 hr = set_srv_desc_from_resource(&view->desc, resource);
2232 else
2234 view->desc = *desc;
2235 hr = normalize_srv_desc(&view->desc, resource);
2237 if (FAILED(hr))
2238 return hr;
2240 if (FAILED(hr = wined3d_shader_resource_view_desc_from_d3d11(&wined3d_desc, &view->desc)))
2241 return hr;
2243 wined3d_mutex_lock();
2244 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
2246 wined3d_mutex_unlock();
2247 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
2248 return E_FAIL;
2251 if (FAILED(hr = wined3d_shader_resource_view_create(&wined3d_desc, wined3d_resource,
2252 view, &d3d_shader_resource_view_wined3d_parent_ops, &view->wined3d_view)))
2254 wined3d_mutex_unlock();
2255 WARN("Failed to create wined3d shader resource view, hr %#x.\n", hr);
2256 return hr;
2259 wined3d_private_store_init(&view->private_store);
2260 wined3d_mutex_unlock();
2261 view->resource = resource;
2262 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
2264 return S_OK;
2267 HRESULT d3d_shader_resource_view_create(struct d3d_device *device, ID3D11Resource *resource,
2268 const D3D11_SHADER_RESOURCE_VIEW_DESC *desc, struct d3d_shader_resource_view **view)
2270 struct d3d_shader_resource_view *object;
2271 HRESULT hr;
2273 if (!(object = heap_alloc_zero(sizeof(*object))))
2274 return E_OUTOFMEMORY;
2276 if (FAILED(hr = d3d_shader_resource_view_init(object, device, resource, desc)))
2278 WARN("Failed to initialize shader resource view, hr %#x.\n", hr);
2279 heap_free(object);
2280 return hr;
2283 TRACE("Created shader resource view %p.\n", object);
2284 *view = object;
2286 return S_OK;
2289 struct d3d_shader_resource_view *unsafe_impl_from_ID3D11ShaderResourceView(ID3D11ShaderResourceView *iface)
2291 if (!iface)
2292 return NULL;
2293 assert(iface->lpVtbl == &d3d11_shader_resource_view_vtbl);
2294 return impl_from_ID3D11ShaderResourceView(iface);
2297 struct d3d_shader_resource_view *unsafe_impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView *iface)
2299 if (!iface)
2300 return NULL;
2301 assert(iface->lpVtbl == (ID3D10ShaderResourceViewVtbl *)&d3d10_shader_resource_view_vtbl);
2302 return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
2305 /* ID3D11UnorderedAccessView methods */
2307 static inline struct d3d11_unordered_access_view *impl_from_ID3D11UnorderedAccessView(ID3D11UnorderedAccessView *iface)
2309 return CONTAINING_RECORD(iface, struct d3d11_unordered_access_view, ID3D11UnorderedAccessView_iface);
2312 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_QueryInterface(ID3D11UnorderedAccessView *iface,
2313 REFIID riid, void **object)
2315 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
2317 if (IsEqualGUID(riid, &IID_ID3D11UnorderedAccessView)
2318 || IsEqualGUID(riid, &IID_ID3D11View)
2319 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
2320 || IsEqualGUID(riid, &IID_IUnknown))
2322 ID3D11UnorderedAccessView_AddRef(*object = iface);
2323 return S_OK;
2326 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
2328 *object = NULL;
2329 return E_NOINTERFACE;
2332 static ULONG STDMETHODCALLTYPE d3d11_unordered_access_view_AddRef(ID3D11UnorderedAccessView *iface)
2334 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2335 ULONG refcount = InterlockedIncrement(&view->refcount);
2337 TRACE("%p increasing refcount to %u.\n", view, refcount);
2339 if (refcount == 1)
2341 ID3D11Device2_AddRef(view->device);
2342 wined3d_mutex_lock();
2343 wined3d_unordered_access_view_incref(view->wined3d_view);
2344 wined3d_mutex_unlock();
2347 return refcount;
2350 static ULONG STDMETHODCALLTYPE d3d11_unordered_access_view_Release(ID3D11UnorderedAccessView *iface)
2352 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2353 ULONG refcount = InterlockedDecrement(&view->refcount);
2355 TRACE("%p decreasing refcount to %u.\n", view, refcount);
2357 if (!refcount)
2359 ID3D11Device2 *device = view->device;
2361 wined3d_mutex_lock();
2362 wined3d_unordered_access_view_decref(view->wined3d_view);
2363 wined3d_mutex_unlock();
2365 ID3D11Device2_Release(device);
2368 return refcount;
2371 static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetDevice(ID3D11UnorderedAccessView *iface,
2372 ID3D11Device **device)
2374 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2376 TRACE("iface %p, device %p.\n", iface, device);
2378 ID3D11Device_AddRef(*device = (ID3D11Device *)view->device);
2381 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_GetPrivateData(ID3D11UnorderedAccessView *iface,
2382 REFGUID guid, UINT *data_size, void *data)
2384 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2386 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
2388 return d3d_get_private_data(&view->private_store, guid, data_size, data);
2391 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_SetPrivateData(ID3D11UnorderedAccessView *iface,
2392 REFGUID guid, UINT data_size, const void *data)
2394 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2396 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
2398 return d3d_set_private_data(&view->private_store, guid, data_size, data);
2401 static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_SetPrivateDataInterface(ID3D11UnorderedAccessView *iface,
2402 REFGUID guid, const IUnknown *data)
2404 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2406 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
2408 return d3d_set_private_data_interface(&view->private_store, guid, data);
2411 static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetResource(ID3D11UnorderedAccessView *iface,
2412 ID3D11Resource **resource)
2414 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2416 TRACE("iface %p, resource %p.\n", iface, resource);
2418 ID3D11Resource_AddRef(*resource = view->resource);
2421 static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetDesc(ID3D11UnorderedAccessView *iface,
2422 D3D11_UNORDERED_ACCESS_VIEW_DESC *desc)
2424 struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface);
2426 TRACE("iface %p, desc %p.\n", iface, desc);
2428 *desc = view->desc;
2431 static const struct ID3D11UnorderedAccessViewVtbl d3d11_unordered_access_view_vtbl =
2433 /* IUnknown methods */
2434 d3d11_unordered_access_view_QueryInterface,
2435 d3d11_unordered_access_view_AddRef,
2436 d3d11_unordered_access_view_Release,
2437 /* ID3D11DeviceChild methods */
2438 d3d11_unordered_access_view_GetDevice,
2439 d3d11_unordered_access_view_GetPrivateData,
2440 d3d11_unordered_access_view_SetPrivateData,
2441 d3d11_unordered_access_view_SetPrivateDataInterface,
2442 /* ID3D11View methods */
2443 d3d11_unordered_access_view_GetResource,
2444 /* ID3D11UnorderedAccessView methods */
2445 d3d11_unordered_access_view_GetDesc,
2448 static void STDMETHODCALLTYPE d3d11_unordered_access_view_wined3d_object_destroyed(void *parent)
2450 struct d3d11_unordered_access_view *view = parent;
2452 wined3d_private_store_cleanup(&view->private_store);
2453 heap_free(parent);
2456 static const struct wined3d_parent_ops d3d11_unordered_access_view_wined3d_parent_ops =
2458 d3d11_unordered_access_view_wined3d_object_destroyed,
2461 static unsigned int wined3d_view_flags_from_d3d11_buffer_uav_flags(unsigned int d3d11_flags)
2463 unsigned int wined3d_flags = d3d11_flags & (WINED3D_VIEW_BUFFER_RAW
2464 | WINED3D_VIEW_BUFFER_APPEND | WINED3D_VIEW_BUFFER_COUNTER);
2466 if (d3d11_flags != wined3d_flags)
2467 FIXME("Unhandled flags %#x.\n", d3d11_flags & ~wined3d_flags);
2469 return wined3d_flags;
2472 static HRESULT wined3d_unordered_access_view_desc_from_d3d11(struct wined3d_view_desc *wined3d_desc,
2473 const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc)
2475 wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);
2477 wined3d_desc->flags = 0;
2478 wined3d_desc->u.texture.level_count = 1;
2479 switch (desc->ViewDimension)
2481 case D3D11_UAV_DIMENSION_BUFFER:
2482 wined3d_desc->flags = wined3d_view_flags_from_d3d11_buffer_uav_flags(desc->u.Buffer.Flags);
2483 wined3d_desc->u.buffer.start_idx = desc->u.Buffer.FirstElement;
2484 wined3d_desc->u.buffer.count = desc->u.Buffer.NumElements;
2485 break;
2487 case D3D11_UAV_DIMENSION_TEXTURE1D:
2488 wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
2489 wined3d_desc->u.texture.layer_idx = 0;
2490 wined3d_desc->u.texture.layer_count = 1;
2491 break;
2493 case D3D11_UAV_DIMENSION_TEXTURE1DARRAY:
2494 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2495 wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
2496 wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
2497 wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
2498 break;
2500 case D3D11_UAV_DIMENSION_TEXTURE2D:
2501 wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
2502 wined3d_desc->u.texture.layer_idx = 0;
2503 wined3d_desc->u.texture.layer_count = 1;
2504 break;
2506 case D3D11_UAV_DIMENSION_TEXTURE2DARRAY:
2507 wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY;
2508 wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
2509 wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
2510 wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
2511 break;
2513 case D3D11_UAV_DIMENSION_TEXTURE3D:
2514 wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MipSlice;
2515 wined3d_desc->u.texture.layer_idx = desc->u.Texture3D.FirstWSlice;
2516 wined3d_desc->u.texture.layer_count = desc->u.Texture3D.WSize;
2517 break;
2519 default:
2520 WARN("Unrecognized view dimension %#x.\n", desc->ViewDimension);
2521 return E_FAIL;
2524 return S_OK;
2527 static HRESULT d3d11_unordered_access_view_init(struct d3d11_unordered_access_view *view,
2528 struct d3d_device *device, ID3D11Resource *resource, const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc)
2530 struct wined3d_resource *wined3d_resource;
2531 struct wined3d_view_desc wined3d_desc;
2532 HRESULT hr;
2534 view->ID3D11UnorderedAccessView_iface.lpVtbl = &d3d11_unordered_access_view_vtbl;
2535 view->refcount = 1;
2537 if (!desc)
2539 hr = set_uav_desc_from_resource(&view->desc, resource);
2541 else
2543 view->desc = *desc;
2544 hr = normalize_uav_desc(&view->desc, resource);
2546 if (FAILED(hr))
2547 return hr;
2549 if (FAILED(hr = wined3d_unordered_access_view_desc_from_d3d11(&wined3d_desc, &view->desc)))
2550 return hr;
2552 wined3d_mutex_lock();
2553 if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
2555 wined3d_mutex_unlock();
2556 ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
2557 return E_FAIL;
2560 if (FAILED(hr = wined3d_unordered_access_view_create(&wined3d_desc, wined3d_resource,
2561 view, &d3d11_unordered_access_view_wined3d_parent_ops, &view->wined3d_view)))
2563 wined3d_mutex_unlock();
2564 WARN("Failed to create wined3d unordered access view, hr %#x.\n", hr);
2565 return hr;
2568 wined3d_private_store_init(&view->private_store);
2569 wined3d_mutex_unlock();
2570 view->resource = resource;
2571 ID3D11Device2_AddRef(view->device = &device->ID3D11Device2_iface);
2573 return S_OK;
2576 HRESULT d3d11_unordered_access_view_create(struct d3d_device *device, ID3D11Resource *resource,
2577 const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, struct d3d11_unordered_access_view **view)
2579 struct d3d11_unordered_access_view *object;
2580 HRESULT hr;
2582 if (!(object = heap_alloc_zero(sizeof(*object))))
2583 return E_OUTOFMEMORY;
2585 if (FAILED(hr = d3d11_unordered_access_view_init(object, device, resource, desc)))
2587 WARN("Failed to initialize unordered access view, hr %#x.\n", hr);
2588 heap_free(object);
2589 return hr;
2592 TRACE("Created unordered access view %p.\n", object);
2593 *view = object;
2595 return S_OK;
2598 struct d3d11_unordered_access_view *unsafe_impl_from_ID3D11UnorderedAccessView(ID3D11UnorderedAccessView *iface)
2600 if (!iface)
2601 return NULL;
2602 assert(iface->lpVtbl == &d3d11_unordered_access_view_vtbl);
2604 return impl_from_ID3D11UnorderedAccessView(iface);