| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D12/DX12Texture.h" |
| 5 | |
| 6 | #include "Common/Align.h" |
| 7 | #include "Common/Assert.h" |
| 8 | #include "Common/StringUtil.h" |
| 9 | |
| 10 | #include "VideoBackends/D3D12/Common.h" |
| 11 | #include "VideoBackends/D3D12/D3D12Gfx.h" |
| 12 | #include "VideoBackends/D3D12/D3D12StreamBuffer.h" |
| 13 | #include "VideoBackends/D3D12/DX12Context.h" |
| 14 | #include "VideoBackends/D3D12/DescriptorHeapManager.h" |
| 15 | |
| 16 | namespace DX12 |
| 17 | { |
| 18 | static D3D12_BOX RectangleToBox(const MathUtil::Rectangle<int>& rc) |
| 19 | { |
| 20 | return D3D12_BOX{static_cast<UINT>(rc.left), static_cast<UINT>(rc.top), 0, |
| 21 | static_cast<UINT>(rc.right), static_cast<UINT>(rc.bottom), 1}; |
| 22 | } |
| 23 | |
| 24 | static ComPtr<ID3D12Resource> CreateTextureUploadBuffer(u32 buffer_size) |
| 25 | { |
| 26 | const D3D12_HEAP_PROPERTIES heap_properties = {D3D12_HEAP_TYPE_UPLOAD}; |
| 27 | const D3D12_RESOURCE_DESC desc = {D3D12_RESOURCE_DIMENSION_BUFFER, |
| 28 | 0, |
| 29 | buffer_size, |
| 30 | 1, |
| 31 | 1, |
| 32 | 1, |
| 33 | DXGI_FORMAT_UNKNOWN, |
| 34 | {1, 0}, |
| 35 | D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 36 | D3D12_RESOURCE_FLAG_NONE}; |
| 37 | |
| 38 | ComPtr<ID3D12Resource> resource; |
| 39 | HRESULT hr = g_dx_context->GetDevice()->CreateCommittedResource( |
| 40 | &heap_properties, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, |
| 41 | IID_PPV_ARGS(&resource)); |
| 42 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create texture upload buffer: {}", DX12HRWrap(hr)); |
| 43 | return resource; |
| 44 | } |
| 45 | |
| 46 | DXTexture::DXTexture(const TextureConfig& config, ID3D12Resource* resource, |
| 47 | D3D12_RESOURCE_STATES state, std::string_view name) |
| 48 | : AbstractTexture(config), m_resource(resource), m_state(state), m_name(UTF8ToWString(name)) |
| 49 | { |
| 50 | if (!m_name.empty()) |
| 51 | { |
| 52 | resource->SetName(m_name.c_str()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | DXTexture::~DXTexture() |
| 57 | { |
| 58 | if (m_uav_descriptor) |
| 59 | { |
| 60 | g_dx_context->DeferDescriptorDestruction(g_dx_context->GetDescriptorHeapManager(), |
| 61 | m_uav_descriptor.index); |
| 62 | } |
| 63 | |
| 64 | if (m_srv_descriptor) |
| 65 | { |
| 66 | g_dx_context->DeferDescriptorDestruction(g_dx_context->GetDescriptorHeapManager(), |
| 67 | m_srv_descriptor.index); |
| 68 | } |
| 69 | if (m_resource) |
| 70 | g_dx_context->DeferResourceDestruction(m_resource.Get()); |
| 71 | } |
| 72 | |
| 73 | std::unique_ptr<DXTexture> DXTexture::Create(const TextureConfig& config, std::string_view name) |
| 74 | { |
| 75 | constexpr D3D12_HEAP_PROPERTIES heap_properties = {D3D12_HEAP_TYPE_DEFAULT}; |
| 76 | D3D12_RESOURCE_STATES resource_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; |
| 77 | D3D12_RESOURCE_FLAGS resource_flags = D3D12_RESOURCE_FLAG_NONE; |
| 78 | if (config.IsRenderTarget()) |
| 79 | { |
| 80 | if (IsDepthFormat(config.format)) |
| 81 | { |
| 82 | resource_state = D3D12_RESOURCE_STATE_DEPTH_WRITE; |
| 83 | resource_flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | resource_state = D3D12_RESOURCE_STATE_RENDER_TARGET; |
| 88 | resource_flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; |
| 89 | } |
| 90 | } |
| 91 | if (config.IsComputeImage()) |
| 92 | resource_flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; |
| 93 | |
| 94 | const D3D12_RESOURCE_DESC resource_desc = { |
| 95 | D3D12_RESOURCE_DIMENSION_TEXTURE2D, |
| 96 | 0, |
| 97 | config.width, |
| 98 | config.height, |
| 99 | static_cast<u16>(config.layers), |
| 100 | static_cast<u16>(config.levels), |
| 101 | D3DCommon::GetDXGIFormatForAbstractFormat(config.format, config.IsRenderTarget()), |
| 102 | {config.samples, 0}, |
| 103 | D3D12_TEXTURE_LAYOUT_UNKNOWN, |
| 104 | resource_flags}; |
| 105 | |
| 106 | D3D12_CLEAR_VALUE optimized_clear_value = {}; |
| 107 | if (config.IsRenderTarget()) |
| 108 | { |
| 109 | optimized_clear_value.Format = |
| 110 | IsDepthFormat(config.format) ? |
| 111 | D3DCommon::GetDSVFormatForAbstractFormat(config.format) : |
| 112 | D3DCommon::GetRTVFormatForAbstractFormat(config.format, false); |
| 113 | } |
| 114 | |
| 115 | ComPtr<ID3D12Resource> resource; |
| 116 | HRESULT hr = g_dx_context->GetDevice()->CreateCommittedResource( |
| 117 | &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, resource_state, |
| 118 | config.IsRenderTarget() ? &optimized_clear_value : nullptr, IID_PPV_ARGS(&resource)); |
| 119 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create D3D12 texture resource: {}", DX12HRWrap(hr)); |
| 120 | if (FAILED(hr)) |
| 121 | return nullptr; |
| 122 | |
| 123 | auto tex = |
| 124 | std::unique_ptr<DXTexture>(new DXTexture(config, resource.Get(), resource_state, name)); |
| 125 | if (!tex->CreateSRVDescriptor() || (config.IsComputeImage() && !tex->CreateUAVDescriptor())) |
| 126 | return nullptr; |
| 127 | |
| 128 | return tex; |
| 129 | } |
| 130 | |
| 131 | std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ID3D12Resource* resource) |
| 132 | { |
| 133 | const D3D12_RESOURCE_DESC desc = resource->GetDesc(); |
| 134 | const AbstractTextureFormat format = D3DCommon::GetAbstractFormatForDXGIFormat(desc.Format); |
| 135 | if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || |
| 136 | format == AbstractTextureFormat::Undefined) |
| 137 | { |
| 138 | PanicAlertFmt("Unknown format for adopted texture"); |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| 142 | TextureConfig config(static_cast<u32>(desc.Width), desc.Height, desc.MipLevels, |
| 143 | desc.DepthOrArraySize, desc.SampleDesc.Count, format, 0, |
| 144 | AbstractTextureType::Texture_2DArray); |
| 145 | if (desc.Flags & |
| 146 | (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) |
| 147 | { |
| 148 | config.flags |= AbstractTextureFlag_RenderTarget; |
| 149 | } |
| 150 | if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) |
| 151 | config.flags |= AbstractTextureFlag_ComputeImage; |
| 152 | |
| 153 | auto tex = |
| 154 | std::unique_ptr<DXTexture>(new DXTexture(config, resource, D3D12_RESOURCE_STATE_COMMON, "")); |
| 155 | if (!tex->CreateSRVDescriptor()) |
| 156 | return nullptr; |
| 157 | |
| 158 | return tex; |
| 159 | } |
| 160 | |
| 161 | bool DXTexture::CreateSRVDescriptor() |
| 162 | { |
| 163 | if (!g_dx_context->GetDescriptorHeapManager().Allocate(&m_srv_descriptor)) |
| 164 | { |
| 165 | PanicAlertFmt("Failed to allocate SRV descriptor"); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | D3D12_SRV_DIMENSION dimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; |
| 170 | if (m_config.type == AbstractTextureType::Texture_2DArray) |
| 171 | { |
| 172 | if (m_config.IsMultisampled()) |
| 173 | dimension = D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY; |
| 174 | else |
| 175 | dimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; |
| 176 | } |
| 177 | else if (m_config.type == AbstractTextureType::Texture_2D) |
| 178 | { |
| 179 | if (m_config.IsMultisampled()) |
| 180 | dimension = D3D12_SRV_DIMENSION_TEXTURE2DMS; |
| 181 | else |
| 182 | dimension = D3D12_SRV_DIMENSION_TEXTURE2D; |
| 183 | } |
| 184 | else if (m_config.type == AbstractTextureType::Texture_CubeMap) |
| 185 | { |
| 186 | dimension = D3D12_SRV_DIMENSION_TEXTURECUBE; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | PanicAlertFmt("Failed to allocate SRV - unhandled type"); |
| 191 | return false; |
| 192 | } |
| 193 | D3D12_SHADER_RESOURCE_VIEW_DESC desc = {D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), |
| 194 | dimension, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; |
| 195 | |
| 196 | if (m_config.type == AbstractTextureType::Texture_CubeMap) |
| 197 | { |
| 198 | desc.TextureCube.MostDetailedMip = 0; |
| 199 | desc.TextureCube.MipLevels = m_config.levels; |
| 200 | desc.TextureCube.ResourceMinLODClamp = 0.0f; |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | if (m_config.IsMultisampled()) |
| 205 | { |
| 206 | desc.Texture2DMSArray.ArraySize = m_config.layers; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | desc.Texture2DArray.MipLevels = m_config.levels; |
| 211 | desc.Texture2DArray.ArraySize = m_config.layers; |
| 212 | } |
| 213 | } |
| 214 | g_dx_context->GetDevice()->CreateShaderResourceView(m_resource.Get(), &desc, |
| 215 | m_srv_descriptor.cpu_handle); |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | bool DXTexture::CreateUAVDescriptor() |
| 220 | { |
| 221 | if (!g_dx_context->GetDescriptorHeapManager().Allocate(&m_uav_descriptor)) |
| 222 | { |
| 223 | PanicAlertFmt("Failed to allocate UAV descriptor"); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | D3D12_UNORDERED_ACCESS_VIEW_DESC desc = { |
| 228 | D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), |
| 229 | D3D12_UAV_DIMENSION_TEXTURE2DARRAY}; |
| 230 | desc.Texture2DArray.ArraySize = m_config.layers; |
| 231 | g_dx_context->GetDevice()->CreateUnorderedAccessView(m_resource.Get(), nullptr, &desc, |
| 232 | m_uav_descriptor.cpu_handle); |
| 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | void DXTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, |
| 238 | size_t buffer_size, u32 layer) |
| 239 | { |
| 240 | // Textures greater than 1024*1024 will be put in staging textures that are released after |
| 241 | // execution instead. A 2048x2048 texture is 16MB, and we'd only fit four of these in our |
| 242 | // streaming buffer and be blocking frequently. Games are unlikely to have textures this |
| 243 | // large anyway, so it's only really an issue for HD texture packs, and memory is not |
| 244 | // a limiting factor in these scenarios anyway. |
| 245 | constexpr u32 STAGING_BUFFER_UPLOAD_THRESHOLD = 1024 * 1024 * 4; |
| 246 | |
| 247 | // Determine the stride in the stream buffer. It must be aligned to 256 bytes. |
| 248 | const u32 block_size = GetBlockSizeForFormat(GetFormat()); |
| 249 | const u32 num_rows = Common::AlignUp(height, block_size) / block_size; |
| 250 | const u32 source_stride = CalculateStrideForFormat(m_config.format, row_length); |
| 251 | const u32 upload_stride = Common::AlignUp(source_stride, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); |
| 252 | const u32 upload_size = upload_stride * num_rows; |
| 253 | |
| 254 | // Both paths need us in COPY_DEST state, and avoids switching back and forth for mips. |
| 255 | TransitionToState(D3D12_RESOURCE_STATE_COPY_DEST); |
| 256 | |
| 257 | ComPtr<ID3D12Resource> staging_buffer; |
| 258 | ID3D12Resource* upload_buffer_resource; |
| 259 | void* upload_buffer_ptr; |
| 260 | u32 upload_buffer_offset; |
| 261 | if (upload_size >= STAGING_BUFFER_UPLOAD_THRESHOLD) |
| 262 | { |
| 263 | const D3D12_RANGE read_range = {0, 0}; |
| 264 | staging_buffer = CreateTextureUploadBuffer(upload_size); |
| 265 | if (!staging_buffer) |
| 266 | { |
| 267 | PanicAlertFmt("Failed to allocate temporary texture upload buffer"); |
| 268 | return; |
| 269 | } |
| 270 | HRESULT hr = staging_buffer->Map(0, &read_range, &upload_buffer_ptr); |
| 271 | if (FAILED(hr)) |
| 272 | { |
| 273 | PanicAlertFmt("Failed to map temporary texture upload buffer: {}", DX12HRWrap(hr)); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | // We defer releasing the buffer until after the command list with the copy has executed. |
| 278 | g_dx_context->DeferResourceDestruction(staging_buffer.Get()); |
| 279 | upload_buffer_resource = staging_buffer.Get(); |
| 280 | upload_buffer_offset = 0; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | if (!g_dx_context->GetTextureUploadBuffer().ReserveMemory( |
| 285 | upload_size, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT)) |
| 286 | { |
| 287 | WARN_LOG_FMT(VIDEO, |
| 288 | "Executing command list while waiting for space in texture upload buffer"); |
| 289 | Gfx::GetInstance()->ExecuteCommandList(false); |
| 290 | if (!g_dx_context->GetTextureUploadBuffer().ReserveMemory( |
| 291 | upload_size, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT)) |
| 292 | { |
| 293 | PanicAlertFmt("Failed to allocate texture upload buffer"); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | upload_buffer_resource = g_dx_context->GetTextureUploadBuffer().GetBuffer(); |
| 299 | upload_buffer_ptr = g_dx_context->GetTextureUploadBuffer().GetCurrentHostPointer(); |
| 300 | upload_buffer_offset = g_dx_context->GetTextureUploadBuffer().GetCurrentOffset(); |
| 301 | } |
| 302 | |
| 303 | // Copy in, slow path if the pitch differs. |
| 304 | if (source_stride != upload_stride) |
| 305 | { |
| 306 | const u8* src_ptr = buffer; |
| 307 | const u32 copy_size = std::min(source_stride, upload_stride); |
| 308 | u8* dst_ptr = static_cast<u8*>(upload_buffer_ptr); |
| 309 | for (u32 i = 0; i < num_rows; i++) |
| 310 | { |
| 311 | std::memcpy(dst_ptr, src_ptr, copy_size); |
| 312 | src_ptr += source_stride; |
| 313 | dst_ptr += upload_stride; |
| 314 | } |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | std::memcpy(upload_buffer_ptr, buffer, std::min<size_t>(buffer_size, upload_size)); |
| 319 | } |
| 320 | |
| 321 | if (staging_buffer) |
| 322 | { |
| 323 | const D3D12_RANGE write_range = {0, std::min<size_t>(buffer_size, upload_size)}; |
| 324 | staging_buffer->Unmap(0, &write_range); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | g_dx_context->GetTextureUploadBuffer().CommitMemory(upload_size); |
| 329 | } |
| 330 | |
| 331 | // Issue copy from buffer->texture. |
| 332 | const u32 aligned_width = Common::AlignUp(width, block_size); |
| 333 | const u32 aligned_height = Common::AlignUp(height, block_size); |
| 334 | const D3D12_TEXTURE_COPY_LOCATION dst_loc = {m_resource.Get(), |
| 335 | D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 336 | {static_cast<UINT>(CalcSubresource(level, layer))}}; |
| 337 | const D3D12_TEXTURE_COPY_LOCATION src_loc = { |
| 338 | upload_buffer_resource, |
| 339 | D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, |
| 340 | {{upload_buffer_offset, D3DCommon::GetDXGIFormatForAbstractFormat(m_config.format, false), |
| 341 | aligned_width, aligned_height, 1, upload_stride}}}; |
| 342 | const D3D12_BOX src_box{0, 0, 0, aligned_width, aligned_height, 1}; |
| 343 | g_dx_context->GetCommandList()->CopyTextureRegion(&dst_loc, 0, 0, 0, &src_loc, &src_box); |
| 344 | |
| 345 | // Preemptively transition to shader read only after uploading the last mip level, as we're |
| 346 | // likely finished with writes to this texture for now. |
| 347 | if (level == (m_config.levels - 1)) |
| 348 | TransitionToState(D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); |
| 349 | } |
| 350 | |
| 351 | void DXTexture::CopyRectangleFromTexture(const AbstractTexture* src, |
| 352 | const MathUtil::Rectangle<int>& src_rect, u32 src_layer, |
| 353 | u32 src_level, const MathUtil::Rectangle<int>& dst_rect, |
| 354 | u32 dst_layer, u32 dst_level) |
| 355 | { |
| 356 | const DXTexture* src_dxtex = static_cast<const DXTexture*>(src); |
| 357 | ASSERT(static_cast<u32>(src_rect.right) <= src->GetWidth() && |
| 358 | static_cast<u32>(src_rect.bottom) <= src->GetHeight() && src_layer <= src->GetLayers() && |
| 359 | src_level <= src->GetLevels() && static_cast<u32>(dst_rect.right) <= GetWidth() && |
| 360 | static_cast<u32>(dst_rect.bottom) <= GetHeight() && dst_layer <= GetLayers() && |
| 361 | dst_level <= GetLevels() && src_rect.GetWidth() == dst_rect.GetWidth() && |
| 362 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 363 | const D3D12_TEXTURE_COPY_LOCATION dst_loc = { |
| 364 | m_resource.Get(), |
| 365 | D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 366 | {static_cast<UINT>(CalcSubresource(dst_level, dst_layer))}}; |
| 367 | const D3D12_TEXTURE_COPY_LOCATION src_loc = { |
| 368 | src_dxtex->m_resource.Get(), |
| 369 | D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 370 | {static_cast<UINT>(src_dxtex->CalcSubresource(src_level, src_layer))}}; |
| 371 | const D3D12_BOX src_box = RectangleToBox(src_rect); |
| 372 | const D3D12_RESOURCE_STATES old_src_state = src_dxtex->m_state; |
| 373 | src_dxtex->TransitionToState(D3D12_RESOURCE_STATE_COPY_SOURCE); |
| 374 | TransitionToState(D3D12_RESOURCE_STATE_COPY_DEST); |
| 375 | |
| 376 | g_dx_context->GetCommandList()->CopyTextureRegion(&dst_loc, dst_rect.left, dst_rect.top, 0, |
| 377 | &src_loc, &src_box); |
| 378 | |
| 379 | // Only restore the source layout. Destination is restored by FinishedRendering(). |
| 380 | src_dxtex->TransitionToState(old_src_state); |
| 381 | } |
| 382 | |
| 383 | void DXTexture::ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect, |
| 384 | u32 layer, u32 level) |
| 385 | { |
| 386 | const DXTexture* src_dxtex = static_cast<const DXTexture*>(src); |
| 387 | |
| 388 | D3D12_RESOURCE_STATES old_src_state = src_dxtex->m_state; |
| 389 | src_dxtex->TransitionToState(D3D12_RESOURCE_STATE_RESOLVE_SOURCE); |
| 390 | TransitionToState(D3D12_RESOURCE_STATE_RESOLVE_DEST); |
| 391 | |
| 392 | g_dx_context->GetCommandList()->ResolveSubresource( |
| 393 | m_resource.Get(), CalcSubresource(level, layer), src_dxtex->m_resource.Get(), |
| 394 | src_dxtex->CalcSubresource(level, layer), |
| 395 | D3DCommon::GetDXGIFormatForAbstractFormat(m_config.format, false)); |
| 396 | |
| 397 | // Only restore the source layout. Destination is restored by FinishedRendering(). |
| 398 | src_dxtex->TransitionToState(old_src_state); |
| 399 | } |
| 400 | |
| 401 | void DXTexture::FinishedRendering() |
| 402 | { |
| 403 | if (m_state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE) |
| 404 | TransitionToState(D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); |
| 405 | } |
| 406 | |
| 407 | void DXTexture::TransitionToState(D3D12_RESOURCE_STATES state) const |
| 408 | { |
| 409 | if (m_state == state) |
| 410 | return; |
| 411 | |
| 412 | ResourceBarrier(g_dx_context->GetCommandList(), m_resource.Get(), m_state, state); |
| 413 | m_state = state; |
| 414 | } |
| 415 | |
| 416 | void DXTexture::DestroyResource() |
| 417 | { |
| 418 | if (m_uav_descriptor) |
| 419 | g_dx_context->GetDescriptorHeapManager().Free(m_uav_descriptor); |
| 420 | |
| 421 | if (m_srv_descriptor) |
| 422 | g_dx_context->GetDescriptorHeapManager().Free(m_srv_descriptor); |
| 423 | |
| 424 | m_resource.Reset(); |
| 425 | } |
| 426 | |
| 427 | DXFramebuffer::DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment, |
| 428 | std::vector<AbstractTexture*> additional_color_attachments, |
| 429 | AbstractTextureFormat color_format, AbstractTextureFormat depth_format, |
| 430 | u32 width, u32 height, u32 layers, u32 samples) |
| 431 | : AbstractFramebuffer(color_attachment, depth_attachment, |
| 432 | std::move(additional_color_attachments), color_format, depth_format, |
| 433 | width, height, layers, samples) |
| 434 | { |
| 435 | } |
| 436 | |
| 437 | DXFramebuffer::~DXFramebuffer() |
| 438 | { |
| 439 | if (m_depth_attachment) |
| 440 | g_dx_context->DeferDescriptorDestruction(g_dx_context->GetDSVHeapManager(), |
| 441 | m_dsv_descriptor.index); |
| 442 | if (m_color_attachment) |
| 443 | { |
| 444 | if (m_int_rtv_descriptor) |
| 445 | { |
| 446 | g_dx_context->DeferDescriptorDestruction(g_dx_context->GetRTVHeapManager(), |
| 447 | m_int_rtv_descriptor.index); |
| 448 | } |
| 449 | } |
| 450 | for (auto render_target : m_render_targets) |
| 451 | { |
| 452 | g_dx_context->DeferDescriptorDestruction(g_dx_context->GetRTVHeapManager(), |
| 453 | render_target.index); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | const D3D12_CPU_DESCRIPTOR_HANDLE* DXFramebuffer::GetIntRTVDescriptorArray() const |
| 458 | { |
| 459 | if (m_color_attachment == nullptr) |
| 460 | return nullptr; |
| 461 | |
| 462 | const auto& handle = m_int_rtv_descriptor.cpu_handle; |
| 463 | |
| 464 | // To save space in the descriptor heap, m_int_rtv_descriptor.cpu_handle.ptr is only allocated |
| 465 | // when the integer RTV format corresponding to the current abstract format differs from the |
| 466 | // non-integer RTV format. Only use the integer handle if it has been allocated. |
| 467 | if (handle.ptr != 0) |
| 468 | return &handle; |
| 469 | |
| 470 | // The integer and non-integer RTV formats are the same, so use the non-integer descriptor. |
| 471 | return GetRTVDescriptorArray(); |
| 472 | } |
| 473 | |
| 474 | void DXFramebuffer::Unbind() |
| 475 | { |
| 476 | static const D3D12_DISCARD_REGION dr = {0, nullptr, 0, 1}; |
| 477 | if (HasColorBuffer()) |
| 478 | { |
| 479 | g_dx_context->GetCommandList()->DiscardResource( |
| 480 | static_cast<DXTexture*>(GetColorAttachment())->GetResource(), &dr); |
| 481 | } |
| 482 | for (auto additional_color_attachment : m_additional_color_attachments) |
| 483 | { |
| 484 | g_dx_context->GetCommandList()->DiscardResource( |
| 485 | static_cast<DXTexture*>(additional_color_attachment)->GetResource(), &dr); |
| 486 | } |
| 487 | if (HasDepthBuffer()) |
| 488 | { |
| 489 | g_dx_context->GetCommandList()->DiscardResource( |
| 490 | static_cast<DXTexture*>(GetDepthAttachment())->GetResource(), &dr); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | void DXFramebuffer::ClearRenderTargets(const ClearColor& color_value, |
| 495 | const D3D12_RECT* rectangle) const |
| 496 | { |
| 497 | for (auto render_target : m_render_targets_raw) |
| 498 | { |
| 499 | g_dx_context->GetCommandList()->ClearRenderTargetView(render_target, color_value.data(), |
| 500 | rectangle ? 1 : 0, rectangle); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void DXFramebuffer::ClearDepth(float depth_value, const D3D12_RECT* rectangle) const |
| 505 | { |
| 506 | if (HasDepthBuffer()) |
| 507 | { |
| 508 | g_dx_context->GetCommandList()->ClearDepthStencilView(GetDSVDescriptor().cpu_handle, |
| 509 | D3D12_CLEAR_FLAG_DEPTH, depth_value, 0, |
| 510 | rectangle ? 1 : 0, rectangle); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | void DXFramebuffer::TransitionRenderTargets() const |
| 515 | { |
| 516 | if (HasColorBuffer()) |
| 517 | { |
| 518 | static_cast<DXTexture*>(GetColorAttachment()) |
| 519 | ->TransitionToState(D3D12_RESOURCE_STATE_RENDER_TARGET); |
| 520 | } |
| 521 | for (auto additional_color_attachment : m_additional_color_attachments) |
| 522 | { |
| 523 | static_cast<DXTexture*>(additional_color_attachment) |
| 524 | ->TransitionToState(D3D12_RESOURCE_STATE_RENDER_TARGET); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | std::unique_ptr<DXFramebuffer> |
| 529 | DXFramebuffer::Create(DXTexture* color_attachment, DXTexture* depth_attachment, |
| 530 | std::vector<AbstractTexture*> additional_color_attachments) |
| 531 | { |
| 532 | if (!ValidateConfig(color_attachment, depth_attachment, additional_color_attachments)) |
| 533 | return nullptr; |
| 534 | |
| 535 | const AbstractTextureFormat color_format = |
| 536 | color_attachment ? color_attachment->GetFormat() : AbstractTextureFormat::Undefined; |
| 537 | const AbstractTextureFormat depth_format = |
| 538 | depth_attachment ? depth_attachment->GetFormat() : AbstractTextureFormat::Undefined; |
| 539 | const DXTexture* either_attachment = color_attachment ? color_attachment : depth_attachment; |
| 540 | const u32 width = either_attachment->GetWidth(); |
| 541 | const u32 height = either_attachment->GetHeight(); |
| 542 | const u32 layers = either_attachment->GetLayers(); |
| 543 | const u32 samples = either_attachment->GetSamples(); |
| 544 | |
| 545 | std::unique_ptr<DXFramebuffer> fb( |
| 546 | new DXFramebuffer(color_attachment, depth_attachment, std::move(additional_color_attachments), |
| 547 | color_format, depth_format, width, height, layers, samples)); |
| 548 | if (!fb->CreateRTVDescriptors() || (color_attachment && !fb->CreateIRTVDescriptor()) || |
| 549 | (depth_attachment && !fb->CreateDSVDescriptor())) |
| 550 | { |
| 551 | return nullptr; |
| 552 | } |
| 553 | |
| 554 | return fb; |
| 555 | } |
| 556 | |
| 557 | bool DXFramebuffer::CreateRTVDescriptors() |
| 558 | { |
| 559 | if (m_color_attachment) |
| 560 | { |
| 561 | if (!CreateRTVDescriptor(m_layers, m_color_attachment)) |
| 562 | { |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | for (auto* attachment : m_additional_color_attachments) |
| 568 | { |
| 569 | if (!CreateRTVDescriptor(1, attachment)) |
| 570 | { |
| 571 | return false; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | bool DXFramebuffer::CreateRTVDescriptor(u32 layers, AbstractTexture* attachment) |
| 579 | { |
| 580 | DescriptorHandle rtv; |
| 581 | if (!g_dx_context->GetRTVHeapManager().Allocate(&rtv)) |
| 582 | { |
| 583 | PanicAlertFmt("Failed to allocate RTV descriptor"); |
| 584 | return false; |
| 585 | } |
| 586 | m_render_targets.push_back(std::move(rtv)); |
| 587 | m_render_targets_raw.push_back(m_render_targets.back().cpu_handle); |
| 588 | |
| 589 | const bool multisampled = m_samples > 1; |
| 590 | D3D12_RENDER_TARGET_VIEW_DESC rtv_desc = { |
| 591 | D3DCommon::GetRTVFormatForAbstractFormat(m_color_format, false), |
| 592 | multisampled ? D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY : D3D12_RTV_DIMENSION_TEXTURE2DARRAY}; |
| 593 | if (multisampled) |
| 594 | rtv_desc.Texture2DMSArray.ArraySize = layers; |
| 595 | else |
| 596 | rtv_desc.Texture2DArray.ArraySize = layers; |
| 597 | g_dx_context->GetDevice()->CreateRenderTargetView( |
| 598 | static_cast<DXTexture*>(attachment)->GetResource(), &rtv_desc, m_render_targets_raw.back()); |
| 599 | |
| 600 | return true; |
| 601 | } |
| 602 | |
| 603 | bool DXFramebuffer::CreateIRTVDescriptor() |
| 604 | { |
| 605 | const bool multisampled = m_samples > 1; |
| 606 | DXGI_FORMAT non_int_format = D3DCommon::GetRTVFormatForAbstractFormat(m_color_format, false); |
| 607 | DXGI_FORMAT int_format = D3DCommon::GetRTVFormatForAbstractFormat(m_color_format, true); |
| 608 | |
| 609 | // If the integer and non-integer RTV formats are the same for a given abstract format we can save |
| 610 | // space in the descriptor heap by only allocating the non-integer descriptor and using it for |
| 611 | // the integer RTV too. |
| 612 | if (int_format != non_int_format) |
| 613 | { |
| 614 | if (!g_dx_context->GetRTVHeapManager().Allocate(&m_int_rtv_descriptor)) |
| 615 | return false; |
| 616 | |
| 617 | D3D12_RENDER_TARGET_VIEW_DESC rtv_desc = {int_format, multisampled ? |
| 618 | D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY : |
| 619 | D3D12_RTV_DIMENSION_TEXTURE2DARRAY}; |
| 620 | if (multisampled) |
| 621 | rtv_desc.Texture2DMSArray.ArraySize = m_layers; |
| 622 | else |
| 623 | rtv_desc.Texture2DArray.ArraySize = m_layers; |
| 624 | g_dx_context->GetDevice()->CreateRenderTargetView( |
| 625 | static_cast<DXTexture*>(m_color_attachment)->GetResource(), &rtv_desc, |
| 626 | m_int_rtv_descriptor.cpu_handle); |
| 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | bool DXFramebuffer::CreateDSVDescriptor() |
| 632 | { |
| 633 | if (!g_dx_context->GetDSVHeapManager().Allocate(&m_dsv_descriptor)) |
| 634 | { |
| 635 | PanicAlertFmt("Failed to allocate RTV descriptor"); |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | const bool multisampled = m_samples > 1; |
| 640 | D3D12_DEPTH_STENCIL_VIEW_DESC dsv_desc = { |
| 641 | D3DCommon::GetDSVFormatForAbstractFormat(m_depth_format), |
| 642 | multisampled ? D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY : D3D12_DSV_DIMENSION_TEXTURE2DARRAY, |
| 643 | D3D12_DSV_FLAG_NONE}; |
| 644 | if (multisampled) |
| 645 | dsv_desc.Texture2DMSArray.ArraySize = m_layers; |
| 646 | else |
| 647 | dsv_desc.Texture2DArray.ArraySize = m_layers; |
| 648 | g_dx_context->GetDevice()->CreateDepthStencilView( |
| 649 | static_cast<DXTexture*>(m_depth_attachment)->GetResource(), &dsv_desc, |
| 650 | m_dsv_descriptor.cpu_handle); |
| 651 | return true; |
| 652 | } |
| 653 | |
| 654 | DXStagingTexture::DXStagingTexture(StagingTextureType type, const TextureConfig& config, |
| 655 | ID3D12Resource* resource, u32 stride, u32 buffer_size) |
| 656 | : AbstractStagingTexture(type, config), m_resource(resource), m_buffer_size(buffer_size) |
| 657 | { |
| 658 | m_map_stride = stride; |
| 659 | } |
| 660 | |
| 661 | DXStagingTexture::~DXStagingTexture() |
| 662 | { |
| 663 | g_dx_context->DeferResourceDestruction(m_resource.Get()); |
| 664 | } |
| 665 | |
| 666 | void DXStagingTexture::CopyFromTexture(const AbstractTexture* src, |
| 667 | const MathUtil::Rectangle<int>& src_rect, u32 src_layer, |
| 668 | u32 src_level, const MathUtil::Rectangle<int>& dst_rect) |
| 669 | { |
| 670 | const DXTexture* src_tex = static_cast<const DXTexture*>(src); |
| 671 | ASSERT(m_type == StagingTextureType::Readback || m_type == StagingTextureType::Mutable); |
| 672 | ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() && |
| 673 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 674 | ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= src_tex->GetWidth() && |
| 675 | src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= src_tex->GetHeight()); |
| 676 | ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= m_config.width && |
| 677 | dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= m_config.height); |
| 678 | |
| 679 | const D3D12_RESOURCE_STATES old_state = src_tex->GetState(); |
| 680 | src_tex->TransitionToState(D3D12_RESOURCE_STATE_COPY_SOURCE); |
| 681 | |
| 682 | // Can't copy while it's mapped like in Vulkan. |
| 683 | Unmap(); |
| 684 | |
| 685 | // Copy from VRAM -> host-visible memory. |
| 686 | const D3D12_TEXTURE_COPY_LOCATION dst_loc = { |
| 687 | m_resource.Get(), |
| 688 | D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, |
| 689 | {0, |
| 690 | {D3DCommon::GetDXGIFormatForAbstractFormat(m_config.format, false), m_config.width, |
| 691 | m_config.height, 1u, static_cast<UINT>(m_map_stride)}}}; |
| 692 | const D3D12_TEXTURE_COPY_LOCATION src_loc = { |
| 693 | src_tex->GetResource(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 694 | static_cast<UINT>(src_tex->CalcSubresource(src_level, src_layer))}; |
| 695 | const D3D12_BOX src_box = RectangleToBox(src_rect); |
| 696 | g_dx_context->GetCommandList()->CopyTextureRegion(&dst_loc, dst_rect.left, dst_rect.top, 0, |
| 697 | &src_loc, &src_box); |
| 698 | |
| 699 | // Restore old source texture layout. |
| 700 | src_tex->TransitionToState(old_state); |
| 701 | |
| 702 | // Data is ready when the current command list is complete. |
| 703 | m_needs_flush = true; |
| 704 | m_completed_fence = g_dx_context->GetCurrentFenceValue(); |
| 705 | } |
| 706 | |
| 707 | void DXStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst, |
| 708 | const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer, |
| 709 | u32 dst_level) |
| 710 | { |
| 711 | const DXTexture* dst_tex = static_cast<const DXTexture*>(dst); |
| 712 | ASSERT(m_type == StagingTextureType::Upload || m_type == StagingTextureType::Mutable); |
| 713 | ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() && |
| 714 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 715 | ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= m_config.width && |
| 716 | src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= m_config.height); |
| 717 | ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= dst_tex->GetWidth() && |
| 718 | dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= dst_tex->GetHeight()); |
| 719 | |
| 720 | const D3D12_RESOURCE_STATES old_state = dst_tex->GetState(); |
| 721 | dst_tex->TransitionToState(D3D12_RESOURCE_STATE_COPY_DEST); |
| 722 | |
| 723 | // Can't copy while it's mapped like in Vulkan. |
| 724 | Unmap(); |
| 725 | |
| 726 | // Copy from VRAM -> host-visible memory. |
| 727 | const D3D12_TEXTURE_COPY_LOCATION dst_loc = { |
| 728 | dst_tex->GetResource(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, |
| 729 | static_cast<UINT>(dst_tex->CalcSubresource(dst_level, dst_layer))}; |
| 730 | const D3D12_TEXTURE_COPY_LOCATION src_loc = { |
| 731 | m_resource.Get(), |
| 732 | D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, |
| 733 | {0, |
| 734 | {D3DCommon::GetDXGIFormatForAbstractFormat(m_config.format, false), m_config.width, |
| 735 | m_config.height, 1u, static_cast<UINT>(m_map_stride)}}}; |
| 736 | const D3D12_BOX src_box = RectangleToBox(src_rect); |
| 737 | g_dx_context->GetCommandList()->CopyTextureRegion(&dst_loc, dst_rect.left, dst_rect.top, 0, |
| 738 | &src_loc, &src_box); |
| 739 | |
| 740 | // Restore old source texture layout. |
| 741 | dst_tex->TransitionToState(old_state); |
| 742 | |
| 743 | // Data is ready when the current command list is complete. |
| 744 | m_needs_flush = true; |
| 745 | m_completed_fence = g_dx_context->GetCurrentFenceValue(); |
| 746 | } |
| 747 | |
| 748 | bool DXStagingTexture::Map() |
| 749 | { |
| 750 | if (m_map_pointer) |
| 751 | return true; |
| 752 | |
| 753 | const D3D12_RANGE read_range = {0u, m_type == StagingTextureType::Upload ? 0u : m_buffer_size}; |
| 754 | HRESULT hr = m_resource->Map(0, &read_range, reinterpret_cast<void**>(&m_map_pointer)); |
| 755 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Map resource failed: {}", DX12HRWrap(hr)); |
| 756 | if (FAILED(hr)) |
| 757 | return false; |
| 758 | |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | void DXStagingTexture::Unmap() |
| 763 | { |
| 764 | if (!m_map_pointer) |
| 765 | return; |
| 766 | |
| 767 | const D3D12_RANGE write_range = {0u, m_type != StagingTextureType::Upload ? 0 : m_buffer_size}; |
| 768 | m_resource->Unmap(0, &write_range); |
| 769 | m_map_pointer = nullptr; |
| 770 | } |
| 771 | |
| 772 | void DXStagingTexture::Flush() |
| 773 | { |
| 774 | if (!m_needs_flush) |
| 775 | return; |
| 776 | |
| 777 | m_needs_flush = false; |
| 778 | |
| 779 | // If the completed fence is the same as the current command buffer fence, we need to execute |
| 780 | // the current list and wait for it to complete. This is the slowest path. Otherwise, if the |
| 781 | // command list with the copy has been submitted, we only need to wait for the fence. |
| 782 | if (m_completed_fence == g_dx_context->GetCurrentFenceValue()) |
| 783 | Gfx::GetInstance()->ExecuteCommandList(true); |
| 784 | else |
| 785 | g_dx_context->WaitForFence(m_completed_fence); |
| 786 | } |
| 787 | |
| 788 | std::unique_ptr<DXStagingTexture> DXStagingTexture::Create(StagingTextureType type, |
| 789 | const TextureConfig& config) |
| 790 | { |
| 791 | ASSERT(config.levels == 1 && config.layers == 1 && config.samples == 1); |
| 792 | |
| 793 | // Readback and mutable share the same heap type. |
| 794 | const bool is_upload = type == StagingTextureType::Upload; |
| 795 | const D3D12_HEAP_PROPERTIES heap_properties = {is_upload ? D3D12_HEAP_TYPE_UPLOAD : |
| 796 | D3D12_HEAP_TYPE_READBACK}; |
| 797 | |
| 798 | const u32 texel_size = AbstractTexture::GetTexelSizeForFormat(config.format); |
| 799 | const u32 stride = Common::AlignUp(config.width * texel_size, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); |
| 800 | const u32 size = stride * config.height; |
| 801 | |
| 802 | const D3D12_RESOURCE_DESC desc = {D3D12_RESOURCE_DIMENSION_BUFFER, |
| 803 | 0, |
| 804 | size, |
| 805 | 1, |
| 806 | 1, |
| 807 | 1, |
| 808 | DXGI_FORMAT_UNKNOWN, |
| 809 | {1, 0}, |
| 810 | D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 811 | D3D12_RESOURCE_FLAG_NONE}; |
| 812 | |
| 813 | // Readback textures are stuck in COPY_DEST and are never GPU readable. |
| 814 | // Upload textures are stuck in GENERIC_READ, and are never CPU readable. |
| 815 | ComPtr<ID3D12Resource> resource; |
| 816 | HRESULT hr = g_dx_context->GetDevice()->CreateCommittedResource( |
| 817 | &heap_properties, D3D12_HEAP_FLAG_NONE, &desc, |
| 818 | is_upload ? D3D12_RESOURCE_STATE_GENERIC_READ : D3D12_RESOURCE_STATE_COPY_DEST, nullptr, |
| 819 | IID_PPV_ARGS(&resource)); |
| 820 | ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create staging texture resource: {}", DX12HRWrap(hr)); |
| 821 | if (FAILED(hr)) |
| 822 | return nullptr; |
| 823 | |
| 824 | return std::unique_ptr<DXStagingTexture>( |
| 825 | new DXStagingTexture(type, config, resource.Get(), stride, size)); |
| 826 | } |
| 827 | |
| 828 | } // namespace DX12 |