DXTexture.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/D3D/DXTexture.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include "Common/Assert.h"
  7. #include "Common/CommonTypes.h"
  8. #include "Common/Logging/Log.h"
  9. #include "VideoBackends/D3D/D3DState.h"
  10. #include "VideoBackends/D3DCommon/D3DCommon.h"
  11. #include "VideoCommon/VideoConfig.h"
  12. namespace DX11
  13. {
  14. DXTexture::DXTexture(const TextureConfig& config, ComPtr<ID3D11Texture2D> texture,
  15. std::string_view name)
  16. : AbstractTexture(config), m_texture(std::move(texture)), m_name(name)
  17. {
  18. if (!m_name.empty())
  19. {
  20. m_texture->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast<UINT>(m_name.size()),
  21. m_name.c_str());
  22. }
  23. }
  24. DXTexture::~DXTexture()
  25. {
  26. if (m_srv && D3D::stateman->UnsetTexture(m_srv.Get()) != 0)
  27. D3D::stateman->ApplyTextures();
  28. }
  29. std::unique_ptr<DXTexture> DXTexture::Create(const TextureConfig& config, std::string_view name)
  30. {
  31. // Use typeless to create the texture when it's a render target, so we can alias it with an
  32. // integer format (for EFB).
  33. const DXGI_FORMAT tex_format =
  34. D3DCommon::GetDXGIFormatForAbstractFormat(config.format, config.IsRenderTarget());
  35. UINT bindflags = D3D11_BIND_SHADER_RESOURCE;
  36. if (config.IsRenderTarget())
  37. bindflags |= IsDepthFormat(config.format) ? D3D11_BIND_DEPTH_STENCIL : D3D11_BIND_RENDER_TARGET;
  38. if (config.IsComputeImage())
  39. bindflags |= D3D11_BIND_UNORDERED_ACCESS;
  40. CD3D11_TEXTURE2D_DESC desc(
  41. tex_format, config.width, config.height, config.layers, config.levels, bindflags,
  42. D3D11_USAGE_DEFAULT, 0, config.samples, 0,
  43. config.type == AbstractTextureType::Texture_CubeMap ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0);
  44. ComPtr<ID3D11Texture2D> d3d_texture;
  45. HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, d3d_texture.GetAddressOf());
  46. if (FAILED(hr))
  47. {
  48. PanicAlertFmt("Failed to create {}x{}x{} D3D backing texture: {}", config.width, config.height,
  49. config.layers, DX11HRWrap(hr));
  50. return nullptr;
  51. }
  52. std::unique_ptr<DXTexture> tex(new DXTexture(config, std::move(d3d_texture), name));
  53. if (!tex->CreateSRV() || (config.IsComputeImage() && !tex->CreateUAV()))
  54. return nullptr;
  55. return tex;
  56. }
  57. std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ComPtr<ID3D11Texture2D> texture)
  58. {
  59. D3D11_TEXTURE2D_DESC desc;
  60. texture->GetDesc(&desc);
  61. // Convert to our texture config format.
  62. TextureConfig config(desc.Width, desc.Height, desc.MipLevels, desc.ArraySize,
  63. desc.SampleDesc.Count,
  64. D3DCommon::GetAbstractFormatForDXGIFormat(desc.Format), 0,
  65. AbstractTextureType::Texture_2DArray);
  66. if (desc.BindFlags & D3D11_BIND_RENDER_TARGET)
  67. config.flags |= AbstractTextureFlag_RenderTarget;
  68. if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS)
  69. config.flags |= AbstractTextureFlag_ComputeImage;
  70. std::unique_ptr<DXTexture> tex(new DXTexture(config, std::move(texture), ""));
  71. if (desc.BindFlags & D3D11_BIND_SHADER_RESOURCE && !tex->CreateSRV())
  72. return nullptr;
  73. if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS && !tex->CreateUAV())
  74. return nullptr;
  75. return tex;
  76. }
  77. bool DXTexture::CreateSRV()
  78. {
  79. D3D_SRV_DIMENSION dimension = D3D_SRV_DIMENSION_TEXTURE2DARRAY;
  80. if (m_config.type == AbstractTextureType::Texture_2DArray)
  81. {
  82. if (m_config.IsMultisampled())
  83. dimension = D3D_SRV_DIMENSION_TEXTURE2DMSARRAY;
  84. else
  85. dimension = D3D_SRV_DIMENSION_TEXTURE2DARRAY;
  86. }
  87. else if (m_config.type == AbstractTextureType::Texture_2D)
  88. {
  89. if (m_config.IsMultisampled())
  90. dimension = D3D_SRV_DIMENSION_TEXTURE2DMS;
  91. else
  92. dimension = D3D_SRV_DIMENSION_TEXTURE2D;
  93. }
  94. else if (m_config.type == AbstractTextureType::Texture_CubeMap)
  95. {
  96. dimension = D3D_SRV_DIMENSION_TEXTURECUBE;
  97. }
  98. else
  99. {
  100. PanicAlertFmt("Failed to create D3D SRV - unhandled type");
  101. return false;
  102. }
  103. const CD3D11_SHADER_RESOURCE_VIEW_DESC desc(
  104. m_texture.Get(), dimension, D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0,
  105. m_config.levels, 0, m_config.layers);
  106. DEBUG_ASSERT(!m_srv);
  107. HRESULT hr = D3D::device->CreateShaderResourceView(m_texture.Get(), &desc, m_srv.GetAddressOf());
  108. if (FAILED(hr))
  109. {
  110. PanicAlertFmt("Failed to create {}x{}x{} D3D SRV: {}", m_config.width, m_config.height,
  111. m_config.layers, DX11HRWrap(hr));
  112. return false;
  113. }
  114. return true;
  115. }
  116. bool DXTexture::CreateUAV()
  117. {
  118. const CD3D11_UNORDERED_ACCESS_VIEW_DESC desc(
  119. m_texture.Get(), D3D11_UAV_DIMENSION_TEXTURE2DARRAY,
  120. D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, 0, m_config.layers);
  121. DEBUG_ASSERT(!m_uav);
  122. HRESULT hr = D3D::device->CreateUnorderedAccessView(m_texture.Get(), &desc, m_uav.GetAddressOf());
  123. if (FAILED(hr))
  124. {
  125. PanicAlertFmt("Failed to create {}x{}x{} D3D UAV: {}", m_config.width, m_config.height,
  126. m_config.layers, DX11HRWrap(hr));
  127. return false;
  128. }
  129. return true;
  130. }
  131. void DXTexture::CopyRectangleFromTexture(const AbstractTexture* src,
  132. const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
  133. u32 src_level, const MathUtil::Rectangle<int>& dst_rect,
  134. u32 dst_layer, u32 dst_level)
  135. {
  136. const DXTexture* srcentry = static_cast<const DXTexture*>(src);
  137. ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() &&
  138. src_rect.GetHeight() == dst_rect.GetHeight());
  139. D3D11_BOX src_box;
  140. src_box.left = src_rect.left;
  141. src_box.top = src_rect.top;
  142. src_box.right = src_rect.right;
  143. src_box.bottom = src_rect.bottom;
  144. src_box.front = 0;
  145. src_box.back = 1;
  146. D3D::context->CopySubresourceRegion(
  147. m_texture.Get(), D3D11CalcSubresource(dst_level, dst_layer, m_config.levels), dst_rect.left,
  148. dst_rect.top, 0, srcentry->m_texture.Get(),
  149. D3D11CalcSubresource(src_level, src_layer, srcentry->m_config.levels), &src_box);
  150. }
  151. void DXTexture::ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect,
  152. u32 layer, u32 level)
  153. {
  154. const DXTexture* srcentry = static_cast<const DXTexture*>(src);
  155. DEBUG_ASSERT(m_config.samples > 1 && m_config.width == srcentry->m_config.width &&
  156. m_config.height == srcentry->m_config.height && m_config.samples == 1);
  157. DEBUG_ASSERT(rect.left + rect.GetWidth() <= static_cast<int>(srcentry->m_config.width) &&
  158. rect.top + rect.GetHeight() <= static_cast<int>(srcentry->m_config.height));
  159. D3D::context->ResolveSubresource(
  160. m_texture.Get(), D3D11CalcSubresource(level, layer, m_config.levels),
  161. srcentry->m_texture.Get(), D3D11CalcSubresource(level, layer, srcentry->m_config.levels),
  162. D3DCommon::GetDXGIFormatForAbstractFormat(m_config.format, false));
  163. }
  164. void DXTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer,
  165. size_t buffer_size, u32 layer)
  166. {
  167. size_t src_pitch = CalculateStrideForFormat(m_config.format, row_length);
  168. D3D::context->UpdateSubresource(m_texture.Get(),
  169. D3D11CalcSubresource(level, layer, m_config.levels), nullptr,
  170. buffer, static_cast<UINT>(src_pitch), 0);
  171. }
  172. DXStagingTexture::DXStagingTexture(StagingTextureType type, const TextureConfig& config,
  173. ComPtr<ID3D11Texture2D> tex)
  174. : AbstractStagingTexture(type, config), m_tex(std::move(tex))
  175. {
  176. }
  177. DXStagingTexture::~DXStagingTexture()
  178. {
  179. if (IsMapped())
  180. DXStagingTexture::Unmap();
  181. }
  182. std::unique_ptr<DXStagingTexture> DXStagingTexture::Create(StagingTextureType type,
  183. const TextureConfig& config)
  184. {
  185. D3D11_USAGE usage;
  186. UINT cpu_flags;
  187. if (type == StagingTextureType::Readback)
  188. {
  189. usage = D3D11_USAGE_STAGING;
  190. cpu_flags = D3D11_CPU_ACCESS_READ;
  191. }
  192. else if (type == StagingTextureType::Upload)
  193. {
  194. usage = D3D11_USAGE_DYNAMIC;
  195. cpu_flags = D3D11_CPU_ACCESS_WRITE;
  196. }
  197. else
  198. {
  199. usage = D3D11_USAGE_STAGING;
  200. cpu_flags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
  201. }
  202. CD3D11_TEXTURE2D_DESC desc(D3DCommon::GetDXGIFormatForAbstractFormat(config.format, false),
  203. config.width, config.height, 1, 1, 0, usage, cpu_flags);
  204. ComPtr<ID3D11Texture2D> texture;
  205. HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, texture.GetAddressOf());
  206. ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create staging texture: {}", DX11HRWrap(hr));
  207. if (FAILED(hr))
  208. return nullptr;
  209. return std::unique_ptr<DXStagingTexture>(new DXStagingTexture(type, config, std::move(texture)));
  210. }
  211. void DXStagingTexture::CopyFromTexture(const AbstractTexture* src,
  212. const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
  213. u32 src_level, const MathUtil::Rectangle<int>& dst_rect)
  214. {
  215. ASSERT(m_type == StagingTextureType::Readback || m_type == StagingTextureType::Mutable);
  216. ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() &&
  217. src_rect.GetHeight() == dst_rect.GetHeight());
  218. ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= src->GetWidth() &&
  219. src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= src->GetHeight());
  220. ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= m_config.width &&
  221. dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= m_config.height);
  222. if (IsMapped())
  223. DXStagingTexture::Unmap();
  224. if (static_cast<u32>(src_rect.GetWidth()) == GetWidth() &&
  225. static_cast<u32>(src_rect.GetHeight()) == GetHeight())
  226. {
  227. // Copy whole resource, needed for depth textures.
  228. D3D::context->CopySubresourceRegion(
  229. m_tex.Get(), 0, 0, 0, 0, static_cast<const DXTexture*>(src)->GetD3DTexture(),
  230. D3D11CalcSubresource(src_level, src_layer, src->GetLevels()), nullptr);
  231. }
  232. else
  233. {
  234. CD3D11_BOX src_box(src_rect.left, src_rect.top, 0, src_rect.right, src_rect.bottom, 1);
  235. D3D::context->CopySubresourceRegion(
  236. m_tex.Get(), 0, static_cast<u32>(dst_rect.left), static_cast<u32>(dst_rect.top), 0,
  237. static_cast<const DXTexture*>(src)->GetD3DTexture(),
  238. D3D11CalcSubresource(src_level, src_layer, src->GetLevels()), &src_box);
  239. }
  240. m_needs_flush = true;
  241. }
  242. void DXStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
  243. const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer,
  244. u32 dst_level)
  245. {
  246. ASSERT(m_type == StagingTextureType::Upload);
  247. ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() &&
  248. src_rect.GetHeight() == dst_rect.GetHeight());
  249. ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= GetWidth() &&
  250. src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= GetHeight());
  251. ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= dst->GetWidth() &&
  252. dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= dst->GetHeight());
  253. if (IsMapped())
  254. DXStagingTexture::Unmap();
  255. if (static_cast<u32>(src_rect.GetWidth()) == dst->GetWidth() &&
  256. static_cast<u32>(src_rect.GetHeight()) == dst->GetHeight())
  257. {
  258. D3D::context->CopySubresourceRegion(
  259. static_cast<const DXTexture*>(dst)->GetD3DTexture(),
  260. D3D11CalcSubresource(dst_level, dst_layer, dst->GetLevels()), 0, 0, 0, m_tex.Get(), 0,
  261. nullptr);
  262. }
  263. else
  264. {
  265. CD3D11_BOX src_box(src_rect.left, src_rect.top, 0, src_rect.right, src_rect.bottom, 1);
  266. D3D::context->CopySubresourceRegion(
  267. static_cast<const DXTexture*>(dst)->GetD3DTexture(),
  268. D3D11CalcSubresource(dst_level, dst_layer, dst->GetLevels()),
  269. static_cast<u32>(dst_rect.left), static_cast<u32>(dst_rect.top), 0, m_tex.Get(), 0,
  270. &src_box);
  271. }
  272. }
  273. bool DXStagingTexture::Map()
  274. {
  275. if (m_map_pointer)
  276. return true;
  277. D3D11_MAP map_type;
  278. if (m_type == StagingTextureType::Readback)
  279. map_type = D3D11_MAP_READ;
  280. else if (m_type == StagingTextureType::Upload)
  281. map_type = D3D11_MAP_WRITE;
  282. else
  283. map_type = D3D11_MAP_READ_WRITE;
  284. D3D11_MAPPED_SUBRESOURCE sr;
  285. HRESULT hr = D3D::context->Map(m_tex.Get(), 0, map_type, 0, &sr);
  286. ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to map readback texture: {}", DX11HRWrap(hr));
  287. if (FAILED(hr))
  288. return false;
  289. m_map_pointer = reinterpret_cast<char*>(sr.pData);
  290. m_map_stride = sr.RowPitch;
  291. return true;
  292. }
  293. void DXStagingTexture::Unmap()
  294. {
  295. if (!m_map_pointer)
  296. return;
  297. D3D::context->Unmap(m_tex.Get(), 0);
  298. m_map_pointer = nullptr;
  299. }
  300. void DXStagingTexture::Flush()
  301. {
  302. // Flushing is handled by the API.
  303. m_needs_flush = false;
  304. }
  305. DXFramebuffer::DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
  306. std::vector<AbstractTexture*> additional_color_attachments,
  307. AbstractTextureFormat color_format, AbstractTextureFormat depth_format,
  308. u32 width, u32 height, u32 layers, u32 samples,
  309. ComPtr<ID3D11RenderTargetView> rtv,
  310. ComPtr<ID3D11RenderTargetView> integer_rtv,
  311. ComPtr<ID3D11DepthStencilView> dsv,
  312. std::vector<ComPtr<ID3D11RenderTargetView>> additional_rtvs)
  313. : AbstractFramebuffer(color_attachment, depth_attachment,
  314. std::move(additional_color_attachments), color_format, depth_format,
  315. width, height, layers, samples),
  316. m_integer_rtv(std::move(integer_rtv)), m_dsv(std::move(dsv))
  317. {
  318. m_render_targets.push_back(std::move(rtv));
  319. for (auto additional_rtv : additional_rtvs)
  320. {
  321. m_render_targets.push_back(std::move(additional_rtv));
  322. }
  323. for (auto& render_target : m_render_targets)
  324. {
  325. m_render_targets_raw.push_back(render_target.Get());
  326. }
  327. }
  328. DXFramebuffer::~DXFramebuffer() = default;
  329. void DXFramebuffer::Unbind()
  330. {
  331. bool should_apply = false;
  332. if (GetColorAttachment() &&
  333. D3D::stateman->UnsetTexture(static_cast<DXTexture*>(GetColorAttachment())->GetD3DSRV()) != 0)
  334. {
  335. should_apply = true;
  336. }
  337. if (GetDepthAttachment() &&
  338. D3D::stateman->UnsetTexture(static_cast<DXTexture*>(GetDepthAttachment())->GetD3DSRV()) != 0)
  339. {
  340. should_apply = true;
  341. }
  342. for (auto additional_color_attachment : m_additional_color_attachments)
  343. {
  344. if (D3D::stateman->UnsetTexture(
  345. static_cast<DXTexture*>(additional_color_attachment)->GetD3DSRV()) != 0)
  346. {
  347. should_apply = true;
  348. }
  349. }
  350. if (should_apply)
  351. {
  352. D3D::stateman->ApplyTextures();
  353. }
  354. }
  355. void DXFramebuffer::Clear(const ClearColor& color_value, float depth_value)
  356. {
  357. if (GetDepthFormat() != AbstractTextureFormat::Undefined)
  358. {
  359. D3D::context->ClearDepthStencilView(GetDSV(), D3D11_CLEAR_DEPTH, depth_value, 0);
  360. }
  361. for (auto render_target : m_render_targets_raw)
  362. {
  363. D3D::context->ClearRenderTargetView(render_target, color_value.data());
  364. }
  365. }
  366. std::unique_ptr<DXFramebuffer>
  367. DXFramebuffer::Create(DXTexture* color_attachment, DXTexture* depth_attachment,
  368. std::vector<AbstractTexture*> additional_color_attachments)
  369. {
  370. if (!ValidateConfig(color_attachment, depth_attachment, additional_color_attachments))
  371. return nullptr;
  372. const AbstractTextureFormat color_format =
  373. color_attachment ? color_attachment->GetFormat() : AbstractTextureFormat::Undefined;
  374. const AbstractTextureFormat depth_format =
  375. depth_attachment ? depth_attachment->GetFormat() : AbstractTextureFormat::Undefined;
  376. const DXTexture* either_attachment = color_attachment ? color_attachment : depth_attachment;
  377. const u32 width = either_attachment->GetWidth();
  378. const u32 height = either_attachment->GetHeight();
  379. const u32 layers = either_attachment->GetLayers();
  380. const u32 samples = either_attachment->GetSamples();
  381. ComPtr<ID3D11RenderTargetView> rtv;
  382. ComPtr<ID3D11RenderTargetView> integer_rtv;
  383. if (color_attachment)
  384. {
  385. CD3D11_RENDER_TARGET_VIEW_DESC desc(
  386. color_attachment->IsMultisampled() ? D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY :
  387. D3D11_RTV_DIMENSION_TEXTURE2DARRAY,
  388. D3DCommon::GetRTVFormatForAbstractFormat(color_attachment->GetFormat(), false), 0, 0,
  389. color_attachment->GetLayers());
  390. HRESULT hr = D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc,
  391. rtv.GetAddressOf());
  392. ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create render target view for framebuffer: {}",
  393. DX11HRWrap(hr));
  394. if (FAILED(hr))
  395. return nullptr;
  396. // Only create the integer RTV when logic ops are supported (Win8+).
  397. DXGI_FORMAT integer_format =
  398. D3DCommon::GetRTVFormatForAbstractFormat(color_attachment->GetFormat(), true);
  399. if (g_ActiveConfig.backend_info.bSupportsLogicOp && integer_format != desc.Format)
  400. {
  401. desc.Format = integer_format;
  402. hr = D3D::device->CreateRenderTargetView(color_attachment->GetD3DTexture(), &desc,
  403. integer_rtv.GetAddressOf());
  404. ASSERT_MSG(VIDEO, SUCCEEDED(hr),
  405. "Failed to create integer render target view for framebuffer: {}", DX11HRWrap(hr));
  406. }
  407. }
  408. std::vector<ComPtr<ID3D11RenderTargetView>> additional_rtvs;
  409. for (auto additional_color_attachment : additional_color_attachments)
  410. {
  411. ComPtr<ID3D11RenderTargetView> additional_rtv;
  412. CD3D11_RENDER_TARGET_VIEW_DESC desc(
  413. additional_color_attachment->IsMultisampled() ? D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY :
  414. D3D11_RTV_DIMENSION_TEXTURE2DARRAY,
  415. D3DCommon::GetRTVFormatForAbstractFormat(additional_color_attachment->GetFormat(), false),
  416. 0, 0, 1);
  417. HRESULT hr = D3D::device->CreateRenderTargetView(
  418. static_cast<DXTexture*>(additional_color_attachment)->GetD3DTexture(), &desc,
  419. additional_rtv.GetAddressOf());
  420. ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Create render target view for framebuffer: {}",
  421. DX11HRWrap(hr));
  422. if (FAILED(hr))
  423. return nullptr;
  424. additional_rtvs.push_back(std::move(additional_rtv));
  425. }
  426. ComPtr<ID3D11DepthStencilView> dsv;
  427. if (depth_attachment)
  428. {
  429. const CD3D11_DEPTH_STENCIL_VIEW_DESC desc(
  430. depth_attachment->GetConfig().IsMultisampled() ? D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY :
  431. D3D11_DSV_DIMENSION_TEXTURE2DARRAY,
  432. D3DCommon::GetDSVFormatForAbstractFormat(depth_attachment->GetFormat()), 0, 0,
  433. depth_attachment->GetLayers(), 0);
  434. HRESULT hr = D3D::device->CreateDepthStencilView(depth_attachment->GetD3DTexture(), &desc,
  435. dsv.GetAddressOf());
  436. ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create depth stencil view for framebuffer: {}",
  437. DX11HRWrap(hr));
  438. if (FAILED(hr))
  439. return nullptr;
  440. }
  441. return std::make_unique<DXFramebuffer>(
  442. color_attachment, depth_attachment, std::move(additional_color_attachments), color_format,
  443. depth_format, width, height, layers, samples, std::move(rtv), std::move(integer_rtv),
  444. std::move(dsv), std::move(additional_rtvs));
  445. }
  446. } // namespace DX11