Debian Codegraph @

Source/Core/VideoBackends/D3D/DXTexture.cpp

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